@orpc/standard-server-node 1.1.1 → 1.2.0
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/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +43 -42
- package/package.json +3 -3
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.
|
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.
|
package/dist/index.mjs
CHANGED
|
@@ -1,52 +1,49 @@
|
|
|
1
1
|
import { Readable } from 'node:stream';
|
|
2
|
-
import {
|
|
3
|
-
import { EventDecoderStream,
|
|
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,6 +57,9 @@ function toEventStream(iterator, options = {}) {
|
|
|
60
57
|
}
|
|
61
58
|
const value = await iterator.next();
|
|
62
59
|
clearInterval(timeout);
|
|
60
|
+
if (cancelled) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
63
|
const meta = getEventMeta(value.value);
|
|
64
64
|
if (!value.done || value.value !== void 0 || meta !== void 0) {
|
|
65
65
|
controller.enqueue(encodeEventMessage({
|
|
@@ -73,6 +73,9 @@ function toEventStream(iterator, options = {}) {
|
|
|
73
73
|
}
|
|
74
74
|
} catch (err) {
|
|
75
75
|
clearInterval(timeout);
|
|
76
|
+
if (cancelled) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
76
79
|
controller.enqueue(encodeEventMessage({
|
|
77
80
|
...getEventMeta(err),
|
|
78
81
|
event: "error",
|
|
@@ -81,12 +84,10 @@ function toEventStream(iterator, options = {}) {
|
|
|
81
84
|
controller.close();
|
|
82
85
|
}
|
|
83
86
|
},
|
|
84
|
-
async cancel(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
await iterator.return?.();
|
|
89
|
-
}
|
|
87
|
+
async cancel() {
|
|
88
|
+
cancelled = true;
|
|
89
|
+
clearInterval(timeout);
|
|
90
|
+
await iterator.return?.();
|
|
90
91
|
}
|
|
91
92
|
});
|
|
92
93
|
return Readable.fromWeb(stream);
|
|
@@ -123,7 +124,7 @@ async function toStandardBody(req) {
|
|
|
123
124
|
return _streamToFile(req, "blob", contentType);
|
|
124
125
|
}
|
|
125
126
|
function toNodeHttpBody(body, headers, options = {}) {
|
|
126
|
-
const currentContentDisposition =
|
|
127
|
+
const currentContentDisposition = flattenHeader(headers["content-disposition"]);
|
|
127
128
|
delete headers["content-type"];
|
|
128
129
|
delete headers["content-disposition"];
|
|
129
130
|
if (body === void 0) {
|
|
@@ -208,7 +209,7 @@ function sendStandardResponse(res, standardResponse, options = {}) {
|
|
|
208
209
|
return new Promise((resolve, reject) => {
|
|
209
210
|
res.on("error", reject);
|
|
210
211
|
res.on("finish", resolve);
|
|
211
|
-
const resHeaders = standardResponse.headers;
|
|
212
|
+
const resHeaders = { ...standardResponse.headers };
|
|
212
213
|
const resBody = toNodeHttpBody(standardResponse.body, resHeaders, options);
|
|
213
214
|
res.writeHead(standardResponse.status, resHeaders);
|
|
214
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": "1.
|
|
4
|
+
"version": "1.2.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@orpc/shared": "1.
|
|
27
|
-
"@orpc/standard-server": "1.
|
|
26
|
+
"@orpc/shared": "1.2.0",
|
|
27
|
+
"@orpc/standard-server": "1.2.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/node": "^22.14.1",
|