@rasputin-ai/elysia 0.5.0-alpha.13 → 0.5.0-alpha.14

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 CHANGED
@@ -81,7 +81,11 @@ The Elysia plugin scopes each HTTP request as an execution automatically. For ba
81
81
 
82
82
  ## Automatic instrumentation
83
83
 
84
- Build-time instrumentation is the production default. Transform compiled unbundled JavaScript after `tsc` so the running process never loads the TypeScript compiler:
84
+ Rasputin wraps your functions so it can show what ran when an error happened.
85
+
86
+ **Compiled JavaScript (use this in production)**
87
+
88
+ Build as you already do, then run `rasputin-instrument` on the output. Start the process normally — no extra flags:
85
89
 
86
90
  ```json
87
91
  {
@@ -93,29 +97,56 @@ Build-time instrumentation is the production default. Transform compiled unbundl
93
97
  }
94
98
  ```
95
99
 
96
- Node `--import` and Bun `--preload` remain compatibility and development fallbacks. They transform application source as it loads, which pulls compiler infrastructure into the process and has materially higher RSS. Do not use those loader measurements in low-overhead comparisons. In `NODE_ENV=production` the loaders print a one-time warning recommending `rasputin-instrument`. Set `RASPUTIN_SUPPRESS_RUNTIME_TRANSFORM_WARNING=1` to hide it.
100
+ Have TypeScript write `.js.map` files next to the output so recorded functions still point at your `.ts` sources. The instrumenter writes `.rasputin/instrumentation-manifest.json` beside `dist`, and `RasputinInit` loads it automatically.
101
+
102
+ **Running TypeScript directly**
103
+
104
+ `rasputin-instrument` rewrites compiled `.js` files on disk. It does not run when you start TypeScript with Bun (`bun src/app.ts`, `bun run src/app.ts`, and similar). Use `--preload` so Bun wraps files as they load:
97
105
 
98
106
  ```json
99
107
  {
100
108
  "scripts": {
101
- "start:node": "node --import @rasputin-ai/elysia/instrument/node dist/app.js",
102
109
  "start:bun": "bun --preload @rasputin-ai/elysia/instrument/bun src/app.ts"
103
110
  }
104
111
  }
105
112
  ```
106
113
 
107
- Runtime state is retained only inside an active execution and attached when that execution throws.
114
+ On Node you can wrap compiled files as they load instead of instrumenting at build time:
115
+
116
+ ```json
117
+ {
118
+ "scripts": {
119
+ "start:node": "node --import @rasputin-ai/elysia/instrument/node dist/app.js"
120
+ }
121
+ }
122
+ ```
123
+
124
+ Use `--preload` / `--import` while you run TypeScript locally. In production, instrument at build time instead — wrapping files at startup is slower and uses more memory.
125
+
126
+ Runtime state is kept only while a request or job is running, and is attached when that work throws.
108
127
 
109
128
  ## Manual capture
110
129
 
111
130
  For background jobs, scheduled tasks, or worker iterations, define an execution:
112
131
 
113
132
  ```ts
114
- await client.execution.run({ kind: 'job', name: 'sync-invoices' }, async () => {
115
- await syncInvoices();
116
- });
133
+ const scope = client.execution.createScope({
134
+ kind: 'job',
135
+ name: 'sync-invoices',
136
+ })
137
+
138
+ try {
139
+ return await scope.run(() => syncInvoices())
140
+ } catch (error) {
141
+ scope.associateError(error)
142
+ throw error
143
+ } finally {
144
+ scope.finish()
145
+ }
117
146
  ```
118
147
 
148
+ `scope.run()` executes the callback under this recording's async context and returns exactly what the callback returned. It does not wrap, replace, or observe Promises.
149
+
119
150
  When automatic instrumentation is unavailable, mark individual functions explicitly:
120
151
 
121
152
  ```ts
@@ -131,14 +162,16 @@ const syncInvoices = client.execution.trace(
131
162
 
132
163
  | Environment | Support |
133
164
  | --- | --- |
134
- | TypeScript compiled to multiple JavaScript files | Preferred: `rasputin-instrument dist` after `tsc`, with adjacent source maps |
135
- | Elysia 1.4 or newer within 1.x on Bun 1.3.x | Supported for directly loaded JavaScript and TypeScript using `instrument/bun` as a compatibility fallback |
136
- | Elysia 1.4 or newer within 1.x on Node.js 22.15 or newer | Supported for unbundled ESM/CJS JavaScript using `instrument/node` as a compatibility fallback; Node 22 and 24 are the supported LTS lines |
137
- | Single-file bundles (esbuild, tsup, webpack, Vite SSR, Next.js) | Automatic instrumentation not supported |
138
- | `tsx`, custom Node loader stacks, and Node's native TypeScript execution | Automatic instrumentation not supported |
165
+ | TypeScript compiled to multiple JavaScript files | Use `rasputin-instrument dist` after `tsc`, with adjacent source maps |
166
+ | Elysia 1.4+ on Bun 1.3.x, running `.ts` or `.js` files directly | Use `--preload @rasputin-ai/elysia/instrument/bun` |
167
+ | Elysia 1.4+ on Node.js 22.15 or newer, unbundled ESM/CJS | Use `rasputin-instrument`, or `--import @rasputin-ai/elysia/instrument/node`; Node 22 and 24 are the supported LTS lines |
168
+
169
+ On Node.js 22, start the process with `--experimental-async-context-frame` (or set `NODE_OPTIONS`) so `AsyncLocalStorage` uses the faster implementation that Node.js 24 enables by default. Rasputin logs this once when the flag is missing; set `RASPUTIN_SUPPRESS_ASYNC_CONTEXT_FRAME_WARNING=1` to hide it.
170
+ | Single-file bundles (esbuild, tsup, webpack, Vite SSR, Next.js) | Automatic instrumentation is not supported |
171
+ | `tsx`, custom Node loader stacks, and Node's native TypeScript execution | Automatic instrumentation is not supported |
139
172
  | Deno and edge runtimes | Not supported |
140
173
 
141
- Tested on Bun 1.3.13, Node.js 24.19.0, and Elysia 1.4.28. Node.js 22.15.0 is the minimum for automatic instrumentation (synchronous module hooks). Use `execution.run()` and `execution.trace()` when automatic instrumentation is not available.
174
+ Tested on Bun 1.3.13, Node.js 24.19.0, and Elysia 1.4.28. Node.js 22.15.0 is the minimum for automatic instrumentation. Use `execution.createScope()` and `execution.trace()` when automatic instrumentation is not available.
142
175
 
143
176
  ## Recorder configuration
144
177
 
@@ -155,7 +188,9 @@ Defaults work for most apps. Customize to exclude noisy sources, redact fields,
155
188
  | `maxArrayElements` | `20` | Maximum items retained from one array, map, or set |
156
189
  | `maxStringLength` | `500` | Maximum characters retained from one string |
157
190
  | `maxSerializedValueBytes` | `8 KiB` | Maximum retained size of one captured value |
158
- | `redactKeys` | `[]` | Additional case-insensitive property names to redact |
191
+ | `runtimeStateTargetBytes` | `256 KiB` | Soft size goal for the snapshot sent with an error; extra reconstruction data is dropped first |
192
+ | `maxRuntimeStateBytes` | `512 KiB` | Hard size limit for that snapshot; values are dropped before call history |
193
+ | `redactKeys` | `[]` | Additional property names to redact (case- and separator-insensitive) |
159
194
  | `excludeSources` | `[]` | Source globs or function-name patterns to exclude |
160
195
 
161
196
  ```ts
@@ -177,4 +212,4 @@ Runtime state can include application data. Use `redactKeys` for sensitive field
177
212
  const { recorder, transport } = client.getStats();
178
213
  ```
179
214
 
180
- Counters are local to the current process and reset on restart.
215
+ These counters describe what this process has recorded and sent since it started. They reset on restart. Compare two snapshots if you want activity over a window, rather than dividing lifetime totals by one request.
package/dist/index.js CHANGED
@@ -8,41 +8,58 @@ import {
8
8
  installAutomaticExecutionRuntime,
9
9
  installGlobalHandlers,
10
10
  scheduleInstrumentationManifestUpload
11
- } from "@rasputin-ai/node";
11
+ } from "@rasputin-ai/node/internal";
12
12
  import { Elysia } from "elysia";
13
13
 
14
14
  // src/sdk-meta.ts
15
15
  var SDK_NAME = "@rasputin-ai/elysia";
16
- var SDK_VERSION = "0.5.0-alpha.13";
16
+ var SDK_VERSION = "0.5.0-alpha.14";
17
17
 
18
18
  // src/rasputin-init.ts
19
- var withExecution = (client, execution, installRuntime, manifest) => {
20
- const uninstallRuntime = installRuntime ? installAutomaticExecutionRuntime(execution) : () => {
19
+ var publicExecution = (runtime) => ({
20
+ createScope: (metadata) => {
21
+ const scope = runtime.createScope(metadata);
22
+ return {
23
+ run: scope.run,
24
+ associateError: scope.associateError,
25
+ finish: scope.finish
26
+ };
27
+ },
28
+ trace: runtime.trace,
29
+ getStats: runtime.getStats
30
+ });
31
+ var withExecution = (client, runtime, installRuntime, manifest, uninstallHandlers = () => {
32
+ }) => {
33
+ const uninstallRuntime = installRuntime ? installAutomaticExecutionRuntime(runtime) : () => {
21
34
  };
22
35
  const manifestUpload = scheduleInstrumentationManifestUpload(manifest);
36
+ const execution = publicExecution(runtime);
23
37
  return {
24
38
  ...client,
25
39
  execution,
26
40
  getStats: () => ({ ...client.getStats(), recorder: execution.getStats() }),
27
41
  verifyConfiguration: manifestUpload.verify,
28
42
  captureException: (error, context) => {
29
- const runtimeState = context?.runtimeState ?? execution.getErrorState(
30
- error,
31
- context?.request ? { kind: "http", request: context.request } : void 0
32
- );
33
- const result = client.captureException(error, { ...context, runtimeState });
43
+ const result = client.captureException(error, context);
34
44
  manifestUpload.flushSoon();
35
45
  return result;
36
46
  },
37
47
  flush: async (timeoutMs) => {
48
+ const startedAt = Date.now();
38
49
  await client.flush(timeoutMs);
39
- await manifestUpload.wait();
50
+ if (timeoutMs === void 0) {
51
+ await manifestUpload.wait();
52
+ return;
53
+ }
54
+ await manifestUpload.wait(Math.max(0, timeoutMs - (Date.now() - startedAt)));
40
55
  },
41
56
  close: async () => {
57
+ uninstallHandlers();
42
58
  uninstallRuntime();
43
59
  manifestUpload.disconnect();
60
+ const startedAt = Date.now();
44
61
  await client.close();
45
- await manifestUpload.wait();
62
+ await manifestUpload.wait(Math.max(0, 2e3 - (Date.now() - startedAt)));
46
63
  }
47
64
  };
48
65
  };
@@ -54,8 +71,12 @@ var toStatus = (status) => {
54
71
  }
55
72
  return void 0;
56
73
  };
57
- var isPromiseLike = (value) => {
58
- return Boolean(value) && typeof value.then === "function";
74
+ var isNativeResponsePromise = (value) => {
75
+ try {
76
+ return Object.prototype.toString.call(value) === "[object Promise]";
77
+ } catch {
78
+ return false;
79
+ }
59
80
  };
60
81
  var finishExecution = (scope, metadata) => {
61
82
  try {
@@ -65,7 +86,7 @@ var finishExecution = (scope, metadata) => {
65
86
  };
66
87
  var rememberExecutionError = (scope, error) => {
67
88
  try {
68
- scope.getErrorState(error);
89
+ scope.associateError(error);
69
90
  } catch {
70
91
  }
71
92
  };
@@ -78,18 +99,18 @@ var buildPlugin = (client, execution, recorderEnabled) => {
78
99
  return (request) => scope.run(() => {
79
100
  try {
80
101
  const response = handler(request);
81
- if (isPromiseLike(response)) {
82
- return Promise.resolve(response).then(
102
+ if (isNativeResponsePromise(response)) {
103
+ Promise.prototype.then.call(
104
+ response,
83
105
  (value) => {
84
106
  finishExecution(scope, { request: { status: value.status } });
85
- return value;
86
107
  },
87
108
  (error) => {
88
109
  rememberExecutionError(scope, error);
89
110
  finishExecution(scope);
90
- throw error;
91
111
  }
92
112
  );
113
+ return response;
93
114
  }
94
115
  finishExecution(scope, { request: { status: response.status } });
95
116
  return response;
@@ -109,11 +130,7 @@ var buildPlugin = (client, execution, recorderEnabled) => {
109
130
  status: toStatus(set.status)
110
131
  };
111
132
  try {
112
- const runtimeState = execution.getErrorState(error, {
113
- kind: "http",
114
- request: requestContext
115
- });
116
- client.captureException(error, { request: requestContext, runtimeState });
133
+ client.captureException(error, { request: requestContext });
117
134
  } catch {
118
135
  }
119
136
  return;
@@ -122,45 +139,58 @@ var buildPlugin = (client, execution, recorderEnabled) => {
122
139
  function RasputinInit(options, hooks = {}) {
123
140
  try {
124
141
  const {
125
- installGlobalHandlers: installProcessHandlers = true,
142
+ installGlobalHandlers: installProcessHandlers = options.installGlobalHandlers !== false,
126
143
  fetch,
127
144
  announceDeploy,
128
- installTransportSignalHandlers
145
+ installTransportSignalHandlers = options.installTransportSignalHandlers
129
146
  } = hooks;
147
+ const enabled = isClientEnabled(options);
148
+ const recorderEnabled = enabled && options.executionRecorder?.enabled !== false;
149
+ const runtime = createExecutionRecorder({
150
+ ...options.executionRecorder,
151
+ enabled: recorderEnabled
152
+ });
130
153
  const client = createClient(options, {
131
154
  fetch,
132
155
  announceDeploy,
133
156
  installTransportSignalHandlers,
134
157
  sdkVersion: SDK_VERSION,
135
- sdkName: SDK_NAME
136
- });
137
- const recorderEnabled = isClientEnabled(options) && options.executionRecorder?.enabled !== false;
138
- const execution = createExecutionRecorder({
139
- ...options.executionRecorder,
140
- enabled: recorderEnabled
141
- });
142
- const wrappedClient = withExecution(client, execution, recorderEnabled, {
143
- projectApiKey: options.projectApiKey,
144
- release: options.release,
145
- apiUrl: options.apiUrl,
146
- fetch,
147
- enabled: isClientEnabled(options),
148
- sourceRoot: options.sourceRoot,
149
- sourceRoots: options.sourceRoots,
150
- logSuccess: options.logSuccess
158
+ sdkName: SDK_NAME,
159
+ resolveRuntimeState: (error, context) => runtime.getErrorState(
160
+ error,
161
+ context?.request ? { kind: "http", request: context.request } : void 0
162
+ )
151
163
  });
152
- if (isClientEnabled(options) && installProcessHandlers) {
153
- installGlobalHandlers(wrappedClient);
164
+ let uninstallHandlers = () => {
165
+ };
166
+ const wrappedClient = withExecution(
167
+ client,
168
+ runtime,
169
+ recorderEnabled,
170
+ {
171
+ projectApiKey: options.projectApiKey ?? "",
172
+ release: options.release,
173
+ apiUrl: options.apiUrl,
174
+ fetch,
175
+ enabled,
176
+ sourceRoot: options.sourceRoot,
177
+ sourceRoots: options.sourceRoots,
178
+ logSuccess: options.logSuccess
179
+ },
180
+ () => uninstallHandlers()
181
+ );
182
+ if (enabled && installProcessHandlers) {
183
+ uninstallHandlers = installGlobalHandlers(wrappedClient);
154
184
  }
155
185
  return {
156
186
  client: wrappedClient,
157
- plugin: buildPlugin(wrappedClient, execution, recorderEnabled)
187
+ plugin: buildPlugin(wrappedClient, runtime, recorderEnabled)
158
188
  };
159
189
  } catch {
160
190
  const client = createClient({ ...options, enabled: false });
161
- const execution = createExecutionRecorder({ enabled: false });
162
- const wrappedClient = withExecution(client, execution, false, {
163
- projectApiKey: options.projectApiKey,
191
+ const runtime = createExecutionRecorder({ enabled: false });
192
+ const wrappedClient = withExecution(client, runtime, false, {
193
+ projectApiKey: options.projectApiKey ?? "",
164
194
  release: options.release,
165
195
  apiUrl: options.apiUrl,
166
196
  enabled: false,
@@ -168,7 +198,7 @@ function RasputinInit(options, hooks = {}) {
168
198
  sourceRoots: options.sourceRoots,
169
199
  logSuccess: options.logSuccess
170
200
  });
171
- return { client: wrappedClient, plugin: buildPlugin(wrappedClient, execution, false) };
201
+ return { client: wrappedClient, plugin: buildPlugin(wrappedClient, runtime, false) };
172
202
  }
173
203
  }
174
204
  export {
@@ -5,18 +5,36 @@
5
5
  * the framework-neutral Node recorder.
6
6
  */
7
7
  import { type RasputinOptions } from '@rasputin-ai/core';
8
- import { type ExecutionRecorderOptions, type RasputinExecution, type RasputinNodeClient } from '@rasputin-ai/node';
8
+ import type { ExecutionRecorderOptions, RasputinExecution, RasputinNodeClient } from '@rasputin-ai/node';
9
9
  import { Elysia } from 'elysia';
10
+ /** Elysia plugin returned by `RasputinInit`. Register it with `.use(plugin)`. */
10
11
  export type RasputinElysiaPlugin = ReturnType<typeof buildPlugin>;
11
12
  export type RasputinElysia = {
13
+ /** Use this for `captureException`, `flush`, and wrapping background jobs. */
12
14
  client: RasputinNodeClient;
15
+ /** Register early with `.use(plugin)` so each HTTP request is recorded. */
13
16
  plugin: RasputinElysiaPlugin;
14
17
  };
15
18
  export type RasputinElysiaOptions = RasputinOptions & {
16
- /** Bounded runtime-state capture. Enabled by default; successful executions are discarded. */
19
+ /**
20
+ * Limits and redaction for the runtime recorder. Recording is on unless you
21
+ * set `enabled: false`. Successful requests and jobs are not kept.
22
+ */
17
23
  executionRecorder?: ExecutionRecorderOptions;
24
+ /**
25
+ * Observe `uncaughtException` / nonfatal `unhandledRejection`. Default true.
26
+ * Rasputin does not become the host crash/recovery owner: it exits only when
27
+ * it is the sole `uncaughtException` listener. Set false when the host owns
28
+ * process diagnostics.
29
+ */
30
+ installGlobalHandlers?: boolean;
31
+ /**
32
+ * Install SIGINT / SIGTERM / beforeExit flush hooks. Default true.
33
+ * Set false when the host owns process shutdown.
34
+ */
35
+ installTransportSignalHandlers?: boolean;
18
36
  };
19
- declare const buildPlugin: (client: RasputinNodeClient, execution: RasputinExecution, recorderEnabled: boolean) => Elysia<"", {
37
+ declare const buildPlugin: (client: RasputinNodeClient, execution: Pick<RasputinExecution, "createScope">, recorderEnabled: boolean) => Elysia<"", {
20
38
  decorator: {
21
39
  rasputin: RasputinNodeClient;
22
40
  };
@@ -1 +1 @@
1
- {"version":3,"file":"rasputin-init.d.ts","sourceRoot":"","sources":["../src/rasputin-init.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAKN,KAAK,eAAe,EACpB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAEN,KAAK,wBAAwB,EAI7B,KAAK,iBA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BAAwB,CAAC;IAC7C;;;;;OAKG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC;;;OAGG;IACH,8BAA8B,CAAC,EAAE,OAAO,CAAC;CACzC,CAAC;AA+GF,QAAA,MAAM,WAAW,GAChB,QAAQ,kBAAkB,EAC1B,WAAW,IAAI,CAAC,iBAAiB,EAAE,aAAa,CAAC,EACjD,iBAAiB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAsDxB,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,qBAAqB,GAAG,cAAc,CAAC"}
@@ -1,4 +1,4 @@
1
1
  /** Generated by scripts/generate-sdk-meta.ts — do not edit. */
2
2
  export declare const SDK_NAME = "@rasputin-ai/elysia";
3
- export declare const SDK_VERSION = "0.5.0-alpha.13";
3
+ export declare const SDK_VERSION = "0.5.0-alpha.14";
4
4
  //# sourceMappingURL=sdk-meta.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rasputin-ai/elysia",
3
- "version": "0.5.0-alpha.13",
3
+ "version": "0.5.0-alpha.14",
4
4
  "description": "Official Rasputin AI SDK for Elysia.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -35,8 +35,8 @@
35
35
  "access": "public"
36
36
  },
37
37
  "dependencies": {
38
- "@rasputin-ai/core": "0.5.0-alpha.13",
39
- "@rasputin-ai/node": "0.5.0-alpha.13"
38
+ "@rasputin-ai/core": "0.5.0-alpha.14",
39
+ "@rasputin-ai/node": "0.5.0-alpha.14"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "elysia": ">=1.4.0 <2"