@nestjs/core
Version:
The core of NestJS, a TypeScript framework for Node.js backends: modules, dependency injection and the request pipeline, on Express, Fastify or node:http.
180 lines (179 loc) • 6.79 kB
JavaScript
import { isFunction, isNil, isObject, isString } from '@nestjs/common/internal';
import { AsyncResource } from 'async_hooks';
import { Readable } from 'stream';
import { types } from 'util';
/**
* What `fetch()` hands back still describes the connection it was received on.
*/
const HOP_BY_HOP_HEADERS = new Set([
'connection',
'keep-alive',
'proxy-connection',
'te',
'trailer',
'transfer-encoding',
'upgrade',
]);
export function isWebResponse(value) {
return Object.prototype.toString.call(value) === '[object Response]';
}
/**
* An async iterable that is also a thenable, like a Mongoose query, stands for
* what it resolves to. Arrays and other sync iterables are not streams either.
*/
export function isStreamingBody(value) {
return (isObject(value) &&
((isFunction(value[Symbol.asyncIterator]) && !isFunction(value['then'])) ||
isWebResponse(value)));
}
function getSource(body) {
return isWebResponse(body) ? body.body : body;
}
/**
* Whether something has read from the body already, as far as it can be told:
* only an async generator itself knows that. A stream that failed instead is
* left to report its own error when it is read.
*/
export function isStreamingBodyConsumed(body) {
const source = getSource(body);
return (!isNil(source) &&
(source.locked === true ||
(Readable.isDisturbed(source) &&
!source.errored)));
}
const ignoreError = () => undefined;
/**
* Listens for the 'error' of a Node.js stream that the router holds before it
* reads it, as a `StreamableFile` does for its own. A stream that fails in the
* meantime, or once it is stopped unread (e.g. `fs.createReadStream()` of a
* file that does not exist), would otherwise emit an 'error' that nothing
* listens for, which ends the process. Whatever reads it still gets the error.
*/
export function listenForStreamError(value) {
const emitter = value;
if (isStreamingBody(value) &&
isFunction(emitter.on) &&
isFunction(emitter.listeners) &&
!emitter.listeners('error').includes(ignoreError)) {
emitter.on('error', ignoreError);
}
}
/**
* Opens the body for reading. Neither the async iterator of a `ReadableStream`
* nor that of a `Readable` stops the stream until the read in progress settles,
* which an upstream that went quiet may never do, so these are stopped directly.
*/
export function openStreamingBody(source) {
if (isFunction(source.getReader)) {
const reader = source.getReader();
return { next: () => reader.read(), return: () => reader.cancel() };
}
const iterator = source[Symbol.asyncIterator]();
return {
next: async () => iterator.next(),
return: async () => {
// Its iterator only listens once it is read, and a stream that is still
// opening what it reads from reports a failure after it is destroyed.
listenForStreamError(source);
source.destroy?.();
return iterator.return?.();
},
};
}
/**
* Stops a body that will not be sent, so that what produces it (e.g. a paid
* LLM call) stops as well.
*/
export async function cancelStreamingBody(body) {
const source = getSource(body);
if (isNil(source) || source.locked) {
return;
}
try {
await openStreamingBody(source).return();
}
catch {
// Nothing to do about it
}
}
/**
* Turns what a body yields into what a response can write: strings and binary
* data as they are, and anything else as a line of JSON (NDJSON).
*/
export function toResponseChunk(value) {
if (isString(value)) {
return value;
}
if (ArrayBuffer.isView(value)) {
return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
}
if (types.isAnyArrayBuffer(value)) {
return Buffer.from(value);
}
const json = JSON.stringify(value);
return json === undefined ? '' : `${json}\n`;
}
export function inferContentType(value) {
if (isString(value)) {
return 'text/plain; charset=utf-8';
}
return ArrayBuffer.isView(value) || types.isAnyArrayBuffer(value)
? 'application/octet-stream'
: 'application/x-ndjson';
}
/**
* The headers of a `Response` to send, as they are unless it came from
* `fetch()`: that decodes the body, but leaves `Content-Encoding` and the
* `Content-Length` of the encoded body in place. `Set-Cookie` is left out, as
* only `getSetCookie()` tells its values apart in every implementation.
*/
export function getWebResponseHeaders(response) {
const isFetched = response.type !== 'default';
const isDecoded = isFetched && response.headers.has('content-encoding');
const headers = [];
response.headers.forEach((value, name) => {
const isDropped = name === 'set-cookie' ||
(isFetched && HOP_BY_HOP_HEADERS.has(name)) ||
(isDecoded && (name === 'content-encoding' || name === 'content-length'));
if (!isDropped) {
headers.push([name, value]);
}
});
return headers;
}
/**
* A stream of what is left of the body, with the chunk that was already read
* first. It reads a chunk only when the response can take one, and destroying
* it (the adapters do when the client goes away) stops the body.
*/
export function toReadable(iterator, firstChunk) {
// Destroyed when the client goes away, which on Node.js before 22.16 happens
// outside of the request's async context. `finally` blocks run in the context
// of whoever stops a generator, and may need AsyncLocalStorage stores.
// The rest of the body likewise runs in the context of whatever asks for the
// next chunk: once the response has made the stream wait, that is what
// drained it, which for a pipelined request is the end of the response before
// it on the same connection, i.e. another request.
const asyncScope = new AsyncResource('StreamingBody');
let pendingChunk = firstChunk;
return Readable.from({
[Symbol.asyncIterator]() {
return this;
},
next: () => asyncScope.runInAsyncScope(async () => {
if (pendingChunk !== undefined) {
const value = pendingChunk;
pendingChunk = undefined;
return { done: false, value };
}
const result = await iterator.next();
return result.done
? { done: true, value: undefined }
: { done: false, value: toResponseChunk(result.value) };
}),
return: async () => {
await asyncScope.runInAsyncScope(() => iterator.return());
return { done: true, value: undefined };
},
});
}