@lunora/platform-node 1.0.0-alpha.13 → 1.0.0-alpha.130
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/README.md +7 -0
- package/dist/conformance/index.mjs +1 -1
- package/dist/index.d.mts +279 -124
- package/dist/index.d.ts +279 -124
- package/dist/index.mjs +1 -1
- package/dist/packem_shared/NODE_SCHEDULER_DISPATCH_PATH-CNBAtztq.mjs +1 -0
- package/dist/packem_shared/createNodeGlobalStore-DiGxjmD7.mjs +1 -0
- package/dist/packem_shared/createNodePlatform-dBsmRW5b.mjs +1 -0
- package/dist/packem_shared/createNodeProfileHandler-MX5xKPg0.mjs +1 -0
- package/dist/packem_shared/createNodeQueueHost-CttDb5sY.mjs +17 -0
- package/dist/packem_shared/createNodeR2Bucket-C_oO6X63.mjs +1 -0
- package/dist/packem_shared/createNodeSchedulerHost-BEAX6I4m.mjs +15 -0
- package/dist/packem_shared/createNodeShardHost-Da19DFX0.mjs +1 -0
- package/dist/packem_shared/createNodeShardRegistry-D-KxwdUc.mjs +1 -0
- package/dist/packem_shared/createNodeShardState-D6XMWbFs.mjs +1 -0
- package/dist/packem_shared/createNodeSocketHost-DQs85G7W.mjs +2 -0
- package/dist/packem_shared/createNodeWorkflowHost-B76TKiw_.mjs +1 -0
- package/dist/packem_shared/hmac-url-BrwGfbCr.mjs +1 -0
- package/package.json +13 -8
- package/dist/packem_shared/createNodeGlobalStore-Cwi33XOk.mjs +0 -1
- package/dist/packem_shared/createNodePlatform-CfKfpghn.mjs +0 -1
- package/dist/packem_shared/createNodeQueueHost--mBLGSxF.mjs +0 -17
- package/dist/packem_shared/createNodeR2Bucket-CmHYlIni.mjs +0 -1
- package/dist/packem_shared/createNodeSchedulerHost-vZgv1CEo.mjs +0 -15
- package/dist/packem_shared/createNodeShardHost-ByDrsNFb.mjs +0 -1
- package/dist/packem_shared/createNodeShardRegistry-Zy8eZoF9.mjs +0 -1
- package/dist/packem_shared/createNodeShardState-CpUzvWwd.mjs +0 -1
- package/dist/packem_shared/createNodeSocketHost-Ce4aH2Kq.mjs +0 -2
- package/dist/packem_shared/createNodeWorkflowHost-CTVBYiuj.mjs +0 -1
package/README.md
CHANGED
|
@@ -17,3 +17,10 @@ Portability was a claim, not a construction check — `PLATFORM_MATRICES` held e
|
|
|
17
17
|
## Scope
|
|
18
18
|
|
|
19
19
|
This is a **spike**, not a production target: it is not wired into `lunora dev`, has no deploy driver, and several capabilities are honestly rated `emulated`/`unsupported` in its capability matrix entry (see `@lunora/platform`'s `NODE_CAPABILITIES`). Wiring it into the dev server is the payoff and a follow-up, not this change.
|
|
20
|
+
|
|
21
|
+
## On-demand profiling
|
|
22
|
+
|
|
23
|
+
`createNodeProfileHandler({ token })` serves an on-demand CPU or heap profile
|
|
24
|
+
(gzip pprof) behind a bearer-guarded route the app mounts itself. The full
|
|
25
|
+
description, including what each profile type does and does not show, is in
|
|
26
|
+
[`docs/index.mdx`](./docs/index.mdx#on-demand-profiling).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createNodeShardKvStore as
|
|
1
|
+
import{createNodeShardKvStore as N}from"../packem_shared/createNodeShardKvStore-DVi1OsvH.mjs";import{createNodeSchedulerHost as k}from"../packem_shared/createNodeSchedulerHost-BEAX6I4m.mjs";import{createNodeShardHost as D}from"../packem_shared/createNodeShardHost-Da19DFX0.mjs";import{createNodeShardRegistry as H}from"../packem_shared/createNodeShardRegistry-D-KxwdUc.mjs";import{createNodeSocketHost as T}from"../packem_shared/createNodeSocketHost-DQs85G7W.mjs";const P=()=>{const{database:o,dispose:m,host:p}=D(),h=N(o),c=H(),{directory:l}=c,{readFrames:w,restoreSocket:u,simulateRecycle:S,socket:y}=T(o),d=new Set,s=new Map,{dispose:f,scheduler:i,simulateDeadLetter:g}=k(o,{onDispatch:(e,a,t)=>{d.add(t.id),s.set(e,(s.get(e)??0)+1)}}),n=()=>{f(),m(),c.close()};return{awaitAlarmFired:async e=>{await new Promise(a=>{setTimeout(a,Math.max(0,e-Date.now())+30)})},awaitJobDispatched:async e=>{const t=(await i.list?.())?.find(r=>r.id===e);return t!==void 0&&await new Promise(r=>{setTimeout(r,Math.max(0,t.scheduledFor-Date.now())+30)}),d.has(e)},cleanup:n,cronTicks:e=>s.get(e)??0,directory:l,disposeTerminally:n,kv:h,readFrames:w,restoreSocket:u,scheduler:i,shard:p,simulateDeadLetter:g,simulateRecycle:S,socket:y}};export{P as createNodeConformanceHost};
|
package/dist/index.d.mts
CHANGED
|
@@ -4,6 +4,39 @@ import Database from 'better-sqlite3';
|
|
|
4
4
|
import { ShardKvStore, QueueBindingLike, MessageBatchLike, SchedulerHost, ShardHost, SocketHost, DirectShardDirectory, PlatformCapabilities, ShardDirectory, R2BucketLike, SocketHandle } from '@lunora/platform';
|
|
5
5
|
import { WorkflowBindingLike } from '@lunora/workflow';
|
|
6
6
|
import { WorkflowRuntime, WorkflowStore } from '@visulima/workflow';
|
|
7
|
+
/** One function call the route has authenticated and parsed. */
|
|
8
|
+
interface NodeDispatchInvocation {
|
|
9
|
+
args: Record<string, unknown>;
|
|
10
|
+
functionPath: string;
|
|
11
|
+
/** The scheduler record or delivery id, used as the shard's idempotency key. */
|
|
12
|
+
id: string | undefined;
|
|
13
|
+
/** Identity the trusted caller forwarded, if any. */
|
|
14
|
+
identity: {
|
|
15
|
+
identity?: string;
|
|
16
|
+
userId?: string;
|
|
17
|
+
} | undefined;
|
|
18
|
+
shardKey: string;
|
|
19
|
+
traceparent: string | undefined;
|
|
20
|
+
}
|
|
21
|
+
interface NodeDispatchRouteOptions {
|
|
22
|
+
/** `LUNORA_ADMIN_TOKEN`. Used as the bearer when no HMAC signature is presented. */
|
|
23
|
+
adminToken?: string;
|
|
24
|
+
/** The shard a body without `shardKey` is sent to. */
|
|
25
|
+
defaultShardKey: string;
|
|
26
|
+
/** Runs the call in-process and returns the shard's response, unchanged. */
|
|
27
|
+
invoke: (call: NodeDispatchInvocation) => Promise<Response>;
|
|
28
|
+
/** `LUNORA_SCHEDULER_SECRET`. When set, a signed request is verified against it. */
|
|
29
|
+
schedulerSecret?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Build the dispatch route. The returned function answers any request it is
|
|
33
|
+
* handed; mounting it at {@link NODE_SCHEDULER_DISPATCH_PATH} is the caller's job.
|
|
34
|
+
* With neither secret configured every request is refused, so an unconfigured
|
|
35
|
+
* route fails closed.
|
|
36
|
+
*/
|
|
37
|
+
declare const createNodeDispatchRoute: (options: NodeDispatchRouteOptions) => ((request: Request) => Promise<Response>);
|
|
38
|
+
/** The path `@lunora/dispatch` and `@lunora/scheduler` POST to. */
|
|
39
|
+
declare const NODE_SCHEDULER_DISPATCH_PATH: "/_lunora/scheduler/dispatch";
|
|
7
40
|
/**
|
|
8
41
|
* The Node store options — the shared store options minus the two this binding
|
|
9
42
|
* owns. `dialect` is fixed (SQLite) and `exec` is the store's own connection;
|
|
@@ -14,13 +47,14 @@ type NodeGlobalContextDatabaseOptions = Omit<SqlCtxDbOptions, "dialect" | "exec"
|
|
|
14
47
|
/**
|
|
15
48
|
* Wrap a `better-sqlite3` connection as the async exec the store core consumes.
|
|
16
49
|
*
|
|
17
|
-
* `batch`
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
50
|
+
* `batch` runs the statements in order inside one `better-sqlite3`
|
|
51
|
+
* transaction, which is synchronous, so nothing else runs between them. There is
|
|
52
|
+
* no round trip to save; atomicity is the point. The store core's FTS5 search
|
|
53
|
+
* companion writes each document as an ordered list of statements, and without
|
|
54
|
+
* this two async writers interleaved at every `await` between them: one could
|
|
55
|
+
* move a document's mapping while the other's entry was mid-write, leaving an
|
|
56
|
+
* entry no later write or purge could reach. The `sqlite` dialect's contract
|
|
57
|
+
* requires an ordered, atomic `batch` for exactly that reason.
|
|
24
58
|
*/
|
|
25
59
|
declare const createNodeSqlExec: (database: Database.Database) => SqlCtxExec;
|
|
26
60
|
/** Options for {@link createNodeGlobalStore}. */
|
|
@@ -197,9 +231,12 @@ interface NodeShardHostOptions {
|
|
|
197
231
|
* `_lunora_alarm` when the host is constructed over an existing database,
|
|
198
232
|
* including an alarm whose time elapsed while nothing was running.
|
|
199
233
|
*
|
|
200
|
-
* A handler that throws is isolated to its own delivery
|
|
234
|
+
* A handler that throws is isolated to its own delivery — it never reaches
|
|
201
235
|
* the caller that set the alarm, because by then that call has long
|
|
202
|
-
* returned
|
|
236
|
+
* returned — and the wakeup is then re-delivered with backoff up to
|
|
237
|
+
* {@link ALARM_RETRY_LIMIT} times, the way workerd retries a throwing
|
|
238
|
+
* `alarm()`. Delivery is at-least-once, so the handler must tolerate
|
|
239
|
+
* running twice for one scheduled timestamp.
|
|
203
240
|
*/
|
|
204
241
|
onAlarm?: () => Promise<void> | void;
|
|
205
242
|
/**
|
|
@@ -218,25 +255,59 @@ interface NodeShardHostOptions {
|
|
|
218
255
|
/**
|
|
219
256
|
* Build a `ShardHost` over a real `better-sqlite3` database.
|
|
220
257
|
*
|
|
221
|
-
* `runSerialized`
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
258
|
+
* `runSerialized` and `transaction` share **one** boundary lock, because they
|
|
259
|
+
* write **one** `better-sqlite3` connection. `transaction` issues raw
|
|
260
|
+
* `BEGIN`/`COMMIT`/`ROLLBACK` — legal here (unlike inside a Cloudflare Durable
|
|
261
|
+
* Object, where the runtime forbids it and callers must use
|
|
262
|
+
* `storage.transaction`) because better-sqlite3 is a plain embedded database
|
|
263
|
+
* with no platform-level transaction primitive layered over it.
|
|
264
|
+
*
|
|
265
|
+
* ## Why one lane and not two
|
|
266
|
+
*
|
|
267
|
+
* This host shipped with two private tail chains, one per entry point, so each
|
|
268
|
+
* serialized against itself and neither against the other. `runSerialized`
|
|
269
|
+
* issues no `BEGIN` of its own, so one of its closures overlapping a bare
|
|
270
|
+
* `transaction()` wrote straight into that transaction's span. `assertOwnTurn`
|
|
271
|
+
* below catches the write — it is the only reason this host never silently lost
|
|
272
|
+
* one the way an unguarded connection would — but catching it is not the same
|
|
273
|
+
* as preventing it, and the refusal lands in the wrong place:
|
|
274
|
+
*
|
|
275
|
+
* 1. A `runSerialized` closure writes a row. Nothing is open, so it commits.
|
|
276
|
+
* 2. It awaits (a scheduler hop, an `onShardInit` read, any real I/O).
|
|
277
|
+
* 3. A bare `transaction()` runs `BEGIN` while it is parked. `transaction` is
|
|
278
|
+
* a public contract surface, and `./node-shard-state` re-exports it as the
|
|
279
|
+
* `storage.transaction` member `ShardDO` documents as "the platform
|
|
280
|
+
* primitive", so a subclass reaches one without going through the engine.
|
|
281
|
+
* 4. The closure resumes and its next write is refused with
|
|
282
|
+
* `SHARD_UNAVAILABLE`, so `runSerialized` **rejects** — with step 1 already
|
|
283
|
+
* durable.
|
|
227
284
|
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
* `
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
* `
|
|
238
|
-
*
|
|
239
|
-
*
|
|
285
|
+
* The boundary the contract promises is atomic-or-nothing to its caller tore in
|
|
286
|
+
* half: a rejection the caller will retry, on top of a write the retry now
|
|
287
|
+
* re-applies. That is `runSerialized`'s "no two closures interleave" guarantee
|
|
288
|
+
* broken by a `transaction` it was never serialized against.
|
|
289
|
+
*
|
|
290
|
+
* ## Why the lock is re-entrant, and why `AsyncLocalStorage` is what makes it so
|
|
291
|
+
*
|
|
292
|
+
* The engine composes the two, and stacks them: `ShardRunner.runInTransaction`
|
|
293
|
+
* is `runSerialized(() => transaction(work))` (`@lunora/shard-engine`), and
|
|
294
|
+
* `ShardDO.fetch` widens a mutation dispatch carrying `x-lunora-mutation-id`
|
|
295
|
+
* into a *further* `runSerialized` span around it (`@lunora/do`). Under two
|
|
296
|
+
* plain FIFO chains that outer span is already fatal on this host — the inner
|
|
297
|
+
* `runSerialized` waits on a `tail` that only settles when the outer closure it
|
|
298
|
+
* is running inside resolves, and because nothing ever resets that `tail`, the
|
|
299
|
+
* shard's write lane is wedged for the life of the process rather than for the
|
|
300
|
+
* life of the request.
|
|
301
|
+
*
|
|
302
|
+
* So the lock is skipped for a boundary opened from inside a boundary, and
|
|
303
|
+
* `AsyncLocalStorage` is the only thing that can tell that case from the one
|
|
304
|
+
* that must queue: its store follows a single call's own await chain without
|
|
305
|
+
* leaking into a sibling chain. A held boolean cannot — it reads `true` for an
|
|
306
|
+
* unrelated *second top-level* `transaction()` too, which would then nest a raw
|
|
307
|
+
* `BEGIN` on a connection already inside one. This is the same distinction
|
|
308
|
+
* workerd's `blockConcurrencyWhile` draws natively with "does not queue events
|
|
309
|
+
* initiated as part of the callback itself", which is what
|
|
310
|
+
* `@lunora/platform-cloudflare` leans on for the identical composition.
|
|
240
311
|
*
|
|
241
312
|
* The returned `dispose()` is this host's lifecycle owner: it clears the
|
|
242
313
|
* pending alarm `setTimeout` (so it can never fire against a connection this
|
|
@@ -310,22 +381,65 @@ interface NodeShardRegistry {
|
|
|
310
381
|
}
|
|
311
382
|
/** Build the in-process shard registry. */
|
|
312
383
|
declare const createNodeShardRegistry: (options?: NodeShardRegistryOptions) => NodeShardRegistry;
|
|
384
|
+
/** Options for {@link createNodeWorkflowHost}. */
|
|
385
|
+
interface NodeWorkflowHostOptions<Workflows extends Record<string, {
|
|
386
|
+
isLunoraWorkflow: true;
|
|
387
|
+
}>> {
|
|
388
|
+
/** Base env merged under the derived workflow bindings — surfaced to workflow bodies as `ctx.env` and used to resolve spawned children. */
|
|
389
|
+
env?: Record<string, unknown>;
|
|
390
|
+
/** How long (ms) the engine holds a cross-process lease while an activation runs, for stores that implement `acquire`. Defaults to 30000. */
|
|
391
|
+
leaseTtlMs?: number;
|
|
392
|
+
/** Where runs are persisted. Required — see the header for why there is no default. `createNodeWorkflowStore(database)` is the durable one. */
|
|
393
|
+
store: WorkflowStore;
|
|
394
|
+
/** The declared workflows keyed by their `lunora/workflows.ts` export name (e.g. `{ orderPipeline: orderPipeline }`). Values must be `defineWorkflow` results. */
|
|
395
|
+
workflows: Workflows;
|
|
396
|
+
}
|
|
397
|
+
/** A fully-wired Node workflow host. */
|
|
398
|
+
interface NodeWorkflowHost<Workflows extends Record<string, {
|
|
399
|
+
isLunoraWorkflow: true;
|
|
400
|
+
}>> {
|
|
401
|
+
/** Per-export-name `WorkflowBindingLike` — the map `ctx.workflows` consumes. */
|
|
402
|
+
readonly bindings: { [K in keyof Workflows]: WorkflowBindingLike; };
|
|
403
|
+
/**
|
|
404
|
+
* The caller's `env` plus one binding per workflow under its class name
|
|
405
|
+
* (`OrderPipelineWorkflow`), the key `ctx.exports` would carry on Cloudflare —
|
|
406
|
+
* merge this into a worker env so `ctx.spawn`/`ctx.parallel` resolve
|
|
407
|
+
* children through the same runtime.
|
|
408
|
+
*/
|
|
409
|
+
readonly env: Record<string, unknown>;
|
|
410
|
+
/** The underlying visulima runtime — `sweep`/`signal` for a dev loop or tests. */
|
|
411
|
+
readonly runtime: WorkflowRuntime;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Create a Node workflow host: compile every declared Lunora workflow onto the
|
|
415
|
+
* visulima engine, derive the workflow env, and expose the per-workflow
|
|
416
|
+
* `WorkflowBindingLike` handles.
|
|
417
|
+
*/
|
|
418
|
+
declare const createNodeWorkflowHost: <Workflows extends Record<string, {
|
|
419
|
+
isLunoraWorkflow: true;
|
|
420
|
+
}>>(options: NodeWorkflowHostOptions<Workflows>) => NodeWorkflowHost<Workflows>;
|
|
313
421
|
/** Every contract this package provides, composed for one Node process. */
|
|
314
422
|
interface NodePlatform<Queues extends Record<string, {
|
|
315
423
|
isLunoraQueue: true;
|
|
424
|
+
}> = Record<string, never>, Workflows extends Record<string, {
|
|
425
|
+
isLunoraWorkflow: true;
|
|
316
426
|
}> = Record<string, never>> {
|
|
317
427
|
/** `using platform = createNodePlatform(...)` support — delegates to `close()`. */
|
|
318
428
|
[Symbol.dispose]: () => void;
|
|
319
429
|
/** What this target supports — see `NODE_CAPABILITIES` in `@lunora/platform`. */
|
|
320
430
|
capabilities: PlatformCapabilities;
|
|
321
431
|
/**
|
|
322
|
-
* Tear this platform instance down:
|
|
323
|
-
*
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
432
|
+
* Tear this platform instance down: every resource this root built, in
|
|
433
|
+
* reverse construction order — the global store's own connection, every
|
|
434
|
+
* armed scheduler job timer, the registry's live shards, and last the shard
|
|
435
|
+
* host's pending alarm timer and its `better-sqlite3` database (and, with
|
|
436
|
+
* it, `kv`'s table — both live on the same connection). Nothing in this
|
|
437
|
+
* package closes these resources on its own — a `NodePlatform` a caller
|
|
438
|
+
* stops using without calling `close()` leaks the open file handles (plus
|
|
439
|
+
* their WAL/SHM sidecar files) and keeps the process alive on outstanding
|
|
440
|
+
* timers. Safe to call more than once. A `createNodePlatform` that throws
|
|
441
|
+
* unwinds the same list itself, so a failed construction leaks nothing and
|
|
442
|
+
* leaves nothing to call this on.
|
|
329
443
|
*
|
|
330
444
|
* **`close()` is a terminal state, not merely a cleanup step.** After it
|
|
331
445
|
* runs: `scheduler.schedule()` throws instead of arming a fresh timer
|
|
@@ -335,13 +449,25 @@ interface NodePlatform<Queues extends Record<string, {
|
|
|
335
449
|
* benign); `shard.alarms.set()`/`delete()` throw before mutating any
|
|
336
450
|
* in-memory state (checked against the connection's own open/closed state,
|
|
337
451
|
* the single source of truth); `shard.alarms.get()` keeps answering
|
|
338
|
-
* whatever it last held.
|
|
339
|
-
*
|
|
452
|
+
* whatever it last held; `sockets.accept()`/`setTag()`/`removeTag()` throw
|
|
453
|
+
* the same way, checked against the same connection state, before touching
|
|
454
|
+
* any runtime socket map or durable row. A no-op instead of a throw would
|
|
455
|
+
* be indistinguishable from a working call — exactly the silent-vanishing
|
|
340
456
|
* this lifecycle exists to end.
|
|
341
457
|
*/
|
|
342
458
|
close: () => void;
|
|
343
459
|
/** In-process shard directory (see `createNodeShardRegistry` for what it can and cannot do). */
|
|
344
460
|
directory: ShardDirectory;
|
|
461
|
+
/**
|
|
462
|
+
* The scheduler dispatch route, served in-process: `Request` in, shard
|
|
463
|
+
* response out, no listener. It answers the path `@lunora/dispatch` and
|
|
464
|
+
* `@lunora/scheduler` POST to. Pass it to `dispatchQueueBatch` as
|
|
465
|
+
* `fetchImpl` so a queue handler's `message.run` / `ctx.run` reaches the
|
|
466
|
+
* shard; the env you pass there must carry `LUNORA_ORIGIN_URL` and the same
|
|
467
|
+
* `LUNORA_ADMIN_TOKEN` as `dispatch.adminToken`. Every request is refused
|
|
468
|
+
* unless `dispatch` supplies a secret.
|
|
469
|
+
*/
|
|
470
|
+
dispatch: (request: Request) => Promise<Response>;
|
|
345
471
|
/**
|
|
346
472
|
* Wait for every promise handed to `shard.waitUntil` to settle.
|
|
347
473
|
*
|
|
@@ -351,8 +477,28 @@ interface NodePlatform<Queues extends Record<string, {
|
|
|
351
477
|
* shutdown is `await platform.drain()` then `platform.close()`.
|
|
352
478
|
*/
|
|
353
479
|
drain: () => Promise<void>;
|
|
480
|
+
/**
|
|
481
|
+
* The `.global()` backend, or `undefined` when the caller named no database
|
|
482
|
+
* file for it. There is no default path, because a global store silently
|
|
483
|
+
* rooted at `:memory:` loses every row when the process exits.
|
|
484
|
+
*
|
|
485
|
+
* **A building block, not a wiring.** Unlike its three siblings, nothing
|
|
486
|
+
* downstream of this composition root reads it: a `.global()` read or write
|
|
487
|
+
* reaches its backend through exactly one seam, `createShardCtxDb({ globalDb
|
|
488
|
+
* })`, and the only thing that passes `globalDb` is the generated `shard.ts`,
|
|
489
|
+
* from its `d1` / `hyperdriveGlobal` config thunks. `createNodePlatform`
|
|
490
|
+
* constructs no shard DO, so it cannot make that hop itself. A caller that
|
|
491
|
+
* wants `.global()` on this host makes it: after `migrate(schema)` has
|
|
492
|
+
* provisioned the tables, give the generated `createShardDO` a `d1` thunk
|
|
493
|
+
* returning `platform.globalTables.writer({ schema, … })` — `writer` builds
|
|
494
|
+
* the `createSqlCtxDb` facade that seam expects, and `node-platform.test.ts`
|
|
495
|
+
* round-trips a row through exactly that object.
|
|
496
|
+
*/
|
|
497
|
+
globalTables?: NodeGlobalStore;
|
|
354
498
|
/** Durable key-value storage backed by the same `better-sqlite3` database as `shard`. */
|
|
355
499
|
kv: ShardKvStore;
|
|
500
|
+
/** The local-filesystem bucket, or `undefined` when the caller named no bucket directory. */
|
|
501
|
+
objectStorage?: R2BucketLike;
|
|
356
502
|
/**
|
|
357
503
|
* The declared queues, or `undefined` when the caller declared none.
|
|
358
504
|
*
|
|
@@ -367,6 +513,28 @@ interface NodePlatform<Queues extends Record<string, {
|
|
|
367
513
|
shard: ShardHost;
|
|
368
514
|
/** Socket registry with mutable tags and SQLite-persisted attachments. */
|
|
369
515
|
sockets: SocketHost;
|
|
516
|
+
/**
|
|
517
|
+
* The declared workflows, or `undefined` when the caller declared none.
|
|
518
|
+
* Runs are persisted to the same `better-sqlite3` database as the shard, so
|
|
519
|
+
* they survive a restart without a second store to configure.
|
|
520
|
+
*/
|
|
521
|
+
workflows?: NodeWorkflowHost<Workflows>;
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* The secrets the dispatch route authenticates with. With neither, the route
|
|
525
|
+
* refuses everything.
|
|
526
|
+
*
|
|
527
|
+
* Queue and topic delivery authenticates with `adminToken` alone:
|
|
528
|
+
* `@lunora/dispatch` always sends `Bearer <LUNORA_ADMIN_TOKEN>` and never signs,
|
|
529
|
+
* so the value here must match the token the caller puts on the env it passes
|
|
530
|
+
* `dispatchQueueBatch`. `schedulerSecret` only verifies a signed body, which the
|
|
531
|
+
* scheduler's own delivery sends; queue delivery never does.
|
|
532
|
+
*/
|
|
533
|
+
interface NodePlatformDispatchOptions {
|
|
534
|
+
/** `LUNORA_ADMIN_TOKEN`. The bearer queue and topic delivery sends, so it must match the caller's env. */
|
|
535
|
+
adminToken?: string;
|
|
536
|
+
/** `LUNORA_SCHEDULER_SECRET`. When set, a signed body is verified against it. Queue delivery does not sign. */
|
|
537
|
+
schedulerSecret?: string;
|
|
370
538
|
}
|
|
371
539
|
/**
|
|
372
540
|
* Options for {@link createNodePlatform} — the shard host's (`path`,
|
|
@@ -376,10 +544,31 @@ interface NodePlatform<Queues extends Record<string, {
|
|
|
376
544
|
* or job with nowhere to land is bookkeeping. `directory` makes the shards the
|
|
377
545
|
* directory resolves for fan-out file-backed too, and `onAlarm` gives their
|
|
378
546
|
* durable alarms somewhere to land.
|
|
547
|
+
*
|
|
548
|
+
* `queues`, `workflows`, `objectStorageDirectory` and `globalTablesPath` are the
|
|
549
|
+
* four declarations — each is absent from the returned platform when omitted.
|
|
379
550
|
*/
|
|
380
551
|
type NodePlatformOptions<Queues extends Record<string, {
|
|
381
552
|
isLunoraQueue: true;
|
|
553
|
+
}> = Record<string, never>, Workflows extends Record<string, {
|
|
554
|
+
isLunoraWorkflow: true;
|
|
382
555
|
}> = Record<string, never>> = {
|
|
556
|
+
/** Secrets for the dispatch route. Omit to leave the route refusing every request. */
|
|
557
|
+
dispatch?: NodePlatformDispatchOptions;
|
|
558
|
+
/**
|
|
559
|
+
* Database file the `.global()` tables live in — its own file, never a
|
|
560
|
+
* shard's, because a table every shard reads must not be inside any one of
|
|
561
|
+
* them. Omit when the app declares no `.global()` table; there is no
|
|
562
|
+
* default, for the same reason the bucket has none.
|
|
563
|
+
*/
|
|
564
|
+
globalTablesPath?: string;
|
|
565
|
+
/**
|
|
566
|
+
* Directory the object-storage bucket keeps its objects in, one file per
|
|
567
|
+
* key. Omit when the app declares no buckets — there is no default,
|
|
568
|
+
* because a bucket silently rooted at the process's working directory is
|
|
569
|
+
* worse than an absent one.
|
|
570
|
+
*/
|
|
571
|
+
objectStorageDirectory?: string;
|
|
383
572
|
/**
|
|
384
573
|
* Deliver one assembled queue batch — wire this to `dispatchQueueBatch`.
|
|
385
574
|
* Required alongside `queues`; without it the messages would be stored
|
|
@@ -388,11 +577,22 @@ type NodePlatformOptions<Queues extends Record<string, {
|
|
|
388
577
|
onQueueBatch?: NodeQueueHostOptions<Queues>["onBatch"];
|
|
389
578
|
/** The app's `defineQueue` results, keyed by export name. Omit when the app declares no queues. */
|
|
390
579
|
queues?: Queues;
|
|
580
|
+
/** The app's `defineWorkflow` results, keyed by export name. Omit when the app declares no workflows. */
|
|
581
|
+
workflows?: Workflows;
|
|
391
582
|
} & NodeSchedulerHostOptions & NodeShardHostOptions & NodeShardRegistryOptions;
|
|
392
583
|
/** Compose every contract this package provides over one `better-sqlite3` database. */
|
|
393
584
|
declare const createNodePlatform: <Queues extends Record<string, {
|
|
394
585
|
isLunoraQueue: true;
|
|
395
|
-
}> = Record<string, never
|
|
586
|
+
}> = Record<string, never>, Workflows extends Record<string, {
|
|
587
|
+
isLunoraWorkflow: true;
|
|
588
|
+
}> = Record<string, never>>(options?: NodePlatformOptions<Queues, Workflows>) => NodePlatform<Queues, Workflows>;
|
|
589
|
+
interface NodeProfileHandlerOptions {
|
|
590
|
+
/** The bearer a caller must present. Required and non-empty: an empty token would make the endpoint open to anyone. */
|
|
591
|
+
token: string;
|
|
592
|
+
}
|
|
593
|
+
type NodeProfileHandler = (request: Request) => Promise<Response>;
|
|
594
|
+
/** The public handler: the real profiler, guarded by `token`. */
|
|
595
|
+
declare const createNodeProfileHandler: ({ token }: NodeProfileHandlerOptions) => NodeProfileHandler;
|
|
396
596
|
/** Options for {@link createNodeR2Bucket}. */
|
|
397
597
|
interface NodeR2BucketOptions {
|
|
398
598
|
/** The bucket directory — created on first write. Objects live here, one file per key. */
|
|
@@ -400,7 +600,7 @@ interface NodeR2BucketOptions {
|
|
|
400
600
|
}
|
|
401
601
|
/**
|
|
402
602
|
* Create an `R2BucketLike` over the local filesystem. Any object shape
|
|
403
|
-
* `createStorage({ bucket })` accepts — `put`/`get`/`head`/`delete`/`list` —
|
|
603
|
+
* `createStorage({ bucket, bucketName })` accepts — `put`/`get`/`head`/`delete`/`list` —
|
|
404
604
|
* maps directly onto a file operation.
|
|
405
605
|
*/
|
|
406
606
|
declare const createNodeR2Bucket: (options: NodeR2BucketOptions) => R2BucketLike;
|
|
@@ -473,48 +673,12 @@ interface NodeSocketHost {
|
|
|
473
673
|
}
|
|
474
674
|
/** Build the socket registry, persisting attachments and tags to `database`. */
|
|
475
675
|
declare const createNodeSocketHost: (database: Database.Database) => NodeSocketHost;
|
|
476
|
-
/** Options for {@link createNodeWorkflowHost}. */
|
|
477
|
-
interface NodeWorkflowHostOptions<Workflows extends Record<string, {
|
|
478
|
-
isLunoraWorkflow: true;
|
|
479
|
-
}>> {
|
|
480
|
-
/** Base env merged under the derived `WORKFLOW_*` bindings — surfaced to workflow bodies as `ctx.env` and used to resolve spawned children. */
|
|
481
|
-
env?: Record<string, unknown>;
|
|
482
|
-
/** How long (ms) the engine holds a cross-process lease while an activation runs, for stores that implement `acquire`. Defaults to 30000. */
|
|
483
|
-
leaseTtlMs?: number;
|
|
484
|
-
/** Where runs are persisted. Required — see the header for why there is no default. `createNodeWorkflowStore(database)` is the durable one. */
|
|
485
|
-
store: WorkflowStore;
|
|
486
|
-
/** The declared workflows keyed by their `lunora/workflows.ts` export name (e.g. `{ orderPipeline: orderPipeline }`). Values must be `defineWorkflow` results. */
|
|
487
|
-
workflows: Workflows;
|
|
488
|
-
}
|
|
489
|
-
/** A fully-wired Node workflow host. */
|
|
490
|
-
interface NodeWorkflowHost<Workflows extends Record<string, {
|
|
491
|
-
isLunoraWorkflow: true;
|
|
492
|
-
}>> {
|
|
493
|
-
/** Per-export-name `WorkflowBindingLike` — the map `ctx.workflows` consumes. */
|
|
494
|
-
readonly bindings: { [K in keyof Workflows]: WorkflowBindingLike; };
|
|
495
|
-
/**
|
|
496
|
-
* The caller's `env` plus one `WORKFLOW_<EXPORT>` binding per workflow —
|
|
497
|
-
* merge this into a worker env so `ctx.spawn`/`ctx.parallel` resolve
|
|
498
|
-
* children through the same runtime.
|
|
499
|
-
*/
|
|
500
|
-
readonly env: Record<string, unknown>;
|
|
501
|
-
/** The underlying visulima runtime — `sweep`/`signal` for a dev loop or tests. */
|
|
502
|
-
readonly runtime: WorkflowRuntime;
|
|
503
|
-
}
|
|
504
|
-
/**
|
|
505
|
-
* Create a Node workflow host: compile every declared Lunora workflow onto the
|
|
506
|
-
* visulima engine, derive the `WORKFLOW_*` env, and expose the per-workflow
|
|
507
|
-
* `WorkflowBindingLike` handles.
|
|
508
|
-
*/
|
|
509
|
-
declare const createNodeWorkflowHost: <Workflows extends Record<string, {
|
|
510
|
-
isLunoraWorkflow: true;
|
|
511
|
-
}>>(options: NodeWorkflowHostOptions<Workflows>) => NodeWorkflowHost<Workflows>;
|
|
512
676
|
/**
|
|
513
677
|
* Build a durable {@link WorkflowStore} over a `better-sqlite3` connection.
|
|
514
678
|
* Pass the result as `createNodeWorkflowHost({ store })`.
|
|
515
679
|
*/
|
|
516
680
|
declare const createNodeWorkflowStore: (database: Database.Database) => WorkflowStore;
|
|
517
|
-
export {
|
|
681
|
+
export { NODE_SCHEDULER_DISPATCH_PATH,
|
|
518
682
|
/**
|
|
519
683
|
* `@lunora/platform-node` — a Node implementation of the `@lunora/platform`
|
|
520
684
|
* host contracts (`ShardHost`, `SocketHost`, `ShardDirectory`, `ShardKvStore`,
|
|
@@ -534,49 +698,29 @@ export {
|
|
|
534
698
|
* TCK, `@lunora/shard-engine/conformance`'s engine suite, and its own
|
|
535
699
|
* lifecycle/global-store tests.
|
|
536
700
|
*
|
|
537
|
-
* **
|
|
538
|
-
* (`@lunora/platform`):
|
|
539
|
-
*
|
|
540
|
-
*
|
|
541
|
-
*
|
|
542
|
-
*
|
|
543
|
-
*
|
|
544
|
-
*
|
|
545
|
-
* a `@lunora/config` deploy driver makes `--target node` resolve. See
|
|
546
|
-
* `plans/234-node-host-findings.md`.
|
|
547
|
-
*/
|
|
548
|
-
type NodeGlobalContextDatabaseOptions,
|
|
549
|
-
/**
|
|
550
|
-
* `@lunora/platform-node` — a Node implementation of the `@lunora/platform`
|
|
551
|
-
* host contracts (`ShardHost`, `SocketHost`, `ShardDirectory`, `ShardKvStore`,
|
|
552
|
-
* `SchedulerHost`) over `better-sqlite3` and an in-process socket/directory/
|
|
553
|
-
* scheduler registry.
|
|
701
|
+
* **Emulated here**, and rated accordingly in `NODE_CAPABILITIES`
|
|
702
|
+
* (`@lunora/platform`): queues over a durable table (`./node-queue-host`), R2
|
|
703
|
+
* buckets over the local filesystem (`./node-r2-bucket`), workflows over the
|
|
704
|
+
* `@visulima/workflow` engine (`./node-workflow-host`), and cross-shard fan-out
|
|
705
|
+
* via `@lunora/runtime`'s query coordinator over the in-process shard registry.
|
|
706
|
+
* `createNodePlatform` binds all three declarations (`queues`, `workflows`,
|
|
707
|
+
* `objectStorageDirectory`) — a capability rated `emulated` with nothing bound
|
|
708
|
+
* is the one combination that fails at runtime with no diagnostic before it.
|
|
554
709
|
*
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
*
|
|
558
|
-
*
|
|
559
|
-
*
|
|
560
|
-
* `@lunora/sql-store` core (`./node-global-store`). Each of those is pinned by
|
|
561
|
-
* a restart test — a second host over the same database file — not only by the
|
|
562
|
-
* TCK's simulated recycle.
|
|
710
|
+
* **Still missing:** a dev server. There is no `lunora dev --target node`, and
|
|
711
|
+
* nothing here owns a timer, so queue delivery is driven by an explicit
|
|
712
|
+
* `poll()`. Also absent are the Cloudflare product bindings with no local
|
|
713
|
+
* equivalent — Vectorize, Workers AI, Browser Rendering, Containers, Analytics
|
|
714
|
+
* Engine, Pipelines, Secrets Store, Hyperdrive.
|
|
563
715
|
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
*
|
|
567
|
-
*
|
|
568
|
-
*
|
|
569
|
-
* (`@lunora/platform`): a dev server — so `lunora dev --target node` does not
|
|
570
|
-
* exist yet — plus most Cloudflare product bindings (Queues, Vectorize, Workers
|
|
571
|
-
* AI, Browser Rendering, Containers, Analytics Engine, Pipelines, Secrets Store,
|
|
572
|
-
* Hyperdrive). R2 buckets are emulated over the local filesystem
|
|
573
|
-
* (`./node-r2-bucket`) and Workflows over the `@visulima/workflow` engine
|
|
574
|
-
* (`./node-workflow-host`). Cross-shard fan-out is emulated via
|
|
575
|
-
* `@lunora/runtime`'s query coordinator over the in-process shard registry, and
|
|
576
|
-
* a `@lunora/config` deploy driver makes `--target node` resolve. See
|
|
716
|
+
* `@lunora/config` ships a `node` **deploy** driver, so `--target node` resolves
|
|
717
|
+
* for `provision` — which reports, once, which declared features this target
|
|
718
|
+
* cannot serve, and writes nothing: there is no hosted control plane to deploy
|
|
719
|
+
* to and no `wrangler`-equivalent to shell out to. A deploy driver is not a dev
|
|
720
|
+
* server, which is why both statements above hold at once. See
|
|
577
721
|
* `plans/234-node-host-findings.md`.
|
|
578
722
|
*/
|
|
579
|
-
type
|
|
723
|
+
type NodeDispatchInvocation,
|
|
580
724
|
/**
|
|
581
725
|
* `@lunora/platform-node` — a Node implementation of the `@lunora/platform`
|
|
582
726
|
* host contracts (`ShardHost`, `SocketHost`, `ShardDirectory`, `ShardKvStore`,
|
|
@@ -596,15 +740,26 @@ type NodeGlobalStore,
|
|
|
596
740
|
* TCK, `@lunora/shard-engine/conformance`'s engine suite, and its own
|
|
597
741
|
* lifecycle/global-store tests.
|
|
598
742
|
*
|
|
599
|
-
* **
|
|
600
|
-
* (`@lunora/platform`):
|
|
601
|
-
*
|
|
602
|
-
*
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
*
|
|
606
|
-
*
|
|
607
|
-
*
|
|
743
|
+
* **Emulated here**, and rated accordingly in `NODE_CAPABILITIES`
|
|
744
|
+
* (`@lunora/platform`): queues over a durable table (`./node-queue-host`), R2
|
|
745
|
+
* buckets over the local filesystem (`./node-r2-bucket`), workflows over the
|
|
746
|
+
* `@visulima/workflow` engine (`./node-workflow-host`), and cross-shard fan-out
|
|
747
|
+
* via `@lunora/runtime`'s query coordinator over the in-process shard registry.
|
|
748
|
+
* `createNodePlatform` binds all three declarations (`queues`, `workflows`,
|
|
749
|
+
* `objectStorageDirectory`) — a capability rated `emulated` with nothing bound
|
|
750
|
+
* is the one combination that fails at runtime with no diagnostic before it.
|
|
751
|
+
*
|
|
752
|
+
* **Still missing:** a dev server. There is no `lunora dev --target node`, and
|
|
753
|
+
* nothing here owns a timer, so queue delivery is driven by an explicit
|
|
754
|
+
* `poll()`. Also absent are the Cloudflare product bindings with no local
|
|
755
|
+
* equivalent — Vectorize, Workers AI, Browser Rendering, Containers, Analytics
|
|
756
|
+
* Engine, Pipelines, Secrets Store, Hyperdrive.
|
|
757
|
+
*
|
|
758
|
+
* `@lunora/config` ships a `node` **deploy** driver, so `--target node` resolves
|
|
759
|
+
* for `provision` — which reports, once, which declared features this target
|
|
760
|
+
* cannot serve, and writes nothing: there is no hosted control plane to deploy
|
|
761
|
+
* to and no `wrangler`-equivalent to shell out to. A deploy driver is not a dev
|
|
762
|
+
* server, which is why both statements above hold at once. See
|
|
608
763
|
* `plans/234-node-host-findings.md`.
|
|
609
764
|
*/
|
|
610
|
-
type NodeGlobalStoreOptions, type NodePlatform, type NodePlatformOptions, type NodeQueueHost, type NodeQueueHostOptions, type NodeR2BucketOptions, type NodeSchedulerHost, type NodeSchedulerHostOptions, type NodeShard, type NodeShardHostOptions, type NodeShardRegistry, type NodeShardRegistryOptions, type NodeShardState, type NodeSocketHost, type NodeWorkflowHost, type NodeWorkflowHostOptions, createNodeGlobalStore, createNodePlatform, createNodeQueueHost, createNodeR2Bucket, createNodeSchedulerHost, createNodeShardHost, createNodeShardKvStore, createNodeShardRegistry, createNodeShardState, createNodeSocketHost, createNodeSqlExec, createNodeWorkflowHost, createNodeWorkflowStore };
|
|
765
|
+
type NodeDispatchRouteOptions, type NodeGlobalContextDatabaseOptions, type NodeGlobalStore, type NodeGlobalStoreOptions, type NodePlatform, type NodePlatformDispatchOptions, type NodePlatformOptions, type NodeProfileHandler, type NodeProfileHandlerOptions, type NodeQueueHost, type NodeQueueHostOptions, type NodeR2BucketOptions, type NodeSchedulerHost, type NodeSchedulerHostOptions, type NodeShard, type NodeShardHostOptions, type NodeShardRegistry, type NodeShardRegistryOptions, type NodeShardState, type NodeSocketHost, type NodeWorkflowHost, type NodeWorkflowHostOptions, createNodeDispatchRoute, createNodeGlobalStore, createNodePlatform, createNodeProfileHandler, createNodeQueueHost, createNodeR2Bucket, createNodeSchedulerHost, createNodeShardHost, createNodeShardKvStore, createNodeShardRegistry, createNodeShardState, createNodeSocketHost, createNodeSqlExec, createNodeWorkflowHost, createNodeWorkflowStore };
|