@supacloud/elysia 0.43.2 → 0.44.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/dist/bun-aot.js CHANGED
@@ -80,18 +80,34 @@ function compileHttpPolicies(route, path, registry = {}) {
80
80
 
81
81
  // src/http-telemetry.ts
82
82
  import { Elysia, StatusMap } from "elysia";
83
+ import { createTraceContext, formatTraceparent, parseTraceparent } from "@supacloud/commands";
83
84
  var requestIds = new WeakMap;
85
+ var requestTraces = new WeakMap;
84
86
  function httpRequestId(request) {
85
87
  return requestIds.get(request);
86
88
  }
89
+ function httpTraceContext(request) {
90
+ return requestTraces.get(request);
91
+ }
87
92
  function createHttpTelemetry(observe) {
88
93
  const starts = new WeakMap;
89
94
  return new Elysia().onRequest(({ request, set }) => {
90
95
  const supplied = request.headers.get("x-request-id");
91
96
  const requestId = supplied && /^[A-Za-z0-9._:-]{1,128}$/.test(supplied) ? supplied : crypto.randomUUID();
92
- starts.set(request, { time: performance.now(), requestId });
97
+ const incomingTrace = parseTraceparent(request.headers.get("traceparent"));
98
+ const ctx = createTraceContext(incomingTrace);
99
+ const trace = {
100
+ traceId: ctx.traceId,
101
+ spanId: ctx.spanId,
102
+ ...ctx.parentSpanId ? { parentSpanId: ctx.parentSpanId } : {},
103
+ traceFlags: ctx.traceFlags,
104
+ traceparent: formatTraceparent(ctx)
105
+ };
106
+ starts.set(request, { time: performance.now(), requestId, trace });
93
107
  requestIds.set(request, requestId);
108
+ requestTraces.set(request, trace);
94
109
  set.headers["x-request-id"] = requestId;
110
+ set.headers["traceparent"] = trace.traceparent;
95
111
  }).onError(() => {
96
112
  return;
97
113
  }).onAfterResponse(async ({ request, responseValue: response, set, route }) => {
@@ -103,6 +119,10 @@ function createHttpTelemetry(observe) {
103
119
  try {
104
120
  await observe(Object.freeze({
105
121
  requestId: start.requestId,
122
+ traceId: start.trace.traceId,
123
+ spanId: start.trace.spanId,
124
+ ...start.trace.parentSpanId ? { parentSpanId: start.trace.parentSpanId } : {},
125
+ traceparent: start.trace.traceparent,
106
126
  method: request.method,
107
127
  route: route ?? "<unmatched>",
108
128
  status,
@@ -111,6 +131,29 @@ function createHttpTelemetry(observe) {
111
131
  } catch {}
112
132
  }).as("global");
113
133
  }
134
+ function createOtlpHttpTelemetryObserver(exporter, options = {}) {
135
+ const prefix = options.spanPrefix ? `${options.spanPrefix} ` : "";
136
+ return (event) => {
137
+ const endTimeMs = Date.now();
138
+ const startTimeMs = Math.max(0, endTimeMs - event.durationMs);
139
+ exporter.record({
140
+ traceId: event.traceId,
141
+ spanId: event.spanId,
142
+ ...event.parentSpanId ? { parentSpanId: event.parentSpanId } : {},
143
+ name: `${prefix}${event.method} ${event.route}`,
144
+ kind: "SERVER",
145
+ startTimeMs,
146
+ endTimeMs,
147
+ statusCode: event.status >= 500 ? "ERROR" : "OK",
148
+ attributes: {
149
+ "http.request.method": event.method,
150
+ "http.route": event.route,
151
+ "http.response.status_code": event.status,
152
+ "supacloud.request_id": event.requestId
153
+ }
154
+ });
155
+ };
156
+ }
114
157
  // src/identity.ts
115
158
  import { createRemoteJWKSet, jwtVerify, errors } from "jose";
116
159
  function httpsUrl(value) {
@@ -197,6 +240,15 @@ function unauthenticated() {
197
240
 
198
241
  // src/http-response-cache.ts
199
242
  import { WebStandardAdapter } from "elysia/adapter/web-standard";
243
+ var PER_REQUEST_HEADERS = new Set(["x-request-id", "traceparent", "tracestate"]);
244
+ var STORABLE_RESPONSE_HEADERS = new Set([
245
+ "content-type",
246
+ "x-request-id",
247
+ "traceparent",
248
+ "tracestate",
249
+ "ratelimit-limit",
250
+ "ratelimit-remaining"
251
+ ]);
200
252
  function mayStore({ http, response }) {
201
253
  if (response === null || typeof response !== "object" || response instanceof Response || !Array.isArray(response) && Object.getPrototypeOf(response) !== Object.prototype)
202
254
  return false;
@@ -206,7 +258,7 @@ function mayStore({ http, response }) {
206
258
  return false;
207
259
  const headers = new Headers(http.set.headers);
208
260
  for (const name of headers.keys()) {
209
- if (!["content-type", "x-request-id", "ratelimit-limit", "ratelimit-remaining"].includes(name))
261
+ if (!STORABLE_RESPONSE_HEADERS.has(name))
210
262
  return false;
211
263
  }
212
264
  return !headers.has("content-type") || headers.get("content-type").split(";")[0].trim() === "application/json";
@@ -247,7 +299,7 @@ function createCachePolicy(options) {
247
299
  const context = options.requireAccess(http.request);
248
300
  if (http.request.headers.has("range") || http.request.headers.has("if-none-match") || http.request.headers.has("if-modified-since") || /(?:no-cache|no-store|max-age\s*=\s*0)/i.test(http.request.headers.get("cache-control") ?? ""))
249
301
  return;
250
- const headers = [...http.request.headers].filter(([name]) => name !== "x-request-id").sort(([a], [b]) => a.localeCompare(b));
302
+ const headers = [...http.request.headers].filter(([name]) => !PER_REQUEST_HEADERS.has(name)).sort(([a], [b]) => a.localeCompare(b));
251
303
  const key = await policyKey([
252
304
  "cache",
253
305
  options.namespace,
@@ -301,7 +353,7 @@ function createCachePolicy(options) {
301
353
  if (!miss || !snapshot || context.response !== snapshot.response || snapshot.response.status !== 200)
302
354
  return;
303
355
  const headers = snapshot.response.headers;
304
- if ([...headers.keys()].some((name) => !["content-type", "x-request-id", "ratelimit-limit", "ratelimit-remaining"].includes(name)))
356
+ if ([...headers.keys()].some((name) => !STORABLE_RESPONSE_HEADERS.has(name)))
305
357
  return;
306
358
  try {
307
359
  const body = await snapshot.body;
@@ -1,7 +1,20 @@
1
1
  import { Elysia } from "elysia";
2
+ import { type OtlpSpanExporter } from "@supacloud/commands";
3
+ export interface HttpTraceContext {
4
+ traceId: string;
5
+ spanId: string;
6
+ parentSpanId?: string;
7
+ traceFlags: string;
8
+ traceparent: string;
9
+ }
2
10
  export declare function httpRequestId(request: Request): string | undefined;
11
+ export declare function httpTraceContext(request: Request): HttpTraceContext | undefined;
3
12
  export interface HttpTelemetryEvent {
4
13
  requestId: string;
14
+ traceId: string;
15
+ spanId: string;
16
+ parentSpanId?: string;
17
+ traceparent: string;
5
18
  method: string;
6
19
  /** Static route template only. Query strings and concrete paths are excluded. */
7
20
  route: string;
@@ -31,3 +44,10 @@ export declare function createHttpTelemetry(observe: HttpTelemetryObserver): Ely
31
44
  parser: {};
32
45
  response: import("elysia/types").ExtractErrorFromHandle<undefined>;
33
46
  }, {}, import("elysia").DefaultEphemeral, import("elysia").DefaultEphemeral>;
47
+ /**
48
+ * Bridges HTTP telemetry events into an OTLP/HTTP span exporter (`SERVER` spans),
49
+ * preserving incoming W3C `traceId` & `parentSpanId` for distributed waterfalls.
50
+ */
51
+ export declare function createOtlpHttpTelemetryObserver(exporter: OtlpSpanExporter, options?: {
52
+ readonly spanPrefix?: string;
53
+ }): HttpTelemetryObserver;
package/dist/index.d.ts CHANGED
@@ -11,8 +11,8 @@ export { createMemoryHttpRateLimitStore, createMemoryHttpCacheStore } from "./ht
11
11
  export type { HttpRateLimitStore, HttpRateLimitResult, HttpCacheStore, HttpCacheEntry } from "./http-policy-stores";
12
12
  export { createPostgresHttpPolicyStores, HTTP_POLICY_STORE_SQL } from "./http-policy-postgres";
13
13
  export type { HttpPolicyDatabase } from "./http-policy-postgres";
14
- export { createHttpTelemetry } from "./http-telemetry";
15
- export type { HttpTelemetryEvent, HttpTelemetryObserver } from "./http-telemetry";
14
+ export { createHttpTelemetry, createOtlpHttpTelemetryObserver, httpTraceContext } from "./http-telemetry";
15
+ export type { HttpTelemetryEvent, HttpTelemetryObserver, HttpTraceContext } from "./http-telemetry";
16
16
  import { type ExecutionObserver } from "./execution";
17
17
  import { SchemaContractError } from "./schema_contract";
18
18
  import { type ApplicationDocumentationOptions } from "./documentation";
package/dist/index.js CHANGED
@@ -80,18 +80,34 @@ function compileHttpPolicies(route, path, registry = {}) {
80
80
 
81
81
  // src/http-telemetry.ts
82
82
  import { Elysia, StatusMap } from "elysia";
83
+ import { createTraceContext, formatTraceparent, parseTraceparent } from "@supacloud/commands";
83
84
  var requestIds = new WeakMap;
85
+ var requestTraces = new WeakMap;
84
86
  function httpRequestId(request) {
85
87
  return requestIds.get(request);
86
88
  }
89
+ function httpTraceContext(request) {
90
+ return requestTraces.get(request);
91
+ }
87
92
  function createHttpTelemetry(observe) {
88
93
  const starts = new WeakMap;
89
94
  return new Elysia().onRequest(({ request, set }) => {
90
95
  const supplied = request.headers.get("x-request-id");
91
96
  const requestId = supplied && /^[A-Za-z0-9._:-]{1,128}$/.test(supplied) ? supplied : crypto.randomUUID();
92
- starts.set(request, { time: performance.now(), requestId });
97
+ const incomingTrace = parseTraceparent(request.headers.get("traceparent"));
98
+ const ctx = createTraceContext(incomingTrace);
99
+ const trace = {
100
+ traceId: ctx.traceId,
101
+ spanId: ctx.spanId,
102
+ ...ctx.parentSpanId ? { parentSpanId: ctx.parentSpanId } : {},
103
+ traceFlags: ctx.traceFlags,
104
+ traceparent: formatTraceparent(ctx)
105
+ };
106
+ starts.set(request, { time: performance.now(), requestId, trace });
93
107
  requestIds.set(request, requestId);
108
+ requestTraces.set(request, trace);
94
109
  set.headers["x-request-id"] = requestId;
110
+ set.headers["traceparent"] = trace.traceparent;
95
111
  }).onError(() => {
96
112
  return;
97
113
  }).onAfterResponse(async ({ request, responseValue: response, set, route }) => {
@@ -103,6 +119,10 @@ function createHttpTelemetry(observe) {
103
119
  try {
104
120
  await observe(Object.freeze({
105
121
  requestId: start.requestId,
122
+ traceId: start.trace.traceId,
123
+ spanId: start.trace.spanId,
124
+ ...start.trace.parentSpanId ? { parentSpanId: start.trace.parentSpanId } : {},
125
+ traceparent: start.trace.traceparent,
106
126
  method: request.method,
107
127
  route: route ?? "<unmatched>",
108
128
  status,
@@ -111,6 +131,29 @@ function createHttpTelemetry(observe) {
111
131
  } catch {}
112
132
  }).as("global");
113
133
  }
134
+ function createOtlpHttpTelemetryObserver(exporter, options = {}) {
135
+ const prefix = options.spanPrefix ? `${options.spanPrefix} ` : "";
136
+ return (event) => {
137
+ const endTimeMs = Date.now();
138
+ const startTimeMs = Math.max(0, endTimeMs - event.durationMs);
139
+ exporter.record({
140
+ traceId: event.traceId,
141
+ spanId: event.spanId,
142
+ ...event.parentSpanId ? { parentSpanId: event.parentSpanId } : {},
143
+ name: `${prefix}${event.method} ${event.route}`,
144
+ kind: "SERVER",
145
+ startTimeMs,
146
+ endTimeMs,
147
+ statusCode: event.status >= 500 ? "ERROR" : "OK",
148
+ attributes: {
149
+ "http.request.method": event.method,
150
+ "http.route": event.route,
151
+ "http.response.status_code": event.status,
152
+ "supacloud.request_id": event.requestId
153
+ }
154
+ });
155
+ };
156
+ }
114
157
  // src/identity.ts
115
158
  import { createRemoteJWKSet, jwtVerify, errors } from "jose";
116
159
  function httpsUrl(value) {
@@ -197,6 +240,15 @@ function unauthenticated() {
197
240
 
198
241
  // src/http-response-cache.ts
199
242
  import { WebStandardAdapter } from "elysia/adapter/web-standard";
243
+ var PER_REQUEST_HEADERS = new Set(["x-request-id", "traceparent", "tracestate"]);
244
+ var STORABLE_RESPONSE_HEADERS = new Set([
245
+ "content-type",
246
+ "x-request-id",
247
+ "traceparent",
248
+ "tracestate",
249
+ "ratelimit-limit",
250
+ "ratelimit-remaining"
251
+ ]);
200
252
  function mayStore({ http, response }) {
201
253
  if (response === null || typeof response !== "object" || response instanceof Response || !Array.isArray(response) && Object.getPrototypeOf(response) !== Object.prototype)
202
254
  return false;
@@ -206,7 +258,7 @@ function mayStore({ http, response }) {
206
258
  return false;
207
259
  const headers = new Headers(http.set.headers);
208
260
  for (const name of headers.keys()) {
209
- if (!["content-type", "x-request-id", "ratelimit-limit", "ratelimit-remaining"].includes(name))
261
+ if (!STORABLE_RESPONSE_HEADERS.has(name))
210
262
  return false;
211
263
  }
212
264
  return !headers.has("content-type") || headers.get("content-type").split(";")[0].trim() === "application/json";
@@ -247,7 +299,7 @@ function createCachePolicy(options) {
247
299
  const context = options.requireAccess(http.request);
248
300
  if (http.request.headers.has("range") || http.request.headers.has("if-none-match") || http.request.headers.has("if-modified-since") || /(?:no-cache|no-store|max-age\s*=\s*0)/i.test(http.request.headers.get("cache-control") ?? ""))
249
301
  return;
250
- const headers = [...http.request.headers].filter(([name]) => name !== "x-request-id").sort(([a], [b]) => a.localeCompare(b));
302
+ const headers = [...http.request.headers].filter(([name]) => !PER_REQUEST_HEADERS.has(name)).sort(([a], [b]) => a.localeCompare(b));
251
303
  const key = await policyKey([
252
304
  "cache",
253
305
  options.namespace,
@@ -301,7 +353,7 @@ function createCachePolicy(options) {
301
353
  if (!miss || !snapshot || context.response !== snapshot.response || snapshot.response.status !== 200)
302
354
  return;
303
355
  const headers = snapshot.response.headers;
304
- if ([...headers.keys()].some((name) => !["content-type", "x-request-id", "ratelimit-limit", "ratelimit-remaining"].includes(name)))
356
+ if ([...headers.keys()].some((name) => !STORABLE_RESPONSE_HEADERS.has(name)))
305
357
  return;
306
358
  try {
307
359
  const body = await snapshot.body;
@@ -3444,6 +3496,7 @@ export {
3444
3496
  createMemoryPolicy,
3445
3497
  createMemorySandbox,
3446
3498
  createModulePlugin,
3499
+ createOtlpHttpTelemetryObserver,
3447
3500
  createPersistentCommandAdapter,
3448
3501
  createPostgresHttpPolicyStores,
3449
3502
  createQueueWorkerTransport,
@@ -3463,6 +3516,7 @@ export {
3463
3516
  executeCompiledCommand,
3464
3517
  executeJob,
3465
3518
  frameworkErrorCode,
3519
+ httpTraceContext,
3466
3520
  previewCompiledCommand,
3467
3521
  registerElysiaRoute,
3468
3522
  requireIdempotencyKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supacloud/elysia",
3
- "version": "0.43.2",
3
+ "version": "0.44.0",
4
4
  "description": "Elysia runtime adapter for SupaCloud compiled modules: application/request scopes, route registration and validation",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -55,7 +55,7 @@
55
55
  },
56
56
  "dependencies": {
57
57
  "@supacloud/app": "0.40.1",
58
- "@supacloud/commands": "0.13.0",
58
+ "@supacloud/commands": "0.14.0",
59
59
  "@supacloud/contracts": "0.17.0",
60
60
  "effect": "4.0.5",
61
61
  "exact-mirror": "1.2.7",
@@ -65,7 +65,7 @@
65
65
  "overrides": {
66
66
  "@supacloud/delivery": "0.30.1",
67
67
  "@supacloud/contracts": "0.17.0",
68
- "@supacloud/commands": "0.13.0",
68
+ "@supacloud/commands": "0.14.0",
69
69
  "@graphql-tools/utils": "^12.0.4",
70
70
  "deepmerge-ts": "8.0.2",
71
71
  "prisma": "6.19.3"
@@ -74,9 +74,9 @@
74
74
  "elysia": "2.0.0-rc.6"
75
75
  },
76
76
  "devDependencies": {
77
- "@supacloud/compiler": "0.67.3",
77
+ "@supacloud/compiler": "0.68.0",
78
78
  "@supacloud/delivery": "0.30.1",
79
- "@supacloud/db": "0.27.1",
79
+ "@supacloud/db": "0.28.0",
80
80
  "@types/bun": "^1.4.5",
81
81
  "elysia": "2.0.0-rc.6",
82
82
  "typescript": "7.1.0-beta"