| 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 | import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
|
|---|
| 9 | import { assert } from 'workbox-core/_private/assert.js';
|
|---|
| 10 | import '../_version.js';
|
|---|
| 11 | /**
|
|---|
| 12 | * @param {Blob} blob A source blob.
|
|---|
| 13 | * @param {number} [start] The offset to use as the start of the
|
|---|
| 14 | * slice.
|
|---|
| 15 | * @param {number} [end] The offset to use as the end of the slice.
|
|---|
| 16 | * @return {Object} An object with `start` and `end` properties, reflecting
|
|---|
| 17 | * the effective boundaries to use given the size of the blob.
|
|---|
| 18 | *
|
|---|
| 19 | * @private
|
|---|
| 20 | */
|
|---|
| 21 | function calculateEffectiveBoundaries(blob, start, end) {
|
|---|
| 22 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 23 | assert.isInstance(blob, Blob, {
|
|---|
| 24 | moduleName: 'workbox-range-requests',
|
|---|
| 25 | funcName: 'calculateEffectiveBoundaries',
|
|---|
| 26 | paramName: 'blob',
|
|---|
| 27 | });
|
|---|
| 28 | }
|
|---|
| 29 | const blobSize = blob.size;
|
|---|
| 30 | if ((end && end > blobSize) || (start && start < 0)) {
|
|---|
| 31 | throw new WorkboxError('range-not-satisfiable', {
|
|---|
| 32 | size: blobSize,
|
|---|
| 33 | end,
|
|---|
| 34 | start,
|
|---|
| 35 | });
|
|---|
| 36 | }
|
|---|
| 37 | let effectiveStart;
|
|---|
| 38 | let effectiveEnd;
|
|---|
| 39 | if (start !== undefined && end !== undefined) {
|
|---|
| 40 | effectiveStart = start;
|
|---|
| 41 | // Range values are inclusive, so add 1 to the value.
|
|---|
| 42 | effectiveEnd = end + 1;
|
|---|
| 43 | }
|
|---|
| 44 | else if (start !== undefined && end === undefined) {
|
|---|
| 45 | effectiveStart = start;
|
|---|
| 46 | effectiveEnd = blobSize;
|
|---|
| 47 | }
|
|---|
| 48 | else if (end !== undefined && start === undefined) {
|
|---|
| 49 | effectiveStart = blobSize - end;
|
|---|
| 50 | effectiveEnd = blobSize;
|
|---|
| 51 | }
|
|---|
| 52 | return {
|
|---|
| 53 | start: effectiveStart,
|
|---|
| 54 | end: effectiveEnd,
|
|---|
| 55 | };
|
|---|
| 56 | }
|
|---|
| 57 | export { calculateEffectiveBoundaries };
|
|---|