export-runtime 0.0.22 → 0.0.23
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/handler.js +14 -35
- package/package.json +1 -1
package/handler.js
CHANGED
|
@@ -323,7 +323,6 @@ export const createHandler = (moduleMap, generatedTypes, minifiedCore, coreId, m
|
|
|
323
323
|
// Track session state and connection state for this WebSocket connection
|
|
324
324
|
const wsSession = { token: null };
|
|
325
325
|
let isClosed = false;
|
|
326
|
-
const pendingOps = new Set();
|
|
327
326
|
|
|
328
327
|
// Safe send that ignores errors when connection is closed
|
|
329
328
|
const safeSend = (data) => {
|
|
@@ -335,22 +334,19 @@ export const createHandler = (moduleMap, generatedTypes, minifiedCore, coreId, m
|
|
|
335
334
|
}
|
|
336
335
|
};
|
|
337
336
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
return await promise;
|
|
344
|
-
} finally {
|
|
345
|
-
pendingOps.delete(promise);
|
|
337
|
+
const handleClose = () => {
|
|
338
|
+
if (isClosed) return;
|
|
339
|
+
isClosed = true;
|
|
340
|
+
if (onClose) {
|
|
341
|
+
try { onClose(); } catch {}
|
|
346
342
|
}
|
|
347
343
|
};
|
|
348
344
|
|
|
349
345
|
server.addEventListener("message", (event) => {
|
|
350
346
|
if (isClosed) return;
|
|
351
347
|
|
|
352
|
-
// Handle message asynchronously
|
|
353
|
-
|
|
348
|
+
// Handle message asynchronously - fire and forget
|
|
349
|
+
(async () => {
|
|
354
350
|
let id;
|
|
355
351
|
try {
|
|
356
352
|
if (isClosed) return;
|
|
@@ -376,22 +372,16 @@ export const createHandler = (moduleMap, generatedTypes, minifiedCore, coreId, m
|
|
|
376
372
|
|
|
377
373
|
if (result) safeSend(stringify({ ...result, id }));
|
|
378
374
|
} catch (err) {
|
|
379
|
-
if (isClosed
|
|
380
|
-
|
|
375
|
+
if (isClosed) return;
|
|
376
|
+
const errStr = String(err);
|
|
377
|
+
if (errStr.includes("Connection closed") || errStr.includes("WebSocket")) return;
|
|
378
|
+
if (id !== undefined) safeSend(stringify({ type: "error", id, error: errStr }));
|
|
381
379
|
}
|
|
382
|
-
});
|
|
383
|
-
});
|
|
384
|
-
|
|
385
|
-
server.addEventListener("close", () => {
|
|
386
|
-
isClosed = true;
|
|
387
|
-
if (onClose) onClose();
|
|
380
|
+
})();
|
|
388
381
|
});
|
|
389
382
|
|
|
390
|
-
server.addEventListener("
|
|
391
|
-
|
|
392
|
-
if (onClose) onClose();
|
|
393
|
-
try { server.close(); } catch {}
|
|
394
|
-
});
|
|
383
|
+
server.addEventListener("close", handleClose);
|
|
384
|
+
server.addEventListener("error", handleClose);
|
|
395
385
|
};
|
|
396
386
|
|
|
397
387
|
return {
|
|
@@ -419,12 +409,6 @@ export const createHandler = (moduleMap, generatedTypes, minifiedCore, coreId, m
|
|
|
419
409
|
const pair = new WebSocketPair();
|
|
420
410
|
const [client, server] = Object.values(pair);
|
|
421
411
|
|
|
422
|
-
// Create a promise that resolves when the WebSocket closes
|
|
423
|
-
const wsLifetime = new Promise((resolve) => {
|
|
424
|
-
server.addEventListener("close", resolve, { once: true });
|
|
425
|
-
server.addEventListener("error", resolve, { once: true });
|
|
426
|
-
});
|
|
427
|
-
|
|
428
412
|
server.accept();
|
|
429
413
|
|
|
430
414
|
if (isShared && env?.SHARED_EXPORT) {
|
|
@@ -437,11 +421,6 @@ export const createHandler = (moduleMap, generatedTypes, minifiedCore, coreId, m
|
|
|
437
421
|
wireWebSocket(server, dispatcher, env, () => dispatcher.clearAll());
|
|
438
422
|
}
|
|
439
423
|
|
|
440
|
-
// Keep worker alive for the WebSocket lifetime
|
|
441
|
-
if (ctx?.waitUntil) {
|
|
442
|
-
ctx.waitUntil(wsLifetime);
|
|
443
|
-
}
|
|
444
|
-
|
|
445
424
|
return new Response(null, { status: 101, webSocket: client });
|
|
446
425
|
}
|
|
447
426
|
|