You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

73 lines
1.9 KiB

  1. /*
  2. Copyright 2017 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import {WorkboxError} from 'workbox-core/_private/WorkboxError.mjs';
  14. import {assert} from 'workbox-core/_private/assert.mjs';
  15. import '../_version.mjs';
  16. /**
  17. * @param {Blob} blob A source blob.
  18. * @param {number|null} start The offset to use as the start of the
  19. * slice.
  20. * @param {number|null} end The offset to use as the end of the slice.
  21. * @return {Object} An object with `start` and `end` properties, reflecting
  22. * the effective boundaries to use given the size of the blob.
  23. *
  24. * @private
  25. */
  26. function calculateEffectiveBoundaries(blob, start, end) {
  27. if (process.env.NODE_ENV !== 'production') {
  28. assert.isInstance(blob, Blob, {
  29. moduleName: 'workbox-range-requests',
  30. funcName: 'calculateEffectiveBoundaries',
  31. paramName: 'blob',
  32. });
  33. }
  34. const blobSize = blob.size;
  35. if (end > blobSize || start < 0) {
  36. throw new WorkboxError('range-not-satisfiable', {
  37. size: blobSize,
  38. end,
  39. start,
  40. });
  41. }
  42. let effectiveStart;
  43. let effectiveEnd;
  44. if (start === null) {
  45. effectiveStart = blobSize - end;
  46. effectiveEnd = blobSize;
  47. } else if (end === null) {
  48. effectiveStart = start;
  49. effectiveEnd = blobSize;
  50. } else {
  51. effectiveStart = start;
  52. // Range values are inclusive, so add 1 to the value.
  53. effectiveEnd = end + 1;
  54. }
  55. return {
  56. start: effectiveStart,
  57. end: effectiveEnd,
  58. };
  59. }
  60. export {calculateEffectiveBoundaries};