@rasputin-ai/node 0.2.0 → 0.4.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 +143 -16
- 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 +64 -0
- package/dist/auto-instrumentation/collect-instrumentation-delta.d.ts.map +1 -0
- package/dist/auto-instrumentation/format-configuration-report.d.ts +14 -0
- package/dist/auto-instrumentation/format-configuration-report.d.ts.map +1 -0
- package/dist/auto-instrumentation/instrumentation-manifest-registry.d.ts +17 -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 +26 -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 +21 -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 +75 -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 +10 -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 +1393 -5
- package/dist/instrument/bun.d.ts +2 -0
- package/dist/instrument/bun.d.ts.map +1 -0
- package/dist/instrument/bun.js +489 -0
- package/dist/instrument/node.d.ts +2 -0
- package/dist/instrument/node.d.ts.map +1 -0
- package/dist/instrument/node.js +544 -0
- package/dist/rasputin-init.d.ts +24 -4
- package/dist/rasputin-init.d.ts.map +1 -1
- package/dist/sdk-meta.d.ts +2 -2
- package/dist/sdk-meta.d.ts.map +1 -1
- package/package.json +15 -4
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
// src/auto-instrumentation/node-hooks.ts
|
|
2
|
+
import * as nodeModule from "node:module";
|
|
3
|
+
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
4
|
+
|
|
5
|
+
// src/auto-instrumentation/instrumentation-manifest-registry.ts
|
|
6
|
+
import { resolve } from "node:path";
|
|
7
|
+
import {
|
|
8
|
+
INSTRUMENTATION_MANIFEST_SCHEMA_VERSION,
|
|
9
|
+
sourceLocatorFromFile as sourceLocatorFromFile2
|
|
10
|
+
} from "@rasputin-ai/core";
|
|
11
|
+
|
|
12
|
+
// src/execution-recorder/automatic-runtime.ts
|
|
13
|
+
import {
|
|
14
|
+
runtimeFunctionId,
|
|
15
|
+
sourceLocatorFromFile
|
|
16
|
+
} from "@rasputin-ai/core";
|
|
17
|
+
var AUTOMATIC_RUNTIME_SYMBOL = "rasputin.execution.runtime.v2";
|
|
18
|
+
var AUTOMATIC_SOURCE_PREFIX = "rasputin-source-v2:";
|
|
19
|
+
var runtimeSymbol = Symbol.for(AUTOMATIC_RUNTIME_SYMBOL);
|
|
20
|
+
var encodeAutomaticFunctionSource = (source) => {
|
|
21
|
+
const locator = sourceLocatorFromFile(source.filePath);
|
|
22
|
+
const value = {
|
|
23
|
+
name: source.name,
|
|
24
|
+
...locator ? { source: { ...locator, line: source.line, column: source.column } } : {}
|
|
25
|
+
};
|
|
26
|
+
return `${AUTOMATIC_SOURCE_PREFIX}${JSON.stringify(value)}`;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// src/auto-instrumentation/instrumentation-manifest-registry.ts
|
|
30
|
+
var INSTRUMENTATION_MANIFEST_SYMBOL = "rasputin.instrumentation.manifest.v2";
|
|
31
|
+
var registrySymbol = Symbol.for(INSTRUMENTATION_MANIFEST_SYMBOL);
|
|
32
|
+
var getRegistry = () => {
|
|
33
|
+
const existing = Reflect.get(globalThis, registrySymbol);
|
|
34
|
+
if (existing?.__rasputinManifestRegistry && existing.files instanceof Map && typeof existing.generation === "number" && typeof existing.uploadedGeneration === "number") {
|
|
35
|
+
return existing;
|
|
36
|
+
}
|
|
37
|
+
const registry = {
|
|
38
|
+
__rasputinManifestRegistry: true,
|
|
39
|
+
files: /* @__PURE__ */ new Map(),
|
|
40
|
+
generation: 0,
|
|
41
|
+
uploadedGeneration: 0
|
|
42
|
+
};
|
|
43
|
+
Reflect.set(globalThis, registrySymbol, registry);
|
|
44
|
+
return registry;
|
|
45
|
+
};
|
|
46
|
+
var recordInstrumentationManifestDelta = (filePath, delta) => {
|
|
47
|
+
const registry = getRegistry();
|
|
48
|
+
registry.files.set(resolve(filePath), delta);
|
|
49
|
+
registry.generation += 1;
|
|
50
|
+
registry.onDirty?.();
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// src/auto-instrumentation/source-classification.ts
|
|
54
|
+
import { isAbsolute, relative, resolve as resolve2, sep } from "node:path";
|
|
55
|
+
var sourceExtension = /\.[cm]?[jt]sx?$/i;
|
|
56
|
+
var alwaysExcludedDirectory = /(^|[\\/])(node_modules|coverage|generated|\.git|\.next|\.svelte-kit|\.output|\.vercel|\.rasputin|\.turbo|\.yarn|\.pnpm|\.bun)([\\/]|$)/;
|
|
57
|
+
var buildOutputDirectory = /(^|[\\/])(dist|build|out)([\\/]|$)/;
|
|
58
|
+
var matches = (pattern, value) => {
|
|
59
|
+
pattern.lastIndex = 0;
|
|
60
|
+
const result = pattern.test(value);
|
|
61
|
+
pattern.lastIndex = 0;
|
|
62
|
+
return result;
|
|
63
|
+
};
|
|
64
|
+
var isWithinRoot = (filePath, root) => {
|
|
65
|
+
const fromRoot = relative(resolve2(root), filePath);
|
|
66
|
+
return fromRoot === "" || !fromRoot.startsWith(`..${sep}`) && fromRoot !== ".." && !isAbsolute(fromRoot);
|
|
67
|
+
};
|
|
68
|
+
var isApplicationSource = (filePath, options = {}) => {
|
|
69
|
+
const absolutePath = resolve2(filePath);
|
|
70
|
+
if (!sourceExtension.test(absolutePath) || alwaysExcludedDirectory.test(absolutePath))
|
|
71
|
+
return false;
|
|
72
|
+
if (options.includeBuildOutput !== true && buildOutputDirectory.test(absolutePath)) return false;
|
|
73
|
+
if (options.excludedRoots?.some((root) => isWithinRoot(absolutePath, root))) return false;
|
|
74
|
+
if (options.exclude && matches(options.exclude, absolutePath)) return false;
|
|
75
|
+
if (options.include && !matches(options.include, absolutePath)) return false;
|
|
76
|
+
if (options.rootDir) {
|
|
77
|
+
if (!isWithinRoot(absolutePath, options.rootDir)) return false;
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// src/auto-instrumentation/source-map-locations.ts
|
|
83
|
+
import { readFileSync } from "node:fs";
|
|
84
|
+
import { dirname, isAbsolute as isAbsolute2, join, resolve as resolve3 } from "node:path";
|
|
85
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
86
|
+
import { LEAST_UPPER_BOUND, originalPositionFor, TraceMap } from "@jridgewell/trace-mapping";
|
|
87
|
+
var sourceMapComment = /(?:\/\/[#@][ \t]*sourceMappingURL=([^\s'"]+)|\/\*[#@][ \t]*sourceMappingURL=([^\s*'"]+)[ \t]*\*\/)\s*$/;
|
|
88
|
+
var decodeDataUrl = (url) => {
|
|
89
|
+
const base64 = /^data:application\/json(?:;charset=[^;,]+)?;base64,(.+)$/i.exec(url);
|
|
90
|
+
if (base64?.[1]) return Buffer.from(base64[1], "base64").toString("utf8");
|
|
91
|
+
const raw = /^data:application\/json(?:;charset=[^;,]+)?,(.+)$/i.exec(url);
|
|
92
|
+
return raw?.[1] ? decodeURIComponent(raw[1]) : void 0;
|
|
93
|
+
};
|
|
94
|
+
var sourceMapFor = (generatedPath, source) => {
|
|
95
|
+
const match = sourceMapComment.exec(source);
|
|
96
|
+
const reference = match?.[1] ?? match?.[2];
|
|
97
|
+
if (reference?.startsWith("data:")) {
|
|
98
|
+
const payload = decodeDataUrl(reference);
|
|
99
|
+
return payload ? { payload, mapUrl: pathToFileURL(generatedPath).href } : void 0;
|
|
100
|
+
}
|
|
101
|
+
const mapPath = reference ? resolve3(dirname(generatedPath), reference) : `${generatedPath}.map`;
|
|
102
|
+
return {
|
|
103
|
+
payload: readFileSync(mapPath, "utf8"),
|
|
104
|
+
mapUrl: pathToFileURL(mapPath).href
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
var toFilePath = (source, generatedPath) => {
|
|
108
|
+
if (source.startsWith("file:")) return fileURLToPath(source);
|
|
109
|
+
return isAbsolute2(source) ? source : join(dirname(generatedPath), source);
|
|
110
|
+
};
|
|
111
|
+
var createSourceMapLocationMapper = (generatedPath, source) => {
|
|
112
|
+
try {
|
|
113
|
+
const sourceMap = sourceMapFor(generatedPath, source);
|
|
114
|
+
if (!sourceMap) return void 0;
|
|
115
|
+
const traceMap = new TraceMap(sourceMap.payload, sourceMap.mapUrl);
|
|
116
|
+
return (generated) => {
|
|
117
|
+
let original = originalPositionFor(traceMap, {
|
|
118
|
+
line: generated.line,
|
|
119
|
+
column: Math.max(0, generated.column - 1)
|
|
120
|
+
});
|
|
121
|
+
if (!original.source || original.line == null) {
|
|
122
|
+
original = originalPositionFor(traceMap, {
|
|
123
|
+
line: generated.line,
|
|
124
|
+
column: 0,
|
|
125
|
+
bias: LEAST_UPPER_BOUND
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
if (!original.source || original.line == null) return void 0;
|
|
129
|
+
return {
|
|
130
|
+
filePath: toFilePath(original.source, generatedPath),
|
|
131
|
+
line: original.line,
|
|
132
|
+
column: (original.column ?? 0) + 1
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
} catch {
|
|
136
|
+
return void 0;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// src/auto-instrumentation/transform-source.ts
|
|
141
|
+
import { resolve as resolve4 } from "node:path";
|
|
142
|
+
import ts2 from "typescript";
|
|
143
|
+
|
|
144
|
+
// src/auto-instrumentation/collect-instrumentation-delta.ts
|
|
145
|
+
import ts from "typescript";
|
|
146
|
+
var paramBindingFromName = (name, sourceFile) => {
|
|
147
|
+
if (ts.isIdentifier(name)) return { kind: "identifier", name: name.text };
|
|
148
|
+
if (ts.isArrayBindingPattern(name)) {
|
|
149
|
+
const elements = name.elements.map(
|
|
150
|
+
(element) => {
|
|
151
|
+
if (ts.isOmittedExpression(element)) return { kind: "omitted" };
|
|
152
|
+
const binding = paramBindingFromName(element.name, sourceFile);
|
|
153
|
+
if (element.dotDotDotToken) return { kind: "rest", binding };
|
|
154
|
+
return binding;
|
|
155
|
+
}
|
|
156
|
+
);
|
|
157
|
+
return { kind: "array", elements };
|
|
158
|
+
}
|
|
159
|
+
const properties = name.elements.map((element) => {
|
|
160
|
+
const binding = paramBindingFromName(element.name, sourceFile);
|
|
161
|
+
const key = element.propertyName ? element.propertyName.getText(sourceFile).replace(/^['"]|['"]$/g, "") : ts.isIdentifier(element.name) ? element.name.text : element.name.getText(sourceFile);
|
|
162
|
+
if (element.dotDotDotToken) {
|
|
163
|
+
const rest = { kind: "rest", binding };
|
|
164
|
+
return { key, binding: rest };
|
|
165
|
+
}
|
|
166
|
+
return { key, binding };
|
|
167
|
+
});
|
|
168
|
+
return { kind: "object", properties };
|
|
169
|
+
};
|
|
170
|
+
var collectFunctionParams = (node, sourceFile) => {
|
|
171
|
+
const params = [];
|
|
172
|
+
for (const parameter of node.parameters) {
|
|
173
|
+
if (ts.isIdentifier(parameter.name) && parameter.name.text === "this") continue;
|
|
174
|
+
const binding = paramBindingFromName(parameter.name, sourceFile);
|
|
175
|
+
params.push(parameter.dotDotDotToken ? { kind: "rest", binding } : binding);
|
|
176
|
+
}
|
|
177
|
+
return { params, paramNames: identifierNames(params) };
|
|
178
|
+
};
|
|
179
|
+
var identifierNames = (bindings) => {
|
|
180
|
+
const names = /* @__PURE__ */ new Set();
|
|
181
|
+
const walk = (node) => {
|
|
182
|
+
if (node.kind === "identifier") {
|
|
183
|
+
names.add(node.name);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (node.kind === "rest") {
|
|
187
|
+
walk(node.binding);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (node.kind === "object") {
|
|
191
|
+
for (const property of node.properties) walk(property.binding);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (node.kind === "array") {
|
|
195
|
+
for (const element of node.elements) {
|
|
196
|
+
if (element.kind !== "omitted") walk(element);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
for (const item of bindings) walk(item);
|
|
201
|
+
return names;
|
|
202
|
+
};
|
|
203
|
+
var mappedLocation = (sourceFile, position, absolutePath, mapLocation) => {
|
|
204
|
+
const generated = sourceFile.getLineAndCharacterOfPosition(position);
|
|
205
|
+
const raw = {
|
|
206
|
+
filePath: absolutePath,
|
|
207
|
+
line: generated.line + 1,
|
|
208
|
+
column: generated.character + 1
|
|
209
|
+
};
|
|
210
|
+
return mapLocation ? mapLocation(raw) : raw;
|
|
211
|
+
};
|
|
212
|
+
var unwrapExpression = (expression) => {
|
|
213
|
+
let current = expression;
|
|
214
|
+
while (ts.isParenthesizedExpression(current) || ts.isNonNullExpression(current)) {
|
|
215
|
+
current = current.expression;
|
|
216
|
+
}
|
|
217
|
+
if (ts.isAwaitExpression(current)) return unwrapExpression(current.expression);
|
|
218
|
+
return current;
|
|
219
|
+
};
|
|
220
|
+
var calleeFromExpression = (expression, sourceFile) => {
|
|
221
|
+
const current = unwrapExpression(expression);
|
|
222
|
+
if (ts.isIdentifier(current)) {
|
|
223
|
+
return { calleeName: current.text, calleeText: current.text };
|
|
224
|
+
}
|
|
225
|
+
if (ts.isPropertyAccessExpression(current) && ts.isIdentifier(current.name)) {
|
|
226
|
+
return {
|
|
227
|
+
calleeName: current.name.text,
|
|
228
|
+
calleeText: current.getText(sourceFile).replace(/\s+/g, "")
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
return null;
|
|
232
|
+
};
|
|
233
|
+
var resultBindingForCall = (call) => {
|
|
234
|
+
let node = call.parent;
|
|
235
|
+
if (ts.isAwaitExpression(node)) node = node.parent;
|
|
236
|
+
if (!ts.isVariableDeclaration(node)) return null;
|
|
237
|
+
if (!ts.isIdentifier(node.name)) return null;
|
|
238
|
+
const initializer = node.initializer;
|
|
239
|
+
if (!initializer) return null;
|
|
240
|
+
const inner = unwrapExpression(initializer);
|
|
241
|
+
if (inner !== call) return null;
|
|
242
|
+
return node.name.text;
|
|
243
|
+
};
|
|
244
|
+
var argIdentifiersForCall = (call) => {
|
|
245
|
+
const identifiers = [];
|
|
246
|
+
call.arguments?.forEach((argument, index) => {
|
|
247
|
+
if (ts.isIdentifier(argument)) {
|
|
248
|
+
identifiers.push({ index, kind: "identifier", name: argument.text });
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (!ts.isObjectLiteralExpression(argument)) return;
|
|
252
|
+
const properties = [];
|
|
253
|
+
for (const property of argument.properties) {
|
|
254
|
+
if (ts.isShorthandPropertyAssignment(property)) {
|
|
255
|
+
properties.push({ key: property.name.text, name: property.name.text });
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
if (ts.isPropertyAssignment(property) && ts.isIdentifier(property.name) && ts.isIdentifier(property.initializer)) {
|
|
259
|
+
properties.push({ key: property.name.text, name: property.initializer.text });
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (properties.length > 0) {
|
|
263
|
+
identifiers.push({ index, kind: "object", properties });
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
return identifiers;
|
|
267
|
+
};
|
|
268
|
+
var collectCallSite = (call, caller, context) => {
|
|
269
|
+
const callee = calleeFromExpression(call.expression, context.sourceFile);
|
|
270
|
+
if (!callee) return null;
|
|
271
|
+
const start = mappedLocation(
|
|
272
|
+
context.sourceFile,
|
|
273
|
+
call.getStart(context.sourceFile),
|
|
274
|
+
context.absolutePath,
|
|
275
|
+
context.mapLocation
|
|
276
|
+
);
|
|
277
|
+
const end = mappedLocation(
|
|
278
|
+
context.sourceFile,
|
|
279
|
+
call.end,
|
|
280
|
+
context.absolutePath,
|
|
281
|
+
context.mapLocation
|
|
282
|
+
);
|
|
283
|
+
if (!start || !end) return null;
|
|
284
|
+
return {
|
|
285
|
+
callerSourceToken: caller.sourceToken,
|
|
286
|
+
calleeName: callee.calleeName,
|
|
287
|
+
calleeText: callee.calleeText,
|
|
288
|
+
filePath: start.filePath,
|
|
289
|
+
line: start.line,
|
|
290
|
+
column: start.column,
|
|
291
|
+
endLine: end.line,
|
|
292
|
+
endColumn: end.column,
|
|
293
|
+
resultBinding: resultBindingForCall(call),
|
|
294
|
+
argIdentifiers: argIdentifiersForCall(call)
|
|
295
|
+
};
|
|
296
|
+
};
|
|
297
|
+
var collectParamDestructure = (declaration, caller, context) => {
|
|
298
|
+
if (!declaration.initializer || !ts.isIdentifier(declaration.initializer)) return null;
|
|
299
|
+
if (!ts.isObjectBindingPattern(declaration.name) && !ts.isArrayBindingPattern(declaration.name)) {
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
const sourceParam = declaration.initializer.text;
|
|
303
|
+
if (!caller.paramNames.has(sourceParam)) return null;
|
|
304
|
+
const start = mappedLocation(
|
|
305
|
+
context.sourceFile,
|
|
306
|
+
declaration.getStart(context.sourceFile),
|
|
307
|
+
context.absolutePath,
|
|
308
|
+
context.mapLocation
|
|
309
|
+
);
|
|
310
|
+
if (!start) return null;
|
|
311
|
+
return {
|
|
312
|
+
callerSourceToken: caller.sourceToken,
|
|
313
|
+
sourceParam,
|
|
314
|
+
filePath: start.filePath,
|
|
315
|
+
line: start.line,
|
|
316
|
+
column: start.column,
|
|
317
|
+
binding: paramBindingFromName(declaration.name, context.sourceFile)
|
|
318
|
+
};
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
// src/auto-instrumentation/transform-source.ts
|
|
322
|
+
var scriptKindFor = (filePath) => {
|
|
323
|
+
if (/\.tsx$/i.test(filePath)) return ts2.ScriptKind.TSX;
|
|
324
|
+
if (/\.jsx$/i.test(filePath)) return ts2.ScriptKind.JSX;
|
|
325
|
+
if (/\.[cm]?ts$/i.test(filePath)) return ts2.ScriptKind.TS;
|
|
326
|
+
return ts2.ScriptKind.JS;
|
|
327
|
+
};
|
|
328
|
+
var functionName = (node, sourceFile) => {
|
|
329
|
+
if (node.name) return node.name.getText(sourceFile).replace(/^['"]|['"]$/g, "");
|
|
330
|
+
const parent = node.parent;
|
|
331
|
+
if (ts2.isVariableDeclaration(parent) && ts2.isIdentifier(parent.name)) return parent.name.text;
|
|
332
|
+
if (ts2.isPropertyAssignment(parent)) return parent.name.getText(sourceFile);
|
|
333
|
+
if (ts2.isPropertyDeclaration(parent) && parent.name) return parent.name.getText(sourceFile);
|
|
334
|
+
const location = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
|
|
335
|
+
return `anonymous@${location.line + 1}:${location.character + 1}`;
|
|
336
|
+
};
|
|
337
|
+
var functionLocationPosition = (node, sourceFile) => {
|
|
338
|
+
if (node.name) return node.name.getStart(sourceFile);
|
|
339
|
+
const parent = node.parent;
|
|
340
|
+
if ((ts2.isVariableDeclaration(parent) || ts2.isPropertyAssignment(parent)) && parent.name) {
|
|
341
|
+
return parent.name.getStart(sourceFile);
|
|
342
|
+
}
|
|
343
|
+
return node.getStart(sourceFile);
|
|
344
|
+
};
|
|
345
|
+
var bindingValue = (name, sourceFile) => {
|
|
346
|
+
if (ts2.isIdentifier(name)) return name.text;
|
|
347
|
+
if (ts2.isArrayBindingPattern(name)) {
|
|
348
|
+
const values = name.elements.map((element) => {
|
|
349
|
+
if (ts2.isOmittedExpression(element)) return "";
|
|
350
|
+
const value = bindingValue(element.name, sourceFile);
|
|
351
|
+
return element.dotDotDotToken ? `...${value}` : value;
|
|
352
|
+
});
|
|
353
|
+
return `[${values.join(",")}]`;
|
|
354
|
+
}
|
|
355
|
+
const properties = name.elements.map((element) => {
|
|
356
|
+
const value = bindingValue(element.name, sourceFile);
|
|
357
|
+
if (element.dotDotDotToken) return `...${value}`;
|
|
358
|
+
if (element.propertyName) return `${element.propertyName.getText(sourceFile)}:${value}`;
|
|
359
|
+
return value;
|
|
360
|
+
});
|
|
361
|
+
return `{${properties.join(",")}}`;
|
|
362
|
+
};
|
|
363
|
+
var arrowArguments = (node) => {
|
|
364
|
+
const values = node.parameters.flatMap((parameter) => {
|
|
365
|
+
if (ts2.isIdentifier(parameter.name) && parameter.name.text === "this") return [];
|
|
366
|
+
const value = bindingValue(parameter.name, node.getSourceFile());
|
|
367
|
+
return [parameter.dotDotDotToken ? `...${value}` : value];
|
|
368
|
+
});
|
|
369
|
+
return `[${values.join(",")}]`;
|
|
370
|
+
};
|
|
371
|
+
var isSupportedFunction = (node) => {
|
|
372
|
+
if (!ts2.isFunctionDeclaration(node) && !ts2.isFunctionExpression(node) && !ts2.isArrowFunction(node) && !ts2.isMethodDeclaration(node) && !ts2.isGetAccessorDeclaration(node) && !ts2.isSetAccessorDeclaration(node)) {
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
if (!node.body || "asteriskToken" in node && node.asteriskToken) return false;
|
|
376
|
+
return true;
|
|
377
|
+
};
|
|
378
|
+
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()};`;
|
|
379
|
+
var collectWrappedFunction = (node, sourceFile, absolutePath, mapLocation) => {
|
|
380
|
+
const start = mappedLocation(
|
|
381
|
+
sourceFile,
|
|
382
|
+
functionLocationPosition(node, sourceFile),
|
|
383
|
+
absolutePath,
|
|
384
|
+
mapLocation
|
|
385
|
+
);
|
|
386
|
+
if (!start) return void 0;
|
|
387
|
+
const end = mappedLocation(sourceFile, node.end, absolutePath, mapLocation) ?? start;
|
|
388
|
+
const name = functionName(node, sourceFile).replace(/@\d+:\d+$/, "");
|
|
389
|
+
const { params, paramNames } = collectFunctionParams(node, sourceFile);
|
|
390
|
+
return {
|
|
391
|
+
sourceToken: encodeAutomaticFunctionSource({
|
|
392
|
+
filePath: start.filePath,
|
|
393
|
+
name,
|
|
394
|
+
line: start.line,
|
|
395
|
+
column: start.column
|
|
396
|
+
}),
|
|
397
|
+
filePath: start.filePath,
|
|
398
|
+
name,
|
|
399
|
+
startLine: start.line,
|
|
400
|
+
startColumn: start.column,
|
|
401
|
+
endLine: end.line,
|
|
402
|
+
endColumn: end.column,
|
|
403
|
+
params,
|
|
404
|
+
paramNames
|
|
405
|
+
};
|
|
406
|
+
};
|
|
407
|
+
var transformSource = (source, options) => {
|
|
408
|
+
if (source.includes("@rasputin-ignore-file")) return void 0;
|
|
409
|
+
const sourceFile = ts2.createSourceFile(
|
|
410
|
+
options.filePath,
|
|
411
|
+
source,
|
|
412
|
+
ts2.ScriptTarget.Latest,
|
|
413
|
+
true,
|
|
414
|
+
scriptKindFor(options.filePath)
|
|
415
|
+
);
|
|
416
|
+
let helperName = "__rasputinAutoRun__";
|
|
417
|
+
while (source.includes(helperName)) helperName += "_";
|
|
418
|
+
const absolutePath = resolve4(options.filePath);
|
|
419
|
+
const edits = [];
|
|
420
|
+
let order = 0;
|
|
421
|
+
const delta = {
|
|
422
|
+
functions: [],
|
|
423
|
+
callSites: [],
|
|
424
|
+
destructures: []
|
|
425
|
+
};
|
|
426
|
+
const collectContext = {
|
|
427
|
+
sourceFile,
|
|
428
|
+
mapLocation: options.mapLocation,
|
|
429
|
+
absolutePath
|
|
430
|
+
};
|
|
431
|
+
const visit = (node, caller) => {
|
|
432
|
+
if (isSupportedFunction(node)) {
|
|
433
|
+
const collected = collectWrappedFunction(node, sourceFile, absolutePath, options.mapLocation);
|
|
434
|
+
if (!collected) {
|
|
435
|
+
ts2.forEachChild(node, (child) => visit(child, caller));
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
delta.functions.push(collected);
|
|
439
|
+
const args = ts2.isArrowFunction(node) ? arrowArguments(node) : "arguments";
|
|
440
|
+
const asyncCallback = node.modifiers?.some(
|
|
441
|
+
(modifier) => modifier.kind === ts2.SyntaxKind.AsyncKeyword
|
|
442
|
+
) ? "async " : "";
|
|
443
|
+
if (ts2.isBlock(node.body)) {
|
|
444
|
+
edits.push({
|
|
445
|
+
position: node.body.getStart(sourceFile) + 1,
|
|
446
|
+
text: `return ${helperName}(${JSON.stringify(collected.sourceToken)},${args},${asyncCallback}()=>{`,
|
|
447
|
+
order: order++
|
|
448
|
+
});
|
|
449
|
+
edits.push({ position: node.body.end - 1, text: "});", order: order++ });
|
|
450
|
+
} else {
|
|
451
|
+
edits.push({
|
|
452
|
+
position: node.body.getStart(sourceFile),
|
|
453
|
+
text: `${helperName}(${JSON.stringify(collected.sourceToken)},${args},${asyncCallback}()=>(`,
|
|
454
|
+
order: order++
|
|
455
|
+
});
|
|
456
|
+
edits.push({ position: node.body.end, text: "))", order: order++ });
|
|
457
|
+
}
|
|
458
|
+
ts2.forEachChild(node, (child) => visit(child, collected));
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
if (caller && (ts2.isCallExpression(node) || ts2.isNewExpression(node))) {
|
|
462
|
+
const site = collectCallSite(node, caller, collectContext);
|
|
463
|
+
if (site) delta.callSites.push(site);
|
|
464
|
+
}
|
|
465
|
+
if (caller && ts2.isVariableDeclaration(node)) {
|
|
466
|
+
const destructure = collectParamDestructure(node, caller, collectContext);
|
|
467
|
+
if (destructure) delta.destructures.push(destructure);
|
|
468
|
+
}
|
|
469
|
+
ts2.forEachChild(node, (child) => visit(child, caller));
|
|
470
|
+
};
|
|
471
|
+
visit(sourceFile);
|
|
472
|
+
if (edits.length === 0) return void 0;
|
|
473
|
+
const shebangEnd = source.startsWith("#!") ? source.indexOf("\n") + 1 : 0;
|
|
474
|
+
edits.push({ position: shebangEnd, text: helperDeclaration(helperName), order: -1 });
|
|
475
|
+
edits.sort((left, right) => right.position - left.position || right.order - left.order);
|
|
476
|
+
let transformed = source;
|
|
477
|
+
for (const edit of edits) {
|
|
478
|
+
transformed = `${transformed.slice(0, edit.position)}${edit.text}${transformed.slice(edit.position)}`;
|
|
479
|
+
}
|
|
480
|
+
return { code: transformed, delta };
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
// src/auto-instrumentation/node-hooks.ts
|
|
484
|
+
var supportedFormat = /^(?:module|commonjs)(?:-typescript)?$/;
|
|
485
|
+
var registerHooks2 = nodeModule.registerHooks;
|
|
486
|
+
var sourceText = (source) => {
|
|
487
|
+
if (typeof source === "string") return source;
|
|
488
|
+
if (source instanceof ArrayBuffer) return Buffer.from(source).toString("utf8");
|
|
489
|
+
if (ArrayBuffer.isView(source)) {
|
|
490
|
+
return Buffer.from(source.buffer, source.byteOffset, source.byteLength).toString("utf8");
|
|
491
|
+
}
|
|
492
|
+
return void 0;
|
|
493
|
+
};
|
|
494
|
+
var registerRasputinNodeHooks = (options = {}) => {
|
|
495
|
+
if (!registerHooks2) {
|
|
496
|
+
console.warn(
|
|
497
|
+
"[rasputin] Automatic instrumentation is disabled because it requires Node.js 22.15.0 or newer."
|
|
498
|
+
);
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
const debug = process.env.RASPUTIN_DEBUG === "1";
|
|
502
|
+
registerHooks2({
|
|
503
|
+
load(url, context, nextLoad) {
|
|
504
|
+
const result = nextLoad(url, context);
|
|
505
|
+
const format = result.format ?? context.format;
|
|
506
|
+
if (!url.startsWith("file:") || !format || !supportedFormat.test(format)) return result;
|
|
507
|
+
let filePath;
|
|
508
|
+
try {
|
|
509
|
+
filePath = fileURLToPath2(url);
|
|
510
|
+
} catch {
|
|
511
|
+
return result;
|
|
512
|
+
}
|
|
513
|
+
if (!isApplicationSource(filePath, {
|
|
514
|
+
includeBuildOutput: true,
|
|
515
|
+
excludedRoots: options.excludedRoots
|
|
516
|
+
}))
|
|
517
|
+
return result;
|
|
518
|
+
const source = sourceText(result.source);
|
|
519
|
+
if (source === void 0) return result;
|
|
520
|
+
try {
|
|
521
|
+
const transformed = transformSource(source, {
|
|
522
|
+
filePath,
|
|
523
|
+
mapLocation: createSourceMapLocationMapper(filePath, source)
|
|
524
|
+
});
|
|
525
|
+
if (!transformed) return result;
|
|
526
|
+
recordInstrumentationManifestDelta(filePath, transformed.delta);
|
|
527
|
+
if (debug) console.info(`[rasputin] instrumented ${filePath}`);
|
|
528
|
+
return { ...result, source: transformed.code };
|
|
529
|
+
} catch {
|
|
530
|
+
return result;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
// src/auto-instrumentation/sdk-source-boundary.ts
|
|
537
|
+
import { dirname as dirname2, resolve as resolve5 } from "node:path";
|
|
538
|
+
import { fileURLToPath as fileURLToPath3 } from "node:url";
|
|
539
|
+
var sdkPackageScopeRootFrom = (instrumentEntryUrl) => resolve5(dirname2(fileURLToPath3(instrumentEntryUrl)), "../../..");
|
|
540
|
+
|
|
541
|
+
// src/instrument/node.ts
|
|
542
|
+
registerRasputinNodeHooks({
|
|
543
|
+
excludedRoots: [sdkPackageScopeRootFrom(import.meta.url)]
|
|
544
|
+
});
|
package/dist/rasputin-init.d.ts
CHANGED
|
@@ -1,4 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Node/Bun composition root for error transport, process handlers, and the bounded execution
|
|
3
|
+
* recorder. Centralizing this wiring keeps initialization fail-safe and ensures every capture path
|
|
4
|
+
* attaches runtime state using the same limits and lifecycle rules.
|
|
5
|
+
*/
|
|
6
|
+
import { type RasputinClient, type RasputinClientStats, type RasputinOptions, type SourceVerification } from '@rasputin-ai/core';
|
|
7
|
+
import { type ExecutionRecorderOptions, type ExecutionRecorderStats, type RasputinExecution } from './execution-recorder';
|
|
8
|
+
export type RasputinNodeOptions = RasputinOptions & {
|
|
9
|
+
/** Bounded runtime-state capture. Enabled by default; successful executions are discarded. */
|
|
10
|
+
executionRecorder?: ExecutionRecorderOptions;
|
|
11
|
+
};
|
|
12
|
+
export type RasputinNodeClientStats = RasputinClientStats & {
|
|
13
|
+
/** Process-local recorder safety, truncation, and memory-pressure counters. */
|
|
14
|
+
recorder: ExecutionRecorderStats;
|
|
15
|
+
};
|
|
16
|
+
export type RasputinNodeClient = Omit<RasputinClient, 'getStats'> & {
|
|
17
|
+
execution: RasputinExecution;
|
|
18
|
+
getStats: () => RasputinNodeClientStats;
|
|
19
|
+
/** Verify package source locators against the configured Git repository and exact release. */
|
|
20
|
+
verifyConfiguration: () => Promise<SourceVerification>;
|
|
21
|
+
};
|
|
2
22
|
/**
|
|
3
23
|
* Boot the Node/Bun SDK: client + process global handlers.
|
|
4
24
|
* Call once at process start. Never throws into the host app.
|
|
@@ -6,10 +26,10 @@ import { type RasputinClient, type RasputinOptions } from '@rasputin-ai/core';
|
|
|
6
26
|
* @example
|
|
7
27
|
* RasputinInit({
|
|
8
28
|
* projectApiKey: process.env.RASPUTIN_PROJECT_API_KEY!,
|
|
29
|
+
* release: process.env.RASPUTIN_RELEASE!,
|
|
9
30
|
* environment: process.env.NODE_ENV ?? 'development',
|
|
10
|
-
*
|
|
11
|
-
* moduleUrl: import.meta.url,
|
|
31
|
+
* enabled: ['staging', 'production'].includes(process.env.NODE_ENV ?? 'development'),
|
|
12
32
|
* });
|
|
13
33
|
*/
|
|
14
|
-
export declare const RasputinInit: (options:
|
|
34
|
+
export declare const RasputinInit: (options: RasputinNodeOptions) => RasputinNodeClient;
|
|
15
35
|
//# sourceMappingURL=rasputin-init.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rasputin-init.d.ts","sourceRoot":"","sources":["../src/rasputin-init.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"rasputin-init.d.ts","sourceRoot":"","sources":["../src/rasputin-init.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAIN,KAAK,cAAc,EACnB,KAAK,mBAAmB,EACxB,KAAK,eAAe,EACpB,KAAK,kBAAkB,EACvB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAEN,KAAK,wBAAwB,EAC7B,KAAK,sBAAsB,EAC3B,KAAK,iBAAiB,EACtB,MAAM,sBAAsB,CAAC;AAK9B,MAAM,MAAM,mBAAmB,GAAG,eAAe,GAAG;IACnD,8FAA8F;IAC9F,iBAAiB,CAAC,EAAE,wBAAwB,CAAC;CAC7C,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG,mBAAmB,GAAG;IAC3D,+EAA+E;IAC/E,QAAQ,EAAE,sBAAsB,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG,IAAI,CAAC,cAAc,EAAE,UAAU,CAAC,GAAG;IACnE,SAAS,EAAE,iBAAiB,CAAC;IAC7B,QAAQ,EAAE,MAAM,uBAAuB,CAAC;IACxC,8FAA8F;IAC9F,mBAAmB,EAAE,MAAM,OAAO,CAAC,kBAAkB,CAAC,CAAC;CACvD,CAAC;AAgDF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,YAAY,GAAI,SAAS,mBAAmB,KAAG,kBA2C3D,CAAC"}
|
package/dist/sdk-meta.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** Generated by
|
|
1
|
+
/** Generated by scripts/generate-sdk-meta.ts — do not edit. */
|
|
2
2
|
export declare const SDK_NAME = "@rasputin-ai/node";
|
|
3
|
-
export declare const SDK_VERSION = "0.
|
|
3
|
+
export declare const SDK_VERSION = "0.4.0";
|
|
4
4
|
//# sourceMappingURL=sdk-meta.d.ts.map
|
package/dist/sdk-meta.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sdk-meta.d.ts","sourceRoot":"","sources":["../src/sdk-meta.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"sdk-meta.d.ts","sourceRoot":"","sources":["../src/sdk-meta.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,eAAO,MAAM,QAAQ,sBAAsB,CAAC;AAC5C,eAAO,MAAM,WAAW,UAAU,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rasputin-ai/node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Official Rasputin AI SDK for Node and Bun.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -8,6 +8,16 @@
|
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"import": "./dist/index.js",
|
|
10
10
|
"default": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./instrument/bun": {
|
|
13
|
+
"types": "./dist/instrument/bun.d.ts",
|
|
14
|
+
"import": "./dist/instrument/bun.js",
|
|
15
|
+
"default": "./dist/instrument/bun.js"
|
|
16
|
+
},
|
|
17
|
+
"./instrument/node": {
|
|
18
|
+
"types": "./dist/instrument/node.d.ts",
|
|
19
|
+
"import": "./dist/instrument/node.js",
|
|
20
|
+
"default": "./dist/instrument/node.js"
|
|
11
21
|
}
|
|
12
22
|
},
|
|
13
23
|
"files": [
|
|
@@ -25,13 +35,14 @@
|
|
|
25
35
|
"access": "public"
|
|
26
36
|
},
|
|
27
37
|
"dependencies": {
|
|
28
|
-
"@
|
|
38
|
+
"@jridgewell/trace-mapping": "0.3.31",
|
|
39
|
+
"@rasputin-ai/core": "0.4.0",
|
|
40
|
+
"typescript": "5"
|
|
29
41
|
},
|
|
30
42
|
"devDependencies": {
|
|
31
43
|
"@rasputin-ai/core": "workspace:*",
|
|
32
44
|
"@types/bun": "latest",
|
|
33
45
|
"@types/node": "22.13.10",
|
|
34
|
-
"esbuild": "0.25.12"
|
|
35
|
-
"typescript": "5"
|
|
46
|
+
"esbuild": "0.25.12"
|
|
36
47
|
}
|
|
37
48
|
}
|