MCPcopy Create free account
hub / github.com/11ty/dev-server / eleventyDevServerMiddleware

Method eleventyDevServerMiddleware

server.js:479–557  ·  view source on GitHub ↗
(req, res, next)

Source from the content-addressed store, hash-verified

477 }
478
479 async eleventyDevServerMiddleware(req, res, next) {
480 if(this.#serverState === "CLOSING") {
481 return res.end("");
482 }
483
484 for(let urlPatternString in this.options.onRequest) {
485 let fn = this.options.onRequest[urlPatternString];
486 let fullPath = this.getServerPath(urlPatternString);
487 let p = new URLPattern({ pathname: fullPath });
488
489 // request url should already include pathprefix.
490 let fullUrl = this.getServerUrlRaw("localhost", req.url);
491 let match = p.exec(fullUrl);
492
493 let u = new URL(fullUrl);
494
495 if(match) {
496 let result = await fn({
497 url: u,
498 pattern: p,
499 patternGroups: match?.pathname?.groups || {},
500 });
501
502 if(!result && result !== "") {
503 continue;
504 }
505
506 if(typeof result === "string") {
507 return res.end(result);
508 }
509
510 if(isPlainObject(result) || result instanceof Response) {
511 if(typeof result.status === "number") {
512 res.statusCode = result.status;
513 }
514
515 if(result.headers instanceof Headers) {
516 for(let [key, value] of result.headers.entries()) {
517 res.setHeader(key, value);
518 }
519 } else if(isPlainObject(result.headers)) {
520 for(let key of Object.keys(result.headers)) {
521 res.setHeader(key, result.headers[key]);
522 }
523 }
524
525 if(result instanceof Response) {
526 // no gzip/br compression here, uncompressed from fetch https://github.com/w3c/ServiceWorker/issues/339
527 res.removeHeader("content-encoding");
528
529 let arrayBuffer = await result.arrayBuffer();
530 res.setHeader("content-length", arrayBuffer.byteLength);
531
532 let buffer = Buffer.from(arrayBuffer);
533 return res.end(buffer);
534 }
535
536 return res.end(result.body || "");

Callers

nothing calls this directly

Calls 4

getServerPathMethod · 0.95
getServerUrlRawMethod · 0.95
#getFileContentsMethod · 0.95
#readFileMethod · 0.95

Tested by

no test coverage detected