Skip to content

feat: don't read full file if range header is present #1002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
37 changes: 20 additions & 17 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ export default function wrapper(context) {
headers = headers(req, res, context);
}

let content;
let fileSize;

if (!filename) {
await goNext();
return;
}

try {
content = context.outputFileSystem.readFileSync(filename);
fileSize = context.outputFileSystem.lstatSync(filename).size;
} catch (_ignoreError) {
await goNext();
return;
Expand Down Expand Up @@ -100,21 +100,24 @@ export default function wrapper(context) {
}

// Buffer
content = handleRangeHeaders(context, content, req, res);

// Express API
if (res.send) {
res.send(content);
}
// Node.js API
else {
res.setHeader("Content-Length", content.length);

if (req.method === "HEAD") {
res.end();
} else {
res.end(content);
}
const ranges = handleRangeHeaders(context, fileSize, req, res);
const stream = context.outputFileSystem.createReadStream(
filename,
ranges
? {
start: ranges.start,
end: ranges.end,
}
: {}
);

const responseSize = ranges ? 1 + (ranges.end - ranges.start) : fileSize;
res.setHeader("Content-Length", responseSize);

if (req.method === "HEAD") {
res.end();
} else {
stream.pipe(res);
}
}
};
Expand Down
20 changes: 8 additions & 12 deletions src/utils/handleRangeHeaders.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import parseRange from "range-parser";

export default function handleRangeHeaders(context, content, req, res) {
export default function handleRangeHeaders(context, size, req, res) {
// assumes express API. For other servers, need to add logic to access
// alternative header APIs
if (res.set) {
Expand All @@ -21,20 +21,20 @@ export default function handleRangeHeaders(context, content, req, res) {
}

if (range) {
const ranges = parseRange(content.length, range);
const ranges = parseRange(size, range);

// unsatisfiable
if (ranges === -1) {
// Express API
if (res.set) {
res.set("Content-Range", `bytes */${content.length}`);
res.set("Content-Range", `bytes */${size}`);
res.status(416);
}
// Node.js API
else {
// eslint-disable-next-line no-param-reassign
res.statusCode = 416;
res.setHeader("Content-Range", `bytes */${content.length}`);
res.setHeader("Content-Range", `bytes */${size}`);
}
} else if (ranges === -2) {
// malformed header treated as regular response
Expand All @@ -47,16 +47,13 @@ export default function handleRangeHeaders(context, content, req, res) {
"A Range header with multiple ranges was provided. Multiple ranges are not supported, so a regular response will be sent for this request."
);
} else {
// valid range header
const { length } = content;

// Express API
if (res.set) {
// Content-Range
res.status(206);
res.set(
"Content-Range",
`bytes ${ranges[0].start}-${ranges[0].end}/${length}`
`bytes ${ranges[0].start}-${ranges[0].end}/${size}`
);
}
// Node.js API
Expand All @@ -66,14 +63,13 @@ export default function handleRangeHeaders(context, content, req, res) {
res.statusCode = 206;
res.setHeader(
"Content-Range",
`bytes ${ranges[0].start}-${ranges[0].end}/${length}`
`bytes ${ranges[0].start}-${ranges[0].end}/${size}`
);
}

// eslint-disable-next-line no-param-reassign
content = content.slice(ranges[0].start, ranges[0].end + 1);
return ranges[0];
}
}

return content;
return null;
}
26 changes: 23 additions & 3 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,23 +231,43 @@ describe.each([
});

it('should return the "206" code for the "GET" request with the valid range header', (done) => {
const fileData = instance.context.outputFileSystem.readFileSync(
path.resolve(outputPath, "bundle.js"),
"utf8"
);
request(app)
.get("/bundle.js")
.set("Range", "bytes=3000-3500")
.expect("Content-Length", "501")
.expect("Content-Range", `bytes 3000-3500/${codeLength}`)
.expect(206, done);
.expect(206)
.then((response) => {
expect(response.text).toBe(fileData.substr(3000, 501));
expect(response.text.length).toBe(501);
done();
});
});

it('should return the "200" code for the "GET" request with malformed range header which is ignored', (done) => {
request(app).get("/bundle.js").set("Range", "abc").expect(200, done);
request(app)
.get("/bundle.js")
.set("Range", "abc")
.expect(200)
.then((response) => {
expect(response.text.length).toBe(codeLength);
done();
});
});

it('should return the "200" code for the "GET" request with multiple range header which is ignored', (done) => {
request(app)
.get("/bundle.js")
.set("Range", "bytes=3000-3100,3200-3300")
.expect(200, done);
.expect(200)
.then((response) => {
expect(response.text.length).toBe(codeLength);
done();
});
});

it('should return the "404" code for the "GET" request with to the non-public path', (done) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ Array [
]
`;

exports[`handleRangeHeaders should handle multiple ranges 1`] = `
exports[`handleRangeHeaders should handle multiple ranges by sending a regular response 1`] = `
Array [
Array [
"A Range header with multiple ranges was provided. Multiple ranges are not supported, so a regular response will be sent for this request.",
],
]
`;

exports[`handleRangeHeaders should handle multiple ranges 2`] = `
exports[`handleRangeHeaders should handle multiple ranges by sending a regular response 2`] = `
Array [
Array [
"Accept-Ranges",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ Array [
]
`;

exports[`handleRangeHeaders should handle multiple ranges 1`] = `
exports[`handleRangeHeaders should handle multiple ranges by sending a regular response 1`] = `
Array [
Array [
"A Range header with multiple ranges was provided. Multiple ranges are not supported, so a regular response will be sent for this request.",
],
]
`;

exports[`handleRangeHeaders should handle multiple ranges 2`] = `
exports[`handleRangeHeaders should handle multiple ranges by sending a regular response 2`] = `
Array [
Array [
"Accept-Ranges",
Expand Down
18 changes: 9 additions & 9 deletions test/utils/handleRangeHeaders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ describe("handleRangeHeaders", () => {
},
};

const contentRes = handleRangeHeaders(context, content, req, res);
expect(contentRes).toEqual("bcde");
const ranges = handleRangeHeaders(context, content.length, req, res);
expect(ranges).toEqual({ start: 1, end: 4 });
expect(res.statusCode).toEqual(206);
expect(res.set.mock.calls).toMatchSnapshot();
});
Expand All @@ -53,8 +53,8 @@ describe("handleRangeHeaders", () => {
},
};

const contentRes = handleRangeHeaders(context, content, req, res);
expect(contentRes).toEqual("abcdef");
const ranges = handleRangeHeaders(context, content, req, res);
expect(ranges).toEqual(null);
expect(context.logger.error.mock.calls).toMatchSnapshot();
expect(res.statusCode).toBeUndefined();
expect(res.set.mock.calls).toMatchSnapshot();
Expand All @@ -78,13 +78,13 @@ describe("handleRangeHeaders", () => {
},
};

const contentRes = handleRangeHeaders(context, content, req, res);
expect(contentRes).toEqual("abcdef");
const ranges = handleRangeHeaders(context, content.length, req, res);
expect(ranges).toEqual(null);
expect(res.statusCode).toEqual(416);
expect(res.set.mock.calls).toMatchSnapshot();
});

it("should handle multiple ranges", () => {
it("should handle multiple ranges by sending a regular response", () => {
const content = "abcdef";
const req = {
headers: {
Expand All @@ -102,8 +102,8 @@ describe("handleRangeHeaders", () => {
},
};

const contentRes = handleRangeHeaders(context, content, req, res);
expect(contentRes).toEqual("abcdef");
const ranges = handleRangeHeaders(context, content.length, req, res);
expect(ranges).toEqual(null);
expect(context.logger.error.mock.calls).toMatchSnapshot();
expect(res.statusCode).toBeUndefined();
expect(res.set.mock.calls).toMatchSnapshot();
Expand Down