@moku-labs/worker 0.3.0 → 0.4.0
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/dist/cli.cjs +367 -73
- package/dist/cli.d.cts +29 -0
- package/dist/cli.d.mts +29 -0
- package/dist/cli.mjs +367 -73
- package/dist/{config-Bj3GUJT_.d.cts → config-50jmPyMv.d.cts} +9 -0
- package/dist/{config-Bj3GUJT_.d.mts → config-50jmPyMv.d.mts} +9 -0
- package/dist/index.cjs +84 -1
- package/dist/index.d.cts +61 -35
- package/dist/index.d.mts +60 -34
- package/dist/index.mjs +84 -1
- package/package.json +1 -1
|
@@ -33,6 +33,15 @@ type WorkerEvents = {
|
|
|
33
33
|
kind: "kv" | "r2" | "d1" | "queue" | "do";
|
|
34
34
|
name: string;
|
|
35
35
|
};
|
|
36
|
+
"provision:plan": {
|
|
37
|
+
exists: number;
|
|
38
|
+
missing: number;
|
|
39
|
+
account: string;
|
|
40
|
+
};
|
|
41
|
+
"provision:skip": {
|
|
42
|
+
kind: "kv" | "r2" | "d1" | "queue" | "do";
|
|
43
|
+
name: string;
|
|
44
|
+
};
|
|
36
45
|
};
|
|
37
46
|
/**
|
|
38
47
|
* Worker-bound plugin context for Layer-3 consumer plugins. Aliases the core
|
package/dist/index.cjs
CHANGED
|
@@ -482,7 +482,90 @@ const serverPlugin = require_storage.createPlugin("server", {
|
|
|
482
482
|
},
|
|
483
483
|
helpers: { endpoint }
|
|
484
484
|
});
|
|
485
|
-
|
|
485
|
+
//#endregion
|
|
486
|
+
//#region src/index.ts
|
|
487
|
+
/**
|
|
488
|
+
* @file `@moku-labs/worker` — server-side Cloudflare Workers app + deploy framework on `@moku-labs/core`.
|
|
489
|
+
*
|
|
490
|
+
* The package root exports the bound {@link createApp} factory (the Layer-3 entry
|
|
491
|
+
* point), {@link createPlugin} for consumer plugins, every runtime plugin instance,
|
|
492
|
+
* the `server`/`durable-objects` helpers, and the framework types. Node-only tooling
|
|
493
|
+
* (`deploy`, `cli`) ships from the separate `@moku-labs/worker/cli` entry, never here.
|
|
494
|
+
*
|
|
495
|
+
* `createApp(options?)` boots a fully-typed, synchronous, per-isolate app. The
|
|
496
|
+
* framework defaults `[logPlugin, envPlugin, stagePlugin, bindingsPlugin, serverPlugin]`
|
|
497
|
+
* are applied first, then the `options` below are shallow-merged on top:
|
|
498
|
+
*
|
|
499
|
+
* - `config` — `Partial<WorkerConfig>`; defaults `{ stage: "production", name: "moku-worker", compatibilityDate: "" }`.
|
|
500
|
+
* `config.stage` is the single stage source: the framework mirrors it into the `stage` core
|
|
501
|
+
* plugin so `ctx.stage.*` / `app.stage.*` stay in lockstep with `ctx.global.stage` (see {@link createApp}).
|
|
502
|
+
* - `pluginConfigs` — per-plugin config overrides keyed by plugin name (e.g. `server.endpoints`, `bindings.required`); default `{}`.
|
|
503
|
+
* - `plugins` — extra `PluginInstance[]` appended to the defaults; default `[]`. Do NOT re-list a default plugin.
|
|
504
|
+
* - `onReady` — optional `(app) => void`, runs after every plugin's `onInit`.
|
|
505
|
+
* - `onError` — optional `(error) => void` boot/lifecycle error handler.
|
|
506
|
+
* - `onStart` / `onStop` — optional `() => void | Promise<void>` runtime lifecycle hooks (`app.start()` / `app.stop()`).
|
|
507
|
+
*
|
|
508
|
+
* Re-listing a default plugin name in `plugins` throws
|
|
509
|
+
* `TypeError: [moku-worker] Duplicate plugin name: "<name>"` — `bindings` and `server`
|
|
510
|
+
* are already wired, so consumers list only the resource plugins they add (`kv`, `d1`, …).
|
|
511
|
+
*
|
|
512
|
+
* Minimal HTTP Worker (shape taken from the server integration test):
|
|
513
|
+
*
|
|
514
|
+
* ```typescript
|
|
515
|
+
* import { createApp, endpoint } from "@moku-labs/worker";
|
|
516
|
+
*
|
|
517
|
+
* export const app = createApp({
|
|
518
|
+
* config: { name: "my-worker", compatibilityDate: "2024-09-23" },
|
|
519
|
+
* pluginConfigs: {
|
|
520
|
+
* server: { endpoints: [endpoint("/health").get(() => new Response("ok"))] }
|
|
521
|
+
* }
|
|
522
|
+
* });
|
|
523
|
+
*
|
|
524
|
+
* // worker.ts — the default export is hand-assembled; no plugin produces it.
|
|
525
|
+
* export default {
|
|
526
|
+
* fetch: (request: Request, env: Record<string, unknown>, ctx: ExecutionContext) =>
|
|
527
|
+
* app.server.handle(request, env, ctx)
|
|
528
|
+
* } satisfies ExportedHandler;
|
|
529
|
+
* ```
|
|
530
|
+
*/
|
|
531
|
+
const framework = require_storage.createCore(require_storage.coreConfig, { plugins: [require_storage.bindingsPlugin, serverPlugin] });
|
|
532
|
+
const { createPlugin } = framework;
|
|
533
|
+
/** The core-bound app factory; wrapped by {@link createApp} to bridge `config.stage`. */
|
|
534
|
+
const boundCreateApp = framework.createApp;
|
|
535
|
+
/**
|
|
536
|
+
* Boots a fully-typed, synchronous, per-isolate Worker app — the Layer-3 entry point.
|
|
537
|
+
*
|
|
538
|
+
* Wraps the core-bound factory to BRIDGE the single consumer-facing `config.stage`
|
|
539
|
+
* into the `stage` core plugin's own config, so the typed accessors
|
|
540
|
+
* (`ctx.stage.isDev()` / `app.stage.current()` / …) can never diverge from the
|
|
541
|
+
* global `ctx.global.stage`. Global config and core-plugin config resolve on two
|
|
542
|
+
* SEPARATE cascades (spec/05 §1b), and a core plugin cannot read global config (its
|
|
543
|
+
* context is `{ config, state }` only — spec/02 §6). `createApp` is the only layer
|
|
544
|
+
* that sees the consumer's chosen stage, so it mirrors `config.stage` into the stage
|
|
545
|
+
* plugin's level-4 `pluginConfigs` override (`WorkerConfig.stage → pluginConfigs.stage.stage`).
|
|
546
|
+
* When `config.stage` is omitted, the global config and the stage plugin both fall back
|
|
547
|
+
* to their identical `"production"` default. See the module JSDoc above for the full
|
|
548
|
+
* options/defaults table.
|
|
549
|
+
*
|
|
550
|
+
* @param options - The createApp options (`config`, `pluginConfigs`, `plugins`, and lifecycle callbacks).
|
|
551
|
+
* @returns The initialized app — every plugin's `onInit` has already run.
|
|
552
|
+
* @example
|
|
553
|
+
* ```typescript
|
|
554
|
+
* const app = createApp({ config: { stage: "development", name: "my-worker" } });
|
|
555
|
+
* app.stage.isDev(); // true — bridged from config.stage
|
|
556
|
+
* ```
|
|
557
|
+
*/
|
|
558
|
+
const createApp = (options) => {
|
|
559
|
+
const explicitStage = options?.config?.stage;
|
|
560
|
+
if (explicitStage === void 0) return boundCreateApp(options);
|
|
561
|
+
return boundCreateApp({
|
|
562
|
+
...options,
|
|
563
|
+
pluginConfigs: {
|
|
564
|
+
...options?.pluginConfigs,
|
|
565
|
+
stage: { stage: explicitStage }
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
};
|
|
486
569
|
//#endregion
|
|
487
570
|
exports.bindingsPlugin = require_storage.bindingsPlugin;
|
|
488
571
|
exports.createApp = createApp;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { i as WorkerPluginCtx, n as WorkerEnv, r as WorkerEvents, t as WorkerConfig } from "./config-
|
|
2
|
-
import { PluginCtx, PluginCtx as PluginCtx$1, PluginInstance } from "@moku-labs/core";
|
|
1
|
+
import { i as WorkerPluginCtx, n as WorkerEnv, r as WorkerEvents, t as WorkerConfig } from "./config-50jmPyMv.cjs";
|
|
3
2
|
import { envPlugin, logPlugin } from "@moku-labs/common";
|
|
3
|
+
import { PluginCtx, PluginCtx as PluginCtx$1, PluginInstance } from "@moku-labs/core";
|
|
4
4
|
|
|
5
5
|
//#region \0rolldown/runtime.js
|
|
6
6
|
declare namespace types_d_exports {
|
|
@@ -994,38 +994,64 @@ declare const stagePlugin: import("@moku-labs/core").CorePluginInstance<"stage",
|
|
|
994
994
|
}>;
|
|
995
995
|
//#endregion
|
|
996
996
|
//#region src/index.d.ts
|
|
997
|
-
declare const
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
997
|
+
declare const createPlugin: import("@moku-labs/core").BoundCreatePluginFunction<WorkerConfig, WorkerEvents, import("@moku-labs/core").CoreApisFromTuple<[import("@moku-labs/core").CorePluginInstance<"log", import("@moku-labs/common").LogConfig, import("@moku-labs/common").LogState, import("@moku-labs/common").LogApi>, import("@moku-labs/core").CorePluginInstance<"env", import("@moku-labs/common").EnvConfig, import("@moku-labs/common").EnvState, import("@moku-labs/common").EnvApi>, import("@moku-labs/core").CorePluginInstance<"stage", {
|
|
998
|
+
stage: "production" | "development" | "test";
|
|
999
|
+
}, Record<string, never>, {
|
|
1000
|
+
isDev: () => boolean;
|
|
1001
|
+
isProduction: () => boolean;
|
|
1002
|
+
current: () => "production" | "development" | "test";
|
|
1003
|
+
}>]>>;
|
|
1004
|
+
/** The core-bound app factory; wrapped by {@link createApp} to bridge `config.stage`. */
|
|
1005
|
+
declare const boundCreateApp: <const ExtraPlugins extends readonly import("@moku-labs/core").AnyPluginInstance[] = readonly []>(options?: import("@moku-labs/core").CreateAppOptions<WorkerConfig, WorkerEvents, (import("@moku-labs/core").PluginInstance<"bindings", Config$4, Record<string, never>, BindingsApi, {}> & Record<never, never>) | (import("@moku-labs/core").PluginInstance<"server", ServerConfig, ServerState, Api$3, {
|
|
1006
|
+
"server:matched": {
|
|
1007
|
+
path: string;
|
|
1008
|
+
method: string;
|
|
1009
|
+
};
|
|
1010
|
+
}> & {
|
|
1011
|
+
endpoint: <Path extends string>(path: Path) => EndpointBuilder<Path>;
|
|
1012
|
+
}) | ExtraPlugins[number], [...ExtraPlugins], import("@moku-labs/core").CoreApisFromTuple<[import("@moku-labs/core").CorePluginInstance<"log", import("@moku-labs/common").LogConfig, import("@moku-labs/common").LogState, import("@moku-labs/common").LogApi>, import("@moku-labs/core").CorePluginInstance<"env", import("@moku-labs/common").EnvConfig, import("@moku-labs/common").EnvState, import("@moku-labs/common").EnvApi>, import("@moku-labs/core").CorePluginInstance<"stage", {
|
|
1013
|
+
stage: "production" | "development" | "test";
|
|
1014
|
+
}, Record<string, never>, {
|
|
1015
|
+
isDev: () => boolean;
|
|
1016
|
+
isProduction: () => boolean;
|
|
1017
|
+
current: () => "production" | "development" | "test";
|
|
1018
|
+
}>]>> | undefined) => import("@moku-labs/core").App<WorkerConfig, WorkerEvents, (import("@moku-labs/core").PluginInstance<"bindings", Config$4, Record<string, never>, BindingsApi, {}> & Record<never, never>) | (import("@moku-labs/core").PluginInstance<"server", ServerConfig, ServerState, Api$3, {
|
|
1019
|
+
"server:matched": {
|
|
1020
|
+
path: string;
|
|
1021
|
+
method: string;
|
|
1022
|
+
};
|
|
1023
|
+
}> & {
|
|
1024
|
+
endpoint: <Path extends string>(path: Path) => EndpointBuilder<Path>;
|
|
1025
|
+
}) | ExtraPlugins[number], import("@moku-labs/core").CoreApisFromTuple<[import("@moku-labs/core").CorePluginInstance<"log", import("@moku-labs/common").LogConfig, import("@moku-labs/common").LogState, import("@moku-labs/common").LogApi>, import("@moku-labs/core").CorePluginInstance<"env", import("@moku-labs/common").EnvConfig, import("@moku-labs/common").EnvState, import("@moku-labs/common").EnvApi>, import("@moku-labs/core").CorePluginInstance<"stage", {
|
|
1026
|
+
stage: "production" | "development" | "test";
|
|
1027
|
+
}, Record<string, never>, {
|
|
1028
|
+
isDev: () => boolean;
|
|
1029
|
+
isProduction: () => boolean;
|
|
1030
|
+
current: () => "production" | "development" | "test";
|
|
1031
|
+
}>]>>;
|
|
1032
|
+
/**
|
|
1033
|
+
* Boots a fully-typed, synchronous, per-isolate Worker app — the Layer-3 entry point.
|
|
1034
|
+
*
|
|
1035
|
+
* Wraps the core-bound factory to BRIDGE the single consumer-facing `config.stage`
|
|
1036
|
+
* into the `stage` core plugin's own config, so the typed accessors
|
|
1037
|
+
* (`ctx.stage.isDev()` / `app.stage.current()` / …) can never diverge from the
|
|
1038
|
+
* global `ctx.global.stage`. Global config and core-plugin config resolve on two
|
|
1039
|
+
* SEPARATE cascades (spec/05 §1b), and a core plugin cannot read global config (its
|
|
1040
|
+
* context is `{ config, state }` only — spec/02 §6). `createApp` is the only layer
|
|
1041
|
+
* that sees the consumer's chosen stage, so it mirrors `config.stage` into the stage
|
|
1042
|
+
* plugin's level-4 `pluginConfigs` override (`WorkerConfig.stage → pluginConfigs.stage.stage`).
|
|
1043
|
+
* When `config.stage` is omitted, the global config and the stage plugin both fall back
|
|
1044
|
+
* to their identical `"production"` default. See the module JSDoc above for the full
|
|
1045
|
+
* options/defaults table.
|
|
1046
|
+
*
|
|
1047
|
+
* @param options - The createApp options (`config`, `pluginConfigs`, `plugins`, and lifecycle callbacks).
|
|
1048
|
+
* @returns The initialized app — every plugin's `onInit` has already run.
|
|
1049
|
+
* @example
|
|
1050
|
+
* ```typescript
|
|
1051
|
+
* const app = createApp({ config: { stage: "development", name: "my-worker" } });
|
|
1052
|
+
* app.stage.isDev(); // true — bridged from config.stage
|
|
1053
|
+
* ```
|
|
1054
|
+
*/
|
|
1055
|
+
declare const createApp: typeof boundCreateApp;
|
|
1030
1056
|
//#endregion
|
|
1031
1057
|
export { type types_d_exports as Bindings, type types_d_exports$1 as D1, type types_d_exports$2 as DurableObjects, type PluginCtx, type types_d_exports$3 as Queues, type types_d_exports$4 as Server, type StageApi, type types_d_exports$5 as Storage, type WorkerConfig, type WorkerEnv, type WorkerEvents, type WorkerPluginCtx, bindingsPlugin, createApp, createPlugin, d1Plugin, defineDurableObject, durableObjectsPlugin, endpoint, envPlugin, kvPlugin, logPlugin, queuesPlugin, serverPlugin, stagePlugin, storagePlugin };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as WorkerPluginCtx, n as WorkerEnv, r as WorkerEvents, t as WorkerConfig } from "./config-
|
|
1
|
+
import { i as WorkerPluginCtx, n as WorkerEnv, r as WorkerEvents, t as WorkerConfig } from "./config-50jmPyMv.mjs";
|
|
2
2
|
import { envPlugin, logPlugin } from "@moku-labs/common";
|
|
3
3
|
import { PluginCtx, PluginCtx as PluginCtx$1, PluginInstance } from "@moku-labs/core";
|
|
4
4
|
|
|
@@ -994,38 +994,64 @@ declare const stagePlugin: import("@moku-labs/core").CorePluginInstance<"stage",
|
|
|
994
994
|
}>;
|
|
995
995
|
//#endregion
|
|
996
996
|
//#region src/index.d.ts
|
|
997
|
-
declare const
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
997
|
+
declare const createPlugin: import("@moku-labs/core").BoundCreatePluginFunction<WorkerConfig, WorkerEvents, import("@moku-labs/core").CoreApisFromTuple<[import("@moku-labs/core").CorePluginInstance<"log", import("@moku-labs/common").LogConfig, import("@moku-labs/common").LogState, import("@moku-labs/common").LogApi>, import("@moku-labs/core").CorePluginInstance<"env", import("@moku-labs/common").EnvConfig, import("@moku-labs/common").EnvState, import("@moku-labs/common").EnvApi>, import("@moku-labs/core").CorePluginInstance<"stage", {
|
|
998
|
+
stage: "production" | "development" | "test";
|
|
999
|
+
}, Record<string, never>, {
|
|
1000
|
+
isDev: () => boolean;
|
|
1001
|
+
isProduction: () => boolean;
|
|
1002
|
+
current: () => "production" | "development" | "test";
|
|
1003
|
+
}>]>>;
|
|
1004
|
+
/** The core-bound app factory; wrapped by {@link createApp} to bridge `config.stage`. */
|
|
1005
|
+
declare const boundCreateApp: <const ExtraPlugins extends readonly import("@moku-labs/core").AnyPluginInstance[] = readonly []>(options?: import("@moku-labs/core").CreateAppOptions<WorkerConfig, WorkerEvents, (import("@moku-labs/core").PluginInstance<"bindings", Config$4, Record<string, never>, BindingsApi, {}> & Record<never, never>) | (import("@moku-labs/core").PluginInstance<"server", ServerConfig, ServerState, Api$3, {
|
|
1006
|
+
"server:matched": {
|
|
1007
|
+
path: string;
|
|
1008
|
+
method: string;
|
|
1009
|
+
};
|
|
1010
|
+
}> & {
|
|
1011
|
+
endpoint: <Path extends string>(path: Path) => EndpointBuilder<Path>;
|
|
1012
|
+
}) | ExtraPlugins[number], [...ExtraPlugins], import("@moku-labs/core").CoreApisFromTuple<[import("@moku-labs/core").CorePluginInstance<"log", import("@moku-labs/common").LogConfig, import("@moku-labs/common").LogState, import("@moku-labs/common").LogApi>, import("@moku-labs/core").CorePluginInstance<"env", import("@moku-labs/common").EnvConfig, import("@moku-labs/common").EnvState, import("@moku-labs/common").EnvApi>, import("@moku-labs/core").CorePluginInstance<"stage", {
|
|
1013
|
+
stage: "production" | "development" | "test";
|
|
1014
|
+
}, Record<string, never>, {
|
|
1015
|
+
isDev: () => boolean;
|
|
1016
|
+
isProduction: () => boolean;
|
|
1017
|
+
current: () => "production" | "development" | "test";
|
|
1018
|
+
}>]>> | undefined) => import("@moku-labs/core").App<WorkerConfig, WorkerEvents, (import("@moku-labs/core").PluginInstance<"bindings", Config$4, Record<string, never>, BindingsApi, {}> & Record<never, never>) | (import("@moku-labs/core").PluginInstance<"server", ServerConfig, ServerState, Api$3, {
|
|
1019
|
+
"server:matched": {
|
|
1020
|
+
path: string;
|
|
1021
|
+
method: string;
|
|
1022
|
+
};
|
|
1023
|
+
}> & {
|
|
1024
|
+
endpoint: <Path extends string>(path: Path) => EndpointBuilder<Path>;
|
|
1025
|
+
}) | ExtraPlugins[number], import("@moku-labs/core").CoreApisFromTuple<[import("@moku-labs/core").CorePluginInstance<"log", import("@moku-labs/common").LogConfig, import("@moku-labs/common").LogState, import("@moku-labs/common").LogApi>, import("@moku-labs/core").CorePluginInstance<"env", import("@moku-labs/common").EnvConfig, import("@moku-labs/common").EnvState, import("@moku-labs/common").EnvApi>, import("@moku-labs/core").CorePluginInstance<"stage", {
|
|
1026
|
+
stage: "production" | "development" | "test";
|
|
1027
|
+
}, Record<string, never>, {
|
|
1028
|
+
isDev: () => boolean;
|
|
1029
|
+
isProduction: () => boolean;
|
|
1030
|
+
current: () => "production" | "development" | "test";
|
|
1031
|
+
}>]>>;
|
|
1032
|
+
/**
|
|
1033
|
+
* Boots a fully-typed, synchronous, per-isolate Worker app — the Layer-3 entry point.
|
|
1034
|
+
*
|
|
1035
|
+
* Wraps the core-bound factory to BRIDGE the single consumer-facing `config.stage`
|
|
1036
|
+
* into the `stage` core plugin's own config, so the typed accessors
|
|
1037
|
+
* (`ctx.stage.isDev()` / `app.stage.current()` / …) can never diverge from the
|
|
1038
|
+
* global `ctx.global.stage`. Global config and core-plugin config resolve on two
|
|
1039
|
+
* SEPARATE cascades (spec/05 §1b), and a core plugin cannot read global config (its
|
|
1040
|
+
* context is `{ config, state }` only — spec/02 §6). `createApp` is the only layer
|
|
1041
|
+
* that sees the consumer's chosen stage, so it mirrors `config.stage` into the stage
|
|
1042
|
+
* plugin's level-4 `pluginConfigs` override (`WorkerConfig.stage → pluginConfigs.stage.stage`).
|
|
1043
|
+
* When `config.stage` is omitted, the global config and the stage plugin both fall back
|
|
1044
|
+
* to their identical `"production"` default. See the module JSDoc above for the full
|
|
1045
|
+
* options/defaults table.
|
|
1046
|
+
*
|
|
1047
|
+
* @param options - The createApp options (`config`, `pluginConfigs`, `plugins`, and lifecycle callbacks).
|
|
1048
|
+
* @returns The initialized app — every plugin's `onInit` has already run.
|
|
1049
|
+
* @example
|
|
1050
|
+
* ```typescript
|
|
1051
|
+
* const app = createApp({ config: { stage: "development", name: "my-worker" } });
|
|
1052
|
+
* app.stage.isDev(); // true — bridged from config.stage
|
|
1053
|
+
* ```
|
|
1054
|
+
*/
|
|
1055
|
+
declare const createApp: typeof boundCreateApp;
|
|
1030
1056
|
//#endregion
|
|
1031
1057
|
export { type types_d_exports as Bindings, type types_d_exports$1 as D1, type types_d_exports$2 as DurableObjects, type PluginCtx, type types_d_exports$3 as Queues, type types_d_exports$4 as Server, type StageApi, type types_d_exports$5 as Storage, type WorkerConfig, type WorkerEnv, type WorkerEvents, type WorkerPluginCtx, bindingsPlugin, createApp, createPlugin, d1Plugin, defineDurableObject, durableObjectsPlugin, endpoint, envPlugin, kvPlugin, logPlugin, queuesPlugin, serverPlugin, stagePlugin, storagePlugin };
|
package/dist/index.mjs
CHANGED
|
@@ -481,6 +481,89 @@ const serverPlugin = createPlugin$1("server", {
|
|
|
481
481
|
},
|
|
482
482
|
helpers: { endpoint }
|
|
483
483
|
});
|
|
484
|
-
|
|
484
|
+
//#endregion
|
|
485
|
+
//#region src/index.ts
|
|
486
|
+
/**
|
|
487
|
+
* @file `@moku-labs/worker` — server-side Cloudflare Workers app + deploy framework on `@moku-labs/core`.
|
|
488
|
+
*
|
|
489
|
+
* The package root exports the bound {@link createApp} factory (the Layer-3 entry
|
|
490
|
+
* point), {@link createPlugin} for consumer plugins, every runtime plugin instance,
|
|
491
|
+
* the `server`/`durable-objects` helpers, and the framework types. Node-only tooling
|
|
492
|
+
* (`deploy`, `cli`) ships from the separate `@moku-labs/worker/cli` entry, never here.
|
|
493
|
+
*
|
|
494
|
+
* `createApp(options?)` boots a fully-typed, synchronous, per-isolate app. The
|
|
495
|
+
* framework defaults `[logPlugin, envPlugin, stagePlugin, bindingsPlugin, serverPlugin]`
|
|
496
|
+
* are applied first, then the `options` below are shallow-merged on top:
|
|
497
|
+
*
|
|
498
|
+
* - `config` — `Partial<WorkerConfig>`; defaults `{ stage: "production", name: "moku-worker", compatibilityDate: "" }`.
|
|
499
|
+
* `config.stage` is the single stage source: the framework mirrors it into the `stage` core
|
|
500
|
+
* plugin so `ctx.stage.*` / `app.stage.*` stay in lockstep with `ctx.global.stage` (see {@link createApp}).
|
|
501
|
+
* - `pluginConfigs` — per-plugin config overrides keyed by plugin name (e.g. `server.endpoints`, `bindings.required`); default `{}`.
|
|
502
|
+
* - `plugins` — extra `PluginInstance[]` appended to the defaults; default `[]`. Do NOT re-list a default plugin.
|
|
503
|
+
* - `onReady` — optional `(app) => void`, runs after every plugin's `onInit`.
|
|
504
|
+
* - `onError` — optional `(error) => void` boot/lifecycle error handler.
|
|
505
|
+
* - `onStart` / `onStop` — optional `() => void | Promise<void>` runtime lifecycle hooks (`app.start()` / `app.stop()`).
|
|
506
|
+
*
|
|
507
|
+
* Re-listing a default plugin name in `plugins` throws
|
|
508
|
+
* `TypeError: [moku-worker] Duplicate plugin name: "<name>"` — `bindings` and `server`
|
|
509
|
+
* are already wired, so consumers list only the resource plugins they add (`kv`, `d1`, …).
|
|
510
|
+
*
|
|
511
|
+
* Minimal HTTP Worker (shape taken from the server integration test):
|
|
512
|
+
*
|
|
513
|
+
* ```typescript
|
|
514
|
+
* import { createApp, endpoint } from "@moku-labs/worker";
|
|
515
|
+
*
|
|
516
|
+
* export const app = createApp({
|
|
517
|
+
* config: { name: "my-worker", compatibilityDate: "2024-09-23" },
|
|
518
|
+
* pluginConfigs: {
|
|
519
|
+
* server: { endpoints: [endpoint("/health").get(() => new Response("ok"))] }
|
|
520
|
+
* }
|
|
521
|
+
* });
|
|
522
|
+
*
|
|
523
|
+
* // worker.ts — the default export is hand-assembled; no plugin produces it.
|
|
524
|
+
* export default {
|
|
525
|
+
* fetch: (request: Request, env: Record<string, unknown>, ctx: ExecutionContext) =>
|
|
526
|
+
* app.server.handle(request, env, ctx)
|
|
527
|
+
* } satisfies ExportedHandler;
|
|
528
|
+
* ```
|
|
529
|
+
*/
|
|
530
|
+
const framework = createCore(coreConfig, { plugins: [bindingsPlugin, serverPlugin] });
|
|
531
|
+
const { createPlugin } = framework;
|
|
532
|
+
/** The core-bound app factory; wrapped by {@link createApp} to bridge `config.stage`. */
|
|
533
|
+
const boundCreateApp = framework.createApp;
|
|
534
|
+
/**
|
|
535
|
+
* Boots a fully-typed, synchronous, per-isolate Worker app — the Layer-3 entry point.
|
|
536
|
+
*
|
|
537
|
+
* Wraps the core-bound factory to BRIDGE the single consumer-facing `config.stage`
|
|
538
|
+
* into the `stage` core plugin's own config, so the typed accessors
|
|
539
|
+
* (`ctx.stage.isDev()` / `app.stage.current()` / …) can never diverge from the
|
|
540
|
+
* global `ctx.global.stage`. Global config and core-plugin config resolve on two
|
|
541
|
+
* SEPARATE cascades (spec/05 §1b), and a core plugin cannot read global config (its
|
|
542
|
+
* context is `{ config, state }` only — spec/02 §6). `createApp` is the only layer
|
|
543
|
+
* that sees the consumer's chosen stage, so it mirrors `config.stage` into the stage
|
|
544
|
+
* plugin's level-4 `pluginConfigs` override (`WorkerConfig.stage → pluginConfigs.stage.stage`).
|
|
545
|
+
* When `config.stage` is omitted, the global config and the stage plugin both fall back
|
|
546
|
+
* to their identical `"production"` default. See the module JSDoc above for the full
|
|
547
|
+
* options/defaults table.
|
|
548
|
+
*
|
|
549
|
+
* @param options - The createApp options (`config`, `pluginConfigs`, `plugins`, and lifecycle callbacks).
|
|
550
|
+
* @returns The initialized app — every plugin's `onInit` has already run.
|
|
551
|
+
* @example
|
|
552
|
+
* ```typescript
|
|
553
|
+
* const app = createApp({ config: { stage: "development", name: "my-worker" } });
|
|
554
|
+
* app.stage.isDev(); // true — bridged from config.stage
|
|
555
|
+
* ```
|
|
556
|
+
*/
|
|
557
|
+
const createApp = (options) => {
|
|
558
|
+
const explicitStage = options?.config?.stage;
|
|
559
|
+
if (explicitStage === void 0) return boundCreateApp(options);
|
|
560
|
+
return boundCreateApp({
|
|
561
|
+
...options,
|
|
562
|
+
pluginConfigs: {
|
|
563
|
+
...options?.pluginConfigs,
|
|
564
|
+
stage: { stage: explicitStage }
|
|
565
|
+
}
|
|
566
|
+
});
|
|
567
|
+
};
|
|
485
568
|
//#endregion
|
|
486
569
|
export { bindingsPlugin, createApp, createPlugin, d1Plugin, defineDurableObject, durableObjectsPlugin, endpoint, envPlugin, kvPlugin, logPlugin, queuesPlugin, serverPlugin, stagePlugin, storagePlugin };
|
package/package.json
CHANGED