zero-com 1.13.4 → 1.14.1

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.
package/lib/common.js CHANGED
@@ -229,9 +229,15 @@ const hasHandleCall = (sourceFile) => {
229
229
  };
230
230
  exports.hasHandleCall = hasHandleCall;
231
231
  const generateRegistryRequires = (registry) => {
232
- return Array.from(registry.keys())
232
+ const requires = Array.from(registry.keys())
233
233
  .map(fp => `require(${JSON.stringify(fp)});`)
234
234
  .join('\n');
235
+ // Initialize ZERO_COM_CONTEXT_STORAGE inline so it lives inside the bundle
236
+ // and gets mangled together with all references. Without this, the runtime
237
+ // module may be externalized (loaded via Node.js require) and set the
238
+ // un-mangled name while the bundle references the mangled name.
239
+ const initContextStorage = `if (!globalThis.${exports.ZERO_COM_CONTEXT_STORAGE}) { globalThis.${exports.ZERO_COM_CONTEXT_STORAGE} = new (require('async_hooks').AsyncLocalStorage)(); }`;
240
+ return initContextStorage + '\n' + requires;
235
241
  };
236
242
  exports.generateRegistryRequires = generateRegistryRequires;
237
243
  const collectHandleCallReplacements = (sourceFile) => {
package/lib/runtime.d.ts CHANGED
@@ -1,8 +1,10 @@
1
+ import { asyncLocalStorage as storage } from './async-local-storage';
1
2
  declare global {
2
3
  var ZERO_COM_SERVER_REGISTRY: {
3
4
  [funcId: string]: (...args: any[]) => any;
4
5
  };
5
6
  var ZERO_COM_CLIENT_CALL: (funcId: string, args: any[]) => any;
7
+ var ZERO_COM_CONTEXT_STORAGE: typeof storage;
6
8
  }
7
9
  export declare function context<T = unknown>(): T;
8
10
  export declare function func<F extends (...args: any[]) => any>(fn: F): F;
package/lib/runtime.js CHANGED
@@ -4,6 +4,13 @@ exports.call = exports.runWithContext = exports.handle = void 0;
4
4
  exports.context = context;
5
5
  exports.func = func;
6
6
  const async_local_storage_1 = require("./async-local-storage");
7
+ // Initialize context storage on globalThis — the single source of truth.
8
+ // All functions (context, handle, runWithContext, ZERO_COM_CLIENT_CALL) use this
9
+ // global instance so that context propagates correctly even when the module is
10
+ // loaded multiple times (e.g. webpack-bundled AND externalized).
11
+ if (!globalThis.ZERO_COM_CONTEXT_STORAGE && async_local_storage_1.asyncLocalStorage) {
12
+ globalThis.ZERO_COM_CONTEXT_STORAGE = async_local_storage_1.asyncLocalStorage;
13
+ }
7
14
  // Default server-side implementation: call directly from registry
8
15
  // This enables server functions to call other server functions without transport.
9
16
  // If a request context exists (set by handle()), it is propagated automatically.
@@ -13,25 +20,27 @@ const async_local_storage_1 = require("./async-local-storage");
13
20
  if (typeof globalThis.ZERO_COM_CLIENT_CALL === 'undefined') {
14
21
  globalThis.ZERO_COM_CLIENT_CALL = (funcId, args) => {
15
22
  var _a;
16
- if (!async_local_storage_1.asyncLocalStorage) {
23
+ const als = globalThis.ZERO_COM_CONTEXT_STORAGE;
24
+ if (!als) {
17
25
  throw new Error('Server function called on client without transport configured. Call call() first.');
18
26
  }
19
27
  const fn = (_a = globalThis.ZERO_COM_SERVER_REGISTRY) === null || _a === void 0 ? void 0 : _a[funcId];
20
28
  if (!fn)
21
29
  throw new Error(`Function not found: ${funcId}`);
22
- const ctx = async_local_storage_1.asyncLocalStorage.getStore();
30
+ const ctx = als.getStore();
23
31
  if (ctx !== undefined) {
24
- return async_local_storage_1.asyncLocalStorage.run(ctx, () => fn(...args));
32
+ return als.run(ctx, () => fn(...args));
25
33
  }
26
34
  return fn(...args);
27
35
  };
28
36
  }
29
37
  // Get the current context - call this inside server functions
30
38
  function context() {
31
- if (!async_local_storage_1.asyncLocalStorage) {
39
+ const als = globalThis.ZERO_COM_CONTEXT_STORAGE;
40
+ if (!als) {
32
41
  throw new Error('context() is only available on the server');
33
42
  }
34
- const ctx = async_local_storage_1.asyncLocalStorage.getStore();
43
+ const ctx = als.getStore();
35
44
  if (ctx === undefined) {
36
45
  throw new Error('context() called outside of a server function');
37
46
  }
@@ -50,18 +59,20 @@ const handle = (funcId, ctx, args) => {
50
59
  if (!fn) {
51
60
  throw new Error(`Function not found in registry: ${funcId}`);
52
61
  }
53
- if (!async_local_storage_1.asyncLocalStorage) {
62
+ const als = globalThis.ZERO_COM_CONTEXT_STORAGE;
63
+ if (!als) {
54
64
  throw new Error('handle() is only available on the server');
55
65
  }
56
- return async_local_storage_1.asyncLocalStorage.run(ctx, () => fn(...args));
66
+ return als.run(ctx, () => fn(...args));
57
67
  };
58
68
  exports.handle = handle;
59
69
  // Run a callback within a context, making context() available inside it.
60
70
  // Use this in server-only code that does not go through handle() (e.g. auth callbacks).
61
71
  const runWithContext = (ctx, fn) => {
62
- if (!async_local_storage_1.asyncLocalStorage)
72
+ const als = globalThis.ZERO_COM_CONTEXT_STORAGE;
73
+ if (!als)
63
74
  throw new Error('runWithContext() is only available on the server');
64
- return async_local_storage_1.asyncLocalStorage.run(ctx, fn);
75
+ return als.run(ctx, fn);
65
76
  };
66
77
  exports.runWithContext = runWithContext;
67
78
  // Client calls this to set up transport (overrides default server-side behavior)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zero-com",
3
- "version": "1.13.4",
3
+ "version": "1.14.1",
4
4
  "main": "index.js",
5
5
  "browser": {
6
6
  "./lib/async-local-storage.js": "./lib/async-local-storage.browser.js"