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