@orpc/standard-server-node 0.0.0-next.6e49c5c → 0.0.0-next.6e7c532

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,7 @@ 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.
56
53
  - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
57
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).
58
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).
package/dist/index.d.mts CHANGED
@@ -1,9 +1,9 @@
1
- import { Readable } from 'node:stream';
1
+ import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
2
+ import Stream, { Readable } from 'node:stream';
2
3
  import { IncomingMessage, ServerResponse } from 'node:http';
3
4
  import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
4
- import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
5
5
 
6
- declare function toEventIterator(stream: Readable): AsyncGenerator<unknown | void, unknown | void, void>;
6
+ declare function toEventIterator(stream: Readable): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
7
7
  interface ToEventStreamOptions {
8
8
  /**
9
9
  * If true, a ping comment is sent periodically to keep the connection alive.
@@ -45,12 +45,17 @@ interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
45
45
  */
46
46
  declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
47
47
 
48
+ declare function toStandardMethod(method: string | undefined): string;
49
+
48
50
  declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
49
51
 
50
52
  interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
51
53
  }
52
54
  declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
53
55
 
54
- declare function toAbortSignal(res: NodeHttpResponse): AbortSignal;
56
+ declare function toAbortSignal(stream: Stream.Writable): AbortSignal;
57
+
58
+ declare function toStandardUrl(req: NodeHttpRequest): URL;
55
59
 
56
- export { type NodeHttpRequest, type NodeHttpResponse, type SendStandardResponseOptions, type ToEventStreamOptions, type ToNodeHttpBodyOptions, sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
60
+ export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
61
+ export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventStreamOptions, ToNodeHttpBodyOptions };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { Readable } from 'node:stream';
1
+ import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
2
+ import Stream, { Readable } from 'node:stream';
2
3
  import { IncomingMessage, ServerResponse } from 'node:http';
3
4
  import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
4
- import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
5
5
 
6
- declare function toEventIterator(stream: Readable): AsyncGenerator<unknown | void, unknown | void, void>;
6
+ declare function toEventIterator(stream: Readable): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
7
7
  interface ToEventStreamOptions {
8
8
  /**
9
9
  * If true, a ping comment is sent periodically to keep the connection alive.
@@ -45,12 +45,17 @@ interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
45
45
  */
46
46
  declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
47
47
 
48
+ declare function toStandardMethod(method: string | undefined): string;
49
+
48
50
  declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
49
51
 
50
52
  interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
51
53
  }
52
54
  declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
53
55
 
54
- declare function toAbortSignal(res: NodeHttpResponse): AbortSignal;
56
+ declare function toAbortSignal(stream: Stream.Writable): AbortSignal;
57
+
58
+ declare function toStandardUrl(req: NodeHttpRequest): URL;
55
59
 
56
- export { type NodeHttpRequest, type NodeHttpResponse, type SendStandardResponseOptions, type ToEventStreamOptions, type ToNodeHttpBodyOptions, sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
60
+ export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
61
+ export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventStreamOptions, ToNodeHttpBodyOptions };
package/dist/index.mjs CHANGED
@@ -1,52 +1,49 @@
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 { createAsyncIteratorObject, parseEmptyableJSON, isTypescriptObject, stringifyJSON, isAsyncIteratorObject, once } from '@orpc/shared';
3
+ import { EventDecoderStream, withEventMeta, ErrorEvent, encodeEventMessage, getEventMeta, getFilenameFromContentDisposition, flattenHeader, generateContentDisposition } from '@orpc/standard-server';
4
4
 
5
5
  function toEventIterator(stream) {
6
6
  const eventStream = Readable.toWeb(stream).pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
7
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;
8
+ return createAsyncIteratorObject(async () => {
9
+ while (true) {
10
+ const { done, value } = await reader.read();
11
+ if (done) {
12
+ return { done: true, value: void 0 };
13
+ }
14
+ switch (value.event) {
15
+ case "message": {
16
+ let message = parseEmptyableJSON(value.data);
17
+ if (isTypescriptObject(message)) {
18
+ message = withEventMeta(message, value);
30
19
  }
31
- case "done": {
32
- let done2 = parseEmptyableJSON(value.data);
33
- if (isTypescriptObject(done2)) {
34
- done2 = withEventMeta(done2, value);
35
- }
36
- return done2;
20
+ return { done: false, value: message };
21
+ }
22
+ case "error": {
23
+ let error = new ErrorEvent({
24
+ data: parseEmptyableJSON(value.data)
25
+ });
26
+ error = withEventMeta(error, value);
27
+ throw error;
28
+ }
29
+ case "done": {
30
+ let done2 = parseEmptyableJSON(value.data);
31
+ if (isTypescriptObject(done2)) {
32
+ done2 = withEventMeta(done2, value);
37
33
  }
34
+ return { done: true, value: done2 };
38
35
  }
39
36
  }
40
- } finally {
41
- await reader.cancel();
42
37
  }
43
- }
44
- return gen();
38
+ }, async () => {
39
+ await reader.cancel();
40
+ });
45
41
  }
46
42
  function toEventStream(iterator, options = {}) {
47
43
  const keepAliveEnabled = options.eventIteratorKeepAliveEnabled ?? true;
48
44
  const keepAliveInterval = options.eventIteratorKeepAliveInterval ?? 5e3;
49
45
  const keepAliveComment = options.eventIteratorKeepAliveComment ?? "";
46
+ let cancelled = false;
50
47
  let timeout;
51
48
  const stream = new ReadableStream({
52
49
  async pull(controller) {
@@ -60,6 +57,9 @@ function toEventStream(iterator, options = {}) {
60
57
  }
61
58
  const value = await iterator.next();
62
59
  clearInterval(timeout);
60
+ if (cancelled) {
61
+ return;
62
+ }
63
63
  const meta = getEventMeta(value.value);
64
64
  if (!value.done || value.value !== void 0 || meta !== void 0) {
65
65
  controller.enqueue(encodeEventMessage({
@@ -73,6 +73,9 @@ function toEventStream(iterator, options = {}) {
73
73
  }
74
74
  } catch (err) {
75
75
  clearInterval(timeout);
76
+ if (cancelled) {
77
+ return;
78
+ }
76
79
  controller.enqueue(encodeEventMessage({
77
80
  ...getEventMeta(err),
78
81
  event: "error",
@@ -81,22 +84,16 @@ function toEventStream(iterator, options = {}) {
81
84
  controller.close();
82
85
  }
83
86
  },
84
- async cancel(reason) {
85
- if (reason) {
86
- await iterator.throw?.(reason);
87
- } else {
88
- await iterator.return?.();
89
- }
87
+ async cancel() {
88
+ cancelled = true;
89
+ clearInterval(timeout);
90
+ await iterator.return?.();
90
91
  }
91
92
  });
92
93
  return Readable.fromWeb(stream);
93
94
  }
94
95
 
95
96
  async function toStandardBody(req) {
96
- const method = req.method ?? "GET";
97
- if (method === "GET" || method === "HEAD") {
98
- return void 0;
99
- }
100
97
  const contentDisposition = req.headers["content-disposition"];
101
98
  const contentType = req.headers["content-type"];
102
99
  if (typeof contentDisposition === "string") {
@@ -123,7 +120,7 @@ async function toStandardBody(req) {
123
120
  return _streamToFile(req, "blob", contentType);
124
121
  }
125
122
  function toNodeHttpBody(body, headers, options = {}) {
126
- const currentContentDisposition = toArray(headers["content-disposition"])[0];
123
+ const currentContentDisposition = flattenHeader(headers["content-disposition"]);
127
124
  delete headers["content-type"];
128
125
  delete headers["content-disposition"];
129
126
  if (body === void 0) {
@@ -176,28 +173,32 @@ async function _streamToFile(stream, fileName, contentType) {
176
173
  return new File(chunks, fileName, { type: contentType });
177
174
  }
178
175
 
179
- function toAbortSignal(res) {
176
+ function toStandardMethod(method) {
177
+ return method ?? "GET";
178
+ }
179
+
180
+ function toAbortSignal(stream) {
180
181
  const controller = new AbortController();
181
- res.on("close", () => {
182
- if (res.errored) {
183
- controller.abort(res.errored.toString());
184
- } else if (!res.writableFinished) {
185
- controller.abort("Client connection prematurely closed.");
186
- } else {
187
- controller.abort("Server closed the connection.");
182
+ stream.once("error", (error) => controller.abort(error));
183
+ stream.once("close", () => {
184
+ if (!stream.writableFinished) {
185
+ controller.abort(new Error("Writable stream closed before it finished writing"));
188
186
  }
189
187
  });
190
188
  return controller.signal;
191
189
  }
192
190
 
193
- function toStandardLazyRequest(req, res) {
194
- const method = req.method ?? "GET";
191
+ function toStandardUrl(req) {
195
192
  const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
196
193
  const host = req.headers.host ?? "localhost";
197
194
  const url = new URL(req.originalUrl ?? req.url ?? "/", `${protocol}//${host}`);
195
+ return url;
196
+ }
197
+
198
+ function toStandardLazyRequest(req, res) {
198
199
  return {
199
- method,
200
- url,
200
+ method: toStandardMethod(req.method),
201
+ url: toStandardUrl(req),
201
202
  headers: req.headers,
202
203
  body: once(() => toStandardBody(req)),
203
204
  signal: toAbortSignal(res)
@@ -206,24 +207,25 @@ function toStandardLazyRequest(req, res) {
206
207
 
207
208
  function sendStandardResponse(res, standardResponse, options = {}) {
208
209
  return new Promise((resolve, reject) => {
209
- res.on("error", reject);
210
- res.on("finish", resolve);
211
- const resHeaders = standardResponse.headers;
210
+ res.once("error", reject);
211
+ res.once("close", resolve);
212
+ const resHeaders = { ...standardResponse.headers };
212
213
  const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options);
213
214
  res.writeHead(standardResponse.status, resHeaders);
214
215
  if (resBody === void 0) {
215
- res.end(resBody);
216
+ res.end();
216
217
  } else if (typeof resBody === "string") {
217
218
  res.end(resBody);
218
219
  } else {
219
- res.on("close", () => {
220
+ res.once("close", () => {
220
221
  if (!resBody.closed) {
221
222
  resBody.destroy(res.errored ?? void 0);
222
223
  }
223
224
  });
225
+ resBody.once("error", (error) => res.destroy(error));
224
226
  resBody.pipe(res);
225
227
  }
226
228
  });
227
229
  }
228
230
 
229
- export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
231
+ 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.6e49c5c",
4
+ "version": "0.0.0-next.6e7c532",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -23,13 +23,13 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@orpc/shared": "0.0.0-next.6e49c5c",
27
- "@orpc/standard-server": "0.0.0-next.6e49c5c"
26
+ "@orpc/shared": "0.0.0-next.6e7c532",
27
+ "@orpc/standard-server": "0.0.0-next.6e7c532"
28
28
  },
29
29
  "devDependencies": {
30
- "@types/node": "^22.13.1",
31
- "@types/supertest": "^6.0.2",
32
- "supertest": "^7.0.0"
30
+ "@types/node": "^22.15.17",
31
+ "@types/supertest": "^6.0.3",
32
+ "supertest": "^7.1.0"
33
33
  },
34
34
  "scripts": {
35
35
  "build": "unbuild",