UNPKG

11.2 kBSource Map (JSON)View Raw
1{"version":3,"names":[],"mappings":"","sources":["packages/workbox-range-requests/browser.mjs"],"sourcesContent":["this.workbox = this.workbox || {};\nthis.workbox.rangeRequests = (function (exports,WorkboxError_mjs,assert_mjs,logger_mjs) {\n 'use strict';\n\n try {\n self.workbox.v['workbox:range-requests:3.6.2'] = 1;\n } catch (e) {} // eslint-disable-line\n\n /*\n Copyright 2017 Google Inc.\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n */\n\n /**\n * @param {Blob} blob A source blob.\n * @param {number|null} start The offset to use as the start of the\n * slice.\n * @param {number|null} end The offset to use as the end of the slice.\n * @return {Object} An object with `start` and `end` properties, reflecting\n * the effective boundaries to use given the size of the blob.\n *\n * @private\n */\n function calculateEffectiveBoundaries(blob, start, end) {\n {\n assert_mjs.assert.isInstance(blob, Blob, {\n moduleName: 'workbox-range-requests',\n funcName: 'calculateEffectiveBoundaries',\n paramName: 'blob'\n });\n }\n\n const blobSize = blob.size;\n\n if (end > blobSize || start < 0) {\n throw new WorkboxError_mjs.WorkboxError('range-not-satisfiable', {\n size: blobSize,\n end,\n start\n });\n }\n\n let effectiveStart;\n let effectiveEnd;\n\n if (start === null) {\n effectiveStart = blobSize - end;\n effectiveEnd = blobSize;\n } else if (end === null) {\n effectiveStart = start;\n effectiveEnd = blobSize;\n } else {\n effectiveStart = start;\n // Range values are inclusive, so add 1 to the value.\n effectiveEnd = end + 1;\n }\n\n return {\n start: effectiveStart,\n end: effectiveEnd\n };\n }\n\n /*\n Copyright 2017 Google Inc.\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n */\n\n /**\n * @param {string} rangeHeader A Range: header value.\n * @return {Object} An object with `start` and `end` properties, reflecting\n * the parsed value of the Range: header. If either the `start` or `end` are\n * omitted, then `null` will be returned.\n *\n * @private\n */\n function parseRangeHeader(rangeHeader) {\n {\n assert_mjs.assert.isType(rangeHeader, 'string', {\n moduleName: 'workbox-range-requests',\n funcName: 'parseRangeHeader',\n paramName: 'rangeHeader'\n });\n }\n\n const normalizedRangeHeader = rangeHeader.trim().toLowerCase();\n if (!normalizedRangeHeader.startsWith('bytes=')) {\n throw new WorkboxError_mjs.WorkboxError('unit-must-be-bytes', { normalizedRangeHeader });\n }\n\n // Specifying multiple ranges separate by commas is valid syntax, but this\n // library only attempts to handle a single, contiguous sequence of bytes.\n // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range#Syntax\n if (normalizedRangeHeader.includes(',')) {\n throw new WorkboxError_mjs.WorkboxError('single-range-only', { normalizedRangeHeader });\n }\n\n const rangeParts = /(\\d*)-(\\d*)/.exec(normalizedRangeHeader);\n // We need either at least one of the start or end values.\n if (rangeParts === null || !(rangeParts[1] || rangeParts[2])) {\n throw new WorkboxError_mjs.WorkboxError('invalid-range-values', { normalizedRangeHeader });\n }\n\n return {\n start: rangeParts[1] === '' ? null : Number(rangeParts[1]),\n end: rangeParts[2] === '' ? null : Number(rangeParts[2])\n };\n }\n\n /**\n * Given a `Request` and `Response` objects as input, this will return a\n * promise for a new `Response`.\n *\n * @param {Request} request A request, which should contain a Range:\n * header.\n * @param {Response} originalResponse An original response containing the full\n * content.\n * @return {Promise<Response>} Either a `206 Partial Content` response, with\n * the response body set to the slice of content specified by the request's\n * `Range:` header, or a `416 Range Not Satisfiable` response if the\n * conditions of the `Range:` header can't be met.\n *\n * @memberof workbox.rangeRequests\n */\n let createPartialResponse = (() => {\n var _ref = babelHelpers.asyncToGenerator(function* (request, originalResponse) {\n try {\n {\n assert_mjs.assert.isInstance(request, Request, {\n moduleName: 'workbox-range-requests',\n funcName: 'createPartialResponse',\n paramName: 'request'\n });\n\n assert_mjs.assert.isInstance(originalResponse, Response, {\n moduleName: 'workbox-range-requests',\n funcName: 'createPartialResponse',\n paramName: 'originalResponse'\n });\n }\n\n const rangeHeader = request.headers.get('range');\n if (!rangeHeader) {\n throw new WorkboxError_mjs.WorkboxError('no-range-header');\n }\n\n const boundaries = parseRangeHeader(rangeHeader);\n const originalBlob = yield originalResponse.blob();\n\n const effectiveBoundaries = calculateEffectiveBoundaries(originalBlob, boundaries.start, boundaries.end);\n\n const slicedBlob = originalBlob.slice(effectiveBoundaries.start, effectiveBoundaries.end);\n const slicedBlobSize = slicedBlob.size;\n\n const slicedResponse = new Response(slicedBlob, {\n // Status code 206 is for a Partial Content response.\n // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206\n status: 206,\n statusText: 'Partial Content',\n headers: originalResponse.headers\n });\n\n slicedResponse.headers.set('Content-Length', slicedBlobSize);\n slicedResponse.headers.set('Content-Range', `bytes ${effectiveBoundaries.start}-${effectiveBoundaries.end - 1}/` + originalBlob.size);\n\n return slicedResponse;\n } catch (error) {\n {\n logger_mjs.logger.warn(`Unable to construct a partial response; returning a ` + `416 Range Not Satisfiable response instead.`);\n logger_mjs.logger.groupCollapsed(`View details here.`);\n logger_mjs.logger.unprefixed.log(error);\n logger_mjs.logger.unprefixed.log(request);\n logger_mjs.logger.unprefixed.log(originalResponse);\n logger_mjs.logger.groupEnd();\n }\n\n return new Response('', {\n status: 416,\n statusText: 'Range Not Satisfiable'\n });\n }\n });\n\n return function createPartialResponse(_x, _x2) {\n return _ref.apply(this, arguments);\n };\n })();\n\n /*\n Copyright 2016 Google Inc. All Rights Reserved.\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n http://www.apache.org/licenses/LICENSE-2.0\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n */\n\n /**\n * The range request plugin makes it easy for a request with a 'Range' header to\n * be fulfilled by a cached response.\n *\n * It does this by intercepting the `cachedResponseWillBeUsed` plugin callback\n * and returning the appropriate subset of the cached response body.\n *\n * @memberof workbox.rangeRequests\n */\n class Plugin {\n /**\n * @param {Object} options\n * @param {Request} options.request The original request, which may or may not\n * contain a Range: header.\n * @param {Response} options.cachedResponse The complete cached response.\n * @return {Promise<Response>} If request contains a 'Range' header, then a\n * new response with status 206 whose body is a subset of `cachedResponse` is\n * returned. Otherwise, `cachedResponse` is returned as-is.\n *\n * @private\n */\n cachedResponseWillBeUsed({ request, cachedResponse }) {\n return babelHelpers.asyncToGenerator(function* () {\n // Only return a sliced response if there's something valid in the cache,\n // and there's a Range: header in the request.\n if (cachedResponse && request.headers.has('range')) {\n return yield createPartialResponse(request, cachedResponse);\n }\n\n // If there was no Range: header, or if cachedResponse wasn't valid, just\n // pass it through as-is.\n return cachedResponse;\n })();\n }\n }\n\n /*\n Copyright 2017 Google Inc.\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n */\n\n /*\n Copyright 2017 Google Inc.\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n */\n\n exports.createPartialResponse = createPartialResponse;\n exports.Plugin = Plugin;\n\n return exports;\n\n}({},workbox.core._private,workbox.core._private,workbox.core._private));\n"],"file":"workbox-range-requests.dev.js"}
\No newline at end of file