Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 {logger} from 'workbox-core/_private/logger.mjs';
  16. import {calculateEffectiveBoundaries} from
  17. './utils/calculateEffectiveBoundaries.mjs';
  18. import {parseRangeHeader} from './utils/parseRangeHeader.mjs';
  19. import './_version.mjs';
  20. /**
  21. * Given a `Request` and `Response` objects as input, this will return a
  22. * promise for a new `Response`.
  23. *
  24. * @param {Request} request A request, which should contain a Range:
  25. * header.
  26. * @param {Response} originalResponse An original response containing the full
  27. * content.
  28. * @return {Promise<Response>} Either a `206 Partial Content` response, with
  29. * the response body set to the slice of content specified by the request's
  30. * `Range:` header, or a `416 Range Not Satisfiable` response if the
  31. * conditions of the `Range:` header can't be met.
  32. *
  33. * @memberof workbox.rangeRequests
  34. */
  35. async function createPartialResponse(request, originalResponse) {
  36. try {
  37. if (process.env.NODE_ENV !== 'production') {
  38. assert.isInstance(request, Request, {
  39. moduleName: 'workbox-range-requests',
  40. funcName: 'createPartialResponse',
  41. paramName: 'request',
  42. });
  43. assert.isInstance(originalResponse, Response, {
  44. moduleName: 'workbox-range-requests',
  45. funcName: 'createPartialResponse',
  46. paramName: 'originalResponse',
  47. });
  48. }
  49. const rangeHeader = request.headers.get('range');
  50. if (!rangeHeader) {
  51. throw new WorkboxError('no-range-header');
  52. }
  53. const boundaries = parseRangeHeader(rangeHeader);
  54. const originalBlob = await originalResponse.blob();
  55. const effectiveBoundaries = calculateEffectiveBoundaries(
  56. originalBlob, boundaries.start, boundaries.end);
  57. const slicedBlob = originalBlob.slice(effectiveBoundaries.start,
  58. effectiveBoundaries.end);
  59. const slicedBlobSize = slicedBlob.size;
  60. const slicedResponse = new Response(slicedBlob, {
  61. // Status code 206 is for a Partial Content response.
  62. // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206
  63. status: 206,
  64. statusText: 'Partial Content',
  65. headers: originalResponse.headers,
  66. });
  67. slicedResponse.headers.set('Content-Length', slicedBlobSize);
  68. slicedResponse.headers.set('Content-Range',
  69. `bytes ${effectiveBoundaries.start}-${effectiveBoundaries.end - 1}/` +
  70. originalBlob.size);
  71. return slicedResponse;
  72. } catch (error) {
  73. if (process.env.NODE_ENV !== 'production') {
  74. logger.warn(`Unable to construct a partial response; returning a ` +
  75. `416 Range Not Satisfiable response instead.`);
  76. logger.groupCollapsed(`View details here.`);
  77. logger.unprefixed.log(error);
  78. logger.unprefixed.log(request);
  79. logger.unprefixed.log(originalResponse);
  80. logger.groupEnd();
  81. }
  82. return new Response('', {
  83. status: 416,
  84. statusText: 'Range Not Satisfiable',
  85. });
  86. }
  87. }
  88. export {createPartialResponse};