123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- this.workbox = this.workbox || {};
- this.workbox.rangeRequests = (function (exports, WorkboxError_js, assert_js, logger_js) {
- 'use strict';
- try {
- self['workbox:range-requests:6.6.0'] && _();
- } catch (e) {}
-
-
- function calculateEffectiveBoundaries(blob, start, end) {
- {
- assert_js.assert.isInstance(blob, Blob, {
- moduleName: 'workbox-range-requests',
- funcName: 'calculateEffectiveBoundaries',
- paramName: 'blob'
- });
- }
- const blobSize = blob.size;
- if (end && end > blobSize || start && start < 0) {
- throw new WorkboxError_js.WorkboxError('range-not-satisfiable', {
- size: blobSize,
- end,
- start
- });
- }
- let effectiveStart;
- let effectiveEnd;
- if (start !== undefined && end !== undefined) {
- effectiveStart = start;
- effectiveEnd = end + 1;
- } else if (start !== undefined && end === undefined) {
- effectiveStart = start;
- effectiveEnd = blobSize;
- } else if (end !== undefined && start === undefined) {
- effectiveStart = blobSize - end;
- effectiveEnd = blobSize;
- }
- return {
- start: effectiveStart,
- end: effectiveEnd
- };
- }
-
-
- function parseRangeHeader(rangeHeader) {
- {
- assert_js.assert.isType(rangeHeader, 'string', {
- moduleName: 'workbox-range-requests',
- funcName: 'parseRangeHeader',
- paramName: 'rangeHeader'
- });
- }
- const normalizedRangeHeader = rangeHeader.trim().toLowerCase();
- if (!normalizedRangeHeader.startsWith('bytes=')) {
- throw new WorkboxError_js.WorkboxError('unit-must-be-bytes', {
- normalizedRangeHeader
- });
- }
-
-
- if (normalizedRangeHeader.includes(',')) {
- throw new WorkboxError_js.WorkboxError('single-range-only', {
- normalizedRangeHeader
- });
- }
- const rangeParts = /(\d*)-(\d*)/.exec(normalizedRangeHeader);
- if (!rangeParts || !(rangeParts[1] || rangeParts[2])) {
- throw new WorkboxError_js.WorkboxError('invalid-range-values', {
- normalizedRangeHeader
- });
- }
- return {
- start: rangeParts[1] === '' ? undefined : Number(rangeParts[1]),
- end: rangeParts[2] === '' ? undefined : Number(rangeParts[2])
- };
- }
-
-
- async function createPartialResponse(request, originalResponse) {
- try {
- if ("dev" !== 'production') {
- assert_js.assert.isInstance(request, Request, {
- moduleName: 'workbox-range-requests',
- funcName: 'createPartialResponse',
- paramName: 'request'
- });
- assert_js.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_js.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) {
- {
- logger_js.logger.warn(`Unable to construct a partial response; returning a ` + `416 Range Not Satisfiable response instead.`);
- logger_js.logger.groupCollapsed(`View details here.`);
- logger_js.logger.log(error);
- logger_js.logger.log(request);
- logger_js.logger.log(originalResponse);
- logger_js.logger.groupEnd();
- }
- return new Response('', {
- status: 416,
- statusText: 'Range Not Satisfiable'
- });
- }
- }
-
-
- class RangeRequestsPlugin {
- constructor() {
-
- this.cachedResponseWillBeUsed = async ({
- request,
- cachedResponse
- }) => {
-
-
- if (cachedResponse && request.headers.has('range')) {
- return await createPartialResponse(request, cachedResponse);
- }
-
- return cachedResponse;
- };
- }
- }
- exports.RangeRequestsPlugin = RangeRequestsPlugin;
- exports.createPartialResponse = createPartialResponse;
- return exports;
- }({}, workbox.core._private, workbox.core._private, workbox.core._private));
|