@sigil-dev/grimoire 0.9.3 → 0.9.5
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/index.ts +37 -35
- package/package.json +9 -12
- package/preload.js +3 -3
- package/server.ts +19 -19
- package/src/client/deps.ts +36 -19
- package/src/client/enhance.ts +102 -98
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +2 -2
- package/src/client/router.ts +174 -68
- package/src/client/scope.ts +22 -22
- package/src/dev/compile-module.ts +215 -163
- package/src/dev/effect-registry.ts +23 -23
- package/src/dev/graph.ts +109 -114
- package/src/dev/hmr-client.ts +202 -185
- package/src/dev/hmr-server.ts +261 -187
- package/src/dev/loader.ts +120 -47
- package/src/dev/paths.ts +14 -14
- package/src/dev/resolve.ts +70 -0
- package/src/dev/runtime-bundle.ts +52 -52
- package/src/dev/scan.ts +78 -0
- package/src/dev/watcher.ts +69 -47
- package/src/env/dynamic-private.ts +19 -19
- package/src/env/dynamic-public.ts +22 -22
- package/src/env/index.ts +25 -25
- package/src/env/plugin.ts +19 -19
- package/src/env/private.ts +5 -5
- package/src/env/public.ts +7 -7
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +114 -114
- package/src/integrations/vite.ts +73 -73
- package/src/logger/index.ts +4 -4
- package/src/logger/instance.ts +6 -6
- package/src/logger/logger.ts +113 -113
- package/src/logger/request-middleware.ts +24 -24
- package/src/logger/transports.ts +117 -117
- package/src/logger/types.ts +39 -39
- package/src/rendering/head.ts +52 -47
- package/src/rendering/hydrate.ts +159 -138
- package/src/rendering/index.ts +140 -18
- package/src/rendering/ssrPlugin.ts +78 -69
- package/src/routing/manifest-gen.ts +13 -1
- package/src/routing/router.ts +135 -109
- package/src/routing/scanner.ts +196 -141
- package/src/routing/transform-routes.ts +102 -102
- package/src/sentinels/error.ts +52 -52
- package/src/sentinels/fail.ts +41 -41
- package/src/sentinels/redirect.ts +35 -35
- package/src/server/build.ts +250 -243
- package/src/server/context.ts +10 -10
- package/src/server/cookie-utils.ts +66 -66
- package/src/server/coordinator.ts +364 -364
- package/src/server/hooks.ts +114 -114
- package/src/server/index.ts +711 -655
- package/src/server/interactive.ts +56 -56
- package/src/server/plugins.ts +121 -121
- package/src/server/worker.ts +58 -58
- package/src/typegen/index.ts +389 -356
- package/src/types.ts +286 -282
- package/test/client-router.test.ts +295 -44
- package/test/context.test.ts +52 -52
- package/test/env.test.ts +90 -90
- package/test/exports.test.ts +37 -37
- package/test/fail.test.ts +46 -46
- package/test/headers.test.ts +100 -100
- package/test/hmr.test.ts +426 -0
- package/test/hydration.test.ts +119 -119
- package/test/middleware.test.ts +223 -223
- package/test/preload.ts +5 -5
- package/test/redirect-error.test.ts +112 -112
- package/test/rendering.test.ts +719 -615
- package/test/routing.test.ts +197 -81
- package/test/scanning.test.ts +314 -200
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +360 -249
- package/test/streaming.test.ts +242 -125
- package/test/transform-routes.test.ts +87 -84
- package/test/typegen.test.ts +697 -682
- package/tsconfig.json +10 -10
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
export interface InteractiveOptions {
|
|
2
|
-
onQuit: () => void | Promise<void>;
|
|
3
|
-
onRestart: () => void | Promise<void>;
|
|
4
|
-
onClear: () => void;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Returns a promise that resolves only when explicitly settled.
|
|
9
|
-
* Used to keep the dev/start process alive until the user quits.
|
|
10
|
-
*/
|
|
11
|
-
export function createWaiter() {
|
|
12
|
-
let resolve!: () => void;
|
|
13
|
-
const promise = new Promise<void>((r) => {
|
|
14
|
-
resolve = r;
|
|
15
|
-
});
|
|
16
|
-
return { promise, resolve };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Listen for interactive keypresses in dev mode.
|
|
21
|
-
* Mirrors Vite's UX: q=quit, r=restart, c=clear.
|
|
22
|
-
* Falls back gracefully if stdin is not a TTY (CI, piped input).
|
|
23
|
-
*/
|
|
24
|
-
export function listenForKeys(opts: InteractiveOptions) {
|
|
25
|
-
if (!process.stdin.isTTY) return;
|
|
26
|
-
|
|
27
|
-
process.stdin.setRawMode(true);
|
|
28
|
-
process.stdin.resume();
|
|
29
|
-
process.stdin.setEncoding("utf8");
|
|
30
|
-
|
|
31
|
-
process.stdin.on("data", async (key: string) => {
|
|
32
|
-
// ctrl+c
|
|
33
|
-
if (key === "\x03") {
|
|
34
|
-
await opts.onQuit();
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
switch (key.toLowerCase()) {
|
|
39
|
-
case "q":
|
|
40
|
-
await opts.onQuit();
|
|
41
|
-
break;
|
|
42
|
-
case "r":
|
|
43
|
-
await opts.onRestart();
|
|
44
|
-
break;
|
|
45
|
-
case "c":
|
|
46
|
-
opts.onClear();
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function stopListening() {
|
|
53
|
-
if (!process.stdin.isTTY) return;
|
|
54
|
-
process.stdin.setRawMode(false);
|
|
55
|
-
process.stdin.pause();
|
|
56
|
-
}
|
|
1
|
+
export interface InteractiveOptions {
|
|
2
|
+
onQuit: () => void | Promise<void>;
|
|
3
|
+
onRestart: () => void | Promise<void>;
|
|
4
|
+
onClear: () => void;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Returns a promise that resolves only when explicitly settled.
|
|
9
|
+
* Used to keep the dev/start process alive until the user quits.
|
|
10
|
+
*/
|
|
11
|
+
export function createWaiter() {
|
|
12
|
+
let resolve!: () => void;
|
|
13
|
+
const promise = new Promise<void>((r) => {
|
|
14
|
+
resolve = r;
|
|
15
|
+
});
|
|
16
|
+
return { promise, resolve };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Listen for interactive keypresses in dev mode.
|
|
21
|
+
* Mirrors Vite's UX: q=quit, r=restart, c=clear.
|
|
22
|
+
* Falls back gracefully if stdin is not a TTY (CI, piped input).
|
|
23
|
+
*/
|
|
24
|
+
export function listenForKeys(opts: InteractiveOptions) {
|
|
25
|
+
if (!process.stdin.isTTY) return;
|
|
26
|
+
|
|
27
|
+
process.stdin.setRawMode(true);
|
|
28
|
+
process.stdin.resume();
|
|
29
|
+
process.stdin.setEncoding("utf8");
|
|
30
|
+
|
|
31
|
+
process.stdin.on("data", async (key: string) => {
|
|
32
|
+
// ctrl+c
|
|
33
|
+
if (key === "\x03") {
|
|
34
|
+
await opts.onQuit();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
switch (key.toLowerCase()) {
|
|
39
|
+
case "q":
|
|
40
|
+
await opts.onQuit();
|
|
41
|
+
break;
|
|
42
|
+
case "r":
|
|
43
|
+
await opts.onRestart();
|
|
44
|
+
break;
|
|
45
|
+
case "c":
|
|
46
|
+
opts.onClear();
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function stopListening() {
|
|
53
|
+
if (!process.stdin.isTTY) return;
|
|
54
|
+
process.stdin.setRawMode(false);
|
|
55
|
+
process.stdin.pause();
|
|
56
|
+
}
|
package/src/server/plugins.ts
CHANGED
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
import type { Server } from "bun"
|
|
2
|
-
import type { RouteTree } from "../routing/scanner";
|
|
3
|
-
import type {
|
|
4
|
-
BuildResult,
|
|
5
|
-
CoordinatorContext,
|
|
6
|
-
GrimoirePlugin,
|
|
7
|
-
LoadContext,
|
|
8
|
-
PluginContext,
|
|
9
|
-
Route,
|
|
10
|
-
WorkerDescriptor,
|
|
11
|
-
WorkerEnv,
|
|
12
|
-
} from "../types";
|
|
13
|
-
|
|
14
|
-
// Fire-and-forget hooks routed through runHook.
|
|
15
|
-
// onRequest and onRouteRender are intentionally excluded: they have distinct
|
|
16
|
-
// calling conventions (middleware chain / inline transform loop) handled in index.ts.
|
|
17
|
-
// Keep HookArgs in sync with GrimoirePlugin in types.ts.
|
|
18
|
-
type HookArgs = {
|
|
19
|
-
onPluginsResolved: [ctx: PluginContext];
|
|
20
|
-
onStart: [server: Server<unknown>];
|
|
21
|
-
onStop: [reason: "shutdown" | "restart"];
|
|
22
|
-
onBuildStart: [];
|
|
23
|
-
onBuildEnd: [result: BuildResult];
|
|
24
|
-
onRouteLoad: [route: Route, context: LoadContext];
|
|
25
|
-
onCoordinatorStart: [ctx: CoordinatorContext];
|
|
26
|
-
onWorkerReady: [worker: WorkerDescriptor];
|
|
27
|
-
onWorkerDeath: [worker: WorkerDescriptor, reason: "crash" | "intentional"];
|
|
28
|
-
};
|
|
29
|
-
type FireAndForgetHook = keyof HookArgs;
|
|
30
|
-
|
|
31
|
-
export async function runHook<K extends FireAndForgetHook>(
|
|
32
|
-
plugins: GrimoirePlugin[],
|
|
33
|
-
hook: K,
|
|
34
|
-
...args: HookArgs[K]
|
|
35
|
-
): Promise<void> {
|
|
36
|
-
for (const plugin of plugins) {
|
|
37
|
-
const fn = plugin[hook] as ((...a: any[]) => any) | undefined;
|
|
38
|
-
await fn?.(...args);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export async function runRequestHooks(
|
|
43
|
-
plugins: GrimoirePlugin[],
|
|
44
|
-
req: Request,
|
|
45
|
-
final: () => Promise<Response>,
|
|
46
|
-
): Promise<Response> {
|
|
47
|
-
const chain = plugins
|
|
48
|
-
.filter((p) => p.onRequest)
|
|
49
|
-
.reduceRight(
|
|
50
|
-
(next, plugin) => async () => plugin.onRequest!(req, next),
|
|
51
|
-
final,
|
|
52
|
-
);
|
|
53
|
-
return chain();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Calls onWorkerSpawn on ALL plugins and merges results.
|
|
58
|
-
* env is combined (later plugins override earlier on conflict).
|
|
59
|
-
* name is last-write-wins.
|
|
60
|
-
*/
|
|
61
|
-
export async function runWorkerSpawn(
|
|
62
|
-
plugins: GrimoirePlugin[],
|
|
63
|
-
worker: WorkerDescriptor,
|
|
64
|
-
): Promise<WorkerEnv> {
|
|
65
|
-
const merged: WorkerEnv = { env: {} };
|
|
66
|
-
for (const plugin of plugins) {
|
|
67
|
-
const result = await plugin.onWorkerSpawn?.(worker);
|
|
68
|
-
//@ts-expect-error WTF TS? its result? meaning its not gonna do anything if null.
|
|
69
|
-
if (result?.env) Object.assign(merged.env!, result.env);
|
|
70
|
-
//@ts-expect-error WTF TS? its result? meaning its not gonna do anything if null.
|
|
71
|
-
if (result?.name) merged.name = result.name;
|
|
72
|
-
}
|
|
73
|
-
return merged;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* First plugin to return a non-null string wins.
|
|
78
|
-
* Returns null if no plugin handles it — coordinator uses default.
|
|
79
|
-
*/
|
|
80
|
-
export async function runSerializeLocals(
|
|
81
|
-
plugins: GrimoirePlugin[],
|
|
82
|
-
locals: App.Locals,
|
|
83
|
-
): Promise<string | null> {
|
|
84
|
-
for (const plugin of plugins) {
|
|
85
|
-
const result = await plugin.serializeLocals?.(locals);
|
|
86
|
-
if (result != null) return result;
|
|
87
|
-
}
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* First plugin to return non-null wins.
|
|
93
|
-
* Returns null if no plugin handles it — worker uses default.
|
|
94
|
-
*/
|
|
95
|
-
export async function runDeserializeLocals(
|
|
96
|
-
plugins: GrimoirePlugin[],
|
|
97
|
-
raw: string,
|
|
98
|
-
): Promise<App.Locals | null> {
|
|
99
|
-
for (const plugin of plugins) {
|
|
100
|
-
const result = await plugin.deserializeLocals?.(raw);
|
|
101
|
-
if (result != null) return result;
|
|
102
|
-
}
|
|
103
|
-
return null;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* First plugin to return a WorkerDescriptor wins.
|
|
108
|
-
* Returns null if no plugin handles it — coordinator uses default routing.
|
|
109
|
-
*/
|
|
110
|
-
export async function runRouteRequest(
|
|
111
|
-
plugins: GrimoirePlugin[],
|
|
112
|
-
req: Request,
|
|
113
|
-
workers: WorkerDescriptor[],
|
|
114
|
-
routes: RouteTree,
|
|
115
|
-
): Promise<WorkerDescriptor | null> {
|
|
116
|
-
for (const plugin of plugins) {
|
|
117
|
-
const result = await plugin.routeRequest?.(req, workers, routes);
|
|
118
|
-
if (result != null) return result;
|
|
119
|
-
}
|
|
120
|
-
return null;
|
|
121
|
-
}
|
|
1
|
+
import type { Server } from "bun";
|
|
2
|
+
import type { RouteTree } from "../routing/scanner";
|
|
3
|
+
import type {
|
|
4
|
+
BuildResult,
|
|
5
|
+
CoordinatorContext,
|
|
6
|
+
GrimoirePlugin,
|
|
7
|
+
LoadContext,
|
|
8
|
+
PluginContext,
|
|
9
|
+
Route,
|
|
10
|
+
WorkerDescriptor,
|
|
11
|
+
WorkerEnv,
|
|
12
|
+
} from "../types";
|
|
13
|
+
|
|
14
|
+
// Fire-and-forget hooks routed through runHook.
|
|
15
|
+
// onRequest and onRouteRender are intentionally excluded: they have distinct
|
|
16
|
+
// calling conventions (middleware chain / inline transform loop) handled in index.ts.
|
|
17
|
+
// Keep HookArgs in sync with GrimoirePlugin in types.ts.
|
|
18
|
+
type HookArgs = {
|
|
19
|
+
onPluginsResolved: [ctx: PluginContext];
|
|
20
|
+
onStart: [server: Server<unknown>];
|
|
21
|
+
onStop: [reason: "shutdown" | "restart"];
|
|
22
|
+
onBuildStart: [];
|
|
23
|
+
onBuildEnd: [result: BuildResult];
|
|
24
|
+
onRouteLoad: [route: Route, context: LoadContext];
|
|
25
|
+
onCoordinatorStart: [ctx: CoordinatorContext];
|
|
26
|
+
onWorkerReady: [worker: WorkerDescriptor];
|
|
27
|
+
onWorkerDeath: [worker: WorkerDescriptor, reason: "crash" | "intentional"];
|
|
28
|
+
};
|
|
29
|
+
type FireAndForgetHook = keyof HookArgs;
|
|
30
|
+
|
|
31
|
+
export async function runHook<K extends FireAndForgetHook>(
|
|
32
|
+
plugins: GrimoirePlugin[],
|
|
33
|
+
hook: K,
|
|
34
|
+
...args: HookArgs[K]
|
|
35
|
+
): Promise<void> {
|
|
36
|
+
for (const plugin of plugins) {
|
|
37
|
+
const fn = plugin[hook] as ((...a: any[]) => any) | undefined;
|
|
38
|
+
await fn?.(...args);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function runRequestHooks(
|
|
43
|
+
plugins: GrimoirePlugin[],
|
|
44
|
+
req: Request,
|
|
45
|
+
final: () => Promise<Response>,
|
|
46
|
+
): Promise<Response> {
|
|
47
|
+
const chain = plugins
|
|
48
|
+
.filter((p) => p.onRequest)
|
|
49
|
+
.reduceRight(
|
|
50
|
+
(next, plugin) => async () => plugin.onRequest!(req, next),
|
|
51
|
+
final,
|
|
52
|
+
);
|
|
53
|
+
return chain();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Calls onWorkerSpawn on ALL plugins and merges results.
|
|
58
|
+
* env is combined (later plugins override earlier on conflict).
|
|
59
|
+
* name is last-write-wins.
|
|
60
|
+
*/
|
|
61
|
+
export async function runWorkerSpawn(
|
|
62
|
+
plugins: GrimoirePlugin[],
|
|
63
|
+
worker: WorkerDescriptor,
|
|
64
|
+
): Promise<WorkerEnv> {
|
|
65
|
+
const merged: WorkerEnv = { env: {} };
|
|
66
|
+
for (const plugin of plugins) {
|
|
67
|
+
const result = await plugin.onWorkerSpawn?.(worker);
|
|
68
|
+
//@ts-expect-error WTF TS? its result? meaning its not gonna do anything if null.
|
|
69
|
+
if (result?.env) Object.assign(merged.env!, result.env);
|
|
70
|
+
//@ts-expect-error WTF TS? its result? meaning its not gonna do anything if null.
|
|
71
|
+
if (result?.name) merged.name = result.name;
|
|
72
|
+
}
|
|
73
|
+
return merged;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* First plugin to return a non-null string wins.
|
|
78
|
+
* Returns null if no plugin handles it — coordinator uses default.
|
|
79
|
+
*/
|
|
80
|
+
export async function runSerializeLocals(
|
|
81
|
+
plugins: GrimoirePlugin[],
|
|
82
|
+
locals: App.Locals,
|
|
83
|
+
): Promise<string | null> {
|
|
84
|
+
for (const plugin of plugins) {
|
|
85
|
+
const result = await plugin.serializeLocals?.(locals);
|
|
86
|
+
if (result != null) return result;
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* First plugin to return non-null wins.
|
|
93
|
+
* Returns null if no plugin handles it — worker uses default.
|
|
94
|
+
*/
|
|
95
|
+
export async function runDeserializeLocals(
|
|
96
|
+
plugins: GrimoirePlugin[],
|
|
97
|
+
raw: string,
|
|
98
|
+
): Promise<App.Locals | null> {
|
|
99
|
+
for (const plugin of plugins) {
|
|
100
|
+
const result = await plugin.deserializeLocals?.(raw);
|
|
101
|
+
if (result != null) return result;
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* First plugin to return a WorkerDescriptor wins.
|
|
108
|
+
* Returns null if no plugin handles it — coordinator uses default routing.
|
|
109
|
+
*/
|
|
110
|
+
export async function runRouteRequest(
|
|
111
|
+
plugins: GrimoirePlugin[],
|
|
112
|
+
req: Request,
|
|
113
|
+
workers: WorkerDescriptor[],
|
|
114
|
+
routes: RouteTree,
|
|
115
|
+
): Promise<WorkerDescriptor | null> {
|
|
116
|
+
for (const plugin of plugins) {
|
|
117
|
+
const result = await plugin.routeRequest?.(req, workers, routes);
|
|
118
|
+
if (result != null) return result;
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
package/src/server/worker.ts
CHANGED
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import { join } from "node:path";
|
|
2
|
-
import type { GrimoireConfig, GrimoirePlugin, WorkerMode } from "../types";
|
|
3
|
-
import { createServer } from "./index";
|
|
4
|
-
|
|
5
|
-
export interface WorkerOptions {
|
|
6
|
-
config: GrimoireConfig;
|
|
7
|
-
plugins: GrimoirePlugin[];
|
|
8
|
-
secret: string;
|
|
9
|
-
mode: WorkerMode;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export async function startWorker(options: WorkerOptions) {
|
|
13
|
-
const { secret, plugins } = options;
|
|
14
|
-
|
|
15
|
-
const workerAuthPlugin: GrimoirePlugin = {
|
|
16
|
-
name: "__grimoire-worker-auth",
|
|
17
|
-
async onRequest(req, next) {
|
|
18
|
-
if (req.headers.get("X-Grimoire-Internal") !== secret) {
|
|
19
|
-
return new Response("Forbidden", { status: 403 });
|
|
20
|
-
}
|
|
21
|
-
return next();
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
return createServer({
|
|
26
|
-
...options.config,
|
|
27
|
-
plugins: [workerAuthPlugin, ...plugins],
|
|
28
|
-
_skipBuild: true,
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// bootstrap when spawned by coordinator
|
|
33
|
-
if (process.env.GRIMOIRE_INTERNAL_SECRET) {
|
|
34
|
-
const secret = process.env.GRIMOIRE_INTERNAL_SECRET;
|
|
35
|
-
const port = Number(process.env.GRIMOIRE_WORKER_PORT ?? 3001);
|
|
36
|
-
const cwd = process.cwd();
|
|
37
|
-
|
|
38
|
-
let config: GrimoireConfig = {};
|
|
39
|
-
try {
|
|
40
|
-
const mod = await import(join(cwd, "sigil.config.ts"));
|
|
41
|
-
config = mod.default ?? {};
|
|
42
|
-
} catch {}
|
|
43
|
-
|
|
44
|
-
let finalConfig: GrimoireConfig = { ...config, port, host: "127.0.0.1" };
|
|
45
|
-
const plugins = finalConfig.plugins ?? [];
|
|
46
|
-
for (const plugin of plugins) {
|
|
47
|
-
finalConfig = plugin.config?.(finalConfig) ?? finalConfig;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
await startWorker({
|
|
51
|
-
config: finalConfig,
|
|
52
|
-
plugins,
|
|
53
|
-
secret,
|
|
54
|
-
mode: (process.env.GRIMOIRE_MODE ?? "full") as WorkerMode,
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
process.send?.({ ready: true });
|
|
58
|
-
}
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import type { GrimoireConfig, GrimoirePlugin, WorkerMode } from "../types";
|
|
3
|
+
import { createServer } from "./index";
|
|
4
|
+
|
|
5
|
+
export interface WorkerOptions {
|
|
6
|
+
config: GrimoireConfig;
|
|
7
|
+
plugins: GrimoirePlugin[];
|
|
8
|
+
secret: string;
|
|
9
|
+
mode: WorkerMode;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function startWorker(options: WorkerOptions) {
|
|
13
|
+
const { secret, plugins } = options;
|
|
14
|
+
|
|
15
|
+
const workerAuthPlugin: GrimoirePlugin = {
|
|
16
|
+
name: "__grimoire-worker-auth",
|
|
17
|
+
async onRequest(req, next) {
|
|
18
|
+
if (req.headers.get("X-Grimoire-Internal") !== secret) {
|
|
19
|
+
return new Response("Forbidden", { status: 403 });
|
|
20
|
+
}
|
|
21
|
+
return next();
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return createServer({
|
|
26
|
+
...options.config,
|
|
27
|
+
plugins: [workerAuthPlugin, ...plugins],
|
|
28
|
+
_skipBuild: true,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// bootstrap when spawned by coordinator
|
|
33
|
+
if (process.env.GRIMOIRE_INTERNAL_SECRET) {
|
|
34
|
+
const secret = process.env.GRIMOIRE_INTERNAL_SECRET;
|
|
35
|
+
const port = Number(process.env.GRIMOIRE_WORKER_PORT ?? 3001);
|
|
36
|
+
const cwd = process.cwd();
|
|
37
|
+
|
|
38
|
+
let config: GrimoireConfig = {};
|
|
39
|
+
try {
|
|
40
|
+
const mod = await import(join(cwd, "sigil.config.ts"));
|
|
41
|
+
config = mod.default ?? {};
|
|
42
|
+
} catch {}
|
|
43
|
+
|
|
44
|
+
let finalConfig: GrimoireConfig = { ...config, port, host: "127.0.0.1" };
|
|
45
|
+
const plugins = finalConfig.plugins ?? [];
|
|
46
|
+
for (const plugin of plugins) {
|
|
47
|
+
finalConfig = plugin.config?.(finalConfig) ?? finalConfig;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await startWorker({
|
|
51
|
+
config: finalConfig,
|
|
52
|
+
plugins,
|
|
53
|
+
secret,
|
|
54
|
+
mode: (process.env.GRIMOIRE_MODE ?? "full") as WorkerMode,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
process.send?.({ ready: true });
|
|
58
|
+
}
|