@ricsam/isolate-client 0.1.22 → 0.1.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.
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ricsam/isolate-client",
3
- "version": "0.1.22",
3
+ "version": "0.1.24",
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();
@@ -160,6 +167,7 @@ async function connect(options = {}) {
160
167
  requestId,
161
168
  options: {
162
169
  memoryLimitMB: runtimeOptions.memoryLimitMB,
170
+ executionTimeout: runtimeOptions.executionTimeout,
163
171
  cwd: runtimeOptions.cwd,
164
172
  callbacks,
165
173
  testEnvironment: testEnvironmentOption,
@@ -260,6 +268,18 @@ function handleMessage(message, state) {
260
268
  }
261
269
  break;
262
270
  }
271
+ case MessageType.CALLBACK_ABORT: {
272
+ const msg = message;
273
+ const invocation = state.activeCallbackInvocations.get(msg.targetRequestId);
274
+ if (invocation) {
275
+ invocation.aborted = true;
276
+ }
277
+ const controller = state.callbackAbortControllers.get(msg.targetRequestId);
278
+ if (controller && !controller.signal.aborted) {
279
+ controller.abort();
280
+ }
281
+ break;
282
+ }
263
283
  case MessageType.ISOLATE_EVENT: {
264
284
  const msg = message;
265
285
  handleIsolateEvent(msg, state);
@@ -422,6 +442,8 @@ function handleMessage(message, state) {
422
442
  }
423
443
  async function handleCallbackInvoke(invoke, state) {
424
444
  const callback = state.callbacks.get(invoke.callbackId);
445
+ const invocationState = { aborted: false };
446
+ state.activeCallbackInvocations.set(invoke.requestId, invocationState);
425
447
  const response = {
426
448
  type: MessageType.CALLBACK_RESPONSE,
427
449
  requestId: invoke.requestId
@@ -431,7 +453,10 @@ async function handleCallbackInvoke(invoke, state) {
431
453
  name: "Error",
432
454
  message: `Unknown callback: ${invoke.callbackId}`
433
455
  };
434
- sendMessage(state.socket, response);
456
+ if (!invocationState.aborted) {
457
+ sendMessage(state.socket, response);
458
+ }
459
+ state.activeCallbackInvocations.delete(invoke.requestId);
435
460
  } else {
436
461
  try {
437
462
  const needsRequestId = state.callbacksNeedingRequestId.has(invoke.callbackId);
@@ -439,9 +464,15 @@ async function handleCallbackInvoke(invoke, state) {
439
464
  if (result && typeof result === "object" && result.__callbackStreaming) {
440
465
  return;
441
466
  }
467
+ if (invocationState.aborted) {
468
+ return;
469
+ }
442
470
  response.result = result;
443
471
  sendMessage(state.socket, response);
444
472
  } catch (err) {
473
+ if (invocationState.aborted) {
474
+ return;
475
+ }
445
476
  const error = err;
446
477
  response.error = {
447
478
  name: error.name,
@@ -449,6 +480,8 @@ async function handleCallbackInvoke(invoke, state) {
449
480
  stack: error.stack
450
481
  };
451
482
  sendMessage(state.socket, response);
483
+ } finally {
484
+ state.activeCallbackInvocations.delete(invoke.requestId);
452
485
  }
453
486
  }
454
487
  }
@@ -629,6 +662,7 @@ async function createRuntime(state, options = {}, namespaceId) {
629
662
  requestId,
630
663
  options: {
631
664
  memoryLimitMB: options.memoryLimitMB,
665
+ executionTimeout: options.executionTimeout,
632
666
  cwd: options.cwd,
633
667
  callbacks,
634
668
  testEnvironment: testEnvironmentOption,
@@ -958,7 +992,8 @@ async function createRuntime(state, options = {}, namespaceId) {
958
992
  requestId: reqId,
959
993
  isolateId,
960
994
  code,
961
- filename: options2?.filename
995
+ filename: options2?.filename,
996
+ executionTimeout: options2?.executionTimeout
962
997
  };
963
998
  await sendRequest(state, req);
964
999
  },
@@ -1054,42 +1089,52 @@ function registerFetchCallback(state, callback) {
1054
1089
  state.callbacks.set(callbackId, async (serialized, requestId) => {
1055
1090
  const data = serialized;
1056
1091
  const signalController = new AbortController;
1092
+ const callbackRequestId = typeof requestId === "number" ? requestId : undefined;
1093
+ if (callbackRequestId !== undefined) {
1094
+ state.callbackAbortControllers.set(callbackRequestId, signalController);
1095
+ }
1057
1096
  if (data.signalAborted) {
1058
1097
  signalController.abort();
1059
1098
  }
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 };
1099
+ try {
1100
+ const init = {
1101
+ method: data.method,
1102
+ headers: data.headers,
1103
+ rawBody: data.body ?? null,
1104
+ body: data.body ?? null,
1105
+ signal: signalController.signal
1106
+ };
1107
+ const response = await callback(data.url, init);
1108
+ const contentLength = response.headers.get("content-length");
1109
+ const knownSize = contentLength ? parseInt(contentLength, 10) : null;
1110
+ const isNetworkResponse = response.url && (response.url.startsWith("http://") || response.url.startsWith("https://"));
1111
+ const shouldStream = isNetworkResponse && !NULL_BODY_STATUSES.has(response.status) && !!response.body && (knownSize === null || knownSize > CALLBACK_STREAM_THRESHOLD);
1112
+ if (shouldStream && response.body) {
1113
+ const streamId = state.nextStreamId++;
1114
+ const headers = [];
1115
+ response.headers.forEach((value, key) => {
1116
+ headers.push([key, value]);
1117
+ });
1118
+ sendMessage(state.socket, {
1119
+ type: MessageType.CALLBACK_STREAM_START,
1120
+ requestId,
1121
+ streamId,
1122
+ metadata: {
1123
+ status: response.status,
1124
+ statusText: response.statusText,
1125
+ headers,
1126
+ url: response.url || undefined
1127
+ }
1128
+ });
1129
+ streamCallbackResponseBody(state, streamId, requestId, response.body);
1130
+ return { __callbackStreaming: true, streamId };
1131
+ }
1132
+ return serializeResponse(response);
1133
+ } finally {
1134
+ if (callbackRequestId !== undefined) {
1135
+ state.callbackAbortControllers.delete(callbackRequestId);
1136
+ }
1091
1137
  }
1092
- return serializeResponse(response);
1093
1138
  });
1094
1139
  return { callbackId, name: "fetch", type: "async" };
1095
1140
  }
@@ -1695,4 +1740,4 @@ export {
1695
1740
  connect
1696
1741
  };
1697
1742
 
1698
- //# debugId=F239739EEADB58BD64756E2164756E21
1743
+ //# debugId=06A31C0AB117B3FE64756E2164756E21