zero-com 1.6.2 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Zero-com
2
2
 
3
- A ~420 Bytes utility for transparently communicating client and server in full-stack projects through compile-time code transformation, with end-to-end static type checking.
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[];
@@ -32,6 +31,7 @@ export declare const getImportedServerFunctions: (sourceFile: SourceFile, regist
32
31
  export declare const transformCallSites: (sourceFile: SourceFile, importedFuncs: Map<string, ServerFuncInfo>) => boolean;
33
32
  export declare const transformHandleCalls: (sourceFile: SourceFile) => boolean;
34
33
  export declare const transformSendCalls: (sourceFile: SourceFile) => boolean;
34
+ export declare const transformFuncCalls: (sourceFile: SourceFile) => boolean;
35
35
  export declare const appendRegistryCode: (sourceFile: SourceFile, fileRegistry: Map<string, ServerFuncInfo>) => string;
36
36
  export type TransformResult = {
37
37
  content: 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.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.EXEC_FUNC_NAME = exports.CALL_NAME = exports.HANDLE_NAME = exports.SERVER_FUNCTION_WRAPPER_NAME = exports.ZERO_COM_SERVER_REGISTRY = exports.ZERO_COM_CLIENT_CALL = void 0;
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
- callExpr.replaceWithText(`${exports.EXEC_FUNC_NAME}(globalThis.${exports.ZERO_COM_SERVER_REGISTRY}[${args[0].getText()}], ${args[1].getText()}, ${args[2].getText()})`);
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;
@@ -208,47 +211,42 @@ const transformSendCalls = (sourceFile) => {
208
211
  return modified;
209
212
  };
210
213
  exports.transformSendCalls = transformSendCalls;
214
+ // Transform func(fn) calls to just fn.
215
+ // The func() wrapper is only needed for type-level transformation (RemoveContextParam).
216
+ // At runtime, we just need the raw function. The registration code (appendRegistryCode)
217
+ // will set requireContext on the function based on compile-time analysis.
218
+ const transformFuncCalls = (sourceFile) => {
219
+ let modified = false;
220
+ sourceFile.forEachDescendant((node) => {
221
+ if (node.getKind() !== ts_morph_1.SyntaxKind.CallExpression)
222
+ return;
223
+ const callExpr = node;
224
+ if (!(0, exports.isFromLibrary)(callExpr, exports.LIBRARY_NAME) || getCalleeName(callExpr) !== exports.SERVER_FUNCTION_WRAPPER_NAME)
225
+ return;
226
+ const args = callExpr.getArguments();
227
+ if (args.length !== 1)
228
+ return;
229
+ // Replace func(fn) with just fn
230
+ callExpr.replaceWithText(args[0].getText());
231
+ modified = true;
232
+ });
233
+ return modified;
234
+ };
235
+ exports.transformFuncCalls = transformFuncCalls;
211
236
  const appendRegistryCode = (sourceFile, fileRegistry) => {
237
+ // Generate registration code for each server function.
238
+ // We set requireContext based on compile-time analysis of the function's first parameter type.
239
+ // If the first param is context<T>, requireContext = true, otherwise false.
240
+ // This allows execFunc() at runtime to know whether to inject the context as the first argument.
212
241
  const registrations = Array.from(fileRegistry.values())
213
- .map(info => `globalThis.${exports.ZERO_COM_SERVER_REGISTRY}['${info.funcId}'] = ${info.exportName}`)
214
- .join(';\n');
242
+ .map(info => `globalThis.${exports.ZERO_COM_SERVER_REGISTRY}['${info.funcId}'] = ${info.exportName};\n` +
243
+ `${info.exportName}.requireContext = ${info.requireContext};`)
244
+ .join('\n');
215
245
  return `${sourceFile.getFullText()}
216
246
  if (!globalThis.${exports.ZERO_COM_SERVER_REGISTRY}) globalThis.${exports.ZERO_COM_SERVER_REGISTRY} = Object.create(null);
217
- ${registrations};`;
247
+ ${registrations}`;
218
248
  };
219
249
  exports.appendRegistryCode = appendRegistryCode;
220
- // Helper to ensure a specific named import exists
221
- const ensureImport = (sourceFile, moduleSpecifier, namedImport) => {
222
- const importDecls = sourceFile.getImportDeclarations().filter(d => d.getModuleSpecifierValue() === moduleSpecifier);
223
- // 1. Look for an existing import declaration that already has named imports or is empty (not namespace/default only)
224
- let targetDecl = importDecls.find(d => !d.getNamespaceImport() && !d.getDefaultImport());
225
- if (!targetDecl) {
226
- // Fallback: look for any declaration that has named imports (mixed with default is fine)
227
- targetDecl = importDecls.find(d => d.getNamedImports().length > 0);
228
- }
229
- if (targetDecl) {
230
- if (!targetDecl.getNamedImports().some(ni => ni.getName() === namedImport)) {
231
- targetDecl.addNamedImport(namedImport);
232
- }
233
- }
234
- else {
235
- // 2. Check for default import where we can append named import
236
- // Avoid namespace imports as we can't add named imports to `import * as ns`
237
- const defaultImportDecl = importDecls.find(d => d.getDefaultImport() && !d.getNamespaceImport());
238
- if (defaultImportDecl) {
239
- if (!defaultImportDecl.getNamedImports().some(ni => ni.getName() === namedImport)) {
240
- defaultImportDecl.addNamedImport(namedImport);
241
- }
242
- }
243
- else {
244
- // 3. Create new import declaration
245
- sourceFile.addImportDeclaration({
246
- moduleSpecifier,
247
- namedImports: [namedImport]
248
- });
249
- }
250
- }
251
- };
252
250
  const transformSourceFile = (filePath, content, registry) => {
253
251
  const project = (0, exports.createProject)();
254
252
  const sourceFile = project.createSourceFile(filePath, content, { overwrite: true });
@@ -262,10 +260,10 @@ const transformSourceFile = (filePath, content, registry) => {
262
260
  const callsTransformed = (0, exports.transformCallSites)(sourceFile, importedFuncs);
263
261
  const handleTransformed = (0, exports.transformHandleCalls)(sourceFile);
264
262
  const sendTransformed = (0, exports.transformSendCalls)(sourceFile);
265
- if (handleTransformed) {
266
- ensureImport(sourceFile, exports.LIBRARY_NAME, exports.EXEC_FUNC_NAME);
267
- }
263
+ // Transform func() calls in files that define server functions.
264
+ // This strips the func() wrapper since it's only needed for types.
268
265
  if (isServerFunctionFile) {
266
+ (0, exports.transformFuncCalls)(sourceFile);
269
267
  return { content: (0, exports.appendRegistryCode)(sourceFile, fileRegistry), transformed: true };
270
268
  }
271
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,24 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.call = exports.handle = exports.execFunc = void 0;
3
+ exports.call = exports.handle = void 0;
4
4
  exports.func = func;
5
5
  // Implementation
6
- function func(fn) {
7
- fn.requireContext = true;
8
- return fn;
6
+ // User-facing function - transformed by plugin to just the inner function.
7
+ // The plugin also appends code to register the function and set requireContext based on
8
+ // compile-time analysis of whether the first parameter is context<T>.
9
+ function func(_fn) {
10
+ throw new Error('func() was not transformed. Ensure the zero-com plugin is configured.');
9
11
  }
10
- // Internal implementation - receives the actual function from registry
11
- const execFunc = (sfn, ctx, args) => {
12
- const fn = sfn;
13
- if (fn.requireContext) {
14
- return fn(ctx, ...args);
15
- }
16
- else {
17
- return fn(...args);
18
- }
19
- };
20
- exports.execFunc = execFunc;
21
- // 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
22
13
  const handle = (_funcId, _ctx, _args) => {
23
14
  throw new Error('handle() was not transformed. Ensure the zero-com plugin is configured.');
24
15
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zero-com",
3
- "version": "1.6.2",
3
+ "version": "1.6.4",
4
4
  "main": "index.js",
5
5
  "repository": "https://github.com/yosbelms/zero-com",
6
6
  "keywords": [