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

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 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*/
8
9import {WorkboxError} from 'workbox-core/_private/WorkboxError.js';
10import {assert} from 'workbox-core/_private/assert.js';
11import '../_version.js';
12
13/**
14 * @param {string} rangeHeader A Range: header value.
15 * @return {Object} An object with `start` and `end` properties, reflecting
16 * the parsed value of the Range: header. If either the `start` or `end` are
17 * omitted, then `null` will be returned.
18 *
19 * @private
20 */
21function parseRangeHeader(rangeHeader: string): {start?: number; end?: number} {
22 if (process.env.NODE_ENV !== 'production') {
23 assert!.isType(rangeHeader, 'string', {
24 moduleName: 'workbox-range-requests',
25 funcName: 'parseRangeHeader',
26 paramName: 'rangeHeader',
27 });
28 }
29
30 const normalizedRangeHeader = rangeHeader.trim().toLowerCase();
31 if (!normalizedRangeHeader.startsWith('bytes=')) {
32 throw new WorkboxError('unit-must-be-bytes', {normalizedRangeHeader});
33 }
34
35 // Specifying multiple ranges separate by commas is valid syntax, but this
36 // library only attempts to handle a single, contiguous sequence of bytes.
37 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range#Syntax
38 if (normalizedRangeHeader.includes(',')) {
39 throw new WorkboxError('single-range-only', {normalizedRangeHeader});
40 }
41
42 const rangeParts = /(\d*)-(\d*)/.exec(normalizedRangeHeader);
43 // We need either at least one of the start or end values.
44 if (!rangeParts || !(rangeParts[1] || rangeParts[2])) {
45 throw new WorkboxError('invalid-range-values', {normalizedRangeHeader});
46 }
47
48 return {
49 start: rangeParts[1] === '' ? undefined : Number(rangeParts[1]),
50 end: rangeParts[2] === '' ? undefined : Number(rangeParts[2]),
51 };
52}
53
54export {parseRangeHeader};
Note: See TracBrowser for help on using the repository browser.