@ricsam/isolate-client 0.1.21 → 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.21",
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
  }
@@ -1047,48 +1079,59 @@ function registerConsoleCallbacks(state, callbacks) {
1047
1079
  return registrations;
1048
1080
  }
1049
1081
  var CALLBACK_STREAM_THRESHOLD = 64 * 1024;
1082
+ var NULL_BODY_STATUSES = new Set([101, 103, 204, 205, 304]);
1050
1083
  function registerFetchCallback(state, callback) {
1051
1084
  const callbackId = state.nextCallbackId++;
1052
1085
  state.callbacksNeedingRequestId.add(callbackId);
1053
1086
  state.callbacks.set(callbackId, async (serialized, requestId) => {
1054
1087
  const data = serialized;
1055
1088
  const signalController = new AbortController;
1089
+ const callbackRequestId = typeof requestId === "number" ? requestId : undefined;
1090
+ if (callbackRequestId !== undefined) {
1091
+ state.callbackAbortControllers.set(callbackRequestId, signalController);
1092
+ }
1056
1093
  if (data.signalAborted) {
1057
1094
  signalController.abort();
1058
1095
  }
1059
- const init = {
1060
- method: data.method,
1061
- headers: data.headers,
1062
- rawBody: data.body ?? null,
1063
- body: data.body ?? null,
1064
- signal: signalController.signal
1065
- };
1066
- const response = await callback(data.url, init);
1067
- const contentLength = response.headers.get("content-length");
1068
- const knownSize = contentLength ? parseInt(contentLength, 10) : null;
1069
- const isNetworkResponse = response.url && (response.url.startsWith("http://") || response.url.startsWith("https://"));
1070
- const shouldStream = isNetworkResponse && response.body && (knownSize === null || knownSize > CALLBACK_STREAM_THRESHOLD);
1071
- if (shouldStream && response.body) {
1072
- const streamId = state.nextStreamId++;
1073
- const headers = [];
1074
- response.headers.forEach((value, key) => {
1075
- headers.push([key, value]);
1076
- });
1077
- sendMessage(state.socket, {
1078
- type: MessageType.CALLBACK_STREAM_START,
1079
- requestId,
1080
- streamId,
1081
- metadata: {
1082
- status: response.status,
1083
- statusText: response.statusText,
1084
- headers,
1085
- url: response.url || undefined
1086
- }
1087
- });
1088
- streamCallbackResponseBody(state, streamId, requestId, response.body);
1089
- 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
+ }
1090
1134
  }
1091
- return serializeResponse(response);
1092
1135
  });
1093
1136
  return { callbackId, name: "fetch", type: "async" };
1094
1137
  }
@@ -1694,4 +1737,4 @@ export {
1694
1737
  connect
1695
1738
  };
1696
1739
 
1697
- //# debugId=17E01CA2373935E664756E2164756E21
1740
+ //# debugId=A9015ACD8149CB6C64756E2164756E21