@ricsam/isolate 0.1.5 → 0.1.7

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.
Files changed (37) hide show
  1. package/README.md +26 -6
  2. package/dist/cjs/internal/async-context/index.cjs +269 -8
  3. package/dist/cjs/internal/async-context/index.cjs.map +3 -3
  4. package/dist/cjs/internal/core/index.cjs +12 -4
  5. package/dist/cjs/internal/core/index.cjs.map +3 -3
  6. package/dist/cjs/internal/fetch/index.cjs +52 -13
  7. package/dist/cjs/internal/fetch/index.cjs.map +3 -3
  8. package/dist/cjs/internal/module-loader/bundle.cjs +111 -35
  9. package/dist/cjs/internal/module-loader/bundle.cjs.map +3 -3
  10. package/dist/cjs/internal/runtime/index.cjs +2 -2
  11. package/dist/cjs/internal/runtime/index.cjs.map +2 -2
  12. package/dist/cjs/internal/server/index.cjs +150 -19
  13. package/dist/cjs/internal/server/index.cjs.map +3 -3
  14. package/dist/cjs/internal/timers/index.cjs +39 -9
  15. package/dist/cjs/internal/timers/index.cjs.map +3 -3
  16. package/dist/cjs/package.json +1 -1
  17. package/dist/cjs/server/app-server.cjs +12 -10
  18. package/dist/cjs/server/app-server.cjs.map +3 -3
  19. package/dist/mjs/internal/async-context/index.mjs +269 -8
  20. package/dist/mjs/internal/async-context/index.mjs.map +3 -3
  21. package/dist/mjs/internal/core/index.mjs +12 -4
  22. package/dist/mjs/internal/core/index.mjs.map +3 -3
  23. package/dist/mjs/internal/fetch/index.mjs +52 -13
  24. package/dist/mjs/internal/fetch/index.mjs.map +3 -3
  25. package/dist/mjs/internal/module-loader/bundle.mjs +111 -35
  26. package/dist/mjs/internal/module-loader/bundle.mjs.map +3 -3
  27. package/dist/mjs/internal/runtime/index.mjs +2 -2
  28. package/dist/mjs/internal/runtime/index.mjs.map +2 -2
  29. package/dist/mjs/internal/server/index.mjs +150 -19
  30. package/dist/mjs/internal/server/index.mjs.map +3 -3
  31. package/dist/mjs/internal/timers/index.mjs +39 -9
  32. package/dist/mjs/internal/timers/index.mjs.map +3 -3
  33. package/dist/mjs/package.json +1 -1
  34. package/dist/mjs/server/app-server.mjs +12 -10
  35. package/dist/mjs/server/app-server.mjs.map +3 -3
  36. package/dist/types/internal/server/index.d.ts +7 -2
  37. package/package.json +2 -2
@@ -1847,11 +1847,16 @@ function setupClientWebSocket(context, clientWsCommandCallbacks) {
1847
1847
  }
1848
1848
  };
1849
1849
 
1850
- const __wrapAsyncContextCallback = (callback) => (
1851
- typeof callback === 'function' && globalThis.AsyncContext?.Snapshot?.wrap
1852
- ? globalThis.AsyncContext.Snapshot.wrap(callback)
1850
+ const __wrapAsyncContextCallback = (callback, options = {}) => (
1851
+ typeof callback === 'function' && globalThis.__isolateAsyncContextInternals?.wrapCallback
1852
+ ? globalThis.__isolateAsyncContextInternals.wrapCallback(callback, options)
1853
1853
  : callback
1854
1854
  );
1855
+ const __releaseAsyncContextCallback = (callback) => (
1856
+ typeof callback === 'function' && globalThis.__isolateAsyncContextInternals?.releaseCallback
1857
+ ? globalThis.__isolateAsyncContextInternals.releaseCallback(callback)
1858
+ : false
1859
+ );
1855
1860
 
1856
1861
  // Helper to dispatch events
1857
1862
  function dispatchEvent(ws, event) {
@@ -1988,13 +1993,20 @@ function setupClientWebSocket(context, clientWsCommandCallbacks) {
1988
1993
  get CLOSED() { return WebSocket.CLOSED; }
1989
1994
 
1990
1995
  _setHandler(type, handler) {
1996
+ const previous = this._handlers.get(type);
1997
+ if (previous?.wrapped) {
1998
+ __releaseAsyncContextCallback(previous.wrapped);
1999
+ }
1991
2000
  if (typeof handler !== 'function') {
1992
2001
  this._handlers.set(type, null);
1993
2002
  return;
1994
2003
  }
1995
2004
  this._handlers.set(type, {
1996
2005
  original: handler,
1997
- wrapped: __wrapAsyncContextCallback(handler),
2006
+ wrapped: __wrapAsyncContextCallback(handler, {
2007
+ resource: this,
2008
+ type: 'isolate.websocket',
2009
+ }),
1998
2010
  });
1999
2011
  }
2000
2012
 
@@ -2074,7 +2086,10 @@ function setupClientWebSocket(context, clientWsCommandCallbacks) {
2074
2086
  if (!listeners.some((entry) => entry.original === listener)) {
2075
2087
  listeners.push({
2076
2088
  original: listener,
2077
- wrapped: __wrapAsyncContextCallback(listener),
2089
+ wrapped: __wrapAsyncContextCallback(listener, {
2090
+ resource: this,
2091
+ type: 'isolate.websocket',
2092
+ }),
2078
2093
  });
2079
2094
  }
2080
2095
  }
@@ -2084,6 +2099,7 @@ function setupClientWebSocket(context, clientWsCommandCallbacks) {
2084
2099
  if (!listeners) return;
2085
2100
  const index = listeners.findIndex((entry) => entry.original === listener || entry.wrapped === listener);
2086
2101
  if (index !== -1) {
2102
+ __releaseAsyncContextCallback(listeners[index].wrapped);
2087
2103
  listeners.splice(index, 1);
2088
2104
  }
2089
2105
  }
@@ -2142,6 +2158,18 @@ function setupClientWebSocket(context, clientWsCommandCallbacks) {
2142
2158
  this.#readyState = WebSocket.CLOSED;
2143
2159
  const event = new _CloseEvent('close', { code, reason, wasClean });
2144
2160
  dispatchEvent(this, event);
2161
+ for (const handler of this._handlers.values()) {
2162
+ if (handler?.wrapped) {
2163
+ __releaseAsyncContextCallback(handler.wrapped);
2164
+ }
2165
+ }
2166
+ for (const listeners of this._listeners.values()) {
2167
+ for (const listener of listeners) {
2168
+ __releaseAsyncContextCallback(listener.wrapped);
2169
+ }
2170
+ }
2171
+ this._handlers.clear();
2172
+ this._listeners.clear();
2145
2173
  __clientWebSockets.delete(this.#socketId);
2146
2174
  }
2147
2175
  }
@@ -2177,9 +2205,9 @@ function setupServe(context) {
2177
2205
  context.evalSync(`
2178
2206
  (function() {
2179
2207
  globalThis.__serveOptions__ = null;
2180
- const __wrapAsyncContextCallback = (callback) => (
2181
- typeof callback === 'function' && globalThis.AsyncContext?.Snapshot?.wrap
2182
- ? globalThis.AsyncContext.Snapshot.wrap(callback)
2208
+ const __wrapAsyncContextCallback = (callback, options = {}) => (
2209
+ typeof callback === 'function' && globalThis.__isolateAsyncContextInternals?.wrapCallback
2210
+ ? globalThis.__isolateAsyncContextInternals.wrapCallback(callback, options)
2183
2211
  : callback
2184
2212
  );
2185
2213
 
@@ -2190,14 +2218,19 @@ function setupServe(context) {
2190
2218
 
2191
2219
  const wrapped = { ...options };
2192
2220
  if (typeof options.fetch === 'function') {
2193
- wrapped.fetch = __wrapAsyncContextCallback(options.fetch);
2221
+ wrapped.fetch = __wrapAsyncContextCallback(options.fetch, {
2222
+ type: 'isolate.serve',
2223
+ });
2194
2224
  }
2195
2225
 
2196
2226
  if (options.websocket && typeof options.websocket === 'object') {
2197
2227
  wrapped.websocket = { ...options.websocket };
2198
2228
  for (const name of ['open', 'message', 'close', 'error']) {
2199
2229
  if (typeof options.websocket[name] === 'function') {
2200
- wrapped.websocket[name] = __wrapAsyncContextCallback(options.websocket[name]);
2230
+ wrapped.websocket[name] = __wrapAsyncContextCallback(
2231
+ options.websocket[name],
2232
+ { type: 'isolate.serve.websocket' },
2233
+ );
2201
2234
  }
2202
2235
  }
2203
2236
  }
@@ -2245,10 +2278,15 @@ async function setupFetch(context, options) {
2245
2278
  (function() {
2246
2279
  const __eventListeners = new Map();
2247
2280
  const __wrapAsyncContextCallback = (callback) => (
2248
- typeof callback === 'function' && globalThis.AsyncContext?.Snapshot?.wrap
2249
- ? globalThis.AsyncContext.Snapshot.wrap(callback)
2281
+ typeof callback === 'function' && globalThis.__isolateAsyncContextInternals?.wrapCallback
2282
+ ? globalThis.__isolateAsyncContextInternals.wrapCallback(callback, { type: 'isolate.event' })
2250
2283
  : callback
2251
2284
  );
2285
+ const __releaseAsyncContextCallback = (callback) => (
2286
+ typeof callback === 'function' && globalThis.__isolateAsyncContextInternals?.releaseCallback
2287
+ ? globalThis.__isolateAsyncContextInternals.releaseCallback(callback)
2288
+ : false
2289
+ );
2252
2290
 
2253
2291
  globalThis.__on = function(event, callback) {
2254
2292
  let listeners = __eventListeners.get(event);
@@ -2260,6 +2298,7 @@ async function setupFetch(context, options) {
2260
2298
  listeners.add(wrapped);
2261
2299
  return function() {
2262
2300
  listeners.delete(wrapped);
2301
+ __releaseAsyncContextCallback(wrapped);
2263
2302
  if (listeners.size === 0) {
2264
2303
  __eventListeners.delete(event);
2265
2304
  }
@@ -2639,4 +2678,4 @@ export {
2639
2678
  clearAllInstanceState
2640
2679
  };
2641
2680
 
2642
- //# debugId=60FF4FEB06AFDED964756E2164756E21
2681
+ //# debugId=30E0F5B08A888C7164756E2164756E21