@whatwg-node/server
Advanced tools
Comparing version 0.9.53 to 0.9.54-alpha-20241111123417-4e4d48a16cce52587b1a06b11e72803047cc473d
@@ -196,3 +196,3 @@ "use strict"; | ||
if (!signal.aborted && !resEnded) { | ||
return (0, uwebsockets_js_1.sendResponseToUwsOpts)(res, response, signal); | ||
return (0, uwebsockets_js_1.sendResponseToUwsOpts)(res, response, signal, fetchAPI); | ||
} | ||
@@ -206,3 +206,3 @@ }) | ||
if (!signal.aborted && !resEnded) { | ||
return (0, uwebsockets_js_1.sendResponseToUwsOpts)(res, response$, signal); | ||
return (0, uwebsockets_js_1.sendResponseToUwsOpts)(res, response$, signal, fetchAPI); | ||
} | ||
@@ -209,0 +209,0 @@ } |
@@ -293,3 +293,3 @@ "use strict"; | ||
serverResponse.statusCode = 404; | ||
serverResponse.end(); | ||
endResponse(serverResponse); | ||
return; | ||
@@ -343,2 +343,5 @@ } | ||
} | ||
if (isReadableStream(fetchBody)) { | ||
return sendReadableStream(serverResponse, fetchBody); | ||
} | ||
if (isAsyncIterable(fetchBody)) { | ||
@@ -348,2 +351,20 @@ return sendAsyncIterable(serverResponse, fetchBody); | ||
} | ||
async function sendReadableStream(serverResponse, readableStream) { | ||
const reader = readableStream.getReader(); | ||
serverResponse.req.once('error', err => { | ||
reader.cancel(err); | ||
}); | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
if (!serverResponse | ||
// @ts-expect-error http and http2 writes are actually compatible | ||
.write(value)) { | ||
await new Promise(resolve => serverResponse.once('drain', resolve)); | ||
} | ||
} | ||
endResponse(serverResponse); | ||
} | ||
function isRequestInit(val) { | ||
@@ -350,0 +371,0 @@ return (val != null && |
@@ -5,2 +5,3 @@ "use strict"; | ||
exports.getRequestFromUWSRequest = getRequestFromUWSRequest; | ||
exports.createWritableFromUWS = createWritableFromUWS; | ||
exports.sendResponseToUwsOpts = sendResponseToUwsOpts; | ||
@@ -160,16 +161,13 @@ exports.fakePromise = fakePromise; | ||
} | ||
async function forwardResponseBodyToUWSResponse(uwsResponse, fetchResponse, signal) { | ||
for await (const chunk of fetchResponse.body) { | ||
if (signal.aborted) { | ||
return; | ||
} | ||
uwsResponse.cork(() => { | ||
function createWritableFromUWS(uwsResponse, fetchAPI) { | ||
return new fetchAPI.WritableStream({ | ||
write(chunk) { | ||
uwsResponse.write(chunk); | ||
}); | ||
} | ||
uwsResponse.cork(() => { | ||
uwsResponse.end(); | ||
}, | ||
close() { | ||
uwsResponse.end(); | ||
}, | ||
}); | ||
} | ||
function sendResponseToUwsOpts(uwsResponse, fetchResponse, signal) { | ||
function sendResponseToUwsOpts(uwsResponse, fetchResponse, signal, fetchAPI) { | ||
if (!fetchResponse) { | ||
@@ -212,3 +210,17 @@ uwsResponse.writeStatus('404 Not Found'); | ||
} | ||
return forwardResponseBodyToUWSResponse(uwsResponse, fetchResponse, signal); | ||
signal.addEventListener('abort', () => { | ||
if (!fetchResponse.body?.locked) { | ||
fetchResponse.body?.cancel(signal.reason); | ||
} | ||
}); | ||
return fetchResponse.body | ||
.pipeTo(createWritableFromUWS(uwsResponse, fetchAPI), { | ||
signal, | ||
}) | ||
.catch(err => { | ||
if (signal.aborted) { | ||
return; | ||
} | ||
throw err; | ||
}); | ||
} | ||
@@ -215,0 +227,0 @@ function fakePromise(value) { |
@@ -192,3 +192,3 @@ /* eslint-disable @typescript-eslint/ban-types */ | ||
if (!signal.aborted && !resEnded) { | ||
return sendResponseToUwsOpts(res, response, signal); | ||
return sendResponseToUwsOpts(res, response, signal, fetchAPI); | ||
} | ||
@@ -202,3 +202,3 @@ }) | ||
if (!signal.aborted && !resEnded) { | ||
return sendResponseToUwsOpts(res, response$, signal); | ||
return sendResponseToUwsOpts(res, response$, signal, fetchAPI); | ||
} | ||
@@ -205,0 +205,0 @@ } |
@@ -271,3 +271,3 @@ export function isAsyncIterable(body) { | ||
serverResponse.statusCode = 404; | ||
serverResponse.end(); | ||
endResponse(serverResponse); | ||
return; | ||
@@ -321,2 +321,5 @@ } | ||
} | ||
if (isReadableStream(fetchBody)) { | ||
return sendReadableStream(serverResponse, fetchBody); | ||
} | ||
if (isAsyncIterable(fetchBody)) { | ||
@@ -326,2 +329,20 @@ return sendAsyncIterable(serverResponse, fetchBody); | ||
} | ||
async function sendReadableStream(serverResponse, readableStream) { | ||
const reader = readableStream.getReader(); | ||
serverResponse.req.once('error', err => { | ||
reader.cancel(err); | ||
}); | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
if (!serverResponse | ||
// @ts-expect-error http and http2 writes are actually compatible | ||
.write(value)) { | ||
await new Promise(resolve => serverResponse.once('drain', resolve)); | ||
} | ||
} | ||
endResponse(serverResponse); | ||
} | ||
export function isRequestInit(val) { | ||
@@ -328,0 +349,0 @@ return (val != null && |
@@ -153,16 +153,13 @@ import { isPromise } from './utils.js'; | ||
} | ||
async function forwardResponseBodyToUWSResponse(uwsResponse, fetchResponse, signal) { | ||
for await (const chunk of fetchResponse.body) { | ||
if (signal.aborted) { | ||
return; | ||
} | ||
uwsResponse.cork(() => { | ||
export function createWritableFromUWS(uwsResponse, fetchAPI) { | ||
return new fetchAPI.WritableStream({ | ||
write(chunk) { | ||
uwsResponse.write(chunk); | ||
}); | ||
} | ||
uwsResponse.cork(() => { | ||
uwsResponse.end(); | ||
}, | ||
close() { | ||
uwsResponse.end(); | ||
}, | ||
}); | ||
} | ||
export function sendResponseToUwsOpts(uwsResponse, fetchResponse, signal) { | ||
export function sendResponseToUwsOpts(uwsResponse, fetchResponse, signal, fetchAPI) { | ||
if (!fetchResponse) { | ||
@@ -205,3 +202,17 @@ uwsResponse.writeStatus('404 Not Found'); | ||
} | ||
return forwardResponseBodyToUWSResponse(uwsResponse, fetchResponse, signal); | ||
signal.addEventListener('abort', () => { | ||
if (!fetchResponse.body?.locked) { | ||
fetchResponse.body?.cancel(signal.reason); | ||
} | ||
}); | ||
return fetchResponse.body | ||
.pipeTo(createWritableFromUWS(uwsResponse, fetchAPI), { | ||
signal, | ||
}) | ||
.catch(err => { | ||
if (signal.aborted) { | ||
return; | ||
} | ||
throw err; | ||
}); | ||
} | ||
@@ -208,0 +219,0 @@ export function fakePromise(value) { |
{ | ||
"name": "@whatwg-node/server", | ||
"version": "0.9.53", | ||
"version": "0.9.54-alpha-20241111123417-4e4d48a16cce52587b1a06b11e72803047cc473d", | ||
"description": "Fetch API compliant HTTP Server adapter", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
@@ -30,4 +30,5 @@ import type { FetchAPI } from './types.js'; | ||
export declare function getRequestFromUWSRequest({ req, res, fetchAPI, signal }: GetRequestFromUWSOpts): Request; | ||
export declare function sendResponseToUwsOpts(uwsResponse: UWSResponse, fetchResponse: Response, signal: ServerAdapterRequestAbortSignal): Promise<void> | undefined; | ||
export declare function createWritableFromUWS(uwsResponse: UWSResponse, fetchAPI: FetchAPI): WritableStream<any>; | ||
export declare function sendResponseToUwsOpts(uwsResponse: UWSResponse, fetchResponse: Response, signal: ServerAdapterRequestAbortSignal, fetchAPI: FetchAPI): Promise<void> | undefined; | ||
export declare function fakePromise<T>(value: T): Promise<T>; | ||
export {}; |
Sorry, the diff of this file is not supported yet
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
151860
3254