UNPKG

@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.

585 lines (584 loc) • 26.8 kB
import { HttpException, HttpStatus, IntrinsicException, Logger, RequestMethod, SSE_ABORT_CONTROLLER, StreamableFile, } from '@nestjs/common'; import { STATUS_CODES } from 'http'; import { EMPTY, lastValueFrom, Observable, isObservable, } from 'rxjs'; import { catchError, map, tap } from 'rxjs/operators'; import { Readable } from 'stream'; import { MESSAGES } from '../constants.js'; import { BaseExceptionFilter } from '../exceptions/base-exception-filter.js'; import { SseStream, } from './sse-stream.js'; import { cancelStreamingBody, getWebResponseHeaders, inferContentType, isStreamingBodyConsumed, isWebResponse, openStreamingBody, toReadable, toResponseChunk, } from './streaming-body.js'; import { isNil, isObject, isString } from '@nestjs/common/internal'; /** * Characters of events that may wait for an SSE client that is not * reading them before its connection is dropped, unless the route sets its * own `maxPendingDataLength` (see `@Sse()`). Events wait once the stream and * socket buffers are full, as a producer cannot be paused. See * `getDataLength()` for what each of them counts for. */ const DEFAULT_MAX_PENDING_DATA_LENGTH = 4 * 1024 * 1024; /** * How often, in milliseconds, an SSE connection that Node.js has stopped * reading from is written to, to find out whether its client is still there * (see `sse()`). A client that left is noticed by the second write at the * latest: the first one may still be accepted, and answered with a reset. */ const PAUSED_SOCKET_PROBE_INTERVAL = 5_000; function isCloseSource(source) { return (typeof source?.once === 'function'); } export function pickCloseSource(socket, response) { if (isCloseSource(socket)) { return socket; } return isCloseSource(response) ? response : undefined; } /** * The callbacks waiting for a connection to close, by close source. A source * gets a single listener, however many of its requests are waiting: the client * chooses how many requests it pipelines on a connection, and an * `EventEmitter` searches all the listeners of an event to remove one of them, * so a listener per request costs time quadratic in their number. */ const closeWatchers = new WeakMap(); /** * Calls `callback` once `source` closes. * @returns a function that stops waiting. Once nothing waits for `source` * anymore, no listener is left on it. */ export function onceClosed(source, callback) { let watcher = closeWatchers.get(source); if (!watcher) { const callbacks = new Set(); const listener = () => { closeWatchers.delete(source); callbacks.forEach(call => call()); }; watcher = { callbacks, listener }; closeWatchers.set(source, watcher); source.once('close', listener); } const { callbacks, listener } = watcher; callbacks.add(callback); return () => { if (callbacks.delete(callback) && callbacks.size === 0 && closeWatchers.get(source)?.callbacks === callbacks) { closeWatchers.delete(source); source.removeListener('close', listener); } }; } /** * The least an event counts for while it waits for a client that is not * reading: about what the shortest one takes once it is written * (`id: 1\ndata: \n\n`). */ const MIN_PENDING_EVENT_LENGTH = 16; function getLength(field) { return isString(field) ? field.length : 0; } /** * What a waiting event counts for. It takes up memory whatever it carries, so * an event with little or no data (e.g. `{ type: 'changed', data: '' }`) * counts as well: events that count for nothing would wait without limit. */ function getDataLength(message) { return Math.max(MIN_PENDING_EVENT_LENGTH, getLength(message.data) + getLength(message.comment) + getLength(message.type) + getLength(message.id)); } /** * Waiting events written in a row before the event loop gets a turn. A write * that does not back the stream up settles through `process.nextTick()` and a * promise, so the events that waited would otherwise all be written before * any other request is served. */ const MAX_WRITES_PER_TURN = 1024; /** * `concatMap(write)` for the events of a client, which decides how many of * them wait by not reading. `concatMap()` `shift()`s each waiting value off an * array of all the waiting ones, so writing them costs time quadratic in * their number. Here they wait in a linked list, and are not written in a * single turn of the event loop. */ function concatMapWrites(write) { return source => new Observable(subscriber => { let head; let tail; let isWriting = false; let isYielding = false; let isSourceComplete = false; let writesLeftInThisTurn = MAX_WRITES_PER_TURN; const writeNext = () => { if (subscriber.closed) { return; } if (!head) { isWriting = false; writesLeftInThisTurn = MAX_WRITES_PER_TURN; if (isSourceComplete) { subscriber.complete(); } return; } const { value } = head; head = head.next; isWriting = true; write(value).then(() => { if (!head || --writesLeftInThisTurn > 0) { return writeNext(); } writesLeftInThisTurn = MAX_WRITES_PER_TURN; isYielding = true; setImmediate(() => { isYielding = false; writeNext(); }); }, err => subscriber.error(err)); }; subscriber.add(source.subscribe({ next: value => { const waiting = { value }; tail = head ? (tail.next = waiting) : (head = waiting); if (isYielding) { // Written in the next turn on top of those that waited before, // or a producer that emits more than MAX_WRITES_PER_TURN events // per turn would get ahead of a client that is reading. writesLeftInThisTurn++; } else if (!isWriting) { writeNext(); } }, error: err => subscriber.error(err), complete: () => { isSourceComplete = true; if (!isWriting) { writeNext(); } }, })); }); } export class RouterResponseController { applicationRef; logger = new Logger(RouterResponseController.name); exceptionFilter = new BaseExceptionFilter(); constructor(applicationRef) { this.applicationRef = applicationRef; } async apply(result, response, httpStatusCode) { return this.applicationRef.reply(response, result, httpStatusCode); } /** * Sends a web `Response`, a `ReadableStream` or an async iterable as it is * produced, through the adapter's `StreamableFile` path, which gives each of * them backpressure, and stops the stream when the client goes away. * * The headers wait for the first chunk, as exception filters can no longer * answer once they are sent. A `Response` is the whole answer: its status and * headers win over those set before it (by `@HttpCode()`, `@Header()` or a * middleware), which it otherwise leaves in place. */ async stream(body, response, request, options = {}) { const webResponse = isWebResponse(body) ? body : undefined; const source = webResponse ? webResponse.body : body; if (isStreamingBodyConsumed(body)) { throw new TypeError('The route handler returned a body that has been read already, so it cannot be sent.'); } const statusCode = webResponse?.status ?? options.statusCode; const reply = (replyBody) => this.applicationRef.reply(response, replyBody, statusCode); if (isNil(source)) { this.setWebResponseHeaders(response, webResponse); return reply(); } const rawRequest = request.raw ?? request; // A HEAD response has no body, so a handler's generator is not even started. // Fastify gets an empty stream rather than an empty body, which would reset // the Content-Length of the Response to 0. if (rawRequest.method === 'HEAD') { void cancelStreamingBody(body); this.setWebResponseHeaders(response, webResponse); return reply(new StreamableFile(Readable.from([]))); } const iterator = openStreamingBody(source); const first = await this.readFirst(iterator, rawRequest, response.raw ?? response); if (!first) { void iterator.return().catch(() => undefined); return; } if (first.done) { this.setWebResponseHeaders(response, webResponse); return reply(); } let firstChunk; try { firstChunk = toResponseChunk(first.value); this.setWebResponseHeaders(response, webResponse); } catch (err) { void iterator.return().catch(() => undefined); throw err; } const stream = toReadable(iterator, firstChunk); // Logged here rather than by the file, as not every adapter reports a // failure to it, and a write to a client that left is not one to log. stream.on('error', err => { if (!(err instanceof IntrinsicException)) { this.logger.error(err); } }); const file = new StreamableFile(stream, { type: webResponse ? undefined : inferContentType(first.value), }).setErrorLogger(() => undefined); try { return await reply(file); } catch (err) { stream.destroy(); throw err; } } /** * Resolves to the first result of the body, or to nothing if the client goes * away first: that result may take a while, and may be paid for. */ readFirst(iterator, request, response) { const disconnectSource = pickCloseSource(request.socket, response); if (disconnectSource?.destroyed) { return Promise.resolve(undefined); } return new Promise((resolve, reject) => { const stopWaitingForClose = disconnectSource && onceClosed(disconnectSource, () => resolve(undefined)); iterator .next() .then(resolve, reject) .finally(() => stopWaitingForClose?.()); }); } setWebResponseHeaders(response, webResponse) { if (!webResponse) { return; } for (const [name, value] of getWebResponseHeaders(webResponse)) { this.applicationRef.setHeader(response, name, value); } for (const cookie of webResponse.headers.getSetCookie()) { this.applicationRef.appendHeader(response, 'Set-Cookie', cookie); } } async redirect(resultOrDeferred, response, redirectResponse) { const result = await this.transformToResult(resultOrDeferred); const statusCode = result && result.statusCode ? result.statusCode : redirectResponse.statusCode ? redirectResponse.statusCode : HttpStatus.FOUND; const url = result && result.url ? result.url : redirectResponse.url; return this.applicationRef.redirect(response, statusCode, url); } async render(resultOrDeferred, response, template) { const result = await this.transformToResult(resultOrDeferred); return this.applicationRef.render(response, template, result); } async transformToResult(resultOrDeferred) { if (isObservable(resultOrDeferred)) { return lastValueFrom(resultOrDeferred); } return resultOrDeferred; } getStatusByMethod(requestMethod) { switch (requestMethod) { case RequestMethod.POST: return HttpStatus.CREATED; default: return HttpStatus.OK; } } setHeaders(response, headers) { headers.forEach(({ name, value }) => this.applicationRef.setHeader(response, name, typeof value === 'function' ? value() : value)); } setStatus(response, statusCode) { this.applicationRef.status(response, statusCode); } async sse(result, response, request, options) { // It's possible that we sent headers already so don't use a stream if (response.writableEnded) { // The response is already gone: abort the request-scoped signal so // handler cleanup wired to @SseSignal() still runs, and swallow late // handler rejections that can no longer be delivered to the client. this.getOrCreateAbortController(request).abort(); Promise.resolve(result).catch((err) => this.logger.error(err)); return; } const stream = new SseStream(request); // Create a per-request AbortController and expose its signal on the request // object so async @Sse() handlers can observe client disconnects (via the // @SseSignal() parameter decorator) and stop/clean up in-flight setup work. // The controller is reused if one was already attached upstream (e.g. when the // handler is wrapped by interceptors and the signal was created earlier). const abortController = this.getOrCreateAbortController(request); return new Promise((resolve, reject) => { let settled = false; let subscription; // The response is a stream in every adapter, so it can still report the // disconnect when the socket cannot. When neither source can, the // listener is skipped rather than breaking the handler. const disconnectSource = pickCloseSource(request.socket, response); // Ends the request-scoped lifetime: stops listening for disconnects and // aborts the signal handed to the route handler. Every terminal path of // the SSE lifecycle (disconnect, completion, error) funnels through here, // so a handler that ties its resources to the signal releases them once, // regardless of how the stream ended. `abort()` is idempotent, so paths // that already aborted on disconnect are unaffected. let stopWaitingForClose; const finalize = () => { stopWaitingForClose?.(); clearInterval(pausedSocketProbe); abortController.abort(); }; // Unsubscribing runs the teardown of the producer, which is application // code, and it is also run outside the handler's promise chain (from a // socket event or a timer), where a throw would go uncaught. const unsubscribe = () => { try { subscription?.unsubscribe(); } catch (err) { this.logger.error(err); } }; // A socket that is neither read from nor written to does not learn that // the client has left, so "close" never comes. Node.js stops reading // from an HTTP/1 connection while the answers to pipelined requests pile // up behind this response, and while nobody reads the rest of the // request body. A producer that stays quiet would then be kept for good, // so a comment, which clients ignore, is written from time to time: the // write fails once the client is gone, which closes the socket. A stream // that is backed up has a write pending already, and nothing is written // before the headers are, while an exception filter can still answer. const pausedSocketProbe = setInterval(() => { if ((request.httpVersionMajor ?? 1) === 1 && request.socket?.isPaused?.() && stream.headersCommitted && !stream.writableNeedDrain) { stream.writeMessage({ comment: '' }, () => undefined); } }, PAUSED_SOCKET_PROBE_INTERVAL); pausedSocketProbe.unref?.(); const endStream = () => { if (!stream.writableEnded) { stream.end(); } }; // Hands the error to the exception filters, which can still answer as // long as no header has been sent. The stream is unpiped first, as the // pipe would otherwise end the response with it, before a filter that // awaits anything gets to write. const fail = (err) => { settled = true; finalize(); unsubscribe(); stream.unpipe(response); endStream(); reject(err); }; // Node validates the header values only once they are written, and they // are also written outside the handler's promise chain (from a timer, // or once the producer completes), where a throw would go uncaught. const commitHeaders = () => { try { stream.commitHeaders(); return true; } catch (err) { fail(err); return false; } }; const onClose = () => { if (settled) { return; } settled = true; finalize(); unsubscribe(); endStream(); response.end(); resolve(); }; stopWaitingForClose = disconnectSource && onceClosed(disconnectSource, onClose); // The client may have disconnected before the handler ran (e.g., during // a guard), in which case "close" has already been emitted. if (disconnectSource?.destroyed) { onClose(); } Promise.resolve(result) .then(observableResult => { if (settled) { // Setup may resolve after the client has disconnected. Leave its // producer unsubscribed, without keeping the router lifecycle open. return; } this.assertObservable(observableResult); stream.pipe(response, { additionalHeaders: options?.additionalHeaders, statusCode: options?.statusCode ?? (() => response.statusCode), }); let pendingDataLength = 0; // A limit that is not a number (e.g. NaN from // "Number(process.env.UNSET)") gets the default, as a missing one // does: no check below trips for NaN. const maxPendingDataLength = options?.maxPendingDataLength == null || isNaN(options.maxPendingDataLength) ? DEFAULT_MAX_PENDING_DATA_LENGTH : options.maxPendingDataLength; // Events wait in concatMapWrites() while the client is not reading. // Past a limit, the connection is dropped as if the client had // disconnected, rather than holding events for a client that may // never read them. An EventSource then reconnects, and sends the // id of the last event it received. A limit of 0, like Infinity, // keeps every event. const dropIfNotReading = () => { if (maxPendingDataLength && pendingDataLength > maxPendingDataLength && stream.writableNeedDrain) { this.logger.warn(`The client is not reading its events: ${pendingDataLength} character(s) of events are queued for it, which exceeds the maximum allowed ${maxPendingDataLength}`); response.destroy?.(); onClose(); } }; subscription = observableResult .pipe(map((message) => { const event = isObject(message) ? message : { data: message }; // The SseStream only receives string data. Serializing here // turns a JSON.stringify error (BigInts, circular references) // into an error of this pipeline, handled by catchError below, // instead of a throw that leaves the stream stuck mid-write. // Fields are copied one by one, as a spread would drop the // getters of an event implemented as a class. return isNil(event.data) || isString(event.data) ? event : { type: event.type, id: event.id, retry: event.retry, comment: event.comment, data: JSON.stringify(event.data), }; }), tap(message => { pendingDataLength += getDataLength(message); dropIfNotReading(); }), concatMapWrites(message => new Promise((resolve, reject) => { stream.writeMessage(message, err => { pendingDataLength -= getDataLength(message); return err ? reject(err) : resolve(); }); // A synchronous producer emits all of its events before // the first write, while the stream is not backed up yet, // and none after it. They are checked again once a write // backs the stream up. dropIfNotReading(); })), catchError(err => { if (!stream.headersCommitted) { throw err; } // No exception filter can answer once the headers are out, so // BaseExceptionFilter's rules apply here: the client is only // sent the message of an HTTP error, unless it must not be // exposed, and any error but an IntrinsicException is logged. // An HttpException that wraps an Error, as in // `new UnauthorizedException(err)`, has the message of that // Error, which the adapters replace by the standard message // of the status when they send the body of the exception. const data = err instanceof HttpException ? err.getResponse() instanceof Error ? (STATUS_CODES[err.getStatus()] ?? 'Error') : err.message : this.exceptionFilter.isHttpError(err) ? this.exceptionFilter.getHttpErrorMessage(err) : MESSAGES.UNKNOWN_EXCEPTION_MESSAGE; if (!(err instanceof IntrinsicException)) { this.logger.error(err); } stream.writeMessage({ type: 'error', data }, writeError => { if (writeError) { this.logger.error(writeError); } }); return EMPTY; })) .subscribe({ error: err => { if (settled) { return; } fail(err); }, complete: () => { if (settled) { return; } // An empty producer is still a successful SSE response. Commit // before ending, since the deferred header task will be skipped. if (!commitHeaders()) { return; } settled = true; finalize(); endStream(); resolve(); }, }); // Guards against a "close" event emitted synchronously during // subscribe, before the subscription is assigned for onClose to cancel. if (settled) { unsubscribe(); } // Commit SSE headers on the next macrotask. Pipe validation errors // propagate through microtasks (which complete before macrotasks), // so if the lifecycle errored, `settled` is already true and we // skip the write. Otherwise headers are sent immediately rather // than waiting for the first Observable emission. setTimeout(() => { if (settled || !commitHeaders()) { return; } // A HEAD response has no body, so it is complete once the headers // are sent. Left open, it would hold back the next request on the // same keep-alive connection until the producer completes. if (request.method === 'HEAD') { settled = true; finalize(); unsubscribe(); endStream(); resolve(); } }, 0); }) .catch(err => { if (settled) { return; } fail(err); }); }); } assertObservable(value) { if (!isObservable(value)) { throw new ReferenceError('You must return an Observable stream to use Server-Sent Events (SSE).'); } } getOrCreateAbortController(request) { const carrier = request; if (!carrier[SSE_ABORT_CONTROLLER]) { carrier[SSE_ABORT_CONTROLLER] = new AbortController(); } return carrier[SSE_ABORT_CONTROLLER]; } }