@ricsam/isolate 0.1.6 → 0.1.7

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.
@@ -48,6 +48,35 @@ function isLinkerConflictError(error) {
48
48
  const message = error instanceof Error ? error.message : String(error ?? "");
49
49
  return message.includes(LINKER_CONFLICT_ERROR);
50
50
  }
51
+ function isDisposedRuntimeError(error) {
52
+ const message = error instanceof Error ? error.message : String(error ?? "");
53
+ return /runtime has been disposed|runtime was permanently disposed|runtime was soft-disposed|isolated is disposed/i.test(message);
54
+ }
55
+ function formatLifecycleReason(action, reason) {
56
+ const trimmedReason = reason?.trim();
57
+ return trimmedReason ? `${action}(${trimmedReason})` : `${action}()`;
58
+ }
59
+ function formatLogValue(value) {
60
+ if (typeof value === "string") {
61
+ return JSON.stringify(value);
62
+ }
63
+ if (typeof value === "number" || typeof value === "boolean" || value === null) {
64
+ return String(value);
65
+ }
66
+ try {
67
+ return JSON.stringify(value);
68
+ } catch {
69
+ return JSON.stringify(String(value));
70
+ }
71
+ }
72
+ function summarizeRequest(request) {
73
+ try {
74
+ const url = new URL(request.url);
75
+ return `${request.method} ${url.origin}${url.pathname}${url.search}`;
76
+ } catch {
77
+ return `${request.method} ${request.url}`;
78
+ }
79
+ }
51
80
 
52
81
  class IsolateServer {
53
82
  namespaceId;
@@ -55,6 +84,8 @@ class IsolateServer {
55
84
  runtime = null;
56
85
  lastStartOptions = null;
57
86
  lifecycleLock = Promise.resolve();
87
+ activeRequestCount = 0;
88
+ lastRuntimeRetirement = null;
58
89
  closed = true;
59
90
  fetch = {
60
91
  dispatchRequest: (request, options) => this.dispatchRequestWithRetry(request, options),
@@ -101,32 +132,78 @@ class IsolateServer {
101
132
  this.runtime = await this.createAndInitializeRuntime(options);
102
133
  });
103
134
  }
104
- async reload() {
135
+ async reload(reason) {
105
136
  const startOptions = this.lastStartOptions;
106
137
  if (!startOptions) {
107
138
  throw new Error("Server not configured. Call start() first.");
108
139
  }
140
+ const lifecycleReason = formatLifecycleReason("IsolateServer.reload", reason);
109
141
  this.closed = false;
110
142
  await this.withLifecycleLock(async () => {
111
- if (this.runtime) {
112
- const runtime = this.runtime;
143
+ const previousRuntime = this.runtime;
144
+ this.log("reload requested", {
145
+ namespaceId: this.namespaceId,
146
+ runtimeId: previousRuntime?.id ?? null,
147
+ reason: lifecycleReason,
148
+ activeRequests: this.activeRequestCount
149
+ });
150
+ if (previousRuntime) {
113
151
  this.runtime = null;
114
- await this.disposeRuntime(runtime, {
152
+ this.recordRuntimeRetirement(previousRuntime, "reload", lifecycleReason, true);
153
+ await this.disposeRuntime(previousRuntime, {
115
154
  hard: true,
116
- reason: "IsolateServer.reload()"
155
+ reason: lifecycleReason
117
156
  });
118
157
  }
119
- this.runtime = await this.createAndInitializeRuntime(startOptions);
158
+ try {
159
+ const nextRuntime = await this.createAndInitializeRuntime(startOptions);
160
+ this.runtime = nextRuntime;
161
+ } catch (error) {
162
+ this.log("reload failed", {
163
+ namespaceId: this.namespaceId,
164
+ previousRuntimeId: previousRuntime?.id ?? null,
165
+ reason: lifecycleReason,
166
+ activeRequests: this.activeRequestCount,
167
+ error: error instanceof Error ? error.message : String(error)
168
+ }, "warn");
169
+ throw error;
170
+ }
171
+ if (previousRuntime && this.lastRuntimeRetirement?.runtimeId === previousRuntime.id && this.lastRuntimeRetirement.action === "reload") {
172
+ this.lastRuntimeRetirement.replacementRuntimeId = this.runtime?.id;
173
+ }
174
+ this.log("reload completed", {
175
+ namespaceId: this.namespaceId,
176
+ previousRuntimeId: previousRuntime?.id ?? null,
177
+ runtimeId: this.runtime?.id ?? null,
178
+ reason: lifecycleReason,
179
+ activeRequests: this.activeRequestCount
180
+ });
120
181
  });
121
182
  }
122
- async close() {
183
+ async close(reason) {
184
+ const lifecycleReason = formatLifecycleReason("IsolateServer.close", reason);
123
185
  await this.withLifecycleLock(async () => {
124
- if (this.runtime) {
125
- const runtime = this.runtime;
186
+ const previousRuntime = this.runtime;
187
+ this.log("close requested", {
188
+ namespaceId: this.namespaceId,
189
+ runtimeId: previousRuntime?.id ?? null,
190
+ reason: lifecycleReason,
191
+ activeRequests: this.activeRequestCount
192
+ });
193
+ if (previousRuntime) {
126
194
  this.runtime = null;
127
- await this.disposeRuntime(runtime);
195
+ this.recordRuntimeRetirement(previousRuntime, "close", lifecycleReason, false);
196
+ await this.disposeRuntime(previousRuntime, {
197
+ reason: lifecycleReason
198
+ });
128
199
  }
129
200
  this.closed = true;
201
+ this.log("close completed", {
202
+ namespaceId: this.namespaceId,
203
+ previousRuntimeId: previousRuntime?.id ?? null,
204
+ reason: lifecycleReason,
205
+ activeRequests: this.activeRequestCount
206
+ });
130
207
  });
131
208
  }
132
209
  getRuntime() {
@@ -205,19 +282,73 @@ class IsolateServer {
205
282
  }
206
283
  return this.runtime;
207
284
  }
285
+ recordRuntimeRetirement(runtime, action, reason, hard) {
286
+ const record = {
287
+ runtimeId: runtime.id,
288
+ action,
289
+ reason,
290
+ hard,
291
+ at: Date.now(),
292
+ activeRequests: this.activeRequestCount
293
+ };
294
+ this.lastRuntimeRetirement = record;
295
+ return record;
296
+ }
297
+ getLastRuntimeRetirementLogFields() {
298
+ if (!this.lastRuntimeRetirement) {
299
+ return {
300
+ lastRetirementAction: "unknown"
301
+ };
302
+ }
303
+ return {
304
+ lastRetirementAction: this.lastRuntimeRetirement.action,
305
+ lastRetirementReason: this.lastRuntimeRetirement.reason,
306
+ lastRetirementRuntimeId: this.lastRuntimeRetirement.runtimeId,
307
+ lastRetirementHard: this.lastRuntimeRetirement.hard,
308
+ lastRetirementAgeMs: Date.now() - this.lastRuntimeRetirement.at,
309
+ lastRetirementActiveRequests: this.lastRuntimeRetirement.activeRequests,
310
+ lastRetirementReplacementRuntimeId: this.lastRuntimeRetirement.replacementRuntimeId ?? null
311
+ };
312
+ }
313
+ log(message, fields, level = "log") {
314
+ const suffix = Object.entries(fields).filter(([, value]) => value !== undefined).map(([key, value]) => `${key}=${formatLogValue(value)}`).join(" ");
315
+ const logger = level === "warn" ? console.warn : console.log;
316
+ logger(`[isolate-server] ${message}${suffix ? `; ${suffix}` : ""}`);
317
+ }
208
318
  async dispatchRequestWithRetry(request, options) {
209
- const runtime = await this.getActiveRuntime();
319
+ this.activeRequestCount += 1;
210
320
  try {
211
- return await runtime.fetch.dispatchRequest(request, options);
212
- } catch (error) {
213
- if (!isLinkerConflictError(error)) {
214
- throw error;
321
+ const runtime = await this.getActiveRuntime();
322
+ try {
323
+ return await runtime.fetch.dispatchRequest(request, options);
324
+ } catch (error) {
325
+ if (!isLinkerConflictError(error) && !isDisposedRuntimeError(error)) {
326
+ throw error;
327
+ }
328
+ const requestSummary = summarizeRequest(request);
329
+ if (isLinkerConflictError(error)) {
330
+ await this.reload(`request-linker-conflict: ${requestSummary}`);
331
+ } else if (this.runtime?.id === runtime.id) {
332
+ this.runtime = null;
333
+ }
334
+ const retryRuntime = await this.getActiveRuntime();
335
+ this.log(isLinkerConflictError(error) ? "request recovered after linker conflict" : "request recovered after disposed runtime", {
336
+ namespaceId: this.namespaceId,
337
+ request: requestSummary,
338
+ requestId: options?.requestId ?? null,
339
+ metadataKeys: Object.keys(options?.metadata ?? {}),
340
+ previousRuntimeId: runtime.id,
341
+ runtimeId: retryRuntime.id,
342
+ activeRequests: this.activeRequestCount,
343
+ error: error instanceof Error ? error.message : String(error),
344
+ ...this.getLastRuntimeRetirementLogFields()
345
+ }, "warn");
346
+ return retryRuntime.fetch.dispatchRequest(request, options);
215
347
  }
216
- await this.reload();
217
- const retryRuntime = await this.getActiveRuntime();
218
- return retryRuntime.fetch.dispatchRequest(request, options);
348
+ } finally {
349
+ this.activeRequestCount -= 1;
219
350
  }
220
351
  }
221
352
  }
222
353
 
223
- //# debugId=92DF47A502A2457564756E2164756E21
354
+ //# debugId=7EA2EF999F10580F64756E2164756E21
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/internal/server/index.ts"],
4
4
  "sourcesContent": [
5
- "import type {\n DaemonConnection,\n DispatchOptions,\n RemoteRuntime,\n RuntimeOptions,\n UpgradeRequest,\n WebSocketCommand,\n} from \"../client/index.cjs\";\nimport { isBenignDisposeError } from \"../client/index.cjs\";\n\nconst LINKER_CONFLICT_ERROR = \"Module is currently being linked by another linker\";\n\nfunction isLinkerConflictError(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error ?? \"\");\n return message.includes(LINKER_CONFLICT_ERROR);\n}\n\nexport interface IsolateServerOptions {\n namespaceId: string;\n getConnection: () => Promise<DaemonConnection>;\n}\n\nexport interface IsolateServerStartOptions {\n runtimeOptions: RuntimeOptions;\n entry: string;\n entryFilename?: string;\n onWebSocketCommand?: (cmd: WebSocketCommand) => void;\n}\n\nexport interface IsolateServerFetch {\n dispatchRequest(request: Request, options?: DispatchOptions): Promise<Response>;\n getUpgradeRequest(): Promise<UpgradeRequest | null>;\n dispatchWebSocketOpen(connectionId: string): Promise<void>;\n dispatchWebSocketMessage(connectionId: string, message: string | ArrayBuffer): Promise<void>;\n dispatchWebSocketClose(connectionId: string, code: number, reason: string): Promise<void>;\n dispatchWebSocketError(connectionId: string, error: Error): Promise<void>;\n hasServeHandler(): Promise<boolean>;\n hasActiveConnections(): Promise<boolean>;\n}\n\nexport class IsolateServer {\n private readonly namespaceId: string;\n private readonly getConnection: () => Promise<DaemonConnection>;\n private runtime: RemoteRuntime | null = null;\n private lastStartOptions: IsolateServerStartOptions | null = null;\n private lifecycleLock: Promise<void> = Promise.resolve();\n private closed = true;\n\n readonly fetch: IsolateServerFetch = {\n dispatchRequest: (request, options) => this.dispatchRequestWithRetry(request, options),\n getUpgradeRequest: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.getUpgradeRequest();\n },\n dispatchWebSocketOpen: async (connectionId) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketOpen(connectionId);\n },\n dispatchWebSocketMessage: async (connectionId, message) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketMessage(connectionId, message);\n },\n dispatchWebSocketClose: async (connectionId, code, reason) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketClose(connectionId, code, reason);\n },\n dispatchWebSocketError: async (connectionId, error) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketError(connectionId, error);\n },\n hasServeHandler: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.hasServeHandler();\n },\n hasActiveConnections: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.hasActiveConnections();\n },\n };\n\n constructor(options: IsolateServerOptions) {\n this.namespaceId = options.namespaceId;\n this.getConnection = options.getConnection;\n }\n\n async start(options: IsolateServerStartOptions): Promise<void> {\n this.lastStartOptions = options;\n this.closed = false;\n\n await this.withLifecycleLock(async () => {\n if (this.runtime) {\n return;\n }\n\n this.runtime = await this.createAndInitializeRuntime(options);\n });\n }\n\n async reload(): Promise<void> {\n const startOptions = this.lastStartOptions;\n if (!startOptions) {\n throw new Error(\"Server not configured. Call start() first.\");\n }\n\n this.closed = false;\n await this.withLifecycleLock(async () => {\n if (this.runtime) {\n const runtime = this.runtime;\n this.runtime = null;\n await this.disposeRuntime(runtime, {\n hard: true,\n reason: \"IsolateServer.reload()\",\n });\n }\n\n this.runtime = await this.createAndInitializeRuntime(startOptions);\n });\n }\n\n async close(): Promise<void> {\n await this.withLifecycleLock(async () => {\n if (this.runtime) {\n const runtime = this.runtime;\n this.runtime = null;\n await this.disposeRuntime(runtime);\n }\n\n this.closed = true;\n });\n }\n\n getRuntime(): RemoteRuntime | null {\n return this.runtime;\n }\n\n private async withLifecycleLock<T>(operation: () => Promise<T>): Promise<T> {\n const previous = this.lifecycleLock;\n let release!: () => void;\n this.lifecycleLock = new Promise<void>((resolve) => {\n release = resolve;\n });\n\n await previous;\n try {\n return await operation();\n } finally {\n release();\n }\n }\n\n private buildRuntimeOptions(options: IsolateServerStartOptions): RuntimeOptions {\n if (options.onWebSocketCommand) {\n return {\n ...options.runtimeOptions,\n onWebSocketCommand: options.onWebSocketCommand,\n };\n }\n\n return options.runtimeOptions;\n }\n\n private async createAndInitializeRuntime(\n options: IsolateServerStartOptions,\n allowRetry: boolean = true\n ): Promise<RemoteRuntime> {\n const connection = await this.getConnection();\n const namespace = connection.createNamespace(this.namespaceId);\n const runtimeOptions = this.buildRuntimeOptions(options);\n const runtime = await namespace.createRuntime(runtimeOptions);\n\n try {\n await runtime.eval(\n `import ${JSON.stringify(options.entry)};`,\n options.entryFilename ?? \"/isolate_server_entry.js\"\n );\n return runtime;\n } catch (error) {\n await this.disposeRuntime(runtime);\n if (!allowRetry || !isLinkerConflictError(error)) {\n throw error;\n }\n\n const retryRuntime = await namespace.createRuntime(runtimeOptions);\n try {\n await retryRuntime.eval(\n `import ${JSON.stringify(options.entry)};`,\n options.entryFilename ?? \"/isolate_server_entry.js\"\n );\n return retryRuntime;\n } catch (retryError) {\n await this.disposeRuntime(retryRuntime);\n throw retryError;\n }\n }\n }\n\n private async disposeRuntime(\n runtime: RemoteRuntime,\n options?: { hard?: boolean; reason?: string }\n ): Promise<void> {\n try {\n await runtime.dispose(options);\n } catch (error) {\n if (!isBenignDisposeError(error)) {\n throw error;\n }\n }\n }\n\n private async ensureStarted(): Promise<void> {\n if (this.runtime) {\n return;\n }\n\n if (!this.lastStartOptions) {\n throw new Error(\"Server not configured. Call start() first.\");\n }\n\n if (this.closed) {\n this.closed = false;\n }\n\n await this.start(this.lastStartOptions);\n }\n\n private async getActiveRuntime(): Promise<RemoteRuntime> {\n await this.ensureStarted();\n if (!this.runtime) {\n throw new Error(\"Server runtime failed to start.\");\n }\n return this.runtime;\n }\n\n private async dispatchRequestWithRetry(\n request: Request,\n options?: DispatchOptions\n ): Promise<Response> {\n const runtime = await this.getActiveRuntime();\n try {\n return await runtime.fetch.dispatchRequest(request, options);\n } catch (error) {\n if (!isLinkerConflictError(error)) {\n throw error;\n }\n\n await this.reload();\n const retryRuntime = await this.getActiveRuntime();\n return retryRuntime.fetch.dispatchRequest(request, options);\n }\n }\n}\n"
5
+ "import type {\n DaemonConnection,\n DispatchOptions,\n RemoteRuntime,\n RuntimeOptions,\n UpgradeRequest,\n WebSocketCommand,\n} from \"../client/index.cjs\";\nimport { isBenignDisposeError } from \"../client/index.cjs\";\n\nconst LINKER_CONFLICT_ERROR = \"Module is currently being linked by another linker\";\n\nfunction isLinkerConflictError(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error ?? \"\");\n return message.includes(LINKER_CONFLICT_ERROR);\n}\n\nfunction isDisposedRuntimeError(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error ?? \"\");\n return /runtime has been disposed|runtime was permanently disposed|runtime was soft-disposed|isolated is disposed/i.test(\n message\n );\n}\n\ntype RuntimeRetirementAction = \"reload\" | \"close\";\n\ninterface RuntimeRetirementRecord {\n runtimeId: string;\n action: RuntimeRetirementAction;\n reason: string;\n hard: boolean;\n at: number;\n activeRequests: number;\n replacementRuntimeId?: string;\n}\n\nfunction formatLifecycleReason(action: string, reason?: string): string {\n const trimmedReason = reason?.trim();\n return trimmedReason ? `${action}(${trimmedReason})` : `${action}()`;\n}\n\nfunction formatLogValue(value: unknown): string {\n if (typeof value === \"string\") {\n return JSON.stringify(value);\n }\n if (typeof value === \"number\" || typeof value === \"boolean\" || value === null) {\n return String(value);\n }\n\n try {\n return JSON.stringify(value);\n } catch {\n return JSON.stringify(String(value));\n }\n}\n\nfunction summarizeRequest(request: Request): string {\n try {\n const url = new URL(request.url);\n return `${request.method} ${url.origin}${url.pathname}${url.search}`;\n } catch {\n return `${request.method} ${request.url}`;\n }\n}\n\nexport interface IsolateServerOptions {\n namespaceId: string;\n getConnection: () => Promise<DaemonConnection>;\n}\n\nexport interface IsolateServerStartOptions {\n runtimeOptions: RuntimeOptions;\n entry: string;\n entryFilename?: string;\n onWebSocketCommand?: (cmd: WebSocketCommand) => void;\n}\n\nexport interface IsolateServerFetch {\n dispatchRequest(request: Request, options?: DispatchOptions): Promise<Response>;\n getUpgradeRequest(): Promise<UpgradeRequest | null>;\n dispatchWebSocketOpen(connectionId: string): Promise<void>;\n dispatchWebSocketMessage(connectionId: string, message: string | ArrayBuffer): Promise<void>;\n dispatchWebSocketClose(connectionId: string, code: number, reason: string): Promise<void>;\n dispatchWebSocketError(connectionId: string, error: Error): Promise<void>;\n hasServeHandler(): Promise<boolean>;\n hasActiveConnections(): Promise<boolean>;\n}\n\nexport class IsolateServer {\n private readonly namespaceId: string;\n private readonly getConnection: () => Promise<DaemonConnection>;\n private runtime: RemoteRuntime | null = null;\n private lastStartOptions: IsolateServerStartOptions | null = null;\n private lifecycleLock: Promise<void> = Promise.resolve();\n private activeRequestCount = 0;\n private lastRuntimeRetirement: RuntimeRetirementRecord | null = null;\n private closed = true;\n\n readonly fetch: IsolateServerFetch = {\n dispatchRequest: (request, options) => this.dispatchRequestWithRetry(request, options),\n getUpgradeRequest: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.getUpgradeRequest();\n },\n dispatchWebSocketOpen: async (connectionId) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketOpen(connectionId);\n },\n dispatchWebSocketMessage: async (connectionId, message) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketMessage(connectionId, message);\n },\n dispatchWebSocketClose: async (connectionId, code, reason) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketClose(connectionId, code, reason);\n },\n dispatchWebSocketError: async (connectionId, error) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketError(connectionId, error);\n },\n hasServeHandler: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.hasServeHandler();\n },\n hasActiveConnections: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.hasActiveConnections();\n },\n };\n\n constructor(options: IsolateServerOptions) {\n this.namespaceId = options.namespaceId;\n this.getConnection = options.getConnection;\n }\n\n async start(options: IsolateServerStartOptions): Promise<void> {\n this.lastStartOptions = options;\n this.closed = false;\n\n await this.withLifecycleLock(async () => {\n if (this.runtime) {\n return;\n }\n\n this.runtime = await this.createAndInitializeRuntime(options);\n });\n }\n\n async reload(reason?: string): Promise<void> {\n const startOptions = this.lastStartOptions;\n if (!startOptions) {\n throw new Error(\"Server not configured. Call start() first.\");\n }\n\n const lifecycleReason = formatLifecycleReason(\"IsolateServer.reload\", reason);\n this.closed = false;\n await this.withLifecycleLock(async () => {\n const previousRuntime = this.runtime;\n this.log(\"reload requested\", {\n namespaceId: this.namespaceId,\n runtimeId: previousRuntime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n });\n\n if (previousRuntime) {\n this.runtime = null;\n this.recordRuntimeRetirement(previousRuntime, \"reload\", lifecycleReason, true);\n await this.disposeRuntime(previousRuntime, {\n hard: true,\n reason: lifecycleReason,\n });\n }\n\n try {\n const nextRuntime = await this.createAndInitializeRuntime(startOptions);\n this.runtime = nextRuntime;\n } catch (error) {\n this.log(\"reload failed\", {\n namespaceId: this.namespaceId,\n previousRuntimeId: previousRuntime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n error: error instanceof Error ? error.message : String(error),\n }, \"warn\");\n throw error;\n }\n\n if (\n previousRuntime &&\n this.lastRuntimeRetirement?.runtimeId === previousRuntime.id &&\n this.lastRuntimeRetirement.action === \"reload\"\n ) {\n this.lastRuntimeRetirement.replacementRuntimeId = this.runtime?.id;\n }\n\n this.log(\"reload completed\", {\n namespaceId: this.namespaceId,\n previousRuntimeId: previousRuntime?.id ?? null,\n runtimeId: this.runtime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n });\n });\n }\n\n async close(reason?: string): Promise<void> {\n const lifecycleReason = formatLifecycleReason(\"IsolateServer.close\", reason);\n await this.withLifecycleLock(async () => {\n const previousRuntime = this.runtime;\n this.log(\"close requested\", {\n namespaceId: this.namespaceId,\n runtimeId: previousRuntime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n });\n\n if (previousRuntime) {\n this.runtime = null;\n this.recordRuntimeRetirement(previousRuntime, \"close\", lifecycleReason, false);\n await this.disposeRuntime(previousRuntime, {\n reason: lifecycleReason,\n });\n }\n\n this.closed = true;\n this.log(\"close completed\", {\n namespaceId: this.namespaceId,\n previousRuntimeId: previousRuntime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n });\n });\n }\n\n getRuntime(): RemoteRuntime | null {\n return this.runtime;\n }\n\n private async withLifecycleLock<T>(operation: () => Promise<T>): Promise<T> {\n const previous = this.lifecycleLock;\n let release!: () => void;\n this.lifecycleLock = new Promise<void>((resolve) => {\n release = resolve;\n });\n\n await previous;\n try {\n return await operation();\n } finally {\n release();\n }\n }\n\n private buildRuntimeOptions(options: IsolateServerStartOptions): RuntimeOptions {\n if (options.onWebSocketCommand) {\n return {\n ...options.runtimeOptions,\n onWebSocketCommand: options.onWebSocketCommand,\n };\n }\n\n return options.runtimeOptions;\n }\n\n private async createAndInitializeRuntime(\n options: IsolateServerStartOptions,\n allowRetry: boolean = true\n ): Promise<RemoteRuntime> {\n const connection = await this.getConnection();\n const namespace = connection.createNamespace(this.namespaceId);\n const runtimeOptions = this.buildRuntimeOptions(options);\n const runtime = await namespace.createRuntime(runtimeOptions);\n\n try {\n await runtime.eval(\n `import ${JSON.stringify(options.entry)};`,\n options.entryFilename ?? \"/isolate_server_entry.js\"\n );\n return runtime;\n } catch (error) {\n await this.disposeRuntime(runtime);\n if (!allowRetry || !isLinkerConflictError(error)) {\n throw error;\n }\n\n const retryRuntime = await namespace.createRuntime(runtimeOptions);\n try {\n await retryRuntime.eval(\n `import ${JSON.stringify(options.entry)};`,\n options.entryFilename ?? \"/isolate_server_entry.js\"\n );\n return retryRuntime;\n } catch (retryError) {\n await this.disposeRuntime(retryRuntime);\n throw retryError;\n }\n }\n }\n\n private async disposeRuntime(\n runtime: RemoteRuntime,\n options?: { hard?: boolean; reason?: string }\n ): Promise<void> {\n try {\n await runtime.dispose(options);\n } catch (error) {\n if (!isBenignDisposeError(error)) {\n throw error;\n }\n }\n }\n\n private async ensureStarted(): Promise<void> {\n if (this.runtime) {\n return;\n }\n\n if (!this.lastStartOptions) {\n throw new Error(\"Server not configured. Call start() first.\");\n }\n\n if (this.closed) {\n this.closed = false;\n }\n\n await this.start(this.lastStartOptions);\n }\n\n private async getActiveRuntime(): Promise<RemoteRuntime> {\n await this.ensureStarted();\n if (!this.runtime) {\n throw new Error(\"Server runtime failed to start.\");\n }\n return this.runtime;\n }\n\n private recordRuntimeRetirement(\n runtime: RemoteRuntime,\n action: RuntimeRetirementAction,\n reason: string,\n hard: boolean\n ): RuntimeRetirementRecord {\n const record: RuntimeRetirementRecord = {\n runtimeId: runtime.id,\n action,\n reason,\n hard,\n at: Date.now(),\n activeRequests: this.activeRequestCount,\n };\n this.lastRuntimeRetirement = record;\n return record;\n }\n\n private getLastRuntimeRetirementLogFields(): Record<string, unknown> {\n if (!this.lastRuntimeRetirement) {\n return {\n lastRetirementAction: \"unknown\",\n };\n }\n\n return {\n lastRetirementAction: this.lastRuntimeRetirement.action,\n lastRetirementReason: this.lastRuntimeRetirement.reason,\n lastRetirementRuntimeId: this.lastRuntimeRetirement.runtimeId,\n lastRetirementHard: this.lastRuntimeRetirement.hard,\n lastRetirementAgeMs: Date.now() - this.lastRuntimeRetirement.at,\n lastRetirementActiveRequests: this.lastRuntimeRetirement.activeRequests,\n lastRetirementReplacementRuntimeId: this.lastRuntimeRetirement.replacementRuntimeId ?? null,\n };\n }\n\n private log(\n message: string,\n fields: Record<string, unknown>,\n level: \"log\" | \"warn\" = \"log\"\n ): void {\n const suffix = Object.entries(fields)\n .filter(([, value]) => value !== undefined)\n .map(([key, value]) => `${key}=${formatLogValue(value)}`)\n .join(\" \");\n\n const logger = level === \"warn\" ? console.warn : console.log;\n logger(`[isolate-server] ${message}${suffix ? `; ${suffix}` : \"\"}`);\n }\n\n private async dispatchRequestWithRetry(\n request: Request,\n options?: DispatchOptions\n ): Promise<Response> {\n this.activeRequestCount += 1;\n try {\n const runtime = await this.getActiveRuntime();\n try {\n return await runtime.fetch.dispatchRequest(request, options);\n } catch (error) {\n if (!isLinkerConflictError(error) && !isDisposedRuntimeError(error)) {\n throw error;\n }\n\n const requestSummary = summarizeRequest(request);\n if (isLinkerConflictError(error)) {\n await this.reload(`request-linker-conflict: ${requestSummary}`);\n } else if (this.runtime?.id === runtime.id) {\n this.runtime = null;\n }\n\n const retryRuntime = await this.getActiveRuntime();\n this.log(\n isLinkerConflictError(error)\n ? \"request recovered after linker conflict\"\n : \"request recovered after disposed runtime\",\n {\n namespaceId: this.namespaceId,\n request: requestSummary,\n requestId: options?.requestId ?? null,\n metadataKeys: Object.keys(options?.metadata ?? {}),\n previousRuntimeId: runtime.id,\n runtimeId: retryRuntime.id,\n activeRequests: this.activeRequestCount,\n error: error instanceof Error ? error.message : String(error),\n ...this.getLastRuntimeRetirementLogFields(),\n },\n \"warn\"\n );\n return retryRuntime.fetch.dispatchRequest(request, options);\n }\n } finally {\n this.activeRequestCount -= 1;\n }\n }\n}\n"
6
6
  ],
7
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQqC,IAArC;AAEA,IAAM,wBAAwB;AAE9B,SAAS,qBAAqB,CAAC,OAAyB;AAAA,EACtD,MAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,SAAS,EAAE;AAAA,EAC3E,OAAO,QAAQ,SAAS,qBAAqB;AAAA;AAAA;AA0BxC,MAAM,cAAc;AAAA,EACR;AAAA,EACA;AAAA,EACT,UAAgC;AAAA,EAChC,mBAAqD;AAAA,EACrD,gBAA+B,QAAQ,QAAQ;AAAA,EAC/C,SAAS;AAAA,EAER,QAA4B;AAAA,IACnC,iBAAiB,CAAC,SAAS,YAAY,KAAK,yBAAyB,SAAS,OAAO;AAAA,IACrF,mBAAmB,YAAY;AAAA,MAC7B,MAAM,UAAU,MAAM,KA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UACnE,MAAM;AAAA,QACR;AAAA,QAEA,MAAM,iBAAiB,iBAAiB,OAAO;AAAA,QAC/C,IAAI,sBAAsB,KAAK,GAAG;AAAA,UAChC,MAAM,KAAK,OAAO,4BAA4B,gBAAgB;AAAA,QAChE,EAAO,SAAI,KAAK,SAAS,OAAO,QAAQ,IAAI;AAAA,UAC1C,KAAK,UAAU;AAAA,QACjB;AAAA,QAEA,MAAM,eAAe,MAAM,KAAK,iBAAiB;AAAA,QACjD,KAAK,IACH,sBAAsB,KAAK,IACvB,4CACA,4CACJ;AAAA,UACE,aAAa,KAAK;AAAA,UAClB,SAAS;AAAA,UACT,WAAW,SAAS,aAAa;AAAA,UACjC,cAAc,OAAO,KAAK,SAAS,YAAY,CAAC,CAAC;AAAA,UACjD,mBAAmB,QAAQ;AAAA,UAC3B,WAAW,aAAa;AAAA,UACxB,gBAAgB,KAAK;AAAA,UACrB,OAAO,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AAAA,aACzD,KAAK,kCAAkC;AAAA,QAC5C,GACA,MACF;AAAA,QACA,OAAO,aAAa,MAAM,gBAAgB,SAAS,OAAO;AAAA;AAAA,cAE5D;AAAA,MACA,KAAK,sBAAsB;AAAA;AAAA;AAGjC;",
8
+ "debugId": "7EA2EF999F10580F64756E2164756E21",
9
9
  "names": []
10
10
  }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/isolate",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "type": "commonjs"
5
5
  }
@@ -122,11 +122,11 @@ async function createAppServerAdapter(getConnection, options) {
122
122
  await server.fetch.dispatchWebSocketError(connectionId, error);
123
123
  }
124
124
  },
125
- reload: async () => {
125
+ reload: async (reason) => {
126
126
  diagnostics.lifecycleState = "reloading";
127
127
  try {
128
- bindingsAdapter.reset("AppServer.reload()");
129
- await server.reload();
128
+ bindingsAdapter.reset(reason ? `AppServer.reload(${reason})` : "AppServer.reload()");
129
+ await server.reload(reason);
130
130
  runtimeId = server.getRuntime()?.id ?? options.key;
131
131
  } finally {
132
132
  diagnostics.lifecycleState = "idle";
@@ -136,14 +136,16 @@ async function createAppServerAdapter(getConnection, options) {
136
136
  diagnostics.lifecycleState = "disposing";
137
137
  try {
138
138
  if (disposeOptions?.hard) {
139
- bindingsAdapter.reset(disposeOptions?.reason ?? "AppServer.dispose(hard)");
140
- await server.reload();
141
- bindingsAdapter.abort(disposeOptions?.reason ?? "AppServer.dispose(hard)");
142
- await server.close();
139
+ const hardDisposeReason = disposeOptions?.reason ? `AppServer.dispose(hard): ${disposeOptions.reason}` : "AppServer.dispose(hard)";
140
+ bindingsAdapter.reset(hardDisposeReason);
141
+ await server.reload(hardDisposeReason);
142
+ bindingsAdapter.abort(hardDisposeReason);
143
+ await server.close(hardDisposeReason);
143
144
  return;
144
145
  }
145
- bindingsAdapter.abort(disposeOptions?.reason ?? "AppServer.dispose()");
146
- await server.close();
146
+ const disposeReason = disposeOptions?.reason ? `AppServer.dispose(): ${disposeOptions.reason}` : "AppServer.dispose()";
147
+ bindingsAdapter.abort(disposeReason);
148
+ await server.close(disposeReason);
147
149
  } finally {
148
150
  diagnostics.lifecycleState = "idle";
149
151
  }
@@ -155,4 +157,4 @@ async function createAppServerAdapter(getConnection, options) {
155
157
  };
156
158
  }
157
159
 
158
- //# debugId=035A30B7A1437CB364756E2164756E21
160
+ //# debugId=34E29BF0A88C356464756E2164756E21
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["../../../src/server/app-server.ts"],
4
4
  "sourcesContent": [
5
- "import { IsolateServer } from \"../internal/server/index.cjs\";\nimport type { DaemonConnection } from \"../internal/client/index.cjs\";\nimport { createRuntimeDiagnostics } from \"../bridge/diagnostics.cjs\";\nimport { createRuntimeBindingsAdapter } from \"../bridge/runtime-bindings.cjs\";\nimport { withRequestContext } from \"../bridge/request-context.cjs\";\nimport type { AppServer, CreateAppServerOptions, RequestResult } from \"../types.cjs\";\n\nexport async function createAppServerAdapter(\n getConnection: () => Promise<DaemonConnection>,\n options: CreateAppServerOptions,\n): Promise<AppServer> {\n const diagnostics = createRuntimeDiagnostics();\n const server = new IsolateServer({\n namespaceId: options.key,\n getConnection,\n });\n\n let runtimeId = options.key;\n const bindingsAdapter = createRuntimeBindingsAdapter(\n options.bindings,\n () => runtimeId,\n diagnostics,\n );\n await server.start({\n entry: options.entry,\n entryFilename: options.entryFilename,\n runtimeOptions: {\n ...bindingsAdapter.runtimeOptions,\n cwd: options.cwd,\n memoryLimitMB: options.memoryLimitMB,\n executionTimeout: options.executionTimeout,\n testEnvironment: options.features?.tests ?? false,\n },\n onWebSocketCommand: (command) => {\n options.webSockets?.onCommand?.(command);\n },\n });\n runtimeId = server.getRuntime()?.id ?? options.key;\n\n async function handleRequest(request: Request, handleOptions?: { requestId?: string; signal?: AbortSignal; metadata?: Record<string, string> }): Promise<RequestResult> {\n diagnostics.activeRequests += 1;\n diagnostics.lifecycleState = \"active\";\n try {\n return await withRequestContext(\n {\n requestId: handleOptions?.requestId,\n metadata: handleOptions?.metadata,\n signal: handleOptions?.signal ?? request.signal,\n },\n async () => {\n const response = await server.fetch.dispatchRequest(request, {\n signal: handleOptions?.signal,\n requestId: handleOptions?.requestId,\n metadata: handleOptions?.metadata,\n });\n const upgradeRequest = await server.fetch.getUpgradeRequest();\n if (upgradeRequest?.requested) {\n return {\n type: \"websocket\",\n upgradeData: {\n requested: upgradeRequest.requested,\n connectionId: upgradeRequest.connectionId,\n },\n };\n }\n\n return {\n type: \"response\",\n response,\n };\n },\n );\n } catch (error) {\n diagnostics.lastError = error instanceof Error ? error.message : String(error);\n throw error;\n } finally {\n diagnostics.activeRequests -= 1;\n diagnostics.lifecycleState = \"idle\";\n }\n }\n\n return {\n handle: handleRequest,\n ws: {\n open: async (connectionId) => {\n await server.fetch.dispatchWebSocketOpen(connectionId);\n },\n message: async (connectionId, data) => {\n await server.fetch.dispatchWebSocketMessage(connectionId, data);\n },\n close: async (connectionId, code, reason) => {\n await server.fetch.dispatchWebSocketClose(connectionId, code, reason);\n },\n error: async (connectionId, error) => {\n await server.fetch.dispatchWebSocketError(connectionId, error);\n },\n },\n reload: async () => {\n diagnostics.lifecycleState = \"reloading\";\n try {\n bindingsAdapter.reset(\"AppServer.reload()\");\n await server.reload();\n runtimeId = server.getRuntime()?.id ?? options.key;\n } finally {\n diagnostics.lifecycleState = \"idle\";\n }\n },\n dispose: async (disposeOptions) => {\n diagnostics.lifecycleState = \"disposing\";\n try {\n if (disposeOptions?.hard) {\n bindingsAdapter.reset(disposeOptions?.reason ?? \"AppServer.dispose(hard)\");\n await server.reload();\n bindingsAdapter.abort(disposeOptions?.reason ?? \"AppServer.dispose(hard)\");\n await server.close();\n return;\n }\n bindingsAdapter.abort(disposeOptions?.reason ?? \"AppServer.dispose()\");\n await server.close();\n } finally {\n diagnostics.lifecycleState = \"idle\";\n }\n },\n diagnostics: async () => ({\n ...diagnostics,\n reused: server.getRuntime()?.reused,\n }),\n };\n}\n"
5
+ "import { IsolateServer } from \"../internal/server/index.cjs\";\nimport type { DaemonConnection } from \"../internal/client/index.cjs\";\nimport { createRuntimeDiagnostics } from \"../bridge/diagnostics.cjs\";\nimport { createRuntimeBindingsAdapter } from \"../bridge/runtime-bindings.cjs\";\nimport { withRequestContext } from \"../bridge/request-context.cjs\";\nimport type { AppServer, CreateAppServerOptions, RequestResult } from \"../types.cjs\";\n\nexport async function createAppServerAdapter(\n getConnection: () => Promise<DaemonConnection>,\n options: CreateAppServerOptions,\n): Promise<AppServer> {\n const diagnostics = createRuntimeDiagnostics();\n const server = new IsolateServer({\n namespaceId: options.key,\n getConnection,\n });\n\n let runtimeId = options.key;\n const bindingsAdapter = createRuntimeBindingsAdapter(\n options.bindings,\n () => runtimeId,\n diagnostics,\n );\n await server.start({\n entry: options.entry,\n entryFilename: options.entryFilename,\n runtimeOptions: {\n ...bindingsAdapter.runtimeOptions,\n cwd: options.cwd,\n memoryLimitMB: options.memoryLimitMB,\n executionTimeout: options.executionTimeout,\n testEnvironment: options.features?.tests ?? false,\n },\n onWebSocketCommand: (command) => {\n options.webSockets?.onCommand?.(command);\n },\n });\n runtimeId = server.getRuntime()?.id ?? options.key;\n\n async function handleRequest(request: Request, handleOptions?: { requestId?: string; signal?: AbortSignal; metadata?: Record<string, string> }): Promise<RequestResult> {\n diagnostics.activeRequests += 1;\n diagnostics.lifecycleState = \"active\";\n try {\n return await withRequestContext(\n {\n requestId: handleOptions?.requestId,\n metadata: handleOptions?.metadata,\n signal: handleOptions?.signal ?? request.signal,\n },\n async () => {\n const response = await server.fetch.dispatchRequest(request, {\n signal: handleOptions?.signal,\n requestId: handleOptions?.requestId,\n metadata: handleOptions?.metadata,\n });\n const upgradeRequest = await server.fetch.getUpgradeRequest();\n if (upgradeRequest?.requested) {\n return {\n type: \"websocket\",\n upgradeData: {\n requested: upgradeRequest.requested,\n connectionId: upgradeRequest.connectionId,\n },\n };\n }\n\n return {\n type: \"response\",\n response,\n };\n },\n );\n } catch (error) {\n diagnostics.lastError = error instanceof Error ? error.message : String(error);\n throw error;\n } finally {\n diagnostics.activeRequests -= 1;\n diagnostics.lifecycleState = \"idle\";\n }\n }\n\n return {\n handle: handleRequest,\n ws: {\n open: async (connectionId) => {\n await server.fetch.dispatchWebSocketOpen(connectionId);\n },\n message: async (connectionId, data) => {\n await server.fetch.dispatchWebSocketMessage(connectionId, data);\n },\n close: async (connectionId, code, reason) => {\n await server.fetch.dispatchWebSocketClose(connectionId, code, reason);\n },\n error: async (connectionId, error) => {\n await server.fetch.dispatchWebSocketError(connectionId, error);\n },\n },\n reload: async (reason) => {\n diagnostics.lifecycleState = \"reloading\";\n try {\n bindingsAdapter.reset(reason ? `AppServer.reload(${reason})` : \"AppServer.reload()\");\n await server.reload(reason);\n runtimeId = server.getRuntime()?.id ?? options.key;\n } finally {\n diagnostics.lifecycleState = \"idle\";\n }\n },\n dispose: async (disposeOptions) => {\n diagnostics.lifecycleState = \"disposing\";\n try {\n if (disposeOptions?.hard) {\n const hardDisposeReason = disposeOptions?.reason\n ? `AppServer.dispose(hard): ${disposeOptions.reason}`\n : \"AppServer.dispose(hard)\";\n bindingsAdapter.reset(hardDisposeReason);\n await server.reload(hardDisposeReason);\n bindingsAdapter.abort(hardDisposeReason);\n await server.close(hardDisposeReason);\n return;\n }\n const disposeReason = disposeOptions?.reason\n ? `AppServer.dispose(): ${disposeOptions.reason}`\n : \"AppServer.dispose()\";\n bindingsAdapter.abort(disposeReason);\n await server.close(disposeReason);\n } finally {\n diagnostics.lifecycleState = \"idle\";\n }\n },\n diagnostics: async () => ({\n ...diagnostics,\n reused: server.getRuntime()?.reused,\n }),\n };\n}\n"
6
6
  ],
7
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAA8B,IAA9B;AAEyC,IAAzC;AAC6C,IAA7C;AACmC,IAAnC;AAGA,eAAsB,sBAAsB,CAC1C,eACA,SACoB;AAAA,EACpB,MAAM,cAAc,4CAAyB;AAAA,EAC7C,MAAM,SAAS,IAAI,4BAAc;AAAA,IAC/B,aAAa,QAAQ;AAAA,IACrB;AAAA,EACF,CAAC;AAAA,EAED,IAAI,YAAY,QAAQ;AAAA,EACxB,MAAM,kBAAkB,qDACtB,QAAQ,UACR,MAAM,WACN,WACF;AAAA,EACA,MAAM,OAAO,MAAM;AAAA,IACjB,OAAO,QAAQ;AAAA,IACf,eAAe,QAAQ;AAAA,IACvB,gBAAgB;AAAA,SACX,gBAAgB;AAAA,MACnB,KAAK,QAAQ;AAAA,MACb,eAAe,QAAQ;AAAA,MACvB,kBAAkB,QAAQ;AAAA,MAC1B,iBAAiB,QAAQ,UAAU,SAAS;AAAA,IAC9C;AAAA,IACA,oBAAoB,CAAC,YAAY;AAAA,MAC/B,QAAQ,YAAY,YAAY,OAAO;AAAA;AAAA,EAE3C,CAAC;AAAA,EACD,YAAY,OAAO,WAAW,GAAG,MAAM,QAAQ;AAAA,EAE/C,eAAe,aAAa,CAAC,SAAkB,eAAyH;AAAA,IACt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,YAAY,iBAAiB;AAAA;AAAA;AAAA,IAGjC,aAAa,aAAa;AAAA,SACrB;AAAA,MACH,QAAQ,OAAO,WAAW,GAAG;AAAA,IAC/B;AAAA,EACF;AAAA;",
8
+ "debugId": "34E29BF0A88C356464756E2164756E21",
9
9
  "names": []
10
10
  }
@@ -5,6 +5,35 @@ function isLinkerConflictError(error) {
5
5
  const message = error instanceof Error ? error.message : String(error ?? "");
6
6
  return message.includes(LINKER_CONFLICT_ERROR);
7
7
  }
8
+ function isDisposedRuntimeError(error) {
9
+ const message = error instanceof Error ? error.message : String(error ?? "");
10
+ return /runtime has been disposed|runtime was permanently disposed|runtime was soft-disposed|isolated is disposed/i.test(message);
11
+ }
12
+ function formatLifecycleReason(action, reason) {
13
+ const trimmedReason = reason?.trim();
14
+ return trimmedReason ? `${action}(${trimmedReason})` : `${action}()`;
15
+ }
16
+ function formatLogValue(value) {
17
+ if (typeof value === "string") {
18
+ return JSON.stringify(value);
19
+ }
20
+ if (typeof value === "number" || typeof value === "boolean" || value === null) {
21
+ return String(value);
22
+ }
23
+ try {
24
+ return JSON.stringify(value);
25
+ } catch {
26
+ return JSON.stringify(String(value));
27
+ }
28
+ }
29
+ function summarizeRequest(request) {
30
+ try {
31
+ const url = new URL(request.url);
32
+ return `${request.method} ${url.origin}${url.pathname}${url.search}`;
33
+ } catch {
34
+ return `${request.method} ${request.url}`;
35
+ }
36
+ }
8
37
 
9
38
  class IsolateServer {
10
39
  namespaceId;
@@ -12,6 +41,8 @@ class IsolateServer {
12
41
  runtime = null;
13
42
  lastStartOptions = null;
14
43
  lifecycleLock = Promise.resolve();
44
+ activeRequestCount = 0;
45
+ lastRuntimeRetirement = null;
15
46
  closed = true;
16
47
  fetch = {
17
48
  dispatchRequest: (request, options) => this.dispatchRequestWithRetry(request, options),
@@ -58,32 +89,78 @@ class IsolateServer {
58
89
  this.runtime = await this.createAndInitializeRuntime(options);
59
90
  });
60
91
  }
61
- async reload() {
92
+ async reload(reason) {
62
93
  const startOptions = this.lastStartOptions;
63
94
  if (!startOptions) {
64
95
  throw new Error("Server not configured. Call start() first.");
65
96
  }
97
+ const lifecycleReason = formatLifecycleReason("IsolateServer.reload", reason);
66
98
  this.closed = false;
67
99
  await this.withLifecycleLock(async () => {
68
- if (this.runtime) {
69
- const runtime = this.runtime;
100
+ const previousRuntime = this.runtime;
101
+ this.log("reload requested", {
102
+ namespaceId: this.namespaceId,
103
+ runtimeId: previousRuntime?.id ?? null,
104
+ reason: lifecycleReason,
105
+ activeRequests: this.activeRequestCount
106
+ });
107
+ if (previousRuntime) {
70
108
  this.runtime = null;
71
- await this.disposeRuntime(runtime, {
109
+ this.recordRuntimeRetirement(previousRuntime, "reload", lifecycleReason, true);
110
+ await this.disposeRuntime(previousRuntime, {
72
111
  hard: true,
73
- reason: "IsolateServer.reload()"
112
+ reason: lifecycleReason
74
113
  });
75
114
  }
76
- this.runtime = await this.createAndInitializeRuntime(startOptions);
115
+ try {
116
+ const nextRuntime = await this.createAndInitializeRuntime(startOptions);
117
+ this.runtime = nextRuntime;
118
+ } catch (error) {
119
+ this.log("reload failed", {
120
+ namespaceId: this.namespaceId,
121
+ previousRuntimeId: previousRuntime?.id ?? null,
122
+ reason: lifecycleReason,
123
+ activeRequests: this.activeRequestCount,
124
+ error: error instanceof Error ? error.message : String(error)
125
+ }, "warn");
126
+ throw error;
127
+ }
128
+ if (previousRuntime && this.lastRuntimeRetirement?.runtimeId === previousRuntime.id && this.lastRuntimeRetirement.action === "reload") {
129
+ this.lastRuntimeRetirement.replacementRuntimeId = this.runtime?.id;
130
+ }
131
+ this.log("reload completed", {
132
+ namespaceId: this.namespaceId,
133
+ previousRuntimeId: previousRuntime?.id ?? null,
134
+ runtimeId: this.runtime?.id ?? null,
135
+ reason: lifecycleReason,
136
+ activeRequests: this.activeRequestCount
137
+ });
77
138
  });
78
139
  }
79
- async close() {
140
+ async close(reason) {
141
+ const lifecycleReason = formatLifecycleReason("IsolateServer.close", reason);
80
142
  await this.withLifecycleLock(async () => {
81
- if (this.runtime) {
82
- const runtime = this.runtime;
143
+ const previousRuntime = this.runtime;
144
+ this.log("close requested", {
145
+ namespaceId: this.namespaceId,
146
+ runtimeId: previousRuntime?.id ?? null,
147
+ reason: lifecycleReason,
148
+ activeRequests: this.activeRequestCount
149
+ });
150
+ if (previousRuntime) {
83
151
  this.runtime = null;
84
- await this.disposeRuntime(runtime);
152
+ this.recordRuntimeRetirement(previousRuntime, "close", lifecycleReason, false);
153
+ await this.disposeRuntime(previousRuntime, {
154
+ reason: lifecycleReason
155
+ });
85
156
  }
86
157
  this.closed = true;
158
+ this.log("close completed", {
159
+ namespaceId: this.namespaceId,
160
+ previousRuntimeId: previousRuntime?.id ?? null,
161
+ reason: lifecycleReason,
162
+ activeRequests: this.activeRequestCount
163
+ });
87
164
  });
88
165
  }
89
166
  getRuntime() {
@@ -162,17 +239,71 @@ class IsolateServer {
162
239
  }
163
240
  return this.runtime;
164
241
  }
242
+ recordRuntimeRetirement(runtime, action, reason, hard) {
243
+ const record = {
244
+ runtimeId: runtime.id,
245
+ action,
246
+ reason,
247
+ hard,
248
+ at: Date.now(),
249
+ activeRequests: this.activeRequestCount
250
+ };
251
+ this.lastRuntimeRetirement = record;
252
+ return record;
253
+ }
254
+ getLastRuntimeRetirementLogFields() {
255
+ if (!this.lastRuntimeRetirement) {
256
+ return {
257
+ lastRetirementAction: "unknown"
258
+ };
259
+ }
260
+ return {
261
+ lastRetirementAction: this.lastRuntimeRetirement.action,
262
+ lastRetirementReason: this.lastRuntimeRetirement.reason,
263
+ lastRetirementRuntimeId: this.lastRuntimeRetirement.runtimeId,
264
+ lastRetirementHard: this.lastRuntimeRetirement.hard,
265
+ lastRetirementAgeMs: Date.now() - this.lastRuntimeRetirement.at,
266
+ lastRetirementActiveRequests: this.lastRuntimeRetirement.activeRequests,
267
+ lastRetirementReplacementRuntimeId: this.lastRuntimeRetirement.replacementRuntimeId ?? null
268
+ };
269
+ }
270
+ log(message, fields, level = "log") {
271
+ const suffix = Object.entries(fields).filter(([, value]) => value !== undefined).map(([key, value]) => `${key}=${formatLogValue(value)}`).join(" ");
272
+ const logger = level === "warn" ? console.warn : console.log;
273
+ logger(`[isolate-server] ${message}${suffix ? `; ${suffix}` : ""}`);
274
+ }
165
275
  async dispatchRequestWithRetry(request, options) {
166
- const runtime = await this.getActiveRuntime();
276
+ this.activeRequestCount += 1;
167
277
  try {
168
- return await runtime.fetch.dispatchRequest(request, options);
169
- } catch (error) {
170
- if (!isLinkerConflictError(error)) {
171
- throw error;
278
+ const runtime = await this.getActiveRuntime();
279
+ try {
280
+ return await runtime.fetch.dispatchRequest(request, options);
281
+ } catch (error) {
282
+ if (!isLinkerConflictError(error) && !isDisposedRuntimeError(error)) {
283
+ throw error;
284
+ }
285
+ const requestSummary = summarizeRequest(request);
286
+ if (isLinkerConflictError(error)) {
287
+ await this.reload(`request-linker-conflict: ${requestSummary}`);
288
+ } else if (this.runtime?.id === runtime.id) {
289
+ this.runtime = null;
290
+ }
291
+ const retryRuntime = await this.getActiveRuntime();
292
+ this.log(isLinkerConflictError(error) ? "request recovered after linker conflict" : "request recovered after disposed runtime", {
293
+ namespaceId: this.namespaceId,
294
+ request: requestSummary,
295
+ requestId: options?.requestId ?? null,
296
+ metadataKeys: Object.keys(options?.metadata ?? {}),
297
+ previousRuntimeId: runtime.id,
298
+ runtimeId: retryRuntime.id,
299
+ activeRequests: this.activeRequestCount,
300
+ error: error instanceof Error ? error.message : String(error),
301
+ ...this.getLastRuntimeRetirementLogFields()
302
+ }, "warn");
303
+ return retryRuntime.fetch.dispatchRequest(request, options);
172
304
  }
173
- await this.reload();
174
- const retryRuntime = await this.getActiveRuntime();
175
- return retryRuntime.fetch.dispatchRequest(request, options);
305
+ } finally {
306
+ this.activeRequestCount -= 1;
176
307
  }
177
308
  }
178
309
  }
@@ -180,4 +311,4 @@ export {
180
311
  IsolateServer
181
312
  };
182
313
 
183
- //# debugId=316F211C11A5AD4564756E2164756E21
314
+ //# debugId=E1A61983BC24D6F664756E2164756E21
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/internal/server/index.ts"],
4
4
  "sourcesContent": [
5
- "import type {\n DaemonConnection,\n DispatchOptions,\n RemoteRuntime,\n RuntimeOptions,\n UpgradeRequest,\n WebSocketCommand,\n} from \"../client/index.mjs\";\nimport { isBenignDisposeError } from \"../client/index.mjs\";\n\nconst LINKER_CONFLICT_ERROR = \"Module is currently being linked by another linker\";\n\nfunction isLinkerConflictError(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error ?? \"\");\n return message.includes(LINKER_CONFLICT_ERROR);\n}\n\nexport interface IsolateServerOptions {\n namespaceId: string;\n getConnection: () => Promise<DaemonConnection>;\n}\n\nexport interface IsolateServerStartOptions {\n runtimeOptions: RuntimeOptions;\n entry: string;\n entryFilename?: string;\n onWebSocketCommand?: (cmd: WebSocketCommand) => void;\n}\n\nexport interface IsolateServerFetch {\n dispatchRequest(request: Request, options?: DispatchOptions): Promise<Response>;\n getUpgradeRequest(): Promise<UpgradeRequest | null>;\n dispatchWebSocketOpen(connectionId: string): Promise<void>;\n dispatchWebSocketMessage(connectionId: string, message: string | ArrayBuffer): Promise<void>;\n dispatchWebSocketClose(connectionId: string, code: number, reason: string): Promise<void>;\n dispatchWebSocketError(connectionId: string, error: Error): Promise<void>;\n hasServeHandler(): Promise<boolean>;\n hasActiveConnections(): Promise<boolean>;\n}\n\nexport class IsolateServer {\n private readonly namespaceId: string;\n private readonly getConnection: () => Promise<DaemonConnection>;\n private runtime: RemoteRuntime | null = null;\n private lastStartOptions: IsolateServerStartOptions | null = null;\n private lifecycleLock: Promise<void> = Promise.resolve();\n private closed = true;\n\n readonly fetch: IsolateServerFetch = {\n dispatchRequest: (request, options) => this.dispatchRequestWithRetry(request, options),\n getUpgradeRequest: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.getUpgradeRequest();\n },\n dispatchWebSocketOpen: async (connectionId) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketOpen(connectionId);\n },\n dispatchWebSocketMessage: async (connectionId, message) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketMessage(connectionId, message);\n },\n dispatchWebSocketClose: async (connectionId, code, reason) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketClose(connectionId, code, reason);\n },\n dispatchWebSocketError: async (connectionId, error) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketError(connectionId, error);\n },\n hasServeHandler: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.hasServeHandler();\n },\n hasActiveConnections: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.hasActiveConnections();\n },\n };\n\n constructor(options: IsolateServerOptions) {\n this.namespaceId = options.namespaceId;\n this.getConnection = options.getConnection;\n }\n\n async start(options: IsolateServerStartOptions): Promise<void> {\n this.lastStartOptions = options;\n this.closed = false;\n\n await this.withLifecycleLock(async () => {\n if (this.runtime) {\n return;\n }\n\n this.runtime = await this.createAndInitializeRuntime(options);\n });\n }\n\n async reload(): Promise<void> {\n const startOptions = this.lastStartOptions;\n if (!startOptions) {\n throw new Error(\"Server not configured. Call start() first.\");\n }\n\n this.closed = false;\n await this.withLifecycleLock(async () => {\n if (this.runtime) {\n const runtime = this.runtime;\n this.runtime = null;\n await this.disposeRuntime(runtime, {\n hard: true,\n reason: \"IsolateServer.reload()\",\n });\n }\n\n this.runtime = await this.createAndInitializeRuntime(startOptions);\n });\n }\n\n async close(): Promise<void> {\n await this.withLifecycleLock(async () => {\n if (this.runtime) {\n const runtime = this.runtime;\n this.runtime = null;\n await this.disposeRuntime(runtime);\n }\n\n this.closed = true;\n });\n }\n\n getRuntime(): RemoteRuntime | null {\n return this.runtime;\n }\n\n private async withLifecycleLock<T>(operation: () => Promise<T>): Promise<T> {\n const previous = this.lifecycleLock;\n let release!: () => void;\n this.lifecycleLock = new Promise<void>((resolve) => {\n release = resolve;\n });\n\n await previous;\n try {\n return await operation();\n } finally {\n release();\n }\n }\n\n private buildRuntimeOptions(options: IsolateServerStartOptions): RuntimeOptions {\n if (options.onWebSocketCommand) {\n return {\n ...options.runtimeOptions,\n onWebSocketCommand: options.onWebSocketCommand,\n };\n }\n\n return options.runtimeOptions;\n }\n\n private async createAndInitializeRuntime(\n options: IsolateServerStartOptions,\n allowRetry: boolean = true\n ): Promise<RemoteRuntime> {\n const connection = await this.getConnection();\n const namespace = connection.createNamespace(this.namespaceId);\n const runtimeOptions = this.buildRuntimeOptions(options);\n const runtime = await namespace.createRuntime(runtimeOptions);\n\n try {\n await runtime.eval(\n `import ${JSON.stringify(options.entry)};`,\n options.entryFilename ?? \"/isolate_server_entry.js\"\n );\n return runtime;\n } catch (error) {\n await this.disposeRuntime(runtime);\n if (!allowRetry || !isLinkerConflictError(error)) {\n throw error;\n }\n\n const retryRuntime = await namespace.createRuntime(runtimeOptions);\n try {\n await retryRuntime.eval(\n `import ${JSON.stringify(options.entry)};`,\n options.entryFilename ?? \"/isolate_server_entry.js\"\n );\n return retryRuntime;\n } catch (retryError) {\n await this.disposeRuntime(retryRuntime);\n throw retryError;\n }\n }\n }\n\n private async disposeRuntime(\n runtime: RemoteRuntime,\n options?: { hard?: boolean; reason?: string }\n ): Promise<void> {\n try {\n await runtime.dispose(options);\n } catch (error) {\n if (!isBenignDisposeError(error)) {\n throw error;\n }\n }\n }\n\n private async ensureStarted(): Promise<void> {\n if (this.runtime) {\n return;\n }\n\n if (!this.lastStartOptions) {\n throw new Error(\"Server not configured. Call start() first.\");\n }\n\n if (this.closed) {\n this.closed = false;\n }\n\n await this.start(this.lastStartOptions);\n }\n\n private async getActiveRuntime(): Promise<RemoteRuntime> {\n await this.ensureStarted();\n if (!this.runtime) {\n throw new Error(\"Server runtime failed to start.\");\n }\n return this.runtime;\n }\n\n private async dispatchRequestWithRetry(\n request: Request,\n options?: DispatchOptions\n ): Promise<Response> {\n const runtime = await this.getActiveRuntime();\n try {\n return await runtime.fetch.dispatchRequest(request, options);\n } catch (error) {\n if (!isLinkerConflictError(error)) {\n throw error;\n }\n\n await this.reload();\n const retryRuntime = await this.getActiveRuntime();\n return retryRuntime.fetch.dispatchRequest(request, options);\n }\n }\n}\n"
5
+ "import type {\n DaemonConnection,\n DispatchOptions,\n RemoteRuntime,\n RuntimeOptions,\n UpgradeRequest,\n WebSocketCommand,\n} from \"../client/index.mjs\";\nimport { isBenignDisposeError } from \"../client/index.mjs\";\n\nconst LINKER_CONFLICT_ERROR = \"Module is currently being linked by another linker\";\n\nfunction isLinkerConflictError(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error ?? \"\");\n return message.includes(LINKER_CONFLICT_ERROR);\n}\n\nfunction isDisposedRuntimeError(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error ?? \"\");\n return /runtime has been disposed|runtime was permanently disposed|runtime was soft-disposed|isolated is disposed/i.test(\n message\n );\n}\n\ntype RuntimeRetirementAction = \"reload\" | \"close\";\n\ninterface RuntimeRetirementRecord {\n runtimeId: string;\n action: RuntimeRetirementAction;\n reason: string;\n hard: boolean;\n at: number;\n activeRequests: number;\n replacementRuntimeId?: string;\n}\n\nfunction formatLifecycleReason(action: string, reason?: string): string {\n const trimmedReason = reason?.trim();\n return trimmedReason ? `${action}(${trimmedReason})` : `${action}()`;\n}\n\nfunction formatLogValue(value: unknown): string {\n if (typeof value === \"string\") {\n return JSON.stringify(value);\n }\n if (typeof value === \"number\" || typeof value === \"boolean\" || value === null) {\n return String(value);\n }\n\n try {\n return JSON.stringify(value);\n } catch {\n return JSON.stringify(String(value));\n }\n}\n\nfunction summarizeRequest(request: Request): string {\n try {\n const url = new URL(request.url);\n return `${request.method} ${url.origin}${url.pathname}${url.search}`;\n } catch {\n return `${request.method} ${request.url}`;\n }\n}\n\nexport interface IsolateServerOptions {\n namespaceId: string;\n getConnection: () => Promise<DaemonConnection>;\n}\n\nexport interface IsolateServerStartOptions {\n runtimeOptions: RuntimeOptions;\n entry: string;\n entryFilename?: string;\n onWebSocketCommand?: (cmd: WebSocketCommand) => void;\n}\n\nexport interface IsolateServerFetch {\n dispatchRequest(request: Request, options?: DispatchOptions): Promise<Response>;\n getUpgradeRequest(): Promise<UpgradeRequest | null>;\n dispatchWebSocketOpen(connectionId: string): Promise<void>;\n dispatchWebSocketMessage(connectionId: string, message: string | ArrayBuffer): Promise<void>;\n dispatchWebSocketClose(connectionId: string, code: number, reason: string): Promise<void>;\n dispatchWebSocketError(connectionId: string, error: Error): Promise<void>;\n hasServeHandler(): Promise<boolean>;\n hasActiveConnections(): Promise<boolean>;\n}\n\nexport class IsolateServer {\n private readonly namespaceId: string;\n private readonly getConnection: () => Promise<DaemonConnection>;\n private runtime: RemoteRuntime | null = null;\n private lastStartOptions: IsolateServerStartOptions | null = null;\n private lifecycleLock: Promise<void> = Promise.resolve();\n private activeRequestCount = 0;\n private lastRuntimeRetirement: RuntimeRetirementRecord | null = null;\n private closed = true;\n\n readonly fetch: IsolateServerFetch = {\n dispatchRequest: (request, options) => this.dispatchRequestWithRetry(request, options),\n getUpgradeRequest: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.getUpgradeRequest();\n },\n dispatchWebSocketOpen: async (connectionId) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketOpen(connectionId);\n },\n dispatchWebSocketMessage: async (connectionId, message) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketMessage(connectionId, message);\n },\n dispatchWebSocketClose: async (connectionId, code, reason) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketClose(connectionId, code, reason);\n },\n dispatchWebSocketError: async (connectionId, error) => {\n const runtime = await this.getActiveRuntime();\n await runtime.fetch.dispatchWebSocketError(connectionId, error);\n },\n hasServeHandler: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.hasServeHandler();\n },\n hasActiveConnections: async () => {\n const runtime = await this.getActiveRuntime();\n return runtime.fetch.hasActiveConnections();\n },\n };\n\n constructor(options: IsolateServerOptions) {\n this.namespaceId = options.namespaceId;\n this.getConnection = options.getConnection;\n }\n\n async start(options: IsolateServerStartOptions): Promise<void> {\n this.lastStartOptions = options;\n this.closed = false;\n\n await this.withLifecycleLock(async () => {\n if (this.runtime) {\n return;\n }\n\n this.runtime = await this.createAndInitializeRuntime(options);\n });\n }\n\n async reload(reason?: string): Promise<void> {\n const startOptions = this.lastStartOptions;\n if (!startOptions) {\n throw new Error(\"Server not configured. Call start() first.\");\n }\n\n const lifecycleReason = formatLifecycleReason(\"IsolateServer.reload\", reason);\n this.closed = false;\n await this.withLifecycleLock(async () => {\n const previousRuntime = this.runtime;\n this.log(\"reload requested\", {\n namespaceId: this.namespaceId,\n runtimeId: previousRuntime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n });\n\n if (previousRuntime) {\n this.runtime = null;\n this.recordRuntimeRetirement(previousRuntime, \"reload\", lifecycleReason, true);\n await this.disposeRuntime(previousRuntime, {\n hard: true,\n reason: lifecycleReason,\n });\n }\n\n try {\n const nextRuntime = await this.createAndInitializeRuntime(startOptions);\n this.runtime = nextRuntime;\n } catch (error) {\n this.log(\"reload failed\", {\n namespaceId: this.namespaceId,\n previousRuntimeId: previousRuntime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n error: error instanceof Error ? error.message : String(error),\n }, \"warn\");\n throw error;\n }\n\n if (\n previousRuntime &&\n this.lastRuntimeRetirement?.runtimeId === previousRuntime.id &&\n this.lastRuntimeRetirement.action === \"reload\"\n ) {\n this.lastRuntimeRetirement.replacementRuntimeId = this.runtime?.id;\n }\n\n this.log(\"reload completed\", {\n namespaceId: this.namespaceId,\n previousRuntimeId: previousRuntime?.id ?? null,\n runtimeId: this.runtime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n });\n });\n }\n\n async close(reason?: string): Promise<void> {\n const lifecycleReason = formatLifecycleReason(\"IsolateServer.close\", reason);\n await this.withLifecycleLock(async () => {\n const previousRuntime = this.runtime;\n this.log(\"close requested\", {\n namespaceId: this.namespaceId,\n runtimeId: previousRuntime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n });\n\n if (previousRuntime) {\n this.runtime = null;\n this.recordRuntimeRetirement(previousRuntime, \"close\", lifecycleReason, false);\n await this.disposeRuntime(previousRuntime, {\n reason: lifecycleReason,\n });\n }\n\n this.closed = true;\n this.log(\"close completed\", {\n namespaceId: this.namespaceId,\n previousRuntimeId: previousRuntime?.id ?? null,\n reason: lifecycleReason,\n activeRequests: this.activeRequestCount,\n });\n });\n }\n\n getRuntime(): RemoteRuntime | null {\n return this.runtime;\n }\n\n private async withLifecycleLock<T>(operation: () => Promise<T>): Promise<T> {\n const previous = this.lifecycleLock;\n let release!: () => void;\n this.lifecycleLock = new Promise<void>((resolve) => {\n release = resolve;\n });\n\n await previous;\n try {\n return await operation();\n } finally {\n release();\n }\n }\n\n private buildRuntimeOptions(options: IsolateServerStartOptions): RuntimeOptions {\n if (options.onWebSocketCommand) {\n return {\n ...options.runtimeOptions,\n onWebSocketCommand: options.onWebSocketCommand,\n };\n }\n\n return options.runtimeOptions;\n }\n\n private async createAndInitializeRuntime(\n options: IsolateServerStartOptions,\n allowRetry: boolean = true\n ): Promise<RemoteRuntime> {\n const connection = await this.getConnection();\n const namespace = connection.createNamespace(this.namespaceId);\n const runtimeOptions = this.buildRuntimeOptions(options);\n const runtime = await namespace.createRuntime(runtimeOptions);\n\n try {\n await runtime.eval(\n `import ${JSON.stringify(options.entry)};`,\n options.entryFilename ?? \"/isolate_server_entry.js\"\n );\n return runtime;\n } catch (error) {\n await this.disposeRuntime(runtime);\n if (!allowRetry || !isLinkerConflictError(error)) {\n throw error;\n }\n\n const retryRuntime = await namespace.createRuntime(runtimeOptions);\n try {\n await retryRuntime.eval(\n `import ${JSON.stringify(options.entry)};`,\n options.entryFilename ?? \"/isolate_server_entry.js\"\n );\n return retryRuntime;\n } catch (retryError) {\n await this.disposeRuntime(retryRuntime);\n throw retryError;\n }\n }\n }\n\n private async disposeRuntime(\n runtime: RemoteRuntime,\n options?: { hard?: boolean; reason?: string }\n ): Promise<void> {\n try {\n await runtime.dispose(options);\n } catch (error) {\n if (!isBenignDisposeError(error)) {\n throw error;\n }\n }\n }\n\n private async ensureStarted(): Promise<void> {\n if (this.runtime) {\n return;\n }\n\n if (!this.lastStartOptions) {\n throw new Error(\"Server not configured. Call start() first.\");\n }\n\n if (this.closed) {\n this.closed = false;\n }\n\n await this.start(this.lastStartOptions);\n }\n\n private async getActiveRuntime(): Promise<RemoteRuntime> {\n await this.ensureStarted();\n if (!this.runtime) {\n throw new Error(\"Server runtime failed to start.\");\n }\n return this.runtime;\n }\n\n private recordRuntimeRetirement(\n runtime: RemoteRuntime,\n action: RuntimeRetirementAction,\n reason: string,\n hard: boolean\n ): RuntimeRetirementRecord {\n const record: RuntimeRetirementRecord = {\n runtimeId: runtime.id,\n action,\n reason,\n hard,\n at: Date.now(),\n activeRequests: this.activeRequestCount,\n };\n this.lastRuntimeRetirement = record;\n return record;\n }\n\n private getLastRuntimeRetirementLogFields(): Record<string, unknown> {\n if (!this.lastRuntimeRetirement) {\n return {\n lastRetirementAction: \"unknown\",\n };\n }\n\n return {\n lastRetirementAction: this.lastRuntimeRetirement.action,\n lastRetirementReason: this.lastRuntimeRetirement.reason,\n lastRetirementRuntimeId: this.lastRuntimeRetirement.runtimeId,\n lastRetirementHard: this.lastRuntimeRetirement.hard,\n lastRetirementAgeMs: Date.now() - this.lastRuntimeRetirement.at,\n lastRetirementActiveRequests: this.lastRuntimeRetirement.activeRequests,\n lastRetirementReplacementRuntimeId: this.lastRuntimeRetirement.replacementRuntimeId ?? null,\n };\n }\n\n private log(\n message: string,\n fields: Record<string, unknown>,\n level: \"log\" | \"warn\" = \"log\"\n ): void {\n const suffix = Object.entries(fields)\n .filter(([, value]) => value !== undefined)\n .map(([key, value]) => `${key}=${formatLogValue(value)}`)\n .join(\" \");\n\n const logger = level === \"warn\" ? console.warn : console.log;\n logger(`[isolate-server] ${message}${suffix ? `; ${suffix}` : \"\"}`);\n }\n\n private async dispatchRequestWithRetry(\n request: Request,\n options?: DispatchOptions\n ): Promise<Response> {\n this.activeRequestCount += 1;\n try {\n const runtime = await this.getActiveRuntime();\n try {\n return await runtime.fetch.dispatchRequest(request, options);\n } catch (error) {\n if (!isLinkerConflictError(error) && !isDisposedRuntimeError(error)) {\n throw error;\n }\n\n const requestSummary = summarizeRequest(request);\n if (isLinkerConflictError(error)) {\n await this.reload(`request-linker-conflict: ${requestSummary}`);\n } else if (this.runtime?.id === runtime.id) {\n this.runtime = null;\n }\n\n const retryRuntime = await this.getActiveRuntime();\n this.log(\n isLinkerConflictError(error)\n ? \"request recovered after linker conflict\"\n : \"request recovered after disposed runtime\",\n {\n namespaceId: this.namespaceId,\n request: requestSummary,\n requestId: options?.requestId ?? null,\n metadataKeys: Object.keys(options?.metadata ?? {}),\n previousRuntimeId: runtime.id,\n runtimeId: retryRuntime.id,\n activeRequests: this.activeRequestCount,\n error: error instanceof Error ? error.message : String(error),\n ...this.getLastRuntimeRetirementLogFields(),\n },\n \"warn\"\n );\n return retryRuntime.fetch.dispatchRequest(request, options);\n }\n } finally {\n this.activeRequestCount -= 1;\n }\n }\n}\n"
6
6
  ],
7
- "mappings": ";AAQA;AAEA,IAAM,wBAAwB;AAE9B,SAAS,qBAAqB,CAAC,OAAyB;AAAA,EACtD,MAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,SAAS,EAAE;AAAA,EAC3E,OAAO,QAAQ,SAAS,qBAAqB;AAAA;AAAA;AA0BxC,MAAM,cAAc;AAAA,EACR;AAAA,EACA;AAAA,EACT,UAAgC;AAAA,EAChC,mBAAqD;AAAA,EACrD,gBAA+B,QAAQ,QAAQ;AAAA,EAC/C,SAAS;AAAA,EAER,QAA4B;AAAA,IACnC,iBAAiB,CAAC,SAAS,YAAY,KAAK,yBAAyB,SAAS,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G;AAAA,UACnE,MAAM;AAAA,QACR;AAAA,QAEA,MAAM,iBAAiB,iBAAiB,OAAO;AAAA,QAC/C,IAAI,sBAAsB,KAAK,GAAG;AAAA,UAChC,MAAM,KAAK,OAAO,4BAA4B,gBAAgB;AAAA,QAChE,EAAO,SAAI,KAAK,SAAS,OAAO,QAAQ,IAAI;AAAA,UAC1C,KAAK,UAAU;AAAA,QACjB;AAAA,QAEA,MAAM,eAAe,MAAM,KAAK,iBAAiB;AAAA,QACjD,KAAK,IACH,sBAAsB,KAAK,IACvB,4CACA,4CACJ;AAAA,UACE,aAAa,KAAK;AAAA,UAClB,SAAS;AAAA,UACT,WAAW,SAAS,aAAa;AAAA,UACjC,cAAc,OAAO,KAAK,SAAS,YAAY,CAAC,CAAC;AAAA,UACjD,mBAAmB,QAAQ;AAAA,UAC3B,WAAW,aAAa;AAAA,UACxB,gBAAgB,KAAK;AAAA,UACrB,OAAO,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AAAA,aACzD,KAAK,kCAAkC;AAAA,QAC5C,GACA,MACF;AAAA,QACA,OAAO,aAAa,MAAM,gBAAgB,SAAS,OAAO;AAAA;AAAA,cAE5D;AAAA,MACA,KAAK,sBAAsB;AAAA;AAAA;AAGjC;",
8
+ "debugId": "E1A61983BC24D6F664756E2164756E21",
9
9
  "names": []
10
10
  }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/isolate",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "type": "module"
5
5
  }
@@ -79,11 +79,11 @@ async function createAppServerAdapter(getConnection, options) {
79
79
  await server.fetch.dispatchWebSocketError(connectionId, error);
80
80
  }
81
81
  },
82
- reload: async () => {
82
+ reload: async (reason) => {
83
83
  diagnostics.lifecycleState = "reloading";
84
84
  try {
85
- bindingsAdapter.reset("AppServer.reload()");
86
- await server.reload();
85
+ bindingsAdapter.reset(reason ? `AppServer.reload(${reason})` : "AppServer.reload()");
86
+ await server.reload(reason);
87
87
  runtimeId = server.getRuntime()?.id ?? options.key;
88
88
  } finally {
89
89
  diagnostics.lifecycleState = "idle";
@@ -93,14 +93,16 @@ async function createAppServerAdapter(getConnection, options) {
93
93
  diagnostics.lifecycleState = "disposing";
94
94
  try {
95
95
  if (disposeOptions?.hard) {
96
- bindingsAdapter.reset(disposeOptions?.reason ?? "AppServer.dispose(hard)");
97
- await server.reload();
98
- bindingsAdapter.abort(disposeOptions?.reason ?? "AppServer.dispose(hard)");
99
- await server.close();
96
+ const hardDisposeReason = disposeOptions?.reason ? `AppServer.dispose(hard): ${disposeOptions.reason}` : "AppServer.dispose(hard)";
97
+ bindingsAdapter.reset(hardDisposeReason);
98
+ await server.reload(hardDisposeReason);
99
+ bindingsAdapter.abort(hardDisposeReason);
100
+ await server.close(hardDisposeReason);
100
101
  return;
101
102
  }
102
- bindingsAdapter.abort(disposeOptions?.reason ?? "AppServer.dispose()");
103
- await server.close();
103
+ const disposeReason = disposeOptions?.reason ? `AppServer.dispose(): ${disposeOptions.reason}` : "AppServer.dispose()";
104
+ bindingsAdapter.abort(disposeReason);
105
+ await server.close(disposeReason);
104
106
  } finally {
105
107
  diagnostics.lifecycleState = "idle";
106
108
  }
@@ -115,4 +117,4 @@ export {
115
117
  createAppServerAdapter
116
118
  };
117
119
 
118
- //# debugId=00809D96D838354264756E2164756E21
120
+ //# debugId=5AA39498EBE5037464756E2164756E21
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["../../../src/server/app-server.ts"],
4
4
  "sourcesContent": [
5
- "import { IsolateServer } from \"../internal/server/index.mjs\";\nimport type { DaemonConnection } from \"../internal/client/index.mjs\";\nimport { createRuntimeDiagnostics } from \"../bridge/diagnostics.mjs\";\nimport { createRuntimeBindingsAdapter } from \"../bridge/runtime-bindings.mjs\";\nimport { withRequestContext } from \"../bridge/request-context.mjs\";\nimport type { AppServer, CreateAppServerOptions, RequestResult } from \"../types.mjs\";\n\nexport async function createAppServerAdapter(\n getConnection: () => Promise<DaemonConnection>,\n options: CreateAppServerOptions,\n): Promise<AppServer> {\n const diagnostics = createRuntimeDiagnostics();\n const server = new IsolateServer({\n namespaceId: options.key,\n getConnection,\n });\n\n let runtimeId = options.key;\n const bindingsAdapter = createRuntimeBindingsAdapter(\n options.bindings,\n () => runtimeId,\n diagnostics,\n );\n await server.start({\n entry: options.entry,\n entryFilename: options.entryFilename,\n runtimeOptions: {\n ...bindingsAdapter.runtimeOptions,\n cwd: options.cwd,\n memoryLimitMB: options.memoryLimitMB,\n executionTimeout: options.executionTimeout,\n testEnvironment: options.features?.tests ?? false,\n },\n onWebSocketCommand: (command) => {\n options.webSockets?.onCommand?.(command);\n },\n });\n runtimeId = server.getRuntime()?.id ?? options.key;\n\n async function handleRequest(request: Request, handleOptions?: { requestId?: string; signal?: AbortSignal; metadata?: Record<string, string> }): Promise<RequestResult> {\n diagnostics.activeRequests += 1;\n diagnostics.lifecycleState = \"active\";\n try {\n return await withRequestContext(\n {\n requestId: handleOptions?.requestId,\n metadata: handleOptions?.metadata,\n signal: handleOptions?.signal ?? request.signal,\n },\n async () => {\n const response = await server.fetch.dispatchRequest(request, {\n signal: handleOptions?.signal,\n requestId: handleOptions?.requestId,\n metadata: handleOptions?.metadata,\n });\n const upgradeRequest = await server.fetch.getUpgradeRequest();\n if (upgradeRequest?.requested) {\n return {\n type: \"websocket\",\n upgradeData: {\n requested: upgradeRequest.requested,\n connectionId: upgradeRequest.connectionId,\n },\n };\n }\n\n return {\n type: \"response\",\n response,\n };\n },\n );\n } catch (error) {\n diagnostics.lastError = error instanceof Error ? error.message : String(error);\n throw error;\n } finally {\n diagnostics.activeRequests -= 1;\n diagnostics.lifecycleState = \"idle\";\n }\n }\n\n return {\n handle: handleRequest,\n ws: {\n open: async (connectionId) => {\n await server.fetch.dispatchWebSocketOpen(connectionId);\n },\n message: async (connectionId, data) => {\n await server.fetch.dispatchWebSocketMessage(connectionId, data);\n },\n close: async (connectionId, code, reason) => {\n await server.fetch.dispatchWebSocketClose(connectionId, code, reason);\n },\n error: async (connectionId, error) => {\n await server.fetch.dispatchWebSocketError(connectionId, error);\n },\n },\n reload: async () => {\n diagnostics.lifecycleState = \"reloading\";\n try {\n bindingsAdapter.reset(\"AppServer.reload()\");\n await server.reload();\n runtimeId = server.getRuntime()?.id ?? options.key;\n } finally {\n diagnostics.lifecycleState = \"idle\";\n }\n },\n dispose: async (disposeOptions) => {\n diagnostics.lifecycleState = \"disposing\";\n try {\n if (disposeOptions?.hard) {\n bindingsAdapter.reset(disposeOptions?.reason ?? \"AppServer.dispose(hard)\");\n await server.reload();\n bindingsAdapter.abort(disposeOptions?.reason ?? \"AppServer.dispose(hard)\");\n await server.close();\n return;\n }\n bindingsAdapter.abort(disposeOptions?.reason ?? \"AppServer.dispose()\");\n await server.close();\n } finally {\n diagnostics.lifecycleState = \"idle\";\n }\n },\n diagnostics: async () => ({\n ...diagnostics,\n reused: server.getRuntime()?.reused,\n }),\n };\n}\n"
5
+ "import { IsolateServer } from \"../internal/server/index.mjs\";\nimport type { DaemonConnection } from \"../internal/client/index.mjs\";\nimport { createRuntimeDiagnostics } from \"../bridge/diagnostics.mjs\";\nimport { createRuntimeBindingsAdapter } from \"../bridge/runtime-bindings.mjs\";\nimport { withRequestContext } from \"../bridge/request-context.mjs\";\nimport type { AppServer, CreateAppServerOptions, RequestResult } from \"../types.mjs\";\n\nexport async function createAppServerAdapter(\n getConnection: () => Promise<DaemonConnection>,\n options: CreateAppServerOptions,\n): Promise<AppServer> {\n const diagnostics = createRuntimeDiagnostics();\n const server = new IsolateServer({\n namespaceId: options.key,\n getConnection,\n });\n\n let runtimeId = options.key;\n const bindingsAdapter = createRuntimeBindingsAdapter(\n options.bindings,\n () => runtimeId,\n diagnostics,\n );\n await server.start({\n entry: options.entry,\n entryFilename: options.entryFilename,\n runtimeOptions: {\n ...bindingsAdapter.runtimeOptions,\n cwd: options.cwd,\n memoryLimitMB: options.memoryLimitMB,\n executionTimeout: options.executionTimeout,\n testEnvironment: options.features?.tests ?? false,\n },\n onWebSocketCommand: (command) => {\n options.webSockets?.onCommand?.(command);\n },\n });\n runtimeId = server.getRuntime()?.id ?? options.key;\n\n async function handleRequest(request: Request, handleOptions?: { requestId?: string; signal?: AbortSignal; metadata?: Record<string, string> }): Promise<RequestResult> {\n diagnostics.activeRequests += 1;\n diagnostics.lifecycleState = \"active\";\n try {\n return await withRequestContext(\n {\n requestId: handleOptions?.requestId,\n metadata: handleOptions?.metadata,\n signal: handleOptions?.signal ?? request.signal,\n },\n async () => {\n const response = await server.fetch.dispatchRequest(request, {\n signal: handleOptions?.signal,\n requestId: handleOptions?.requestId,\n metadata: handleOptions?.metadata,\n });\n const upgradeRequest = await server.fetch.getUpgradeRequest();\n if (upgradeRequest?.requested) {\n return {\n type: \"websocket\",\n upgradeData: {\n requested: upgradeRequest.requested,\n connectionId: upgradeRequest.connectionId,\n },\n };\n }\n\n return {\n type: \"response\",\n response,\n };\n },\n );\n } catch (error) {\n diagnostics.lastError = error instanceof Error ? error.message : String(error);\n throw error;\n } finally {\n diagnostics.activeRequests -= 1;\n diagnostics.lifecycleState = \"idle\";\n }\n }\n\n return {\n handle: handleRequest,\n ws: {\n open: async (connectionId) => {\n await server.fetch.dispatchWebSocketOpen(connectionId);\n },\n message: async (connectionId, data) => {\n await server.fetch.dispatchWebSocketMessage(connectionId, data);\n },\n close: async (connectionId, code, reason) => {\n await server.fetch.dispatchWebSocketClose(connectionId, code, reason);\n },\n error: async (connectionId, error) => {\n await server.fetch.dispatchWebSocketError(connectionId, error);\n },\n },\n reload: async (reason) => {\n diagnostics.lifecycleState = \"reloading\";\n try {\n bindingsAdapter.reset(reason ? `AppServer.reload(${reason})` : \"AppServer.reload()\");\n await server.reload(reason);\n runtimeId = server.getRuntime()?.id ?? options.key;\n } finally {\n diagnostics.lifecycleState = \"idle\";\n }\n },\n dispose: async (disposeOptions) => {\n diagnostics.lifecycleState = \"disposing\";\n try {\n if (disposeOptions?.hard) {\n const hardDisposeReason = disposeOptions?.reason\n ? `AppServer.dispose(hard): ${disposeOptions.reason}`\n : \"AppServer.dispose(hard)\";\n bindingsAdapter.reset(hardDisposeReason);\n await server.reload(hardDisposeReason);\n bindingsAdapter.abort(hardDisposeReason);\n await server.close(hardDisposeReason);\n return;\n }\n const disposeReason = disposeOptions?.reason\n ? `AppServer.dispose(): ${disposeOptions.reason}`\n : \"AppServer.dispose()\";\n bindingsAdapter.abort(disposeReason);\n await server.close(disposeReason);\n } finally {\n diagnostics.lifecycleState = \"idle\";\n }\n },\n diagnostics: async () => ({\n ...diagnostics,\n reused: server.getRuntime()?.reused,\n }),\n };\n}\n"
6
6
  ],
7
- "mappings": ";AAAA;AAEA;AACA;AACA;AAGA,eAAsB,sBAAsB,CAC1C,eACA,SACoB;AAAA,EACpB,MAAM,cAAc,yBAAyB;AAAA,EAC7C,MAAM,SAAS,IAAI,cAAc;AAAA,IAC/B,aAAa,QAAQ;AAAA,IACrB;AAAA,EACF,CAAC;AAAA,EAED,IAAI,YAAY,QAAQ;AAAA,EACxB,MAAM,kBAAkB,6BACtB,QAAQ,UACR,MAAM,WACN,WACF;AAAA,EACA,MAAM,OAAO,MAAM;AAAA,IACjB,OAAO,QAAQ;AAAA,IACf,eAAe,QAAQ;AAAA,IACvB,gBAAgB;AAAA,SACX,gBAAgB;AAAA,MACnB,KAAK,QAAQ;AAAA,MACb,eAAe,QAAQ;AAAA,MACvB,kBAAkB,QAAQ;AAAA,MAC1B,iBAAiB,QAAQ,UAAU,SAAS;AAAA,IAC9C;AAAA,IACA,oBAAoB,CAAC,YAAY;AAAA,MAC/B,QAAQ,YAAY,YAAY,OAAO;AAAA;AAAA,EAE3C,CAAC;AAAA,EACD,YAAY,OAAO,WAAW,GAAG,MAAM,QAAQ;AAAA,EAE/C,eAAe,aAAa,CAAC,SAAkB,eAAyH;AAAA,IACtK,YAAY,kBAAkB;AAAA,IAC9B,YAAY,iBAAiB;AAAA,IAC7B,IAAI;AAAA,MACF,OAAO,MAAM,mBACX;AAAA,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@@ -25,12 +25,14 @@ export declare class IsolateServer {
25
25
  private runtime;
26
26
  private lastStartOptions;
27
27
  private lifecycleLock;
28
+ private activeRequestCount;
29
+ private lastRuntimeRetirement;
28
30
  private closed;
29
31
  readonly fetch: IsolateServerFetch;
30
32
  constructor(options: IsolateServerOptions);
31
33
  start(options: IsolateServerStartOptions): Promise<void>;
32
- reload(): Promise<void>;
33
- close(): Promise<void>;
34
+ reload(reason?: string): Promise<void>;
35
+ close(reason?: string): Promise<void>;
34
36
  getRuntime(): RemoteRuntime | null;
35
37
  private withLifecycleLock;
36
38
  private buildRuntimeOptions;
@@ -38,5 +40,8 @@ export declare class IsolateServer {
38
40
  private disposeRuntime;
39
41
  private ensureStarted;
40
42
  private getActiveRuntime;
43
+ private recordRuntimeRetirement;
44
+ private getLastRuntimeRetirementLogFields;
45
+ private log;
41
46
  private dispatchRequestWithRetry;
42
47
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ricsam/isolate",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "type": "module",
5
5
  "description": "Unified runtime host for app servers, script runtimes, browser runtimes, module resolution, file bindings, and typechecking",
6
6
  "author": "ricsam <oss@ricsam.dev>",