@hono/node-server
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -37,7 +37,17 @@ "use strict"; | ||
} | ||
catch { | ||
catch (e) { | ||
res = new fetch_1.Response(null, { status: 500 }); | ||
if (e instanceof Error) { | ||
// timeout error emits 504 timeout | ||
if (e.name === 'TimeoutError' || e.constructor.name === 'TimeoutError') { | ||
res = new fetch_1.Response(null, { status: 504 }); | ||
} | ||
} | ||
} | ||
const contentType = res.headers.get('content-type') || ''; | ||
// nginx buffering variant | ||
const buffering = res.headers.get('x-accel-buffering') || ''; | ||
const contentEncoding = res.headers.get('content-encoding'); | ||
const contentLength = res.headers.get('content-length'); | ||
const transferEncoding = res.headers.get('transfer-encoding'); | ||
for (const [k, v] of res.headers) { | ||
@@ -53,11 +63,31 @@ if (k === 'set-cookie') { | ||
if (res.body) { | ||
if (!contentEncoding && contentType.startsWith('text')) { | ||
outgoing.end(await res.text()); | ||
try { | ||
/** | ||
* If content-encoding is set, we assume that the response should be not decoded. | ||
* Else if transfer-encoding is set, we assume that the response should be streamed. | ||
* Else if content-length is set, we assume that the response content has been taken care of. | ||
* Else if x-accel-buffering is set to no, we assume that the response should be streamed. | ||
* Else if content-type is not application/json nor text/* but can be text/event-stream, | ||
* we assume that the response should be streamed. | ||
*/ | ||
if (contentEncoding || | ||
transferEncoding || | ||
contentLength || | ||
/^no$/i.test(buffering) || | ||
!/^(application\/json\b|text\/(?!event-stream\b))/i.test(contentType)) { | ||
await (0, stream_1.writeReadableStreamToWritable)(res.body, outgoing); | ||
} | ||
else { | ||
const text = await res.text(); | ||
outgoing.setHeader('Content-Length', Buffer.byteLength(text)); | ||
outgoing.end(text); | ||
} | ||
} | ||
else if (!contentEncoding && contentType.startsWith('application/json')) { | ||
outgoing.end(await res.text()); | ||
catch (e) { | ||
// try to catch any error, to avoid crash | ||
console.error(e); | ||
const err = e instanceof Error ? e : new Error('unknown error', { cause: e }); | ||
// destroy error must accept an instance of Error | ||
outgoing.destroy(err); | ||
} | ||
else { | ||
await (0, stream_1.writeReadableStreamToWritable)(res.body, outgoing); | ||
} | ||
} | ||
@@ -64,0 +94,0 @@ else { |
/// <reference types="node" /> | ||
import type { Writable } from 'node:stream'; | ||
export declare function writeReadableStreamToWritable(stream: ReadableStream, writable: Writable): Promise<void>; | ||
/** | ||
* Credits: | ||
* - https://github.com/remix-run/remix/blob/e77e2eb/packages/remix-node/stream.ts | ||
*/ |
@@ -5,24 +5,22 @@ "use strict"; | ||
async function writeReadableStreamToWritable(stream, writable) { | ||
let reader = stream.getReader(); | ||
async function read() { | ||
let { done, value } = await reader.read(); | ||
if (done) { | ||
writable.end(); | ||
return; | ||
} | ||
writable.write(value); | ||
await read(); | ||
const reader = stream.getReader(); | ||
function onClose() { | ||
reader.cancel(new Error('Response writer closed')); | ||
} | ||
writable.once('close', onClose); | ||
try { | ||
await read(); | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
writable.end(); | ||
return; | ||
} | ||
writable.write(value); | ||
} | ||
} | ||
catch (error) { | ||
writable.destroy(error); | ||
throw error; | ||
finally { | ||
writable.off('close', onClose); | ||
reader.releaseLock(); | ||
} | ||
} | ||
exports.writeReadableStreamToWritable = writeReadableStreamToWritable; | ||
/** | ||
* Credits: | ||
* - https://github.com/remix-run/remix/blob/e77e2eb/packages/remix-node/stream.ts | ||
*/ |
{ | ||
"name": "@hono/node-server", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "HTTP Server for Hono on Node.js", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18571
379