@whatwg-node/server
Advanced tools
Comparing version 0.9.48 to 0.9.49-rc-20240821212455-6b203235243a2f901d5cf87105a3e2551a4326dd
@@ -50,2 +50,3 @@ "use strict"; | ||
onResponse({ request, response, setResponse, fetchAPI, serverContext }) { | ||
const waitUntil = serverContext.waitUntil?.bind(serverContext) || (() => { }); | ||
// Hack for avoiding to create whatwg-node to create a readable stream until it's needed | ||
@@ -64,17 +65,11 @@ if (response['bodyInit'] || response.body) { | ||
const writer = compressionStream.writable.getWriter(); | ||
serverContext.waitUntil(writer.write(bufOfRes)); | ||
serverContext.waitUntil(writer.close()); | ||
const reader = compressionStream.readable.getReader(); | ||
return Promise.resolve().then(async () => { | ||
const chunks = []; | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
reader.releaseLock(); | ||
break; | ||
} | ||
else if (value) { | ||
chunks.push(...value); | ||
} | ||
} | ||
waitUntil(writer.write(bufOfRes)); | ||
waitUntil(writer.close()); | ||
const uint8Arrays$ = (0, utils_js_1.isReadable)(compressionStream.readable['readable']) | ||
? collectReadableValues(compressionStream.readable['readable']) | ||
: (0, utils_js_1.isAsyncIterable)(compressionStream.readable) | ||
? collectAsyncIterableValues(compressionStream.readable) | ||
: collectReadableStreamValues(compressionStream.readable); | ||
return uint8Arrays$.then(uint8Arrays => { | ||
const chunks = uint8Arrays.flatMap(uint8Array => [...uint8Array]); | ||
const uint8Array = new Uint8Array(chunks); | ||
@@ -90,3 +85,3 @@ const newHeaders = new fetchAPI.Headers(response.headers); | ||
setResponse(compressedResponse); | ||
serverContext.waitUntil(compressionStream.writable.close()); | ||
waitUntil(compressionStream.writable.close()); | ||
}); | ||
@@ -112,1 +107,31 @@ } | ||
} | ||
function collectReadableValues(readable) { | ||
const values = []; | ||
readable.on('data', value => values.push(value)); | ||
return new Promise((resolve, reject) => { | ||
readable.once('end', () => resolve(values)); | ||
readable.once('error', reject); | ||
}); | ||
} | ||
async function collectAsyncIterableValues(asyncIterable) { | ||
const values = []; | ||
for await (const value of asyncIterable) { | ||
values.push(value); | ||
} | ||
return values; | ||
} | ||
async function collectReadableStreamValues(readableStream) { | ||
const reader = readableStream.getReader(); | ||
const values = []; | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
reader.releaseLock(); | ||
break; | ||
} | ||
else if (value) { | ||
values.push(value); | ||
} | ||
} | ||
return values; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { decompressedResponseMap, getSupportedEncodings } from '../utils.js'; | ||
import { decompressedResponseMap, getSupportedEncodings, isAsyncIterable, isReadable, } from '../utils.js'; | ||
export function useContentEncoding() { | ||
@@ -47,2 +47,3 @@ const encodingMap = new WeakMap(); | ||
onResponse({ request, response, setResponse, fetchAPI, serverContext }) { | ||
const waitUntil = serverContext.waitUntil?.bind(serverContext) || (() => { }); | ||
// Hack for avoiding to create whatwg-node to create a readable stream until it's needed | ||
@@ -61,17 +62,11 @@ if (response['bodyInit'] || response.body) { | ||
const writer = compressionStream.writable.getWriter(); | ||
serverContext.waitUntil(writer.write(bufOfRes)); | ||
serverContext.waitUntil(writer.close()); | ||
const reader = compressionStream.readable.getReader(); | ||
return Promise.resolve().then(async () => { | ||
const chunks = []; | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
reader.releaseLock(); | ||
break; | ||
} | ||
else if (value) { | ||
chunks.push(...value); | ||
} | ||
} | ||
waitUntil(writer.write(bufOfRes)); | ||
waitUntil(writer.close()); | ||
const uint8Arrays$ = isReadable(compressionStream.readable['readable']) | ||
? collectReadableValues(compressionStream.readable['readable']) | ||
: isAsyncIterable(compressionStream.readable) | ||
? collectAsyncIterableValues(compressionStream.readable) | ||
: collectReadableStreamValues(compressionStream.readable); | ||
return uint8Arrays$.then(uint8Arrays => { | ||
const chunks = uint8Arrays.flatMap(uint8Array => [...uint8Array]); | ||
const uint8Array = new Uint8Array(chunks); | ||
@@ -87,3 +82,3 @@ const newHeaders = new fetchAPI.Headers(response.headers); | ||
setResponse(compressedResponse); | ||
serverContext.waitUntil(compressionStream.writable.close()); | ||
waitUntil(compressionStream.writable.close()); | ||
}); | ||
@@ -109,1 +104,31 @@ } | ||
} | ||
function collectReadableValues(readable) { | ||
const values = []; | ||
readable.on('data', value => values.push(value)); | ||
return new Promise((resolve, reject) => { | ||
readable.once('end', () => resolve(values)); | ||
readable.once('error', reject); | ||
}); | ||
} | ||
async function collectAsyncIterableValues(asyncIterable) { | ||
const values = []; | ||
for await (const value of asyncIterable) { | ||
values.push(value); | ||
} | ||
return values; | ||
} | ||
async function collectReadableStreamValues(readableStream) { | ||
const reader = readableStream.getReader(); | ||
const values = []; | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
reader.releaseLock(); | ||
break; | ||
} | ||
else if (value) { | ||
values.push(value); | ||
} | ||
} | ||
return values; | ||
} |
{ | ||
"name": "@whatwg-node/server", | ||
"version": "0.9.48", | ||
"version": "0.9.49-rc-20240821212455-6b203235243a2f901d5cf87105a3e2551a4326dd", | ||
"description": "Fetch API compliant HTTP Server adapter", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
143076
2944