-
-
Notifications
You must be signed in to change notification settings - Fork 361
/
middleware.js
758 lines (623 loc) · 20.4 KB
/
middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
const path = require("path");
const mime = require("mime-types");
const onFinishedStream = require("on-finished");
const getFilenameFromUrl = require("./utils/getFilenameFromUrl");
const {
setStatusCode,
send,
pipe,
createReadStreamOrReadFileSync,
} = require("./utils/compatibleAPI");
const ready = require("./utils/ready");
const parseTokenList = require("./utils/parseTokenList");
const memorize = require("./utils/memorize");
/** @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("fs").ReadStream} ReadStream */
const BYTES_RANGE_REGEXP = /^ *bytes/i;
/**
* @param {string} type
* @param {number} size
* @param {import("range-parser").Range} [range]
* @returns {string}
*/
function getValueContentRangeHeader(type, size, range) {
return `${type} ${range ? `${range.start}-${range.end}` : "*"}/${size}`;
}
/**
* Parse an HTTP Date into a number.
*
* @param {string} date
* @returns {number}
*/
function parseHttpDate(date) {
const timestamp = date && Date.parse(date);
// istanbul ignore next: guard against date.js Date.parse patching
return typeof timestamp === "number" ? timestamp : NaN;
}
const CACHE_CONTROL_NO_CACHE_REGEXP = /(?:^|,)\s*?no-cache\s*?(?:,|$)/;
/**
* @param {import("fs").ReadStream} stream stream
* @param {boolean} suppress do need suppress?
* @returns {void}
*/
function destroyStream(stream, suppress) {
if (typeof stream.destroy === "function") {
stream.destroy();
}
if (typeof stream.close === "function") {
// Node.js core bug workaround
stream.on(
"open",
/**
* @this {import("fs").ReadStream}
*/
function onOpenClose() {
// @ts-ignore
if (typeof this.fd === "number") {
// actually close down the fd
this.close();
}
},
);
}
if (typeof stream.addListener === "function" && suppress) {
stream.removeAllListeners("error");
stream.addListener("error", () => {});
}
}
/** @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
* @returns {import("range-parser").Result | import("range-parser").Ranges}
*/
(value) => {
const [len, rangeHeader] = value.split("|");
// eslint-disable-next-line global-require
return require("range-parser")(Number(len), rangeHeader, {
combine: true,
});
},
);
/**
* @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
* @return {import("./index.js").Middleware<Request, Response>}
*/
function wrapper(context) {
return async function middleware(req, res, next) {
const acceptedMethods = context.options.methods || ["GET", "HEAD"];
// fixes #282. credit @cexoso. in certain edge situations res.locals is undefined.
// eslint-disable-next-line no-param-reassign
res.locals = res.locals || {};
async function goNext() {
if (!context.options.serverSideRender) {
return next();
}
return new Promise((resolve) => {
ready(
context,
() => {
/** @type {any} */
// eslint-disable-next-line no-param-reassign
(res.locals).webpack = { devMiddleware: context };
resolve(next());
},
req,
);
});
}
if (req.method && !acceptedMethods.includes(req.method)) {
await goNext();
return;
}
/**
* @param {number} status status
* @param {Partial<SendErrorOptions<Request, Response>>=} options options
* @returns {void}
*/
function sendError(status, options) {
// eslint-disable-next-line global-require
const escapeHtml = require("./utils/escapeHtml");
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>`,
"utf-8",
);
// Clear existing headers
const headers = res.getHeaderNames();
for (let i = 0; i < headers.length; i++) {
res.removeHeader(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") {
res.setHeader(key, value);
}
}
}
// Send basic response
setStatusCode(res, status);
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.setHeader("Content-Security-Policy", "default-src 'none'");
res.setHeader("X-Content-Type-Options", "nosniff");
let byteLength = Buffer.byteLength(document);
if (options && options.modifyResponseData) {
({ data: document, byteLength } =
/** @type {{ data: Buffer, byteLength: number }} */
(options.modifyResponseData(req, res, document, byteLength)));
}
res.setHeader("Content-Length", byteLength);
res.end(document);
}
function isConditionalGET() {
return (
req.headers["if-match"] ||
req.headers["if-unmodified-since"] ||
req.headers["if-none-match"] ||
req.headers["if-modified-since"]
);
}
function isPreconditionFailure() {
// if-match
const ifMatch = req.headers["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 = res.getHeader("ETag");
return (
!etag ||
(ifMatch !== "*" &&
parseTokenList(ifMatch).every(
(match) =>
match !== etag &&
match !== `W/${etag}` &&
`W/${match}` !== etag,
))
);
}
// if-unmodified-since
const ifUnmodifiedSince = req.headers["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 (!isNaN(unmodifiedSince)) {
const lastModified = parseHttpDate(
/** @type {string} */ (res.getHeader("Last-Modified")),
);
return isNaN(lastModified) || lastModified > unmodifiedSince;
}
}
return false;
}
/**
* @returns {boolean} is cachable
*/
function isCachable() {
return (
(res.statusCode >= 200 && res.statusCode < 300) ||
res.statusCode === 304
);
}
/**
* @param {import("http").OutgoingHttpHeaders} resHeaders
* @returns {boolean}
*/
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 = req.headers["cache-control"];
if (cacheControl && CACHE_CONTROL_NO_CACHE_REGEXP.test(cacheControl)) {
return false;
}
// fields
const noneMatch = req.headers["if-none-match"];
const modifiedSince = req.headers["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;
}
function isRangeFresh() {
const ifRange =
/** @type {string | undefined} */
(req.headers["if-range"]);
if (!ifRange) {
return true;
}
// if-range as etag
if (ifRange.indexOf('"') !== -1) {
const etag = /** @type {string | undefined} */ (res.getHeader("ETag"));
if (!etag) {
return true;
}
return Boolean(etag && ifRange.indexOf(etag) !== -1);
}
// if-range as modified date
const lastModified =
/** @type {string | undefined} */
(res.getHeader("Last-Modified"));
if (!lastModified) {
return true;
}
return parseHttpDate(lastModified) <= parseHttpDate(ifRange);
}
/**
* @returns {string | undefined}
*/
function getRangeHeader() {
const rage = req.headers.range;
if (rage && BYTES_RANGE_REGEXP.test(rage)) {
return rage;
}
// eslint-disable-next-line no-undefined
return undefined;
}
/**
* @param {import("range-parser").Range} range
* @returns {[number, number]}
*/
function getOffsetAndLenFromRange(range) {
const offset = range.start;
const len = range.end - range.start + 1;
return [offset, len];
}
/**
* @param {number} offset
* @param {number} len
* @returns {[number, number]}
*/
function calcStartAndEnd(offset, len) {
const start = offset;
const end = Math.max(offset, offset + len - 1);
return [start, end];
}
async function processRequest() {
// Pipe and SendFile
/** @type {import("./utils/getFilenameFromUrl").Extra} */
const extra = {};
const filename = getFilenameFromUrl(
context,
/** @type {string} */ (req.url),
extra,
);
if (extra.errorCode) {
if (extra.errorCode === 403) {
context.logger.error(`Malicious path "${filename}".`);
}
sendError(extra.errorCode, {
modifyResponseData: context.options.modifyResponseData,
});
return;
}
if (!filename) {
await goNext();
return;
}
const { size } = /** @type {import("fs").Stats} */ (extra.stats);
let len = size;
let offset = 0;
// Send logic
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)) {
// eslint-disable-next-line guard-for-in
for (const name in headers) {
allHeaders.push({ key: name, value: headers[name] });
}
headers = allHeaders;
}
headers.forEach((header) => {
res.setHeader(header.key, header.value);
});
}
if (!res.getHeader("Content-Type")) {
// content-type name(like application/javascript; charset=utf-8) or false
const contentType = mime.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) {
res.setHeader("Content-Type", contentType);
} else if (context.options.mimeTypeDefault) {
res.setHeader("Content-Type", context.options.mimeTypeDefault);
}
}
if (!res.getHeader("Accept-Ranges")) {
res.setHeader("Accept-Ranges", "bytes");
}
if (context.options.lastModified && !res.getHeader("Last-Modified")) {
const modified =
/** @type {import("fs").Stats} */
(extra.stats).mtime.toUTCString();
res.setHeader("Last-Modified", modified);
}
/** @type {number} */
let start;
/** @type {number} */
let end;
/** @type {undefined | Buffer | ReadStream} */
let bufferOrStream;
/** @type {number} */
let byteLength;
const rangeHeader = getRangeHeader();
if (context.options.etag && !res.getHeader("ETag")) {
/** @type {import("fs").Stats | Buffer | ReadStream | undefined} */
let value;
// TODO cache etag generation?
if (context.options.etag === "weak") {
value = /** @type {import("fs").Stats} */ (extra.stats);
} else {
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 = createReadStreamOrReadFileSync(
filename,
context.outputFileSystem,
start,
end,
);
value = result.bufferOrStream;
({ bufferOrStream, byteLength } = result);
} catch (_err) {
// Ignore here
}
}
if (value) {
// eslint-disable-next-line global-require
const result = await require("./utils/etag")(value);
// Because we already read stream, we can cache buffer to avoid extra read from fs
if (result.buffer) {
bufferOrStream = result.buffer;
}
res.setHeader("ETag", result.hash);
}
}
// Conditional GET support
if (isConditionalGET()) {
if (isPreconditionFailure()) {
sendError(412, {
modifyResponseData: context.options.modifyResponseData,
});
return;
}
// For Koa
if (res.statusCode === 404) {
setStatusCode(res, 200);
}
if (
isCachable() &&
isFresh({
etag: /** @type {string | undefined} */ (res.getHeader("ETag")),
"last-modified":
/** @type {string | undefined} */
(res.getHeader("Last-Modified")),
})
) {
setStatusCode(res, 304);
// Remove content header fields
res.removeHeader("Content-Encoding");
res.removeHeader("Content-Language");
res.removeHeader("Content-Length");
res.removeHeader("Content-Range");
res.removeHeader("Content-Type");
res.end();
return;
}
}
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.");
res.setHeader(
"Content-Range",
getValueContentRangeHeader("bytes", size),
);
sendError(416, {
headers: {
"Content-Range": res.getHeader("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) {
context.logger.error(
"A 'Range' header with multiple ranges was provided. Multiple ranges are not supported, so a regular response will be sent for this request.",
);
}
if (parsedRanges !== -2 && parsedRanges.length === 1) {
// Content-Range
setStatusCode(res, 206);
res.setHeader(
"Content-Range",
getValueContentRangeHeader(
"bytes",
size,
/** @type {import("range-parser").Ranges} */ (parsedRanges)[0],
),
);
[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 } = createReadStreamOrReadFileSync(
filename,
context.outputFileSystem,
start,
end,
));
} catch (_ignoreError) {
await goNext();
return;
}
}
if (context.options.modifyResponseData) {
({ data: bufferOrStream, byteLength } =
context.options.modifyResponseData(
req,
res,
bufferOrStream,
// @ts-ignore
byteLength,
));
}
// @ts-ignore
res.setHeader("Content-Length", byteLength);
if (req.method === "HEAD") {
// For Koa
if (res.statusCode === 404) {
setStatusCode(res, 200);
}
res.end();
return;
}
const isPipeSupports =
typeof (
/** @type {import("fs").ReadStream} */ (bufferOrStream).pipe
) === "function";
if (!isPipeSupports) {
send(res, /** @type {Buffer} */ (bufferOrStream));
return;
}
// Cleanup
const cleanup = () => {
destroyStream(
/** @type {import("fs").ReadStream} */ (bufferOrStream),
true,
);
};
// Error handling
/** @type {import("fs").ReadStream} */
(bufferOrStream).on("error", (error) => {
// clean up stream early
cleanup();
// Handle Error
switch (/** @type {NodeJS.ErrnoException} */ (error).code) {
case "ENAMETOOLONG":
case "ENOENT":
case "ENOTDIR":
sendError(404, {
modifyResponseData: context.options.modifyResponseData,
});
break;
default:
sendError(500, {
modifyResponseData: context.options.modifyResponseData,
});
break;
}
});
pipe(res, /** @type {ReadStream} */ (bufferOrStream));
// Response finished, cleanup
onFinishedStream(res, cleanup);
}
ready(context, processRequest, req);
};
}
module.exports = wrapper;