@ricsam/isolate-client 0.1.22 → 0.1.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.
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/isolate-client",
3
- "version": "0.1.22",
3
+ "version": "0.1.23",
4
4
  "type": "commonjs"
5
5
  }
@@ -39,6 +39,8 @@ async function connect(options = {}) {
39
39
  uploadStreams: new Map,
40
40
  moduleSourceCache: new Map,
41
41
  callbackStreamReaders: new Map,
42
+ activeCallbackInvocations: new Map,
43
+ callbackAbortControllers: new Map,
42
44
  closing: false,
43
45
  namespacedRuntimes: new Map
44
46
  };
@@ -75,6 +77,11 @@ async function connect(options = {}) {
75
77
  }
76
78
  }
77
79
  state.uploadStreams.clear();
80
+ for (const [, controller] of state.callbackAbortControllers) {
81
+ controller.abort();
82
+ }
83
+ state.callbackAbortControllers.clear();
84
+ state.activeCallbackInvocations.clear();
78
85
  if (!state.closing && state.namespacedRuntimes.size > 0) {
79
86
  state.reconnecting = reconnect(state, options).catch(() => {
80
87
  state.namespacedRuntimes.clear();
@@ -260,6 +267,18 @@ function handleMessage(message, state) {
260
267
  }
261
268
  break;
262
269
  }
270
+ case MessageType.CALLBACK_ABORT: {
271
+ const msg = message;
272
+ const invocation = state.activeCallbackInvocations.get(msg.targetRequestId);
273
+ if (invocation) {
274
+ invocation.aborted = true;
275
+ }
276
+ const controller = state.callbackAbortControllers.get(msg.targetRequestId);
277
+ if (controller && !controller.signal.aborted) {
278
+ controller.abort();
279
+ }
280
+ break;
281
+ }
263
282
  case MessageType.ISOLATE_EVENT: {
264
283
  const msg = message;
265
284
  handleIsolateEvent(msg, state);
@@ -422,6 +441,8 @@ function handleMessage(message, state) {
422
441
  }
423
442
  async function handleCallbackInvoke(invoke, state) {
424
443
  const callback = state.callbacks.get(invoke.callbackId);
444
+ const invocationState = { aborted: false };
445
+ state.activeCallbackInvocations.set(invoke.requestId, invocationState);
425
446
  const response = {
426
447
  type: MessageType.CALLBACK_RESPONSE,
427
448
  requestId: invoke.requestId
@@ -431,7 +452,10 @@ async function handleCallbackInvoke(invoke, state) {
431
452
  name: "Error",
432
453
  message: `Unknown callback: ${invoke.callbackId}`
433
454
  };
434
- sendMessage(state.socket, response);
455
+ if (!invocationState.aborted) {
456
+ sendMessage(state.socket, response);
457
+ }
458
+ state.activeCallbackInvocations.delete(invoke.requestId);
435
459
  } else {
436
460
  try {
437
461
  const needsRequestId = state.callbacksNeedingRequestId.has(invoke.callbackId);
@@ -439,9 +463,15 @@ async function handleCallbackInvoke(invoke, state) {
439
463
  if (result && typeof result === "object" && result.__callbackStreaming) {
440
464
  return;
441
465
  }
466
+ if (invocationState.aborted) {
467
+ return;
468
+ }
442
469
  response.result = result;
443
470
  sendMessage(state.socket, response);
444
471
  } catch (err) {
472
+ if (invocationState.aborted) {
473
+ return;
474
+ }
445
475
  const error = err;
446
476
  response.error = {
447
477
  name: error.name,
@@ -449,6 +479,8 @@ async function handleCallbackInvoke(invoke, state) {
449
479
  stack: error.stack
450
480
  };
451
481
  sendMessage(state.socket, response);
482
+ } finally {
483
+ state.activeCallbackInvocations.delete(invoke.requestId);
452
484
  }
453
485
  }
454
486
  }
@@ -1054,42 +1086,52 @@ function registerFetchCallback(state, callback) {
1054
1086
  state.callbacks.set(callbackId, async (serialized, requestId) => {
1055
1087
  const data = serialized;
1056
1088
  const signalController = new AbortController;
1089
+ const callbackRequestId = typeof requestId === "number" ? requestId : undefined;
1090
+ if (callbackRequestId !== undefined) {
1091
+ state.callbackAbortControllers.set(callbackRequestId, signalController);
1092
+ }
1057
1093
  if (data.signalAborted) {
1058
1094
  signalController.abort();
1059
1095
  }
1060
- const init = {
1061
- method: data.method,
1062
- headers: data.headers,
1063
- rawBody: data.body ?? null,
1064
- body: data.body ?? null,
1065
- signal: signalController.signal
1066
- };
1067
- const response = await callback(data.url, init);
1068
- const contentLength = response.headers.get("content-length");
1069
- const knownSize = contentLength ? parseInt(contentLength, 10) : null;
1070
- const isNetworkResponse = response.url && (response.url.startsWith("http://") || response.url.startsWith("https://"));
1071
- const shouldStream = isNetworkResponse && !NULL_BODY_STATUSES.has(response.status) && !!response.body && (knownSize === null || knownSize > CALLBACK_STREAM_THRESHOLD);
1072
- if (shouldStream && response.body) {
1073
- const streamId = state.nextStreamId++;
1074
- const headers = [];
1075
- response.headers.forEach((value, key) => {
1076
- headers.push([key, value]);
1077
- });
1078
- sendMessage(state.socket, {
1079
- type: MessageType.CALLBACK_STREAM_START,
1080
- requestId,
1081
- streamId,
1082
- metadata: {
1083
- status: response.status,
1084
- statusText: response.statusText,
1085
- headers,
1086
- url: response.url || undefined
1087
- }
1088
- });
1089
- streamCallbackResponseBody(state, streamId, requestId, response.body);
1090
- return { __callbackStreaming: true, streamId };
1096
+ try {
1097
+ const init = {
1098
+ method: data.method,
1099
+ headers: data.headers,
1100
+ rawBody: data.body ?? null,
1101
+ body: data.body ?? null,
1102
+ signal: signalController.signal
1103
+ };
1104
+ const response = await callback(data.url, init);
1105
+ const contentLength = response.headers.get("content-length");
1106
+ const knownSize = contentLength ? parseInt(contentLength, 10) : null;
1107
+ const isNetworkResponse = response.url && (response.url.startsWith("http://") || response.url.startsWith("https://"));
1108
+ const shouldStream = isNetworkResponse && !NULL_BODY_STATUSES.has(response.status) && !!response.body && (knownSize === null || knownSize > CALLBACK_STREAM_THRESHOLD);
1109
+ if (shouldStream && response.body) {
1110
+ const streamId = state.nextStreamId++;
1111
+ const headers = [];
1112
+ response.headers.forEach((value, key) => {
1113
+ headers.push([key, value]);
1114
+ });
1115
+ sendMessage(state.socket, {
1116
+ type: MessageType.CALLBACK_STREAM_START,
1117
+ requestId,
1118
+ streamId,
1119
+ metadata: {
1120
+ status: response.status,
1121
+ statusText: response.statusText,
1122
+ headers,
1123
+ url: response.url || undefined
1124
+ }
1125
+ });
1126
+ streamCallbackResponseBody(state, streamId, requestId, response.body);
1127
+ return { __callbackStreaming: true, streamId };
1128
+ }
1129
+ return serializeResponse(response);
1130
+ } finally {
1131
+ if (callbackRequestId !== undefined) {
1132
+ state.callbackAbortControllers.delete(callbackRequestId);
1133
+ }
1091
1134
  }
1092
- return serializeResponse(response);
1093
1135
  });
1094
1136
  return { callbackId, name: "fetch", type: "async" };
1095
1137
  }
@@ -1695,4 +1737,4 @@ export {
1695
1737
  connect
1696
1738
  };
1697
1739
 
1698
- //# debugId=F239739EEADB58BD64756E2164756E21
1740
+ //# debugId=A9015ACD8149CB6C64756E2164756E21