@orpc/standard-server-node 0.0.0-next.e7ee5a9 → 0.0.0-next.e8416db
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 +35 -22
- package/dist/index.d.mts +25 -27
- package/dist/index.d.ts +25 -27
- package/dist/index.mjs +74 -143
- package/package.json +8 -7
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<image align="center" src="https://orpc.
|
|
2
|
+
<image align="center" src="https://orpc.dev/logo.webp" width=280 alt="oRPC logo" />
|
|
3
3
|
</div>
|
|
4
4
|
|
|
5
5
|
<h1></h1>
|
|
@@ -17,52 +17,65 @@
|
|
|
17
17
|
<a href="https://discord.gg/TXEbwRBvQn">
|
|
18
18
|
<img alt="Discord" src="https://img.shields.io/discord/1308966753044398161?color=7389D8&label&logo=discord&logoColor=ffffff" />
|
|
19
19
|
</a>
|
|
20
|
+
<a href="https://deepwiki.com/unnoq/orpc">
|
|
21
|
+
<img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki">
|
|
22
|
+
</a>
|
|
20
23
|
</div>
|
|
21
24
|
|
|
22
25
|
<h3 align="center">Typesafe APIs Made Simple 🪄</h3>
|
|
23
26
|
|
|
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
|
|
27
|
+
**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
28
|
|
|
26
29
|
---
|
|
27
30
|
|
|
28
31
|
## Highlights
|
|
29
32
|
|
|
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.
|
|
33
|
+
- **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
|
|
34
|
+
- **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
|
|
35
|
+
- **📝 Contract-First Development**: Optionally define your API contract before implementation.
|
|
36
|
+
- **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
|
|
37
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), SWR, Pinia Colada, and more.
|
|
38
|
+
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
|
39
|
+
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
|
40
|
+
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
|
41
|
+
- **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
|
|
42
|
+
- **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
|
|
43
|
+
- **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
|
|
44
|
+
- **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
|
|
46
45
|
|
|
47
46
|
## Documentation
|
|
48
47
|
|
|
49
|
-
You can find the full documentation [here](https://orpc.
|
|
48
|
+
You can find the full documentation [here](https://orpc.dev).
|
|
50
49
|
|
|
51
50
|
## Packages
|
|
52
51
|
|
|
53
52
|
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
54
53
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
55
54
|
- [@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
55
|
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
56
|
+
- [@orpc/otel](https://www.npmjs.com/package/@orpc/otel): [OpenTelemetry](https://opentelemetry.io/) integration for observability.
|
|
57
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
|
58
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
59
|
+
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
|
60
|
+
- [@orpc/experimental-react-swr](https://www.npmjs.com/package/@orpc/experimental-react-swr): [SWR](https://swr.vercel.app/) integration.
|
|
61
|
+
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
62
|
+
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
|
|
60
63
|
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
64
|
+
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
|
65
|
+
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
|
61
66
|
|
|
62
67
|
## `@orpc/standard-server-node`
|
|
63
68
|
|
|
64
69
|
[Node.js](https://nodejs.org) server adapter for oRPC.
|
|
65
70
|
|
|
71
|
+
## Sponsors
|
|
72
|
+
|
|
73
|
+
<p align="center">
|
|
74
|
+
<a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
|
|
75
|
+
<img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
|
|
76
|
+
</a>
|
|
77
|
+
</p>
|
|
78
|
+
|
|
66
79
|
## License
|
|
67
80
|
|
|
68
81
|
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,13 @@
|
|
|
1
|
-
import { StandardBody, StandardHeaders,
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
|
|
2
|
+
import { ToEventIteratorOptions as ToEventIteratorOptions$1, ToEventStreamOptions as ToEventStreamOptions$1 } from '@orpc/standard-server-fetch';
|
|
3
|
+
import Stream, { Readable } from 'node:stream';
|
|
4
|
+
import { IncomingMessage, ServerResponse, OutgoingHttpHeaders } from 'node:http';
|
|
4
5
|
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
* @default true
|
|
12
|
-
*/
|
|
13
|
-
eventSourcePingEnabled?: boolean;
|
|
14
|
-
/**
|
|
15
|
-
* Interval (in milliseconds) between ping comments sent after the last event.
|
|
16
|
-
*
|
|
17
|
-
* @default 5000
|
|
18
|
-
*/
|
|
19
|
-
eventSourcePingInterval?: number;
|
|
20
|
-
/**
|
|
21
|
-
* The content of the ping comment. Must not include newline characters.
|
|
22
|
-
*
|
|
23
|
-
* @default ''
|
|
24
|
-
*/
|
|
25
|
-
eventSourcePingContent?: string;
|
|
7
|
+
interface ToEventIteratorOptions extends ToEventIteratorOptions$1 {
|
|
8
|
+
}
|
|
9
|
+
declare function toEventIterator(stream: Readable, options?: ToEventIteratorOptions): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
|
|
10
|
+
interface ToEventStreamOptions extends ToEventStreamOptions$1 {
|
|
26
11
|
}
|
|
27
12
|
declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
|
|
28
13
|
|
|
@@ -32,10 +17,16 @@ type NodeHttpRequest = (IncomingMessage | Http2ServerRequest) & {
|
|
|
32
17
|
* This is useful for `express.js` middleware.
|
|
33
18
|
*/
|
|
34
19
|
originalUrl?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Prefer parsed body if it is available.
|
|
22
|
+
*/
|
|
23
|
+
body?: unknown;
|
|
35
24
|
};
|
|
36
25
|
type NodeHttpResponse = ServerResponse | Http2ServerResponse;
|
|
37
26
|
|
|
38
|
-
|
|
27
|
+
interface ToStandardBodyOptions extends ToEventIteratorOptions {
|
|
28
|
+
}
|
|
29
|
+
declare function toStandardBody(req: NodeHttpRequest, options?: ToStandardBodyOptions): Promise<StandardBody>;
|
|
39
30
|
interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
|
|
40
31
|
}
|
|
41
32
|
/**
|
|
@@ -45,12 +36,19 @@ interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
|
|
|
45
36
|
*/
|
|
46
37
|
declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
|
|
47
38
|
|
|
48
|
-
declare function
|
|
39
|
+
declare function toNodeHttpHeaders(headers: StandardHeaders): OutgoingHttpHeaders;
|
|
40
|
+
|
|
41
|
+
declare function toStandardMethod(method: string | undefined): string;
|
|
42
|
+
|
|
43
|
+
declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
|
|
49
44
|
|
|
50
45
|
interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
|
|
51
46
|
}
|
|
52
47
|
declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
|
|
53
48
|
|
|
54
|
-
declare function toAbortSignal(
|
|
49
|
+
declare function toAbortSignal(stream: Stream.Writable): AbortSignal;
|
|
50
|
+
|
|
51
|
+
declare function toStandardUrl(req: NodeHttpRequest): URL;
|
|
55
52
|
|
|
56
|
-
export {
|
|
53
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toNodeHttpHeaders, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
|
|
54
|
+
export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventIteratorOptions, ToEventStreamOptions, ToNodeHttpBodyOptions, ToStandardBodyOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,28 +1,13 @@
|
|
|
1
|
-
import { StandardBody, StandardHeaders,
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
|
|
2
|
+
import { ToEventIteratorOptions as ToEventIteratorOptions$1, ToEventStreamOptions as ToEventStreamOptions$1 } from '@orpc/standard-server-fetch';
|
|
3
|
+
import Stream, { Readable } from 'node:stream';
|
|
4
|
+
import { IncomingMessage, ServerResponse, OutgoingHttpHeaders } from 'node:http';
|
|
4
5
|
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
* @default true
|
|
12
|
-
*/
|
|
13
|
-
eventSourcePingEnabled?: boolean;
|
|
14
|
-
/**
|
|
15
|
-
* Interval (in milliseconds) between ping comments sent after the last event.
|
|
16
|
-
*
|
|
17
|
-
* @default 5000
|
|
18
|
-
*/
|
|
19
|
-
eventSourcePingInterval?: number;
|
|
20
|
-
/**
|
|
21
|
-
* The content of the ping comment. Must not include newline characters.
|
|
22
|
-
*
|
|
23
|
-
* @default ''
|
|
24
|
-
*/
|
|
25
|
-
eventSourcePingContent?: string;
|
|
7
|
+
interface ToEventIteratorOptions extends ToEventIteratorOptions$1 {
|
|
8
|
+
}
|
|
9
|
+
declare function toEventIterator(stream: Readable, options?: ToEventIteratorOptions): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
|
|
10
|
+
interface ToEventStreamOptions extends ToEventStreamOptions$1 {
|
|
26
11
|
}
|
|
27
12
|
declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
|
|
28
13
|
|
|
@@ -32,10 +17,16 @@ type NodeHttpRequest = (IncomingMessage | Http2ServerRequest) & {
|
|
|
32
17
|
* This is useful for `express.js` middleware.
|
|
33
18
|
*/
|
|
34
19
|
originalUrl?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Prefer parsed body if it is available.
|
|
22
|
+
*/
|
|
23
|
+
body?: unknown;
|
|
35
24
|
};
|
|
36
25
|
type NodeHttpResponse = ServerResponse | Http2ServerResponse;
|
|
37
26
|
|
|
38
|
-
|
|
27
|
+
interface ToStandardBodyOptions extends ToEventIteratorOptions {
|
|
28
|
+
}
|
|
29
|
+
declare function toStandardBody(req: NodeHttpRequest, options?: ToStandardBodyOptions): Promise<StandardBody>;
|
|
39
30
|
interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
|
|
40
31
|
}
|
|
41
32
|
/**
|
|
@@ -45,12 +36,19 @@ interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
|
|
|
45
36
|
*/
|
|
46
37
|
declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
|
|
47
38
|
|
|
48
|
-
declare function
|
|
39
|
+
declare function toNodeHttpHeaders(headers: StandardHeaders): OutgoingHttpHeaders;
|
|
40
|
+
|
|
41
|
+
declare function toStandardMethod(method: string | undefined): string;
|
|
42
|
+
|
|
43
|
+
declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
|
|
49
44
|
|
|
50
45
|
interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
|
|
51
46
|
}
|
|
52
47
|
declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
|
|
53
48
|
|
|
54
|
-
declare function toAbortSignal(
|
|
49
|
+
declare function toAbortSignal(stream: Stream.Writable): AbortSignal;
|
|
50
|
+
|
|
51
|
+
declare function toStandardUrl(req: NodeHttpRequest): URL;
|
|
55
52
|
|
|
56
|
-
export {
|
|
53
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toNodeHttpHeaders, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
|
|
54
|
+
export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventIteratorOptions, ToEventStreamOptions, ToNodeHttpBodyOptions, ToStandardBodyOptions };
|
package/dist/index.mjs
CHANGED
|
@@ -1,127 +1,48 @@
|
|
|
1
1
|
import { Readable } from 'node:stream';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { runWithSpan, parseEmptyableJSON, isAsyncIteratorObject, stringifyJSON, AbortError, guard, 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
|
-
function toEventIterator(stream) {
|
|
6
|
-
|
|
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();
|
|
6
|
+
function toEventIterator(stream, options = {}) {
|
|
7
|
+
return toEventIterator$1(Readable.toWeb(stream), options);
|
|
45
8
|
}
|
|
46
9
|
function toEventStream(iterator, options = {}) {
|
|
47
|
-
|
|
48
|
-
const pingInterval = options.eventSourcePingInterval ?? 5e3;
|
|
49
|
-
const pingContent = options.eventSourcePingContent ?? "";
|
|
50
|
-
let timeout;
|
|
51
|
-
const stream = new ReadableStream({
|
|
52
|
-
async pull(controller) {
|
|
53
|
-
try {
|
|
54
|
-
if (pingEnabled) {
|
|
55
|
-
timeout = setInterval(() => {
|
|
56
|
-
controller.enqueue(encodeEventMessage({
|
|
57
|
-
comments: [pingContent]
|
|
58
|
-
}));
|
|
59
|
-
}, pingInterval);
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return void 0;
|
|
13
|
+
function toStandardBody(req, options = {}) {
|
|
14
|
+
if (req.body !== void 0) {
|
|
15
|
+
return Promise.resolve(req.body);
|
|
96
16
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
17
|
+
return runWithSpan({ name: "parse_standard_body", signal: options.signal }, async () => {
|
|
18
|
+
const contentDisposition = req.headers["content-disposition"];
|
|
19
|
+
const contentType = req.headers["content-type"];
|
|
20
|
+
if (typeof contentDisposition === "string") {
|
|
21
|
+
const fileName = getFilenameFromContentDisposition(contentDisposition) ?? "blob";
|
|
102
22
|
return _streamToFile(req, fileName, contentType ?? "");
|
|
103
23
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
24
|
+
if (!contentType || contentType.startsWith("application/json")) {
|
|
25
|
+
const text = await _streamToString(req);
|
|
26
|
+
return parseEmptyableJSON(text);
|
|
27
|
+
}
|
|
28
|
+
if (contentType.startsWith("multipart/form-data")) {
|
|
29
|
+
return _streamToFormData(req, contentType);
|
|
30
|
+
}
|
|
31
|
+
if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
|
32
|
+
const text = await _streamToString(req);
|
|
33
|
+
return new URLSearchParams(text);
|
|
34
|
+
}
|
|
35
|
+
if (contentType.startsWith("text/event-stream")) {
|
|
36
|
+
return toEventIterator(req, options);
|
|
37
|
+
}
|
|
38
|
+
if (contentType.startsWith("text/plain")) {
|
|
39
|
+
return _streamToString(req);
|
|
40
|
+
}
|
|
41
|
+
return _streamToFile(req, "blob", contentType);
|
|
42
|
+
});
|
|
123
43
|
}
|
|
124
44
|
function toNodeHttpBody(body, headers, options = {}) {
|
|
45
|
+
const currentContentDisposition = flattenHeader(headers["content-disposition"]);
|
|
125
46
|
delete headers["content-type"];
|
|
126
47
|
delete headers["content-disposition"];
|
|
127
48
|
if (body === void 0) {
|
|
@@ -130,10 +51,7 @@ function toNodeHttpBody(body, headers, options = {}) {
|
|
|
130
51
|
if (body instanceof Blob) {
|
|
131
52
|
headers["content-type"] = body.type;
|
|
132
53
|
headers["content-length"] = body.size.toString();
|
|
133
|
-
headers["content-disposition"] =
|
|
134
|
-
body instanceof File ? body.name : "blob",
|
|
135
|
-
{ type: "inline" }
|
|
136
|
-
);
|
|
54
|
+
headers["content-disposition"] = currentContentDisposition ?? generateContentDisposition(body instanceof File ? body.name : "blob");
|
|
137
55
|
return Readable.fromWeb(body.stream());
|
|
138
56
|
}
|
|
139
57
|
if (body instanceof FormData) {
|
|
@@ -147,8 +65,6 @@ function toNodeHttpBody(body, headers, options = {}) {
|
|
|
147
65
|
}
|
|
148
66
|
if (isAsyncIteratorObject(body)) {
|
|
149
67
|
headers["content-type"] = "text/event-stream";
|
|
150
|
-
headers["cache-control"] = "no-cache";
|
|
151
|
-
headers.connection = "keep-alive";
|
|
152
68
|
return toEventStream(body, options);
|
|
153
69
|
}
|
|
154
70
|
headers["content-type"] = "application/json";
|
|
@@ -177,55 +93,70 @@ async function _streamToFile(stream, fileName, contentType) {
|
|
|
177
93
|
return new File(chunks, fileName, { type: contentType });
|
|
178
94
|
}
|
|
179
95
|
|
|
180
|
-
function
|
|
96
|
+
function toNodeHttpHeaders(headers) {
|
|
97
|
+
const nodeHttpHeaders = {};
|
|
98
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
99
|
+
if (value !== void 0) {
|
|
100
|
+
nodeHttpHeaders[key] = value;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return nodeHttpHeaders;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function toStandardMethod(method) {
|
|
107
|
+
return method ?? "GET";
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function toAbortSignal(stream) {
|
|
181
111
|
const controller = new AbortController();
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
controller.abort("Client connection prematurely closed.");
|
|
187
|
-
} else {
|
|
188
|
-
controller.abort("Server closed the connection.");
|
|
112
|
+
stream.once("error", (error) => controller.abort(error));
|
|
113
|
+
stream.once("close", () => {
|
|
114
|
+
if (!stream.writableFinished) {
|
|
115
|
+
controller.abort(new AbortError("Writable stream closed before it finished writing"));
|
|
189
116
|
}
|
|
190
117
|
});
|
|
191
118
|
return controller.signal;
|
|
192
119
|
}
|
|
193
120
|
|
|
194
|
-
function
|
|
195
|
-
const method = req.method ?? "GET";
|
|
121
|
+
function toStandardUrl(req) {
|
|
196
122
|
const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
|
|
197
|
-
const
|
|
198
|
-
const
|
|
123
|
+
const origin = guard(() => new URL(`${protocol}//${req.headers.host ?? "localhost"}`).origin) ?? `${protocol}//localhost`;
|
|
124
|
+
const path = req.originalUrl ?? req.url ?? "/";
|
|
125
|
+
return new URL(`${origin}${path.startsWith("/") ? "" : "/"}${path}`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function toStandardLazyRequest(req, res) {
|
|
129
|
+
const signal = toAbortSignal(res);
|
|
199
130
|
return {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
url,
|
|
131
|
+
method: toStandardMethod(req.method),
|
|
132
|
+
url: toStandardUrl(req),
|
|
203
133
|
headers: req.headers,
|
|
204
|
-
body: once(() => toStandardBody(req)),
|
|
205
|
-
signal
|
|
134
|
+
body: once(() => toStandardBody(req, { signal })),
|
|
135
|
+
signal
|
|
206
136
|
};
|
|
207
137
|
}
|
|
208
138
|
|
|
209
139
|
function sendStandardResponse(res, standardResponse, options = {}) {
|
|
210
140
|
return new Promise((resolve, reject) => {
|
|
211
|
-
res.
|
|
212
|
-
res.
|
|
213
|
-
const resHeaders = standardResponse.headers;
|
|
141
|
+
res.once("error", reject);
|
|
142
|
+
res.once("close", resolve);
|
|
143
|
+
const resHeaders = { ...standardResponse.headers };
|
|
214
144
|
const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options);
|
|
215
|
-
res.writeHead(standardResponse.status, resHeaders);
|
|
145
|
+
res.writeHead(standardResponse.status, toNodeHttpHeaders(resHeaders));
|
|
216
146
|
if (resBody === void 0) {
|
|
217
|
-
res.end(
|
|
147
|
+
res.end();
|
|
218
148
|
} else if (typeof resBody === "string") {
|
|
219
149
|
res.end(resBody);
|
|
220
150
|
} else {
|
|
221
|
-
res.
|
|
151
|
+
res.once("close", () => {
|
|
222
152
|
if (!resBody.closed) {
|
|
223
153
|
resBody.destroy(res.errored ?? void 0);
|
|
224
154
|
}
|
|
225
155
|
});
|
|
156
|
+
resBody.once("error", (error) => res.destroy(error));
|
|
226
157
|
resBody.pipe(res);
|
|
227
158
|
}
|
|
228
159
|
});
|
|
229
160
|
}
|
|
230
161
|
|
|
231
|
-
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody,
|
|
162
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toNodeHttpHeaders, toStandardBody, toStandardLazyRequest, toStandardMethod, toStandardUrl };
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
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.e8416db",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"homepage": "https://orpc.
|
|
6
|
+
"homepage": "https://orpc.dev",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/unnoq/orpc.git",
|
|
@@ -23,13 +23,14 @@
|
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@orpc/shared": "0.0.0-next.
|
|
27
|
-
"@orpc/standard-server": "0.0.0-next.
|
|
26
|
+
"@orpc/shared": "0.0.0-next.e8416db",
|
|
27
|
+
"@orpc/standard-server-fetch": "0.0.0-next.e8416db",
|
|
28
|
+
"@orpc/standard-server": "0.0.0-next.e8416db"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|
|
30
|
-
"@types/node": "^22.
|
|
31
|
-
"@types/supertest": "^6.0.
|
|
32
|
-
"supertest": "^7.
|
|
31
|
+
"@types/node": "^22.15.30",
|
|
32
|
+
"@types/supertest": "^6.0.3",
|
|
33
|
+
"supertest": "^7.1.4"
|
|
33
34
|
},
|
|
34
35
|
"scripts": {
|
|
35
36
|
"build": "unbuild",
|