@hono/node-server 0.3.0 → 0.4.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/listener.js +38 -8
- package/dist/stream.d.ts +0 -4
- package/dist/stream.js +15 -17
- package/package.json +1 -1
package/dist/listener.js
CHANGED
|
@@ -34,11 +34,21 @@ const getRequestListener = (fetchCallback) => {
|
|
|
34
34
|
try {
|
|
35
35
|
res = (await fetchCallback(new Request(url.toString(), init)));
|
|
36
36
|
}
|
|
37
|
-
catch {
|
|
37
|
+
catch (e) {
|
|
38
38
|
res = new fetch_1.Response(null, { status: 500 });
|
|
39
|
+
if (e instanceof Error) {
|
|
40
|
+
// timeout error emits 504 timeout
|
|
41
|
+
if (e.name === 'TimeoutError' || e.constructor.name === 'TimeoutError') {
|
|
42
|
+
res = new fetch_1.Response(null, { status: 504 });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
39
45
|
}
|
|
40
46
|
const contentType = res.headers.get('content-type') || '';
|
|
47
|
+
// nginx buffering variant
|
|
48
|
+
const buffering = res.headers.get('x-accel-buffering') || '';
|
|
41
49
|
const contentEncoding = res.headers.get('content-encoding');
|
|
50
|
+
const contentLength = res.headers.get('content-length');
|
|
51
|
+
const transferEncoding = res.headers.get('transfer-encoding');
|
|
42
52
|
for (const [k, v] of res.headers) {
|
|
43
53
|
if (k === 'set-cookie') {
|
|
44
54
|
outgoing.setHeader(k, res.headers.getAll(k));
|
|
@@ -49,14 +59,34 @@ const getRequestListener = (fetchCallback) => {
|
|
|
49
59
|
}
|
|
50
60
|
outgoing.statusCode = res.status;
|
|
51
61
|
if (res.body) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
62
|
+
try {
|
|
63
|
+
/**
|
|
64
|
+
* If content-encoding is set, we assume that the response should be not decoded.
|
|
65
|
+
* Else if transfer-encoding is set, we assume that the response should be streamed.
|
|
66
|
+
* Else if content-length is set, we assume that the response content has been taken care of.
|
|
67
|
+
* Else if x-accel-buffering is set to no, we assume that the response should be streamed.
|
|
68
|
+
* Else if content-type is not application/json nor text/* but can be text/event-stream,
|
|
69
|
+
* we assume that the response should be streamed.
|
|
70
|
+
*/
|
|
71
|
+
if (contentEncoding ||
|
|
72
|
+
transferEncoding ||
|
|
73
|
+
contentLength ||
|
|
74
|
+
/^no$/i.test(buffering) ||
|
|
75
|
+
!/^(application\/json\b|text\/(?!event-stream\b))/i.test(contentType)) {
|
|
76
|
+
await (0, stream_1.writeReadableStreamToWritable)(res.body, outgoing);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
const text = await res.text();
|
|
80
|
+
outgoing.setHeader('Content-Length', Buffer.byteLength(text));
|
|
81
|
+
outgoing.end(text);
|
|
82
|
+
}
|
|
57
83
|
}
|
|
58
|
-
|
|
59
|
-
|
|
84
|
+
catch (e) {
|
|
85
|
+
// try to catch any error, to avoid crash
|
|
86
|
+
console.error(e);
|
|
87
|
+
const err = e instanceof Error ? e : new Error('unknown error', { cause: e });
|
|
88
|
+
// destroy error must accept an instance of Error
|
|
89
|
+
outgoing.destroy(err);
|
|
60
90
|
}
|
|
61
91
|
}
|
|
62
92
|
else {
|
package/dist/stream.d.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type { Writable } from 'node:stream';
|
|
3
3
|
export declare function writeReadableStreamToWritable(stream: ReadableStream, writable: Writable): Promise<void>;
|
|
4
|
-
/**
|
|
5
|
-
* Credits:
|
|
6
|
-
* - https://github.com/remix-run/remix/blob/e77e2eb/packages/remix-node/stream.ts
|
|
7
|
-
*/
|
package/dist/stream.js
CHANGED
|
@@ -2,26 +2,24 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.writeReadableStreamToWritable = void 0;
|
|
4
4
|
async function writeReadableStreamToWritable(stream, writable) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
if (done) {
|
|
9
|
-
writable.end();
|
|
10
|
-
return;
|
|
11
|
-
}
|
|
12
|
-
writable.write(value);
|
|
13
|
-
await read();
|
|
5
|
+
const reader = stream.getReader();
|
|
6
|
+
function onClose() {
|
|
7
|
+
reader.cancel(new Error('Response writer closed'));
|
|
14
8
|
}
|
|
9
|
+
writable.once('close', onClose);
|
|
15
10
|
try {
|
|
16
|
-
|
|
11
|
+
while (true) {
|
|
12
|
+
const { done, value } = await reader.read();
|
|
13
|
+
if (done) {
|
|
14
|
+
writable.end();
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
writable.write(value);
|
|
18
|
+
}
|
|
17
19
|
}
|
|
18
|
-
|
|
19
|
-
writable.
|
|
20
|
-
|
|
20
|
+
finally {
|
|
21
|
+
writable.off('close', onClose);
|
|
22
|
+
reader.releaseLock();
|
|
21
23
|
}
|
|
22
24
|
}
|
|
23
25
|
exports.writeReadableStreamToWritable = writeReadableStreamToWritable;
|
|
24
|
-
/**
|
|
25
|
-
* Credits:
|
|
26
|
-
* - https://github.com/remix-run/remix/blob/e77e2eb/packages/remix-node/stream.ts
|
|
27
|
-
*/
|