@bitfab/sdk 0.38.1 → 0.38.3
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/dist/autoTrace.cjs +187 -0
- package/dist/autoTrace.cjs.map +1 -0
- package/dist/autoTrace.d.cts +39 -0
- package/dist/autoTrace.d.ts +39 -0
- package/dist/autoTrace.js +20 -0
- package/dist/chunk-GFBQ2AMO.js +129 -0
- package/dist/chunk-GFBQ2AMO.js.map +1 -0
- package/dist/chunk-H6LZRFMN.js +59 -0
- package/dist/chunk-H6LZRFMN.js.map +1 -0
- package/dist/{chunk-WD4AO3BK.js → chunk-RJPHMJWO.js} +243 -24
- package/dist/chunk-RJPHMJWO.js.map +1 -0
- package/dist/{chunk-MGA7ROIK.js → chunk-WX6AEFCP.js} +18 -58
- package/dist/chunk-WX6AEFCP.js.map +1 -0
- package/dist/index.cjs +315 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +94 -4
- package/dist/index.d.ts +94 -4
- package/dist/index.js +6 -2
- package/dist/node.cjs +315 -16
- package/dist/node.cjs.map +1 -1
- package/dist/node.d.cts +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.js +9 -4
- package/dist/node.js.map +1 -1
- package/dist/{replay-WSFDZ2UV.js → replay-FAO7D7CG.js} +3 -2
- package/dist/replay-FAO7D7CG.js.map +1 -0
- package/dist/replayCli.js +620 -0
- package/dist/replayCli.js.map +1 -0
- package/package.json +16 -2
- package/dist/chunk-MGA7ROIK.js.map +0 -1
- package/dist/chunk-WD4AO3BK.js.map +0 -1
- /package/dist/{replay-WSFDZ2UV.js.map → autoTrace.js.map} +0 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/autoTrace.ts
|
|
21
|
+
var autoTrace_exports = {};
|
|
22
|
+
__export(autoTrace_exports, {
|
|
23
|
+
__bitfabAutoSpan: () => __bitfabAutoSpan,
|
|
24
|
+
__bitfabAutoTraceActive: () => __bitfabAutoTraceActive,
|
|
25
|
+
__bitfabAutoWrap: () => __bitfabAutoWrap,
|
|
26
|
+
__setBitfabAutoTraceCapturePolicy: () => __setBitfabAutoTraceCapturePolicy,
|
|
27
|
+
getAutoTraceCapturePolicy: () => getAutoTraceCapturePolicy,
|
|
28
|
+
runWithAutoTraceContext: () => runWithAutoTraceContext,
|
|
29
|
+
runWithAutoTraceRootContext: () => runWithAutoTraceRootContext
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(autoTrace_exports);
|
|
32
|
+
|
|
33
|
+
// src/asyncStorage.ts
|
|
34
|
+
var AsyncLocalStorageClass = null;
|
|
35
|
+
var initDone = false;
|
|
36
|
+
function registerAsyncLocalStorageClass(cls) {
|
|
37
|
+
if (!AsyncLocalStorageClass) {
|
|
38
|
+
AsyncLocalStorageClass = cls;
|
|
39
|
+
}
|
|
40
|
+
initDone = true;
|
|
41
|
+
}
|
|
42
|
+
var asyncStorageReady = (typeof process !== "undefined" && process.versions?.node ? (
|
|
43
|
+
// The join trick hides "node:async_hooks" from static analysis so
|
|
44
|
+
// bundlers that ban Node.js built-ins don't fail at build time.
|
|
45
|
+
// webpackIgnore tells webpack/turbopack to emit a native import()
|
|
46
|
+
// so Node.js can resolve the module at runtime.
|
|
47
|
+
import(
|
|
48
|
+
/* webpackIgnore: true */
|
|
49
|
+
["node", "async_hooks"].join(":")
|
|
50
|
+
).then(
|
|
51
|
+
(mod) => {
|
|
52
|
+
registerAsyncLocalStorageClass(mod.AsyncLocalStorage);
|
|
53
|
+
}
|
|
54
|
+
).catch(() => {
|
|
55
|
+
})
|
|
56
|
+
) : Promise.resolve()).then(() => {
|
|
57
|
+
initDone = true;
|
|
58
|
+
});
|
|
59
|
+
function createAsyncLocalStorage() {
|
|
60
|
+
return AsyncLocalStorageClass ? new AsyncLocalStorageClass() : null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/autoTrace.ts
|
|
64
|
+
var autoTraceGlobal = globalThis;
|
|
65
|
+
var autoTraceState = autoTraceGlobal.__bitfabAutoTraceStateV3 ?? {
|
|
66
|
+
storage: null,
|
|
67
|
+
browserScope: void 0,
|
|
68
|
+
capturePolicies: /* @__PURE__ */ new WeakMap(),
|
|
69
|
+
activeRoots: 0
|
|
70
|
+
};
|
|
71
|
+
autoTraceGlobal.__bitfabAutoTraceStateV3 = autoTraceState;
|
|
72
|
+
function initializeAutoTraceStorage() {
|
|
73
|
+
autoTraceState.storage ?? (autoTraceState.storage = createAsyncLocalStorage());
|
|
74
|
+
}
|
|
75
|
+
function runWithAutoTraceContext(context, fn, depth = 0) {
|
|
76
|
+
initializeAutoTraceStorage();
|
|
77
|
+
const scope = { context, depth };
|
|
78
|
+
if (autoTraceState.storage) {
|
|
79
|
+
return autoTraceState.storage.run(scope, fn);
|
|
80
|
+
}
|
|
81
|
+
const previous = autoTraceState.browserScope;
|
|
82
|
+
autoTraceState.browserScope = scope;
|
|
83
|
+
try {
|
|
84
|
+
return fn();
|
|
85
|
+
} finally {
|
|
86
|
+
autoTraceState.browserScope = previous;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function runWithAutoTraceRootContext(context, fn) {
|
|
90
|
+
autoTraceState.activeRoots += 1;
|
|
91
|
+
let result;
|
|
92
|
+
try {
|
|
93
|
+
result = runWithAutoTraceContext(context, fn);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
autoTraceState.activeRoots -= 1;
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
if (isAutoTraceAsyncGenerator(result)) {
|
|
99
|
+
autoTraceState.activeRoots -= 1;
|
|
100
|
+
return wrapAutoTraceAsyncGenerator(context, result);
|
|
101
|
+
}
|
|
102
|
+
if (result instanceof Promise) {
|
|
103
|
+
return result.finally(() => {
|
|
104
|
+
autoTraceState.activeRoots -= 1;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
autoTraceState.activeRoots -= 1;
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
function isAutoTraceAsyncGenerator(value) {
|
|
111
|
+
if (value === null || typeof value !== "object") {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
const candidate = value;
|
|
115
|
+
return typeof candidate.next === "function" && typeof candidate.return === "function" && typeof candidate.throw === "function" && typeof candidate[Symbol.asyncIterator] === "function";
|
|
116
|
+
}
|
|
117
|
+
function wrapAutoTraceAsyncGenerator(context, source) {
|
|
118
|
+
const step = (method, value) => runWithAutoTraceRootContext(context, () => source[method](value));
|
|
119
|
+
const wrapped = {
|
|
120
|
+
next: (value) => step("next", value),
|
|
121
|
+
return: (value) => step("return", value),
|
|
122
|
+
throw: (error) => step("throw", error),
|
|
123
|
+
[Symbol.asyncIterator]: () => wrapped
|
|
124
|
+
};
|
|
125
|
+
return wrapped;
|
|
126
|
+
}
|
|
127
|
+
function __bitfabAutoSpan(definition, inputs, fn) {
|
|
128
|
+
const scope = currentAutoTraceScope();
|
|
129
|
+
if (!scope) {
|
|
130
|
+
return fn();
|
|
131
|
+
}
|
|
132
|
+
return scope.context.invoke(definition, inputs, fn, scope.depth);
|
|
133
|
+
}
|
|
134
|
+
function __bitfabAutoWrap(definition, fn) {
|
|
135
|
+
if (fn.name === "") {
|
|
136
|
+
const nameParts = definition.name.split(".");
|
|
137
|
+
const inferredName = nameParts[nameParts.length - 1];
|
|
138
|
+
if (inferredName !== void 0) {
|
|
139
|
+
Object.defineProperty(fn, "name", {
|
|
140
|
+
configurable: true,
|
|
141
|
+
value: inferredName
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const target = fn;
|
|
146
|
+
return new Proxy(target, {
|
|
147
|
+
apply(callTarget, thisArg, args) {
|
|
148
|
+
if (!__bitfabAutoTraceActive()) {
|
|
149
|
+
return Reflect.apply(callTarget, thisArg, args);
|
|
150
|
+
}
|
|
151
|
+
return __bitfabAutoSpan(
|
|
152
|
+
definition,
|
|
153
|
+
args,
|
|
154
|
+
() => Reflect.apply(callTarget, thisArg, args)
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
function __bitfabAutoTraceActive() {
|
|
160
|
+
if (autoTraceState.activeRoots === 0) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
return currentAutoTraceScope() !== void 0;
|
|
164
|
+
}
|
|
165
|
+
function currentAutoTraceScope() {
|
|
166
|
+
initializeAutoTraceStorage();
|
|
167
|
+
return autoTraceState.storage?.getStore() ?? autoTraceState.browserScope;
|
|
168
|
+
}
|
|
169
|
+
function __setBitfabAutoTraceCapturePolicy(client, traceFunctionKey, functionIds) {
|
|
170
|
+
const policies = autoTraceState.capturePolicies.get(client) ?? /* @__PURE__ */ new Map();
|
|
171
|
+
policies.set(traceFunctionKey, new Set(functionIds));
|
|
172
|
+
autoTraceState.capturePolicies.set(client, policies);
|
|
173
|
+
}
|
|
174
|
+
function getAutoTraceCapturePolicy(client, traceFunctionKey) {
|
|
175
|
+
return autoTraceState.capturePolicies.get(client)?.get(traceFunctionKey) ?? /* @__PURE__ */ new Set();
|
|
176
|
+
}
|
|
177
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
178
|
+
0 && (module.exports = {
|
|
179
|
+
__bitfabAutoSpan,
|
|
180
|
+
__bitfabAutoTraceActive,
|
|
181
|
+
__bitfabAutoWrap,
|
|
182
|
+
__setBitfabAutoTraceCapturePolicy,
|
|
183
|
+
getAutoTraceCapturePolicy,
|
|
184
|
+
runWithAutoTraceContext,
|
|
185
|
+
runWithAutoTraceRootContext
|
|
186
|
+
});
|
|
187
|
+
//# sourceMappingURL=autoTrace.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/autoTrace.ts","../src/asyncStorage.ts"],"sourcesContent":["import {\n type AsyncLocalStorageLike,\n createAsyncLocalStorage,\n} from \"./asyncStorage.js\"\n\nexport interface AutoTraceFunctionDefinition {\n id: string\n name: string\n file: string\n line: number\n column: number\n wrapper?: boolean\n}\n\nexport interface AutoTraceContext {\n invoke<T>(\n definition: AutoTraceFunctionDefinition,\n inputs: unknown[],\n fn: () => T,\n depth: number,\n ): T\n}\n\ninterface AutoTraceScope {\n context: AutoTraceContext\n depth: number\n}\n\ninterface AutoTraceState {\n storage: AsyncLocalStorageLike<AutoTraceScope> | null\n browserScope: AutoTraceScope | undefined\n capturePolicies: WeakMap<object, Map<string, ReadonlySet<string>>>\n activeRoots: number\n}\n\ninterface AutoTraceGlobal {\n __bitfabAutoTraceStateV3?: AutoTraceState\n}\n\ninterface AutoTraceAsyncGenerator {\n next(value?: unknown): Promise<IteratorResult<unknown, unknown>>\n return(value?: unknown): Promise<IteratorResult<unknown, unknown>>\n throw(error?: unknown): Promise<IteratorResult<unknown, unknown>>\n [Symbol.asyncIterator](): AutoTraceAsyncGenerator\n}\n\nconst autoTraceGlobal = globalThis as unknown as AutoTraceGlobal\nconst autoTraceState: AutoTraceState =\n autoTraceGlobal.__bitfabAutoTraceStateV3 ?? {\n storage: null,\n browserScope: undefined,\n capturePolicies: new WeakMap(),\n activeRoots: 0,\n }\nautoTraceGlobal.__bitfabAutoTraceStateV3 = autoTraceState\n\nfunction initializeAutoTraceStorage(): void {\n autoTraceState.storage ??= createAsyncLocalStorage<AutoTraceScope>()\n}\n\nexport function runWithAutoTraceContext<T>(\n context: AutoTraceContext,\n fn: () => T,\n depth = 0,\n): T {\n initializeAutoTraceStorage()\n const scope = { context, depth }\n if (autoTraceState.storage) {\n return autoTraceState.storage.run(scope, fn)\n }\n\n const previous = autoTraceState.browserScope\n autoTraceState.browserScope = scope\n try {\n return fn()\n } finally {\n autoTraceState.browserScope = previous\n }\n}\n\nexport function runWithAutoTraceRootContext<T>(\n context: AutoTraceContext,\n fn: () => T,\n): T {\n autoTraceState.activeRoots += 1\n let result: T\n try {\n result = runWithAutoTraceContext(context, fn)\n } catch (error) {\n autoTraceState.activeRoots -= 1\n throw error\n }\n\n if (isAutoTraceAsyncGenerator(result)) {\n autoTraceState.activeRoots -= 1\n return wrapAutoTraceAsyncGenerator(context, result) as T\n }\n\n if (result instanceof Promise) {\n return result.finally(() => {\n autoTraceState.activeRoots -= 1\n }) as T\n }\n\n autoTraceState.activeRoots -= 1\n return result\n}\n\nfunction isAutoTraceAsyncGenerator(\n value: unknown,\n): value is AutoTraceAsyncGenerator {\n if (value === null || typeof value !== \"object\") {\n return false\n }\n const candidate = value as Record<PropertyKey, unknown>\n return (\n typeof candidate.next === \"function\" &&\n typeof candidate.return === \"function\" &&\n typeof candidate.throw === \"function\" &&\n typeof candidate[Symbol.asyncIterator] === \"function\"\n )\n}\n\nfunction wrapAutoTraceAsyncGenerator(\n context: AutoTraceContext,\n source: AutoTraceAsyncGenerator,\n): AutoTraceAsyncGenerator {\n const step = (\n method: \"next\" | \"return\" | \"throw\",\n value?: unknown,\n ): Promise<IteratorResult<unknown, unknown>> =>\n runWithAutoTraceRootContext(context, () => source[method](value))\n const wrapped: AutoTraceAsyncGenerator = {\n next: (value) => step(\"next\", value),\n return: (value) => step(\"return\", value),\n throw: (error) => step(\"throw\", error),\n [Symbol.asyncIterator]: () => wrapped,\n }\n return wrapped\n}\n\nexport function __bitfabAutoSpan<T>(\n definition: AutoTraceFunctionDefinition,\n inputs: unknown[],\n fn: () => T,\n): T {\n const scope = currentAutoTraceScope()\n if (!scope) {\n return fn()\n }\n return scope.context.invoke(definition, inputs, fn, scope.depth)\n}\n\n/**\n * Preserve a function's original call arguments for transform cases where\n * parameter bindings discard them, such as destructured arrow parameters.\n *\n * This helper is internal transform/runtime protocol. The proxy preserves the\n * target's callability, arity, async identity, and non-constructibility while\n * only allocating a trace closure beneath an active automatic trace root.\n *\n * @experimental The automatic tracing protocol may change.\n */\nexport function __bitfabAutoWrap<T extends (...args: never[]) => unknown>(\n definition: AutoTraceFunctionDefinition,\n fn: T,\n): T {\n if (fn.name === \"\") {\n const nameParts = definition.name.split(\".\")\n const inferredName = nameParts[nameParts.length - 1]\n if (inferredName !== undefined) {\n Object.defineProperty(fn, \"name\", {\n configurable: true,\n value: inferredName,\n })\n }\n }\n\n const target = fn as unknown as (...args: unknown[]) => unknown\n return new Proxy(target, {\n apply(callTarget, thisArg, args) {\n if (!__bitfabAutoTraceActive()) {\n return Reflect.apply(callTarget, thisArg, args)\n }\n return __bitfabAutoSpan(definition, args, () =>\n Reflect.apply(callTarget, thisArg, args),\n )\n },\n }) as unknown as T\n}\n\n/**\n * Return whether the current call is inside an automatic trace root.\n *\n * Build transforms use this before allocating function metadata, captured\n * inputs, or an invocation closure. It is internal transform/runtime protocol,\n * not a supported application API.\n *\n * @experimental The automatic tracing protocol may change.\n */\nexport function __bitfabAutoTraceActive(): boolean {\n if (autoTraceState.activeRoots === 0) {\n return false\n }\n return currentAutoTraceScope() !== undefined\n}\n\nfunction currentAutoTraceScope(): AutoTraceScope | undefined {\n initializeAutoTraceStorage()\n return autoTraceState.storage?.getStore() ?? autoTraceState.browserScope\n}\n\nexport function __setBitfabAutoTraceCapturePolicy(\n client: object,\n traceFunctionKey: string,\n functionIds: Iterable<string>,\n): void {\n const policies = autoTraceState.capturePolicies.get(client) ?? new Map()\n policies.set(traceFunctionKey, new Set(functionIds))\n autoTraceState.capturePolicies.set(client, policies)\n}\n\nexport function getAutoTraceCapturePolicy(\n client: object,\n traceFunctionKey: string,\n): ReadonlySet<string> {\n return (\n autoTraceState.capturePolicies.get(client)?.get(traceFunctionKey) ??\n new Set()\n )\n}\n","/**\n * Shared AsyncLocalStorage loader.\n *\n * Provides two ways to initialize AsyncLocalStorage:\n *\n * 1. **Synchronous registration** (preferred for Node.js):\n * `asyncStorageNode.ts` calls `registerAsyncLocalStorageClass()` at module\n * evaluation time, so the class is available immediately - no async gap.\n * The `node.ts` entry point imports it before anything else.\n *\n * 2. **Async dynamic import** (fallback for the default entry point):\n * Loads `node:async_hooks` via a bundler-safe dynamic import. This is used\n * by the default `index.ts` entry point so the SDK works in browsers\n * (where the import silently fails) and in Node.js when imported via the\n * default entry point.\n *\n * ## Why the dynamic import looks like this\n *\n * We need to handle three environments:\n *\n * 1. **Pure Node.js** - `import(\"node:async_hooks\")` works natively.\n * 2. **Webpack/Turbopack (Next.js server)** - The bundler processes\n * `import()` calls at build time. The `webpackIgnore` magic comment tells\n * webpack (and turbopack) to emit a native `import()` call instead of\n * trying to resolve it, so Node.js handles it at runtime.\n * 3. **Browsers / Edge** - The `process.versions?.node` guard prevents\n * execution entirely. If it somehow runs, `.catch(() => {})` swallows\n * the failure.\n */\n\nexport interface AsyncLocalStorageLike<T> {\n getStore(): T | undefined\n run<R>(store: T, fn: () => R): R\n}\n\nlet AsyncLocalStorageClass: (new () => AsyncLocalStorageLike<unknown>) | null =\n null\nlet initDone = false\n\n/**\n * Register the AsyncLocalStorage class synchronously.\n *\n * Called by `asyncStorageNode.ts` at module evaluation time so the class\n * is available before any span is created - no async gap, no race condition.\n *\n * Safe to call multiple times; subsequent calls are no-ops.\n */\nexport function registerAsyncLocalStorageClass(\n cls: new () => AsyncLocalStorageLike<unknown>,\n): void {\n if (!AsyncLocalStorageClass) {\n AsyncLocalStorageClass = cls\n }\n initDone = true\n}\n\n/**\n * Assert that AsyncLocalStorage was registered successfully.\n *\n * Called by `node.ts` after importing `asyncStorageNode.ts` to catch\n * import-order bugs at startup rather than silently degrading to the\n * browser fallback (flat spans with no nesting).\n *\n * This should ONLY be called from the Node.js entry point where we\n * know `node:async_hooks` must be available.\n */\nexport function assertAsyncStorageRegistered(): void {\n if (!AsyncLocalStorageClass) {\n console.warn(\n \"Bitfab: AsyncLocalStorage not available - nested span context will not propagate.\",\n )\n }\n}\n\nexport const asyncStorageReady: Promise<void> = (\n typeof process !== \"undefined\" && process.versions?.node\n ? // The join trick hides \"node:async_hooks\" from static analysis so\n // bundlers that ban Node.js built-ins don't fail at build time.\n // webpackIgnore tells webpack/turbopack to emit a native import()\n // so Node.js can resolve the module at runtime.\n import(\n /* webpackIgnore: true */\n [\"node\", \"async_hooks\"].join(\":\")\n )\n .then(\n (mod: {\n AsyncLocalStorage: new () => AsyncLocalStorageLike<unknown>\n }) => {\n registerAsyncLocalStorageClass(mod.AsyncLocalStorage)\n },\n )\n .catch(() => {})\n : Promise.resolve()\n).then(() => {\n initDone = true\n})\n\nexport function isAsyncStorageInitDone(): boolean {\n return initDone\n}\n\nexport function createAsyncLocalStorage<T>(): AsyncLocalStorageLike<T> | null {\n return AsyncLocalStorageClass\n ? (new AsyncLocalStorageClass() as AsyncLocalStorageLike<T>)\n : null\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACmCA,IAAI,yBACF;AACF,IAAI,WAAW;AAUR,SAAS,+BACd,KACM;AACN,MAAI,CAAC,wBAAwB;AAC3B,6BAAyB;AAAA,EAC3B;AACA,aAAW;AACb;AAoBO,IAAM,qBACX,OAAO,YAAY,eAAe,QAAQ,UAAU;AAAA;AAAA;AAAA;AAAA;AAAA,EAKhD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I;AAAA,MAChD;AACA,aAAO;AAAA,QAAiB;AAAA,QAAY;AAAA,QAAM,MACxC,QAAQ,MAAM,YAAY,SAAS,IAAI;AAAA,MACzC;AAAA,IACF;AAAA,EACF,CAAC;AACH;AAWO,SAAS,0BAAmC;AACjD,MAAI,eAAe,gBAAgB,GAAG;AACpC,WAAO;AAAA,EACT;AACA,SAAO,sBAAsB,MAAM;AACrC;AAEA,SAAS,wBAAoD;AAC3D,6BAA2B;AAC3B,SAAO,eAAe,SAAS,SAAS,KAAK,eAAe;AAC9D;AAEO,SAAS,kCACd,QACA,kBACA,aACM;AACN,QAAM,WAAW,eAAe,gBAAgB,IAAI,MAAM,KAAK,oBAAI,IAAI;AACvE,WAAS,IAAI,kBAAkB,IAAI,IAAI,WAAW,CAAC;AACnD,iBAAe,gBAAgB,IAAI,QAAQ,QAAQ;AACrD;AAEO,SAAS,0BACd,QACA,kBACqB;AACrB,SACE,eAAe,gBAAgB,IAAI,MAAM,GAAG,IAAI,gBAAgB,KAChE,oBAAI,IAAI;AAEZ;","names":[]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
interface AutoTraceFunctionDefinition {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
file: string;
|
|
5
|
+
line: number;
|
|
6
|
+
column: number;
|
|
7
|
+
wrapper?: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface AutoTraceContext {
|
|
10
|
+
invoke<T>(definition: AutoTraceFunctionDefinition, inputs: unknown[], fn: () => T, depth: number): T;
|
|
11
|
+
}
|
|
12
|
+
declare function runWithAutoTraceContext<T>(context: AutoTraceContext, fn: () => T, depth?: number): T;
|
|
13
|
+
declare function runWithAutoTraceRootContext<T>(context: AutoTraceContext, fn: () => T): T;
|
|
14
|
+
declare function __bitfabAutoSpan<T>(definition: AutoTraceFunctionDefinition, inputs: unknown[], fn: () => T): T;
|
|
15
|
+
/**
|
|
16
|
+
* Preserve a function's original call arguments for transform cases where
|
|
17
|
+
* parameter bindings discard them, such as destructured arrow parameters.
|
|
18
|
+
*
|
|
19
|
+
* This helper is internal transform/runtime protocol. The proxy preserves the
|
|
20
|
+
* target's callability, arity, async identity, and non-constructibility while
|
|
21
|
+
* only allocating a trace closure beneath an active automatic trace root.
|
|
22
|
+
*
|
|
23
|
+
* @experimental The automatic tracing protocol may change.
|
|
24
|
+
*/
|
|
25
|
+
declare function __bitfabAutoWrap<T extends (...args: never[]) => unknown>(definition: AutoTraceFunctionDefinition, fn: T): T;
|
|
26
|
+
/**
|
|
27
|
+
* Return whether the current call is inside an automatic trace root.
|
|
28
|
+
*
|
|
29
|
+
* Build transforms use this before allocating function metadata, captured
|
|
30
|
+
* inputs, or an invocation closure. It is internal transform/runtime protocol,
|
|
31
|
+
* not a supported application API.
|
|
32
|
+
*
|
|
33
|
+
* @experimental The automatic tracing protocol may change.
|
|
34
|
+
*/
|
|
35
|
+
declare function __bitfabAutoTraceActive(): boolean;
|
|
36
|
+
declare function __setBitfabAutoTraceCapturePolicy(client: object, traceFunctionKey: string, functionIds: Iterable<string>): void;
|
|
37
|
+
declare function getAutoTraceCapturePolicy(client: object, traceFunctionKey: string): ReadonlySet<string>;
|
|
38
|
+
|
|
39
|
+
export { type AutoTraceContext, type AutoTraceFunctionDefinition, __bitfabAutoSpan, __bitfabAutoTraceActive, __bitfabAutoWrap, __setBitfabAutoTraceCapturePolicy, getAutoTraceCapturePolicy, runWithAutoTraceContext, runWithAutoTraceRootContext };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
interface AutoTraceFunctionDefinition {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
file: string;
|
|
5
|
+
line: number;
|
|
6
|
+
column: number;
|
|
7
|
+
wrapper?: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface AutoTraceContext {
|
|
10
|
+
invoke<T>(definition: AutoTraceFunctionDefinition, inputs: unknown[], fn: () => T, depth: number): T;
|
|
11
|
+
}
|
|
12
|
+
declare function runWithAutoTraceContext<T>(context: AutoTraceContext, fn: () => T, depth?: number): T;
|
|
13
|
+
declare function runWithAutoTraceRootContext<T>(context: AutoTraceContext, fn: () => T): T;
|
|
14
|
+
declare function __bitfabAutoSpan<T>(definition: AutoTraceFunctionDefinition, inputs: unknown[], fn: () => T): T;
|
|
15
|
+
/**
|
|
16
|
+
* Preserve a function's original call arguments for transform cases where
|
|
17
|
+
* parameter bindings discard them, such as destructured arrow parameters.
|
|
18
|
+
*
|
|
19
|
+
* This helper is internal transform/runtime protocol. The proxy preserves the
|
|
20
|
+
* target's callability, arity, async identity, and non-constructibility while
|
|
21
|
+
* only allocating a trace closure beneath an active automatic trace root.
|
|
22
|
+
*
|
|
23
|
+
* @experimental The automatic tracing protocol may change.
|
|
24
|
+
*/
|
|
25
|
+
declare function __bitfabAutoWrap<T extends (...args: never[]) => unknown>(definition: AutoTraceFunctionDefinition, fn: T): T;
|
|
26
|
+
/**
|
|
27
|
+
* Return whether the current call is inside an automatic trace root.
|
|
28
|
+
*
|
|
29
|
+
* Build transforms use this before allocating function metadata, captured
|
|
30
|
+
* inputs, or an invocation closure. It is internal transform/runtime protocol,
|
|
31
|
+
* not a supported application API.
|
|
32
|
+
*
|
|
33
|
+
* @experimental The automatic tracing protocol may change.
|
|
34
|
+
*/
|
|
35
|
+
declare function __bitfabAutoTraceActive(): boolean;
|
|
36
|
+
declare function __setBitfabAutoTraceCapturePolicy(client: object, traceFunctionKey: string, functionIds: Iterable<string>): void;
|
|
37
|
+
declare function getAutoTraceCapturePolicy(client: object, traceFunctionKey: string): ReadonlySet<string>;
|
|
38
|
+
|
|
39
|
+
export { type AutoTraceContext, type AutoTraceFunctionDefinition, __bitfabAutoSpan, __bitfabAutoTraceActive, __bitfabAutoWrap, __setBitfabAutoTraceCapturePolicy, getAutoTraceCapturePolicy, runWithAutoTraceContext, runWithAutoTraceRootContext };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__bitfabAutoSpan,
|
|
3
|
+
__bitfabAutoTraceActive,
|
|
4
|
+
__bitfabAutoWrap,
|
|
5
|
+
__setBitfabAutoTraceCapturePolicy,
|
|
6
|
+
getAutoTraceCapturePolicy,
|
|
7
|
+
runWithAutoTraceContext,
|
|
8
|
+
runWithAutoTraceRootContext
|
|
9
|
+
} from "./chunk-GFBQ2AMO.js";
|
|
10
|
+
import "./chunk-H6LZRFMN.js";
|
|
11
|
+
export {
|
|
12
|
+
__bitfabAutoSpan,
|
|
13
|
+
__bitfabAutoTraceActive,
|
|
14
|
+
__bitfabAutoWrap,
|
|
15
|
+
__setBitfabAutoTraceCapturePolicy,
|
|
16
|
+
getAutoTraceCapturePolicy,
|
|
17
|
+
runWithAutoTraceContext,
|
|
18
|
+
runWithAutoTraceRootContext
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=autoTrace.js.map
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createAsyncLocalStorage
|
|
3
|
+
} from "./chunk-H6LZRFMN.js";
|
|
4
|
+
|
|
5
|
+
// src/autoTrace.ts
|
|
6
|
+
var autoTraceGlobal = globalThis;
|
|
7
|
+
var autoTraceState = autoTraceGlobal.__bitfabAutoTraceStateV3 ?? {
|
|
8
|
+
storage: null,
|
|
9
|
+
browserScope: void 0,
|
|
10
|
+
capturePolicies: /* @__PURE__ */ new WeakMap(),
|
|
11
|
+
activeRoots: 0
|
|
12
|
+
};
|
|
13
|
+
autoTraceGlobal.__bitfabAutoTraceStateV3 = autoTraceState;
|
|
14
|
+
function initializeAutoTraceStorage() {
|
|
15
|
+
autoTraceState.storage ?? (autoTraceState.storage = createAsyncLocalStorage());
|
|
16
|
+
}
|
|
17
|
+
function runWithAutoTraceContext(context, fn, depth = 0) {
|
|
18
|
+
initializeAutoTraceStorage();
|
|
19
|
+
const scope = { context, depth };
|
|
20
|
+
if (autoTraceState.storage) {
|
|
21
|
+
return autoTraceState.storage.run(scope, fn);
|
|
22
|
+
}
|
|
23
|
+
const previous = autoTraceState.browserScope;
|
|
24
|
+
autoTraceState.browserScope = scope;
|
|
25
|
+
try {
|
|
26
|
+
return fn();
|
|
27
|
+
} finally {
|
|
28
|
+
autoTraceState.browserScope = previous;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function runWithAutoTraceRootContext(context, fn) {
|
|
32
|
+
autoTraceState.activeRoots += 1;
|
|
33
|
+
let result;
|
|
34
|
+
try {
|
|
35
|
+
result = runWithAutoTraceContext(context, fn);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
autoTraceState.activeRoots -= 1;
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
if (isAutoTraceAsyncGenerator(result)) {
|
|
41
|
+
autoTraceState.activeRoots -= 1;
|
|
42
|
+
return wrapAutoTraceAsyncGenerator(context, result);
|
|
43
|
+
}
|
|
44
|
+
if (result instanceof Promise) {
|
|
45
|
+
return result.finally(() => {
|
|
46
|
+
autoTraceState.activeRoots -= 1;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
autoTraceState.activeRoots -= 1;
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
function isAutoTraceAsyncGenerator(value) {
|
|
53
|
+
if (value === null || typeof value !== "object") {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
const candidate = value;
|
|
57
|
+
return typeof candidate.next === "function" && typeof candidate.return === "function" && typeof candidate.throw === "function" && typeof candidate[Symbol.asyncIterator] === "function";
|
|
58
|
+
}
|
|
59
|
+
function wrapAutoTraceAsyncGenerator(context, source) {
|
|
60
|
+
const step = (method, value) => runWithAutoTraceRootContext(context, () => source[method](value));
|
|
61
|
+
const wrapped = {
|
|
62
|
+
next: (value) => step("next", value),
|
|
63
|
+
return: (value) => step("return", value),
|
|
64
|
+
throw: (error) => step("throw", error),
|
|
65
|
+
[Symbol.asyncIterator]: () => wrapped
|
|
66
|
+
};
|
|
67
|
+
return wrapped;
|
|
68
|
+
}
|
|
69
|
+
function __bitfabAutoSpan(definition, inputs, fn) {
|
|
70
|
+
const scope = currentAutoTraceScope();
|
|
71
|
+
if (!scope) {
|
|
72
|
+
return fn();
|
|
73
|
+
}
|
|
74
|
+
return scope.context.invoke(definition, inputs, fn, scope.depth);
|
|
75
|
+
}
|
|
76
|
+
function __bitfabAutoWrap(definition, fn) {
|
|
77
|
+
if (fn.name === "") {
|
|
78
|
+
const nameParts = definition.name.split(".");
|
|
79
|
+
const inferredName = nameParts[nameParts.length - 1];
|
|
80
|
+
if (inferredName !== void 0) {
|
|
81
|
+
Object.defineProperty(fn, "name", {
|
|
82
|
+
configurable: true,
|
|
83
|
+
value: inferredName
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const target = fn;
|
|
88
|
+
return new Proxy(target, {
|
|
89
|
+
apply(callTarget, thisArg, args) {
|
|
90
|
+
if (!__bitfabAutoTraceActive()) {
|
|
91
|
+
return Reflect.apply(callTarget, thisArg, args);
|
|
92
|
+
}
|
|
93
|
+
return __bitfabAutoSpan(
|
|
94
|
+
definition,
|
|
95
|
+
args,
|
|
96
|
+
() => Reflect.apply(callTarget, thisArg, args)
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function __bitfabAutoTraceActive() {
|
|
102
|
+
if (autoTraceState.activeRoots === 0) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
return currentAutoTraceScope() !== void 0;
|
|
106
|
+
}
|
|
107
|
+
function currentAutoTraceScope() {
|
|
108
|
+
initializeAutoTraceStorage();
|
|
109
|
+
return autoTraceState.storage?.getStore() ?? autoTraceState.browserScope;
|
|
110
|
+
}
|
|
111
|
+
function __setBitfabAutoTraceCapturePolicy(client, traceFunctionKey, functionIds) {
|
|
112
|
+
const policies = autoTraceState.capturePolicies.get(client) ?? /* @__PURE__ */ new Map();
|
|
113
|
+
policies.set(traceFunctionKey, new Set(functionIds));
|
|
114
|
+
autoTraceState.capturePolicies.set(client, policies);
|
|
115
|
+
}
|
|
116
|
+
function getAutoTraceCapturePolicy(client, traceFunctionKey) {
|
|
117
|
+
return autoTraceState.capturePolicies.get(client)?.get(traceFunctionKey) ?? /* @__PURE__ */ new Set();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export {
|
|
121
|
+
runWithAutoTraceContext,
|
|
122
|
+
runWithAutoTraceRootContext,
|
|
123
|
+
__bitfabAutoSpan,
|
|
124
|
+
__bitfabAutoWrap,
|
|
125
|
+
__bitfabAutoTraceActive,
|
|
126
|
+
__setBitfabAutoTraceCapturePolicy,
|
|
127
|
+
getAutoTraceCapturePolicy
|
|
128
|
+
};
|
|
129
|
+
//# sourceMappingURL=chunk-GFBQ2AMO.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/autoTrace.ts"],"sourcesContent":["import {\n type AsyncLocalStorageLike,\n createAsyncLocalStorage,\n} from \"./asyncStorage.js\"\n\nexport interface AutoTraceFunctionDefinition {\n id: string\n name: string\n file: string\n line: number\n column: number\n wrapper?: boolean\n}\n\nexport interface AutoTraceContext {\n invoke<T>(\n definition: AutoTraceFunctionDefinition,\n inputs: unknown[],\n fn: () => T,\n depth: number,\n ): T\n}\n\ninterface AutoTraceScope {\n context: AutoTraceContext\n depth: number\n}\n\ninterface AutoTraceState {\n storage: AsyncLocalStorageLike<AutoTraceScope> | null\n browserScope: AutoTraceScope | undefined\n capturePolicies: WeakMap<object, Map<string, ReadonlySet<string>>>\n activeRoots: number\n}\n\ninterface AutoTraceGlobal {\n __bitfabAutoTraceStateV3?: AutoTraceState\n}\n\ninterface AutoTraceAsyncGenerator {\n next(value?: unknown): Promise<IteratorResult<unknown, unknown>>\n return(value?: unknown): Promise<IteratorResult<unknown, unknown>>\n throw(error?: unknown): Promise<IteratorResult<unknown, unknown>>\n [Symbol.asyncIterator](): AutoTraceAsyncGenerator\n}\n\nconst autoTraceGlobal = globalThis as unknown as AutoTraceGlobal\nconst autoTraceState: AutoTraceState =\n autoTraceGlobal.__bitfabAutoTraceStateV3 ?? {\n storage: null,\n browserScope: undefined,\n capturePolicies: new WeakMap(),\n activeRoots: 0,\n }\nautoTraceGlobal.__bitfabAutoTraceStateV3 = autoTraceState\n\nfunction initializeAutoTraceStorage(): void {\n autoTraceState.storage ??= createAsyncLocalStorage<AutoTraceScope>()\n}\n\nexport function runWithAutoTraceContext<T>(\n context: AutoTraceContext,\n fn: () => T,\n depth = 0,\n): T {\n initializeAutoTraceStorage()\n const scope = { context, depth }\n if (autoTraceState.storage) {\n return autoTraceState.storage.run(scope, fn)\n }\n\n const previous = autoTraceState.browserScope\n autoTraceState.browserScope = scope\n try {\n return fn()\n } finally {\n autoTraceState.browserScope = previous\n }\n}\n\nexport function runWithAutoTraceRootContext<T>(\n context: AutoTraceContext,\n fn: () => T,\n): T {\n autoTraceState.activeRoots += 1\n let result: T\n try {\n result = runWithAutoTraceContext(context, fn)\n } catch (error) {\n autoTraceState.activeRoots -= 1\n throw error\n }\n\n if (isAutoTraceAsyncGenerator(result)) {\n autoTraceState.activeRoots -= 1\n return wrapAutoTraceAsyncGenerator(context, result) as T\n }\n\n if (result instanceof Promise) {\n return result.finally(() => {\n autoTraceState.activeRoots -= 1\n }) as T\n }\n\n autoTraceState.activeRoots -= 1\n return result\n}\n\nfunction isAutoTraceAsyncGenerator(\n value: unknown,\n): value is AutoTraceAsyncGenerator {\n if (value === null || typeof value !== \"object\") {\n return false\n }\n const candidate = value as Record<PropertyKey, unknown>\n return (\n typeof candidate.next === \"function\" &&\n typeof candidate.return === \"function\" &&\n typeof candidate.throw === \"function\" &&\n typeof candidate[Symbol.asyncIterator] === \"function\"\n )\n}\n\nfunction wrapAutoTraceAsyncGenerator(\n context: AutoTraceContext,\n source: AutoTraceAsyncGenerator,\n): AutoTraceAsyncGenerator {\n const step = (\n method: \"next\" | \"return\" | \"throw\",\n value?: unknown,\n ): Promise<IteratorResult<unknown, unknown>> =>\n runWithAutoTraceRootContext(context, () => source[method](value))\n const wrapped: AutoTraceAsyncGenerator = {\n next: (value) => step(\"next\", value),\n return: (value) => step(\"return\", value),\n throw: (error) => step(\"throw\", error),\n [Symbol.asyncIterator]: () => wrapped,\n }\n return wrapped\n}\n\nexport function __bitfabAutoSpan<T>(\n definition: AutoTraceFunctionDefinition,\n inputs: unknown[],\n fn: () => T,\n): T {\n const scope = currentAutoTraceScope()\n if (!scope) {\n return fn()\n }\n return scope.context.invoke(definition, inputs, fn, scope.depth)\n}\n\n/**\n * Preserve a function's original call arguments for transform cases where\n * parameter bindings discard them, such as destructured arrow parameters.\n *\n * This helper is internal transform/runtime protocol. The proxy preserves the\n * target's callability, arity, async identity, and non-constructibility while\n * only allocating a trace closure beneath an active automatic trace root.\n *\n * @experimental The automatic tracing protocol may change.\n */\nexport function __bitfabAutoWrap<T extends (...args: never[]) => unknown>(\n definition: AutoTraceFunctionDefinition,\n fn: T,\n): T {\n if (fn.name === \"\") {\n const nameParts = definition.name.split(\".\")\n const inferredName = nameParts[nameParts.length - 1]\n if (inferredName !== undefined) {\n Object.defineProperty(fn, \"name\", {\n configurable: true,\n value: inferredName,\n })\n }\n }\n\n const target = fn as unknown as (...args: unknown[]) => unknown\n return new Proxy(target, {\n apply(callTarget, thisArg, args) {\n if (!__bitfabAutoTraceActive()) {\n return Reflect.apply(callTarget, thisArg, args)\n }\n return __bitfabAutoSpan(definition, args, () =>\n Reflect.apply(callTarget, thisArg, args),\n )\n },\n }) as unknown as T\n}\n\n/**\n * Return whether the current call is inside an automatic trace root.\n *\n * Build transforms use this before allocating function metadata, captured\n * inputs, or an invocation closure. It is internal transform/runtime protocol,\n * not a supported application API.\n *\n * @experimental The automatic tracing protocol may change.\n */\nexport function __bitfabAutoTraceActive(): boolean {\n if (autoTraceState.activeRoots === 0) {\n return false\n }\n return currentAutoTraceScope() !== undefined\n}\n\nfunction currentAutoTraceScope(): AutoTraceScope | undefined {\n initializeAutoTraceStorage()\n return autoTraceState.storage?.getStore() ?? autoTraceState.browserScope\n}\n\nexport function __setBitfabAutoTraceCapturePolicy(\n client: object,\n traceFunctionKey: string,\n functionIds: Iterable<string>,\n): void {\n const policies = autoTraceState.capturePolicies.get(client) ?? new Map()\n policies.set(traceFunctionKey, new Set(functionIds))\n autoTraceState.capturePolicies.set(client, policies)\n}\n\nexport function getAutoTraceCapturePolicy(\n client: object,\n traceFunctionKey: string,\n): ReadonlySet<string> {\n return (\n autoTraceState.capturePolicies.get(client)?.get(traceFunctionKey) ??\n new Set()\n )\n}\n"],"mappings":";;;;;AA8CA,IAAM,kBAAkB;AACxB,IAAM,iBACJ,gBAAgB,4BAA4B;AAAA,EAC1C,SAAS;AAAA,EACT,cAAc;AAAA,EACd,iBAAiB,oBAAI,QAAQ;AAAA,EAC7B,aAAa;AACf;AACF,gBAAgB,2BAA2B;AAE3C,SAAS,6BAAmC;AAC1C,iBAAe,YAAf,eAAe,UAAY,wBAAwC;AACrE;AAEO,SAAS,wBACd,SACA,IACA,QAAQ,GACL;AACH,6BAA2B;AAC3B,QAAM,QAAQ,EAAE,SAAS,MAAM;AAC/B,MAAI,eAAe,SAAS;AAC1B,WAAO,eAAe,QAAQ,IAAI,OAAO,EAAE;AAAA,EAC7C;AAEA,QAAM,WAAW,eAAe;AAChC,iBAAe,eAAe;AAC9B,MAAI;AACF,WAAO,GAAG;AAAA,EACZ,UAAE;AACA,mBAAe,eAAe;AAAA,EAChC;AACF;AAEO,SAAS,4BACd,SACA,IACG;AACH,iBAAe,eAAe;AAC9B,MAAI;AACJ,MAAI;AACF,aAAS,wBAAwB,SAAS,EAAE;AAAA,EAC9C,SAAS,OAAO;AACd,mBAAe,eAAe;AAC9B,UAAM;AAAA,EACR;AAEA,MAAI,0BAA0B,MAAM,GAAG;AACrC,mBAAe,eAAe;AAC9B,WAAO,4BAA4B,SAAS,MAAM;AAAA,EACpD;AAEA,MAAI,kBAAkB,SAAS;AAC7B,WAAO,OAAO,QAAQ,MAAM;AAC1B,qBAAe,eAAe;AAAA,IAChC,CAAC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Y,SAAS,IAAI;AAAA,MAChD;AACA,aAAO;AAAA,QAAiB;AAAA,QAAY;AAAA,QAAM,MACxC,QAAQ,MAAM,YAAY,SAAS,IAAI;AAAA,MACzC;AAAA,IACF;AAAA,EACF,CAAC;AACH;AAWO,SAAS,0BAAmC;AACjD,MAAI,eAAe,gBAAgB,GAAG;AACpC,WAAO;AAAA,EACT;AACA,SAAO,sBAAsB,MAAM;AACrC;AAEA,SAAS,wBAAoD;AAC3D,6BAA2B;AAC3B,SAAO,eAAe,SAAS,SAAS,KAAK,eAAe;AAC9D;AAEO,SAAS,kCACd,QACA,kBACA,aACM;AACN,QAAM,WAAW,eAAe,gBAAgB,IAAI,MAAM,KAAK,oBAAI,IAAI;AACvE,WAAS,IAAI,kBAAkB,IAAI,IAAI,WAAW,CAAC;AACnD,iBAAe,gBAAgB,IAAI,QAAQ,QAAQ;AACrD;AAEO,SAAS,0BACd,QACA,kBACqB;AACrB,SACE,eAAe,gBAAgB,IAAI,MAAM,GAAG,IAAI,gBAAgB,KAChE,oBAAI,IAAI;AAEZ;","names":[]}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
var __typeError = (msg) => {
|
|
2
|
+
throw TypeError(msg);
|
|
3
|
+
};
|
|
4
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
5
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
6
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
7
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
8
|
+
|
|
9
|
+
// src/asyncStorage.ts
|
|
10
|
+
var AsyncLocalStorageClass = null;
|
|
11
|
+
var initDone = false;
|
|
12
|
+
function registerAsyncLocalStorageClass(cls) {
|
|
13
|
+
if (!AsyncLocalStorageClass) {
|
|
14
|
+
AsyncLocalStorageClass = cls;
|
|
15
|
+
}
|
|
16
|
+
initDone = true;
|
|
17
|
+
}
|
|
18
|
+
function assertAsyncStorageRegistered() {
|
|
19
|
+
if (!AsyncLocalStorageClass) {
|
|
20
|
+
console.warn(
|
|
21
|
+
"Bitfab: AsyncLocalStorage not available - nested span context will not propagate."
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
var asyncStorageReady = (typeof process !== "undefined" && process.versions?.node ? (
|
|
26
|
+
// The join trick hides "node:async_hooks" from static analysis so
|
|
27
|
+
// bundlers that ban Node.js built-ins don't fail at build time.
|
|
28
|
+
// webpackIgnore tells webpack/turbopack to emit a native import()
|
|
29
|
+
// so Node.js can resolve the module at runtime.
|
|
30
|
+
import(
|
|
31
|
+
/* webpackIgnore: true */
|
|
32
|
+
["node", "async_hooks"].join(":")
|
|
33
|
+
).then(
|
|
34
|
+
(mod) => {
|
|
35
|
+
registerAsyncLocalStorageClass(mod.AsyncLocalStorage);
|
|
36
|
+
}
|
|
37
|
+
).catch(() => {
|
|
38
|
+
})
|
|
39
|
+
) : Promise.resolve()).then(() => {
|
|
40
|
+
initDone = true;
|
|
41
|
+
});
|
|
42
|
+
function isAsyncStorageInitDone() {
|
|
43
|
+
return initDone;
|
|
44
|
+
}
|
|
45
|
+
function createAsyncLocalStorage() {
|
|
46
|
+
return AsyncLocalStorageClass ? new AsyncLocalStorageClass() : null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export {
|
|
50
|
+
__privateGet,
|
|
51
|
+
__privateAdd,
|
|
52
|
+
__privateSet,
|
|
53
|
+
registerAsyncLocalStorageClass,
|
|
54
|
+
assertAsyncStorageRegistered,
|
|
55
|
+
asyncStorageReady,
|
|
56
|
+
isAsyncStorageInitDone,
|
|
57
|
+
createAsyncLocalStorage
|
|
58
|
+
};
|
|
59
|
+
//# sourceMappingURL=chunk-H6LZRFMN.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/asyncStorage.ts"],"sourcesContent":["/**\n * Shared AsyncLocalStorage loader.\n *\n * Provides two ways to initialize AsyncLocalStorage:\n *\n * 1. **Synchronous registration** (preferred for Node.js):\n * `asyncStorageNode.ts` calls `registerAsyncLocalStorageClass()` at module\n * evaluation time, so the class is available immediately - no async gap.\n * The `node.ts` entry point imports it before anything else.\n *\n * 2. **Async dynamic import** (fallback for the default entry point):\n * Loads `node:async_hooks` via a bundler-safe dynamic import. This is used\n * by the default `index.ts` entry point so the SDK works in browsers\n * (where the import silently fails) and in Node.js when imported via the\n * default entry point.\n *\n * ## Why the dynamic import looks like this\n *\n * We need to handle three environments:\n *\n * 1. **Pure Node.js** - `import(\"node:async_hooks\")` works natively.\n * 2. **Webpack/Turbopack (Next.js server)** - The bundler processes\n * `import()` calls at build time. The `webpackIgnore` magic comment tells\n * webpack (and turbopack) to emit a native `import()` call instead of\n * trying to resolve it, so Node.js handles it at runtime.\n * 3. **Browsers / Edge** - The `process.versions?.node` guard prevents\n * execution entirely. If it somehow runs, `.catch(() => {})` swallows\n * the failure.\n */\n\nexport interface AsyncLocalStorageLike<T> {\n getStore(): T | undefined\n run<R>(store: T, fn: () => R): R\n}\n\nlet AsyncLocalStorageClass: (new () => AsyncLocalStorageLike<unknown>) | null =\n null\nlet initDone = false\n\n/**\n * Register the AsyncLocalStorage class synchronously.\n *\n * Called by `asyncStorageNode.ts` at module evaluation time so the class\n * is available before any span is created - no async gap, no race condition.\n *\n * Safe to call multiple times; subsequent calls are no-ops.\n */\nexport function registerAsyncLocalStorageClass(\n cls: new () => AsyncLocalStorageLike<unknown>,\n): void {\n if (!AsyncLocalStorageClass) {\n AsyncLocalStorageClass = cls\n }\n initDone = true\n}\n\n/**\n * Assert that AsyncLocalStorage was registered successfully.\n *\n * Called by `node.ts` after importing `asyncStorageNode.ts` to catch\n * import-order bugs at startup rather than silently degrading to the\n * browser fallback (flat spans with no nesting).\n *\n * This should ONLY be called from the Node.js entry point where we\n * know `node:async_hooks` must be available.\n */\nexport function assertAsyncStorageRegistered(): void {\n if (!AsyncLocalStorageClass) {\n console.warn(\n \"Bitfab: AsyncLocalStorage not available - nested span context will not propagate.\",\n )\n }\n}\n\nexport const asyncStorageReady: Promise<void> = (\n typeof process !== \"undefined\" && process.versions?.node\n ? // The join trick hides \"node:async_hooks\" from static analysis so\n // bundlers that ban Node.js built-ins don't fail at build time.\n // webpackIgnore tells webpack/turbopack to emit a native import()\n // so Node.js can resolve the module at runtime.\n import(\n /* webpackIgnore: true */\n [\"node\", \"async_hooks\"].join(\":\")\n )\n .then(\n (mod: {\n AsyncLocalStorage: new () => AsyncLocalStorageLike<unknown>\n }) => {\n registerAsyncLocalStorageClass(mod.AsyncLocalStorage)\n },\n )\n .catch(() => {})\n : Promise.resolve()\n).then(() => {\n initDone = true\n})\n\nexport function isAsyncStorageInitDone(): boolean {\n return initDone\n}\n\nexport function createAsyncLocalStorage<T>(): AsyncLocalStorageLike<T> | null {\n return AsyncLocalStorageClass\n ? (new AsyncLocalStorageClass() as AsyncLocalStorageLike<T>)\n : null\n}\n"],"mappings":";;;;;;;;;AAmCA,IAAI,yBACF;AACF,IAAI,WAAW;AAUR,SAAS,+BACd,KACM;AACN,MAAI,CAAC,wBAAwB;AAC3B,6BAAyB;AAAA,EAC3B;AACA,aAAW;AACb;AAYO,SAAS,+BAAqC;AACnD,MAAI,CAAC,wBAAwB;AAC3B,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF;AACF;AAEO,IAAM,qBACX,OAAO,YAAY,eAAe,QAAQ,UAAU;AAAA;AAAA;AAAA;AAAA;AAAA,EAKhD;AAAA;AAAA,IAEE,CAAC,QAAQ,aAAa,EAAE,KAAK,GAAG;AAAA,IAE/B;AAAA,IACC,CAAC,QAEK;AACJ,qCAA+B,IAAI,iBAAiB;AAAA,IACtD;AAAA,EACF,EACC,MAAM,MAAM;AAAA,EAAC,CAAC;AAAA,IACjB,QAAQ,QAAQ,GACpB,KAAK,MAAM;AACX,aAAW;AACb,CAAC;AAEM,SAAS,yBAAkC;AAChD,SAAO;AACT;AAEO,SAAS,0BAA8D;AAC5E,SAAO,yBACF,IAAI,uBAAuB,IAC5B;AACN;","names":[]}
|