zero-com 1.6.3 → 1.6.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/README.md +1 -1
- package/lib/common.d.ts +0 -1
- package/lib/common.js +7 -42
- package/lib/runtime.d.ts +0 -1
- package/lib/runtime.js +2 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Zero-com
|
|
2
2
|
|
|
3
|
-
The
|
|
3
|
+
The 0 bytes utility for transparently communicating client and server in full-stack projects through compile-time code transformation, with end-to-end static type checking.
|
|
4
4
|
|
|
5
5
|
## Table of Contents
|
|
6
6
|
|
package/lib/common.d.ts
CHANGED
|
@@ -14,7 +14,6 @@ export declare const ZERO_COM_SERVER_REGISTRY = "ZERO_COM_SERVER_REGISTRY";
|
|
|
14
14
|
export declare const SERVER_FUNCTION_WRAPPER_NAME = "func";
|
|
15
15
|
export declare const HANDLE_NAME = "handle";
|
|
16
16
|
export declare const CALL_NAME = "call";
|
|
17
|
-
export declare const EXEC_FUNC_NAME = "execFunc";
|
|
18
17
|
export declare const CONTEXT_TYPE_NAME = "context";
|
|
19
18
|
export declare const LIBRARY_NAME = "zero-com";
|
|
20
19
|
export declare const FILE_EXTENSIONS: 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.appendRegistryCode = exports.transformFuncCalls = exports.transformSendCalls = exports.transformHandleCalls = exports.transformCallSites = exports.getImportedServerFunctions = exports.buildRegistry = exports.createProject = exports.resolveFilePath = exports.isFromLibrary = exports.getReplacements = exports.generateCompilationId = exports.formatFuncIdName = exports.FILE_EXTENSIONS = exports.LIBRARY_NAME = exports.CONTEXT_TYPE_NAME = exports.
|
|
6
|
+
exports.applyReplacements = exports.emitToJs = exports.transformSourceFile = exports.appendRegistryCode = exports.transformFuncCalls = exports.transformSendCalls = exports.transformHandleCalls = exports.transformCallSites = exports.getImportedServerFunctions = exports.buildRegistry = exports.createProject = exports.resolveFilePath = exports.isFromLibrary = exports.getReplacements = exports.generateCompilationId = exports.formatFuncIdName = exports.FILE_EXTENSIONS = exports.LIBRARY_NAME = exports.CONTEXT_TYPE_NAME = exports.CALL_NAME = exports.HANDLE_NAME = exports.SERVER_FUNCTION_WRAPPER_NAME = 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 ts_morph_1 = require("ts-morph");
|
|
@@ -13,7 +13,6 @@ exports.ZERO_COM_SERVER_REGISTRY = 'ZERO_COM_SERVER_REGISTRY';
|
|
|
13
13
|
exports.SERVER_FUNCTION_WRAPPER_NAME = 'func';
|
|
14
14
|
exports.HANDLE_NAME = 'handle';
|
|
15
15
|
exports.CALL_NAME = 'call';
|
|
16
|
-
exports.EXEC_FUNC_NAME = 'execFunc';
|
|
17
16
|
exports.CONTEXT_TYPE_NAME = 'context';
|
|
18
17
|
exports.LIBRARY_NAME = 'zero-com';
|
|
19
18
|
exports.FILE_EXTENSIONS = ['', '.ts', '.tsx', '.js', '.jsx', '.mjs'];
|
|
@@ -185,7 +184,11 @@ const transformHandleCalls = (sourceFile) => {
|
|
|
185
184
|
const args = callExpr.getArguments();
|
|
186
185
|
if (args.length < 3)
|
|
187
186
|
return;
|
|
188
|
-
|
|
187
|
+
// Inline the logic directly using a named function for better stack traces.
|
|
188
|
+
const funcId = args[0].getText();
|
|
189
|
+
const ctx = args[1].getText();
|
|
190
|
+
const argsArray = args[2].getText();
|
|
191
|
+
callExpr.replaceWithText(`(function handle(__fn, __ctx, __args) { return __fn.requireContext ? __fn(__ctx, ...__args) : __fn(...__args); })(globalThis.${exports.ZERO_COM_SERVER_REGISTRY}[${funcId}], ${ctx}, ${argsArray})`);
|
|
189
192
|
modified = true;
|
|
190
193
|
});
|
|
191
194
|
return modified;
|
|
@@ -244,38 +247,6 @@ if (!globalThis.${exports.ZERO_COM_SERVER_REGISTRY}) globalThis.${exports.ZERO_C
|
|
|
244
247
|
${registrations}`;
|
|
245
248
|
};
|
|
246
249
|
exports.appendRegistryCode = appendRegistryCode;
|
|
247
|
-
// Helper to ensure a specific named import exists
|
|
248
|
-
const ensureImport = (sourceFile, moduleSpecifier, namedImport) => {
|
|
249
|
-
const importDecls = sourceFile.getImportDeclarations().filter(d => d.getModuleSpecifierValue() === moduleSpecifier);
|
|
250
|
-
// 1. Look for an existing import declaration that already has named imports or is empty (not namespace/default only)
|
|
251
|
-
let targetDecl = importDecls.find(d => !d.getNamespaceImport() && !d.getDefaultImport());
|
|
252
|
-
if (!targetDecl) {
|
|
253
|
-
// Fallback: look for any declaration that has named imports (mixed with default is fine)
|
|
254
|
-
targetDecl = importDecls.find(d => d.getNamedImports().length > 0);
|
|
255
|
-
}
|
|
256
|
-
if (targetDecl) {
|
|
257
|
-
if (!targetDecl.getNamedImports().some(ni => ni.getName() === namedImport)) {
|
|
258
|
-
targetDecl.addNamedImport(namedImport);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
else {
|
|
262
|
-
// 2. Check for default import where we can append named import
|
|
263
|
-
// Avoid namespace imports as we can't add named imports to `import * as ns`
|
|
264
|
-
const defaultImportDecl = importDecls.find(d => d.getDefaultImport() && !d.getNamespaceImport());
|
|
265
|
-
if (defaultImportDecl) {
|
|
266
|
-
if (!defaultImportDecl.getNamedImports().some(ni => ni.getName() === namedImport)) {
|
|
267
|
-
defaultImportDecl.addNamedImport(namedImport);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
else {
|
|
271
|
-
// 3. Create new import declaration
|
|
272
|
-
sourceFile.addImportDeclaration({
|
|
273
|
-
moduleSpecifier,
|
|
274
|
-
namedImports: [namedImport]
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
250
|
const transformSourceFile = (filePath, content, registry) => {
|
|
280
251
|
const project = (0, exports.createProject)();
|
|
281
252
|
const sourceFile = project.createSourceFile(filePath, content, { overwrite: true });
|
|
@@ -291,14 +262,8 @@ const transformSourceFile = (filePath, content, registry) => {
|
|
|
291
262
|
const sendTransformed = (0, exports.transformSendCalls)(sourceFile);
|
|
292
263
|
// Transform func() calls in files that define server functions.
|
|
293
264
|
// This strips the func() wrapper since it's only needed for types.
|
|
294
|
-
let funcTransformed = false;
|
|
295
|
-
if (isServerFunctionFile) {
|
|
296
|
-
funcTransformed = (0, exports.transformFuncCalls)(sourceFile);
|
|
297
|
-
}
|
|
298
|
-
if (handleTransformed) {
|
|
299
|
-
ensureImport(sourceFile, exports.LIBRARY_NAME, exports.EXEC_FUNC_NAME);
|
|
300
|
-
}
|
|
301
265
|
if (isServerFunctionFile) {
|
|
266
|
+
(0, exports.transformFuncCalls)(sourceFile);
|
|
302
267
|
return { content: (0, exports.appendRegistryCode)(sourceFile, fileRegistry), transformed: true };
|
|
303
268
|
}
|
|
304
269
|
if (callsTransformed || handleTransformed || sendTransformed) {
|
package/lib/runtime.d.ts
CHANGED
|
@@ -10,7 +10,6 @@ export type context<T = unknown> = T & {
|
|
|
10
10
|
};
|
|
11
11
|
type RemoveContextParam<F> = F extends (ctx: infer C, ...args: infer A) => infer R ? C extends context<unknown> ? (...args: A) => R : F : F;
|
|
12
12
|
export declare function func<F extends (...args: any[]) => any>(fn: F): RemoveContextParam<F>;
|
|
13
|
-
export declare const execFunc: (sfn: ReturnType<typeof func>, ctx: any, args: any[]) => ReturnType<typeof sfn>;
|
|
14
13
|
export declare const handle: (_funcId: string, _ctx: any, _args: any[]) => any;
|
|
15
14
|
export declare const call: (_fn: (funcId: string, args: any[]) => Promise<any>) => void;
|
|
16
15
|
export {};
|
package/lib/runtime.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.call = exports.handle =
|
|
3
|
+
exports.call = exports.handle = void 0;
|
|
4
4
|
exports.func = func;
|
|
5
5
|
// Implementation
|
|
6
6
|
// User-facing function - transformed by plugin to just the inner function.
|
|
@@ -9,18 +9,7 @@ exports.func = func;
|
|
|
9
9
|
function func(_fn) {
|
|
10
10
|
throw new Error('func() was not transformed. Ensure the zero-com plugin is configured.');
|
|
11
11
|
}
|
|
12
|
-
//
|
|
13
|
-
const execFunc = (sfn, ctx, args) => {
|
|
14
|
-
const fn = sfn;
|
|
15
|
-
if (fn.requireContext) {
|
|
16
|
-
return fn(ctx, ...args);
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
return fn(...args);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
exports.execFunc = execFunc;
|
|
23
|
-
// User-facing function - transformed by plugin to execFunc(globalThis.ZERO_COM_SERVER_REGISTRY[funcId], ctx, args)
|
|
12
|
+
// User-facing function - transformed by plugin to inline code that checks requireContext and calls the function
|
|
24
13
|
const handle = (_funcId, _ctx, _args) => {
|
|
25
14
|
throw new Error('handle() was not transformed. Ensure the zero-com plugin is configured.');
|
|
26
15
|
};
|