webpack-dev-middleware
Version:
A development middleware for webpack
1,023 lines (949 loc) • 36 kB
JavaScript
const path = require("node:path");
const querystring = require("node:querystring");
const {
finished
} = require("node:stream");
const {
createMultipartByteranges,
createReadStreamOrReadFile,
destroyStream,
escapeHtml,
etag,
finish,
getHeadersSent,
getOutgoing,
getRequestHeader,
getRequestMethod,
getRequestURL,
getResponseHeader,
getResponseHeaders,
getStatusCode,
getValueContentRangeHeader,
initState,
memorize,
parseHttpDate,
parseTokenList,
pathMatch: hotPathMatch,
pipe,
removeResponseHeader,
send,
setResponseHeader,
setState,
setStatusCode
} = require("./utils");
/** @typedef {import("fs").ReadStream} ReadStream */
/** @typedef {import("webpack").Compiler} Compiler */
/** @typedef {import("webpack").Stats} Stats */
/** @typedef {import("webpack").MultiStats} MultiStats */
/** @typedef {import("webpack").Asset} Asset */
/** @typedef {import("./index.js").NextFunction} NextFunction */
/** @typedef {import("./index.js").IncomingMessage} IncomingMessage */
/** @typedef {import("./index.js").ServerResponse} ServerResponse */
/** @typedef {import("./index.js").NormalizedHeaders} NormalizedHeaders */
/** @typedef {import("./index.js").OutputFileSystem} OutputFileSystem */
const BYTES_RANGE_REGEXP = /^ *bytes/i;
/**
* @param {string} input input
* @returns {string} unescape input
*/
function decode(input) {
return querystring.unescape(input);
}
// Only the pathname is ever read, and a `URL` cannot be carried around by
// spreading it — its properties live on the prototype, so `{ ...url }` copies
// nothing. The decoded pathname is returned on its own instead.
const memoizedParsePathname = memorize(url =>
// `URL` does not decode the pathname the way we need it.
decode(new URL(url, "http://localhost").pathname));
const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
/** @typedef {import("fs").Stats} FSStats */
/**
* @typedef {object} Extra
* @property {FSStats} stats stats
* @property {boolean=} immutable true when immutable, otherwise false
* @property {OutputFileSystem} outputFileSystem outputFileSystem
*/
/**
* decodeURIComponent.
*
* Allows V8 to only deoptimize this fn instead of all of send().
* @param {string} input
* @returns {string}
*/
class FilenameError extends Error {
/**
* @param {string} message message
* @param {number=} code error code
*/
constructor(message, code) {
super(message);
this.name = "FilenameError";
this.statusCode = code;
}
}
/** @typedef {{ filename: string, extra: Extra }} FilenameWithExtra */
/**
* @param {unknown} error error
* @returns {boolean} true when error is like not found, otherwise false
*/
function isNotFoundError(error) {
switch (/** @type {NodeJS.ErrnoException} */error.code) {
case "ENAMETOOLONG":
case "ENOENT":
case "ENOTDIR":
return true;
default:
return false;
}
}
/**
* @template {IncomingMessage} Request
* @template {ServerResponse} Response
* @param {import("./index.js").FilledContext<Request, Response>} context context
* @param {string} url url
* @returns {Promise<FilenameWithExtra | undefined>} result of get filename from url
*/
async function getFilenameFromUrl(context, url) {
/** @type {string} */
let pathname;
try {
// The `url` property of the `request` contains only `pathname`, `search`
// and `hash`.
pathname = memoizedParsePathname(url);
} catch {
return;
}
const {
options,
stats
} = context;
/** @type {Stats[]} */
const allStats = /** @type {MultiStats} */
stats.stats || [(/** @type {Stats} */stats)];
const index = options.index === false ? (/** @type {string[]} */[]) : typeof options.index === "undefined" || options.index === true ? ["index.html"] : [options.index];
for (const {
compilation
} of allStats) {
if (compilation.options.devServer === false) {
continue;
}
/** @type {string} */
let publicPathPathname;
const publicPath = options.publicPath || compilation.options.output.publicPath || "";
try {
publicPathPathname = memoizedParsePathname(publicPath === "auto" ? "/" : compilation.getPath(publicPath));
} catch {
continue;
}
/** @type {string | undefined} */
let filename;
if (pathname && publicPathPathname && pathname.startsWith(publicPathPathname)) {
// Null byte(s)
if (pathname.includes("\0")) {
throw new FilenameError("Bad Request", 400);
}
// ".." is malicious
if (UP_PATH_REGEXP.test(path.normalize(`./${pathname}`))) {
throw new FilenameError("Forbidden", 403);
}
// send file logic
// The `output.path` is always present and always absolute
const outputPath = compilation.getPath(compilation.outputOptions.path || "");
// What is left of the request once the `publicPath` prefix is off, and
// what gets joined onto the output root.
// `/complex/foo.js` => `foo.js`
const relativePathname = pathname.slice(publicPathPathname.length);
// A `..` in that remainder walks out of the output root, and the guard
// above catches almost none of them: it tests the *normalized* whole
// pathname, where `/public/../secret` has become `secret` with no `..`
// segment left to match, and `/assets../secret` never had a `..` segment
// to begin with — it is a sibling that merely shares the prefix, which a
// `publicPath` with no trailing slash makes possible. So the remainder is
// checked on its own, before it is joined onto anything.
if (UP_PATH_REGEXP.test(path.normalize(`./${relativePathname}`))) {
throw new FilenameError("Forbidden", 403);
}
// `foo.js` => `/home/user/my-project/dist/foo.js`
filename = path.join(outputPath, relativePathname);
// ... and whatever that resolved to has to be inside the output root.
// Nothing above is expected to let an escape through any more, which is
// the point of keeping this: it is the one check that reasons about the
// final path rather than the shape of the request, so it still holds if
// some encoding or platform quirk gets past the others.
const relative = path.relative(outputPath, filename);
if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
throw new FilenameError("Forbidden", 403);
}
const {
assetsInfo
} = compilation;
const {
outputFileSystem
} = /** @type {Compiler & { outputFileSystem: OutputFileSystem }} */
compilation.compiler;
/**
* @param {string} filename filename
* @returns {Promise<FilenameWithExtra | undefined>} filename when found, otherwise undefined
*/
const resolveIndex = async filename => {
if (index.length === 0) {
return;
}
filename = path.join(filename, index[0]);
let stats;
try {
stats = await new Promise((resolve, reject) => {
outputFileSystem.stat(filename, (err, res) => {
if (err) {
reject(err);
return;
}
resolve(res);
});
});
} catch (err) {
if (isNotFoundError(err)) return;
throw err;
}
if (/** @type {FSStats} */stats.isDirectory()) {
return resolveIndex(filename);
}
const extra = {
immutable: assetsInfo ? assetsInfo.get(pathname.slice(publicPathPathname.length))?.immutable : false,
outputFileSystem,
stats: (/** @type {FSStats} */stats)
};
return {
filename,
extra
};
};
/**
* @param {string} filename filename
* @returns {Promise<FilenameWithExtra | undefined>} filename when found, otherwise undefined
*/
const resolveFile = async filename => {
let stats;
try {
stats = await new Promise((resolve, reject) => {
outputFileSystem.stat(filename, (err, res) => {
if (err) {
reject(err);
return;
}
resolve(res);
});
});
} catch (err) {
if (isNotFoundError(err)) return;
throw err;
}
if (/** @type {FSStats} */stats.isDirectory()) {
// Different between `send` and our logic is here, `send` makes a redirect, we just return a file.
return resolveIndex(filename);
}
if (filename.endsWith(path.sep)) {
return;
}
/** @type {Extra} */
const extra = {
immutable: assetsInfo ? assetsInfo.get(pathname.slice(publicPathPathname.length))?.immutable : false,
outputFileSystem,
stats: (/** @type {FSStats} */stats)
};
return {
filename,
extra
};
};
// send index logic
if (index.length > 0 && pathname.endsWith("/")) {
const result = await resolveIndex(filename);
if (!result) {
continue;
}
return result;
}
// send file logic
const result = await resolveFile(filename);
if (!result) {
continue;
}
return result;
}
}
}
const CACHE_CONTROL_NO_CACHE_REGEXP = /(?:^|,)\s*?no-cache\s*?(?:,|$)/;
/** @type {Record<number, string>} */
const statuses = {
400: "Bad Request",
403: "Forbidden",
404: "Not Found",
416: "Range Not Satisfiable",
500: "Internal Server Error"
};
const parseRangeHeaders = memorize(
/**
* @param {string} value value
* @returns {import("range-parser").Result | import("range-parser").Ranges} ranges
*/
value => {
const [len, rangeHeader] = value.split("|");
return require("range-parser")(Number(len), rangeHeader, {
combine: true
});
});
const parseRequestedRanges = memorize(
/**
* The ranges as they were asked for, before any are merged.
* @param {string} value value
* @returns {import("range-parser").Result | import("range-parser").Ranges} ranges
*/
value => {
const [len, rangeHeader] = value.split("|");
return require("range-parser")(Number(len), rangeHeader);
});
/**
* Merged ranges in the order they were asked for: each by the first requested
* range it holds. `range-parser` gives a merged range the place of the last
* one merged into it, so `bytes=100-200,400-500,0-300` would put `400-500`
* before the `0-300` that holds the first range asked for.
* @param {import("range-parser").Range[]} merged the merged ranges
* @param {import("range-parser").Range[]} requested the ranges as asked for
* @returns {import("range-parser").Range[]} the merged ranges, ordered
*/
function orderAsRequested(merged, requested) {
const firstRequested = new Map();
for (const range of merged) {
firstRequested.set(range, requested.findIndex(wanted => wanted.start >= range.start && wanted.end <= range.end));
}
return merged.toSorted((a, b) => firstRequested.get(a) - firstRequested.get(b));
}
const MAX_MAX_AGE = 31536000000;
// The most ranges a `multipart/byteranges` answer is built for, as Apache's
// `MaxRanges` default: each one costs a read and a part header, and a request
// for more is answered with the whole file instead.
const MAX_RANGES = 200;
/**
* @template {IncomingMessage} Request
* @template {ServerResponse} Response
* @typedef {object} SendErrorOptions send error options
* @property {Record<string, number | string | string[] | undefined>=} headers headers
* @property {import("./index").ModifyResponseData<Request, Response>=} modifyResponseData modify response data callback
*/
/**
* @template {IncomingMessage} Request
* @template {ServerResponse} Response
* @param {import("./index.js").FilledContext<Request, Response>} context context
* @param {import("./index.js").Callback} callback callback
* @param {Request=} req req
* @returns {void}
*/
function ready(context, callback, req) {
if (context.state) {
callback(context.stats);
return;
}
const name = req && req.url || callback.name;
context.logger.info(`wait until bundle finished${name ? `: ${name}` : ""}`);
context.callbacks.push(callback);
}
/**
* @template {IncomingMessage} Request
* @template {ServerResponse} Response
* @param {import("./index.js").FilledContext<Request, Response>} context context
* @returns {import("./index.js").Middleware<Request, Response>} wrapper
*/
function wrapper(context) {
return async function middleware(req, res, next) {
// Intercept the Server-Sent Events handshake when the `hot` option is
// enabled. `GET` only: a `HEAD` (or any other method) would be handed a
// body and left hanging on a stream it cannot read.
if (context.hot && getRequestMethod(req) === "GET" && hotPathMatch(getRequestURL(req), context.hot.path)) {
try {
context.hot.handle(req, res);
} catch (error) {
return next(/** @type {NodeJS.ErrnoException} */error);
}
return;
}
/**
* @param {NodeJS.ErrnoException=} err an error
* @returns {Promise<void>}
*/
async function goNext(err) {
if (!context.options.serverSideRender) {
return next(err);
}
return new Promise(resolve => {
ready(context, () => {
setState(res, "webpack", {
devMiddleware: context
});
resolve(next(err));
}, req);
});
}
const acceptedMethods = context.options.methods || ["GET", "HEAD"];
initState(res);
const method = getRequestMethod(req);
if (method && !acceptedMethods.includes(method)) {
await goNext();
return;
}
/**
* @param {string} message an error message
* @param {number} status status
* @param {Partial<SendErrorOptions<Request, Response>>=} options options
* @returns {Promise<void>}
*/
async function sendError(message, status, options) {
if (context.options.forwardError) {
if (!getHeadersSent(res)) {
const headers = getResponseHeaders(res);
for (let i = 0; i < headers.length; i++) {
removeResponseHeader(res, headers[i]);
}
}
const error = /** @type {Error & { statusCode: number }} */
new Error(message);
error.statusCode = status;
await goNext(error);
// need the return for prevent to execute the code below and override the status and body set by user in the next middleware
return;
}
// Too late for an error page: the status and part of the body are out
// already — a `multipart/byteranges` body whose later range fails to
// read, say. Cut the response off, so the client sees it incomplete
// rather than waiting on it.
if (getHeadersSent(res)) {
getOutgoing(res).destroy();
return;
}
const content = statuses[status] || String(status);
let document = Buffer.from(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>${escapeHtml(content)}</pre>
</body>
</html>`, "utf8");
// Clear existing headers
const headers = getResponseHeaders(res);
for (let i = 0; i < headers.length; i++) {
removeResponseHeader(res, headers[i]);
}
if (options && options.headers) {
const keys = Object.keys(options.headers);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = options.headers[key];
if (typeof value !== "undefined") {
setResponseHeader(res, key, value);
}
}
}
// Send basic response
setStatusCode(res, status);
setResponseHeader(res, "Content-Type", "text/html; charset=utf-8");
setResponseHeader(res, "Content-Security-Policy", "default-src 'none'");
setResponseHeader(res, "X-Content-Type-Options", "nosniff");
let byteLength = Buffer.byteLength(document);
if (options && options.modifyResponseData) {
({
data: document,
byteLength
} = /** @type {{ data: Buffer<ArrayBuffer>, byteLength: number }} */
options.modifyResponseData(req, res, document, byteLength));
}
setResponseHeader(res, "Content-Length", byteLength);
finish(res, document);
}
/**
* @param {NodeJS.ErrnoException} error error
* @param {string=} message override message
* @param {number=} code override code
* @returns {Promise<void>}
*/
async function errorHandler(error, message, code) {
switch (error.code) {
case "ENAMETOOLONG":
case "ENOENT":
case "ENOTDIR":
await sendError(error.message, 404, {
modifyResponseData: context.options.modifyResponseData
});
break;
default:
await sendError(message || error.message, code || 500, {
modifyResponseData: context.options.modifyResponseData
});
break;
}
}
/**
* @returns {string | string[] | undefined} something when conditional get exist
*/
function isConditionalGET() {
return getRequestHeader(req, "if-match") || getRequestHeader(req, "if-unmodified-since") || getRequestHeader(req, "if-none-match") || getRequestHeader(req, "if-modified-since");
}
/**
* @returns {boolean} true when precondition failure, otherwise false
*/
function isPreconditionFailure() {
// if-match
const ifMatch = /** @type {string} */getRequestHeader(req, "if-match");
// A recipient MUST ignore If-Unmodified-Since if the request contains
// an If-Match header field; the condition in If-Match is considered to
// be a more accurate replacement for the condition in
// If-Unmodified-Since, and the two are only combined for the sake of
// interoperating with older intermediaries that might not implement If-Match.
if (ifMatch) {
const etag = getResponseHeader(res, "ETag");
return !etag || ifMatch !== "*" && parseTokenList(ifMatch).every(match => match !== etag && match !== `W/${etag}` && `W/${match}` !== etag);
}
// if-unmodified-since
const ifUnmodifiedSince = /** @type {string} */
getRequestHeader(req, "if-unmodified-since");
if (ifUnmodifiedSince) {
const unmodifiedSince = parseHttpDate(ifUnmodifiedSince);
// A recipient MUST ignore the If-Unmodified-Since header field if the
// received field-value is not a valid HTTP-date.
if (!Number.isNaN(unmodifiedSince)) {
const lastModified = parseHttpDate(/** @type {string} */getResponseHeader(res, "Last-Modified"));
return Number.isNaN(lastModified) || lastModified > unmodifiedSince;
}
}
return false;
}
/**
* @returns {boolean} is cachable
*/
function isCachable() {
const statusCode = getStatusCode(res);
return statusCode >= 200 && statusCode < 300 || statusCode === 304 ||
// For Koa and Hono, because by default status code is 404, but we already found a file
statusCode === 404;
}
/**
* @param {import("http").OutgoingHttpHeaders} resHeaders res header
* @returns {boolean} true when fresh, otherwise false
*/
function isFresh(resHeaders) {
// Always return stale when Cache-Control: no-cache to support end-to-end reload requests
// https://tools.ietf.org/html/rfc2616#section-14.9.4
const cacheControl = /** @type {string} */
getRequestHeader(req, "cache-control");
if (cacheControl && CACHE_CONTROL_NO_CACHE_REGEXP.test(cacheControl)) {
return false;
}
// fields
const noneMatch = /** @type {string} */
getRequestHeader(req, "if-none-match");
const modifiedSince = /** @type {string} */
getRequestHeader(req, "if-modified-since");
// unconditional request
if (!noneMatch && !modifiedSince) {
return false;
}
// if-none-match
if (noneMatch && noneMatch !== "*") {
if (!resHeaders.etag) {
return false;
}
const matches = parseTokenList(noneMatch);
let etagStale = true;
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
if (match === resHeaders.etag || match === `W/${resHeaders.etag}` || `W/${match}` === resHeaders.etag) {
etagStale = false;
break;
}
}
if (etagStale) {
return false;
}
}
// A recipient MUST ignore If-Modified-Since if the request contains an If-None-Match header field;
// the condition in If-None-Match is considered to be a more accurate replacement for the condition in If-Modified-Since,
// and the two are only combined for the sake of interoperating with older intermediaries that might not implement If-None-Match.
if (noneMatch) {
return true;
}
// if-modified-since
if (modifiedSince) {
const lastModified = resHeaders["last-modified"];
// A recipient MUST ignore the If-Modified-Since header field if the
// received field-value is not a valid HTTP-date, or if the request
// method is neither GET nor HEAD.
const modifiedStale = !lastModified || !(parseHttpDate(lastModified) <= parseHttpDate(modifiedSince));
if (modifiedStale) {
return false;
}
}
return true;
}
/**
* @returns {boolean} true when range is fresh, otherwise false
*/
function isRangeFresh() {
const ifRange = /** @type {string | undefined} */
getRequestHeader(req, "if-range");
if (!ifRange) {
return true;
}
// if-range as etag
if (ifRange.includes('"')) {
const etag = /** @type {string | undefined} */
getResponseHeader(res, "ETag");
if (!etag) {
return true;
}
return Boolean(etag && ifRange.includes(etag));
}
// if-range as modified date
const lastModified = /** @type {string | undefined} */
getResponseHeader(res, "Last-Modified");
if (!lastModified) {
return true;
}
return parseHttpDate(lastModified) <= parseHttpDate(ifRange);
}
/**
* @returns {string | undefined} range header
*/
function getRangeHeader() {
const range = /** @type {string} */getRequestHeader(req, "range");
if (!range || !BYTES_RANGE_REGEXP.test(range)) {
return undefined;
}
// Counted as written, before any are merged: 201 copies of one range
// are still 201 asked for.
if (range.split(",").length > MAX_RANGES) {
context.logger.warn(`A 'Range' header with more than ${MAX_RANGES} ranges was provided. A regular response will be sent for this request.`);
return undefined;
}
return range;
}
/**
* @param {import("range-parser").Range} range range
* @returns {[number, number]} offset and length
*/
function getOffsetAndLenFromRange(range) {
const offset = range.start;
const len = range.end - range.start + 1;
return [offset, len];
}
/**
* @param {number} offset offset
* @param {number} len len
* @returns {[number, number]} start and end
*/
function calcStartAndEnd(offset, len) {
const start = offset;
const end = Math.max(offset, offset + len - 1);
return [start, end];
}
/**
* @returns {Promise<void>}
*/
async function processRequest() {
// Pipe and SendFile
/** @type {FilenameWithExtra | undefined} */
let resolved;
const requestUrl = /** @type {string} */getRequestURL(req);
try {
resolved = await getFilenameFromUrl(context, requestUrl);
} catch (err) {
// Fallback to 403 for unknown errors
const errorCode = typeof err === "object" && err !== null && typeof (/** @type {FilenameError} */err.statusCode) !== "undefined" ? /** @type {FilenameError} */err.statusCode : undefined;
if (errorCode === 403) {
context.logger.error(`Malicious path "${requestUrl}".`);
}
await errorHandler(/** @type {NodeJS.ErrnoException} */err, errorCode === 400 ? "Bad Request" : errorCode === 403 ? "Forbidden" : undefined, errorCode);
return;
}
if (!resolved) {
await goNext();
return;
}
if (getHeadersSent(res)) {
await goNext();
return;
}
const {
extra,
filename
} = resolved;
const {
size
} = extra.stats;
let len = size;
let offset = 0;
// Send logic
if (context.options.headers) {
let {
headers
} = context.options;
if (typeof headers === "function") {
headers = /** @type {NormalizedHeaders} */
headers(req, res, context);
}
/**
* @type {{ key: string, value: string | number }[]}
*/
const allHeaders = [];
if (typeof headers !== "undefined") {
if (!Array.isArray(headers)) {
for (const name in headers) {
allHeaders.push({
key: name,
value: headers[name]
});
}
headers = allHeaders;
}
for (const {
key,
value
} of headers) {
setResponseHeader(res, key, value);
}
}
}
if (!getResponseHeader(res, "Accept-Ranges")) {
setResponseHeader(res, "Accept-Ranges", "bytes");
}
if (!getResponseHeader(res, "Cache-Control")) {
const {
control: cacheControl,
immutable: cacheImmutable
} = context.options.cache;
let cacheControlValue;
if ((cacheImmutable === undefined || cacheImmutable) && extra.immutable) {
cacheControlValue = `public, max-age=${Math.floor(MAX_MAX_AGE / 1000)}, immutable`;
} else if (typeof cacheControl === "boolean") {
cacheControlValue = `public, max-age=${Math.floor(MAX_MAX_AGE / 1000)}`;
} else if (typeof cacheControl === "number") {
const maxAge = Math.min(Math.max(0, cacheControl), MAX_MAX_AGE);
cacheControlValue = `public, max-age=${Math.floor(maxAge / 1000)}`;
} else if (typeof cacheControl === "string") {
cacheControlValue = cacheControl;
} else if (cacheControl) {
const maxAge = cacheControl.maxAge !== undefined ? Math.min(Math.max(0, cacheControl.maxAge), MAX_MAX_AGE) : MAX_MAX_AGE;
cacheControlValue = `public, max-age=${Math.floor(maxAge / 1000)}`;
if (cacheControl.immutable) {
cacheControlValue += ", immutable";
}
}
if (cacheControlValue) {
setResponseHeader(res, "Cache-Control", cacheControlValue);
}
}
if (context.options.cache.lastModified && !getResponseHeader(res, "Last-Modified")) {
const modified = extra.stats.mtime.toUTCString();
setResponseHeader(res, "Last-Modified", modified);
}
/** @type {number} */
let start;
/** @type {number} */
let end;
/** @type {undefined | Buffer | ReadStream | import("stream").Readable} */
let bufferOrStream;
/** @type {number | undefined} */
let byteLength;
const rangeHeader = getRangeHeader();
if (context.options.cache.etag && !getResponseHeader(res, "ETag")) {
const isStrongETag = context.options.cache.etag === "strong";
// TODO cache strong etag generation?
if (isStrongETag) {
if (rangeHeader) {
const parsedRanges = /** @type {import("range-parser").Ranges | import("range-parser").Result} */
parseRangeHeaders(`${size}|${rangeHeader}`);
if (parsedRanges !== -2 && parsedRanges !== -1 && parsedRanges.length === 1) {
[offset, len] = getOffsetAndLenFromRange(parsedRanges[0]);
}
}
[start, end] = calcStartAndEnd(offset, len);
try {
const result = createReadStreamOrReadFile(filename, extra.outputFileSystem, start, end);
({
bufferOrStream,
byteLength
} = result);
} catch (error) {
await errorHandler(/** @type {NodeJS.ErrnoException} */error);
return;
}
}
const result = await etag(isStrongETag ? (/** @type {Buffer | ReadStream} */bufferOrStream) : extra.stats);
// Because we already read stream, we can cache buffer to avoid extra read from fs
if (result.buffer) {
bufferOrStream = result.buffer;
}
setResponseHeader(res, "ETag", result.hash);
}
if (!getResponseHeader(res, "Content-Type") || getStatusCode(res) === 404) {
removeResponseHeader(res, "Content-Type");
// content-type name (like application/javascript; charset=utf-8) or false
const contentType = context.mimeTypes.contentType(path.extname(filename));
// Only set content-type header if media type is known
// https://tools.ietf.org/html/rfc7231#section-3.1.1.5
if (contentType) {
setResponseHeader(res, "Content-Type", contentType);
} else if (context.options.mime.default) {
setResponseHeader(res, "Content-Type", context.options.mime.default);
}
}
// Conditional GET support
if (isConditionalGET()) {
if (isPreconditionFailure()) {
await sendError("Precondition Failed", 412, {
modifyResponseData: context.options.modifyResponseData
});
return;
}
if (isCachable() && isFresh({
etag: (/** @type {string | undefined} */
getResponseHeader(res, "ETag")),
"last-modified": (/** @type {string | undefined} */
getResponseHeader(res, "Last-Modified"))
})) {
setStatusCode(res, 304);
// Remove content header fields
removeResponseHeader(res, "Content-Encoding");
removeResponseHeader(res, "Content-Language");
removeResponseHeader(res, "Content-Length");
removeResponseHeader(res, "Content-Range");
removeResponseHeader(res, "Content-Type");
finish(res);
return;
}
}
let isPartialContent = false;
if (rangeHeader) {
let parsedRanges = /** @type {import("range-parser").Ranges | import("range-parser").Result | []} */
parseRangeHeaders(`${size}|${rangeHeader}`);
// If-Range support
if (!isRangeFresh()) {
parsedRanges = [];
}
if (parsedRanges === -1) {
context.logger.error("Unsatisfiable range for 'Range' header.");
setResponseHeader(res, "Content-Range", getValueContentRangeHeader("bytes", size));
await sendError("Range Not Satisfiable", 416, {
headers: {
"Content-Range": getResponseHeader(res, "Content-Range")
},
modifyResponseData: context.options.modifyResponseData
});
return;
} else if (parsedRanges === -2) {
context.logger.error("A malformed 'Range' header was provided. A regular response will be sent for this request.");
} else if (parsedRanges.length > 1) {
// Still more than one once overlapping and adjacent ranges are
// merged: the parts of a `multipart/byteranges` body.
setStatusCode(res, 206);
removeResponseHeader(res, "Content-Range");
// Whole when a strong `ETag` read it already (it buffers what it
// reads), so each part can be cut from it rather than read again.
const whole = Buffer.isBuffer(bufferOrStream) ? bufferOrStream : undefined;
let boundary;
try {
({
bufferOrStream,
byteLength,
boundary
} = createMultipartByteranges(filename, extra.outputFileSystem, orderAsRequested(/** @type {import("range-parser").Ranges} */parsedRanges, /** @type {import("range-parser").Ranges} */
parseRequestedRanges(`${size}|${rangeHeader}`)), size, /** @type {string | undefined} */
getResponseHeader(res, "Content-Type"), whole));
} catch (error) {
await errorHandler(/** @type {NodeJS.ErrnoException} */error);
return;
}
// Removed first: on some frameworks setting a header adds a value.
removeResponseHeader(res, "Content-Type");
setResponseHeader(res, "Content-Type", `multipart/byteranges; boundary=${boundary}`);
isPartialContent = true;
}
if (parsedRanges !== -2 && parsedRanges.length === 1) {
// Content-Range
setStatusCode(res, 206);
setResponseHeader(res, "Content-Range", getValueContentRangeHeader("bytes", size, /** @type {import("range-parser").Ranges} */parsedRanges[0]));
isPartialContent = true;
[offset, len] = getOffsetAndLenFromRange(parsedRanges[0]);
}
}
// When strong Etag generation is enabled we already read file, so we can skip extra fs call
if (!bufferOrStream) {
[start, end] = calcStartAndEnd(offset, len);
try {
({
bufferOrStream,
byteLength
} = createReadStreamOrReadFile(filename, extra.outputFileSystem, start, end));
} catch (error) {
await errorHandler(/** @type {NodeJS.ErrnoException} */error);
return;
}
}
if (context.options.modifyResponseData) {
({
data: bufferOrStream,
byteLength
} = context.options.modifyResponseData(req, res, /** @type {Buffer | import("stream").Readable} */bufferOrStream, /** @type {number} */
byteLength));
}
setResponseHeader(res, "Content-Length", /** @type {number} */
byteLength);
if (method === "HEAD") {
if (!isPartialContent) {
setStatusCode(res, 200);
}
finish(res);
return;
}
if (!isPartialContent) {
setStatusCode(res, 200);
}
const isPipeSupports = typeof (/** @type {import("fs").ReadStream} */bufferOrStream.pipe) === "function";
if (!isPipeSupports) {
send(res, /** @type {Buffer} */bufferOrStream);
return;
}
// Error handling
/** @type {import("fs").ReadStream} */
bufferOrStream.on("error", error => {
context.logger.error("Stream error:", error);
// clean up stream early
destroyStream(/** @type {import("fs").ReadStream} */bufferOrStream, true);
// A stream can fail part way — a `multipart/byteranges` body whose
// later range cannot be read — when there is no error page left to
// send. `sendError` cuts such a response off; where a framework does
// not report the headers as sent, sending the page throws, and the
// response is cut off here instead of the error going unhandled.
errorHandler(error).catch(() => {
getOutgoing(res).destroy();
});
});
pipe(res, /** @type {ReadStream} */bufferOrStream);
const outgoing = getOutgoing(res);
if (outgoing) {
// Response finished, cleanup
finished(outgoing, err => {
if (err) {
context.logger.error("Stream error:", err);
}
destroyStream(/** @type {import("fs").ReadStream} */bufferOrStream, true);
});
}
}
ready(context, processRequest, req);
};
}
module.exports = wrapper;
module.exports.getFilenameFromUrl = getFilenameFromUrl;
module.exports.ready = ready;