Skip to content

Commit

Permalink
Merge pull request #62 from joemaller/main
Browse files Browse the repository at this point in the history
Add support for byte-range requests
  • Loading branch information
zachleat authored Apr 19, 2024
2 parents bc1cdd1 + 0a56a88 commit fc4c3cb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"minimist": "^1.2.8",
"morphdom": "^2.7.0",
"please-upgrade-node": "^3.2.0",
"send": "^0.18.0",
"ssri": "^8.0.1",
"urlpattern-polyfill": "^10.0.0",
"ws": "^8.13.0"
Expand Down
6 changes: 6 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const WebSocket = require("ws");
const { WebSocketServer } = WebSocket;
const mime = require("mime");
const ssri = require("ssri");
const send = require("send");
const devip = require("dev-ip");
const chokidar = require("chokidar");
const { TemplatePath, isPlainObject } = require("@11ty/eleventy-utils");
Expand Down Expand Up @@ -515,6 +516,11 @@ class EleventyDevServer {
debug( req.url, match );

if (match) {
// Content-Range request, probably Safari trying to stream video
if (req.headers.range) {
return send(req, match.filepath).pipe(res);
}

if (match.statusCode === 200 && match.filepath) {
return this.renderFile(match.filepath, res);
}
Expand Down
46 changes: 43 additions & 3 deletions test/testServerRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function getOptions(options = {}) {
info: function() {},
error: function() {},
};
options.portReassignmentRetryCount = 100;
return options;
}

Expand Down Expand Up @@ -42,7 +43,7 @@ async function makeRequestTo(t, server, path) {
})
}

async function fetchHeadersForRequest(t, server, path) {
async function fetchHeadersForRequest(t, server, path, extras) {
let port = await server.getPort();

return new Promise(resolve => {
Expand All @@ -51,12 +52,18 @@ async function fetchHeadersForRequest(t, server, path) {
port,
path,
method: 'GET',
...extras,
};

// Available status codes can be found here: http.STATUS_CODES
const successCodes = [
200, // OK
206, // Partial Content
];
http.get(options, (res) => {
const { statusCode } = res;
if(statusCode !== 200) {
throw new Error("Invalid status code" + statusCode);
if (!successCodes.includes(statusCode)) {
throw new Error("Invalid status code " + statusCode);
}

let headers = res.headers;
Expand Down Expand Up @@ -302,3 +309,36 @@ test("Content-Type header via middleware", async t => {

server.close();
});

test("Content-Range request", async (t) => {
let server = new EleventyDevServer(
"test-server",
"./test/stubs/",
getOptions()
);
server.serve(8100);

const options = { headers: { Range: "bytes=0-48" } };
let data = await fetchHeadersForRequest(t, server, `/index.html`, options);
t.true("accept-ranges" in data);
t.true(data["accept-ranges"] === "bytes");
t.true("content-range" in data);
t.true(data["content-range"].startsWith("bytes 0-48/"));

server.close();
});

test("Standard request does not include range headers", async (t) => {
let server = new EleventyDevServer(
"test-server",
"./test/stubs/",
getOptions()
);
server.serve(8100);

let data = await fetchHeadersForRequest(t, server, `/index.html`);
t.false("accept-ranges" in data);
t.false("content-range" in data);

server.close();
});

0 comments on commit fc4c3cb

Please sign in to comment.