zero-com 1.13.3 → 1.14.0
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.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const asyncLocalStorage: undefined;
|
package/lib/runtime.d.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
+
import { asyncLocalStorage as storage } from './async-local-storage';
|
|
1
2
|
declare global {
|
|
2
3
|
var ZERO_COM_SERVER_REGISTRY: {
|
|
3
4
|
[funcId: string]: (...args: any[]) => any;
|
|
4
5
|
};
|
|
5
6
|
var ZERO_COM_CLIENT_CALL: (funcId: string, args: any[]) => any;
|
|
6
|
-
var ZERO_COM_CONTEXT_STORAGE:
|
|
7
|
-
run: <T>(ctx: any, fn: () => T) => T;
|
|
8
|
-
getStore: () => any;
|
|
9
|
-
} | undefined;
|
|
7
|
+
var ZERO_COM_CONTEXT_STORAGE: typeof storage;
|
|
10
8
|
}
|
|
11
9
|
export declare function context<T = unknown>(): T;
|
|
12
10
|
export declare function func<F extends (...args: any[]) => any>(fn: F): F;
|
package/lib/runtime.js
CHANGED
|
@@ -3,37 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.call = exports.runWithContext = exports.handle = void 0;
|
|
4
4
|
exports.context = context;
|
|
5
5
|
exports.func = func;
|
|
6
|
-
|
|
7
|
-
//
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// where require() is not available.
|
|
14
|
-
const mod = typeof require === 'function'
|
|
15
|
-
? require('async_hooks')
|
|
16
|
-
: process.getBuiltinModule('node:async_hooks');
|
|
17
|
-
globalThis.ZERO_COM_CONTEXT_STORAGE = new mod.AsyncLocalStorage();
|
|
18
|
-
}
|
|
19
|
-
catch (_a) {
|
|
20
|
-
// Browser environment - context storage not available
|
|
21
|
-
globalThis.ZERO_COM_CONTEXT_STORAGE = undefined;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return globalThis.ZERO_COM_CONTEXT_STORAGE;
|
|
25
|
-
}
|
|
26
|
-
// Get the current context - call this inside server functions
|
|
27
|
-
function context() {
|
|
28
|
-
const storage = getContextStorage();
|
|
29
|
-
if (!storage) {
|
|
30
|
-
throw new Error('context() is only available on the server');
|
|
31
|
-
}
|
|
32
|
-
const ctx = storage.getStore();
|
|
33
|
-
if (ctx === undefined) {
|
|
34
|
-
throw new Error('context() called outside of a server function');
|
|
35
|
-
}
|
|
36
|
-
return ctx;
|
|
6
|
+
const async_local_storage_1 = require("./async-local-storage");
|
|
7
|
+
// Initialize context storage on globalThis — the single source of truth.
|
|
8
|
+
// All functions (context, handle, runWithContext, ZERO_COM_CLIENT_CALL) use this
|
|
9
|
+
// global instance so that context propagates correctly even when the module is
|
|
10
|
+
// loaded multiple times (e.g. webpack-bundled AND externalized).
|
|
11
|
+
if (!globalThis.ZERO_COM_CONTEXT_STORAGE && async_local_storage_1.asyncLocalStorage) {
|
|
12
|
+
globalThis.ZERO_COM_CONTEXT_STORAGE = async_local_storage_1.asyncLocalStorage;
|
|
37
13
|
}
|
|
38
14
|
// Default server-side implementation: call directly from registry
|
|
39
15
|
// This enables server functions to call other server functions without transport.
|
|
@@ -44,20 +20,32 @@ function context() {
|
|
|
44
20
|
if (typeof globalThis.ZERO_COM_CLIENT_CALL === 'undefined') {
|
|
45
21
|
globalThis.ZERO_COM_CLIENT_CALL = (funcId, args) => {
|
|
46
22
|
var _a;
|
|
47
|
-
const
|
|
48
|
-
if (!
|
|
23
|
+
const als = globalThis.ZERO_COM_CONTEXT_STORAGE;
|
|
24
|
+
if (!als) {
|
|
49
25
|
throw new Error('Server function called on client without transport configured. Call call() first.');
|
|
50
26
|
}
|
|
51
27
|
const fn = (_a = globalThis.ZERO_COM_SERVER_REGISTRY) === null || _a === void 0 ? void 0 : _a[funcId];
|
|
52
28
|
if (!fn)
|
|
53
29
|
throw new Error(`Function not found: ${funcId}`);
|
|
54
|
-
const ctx =
|
|
30
|
+
const ctx = als.getStore();
|
|
55
31
|
if (ctx !== undefined) {
|
|
56
|
-
return
|
|
32
|
+
return als.run(ctx, () => fn(...args));
|
|
57
33
|
}
|
|
58
34
|
return fn(...args);
|
|
59
35
|
};
|
|
60
36
|
}
|
|
37
|
+
// Get the current context - call this inside server functions
|
|
38
|
+
function context() {
|
|
39
|
+
const als = globalThis.ZERO_COM_CONTEXT_STORAGE;
|
|
40
|
+
if (!als) {
|
|
41
|
+
throw new Error('context() is only available on the server');
|
|
42
|
+
}
|
|
43
|
+
const ctx = als.getStore();
|
|
44
|
+
if (ctx === undefined) {
|
|
45
|
+
throw new Error('context() called outside of a server function');
|
|
46
|
+
}
|
|
47
|
+
return ctx;
|
|
48
|
+
}
|
|
61
49
|
// func() just returns the function as-is
|
|
62
50
|
// In production mode: transformed by plugin to just the inner function
|
|
63
51
|
function func(fn) {
|
|
@@ -71,20 +59,20 @@ const handle = (funcId, ctx, args) => {
|
|
|
71
59
|
if (!fn) {
|
|
72
60
|
throw new Error(`Function not found in registry: ${funcId}`);
|
|
73
61
|
}
|
|
74
|
-
const
|
|
75
|
-
if (!
|
|
62
|
+
const als = globalThis.ZERO_COM_CONTEXT_STORAGE;
|
|
63
|
+
if (!als) {
|
|
76
64
|
throw new Error('handle() is only available on the server');
|
|
77
65
|
}
|
|
78
|
-
return
|
|
66
|
+
return als.run(ctx, () => fn(...args));
|
|
79
67
|
};
|
|
80
68
|
exports.handle = handle;
|
|
81
69
|
// Run a callback within a context, making context() available inside it.
|
|
82
70
|
// Use this in server-only code that does not go through handle() (e.g. auth callbacks).
|
|
83
71
|
const runWithContext = (ctx, fn) => {
|
|
84
|
-
const
|
|
85
|
-
if (!
|
|
72
|
+
const als = globalThis.ZERO_COM_CONTEXT_STORAGE;
|
|
73
|
+
if (!als)
|
|
86
74
|
throw new Error('runWithContext() is only available on the server');
|
|
87
|
-
return
|
|
75
|
+
return als.run(ctx, fn);
|
|
88
76
|
};
|
|
89
77
|
exports.runWithContext = runWithContext;
|
|
90
78
|
// Client calls this to set up transport (overrides default server-side behavior)
|
package/package.json
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zero-com",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.0",
|
|
4
4
|
"main": "index.js",
|
|
5
|
+
"browser": {
|
|
6
|
+
"./lib/async-local-storage.js": "./lib/async-local-storage.browser.js"
|
|
7
|
+
},
|
|
5
8
|
"repository": "https://github.com/yosbelms/zero-com",
|
|
6
9
|
"keywords": [
|
|
7
10
|
"webpack",
|