export-runtime 0.0.23 → 0.0.24
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 +39 -27
- package/package.json +1 -1
package/handler.js
CHANGED
|
@@ -343,41 +343,53 @@ export const createHandler = (moduleMap, generatedTypes, minifiedCore, coreId, m
|
|
|
343
343
|
};
|
|
344
344
|
|
|
345
345
|
server.addEventListener("message", (event) => {
|
|
346
|
-
|
|
346
|
+
// Wrap entire handler in try-catch to prevent any unhandled errors
|
|
347
|
+
try {
|
|
348
|
+
if (isClosed) return;
|
|
347
349
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
+
// Parse message synchronously to get the id
|
|
351
|
+
let msg;
|
|
350
352
|
let id;
|
|
351
353
|
try {
|
|
352
|
-
|
|
353
|
-
const msg = parse(event.data);
|
|
354
|
+
msg = parse(event.data);
|
|
354
355
|
id = msg.id;
|
|
356
|
+
} catch {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
355
359
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
if (isClosed) return;
|
|
364
|
-
const result = await dispatchMessage(dispatcher, msg, env, wsSession);
|
|
360
|
+
// Handle ping synchronously - no async needed
|
|
361
|
+
if (msg.type === "ping") {
|
|
362
|
+
safeSend(stringify({ type: "pong", id }));
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
365
|
|
|
366
|
-
|
|
366
|
+
// Handle auth token updates synchronously
|
|
367
|
+
if (msg.type === "auth" && msg.token && !msg.method) {
|
|
368
|
+
wsSession.token = msg.token;
|
|
369
|
+
safeSend(stringify({ type: "auth-result", id, success: true }));
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
367
372
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
373
|
+
// Handle other messages asynchronously with full error protection
|
|
374
|
+
(async () => {
|
|
375
|
+
try {
|
|
376
|
+
if (isClosed) return;
|
|
377
|
+
const result = await dispatchMessage(dispatcher, msg, env, wsSession);
|
|
378
|
+
if (isClosed) return;
|
|
379
|
+
|
|
380
|
+
// Extract token from auth responses
|
|
381
|
+
if (result?.value?.token && msg.type === "auth") {
|
|
382
|
+
wsSession.token = result.value.token;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (result) safeSend(stringify({ ...result, id }));
|
|
386
|
+
} catch {
|
|
387
|
+
// Silently ignore all errors - connection may be closing
|
|
371
388
|
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
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 }));
|
|
379
|
-
}
|
|
380
|
-
})();
|
|
389
|
+
})().catch(() => {}); // Double-catch to ensure no unhandled promise rejection
|
|
390
|
+
} catch {
|
|
391
|
+
// Silently ignore outer errors too
|
|
392
|
+
}
|
|
381
393
|
});
|
|
382
394
|
|
|
383
395
|
server.addEventListener("close", handleClose);
|