Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 1 addition & 120 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"mime": "^4.1.0",
"minimist": "^1.2.8",
"morphdom": "^2.7.8",
"send": "^1.2.1",
"range-parser": "^1.2.1",
"urlpattern-polyfill": "^10.1.0",
"ws": "^8.20.0"
},
Expand Down
75 changes: 70 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "urlpattern-polyfill";
import finalhandler from "finalhandler";
import WebSocket, { WebSocketServer } from "ws";
import mime from "mime";
import send from "send";
import parseRange from "range-parser";
import chokidar from "chokidar";
import { TemplatePath, isPlainObject } from "@11ty/eleventy-utils";
import debugUtil from "debug";
Expand Down Expand Up @@ -648,7 +648,20 @@ export default class EleventyDevServer {
return `sha512-${crypto.createHash("sha512").update(data).digest("base64")}`
}

// This runs at the end of the middleware chain
/**
* @param {String} type
* @param {number} size
* @param {number=} range
*/
#contentRange(type, size, range) {
return type + ' ' + (range ? range.start + '-' + range.end : '*') + '/' + size
}

/**
* @param {import('node:http').IncomingMessage} req
* @param {import('node:http').OutgoingMessage} res
* This runs at the end of the middleware chain
*/
eleventyProjectMiddleware(req, res) {
// Known issue with `finalhandler` and HTTP/2:
// UnsupportedWarning: Status message is not supported by HTTP/2 (RFC7540 8.1.2.4)
Expand Down Expand Up @@ -678,10 +691,62 @@ export default class EleventyDevServer {
if (match) {
if (match.statusCode === 200 && match.filepath) {
// Content-Range request, probably Safari trying to stream video
if (req.headers.range) {
return send(req, match.filepath).pipe(res);
// If the client includes an If-Range header,
// serve them the whole thing. We don't include
// last-modified or etags headers, so these
// requests are invalid.
if (req.headers.range && !req.headers['if-range']) {
return fs.stat(match.filepath, (err, stat) => {
if (err) {
res.statusCode = 404;
res.send('File not found');
return;
}

let len = stat.size;
let offset = 0;

const ranges = parseRange(len, req.headers.range, {
combine: true
})

// Tell clients that they can send ranges.
res.setHeader('Accept-Ranges', 'bytes');
res.setHeader('Cache-Control', 'public, max-age=0');

// unsatisfiable
if (ranges === -1) {
// 416 Requested Range Not Satisfiable
res.statusCode = 416;
res.setHeader('Content-Range', this.#contentRange('bytes', len))
return res.end();
} else if (ranges !== -2 && ranges.length === 1) {
// valid (syntactically invalid/multiple ranges are treated as a regular response)
// Content-Range
res.statusCode = 206;
res.setHeader('Content-Range', this.#contentRange('bytes', len, ranges[0]))

// adjust for requested range
let start = ranges[0].start
len = ranges[0].end - ranges[0].start + 1
let end = Math.max(offset, offset + len - 1)
res.setHeader('Content-Length', len)
if (req.method === 'HEAD') {
res.end()
return
}
const stream = fs.createReadStream(match.filepath, {
start, end
});
stream.pipe(res);
const cleanup = () => {
stream.destroy();
}
stream.on('error', cleanup);
stream.on('end', cleanup);
}
});
}

return this.renderFile(match.filepath, res);
}

Expand Down