@bitfab/sdk 0.64.24 → 0.64.26

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.
Files changed (100) hide show
  1. package/dist/{chunk-EXTDH26A.js → chunk-26WDOYFS.js} +27 -6
  2. package/dist/chunk-26WDOYFS.js.map +1 -0
  3. package/dist/{chunk-E6YNESVO.js → chunk-FSVGIKQT.js} +2 -2
  4. package/dist/chunk-GSHCHCB5.js +433 -0
  5. package/dist/chunk-GSHCHCB5.js.map +1 -0
  6. package/dist/chunk-IBWW3SP4.js +45 -0
  7. package/dist/chunk-IBWW3SP4.js.map +1 -0
  8. package/dist/{chunk-3YAEAQI5.js → chunk-IHQKJWNN.js} +2 -2
  9. package/dist/{chunk-3YAEAQI5.js.map → chunk-IHQKJWNN.js.map} +1 -1
  10. package/dist/{chunk-OXFZJP4E.js → chunk-MN3E2RUU.js} +6 -8
  11. package/dist/chunk-MN3E2RUU.js.map +1 -0
  12. package/dist/{chunk-7A5E42SY.js → chunk-NK5RZ3UH.js} +2 -2
  13. package/dist/{chunk-7A5E42SY.js.map → chunk-NK5RZ3UH.js.map} +1 -1
  14. package/dist/{chunk-NOWQOHCR.js → chunk-PPNFMVOH.js} +2 -2
  15. package/dist/{chunk-3NIUOINN.js → chunk-W6I5QA62.js} +4 -4
  16. package/dist/{http-UQTRAPDD.js → http-3WBKIQI4.js} +2 -2
  17. package/dist/{http-G3U247I4.js → http-BK6UJNXI.js} +2 -2
  18. package/dist/index.cjs +23 -1
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +14 -2
  21. package/dist/index.d.ts +14 -2
  22. package/dist/index.js +3 -3
  23. package/dist/node.cjs +23 -1
  24. package/dist/node.cjs.map +1 -1
  25. package/dist/node.d.cts +1 -1
  26. package/dist/node.d.ts +1 -1
  27. package/dist/node.js +3 -3
  28. package/dist/{replay-MPMU3VBP.js → replay-OEANLAUE.js} +3 -3
  29. package/dist/{replay-SK4FOKUB.js → replay-ZTEDZUSG.js} +3 -3
  30. package/dist/replayCli.js +3 -3
  31. package/dist/seedCli.js +3 -3
  32. package/dist/transform/babel.cjs +68 -34
  33. package/dist/transform/babel.cjs.map +1 -1
  34. package/dist/transform/babel.d.cts +2 -2
  35. package/dist/transform/babel.d.ts +2 -2
  36. package/dist/transform/babel.js +2 -374
  37. package/dist/transform/babel.js.map +1 -1
  38. package/dist/transform/bun.cjs +389 -464
  39. package/dist/transform/bun.cjs.map +1 -1
  40. package/dist/transform/bun.js +3 -3
  41. package/dist/transform/esbuild.cjs +389 -464
  42. package/dist/transform/esbuild.cjs.map +1 -1
  43. package/dist/transform/esbuild.js +3 -3
  44. package/dist/transform/loaderRuntime.cjs +448 -498
  45. package/dist/transform/loaderRuntime.cjs.map +1 -1
  46. package/dist/transform/loaderRuntime.js +90 -70
  47. package/dist/transform/loaderRuntime.js.map +1 -1
  48. package/dist/transform/nest.cjs +389 -464
  49. package/dist/transform/nest.cjs.map +1 -1
  50. package/dist/transform/nest.js +3 -3
  51. package/dist/transform/next.cjs +389 -464
  52. package/dist/transform/next.cjs.map +1 -1
  53. package/dist/transform/next.js +3 -3
  54. package/dist/transform/nodeLoader.js +451 -498
  55. package/dist/transform/nodeLoader.js.map +1 -1
  56. package/dist/transform/rolldown.cjs +389 -464
  57. package/dist/transform/rolldown.cjs.map +1 -1
  58. package/dist/transform/rolldown.js +3 -3
  59. package/dist/transform/rollup.cjs +389 -464
  60. package/dist/transform/rollup.cjs.map +1 -1
  61. package/dist/transform/rollup.js +3 -3
  62. package/dist/transform/rsbuild.cjs +389 -464
  63. package/dist/transform/rsbuild.cjs.map +1 -1
  64. package/dist/transform/rsbuild.js +3 -3
  65. package/dist/transform/rspack.cjs +389 -464
  66. package/dist/transform/rspack.cjs.map +1 -1
  67. package/dist/transform/rspack.js +3 -3
  68. package/dist/transform/swc.cjs +402 -471
  69. package/dist/transform/swc.cjs.map +1 -1
  70. package/dist/transform/swc.js +19 -13
  71. package/dist/transform/swc.js.map +1 -1
  72. package/dist/transform/unplugin.cjs +389 -464
  73. package/dist/transform/unplugin.cjs.map +1 -1
  74. package/dist/transform/unplugin.js +3 -3
  75. package/dist/transform/vite.cjs +389 -464
  76. package/dist/transform/vite.cjs.map +1 -1
  77. package/dist/transform/vite.js +3 -3
  78. package/dist/transform/webpack.cjs +389 -464
  79. package/dist/transform/webpack.cjs.map +1 -1
  80. package/dist/transform/webpack.js +3 -3
  81. package/package.json +10 -21
  82. package/dist/chunk-6R33XLNY.js +0 -28
  83. package/dist/chunk-6R33XLNY.js.map +0 -1
  84. package/dist/chunk-EXTDH26A.js.map +0 -1
  85. package/dist/chunk-OXFZJP4E.js.map +0 -1
  86. package/dist/chunk-PACLPKDH.js +0 -513
  87. package/dist/chunk-PACLPKDH.js.map +0 -1
  88. package/dist/transform/typescript.cjs +0 -566
  89. package/dist/transform/typescript.cjs.map +0 -1
  90. package/dist/transform/typescript.d.cts +0 -22
  91. package/dist/transform/typescript.d.ts +0 -22
  92. package/dist/transform/typescript.js +0 -10
  93. package/dist/transform/typescript.js.map +0 -1
  94. /package/dist/{chunk-E6YNESVO.js.map → chunk-FSVGIKQT.js.map} +0 -0
  95. /package/dist/{chunk-NOWQOHCR.js.map → chunk-PPNFMVOH.js.map} +0 -0
  96. /package/dist/{chunk-3NIUOINN.js.map → chunk-W6I5QA62.js.map} +0 -0
  97. /package/dist/{http-G3U247I4.js.map → http-3WBKIQI4.js.map} +0 -0
  98. /package/dist/{http-UQTRAPDD.js.map → http-BK6UJNXI.js.map} +0 -0
  99. /package/dist/{replay-MPMU3VBP.js.map → replay-OEANLAUE.js.map} +0 -0
  100. /package/dist/{replay-SK4FOKUB.js.map → replay-ZTEDZUSG.js.map} +0 -0
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // ../bitfab-typescript-transform/src/rsbuildPlugin.ts
@@ -37,12 +27,23 @@ module.exports = __toCommonJS(rsbuildPlugin_exports);
37
27
  // ../bitfab-typescript-transform/src/unplugin.ts
38
28
  var import_unplugin = require("unplugin");
39
29
 
30
+ // ../bitfab-typescript-transform/src/transform.ts
31
+ var import_core = require("@babel/core");
32
+
40
33
  // ../bitfab-typescript-transform/src/transformOptions.ts
41
34
  var AUTO_TRACE_FUNCTION_ID_VERSION = "ts-auto-v1";
42
35
  var AUTO_TRACE_TRANSFORM_MARKER = "__bitfab_auto_trace_transformed_v1__";
43
36
  function autoTraceFunctionId(file, name) {
44
37
  return `${AUTO_TRACE_FUNCTION_ID_VERSION}:${file}#${name}`;
45
38
  }
39
+ function sourceFilename(filename) {
40
+ return filename.replace(/\\/g, "/").split(/[?#]/, 1)[0] ?? "";
41
+ }
42
+ function autoTraceFilename(filename, cwd) {
43
+ const normalized = sourceFilename(filename);
44
+ const normalizedCwd = cwd.replace(/\\/g, "/").replace(/\/$/, "");
45
+ return normalized.startsWith(`${normalizedCwd}/`) ? normalized.slice(normalizedCwd.length + 1) : normalized;
46
+ }
46
47
  function shouldTransformBitfabFile(filename, options) {
47
48
  if (!filename) {
48
49
  return false;
@@ -50,7 +51,7 @@ function shouldTransformBitfabFile(filename, options) {
50
51
  if (options.include && !options.include(filename)) {
51
52
  return false;
52
53
  }
53
- const normalized = filename.replace(/\\/g, "/").split(/[?#]/, 1)[0] ?? "";
54
+ const normalized = sourceFilename(filename);
54
55
  const normalizedCwd = options.cwd?.replace(/\\/g, "/").replace(/\/$/, "");
55
56
  const isAbsolute = normalized.startsWith("/") || /^[A-Za-z]:\//.test(normalized);
56
57
  if (normalizedCwd && isAbsolute && normalized !== normalizedCwd && !normalized.startsWith(`${normalizedCwd}/`)) {
@@ -59,506 +60,432 @@ function shouldTransformBitfabFile(filename, options) {
59
60
  return !normalized.includes("/node_modules/") && !normalized.endsWith(".d.ts") && /\.[cm]?[jt]sx?$/.test(normalized);
60
61
  }
61
62
 
62
- // ../bitfab-typescript-transform/src/typescriptTransformer.ts
63
- var import_typescript = __toESM(require("typescript"), 1);
64
- function hasUseClientDirective(sourceFile) {
65
- return sourceFile.statements.some(
66
- (statement) => import_typescript.default.isExpressionStatement(statement) && import_typescript.default.isStringLiteral(statement.expression) && statement.expression.text === "use client"
67
- );
68
- }
69
- function hasTransformMarker(sourceFile) {
70
- return sourceFile.statements.some(
71
- (statement) => import_typescript.default.isVariableStatement(statement) && statement.declarationList.declarations.some(
72
- (declaration) => import_typescript.default.isIdentifier(declaration.name) && declaration.name.text === AUTO_TRACE_TRANSFORM_MARKER
73
- )
74
- );
75
- }
76
- function isBitfabMemberCall(node, memberName) {
77
- return import_typescript.default.isCallExpression(node) && import_typescript.default.isPropertyAccessExpression(node.expression) && node.expression.name.text === memberName;
63
+ // ../bitfab-typescript-transform/src/babelPlugin.ts
64
+ function propertyName(node, types) {
65
+ if (types.isIdentifier(node)) {
66
+ return node.name;
67
+ }
68
+ if (types.isStringLiteral(node)) {
69
+ return node.value;
70
+ }
71
+ return null;
78
72
  }
79
- function withNodeFunctionName(node) {
80
- if (!isBitfabMemberCall(node, "withNode") || node.arguments.length > 2) {
81
- return null;
73
+ function functionName(path, types) {
74
+ const node = path.node;
75
+ if (types.isFunctionDeclaration(node) || types.isFunctionExpression(node)) {
76
+ if (node.id) {
77
+ return node.id.name;
78
+ }
82
79
  }
83
- const fn = node.arguments[node.arguments.length - 1];
84
- if (import_typescript.default.isIdentifier(fn)) {
85
- return fn.text;
80
+ if ((types.isClassMethod(node) || types.isObjectMethod(node)) && !node.computed && types.isIdentifier(node.key)) {
81
+ return node.key.name;
86
82
  }
87
- if (import_typescript.default.isPropertyAccessExpression(fn)) {
88
- return fn.name.text;
83
+ const parent = path.parentPath?.node;
84
+ if (types.isVariableDeclarator(parent)) {
85
+ return propertyName(parent.id, types) ?? "anonymous";
89
86
  }
90
- if (import_typescript.default.isFunctionExpression(fn) && fn.name) {
91
- return fn.name.text;
87
+ if (types.isObjectProperty(parent) && !parent.computed) {
88
+ return propertyName(parent.key, types) ?? "anonymous";
92
89
  }
93
- return null;
90
+ const { line, column } = sourcePosition(path);
91
+ return `anonymous-${line}-${column}`;
94
92
  }
95
- function traceDecorators(node) {
96
- if (!import_typescript.default.canHaveDecorators(node)) {
97
- return [];
93
+ function isInstrumentableFunction(node, types) {
94
+ if (node.generator || types.isClassPrivateMethod(node)) {
95
+ return false;
98
96
  }
99
- return import_typescript.default.getDecorators(node) ?? [];
97
+ return !(types.isClassMethod(node) || types.isObjectMethod(node)) || node.kind === "method";
100
98
  }
101
- function isTraceDecoratorRoot(node) {
102
- return traceDecorators(node).some(
103
- (decorator) => isBitfabMemberCall(decorator.expression, "trace")
104
- );
99
+ function sourcePosition(path) {
100
+ const exportDeclaration = path.parentPath?.isExportDeclaration() ? path.parentPath.node : void 0;
101
+ const start = (exportDeclaration ?? path.node).loc?.start;
102
+ return { line: start?.line ?? 0, column: start?.column ?? 0 };
105
103
  }
106
- function collectBitfabWrappedFunctions(sourceFile) {
107
- const inlineRoots = /* @__PURE__ */ new Set();
108
- const referencedRoots = /* @__PURE__ */ new Set();
109
- function visit(node) {
110
- if (isBitfabMemberCall(node, "withTrace") || isBitfabMemberCall(node, "withSpan")) {
111
- const fn = node.arguments[node.arguments.length - 1];
112
- if (fn && (import_typescript.default.isArrowFunction(fn) || import_typescript.default.isFunctionExpression(fn))) {
113
- inlineRoots.add(fn);
114
- } else if (fn && import_typescript.default.isIdentifier(fn)) {
115
- referencedRoots.add(fn.text);
104
+ function qualifiedFunctionName(path, types) {
105
+ const ownName = functionName(path, types);
106
+ if (ownName.startsWith("anonymous-")) {
107
+ return ownName;
108
+ }
109
+ const scopes = [];
110
+ let ancestor = path.parentPath;
111
+ while (ancestor) {
112
+ if (ancestor.isFunction()) {
113
+ if (!isInstrumentableFunction(ancestor.node, types)) {
114
+ ancestor = ancestor.parentPath;
115
+ continue;
116
+ }
117
+ const name = functionName(
118
+ ancestor,
119
+ types
120
+ );
121
+ if (!name.startsWith("anonymous-")) {
122
+ scopes.unshift(name);
123
+ }
124
+ } else if ((ancestor.isClassDeclaration() || ancestor.isClassExpression()) && ancestor.node.id) {
125
+ scopes.unshift(ancestor.node.id.name);
126
+ } else if (ancestor.isObjectExpression()) {
127
+ const parent = ancestor.parentPath?.node;
128
+ if (types.isVariableDeclarator(parent)) {
129
+ const name = propertyName(parent.id, types);
130
+ if (name) {
131
+ scopes.unshift(name);
132
+ }
133
+ } else if (types.isObjectProperty(parent) && !parent.computed) {
134
+ const name = propertyName(parent.key, types);
135
+ if (name) {
136
+ scopes.unshift(name);
137
+ }
116
138
  }
117
139
  }
118
- import_typescript.default.forEachChild(node, visit);
140
+ ancestor = ancestor.parentPath;
119
141
  }
120
- visit(sourceFile);
121
- return { inlineRoots, referencedRoots };
142
+ return [...scopes, ownName].join(".");
122
143
  }
123
- function declaredFunctionName(node) {
124
- if (node.name && import_typescript.default.isIdentifier(node.name)) {
125
- return node.name.text;
144
+ function isTraceDecoratorRoot(path, types) {
145
+ const node = path.node;
146
+ if (!types.isClassMethod(node) && !types.isClassPrivateMethod(node)) {
147
+ return false;
126
148
  }
127
- if ((import_typescript.default.isArrowFunction(node) || import_typescript.default.isFunctionExpression(node)) && import_typescript.default.isVariableDeclaration(node.parent) && import_typescript.default.isIdentifier(node.parent.name)) {
128
- return node.parent.name.text;
149
+ return node.decorators?.some((decorator) => {
150
+ const expression = decorator.expression;
151
+ if (!types.isCallExpression(expression)) {
152
+ return false;
153
+ }
154
+ const callee = expression.callee;
155
+ return types.isMemberExpression(callee) && !callee.computed && types.isIdentifier(callee.property, { name: "trace" });
156
+ }) ?? false;
157
+ }
158
+ function isBitfabWrapperArgument(path, types) {
159
+ const parent = path.parentPath?.node;
160
+ if (!types.isCallExpression(parent)) {
161
+ return false;
129
162
  }
130
- if ((import_typescript.default.isArrowFunction(node) || import_typescript.default.isFunctionExpression(node)) && import_typescript.default.isPropertyAssignment(node.parent) && (import_typescript.default.isIdentifier(node.parent.name) || import_typescript.default.isStringLiteral(node.parent.name))) {
131
- return node.parent.name.text;
163
+ if (!parent.arguments.some((argument) => argument === path.node)) {
164
+ return false;
132
165
  }
133
- return null;
166
+ const callee = parent.callee;
167
+ return types.isMemberExpression(callee) && !callee.computed && types.isIdentifier(callee.property) && (callee.property.name === "withTrace" || callee.property.name === "withSpan");
134
168
  }
135
- function objectContainerName(node) {
136
- if (import_typescript.default.isVariableDeclaration(node.parent) && import_typescript.default.isIdentifier(node.parent.name)) {
137
- return node.parent.name.text;
169
+ function withNodeFunctionName(path, types) {
170
+ if (path.node.arguments.length > 2) {
171
+ return null;
138
172
  }
139
- if (import_typescript.default.isPropertyAssignment(node.parent) && (import_typescript.default.isIdentifier(node.parent.name) || import_typescript.default.isStringLiteral(node.parent.name))) {
140
- return node.parent.name.text;
173
+ const callee = path.node.callee;
174
+ if (!types.isMemberExpression(callee) || callee.computed || !types.isIdentifier(callee.property, { name: "withNode" })) {
175
+ return null;
176
+ }
177
+ const fn = path.node.arguments[path.node.arguments.length - 1];
178
+ if (types.isIdentifier(fn)) {
179
+ return fn.name;
180
+ }
181
+ if (types.isMemberExpression(fn) && !fn.computed) {
182
+ return propertyName(fn.property, types);
183
+ }
184
+ if (types.isFunctionExpression(fn) && fn.id) {
185
+ return fn.id.name;
141
186
  }
142
187
  return null;
143
188
  }
144
- function functionName(node) {
145
- const ownName = declaredFunctionName(node);
146
- if (ownName) {
147
- const scopes = [];
148
- let ancestor = node.parent;
149
- while (ancestor) {
150
- if (isInstrumentableFunction(ancestor)) {
151
- const name = declaredFunctionName(ancestor);
152
- if (name) {
153
- scopes.unshift(name);
154
- }
155
- } else if ((import_typescript.default.isClassDeclaration(ancestor) || import_typescript.default.isClassExpression(ancestor)) && ancestor.name) {
156
- scopes.unshift(ancestor.name.text);
157
- } else if (import_typescript.default.isObjectLiteralExpression(ancestor)) {
158
- const name = objectContainerName(ancestor);
159
- if (name) {
160
- scopes.unshift(name);
161
- }
162
- }
163
- ancestor = ancestor.parent;
164
- }
165
- return [...scopes, ownName].join(".");
189
+ function functionBindingName(path, types) {
190
+ if (types.isFunctionDeclaration(path.node) && path.node.id) {
191
+ return path.node.id.name;
192
+ }
193
+ const parent = path.parentPath?.node;
194
+ if (types.isVariableDeclarator(parent) && types.isIdentifier(parent.id)) {
195
+ return parent.id.name;
166
196
  }
167
- const sourceFile = node.getSourceFile();
168
- const position = sourceFile.getLineAndCharacterOfPosition(node.getStart());
169
- return `anonymous-${position.line + 1}-${position.character}`;
197
+ return null;
170
198
  }
171
- function isBitfabWrappedFunction(node, inlineRoots, referencedRoots) {
172
- if (inlineRoots.has(node)) {
199
+ function isBitfabWrappedFunction(path, types) {
200
+ if (isBitfabWrapperArgument(path, types)) {
173
201
  return true;
174
202
  }
175
- const name = declaredFunctionName(node);
176
- return name !== null && referencedRoots.has(name);
177
- }
178
- function isInstrumentableFunction(node) {
179
- if (!import_typescript.default.isFunctionDeclaration(node) && !import_typescript.default.isFunctionExpression(node) && !import_typescript.default.isArrowFunction(node) && !import_typescript.default.isMethodDeclaration(node)) {
203
+ const bindingName = functionBindingName(path, types);
204
+ if (!bindingName) {
180
205
  return false;
181
206
  }
182
- return node.body !== void 0 && node.asteriskToken === void 0 && (!node.name || !import_typescript.default.isPrivateIdentifier(node.name));
207
+ const binding = path.scope.getBinding(bindingName);
208
+ return binding?.referencePaths.some(
209
+ (referencePath) => isBitfabWrapperArgument(referencePath, types)
210
+ ) ?? false;
183
211
  }
184
- function normalizedFilename(filename, cwd) {
185
- const normalized = filename.replace(/\\/g, "/").split(/[?#]/, 1)[0] ?? "";
186
- const normalizedCwd = cwd.replace(/\\/g, "/").replace(/\/$/, "");
187
- return normalized.startsWith(`${normalizedCwd}/`) ? normalized.slice(normalizedCwd.length + 1) : normalized;
212
+ function isBitfabManagedFunction(path, types) {
213
+ return isTraceDecoratorRoot(path, types) || isBitfabWrappedFunction(path, types);
188
214
  }
189
- function parameterInputs(factory, node) {
190
- return factory.createArrayLiteralExpression(
191
- node.parameters.map((parameter, index) => {
192
- if (!import_typescript.default.isArrowFunction(node)) {
193
- if (parameter.dotDotDotToken) {
194
- return factory.createSpreadElement(
195
- factory.createCallExpression(
196
- factory.createPropertyAccessExpression(
197
- factory.createArrayLiteralExpression([
198
- factory.createSpreadElement(
199
- factory.createIdentifier("arguments")
200
- )
215
+ function runtimeParameters(node, types) {
216
+ return node.params.filter(
217
+ (parameter) => !types.isIdentifier(parameter, { name: "this" })
218
+ );
219
+ }
220
+ function parameterInputs(path, types) {
221
+ const elements = [];
222
+ for (const [index, parameter] of runtimeParameters(
223
+ path.node,
224
+ types
225
+ ).entries()) {
226
+ if (!types.isArrowFunctionExpression(path.node)) {
227
+ if (types.isRestElement(parameter)) {
228
+ elements.push(
229
+ types.spreadElement(
230
+ types.callExpression(
231
+ types.memberExpression(
232
+ types.arrayExpression([
233
+ types.spreadElement(types.identifier("arguments"))
201
234
  ]),
202
- "slice"
235
+ types.identifier("slice")
203
236
  ),
204
- void 0,
205
- [factory.createNumericLiteral(index)]
237
+ [types.numericLiteral(index)]
206
238
  )
207
- );
208
- }
209
- return factory.createElementAccessExpression(
210
- factory.createIdentifier("arguments"),
211
- factory.createNumericLiteral(index)
239
+ )
240
+ );
241
+ } else {
242
+ elements.push(
243
+ types.memberExpression(
244
+ types.identifier("arguments"),
245
+ types.numericLiteral(index),
246
+ true
247
+ )
212
248
  );
213
249
  }
214
- if (import_typescript.default.isIdentifier(parameter.name)) {
215
- const identifier = factory.createIdentifier(parameter.name.text);
216
- return parameter.dotDotDotToken ? factory.createSpreadElement(identifier) : identifier;
217
- }
218
- throw new Error(
219
- "Bitfab transform invariant: arrow parameter requires argument wrapper"
250
+ continue;
251
+ }
252
+ if (types.isIdentifier(parameter)) {
253
+ elements.push(types.identifier(parameter.name));
254
+ } else if (types.isRestElement(parameter) && types.isIdentifier(parameter.argument)) {
255
+ elements.push(
256
+ types.spreadElement(types.identifier(parameter.argument.name))
220
257
  );
221
- })
222
- );
258
+ }
259
+ }
260
+ return types.arrayExpression(elements);
223
261
  }
224
- function needsArgumentWrapper(node) {
225
- return import_typescript.default.isArrowFunction(node) && node.parameters.some(
226
- (parameter) => !import_typescript.default.isIdentifier(parameter.name) || parameter.initializer !== void 0
262
+ function needsArgumentWrapper(path, types) {
263
+ return types.isArrowFunctionExpression(path.node) && path.node.params.some(
264
+ (parameter) => !types.isIdentifier(parameter) && !(types.isRestElement(parameter) && types.isIdentifier(parameter.argument))
227
265
  );
228
266
  }
229
- function isRestWrapper(node) {
230
- return node.parameters.length === 1 && node.parameters[0]?.dotDotDotToken !== void 0;
267
+ function isRestWrapper(path, types) {
268
+ const parameters = runtimeParameters(path.node, types);
269
+ return parameters.length === 1 && types.isRestElement(parameters[0]);
231
270
  }
232
- function splitDirectivePrologue(body) {
233
- const firstNonDirective = body.statements.findIndex(
234
- (statement) => !(import_typescript.default.isExpressionStatement(statement) && import_typescript.default.isStringLiteral(statement.expression))
235
- );
236
- if (firstNonDirective === -1) {
237
- return { directives: body.statements, statements: [] };
238
- }
271
+ function bitfabBabelPlugin(api) {
272
+ const { types } = api;
239
273
  return {
240
- directives: body.statements.slice(0, firstNonDirective),
241
- statements: body.statements.slice(firstNonDirective)
242
- };
243
- }
244
- function updateFunctionBody(factory, node, body) {
245
- if (import_typescript.default.isFunctionDeclaration(node)) {
246
- return factory.updateFunctionDeclaration(
247
- node,
248
- node.modifiers,
249
- node.asteriskToken,
250
- node.name,
251
- node.typeParameters,
252
- node.parameters,
253
- node.type,
254
- body
255
- );
256
- }
257
- if (import_typescript.default.isFunctionExpression(node)) {
258
- return factory.updateFunctionExpression(
259
- node,
260
- node.modifiers,
261
- node.asteriskToken,
262
- node.name,
263
- node.typeParameters,
264
- node.parameters,
265
- node.type,
266
- body
267
- );
268
- }
269
- if (import_typescript.default.isArrowFunction(node)) {
270
- return factory.updateArrowFunction(
271
- node,
272
- node.modifiers,
273
- node.typeParameters,
274
- node.parameters,
275
- node.type,
276
- node.equalsGreaterThanToken,
277
- body
278
- );
279
- }
280
- if (import_typescript.default.isMethodDeclaration(node)) {
281
- return factory.updateMethodDeclaration(
282
- node,
283
- node.modifiers,
284
- node.asteriskToken,
285
- node.name,
286
- node.questionToken,
287
- node.typeParameters,
288
- node.parameters,
289
- node.type,
290
- body
291
- );
292
- }
293
- return node;
294
- }
295
- function createBitfabTypeScriptTransformer(options = {}) {
296
- return (context) => (sourceFile) => {
297
- if (hasUseClientDirective(sourceFile) || hasTransformMarker(sourceFile) || !shouldTransformBitfabFile(sourceFile.fileName, options)) {
298
- return sourceFile;
299
- }
300
- const { factory } = context;
301
- const { inlineRoots, referencedRoots } = collectBitfabWrappedFunctions(sourceFile);
302
- const runtimeIdentifier = factory.createUniqueName("bitfabAutoSpan");
303
- const activeIdentifier = factory.createUniqueName("bitfabAutoTraceActive");
304
- const wrapperIdentifier = factory.createUniqueName("bitfabAutoWrap");
305
- const transformedNodes = /* @__PURE__ */ new Set();
306
- const definitions = [];
307
- let usedRuntime = false;
308
- let usedWrapper = false;
309
- const cwd = options.cwd ?? process.cwd();
310
- const file = normalizedFilename(sourceFile.fileName, cwd);
311
- const visitor = (node) => {
312
- const visited = import_typescript.default.visitEachChild(node, visitor, context);
313
- if (import_typescript.default.isCallExpression(node) && import_typescript.default.isCallExpression(visited)) {
314
- const configuredName = withNodeFunctionName(node);
315
- if (configuredName !== null) {
316
- const args = [...visited.arguments];
317
- if (args.length === 1) {
318
- args.push(factory.createIdentifier("undefined"));
319
- }
320
- args.push(factory.createStringLiteral(configuredName));
321
- return factory.updateCallExpression(
322
- visited,
323
- visited.expression,
324
- visited.typeArguments,
325
- args
274
+ name: "bitfab-auto-trace",
275
+ visitor: {
276
+ Program: {
277
+ enter(path, state) {
278
+ const filename = state.file.opts.filename ?? "unknown";
279
+ state.skipFile = !shouldTransformBitfabFile(filename, state.opts) || path.scope.hasBinding(AUTO_TRACE_TRANSFORM_MARKER) || path.node.directives.some(
280
+ (directive) => directive.value.value === "use client"
326
281
  );
327
- }
328
- }
329
- if (!isInstrumentableFunction(node)) {
330
- return visited;
331
- }
332
- if (!isInstrumentableFunction(visited) || transformedNodes.has(visited) || isTraceDecoratorRoot(node) || isBitfabWrappedFunction(node, inlineRoots, referencedRoots)) {
333
- return visited;
334
- }
335
- const name = functionName(node);
336
- if (name.startsWith("anonymous-")) {
337
- return visited;
338
- }
339
- const position = sourceFile.getLineAndCharacterOfPosition(node.getStart());
340
- const definition = factory.createObjectLiteralExpression(
341
- [
342
- factory.createPropertyAssignment(
343
- "id",
344
- factory.createStringLiteral(autoTraceFunctionId(file, name))
345
- ),
346
- factory.createPropertyAssignment(
347
- "name",
348
- factory.createStringLiteral(name)
349
- ),
350
- factory.createPropertyAssignment(
351
- "file",
352
- factory.createStringLiteral(file)
353
- ),
354
- factory.createPropertyAssignment(
355
- "line",
356
- factory.createNumericLiteral(position.line + 1)
357
- ),
358
- factory.createPropertyAssignment(
359
- "column",
360
- factory.createNumericLiteral(position.character)
361
- ),
362
- ...node.modifiers?.some(
363
- (modifier) => modifier.kind === import_typescript.default.SyntaxKind.AsyncKeyword
364
- ) ? [factory.createPropertyAssignment("async", factory.createTrue())] : [],
365
- ...isRestWrapper(node) ? [
366
- factory.createPropertyAssignment(
367
- "wrapper",
368
- factory.createTrue()
369
- )
370
- ] : []
371
- ],
372
- false
373
- );
374
- const definitionIdentifier = factory.createUniqueName(
375
- "bitfabAutoDefinition"
376
- );
377
- definitions.push(
378
- factory.createVariableStatement(
379
- void 0,
380
- factory.createVariableDeclarationList(
381
- [
382
- factory.createVariableDeclaration(
383
- definitionIdentifier,
384
- void 0,
385
- void 0,
386
- definition
387
- )
388
- ],
389
- import_typescript.default.NodeFlags.Const
390
- )
391
- )
392
- );
393
- if (needsArgumentWrapper(visited)) {
394
- usedRuntime = true;
395
- usedWrapper = true;
396
- return factory.createCallExpression(wrapperIdentifier, void 0, [
397
- definitionIdentifier,
398
- visited
399
- ]);
400
- }
401
- const visitedBody = visited.body;
402
- const originalBody = import_typescript.default.isBlock(visitedBody) ? visitedBody : factory.createBlock(
403
- [factory.createReturnStatement(visitedBody)],
404
- true
405
- );
406
- const { directives, statements } = splitDirectivePrologue(originalBody);
407
- const invoke = factory.createArrowFunction(
408
- hasAsyncModifier(node) ? [factory.createModifier(import_typescript.default.SyntaxKind.AsyncKeyword)] : void 0,
409
- void 0,
410
- [],
411
- void 0,
412
- factory.createToken(import_typescript.default.SyntaxKind.EqualsGreaterThanToken),
413
- factory.updateBlock(originalBody, statements)
414
- );
415
- const body = factory.createBlock(
416
- [
417
- ...directives,
418
- factory.createIfStatement(
419
- factory.createCallExpression(activeIdentifier, void 0, []),
420
- factory.createBlock(
282
+ state.runtimeIdentifier = path.scope.generateUidIdentifier("bitfabAutoSpan");
283
+ state.activeIdentifier = path.scope.generateUidIdentifier(
284
+ "bitfabAutoTraceActive"
285
+ );
286
+ state.wrapperIdentifier = path.scope.generateUidIdentifier("bitfabAutoWrap");
287
+ state.definitions = [];
288
+ state.usedRuntime = false;
289
+ state.usedWrapper = false;
290
+ },
291
+ exit(path, state) {
292
+ if (state.skipFile || !state.usedRuntime || !state.runtimeIdentifier || !state.activeIdentifier || !state.wrapperIdentifier) {
293
+ return;
294
+ }
295
+ state.file.metadata[AUTO_TRACE_TRANSFORM_MARKER] = true;
296
+ path.unshiftContainer("body", [
297
+ types.importDeclaration(
421
298
  [
422
- factory.createReturnStatement(
423
- factory.createCallExpression(runtimeIdentifier, void 0, [
424
- definitionIdentifier,
425
- parameterInputs(factory, visited),
426
- invoke
427
- ])
428
- )
429
- ],
430
- true
431
- )
432
- ),
433
- ...statements
434
- ],
435
- true
436
- );
437
- const updated = updateFunctionBody(factory, visited, body);
438
- transformedNodes.add(updated);
439
- usedRuntime = true;
440
- return updated;
441
- };
442
- const transformed = import_typescript.default.visitEachChild(sourceFile, visitor, context);
443
- if (!usedRuntime) {
444
- return transformed;
445
- }
446
- const runtimeImport = factory.createImportDeclaration(
447
- void 0,
448
- factory.createImportClause(
449
- false,
450
- void 0,
451
- factory.createNamedImports([
452
- factory.createImportSpecifier(
453
- false,
454
- factory.createIdentifier("__bitfabAutoSpan"),
455
- runtimeIdentifier
456
- ),
457
- factory.createImportSpecifier(
458
- false,
459
- factory.createIdentifier("__bitfabAutoTraceActive"),
460
- activeIdentifier
461
- ),
462
- ...usedWrapper ? [
463
- factory.createImportSpecifier(
464
- false,
465
- factory.createIdentifier("__bitfabAutoWrap"),
466
- wrapperIdentifier
467
- )
468
- ] : []
469
- ])
470
- ),
471
- factory.createStringLiteral(options.runtimeModule ?? "@bitfab/sdk/auto")
472
- );
473
- const transformMarker = factory.createVariableStatement(
474
- void 0,
475
- factory.createVariableDeclarationList(
476
- [
477
- factory.createVariableDeclaration(
478
- factory.createIdentifier(AUTO_TRACE_TRANSFORM_MARKER),
479
- void 0,
480
- void 0,
481
- factory.createAssignment(
482
- factory.createPropertyAccessExpression(
483
- factory.createIdentifier("globalThis"),
484
- factory.createIdentifier(AUTO_TRACE_TRANSFORM_MARKER)
485
- ),
486
- factory.createTrue()
487
- )
488
- )
489
- ],
490
- import_typescript.default.NodeFlags.Const
491
- )
492
- );
493
- return factory.updateSourceFile(transformed, [
494
- context.getCompilerOptions().module === import_typescript.default.ModuleKind.CommonJS ? factory.createVariableStatement(
495
- void 0,
496
- factory.createVariableDeclarationList(
497
- [
498
- factory.createVariableDeclaration(
499
- factory.createObjectBindingPattern([
500
- factory.createBindingElement(
501
- void 0,
502
- "__bitfabAutoSpan",
503
- runtimeIdentifier
299
+ types.importSpecifier(
300
+ state.runtimeIdentifier,
301
+ types.identifier("__bitfabAutoSpan")
504
302
  ),
505
- factory.createBindingElement(
506
- void 0,
507
- "__bitfabAutoTraceActive",
508
- activeIdentifier
303
+ types.importSpecifier(
304
+ state.activeIdentifier,
305
+ types.identifier("__bitfabAutoTraceActive")
509
306
  ),
510
- ...usedWrapper ? [
511
- factory.createBindingElement(
512
- void 0,
513
- "__bitfabAutoWrap",
514
- wrapperIdentifier
307
+ ...state.usedWrapper ? [
308
+ types.importSpecifier(
309
+ state.wrapperIdentifier,
310
+ types.identifier("__bitfabAutoWrap")
515
311
  )
516
312
  ] : []
517
- ]),
518
- void 0,
519
- void 0,
520
- factory.createCallExpression(
521
- factory.createIdentifier("require"),
522
- void 0,
523
- [
524
- factory.createStringLiteral(
525
- options.runtimeModule ?? "@bitfab/sdk/auto"
526
- )
527
- ]
313
+ ],
314
+ types.stringLiteral(
315
+ state.opts.runtimeModule ?? "@bitfab/sdk/auto"
528
316
  )
529
- )
530
- ],
531
- import_typescript.default.NodeFlags.Const
532
- )
533
- ) : runtimeImport,
534
- transformMarker,
535
- ...definitions,
536
- ...transformed.statements
537
- ]);
317
+ ),
318
+ types.variableDeclaration("const", [
319
+ types.variableDeclarator(
320
+ types.identifier(AUTO_TRACE_TRANSFORM_MARKER),
321
+ types.assignmentExpression(
322
+ "=",
323
+ types.memberExpression(
324
+ types.identifier("globalThis"),
325
+ types.identifier(AUTO_TRACE_TRANSFORM_MARKER)
326
+ ),
327
+ types.booleanLiteral(true)
328
+ )
329
+ )
330
+ ]),
331
+ ...state.definitions ?? []
332
+ ]);
333
+ }
334
+ },
335
+ CallExpression: {
336
+ exit(path, state) {
337
+ if (state.skipFile) {
338
+ return;
339
+ }
340
+ const configuredName = withNodeFunctionName(path, types);
341
+ if (configuredName === null) {
342
+ return;
343
+ }
344
+ if (path.node.arguments.length === 1) {
345
+ path.node.arguments.push(types.identifier("undefined"));
346
+ }
347
+ path.node.arguments.push(types.stringLiteral(configuredName));
348
+ }
349
+ },
350
+ Function: {
351
+ exit(path, state) {
352
+ const node = path.node;
353
+ if (state.skipFile || node._bitfabInstrumented || !isInstrumentableFunction(node, types) || isBitfabManagedFunction(path, types) || !state.runtimeIdentifier || !state.activeIdentifier || !state.wrapperIdentifier) {
354
+ return;
355
+ }
356
+ const name = qualifiedFunctionName(path, types);
357
+ if (name.startsWith("anonymous-")) {
358
+ return;
359
+ }
360
+ const normalizedFilename = autoTraceFilename(
361
+ state.file.opts.filename ?? "unknown",
362
+ state.opts.cwd ?? state.file.opts.cwd ?? process.cwd()
363
+ );
364
+ const { line, column } = sourcePosition(path);
365
+ const definition = types.objectExpression([
366
+ types.objectProperty(
367
+ types.identifier("id"),
368
+ types.stringLiteral(
369
+ autoTraceFunctionId(normalizedFilename, name)
370
+ )
371
+ ),
372
+ types.objectProperty(
373
+ types.identifier("name"),
374
+ types.stringLiteral(name)
375
+ ),
376
+ types.objectProperty(
377
+ types.identifier("file"),
378
+ types.stringLiteral(normalizedFilename)
379
+ ),
380
+ types.objectProperty(
381
+ types.identifier("line"),
382
+ types.numericLiteral(line)
383
+ ),
384
+ types.objectProperty(
385
+ types.identifier("column"),
386
+ types.numericLiteral(column)
387
+ ),
388
+ ...node.async ? [
389
+ types.objectProperty(
390
+ types.identifier("async"),
391
+ types.booleanLiteral(true)
392
+ )
393
+ ] : [],
394
+ ...isRestWrapper(path, types) ? [
395
+ types.objectProperty(
396
+ types.identifier("wrapper"),
397
+ types.booleanLiteral(true)
398
+ )
399
+ ] : []
400
+ ]);
401
+ const definitionIdentifier = path.scope.getProgramParent().generateUidIdentifier("bitfabAutoDefinition");
402
+ state.definitions?.push(
403
+ types.variableDeclaration("const", [
404
+ types.variableDeclarator(definitionIdentifier, definition)
405
+ ])
406
+ );
407
+ if (needsArgumentWrapper(path, types)) {
408
+ node._bitfabInstrumented = true;
409
+ path.replaceWith(
410
+ types.callExpression(types.cloneNode(state.wrapperIdentifier), [
411
+ types.cloneNode(definitionIdentifier),
412
+ node
413
+ ])
414
+ );
415
+ state.usedRuntime = true;
416
+ state.usedWrapper = true;
417
+ return;
418
+ }
419
+ const originalBody = types.isBlockStatement(node.body) ? node.body : types.blockStatement([types.returnStatement(node.body)]);
420
+ const directives = originalBody.directives;
421
+ originalBody.directives = [];
422
+ const body = types.blockStatement(
423
+ [
424
+ types.ifStatement(
425
+ types.callExpression(
426
+ types.cloneNode(state.activeIdentifier),
427
+ []
428
+ ),
429
+ types.blockStatement([
430
+ types.returnStatement(
431
+ types.callExpression(
432
+ types.cloneNode(state.runtimeIdentifier),
433
+ [
434
+ types.cloneNode(definitionIdentifier),
435
+ parameterInputs(path, types),
436
+ types.arrowFunctionExpression(
437
+ [],
438
+ types.cloneNode(originalBody, true),
439
+ node.async ?? false
440
+ )
441
+ ]
442
+ )
443
+ )
444
+ ])
445
+ ),
446
+ ...originalBody.body
447
+ ],
448
+ directives
449
+ );
450
+ node.body = body;
451
+ node._bitfabInstrumented = true;
452
+ state.usedRuntime = true;
453
+ }
454
+ }
455
+ }
538
456
  };
539
457
  }
540
- function hasAsyncModifier(node) {
541
- return import_typescript.default.canHaveModifiers(node) ? import_typescript.default.getModifiers(node)?.some((modifier) => modifier.kind === import_typescript.default.SyntaxKind.AsyncKeyword) ?? false : false;
458
+
459
+ // ../bitfab-typescript-transform/src/transform.ts
460
+ function parserPlugins(filename) {
461
+ const path = sourceFilename(filename);
462
+ const typescript = /\.[cm]?tsx?$/.test(path);
463
+ const jsx = /\.tsx$/.test(path) || /\.[cm]?jsx?$/.test(path);
464
+ return [
465
+ ...typescript ? ["typescript"] : [],
466
+ ...jsx ? ["jsx"] : [],
467
+ "decorators"
468
+ ];
542
469
  }
543
- function transformBitfabTypeScriptSource(source, filename, options = {}) {
544
- const sourceFilename = filename.replace(/\\/g, "/").split(/[?#]/, 1)[0] ?? filename;
545
- const sourceFile = import_typescript.default.createSourceFile(
546
- sourceFilename,
547
- source,
548
- import_typescript.default.ScriptTarget.Latest,
549
- true,
550
- sourceFilename.endsWith("x") ? import_typescript.default.ScriptKind.TSX : import_typescript.default.ScriptKind.TS
551
- );
552
- const result = import_typescript.default.transform(sourceFile, [
553
- createBitfabTypeScriptTransformer(options)
554
- ]);
555
- try {
556
- return {
557
- code: import_typescript.default.createPrinter().printFile(result.transformed[0])
558
- };
559
- } finally {
560
- result.dispose();
470
+ function transformBitfabSource(source, filename, options = {}) {
471
+ if (!shouldTransformBitfabFile(filename, options)) {
472
+ return { code: source };
473
+ }
474
+ const result = (0, import_core.transformSync)(source, {
475
+ babelrc: false,
476
+ configFile: false,
477
+ filename: sourceFilename(filename),
478
+ parserOpts: { plugins: parserPlugins(filename), sourceType: "module" },
479
+ plugins: [[bitfabBabelPlugin, options]],
480
+ sourceMaps: true
481
+ });
482
+ if (!result?.code || !result.metadata?.[AUTO_TRACE_TRANSFORM_MARKER]) {
483
+ return { code: source };
561
484
  }
485
+ return {
486
+ code: result.code,
487
+ ...result.map ? { map: result.map } : {}
488
+ };
562
489
  }
563
490
 
564
491
  // ../bitfab-typescript-transform/src/unplugin.ts
@@ -571,10 +498,8 @@ var bitfabTraceUnplugin = (0, import_unplugin.createUnplugin)((options) => {
571
498
  if (!shouldTransformBitfabFile(filename, resolvedOptions)) {
572
499
  return null;
573
500
  }
574
- return {
575
- ...transformBitfabTypeScriptSource(source, filename, resolvedOptions),
576
- map: null
577
- };
501
+ const result = transformBitfabSource(source, filename, resolvedOptions);
502
+ return result.map ? result : null;
578
503
  }
579
504
  };
580
505
  });