zero-com 1.14.0 → 1.14.2
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 +4 -0
- package/lib/common.js +51 -2
- 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;
|
|
@@ -41,6 +43,8 @@ export declare const collectCallSiteReplacements: (sourceFile: SourceFile, impor
|
|
|
41
43
|
export declare const hasHandleCall: (sourceFile: SourceFile) => boolean;
|
|
42
44
|
export declare const generateRegistryRequires: (registry: ServerFuncRegistry) => string;
|
|
43
45
|
export declare const collectHandleCallReplacements: (sourceFile: SourceFile) => Replacement[];
|
|
46
|
+
export declare const collectContextCallReplacements: (sourceFile: SourceFile) => Replacement[];
|
|
47
|
+
export declare const collectRunWithContextCallReplacements: (sourceFile: SourceFile) => Replacement[];
|
|
44
48
|
export declare const collectSendCallReplacements: (sourceFile: SourceFile) => Replacement[];
|
|
45
49
|
export declare const collectFuncCallReplacements: (sourceFile: SourceFile) => Replacement[];
|
|
46
50
|
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.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) => {
|
|
@@ -229,9 +231,15 @@ const hasHandleCall = (sourceFile) => {
|
|
|
229
231
|
};
|
|
230
232
|
exports.hasHandleCall = hasHandleCall;
|
|
231
233
|
const generateRegistryRequires = (registry) => {
|
|
232
|
-
|
|
234
|
+
const requires = Array.from(registry.keys())
|
|
233
235
|
.map(fp => `require(${JSON.stringify(fp)});`)
|
|
234
236
|
.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;
|
|
235
243
|
};
|
|
236
244
|
exports.generateRegistryRequires = generateRegistryRequires;
|
|
237
245
|
const collectHandleCallReplacements = (sourceFile) => {
|
|
@@ -258,6 +266,45 @@ const collectHandleCallReplacements = (sourceFile) => {
|
|
|
258
266
|
return replacements;
|
|
259
267
|
};
|
|
260
268
|
exports.collectHandleCallReplacements = collectHandleCallReplacements;
|
|
269
|
+
const collectContextCallReplacements = (sourceFile) => {
|
|
270
|
+
const replacements = [];
|
|
271
|
+
sourceFile.forEachDescendant((node) => {
|
|
272
|
+
if (node.getKind() !== ts_morph_1.SyntaxKind.CallExpression)
|
|
273
|
+
return;
|
|
274
|
+
const callExpr = node;
|
|
275
|
+
if (!(0, exports.isFromLibrary)(callExpr, exports.LIBRARY_NAME) || getCalleeName(callExpr) !== exports.CONTEXT_NAME)
|
|
276
|
+
return;
|
|
277
|
+
replacements.push({
|
|
278
|
+
start: callExpr.getStart(),
|
|
279
|
+
end: callExpr.getEnd(),
|
|
280
|
+
content: `globalThis.${exports.ZERO_COM_CONTEXT_STORAGE}.getStore()`
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
return replacements;
|
|
284
|
+
};
|
|
285
|
+
exports.collectContextCallReplacements = collectContextCallReplacements;
|
|
286
|
+
const collectRunWithContextCallReplacements = (sourceFile) => {
|
|
287
|
+
const replacements = [];
|
|
288
|
+
sourceFile.forEachDescendant((node) => {
|
|
289
|
+
if (node.getKind() !== ts_morph_1.SyntaxKind.CallExpression)
|
|
290
|
+
return;
|
|
291
|
+
const callExpr = node;
|
|
292
|
+
if (!(0, exports.isFromLibrary)(callExpr, exports.LIBRARY_NAME) || getCalleeName(callExpr) !== exports.RUN_WITH_CONTEXT_NAME)
|
|
293
|
+
return;
|
|
294
|
+
const args = callExpr.getArguments();
|
|
295
|
+
if (args.length < 2)
|
|
296
|
+
return;
|
|
297
|
+
const ctx = args[0].getText();
|
|
298
|
+
const fn = args[1].getText();
|
|
299
|
+
replacements.push({
|
|
300
|
+
start: callExpr.getStart(),
|
|
301
|
+
end: callExpr.getEnd(),
|
|
302
|
+
content: `globalThis.${exports.ZERO_COM_CONTEXT_STORAGE}.run(${ctx}, ${fn})`
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
return replacements;
|
|
306
|
+
};
|
|
307
|
+
exports.collectRunWithContextCallReplacements = collectRunWithContextCallReplacements;
|
|
261
308
|
const collectSendCallReplacements = (sourceFile) => {
|
|
262
309
|
const replacements = [];
|
|
263
310
|
sourceFile.forEachDescendant((node) => {
|
|
@@ -357,6 +404,8 @@ const transformSourceFile = (filePath, content, registry, options = {}, project)
|
|
|
357
404
|
if (!development) {
|
|
358
405
|
replacements.push(...(0, exports.collectHandleCallReplacements)(sourceFile));
|
|
359
406
|
replacements.push(...(0, exports.collectSendCallReplacements)(sourceFile));
|
|
407
|
+
replacements.push(...(0, exports.collectContextCallReplacements)(sourceFile));
|
|
408
|
+
replacements.push(...(0, exports.collectRunWithContextCallReplacements)(sourceFile));
|
|
360
409
|
}
|
|
361
410
|
// Handle server function files
|
|
362
411
|
if (isServerFunctionFile) {
|