12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
- import { assert } from 'workbox-core/_private/assert.js';
- import { logger } from 'workbox-core/_private/logger.js';
- import { calculateEffectiveBoundaries } from './utils/calculateEffectiveBoundaries.js';
- import { parseRangeHeader } from './utils/parseRangeHeader.js';
- import './_version.js';
- async function createPartialResponse(request, originalResponse) {
- try {
- if (process.env.NODE_ENV !== 'production') {
- assert.isInstance(request, Request, {
- moduleName: 'workbox-range-requests',
- funcName: 'createPartialResponse',
- paramName: 'request',
- });
- assert.isInstance(originalResponse, Response, {
- moduleName: 'workbox-range-requests',
- funcName: 'createPartialResponse',
- paramName: 'originalResponse',
- });
- }
- if (originalResponse.status === 206) {
-
-
- return originalResponse;
- }
- const rangeHeader = request.headers.get('range');
- if (!rangeHeader) {
- throw new WorkboxError('no-range-header');
- }
- const boundaries = parseRangeHeader(rangeHeader);
- const originalBlob = await originalResponse.blob();
- const effectiveBoundaries = calculateEffectiveBoundaries(originalBlob, boundaries.start, boundaries.end);
- const slicedBlob = originalBlob.slice(effectiveBoundaries.start, effectiveBoundaries.end);
- const slicedBlobSize = slicedBlob.size;
- const slicedResponse = new Response(slicedBlob, {
-
-
- status: 206,
- statusText: 'Partial Content',
- headers: originalResponse.headers,
- });
- slicedResponse.headers.set('Content-Length', String(slicedBlobSize));
- slicedResponse.headers.set('Content-Range', `bytes ${effectiveBoundaries.start}-${effectiveBoundaries.end - 1}/` +
- `${originalBlob.size}`);
- return slicedResponse;
- }
- catch (error) {
- if (process.env.NODE_ENV !== 'production') {
- logger.warn(`Unable to construct a partial response; returning a ` +
- `416 Range Not Satisfiable response instead.`);
- logger.groupCollapsed(`View details here.`);
- logger.log(error);
- logger.log(request);
- logger.log(originalResponse);
- logger.groupEnd();
- }
- return new Response('', {
- status: 416,
- statusText: 'Range Not Satisfiable',
- });
- }
- }
- export { createPartialResponse };
|