toiljs 0.0.140 → 0.0.141
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/CHANGELOG.md +8 -0
- package/build/cli/index.js +381 -366
- package/build/compiler/config.d.ts +2 -0
- package/build/compiler/config.js +3 -0
- package/build/compiler/index.js +1 -1
- package/build/compiler/toil-docs.generated.js +3 -3
- package/build/devserver/daemon/host.d.ts +1 -0
- package/build/devserver/daemon/index.d.ts +2 -0
- package/build/devserver/daemon/index.js +1 -0
- package/build/devserver/daemon/rpc.d.ts +3 -0
- package/build/devserver/daemon/rpc.js +37 -0
- package/build/devserver/daemon/runtime.d.ts +9 -1
- package/build/devserver/daemon/runtime.js +63 -2
- package/build/devserver/daemon/storage.d.ts +35 -0
- package/build/devserver/daemon/storage.js +200 -0
- package/build/devserver/daemon/worker.d.ts +1 -0
- package/build/devserver/daemon/worker.js +42 -0
- package/build/devserver/db/database.d.ts +2 -1
- package/build/devserver/db/database.js +4 -1
- package/build/devserver/db/limits.d.ts +8 -1
- package/build/devserver/db/limits.js +14 -0
- package/build/devserver/db/materialization.d.ts +1 -0
- package/build/devserver/db/materialization.js +13 -0
- package/build/devserver/production.js +7 -6
- package/build/devserver/runtime/host.js +2 -1
- package/build/devserver/runtime/logging.d.ts +1 -0
- package/build/devserver/runtime/logging.js +83 -0
- package/build/devserver/server.js +5 -4
- package/package.json +22 -19
- package/server/globals/toil-log.ts +21 -0
- package/server/globals.d.ts +8 -0
|
@@ -4,6 +4,7 @@ import { type DbDevState } from '../db/index.js';
|
|
|
4
4
|
import { type CryptoState } from '../runtime/crypto.js';
|
|
5
5
|
import { type MemoryRef } from '../runtime/host.js';
|
|
6
6
|
export interface ResolvedDaemonConfig {
|
|
7
|
+
readonly enabled?: boolean;
|
|
7
8
|
readonly region: string | null;
|
|
8
9
|
readonly standbyRegion: string | null;
|
|
9
10
|
readonly defaultIntervalMs: number;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type DbBudgetSnapshot } from '../db/limits.js';
|
|
2
2
|
import type { DaemonHttpMock } from './http.js';
|
|
3
|
+
import { type MemoryRef } from '../runtime/host.js';
|
|
3
4
|
import { type ScheduledTask } from './catalog.js';
|
|
4
5
|
import { type DaemonRuntime, type ResolvedDaemonConfig } from './host.js';
|
|
5
6
|
export declare function daemonEmulationEnabled(nodeMode: string): boolean;
|
|
@@ -7,6 +8,7 @@ export interface DaemonHostOptions {
|
|
|
7
8
|
readonly clock?: () => number;
|
|
8
9
|
readonly httpMock?: DaemonHttpMock;
|
|
9
10
|
readonly logging?: boolean;
|
|
11
|
+
readonly sharedImports?: (ref: MemoryRef) => Record<string, (...args: number[]) => number | bigint>;
|
|
10
12
|
}
|
|
11
13
|
export interface DaemonTaskResult {
|
|
12
14
|
task: string;
|
|
@@ -181,6 +181,7 @@ export class DaemonHost {
|
|
|
181
181
|
this.state.db.knownDerives = new Set(this.derives.map(deriveContinuationKey));
|
|
182
182
|
this.state.db.functionKind = DbFunctionKind.Action;
|
|
183
183
|
const imports = buildDaemonImports(ref, this.state, this);
|
|
184
|
+
Object.assign(imports.env, this.options.sharedImports?.(ref));
|
|
184
185
|
const provided = imports;
|
|
185
186
|
const missing = WebAssembly.Module.imports(this.module)
|
|
186
187
|
.filter((i) => i.kind !== 'function' || !Object.hasOwn(provided[i.module] ?? {}, i.name))
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type MessagePort } from 'node:worker_threads';
|
|
2
|
+
export declare function synchronousCaller(port: MessagePort, signal: Int32Array): (request: unknown) => unknown;
|
|
3
|
+
export declare function serveCalls<Request>(port: MessagePort, signal: Int32Array, handle: (request: Request) => unknown): void;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { deserialize, serialize } from 'node:v8';
|
|
2
|
+
import { receiveMessageOnPort } from 'node:worker_threads';
|
|
3
|
+
export function synchronousCaller(port, signal) {
|
|
4
|
+
let usable = true;
|
|
5
|
+
return (request) => {
|
|
6
|
+
if (!usable)
|
|
7
|
+
throw new Error('daemon storage bridge is unavailable');
|
|
8
|
+
Atomics.store(signal, 0, 0);
|
|
9
|
+
port.postMessage(serialize(request));
|
|
10
|
+
if (Atomics.wait(signal, 0, 0, 120_000) === 'timed-out') {
|
|
11
|
+
usable = false;
|
|
12
|
+
port.close();
|
|
13
|
+
throw new Error('daemon storage bridge timed out');
|
|
14
|
+
}
|
|
15
|
+
const reply = receiveMessageOnPort(port);
|
|
16
|
+
if (reply === undefined)
|
|
17
|
+
throw new Error('daemon storage bridge closed without a reply');
|
|
18
|
+
const { result, error } = deserialize(Buffer.from(reply.message));
|
|
19
|
+
if (error !== undefined)
|
|
20
|
+
throw error;
|
|
21
|
+
return result;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export function serveCalls(port, signal, handle) {
|
|
25
|
+
port.on('message', (bytes) => {
|
|
26
|
+
let reply;
|
|
27
|
+
try {
|
|
28
|
+
reply = { result: handle(deserialize(Buffer.from(bytes))) };
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
reply = { error: error instanceof Error ? error : new Error(String(error)) };
|
|
32
|
+
}
|
|
33
|
+
port.postMessage(serialize(reply));
|
|
34
|
+
Atomics.store(signal, 0, 1);
|
|
35
|
+
Atomics.notify(signal, 0);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
import { DaemonHost } from './index.js';
|
|
1
|
+
import { DaemonHost, type DaemonHostOptions } from './index.js';
|
|
2
2
|
import type { ResolvedDaemonConfig } from './host.js';
|
|
3
3
|
export interface DaemonRuntimeOptions {
|
|
4
4
|
readonly coldWasmFile?: string;
|
|
5
|
+
readonly hotWasmFile?: string;
|
|
5
6
|
readonly nodeMode?: string;
|
|
6
7
|
readonly daemon?: ResolvedDaemonConfig;
|
|
7
8
|
readonly logging?: boolean;
|
|
8
9
|
readonly pollMs?: number;
|
|
10
|
+
readonly sharedImports?: DaemonHostOptions['sharedImports'];
|
|
9
11
|
}
|
|
10
12
|
export interface RunningDaemonRuntime {
|
|
11
13
|
readonly host: DaemonHost;
|
|
12
14
|
close(): void;
|
|
13
15
|
}
|
|
14
16
|
export declare function startDaemonRuntime(options: DaemonRuntimeOptions): RunningDaemonRuntime | null;
|
|
17
|
+
export interface RunningDaemonWorker {
|
|
18
|
+
readonly threadId: number;
|
|
19
|
+
readonly ready: Promise<void>;
|
|
20
|
+
close(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
export declare function startDaemonWorker(options: Omit<DaemonRuntimeOptions, 'sharedImports'>): RunningDaemonWorker | null;
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import pc from 'picocolors';
|
|
2
|
+
import { MessageChannel, Worker } from 'node:worker_threads';
|
|
2
3
|
import { DaemonHost, daemonEmulationEnabled } from './index.js';
|
|
4
|
+
import { serveCalls } from './rpc.js';
|
|
5
|
+
import { storageReceiver } from './storage.js';
|
|
3
6
|
export function startDaemonRuntime(options) {
|
|
4
7
|
const nodeMode = options.nodeMode ?? 'all';
|
|
5
8
|
if (options.coldWasmFile === undefined ||
|
|
6
|
-
!daemonEmulationEnabled(nodeMode) ||
|
|
9
|
+
!(options.daemon?.enabled ?? daemonEmulationEnabled(nodeMode)) ||
|
|
7
10
|
options.daemon === undefined) {
|
|
8
11
|
return null;
|
|
9
12
|
}
|
|
10
|
-
const host = new DaemonHost(options.coldWasmFile, options.daemon,
|
|
13
|
+
const host = new DaemonHost(options.coldWasmFile, options.daemon, 'daemon', undefined, {
|
|
11
14
|
logging: options.logging,
|
|
15
|
+
sharedImports: options.sharedImports,
|
|
12
16
|
});
|
|
13
17
|
const pollDaemon = () => {
|
|
14
18
|
try {
|
|
@@ -29,3 +33,60 @@ export function startDaemonRuntime(options) {
|
|
|
29
33
|
},
|
|
30
34
|
};
|
|
31
35
|
}
|
|
36
|
+
export function startDaemonWorker(options) {
|
|
37
|
+
if (options.coldWasmFile === undefined ||
|
|
38
|
+
options.daemon === undefined ||
|
|
39
|
+
!(options.daemon.enabled ?? daemonEmulationEnabled(options.nodeMode ?? 'all')))
|
|
40
|
+
return null;
|
|
41
|
+
const { port1, port2 } = new MessageChannel();
|
|
42
|
+
const signal = new Int32Array(new SharedArrayBuffer(4));
|
|
43
|
+
serveCalls(port1, signal, storageReceiver());
|
|
44
|
+
let worker;
|
|
45
|
+
try {
|
|
46
|
+
worker = new Worker(new URL('./worker.js', import.meta.url), {
|
|
47
|
+
name: 'toil-l4',
|
|
48
|
+
workerData: { options, port: port2, signal },
|
|
49
|
+
transferList: [port2],
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
port1.close();
|
|
54
|
+
port2.close();
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
let exited = false;
|
|
58
|
+
const exit = new Promise((resolve) => worker.once('exit', () => {
|
|
59
|
+
exited = true;
|
|
60
|
+
port1.close();
|
|
61
|
+
resolve();
|
|
62
|
+
}));
|
|
63
|
+
const ready = new Promise((resolve, reject) => {
|
|
64
|
+
worker.once('message', () => resolve());
|
|
65
|
+
worker.once('error', reject);
|
|
66
|
+
worker.once('exit', (code) => reject(new Error(`L4 worker exited before ready (${String(code)})`)));
|
|
67
|
+
});
|
|
68
|
+
void ready.catch((error) => process.stderr.write(`toiljs L4 worker failed: ${String(error)}\n`));
|
|
69
|
+
worker.on('error', (error) => {
|
|
70
|
+
process.stderr.write(`toiljs L4 worker error: ${String(error)}\n`);
|
|
71
|
+
});
|
|
72
|
+
let closing;
|
|
73
|
+
return {
|
|
74
|
+
threadId: worker.threadId,
|
|
75
|
+
ready,
|
|
76
|
+
close: () => (closing ??= (async () => {
|
|
77
|
+
if (exited)
|
|
78
|
+
return;
|
|
79
|
+
worker.postMessage('shutdown');
|
|
80
|
+
const timer = setTimeout(() => {
|
|
81
|
+
void worker.terminate();
|
|
82
|
+
}, 1500);
|
|
83
|
+
timer.unref();
|
|
84
|
+
try {
|
|
85
|
+
await exit;
|
|
86
|
+
}
|
|
87
|
+
finally {
|
|
88
|
+
clearTimeout(timer);
|
|
89
|
+
}
|
|
90
|
+
})()),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { dbLimitUsage } from '../db/limits.js';
|
|
2
|
+
import { type DbDevState } from '../db/types.js';
|
|
3
|
+
import { type MemoryRef } from '../runtime/host.js';
|
|
4
|
+
type Call = (request: unknown) => unknown;
|
|
5
|
+
interface MemoryInput {
|
|
6
|
+
size: number;
|
|
7
|
+
now: number;
|
|
8
|
+
regions: {
|
|
9
|
+
offset: number;
|
|
10
|
+
bytes: Buffer;
|
|
11
|
+
}[];
|
|
12
|
+
}
|
|
13
|
+
interface StorageRequest {
|
|
14
|
+
method: string;
|
|
15
|
+
args: unknown[];
|
|
16
|
+
db?: DbDevState;
|
|
17
|
+
usage?: ReturnType<typeof dbLimitUsage>;
|
|
18
|
+
memory?: MemoryInput;
|
|
19
|
+
}
|
|
20
|
+
interface StorageReply {
|
|
21
|
+
value?: unknown;
|
|
22
|
+
error?: Error;
|
|
23
|
+
output?: {
|
|
24
|
+
offset: number;
|
|
25
|
+
bytes: Buffer;
|
|
26
|
+
};
|
|
27
|
+
db?: DbDevState;
|
|
28
|
+
usage?: ReturnType<typeof dbLimitUsage>;
|
|
29
|
+
}
|
|
30
|
+
export declare function installWorkerStorage(call: Call): void;
|
|
31
|
+
export declare function workerSharedImports(call: Call, ref: MemoryRef): {
|
|
32
|
+
[k: string]: (...args: number[]) => number;
|
|
33
|
+
};
|
|
34
|
+
export declare function storageReceiver(): (request: StorageRequest) => StorageReply;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { devDb, DevDatabase, installDatabaseBackend } from '../db/database.js';
|
|
2
|
+
import { dbLimitUsage, restoreDbLimitUsage } from '../db/limits.js';
|
|
3
|
+
import { queueMaterialization, withoutMaterializationScheduling } from '../db/materialization.js';
|
|
4
|
+
import { DbFunctionKind } from '../db/types.js';
|
|
5
|
+
import { buildEnvImports, freshDispatchState } from '../runtime/host.js';
|
|
6
|
+
const keyed = { pairs: [1] };
|
|
7
|
+
const valued = { pairs: [1, 3] };
|
|
8
|
+
const writing = { pairs: [1, 3], idem: 5 };
|
|
9
|
+
const layouts = {
|
|
10
|
+
resolveCollection: { pairs: [0], output: 2, outputSize: 4 },
|
|
11
|
+
get: keyed,
|
|
12
|
+
getMany: keyed,
|
|
13
|
+
exists: keyed,
|
|
14
|
+
create: writing,
|
|
15
|
+
patch: writing,
|
|
16
|
+
upsert: writing,
|
|
17
|
+
delete: { pairs: [1], idem: 3 },
|
|
18
|
+
getDelete: { pairs: [1], idem: 3 },
|
|
19
|
+
uniqueLookup: keyed,
|
|
20
|
+
uniqueClaim: writing,
|
|
21
|
+
uniqueRelease: valued,
|
|
22
|
+
membershipContains: valued,
|
|
23
|
+
membershipAdd: valued,
|
|
24
|
+
membershipRemove: valued,
|
|
25
|
+
membershipList: keyed,
|
|
26
|
+
membershipPage: valued,
|
|
27
|
+
documentsScan: keyed,
|
|
28
|
+
viewGet: keyed,
|
|
29
|
+
viewPublish: valued,
|
|
30
|
+
viewDelete: keyed,
|
|
31
|
+
counterGet: keyed,
|
|
32
|
+
counterAdd: { pairs: [1], idem: 4 },
|
|
33
|
+
append: writing,
|
|
34
|
+
appendOnce: { pairs: [1, 3, 5] },
|
|
35
|
+
eventsGet: valued,
|
|
36
|
+
eventsReference: valued,
|
|
37
|
+
eventsRedact: keyed,
|
|
38
|
+
eventsEntries: keyed,
|
|
39
|
+
eventsLast: keyed,
|
|
40
|
+
compareExchange: { pairs: [1, 3, 5] },
|
|
41
|
+
compareDelete: valued,
|
|
42
|
+
enqueue: writing,
|
|
43
|
+
latest: keyed,
|
|
44
|
+
since: keyed,
|
|
45
|
+
capacitySetTotal: keyed,
|
|
46
|
+
capacityAvailable: keyed,
|
|
47
|
+
capacityReserve: { pairs: [1], idem: 5 },
|
|
48
|
+
capacityConfirm: keyed,
|
|
49
|
+
capacityCancel: keyed,
|
|
50
|
+
'data.atomic_get': valued,
|
|
51
|
+
'data.atomic_batch': { pairs: [0] },
|
|
52
|
+
env_get: { pairs: [0], output: 2 },
|
|
53
|
+
env_get_secure: { pairs: [0], output: 2 },
|
|
54
|
+
email_send: { pairs: [0] },
|
|
55
|
+
email_send_detached: { pairs: [0] },
|
|
56
|
+
};
|
|
57
|
+
const sharedServices = ['env_get', 'env_get_secure', 'email_send', 'email_send_detached'];
|
|
58
|
+
const storageMethods = new Set([
|
|
59
|
+
'setCatalog',
|
|
60
|
+
'persist',
|
|
61
|
+
'pendingContinuations',
|
|
62
|
+
'acknowledgeContinuation',
|
|
63
|
+
'commitDeriveCheckpoints',
|
|
64
|
+
'requestContinuation',
|
|
65
|
+
]);
|
|
66
|
+
function captureMemory(ref, layout, args) {
|
|
67
|
+
if (ref.memory === null)
|
|
68
|
+
throw new Error('daemon database memory is not bound');
|
|
69
|
+
const memory = Buffer.from(ref.memory.buffer);
|
|
70
|
+
const regions = [];
|
|
71
|
+
const capture = (offset, size) => {
|
|
72
|
+
if (!Number.isInteger(offset) ||
|
|
73
|
+
!Number.isInteger(size) ||
|
|
74
|
+
offset < 0 ||
|
|
75
|
+
size < 0 ||
|
|
76
|
+
offset > memory.length ||
|
|
77
|
+
size > memory.length - offset)
|
|
78
|
+
throw new Error('daemon database input out of bounds');
|
|
79
|
+
regions.push({ offset, bytes: Buffer.from(memory.subarray(offset, offset + size)) });
|
|
80
|
+
};
|
|
81
|
+
for (const index of layout.pairs ?? [])
|
|
82
|
+
capture(Number(args[index]), Number(args[index + 1]));
|
|
83
|
+
if (layout.idem !== undefined && args[layout.idem] !== 0)
|
|
84
|
+
capture(Number(args[layout.idem]), 16);
|
|
85
|
+
return { size: memory.length, now: ref.now?.() ?? Date.now(), regions };
|
|
86
|
+
}
|
|
87
|
+
export function installWorkerStorage(call) {
|
|
88
|
+
const local = new DevDatabase();
|
|
89
|
+
const invoke = (method, args, ref, db) => {
|
|
90
|
+
const layout = layouts[method];
|
|
91
|
+
const reply = call({
|
|
92
|
+
kind: 'storage',
|
|
93
|
+
method,
|
|
94
|
+
args,
|
|
95
|
+
db,
|
|
96
|
+
usage: db === undefined ? undefined : dbLimitUsage(db),
|
|
97
|
+
memory: ref === undefined ? undefined : captureMemory(ref, layout, args),
|
|
98
|
+
});
|
|
99
|
+
if (db !== undefined) {
|
|
100
|
+
Object.assign(db, reply.db);
|
|
101
|
+
restoreDbLimitUsage(db, reply.usage);
|
|
102
|
+
if (db.functionKind === DbFunctionKind.Job)
|
|
103
|
+
for (const collection of db.writtenCollections)
|
|
104
|
+
queueMaterialization(collection);
|
|
105
|
+
}
|
|
106
|
+
if (ref?.memory !== null && ref?.memory !== undefined && reply.output !== undefined)
|
|
107
|
+
Buffer.from(ref.memory.buffer).set(reply.output.bytes, reply.output.offset);
|
|
108
|
+
if (reply.error !== undefined)
|
|
109
|
+
throw reply.error;
|
|
110
|
+
return reply.value;
|
|
111
|
+
};
|
|
112
|
+
installDatabaseBackend(new Proxy(local, {
|
|
113
|
+
get(target, key) {
|
|
114
|
+
if (key === 'takeResult' || key === 'resultSchemaVersion')
|
|
115
|
+
return target[key].bind(target);
|
|
116
|
+
if (key === 'atomicImports')
|
|
117
|
+
return (ref, db) => Object.fromEntries(Object.keys(target.atomicImports(ref, db)).map((name) => [
|
|
118
|
+
name,
|
|
119
|
+
(...args) => invoke(name, args, ref, db),
|
|
120
|
+
]));
|
|
121
|
+
if (Object.hasOwn(layouts, key))
|
|
122
|
+
return (ref, db, ...args) => invoke(key, args, ref, db);
|
|
123
|
+
if (key === 'commitDeriveCheckpoints' || key === 'requestContinuation')
|
|
124
|
+
return (db) => invoke(key, [], undefined, db);
|
|
125
|
+
if (storageMethods.has(key))
|
|
126
|
+
return (...args) => invoke(key, args);
|
|
127
|
+
throw new Error(`Unsupported daemon database operation: ${key}`);
|
|
128
|
+
},
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
export function workerSharedImports(call, ref) {
|
|
132
|
+
return Object.fromEntries(sharedServices.map((method) => [
|
|
133
|
+
method,
|
|
134
|
+
(...args) => {
|
|
135
|
+
const reply = call({
|
|
136
|
+
kind: 'storage',
|
|
137
|
+
method,
|
|
138
|
+
args,
|
|
139
|
+
memory: captureMemory(ref, layouts[method], args),
|
|
140
|
+
});
|
|
141
|
+
if (reply.output !== undefined && ref.memory !== null)
|
|
142
|
+
Buffer.from(ref.memory.buffer).set(reply.output.bytes, reply.output.offset);
|
|
143
|
+
if (reply.error !== undefined)
|
|
144
|
+
throw reply.error;
|
|
145
|
+
return reply.value;
|
|
146
|
+
},
|
|
147
|
+
]));
|
|
148
|
+
}
|
|
149
|
+
export function storageReceiver() {
|
|
150
|
+
let mirror = new WebAssembly.Memory({ initial: 0 });
|
|
151
|
+
return (request) => {
|
|
152
|
+
const { method, args, db, memory } = request;
|
|
153
|
+
const ref = { memory: null };
|
|
154
|
+
if (memory !== undefined) {
|
|
155
|
+
if (mirror.buffer.byteLength !== memory.size)
|
|
156
|
+
mirror = new WebAssembly.Memory({ initial: memory.size / 65536 });
|
|
157
|
+
for (const region of memory.regions)
|
|
158
|
+
new Uint8Array(mirror.buffer).set(region.bytes, region.offset);
|
|
159
|
+
ref.memory = mirror;
|
|
160
|
+
ref.now = () => memory.now;
|
|
161
|
+
}
|
|
162
|
+
if (db !== undefined)
|
|
163
|
+
restoreDbLimitUsage(db, request.usage);
|
|
164
|
+
let value;
|
|
165
|
+
let error;
|
|
166
|
+
try {
|
|
167
|
+
value = withoutMaterializationScheduling(() => {
|
|
168
|
+
if (sharedServices.includes(method)) {
|
|
169
|
+
const service = buildEnvImports(ref, freshDispatchState())[method];
|
|
170
|
+
return service(...args);
|
|
171
|
+
}
|
|
172
|
+
if (method.startsWith('data.atomic_')) {
|
|
173
|
+
if (db === undefined)
|
|
174
|
+
throw new Error('Missing daemon database state');
|
|
175
|
+
return devDb.atomicImports(ref, db)[method](...args);
|
|
176
|
+
}
|
|
177
|
+
const operation = devDb[method];
|
|
178
|
+
if (Object.hasOwn(layouts, method))
|
|
179
|
+
return operation.call(devDb, ref, db, ...args);
|
|
180
|
+
if (storageMethods.has(method))
|
|
181
|
+
return operation.call(devDb, ...(db === undefined ? args : [db]));
|
|
182
|
+
throw new Error(`Unsupported daemon database operation: ${method}`);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
catch (caught) {
|
|
186
|
+
error = caught instanceof Error ? caught : new Error(String(caught));
|
|
187
|
+
}
|
|
188
|
+
const layout = layouts[method];
|
|
189
|
+
let output;
|
|
190
|
+
if (error === undefined &&
|
|
191
|
+
layout?.output !== undefined &&
|
|
192
|
+
typeof value === 'number' &&
|
|
193
|
+
value >= 0) {
|
|
194
|
+
const offset = Number(args[layout.output]);
|
|
195
|
+
const size = layout.outputSize ?? value;
|
|
196
|
+
output = { offset, bytes: Buffer.from(mirror.buffer, offset, size) };
|
|
197
|
+
}
|
|
198
|
+
return { value, error, output, db, usage: db === undefined ? undefined : dbLimitUsage(db) };
|
|
199
|
+
};
|
|
200
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { parentPort, workerData } from 'node:worker_threads';
|
|
2
|
+
import { startDaemonRuntime } from './runtime.js';
|
|
3
|
+
import { synchronousCaller } from './rpc.js';
|
|
4
|
+
import { installWorkerStorage, workerSharedImports } from './storage.js';
|
|
5
|
+
import { WasmServerModule } from '../runtime/module.js';
|
|
6
|
+
if (parentPort === null)
|
|
7
|
+
throw new Error('L4 must run in a worker thread');
|
|
8
|
+
const primary = parentPort;
|
|
9
|
+
const { options, port, signal } = workerData;
|
|
10
|
+
const call = synchronousCaller(port, signal);
|
|
11
|
+
installWorkerStorage(call);
|
|
12
|
+
const hot = options.hotWasmFile === undefined
|
|
13
|
+
? null
|
|
14
|
+
: new WasmServerModule(options.hotWasmFile, options.logging);
|
|
15
|
+
const refreshHot = () => {
|
|
16
|
+
try {
|
|
17
|
+
hot?.refresh();
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
process.stderr.write(`toiljs L4 materializer reload failed: ${String(error)}\n`);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
refreshHot();
|
|
24
|
+
const hotTimer = setInterval(refreshHot, options.pollMs ?? 500);
|
|
25
|
+
hotTimer.unref();
|
|
26
|
+
const runtime = startDaemonRuntime({
|
|
27
|
+
...options,
|
|
28
|
+
sharedImports: (ref) => workerSharedImports(call, ref),
|
|
29
|
+
});
|
|
30
|
+
primary.on('message', (message) => {
|
|
31
|
+
if (message !== 'shutdown')
|
|
32
|
+
return;
|
|
33
|
+
try {
|
|
34
|
+
runtime?.close();
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
clearInterval(hotTimer);
|
|
38
|
+
port.close();
|
|
39
|
+
primary.close();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
primary.postMessage('ready');
|
|
@@ -98,7 +98,8 @@ export declare class DevDatabase {
|
|
|
98
98
|
resetForTests(): void;
|
|
99
99
|
setCatalogForTests(entries: Record<string, CatalogSeedEntry>): void;
|
|
100
100
|
}
|
|
101
|
-
export declare
|
|
101
|
+
export declare let devDb: DevDatabase;
|
|
102
|
+
export declare function installDatabaseBackend(backend: DevDatabase): void;
|
|
102
103
|
export declare function setDbCatalog(wasm: Buffer, merge?: boolean): void;
|
|
103
104
|
export declare function configureDbPersistence(filePath: string): void;
|
|
104
105
|
export declare function persistDb(): void;
|
|
@@ -1523,7 +1523,10 @@ export class DevDatabase {
|
|
|
1523
1523
|
this.catalog = { kind: 'present', collections };
|
|
1524
1524
|
}
|
|
1525
1525
|
}
|
|
1526
|
-
export
|
|
1526
|
+
export let devDb = new DevDatabase();
|
|
1527
|
+
export function installDatabaseBackend(backend) {
|
|
1528
|
+
devDb = backend;
|
|
1529
|
+
}
|
|
1527
1530
|
export function setDbCatalog(wasm, merge = false) {
|
|
1528
1531
|
devDb.setCatalog(wasm, merge);
|
|
1529
1532
|
}
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import { type DbDevState } from './types.js';
|
|
1
|
+
import { DbFunctionKind, type DbDevState } from './types.js';
|
|
2
2
|
type Imports = Record<string, (...args: number[]) => number | bigint>;
|
|
3
|
+
export declare function dbLimitUsage(db: DbDevState): {
|
|
4
|
+
kind: DbFunctionKind;
|
|
5
|
+
ops: number;
|
|
6
|
+
writes: number;
|
|
7
|
+
bytes: number;
|
|
8
|
+
} | undefined;
|
|
9
|
+
export declare function restoreDbLimitUsage(db: DbDevState, usage: ReturnType<typeof dbLimitUsage>): void;
|
|
3
10
|
export interface DbBudgetSnapshot {
|
|
4
11
|
ops: number;
|
|
5
12
|
writes: number;
|
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
import { DbFunctionKind } from './types.js';
|
|
2
2
|
const counters = new WeakMap();
|
|
3
|
+
export function dbLimitUsage(db) {
|
|
4
|
+
return counters.get(db);
|
|
5
|
+
}
|
|
6
|
+
export function restoreDbLimitUsage(db, usage) {
|
|
7
|
+
if (usage === undefined)
|
|
8
|
+
counters.delete(db);
|
|
9
|
+
else {
|
|
10
|
+
const existing = counters.get(db);
|
|
11
|
+
if (existing === undefined)
|
|
12
|
+
counters.set(db, usage);
|
|
13
|
+
else
|
|
14
|
+
Object.assign(existing, usage);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
3
17
|
const exempt = new Set([
|
|
4
18
|
'data.resolve_collection',
|
|
5
19
|
'data.take_result',
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export declare function withoutMaterializationScheduling<T>(run: () => T): T;
|
|
1
2
|
export declare function registerMaterializer(key: string, runner: (written: ReadonlySet<string>) => void): void;
|
|
2
3
|
export declare function unregisterMaterializer(key: string): void;
|
|
3
4
|
export declare function queueMaterialization(collection: string): void;
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
const runners = new Map();
|
|
2
2
|
const pending = new Set();
|
|
3
3
|
let scheduled = false;
|
|
4
|
+
let schedulingSuppressed = false;
|
|
5
|
+
export function withoutMaterializationScheduling(run) {
|
|
6
|
+
const previous = schedulingSuppressed;
|
|
7
|
+
schedulingSuppressed = true;
|
|
8
|
+
try {
|
|
9
|
+
return run();
|
|
10
|
+
}
|
|
11
|
+
finally {
|
|
12
|
+
schedulingSuppressed = previous;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
4
15
|
export function registerMaterializer(key, runner) {
|
|
5
16
|
runners.set(key, runner);
|
|
6
17
|
}
|
|
@@ -8,6 +19,8 @@ export function unregisterMaterializer(key) {
|
|
|
8
19
|
runners.delete(key);
|
|
9
20
|
}
|
|
10
21
|
export function queueMaterialization(collection) {
|
|
22
|
+
if (schedulingSuppressed)
|
|
23
|
+
return;
|
|
11
24
|
pending.add(collection);
|
|
12
25
|
if (scheduled)
|
|
13
26
|
return;
|
|
@@ -6,7 +6,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
6
6
|
import { Server, } from '@dacely/hyper-express';
|
|
7
7
|
import pc from 'picocolors';
|
|
8
8
|
import { installSecurityHeaders, sendSecureStaticFile, writeResponseHeader } from 'toiljs/backend';
|
|
9
|
-
import {
|
|
9
|
+
import { startDaemonWorker } from './daemon/runtime.js';
|
|
10
10
|
import { configureDbPersistence } from './db/index.js';
|
|
11
11
|
import { initEmailService } from './email/index.js';
|
|
12
12
|
import { assembleRouteSsr, dispatchEnvelopeRequest, installRuntimeErrorHandler, isDispatchableMethod, prepareSsrResponse, prepareWasmResponse, resolveDynamicFile, resolveStaticFile, runtimeServerOptions, toEnvelopeRequest, } from './http/runtime.js';
|
|
@@ -182,8 +182,9 @@ function createBuiltRuntime(options, paths) {
|
|
|
182
182
|
}
|
|
183
183
|
function startBuiltDaemon(options, paths) {
|
|
184
184
|
configureDbPersistence(path.join(paths.projectRoot, '.toil', 'devdata.json'));
|
|
185
|
-
return
|
|
185
|
+
return startDaemonWorker({
|
|
186
186
|
coldWasmFile: options.coldWasmFile,
|
|
187
|
+
hotWasmFile: options.wasmFile,
|
|
187
188
|
nodeMode: options.nodeMode,
|
|
188
189
|
daemon: options.daemon,
|
|
189
190
|
logging: options.logging === true,
|
|
@@ -345,13 +346,13 @@ async function startBuiltServerSingle(options) {
|
|
|
345
346
|
return {
|
|
346
347
|
...server,
|
|
347
348
|
close: async () => {
|
|
348
|
-
daemon?.close();
|
|
349
|
+
await daemon?.close();
|
|
349
350
|
await server.close();
|
|
350
351
|
},
|
|
351
352
|
};
|
|
352
353
|
}
|
|
353
354
|
catch (e) {
|
|
354
|
-
daemon?.close();
|
|
355
|
+
await daemon?.close();
|
|
355
356
|
throw e;
|
|
356
357
|
}
|
|
357
358
|
}
|
|
@@ -473,7 +474,7 @@ async function startThreadedBuiltServer(options, threads) {
|
|
|
473
474
|
}
|
|
474
475
|
catch (e) {
|
|
475
476
|
closing = true;
|
|
476
|
-
daemon?.close();
|
|
477
|
+
await daemon?.close();
|
|
477
478
|
await Promise.all([...workers.values()].map((worker) => stopWorker(worker)));
|
|
478
479
|
throw e;
|
|
479
480
|
}
|
|
@@ -495,7 +496,7 @@ async function startThreadedBuiltServer(options, threads) {
|
|
|
495
496
|
},
|
|
496
497
|
close: async () => {
|
|
497
498
|
closing = true;
|
|
498
|
-
daemon?.close();
|
|
499
|
+
await daemon?.close();
|
|
499
500
|
await Promise.all([...workers.values()].map((worker) => stopWorker(worker)));
|
|
500
501
|
},
|
|
501
502
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { buildBase64Imports } from './base64.js';
|
|
2
2
|
import { googleVerifyImport } from './google.js';
|
|
3
|
+
import { sanitizeServerLog } from './logging.js';
|
|
3
4
|
import { buildImageImports, freshImageState } from '../images/index.js';
|
|
4
5
|
import { devEnvGet, devEnvGetSecure } from '../config/env.js';
|
|
5
6
|
import { ratelimitCheck } from '../config/ratelimit.js';
|
|
@@ -146,7 +147,7 @@ function buildLogImports(ref, db) {
|
|
|
146
147
|
return 0;
|
|
147
148
|
db.logMessages++;
|
|
148
149
|
const write = level === 0 ? console.info : level === 1 ? console.warn : console.error;
|
|
149
|
-
write('[toil guest]', message);
|
|
150
|
+
write('[toil guest]', sanitizeServerLog(message));
|
|
150
151
|
return 1;
|
|
151
152
|
},
|
|
152
153
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function sanitizeServerLog(message: string): string;
|