@hono/node-server
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -19,2 +19,5 @@ import { atob, btoa } from './base64'; | ||
WritableStream: typeof WritableStream; | ||
TransformStream: typeof TransformStream; | ||
TextDecoderStream: typeof TextDecoderStream; | ||
TextEncoderStream: typeof TextEncoderStream; | ||
crypto: Crypto; | ||
@@ -21,0 +24,0 @@ } |
@@ -26,2 +26,5 @@ "use strict"; | ||
global.WritableStream = web_stream_1.WritableStream; | ||
global.TransformStream = web_stream_1.TransformStream; | ||
global.TextDecoderStream = web_stream_1.TextDecoderStream; | ||
global.TextEncoderStream = web_stream_1.TextEncoderStream; | ||
if (typeof global.crypto === 'undefined') { | ||
@@ -28,0 +31,0 @@ // If crypto.subtle is undefined, we're in a Node.js v16 environment |
@@ -21,13 +21,7 @@ "use strict"; | ||
headers: headerRecord, | ||
// duplex: 'half', should used in nodejs 18 | ||
}; | ||
if (!(method === 'GET' || method === 'HEAD')) { | ||
// lazy-consume request body | ||
init.body = new ReadableStream({ | ||
start: async (controller) => { | ||
for await (const chunk of incoming) { | ||
controller.enqueue(chunk); | ||
} | ||
controller.close(); | ||
} | ||
}); | ||
init.body = (0, stream_1.nodeReadableToWebReadableStream)(incoming); | ||
} | ||
@@ -34,0 +28,0 @@ let res; |
/// <reference types="node" /> | ||
import type { Writable } from 'node:stream'; | ||
import { Writable, Readable } from 'node:stream'; | ||
/** pipeline will assure the backpressure and reduce huge memory usage */ | ||
export declare function writeReadableStreamToWritable(stream: ReadableStream, writable: Writable): Promise<void>; | ||
/** This implementation use nodejs Readable::fromWeb as references */ | ||
export declare function webReadableStreamToNodeReadable(stream: ReadableStream): Readable; | ||
export declare function nodeReadableToWebReadableStream(readable: Readable): ReadableStream<Uint8Array>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.writeReadableStreamToWritable = void 0; | ||
exports.nodeReadableToWebReadableStream = exports.webReadableStreamToNodeReadable = exports.writeReadableStreamToWritable = void 0; | ||
const node_stream_1 = require("node:stream"); | ||
const node_util_1 = require("node:util"); | ||
const pipelinePromise = (0, node_util_1.promisify)(node_stream_1.pipeline); | ||
/** pipeline will assure the backpressure and reduce huge memory usage */ | ||
async function writeReadableStreamToWritable(stream, writable) { | ||
const readable = webReadableStreamToNodeReadable(stream); | ||
return pipelinePromise(readable, writable); | ||
} | ||
exports.writeReadableStreamToWritable = writeReadableStreamToWritable; | ||
/** This implementation use nodejs Readable::fromWeb as references */ | ||
function webReadableStreamToNodeReadable(stream) { | ||
const reader = stream.getReader(); | ||
function onClose() { | ||
reader.cancel(new Error('Response writer closed')); | ||
} | ||
writable.once('close', onClose); | ||
try { | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
writable.end(); | ||
let closed = false; | ||
const readable = new node_stream_1.Readable({ | ||
read() { | ||
reader | ||
.read() | ||
.then(({ done, value }) => { | ||
if (done) { | ||
this.push(null); | ||
} | ||
else { | ||
this.push(value); | ||
} | ||
}) | ||
.catch(e => { | ||
readable.destroy(e); | ||
}); | ||
}, | ||
destroy(error, callback) { | ||
const done = () => { | ||
try { | ||
callback(error); | ||
} | ||
catch (err) { | ||
process.nextTick(() => { | ||
throw err; | ||
}); | ||
} | ||
}; | ||
if (!closed) { | ||
reader.cancel(error).then(done, done); | ||
return; | ||
} | ||
writable.write(value); | ||
done(); | ||
}, | ||
}); | ||
reader.closed.then(() => { | ||
closed = true; | ||
}, error => { | ||
readable.destroy(error); | ||
}); | ||
return readable; | ||
} | ||
exports.webReadableStreamToNodeReadable = webReadableStreamToNodeReadable; | ||
function nodeReadableToWebReadableStream(readable) { | ||
if (readable.destroyed) { | ||
const stream = new ReadableStream(); | ||
stream.cancel(); | ||
return stream; | ||
} | ||
const highWaterMark = readable.readableHighWaterMark; | ||
const strategy = { highWaterMark }; | ||
let controller; | ||
const onData = (chunk) => { | ||
// Copy the Buffer to detach it from the pool. | ||
if (Buffer.isBuffer(chunk)) { | ||
chunk = new Uint8Array(chunk); | ||
} | ||
} | ||
finally { | ||
writable.off('close', onClose); | ||
reader.releaseLock(); | ||
} | ||
controller.enqueue(chunk); | ||
if (controller.desiredSize !== null && controller.desiredSize <= 0) { | ||
readable.pause(); | ||
} | ||
}; | ||
readable.pause(); | ||
const cleanup = (0, node_stream_1.finished)(readable, error => { | ||
if (error?.code === 'ERR_STREAM_PREMATURE_CLOSE') { | ||
const err = new Error(undefined, { cause: error }); | ||
Object.defineProperty(err, 'name', 'AbortError'); | ||
error = err; | ||
} | ||
cleanup(); | ||
// This is a protection against non-standard, legacy streams | ||
// that happen to emit an error event again after finished is called. | ||
readable.on('error', () => { }); | ||
if (error) { | ||
return controller.error(error); | ||
} | ||
controller.close(); | ||
}); | ||
readable.on('data', onData); | ||
return new ReadableStream({ | ||
start(c) { | ||
controller = c; | ||
}, | ||
pull() { | ||
readable.resume(); | ||
}, | ||
cancel(reason) { | ||
readable.destroy(reason); | ||
}, | ||
}, strategy); | ||
} | ||
exports.writeReadableStreamToWritable = writeReadableStreamToWritable; | ||
exports.nodeReadableToWebReadableStream = nodeReadableToWebReadableStream; |
{ | ||
"name": "@hono/node-server", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"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
22844
477