@orpc/standard-server-fetch 1.7.11 → 1.8.1

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
@@ -30,6 +30,7 @@
30
30
  - **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
31
31
  - **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
32
32
  - **📝 Contract-First Development**: Optionally define your API contract before implementation.
33
+ - **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
33
34
  - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), Pinia Colada, and more.
34
35
  - **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
35
36
  - **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
@@ -38,7 +39,6 @@
38
39
  - **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
39
40
  - **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
40
41
  - **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
41
- - **🛡️ Reliability**: Well-tested, TypeScript-based, production-ready, and MIT licensed.
42
42
 
43
43
  ## Documentation
44
44
 
@@ -50,6 +50,7 @@ You can find the full documentation [here](https://orpc.unnoq.com).
50
50
  - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
51
51
  - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
52
52
  - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
53
+ - [@orpc/otel](https://www.npmjs.com/package/@orpc/otel): [OpenTelemetry](https://opentelemetry.io/) integration for observability.
53
54
  - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
54
55
  - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
55
56
  - [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
package/dist/index.d.mts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { StandardBody, StandardHeaders, StandardLazyRequest, StandardRequest, StandardResponse, StandardLazyResponse } from '@orpc/standard-server';
2
- import { AsyncIteratorClass } from '@orpc/shared';
2
+ import { SetSpanErrorOptions, AsyncIteratorClass } from '@orpc/shared';
3
3
 
4
- declare function toEventIterator(stream: ReadableStream<Uint8Array> | null): AsyncIteratorClass<unknown>;
4
+ interface ToEventIteratorOptions extends SetSpanErrorOptions {
5
+ }
6
+ declare function toEventIterator(stream: ReadableStream<Uint8Array> | null, options?: ToEventIteratorOptions): AsyncIteratorClass<unknown>;
5
7
  interface ToEventStreamOptions {
6
8
  /**
7
9
  * If true, a ping comment is sent periodically to keep the connection alive.
@@ -24,7 +26,9 @@ interface ToEventStreamOptions {
24
26
  }
25
27
  declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array>;
26
28
 
27
- declare function toStandardBody(re: Request | Response): Promise<StandardBody>;
29
+ interface ToStandardBodyOptions extends ToEventIteratorOptions {
30
+ }
31
+ declare function toStandardBody(re: Request | Response, options?: ToStandardBodyOptions): Promise<StandardBody>;
28
32
  interface ToFetchBodyOptions extends ToEventStreamOptions {
29
33
  }
30
34
  /**
@@ -52,7 +56,9 @@ declare function toFetchRequest(request: StandardRequest, options?: ToFetchReque
52
56
  interface ToFetchResponseOptions extends ToFetchBodyOptions {
53
57
  }
54
58
  declare function toFetchResponse(response: StandardResponse, options?: ToFetchResponseOptions): Response;
55
- declare function toStandardLazyResponse(response: Response): StandardLazyResponse;
59
+ interface ToStandardLazyResponseOptions extends ToStandardBodyOptions {
60
+ }
61
+ declare function toStandardLazyResponse(response: Response, options?: ToStandardLazyResponseOptions): StandardLazyResponse;
56
62
 
57
63
  export { toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchRequest, toFetchResponse, toStandardBody, toStandardHeaders, toStandardLazyRequest, toStandardLazyResponse };
58
- export type { ToEventStreamOptions, ToFetchBodyOptions, ToFetchRequestOptions, ToFetchResponseOptions };
64
+ export type { ToEventIteratorOptions, ToEventStreamOptions, ToFetchBodyOptions, ToFetchRequestOptions, ToFetchResponseOptions, ToStandardBodyOptions, ToStandardLazyResponseOptions };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { StandardBody, StandardHeaders, StandardLazyRequest, StandardRequest, StandardResponse, StandardLazyResponse } from '@orpc/standard-server';
2
- import { AsyncIteratorClass } from '@orpc/shared';
2
+ import { SetSpanErrorOptions, AsyncIteratorClass } from '@orpc/shared';
3
3
 
4
- declare function toEventIterator(stream: ReadableStream<Uint8Array> | null): AsyncIteratorClass<unknown>;
4
+ interface ToEventIteratorOptions extends SetSpanErrorOptions {
5
+ }
6
+ declare function toEventIterator(stream: ReadableStream<Uint8Array> | null, options?: ToEventIteratorOptions): AsyncIteratorClass<unknown>;
5
7
  interface ToEventStreamOptions {
6
8
  /**
7
9
  * If true, a ping comment is sent periodically to keep the connection alive.
@@ -24,7 +26,9 @@ interface ToEventStreamOptions {
24
26
  }
25
27
  declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array>;
26
28
 
27
- declare function toStandardBody(re: Request | Response): Promise<StandardBody>;
29
+ interface ToStandardBodyOptions extends ToEventIteratorOptions {
30
+ }
31
+ declare function toStandardBody(re: Request | Response, options?: ToStandardBodyOptions): Promise<StandardBody>;
28
32
  interface ToFetchBodyOptions extends ToEventStreamOptions {
29
33
  }
30
34
  /**
@@ -52,7 +56,9 @@ declare function toFetchRequest(request: StandardRequest, options?: ToFetchReque
52
56
  interface ToFetchResponseOptions extends ToFetchBodyOptions {
53
57
  }
54
58
  declare function toFetchResponse(response: StandardResponse, options?: ToFetchResponseOptions): Response;
55
- declare function toStandardLazyResponse(response: Response): StandardLazyResponse;
59
+ interface ToStandardLazyResponseOptions extends ToStandardBodyOptions {
60
+ }
61
+ declare function toStandardLazyResponse(response: Response, options?: ToStandardLazyResponseOptions): StandardLazyResponse;
56
62
 
57
63
  export { toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchRequest, toFetchResponse, toStandardBody, toStandardHeaders, toStandardLazyRequest, toStandardLazyResponse };
58
- export type { ToEventStreamOptions, ToFetchBodyOptions, ToFetchRequestOptions, ToFetchResponseOptions };
64
+ export type { ToEventIteratorOptions, ToEventStreamOptions, ToFetchBodyOptions, ToFetchRequestOptions, ToFetchResponseOptions, ToStandardBodyOptions, ToStandardLazyResponseOptions };
package/dist/index.mjs CHANGED
@@ -1,44 +1,69 @@
1
- import { AsyncIteratorClass, parseEmptyableJSON, isTypescriptObject, stringifyJSON, isAsyncIteratorObject, once } from '@orpc/shared';
1
+ import { AsyncIteratorClass, startSpan, runInSpanContext, parseEmptyableJSON, isTypescriptObject, setSpanError, stringifyJSON, runWithSpan, isAsyncIteratorObject, once } from '@orpc/shared';
2
2
  import { EventDecoderStream, withEventMeta, ErrorEvent, encodeEventMessage, getEventMeta, getFilenameFromContentDisposition, generateContentDisposition } from '@orpc/standard-server';
3
3
 
4
- function toEventIterator(stream) {
4
+ function toEventIterator(stream, options = {}) {
5
5
  const eventStream = stream?.pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
6
6
  const reader = eventStream?.getReader();
7
+ let span;
7
8
  return new AsyncIteratorClass(async () => {
8
- while (true) {
9
- if (reader === void 0) {
10
- return { done: true, value: void 0 };
11
- }
12
- const { done, value } = await reader.read();
13
- if (done) {
14
- return { done: true, value: void 0 };
15
- }
16
- switch (value.event) {
17
- case "message": {
18
- let message = parseEmptyableJSON(value.data);
19
- if (isTypescriptObject(message)) {
20
- message = withEventMeta(message, value);
21
- }
22
- return { done: false, value: message };
9
+ span ??= startSpan("consume_event_iterator_stream");
10
+ try {
11
+ while (true) {
12
+ if (reader === void 0) {
13
+ return { done: true, value: void 0 };
23
14
  }
24
- case "error": {
25
- let error = new ErrorEvent({
26
- data: parseEmptyableJSON(value.data)
27
- });
28
- error = withEventMeta(error, value);
29
- throw error;
15
+ const { done, value } = await runInSpanContext(span, () => reader.read());
16
+ if (done) {
17
+ return { done: true, value: void 0 };
30
18
  }
31
- case "done": {
32
- let done2 = parseEmptyableJSON(value.data);
33
- if (isTypescriptObject(done2)) {
34
- done2 = withEventMeta(done2, value);
19
+ switch (value.event) {
20
+ case "message": {
21
+ let message = parseEmptyableJSON(value.data);
22
+ if (isTypescriptObject(message)) {
23
+ message = withEventMeta(message, value);
24
+ }
25
+ span?.addEvent("message");
26
+ return { done: false, value: message };
27
+ }
28
+ case "error": {
29
+ let error = new ErrorEvent({
30
+ data: parseEmptyableJSON(value.data)
31
+ });
32
+ error = withEventMeta(error, value);
33
+ span?.addEvent("error");
34
+ throw error;
35
+ }
36
+ case "done": {
37
+ let done2 = parseEmptyableJSON(value.data);
38
+ if (isTypescriptObject(done2)) {
39
+ done2 = withEventMeta(done2, value);
40
+ }
41
+ span?.addEvent("done");
42
+ return { done: true, value: done2 };
43
+ }
44
+ default: {
45
+ span?.addEvent("maybe_keepalive");
35
46
  }
36
- return { done: true, value: done2 };
37
47
  }
38
48
  }
49
+ } catch (e) {
50
+ if (!(e instanceof ErrorEvent)) {
51
+ setSpanError(span, e, options);
52
+ }
53
+ throw e;
54
+ }
55
+ }, async (reason) => {
56
+ try {
57
+ if (reason !== "next") {
58
+ span?.addEvent("cancelled");
59
+ }
60
+ await runInSpanContext(span, () => reader?.cancel());
61
+ } catch (e) {
62
+ setSpanError(span, e, options);
63
+ throw e;
64
+ } finally {
65
+ span?.end();
39
66
  }
40
- }, async () => {
41
- await reader?.cancel();
42
67
  });
43
68
  }
44
69
  function toEventStream(iterator, options = {}) {
@@ -47,7 +72,11 @@ function toEventStream(iterator, options = {}) {
47
72
  const keepAliveComment = options.eventIteratorKeepAliveComment ?? "";
48
73
  let cancelled = false;
49
74
  let timeout;
75
+ let span;
50
76
  const stream = new ReadableStream({
77
+ start() {
78
+ span = startSpan("stream_event_iterator");
79
+ },
51
80
  async pull(controller) {
52
81
  try {
53
82
  if (keepAliveEnabled) {
@@ -55,77 +84,101 @@ function toEventStream(iterator, options = {}) {
55
84
  controller.enqueue(encodeEventMessage({
56
85
  comments: [keepAliveComment]
57
86
  }));
87
+ span?.addEvent("keepalive");
58
88
  }, keepAliveInterval);
59
89
  }
60
- const value = await iterator.next();
90
+ const value = await runInSpanContext(span, () => iterator.next());
61
91
  clearInterval(timeout);
62
92
  if (cancelled) {
63
93
  return;
64
94
  }
65
95
  const meta = getEventMeta(value.value);
66
96
  if (!value.done || value.value !== void 0 || meta !== void 0) {
97
+ const event = value.done ? "done" : "message";
67
98
  controller.enqueue(encodeEventMessage({
68
99
  ...meta,
69
- event: value.done ? "done" : "message",
100
+ event,
70
101
  data: stringifyJSON(value.value)
71
102
  }));
103
+ span?.addEvent(event);
72
104
  }
73
105
  if (value.done) {
74
106
  controller.close();
107
+ span?.end();
75
108
  }
76
109
  } catch (err) {
77
110
  clearInterval(timeout);
78
111
  if (cancelled) {
79
112
  return;
80
113
  }
81
- controller.enqueue(encodeEventMessage({
82
- ...getEventMeta(err),
83
- event: "error",
84
- data: err instanceof ErrorEvent ? stringifyJSON(err.data) : void 0
85
- }));
86
- controller.close();
114
+ if (err instanceof ErrorEvent) {
115
+ controller.enqueue(encodeEventMessage({
116
+ ...getEventMeta(err),
117
+ event: "error",
118
+ data: stringifyJSON(err.data)
119
+ }));
120
+ span?.addEvent("error");
121
+ controller.close();
122
+ } else {
123
+ setSpanError(span, err);
124
+ controller.error(err);
125
+ }
126
+ span?.end();
87
127
  }
88
128
  },
89
129
  async cancel() {
90
- cancelled = true;
91
- clearInterval(timeout);
92
- await iterator.return?.();
130
+ try {
131
+ cancelled = true;
132
+ clearInterval(timeout);
133
+ span?.addEvent("cancelled");
134
+ await runInSpanContext(span, () => iterator.return?.());
135
+ } catch (e) {
136
+ setSpanError(span, e);
137
+ throw e;
138
+ } finally {
139
+ span?.end();
140
+ }
93
141
  }
94
142
  }).pipeThrough(new TextEncoderStream());
95
143
  return stream;
96
144
  }
97
145
 
98
- async function toStandardBody(re) {
99
- const contentDisposition = re.headers.get("content-disposition");
100
- if (typeof contentDisposition === "string") {
101
- const fileName = getFilenameFromContentDisposition(contentDisposition) ?? "blob";
102
- const blob2 = await re.blob();
103
- return new File([blob2], fileName, {
104
- type: blob2.type
105
- });
106
- }
107
- const contentType = re.headers.get("content-type");
108
- if (!contentType || contentType.startsWith("application/json")) {
109
- const text = await re.text();
110
- return parseEmptyableJSON(text);
111
- }
112
- if (contentType.startsWith("multipart/form-data")) {
113
- return await re.formData();
114
- }
115
- if (contentType.startsWith("application/x-www-form-urlencoded")) {
116
- const text = await re.text();
117
- return new URLSearchParams(text);
118
- }
119
- if (contentType.startsWith("text/event-stream")) {
120
- return toEventIterator(re.body);
121
- }
122
- if (contentType.startsWith("text/plain")) {
123
- return await re.text();
124
- }
125
- const blob = await re.blob();
126
- return new File([blob], "blob", {
127
- type: blob.type
128
- });
146
+ function toStandardBody(re, options = {}) {
147
+ return runWithSpan(
148
+ { name: "parse_standard_body", signal: options.signal },
149
+ async () => {
150
+ const contentDisposition = re.headers.get("content-disposition");
151
+ if (typeof contentDisposition === "string") {
152
+ const fileName = getFilenameFromContentDisposition(contentDisposition) ?? "blob";
153
+ const blob2 = await re.blob();
154
+ return new File([blob2], fileName, {
155
+ type: blob2.type
156
+ });
157
+ }
158
+ const contentType = re.headers.get("content-type");
159
+ if (!contentType || contentType.startsWith("application/json")) {
160
+ const text = await re.text();
161
+ return parseEmptyableJSON(text);
162
+ }
163
+ if (contentType.startsWith("multipart/form-data")) {
164
+ return await re.formData();
165
+ }
166
+ if (contentType.startsWith("application/x-www-form-urlencoded")) {
167
+ const text = await re.text();
168
+ return new URLSearchParams(text);
169
+ }
170
+ if (contentType.startsWith("text/event-stream")) {
171
+ return toEventIterator(re.body, options);
172
+ }
173
+ if (contentType.startsWith("text/plain")) {
174
+ return await re.text();
175
+ }
176
+ const blob = await re.blob();
177
+ return new File([blob], "blob", {
178
+ type: blob.type
179
+ });
180
+ }
181
+ );
129
182
  }
130
183
  function toFetchBody(body, headers, options = {}) {
131
184
  const currentContentDisposition = headers.get("content-disposition");
@@ -187,7 +240,7 @@ function toStandardLazyRequest(request) {
187
240
  url: new URL(request.url),
188
241
  signal: request.signal,
189
242
  method: request.method,
190
- body: once(() => toStandardBody(request)),
243
+ body: once(() => toStandardBody(request, { signal: request.signal })),
191
244
  get headers() {
192
245
  const headers = toStandardHeaders(request.headers);
193
246
  Object.defineProperty(this, "headers", { value: headers, writable: true });
@@ -214,9 +267,9 @@ function toFetchResponse(response, options = {}) {
214
267
  const body = toFetchBody(response.body, headers, options);
215
268
  return new Response(body, { headers, status: response.status });
216
269
  }
217
- function toStandardLazyResponse(response) {
270
+ function toStandardLazyResponse(response, options = {}) {
218
271
  return {
219
- body: once(() => toStandardBody(response)),
272
+ body: once(() => toStandardBody(response, options)),
220
273
  status: response.status,
221
274
  get headers() {
222
275
  const headers = toStandardHeaders(response.headers);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/standard-server-fetch",
3
3
  "type": "module",
4
- "version": "1.7.11",
4
+ "version": "1.8.1",
5
5
  "license": "MIT",
6
6
  "homepage": "https://unnoq.com",
7
7
  "repository": {
@@ -23,8 +23,8 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@orpc/shared": "1.7.11",
27
- "@orpc/standard-server": "1.7.11"
26
+ "@orpc/shared": "1.8.1",
27
+ "@orpc/standard-server": "1.8.1"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@hono/node-server": "^1.18.0"