@orpc/standard-server-node 0.0.0-next.ca29a36 → 0.0.0-next.cbe59c9
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 +28 -18
- package/dist/index.d.mts +57 -0
- package/dist/index.d.ts +57 -0
- package/dist/{index.js → index.mjs} +82 -105
- package/package.json +10 -15
- package/dist/src/body.d.ts +0 -10
- package/dist/src/event-source.d.ts +0 -4
- package/dist/src/index.d.ts +0 -7
- package/dist/src/request.d.ts +0 -4
- package/dist/src/response.d.ts +0 -4
- 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), 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,30 @@ You can find the full documentation [here](https://orpc.unnoq.com).
|
|
|
53
49
|
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
54
50
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
55
51
|
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
52
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with NestJS.
|
|
53
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
56
54
|
- [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
|
|
57
55
|
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
|
|
56
|
+
- [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
|
|
57
|
+
- [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
|
|
58
58
|
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
59
59
|
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
60
60
|
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
61
|
+
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
|
62
|
+
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
|
61
63
|
|
|
62
64
|
## `@orpc/standard-server-node`
|
|
63
65
|
|
|
64
66
|
[Node.js](https://nodejs.org) server adapter for oRPC.
|
|
65
67
|
|
|
68
|
+
## Sponsors
|
|
69
|
+
|
|
70
|
+
<p align="center">
|
|
71
|
+
<a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
|
|
72
|
+
<img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
|
|
73
|
+
</a>
|
|
74
|
+
</p>
|
|
75
|
+
|
|
66
76
|
## License
|
|
67
77
|
|
|
68
78
|
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,57 @@
|
|
|
1
|
+
import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
|
|
2
|
+
import { Readable } from 'node:stream';
|
|
3
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
+
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
5
|
+
|
|
6
|
+
declare function toEventIterator(stream: Readable): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
|
|
7
|
+
interface ToEventStreamOptions {
|
|
8
|
+
/**
|
|
9
|
+
* If true, a ping comment is sent periodically to keep the connection alive.
|
|
10
|
+
*
|
|
11
|
+
* @default true
|
|
12
|
+
*/
|
|
13
|
+
eventIteratorKeepAliveEnabled?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Interval (in milliseconds) between ping comments sent after the last event.
|
|
16
|
+
*
|
|
17
|
+
* @default 5000
|
|
18
|
+
*/
|
|
19
|
+
eventIteratorKeepAliveInterval?: number;
|
|
20
|
+
/**
|
|
21
|
+
* The content of the ping comment. Must not include newline characters.
|
|
22
|
+
*
|
|
23
|
+
* @default ''
|
|
24
|
+
*/
|
|
25
|
+
eventIteratorKeepAliveComment?: string;
|
|
26
|
+
}
|
|
27
|
+
declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
|
|
28
|
+
|
|
29
|
+
type NodeHttpRequest = (IncomingMessage | Http2ServerRequest) & {
|
|
30
|
+
/**
|
|
31
|
+
* Replace `req.url` with `req.originalUrl` when `req.originalUrl` is available.
|
|
32
|
+
* This is useful for `express.js` middleware.
|
|
33
|
+
*/
|
|
34
|
+
originalUrl?: string;
|
|
35
|
+
};
|
|
36
|
+
type NodeHttpResponse = ServerResponse | Http2ServerResponse;
|
|
37
|
+
|
|
38
|
+
declare function toStandardBody(req: NodeHttpRequest): Promise<StandardBody>;
|
|
39
|
+
interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @param body
|
|
43
|
+
* @param headers - WARNING: The headers can be changed by the function and effects on the original headers.
|
|
44
|
+
* @param options
|
|
45
|
+
*/
|
|
46
|
+
declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
|
|
47
|
+
|
|
48
|
+
declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
|
|
49
|
+
|
|
50
|
+
interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
|
|
51
|
+
}
|
|
52
|
+
declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
|
|
53
|
+
|
|
54
|
+
declare function toAbortSignal(res: NodeHttpResponse): AbortSignal;
|
|
55
|
+
|
|
56
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
|
|
57
|
+
export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventStreamOptions, ToNodeHttpBodyOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { StandardBody, StandardHeaders, StandardLazyRequest, StandardResponse } from '@orpc/standard-server';
|
|
2
|
+
import { Readable } from 'node:stream';
|
|
3
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
+
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
5
|
+
|
|
6
|
+
declare function toEventIterator(stream: Readable): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
|
|
7
|
+
interface ToEventStreamOptions {
|
|
8
|
+
/**
|
|
9
|
+
* If true, a ping comment is sent periodically to keep the connection alive.
|
|
10
|
+
*
|
|
11
|
+
* @default true
|
|
12
|
+
*/
|
|
13
|
+
eventIteratorKeepAliveEnabled?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Interval (in milliseconds) between ping comments sent after the last event.
|
|
16
|
+
*
|
|
17
|
+
* @default 5000
|
|
18
|
+
*/
|
|
19
|
+
eventIteratorKeepAliveInterval?: number;
|
|
20
|
+
/**
|
|
21
|
+
* The content of the ping comment. Must not include newline characters.
|
|
22
|
+
*
|
|
23
|
+
* @default ''
|
|
24
|
+
*/
|
|
25
|
+
eventIteratorKeepAliveComment?: string;
|
|
26
|
+
}
|
|
27
|
+
declare function toEventStream(iterator: AsyncIterator<unknown | void, unknown | void, void>, options?: ToEventStreamOptions): Readable;
|
|
28
|
+
|
|
29
|
+
type NodeHttpRequest = (IncomingMessage | Http2ServerRequest) & {
|
|
30
|
+
/**
|
|
31
|
+
* Replace `req.url` with `req.originalUrl` when `req.originalUrl` is available.
|
|
32
|
+
* This is useful for `express.js` middleware.
|
|
33
|
+
*/
|
|
34
|
+
originalUrl?: string;
|
|
35
|
+
};
|
|
36
|
+
type NodeHttpResponse = ServerResponse | Http2ServerResponse;
|
|
37
|
+
|
|
38
|
+
declare function toStandardBody(req: NodeHttpRequest): Promise<StandardBody>;
|
|
39
|
+
interface ToNodeHttpBodyOptions extends ToEventStreamOptions {
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @param body
|
|
43
|
+
* @param headers - WARNING: The headers can be changed by the function and effects on the original headers.
|
|
44
|
+
* @param options
|
|
45
|
+
*/
|
|
46
|
+
declare function toNodeHttpBody(body: StandardBody, headers: StandardHeaders, options?: ToNodeHttpBodyOptions): Readable | undefined | string;
|
|
47
|
+
|
|
48
|
+
declare function toStandardLazyRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardLazyRequest;
|
|
49
|
+
|
|
50
|
+
interface SendStandardResponseOptions extends ToNodeHttpBodyOptions {
|
|
51
|
+
}
|
|
52
|
+
declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse, options?: SendStandardResponseOptions): Promise<void>;
|
|
53
|
+
|
|
54
|
+
declare function toAbortSignal(res: NodeHttpResponse): AbortSignal;
|
|
55
|
+
|
|
56
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
|
|
57
|
+
export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventStreamOptions, ToNodeHttpBodyOptions };
|
|
@@ -1,82 +1,81 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { contentDisposition, parseContentDisposition } from "@orpc/standard-server";
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { createAsyncIteratorObject, parseEmptyableJSON, isTypescriptObject, stringifyJSON, isAsyncIteratorObject, once } from '@orpc/shared';
|
|
3
|
+
import { EventDecoderStream, withEventMeta, ErrorEvent, encodeEventMessage, getEventMeta, getFilenameFromContentDisposition, flattenHeader, generateContentDisposition } from '@orpc/standard-server';
|
|
5
4
|
|
|
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
|
-
UnknownEvent,
|
|
15
|
-
withEventMeta
|
|
16
|
-
} from "@orpc/standard-server";
|
|
17
5
|
function toEventIterator(stream) {
|
|
18
6
|
const eventStream = Readable.toWeb(stream).pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
|
|
19
7
|
const reader = eventStream.getReader();
|
|
20
|
-
async
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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;
|
|
8
|
+
return createAsyncIteratorObject(async () => {
|
|
9
|
+
while (true) {
|
|
10
|
+
const { done, value } = await reader.read();
|
|
11
|
+
if (done) {
|
|
12
|
+
return { done: true, value: void 0 };
|
|
13
|
+
}
|
|
14
|
+
switch (value.event) {
|
|
15
|
+
case "message": {
|
|
16
|
+
let message = parseEmptyableJSON(value.data);
|
|
17
|
+
if (isTypescriptObject(message)) {
|
|
18
|
+
message = withEventMeta(message, value);
|
|
49
19
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
20
|
+
return { done: false, value: message };
|
|
21
|
+
}
|
|
22
|
+
case "error": {
|
|
23
|
+
let error = new ErrorEvent({
|
|
24
|
+
data: parseEmptyableJSON(value.data)
|
|
25
|
+
});
|
|
26
|
+
error = withEventMeta(error, value);
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
case "done": {
|
|
30
|
+
let done2 = parseEmptyableJSON(value.data);
|
|
31
|
+
if (isTypescriptObject(done2)) {
|
|
32
|
+
done2 = withEventMeta(done2, value);
|
|
57
33
|
}
|
|
34
|
+
return { done: true, value: done2 };
|
|
58
35
|
}
|
|
59
36
|
}
|
|
60
|
-
} finally {
|
|
61
|
-
await reader.cancel();
|
|
62
37
|
}
|
|
63
|
-
}
|
|
64
|
-
|
|
38
|
+
}, async () => {
|
|
39
|
+
await reader.cancel();
|
|
40
|
+
});
|
|
65
41
|
}
|
|
66
|
-
function toEventStream(iterator) {
|
|
42
|
+
function toEventStream(iterator, options = {}) {
|
|
43
|
+
const keepAliveEnabled = options.eventIteratorKeepAliveEnabled ?? true;
|
|
44
|
+
const keepAliveInterval = options.eventIteratorKeepAliveInterval ?? 5e3;
|
|
45
|
+
const keepAliveComment = options.eventIteratorKeepAliveComment ?? "";
|
|
46
|
+
let cancelled = false;
|
|
47
|
+
let timeout;
|
|
67
48
|
const stream = new ReadableStream({
|
|
68
49
|
async pull(controller) {
|
|
69
50
|
try {
|
|
51
|
+
if (keepAliveEnabled) {
|
|
52
|
+
timeout = setInterval(() => {
|
|
53
|
+
controller.enqueue(encodeEventMessage({
|
|
54
|
+
comments: [keepAliveComment]
|
|
55
|
+
}));
|
|
56
|
+
}, keepAliveInterval);
|
|
57
|
+
}
|
|
70
58
|
const value = await iterator.next();
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
59
|
+
clearInterval(timeout);
|
|
60
|
+
if (cancelled) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const meta = getEventMeta(value.value);
|
|
64
|
+
if (!value.done || value.value !== void 0 || meta !== void 0) {
|
|
65
|
+
controller.enqueue(encodeEventMessage({
|
|
66
|
+
...meta,
|
|
67
|
+
event: value.done ? "done" : "message",
|
|
68
|
+
data: stringifyJSON(value.value)
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
76
71
|
if (value.done) {
|
|
77
72
|
controller.close();
|
|
78
73
|
}
|
|
79
74
|
} catch (err) {
|
|
75
|
+
clearInterval(timeout);
|
|
76
|
+
if (cancelled) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
80
79
|
controller.enqueue(encodeEventMessage({
|
|
81
80
|
...getEventMeta(err),
|
|
82
81
|
event: "error",
|
|
@@ -85,34 +84,29 @@ function toEventStream(iterator) {
|
|
|
85
84
|
controller.close();
|
|
86
85
|
}
|
|
87
86
|
},
|
|
88
|
-
async cancel(
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
await iterator.return?.();
|
|
93
|
-
}
|
|
87
|
+
async cancel() {
|
|
88
|
+
cancelled = true;
|
|
89
|
+
clearInterval(timeout);
|
|
90
|
+
await iterator.return?.();
|
|
94
91
|
}
|
|
95
92
|
});
|
|
96
93
|
return Readable.fromWeb(stream);
|
|
97
94
|
}
|
|
98
95
|
|
|
99
|
-
// src/body.ts
|
|
100
96
|
async function toStandardBody(req) {
|
|
101
97
|
const method = req.method ?? "GET";
|
|
102
98
|
if (method === "GET" || method === "HEAD") {
|
|
103
99
|
return void 0;
|
|
104
100
|
}
|
|
105
|
-
const
|
|
101
|
+
const contentDisposition = req.headers["content-disposition"];
|
|
106
102
|
const contentType = req.headers["content-type"];
|
|
107
|
-
if (
|
|
108
|
-
const fileName =
|
|
109
|
-
|
|
110
|
-
return _streamToFile(req, fileName, contentType ?? "");
|
|
111
|
-
}
|
|
103
|
+
if (typeof contentDisposition === "string") {
|
|
104
|
+
const fileName = getFilenameFromContentDisposition(contentDisposition) ?? "blob";
|
|
105
|
+
return _streamToFile(req, fileName, contentType ?? "");
|
|
112
106
|
}
|
|
113
107
|
if (!contentType || contentType.startsWith("application/json")) {
|
|
114
108
|
const text = await _streamToString(req);
|
|
115
|
-
return
|
|
109
|
+
return parseEmptyableJSON(text);
|
|
116
110
|
}
|
|
117
111
|
if (contentType.startsWith("multipart/form-data")) {
|
|
118
112
|
return _streamToFormData(req, contentType);
|
|
@@ -129,7 +123,8 @@ async function toStandardBody(req) {
|
|
|
129
123
|
}
|
|
130
124
|
return _streamToFile(req, "blob", contentType);
|
|
131
125
|
}
|
|
132
|
-
function toNodeHttpBody(body, headers) {
|
|
126
|
+
function toNodeHttpBody(body, headers, options = {}) {
|
|
127
|
+
const currentContentDisposition = flattenHeader(headers["content-disposition"]);
|
|
133
128
|
delete headers["content-type"];
|
|
134
129
|
delete headers["content-disposition"];
|
|
135
130
|
if (body === void 0) {
|
|
@@ -138,16 +133,13 @@ function toNodeHttpBody(body, headers) {
|
|
|
138
133
|
if (body instanceof Blob) {
|
|
139
134
|
headers["content-type"] = body.type;
|
|
140
135
|
headers["content-length"] = body.size.toString();
|
|
141
|
-
headers["content-disposition"] =
|
|
142
|
-
|
|
143
|
-
{ type: "inline" }
|
|
144
|
-
);
|
|
145
|
-
return Readable2.fromWeb(body.stream());
|
|
136
|
+
headers["content-disposition"] = currentContentDisposition ?? generateContentDisposition(body instanceof File ? body.name : "blob");
|
|
137
|
+
return Readable.fromWeb(body.stream());
|
|
146
138
|
}
|
|
147
139
|
if (body instanceof FormData) {
|
|
148
140
|
const response = new Response(body);
|
|
149
141
|
headers["content-type"] = response.headers.get("content-type");
|
|
150
|
-
return
|
|
142
|
+
return Readable.fromWeb(response.body);
|
|
151
143
|
}
|
|
152
144
|
if (body instanceof URLSearchParams) {
|
|
153
145
|
headers["content-type"] = "application/x-www-form-urlencoded";
|
|
@@ -157,10 +149,10 @@ function toNodeHttpBody(body, headers) {
|
|
|
157
149
|
headers["content-type"] = "text/event-stream";
|
|
158
150
|
headers["cache-control"] = "no-cache";
|
|
159
151
|
headers.connection = "keep-alive";
|
|
160
|
-
return toEventStream(body);
|
|
152
|
+
return toEventStream(body, options);
|
|
161
153
|
}
|
|
162
154
|
headers["content-type"] = "application/json";
|
|
163
|
-
return
|
|
155
|
+
return stringifyJSON(body);
|
|
164
156
|
}
|
|
165
157
|
function _streamToFormData(stream, contentType) {
|
|
166
158
|
const response = new Response(stream, {
|
|
@@ -185,10 +177,6 @@ async function _streamToFile(stream, fileName, contentType) {
|
|
|
185
177
|
return new File(chunks, fileName, { type: contentType });
|
|
186
178
|
}
|
|
187
179
|
|
|
188
|
-
// src/request.ts
|
|
189
|
-
import { once } from "@orpc/shared";
|
|
190
|
-
|
|
191
|
-
// src/signal.ts
|
|
192
180
|
function toAbortSignal(res) {
|
|
193
181
|
const controller = new AbortController();
|
|
194
182
|
res.on("close", () => {
|
|
@@ -203,14 +191,12 @@ function toAbortSignal(res) {
|
|
|
203
191
|
return controller.signal;
|
|
204
192
|
}
|
|
205
193
|
|
|
206
|
-
|
|
207
|
-
function toStandardRequest(req, res) {
|
|
194
|
+
function toStandardLazyRequest(req, res) {
|
|
208
195
|
const method = req.method ?? "GET";
|
|
209
196
|
const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
|
|
210
197
|
const host = req.headers.host ?? "localhost";
|
|
211
198
|
const url = new URL(req.originalUrl ?? req.url ?? "/", `${protocol}//${host}`);
|
|
212
199
|
return {
|
|
213
|
-
raw: { request: req, response: res },
|
|
214
200
|
method,
|
|
215
201
|
url,
|
|
216
202
|
headers: req.headers,
|
|
@@ -219,13 +205,12 @@ function toStandardRequest(req, res) {
|
|
|
219
205
|
};
|
|
220
206
|
}
|
|
221
207
|
|
|
222
|
-
|
|
223
|
-
function sendStandardResponse(res, standardResponse) {
|
|
208
|
+
function sendStandardResponse(res, standardResponse, options = {}) {
|
|
224
209
|
return new Promise((resolve, reject) => {
|
|
225
210
|
res.on("error", reject);
|
|
226
211
|
res.on("finish", resolve);
|
|
227
|
-
const resHeaders = standardResponse.headers;
|
|
228
|
-
const resBody = toNodeHttpBody(standardResponse.body, resHeaders);
|
|
212
|
+
const resHeaders = { ...standardResponse.headers };
|
|
213
|
+
const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options);
|
|
229
214
|
res.writeHead(standardResponse.status, resHeaders);
|
|
230
215
|
if (resBody === void 0) {
|
|
231
216
|
res.end(resBody);
|
|
@@ -241,13 +226,5 @@ function sendStandardResponse(res, standardResponse) {
|
|
|
241
226
|
}
|
|
242
227
|
});
|
|
243
228
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
toAbortSignal,
|
|
247
|
-
toEventIterator,
|
|
248
|
-
toEventStream,
|
|
249
|
-
toNodeHttpBody,
|
|
250
|
-
toStandardBody,
|
|
251
|
-
toStandardRequest
|
|
252
|
-
};
|
|
253
|
-
//# sourceMappingURL=index.js.map
|
|
229
|
+
|
|
230
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
|
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.cbe59c9",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -14,30 +14,25 @@
|
|
|
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.cbe59c9",
|
|
27
|
+
"@orpc/standard-server": "0.0.0-next.cbe59c9"
|
|
33
28
|
},
|
|
34
29
|
"devDependencies": {
|
|
35
|
-
"@types/node": "^22.
|
|
36
|
-
"@types/supertest": "^6.0.
|
|
37
|
-
"supertest": "^7.
|
|
30
|
+
"@types/node": "^22.14.1",
|
|
31
|
+
"@types/supertest": "^6.0.3",
|
|
32
|
+
"supertest": "^7.1.0"
|
|
38
33
|
},
|
|
39
34
|
"scripts": {
|
|
40
|
-
"build": "
|
|
35
|
+
"build": "unbuild",
|
|
41
36
|
"build:watch": "pnpm run build --watch",
|
|
42
37
|
"type:check": "tsc -b"
|
|
43
38
|
}
|
package/dist/src/body.d.ts
DELETED
|
@@ -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
|
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,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
|
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
|