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

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,76 @@
1
+ <div align="center">
2
+ <image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
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
25
+
26
+ ---
27
+
28
+ ## Highlights
29
+
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.
42
+
43
+ ## Documentation
44
+
45
+ You can find the full documentation [here](https://orpc.unnoq.com).
46
+
47
+ ## Packages
48
+
49
+ - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
50
+ - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
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/).
54
+ - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
55
+ - [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
56
+ - [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
57
+ - [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
58
+ - [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
59
+ - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
60
+ - [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
61
+
62
+ ## `@orpc/standard-server-node`
63
+
64
+ [Node.js](https://nodejs.org) server adapter for oRPC.
65
+
66
+ ## Sponsors
67
+
68
+ <p align="center">
69
+ <a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
70
+ <img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
71
+ </a>
72
+ </p>
73
+
74
+ ## License
75
+
76
+ Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
@@ -0,0 +1,44 @@
1
+ import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
2
+ import { ToEventStreamOptions as ToEventStreamOptions$1 } from '@orpc/standard-server-fetch';
3
+ import Stream, { Readable } from 'node:stream';
4
+ import { IncomingMessage, ServerResponse } from 'node:http';
5
+ import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
6
+
7
+ declare function toEventIterator(stream: Readable): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
8
+ interface ToEventStreamOptions extends ToEventStreamOptions$1 {
9
+ }
10
+ declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
11
+
12
+ type NodeHttpRequest = (IncomingMessage | Http2ServerRequest) & {
13
+ /**
14
+ * Replace `req.url` with `req.originalUrl` when `req.originalUrl` is available.
15
+ * This is useful for `express.js` middleware.
16
+ */
17
+ originalUrl?: string;
18
+ };
19
+ type NodeHttpResponse = ServerResponse | Http2ServerResponse;
20
+
21
+ declare function toStandardBody(req: NodeHttpRequest): Promise<StandardBody>;
22
+ interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
23
+ }
24
+ /**
25
+ * @param body
26
+ * @param headers - WARNING: The headers can be changed by the function and effects on the original headers.
27
+ * @param options
28
+ */
29
+ declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
30
+
31
+ declare function toStandardMethod(method: string | undefined): string;
32
+
33
+ declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
34
+
35
+ interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
36
+ }
37
+ declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
38
+
39
+ declare function toAbortSignal(stream: Stream.Writable): AbortSignal;
40
+
41
+ declare function toStandardUrl(req: NodeHttpRequest): URL;
42
+
43
+ export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
44
+ export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventStreamOptions, ToNodeHttpBodyOptions };
@@ -0,0 +1,44 @@
1
+ import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
2
+ import { ToEventStreamOptions as ToEventStreamOptions$1 } from '@orpc/standard-server-fetch';
3
+ import Stream, { Readable } from 'node:stream';
4
+ import { IncomingMessage, ServerResponse } from 'node:http';
5
+ import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
6
+
7
+ declare function toEventIterator(stream: Readable): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
8
+ interface ToEventStreamOptions extends ToEventStreamOptions$1 {
9
+ }
10
+ declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
11
+
12
+ type NodeHttpRequest = (IncomingMessage | Http2ServerRequest) & {
13
+ /**
14
+ * Replace `req.url` with `req.originalUrl` when `req.originalUrl` is available.
15
+ * This is useful for `express.js` middleware.
16
+ */
17
+ originalUrl?: string;
18
+ };
19
+ type NodeHttpResponse = ServerResponse | Http2ServerResponse;
20
+
21
+ declare function toStandardBody(req: NodeHttpRequest): Promise<StandardBody>;
22
+ interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
23
+ }
24
+ /**
25
+ * @param body
26
+ * @param headers - WARNING: The headers can be changed by the function and effects on the original headers.
27
+ * @param options
28
+ */
29
+ declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
30
+
31
+ declare function toStandardMethod(method: string | undefined): string;
32
+
33
+ declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
34
+
35
+ interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
36
+ }
37
+ declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
38
+
39
+ declare function toAbortSignal(stream: Stream.Writable): AbortSignal;
40
+
41
+ declare function toStandardUrl(req: NodeHttpRequest): URL;
42
+
43
+ export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
44
+ export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventStreamOptions, ToNodeHttpBodyOptions };
package/dist/index.mjs ADDED
@@ -0,0 +1,146 @@
1
+ import { Readable } from 'node:stream';
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';
5
+
6
+ function toEventIterator(stream) {
7
+ return toEventIterator$1(Readable.toWeb(stream));
8
+ }
9
+ function toEventStream(iterator, options = {}) {
10
+ return Readable.fromWeb(toEventStream$1(iterator, options));
11
+ }
12
+
13
+ async function toStandardBody(req) {
14
+ const contentDisposition = req.headers["content-disposition"];
15
+ const contentType = req.headers["content-type"];
16
+ if (typeof contentDisposition === "string") {
17
+ const fileName = getFilenameFromContentDisposition(contentDisposition) ?? "blob";
18
+ return _streamToFile(req, fileName, contentType ?? "");
19
+ }
20
+ if (!contentType || contentType.startsWith("application/json")) {
21
+ const text = await _streamToString(req);
22
+ return parseEmptyableJSON(text);
23
+ }
24
+ if (contentType.startsWith("multipart/form-data")) {
25
+ return _streamToFormData(req, contentType);
26
+ }
27
+ if (contentType.startsWith("application/x-www-form-urlencoded")) {
28
+ const text = await _streamToString(req);
29
+ return new URLSearchParams(text);
30
+ }
31
+ if (contentType.startsWith("text/event-stream")) {
32
+ return toEventIterator(req);
33
+ }
34
+ if (contentType.startsWith("text/plain")) {
35
+ return _streamToString(req);
36
+ }
37
+ return _streamToFile(req, "blob", contentType);
38
+ }
39
+ function toNodeHttpBody(body, headers, options = {}) {
40
+ const currentContentDisposition = flattenHeader(headers["content-disposition"]);
41
+ delete headers["content-type"];
42
+ delete headers["content-disposition"];
43
+ if (body === void 0) {
44
+ return;
45
+ }
46
+ if (body instanceof Blob) {
47
+ headers["content-type"] = body.type;
48
+ headers["content-length"] = body.size.toString();
49
+ headers["content-disposition"] = currentContentDisposition ?? generateContentDisposition(body instanceof File ? body.name : "blob");
50
+ return Readable.fromWeb(body.stream());
51
+ }
52
+ if (body instanceof FormData) {
53
+ const response = new Response(body);
54
+ headers["content-type"] = response.headers.get("content-type");
55
+ return Readable.fromWeb(response.body);
56
+ }
57
+ if (body instanceof URLSearchParams) {
58
+ headers["content-type"] = "application/x-www-form-urlencoded";
59
+ return body.toString();
60
+ }
61
+ if (isAsyncIteratorObject(body)) {
62
+ headers["content-type"] = "text/event-stream";
63
+ return toEventStream(body, options);
64
+ }
65
+ headers["content-type"] = "application/json";
66
+ return stringifyJSON(body);
67
+ }
68
+ function _streamToFormData(stream, contentType) {
69
+ const response = new Response(stream, {
70
+ headers: {
71
+ "content-type": contentType
72
+ }
73
+ });
74
+ return response.formData();
75
+ }
76
+ async function _streamToString(stream) {
77
+ let string = "";
78
+ for await (const chunk of stream) {
79
+ string += chunk.toString();
80
+ }
81
+ return string;
82
+ }
83
+ async function _streamToFile(stream, fileName, contentType) {
84
+ const chunks = [];
85
+ for await (const chunk of stream) {
86
+ chunks.push(chunk);
87
+ }
88
+ return new File(chunks, fileName, { type: contentType });
89
+ }
90
+
91
+ function toStandardMethod(method) {
92
+ return method ?? "GET";
93
+ }
94
+
95
+ function toAbortSignal(stream) {
96
+ const controller = new AbortController();
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"));
101
+ }
102
+ });
103
+ return controller.signal;
104
+ }
105
+
106
+ function toStandardUrl(req) {
107
+ const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
108
+ const host = req.headers.host ?? "localhost";
109
+ const url = new URL(req.originalUrl ?? req.url ?? "/", `${protocol}//${host}`);
110
+ return url;
111
+ }
112
+
113
+ function toStandardLazyRequest(req, res) {
114
+ return {
115
+ method: toStandardMethod(req.method),
116
+ url: toStandardUrl(req),
117
+ headers: req.headers,
118
+ body: once(() => toStandardBody(req)),
119
+ signal: toAbortSignal(res)
120
+ };
121
+ }
122
+
123
+ function sendStandardResponse(res, standardResponse, options = {}) {
124
+ return new Promise((resolve, reject) => {
125
+ res.once("error", reject);
126
+ res.once("close", resolve);
127
+ const resHeaders = { ...standardResponse.headers };
128
+ const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options);
129
+ res.writeHead(standardResponse.status, resHeaders);
130
+ if (resBody === void 0) {
131
+ res.end();
132
+ } else if (typeof resBody === "string") {
133
+ res.end(resBody);
134
+ } else {
135
+ res.once("close", () => {
136
+ if (!resBody.closed) {
137
+ resBody.destroy(res.errored ?? void 0);
138
+ }
139
+ });
140
+ resBody.once("error", (error) => res.destroy(error));
141
+ resBody.pipe(res);
142
+ }
143
+ });
144
+ }
145
+
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.cc4cb21",
4
+ "version": "0.0.0-next.cd10cfb",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -14,30 +14,26 @@
14
14
  ],
15
15
  "exports": {
16
16
  ".": {
17
- "types": "./dist/src/index.d.ts",
18
- "import": "./dist/index.js",
19
- "default": "./dist/index.js"
20
- },
21
- "./🔒/*": {
22
- "types": "./dist/src/*.d.ts"
17
+ "types": "./dist/index.d.mts",
18
+ "import": "./dist/index.mjs",
19
+ "default": "./dist/index.mjs"
23
20
  }
24
21
  },
25
22
  "files": [
26
- "!**/*.map",
27
- "!**/*.tsbuildinfo",
28
23
  "dist"
29
24
  ],
30
25
  "dependencies": {
31
- "@orpc/standard-server": "0.0.0-next.cc4cb21",
32
- "@orpc/shared": "0.0.0-next.cc4cb21"
26
+ "@orpc/shared": "0.0.0-next.cd10cfb",
27
+ "@orpc/standard-server": "0.0.0-next.cd10cfb",
28
+ "@orpc/standard-server-fetch": "0.0.0-next.cd10cfb"
33
29
  },
34
30
  "devDependencies": {
35
- "@types/node": "^22.13.1",
36
- "@types/supertest": "^6.0.2",
37
- "supertest": "^7.0.0"
31
+ "@types/node": "^22.15.18",
32
+ "@types/supertest": "^6.0.3",
33
+ "supertest": "^7.1.0"
38
34
  },
39
35
  "scripts": {
40
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
36
+ "build": "unbuild",
41
37
  "build:watch": "pnpm run build --watch",
42
38
  "type:check": "tsc -b"
43
39
  }
package/dist/index.js DELETED
@@ -1,253 +0,0 @@
1
- // src/body.ts
2
- import { Readable as Readable2 } from "node:stream";
3
- import { isAsyncIteratorObject, parseEmptyableJSON as parseEmptyableJSON2 } from "@orpc/shared";
4
- import { contentDisposition, parseContentDisposition } from "@orpc/standard-server";
5
-
6
- // src/event-source.ts
7
- import { Readable } from "node:stream";
8
- import { isTypescriptObject, parseEmptyableJSON } from "@orpc/shared";
9
- import {
10
- encodeEventMessage,
11
- ErrorEvent,
12
- EventDecoderStream,
13
- getEventMeta,
14
- UnknownEvent,
15
- withEventMeta
16
- } from "@orpc/standard-server";
17
- function toEventIterator(stream) {
18
- const eventStream = Readable.toWeb(stream).pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
19
- const reader = eventStream.getReader();
20
- async function* gen() {
21
- try {
22
- while (true) {
23
- const { done, value } = await reader.read();
24
- if (done) {
25
- return;
26
- }
27
- switch (value.event) {
28
- case "message": {
29
- let message = parseEmptyableJSON(value.data);
30
- if (isTypescriptObject(message)) {
31
- message = withEventMeta(message, value);
32
- }
33
- yield message;
34
- break;
35
- }
36
- case "error": {
37
- let error = new ErrorEvent({
38
- data: parseEmptyableJSON(value.data)
39
- });
40
- error = withEventMeta(error, value);
41
- throw error;
42
- }
43
- case "done": {
44
- let done2 = parseEmptyableJSON(value.data);
45
- if (isTypescriptObject(done2)) {
46
- done2 = withEventMeta(done2, value);
47
- }
48
- return done2;
49
- }
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
- }
59
- }
60
- } finally {
61
- await reader.cancel();
62
- }
63
- }
64
- return gen();
65
- }
66
- function toEventStream(iterator) {
67
- const stream = new ReadableStream({
68
- async pull(controller) {
69
- try {
70
- const value = await iterator.next();
71
- controller.enqueue(encodeEventMessage({
72
- ...getEventMeta(value.value),
73
- event: value.done ? "done" : "message",
74
- data: JSON.stringify(value.value)
75
- }));
76
- if (value.done) {
77
- controller.close();
78
- }
79
- } catch (err) {
80
- controller.enqueue(encodeEventMessage({
81
- ...getEventMeta(err),
82
- event: "error",
83
- data: err instanceof ErrorEvent ? JSON.stringify(err.data) : void 0
84
- }));
85
- controller.close();
86
- }
87
- },
88
- async cancel(reason) {
89
- if (reason) {
90
- await iterator.throw?.(reason);
91
- } else {
92
- await iterator.return?.();
93
- }
94
- }
95
- });
96
- return Readable.fromWeb(stream);
97
- }
98
-
99
- // src/body.ts
100
- async function toStandardBody(req) {
101
- const method = req.method ?? "GET";
102
- if (method === "GET" || method === "HEAD") {
103
- return void 0;
104
- }
105
- const contentDisposition2 = req.headers["content-disposition"];
106
- const contentType = req.headers["content-type"];
107
- if (contentDisposition2) {
108
- const fileName = parseContentDisposition(contentDisposition2).parameters.filename;
109
- if (typeof fileName === "string") {
110
- return _streamToFile(req, fileName, contentType ?? "");
111
- }
112
- }
113
- if (!contentType || contentType.startsWith("application/json")) {
114
- const text = await _streamToString(req);
115
- return parseEmptyableJSON2(text);
116
- }
117
- if (contentType.startsWith("multipart/form-data")) {
118
- return _streamToFormData(req, contentType);
119
- }
120
- if (contentType.startsWith("application/x-www-form-urlencoded")) {
121
- const text = await _streamToString(req);
122
- return new URLSearchParams(text);
123
- }
124
- if (contentType.startsWith("text/event-stream")) {
125
- return toEventIterator(req);
126
- }
127
- if (contentType.startsWith("text/plain")) {
128
- return _streamToString(req);
129
- }
130
- return _streamToFile(req, "blob", contentType);
131
- }
132
- function toNodeHttpBody(body, headers) {
133
- delete headers["content-type"];
134
- delete headers["content-disposition"];
135
- if (body === void 0) {
136
- return;
137
- }
138
- if (body instanceof Blob) {
139
- headers["content-type"] = body.type;
140
- headers["content-length"] = body.size.toString();
141
- headers["content-disposition"] = contentDisposition(
142
- body instanceof File ? body.name : "blob",
143
- { type: "inline" }
144
- );
145
- return Readable2.fromWeb(body.stream());
146
- }
147
- if (body instanceof FormData) {
148
- const response = new Response(body);
149
- headers["content-type"] = response.headers.get("content-type");
150
- return Readable2.fromWeb(response.body);
151
- }
152
- if (body instanceof URLSearchParams) {
153
- headers["content-type"] = "application/x-www-form-urlencoded";
154
- return body.toString();
155
- }
156
- if (isAsyncIteratorObject(body)) {
157
- headers["content-type"] = "text/event-stream";
158
- headers["cache-control"] = "no-cache";
159
- headers.connection = "keep-alive";
160
- return toEventStream(body);
161
- }
162
- headers["content-type"] = "application/json";
163
- return JSON.stringify(body);
164
- }
165
- function _streamToFormData(stream, contentType) {
166
- const response = new Response(stream, {
167
- headers: {
168
- "content-type": contentType
169
- }
170
- });
171
- return response.formData();
172
- }
173
- async function _streamToString(stream) {
174
- let string = "";
175
- for await (const chunk of stream) {
176
- string += chunk.toString();
177
- }
178
- return string;
179
- }
180
- async function _streamToFile(stream, fileName, contentType) {
181
- const chunks = [];
182
- for await (const chunk of stream) {
183
- chunks.push(chunk);
184
- }
185
- return new File(chunks, fileName, { type: contentType });
186
- }
187
-
188
- // src/request.ts
189
- import { once } from "@orpc/shared";
190
-
191
- // src/signal.ts
192
- function toAbortSignal(res) {
193
- const controller = new AbortController();
194
- res.on("close", () => {
195
- if (res.errored) {
196
- controller.abort(res.errored.toString());
197
- } else if (!res.writableFinished) {
198
- controller.abort("Client connection prematurely closed.");
199
- } else {
200
- controller.abort("Server closed the connection.");
201
- }
202
- });
203
- return controller.signal;
204
- }
205
-
206
- // src/request.ts
207
- function toStandardRequest(req, res) {
208
- const method = req.method ?? "GET";
209
- const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
210
- const host = req.headers.host ?? "localhost";
211
- const url = new URL(req.originalUrl ?? req.url ?? "/", `${protocol}//${host}`);
212
- return {
213
- raw: { request: req, response: res },
214
- method,
215
- url,
216
- headers: req.headers,
217
- body: once(() => toStandardBody(req)),
218
- signal: toAbortSignal(res)
219
- };
220
- }
221
-
222
- // src/response.ts
223
- function sendStandardResponse(res, standardResponse) {
224
- return new Promise((resolve, reject) => {
225
- res.on("error", reject);
226
- res.on("finish", resolve);
227
- const resHeaders = standardResponse.headers;
228
- const resBody = toNodeHttpBody(standardResponse.body, resHeaders);
229
- res.writeHead(standardResponse.status, resHeaders);
230
- if (resBody === void 0) {
231
- res.end(resBody);
232
- } else if (typeof resBody === "string") {
233
- res.end(resBody);
234
- } else {
235
- res.on("close", () => {
236
- if (!resBody.closed) {
237
- resBody.destroy(res.errored ?? void 0);
238
- }
239
- });
240
- resBody.pipe(res);
241
- }
242
- });
243
- }
244
- export {
245
- sendStandardResponse,
246
- toAbortSignal,
247
- toEventIterator,
248
- toEventStream,
249
- toNodeHttpBody,
250
- toStandardBody,
251
- toStandardRequest
252
- };
253
- //# sourceMappingURL=index.js.map
@@ -1,10 +0,0 @@
1
- import type { StandardBody, StandardHeaders } from '@orpc/standard-server';
2
- import type { NodeHttpRequest } from './types';
3
- import { Readable } from 'node:stream';
4
- export declare function toStandardBody(req: NodeHttpRequest): Promise<StandardBody>;
5
- /**
6
- * @param body
7
- * @param headers - The headers can be changed by the function and effects on the original headers.
8
- */
9
- export declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders): Readable | undefined | string;
10
- //# sourceMappingURL=body.d.ts.map
@@ -1,4 +0,0 @@
1
- import { Readable } from 'node:stream';
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;
4
- //# sourceMappingURL=event-source.d.ts.map
@@ -1,7 +0,0 @@
1
- export * from './body';
2
- export * from './event-source';
3
- export * from './request';
4
- export * from './response';
5
- export * from './signal';
6
- export * from './types';
7
- //# sourceMappingURL=index.d.ts.map
@@ -1,4 +0,0 @@
1
- import type { StandardRequest } from '@orpc/standard-server';
2
- import type { NodeHttpRequest, NodeHttpResponse } from './types';
3
- export declare function toStandardRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardRequest;
4
- //# sourceMappingURL=request.d.ts.map
@@ -1,4 +0,0 @@
1
- import type { StandardResponse } from '@orpc/standard-server';
2
- import type { NodeHttpResponse } from './types';
3
- export declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse): Promise<void>;
4
- //# sourceMappingURL=response.d.ts.map
@@ -1,3 +0,0 @@
1
- import type { NodeHttpResponse } from './types';
2
- export declare function toAbortSignal(res: NodeHttpResponse): AbortSignal;
3
- //# sourceMappingURL=signal.d.ts.map
@@ -1,11 +0,0 @@
1
- import type { IncomingMessage, ServerResponse } from 'node:http';
2
- import type { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
3
- export type NodeHttpRequest = (IncomingMessage | Http2ServerRequest) & {
4
- /**
5
- * Replace `req.url` with `req.originalUrl` when `req.originalUrl` is available.
6
- * This is useful for `express.js` middleware.
7
- */
8
- originalUrl?: string;
9
- };
10
- export type NodeHttpResponse = ServerResponse | Http2ServerResponse;
11
- //# sourceMappingURL=types.d.ts.map