@orpc/standard-server-node 0.0.0-next.d5f6b77 → 0.0.0-next.d636350

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,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
- - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
33
+ - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), Pinia Colada, and more.
34
34
  - **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
35
35
  - **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
36
36
  - **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
@@ -49,13 +49,12 @@ 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/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
52
54
  - [@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).
55
+ - [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
57
56
  - [@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.
57
+ - [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
59
58
  - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
60
59
  - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
61
60
  - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
package/dist/index.d.mts CHANGED
@@ -1,28 +1,11 @@
1
1
  import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
2
- import { Readable } from 'node:stream';
2
+ import { ToEventStreamOptions as ToEventStreamOptions$1 } from '@orpc/standard-server-fetch';
3
+ import Stream, { Readable } from 'node:stream';
3
4
  import { IncomingMessage, ServerResponse } from 'node:http';
4
5
  import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
5
6
 
6
- declare function toEventIterator(stream: Readable): AsyncGenerator<unknown | void, unknown | void, void>;
7
- interface ToEventStreamOptions {
8
- /**
9
- * If true, a ping comment is sent periodically to keep the connection alive.
10
- *
11
- * @default true
12
- */
13
- eventIteratorKeepAliveEnabled?: boolean;
14
- /**
15
- * Interval (in milliseconds) between ping comments sent after the last event.
16
- *
17
- * @default 5000
18
- */
19
- eventIteratorKeepAliveInterval?: number;
20
- /**
21
- * The content of the ping comment. Must not include newline characters.
22
- *
23
- * @default ''
24
- */
25
- eventIteratorKeepAliveComment?: string;
7
+ declare function toEventIterator(stream: Readable): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
8
+ interface ToEventStreamOptions extends ToEventStreamOptions$1 {
26
9
  }
27
10
  declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
28
11
 
@@ -45,13 +28,17 @@ interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
45
28
  */
46
29
  declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
47
30
 
31
+ declare function toStandardMethod(method: string | undefined): string;
32
+
48
33
  declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
49
34
 
50
35
  interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
51
36
  }
52
37
  declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
53
38
 
54
- declare function toAbortSignal(res: NodeHttpResponse): AbortSignal;
39
+ declare function toAbortSignal(stream: Stream.Writable): AbortSignal;
40
+
41
+ declare function toStandardUrl(req: NodeHttpRequest): URL;
55
42
 
56
- export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
43
+ export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
57
44
  export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventStreamOptions, ToNodeHttpBodyOptions };
package/dist/index.d.ts CHANGED
@@ -1,28 +1,11 @@
1
1
  import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
2
- import { Readable } from 'node:stream';
2
+ import { ToEventStreamOptions as ToEventStreamOptions$1 } from '@orpc/standard-server-fetch';
3
+ import Stream, { Readable } from 'node:stream';
3
4
  import { IncomingMessage, ServerResponse } from 'node:http';
4
5
  import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
5
6
 
6
- declare function toEventIterator(stream: Readable): AsyncGenerator<unknown | void, unknown | void, void>;
7
- interface ToEventStreamOptions {
8
- /**
9
- * If true, a ping comment is sent periodically to keep the connection alive.
10
- *
11
- * @default true
12
- */
13
- eventIteratorKeepAliveEnabled?: boolean;
14
- /**
15
- * Interval (in milliseconds) between ping comments sent after the last event.
16
- *
17
- * @default 5000
18
- */
19
- eventIteratorKeepAliveInterval?: number;
20
- /**
21
- * The content of the ping comment. Must not include newline characters.
22
- *
23
- * @default ''
24
- */
25
- eventIteratorKeepAliveComment?: string;
7
+ declare function toEventIterator(stream: Readable): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
8
+ interface ToEventStreamOptions extends ToEventStreamOptions$1 {
26
9
  }
27
10
  declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
28
11
 
@@ -45,13 +28,17 @@ interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
45
28
  */
46
29
  declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
47
30
 
31
+ declare function toStandardMethod(method: string | undefined): string;
32
+
48
33
  declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
49
34
 
50
35
  interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
51
36
  }
52
37
  declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
53
38
 
54
- declare function toAbortSignal(res: NodeHttpResponse): AbortSignal;
39
+ declare function toAbortSignal(stream: Stream.Writable): AbortSignal;
40
+
41
+ declare function toStandardUrl(req: NodeHttpRequest): URL;
55
42
 
56
- export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
43
+ export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
57
44
  export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventStreamOptions, ToNodeHttpBodyOptions };
package/dist/index.mjs CHANGED
@@ -1,111 +1,16 @@
1
1
  import { Readable } from 'node:stream';
2
- import { stringifyJSON, parseEmptyableJSON, isTypescriptObject, toArray, isAsyncIteratorObject, once } from '@orpc/shared';
3
- import { EventDecoderStream, encodeEventMessage, getEventMeta, ErrorEvent, withEventMeta, getFilenameFromContentDisposition, generateContentDisposition } from '@orpc/standard-server';
2
+ import { parseEmptyableJSON, isAsyncIteratorObject, stringifyJSON, once } from '@orpc/shared';
3
+ import { getFilenameFromContentDisposition, flattenHeader, generateContentDisposition } from '@orpc/standard-server';
4
+ import { toEventIterator as toEventIterator$1, toEventStream as toEventStream$1 } from '@orpc/standard-server-fetch';
4
5
 
5
6
  function toEventIterator(stream) {
6
- const eventStream = Readable.toWeb(stream).pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
7
- const reader = eventStream.getReader();
8
- async function* gen() {
9
- try {
10
- while (true) {
11
- const { done, value } = await reader.read();
12
- if (done) {
13
- return;
14
- }
15
- switch (value.event) {
16
- case "message": {
17
- let message = parseEmptyableJSON(value.data);
18
- if (isTypescriptObject(message)) {
19
- message = withEventMeta(message, value);
20
- }
21
- yield message;
22
- break;
23
- }
24
- case "error": {
25
- let error = new ErrorEvent({
26
- data: parseEmptyableJSON(value.data)
27
- });
28
- error = withEventMeta(error, value);
29
- throw error;
30
- }
31
- case "done": {
32
- let done2 = parseEmptyableJSON(value.data);
33
- if (isTypescriptObject(done2)) {
34
- done2 = withEventMeta(done2, value);
35
- }
36
- return done2;
37
- }
38
- }
39
- }
40
- } finally {
41
- await reader.cancel();
42
- }
43
- }
44
- return gen();
7
+ return toEventIterator$1(Readable.toWeb(stream));
45
8
  }
46
9
  function toEventStream(iterator, options = {}) {
47
- const keepAliveEnabled = options.eventIteratorKeepAliveEnabled ?? true;
48
- const keepAliveInterval = options.eventIteratorKeepAliveInterval ?? 5e3;
49
- const keepAliveComment = options.eventIteratorKeepAliveComment ?? "";
50
- let cancelled = false;
51
- let timeout;
52
- const stream = new ReadableStream({
53
- async pull(controller) {
54
- try {
55
- if (keepAliveEnabled) {
56
- timeout = setInterval(() => {
57
- controller.enqueue(encodeEventMessage({
58
- comments: [keepAliveComment]
59
- }));
60
- }, keepAliveInterval);
61
- }
62
- const value = await iterator.next();
63
- clearInterval(timeout);
64
- if (cancelled) {
65
- return;
66
- }
67
- const meta = getEventMeta(value.value);
68
- if (!value.done || value.value !== void 0 || meta !== void 0) {
69
- controller.enqueue(encodeEventMessage({
70
- ...meta,
71
- event: value.done ? "done" : "message",
72
- data: stringifyJSON(value.value)
73
- }));
74
- }
75
- if (value.done) {
76
- controller.close();
77
- }
78
- } catch (err) {
79
- clearInterval(timeout);
80
- if (cancelled) {
81
- return;
82
- }
83
- controller.enqueue(encodeEventMessage({
84
- ...getEventMeta(err),
85
- event: "error",
86
- data: err instanceof ErrorEvent ? stringifyJSON(err.data) : void 0
87
- }));
88
- controller.close();
89
- }
90
- },
91
- async cancel(reason) {
92
- cancelled = true;
93
- clearInterval(timeout);
94
- if (reason) {
95
- await iterator.throw?.(reason);
96
- } else {
97
- await iterator.return?.();
98
- }
99
- }
100
- });
101
- return Readable.fromWeb(stream);
10
+ return Readable.fromWeb(toEventStream$1(iterator, options));
102
11
  }
103
12
 
104
13
  async function toStandardBody(req) {
105
- const method = req.method ?? "GET";
106
- if (method === "GET" || method === "HEAD") {
107
- return void 0;
108
- }
109
14
  const contentDisposition = req.headers["content-disposition"];
110
15
  const contentType = req.headers["content-type"];
111
16
  if (typeof contentDisposition === "string") {
@@ -132,7 +37,7 @@ async function toStandardBody(req) {
132
37
  return _streamToFile(req, "blob", contentType);
133
38
  }
134
39
  function toNodeHttpBody(body, headers, options = {}) {
135
- const currentContentDisposition = toArray(headers["content-disposition"])[0];
40
+ const currentContentDisposition = flattenHeader(headers["content-disposition"]);
136
41
  delete headers["content-type"];
137
42
  delete headers["content-disposition"];
138
43
  if (body === void 0) {
@@ -155,8 +60,6 @@ function toNodeHttpBody(body, headers, options = {}) {
155
60
  }
156
61
  if (isAsyncIteratorObject(body)) {
157
62
  headers["content-type"] = "text/event-stream";
158
- headers["cache-control"] = "no-cache";
159
- headers.connection = "keep-alive";
160
63
  return toEventStream(body, options);
161
64
  }
162
65
  headers["content-type"] = "application/json";
@@ -185,28 +88,32 @@ async function _streamToFile(stream, fileName, contentType) {
185
88
  return new File(chunks, fileName, { type: contentType });
186
89
  }
187
90
 
188
- function toAbortSignal(res) {
91
+ function toStandardMethod(method) {
92
+ return method ?? "GET";
93
+ }
94
+
95
+ function toAbortSignal(stream) {
189
96
  const controller = new AbortController();
190
- res.on("close", () => {
191
- if (res.errored) {
192
- controller.abort(res.errored.toString());
193
- } else if (!res.writableFinished) {
194
- controller.abort("Client connection prematurely closed.");
195
- } else {
196
- controller.abort("Server closed the connection.");
97
+ stream.once("error", (error) => controller.abort(error));
98
+ stream.once("close", () => {
99
+ if (!stream.writableFinished) {
100
+ controller.abort(new Error("Writable stream closed before it finished writing"));
197
101
  }
198
102
  });
199
103
  return controller.signal;
200
104
  }
201
105
 
202
- function toStandardLazyRequest(req, res) {
203
- const method = req.method ?? "GET";
106
+ function toStandardUrl(req) {
204
107
  const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
205
108
  const host = req.headers.host ?? "localhost";
206
109
  const url = new URL(req.originalUrl ?? req.url ?? "/", `${protocol}//${host}`);
110
+ return url;
111
+ }
112
+
113
+ function toStandardLazyRequest(req, res) {
207
114
  return {
208
- method,
209
- url,
115
+ method: toStandardMethod(req.method),
116
+ url: toStandardUrl(req),
210
117
  headers: req.headers,
211
118
  body: once(() => toStandardBody(req)),
212
119
  signal: toAbortSignal(res)
@@ -215,24 +122,25 @@ function toStandardLazyRequest(req, res) {
215
122
 
216
123
  function sendStandardResponse(res, standardResponse, options = {}) {
217
124
  return new Promise((resolve, reject) => {
218
- res.on("error", reject);
219
- res.on("finish", resolve);
220
- const resHeaders = standardResponse.headers;
125
+ res.once("error", reject);
126
+ res.once("close", resolve);
127
+ const resHeaders = { ...standardResponse.headers };
221
128
  const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options);
222
129
  res.writeHead(standardResponse.status, resHeaders);
223
130
  if (resBody === void 0) {
224
- res.end(resBody);
131
+ res.end();
225
132
  } else if (typeof resBody === "string") {
226
133
  res.end(resBody);
227
134
  } else {
228
- res.on("close", () => {
135
+ res.once("close", () => {
229
136
  if (!resBody.closed) {
230
137
  resBody.destroy(res.errored ?? void 0);
231
138
  }
232
139
  });
140
+ resBody.once("error", (error) => res.destroy(error));
233
141
  resBody.pipe(res);
234
142
  }
235
143
  });
236
144
  }
237
145
 
238
- export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
146
+ export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/standard-server-node",
3
3
  "type": "module",
4
- "version": "0.0.0-next.d5f6b77",
4
+ "version": "0.0.0-next.d636350",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -23,11 +23,12 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@orpc/shared": "0.0.0-next.d5f6b77",
27
- "@orpc/standard-server": "0.0.0-next.d5f6b77"
26
+ "@orpc/standard-server-fetch": "0.0.0-next.d636350",
27
+ "@orpc/shared": "0.0.0-next.d636350",
28
+ "@orpc/standard-server": "0.0.0-next.d636350"
28
29
  },
29
30
  "devDependencies": {
30
- "@types/node": "^22.14.1",
31
+ "@types/node": "^22.15.18",
31
32
  "@types/supertest": "^6.0.3",
32
33
  "supertest": "^7.1.0"
33
34
  },