@orpc/standard-server-node 0.0.0-next.cc4cb21 → 0.0.0-next.d0e429d

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 ADDED
@@ -0,0 +1,68 @@
1
+ <div align="center">
2
+ <image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 />
3
+ </div>
4
+
5
+ <h1></h1>
6
+
7
+ <div align="center">
8
+ <a href="https://codecov.io/gh/unnoq/orpc">
9
+ <img alt="codecov" src="https://codecov.io/gh/unnoq/orpc/branch/main/graph/badge.svg">
10
+ </a>
11
+ <a href="https://www.npmjs.com/package/@orpc/standard-server-node">
12
+ <img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fstandard-server-node?logo=npm" />
13
+ </a>
14
+ <a href="https://github.com/unnoq/orpc/blob/main/LICENSE">
15
+ <img alt="MIT License" src="https://img.shields.io/github/license/unnoq/orpc?logo=open-source-initiative" />
16
+ </a>
17
+ <a href="https://discord.gg/TXEbwRBvQn">
18
+ <img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
19
+ </a>
20
+ </div>
21
+
22
+ <h3 align="center">Typesafe APIs Made Simple 🪄</h3>
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.
25
+
26
+ ---
27
+
28
+ ## Highlights
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), 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.
46
+
47
+ ## Documentation
48
+
49
+ You can find the full documentation [here](https://orpc.unnoq.com).
50
+
51
+ ## Packages
52
+
53
+ - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
54
+ - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
55
+ - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
56
+ - [@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
+ - [@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
+ - [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
59
+ - [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
60
+ - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
61
+
62
+ ## `@orpc/standard-server-node`
63
+
64
+ [Node.js](https://nodejs.org) server adapter for oRPC.
65
+
66
+ ## License
67
+
68
+ Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
package/dist/index.js CHANGED
@@ -1,17 +1,16 @@
1
1
  // src/body.ts
2
2
  import { Readable as Readable2 } from "node:stream";
3
- import { isAsyncIteratorObject, parseEmptyableJSON as parseEmptyableJSON2 } from "@orpc/shared";
3
+ import { isAsyncIteratorObject, parseEmptyableJSON as parseEmptyableJSON2, stringifyJSON as stringifyJSON2 } from "@orpc/shared";
4
4
  import { contentDisposition, parseContentDisposition } from "@orpc/standard-server";
5
5
 
6
6
  // src/event-source.ts
7
7
  import { Readable } from "node:stream";
8
- import { isTypescriptObject, parseEmptyableJSON } from "@orpc/shared";
8
+ import { isTypescriptObject, parseEmptyableJSON, stringifyJSON } from "@orpc/shared";
9
9
  import {
10
10
  encodeEventMessage,
11
11
  ErrorEvent,
12
12
  EventDecoderStream,
13
13
  getEventMeta,
14
- UnknownEvent,
15
14
  withEventMeta
16
15
  } from "@orpc/standard-server";
17
16
  function toEventIterator(stream) {
@@ -47,14 +46,6 @@ function toEventIterator(stream) {
47
46
  }
48
47
  return done2;
49
48
  }
50
- default: {
51
- let error = new UnknownEvent({
52
- message: `Unknown event: ${value.event}`,
53
- data: parseEmptyableJSON(value.data)
54
- });
55
- error = withEventMeta(error, value);
56
- throw error;
57
- }
58
49
  }
59
50
  }
60
51
  } finally {
@@ -63,24 +54,37 @@ function toEventIterator(stream) {
63
54
  }
64
55
  return gen();
65
56
  }
66
- function toEventStream(iterator) {
57
+ function toEventStream(iterator, options = {}) {
58
+ const pingEnabled = options.eventSourcePingEnabled ?? true;
59
+ const pingInterval = options.eventSourcePingInterval ?? 5e3;
60
+ const pingContent = options.eventSourcePingContent ?? "";
61
+ let timeout;
67
62
  const stream = new ReadableStream({
68
63
  async pull(controller) {
69
64
  try {
65
+ if (pingEnabled) {
66
+ timeout = setInterval(() => {
67
+ controller.enqueue(encodeEventMessage({
68
+ comments: [pingContent]
69
+ }));
70
+ }, pingInterval);
71
+ }
70
72
  const value = await iterator.next();
73
+ clearInterval(timeout);
71
74
  controller.enqueue(encodeEventMessage({
72
75
  ...getEventMeta(value.value),
73
76
  event: value.done ? "done" : "message",
74
- data: JSON.stringify(value.value)
77
+ data: stringifyJSON(value.value)
75
78
  }));
76
79
  if (value.done) {
77
80
  controller.close();
78
81
  }
79
82
  } catch (err) {
83
+ clearInterval(timeout);
80
84
  controller.enqueue(encodeEventMessage({
81
85
  ...getEventMeta(err),
82
86
  event: "error",
83
- data: err instanceof ErrorEvent ? JSON.stringify(err.data) : void 0
87
+ data: err instanceof ErrorEvent ? stringifyJSON(err.data) : void 0
84
88
  }));
85
89
  controller.close();
86
90
  }
@@ -129,7 +133,7 @@ async function toStandardBody(req) {
129
133
  }
130
134
  return _streamToFile(req, "blob", contentType);
131
135
  }
132
- function toNodeHttpBody(body, headers) {
136
+ function toNodeHttpBody(body, headers, options = {}) {
133
137
  delete headers["content-type"];
134
138
  delete headers["content-disposition"];
135
139
  if (body === void 0) {
@@ -157,10 +161,10 @@ function toNodeHttpBody(body, headers) {
157
161
  headers["content-type"] = "text/event-stream";
158
162
  headers["cache-control"] = "no-cache";
159
163
  headers.connection = "keep-alive";
160
- return toEventStream(body);
164
+ return toEventStream(body, options);
161
165
  }
162
166
  headers["content-type"] = "application/json";
163
- return JSON.stringify(body);
167
+ return stringifyJSON2(body);
164
168
  }
165
169
  function _streamToFormData(stream, contentType) {
166
170
  const response = new Response(stream, {
@@ -220,12 +224,12 @@ function toStandardRequest(req, res) {
220
224
  }
221
225
 
222
226
  // src/response.ts
223
- function sendStandardResponse(res, standardResponse) {
227
+ function sendStandardResponse(res, standardResponse, options = {}) {
224
228
  return new Promise((resolve, reject) => {
225
229
  res.on("error", reject);
226
230
  res.on("finish", resolve);
227
231
  const resHeaders = standardResponse.headers;
228
- const resBody = toNodeHttpBody(standardResponse.body, resHeaders);
232
+ const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options);
229
233
  res.writeHead(standardResponse.status, resHeaders);
230
234
  if (resBody === void 0) {
231
235
  res.end(resBody);
@@ -1,10 +1,14 @@
1
1
  import type { StandardBody, StandardHeaders } from '@orpc/standard-server';
2
+ import type { ToEventStreamOptions } from './event-source';
2
3
  import type { NodeHttpRequest } from './types';
3
4
  import { Readable } from 'node:stream';
4
5
  export declare function toStandardBody(req: NodeHttpRequest): Promise<StandardBody>;
6
+ export interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
7
+ }
5
8
  /**
6
9
  * @param body
7
- * @param headers - The headers can be changed by the function and effects on the original headers.
10
+ * @param headers - WARNING: The headers can be changed by the function and effects on the original headers.
11
+ * @param options
8
12
  */
9
- export declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders): Readable | undefined | string;
13
+ export declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
10
14
  //# sourceMappingURL=body.d.ts.map
@@ -1,4 +1,24 @@
1
1
  import { Readable } from 'node:stream';
2
2
  export declare function toEventIterator(stream: Readable): AsyncGenerator<unknown | void, unknown | void, void>;
3
- export declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>): Readable;
3
+ export interface ToEventStreamOptions {
4
+ /**
5
+ * If true, a ping comment is sent periodically to keep the connection alive.
6
+ *
7
+ * @default true
8
+ */
9
+ eventSourcePingEnabled?: boolean;
10
+ /**
11
+ * Interval (in milliseconds) between ping comments sent after the last event.
12
+ *
13
+ * @default 5000
14
+ */
15
+ eventSourcePingInterval?: number;
16
+ /**
17
+ * The content of the ping comment. Must not include newline characters.
18
+ *
19
+ * @default ''
20
+ */
21
+ eventSourcePingContent?: string;
22
+ }
23
+ export declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
4
24
  //# sourceMappingURL=event-source.d.ts.map
@@ -1,4 +1,7 @@
1
1
  import type { StandardResponse } from '@orpc/standard-server';
2
+ import type { ToNodeHttpBodyOptions } from './body';
2
3
  import type { NodeHttpResponse } from './types';
3
- export declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse): Promise<void>;
4
+ export interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
5
+ }
6
+ export declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
4
7
  //# sourceMappingURL=response.d.ts.map
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.cc4cb21",
4
+ "version": "0.0.0-next.d0e429d",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -28,8 +28,8 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@orpc/standard-server": "0.0.0-next.cc4cb21",
32
- "@orpc/shared": "0.0.0-next.cc4cb21"
31
+ "@orpc/shared": "0.0.0-next.d0e429d",
32
+ "@orpc/standard-server": "0.0.0-next.d0e429d"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/node": "^22.13.1",