@rasputin-ai/node 0.1.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +129 -14
- package/dist/auto-instrumentation/bun-plugin.d.ts +23 -0
- package/dist/auto-instrumentation/bun-plugin.d.ts.map +1 -0
- package/dist/auto-instrumentation/collect-instrumentation-delta.d.ts +58 -0
- package/dist/auto-instrumentation/collect-instrumentation-delta.d.ts.map +1 -0
- package/dist/auto-instrumentation/instrumentation-manifest-registry.d.ts +11 -0
- package/dist/auto-instrumentation/instrumentation-manifest-registry.d.ts.map +1 -0
- package/dist/auto-instrumentation/node-hooks.d.ts +6 -0
- package/dist/auto-instrumentation/node-hooks.d.ts.map +1 -0
- package/dist/auto-instrumentation/schedule-instrumentation-manifest-upload.d.ts +16 -0
- package/dist/auto-instrumentation/schedule-instrumentation-manifest-upload.d.ts.map +1 -0
- package/dist/auto-instrumentation/sdk-source-boundary.d.ts +7 -0
- package/dist/auto-instrumentation/sdk-source-boundary.d.ts.map +1 -0
- package/dist/auto-instrumentation/source-classification.d.ts +10 -0
- package/dist/auto-instrumentation/source-classification.d.ts.map +1 -0
- package/dist/auto-instrumentation/source-map-locations.d.ts +3 -0
- package/dist/auto-instrumentation/source-map-locations.d.ts.map +1 -0
- package/dist/auto-instrumentation/transform-source.d.ts +18 -0
- package/dist/auto-instrumentation/transform-source.d.ts.map +1 -0
- package/dist/execution-recorder/automatic-runtime.d.ts +18 -0
- package/dist/execution-recorder/automatic-runtime.d.ts.map +1 -0
- package/dist/execution-recorder/call-aware-event-buffer.d.ts +23 -0
- package/dist/execution-recorder/call-aware-event-buffer.d.ts.map +1 -0
- package/dist/execution-recorder/execution-recorder-types.d.ts +71 -0
- package/dist/execution-recorder/execution-recorder-types.d.ts.map +1 -0
- package/dist/execution-recorder/execution-recorder.d.ts +4 -0
- package/dist/execution-recorder/execution-recorder.d.ts.map +1 -0
- package/dist/execution-recorder/index.d.ts +3 -0
- package/dist/execution-recorder/index.d.ts.map +1 -0
- package/dist/execution-recorder/recorder-memory-budget.d.ts +37 -0
- package/dist/execution-recorder/recorder-memory-budget.d.ts.map +1 -0
- package/dist/execution-recorder/safe-serialize.d.ts +10 -0
- package/dist/execution-recorder/safe-serialize.d.ts.map +1 -0
- package/dist/execution-recorder/source-exclusions.d.ts +9 -0
- package/dist/execution-recorder/source-exclusions.d.ts.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1201 -8
- package/dist/install-global-handlers.d.ts.map +1 -1
- package/dist/instrument/bun.d.ts +2 -0
- package/dist/instrument/bun.d.ts.map +1 -0
- package/dist/instrument/bun.js +477 -0
- package/dist/instrument/node.d.ts +2 -0
- package/dist/instrument/node.d.ts.map +1 -0
- package/dist/instrument/node.js +532 -0
- package/dist/rasputin-init.d.ts +29 -2
- package/dist/rasputin-init.d.ts.map +1 -1
- package/dist/sdk-meta.d.ts +1 -1
- package/package.json +15 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"install-global-handlers.d.ts","sourceRoot":"","sources":["../src/install-global-handlers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,MAAM,MAAM,4BAA4B,GAAG;IAC1C;;;;OAIG;IACH,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,gDAAgD;IAChD,cAAc,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;
|
|
1
|
+
{"version":3,"file":"install-global-handlers.d.ts","sourceRoot":"","sources":["../src/install-global-handlers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,MAAM,MAAM,4BAA4B,GAAG;IAC1C;;;;OAIG;IACH,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,gDAAgD;IAChD,cAAc,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAQF;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,GACjC,QAAQ,IAAI,CAAC,cAAc,EAAE,kBAAkB,GAAG,OAAO,CAAC,EAC1D,UAAS,4BAAiC,KACxC,CAAC,MAAM,IAAI,CAuDb,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bun.d.ts","sourceRoot":"","sources":["../../src/instrument/bun.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
// src/instrument/bun.ts
|
|
2
|
+
import { plugin } from "bun";
|
|
3
|
+
|
|
4
|
+
// src/auto-instrumentation/bun-plugin.ts
|
|
5
|
+
import { readFile } from "node:fs/promises";
|
|
6
|
+
import { resolve as resolve5 } from "node:path";
|
|
7
|
+
|
|
8
|
+
// src/auto-instrumentation/instrumentation-manifest-registry.ts
|
|
9
|
+
import { isAbsolute as isAbsolute2, relative as relative2, resolve as resolve2, sep as sep2 } from "node:path";
|
|
10
|
+
|
|
11
|
+
// src/execution-recorder/automatic-runtime.ts
|
|
12
|
+
import { isAbsolute, relative, resolve, sep } from "node:path";
|
|
13
|
+
var AUTOMATIC_RUNTIME_SYMBOL = "rasputin.execution.runtime.v1";
|
|
14
|
+
var AUTOMATIC_SOURCE_SEPARATOR = "\0";
|
|
15
|
+
var runtimeSymbol = Symbol.for(AUTOMATIC_RUNTIME_SYMBOL);
|
|
16
|
+
var encodeAutomaticFunctionSource = (source) => [resolve(source.filePath), source.name, String(source.line), String(source.column)].join(
|
|
17
|
+
AUTOMATIC_SOURCE_SEPARATOR
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
// src/auto-instrumentation/instrumentation-manifest-registry.ts
|
|
21
|
+
var INSTRUMENTATION_MANIFEST_SYMBOL = "rasputin.instrumentation.manifest.v1";
|
|
22
|
+
var registrySymbol = Symbol.for(INSTRUMENTATION_MANIFEST_SYMBOL);
|
|
23
|
+
var getRegistry = () => {
|
|
24
|
+
const existing = Reflect.get(globalThis, registrySymbol);
|
|
25
|
+
if (existing?.__rasputinManifestRegistry && existing.files instanceof Map && typeof existing.generation === "number" && typeof existing.uploadedGeneration === "number") {
|
|
26
|
+
return existing;
|
|
27
|
+
}
|
|
28
|
+
const registry = {
|
|
29
|
+
__rasputinManifestRegistry: true,
|
|
30
|
+
files: /* @__PURE__ */ new Map(),
|
|
31
|
+
generation: 0,
|
|
32
|
+
uploadedGeneration: 0
|
|
33
|
+
};
|
|
34
|
+
Reflect.set(globalThis, registrySymbol, registry);
|
|
35
|
+
return registry;
|
|
36
|
+
};
|
|
37
|
+
var recordInstrumentationManifestDelta = (filePath, delta) => {
|
|
38
|
+
const registry = getRegistry();
|
|
39
|
+
registry.files.set(resolve2(filePath), delta);
|
|
40
|
+
registry.generation += 1;
|
|
41
|
+
registry.onDirty?.();
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/auto-instrumentation/source-classification.ts
|
|
45
|
+
import { isAbsolute as isAbsolute3, relative as relative3, resolve as resolve3, sep as sep3 } from "node:path";
|
|
46
|
+
var sourceExtension = /\.[cm]?[jt]sx?$/i;
|
|
47
|
+
var alwaysExcludedDirectory = /(^|[\\/])(node_modules|coverage|generated|\.git|\.next|\.svelte-kit|\.output|\.vercel|\.rasputin|\.turbo|\.yarn|\.pnpm|\.bun)([\\/]|$)/;
|
|
48
|
+
var buildOutputDirectory = /(^|[\\/])(dist|build|out)([\\/]|$)/;
|
|
49
|
+
var matches = (pattern, value) => {
|
|
50
|
+
pattern.lastIndex = 0;
|
|
51
|
+
const result = pattern.test(value);
|
|
52
|
+
pattern.lastIndex = 0;
|
|
53
|
+
return result;
|
|
54
|
+
};
|
|
55
|
+
var isWithinRoot = (filePath, root) => {
|
|
56
|
+
const fromRoot = relative3(resolve3(root), filePath);
|
|
57
|
+
return fromRoot === "" || !fromRoot.startsWith(`..${sep3}`) && fromRoot !== ".." && !isAbsolute3(fromRoot);
|
|
58
|
+
};
|
|
59
|
+
var isApplicationSource = (filePath, options = {}) => {
|
|
60
|
+
const absolutePath = resolve3(filePath);
|
|
61
|
+
if (!sourceExtension.test(absolutePath) || alwaysExcludedDirectory.test(absolutePath))
|
|
62
|
+
return false;
|
|
63
|
+
if (options.includeBuildOutput !== true && buildOutputDirectory.test(absolutePath)) return false;
|
|
64
|
+
if (options.excludedRoots?.some((root) => isWithinRoot(absolutePath, root))) return false;
|
|
65
|
+
if (options.exclude && matches(options.exclude, absolutePath)) return false;
|
|
66
|
+
if (options.include && !matches(options.include, absolutePath)) return false;
|
|
67
|
+
if (options.rootDir) {
|
|
68
|
+
if (!isWithinRoot(absolutePath, options.rootDir)) return false;
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// src/auto-instrumentation/transform-source.ts
|
|
74
|
+
import { resolve as resolve4 } from "node:path";
|
|
75
|
+
import ts2 from "typescript";
|
|
76
|
+
|
|
77
|
+
// src/auto-instrumentation/collect-instrumentation-delta.ts
|
|
78
|
+
import ts from "typescript";
|
|
79
|
+
var paramBindingFromName = (name, sourceFile) => {
|
|
80
|
+
if (ts.isIdentifier(name)) return { kind: "identifier", name: name.text };
|
|
81
|
+
if (ts.isArrayBindingPattern(name)) {
|
|
82
|
+
const elements = name.elements.map(
|
|
83
|
+
(element) => {
|
|
84
|
+
if (ts.isOmittedExpression(element)) return { kind: "omitted" };
|
|
85
|
+
const binding = paramBindingFromName(element.name, sourceFile);
|
|
86
|
+
if (element.dotDotDotToken) return { kind: "rest", binding };
|
|
87
|
+
return binding;
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
return { kind: "array", elements };
|
|
91
|
+
}
|
|
92
|
+
const properties = name.elements.map((element) => {
|
|
93
|
+
const binding = paramBindingFromName(element.name, sourceFile);
|
|
94
|
+
const key = element.propertyName ? element.propertyName.getText(sourceFile).replace(/^['"]|['"]$/g, "") : ts.isIdentifier(element.name) ? element.name.text : element.name.getText(sourceFile);
|
|
95
|
+
if (element.dotDotDotToken) {
|
|
96
|
+
const rest = { kind: "rest", binding };
|
|
97
|
+
return { key, binding: rest };
|
|
98
|
+
}
|
|
99
|
+
return { key, binding };
|
|
100
|
+
});
|
|
101
|
+
return { kind: "object", properties };
|
|
102
|
+
};
|
|
103
|
+
var collectFunctionParams = (node, sourceFile) => {
|
|
104
|
+
const params = [];
|
|
105
|
+
for (const parameter of node.parameters) {
|
|
106
|
+
if (ts.isIdentifier(parameter.name) && parameter.name.text === "this") continue;
|
|
107
|
+
const binding = paramBindingFromName(parameter.name, sourceFile);
|
|
108
|
+
params.push(parameter.dotDotDotToken ? { kind: "rest", binding } : binding);
|
|
109
|
+
}
|
|
110
|
+
return { params, paramNames: identifierNames(params) };
|
|
111
|
+
};
|
|
112
|
+
var identifierNames = (bindings) => {
|
|
113
|
+
const names = /* @__PURE__ */ new Set();
|
|
114
|
+
const walk = (node) => {
|
|
115
|
+
if (node.kind === "identifier") {
|
|
116
|
+
names.add(node.name);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (node.kind === "rest") {
|
|
120
|
+
walk(node.binding);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (node.kind === "object") {
|
|
124
|
+
for (const property of node.properties) walk(property.binding);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (node.kind === "array") {
|
|
128
|
+
for (const element of node.elements) {
|
|
129
|
+
if (element.kind !== "omitted") walk(element);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
for (const item of bindings) walk(item);
|
|
134
|
+
return names;
|
|
135
|
+
};
|
|
136
|
+
var mappedLocation = (sourceFile, position, absolutePath, mapLocation) => {
|
|
137
|
+
const generated = sourceFile.getLineAndCharacterOfPosition(position);
|
|
138
|
+
const raw = {
|
|
139
|
+
filePath: absolutePath,
|
|
140
|
+
line: generated.line + 1,
|
|
141
|
+
column: generated.character + 1
|
|
142
|
+
};
|
|
143
|
+
return mapLocation ? mapLocation(raw) : raw;
|
|
144
|
+
};
|
|
145
|
+
var unwrapExpression = (expression) => {
|
|
146
|
+
let current = expression;
|
|
147
|
+
while (ts.isParenthesizedExpression(current) || ts.isNonNullExpression(current)) {
|
|
148
|
+
current = current.expression;
|
|
149
|
+
}
|
|
150
|
+
if (ts.isAwaitExpression(current)) return unwrapExpression(current.expression);
|
|
151
|
+
return current;
|
|
152
|
+
};
|
|
153
|
+
var calleeFromExpression = (expression, sourceFile) => {
|
|
154
|
+
const current = unwrapExpression(expression);
|
|
155
|
+
if (ts.isIdentifier(current)) {
|
|
156
|
+
return { calleeName: current.text, calleeText: current.text };
|
|
157
|
+
}
|
|
158
|
+
if (ts.isPropertyAccessExpression(current) && ts.isIdentifier(current.name)) {
|
|
159
|
+
return {
|
|
160
|
+
calleeName: current.name.text,
|
|
161
|
+
calleeText: current.getText(sourceFile).replace(/\s+/g, "")
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
return null;
|
|
165
|
+
};
|
|
166
|
+
var resultBindingForCall = (call) => {
|
|
167
|
+
let node = call.parent;
|
|
168
|
+
if (ts.isAwaitExpression(node)) node = node.parent;
|
|
169
|
+
if (!ts.isVariableDeclaration(node)) return null;
|
|
170
|
+
if (!ts.isIdentifier(node.name)) return null;
|
|
171
|
+
const initializer = node.initializer;
|
|
172
|
+
if (!initializer) return null;
|
|
173
|
+
const inner = unwrapExpression(initializer);
|
|
174
|
+
if (inner !== call) return null;
|
|
175
|
+
return node.name.text;
|
|
176
|
+
};
|
|
177
|
+
var argIdentifiersForCall = (call) => {
|
|
178
|
+
const identifiers = [];
|
|
179
|
+
call.arguments?.forEach((argument, index) => {
|
|
180
|
+
if (ts.isIdentifier(argument)) {
|
|
181
|
+
identifiers.push({ index, kind: "identifier", name: argument.text });
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
if (!ts.isObjectLiteralExpression(argument)) return;
|
|
185
|
+
const properties = [];
|
|
186
|
+
for (const property of argument.properties) {
|
|
187
|
+
if (ts.isShorthandPropertyAssignment(property)) {
|
|
188
|
+
properties.push({ key: property.name.text, name: property.name.text });
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (ts.isPropertyAssignment(property) && ts.isIdentifier(property.name) && ts.isIdentifier(property.initializer)) {
|
|
192
|
+
properties.push({ key: property.name.text, name: property.initializer.text });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (properties.length > 0) {
|
|
196
|
+
identifiers.push({ index, kind: "object", properties });
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
return identifiers;
|
|
200
|
+
};
|
|
201
|
+
var collectCallSite = (call, caller, context) => {
|
|
202
|
+
const callee = calleeFromExpression(call.expression, context.sourceFile);
|
|
203
|
+
if (!callee) return null;
|
|
204
|
+
const start = mappedLocation(
|
|
205
|
+
context.sourceFile,
|
|
206
|
+
call.getStart(context.sourceFile),
|
|
207
|
+
context.absolutePath,
|
|
208
|
+
context.mapLocation
|
|
209
|
+
);
|
|
210
|
+
const end = mappedLocation(
|
|
211
|
+
context.sourceFile,
|
|
212
|
+
call.end,
|
|
213
|
+
context.absolutePath,
|
|
214
|
+
context.mapLocation
|
|
215
|
+
);
|
|
216
|
+
if (!start || !end) return null;
|
|
217
|
+
return {
|
|
218
|
+
callerSourceToken: caller.sourceToken,
|
|
219
|
+
calleeName: callee.calleeName,
|
|
220
|
+
calleeText: callee.calleeText,
|
|
221
|
+
filePath: start.filePath,
|
|
222
|
+
line: start.line,
|
|
223
|
+
column: start.column,
|
|
224
|
+
endLine: end.line,
|
|
225
|
+
endColumn: end.column,
|
|
226
|
+
resultBinding: resultBindingForCall(call),
|
|
227
|
+
argIdentifiers: argIdentifiersForCall(call)
|
|
228
|
+
};
|
|
229
|
+
};
|
|
230
|
+
var collectParamDestructure = (declaration, caller, context) => {
|
|
231
|
+
if (!declaration.initializer || !ts.isIdentifier(declaration.initializer)) return null;
|
|
232
|
+
if (!ts.isObjectBindingPattern(declaration.name) && !ts.isArrayBindingPattern(declaration.name)) {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
const sourceParam = declaration.initializer.text;
|
|
236
|
+
if (!caller.paramNames.has(sourceParam)) return null;
|
|
237
|
+
const start = mappedLocation(
|
|
238
|
+
context.sourceFile,
|
|
239
|
+
declaration.getStart(context.sourceFile),
|
|
240
|
+
context.absolutePath,
|
|
241
|
+
context.mapLocation
|
|
242
|
+
);
|
|
243
|
+
if (!start) return null;
|
|
244
|
+
return {
|
|
245
|
+
callerSourceToken: caller.sourceToken,
|
|
246
|
+
sourceParam,
|
|
247
|
+
filePath: start.filePath,
|
|
248
|
+
line: start.line,
|
|
249
|
+
column: start.column,
|
|
250
|
+
binding: paramBindingFromName(declaration.name, context.sourceFile)
|
|
251
|
+
};
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// src/auto-instrumentation/transform-source.ts
|
|
255
|
+
var scriptKindFor = (filePath) => {
|
|
256
|
+
if (/\.tsx$/i.test(filePath)) return ts2.ScriptKind.TSX;
|
|
257
|
+
if (/\.jsx$/i.test(filePath)) return ts2.ScriptKind.JSX;
|
|
258
|
+
if (/\.[cm]?ts$/i.test(filePath)) return ts2.ScriptKind.TS;
|
|
259
|
+
return ts2.ScriptKind.JS;
|
|
260
|
+
};
|
|
261
|
+
var functionName = (node, sourceFile) => {
|
|
262
|
+
if (node.name) return node.name.getText(sourceFile).replace(/^['"]|['"]$/g, "");
|
|
263
|
+
const parent = node.parent;
|
|
264
|
+
if (ts2.isVariableDeclaration(parent) && ts2.isIdentifier(parent.name)) return parent.name.text;
|
|
265
|
+
if (ts2.isPropertyAssignment(parent)) return parent.name.getText(sourceFile);
|
|
266
|
+
if (ts2.isPropertyDeclaration(parent) && parent.name) return parent.name.getText(sourceFile);
|
|
267
|
+
const location = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
|
|
268
|
+
return `anonymous@${location.line + 1}:${location.character + 1}`;
|
|
269
|
+
};
|
|
270
|
+
var functionLocationPosition = (node, sourceFile) => {
|
|
271
|
+
if (node.name) return node.name.getStart(sourceFile);
|
|
272
|
+
const parent = node.parent;
|
|
273
|
+
if ((ts2.isVariableDeclaration(parent) || ts2.isPropertyAssignment(parent)) && parent.name) {
|
|
274
|
+
return parent.name.getStart(sourceFile);
|
|
275
|
+
}
|
|
276
|
+
return node.getStart(sourceFile);
|
|
277
|
+
};
|
|
278
|
+
var bindingValue = (name, sourceFile) => {
|
|
279
|
+
if (ts2.isIdentifier(name)) return name.text;
|
|
280
|
+
if (ts2.isArrayBindingPattern(name)) {
|
|
281
|
+
const values = name.elements.map((element) => {
|
|
282
|
+
if (ts2.isOmittedExpression(element)) return "";
|
|
283
|
+
const value = bindingValue(element.name, sourceFile);
|
|
284
|
+
return element.dotDotDotToken ? `...${value}` : value;
|
|
285
|
+
});
|
|
286
|
+
return `[${values.join(",")}]`;
|
|
287
|
+
}
|
|
288
|
+
const properties = name.elements.map((element) => {
|
|
289
|
+
const value = bindingValue(element.name, sourceFile);
|
|
290
|
+
if (element.dotDotDotToken) return `...${value}`;
|
|
291
|
+
if (element.propertyName) return `${element.propertyName.getText(sourceFile)}:${value}`;
|
|
292
|
+
return value;
|
|
293
|
+
});
|
|
294
|
+
return `{${properties.join(",")}}`;
|
|
295
|
+
};
|
|
296
|
+
var arrowArguments = (node) => {
|
|
297
|
+
const values = node.parameters.flatMap((parameter) => {
|
|
298
|
+
if (ts2.isIdentifier(parameter.name) && parameter.name.text === "this") return [];
|
|
299
|
+
const value = bindingValue(parameter.name, node.getSourceFile());
|
|
300
|
+
return [parameter.dotDotDotToken ? `...${value}` : value];
|
|
301
|
+
});
|
|
302
|
+
return `[${values.join(",")}]`;
|
|
303
|
+
};
|
|
304
|
+
var isSupportedFunction = (node) => {
|
|
305
|
+
if (!ts2.isFunctionDeclaration(node) && !ts2.isFunctionExpression(node) && !ts2.isArrowFunction(node) && !ts2.isMethodDeclaration(node) && !ts2.isGetAccessorDeclaration(node) && !ts2.isSetAccessorDeclaration(node)) {
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
if (!node.body || "asteriskToken" in node && node.asteriskToken) return false;
|
|
309
|
+
return true;
|
|
310
|
+
};
|
|
311
|
+
var helperDeclaration = (helperName) => `const ${helperName}=(functionId,args,callback)=>{const runtime=Reflect.get(globalThis,Symbol.for(${JSON.stringify(AUTOMATIC_RUNTIME_SYMBOL)}));return runtime?runtime.run(functionId,args,callback):callback()};`;
|
|
312
|
+
var collectWrappedFunction = (node, sourceFile, absolutePath, mapLocation) => {
|
|
313
|
+
const start = mappedLocation(
|
|
314
|
+
sourceFile,
|
|
315
|
+
functionLocationPosition(node, sourceFile),
|
|
316
|
+
absolutePath,
|
|
317
|
+
mapLocation
|
|
318
|
+
);
|
|
319
|
+
if (!start) return void 0;
|
|
320
|
+
const end = mappedLocation(sourceFile, node.end, absolutePath, mapLocation) ?? start;
|
|
321
|
+
const name = functionName(node, sourceFile).replace(/@\d+:\d+$/, "");
|
|
322
|
+
const { params, paramNames } = collectFunctionParams(node, sourceFile);
|
|
323
|
+
return {
|
|
324
|
+
sourceToken: encodeAutomaticFunctionSource({
|
|
325
|
+
filePath: start.filePath,
|
|
326
|
+
name,
|
|
327
|
+
line: start.line,
|
|
328
|
+
column: start.column
|
|
329
|
+
}),
|
|
330
|
+
filePath: start.filePath,
|
|
331
|
+
name,
|
|
332
|
+
startLine: start.line,
|
|
333
|
+
startColumn: start.column,
|
|
334
|
+
endLine: end.line,
|
|
335
|
+
endColumn: end.column,
|
|
336
|
+
params,
|
|
337
|
+
paramNames
|
|
338
|
+
};
|
|
339
|
+
};
|
|
340
|
+
var transformSource = (source, options) => {
|
|
341
|
+
if (source.includes("@rasputin-ignore-file")) return void 0;
|
|
342
|
+
const sourceFile = ts2.createSourceFile(
|
|
343
|
+
options.filePath,
|
|
344
|
+
source,
|
|
345
|
+
ts2.ScriptTarget.Latest,
|
|
346
|
+
true,
|
|
347
|
+
scriptKindFor(options.filePath)
|
|
348
|
+
);
|
|
349
|
+
let helperName = "__rasputinAutoRun__";
|
|
350
|
+
while (source.includes(helperName)) helperName += "_";
|
|
351
|
+
const absolutePath = resolve4(options.filePath);
|
|
352
|
+
const edits = [];
|
|
353
|
+
let order = 0;
|
|
354
|
+
const delta = {
|
|
355
|
+
functions: [],
|
|
356
|
+
callSites: [],
|
|
357
|
+
destructures: []
|
|
358
|
+
};
|
|
359
|
+
const collectContext = {
|
|
360
|
+
sourceFile,
|
|
361
|
+
mapLocation: options.mapLocation,
|
|
362
|
+
absolutePath
|
|
363
|
+
};
|
|
364
|
+
const visit = (node, caller) => {
|
|
365
|
+
if (isSupportedFunction(node)) {
|
|
366
|
+
const collected = collectWrappedFunction(node, sourceFile, absolutePath, options.mapLocation);
|
|
367
|
+
if (!collected) {
|
|
368
|
+
ts2.forEachChild(node, (child) => visit(child, caller));
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
delta.functions.push(collected);
|
|
372
|
+
const args = ts2.isArrowFunction(node) ? arrowArguments(node) : "arguments";
|
|
373
|
+
const asyncCallback = node.modifiers?.some(
|
|
374
|
+
(modifier) => modifier.kind === ts2.SyntaxKind.AsyncKeyword
|
|
375
|
+
) ? "async " : "";
|
|
376
|
+
if (ts2.isBlock(node.body)) {
|
|
377
|
+
edits.push({
|
|
378
|
+
position: node.body.getStart(sourceFile) + 1,
|
|
379
|
+
text: `return ${helperName}(${JSON.stringify(collected.sourceToken)},${args},${asyncCallback}()=>{`,
|
|
380
|
+
order: order++
|
|
381
|
+
});
|
|
382
|
+
edits.push({ position: node.body.end - 1, text: "});", order: order++ });
|
|
383
|
+
} else {
|
|
384
|
+
edits.push({
|
|
385
|
+
position: node.body.getStart(sourceFile),
|
|
386
|
+
text: `${helperName}(${JSON.stringify(collected.sourceToken)},${args},${asyncCallback}()=>(`,
|
|
387
|
+
order: order++
|
|
388
|
+
});
|
|
389
|
+
edits.push({ position: node.body.end, text: "))", order: order++ });
|
|
390
|
+
}
|
|
391
|
+
ts2.forEachChild(node, (child) => visit(child, collected));
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
if (caller && (ts2.isCallExpression(node) || ts2.isNewExpression(node))) {
|
|
395
|
+
const site = collectCallSite(node, caller, collectContext);
|
|
396
|
+
if (site) delta.callSites.push(site);
|
|
397
|
+
}
|
|
398
|
+
if (caller && ts2.isVariableDeclaration(node)) {
|
|
399
|
+
const destructure = collectParamDestructure(node, caller, collectContext);
|
|
400
|
+
if (destructure) delta.destructures.push(destructure);
|
|
401
|
+
}
|
|
402
|
+
ts2.forEachChild(node, (child) => visit(child, caller));
|
|
403
|
+
};
|
|
404
|
+
visit(sourceFile);
|
|
405
|
+
if (edits.length === 0) return void 0;
|
|
406
|
+
const shebangEnd = source.startsWith("#!") ? source.indexOf("\n") + 1 : 0;
|
|
407
|
+
edits.push({ position: shebangEnd, text: helperDeclaration(helperName), order: -1 });
|
|
408
|
+
edits.sort((left, right) => right.position - left.position || right.order - left.order);
|
|
409
|
+
let transformed = source;
|
|
410
|
+
for (const edit of edits) {
|
|
411
|
+
transformed = `${transformed.slice(0, edit.position)}${edit.text}${transformed.slice(edit.position)}`;
|
|
412
|
+
}
|
|
413
|
+
return { code: transformed, delta };
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
// src/auto-instrumentation/bun-plugin.ts
|
|
417
|
+
var loaderFor = (filePath) => {
|
|
418
|
+
if (/\.tsx$/i.test(filePath)) return "tsx";
|
|
419
|
+
if (/\.jsx$/i.test(filePath)) return "jsx";
|
|
420
|
+
if (/\.[cm]?ts$/i.test(filePath)) return "ts";
|
|
421
|
+
return "js";
|
|
422
|
+
};
|
|
423
|
+
var loadedSourceFilter = /^(?!.*[\\/](?:node_modules|dist|build|out|coverage|generated|\.git|\.next|\.svelte-kit|\.output|\.vercel|\.rasputin|\.turbo|\.yarn|\.pnpm|\.bun)[\\/]).*\.[cm]?[jt]sx?$/i;
|
|
424
|
+
var isLoadedApplicationSource = (filePath, options = {}) => isApplicationSource(filePath, { ...options, includeBuildOutput: false });
|
|
425
|
+
var loadSourceForInstrumentation = async (filePath, options = {}) => {
|
|
426
|
+
const absolutePath = resolve5(filePath);
|
|
427
|
+
const source = await readFile(absolutePath, "utf8");
|
|
428
|
+
if (!isLoadedApplicationSource(absolutePath, options)) {
|
|
429
|
+
return { contents: source, loader: loaderFor(absolutePath), transformed: false };
|
|
430
|
+
}
|
|
431
|
+
try {
|
|
432
|
+
const transformed = transformSource(source, { filePath: absolutePath });
|
|
433
|
+
if (transformed) {
|
|
434
|
+
recordInstrumentationManifestDelta(absolutePath, transformed.delta);
|
|
435
|
+
}
|
|
436
|
+
return {
|
|
437
|
+
contents: transformed?.code ?? source,
|
|
438
|
+
loader: loaderFor(absolutePath),
|
|
439
|
+
transformed: transformed !== void 0
|
|
440
|
+
};
|
|
441
|
+
} catch {
|
|
442
|
+
return { contents: source, loader: loaderFor(absolutePath), transformed: false };
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
var createRasputinBunPlugin = (options = {}) => {
|
|
446
|
+
const debug = options.debug ?? process.env.RASPUTIN_DEBUG === "1";
|
|
447
|
+
return {
|
|
448
|
+
name: "rasputin-automatic-execution-instrumentation",
|
|
449
|
+
setup(build) {
|
|
450
|
+
if (debug) {
|
|
451
|
+
console.info(
|
|
452
|
+
options.rootDir ? `[rasputin] automatic instrumentation active (source root: ${resolve5(options.rootDir)})` : "[rasputin] automatic instrumentation active (loaded local source)"
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
build.onLoad({ filter: loadedSourceFilter }, async ({ path }) => {
|
|
456
|
+
const absolutePath = resolve5(path);
|
|
457
|
+
const result = await loadSourceForInstrumentation(absolutePath, options);
|
|
458
|
+
if (debug && result.transformed) {
|
|
459
|
+
console.info(`[rasputin] instrumented ${absolutePath}`);
|
|
460
|
+
}
|
|
461
|
+
return { contents: result.contents, loader: result.loader };
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
// src/auto-instrumentation/sdk-source-boundary.ts
|
|
468
|
+
import { dirname, resolve as resolve6 } from "node:path";
|
|
469
|
+
import { fileURLToPath } from "node:url";
|
|
470
|
+
var sdkPackageScopeRootFrom = (instrumentEntryUrl) => resolve6(dirname(fileURLToPath(instrumentEntryUrl)), "../../..");
|
|
471
|
+
|
|
472
|
+
// src/instrument/bun.ts
|
|
473
|
+
plugin(
|
|
474
|
+
createRasputinBunPlugin({
|
|
475
|
+
excludedRoots: [sdkPackageScopeRootFrom(import.meta.url)]
|
|
476
|
+
})
|
|
477
|
+
);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../../src/instrument/node.ts"],"names":[],"mappings":""}
|