25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

parseRangeHeader.mjs 2.2 KiB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 {string} rangeHeader A Range: header value.
  18. * @return {Object} An object with `start` and `end` properties, reflecting
  19. * the parsed value of the Range: header. If either the `start` or `end` are
  20. * omitted, then `null` will be returned.
  21. *
  22. * @private
  23. */
  24. function parseRangeHeader(rangeHeader) {
  25. if (process.env.NODE_ENV !== 'production') {
  26. assert.isType(rangeHeader, 'string', {
  27. moduleName: 'workbox-range-requests',
  28. funcName: 'parseRangeHeader',
  29. paramName: 'rangeHeader',
  30. });
  31. }
  32. const normalizedRangeHeader = rangeHeader.trim().toLowerCase();
  33. if (!normalizedRangeHeader.startsWith('bytes=')) {
  34. throw new WorkboxError('unit-must-be-bytes', {normalizedRangeHeader});
  35. }
  36. // Specifying multiple ranges separate by commas is valid syntax, but this
  37. // library only attempts to handle a single, contiguous sequence of bytes.
  38. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range#Syntax
  39. if (normalizedRangeHeader.includes(',')) {
  40. throw new WorkboxError('single-range-only', {normalizedRangeHeader});
  41. }
  42. const rangeParts = /(\d*)-(\d*)/.exec(normalizedRangeHeader);
  43. // We need either at least one of the start or end values.
  44. if (rangeParts === null || !(rangeParts[1] || rangeParts[2])) {
  45. throw new WorkboxError('invalid-range-values', {normalizedRangeHeader});
  46. }
  47. return {
  48. start: rangeParts[1] === '' ? null : Number(rangeParts[1]),
  49. end: rangeParts[2] === '' ? null : Number(rangeParts[2]),
  50. };
  51. }
  52. export {parseRangeHeader};