Files
buuh/packages/bankai/test/document.test.js
T
Bendik Aagaard LynghaugandClaude Fable 5 7e08b585ac feat(bankai): bankai v10 — the isomorphic compiler & server on Vite 8
One command, zero config, and only the client ever bundles: v8 server
code is plain ESM that Node runs as-authored, so there is no server
build to rot (the lesson of v9).

- start: Vite middleware mode + HMR with per-request streaming SSR via
  ssrLoadModule; a virtual client entry generates the browser glue so
  the user writes exactly one isomorphic module (plan decision D5).
- build: client bundle via Vite 8/Rolldown, manifest-derived route
  assets in dist/bankai.json, service worker built with the precache
  list injected (choo-service-worker convention, manifest edition),
  brotli+gzip precompression of every text asset.
- serve: immutable caching + precompressed negotiation for hashed
  assets, 103 Early Hints (res.writeEarlyHints) with the route's assets
  before every page, streaming SSR, and a window.initialState tail with
  script-breakout-safe serialization and choo internals filtered out.
- inspect: raw/gzip/brotli size report.

Integration tests drive the real counter example through build and
serve, asserting the 103 interim response at the HTTP level.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014NgfSjHE11oFpoSnLVKLXd
2026-09-08 19:32:37 +02:00

56 lines
2.0 KiB
JavaScript

import { test } from 'node:test'
import assert from 'node:assert'
import { documentHead, documentTail, serializeState, assetLinks } from '../lib/document.js'
test('documentHead renders title, css, preloads and module scripts', () => {
const head = documentHead({
title: 'a < b',
css: ['/assets/x.css'],
modulepreload: ['/assets/dep.js'],
scripts: ['/assets/main.js']
})
assert.match(head, /<title>a &lt; b<\/title>/)
assert.match(head, /<link rel="stylesheet" href="\/assets\/x.css">/)
assert.match(head, /<link rel="modulepreload" href="\/assets\/dep.js">/)
assert.match(head, /<script type="module" src="\/assets\/main.js"><\/script>/)
assert.match(head, /^<!doctype html>/)
})
test('serializeState blocks script breakouts and drops recomputed internals', () => {
const json = serializeState({
events: { RENDER: 'render' },
cache: null,
prefetch: [],
count: 3,
evil: '</script><script>alert(1)</script>',
nested: { events: 'keep me' }
})
assert.ok(!json.includes('</script'), 'no closing-script breakout')
const parsed = JSON.parse(json.replace(/\\u003c/g, '<'))
assert.strictEqual(parsed.count, 3)
assert.strictEqual(parsed.events, undefined, 'top-level events dropped')
assert.strictEqual(parsed.cache, undefined)
assert.strictEqual(parsed.prefetch, undefined)
assert.strictEqual(parsed.nested.events, 'keep me', 'only top-level internals dropped')
})
test('documentTail is an inline script plus the html close', () => {
const tail = documentTail({ count: 1 })
assert.match(tail, /<script>window.initialState=\{"count":1\}<\/script>/)
assert.match(tail, /<\/html>/)
})
test('assetLinks builds Early Hints link values', () => {
const links = assetLinks({
scripts: ['/assets/main.js'],
modulepreload: ['/assets/dep.js'],
css: ['/assets/x.css']
})
assert.deepStrictEqual(links, [
'</assets/main.js>; rel=modulepreload',
'</assets/dep.js>; rel=modulepreload',
'</assets/x.css>; rel=preload; as=style'
])
})