@orpc/standard-server-node 0.0.0-next.ca29a36 → 0.0.0-next.cd121e3
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 +1 -1
- package/dist/index.d.mts +56 -0
- package/dist/index.d.ts +56 -0
- package/dist/{index.js → index.mjs} +27 -49
- package/package.json +7 -12
- 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
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { StandardBody, StandardHeaders, StandardRequest, 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): 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
|
+
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;
|
|
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 toStandardRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardRequest;
|
|
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 { type NodeHttpRequest, type NodeHttpResponse, type SendStandardResponseOptions, type ToEventStreamOptions, type ToNodeHttpBodyOptions, sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardRequest };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { StandardBody, StandardHeaders, StandardRequest, 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): 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
|
+
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;
|
|
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 toStandardRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardRequest;
|
|
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 { type NodeHttpRequest, type NodeHttpResponse, type SendStandardResponseOptions, type ToEventStreamOptions, type ToNodeHttpBodyOptions, sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardRequest };
|
|
@@ -1,19 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { contentDisposition, parseContentDisposition } from "@orpc/standard-server";
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { stringifyJSON, parseEmptyableJSON, isTypescriptObject, isAsyncIteratorObject, once } from '@orpc/shared';
|
|
3
|
+
import { EventDecoderStream, encodeEventMessage, getEventMeta, ErrorEvent, withEventMeta, parseContentDisposition, contentDisposition } 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();
|
|
@@ -47,14 +35,6 @@ function toEventIterator(stream) {
|
|
|
47
35
|
}
|
|
48
36
|
return done2;
|
|
49
37
|
}
|
|
50
|
-
default: {
|
|
51
|
-
let error = new UnknownEvent({
|
|
52
|
-
message: `Unknown event: ${value.event}`,
|
|
53
|
-
data: parseEmptyableJSON(value.data)
|
|
54
|
-
});
|
|
55
|
-
error = withEventMeta(error, value);
|
|
56
|
-
throw error;
|
|
57
|
-
}
|
|
58
38
|
}
|
|
59
39
|
}
|
|
60
40
|
} finally {
|
|
@@ -63,11 +43,23 @@ function toEventIterator(stream) {
|
|
|
63
43
|
}
|
|
64
44
|
return gen();
|
|
65
45
|
}
|
|
66
|
-
function toEventStream(iterator) {
|
|
46
|
+
function toEventStream(iterator, options = {}) {
|
|
47
|
+
const pingEnabled = options.eventSourcePingEnabled ?? true;
|
|
48
|
+
const pingInterval = options.eventSourcePingInterval ?? 5e3;
|
|
49
|
+
const pingContent = options.eventSourcePingContent ?? "";
|
|
50
|
+
let timeout;
|
|
67
51
|
const stream = new ReadableStream({
|
|
68
52
|
async pull(controller) {
|
|
69
53
|
try {
|
|
54
|
+
if (pingEnabled) {
|
|
55
|
+
timeout = setInterval(() => {
|
|
56
|
+
controller.enqueue(encodeEventMessage({
|
|
57
|
+
comments: [pingContent]
|
|
58
|
+
}));
|
|
59
|
+
}, pingInterval);
|
|
60
|
+
}
|
|
70
61
|
const value = await iterator.next();
|
|
62
|
+
clearInterval(timeout);
|
|
71
63
|
controller.enqueue(encodeEventMessage({
|
|
72
64
|
...getEventMeta(value.value),
|
|
73
65
|
event: value.done ? "done" : "message",
|
|
@@ -77,6 +69,7 @@ function toEventStream(iterator) {
|
|
|
77
69
|
controller.close();
|
|
78
70
|
}
|
|
79
71
|
} catch (err) {
|
|
72
|
+
clearInterval(timeout);
|
|
80
73
|
controller.enqueue(encodeEventMessage({
|
|
81
74
|
...getEventMeta(err),
|
|
82
75
|
event: "error",
|
|
@@ -96,7 +89,6 @@ function toEventStream(iterator) {
|
|
|
96
89
|
return Readable.fromWeb(stream);
|
|
97
90
|
}
|
|
98
91
|
|
|
99
|
-
// src/body.ts
|
|
100
92
|
async function toStandardBody(req) {
|
|
101
93
|
const method = req.method ?? "GET";
|
|
102
94
|
if (method === "GET" || method === "HEAD") {
|
|
@@ -112,7 +104,7 @@ async function toStandardBody(req) {
|
|
|
112
104
|
}
|
|
113
105
|
if (!contentType || contentType.startsWith("application/json")) {
|
|
114
106
|
const text = await _streamToString(req);
|
|
115
|
-
return
|
|
107
|
+
return parseEmptyableJSON(text);
|
|
116
108
|
}
|
|
117
109
|
if (contentType.startsWith("multipart/form-data")) {
|
|
118
110
|
return _streamToFormData(req, contentType);
|
|
@@ -129,7 +121,7 @@ async function toStandardBody(req) {
|
|
|
129
121
|
}
|
|
130
122
|
return _streamToFile(req, "blob", contentType);
|
|
131
123
|
}
|
|
132
|
-
function toNodeHttpBody(body, headers) {
|
|
124
|
+
function toNodeHttpBody(body, headers, options = {}) {
|
|
133
125
|
delete headers["content-type"];
|
|
134
126
|
delete headers["content-disposition"];
|
|
135
127
|
if (body === void 0) {
|
|
@@ -142,12 +134,12 @@ function toNodeHttpBody(body, headers) {
|
|
|
142
134
|
body instanceof File ? body.name : "blob",
|
|
143
135
|
{ type: "inline" }
|
|
144
136
|
);
|
|
145
|
-
return
|
|
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,7 +191,6 @@ function toAbortSignal(res) {
|
|
|
203
191
|
return controller.signal;
|
|
204
192
|
}
|
|
205
193
|
|
|
206
|
-
// src/request.ts
|
|
207
194
|
function toStandardRequest(req, res) {
|
|
208
195
|
const method = req.method ?? "GET";
|
|
209
196
|
const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
|
|
@@ -219,13 +206,12 @@ function toStandardRequest(req, res) {
|
|
|
219
206
|
};
|
|
220
207
|
}
|
|
221
208
|
|
|
222
|
-
|
|
223
|
-
function sendStandardResponse(res, standardResponse) {
|
|
209
|
+
function sendStandardResponse(res, standardResponse, options = {}) {
|
|
224
210
|
return new Promise((resolve, reject) => {
|
|
225
211
|
res.on("error", reject);
|
|
226
212
|
res.on("finish", resolve);
|
|
227
213
|
const resHeaders = standardResponse.headers;
|
|
228
|
-
const resBody = toNodeHttpBody(standardResponse.body, resHeaders);
|
|
214
|
+
const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options);
|
|
229
215
|
res.writeHead(standardResponse.status, resHeaders);
|
|
230
216
|
if (resBody === void 0) {
|
|
231
217
|
res.end(resBody);
|
|
@@ -241,13 +227,5 @@ function sendStandardResponse(res, standardResponse) {
|
|
|
241
227
|
}
|
|
242
228
|
});
|
|
243
229
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
toAbortSignal,
|
|
247
|
-
toEventIterator,
|
|
248
|
-
toEventStream,
|
|
249
|
-
toNodeHttpBody,
|
|
250
|
-
toStandardBody,
|
|
251
|
-
toStandardRequest
|
|
252
|
-
};
|
|
253
|
-
//# sourceMappingURL=index.js.map
|
|
230
|
+
|
|
231
|
+
export { sendStandardResponse, toAbortSignal, toEventIterator, toEventStream, toNodeHttpBody, toStandardBody, toStandardRequest };
|
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.cd121e3",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -14,22 +14,17 @@
|
|
|
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.cd121e3",
|
|
27
|
+
"@orpc/standard-server": "0.0.0-next.cd121e3"
|
|
33
28
|
},
|
|
34
29
|
"devDependencies": {
|
|
35
30
|
"@types/node": "^22.13.1",
|
|
@@ -37,7 +32,7 @@
|
|
|
37
32
|
"supertest": "^7.0.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
|