@orpc/standard-server-node 0.0.0-next.e563486 → 0.0.0-next.e565b64

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
@@ -21,28 +21,24 @@
21
21
 
22
22
  <h3 align="center">Typesafe APIs Made Simple 🪄</h3>
23
23
 
24
- **oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards, ensuring a smooth and enjoyable developer experience.
24
+ **oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
25
25
 
26
26
  ---
27
27
 
28
28
  ## Highlights
29
29
 
30
- - **End-to-End Type Safety 🔒**: Ensure complete type safety from inputs to outputs and errors, bridging server and client seamlessly.
31
- - **First-Class OpenAPI 📄**: Adheres to the OpenAPI standard out of the box, ensuring seamless integration and comprehensive API documentation.
32
- - **Contract-First Development 📜**: (Optional) Define your API contract upfront and implement it with confidence.
33
- - **Exceptional Developer Experience ✨**: Enjoy a streamlined workflow with robust typing and clear, in-code documentation.
34
- - **Multi-Runtime Support 🌍**: Run your code seamlessly on Cloudflare, Deno, Bun, Node.js, and more.
35
- - **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
36
- - **Server Actions ⚡️**: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.
37
- - **Standard Schema Support 🗂️**: Effortlessly work with Zod, Valibot, ArkType, and others right out of the box.
38
- - **Fast & Lightweight 💨**: Built on native APIs across all runtimes – optimized for speed and efficiency.
39
- - **Native Types 📦**: Enjoy built-in support for Date, File, Blob, BigInt, URL and more with no extra setup.
40
- - **Lazy Router ⏱️**: Improve cold start times with our lazy routing feature.
41
- - **SSE & Streaming 📡**: Provides SSE and streaming features – perfect for real-time notifications and AI-powered streaming responses.
42
- - **Reusability 🔄**: Write once and reuse your code across multiple purposes effortlessly.
43
- - **Extendability 🔌**: Easily enhance oRPC with plugins, middleware, and interceptors.
44
- - **Reliability 🛡️**: Well-tested, fully TypeScript, production-ready, and MIT licensed for peace of mind.
45
- - **Simplicity 💡**: Enjoy straightforward, clean code with no hidden magic.
30
+ - **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
31
+ - **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
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.
34
+ - **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
35
+ - **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
36
+ - **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
37
+ - **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
38
+ - **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
39
+ - **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
40
+ - **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
41
+ - **🛡️ Reliability**: Well-tested, TypeScript-based, production-ready, and MIT licensed.
46
42
 
47
43
  ## Documentation
48
44
 
@@ -53,6 +49,8 @@ You can find the full documentation [here](https://orpc.unnoq.com).
53
49
  - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
54
50
  - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
55
51
  - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
52
+ - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with NestJS.
53
+ - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
56
54
  - [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
57
55
  - [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
58
56
  - [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
@@ -67,6 +65,14 @@ You can find the full documentation [here](https://orpc.unnoq.com).
67
65
 
68
66
  [Node.js](https://nodejs.org) server adapter for oRPC.
69
67
 
68
+ ## Sponsors
69
+
70
+ <p align="center">
71
+ <a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
72
+ <img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
73
+ </a>
74
+ </p>
75
+
70
76
  ## License
71
77
 
72
78
  Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
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,12 +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 { type NodeHttpRequest, type NodeHttpResponse, type SendStandardResponseOptions, type ToEventStreamOptions, type ToNodeHttpBodyOptions, sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
43
+ export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
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,12 +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 { type NodeHttpRequest, type NodeHttpResponse, type SendStandardResponseOptions, type ToEventStreamOptions, type ToNodeHttpBodyOptions, sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
43
+ export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
44
+ export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventStreamOptions, ToNodeHttpBodyOptions };
package/dist/index.mjs CHANGED
@@ -1,106 +1,21 @@
1
1
  import { Readable } from 'node:stream';
2
- import { stringifyJSON, parseEmptyableJSON, isTypescriptObject, isAsyncIteratorObject, once } from '@orpc/shared';
3
- import { EventDecoderStream, encodeEventMessage, getEventMeta, ErrorEvent, withEventMeta, parseContentDisposition, contentDisposition } 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 timeout;
51
- const stream = new ReadableStream({
52
- async pull(controller) {
53
- try {
54
- if (keepAliveEnabled) {
55
- timeout = setInterval(() => {
56
- controller.enqueue(encodeEventMessage({
57
- comments: [keepAliveComment]
58
- }));
59
- }, keepAliveInterval);
60
- }
61
- const value = await iterator.next();
62
- clearInterval(timeout);
63
- controller.enqueue(encodeEventMessage({
64
- ...getEventMeta(value.value),
65
- event: value.done ? "done" : "message",
66
- data: stringifyJSON(value.value)
67
- }));
68
- if (value.done) {
69
- controller.close();
70
- }
71
- } catch (err) {
72
- clearInterval(timeout);
73
- controller.enqueue(encodeEventMessage({
74
- ...getEventMeta(err),
75
- event: "error",
76
- data: err instanceof ErrorEvent ? stringifyJSON(err.data) : void 0
77
- }));
78
- controller.close();
79
- }
80
- },
81
- async cancel(reason) {
82
- if (reason) {
83
- await iterator.throw?.(reason);
84
- } else {
85
- await iterator.return?.();
86
- }
87
- }
88
- });
89
- return Readable.fromWeb(stream);
10
+ return Readable.fromWeb(toEventStream$1(iterator, options));
90
11
  }
91
12
 
92
13
  async function toStandardBody(req) {
93
- const method = req.method ?? "GET";
94
- if (method === "GET" || method === "HEAD") {
95
- return void 0;
96
- }
97
- const contentDisposition2 = req.headers["content-disposition"];
14
+ const contentDisposition = req.headers["content-disposition"];
98
15
  const contentType = req.headers["content-type"];
99
- if (contentDisposition2) {
100
- const fileName = parseContentDisposition(contentDisposition2).parameters.filename;
101
- if (typeof fileName === "string") {
102
- return _streamToFile(req, fileName, contentType ?? "");
103
- }
16
+ if (typeof contentDisposition === "string") {
17
+ const fileName = getFilenameFromContentDisposition(contentDisposition) ?? "blob";
18
+ return _streamToFile(req, fileName, contentType ?? "");
104
19
  }
105
20
  if (!contentType || contentType.startsWith("application/json")) {
106
21
  const text = await _streamToString(req);
@@ -122,6 +37,7 @@ async function toStandardBody(req) {
122
37
  return _streamToFile(req, "blob", contentType);
123
38
  }
124
39
  function toNodeHttpBody(body, headers, options = {}) {
40
+ const currentContentDisposition = flattenHeader(headers["content-disposition"]);
125
41
  delete headers["content-type"];
126
42
  delete headers["content-disposition"];
127
43
  if (body === void 0) {
@@ -130,10 +46,7 @@ function toNodeHttpBody(body, headers, options = {}) {
130
46
  if (body instanceof Blob) {
131
47
  headers["content-type"] = body.type;
132
48
  headers["content-length"] = body.size.toString();
133
- headers["content-disposition"] = contentDisposition(
134
- body instanceof File ? body.name : "blob",
135
- { type: "inline" }
136
- );
49
+ headers["content-disposition"] = currentContentDisposition ?? generateContentDisposition(body instanceof File ? body.name : "blob");
137
50
  return Readable.fromWeb(body.stream());
138
51
  }
139
52
  if (body instanceof FormData) {
@@ -177,29 +90,32 @@ async function _streamToFile(stream, fileName, contentType) {
177
90
  return new File(chunks, fileName, { type: contentType });
178
91
  }
179
92
 
180
- function toAbortSignal(res) {
93
+ function toStandardMethod(method) {
94
+ return method ?? "GET";
95
+ }
96
+
97
+ function toAbortSignal(stream) {
181
98
  const controller = new AbortController();
182
- res.on("close", () => {
183
- if (res.errored) {
184
- controller.abort(res.errored.toString());
185
- } else if (!res.writableFinished) {
186
- controller.abort("Client connection prematurely closed.");
187
- } else {
188
- controller.abort("Server closed the connection.");
99
+ stream.once("error", (error) => controller.abort(error));
100
+ stream.once("close", () => {
101
+ if (!stream.writableFinished) {
102
+ controller.abort(new Error("Writable stream closed before it finished writing"));
189
103
  }
190
104
  });
191
105
  return controller.signal;
192
106
  }
193
107
 
194
- function toStandardLazyRequest(req, res) {
195
- const method = req.method ?? "GET";
108
+ function toStandardUrl(req) {
196
109
  const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
197
110
  const host = req.headers.host ?? "localhost";
198
111
  const url = new URL(req.originalUrl ?? req.url ?? "/", `${protocol}//${host}`);
112
+ return url;
113
+ }
114
+
115
+ function toStandardLazyRequest(req, res) {
199
116
  return {
200
- raw: { request: req, response: res },
201
- method,
202
- url,
117
+ method: toStandardMethod(req.method),
118
+ url: toStandardUrl(req),
203
119
  headers: req.headers,
204
120
  body: once(() => toStandardBody(req)),
205
121
  signal: toAbortSignal(res)
@@ -208,24 +124,25 @@ function toStandardLazyRequest(req, res) {
208
124
 
209
125
  function sendStandardResponse(res, standardResponse, options = {}) {
210
126
  return new Promise((resolve, reject) => {
211
- res.on("error", reject);
212
- res.on("finish", resolve);
213
- const resHeaders = standardResponse.headers;
127
+ res.once("error", reject);
128
+ res.once("close", resolve);
129
+ const resHeaders = { ...standardResponse.headers };
214
130
  const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options);
215
131
  res.writeHead(standardResponse.status, resHeaders);
216
132
  if (resBody === void 0) {
217
- res.end(resBody);
133
+ res.end();
218
134
  } else if (typeof resBody === "string") {
219
135
  res.end(resBody);
220
136
  } else {
221
- res.on("close", () => {
137
+ res.once("close", () => {
222
138
  if (!resBody.closed) {
223
139
  resBody.destroy(res.errored ?? void 0);
224
140
  }
225
141
  });
142
+ resBody.once("error", (error) => res.destroy(error));
226
143
  resBody.pipe(res);
227
144
  }
228
145
  });
229
146
  }
230
147
 
231
- export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
148
+ 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.e563486",
4
+ "version": "0.0.0-next.e565b64",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -23,13 +23,14 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@orpc/shared": "0.0.0-next.e563486",
27
- "@orpc/standard-server": "0.0.0-next.e563486"
26
+ "@orpc/shared": "0.0.0-next.e565b64",
27
+ "@orpc/standard-server": "0.0.0-next.e565b64",
28
+ "@orpc/standard-server-fetch": "0.0.0-next.e565b64"
28
29
  },
29
30
  "devDependencies": {
30
- "@types/node": "^22.13.1",
31
- "@types/supertest": "^6.0.2",
32
- "supertest": "^7.0.0"
31
+ "@types/node": "^22.15.17",
32
+ "@types/supertest": "^6.0.3",
33
+ "supertest": "^7.1.0"
33
34
  },
34
35
  "scripts": {
35
36
  "build": "unbuild",