zero-com 1.14.2 → 1.14.4

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.d.ts CHANGED
@@ -41,6 +41,7 @@ export declare const updateRegistryForFile: (filePath: string, contextDir: strin
41
41
  export declare const getImportedServerFunctions: (sourceFile: SourceFile, registry: ServerFuncRegistry) => Map<string, ServerFuncInfo>;
42
42
  export declare const collectCallSiteReplacements: (sourceFile: SourceFile, importedFuncs: Map<string, ServerFuncInfo>) => Replacement[];
43
43
  export declare const hasHandleCall: (sourceFile: SourceFile) => boolean;
44
+ export declare const generateContextStorageInit: () => string;
44
45
  export declare const generateRegistryRequires: (registry: ServerFuncRegistry) => string;
45
46
  export declare const collectHandleCallReplacements: (sourceFile: SourceFile) => Replacement[];
46
47
  export declare const collectContextCallReplacements: (sourceFile: SourceFile) => Replacement[];
package/lib/common.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.applyReplacements = exports.emitToJs = exports.transformSourceFile = exports.generateClientStubs = exports.appendRegistryCode = exports.applyReplacementsWithMap = exports.collectFuncCallReplacements = exports.collectSendCallReplacements = exports.collectRunWithContextCallReplacements = exports.collectContextCallReplacements = exports.collectHandleCallReplacements = exports.generateRegistryRequires = exports.hasHandleCall = exports.collectCallSiteReplacements = exports.getImportedServerFunctions = exports.updateRegistryForFile = exports.buildRegistry = exports.mightNeedTransform = exports.createProject = exports.resolveFilePath = exports.isFromLibrary = exports.getReplacements = exports.generateCompilationId = exports.formatFuncIdName = exports.FILE_EXTENSIONS = exports.LIBRARY_NAME = exports.RUN_WITH_CONTEXT_NAME = exports.CONTEXT_NAME = exports.CALL_NAME = exports.HANDLE_NAME = exports.SERVER_FUNCTION_WRAPPER_NAME = exports.ZERO_COM_CONTEXT_STORAGE = exports.ZERO_COM_SERVER_REGISTRY = exports.ZERO_COM_CLIENT_CALL = void 0;
6
+ exports.applyReplacements = exports.emitToJs = exports.transformSourceFile = exports.generateClientStubs = exports.appendRegistryCode = exports.applyReplacementsWithMap = exports.collectFuncCallReplacements = exports.collectSendCallReplacements = exports.collectRunWithContextCallReplacements = exports.collectContextCallReplacements = exports.collectHandleCallReplacements = exports.generateRegistryRequires = exports.generateContextStorageInit = exports.hasHandleCall = exports.collectCallSiteReplacements = exports.getImportedServerFunctions = exports.updateRegistryForFile = exports.buildRegistry = exports.mightNeedTransform = exports.createProject = exports.resolveFilePath = exports.isFromLibrary = exports.getReplacements = exports.generateCompilationId = exports.formatFuncIdName = exports.FILE_EXTENSIONS = exports.LIBRARY_NAME = exports.RUN_WITH_CONTEXT_NAME = exports.CONTEXT_NAME = exports.CALL_NAME = exports.HANDLE_NAME = exports.SERVER_FUNCTION_WRAPPER_NAME = exports.ZERO_COM_CONTEXT_STORAGE = exports.ZERO_COM_SERVER_REGISTRY = exports.ZERO_COM_CLIENT_CALL = void 0;
7
7
  const fs_1 = __importDefault(require("fs"));
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const magic_string_1 = __importDefault(require("magic-string"));
@@ -230,16 +230,19 @@ const hasHandleCall = (sourceFile) => {
230
230
  return found;
231
231
  };
232
232
  exports.hasHandleCall = hasHandleCall;
233
+ // Initialize ZERO_COM_CONTEXT_STORAGE inline so it lives inside the bundle
234
+ // and gets mangled together with all references. Without this, the runtime
235
+ // module may be externalized (loaded via Node.js require) and set the
236
+ // un-mangled name while the bundle references the mangled name.
237
+ const generateContextStorageInit = () => {
238
+ return `if (!globalThis.${exports.ZERO_COM_CONTEXT_STORAGE}) { globalThis.${exports.ZERO_COM_CONTEXT_STORAGE} = new (require('async_hooks').AsyncLocalStorage)(); }`;
239
+ };
240
+ exports.generateContextStorageInit = generateContextStorageInit;
233
241
  const generateRegistryRequires = (registry) => {
234
242
  const requires = Array.from(registry.keys())
235
243
  .map(fp => `require(${JSON.stringify(fp)});`)
236
244
  .join('\n');
237
- // Initialize ZERO_COM_CONTEXT_STORAGE inline so it lives inside the bundle
238
- // and gets mangled together with all references. Without this, the runtime
239
- // module may be externalized (loaded via Node.js require) and set the
240
- // un-mangled name while the bundle references the mangled name.
241
- const initContextStorage = `if (!globalThis.${exports.ZERO_COM_CONTEXT_STORAGE}) { globalThis.${exports.ZERO_COM_CONTEXT_STORAGE} = new (require('async_hooks').AsyncLocalStorage)(); }`;
242
- return initContextStorage + '\n' + requires;
245
+ return (0, exports.generateContextStorageInit)() + '\n' + requires;
243
246
  };
244
247
  exports.generateRegistryRequires = generateRegistryRequires;
245
248
  const collectHandleCallReplacements = (sourceFile) => {
@@ -423,9 +426,18 @@ const transformSourceFile = (filePath, content, registry, options = {}, project)
423
426
  return { content: (0, exports.appendRegistryCode)(sourceFile, fileRegistry), transformed: true };
424
427
  }
425
428
  const isHandleFile = target === 'server' && (0, exports.hasHandleCall)(sourceFile);
429
+ // Any server file that references ZERO_COM_CONTEXT_STORAGE after transformation
430
+ // needs the inline init so the global is available even without the handle file.
431
+ const needsContextStorageInit = target === 'server' && !development && replacements.some(r => r.content.includes(exports.ZERO_COM_CONTEXT_STORAGE));
426
432
  if (replacements.length > 0) {
427
433
  const { code, map } = (0, exports.applyReplacementsWithMap)(content, replacements, filePath);
428
- const finalContent = isHandleFile ? (0, exports.generateRegistryRequires)(registry) + '\n' + code : code;
434
+ let finalContent = code;
435
+ if (isHandleFile) {
436
+ finalContent = (0, exports.generateRegistryRequires)(registry) + '\n' + finalContent;
437
+ }
438
+ else if (needsContextStorageInit) {
439
+ finalContent = (0, exports.generateContextStorageInit)() + '\n' + finalContent;
440
+ }
429
441
  return { content: finalContent, transformed: true, map };
430
442
  }
431
443
  if (isHandleFile) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zero-com",
3
- "version": "1.14.2",
3
+ "version": "1.14.4",
4
4
  "main": "index.js",
5
5
  "browser": {
6
6
  "./lib/async-local-storage.js": "./lib/async-local-storage.browser.js"