veryfront 0.1.73 → 0.1.75
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/esm/cli/commands/knowledge/command-help.d.ts.map +1 -1
- package/esm/cli/commands/knowledge/command-help.js +3 -1
- package/esm/cli/commands/knowledge/command.d.ts +34 -5
- package/esm/cli/commands/knowledge/command.d.ts.map +1 -1
- package/esm/cli/commands/knowledge/command.js +151 -22
- package/esm/cli/commands/knowledge/parser-source.d.ts.map +1 -1
- package/esm/cli/commands/knowledge/parser-source.js +110 -5
- package/esm/deno.d.ts +2 -0
- package/esm/deno.js +3 -1
- package/esm/src/data/data-fetcher.d.ts +11 -1
- package/esm/src/data/data-fetcher.d.ts.map +1 -1
- package/esm/src/data/data-fetcher.js +5 -2
- package/esm/src/data/index.d.ts +1 -1
- package/esm/src/data/index.d.ts.map +1 -1
- package/esm/src/data/server-data-fetcher.d.ts +14 -1
- package/esm/src/data/server-data-fetcher.d.ts.map +1 -1
- package/esm/src/data/server-data-fetcher.js +49 -3
- package/esm/src/rendering/orchestrator/lifecycle.d.ts +4 -0
- package/esm/src/rendering/orchestrator/lifecycle.d.ts.map +1 -1
- package/esm/src/rendering/orchestrator/lifecycle.js +8 -0
- package/esm/src/rendering/orchestrator/pipeline.d.ts.map +1 -1
- package/esm/src/rendering/orchestrator/pipeline.js +6 -1
- package/esm/src/rendering/orchestrator/ssr-orchestrator.d.ts +26 -1
- package/esm/src/rendering/orchestrator/ssr-orchestrator.d.ts.map +1 -1
- package/esm/src/rendering/orchestrator/ssr-orchestrator.js +77 -1
- package/esm/src/routing/api/handler.d.ts.map +1 -1
- package/esm/src/routing/api/handler.js +6 -2
- package/esm/src/routing/api/route-executor.d.ts +8 -2
- package/esm/src/routing/api/route-executor.d.ts.map +1 -1
- package/esm/src/routing/api/route-executor.js +131 -3
- package/esm/src/security/deno-permissions.d.ts +6 -0
- package/esm/src/security/deno-permissions.d.ts.map +1 -1
- package/esm/src/security/deno-permissions.js +10 -0
- package/esm/src/security/sandbox/project-worker.d.ts +61 -0
- package/esm/src/security/sandbox/project-worker.d.ts.map +1 -0
- package/esm/src/security/sandbox/project-worker.js +318 -0
- package/esm/src/security/sandbox/worker-permissions.d.ts +30 -0
- package/esm/src/security/sandbox/worker-permissions.d.ts.map +1 -0
- package/esm/src/security/sandbox/worker-permissions.js +60 -0
- package/esm/src/security/sandbox/worker-pool.d.ts +87 -0
- package/esm/src/security/sandbox/worker-pool.d.ts.map +1 -0
- package/esm/src/security/sandbox/worker-pool.js +356 -0
- package/esm/src/security/sandbox/worker-types.d.ts +165 -0
- package/esm/src/security/sandbox/worker-types.d.ts.map +1 -0
- package/esm/src/security/sandbox/worker-types.js +17 -0
- package/esm/src/server/handlers/request/ssr/ssr.handler.d.ts +2 -0
- package/esm/src/server/handlers/request/ssr/ssr.handler.d.ts.map +1 -1
- package/esm/src/server/handlers/request/ssr/ssr.handler.js +6 -2
- package/esm/src/server/project-env/storage.d.ts +6 -0
- package/esm/src/server/project-env/storage.d.ts.map +1 -1
- package/esm/src/server/project-env/storage.js +8 -0
- package/esm/src/server/runtime-handler/adapter-factory.d.ts +3 -0
- package/esm/src/server/runtime-handler/adapter-factory.d.ts.map +1 -1
- package/esm/src/server/runtime-handler/adapter-factory.js +6 -5
- package/esm/src/server/runtime-handler/index.d.ts +33 -0
- package/esm/src/server/runtime-handler/index.d.ts.map +1 -1
- package/esm/src/server/runtime-handler/index.js +103 -37
- package/esm/src/server/runtime-handler/local-project-discovery.d.ts +32 -4
- package/esm/src/server/runtime-handler/local-project-discovery.d.ts.map +1 -1
- package/esm/src/server/runtime-handler/local-project-discovery.js +46 -16
- package/esm/src/server/runtime-handler/project-isolation.d.ts +5 -0
- package/esm/src/server/runtime-handler/project-isolation.d.ts.map +1 -1
- package/esm/src/server/runtime-handler/project-isolation.js +44 -0
- package/esm/src/server/services/rendering/ssr.service.d.ts +19 -1
- package/esm/src/server/services/rendering/ssr.service.d.ts.map +1 -1
- package/esm/src/server/services/rendering/ssr.service.js +9 -1
- package/esm/src/server/shared/renderer/adapter.d.ts +25 -0
- package/esm/src/server/shared/renderer/adapter.d.ts.map +1 -1
- package/esm/src/server/shared/renderer/adapter.js +83 -10
- package/esm/src/server/shared/renderer/index.d.ts +1 -1
- package/esm/src/server/shared/renderer/index.d.ts.map +1 -1
- package/esm/src/server/shared/renderer/index.js +1 -1
- package/esm/src/server/shared/renderer/memory/pressure.d.ts +7 -0
- package/esm/src/server/shared/renderer/memory/pressure.d.ts.map +1 -1
- package/esm/src/server/shared/renderer/memory/pressure.js +7 -0
- package/esm/src/transforms/pipeline/stages/ssr-vf-modules/path-resolver.d.ts +4 -4
- package/esm/src/transforms/pipeline/stages/ssr-vf-modules/path-resolver.d.ts.map +1 -1
- package/esm/src/transforms/pipeline/stages/ssr-vf-modules/path-resolver.js +15 -15
- package/esm/src/utils/index.d.ts +10 -1
- package/esm/src/utils/index.d.ts.map +1 -1
- package/esm/src/utils/index.js +9 -1
- package/esm/src/utils/logger/index.d.ts +1 -1
- package/esm/src/utils/logger/index.d.ts.map +1 -1
- package/esm/src/utils/logger/index.js +1 -1
- package/esm/src/utils/logger/logger.d.ts +14 -0
- package/esm/src/utils/logger/logger.d.ts.map +1 -1
- package/esm/src/utils/logger/logger.js +17 -0
- package/esm/src/workflow/claude-code/tool.d.ts +5 -5
- package/package.json +4 -1
- package/src/cli/commands/knowledge/command-help.ts +3 -1
- package/src/cli/commands/knowledge/command.ts +180 -22
- package/src/cli/commands/knowledge/parser-source.ts +110 -5
- package/src/deno.js +3 -1
- package/src/src/data/data-fetcher.ts +18 -2
- package/src/src/data/index.ts +1 -1
- package/src/src/data/server-data-fetcher.ts +78 -3
- package/src/src/rendering/orchestrator/lifecycle.ts +11 -0
- package/src/src/rendering/orchestrator/pipeline.ts +7 -2
- package/src/src/rendering/orchestrator/ssr-orchestrator.ts +119 -0
- package/src/src/routing/api/handler.ts +16 -3
- package/src/src/routing/api/route-executor.ts +222 -1
- package/src/src/security/deno-permissions.ts +11 -0
- package/src/src/security/sandbox/project-worker.ts +416 -0
- package/src/src/security/sandbox/worker-permissions.ts +74 -0
- package/src/src/security/sandbox/worker-pool.ts +451 -0
- package/src/src/security/sandbox/worker-types.ts +209 -0
- package/src/src/server/handlers/request/ssr/ssr.handler.ts +11 -2
- package/src/src/server/project-env/storage.ts +9 -0
- package/src/src/server/runtime-handler/adapter-factory.ts +13 -5
- package/src/src/server/runtime-handler/index.ts +132 -39
- package/src/src/server/runtime-handler/local-project-discovery.ts +51 -17
- package/src/src/server/runtime-handler/project-isolation.ts +53 -0
- package/src/src/server/services/rendering/ssr.service.ts +34 -3
- package/src/src/server/shared/renderer/adapter.ts +107 -8
- package/src/src/server/shared/renderer/index.ts +7 -1
- package/src/src/server/shared/renderer/memory/pressure.ts +8 -0
- package/src/src/transforms/pipeline/stages/ssr-vf-modules/path-resolver.ts +18 -12
- package/src/src/utils/index.ts +11 -0
- package/src/src/utils/logger/index.ts +1 -0
- package/src/src/utils/logger/logger.ts +34 -0
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Project Worker
|
|
3
|
+
*
|
|
4
|
+
* Wraps a single Deno Worker for one project. Manages the Worker lifecycle,
|
|
5
|
+
* sends/receives structured messages, enforces per-request timeouts,
|
|
6
|
+
* and serializes errors across the Worker boundary.
|
|
7
|
+
*
|
|
8
|
+
* @module security/sandbox/project-worker
|
|
9
|
+
*/
|
|
10
|
+
import * as dntShim from "../../../_dnt.shims.js";
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
import { serverLogger } from "../../utils/index.js";
|
|
14
|
+
import { isCompiledBinary } from "../../utils/index.js";
|
|
15
|
+
import { withSpan } from "../../observability/tracing/otlp-setup.js";
|
|
16
|
+
import { TIMEOUT_ERROR, UNKNOWN_ERROR } from "../../errors/index.js";
|
|
17
|
+
import type { WorkerPermissions } from "./worker-permissions.js";
|
|
18
|
+
import type {
|
|
19
|
+
WorkerRequest,
|
|
20
|
+
WorkerResponse,
|
|
21
|
+
WorkerStreamChunk,
|
|
22
|
+
WorkerStreamEnd,
|
|
23
|
+
} from "./worker-types.js";
|
|
24
|
+
|
|
25
|
+
const logger = serverLogger.component("project-worker");
|
|
26
|
+
|
|
27
|
+
type ExtendedWorkerOptions = {
|
|
28
|
+
type: "module";
|
|
29
|
+
name?: string;
|
|
30
|
+
deno?: { permissions: WorkerPermissions };
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export interface ProjectWorkerOptions {
|
|
34
|
+
projectId: string;
|
|
35
|
+
permissions: WorkerPermissions;
|
|
36
|
+
requestTimeoutMs: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface PendingRequest {
|
|
40
|
+
resolve: (value: WorkerResponse) => void;
|
|
41
|
+
reject: (error: Error) => void;
|
|
42
|
+
timer: ReturnType<typeof dntShim.setTimeout>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface StreamHandler {
|
|
46
|
+
onChunk: (chunk: Uint8Array) => void;
|
|
47
|
+
onEnd: () => void;
|
|
48
|
+
onError: (error: Error) => void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Status of a project worker.
|
|
53
|
+
*/
|
|
54
|
+
export type WorkerStatus = "idle" | "busy" | "crashed" | "terminated";
|
|
55
|
+
|
|
56
|
+
export class ProjectWorker {
|
|
57
|
+
readonly projectId: string;
|
|
58
|
+
|
|
59
|
+
private worker: Worker | null = null;
|
|
60
|
+
private pending = new Map<string, PendingRequest>();
|
|
61
|
+
private streamHandlers = new Map<string, StreamHandler>();
|
|
62
|
+
private requestTimeoutMs: number;
|
|
63
|
+
private permissions: WorkerPermissions;
|
|
64
|
+
private _requestCount = 0;
|
|
65
|
+
private _lastActivityAt = Date.now();
|
|
66
|
+
private _status: WorkerStatus = "idle";
|
|
67
|
+
|
|
68
|
+
constructor(options: ProjectWorkerOptions) {
|
|
69
|
+
this.projectId = options.projectId;
|
|
70
|
+
this.permissions = options.permissions;
|
|
71
|
+
this.requestTimeoutMs = options.requestTimeoutMs;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get status(): WorkerStatus {
|
|
75
|
+
return this._status;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get requestCount(): number {
|
|
79
|
+
return this._requestCount;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
get lastActivityAt(): number {
|
|
83
|
+
return this._lastActivityAt;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
get hasPendingRequests(): boolean {
|
|
87
|
+
return this.pending.size > 0;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Start the worker. Idempotent — safe to call if already running.
|
|
92
|
+
*/
|
|
93
|
+
start(): void {
|
|
94
|
+
if (this.worker) return;
|
|
95
|
+
|
|
96
|
+
const workerUrl = this.getWorkerScriptUrl();
|
|
97
|
+
|
|
98
|
+
const workerOptions: ExtendedWorkerOptions = {
|
|
99
|
+
type: "module",
|
|
100
|
+
name: `project-worker-${this.projectId}`,
|
|
101
|
+
deno: { permissions: this.permissions },
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// @ts-ignore - Deno Worker accepts extended options
|
|
105
|
+
this.worker = new Worker(workerUrl, workerOptions);
|
|
106
|
+
this._status = "idle";
|
|
107
|
+
|
|
108
|
+
this.worker.onmessage = (event: MessageEvent) => {
|
|
109
|
+
this.handleMessage(event.data);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
this.worker.onerror = (event) => {
|
|
113
|
+
logger.error("Worker error", {
|
|
114
|
+
projectId: this.projectId,
|
|
115
|
+
error: event.message ?? String(event),
|
|
116
|
+
});
|
|
117
|
+
this._status = "crashed";
|
|
118
|
+
this.rejectAllPending("Worker crashed");
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
logger.debug("Worker started", { projectId: this.projectId });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Execute a request in this worker. Returns a typed response.
|
|
126
|
+
*/
|
|
127
|
+
execute(request: WorkerRequest): Promise<WorkerResponse> {
|
|
128
|
+
return withSpan(
|
|
129
|
+
"worker.execute",
|
|
130
|
+
() => {
|
|
131
|
+
if (!this.worker || this._status === "crashed" || this._status === "terminated") {
|
|
132
|
+
return Promise.reject(
|
|
133
|
+
UNKNOWN_ERROR.create({ detail: `Worker not available (status: ${this._status})` }),
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
this._requestCount++;
|
|
138
|
+
this._lastActivityAt = Date.now();
|
|
139
|
+
this._status = "busy";
|
|
140
|
+
|
|
141
|
+
return new Promise<WorkerResponse>((resolve, reject) => {
|
|
142
|
+
const timer = dntShim.setTimeout(() => {
|
|
143
|
+
this.pending.delete(request.id);
|
|
144
|
+
this.updateIdleStatus();
|
|
145
|
+
reject(
|
|
146
|
+
TIMEOUT_ERROR.create({
|
|
147
|
+
detail: `Worker request timed out after ${this.requestTimeoutMs}ms`,
|
|
148
|
+
}),
|
|
149
|
+
);
|
|
150
|
+
}, this.requestTimeoutMs);
|
|
151
|
+
|
|
152
|
+
this.pending.set(request.id, { resolve, reject, timer });
|
|
153
|
+
this.worker!.postMessage(request);
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
"worker.projectId": this.projectId,
|
|
158
|
+
"worker.requestType": request.type,
|
|
159
|
+
"worker.requestId": request.id,
|
|
160
|
+
},
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Execute a streaming request. Returns a ReadableStream that yields
|
|
166
|
+
* chunks as they arrive from the Worker via postMessage.
|
|
167
|
+
*
|
|
168
|
+
* Used for streaming SSR where the Worker sends chunks progressively.
|
|
169
|
+
* Falls back to a single-chunk stream if the Worker returns a non-streaming
|
|
170
|
+
* response (ssr-result with full HTML).
|
|
171
|
+
*/
|
|
172
|
+
executeStream(request: WorkerRequest): ReadableStream<Uint8Array> {
|
|
173
|
+
if (!this.worker || this._status === "crashed" || this._status === "terminated") {
|
|
174
|
+
throw UNKNOWN_ERROR.create({ detail: `Worker not available (status: ${this._status})` });
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
this._requestCount++;
|
|
178
|
+
this._lastActivityAt = Date.now();
|
|
179
|
+
this._status = "busy";
|
|
180
|
+
|
|
181
|
+
const requestId = request.id;
|
|
182
|
+
const encoder = new TextEncoder();
|
|
183
|
+
|
|
184
|
+
return new ReadableStream<Uint8Array>({
|
|
185
|
+
start: (controller) => {
|
|
186
|
+
let timer = dntShim.setTimeout(() => {
|
|
187
|
+
this.streamHandlers.delete(requestId);
|
|
188
|
+
this.pending.delete(requestId);
|
|
189
|
+
this.updateIdleStatus();
|
|
190
|
+
controller.error(
|
|
191
|
+
TIMEOUT_ERROR.create({
|
|
192
|
+
detail: `Worker stream timed out after ${this.requestTimeoutMs}ms`,
|
|
193
|
+
}),
|
|
194
|
+
);
|
|
195
|
+
}, this.requestTimeoutMs);
|
|
196
|
+
|
|
197
|
+
const resetTimer = () => {
|
|
198
|
+
clearTimeout(timer);
|
|
199
|
+
timer = dntShim.setTimeout(() => {
|
|
200
|
+
this.streamHandlers.delete(requestId);
|
|
201
|
+
this.pending.delete(requestId);
|
|
202
|
+
this.updateIdleStatus();
|
|
203
|
+
controller.error(
|
|
204
|
+
TIMEOUT_ERROR.create({
|
|
205
|
+
detail: `Worker stream timed out after ${this.requestTimeoutMs}ms`,
|
|
206
|
+
}),
|
|
207
|
+
);
|
|
208
|
+
}, this.requestTimeoutMs);
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// Register a stream handler for this request
|
|
212
|
+
this.streamHandlers.set(requestId, {
|
|
213
|
+
onChunk: (chunk: Uint8Array) => {
|
|
214
|
+
resetTimer();
|
|
215
|
+
controller.enqueue(chunk);
|
|
216
|
+
},
|
|
217
|
+
onEnd: () => {
|
|
218
|
+
clearTimeout(timer);
|
|
219
|
+
this.streamHandlers.delete(requestId);
|
|
220
|
+
this.pending.delete(requestId);
|
|
221
|
+
this.updateIdleStatus();
|
|
222
|
+
controller.close();
|
|
223
|
+
},
|
|
224
|
+
onError: (error: Error) => {
|
|
225
|
+
clearTimeout(timer);
|
|
226
|
+
this.streamHandlers.delete(requestId);
|
|
227
|
+
this.pending.delete(requestId);
|
|
228
|
+
this.updateIdleStatus();
|
|
229
|
+
controller.error(error);
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// Also register in pending for non-streaming responses (fallback)
|
|
234
|
+
this.pending.set(requestId, {
|
|
235
|
+
resolve: (response) => {
|
|
236
|
+
clearTimeout(timer);
|
|
237
|
+
this.streamHandlers.delete(requestId);
|
|
238
|
+
this.pending.delete(requestId);
|
|
239
|
+
this.updateIdleStatus();
|
|
240
|
+
|
|
241
|
+
// If we get an ssr-result, emit it as a single chunk
|
|
242
|
+
if (response.type === "ssr-result") {
|
|
243
|
+
controller.enqueue(encoder.encode(response.html));
|
|
244
|
+
controller.close();
|
|
245
|
+
} else if (response.type === "error") {
|
|
246
|
+
const err = new Error(response.error.message);
|
|
247
|
+
err.name = response.error.name;
|
|
248
|
+
controller.error(err);
|
|
249
|
+
} else {
|
|
250
|
+
controller.close();
|
|
251
|
+
}
|
|
252
|
+
},
|
|
253
|
+
reject: (error) => {
|
|
254
|
+
clearTimeout(timer);
|
|
255
|
+
this.streamHandlers.delete(requestId);
|
|
256
|
+
this.pending.delete(requestId);
|
|
257
|
+
this.updateIdleStatus();
|
|
258
|
+
controller.error(error);
|
|
259
|
+
},
|
|
260
|
+
timer,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
this.worker!.postMessage(request);
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Health check — send a ping and wait for pong.
|
|
270
|
+
*/
|
|
271
|
+
async isHealthy(timeoutMs = 5_000): Promise<boolean> {
|
|
272
|
+
if (!this.worker || this._status === "crashed" || this._status === "terminated") {
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const id = dntShim.crypto.randomUUID();
|
|
277
|
+
|
|
278
|
+
return new Promise<boolean>((resolve) => {
|
|
279
|
+
const timer = dntShim.setTimeout(() => {
|
|
280
|
+
this.pending.delete(id);
|
|
281
|
+
resolve(false);
|
|
282
|
+
}, timeoutMs);
|
|
283
|
+
|
|
284
|
+
this.pending.set(id, {
|
|
285
|
+
resolve: () => {
|
|
286
|
+
clearTimeout(timer);
|
|
287
|
+
this.pending.delete(id);
|
|
288
|
+
resolve(true);
|
|
289
|
+
},
|
|
290
|
+
reject: () => {
|
|
291
|
+
clearTimeout(timer);
|
|
292
|
+
this.pending.delete(id);
|
|
293
|
+
resolve(false);
|
|
294
|
+
},
|
|
295
|
+
timer,
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
this.worker!.postMessage({ type: "ping", id });
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Clear the worker's module cache. Used for dev mode hot reload.
|
|
304
|
+
*/
|
|
305
|
+
clearModuleCache(): void {
|
|
306
|
+
if (!this.worker || this._status === "crashed" || this._status === "terminated") return;
|
|
307
|
+
this.worker.postMessage({ type: "clear-cache" });
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Terminate the worker. Rejects all pending requests.
|
|
312
|
+
*/
|
|
313
|
+
terminate(): void {
|
|
314
|
+
if (!this.worker) return;
|
|
315
|
+
|
|
316
|
+
this._status = "terminated";
|
|
317
|
+
this.rejectAllPending("Worker terminated");
|
|
318
|
+
|
|
319
|
+
try {
|
|
320
|
+
this.worker.terminate();
|
|
321
|
+
} catch (error) {
|
|
322
|
+
logger.debug("Worker terminate failed", {
|
|
323
|
+
projectId: this.projectId,
|
|
324
|
+
error,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
this.worker = null;
|
|
329
|
+
logger.debug("Worker terminated", { projectId: this.projectId });
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// -----------------------------------------------------------------------
|
|
333
|
+
// Private
|
|
334
|
+
// -----------------------------------------------------------------------
|
|
335
|
+
|
|
336
|
+
private getWorkerScriptUrl(): string {
|
|
337
|
+
// In compiled binary mode, use a data URL because blob URLs don't work
|
|
338
|
+
// See: deno-sandbox.ts for the same pattern
|
|
339
|
+
if (isCompiledBinary()) {
|
|
340
|
+
// For compiled binaries, we'd need to inline the worker script.
|
|
341
|
+
// For now, fall through to the import.meta.resolve path which works
|
|
342
|
+
// in development and standard Deno execution.
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Use import.meta.resolve to get the absolute URL of the worker script.
|
|
346
|
+
// This works in both `deno run` and `deno compile` contexts.
|
|
347
|
+
return import.meta.resolve("./worker-script.ts");
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private handleMessage(
|
|
351
|
+
data:
|
|
352
|
+
| WorkerResponse
|
|
353
|
+
| WorkerStreamChunk
|
|
354
|
+
| WorkerStreamEnd
|
|
355
|
+
| { type: "pong"; id: string },
|
|
356
|
+
): void {
|
|
357
|
+
if (data.type === "pong") {
|
|
358
|
+
const pending = this.pending.get((data as { id: string }).id);
|
|
359
|
+
if (pending) {
|
|
360
|
+
clearTimeout(pending.timer);
|
|
361
|
+
pending.resolve(data as unknown as WorkerResponse);
|
|
362
|
+
this.pending.delete((data as { id: string }).id);
|
|
363
|
+
}
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Handle streaming SSR chunks
|
|
368
|
+
if (data.type === "stream-chunk") {
|
|
369
|
+
const handler = this.streamHandlers.get(data.id);
|
|
370
|
+
if (handler) handler.onChunk(data.chunk);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (data.type === "stream-end") {
|
|
375
|
+
const handler = this.streamHandlers.get(data.id);
|
|
376
|
+
if (handler) handler.onEnd();
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const response = data as WorkerResponse;
|
|
381
|
+
const pending = this.pending.get(response.id);
|
|
382
|
+
if (!pending) {
|
|
383
|
+
logger.warn("Received response for unknown request", {
|
|
384
|
+
projectId: this.projectId,
|
|
385
|
+
id: response.id,
|
|
386
|
+
});
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
clearTimeout(pending.timer);
|
|
391
|
+
this.pending.delete(response.id);
|
|
392
|
+
this.updateIdleStatus();
|
|
393
|
+
|
|
394
|
+
pending.resolve(response);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
private updateIdleStatus(): void {
|
|
398
|
+
if (this.pending.size === 0 && this._status === "busy") {
|
|
399
|
+
this._status = "idle";
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
private rejectAllPending(reason: string): void {
|
|
404
|
+
for (const [id, pending] of this.pending) {
|
|
405
|
+
clearTimeout(pending.timer);
|
|
406
|
+
pending.reject(UNKNOWN_ERROR.create({ detail: reason }));
|
|
407
|
+
this.pending.delete(id);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Clean up stream handlers
|
|
411
|
+
for (const [id, handler] of this.streamHandlers) {
|
|
412
|
+
handler.onError(UNKNOWN_ERROR.create({ detail: reason }));
|
|
413
|
+
this.streamHandlers.delete(id);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker Permission Builder
|
|
3
|
+
*
|
|
4
|
+
* Builds scoped Deno Worker permissions for per-project isolation.
|
|
5
|
+
* Each project worker gets the minimum required permissions.
|
|
6
|
+
*
|
|
7
|
+
* @module security/sandbox/worker-permissions
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Deno Worker permission object.
|
|
12
|
+
* See: https://docs.deno.com/runtime/fundamentals/permissions/
|
|
13
|
+
*/
|
|
14
|
+
import * as dntShim from "../../../_dnt.shims.js";
|
|
15
|
+
|
|
16
|
+
export interface WorkerPermissions {
|
|
17
|
+
read: string[] | boolean;
|
|
18
|
+
write: boolean;
|
|
19
|
+
net: boolean;
|
|
20
|
+
env: boolean;
|
|
21
|
+
run: boolean;
|
|
22
|
+
ffi: boolean;
|
|
23
|
+
sys: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Build scoped permissions for a project worker.
|
|
28
|
+
*
|
|
29
|
+
* - read: restricted to the project temp dir (transformed modules) and cache dirs
|
|
30
|
+
* - write: denied (workers produce output via postMessage, not filesystem)
|
|
31
|
+
* - net: allowed (data fetchers and API routes may call external APIs)
|
|
32
|
+
* - env: allowed (user code reads API keys and config from environment)
|
|
33
|
+
* - run: denied (no subprocess spawning from user code)
|
|
34
|
+
* - ffi: denied (no native code from user code)
|
|
35
|
+
* - sys: denied (no system info access from user code)
|
|
36
|
+
*/
|
|
37
|
+
export function buildWorkerPermissions(
|
|
38
|
+
readPaths: string[],
|
|
39
|
+
): WorkerPermissions {
|
|
40
|
+
// In compiled binaries, user modules import from the VFS temp dir which
|
|
41
|
+
// is outside the project directory. Rather than trying to enumerate all
|
|
42
|
+
// read paths, grant full read access — the security boundary is enforced
|
|
43
|
+
// by denying write/run/ffi/sys, not by restricting reads.
|
|
44
|
+
// Check for compiled binary by testing if execPath is NOT "deno"/"deno.exe"
|
|
45
|
+
try {
|
|
46
|
+
const exec = typeof dntShim.Deno !== "undefined" ? dntShim.Deno.execPath?.() : undefined;
|
|
47
|
+
if (exec) {
|
|
48
|
+
const name = exec.split(/[/\\]/).pop()?.toLowerCase() ?? "";
|
|
49
|
+
if (name !== "deno" && name !== "deno.exe") {
|
|
50
|
+
return {
|
|
51
|
+
read: true,
|
|
52
|
+
write: false,
|
|
53
|
+
net: true,
|
|
54
|
+
env: true,
|
|
55
|
+
run: false,
|
|
56
|
+
ffi: false,
|
|
57
|
+
sys: false,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
} catch {
|
|
62
|
+
// execPath may not be available
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
read: readPaths.length > 0 ? readPaths : false,
|
|
67
|
+
write: false,
|
|
68
|
+
net: true,
|
|
69
|
+
env: true,
|
|
70
|
+
run: false,
|
|
71
|
+
ffi: false,
|
|
72
|
+
sys: false,
|
|
73
|
+
};
|
|
74
|
+
}
|