@orpc/standard-server-node 0.0.0-next.d0e429d → 0.0.0-next.d146c28
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 +29 -21
- package/dist/index.d.mts +44 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.mjs +146 -0
- package/package.json +11 -15
- package/dist/index.js +0 -257
- package/dist/src/body.d.ts +0 -14
- package/dist/src/event-source.d.ts +0 -24
- package/dist/src/index.d.ts +0 -7
- package/dist/src/request.d.ts +0 -4
- package/dist/src/response.d.ts +0 -7
- package/dist/src/signal.d.ts +0 -3
- package/dist/src/types.d.ts +0 -11
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 />
|
|
2
|
+
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
|
|
3
3
|
</div>
|
|
4
4
|
|
|
5
5
|
<h1></h1>
|
|
@@ -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
|
|
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
|
-
-
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
41
|
-
-
|
|
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, Angular), 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,16 +49,28 @@ 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.
|
|
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
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.
|
|
60
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
61
|
|
|
62
62
|
## `@orpc/standard-server-node`
|
|
63
63
|
|
|
64
64
|
[Node.js](https://nodejs.org) server adapter for oRPC.
|
|
65
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
|
+
|
|
66
74
|
## License
|
|
67
75
|
|
|
68
76
|
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
package/dist/index.d.mts
ADDED
|
@@ -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.d.ts
ADDED
|
@@ -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.
|
|
4
|
+
"version": "0.0.0-next.d146c28",
|
|
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/
|
|
18
|
-
"import": "./dist/index.
|
|
19
|
-
"default": "./dist/index.
|
|
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/shared": "0.0.0-next.
|
|
32
|
-
"@orpc/standard-server": "0.0.0-next.
|
|
26
|
+
"@orpc/shared": "0.0.0-next.d146c28",
|
|
27
|
+
"@orpc/standard-server-fetch": "0.0.0-next.d146c28",
|
|
28
|
+
"@orpc/standard-server": "0.0.0-next.d146c28"
|
|
33
29
|
},
|
|
34
30
|
"devDependencies": {
|
|
35
|
-
"@types/node": "^22.
|
|
36
|
-
"@types/supertest": "^6.0.
|
|
37
|
-
"supertest": "^7.
|
|
31
|
+
"@types/node": "^22.15.30",
|
|
32
|
+
"@types/supertest": "^6.0.3",
|
|
33
|
+
"supertest": "^7.1.3"
|
|
38
34
|
},
|
|
39
35
|
"scripts": {
|
|
40
|
-
"build": "
|
|
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,257 +0,0 @@
|
|
|
1
|
-
// src/body.ts
|
|
2
|
-
import { Readable as Readable2 } from "node:stream";
|
|
3
|
-
import { isAsyncIteratorObject, parseEmptyableJSON as parseEmptyableJSON2, stringifyJSON as stringifyJSON2 } 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, stringifyJSON } from "@orpc/shared";
|
|
9
|
-
import {
|
|
10
|
-
encodeEventMessage,
|
|
11
|
-
ErrorEvent,
|
|
12
|
-
EventDecoderStream,
|
|
13
|
-
getEventMeta,
|
|
14
|
-
withEventMeta
|
|
15
|
-
} from "@orpc/standard-server";
|
|
16
|
-
function toEventIterator(stream) {
|
|
17
|
-
const eventStream = Readable.toWeb(stream).pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
|
|
18
|
-
const reader = eventStream.getReader();
|
|
19
|
-
async function* gen() {
|
|
20
|
-
try {
|
|
21
|
-
while (true) {
|
|
22
|
-
const { done, value } = await reader.read();
|
|
23
|
-
if (done) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
switch (value.event) {
|
|
27
|
-
case "message": {
|
|
28
|
-
let message = parseEmptyableJSON(value.data);
|
|
29
|
-
if (isTypescriptObject(message)) {
|
|
30
|
-
message = withEventMeta(message, value);
|
|
31
|
-
}
|
|
32
|
-
yield message;
|
|
33
|
-
break;
|
|
34
|
-
}
|
|
35
|
-
case "error": {
|
|
36
|
-
let error = new ErrorEvent({
|
|
37
|
-
data: parseEmptyableJSON(value.data)
|
|
38
|
-
});
|
|
39
|
-
error = withEventMeta(error, value);
|
|
40
|
-
throw error;
|
|
41
|
-
}
|
|
42
|
-
case "done": {
|
|
43
|
-
let done2 = parseEmptyableJSON(value.data);
|
|
44
|
-
if (isTypescriptObject(done2)) {
|
|
45
|
-
done2 = withEventMeta(done2, value);
|
|
46
|
-
}
|
|
47
|
-
return done2;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
} finally {
|
|
52
|
-
await reader.cancel();
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return gen();
|
|
56
|
-
}
|
|
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;
|
|
62
|
-
const stream = new ReadableStream({
|
|
63
|
-
async pull(controller) {
|
|
64
|
-
try {
|
|
65
|
-
if (pingEnabled) {
|
|
66
|
-
timeout = setInterval(() => {
|
|
67
|
-
controller.enqueue(encodeEventMessage({
|
|
68
|
-
comments: [pingContent]
|
|
69
|
-
}));
|
|
70
|
-
}, pingInterval);
|
|
71
|
-
}
|
|
72
|
-
const value = await iterator.next();
|
|
73
|
-
clearInterval(timeout);
|
|
74
|
-
controller.enqueue(encodeEventMessage({
|
|
75
|
-
...getEventMeta(value.value),
|
|
76
|
-
event: value.done ? "done" : "message",
|
|
77
|
-
data: stringifyJSON(value.value)
|
|
78
|
-
}));
|
|
79
|
-
if (value.done) {
|
|
80
|
-
controller.close();
|
|
81
|
-
}
|
|
82
|
-
} catch (err) {
|
|
83
|
-
clearInterval(timeout);
|
|
84
|
-
controller.enqueue(encodeEventMessage({
|
|
85
|
-
...getEventMeta(err),
|
|
86
|
-
event: "error",
|
|
87
|
-
data: err instanceof ErrorEvent ? stringifyJSON(err.data) : void 0
|
|
88
|
-
}));
|
|
89
|
-
controller.close();
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
async cancel(reason) {
|
|
93
|
-
if (reason) {
|
|
94
|
-
await iterator.throw?.(reason);
|
|
95
|
-
} else {
|
|
96
|
-
await iterator.return?.();
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
return Readable.fromWeb(stream);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// src/body.ts
|
|
104
|
-
async function toStandardBody(req) {
|
|
105
|
-
const method = req.method ?? "GET";
|
|
106
|
-
if (method === "GET" || method === "HEAD") {
|
|
107
|
-
return void 0;
|
|
108
|
-
}
|
|
109
|
-
const contentDisposition2 = req.headers["content-disposition"];
|
|
110
|
-
const contentType = req.headers["content-type"];
|
|
111
|
-
if (contentDisposition2) {
|
|
112
|
-
const fileName = parseContentDisposition(contentDisposition2).parameters.filename;
|
|
113
|
-
if (typeof fileName === "string") {
|
|
114
|
-
return _streamToFile(req, fileName, contentType ?? "");
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
if (!contentType || contentType.startsWith("application/json")) {
|
|
118
|
-
const text = await _streamToString(req);
|
|
119
|
-
return parseEmptyableJSON2(text);
|
|
120
|
-
}
|
|
121
|
-
if (contentType.startsWith("multipart/form-data")) {
|
|
122
|
-
return _streamToFormData(req, contentType);
|
|
123
|
-
}
|
|
124
|
-
if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
|
125
|
-
const text = await _streamToString(req);
|
|
126
|
-
return new URLSearchParams(text);
|
|
127
|
-
}
|
|
128
|
-
if (contentType.startsWith("text/event-stream")) {
|
|
129
|
-
return toEventIterator(req);
|
|
130
|
-
}
|
|
131
|
-
if (contentType.startsWith("text/plain")) {
|
|
132
|
-
return _streamToString(req);
|
|
133
|
-
}
|
|
134
|
-
return _streamToFile(req, "blob", contentType);
|
|
135
|
-
}
|
|
136
|
-
function toNodeHttpBody(body, headers, options = {}) {
|
|
137
|
-
delete headers["content-type"];
|
|
138
|
-
delete headers["content-disposition"];
|
|
139
|
-
if (body === void 0) {
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
if (body instanceof Blob) {
|
|
143
|
-
headers["content-type"] = body.type;
|
|
144
|
-
headers["content-length"] = body.size.toString();
|
|
145
|
-
headers["content-disposition"] = contentDisposition(
|
|
146
|
-
body instanceof File ? body.name : "blob",
|
|
147
|
-
{ type: "inline" }
|
|
148
|
-
);
|
|
149
|
-
return Readable2.fromWeb(body.stream());
|
|
150
|
-
}
|
|
151
|
-
if (body instanceof FormData) {
|
|
152
|
-
const response = new Response(body);
|
|
153
|
-
headers["content-type"] = response.headers.get("content-type");
|
|
154
|
-
return Readable2.fromWeb(response.body);
|
|
155
|
-
}
|
|
156
|
-
if (body instanceof URLSearchParams) {
|
|
157
|
-
headers["content-type"] = "application/x-www-form-urlencoded";
|
|
158
|
-
return body.toString();
|
|
159
|
-
}
|
|
160
|
-
if (isAsyncIteratorObject(body)) {
|
|
161
|
-
headers["content-type"] = "text/event-stream";
|
|
162
|
-
headers["cache-control"] = "no-cache";
|
|
163
|
-
headers.connection = "keep-alive";
|
|
164
|
-
return toEventStream(body, options);
|
|
165
|
-
}
|
|
166
|
-
headers["content-type"] = "application/json";
|
|
167
|
-
return stringifyJSON2(body);
|
|
168
|
-
}
|
|
169
|
-
function _streamToFormData(stream, contentType) {
|
|
170
|
-
const response = new Response(stream, {
|
|
171
|
-
headers: {
|
|
172
|
-
"content-type": contentType
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
return response.formData();
|
|
176
|
-
}
|
|
177
|
-
async function _streamToString(stream) {
|
|
178
|
-
let string = "";
|
|
179
|
-
for await (const chunk of stream) {
|
|
180
|
-
string += chunk.toString();
|
|
181
|
-
}
|
|
182
|
-
return string;
|
|
183
|
-
}
|
|
184
|
-
async function _streamToFile(stream, fileName, contentType) {
|
|
185
|
-
const chunks = [];
|
|
186
|
-
for await (const chunk of stream) {
|
|
187
|
-
chunks.push(chunk);
|
|
188
|
-
}
|
|
189
|
-
return new File(chunks, fileName, { type: contentType });
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// src/request.ts
|
|
193
|
-
import { once } from "@orpc/shared";
|
|
194
|
-
|
|
195
|
-
// src/signal.ts
|
|
196
|
-
function toAbortSignal(res) {
|
|
197
|
-
const controller = new AbortController();
|
|
198
|
-
res.on("close", () => {
|
|
199
|
-
if (res.errored) {
|
|
200
|
-
controller.abort(res.errored.toString());
|
|
201
|
-
} else if (!res.writableFinished) {
|
|
202
|
-
controller.abort("Client connection prematurely closed.");
|
|
203
|
-
} else {
|
|
204
|
-
controller.abort("Server closed the connection.");
|
|
205
|
-
}
|
|
206
|
-
});
|
|
207
|
-
return controller.signal;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// src/request.ts
|
|
211
|
-
function toStandardRequest(req, res) {
|
|
212
|
-
const method = req.method ?? "GET";
|
|
213
|
-
const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
|
|
214
|
-
const host = req.headers.host ?? "localhost";
|
|
215
|
-
const url = new URL(req.originalUrl ?? req.url ?? "/", `${protocol}//${host}`);
|
|
216
|
-
return {
|
|
217
|
-
raw: { request: req, response: res },
|
|
218
|
-
method,
|
|
219
|
-
url,
|
|
220
|
-
headers: req.headers,
|
|
221
|
-
body: once(() => toStandardBody(req)),
|
|
222
|
-
signal: toAbortSignal(res)
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
// src/response.ts
|
|
227
|
-
function sendStandardResponse(res, standardResponse, options = {}) {
|
|
228
|
-
return new Promise((resolve, reject) => {
|
|
229
|
-
res.on("error", reject);
|
|
230
|
-
res.on("finish", resolve);
|
|
231
|
-
const resHeaders = standardResponse.headers;
|
|
232
|
-
const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options);
|
|
233
|
-
res.writeHead(standardResponse.status, resHeaders);
|
|
234
|
-
if (resBody === void 0) {
|
|
235
|
-
res.end(resBody);
|
|
236
|
-
} else if (typeof resBody === "string") {
|
|
237
|
-
res.end(resBody);
|
|
238
|
-
} else {
|
|
239
|
-
res.on("close", () => {
|
|
240
|
-
if (!resBody.closed) {
|
|
241
|
-
resBody.destroy(res.errored ?? void 0);
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
resBody.pipe(res);
|
|
245
|
-
}
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
export {
|
|
249
|
-
sendStandardResponse,
|
|
250
|
-
toAbortSignal,
|
|
251
|
-
toEventIterator,
|
|
252
|
-
toEventStream,
|
|
253
|
-
toNodeHttpBody,
|
|
254
|
-
toStandardBody,
|
|
255
|
-
toStandardRequest
|
|
256
|
-
};
|
|
257
|
-
//# sourceMappingURL=index.js.map
|
package/dist/src/body.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { StandardBody, StandardHeaders } from '@orpc/standard-server';
|
|
2
|
-
import type { ToEventStreamOptions } from './event-source';
|
|
3
|
-
import type { NodeHttpRequest } from './types';
|
|
4
|
-
import { Readable } from 'node:stream';
|
|
5
|
-
export declare function toStandardBody(req: NodeHttpRequest): Promise<StandardBody>;
|
|
6
|
-
export interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* @param body
|
|
10
|
-
* @param headers - WARNING: The headers can be changed by the function and effects on the original headers.
|
|
11
|
-
* @param options
|
|
12
|
-
*/
|
|
13
|
-
export declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
|
|
14
|
-
//# sourceMappingURL=body.d.ts.map
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Readable } from 'node:stream';
|
|
2
|
-
export declare function toEventIterator(stream: Readable): AsyncGenerator<unknown | void, unknown | void, void>;
|
|
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;
|
|
24
|
-
//# sourceMappingURL=event-source.d.ts.map
|
package/dist/src/index.d.ts
DELETED
package/dist/src/request.d.ts
DELETED
|
@@ -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
|
package/dist/src/response.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { StandardResponse } from '@orpc/standard-server';
|
|
2
|
-
import type { ToNodeHttpBodyOptions } from './body';
|
|
3
|
-
import type { NodeHttpResponse } from './types';
|
|
4
|
-
export interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
|
|
5
|
-
}
|
|
6
|
-
export declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
|
|
7
|
-
//# sourceMappingURL=response.d.ts.map
|
package/dist/src/signal.d.ts
DELETED
package/dist/src/types.d.ts
DELETED
|
@@ -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
|