export-runtime 0.0.17 → 0.0.18
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 +25 -5
- package/package.json +1 -1
package/handler.js
CHANGED
|
@@ -320,10 +320,22 @@ export const createHandler = (moduleMap, generatedTypes, minifiedCore, coreId, m
|
|
|
320
320
|
};
|
|
321
321
|
|
|
322
322
|
const wireWebSocket = (server, dispatcher, env, onClose) => {
|
|
323
|
-
// Track session state for this WebSocket connection
|
|
323
|
+
// Track session state and connection state for this WebSocket connection
|
|
324
324
|
const wsSession = { token: null };
|
|
325
|
+
let isClosed = false;
|
|
326
|
+
|
|
327
|
+
// Safe send that ignores errors when connection is closed
|
|
328
|
+
const safeSend = (data) => {
|
|
329
|
+
if (isClosed) return;
|
|
330
|
+
try {
|
|
331
|
+
server.send(data);
|
|
332
|
+
} catch {
|
|
333
|
+
// Connection already closed, ignore
|
|
334
|
+
}
|
|
335
|
+
};
|
|
325
336
|
|
|
326
337
|
server.addEventListener("message", async (event) => {
|
|
338
|
+
if (isClosed) return;
|
|
327
339
|
let id;
|
|
328
340
|
try {
|
|
329
341
|
const msg = parse(event.data);
|
|
@@ -333,7 +345,7 @@ export const createHandler = (moduleMap, generatedTypes, minifiedCore, coreId, m
|
|
|
333
345
|
if (msg.type === "auth" && msg.token && !msg.method) {
|
|
334
346
|
// Direct token send on reconnect - just update session
|
|
335
347
|
wsSession.token = msg.token;
|
|
336
|
-
|
|
348
|
+
safeSend(stringify({ type: "auth-result", id, success: true }));
|
|
337
349
|
return;
|
|
338
350
|
}
|
|
339
351
|
|
|
@@ -344,12 +356,20 @@ export const createHandler = (moduleMap, generatedTypes, minifiedCore, coreId, m
|
|
|
344
356
|
wsSession.token = result.value.token;
|
|
345
357
|
}
|
|
346
358
|
|
|
347
|
-
if (result)
|
|
359
|
+
if (result) safeSend(stringify({ ...result, id }));
|
|
348
360
|
} catch (err) {
|
|
349
|
-
if (id !== undefined)
|
|
361
|
+
if (id !== undefined) safeSend(stringify({ type: "error", id, error: String(err) }));
|
|
350
362
|
}
|
|
351
363
|
});
|
|
352
|
-
|
|
364
|
+
|
|
365
|
+
server.addEventListener("close", () => {
|
|
366
|
+
isClosed = true;
|
|
367
|
+
if (onClose) onClose();
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
server.addEventListener("error", () => {
|
|
371
|
+
isClosed = true;
|
|
372
|
+
});
|
|
353
373
|
};
|
|
354
374
|
|
|
355
375
|
return {
|