[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = handleRangeHeaders;
|
---|
| 7 |
|
---|
| 8 | var _rangeParser = _interopRequireDefault(require("range-parser"));
|
---|
| 9 |
|
---|
| 10 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 11 |
|
---|
| 12 | function handleRangeHeaders(context, content, req, res) {
|
---|
| 13 | // assumes express API. For other servers, need to add logic to access
|
---|
| 14 | // alternative header APIs
|
---|
| 15 | if (res.set) {
|
---|
| 16 | res.set("Accept-Ranges", "bytes");
|
---|
| 17 | } else {
|
---|
| 18 | res.setHeader("Accept-Ranges", "bytes");
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | let range; // Express API
|
---|
| 22 |
|
---|
| 23 | if (req.get) {
|
---|
| 24 | range = req.get("range");
|
---|
| 25 | } // Node.js API
|
---|
| 26 | else {
|
---|
| 27 | ({
|
---|
| 28 | range
|
---|
| 29 | } = req.headers);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | if (range) {
|
---|
| 33 | const ranges = (0, _rangeParser.default)(content.length, range); // unsatisfiable
|
---|
| 34 |
|
---|
| 35 | if (ranges === -1) {
|
---|
| 36 | // Express API
|
---|
| 37 | if (res.set) {
|
---|
| 38 | res.set("Content-Range", `bytes */${content.length}`);
|
---|
| 39 | res.status(416);
|
---|
| 40 | } // Node.js API
|
---|
| 41 | else {
|
---|
| 42 | // eslint-disable-next-line no-param-reassign
|
---|
| 43 | res.statusCode = 416;
|
---|
| 44 | res.setHeader("Content-Range", `bytes */${content.length}`);
|
---|
| 45 | }
|
---|
| 46 | } else if (ranges === -2) {
|
---|
| 47 | // malformed header treated as regular response
|
---|
| 48 | context.logger.error("A malformed Range header was provided. A regular response will be sent for this request.");
|
---|
| 49 | } else if (ranges.length !== 1) {
|
---|
| 50 | // multiple ranges treated as regular response
|
---|
| 51 | 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.");
|
---|
| 52 | } else {
|
---|
| 53 | // valid range header
|
---|
| 54 | const {
|
---|
| 55 | length
|
---|
| 56 | } = content; // Express API
|
---|
| 57 |
|
---|
| 58 | if (res.set) {
|
---|
| 59 | // Content-Range
|
---|
| 60 | res.status(206);
|
---|
| 61 | res.set("Content-Range", `bytes ${ranges[0].start}-${ranges[0].end}/${length}`);
|
---|
| 62 | } // Node.js API
|
---|
| 63 | else {
|
---|
| 64 | // Content-Range
|
---|
| 65 | // eslint-disable-next-line no-param-reassign
|
---|
| 66 | res.statusCode = 206;
|
---|
| 67 | res.setHeader("Content-Range", `bytes ${ranges[0].start}-${ranges[0].end}/${length}`);
|
---|
| 68 | } // eslint-disable-next-line no-param-reassign
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | content = content.slice(ranges[0].start, ranges[0].end + 1);
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | return content;
|
---|
| 76 | } |
---|