toiljs 0.0.59 → 0.0.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +31 -0
- package/CHANGELOG.md +15 -0
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +311 -118
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/index.d.ts +1 -1
- package/build/client/index.js +1 -1
- package/build/client/routing/mount.js +12 -1
- package/build/client/ssr/markers.d.ts +1 -0
- package/build/client/ssr/markers.js +3 -0
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/config.d.ts +21 -0
- package/build/compiler/config.js +35 -0
- package/build/compiler/docs.d.ts +2 -1
- package/build/compiler/docs.js +33 -304
- package/build/compiler/index.d.ts +13 -0
- package/build/compiler/index.js +113 -21
- package/build/compiler/template-build.d.ts +21 -1
- package/build/compiler/template-build.js +110 -26
- package/build/compiler/toil-docs.generated.d.ts +1 -0
- package/build/compiler/toil-docs.generated.js +20 -0
- package/build/devserver/.tsbuildinfo +1 -1
- package/build/devserver/daemon/catalog.d.ts +26 -0
- package/build/devserver/daemon/catalog.js +48 -0
- package/build/devserver/daemon/cron.d.ts +4 -0
- package/build/devserver/daemon/cron.js +50 -0
- package/build/devserver/daemon/host.d.ts +37 -0
- package/build/devserver/daemon/host.js +94 -0
- package/build/devserver/daemon/index.d.ts +34 -0
- package/build/devserver/daemon/index.js +241 -0
- package/build/devserver/db/catalog.d.ts +2 -0
- package/build/devserver/db/catalog.js +80 -0
- package/build/devserver/db/database.d.ts +80 -0
- package/build/devserver/db/database.js +1032 -0
- package/build/devserver/db/index.d.ts +3 -0
- package/build/devserver/db/index.js +3 -0
- package/build/devserver/db/routeKinds.d.ts +8 -0
- package/build/devserver/db/routeKinds.js +139 -0
- package/build/devserver/db/types.d.ts +121 -0
- package/build/devserver/db/types.js +52 -0
- package/build/devserver/email/index.js +1 -1
- package/build/devserver/index.d.ts +19 -24
- package/build/devserver/index.js +11 -165
- package/build/devserver/mstore/store.d.ts +18 -0
- package/build/devserver/mstore/store.js +82 -0
- package/build/devserver/{host.d.ts → runtime/host.d.ts} +7 -1
- package/build/devserver/{host.js → runtime/host.js} +51 -7
- package/build/devserver/{module.d.ts → runtime/module.d.ts} +2 -1
- package/build/devserver/{module.js → runtime/module.js} +34 -1
- package/build/devserver/server.d.ts +23 -0
- package/build/devserver/server.js +223 -0
- package/build/devserver/ssr.d.ts +25 -0
- package/build/devserver/ssr.js +114 -0
- package/build/devserver/wasm/sections.d.ts +2 -0
- package/build/devserver/wasm/sections.js +42 -0
- package/build/devserver/wasm/surface.d.ts +18 -0
- package/build/devserver/wasm/surface.js +41 -0
- package/docs/README.md +4 -4
- package/docs/auth-todo.md +6 -6
- package/docs/caching.md +5 -5
- package/docs/cli.md +15 -0
- package/docs/client.md +40 -0
- package/docs/crypto.md +4 -4
- package/docs/data.md +6 -6
- package/docs/email.md +28 -28
- package/docs/environment.md +10 -10
- package/docs/index.md +26 -0
- package/docs/ratelimit.md +10 -10
- package/docs/routing.md +2 -2
- package/docs/server.md +61 -0
- package/docs/ssr.md +561 -113
- package/docs/styling.md +22 -0
- package/docs/time.md +3 -3
- package/eslint.config.js +10 -1
- package/examples/basic/client/components/Header.tsx +3 -0
- package/examples/basic/client/routes/features/actions.tsx +0 -2
- package/examples/basic/client/routes/hello.tsx +89 -19
- package/examples/basic/client/styles/main.css +48 -0
- package/examples/basic/server/SsrHelloRender.ts +97 -0
- package/examples/basic/server/main.ts +5 -0
- package/examples/basic/server/migrations/GuestEntry.migration.ts +39 -0
- package/examples/basic/server/streams/Echo.ts +49 -0
- package/package.json +12 -10
- package/scripts/gen-toil-docs.mjs +96 -0
- package/server/runtime/time.ts +3 -3
- package/src/cli/create.ts +40 -3
- package/src/cli/db.ts +158 -0
- package/src/cli/diagnostics.ts +19 -0
- package/src/cli/doctor.ts +20 -0
- package/src/cli/index.ts +10 -0
- package/src/cli/update.ts +58 -0
- package/src/client/index.ts +1 -1
- package/src/client/routing/mount.tsx +18 -2
- package/src/client/ssr/markers.tsx +22 -0
- package/src/compiler/config.ts +88 -2
- package/src/compiler/docs.ts +47 -308
- package/src/compiler/index.ts +236 -32
- package/src/compiler/ssr-codegen.ts +1 -1
- package/src/compiler/template-build.ts +247 -46
- package/src/compiler/toil-docs.generated.ts +26 -0
- package/src/devserver/daemon/catalog.ts +120 -0
- package/src/devserver/daemon/cron.ts +87 -0
- package/src/devserver/daemon/host.ts +224 -0
- package/src/devserver/daemon/index.ts +349 -0
- package/src/devserver/db/catalog.ts +108 -0
- package/src/devserver/db/database.ts +1633 -0
- package/src/devserver/db/index.ts +18 -0
- package/src/devserver/db/routeKinds.ts +147 -0
- package/src/devserver/db/types.ts +139 -0
- package/src/devserver/email/index.ts +1 -1
- package/src/devserver/index.ts +31 -287
- package/src/devserver/mstore/store.ts +121 -0
- package/src/devserver/{host.ts → runtime/host.ts} +98 -7
- package/src/devserver/{module.ts → runtime/module.ts} +47 -1
- package/src/devserver/server.ts +393 -0
- package/src/devserver/ssr.ts +166 -0
- package/src/devserver/wasm/sections.ts +59 -0
- package/src/devserver/wasm/surface.ts +88 -0
- package/test/daemon-build.test.ts +198 -0
- package/test/daemon-catalog.test.ts +265 -0
- package/test/daemon-emulation.test.ts +216 -0
- package/test/db.test.ts +0 -0
- package/test/devserver-database.test.ts +510 -14
- package/test/devserver-pqauth.test.ts +1 -1
- package/test/devserver-secrets.test.ts +5 -1
- package/test/doctor.test.ts +13 -0
- package/test/email-preview.test.ts +6 -1
- package/test/example-guestbook.test.ts +43 -1
- package/test/fixtures/daemon-app.ts +56 -0
- package/test/global-setup.ts +17 -0
- package/test/pqauth-e2e.test.ts +1 -1
- package/test/ssr-render.test.ts +94 -27
- package/test/ssr-template.test.tsx +44 -1
- package/vitest.config.ts +3 -0
- package/build/devserver/database.d.ts +0 -8
- package/build/devserver/database.js +0 -418
- package/src/devserver/database.ts +0 -618
- /package/build/devserver/{dotenv.d.ts → config/dotenv.d.ts} +0 -0
- /package/build/devserver/{dotenv.js → config/dotenv.js} +0 -0
- /package/build/devserver/{env.d.ts → config/env.d.ts} +0 -0
- /package/build/devserver/{env.js → config/env.js} +0 -0
- /package/build/devserver/{ratelimit.d.ts → config/ratelimit.d.ts} +0 -0
- /package/build/devserver/{ratelimit.js → config/ratelimit.js} +0 -0
- /package/build/devserver/{cache.d.ts → http/cache.d.ts} +0 -0
- /package/build/devserver/{cache.js → http/cache.js} +0 -0
- /package/build/devserver/{envelope.d.ts → http/envelope.d.ts} +0 -0
- /package/build/devserver/{envelope.js → http/envelope.js} +0 -0
- /package/build/devserver/{proxy.d.ts → http/proxy.d.ts} +0 -0
- /package/build/devserver/{proxy.js → http/proxy.js} +0 -0
- /package/build/devserver/{crypto.d.ts → runtime/crypto.d.ts} +0 -0
- /package/build/devserver/{crypto.js → runtime/crypto.js} +0 -0
- /package/src/devserver/{dotenv.ts → config/dotenv.ts} +0 -0
- /package/src/devserver/{env.ts → config/env.ts} +0 -0
- /package/src/devserver/{ratelimit.ts → config/ratelimit.ts} +0 -0
- /package/src/devserver/{cache.ts → http/cache.ts} +0 -0
- /package/src/devserver/{envelope.ts → http/envelope.ts} +0 -0
- /package/src/devserver/{proxy.ts → http/proxy.ts} +0 -0
- /package/src/devserver/{crypto.ts → runtime/crypto.ts} +0 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse a compiled artifact's `toil.surface` custom section. Emitted into EVERY
|
|
3
|
+
* Toil artifact by the toilscript `buildToilSurface` pass (hot AND cold). The dev
|
|
4
|
+
* server reads it to decide whether each artifact carries the daemon surface
|
|
5
|
+
* (start the daemon emulator) or the stream surface (Phase 4, deferred).
|
|
6
|
+
*
|
|
7
|
+
* Byte layout (RECONCILIATION Part 5, all little-endian; mirrors the toilscript
|
|
8
|
+
* `CatWriter` emitter byte-for-byte):
|
|
9
|
+
*
|
|
10
|
+
* u16 format_version = 1
|
|
11
|
+
* u8 target_mode (0 = hot, 1 = cold; there is no target_mode = 2)
|
|
12
|
+
* u8 reserved0
|
|
13
|
+
* u32 surface_flags (bit0 rest, bit1 stream, bit2 daemon,
|
|
14
|
+
* bit3 scheduled, bit4 database, bit5 render)
|
|
15
|
+
* u16 abi_version
|
|
16
|
+
* str build_id (u32 len + UTF-8)
|
|
17
|
+
* u32 fingerprint
|
|
18
|
+
* u32 data_coherence_hash
|
|
19
|
+
* u32 pair_coherence_hash (exactly THREE u32 after build_id, not four)
|
|
20
|
+
*
|
|
21
|
+
* Fail-closed per Part 5's host rule: an ABSENT section is "legacy single
|
|
22
|
+
* artifact, load as hot" (NOT a hard reject); a PRESENT-but-unparseable section is
|
|
23
|
+
* a corrupt artifact -> do not start that artifact's emulator.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { DataReader } from 'toiljs/io';
|
|
27
|
+
|
|
28
|
+
import { customSection } from './sections.js';
|
|
29
|
+
|
|
30
|
+
export interface SurfaceFlags {
|
|
31
|
+
readonly rest: boolean;
|
|
32
|
+
readonly stream: boolean;
|
|
33
|
+
readonly daemon: boolean;
|
|
34
|
+
readonly scheduled: boolean;
|
|
35
|
+
readonly database: boolean;
|
|
36
|
+
readonly render: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface Surface {
|
|
40
|
+
/** 0 = hot, 1 = cold. */
|
|
41
|
+
readonly targetMode: 'hot' | 'cold';
|
|
42
|
+
readonly flags: SurfaceFlags;
|
|
43
|
+
readonly abiVersion: number;
|
|
44
|
+
readonly buildId: string;
|
|
45
|
+
readonly fingerprint: number;
|
|
46
|
+
readonly dataCoherenceHash: number;
|
|
47
|
+
readonly pairCoherenceHash: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** `'absent'` => legacy single artifact (load as hot, no emulators).
|
|
51
|
+
* `'invalid'` => present but corrupt (fail closed). Otherwise the parsed surface. */
|
|
52
|
+
export function parseSurface(wasm: Buffer): Surface | 'absent' | 'invalid' {
|
|
53
|
+
let sec: Buffer | null;
|
|
54
|
+
try {
|
|
55
|
+
sec = customSection(wasm, 'toil.surface');
|
|
56
|
+
} catch {
|
|
57
|
+
return 'invalid'; // garbage section table
|
|
58
|
+
}
|
|
59
|
+
if (sec === null) return 'absent';
|
|
60
|
+
|
|
61
|
+
const r = new DataReader(sec);
|
|
62
|
+
r.readU16(); // format_version
|
|
63
|
+
const targetMode = r.readU8() === 1 ? 'cold' : 'hot';
|
|
64
|
+
r.readU8(); // reserved0
|
|
65
|
+
const f = r.readU32(); // surface_flags
|
|
66
|
+
const abiVersion = r.readU16();
|
|
67
|
+
const buildId = r.readString();
|
|
68
|
+
const fingerprint = r.readU32();
|
|
69
|
+
const dataCoherenceHash = r.readU32(); // exactly THREE u32 after build_id
|
|
70
|
+
const pairCoherenceHash = r.readU32();
|
|
71
|
+
if (!r.ok) return 'invalid'; // PRESENT but corrupt => fail closed
|
|
72
|
+
return {
|
|
73
|
+
targetMode,
|
|
74
|
+
flags: {
|
|
75
|
+
rest: !!(f & 1),
|
|
76
|
+
stream: !!(f & 2),
|
|
77
|
+
daemon: !!(f & 4),
|
|
78
|
+
scheduled: !!(f & 8),
|
|
79
|
+
database: !!(f & 16),
|
|
80
|
+
render: !!(f & 32),
|
|
81
|
+
},
|
|
82
|
+
abiVersion,
|
|
83
|
+
buildId,
|
|
84
|
+
fingerprint,
|
|
85
|
+
dataCoherenceHash,
|
|
86
|
+
pairCoherenceHash,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two-pass build pipeline (doc 08 section 1). Asserts:
|
|
3
|
+
*
|
|
4
|
+
* - `serverArtifacts` derives the `-hot`/`-cold` paths from `outFile` when
|
|
5
|
+
* `hotFile`/`coldFile` are absent, and honors them when present.
|
|
6
|
+
* - `SURFACE_DECORATOR` matches `@stream`/`@daemon`/`@scheduled` at line start
|
|
7
|
+
* (not in a comment).
|
|
8
|
+
* - `buildServer` on a project that declares a `@daemon` runs TWO toilscript
|
|
9
|
+
* passes (one `--targetMode cold`, one `--targetMode hot`) and produces BOTH
|
|
10
|
+
* `release-hot.wasm` and `release-cold.wasm`; the cold artifact decodes to a
|
|
11
|
+
* daemon catalog and its `toil.surface` is target_mode = cold.
|
|
12
|
+
* - a project with only the legacy request surface keeps the single-artifact
|
|
13
|
+
* path (no cold pass, no cold artifact).
|
|
14
|
+
*
|
|
15
|
+
* The build invokes the LOCAL toilscript (branch feat/streams-phase0-compiler),
|
|
16
|
+
* which supports `--targetMode`; the test links it into the fixture project's
|
|
17
|
+
* `node_modules` the same way the dev build resolves it (`require.resolve`).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
|
|
21
|
+
import { tmpdir } from 'node:os';
|
|
22
|
+
import { dirname, join } from 'node:path';
|
|
23
|
+
import { fileURLToPath } from 'node:url';
|
|
24
|
+
|
|
25
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
26
|
+
|
|
27
|
+
import {
|
|
28
|
+
buildServer,
|
|
29
|
+
serverArtifacts,
|
|
30
|
+
splitSurfaceFiles,
|
|
31
|
+
SURFACE_DECORATOR,
|
|
32
|
+
} from '../src/compiler/index.js';
|
|
33
|
+
import { parseDaemonCatalog } from '../src/devserver/daemon/catalog.js';
|
|
34
|
+
import { parseSurface } from '../src/devserver/wasm/surface.js';
|
|
35
|
+
|
|
36
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
37
|
+
const LOCAL_TOILSCRIPT = join(here, '..', '..', 'toilscript');
|
|
38
|
+
|
|
39
|
+
let tmp: string;
|
|
40
|
+
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
tmp = mkdtempSync(join(tmpdir(), 'daemon-build-'));
|
|
43
|
+
});
|
|
44
|
+
afterEach(() => {
|
|
45
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
/** Scaffold a minimal project at `tmp` with `server/main.ts` = `serverSrc`, the
|
|
49
|
+
* given toilconfig, and `node_modules/toilscript` symlinked to the local build. */
|
|
50
|
+
function scaffold(serverSrc: string, toilconfig: object): void {
|
|
51
|
+
writeFileSync(join(tmp, 'package.json'), JSON.stringify({ name: 'fixture', type: 'module' }));
|
|
52
|
+
writeFileSync(join(tmp, 'toilconfig.json'), JSON.stringify(toilconfig, null, 2));
|
|
53
|
+
mkdirSync(join(tmp, 'server'), { recursive: true });
|
|
54
|
+
writeFileSync(join(tmp, 'server', 'main.ts'), serverSrc);
|
|
55
|
+
mkdirSync(join(tmp, 'node_modules'), { recursive: true });
|
|
56
|
+
symlinkSync(LOCAL_TOILSCRIPT, join(tmp, 'node_modules', 'toilscript'), 'dir');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const BASE_TOILCONFIG = {
|
|
60
|
+
entries: ['server/main.ts'],
|
|
61
|
+
targets: { release: { outFile: 'build/server/release.wasm' } },
|
|
62
|
+
options: { runtime: 'stub', optimizeLevel: 0, shrinkLevel: 0 },
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// A @daemon that declares its host imports directly (no toiljs globals lib needed).
|
|
66
|
+
const DAEMON_SRC = `@daemon
|
|
67
|
+
class Jobs {
|
|
68
|
+
@scheduled("2s") fast(): void {}
|
|
69
|
+
@scheduled("0 0 * * *") nightly(): void {}
|
|
70
|
+
}
|
|
71
|
+
export function probe(): i32 { return 1; }
|
|
72
|
+
`;
|
|
73
|
+
|
|
74
|
+
const LEGACY_SRC = `export function handle(ofs: i32, len: i32): i64 { return 0; }
|
|
75
|
+
export function probe(): i32 { return 1; }
|
|
76
|
+
`;
|
|
77
|
+
|
|
78
|
+
describe('serverArtifacts path derivation', () => {
|
|
79
|
+
it('derives -hot/-cold from outFile when hotFile/coldFile are absent', () => {
|
|
80
|
+
writeFileSync(
|
|
81
|
+
join(tmp, 'toilconfig.json'),
|
|
82
|
+
JSON.stringify({ targets: { release: { outFile: 'build/server/release.wasm' } } }),
|
|
83
|
+
);
|
|
84
|
+
const a = serverArtifacts(tmp);
|
|
85
|
+
expect(a.hot).toBe(join(tmp, 'build/server/release-hot.wasm'));
|
|
86
|
+
expect(a.cold).toBe(join(tmp, 'build/server/release-cold.wasm'));
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('honors explicit hotFile/coldFile when present', () => {
|
|
90
|
+
writeFileSync(
|
|
91
|
+
join(tmp, 'toilconfig.json'),
|
|
92
|
+
JSON.stringify({
|
|
93
|
+
targets: {
|
|
94
|
+
release: {
|
|
95
|
+
outFile: 'build/server/release.wasm',
|
|
96
|
+
hotFile: 'out/hot.wasm',
|
|
97
|
+
coldFile: 'out/cold.wasm',
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
}),
|
|
101
|
+
);
|
|
102
|
+
const a = serverArtifacts(tmp);
|
|
103
|
+
expect(a.hot).toBe(join(tmp, 'out/hot.wasm'));
|
|
104
|
+
expect(a.cold).toBe(join(tmp, 'out/cold.wasm'));
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('SURFACE_DECORATOR', () => {
|
|
109
|
+
it('matches the streams/daemon decorators at line start', () => {
|
|
110
|
+
for (const deco of ['@stream', '@daemon', '@scheduled', '@rest', '@data']) {
|
|
111
|
+
expect(SURFACE_DECORATOR.test(`${deco} class X {}`)).toBe(true);
|
|
112
|
+
expect(SURFACE_DECORATOR.test(` ${deco}\nclass X {}`)).toBe(true);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('does NOT match a decorator mentioned only in a comment', () => {
|
|
117
|
+
expect(SURFACE_DECORATOR.test('// the @daemon decorator marks a cold class')).toBe(false);
|
|
118
|
+
expect(SURFACE_DECORATOR.test('const s = "uses @scheduled internally";')).toBe(false);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('splitSurfaceFiles per-pass classification', () => {
|
|
123
|
+
/** Lay down `name -> contents` files under `tmp` and return their relative paths. */
|
|
124
|
+
function lay(files: Record<string, string>): string[] {
|
|
125
|
+
mkdirSync(join(tmp, 'server'), { recursive: true });
|
|
126
|
+
const rels: string[] = [];
|
|
127
|
+
for (const [name, src] of Object.entries(files)) {
|
|
128
|
+
writeFileSync(join(tmp, name), src);
|
|
129
|
+
rels.push(name);
|
|
130
|
+
}
|
|
131
|
+
return rels;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
it('drops daemon-only files from the hot pass and hot-only files from the cold pass', () => {
|
|
135
|
+
const rels = lay({
|
|
136
|
+
'server/jobs.ts': '@daemon\nclass J { @scheduled("1s") t(): void {} }\n',
|
|
137
|
+
'server/api.ts': '@rest\nclass A {}\n',
|
|
138
|
+
'server/model.ts': '@data\nclass M {}\n',
|
|
139
|
+
'server/util.ts': 'export function helper(): i32 { return 1; }\n',
|
|
140
|
+
});
|
|
141
|
+
const split = splitSurfaceFiles(tmp, rels);
|
|
142
|
+
expect(split.hasDaemon).toBe(true);
|
|
143
|
+
// hot pass: everything except the daemon-only jobs.ts.
|
|
144
|
+
expect(split.hot.sort()).toEqual(
|
|
145
|
+
['server/api.ts', 'server/model.ts', 'server/util.ts'].sort(),
|
|
146
|
+
);
|
|
147
|
+
// cold pass: everything except the hot-only api.ts.
|
|
148
|
+
expect(split.cold.sort()).toEqual(
|
|
149
|
+
['server/jobs.ts', 'server/model.ts', 'server/util.ts'].sort(),
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('keeps a file that mixes both surfaces in both passes', () => {
|
|
154
|
+
const rels = lay({ 'server/both.ts': '@daemon\nclass J {}\n@rest\nclass A {}\n' });
|
|
155
|
+
const split = splitSurfaceFiles(tmp, rels);
|
|
156
|
+
expect(split.hot).toContain('server/both.ts');
|
|
157
|
+
expect(split.cold).toContain('server/both.ts');
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// Needs the local toilscript dev build (with --targetMode) linked as a sibling
|
|
162
|
+
// repo; skip where it is absent (e.g. CI, which has only the published dep).
|
|
163
|
+
describe.skipIf(!existsSync(LOCAL_TOILSCRIPT))('buildServer two-pass (daemon project)', () => {
|
|
164
|
+
it('runs the cold pass and produces the cold artifact with a daemon catalog', async () => {
|
|
165
|
+
scaffold(DAEMON_SRC, BASE_TOILCONFIG);
|
|
166
|
+
await buildServer(tmp);
|
|
167
|
+
|
|
168
|
+
const cold = join(tmp, 'build/server/release-cold.wasm');
|
|
169
|
+
expect(existsSync(cold), 'cold artifact missing').toBe(true);
|
|
170
|
+
|
|
171
|
+
// The cold artifact carries the daemon surface + catalog (decoded byte-for-byte).
|
|
172
|
+
const coldBytes = readFileSync(cold);
|
|
173
|
+
const surface = parseSurface(coldBytes);
|
|
174
|
+
expect(surface !== 'absent' && surface !== 'invalid' && surface.targetMode).toBe('cold');
|
|
175
|
+
expect(surface !== 'absent' && surface !== 'invalid' && surface.flags.daemon).toBe(true);
|
|
176
|
+
|
|
177
|
+
const catalog = parseDaemonCatalog(coldBytes);
|
|
178
|
+
expect(catalog).not.toBeNull();
|
|
179
|
+
expect(catalog!.hasDaemon).toBe(true);
|
|
180
|
+
expect(catalog!.tasks.map((t) => t.name)).toEqual(['fast', 'nightly']);
|
|
181
|
+
expect(catalog!.tasks[0].schedule.kind).toBe('interval');
|
|
182
|
+
expect(catalog!.tasks[1].schedule.kind).toBe('cron');
|
|
183
|
+
|
|
184
|
+
// A daemon-only project (no request/stream surface) has no hot files, so the hot pass is
|
|
185
|
+
// skipped (toilscript would HARD-ERROR a @daemon class under --targetMode hot). The legacy
|
|
186
|
+
// single-artifact `release.wasm` is therefore not produced for a pure background worker.
|
|
187
|
+
expect(existsSync(join(tmp, 'build/server/release.wasm'))).toBe(false);
|
|
188
|
+
}, 60_000);
|
|
189
|
+
|
|
190
|
+
it('keeps the single-artifact path for a legacy (no-daemon) project', async () => {
|
|
191
|
+
scaffold(LEGACY_SRC, BASE_TOILCONFIG);
|
|
192
|
+
await buildServer(tmp);
|
|
193
|
+
|
|
194
|
+
expect(existsSync(join(tmp, 'build/server/release.wasm'))).toBe(true);
|
|
195
|
+
// No @daemon -> no cold pass -> no cold artifact.
|
|
196
|
+
expect(existsSync(join(tmp, 'build/server/release-cold.wasm'))).toBe(false);
|
|
197
|
+
}, 60_000);
|
|
198
|
+
});
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for the daemon-side custom-section parsers and cron evaluator
|
|
3
|
+
* (RECONCILIATION Part 5 byte layouts / F6 bitmask cron). These mirror the DB
|
|
4
|
+
* catalog tests' style: hand-build the Part 5 bytes with `DataWriter`, wrap them
|
|
5
|
+
* in a minimal wasm custom section, and assert the parser decodes them field-for-
|
|
6
|
+
* field and fails CLOSED on a truncated/garbage section.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, expect, it } from 'vitest';
|
|
10
|
+
|
|
11
|
+
import { DataWriter } from '../src/io/codec.js';
|
|
12
|
+
import { parseDaemonCatalog } from '../src/devserver/daemon/catalog.js';
|
|
13
|
+
import { cronMatches, cronNeverFires, nextCronFireMs } from '../src/devserver/daemon/cron.js';
|
|
14
|
+
import { customSection } from '../src/devserver/wasm/sections.js';
|
|
15
|
+
import { parseSurface } from '../src/devserver/wasm/surface.js';
|
|
16
|
+
|
|
17
|
+
/** Wrap a section payload (after the name) into a minimal one-section wasm module.
|
|
18
|
+
* Mirrors the DB catalog test's `wasmWithSection` helper. */
|
|
19
|
+
function wasmWithSection(name: string, payload: Uint8Array): Buffer {
|
|
20
|
+
const nameBytes = Buffer.from(name);
|
|
21
|
+
const sectionPayload = Buffer.concat([
|
|
22
|
+
Buffer.from([nameBytes.length]),
|
|
23
|
+
nameBytes,
|
|
24
|
+
Buffer.from(payload),
|
|
25
|
+
]);
|
|
26
|
+
return Buffer.concat([
|
|
27
|
+
Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x00, sectionPayload.length]),
|
|
28
|
+
sectionPayload,
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// --- toildaemon.catalog (Part 5) byte builders -----------------------------
|
|
33
|
+
|
|
34
|
+
interface BuildTask {
|
|
35
|
+
name: string;
|
|
36
|
+
taskIndex: number;
|
|
37
|
+
kind: 0 | 1;
|
|
38
|
+
intervalMs?: bigint;
|
|
39
|
+
cron?: { minute: bigint; hour: number; dom: number; month: number; dow: number };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Emit a Part 5 `toildaemon.catalog` body (the bytes AFTER the section name). */
|
|
43
|
+
function buildDaemonCatalogBytes(hasDaemon: boolean, tasks: BuildTask[]): Uint8Array {
|
|
44
|
+
const w = new DataWriter();
|
|
45
|
+
w.writeU16(1); // format_version
|
|
46
|
+
w.writeU8(hasDaemon ? 1 : 0);
|
|
47
|
+
w.writeU16(tasks.length); // n_scheduled
|
|
48
|
+
for (const t of tasks) {
|
|
49
|
+
w.writeString(t.name);
|
|
50
|
+
w.writeU16(t.taskIndex);
|
|
51
|
+
w.writeU8(t.kind);
|
|
52
|
+
w.writeU64(t.kind === 0 ? (t.intervalMs ?? 0n) : 0n); // interval_ms
|
|
53
|
+
w.writeU64(t.cron?.minute ?? 0n); // cron_minute_mask
|
|
54
|
+
w.writeU32(t.cron?.hour ?? 0); // cron_hour_mask
|
|
55
|
+
w.writeU32(t.cron?.dom ?? 0); // cron_dom_mask
|
|
56
|
+
w.writeU16(t.cron?.month ?? 0); // cron_month_mask
|
|
57
|
+
w.writeU8(t.cron?.dow ?? 0); // cron_dow_mask
|
|
58
|
+
w.writeU8(0); // overlap_policy
|
|
59
|
+
w.writeU8(0); // catchup_policy
|
|
60
|
+
w.writeU64(0n); // gas_hint
|
|
61
|
+
}
|
|
62
|
+
return w.toBytes();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
describe('parseDaemonCatalog (Part 5)', () => {
|
|
66
|
+
it('decodes an interval task and a cron task field-for-field', () => {
|
|
67
|
+
// `0 */6 * * *`: minute 0; hours 0,6,12,18; dom 1..31; month 1..12; dow 0..6.
|
|
68
|
+
const payload = buildDaemonCatalogBytes(true, [
|
|
69
|
+
{ name: 'tick', taskIndex: 0, kind: 0, intervalMs: 1000n },
|
|
70
|
+
{
|
|
71
|
+
name: 'sixHourly',
|
|
72
|
+
taskIndex: 1,
|
|
73
|
+
kind: 1,
|
|
74
|
+
cron: {
|
|
75
|
+
minute: 1n, // bit 0 (minute 0)
|
|
76
|
+
hour: (1 << 0) | (1 << 6) | (1 << 12) | (1 << 18),
|
|
77
|
+
dom: 0xfffffffe, // bits 1..31
|
|
78
|
+
month: 0x1ffe, // bits 1..12
|
|
79
|
+
dow: 0x7f, // bits 0..6
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
]);
|
|
83
|
+
const wasm = wasmWithSection('toildaemon.catalog', payload);
|
|
84
|
+
const cat = parseDaemonCatalog(wasm);
|
|
85
|
+
expect(cat).not.toBeNull();
|
|
86
|
+
expect(cat!.hasDaemon).toBe(true);
|
|
87
|
+
expect(cat!.tasks).toHaveLength(2);
|
|
88
|
+
|
|
89
|
+
const [interval, cron] = cat!.tasks;
|
|
90
|
+
expect(interval.name).toBe('tick');
|
|
91
|
+
expect(interval.taskIndex).toBe(0);
|
|
92
|
+
expect(interval.schedule).toEqual({ kind: 'interval', ms: 1000 });
|
|
93
|
+
expect(interval.overlapPolicy).toBe(0);
|
|
94
|
+
expect(interval.catchupPolicy).toBe(0);
|
|
95
|
+
expect(interval.gasHint).toBe(0n);
|
|
96
|
+
|
|
97
|
+
expect(cron.name).toBe('sixHourly');
|
|
98
|
+
expect(cron.taskIndex).toBe(1);
|
|
99
|
+
expect(cron.schedule.kind).toBe('cron');
|
|
100
|
+
if (cron.schedule.kind === 'cron') {
|
|
101
|
+
// The masks round-trip as BIT TESTS, never a string parse.
|
|
102
|
+
const m = cron.schedule.masks;
|
|
103
|
+
expect(m.minute).toBe(1n);
|
|
104
|
+
expect((m.hour & (1 << 6)) !== 0).toBe(true);
|
|
105
|
+
expect((m.hour & (1 << 7)) !== 0).toBe(false);
|
|
106
|
+
expect(m.dom).toBe(0xfffffffe);
|
|
107
|
+
expect(m.month).toBe(0x1ffe);
|
|
108
|
+
expect(m.dow).toBe(0x7f);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('decodes a 60-bit minute mask without precision loss', () => {
|
|
113
|
+
// minute 59 = bit 59, which is above 2^53 and must survive as a bigint.
|
|
114
|
+
const minuteMask = 1n << 59n;
|
|
115
|
+
const payload = buildDaemonCatalogBytes(true, [
|
|
116
|
+
{
|
|
117
|
+
name: 'lateMinute',
|
|
118
|
+
taskIndex: 0,
|
|
119
|
+
kind: 1,
|
|
120
|
+
cron: { minute: minuteMask, hour: 0xffffff, dom: 0xfffffffe, month: 0x1ffe, dow: 0x7f },
|
|
121
|
+
},
|
|
122
|
+
]);
|
|
123
|
+
const cat = parseDaemonCatalog(wasmWithSection('toildaemon.catalog', payload));
|
|
124
|
+
const t = cat!.tasks[0];
|
|
125
|
+
expect(t.schedule.kind === 'cron' && t.schedule.masks.minute).toBe(minuteMask);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('returns null for an absent section (no daemon -> emulator stays off)', () => {
|
|
129
|
+
const wasm = wasmWithSection('toildb.catalog', Buffer.from([0x01, 0x00]));
|
|
130
|
+
expect(parseDaemonCatalog(wasm)).toBeNull();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('fails closed: a truncated record yields only the cleanly-decoded prefix', () => {
|
|
134
|
+
// Claim 2 tasks but truncate inside the second record's interval_ms.
|
|
135
|
+
const full = buildDaemonCatalogBytes(true, [
|
|
136
|
+
{ name: 'a', taskIndex: 0, kind: 0, intervalMs: 5000n },
|
|
137
|
+
{ name: 'b', taskIndex: 1, kind: 0, intervalMs: 9000n },
|
|
138
|
+
]);
|
|
139
|
+
const truncated = full.subarray(0, full.length - 20); // chop the 2nd record's tail
|
|
140
|
+
const cat = parseDaemonCatalog(wasmWithSection('toildaemon.catalog', truncated));
|
|
141
|
+
expect(cat).not.toBeNull();
|
|
142
|
+
// Only the first task survived; the loop stopped on the short read.
|
|
143
|
+
expect(cat!.tasks.map((t) => t.name)).toEqual(['a']);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('returns null for a wholly garbage section (header short-read, no daemon)', () => {
|
|
147
|
+
const cat = parseDaemonCatalog(wasmWithSection('toildaemon.catalog', Buffer.from([0x01])));
|
|
148
|
+
expect(cat).toBeNull();
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// --- toil.surface (Part 5) -------------------------------------------------
|
|
153
|
+
|
|
154
|
+
function buildSurfaceBytes(opts: {
|
|
155
|
+
mode: 0 | 1;
|
|
156
|
+
flags: number;
|
|
157
|
+
abi?: number;
|
|
158
|
+
buildId?: string;
|
|
159
|
+
}): Uint8Array {
|
|
160
|
+
const w = new DataWriter();
|
|
161
|
+
w.writeU16(1); // format_version
|
|
162
|
+
w.writeU8(opts.mode); // target_mode
|
|
163
|
+
w.writeU8(0); // reserved0
|
|
164
|
+
w.writeU32(opts.flags); // surface_flags
|
|
165
|
+
w.writeU16(opts.abi ?? 1); // abi_version
|
|
166
|
+
w.writeString(opts.buildId ?? ''); // build_id
|
|
167
|
+
w.writeU32(0xdeadbeef); // fingerprint
|
|
168
|
+
w.writeU32(0x11111111); // data_coherence_hash
|
|
169
|
+
w.writeU32(0x22222222); // pair_coherence_hash (exactly THREE u32 after build_id)
|
|
170
|
+
return w.toBytes();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
describe('parseSurface (Part 5)', () => {
|
|
174
|
+
it('decodes a cold daemon surface with exactly three trailing u32 hashes', () => {
|
|
175
|
+
const flags = 0b000100 | 0b001000; // daemon (bit2) + scheduled (bit3)
|
|
176
|
+
const s = parseSurface(wasmWithSection('toil.surface', buildSurfaceBytes({ mode: 1, flags })));
|
|
177
|
+
expect(s).not.toBe('absent');
|
|
178
|
+
expect(s).not.toBe('invalid');
|
|
179
|
+
if (s !== 'absent' && s !== 'invalid') {
|
|
180
|
+
expect(s.targetMode).toBe('cold');
|
|
181
|
+
expect(s.flags.daemon).toBe(true);
|
|
182
|
+
expect(s.flags.scheduled).toBe(true);
|
|
183
|
+
expect(s.flags.rest).toBe(false);
|
|
184
|
+
expect(s.fingerprint).toBe(0xdeadbeef);
|
|
185
|
+
expect(s.dataCoherenceHash).toBe(0x11111111);
|
|
186
|
+
expect(s.pairCoherenceHash).toBe(0x22222222);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('decodes a hot surface (target_mode 0)', () => {
|
|
191
|
+
const s = parseSurface(wasmWithSection('toil.surface', buildSurfaceBytes({ mode: 0, flags: 1 })));
|
|
192
|
+
expect(s !== 'absent' && s !== 'invalid' && s.targetMode).toBe('hot');
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("treats an ABSENT section as 'absent' (legacy single artifact, load as hot)", () => {
|
|
196
|
+
const wasm = wasmWithSection('toildb.catalog', Buffer.from([0x01, 0x00]));
|
|
197
|
+
expect(parseSurface(wasm)).toBe('absent');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("fails closed: a PRESENT but truncated section is 'invalid'", () => {
|
|
201
|
+
const full = buildSurfaceBytes({ mode: 1, flags: 4 });
|
|
202
|
+
const truncated = full.subarray(0, full.length - 3); // chop a trailing hash
|
|
203
|
+
expect(parseSurface(wasmWithSection('toil.surface', truncated))).toBe('invalid');
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
describe('customSection bounds-checking', () => {
|
|
208
|
+
it('returns null for a non-wasm buffer', () => {
|
|
209
|
+
expect(customSection(Buffer.from('not a wasm module at all'), 'toil.surface')).toBeNull();
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('returns null for a truncated section table (no over-read)', () => {
|
|
213
|
+
// Magic + version, then a custom-section id with a length that runs past the end.
|
|
214
|
+
const wasm = Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x7f]);
|
|
215
|
+
expect(customSection(wasm, 'toil.surface')).toBeNull();
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe('cron bitmask evaluation (F6, never a string parse)', () => {
|
|
220
|
+
// `0 */6 * * *`: minute 0; hours 0,6,12,18; every dom/month/dow.
|
|
221
|
+
const sixHourly = {
|
|
222
|
+
minute: 1n, // bit 0
|
|
223
|
+
hour: (1 << 0) | (1 << 6) | (1 << 12) | (1 << 18),
|
|
224
|
+
dom: 0xfffffffe,
|
|
225
|
+
month: 0x1ffe,
|
|
226
|
+
dow: 0x7f,
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
it('matches the right minute/hour and rejects others', () => {
|
|
230
|
+
expect(cronMatches(sixHourly, new Date(2026, 5, 22, 6, 0, 0))).toBe(true);
|
|
231
|
+
expect(cronMatches(sixHourly, new Date(2026, 5, 22, 12, 0, 0))).toBe(true);
|
|
232
|
+
expect(cronMatches(sixHourly, new Date(2026, 5, 22, 6, 1, 0))).toBe(false); // wrong minute
|
|
233
|
+
expect(cronMatches(sixHourly, new Date(2026, 5, 22, 7, 0, 0))).toBe(false); // wrong hour
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('computes the next fire time by walking forward minute by minute', () => {
|
|
237
|
+
const from = new Date(2026, 5, 22, 6, 1, 0).getTime();
|
|
238
|
+
const next = nextCronFireMs(sixHourly, from);
|
|
239
|
+
expect(next).not.toBeNull();
|
|
240
|
+
expect(new Date(next!).getHours()).toBe(12);
|
|
241
|
+
expect(new Date(next!).getMinutes()).toBe(0);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('honors the dom/dow union rule', () => {
|
|
245
|
+
// Fire on the 1st of the month OR on Sunday (both restricted -> union).
|
|
246
|
+
const masks = {
|
|
247
|
+
minute: 1n, // minute 0
|
|
248
|
+
hour: 1, // hour 0
|
|
249
|
+
dom: 1 << 1, // only day-of-month 1
|
|
250
|
+
month: 0x1ffe, // every month
|
|
251
|
+
dow: 1 << 0, // only Sunday
|
|
252
|
+
};
|
|
253
|
+
// 2026-06-01 is a Monday -> matches via dom (the 1st).
|
|
254
|
+
expect(cronMatches(masks, new Date(2026, 5, 1, 0, 0, 0))).toBe(true);
|
|
255
|
+
// 2026-06-07 is a Sunday -> matches via dow.
|
|
256
|
+
expect(cronMatches(masks, new Date(2026, 5, 7, 0, 0, 0))).toBe(true);
|
|
257
|
+
// 2026-06-03 is a Wednesday, not the 1st -> no match.
|
|
258
|
+
expect(cronMatches(masks, new Date(2026, 5, 3, 0, 0, 0))).toBe(false);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it('flags an all-zero (unsatisfiable) mask as never-firing', () => {
|
|
262
|
+
expect(cronNeverFires({ minute: 0n, hour: 0, dom: 0, month: 0, dow: 0 })).toBe(true);
|
|
263
|
+
expect(cronNeverFires(sixHourly)).toBe(false);
|
|
264
|
+
});
|
|
265
|
+
});
|