source: frontend/node_modules/workbox-range-requests/utils/parseRangeHeader.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.9 KB
Line 
1/*
2 Copyright 2018 Google LLC
3
4 Use of this source code is governed by an MIT-style
5 license that can be found in the LICENSE file or at
6 https://opensource.org/licenses/MIT.
7*/
8import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
9import { assert } from 'workbox-core/_private/assert.js';
10import '../_version.js';
11/**
12 * @param {string} rangeHeader A Range: header value.
13 * @return {Object} An object with `start` and `end` properties, reflecting
14 * the parsed value of the Range: header. If either the `start` or `end` are
15 * omitted, then `null` will be returned.
16 *
17 * @private
18 */
19function parseRangeHeader(rangeHeader) {
20 if (process.env.NODE_ENV !== 'production') {
21 assert.isType(rangeHeader, 'string', {
22 moduleName: 'workbox-range-requests',
23 funcName: 'parseRangeHeader',
24 paramName: 'rangeHeader',
25 });
26 }
27 const normalizedRangeHeader = rangeHeader.trim().toLowerCase();
28 if (!normalizedRangeHeader.startsWith('bytes=')) {
29 throw new WorkboxError('unit-must-be-bytes', { normalizedRangeHeader });
30 }
31 // Specifying multiple ranges separate by commas is valid syntax, but this
32 // library only attempts to handle a single, contiguous sequence of bytes.
33 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range#Syntax
34 if (normalizedRangeHeader.includes(',')) {
35 throw new WorkboxError('single-range-only', { normalizedRangeHeader });
36 }
37 const rangeParts = /(\d*)-(\d*)/.exec(normalizedRangeHeader);
38 // We need either at least one of the start or end values.
39 if (!rangeParts || !(rangeParts[1] || rangeParts[2])) {
40 throw new WorkboxError('invalid-range-values', { normalizedRangeHeader });
41 }
42 return {
43 start: rangeParts[1] === '' ? undefined : Number(rangeParts[1]),
44 end: rangeParts[2] === '' ? undefined : Number(rangeParts[2]),
45 };
46}
47export { parseRangeHeader };
Note: See TracBrowser for help on using the repository browser.