@orpc/standard-server-fetch 0.0.0-next.ff41b3a → 0.0.0-next.ff7ad2e

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,7 +30,8 @@
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
- - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
33
+ - **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
34
+ - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), SWR, 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.
36
37
  - **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
@@ -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
 
@@ -49,13 +49,14 @@ You can find the full documentation [here](https://orpc.unnoq.com).
49
49
  - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
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
+ - [@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.
54
+ - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
52
55
  - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
53
- - [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
54
- - [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
55
- - [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
56
- - [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
56
+ - [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
57
+ - [@orpc/experimental-react-swr](https://www.npmjs.com/package/@orpc/experimental-react-swr): [SWR](https://swr.vercel.app/) integration.
57
58
  - [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
58
- - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
59
+ - [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
59
60
  - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
60
61
  - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
61
62
  - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
package/dist/index.d.mts CHANGED
@@ -1,6 +1,9 @@
1
1
  import { StandardBody, StandardHeaders, StandardLazyRequest, StandardRequest, StandardResponse, StandardLazyResponse } from '@orpc/standard-server';
2
+ import { SetSpanErrorOptions, AsyncIteratorClass } from '@orpc/shared';
2
3
 
3
- declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown | void, unknown | void, void>;
4
+ interface ToEventIteratorOptions extends SetSpanErrorOptions {
5
+ }
6
+ declare function toEventIterator(stream: ReadableStream<Uint8Array> | null, options?: ToEventIteratorOptions): AsyncIteratorClass<unknown>;
4
7
  interface ToEventStreamOptions {
5
8
  /**
6
9
  * If true, a ping comment is sent periodically to keep the connection alive.
@@ -23,7 +26,9 @@ interface ToEventStreamOptions {
23
26
  }
24
27
  declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array>;
25
28
 
26
- 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>;
27
32
  interface ToFetchBodyOptions extends ToEventStreamOptions {
28
33
  }
29
34
  /**
@@ -51,7 +56,9 @@ declare function toFetchRequest(request: StandardRequest, options?: ToFetchReque
51
56
  interface ToFetchResponseOptions extends ToFetchBodyOptions {
52
57
  }
53
58
  declare function toFetchResponse(response: StandardResponse, options?: ToFetchResponseOptions): Response;
54
- declare function toStandardLazyResponse(response: Response): StandardLazyResponse;
59
+ interface ToStandardLazyResponseOptions extends ToStandardBodyOptions {
60
+ }
61
+ declare function toStandardLazyResponse(response: Response, options?: ToStandardLazyResponseOptions): StandardLazyResponse;
55
62
 
56
63
  export { toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchRequest, toFetchResponse, toStandardBody, toStandardHeaders, toStandardLazyRequest, toStandardLazyResponse };
57
- export type { ToEventStreamOptions, ToFetchBodyOptions, ToFetchRequestOptions, ToFetchResponseOptions };
64
+ export type { ToEventIteratorOptions, ToEventStreamOptions, ToFetchBodyOptions, ToFetchRequestOptions, ToFetchResponseOptions, ToStandardBodyOptions, ToStandardLazyResponseOptions };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import { StandardBody, StandardHeaders, StandardLazyRequest, StandardRequest, StandardResponse, StandardLazyResponse } from '@orpc/standard-server';
2
+ import { SetSpanErrorOptions, AsyncIteratorClass } from '@orpc/shared';
2
3
 
3
- declare function toEventIterator(stream: ReadableStream<Uint8Array>): AsyncGenerator<unknown | void, unknown | void, void>;
4
+ interface ToEventIteratorOptions extends SetSpanErrorOptions {
5
+ }
6
+ declare function toEventIterator(stream: ReadableStream<Uint8Array> | null, options?: ToEventIteratorOptions): AsyncIteratorClass<unknown>;
4
7
  interface ToEventStreamOptions {
5
8
  /**
6
9
  * If true, a ping comment is sent periodically to keep the connection alive.
@@ -23,7 +26,9 @@ interface ToEventStreamOptions {
23
26
  }
24
27
  declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): ReadableStream<Uint8Array>;
25
28
 
26
- 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>;
27
32
  interface ToFetchBodyOptions extends ToEventStreamOptions {
28
33
  }
29
34
  /**
@@ -51,7 +56,9 @@ declare function toFetchRequest(request: StandardRequest, options?: ToFetchReque
51
56
  interface ToFetchResponseOptions extends ToFetchBodyOptions {
52
57
  }
53
58
  declare function toFetchResponse(response: StandardResponse, options?: ToFetchResponseOptions): Response;
54
- declare function toStandardLazyResponse(response: Response): StandardLazyResponse;
59
+ interface ToStandardLazyResponseOptions extends ToStandardBodyOptions {
60
+ }
61
+ declare function toStandardLazyResponse(response: Response, options?: ToStandardLazyResponseOptions): StandardLazyResponse;
55
62
 
56
63
  export { toEventIterator, toEventStream, toFetchBody, toFetchHeaders, toFetchRequest, toFetchResponse, toStandardBody, toStandardHeaders, toStandardLazyRequest, toStandardLazyResponse };
57
- export type { ToEventStreamOptions, ToFetchBodyOptions, ToFetchRequestOptions, ToFetchResponseOptions };
64
+ export type { ToEventIteratorOptions, ToEventStreamOptions, ToFetchBodyOptions, ToFetchRequestOptions, ToFetchResponseOptions, ToStandardBodyOptions, ToStandardLazyResponseOptions };
package/dist/index.mjs CHANGED
@@ -1,15 +1,20 @@
1
- import { stringifyJSON, parseEmptyableJSON, isTypescriptObject, isAsyncIteratorObject, once } from '@orpc/shared';
2
- import { EventDecoderStream, encodeEventMessage, getEventMeta, ErrorEvent, withEventMeta, getFilenameFromContentDisposition, generateContentDisposition } from '@orpc/standard-server';
1
+ import { AsyncIteratorClass, startSpan, runInSpanContext, parseEmptyableJSON, isTypescriptObject, setSpanError, stringifyJSON, runWithSpan, isAsyncIteratorObject, once } from '@orpc/shared';
2
+ import { EventDecoderStream, withEventMeta, ErrorEvent, encodeEventMessage, getEventMeta, getFilenameFromContentDisposition, generateContentDisposition } from '@orpc/standard-server';
3
3
 
4
- function toEventIterator(stream) {
5
- const eventStream = stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
6
- const reader = eventStream.getReader();
7
- async function* gen() {
4
+ function toEventIterator(stream, options = {}) {
5
+ const eventStream = stream?.pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
6
+ const reader = eventStream?.getReader();
7
+ let span;
8
+ return new AsyncIteratorClass(async () => {
9
+ span ??= startSpan("consume_event_iterator_stream");
8
10
  try {
9
11
  while (true) {
10
- const { done, value } = await reader.read();
12
+ if (reader === void 0) {
13
+ return { done: true, value: void 0 };
14
+ }
15
+ const { done, value } = await runInSpanContext(span, () => reader.read());
11
16
  if (done) {
12
- return;
17
+ return { done: true, value: void 0 };
13
18
  }
14
19
  switch (value.event) {
15
20
  case "message": {
@@ -17,14 +22,15 @@ function toEventIterator(stream) {
17
22
  if (isTypescriptObject(message)) {
18
23
  message = withEventMeta(message, value);
19
24
  }
20
- yield message;
21
- break;
25
+ span?.addEvent("message");
26
+ return { done: false, value: message };
22
27
  }
23
28
  case "error": {
24
29
  let error = new ErrorEvent({
25
30
  data: parseEmptyableJSON(value.data)
26
31
  });
27
32
  error = withEventMeta(error, value);
33
+ span?.addEvent("error");
28
34
  throw error;
29
35
  }
30
36
  case "done": {
@@ -32,22 +38,45 @@ function toEventIterator(stream) {
32
38
  if (isTypescriptObject(done2)) {
33
39
  done2 = withEventMeta(done2, value);
34
40
  }
35
- return done2;
41
+ span?.addEvent("done");
42
+ return { done: true, value: done2 };
43
+ }
44
+ default: {
45
+ span?.addEvent("maybe_keepalive");
36
46
  }
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;
39
64
  } finally {
40
- await reader.cancel();
65
+ span?.end();
41
66
  }
42
- }
43
- return gen();
67
+ });
44
68
  }
45
69
  function toEventStream(iterator, options = {}) {
46
70
  const keepAliveEnabled = options.eventIteratorKeepAliveEnabled ?? true;
47
71
  const keepAliveInterval = options.eventIteratorKeepAliveInterval ?? 5e3;
48
72
  const keepAliveComment = options.eventIteratorKeepAliveComment ?? "";
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,76 +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);
92
+ if (cancelled) {
93
+ return;
94
+ }
62
95
  const meta = getEventMeta(value.value);
63
96
  if (!value.done || value.value !== void 0 || meta !== void 0) {
97
+ const event = value.done ? "done" : "message";
64
98
  controller.enqueue(encodeEventMessage({
65
99
  ...meta,
66
- event: value.done ? "done" : "message",
100
+ event,
67
101
  data: stringifyJSON(value.value)
68
102
  }));
103
+ span?.addEvent(event);
69
104
  }
70
105
  if (value.done) {
71
106
  controller.close();
107
+ span?.end();
72
108
  }
73
109
  } catch (err) {
74
110
  clearInterval(timeout);
75
- controller.enqueue(encodeEventMessage({
76
- ...getEventMeta(err),
77
- event: "error",
78
- data: err instanceof ErrorEvent ? stringifyJSON(err.data) : void 0
79
- }));
80
- controller.close();
111
+ if (cancelled) {
112
+ return;
113
+ }
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();
81
127
  }
82
128
  },
83
- async cancel(reason) {
84
- if (reason) {
85
- await iterator.throw?.(reason);
86
- } else {
87
- await iterator.return?.();
129
+ async cancel() {
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();
88
140
  }
89
141
  }
90
142
  }).pipeThrough(new TextEncoderStream());
91
143
  return stream;
92
144
  }
93
145
 
94
- async function toStandardBody(re) {
95
- if (!re.body) {
96
- return void 0;
97
- }
98
- const contentDisposition = re.headers.get("content-disposition");
99
- if (typeof contentDisposition === "string") {
100
- const fileName = getFilenameFromContentDisposition(contentDisposition) ?? "blob";
101
- const blob2 = await re.blob();
102
- return new File([blob2], fileName, {
103
- type: blob2.type
104
- });
105
- }
106
- const contentType = re.headers.get("content-type");
107
- if (!contentType || contentType.startsWith("application/json")) {
108
- const text = await re.text();
109
- return parseEmptyableJSON(text);
110
- }
111
- if (contentType.startsWith("multipart/form-data")) {
112
- return await re.formData();
113
- }
114
- if (contentType.startsWith("application/x-www-form-urlencoded")) {
115
- const text = await re.text();
116
- return new URLSearchParams(text);
117
- }
118
- if (contentType.startsWith("text/event-stream")) {
119
- return toEventIterator(re.body);
120
- }
121
- if (contentType.startsWith("text/plain")) {
122
- return await re.text();
123
- }
124
- const blob = await re.blob();
125
- return new File([blob], "blob", {
126
- type: blob.type
127
- });
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
+ );
128
182
  }
129
183
  function toFetchBody(body, headers, options = {}) {
130
184
  const currentContentDisposition = headers.get("content-disposition");
@@ -150,8 +204,6 @@ function toFetchBody(body, headers, options = {}) {
150
204
  }
151
205
  if (isAsyncIteratorObject(body)) {
152
206
  headers.set("content-type", "text/event-stream");
153
- headers.set("cache-control", "no-cache");
154
- headers.set("connection", "keep-alive");
155
207
  return toEventStream(body, options);
156
208
  }
157
209
  headers.set("content-type", "application/json");
@@ -188,7 +240,7 @@ function toStandardLazyRequest(request) {
188
240
  url: new URL(request.url),
189
241
  signal: request.signal,
190
242
  method: request.method,
191
- body: once(() => toStandardBody(request)),
243
+ body: once(() => toStandardBody(request, { signal: request.signal })),
192
244
  get headers() {
193
245
  const headers = toStandardHeaders(request.headers);
194
246
  Object.defineProperty(this, "headers", { value: headers, writable: true });
@@ -215,9 +267,9 @@ function toFetchResponse(response, options = {}) {
215
267
  const body = toFetchBody(response.body, headers, options);
216
268
  return new Response(body, { headers, status: response.status });
217
269
  }
218
- function toStandardLazyResponse(response) {
270
+ function toStandardLazyResponse(response, options = {}) {
219
271
  return {
220
- body: once(() => toStandardBody(response)),
272
+ body: once(() => toStandardBody(response, options)),
221
273
  status: response.status,
222
274
  get headers() {
223
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": "0.0.0-next.ff41b3a",
4
+ "version": "0.0.0-next.ff7ad2e",
5
5
  "license": "MIT",
6
6
  "homepage": "https://unnoq.com",
7
7
  "repository": {
@@ -23,11 +23,11 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@orpc/shared": "0.0.0-next.ff41b3a",
27
- "@orpc/standard-server": "0.0.0-next.ff41b3a"
26
+ "@orpc/standard-server": "0.0.0-next.ff7ad2e",
27
+ "@orpc/shared": "0.0.0-next.ff7ad2e"
28
28
  },
29
29
  "devDependencies": {
30
- "@hono/node-server": "^1.14.1"
30
+ "@hono/node-server": "^1.18.2"
31
31
  },
32
32
  "scripts": {
33
33
  "build": "unbuild",