@ricsam/isolate-daemon 0.1.16 → 0.1.17

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.
@@ -69,7 +69,9 @@ function handleConnection(socket, state) {
69
69
  nextStreamId: 1,
70
70
  activeStreams: new Map,
71
71
  streamReceivers: new Map,
72
- callbackStreamReceivers: new Map
72
+ callbackStreamReceivers: new Map,
73
+ dispatchAbortControllers: new Map,
74
+ earlyAbortedDispatches: new Set
73
75
  };
74
76
  state.connections.set(socket, connection);
75
77
  const parser = import_isolate_protocol.createFrameParser();
@@ -86,6 +88,11 @@ function handleConnection(socket, state) {
86
88
  }
87
89
  });
88
90
  socket.on("close", () => {
91
+ for (const [, controller] of connection.dispatchAbortControllers) {
92
+ controller.abort();
93
+ }
94
+ connection.dispatchAbortControllers.clear();
95
+ connection.earlyAbortedDispatches.clear();
89
96
  for (const isolateId of connection.isolates) {
90
97
  const instance = state.isolates.get(isolateId);
91
98
  if (instance) {
@@ -147,6 +154,9 @@ async function handleMessage(message, connection, state) {
147
154
  case import_isolate_protocol.MessageType.DISPATCH_REQUEST:
148
155
  await handleDispatchRequest(message, connection, state);
149
156
  break;
157
+ case import_isolate_protocol.MessageType.DISPATCH_REQUEST_ABORT:
158
+ handleDispatchRequestAbort(message, connection);
159
+ break;
150
160
  case import_isolate_protocol.MessageType.CALLBACK_RESPONSE:
151
161
  handleCallbackResponse(message, connection);
152
162
  break;
@@ -756,7 +766,8 @@ async function handleCreateRuntime(message, connection, state) {
756
766
  url,
757
767
  method: init.method,
758
768
  headers: init.headers,
759
- body: init.rawBody
769
+ body: init.rawBody,
770
+ signalAborted: init.signal.aborted
760
771
  };
761
772
  const result = await invokeCallbackWithReconnect(callbackContext, () => callbackContext.fetch, [serialized], "Fetch", invokeClientCallback);
762
773
  if (result && typeof result === "object" && result.__streamingResponse) {
@@ -996,6 +1007,12 @@ async function handleDispatchRequest(message, connection, state) {
996
1007
  return;
997
1008
  }
998
1009
  instance.lastActivity = Date.now();
1010
+ const dispatchAbortController = new AbortController;
1011
+ connection.dispatchAbortControllers.set(message.requestId, dispatchAbortController);
1012
+ if (connection.earlyAbortedDispatches.has(message.requestId) || message.request.signalAborted) {
1013
+ dispatchAbortController.abort();
1014
+ connection.earlyAbortedDispatches.delete(message.requestId);
1015
+ }
999
1016
  try {
1000
1017
  let requestBody = null;
1001
1018
  if (message.request.bodyStreamId !== undefined) {
@@ -1006,9 +1023,12 @@ async function handleDispatchRequest(message, connection, state) {
1006
1023
  const request = new Request(message.request.url, {
1007
1024
  method: message.request.method,
1008
1025
  headers: message.request.headers,
1009
- body: requestBody
1026
+ body: requestBody,
1027
+ signal: dispatchAbortController.signal
1028
+ });
1029
+ const response = await instance.runtime.fetch.dispatchRequest(request, {
1030
+ signal: dispatchAbortController.signal
1010
1031
  });
1011
- const response = await instance.runtime.fetch.dispatchRequest(request);
1012
1032
  if (response.body) {
1013
1033
  await sendStreamedResponse(connection, message.requestId, response);
1014
1034
  } else {
@@ -1028,7 +1048,18 @@ async function handleDispatchRequest(message, connection, state) {
1028
1048
  } catch (err) {
1029
1049
  const error = err;
1030
1050
  sendError(connection.socket, message.requestId, import_isolate_protocol.ErrorCode.SCRIPT_ERROR, error.message, { name: error.name, stack: error.stack });
1051
+ } finally {
1052
+ connection.dispatchAbortControllers.delete(message.requestId);
1053
+ connection.earlyAbortedDispatches.delete(message.requestId);
1054
+ }
1055
+ }
1056
+ function handleDispatchRequestAbort(message, connection) {
1057
+ const controller = connection.dispatchAbortControllers.get(message.targetRequestId);
1058
+ if (controller) {
1059
+ controller.abort();
1060
+ return;
1031
1061
  }
1062
+ connection.earlyAbortedDispatches.add(message.targetRequestId);
1032
1063
  }
1033
1064
  function receiveStreamedBody(connection, streamId) {
1034
1065
  return new Promise((resolve, reject) => {
@@ -1661,4 +1692,4 @@ async function handleClearCollectedData(message, connection, state) {
1661
1692
  }
1662
1693
  }
1663
1694
 
1664
- //# debugId=D9712A3ECDBAF18B64756E2164756E21
1695
+ //# debugId=B1D2B1F7510AB4C564756E2164756E21