@orpc/standard-server-node 0.0.0-next.f4d410a → 0.0.0-next.f4ed9ab
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 +25 -17
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.mjs +56 -57
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -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,6 +49,8 @@ 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).
|
|
58
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).
|
|
@@ -60,11 +58,21 @@ You can find the full documentation [here](https://orpc.unnoq.com).
|
|
|
60
58
|
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
61
59
|
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
62
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/).
|
|
63
63
|
|
|
64
64
|
## `@orpc/standard-server-node`
|
|
65
65
|
|
|
66
66
|
[Node.js](https://nodejs.org) server adapter for oRPC.
|
|
67
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
|
+
|
|
68
76
|
## License
|
|
69
77
|
|
|
70
78
|
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
package/dist/index.d.mts
CHANGED
|
@@ -3,7 +3,7 @@ import { Readable } from 'node:stream';
|
|
|
3
3
|
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
4
|
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
5
5
|
|
|
6
|
-
declare function toEventIterator(stream: Readable): AsyncGenerator<unknown | void, unknown | void, void>;
|
|
6
|
+
declare function toEventIterator(stream: Readable): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
|
|
7
7
|
interface ToEventStreamOptions {
|
|
8
8
|
/**
|
|
9
9
|
* If true, a ping comment is sent periodically to keep the connection alive.
|
|
@@ -53,4 +53,5 @@ declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: S
|
|
|
53
53
|
|
|
54
54
|
declare function toAbortSignal(res: NodeHttpResponse): AbortSignal;
|
|
55
55
|
|
|
56
|
-
export {
|
|
56
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
|
|
57
|
+
export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventStreamOptions, ToNodeHttpBodyOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Readable } from 'node:stream';
|
|
|
3
3
|
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
4
|
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
5
5
|
|
|
6
|
-
declare function toEventIterator(stream: Readable): AsyncGenerator<unknown | void, unknown | void, void>;
|
|
6
|
+
declare function toEventIterator(stream: Readable): AsyncIteratorObject<unknown | void, unknown | void, void> & AsyncGenerator<unknown | void, unknown | void, void>;
|
|
7
7
|
interface ToEventStreamOptions {
|
|
8
8
|
/**
|
|
9
9
|
* If true, a ping comment is sent periodically to keep the connection alive.
|
|
@@ -53,4 +53,5 @@ declare function sendStandardResponse(res: NodeHttpResponse, standardResponse: S
|
|
|
53
53
|
|
|
54
54
|
declare function toAbortSignal(res: NodeHttpResponse): AbortSignal;
|
|
55
55
|
|
|
56
|
-
export {
|
|
56
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardLazyRequest };
|
|
57
|
+
export type { NodeHttpRequest, NodeHttpResponse, SendStandardResponseOptions, ToEventStreamOptions, ToNodeHttpBodyOptions };
|
package/dist/index.mjs
CHANGED
|
@@ -1,52 +1,49 @@
|
|
|
1
1
|
import { Readable } from 'node:stream';
|
|
2
|
-
import {
|
|
3
|
-
import { EventDecoderStream, encodeEventMessage, getEventMeta,
|
|
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';
|
|
4
4
|
|
|
5
5
|
function toEventIterator(stream) {
|
|
6
6
|
const eventStream = Readable.toWeb(stream).pipeThrough(new TextDecoderStream()).pipeThrough(new EventDecoderStream());
|
|
7
7
|
const reader = eventStream.getReader();
|
|
8
|
-
async
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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;
|
|
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);
|
|
30
19
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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);
|
|
37
33
|
}
|
|
34
|
+
return { done: true, value: done2 };
|
|
38
35
|
}
|
|
39
36
|
}
|
|
40
|
-
} finally {
|
|
41
|
-
await reader.cancel();
|
|
42
37
|
}
|
|
43
|
-
}
|
|
44
|
-
|
|
38
|
+
}, async () => {
|
|
39
|
+
await reader.cancel();
|
|
40
|
+
});
|
|
45
41
|
}
|
|
46
42
|
function toEventStream(iterator, options = {}) {
|
|
47
43
|
const keepAliveEnabled = options.eventIteratorKeepAliveEnabled ?? true;
|
|
48
44
|
const keepAliveInterval = options.eventIteratorKeepAliveInterval ?? 5e3;
|
|
49
45
|
const keepAliveComment = options.eventIteratorKeepAliveComment ?? "";
|
|
46
|
+
let cancelled = false;
|
|
50
47
|
let timeout;
|
|
51
48
|
const stream = new ReadableStream({
|
|
52
49
|
async pull(controller) {
|
|
@@ -60,16 +57,25 @@ function toEventStream(iterator, options = {}) {
|
|
|
60
57
|
}
|
|
61
58
|
const value = await iterator.next();
|
|
62
59
|
clearInterval(timeout);
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
+
}
|
|
68
71
|
if (value.done) {
|
|
69
72
|
controller.close();
|
|
70
73
|
}
|
|
71
74
|
} catch (err) {
|
|
72
75
|
clearInterval(timeout);
|
|
76
|
+
if (cancelled) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
73
79
|
controller.enqueue(encodeEventMessage({
|
|
74
80
|
...getEventMeta(err),
|
|
75
81
|
event: "error",
|
|
@@ -78,12 +84,10 @@ function toEventStream(iterator, options = {}) {
|
|
|
78
84
|
controller.close();
|
|
79
85
|
}
|
|
80
86
|
},
|
|
81
|
-
async cancel(
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
await iterator.return?.();
|
|
86
|
-
}
|
|
87
|
+
async cancel() {
|
|
88
|
+
cancelled = true;
|
|
89
|
+
clearInterval(timeout);
|
|
90
|
+
await iterator.return?.();
|
|
87
91
|
}
|
|
88
92
|
});
|
|
89
93
|
return Readable.fromWeb(stream);
|
|
@@ -94,13 +98,11 @@ async function toStandardBody(req) {
|
|
|
94
98
|
if (method === "GET" || method === "HEAD") {
|
|
95
99
|
return void 0;
|
|
96
100
|
}
|
|
97
|
-
const
|
|
101
|
+
const contentDisposition = req.headers["content-disposition"];
|
|
98
102
|
const contentType = req.headers["content-type"];
|
|
99
|
-
if (
|
|
100
|
-
const fileName =
|
|
101
|
-
|
|
102
|
-
return _streamToFile(req, fileName, contentType ?? "");
|
|
103
|
-
}
|
|
103
|
+
if (typeof contentDisposition === "string") {
|
|
104
|
+
const fileName = getFilenameFromContentDisposition(contentDisposition) ?? "blob";
|
|
105
|
+
return _streamToFile(req, fileName, contentType ?? "");
|
|
104
106
|
}
|
|
105
107
|
if (!contentType || contentType.startsWith("application/json")) {
|
|
106
108
|
const text = await _streamToString(req);
|
|
@@ -122,6 +124,7 @@ async function toStandardBody(req) {
|
|
|
122
124
|
return _streamToFile(req, "blob", contentType);
|
|
123
125
|
}
|
|
124
126
|
function toNodeHttpBody(body, headers, options = {}) {
|
|
127
|
+
const currentContentDisposition = flattenHeader(headers["content-disposition"]);
|
|
125
128
|
delete headers["content-type"];
|
|
126
129
|
delete headers["content-disposition"];
|
|
127
130
|
if (body === void 0) {
|
|
@@ -130,10 +133,7 @@ function toNodeHttpBody(body, headers, options = {}) {
|
|
|
130
133
|
if (body instanceof Blob) {
|
|
131
134
|
headers["content-type"] = body.type;
|
|
132
135
|
headers["content-length"] = body.size.toString();
|
|
133
|
-
headers["content-disposition"] =
|
|
134
|
-
body instanceof File ? body.name : "blob",
|
|
135
|
-
{ type: "inline" }
|
|
136
|
-
);
|
|
136
|
+
headers["content-disposition"] = currentContentDisposition ?? generateContentDisposition(body instanceof File ? body.name : "blob");
|
|
137
137
|
return Readable.fromWeb(body.stream());
|
|
138
138
|
}
|
|
139
139
|
if (body instanceof FormData) {
|
|
@@ -197,7 +197,6 @@ function toStandardLazyRequest(req, res) {
|
|
|
197
197
|
const host = req.headers.host ?? "localhost";
|
|
198
198
|
const url = new URL(req.originalUrl ?? req.url ?? "/", `${protocol}//${host}`);
|
|
199
199
|
return {
|
|
200
|
-
raw: { request: req, response: res },
|
|
201
200
|
method,
|
|
202
201
|
url,
|
|
203
202
|
headers: req.headers,
|
|
@@ -210,7 +209,7 @@ function sendStandardResponse(res, standardResponse, options = {}) {
|
|
|
210
209
|
return new Promise((resolve, reject) => {
|
|
211
210
|
res.on("error", reject);
|
|
212
211
|
res.on("finish", resolve);
|
|
213
|
-
const resHeaders = standardResponse.headers;
|
|
212
|
+
const resHeaders = { ...standardResponse.headers };
|
|
214
213
|
const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options);
|
|
215
214
|
res.writeHead(standardResponse.status, resHeaders);
|
|
216
215
|
if (resBody === void 0) {
|
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.f4ed9ab",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -23,13 +23,13 @@
|
|
|
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.f4ed9ab",
|
|
27
|
+
"@orpc/standard-server": "0.0.0-next.f4ed9ab"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@types/node": "^22.
|
|
31
|
-
"@types/supertest": "^6.0.
|
|
32
|
-
"supertest": "^7.
|
|
30
|
+
"@types/node": "^22.15.17",
|
|
31
|
+
"@types/supertest": "^6.0.3",
|
|
32
|
+
"supertest": "^7.1.0"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
35
|
"build": "unbuild",
|