zero-com 1.14.1 → 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 +5 -0
- package/lib/common.js +63 -8
- package/package.json +1 -1
package/lib/common.d.ts
CHANGED
|
@@ -22,6 +22,8 @@ export declare const ZERO_COM_CONTEXT_STORAGE = "ZERO_COM_CONTEXT_STORAGE";
|
|
|
22
22
|
export declare const SERVER_FUNCTION_WRAPPER_NAME = "func";
|
|
23
23
|
export declare const HANDLE_NAME = "handle";
|
|
24
24
|
export declare const CALL_NAME = "call";
|
|
25
|
+
export declare const CONTEXT_NAME = "context";
|
|
26
|
+
export declare const RUN_WITH_CONTEXT_NAME = "runWithContext";
|
|
25
27
|
export declare const LIBRARY_NAME = "zero-com";
|
|
26
28
|
export declare const FILE_EXTENSIONS: string[];
|
|
27
29
|
export declare const formatFuncIdName: (funcName: string, filePath: string, line: number) => string;
|
|
@@ -39,8 +41,11 @@ export declare const updateRegistryForFile: (filePath: string, contextDir: strin
|
|
|
39
41
|
export declare const getImportedServerFunctions: (sourceFile: SourceFile, registry: ServerFuncRegistry) => Map<string, ServerFuncInfo>;
|
|
40
42
|
export declare const collectCallSiteReplacements: (sourceFile: SourceFile, importedFuncs: Map<string, ServerFuncInfo>) => Replacement[];
|
|
41
43
|
export declare const hasHandleCall: (sourceFile: SourceFile) => boolean;
|
|
44
|
+
export declare const generateContextStorageInit: () => string;
|
|
42
45
|
export declare const generateRegistryRequires: (registry: ServerFuncRegistry) => string;
|
|
43
46
|
export declare const collectHandleCallReplacements: (sourceFile: SourceFile) => Replacement[];
|
|
47
|
+
export declare const collectContextCallReplacements: (sourceFile: SourceFile) => Replacement[];
|
|
48
|
+
export declare const collectRunWithContextCallReplacements: (sourceFile: SourceFile) => Replacement[];
|
|
44
49
|
export declare const collectSendCallReplacements: (sourceFile: SourceFile) => Replacement[];
|
|
45
50
|
export declare const collectFuncCallReplacements: (sourceFile: SourceFile) => Replacement[];
|
|
46
51
|
export declare const applyReplacementsWithMap: (source: string, replacements: Replacement[], filePath: string) => {
|
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.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.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"));
|
|
@@ -15,6 +15,8 @@ exports.ZERO_COM_CONTEXT_STORAGE = 'ZERO_COM_CONTEXT_STORAGE';
|
|
|
15
15
|
exports.SERVER_FUNCTION_WRAPPER_NAME = 'func';
|
|
16
16
|
exports.HANDLE_NAME = 'handle';
|
|
17
17
|
exports.CALL_NAME = 'call';
|
|
18
|
+
exports.CONTEXT_NAME = 'context';
|
|
19
|
+
exports.RUN_WITH_CONTEXT_NAME = 'runWithContext';
|
|
18
20
|
exports.LIBRARY_NAME = 'zero-com';
|
|
19
21
|
exports.FILE_EXTENSIONS = ['', '.ts', '.tsx', '.js', '.jsx', '.mjs'];
|
|
20
22
|
const formatFuncIdName = (funcName, filePath, line) => {
|
|
@@ -228,16 +230,19 @@ const hasHandleCall = (sourceFile) => {
|
|
|
228
230
|
return found;
|
|
229
231
|
};
|
|
230
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;
|
|
231
241
|
const generateRegistryRequires = (registry) => {
|
|
232
242
|
const requires = Array.from(registry.keys())
|
|
233
243
|
.map(fp => `require(${JSON.stringify(fp)});`)
|
|
234
244
|
.join('\n');
|
|
235
|
-
|
|
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;
|
|
245
|
+
return (0, exports.generateContextStorageInit)() + '\n' + requires;
|
|
241
246
|
};
|
|
242
247
|
exports.generateRegistryRequires = generateRegistryRequires;
|
|
243
248
|
const collectHandleCallReplacements = (sourceFile) => {
|
|
@@ -264,6 +269,45 @@ const collectHandleCallReplacements = (sourceFile) => {
|
|
|
264
269
|
return replacements;
|
|
265
270
|
};
|
|
266
271
|
exports.collectHandleCallReplacements = collectHandleCallReplacements;
|
|
272
|
+
const collectContextCallReplacements = (sourceFile) => {
|
|
273
|
+
const replacements = [];
|
|
274
|
+
sourceFile.forEachDescendant((node) => {
|
|
275
|
+
if (node.getKind() !== ts_morph_1.SyntaxKind.CallExpression)
|
|
276
|
+
return;
|
|
277
|
+
const callExpr = node;
|
|
278
|
+
if (!(0, exports.isFromLibrary)(callExpr, exports.LIBRARY_NAME) || getCalleeName(callExpr) !== exports.CONTEXT_NAME)
|
|
279
|
+
return;
|
|
280
|
+
replacements.push({
|
|
281
|
+
start: callExpr.getStart(),
|
|
282
|
+
end: callExpr.getEnd(),
|
|
283
|
+
content: `globalThis.${exports.ZERO_COM_CONTEXT_STORAGE}.getStore()`
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
return replacements;
|
|
287
|
+
};
|
|
288
|
+
exports.collectContextCallReplacements = collectContextCallReplacements;
|
|
289
|
+
const collectRunWithContextCallReplacements = (sourceFile) => {
|
|
290
|
+
const replacements = [];
|
|
291
|
+
sourceFile.forEachDescendant((node) => {
|
|
292
|
+
if (node.getKind() !== ts_morph_1.SyntaxKind.CallExpression)
|
|
293
|
+
return;
|
|
294
|
+
const callExpr = node;
|
|
295
|
+
if (!(0, exports.isFromLibrary)(callExpr, exports.LIBRARY_NAME) || getCalleeName(callExpr) !== exports.RUN_WITH_CONTEXT_NAME)
|
|
296
|
+
return;
|
|
297
|
+
const args = callExpr.getArguments();
|
|
298
|
+
if (args.length < 2)
|
|
299
|
+
return;
|
|
300
|
+
const ctx = args[0].getText();
|
|
301
|
+
const fn = args[1].getText();
|
|
302
|
+
replacements.push({
|
|
303
|
+
start: callExpr.getStart(),
|
|
304
|
+
end: callExpr.getEnd(),
|
|
305
|
+
content: `globalThis.${exports.ZERO_COM_CONTEXT_STORAGE}.run(${ctx}, ${fn})`
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
return replacements;
|
|
309
|
+
};
|
|
310
|
+
exports.collectRunWithContextCallReplacements = collectRunWithContextCallReplacements;
|
|
267
311
|
const collectSendCallReplacements = (sourceFile) => {
|
|
268
312
|
const replacements = [];
|
|
269
313
|
sourceFile.forEachDescendant((node) => {
|
|
@@ -363,6 +407,8 @@ const transformSourceFile = (filePath, content, registry, options = {}, project)
|
|
|
363
407
|
if (!development) {
|
|
364
408
|
replacements.push(...(0, exports.collectHandleCallReplacements)(sourceFile));
|
|
365
409
|
replacements.push(...(0, exports.collectSendCallReplacements)(sourceFile));
|
|
410
|
+
replacements.push(...(0, exports.collectContextCallReplacements)(sourceFile));
|
|
411
|
+
replacements.push(...(0, exports.collectRunWithContextCallReplacements)(sourceFile));
|
|
366
412
|
}
|
|
367
413
|
// Handle server function files
|
|
368
414
|
if (isServerFunctionFile) {
|
|
@@ -380,9 +426,18 @@ const transformSourceFile = (filePath, content, registry, options = {}, project)
|
|
|
380
426
|
return { content: (0, exports.appendRegistryCode)(sourceFile, fileRegistry), transformed: true };
|
|
381
427
|
}
|
|
382
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));
|
|
383
432
|
if (replacements.length > 0) {
|
|
384
433
|
const { code, map } = (0, exports.applyReplacementsWithMap)(content, replacements, filePath);
|
|
385
|
-
|
|
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
|
+
}
|
|
386
441
|
return { content: finalContent, transformed: true, map };
|
|
387
442
|
}
|
|
388
443
|
if (isHandleFile) {
|