UNPKG

next

Version:

The React Framework

194 lines (193 loc) 7.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "default", { enumerable: true, get: function() { return RenderResult; } }); const _nodewebstreamshelper = require("./stream-utils/node-web-streams-helper"); const _pipereadable = require("./pipe-readable"); class RenderResult { /** * Creates a new RenderResult instance from a static response. * * @param value the static response value * @returns a new RenderResult instance */ static fromStatic(value) { return new RenderResult(value, { metadata: {} }); } constructor(response, { contentType, waitUntil, metadata }){ this.response = response; this.contentType = contentType; this.metadata = metadata; this.waitUntil = waitUntil; } assignMetadata(metadata) { Object.assign(this.metadata, metadata); } /** * Returns true if the response is null. It can be null if the response was * not found or was already sent. */ get isNull() { return this.response === null; } /** * Returns false if the response is a string. It can be a string if the page * was prerendered. If it's not, then it was generated dynamically. */ get isDynamic() { return typeof this.response !== 'string'; } toUnchunkedBuffer(stream = false) { if (this.response === null) { throw Object.defineProperty(new Error('Invariant: null responses cannot be unchunked'), "__NEXT_ERROR_CODE", { value: "E274", enumerable: false, configurable: true }); } if (typeof this.response !== 'string') { if (!stream) { throw Object.defineProperty(new Error('Invariant: dynamic responses cannot be unchunked. This is a bug in Next.js'), "__NEXT_ERROR_CODE", { value: "E81", enumerable: false, configurable: true }); } return (0, _nodewebstreamshelper.streamToBuffer)(this.readable); } return Buffer.from(this.response); } toUnchunkedString(stream = false) { if (this.response === null) { throw Object.defineProperty(new Error('Invariant: null responses cannot be unchunked'), "__NEXT_ERROR_CODE", { value: "E274", enumerable: false, configurable: true }); } if (typeof this.response !== 'string') { if (!stream) { throw Object.defineProperty(new Error('Invariant: dynamic responses cannot be unchunked. This is a bug in Next.js'), "__NEXT_ERROR_CODE", { value: "E81", enumerable: false, configurable: true }); } return (0, _nodewebstreamshelper.streamToString)(this.readable); } return this.response; } /** * Returns the response if it is a stream, or throws an error if it is a * string. */ get readable() { if (this.response === null) { throw Object.defineProperty(new Error('Invariant: null responses cannot be streamed'), "__NEXT_ERROR_CODE", { value: "E14", enumerable: false, configurable: true }); } if (typeof this.response === 'string') { throw Object.defineProperty(new Error('Invariant: static responses cannot be streamed'), "__NEXT_ERROR_CODE", { value: "E151", enumerable: false, configurable: true }); } if (Buffer.isBuffer(this.response)) { return (0, _nodewebstreamshelper.streamFromBuffer)(this.response); } // If the response is an array of streams, then chain them together. if (Array.isArray(this.response)) { return (0, _nodewebstreamshelper.chainStreams)(...this.response); } return this.response; } /** * Chains a new stream to the response. This will convert the response to an * array of streams if it is not already one and will add the new stream to * the end. When this response is piped, all of the streams will be piped * one after the other. * * @param readable The new stream to chain */ chain(readable) { if (this.response === null) { throw Object.defineProperty(new Error('Invariant: response is null. This is a bug in Next.js'), "__NEXT_ERROR_CODE", { value: "E258", enumerable: false, configurable: true }); } // If the response is not an array of streams already, make it one. let responses; if (typeof this.response === 'string') { responses = [ (0, _nodewebstreamshelper.streamFromString)(this.response) ]; } else if (Array.isArray(this.response)) { responses = this.response; } else if (Buffer.isBuffer(this.response)) { responses = [ (0, _nodewebstreamshelper.streamFromBuffer)(this.response) ]; } else { responses = [ this.response ]; } // Add the new stream to the array. responses.push(readable); // Update the response. this.response = responses; } /** * Pipes the response to a writable stream. This will close/cancel the * writable stream if an error is encountered. If this doesn't throw, then * the writable stream will be closed or aborted. * * @param writable Writable stream to pipe the response to */ async pipeTo(writable) { try { await this.readable.pipeTo(writable, { // We want to close the writable stream ourselves so that we can wait // for the waitUntil promise to resolve before closing it. If an error // is encountered, we'll abort the writable stream if we swallowed the // error. preventClose: true }); // If there is a waitUntil promise, wait for it to resolve before // closing the writable stream. if (this.waitUntil) await this.waitUntil; // Close the writable stream. await writable.close(); } catch (err) { // If this is an abort error, we should abort the writable stream (as we // took ownership of it when we started piping). We don't need to re-throw // because we handled the error. if ((0, _pipereadable.isAbortError)(err)) { // Abort the writable stream if an error is encountered. await writable.abort(err); return; } // We're not aborting the writer here as when this method throws it's not // clear as to how so the caller should assume it's their responsibility // to clean up the writer. throw err; } } /** * Pipes the response to a node response. This will close/cancel the node * response if an error is encountered. * * @param res */ async pipeToNodeResponse(res) { await (0, _pipereadable.pipeToNodeResponse)(this.readable, res, this.waitUntil); } } //# sourceMappingURL=render-result.js.map