workbox-range-requests
Advanced tools
Comparing version 5.0.0-alpha.0 to 5.0.0-alpha.1
this.workbox = this.workbox || {}; | ||
this.workbox.rangeRequests = (function (exports, WorkboxError_mjs, assert_mjs, logger_mjs) { | ||
'use strict'; | ||
this.workbox.rangeRequests = (function (exports, WorkboxError_js, assert_js, logger_js) { | ||
'use strict'; | ||
try { | ||
self['workbox:range-requests:5.0.0-alpha.0'] && _(); | ||
} catch (e) {} // eslint-disable-line | ||
// @ts-ignore | ||
try { | ||
self['workbox:range-requests:5.0.0-alpha.1'] && _(); | ||
} catch (e) {} | ||
/* | ||
Copyright 2018 Google LLC | ||
/* | ||
Copyright 2018 Google LLC | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
/** | ||
* @param {Blob} blob A source blob. | ||
* @param {number|null} start The offset to use as the start of the | ||
* slice. | ||
* @param {number|null} end The offset to use as the end of the slice. | ||
* @return {Object} An object with `start` and `end` properties, reflecting | ||
* the effective boundaries to use given the size of the blob. | ||
* | ||
* @private | ||
*/ | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
/** | ||
* @param {Blob} blob A source blob. | ||
* @param {number} [start] The offset to use as the start of the | ||
* slice. | ||
* @param {number} [end] The offset to use as the end of the slice. | ||
* @return {Object} An object with `start` and `end` properties, reflecting | ||
* the effective boundaries to use given the size of the blob. | ||
* | ||
* @private | ||
*/ | ||
function calculateEffectiveBoundaries(blob, start, end) { | ||
{ | ||
assert_mjs.assert.isInstance(blob, Blob, { | ||
moduleName: 'workbox-range-requests', | ||
funcName: 'calculateEffectiveBoundaries', | ||
paramName: 'blob' | ||
}); | ||
} | ||
function calculateEffectiveBoundaries(blob, start, end) { | ||
{ | ||
assert_js.assert.isInstance(blob, Blob, { | ||
moduleName: 'workbox-range-requests', | ||
funcName: 'calculateEffectiveBoundaries', | ||
paramName: 'blob' | ||
}); | ||
} | ||
const blobSize = blob.size; | ||
const blobSize = blob.size; | ||
if (end > blobSize || start < 0) { | ||
throw new WorkboxError_mjs.WorkboxError('range-not-satisfiable', { | ||
size: blobSize, | ||
end, | ||
start | ||
}); | ||
} | ||
if (end && end > blobSize || start && start < 0) { | ||
throw new WorkboxError_js.WorkboxError('range-not-satisfiable', { | ||
size: blobSize, | ||
end, | ||
start | ||
}); | ||
} | ||
let effectiveStart; | ||
let effectiveEnd; | ||
let effectiveStart; | ||
let effectiveEnd; | ||
if (start === null) { | ||
effectiveStart = blobSize - end; | ||
effectiveEnd = blobSize; | ||
} else if (end === null) { | ||
effectiveStart = start; | ||
effectiveEnd = blobSize; | ||
} else { | ||
effectiveStart = start; // Range values are inclusive, so add 1 to the value. | ||
if (start && end) { | ||
effectiveStart = start; // Range values are inclusive, so add 1 to the value. | ||
effectiveEnd = end + 1; | ||
effectiveEnd = end + 1; | ||
} else if (start && !end) { | ||
effectiveStart = start; | ||
effectiveEnd = blobSize; | ||
} else if (end && !start) { | ||
effectiveStart = blobSize - end; | ||
effectiveEnd = blobSize; | ||
} | ||
return { | ||
start: effectiveStart, | ||
end: effectiveEnd | ||
}; | ||
} | ||
return { | ||
start: effectiveStart, | ||
end: effectiveEnd | ||
}; | ||
} | ||
/* | ||
Copyright 2018 Google LLC | ||
/* | ||
Copyright 2018 Google LLC | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
/** | ||
* @param {string} rangeHeader A Range: header value. | ||
* @return {Object} An object with `start` and `end` properties, reflecting | ||
* the parsed value of the Range: header. If either the `start` or `end` are | ||
* omitted, then `null` will be returned. | ||
* | ||
* @private | ||
*/ | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
/** | ||
* @param {string} rangeHeader A Range: header value. | ||
* @return {Object} An object with `start` and `end` properties, reflecting | ||
* the parsed value of the Range: header. If either the `start` or `end` are | ||
* omitted, then `null` will be returned. | ||
* | ||
* @private | ||
*/ | ||
function parseRangeHeader(rangeHeader) { | ||
{ | ||
assert_js.assert.isType(rangeHeader, 'string', { | ||
moduleName: 'workbox-range-requests', | ||
funcName: 'parseRangeHeader', | ||
paramName: 'rangeHeader' | ||
}); | ||
} | ||
function parseRangeHeader(rangeHeader) { | ||
{ | ||
assert_mjs.assert.isType(rangeHeader, 'string', { | ||
moduleName: 'workbox-range-requests', | ||
funcName: 'parseRangeHeader', | ||
paramName: 'rangeHeader' | ||
}); | ||
} | ||
const normalizedRangeHeader = rangeHeader.trim().toLowerCase(); | ||
const normalizedRangeHeader = rangeHeader.trim().toLowerCase(); | ||
if (!normalizedRangeHeader.startsWith('bytes=')) { | ||
throw new WorkboxError_js.WorkboxError('unit-must-be-bytes', { | ||
normalizedRangeHeader | ||
}); | ||
} // Specifying multiple ranges separate by commas is valid syntax, but this | ||
// library only attempts to handle a single, contiguous sequence of bytes. | ||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range#Syntax | ||
if (!normalizedRangeHeader.startsWith('bytes=')) { | ||
throw new WorkboxError_mjs.WorkboxError('unit-must-be-bytes', { | ||
normalizedRangeHeader | ||
}); | ||
} // Specifying multiple ranges separate by commas is valid syntax, but this | ||
// library only attempts to handle a single, contiguous sequence of bytes. | ||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range#Syntax | ||
if (normalizedRangeHeader.includes(',')) { | ||
throw new WorkboxError_js.WorkboxError('single-range-only', { | ||
normalizedRangeHeader | ||
}); | ||
} | ||
if (normalizedRangeHeader.includes(',')) { | ||
throw new WorkboxError_mjs.WorkboxError('single-range-only', { | ||
normalizedRangeHeader | ||
}); | ||
} | ||
const rangeParts = /(\d*)-(\d*)/.exec(normalizedRangeHeader); // We need either at least one of the start or end values. | ||
const rangeParts = /(\d*)-(\d*)/.exec(normalizedRangeHeader); // We need either at least one of the start or end values. | ||
if (!rangeParts || !(rangeParts[1] || rangeParts[2])) { | ||
throw new WorkboxError_js.WorkboxError('invalid-range-values', { | ||
normalizedRangeHeader | ||
}); | ||
} | ||
if (rangeParts === null || !(rangeParts[1] || rangeParts[2])) { | ||
throw new WorkboxError_mjs.WorkboxError('invalid-range-values', { | ||
normalizedRangeHeader | ||
}); | ||
return { | ||
start: rangeParts[1] === '' ? undefined : Number(rangeParts[1]), | ||
end: rangeParts[2] === '' ? undefined : Number(rangeParts[2]) | ||
}; | ||
} | ||
return { | ||
start: rangeParts[1] === '' ? null : Number(rangeParts[1]), | ||
end: rangeParts[2] === '' ? null : Number(rangeParts[2]) | ||
}; | ||
} | ||
/* | ||
Copyright 2018 Google LLC | ||
/* | ||
Copyright 2018 Google LLC | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
/** | ||
* Given a `Request` and `Response` objects as input, this will return a | ||
* promise for a new `Response`. | ||
* | ||
* If the original `Response` already contains partial content (i.e. it has | ||
* a status of 206), then this assumes it already fulfills the `Range:` | ||
* requirements, and will return it as-is. | ||
* | ||
* @param {Request} request A request, which should contain a Range: | ||
* header. | ||
* @param {Response} originalResponse A response. | ||
* @return {Promise<Response>} Either a `206 Partial Content` response, with | ||
* the response body set to the slice of content specified by the request's | ||
* `Range:` header, or a `416 Range Not Satisfiable` response if the | ||
* conditions of the `Range:` header can't be met. | ||
* | ||
* @memberof workbox.rangeRequests | ||
*/ | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
/** | ||
* Given a `Request` and `Response` objects as input, this will return a | ||
* promise for a new `Response`. | ||
* | ||
* If the original `Response` already contains partial content (i.e. it has | ||
* a status of 206), then this assumes it already fulfills the `Range:` | ||
* requirements, and will return it as-is. | ||
* | ||
* @param {Request} request A request, which should contain a Range: | ||
* header. | ||
* @param {Response} originalResponse A response. | ||
* @return {Promise<Response>} Either a `206 Partial Content` response, with | ||
* the response body set to the slice of content specified by the request's | ||
* `Range:` header, or a `416 Range Not Satisfiable` response if the | ||
* conditions of the `Range:` header can't be met. | ||
* | ||
* @memberof workbox.rangeRequests | ||
*/ | ||
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' | ||
}); | ||
} | ||
async function createPartialResponse(request, originalResponse) { | ||
try { | ||
{ | ||
assert_mjs.assert.isInstance(request, Request, { | ||
moduleName: 'workbox-range-requests', | ||
funcName: 'createPartialResponse', | ||
paramName: 'request' | ||
}); | ||
assert_mjs.assert.isInstance(originalResponse, Response, { | ||
moduleName: 'workbox-range-requests', | ||
funcName: 'createPartialResponse', | ||
paramName: 'originalResponse' | ||
}); | ||
} | ||
if (originalResponse.status === 206) { | ||
// If we already have a 206, then just pass it through as-is; | ||
// see https://github.com/GoogleChrome/workbox/issues/1720 | ||
return originalResponse; | ||
} | ||
if (originalResponse.status === 206) { | ||
// If we already have a 206, then just pass it through as-is; | ||
// see https://github.com/GoogleChrome/workbox/issues/1720 | ||
return originalResponse; | ||
} | ||
const rangeHeader = request.headers.get('range'); | ||
const rangeHeader = request.headers.get('range'); | ||
if (!rangeHeader) { | ||
throw new WorkboxError_js.WorkboxError('no-range-header'); | ||
} | ||
if (!rangeHeader) { | ||
throw new WorkboxError_mjs.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 code 206 is for a Partial Content response. | ||
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206 | ||
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(); | ||
} | ||
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 code 206 is for a Partial Content response. | ||
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206 | ||
status: 206, | ||
statusText: 'Partial Content', | ||
headers: originalResponse.headers | ||
}); | ||
slicedResponse.headers.set('Content-Length', slicedBlobSize); | ||
slicedResponse.headers.set('Content-Range', `bytes ${effectiveBoundaries.start}-${effectiveBoundaries.end - 1}/` + originalBlob.size); | ||
return slicedResponse; | ||
} catch (error) { | ||
{ | ||
logger_mjs.logger.warn(`Unable to construct a partial response; returning a ` + `416 Range Not Satisfiable response instead.`); | ||
logger_mjs.logger.groupCollapsed(`View details here.`); | ||
logger_mjs.logger.log(error); | ||
logger_mjs.logger.log(request); | ||
logger_mjs.logger.log(originalResponse); | ||
logger_mjs.logger.groupEnd(); | ||
return new Response('', { | ||
status: 416, | ||
statusText: 'Range Not Satisfiable' | ||
}); | ||
} | ||
return new Response('', { | ||
status: 416, | ||
statusText: 'Range Not Satisfiable' | ||
}); | ||
} | ||
} | ||
/* | ||
Copyright 2018 Google LLC | ||
/* | ||
Copyright 2018 Google LLC | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
/** | ||
* The range request plugin makes it easy for a request with a 'Range' header to | ||
* be fulfilled by a cached response. | ||
* | ||
* It does this by intercepting the `cachedResponseWillBeUsed` plugin callback | ||
* and returning the appropriate subset of the cached response body. | ||
* | ||
* @memberof workbox.rangeRequests | ||
*/ | ||
class Plugin { | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
/** | ||
* @param {Object} options | ||
* @param {Request} options.request The original request, which may or may not | ||
* contain a Range: header. | ||
* @param {Response} options.cachedResponse The complete cached response. | ||
* @return {Promise<Response>} If request contains a 'Range' header, then a | ||
* new response with status 206 whose body is a subset of `cachedResponse` is | ||
* returned. Otherwise, `cachedResponse` is returned as-is. | ||
* The range request plugin makes it easy for a request with a 'Range' header to | ||
* be fulfilled by a cached response. | ||
* | ||
* @private | ||
* It does this by intercepting the `cachedResponseWillBeUsed` plugin callback | ||
* and returning the appropriate subset of the cached response body. | ||
* | ||
* @memberof workbox.rangeRequests | ||
*/ | ||
async cachedResponseWillBeUsed({ | ||
request, | ||
cachedResponse | ||
}) { | ||
// Only return a sliced response if there's something valid in the cache, | ||
// and there's a Range: header in the request. | ||
if (cachedResponse && request.headers.has('range')) { | ||
return await createPartialResponse(request, cachedResponse); | ||
} // If there was no Range: header, or if cachedResponse wasn't valid, just | ||
// pass it through as-is. | ||
class Plugin { | ||
constructor() { | ||
/** | ||
* @param {Object} options | ||
* @param {Request} options.request The original request, which may or may not | ||
* contain a Range: header. | ||
* @param {Response} options.cachedResponse The complete cached response. | ||
* @return {Promise<Response>} If request contains a 'Range' header, then a | ||
* new response with status 206 whose body is a subset of `cachedResponse` is | ||
* returned. Otherwise, `cachedResponse` is returned as-is. | ||
* | ||
* @private | ||
*/ | ||
this.cachedResponseWillBeUsed = async ({ | ||
request, | ||
cachedResponse | ||
}) => { | ||
// Only return a sliced response if there's something valid in the cache, | ||
// and there's a Range: header in the request. | ||
if (cachedResponse && request.headers.has('range')) { | ||
return await createPartialResponse(request, cachedResponse); | ||
} // If there was no Range: header, or if cachedResponse wasn't valid, just | ||
// pass it through as-is. | ||
return cachedResponse; | ||
return cachedResponse; | ||
}; | ||
} | ||
} | ||
} | ||
/* | ||
Copyright 2018 Google LLC | ||
/* | ||
Copyright 2018 Google LLC | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
exports.Plugin = Plugin; | ||
exports.createPartialResponse = createPartialResponse; | ||
exports.createPartialResponse = createPartialResponse; | ||
exports.Plugin = Plugin; | ||
return exports; | ||
return exports; | ||
}({}, workbox.core._private, workbox.core._private, workbox.core._private)); | ||
//# sourceMappingURL=workbox-range-requests.dev.js.map |
@@ -1,2 +0,2 @@ | ||
this.workbox=this.workbox||{},this.workbox.rangeRequests=function(e,n){"use strict";try{self["workbox:range-requests:5.0.0-alpha.0"]&&_()}catch(e){}async function t(e,t){try{if(206===t.status)return t;const s=e.headers.get("range");if(!s)throw new n.WorkboxError("no-range-header");const a=function(e){const t=e.trim().toLowerCase();if(!t.startsWith("bytes="))throw new n.WorkboxError("unit-must-be-bytes",{normalizedRangeHeader:t});if(t.includes(","))throw new n.WorkboxError("single-range-only",{normalizedRangeHeader:t});const s=/(\d*)-(\d*)/.exec(t);if(null===s||!s[1]&&!s[2])throw new n.WorkboxError("invalid-range-values",{normalizedRangeHeader:t});return{start:""===s[1]?null:Number(s[1]),end:""===s[2]?null:Number(s[2])}}(s),r=await t.blob(),i=function(e,t,s){const a=e.size;if(s>a||t<0)throw new n.WorkboxError("range-not-satisfiable",{size:a,end:s,start:t});let r,i;return null===t?(r=a-s,i=a):null===s?(r=t,i=a):(r=t,i=s+1),{start:r,end:i}}(r,a.start,a.end),o=r.slice(i.start,i.end),u=o.size,l=new Response(o,{status:206,statusText:"Partial Content",headers:t.headers});return l.headers.set("Content-Length",u),l.headers.set("Content-Range",`bytes ${i.start}-${i.end-1}/`+r.size),l}catch(e){return new Response("",{status:416,statusText:"Range Not Satisfiable"})}}return e.createPartialResponse=t,e.Plugin=class{async cachedResponseWillBeUsed({request:e,cachedResponse:n}){return n&&e.headers.has("range")?await t(e,n):n}},e}({},workbox.core._private); | ||
this.workbox=this.workbox||{},this.workbox.rangeRequests=function(t,e,n){"use strict";try{self["workbox:range-requests:5.0.0-alpha.1"]&&_()}catch(t){}async function r(t,n){try{if(206===n.status)return n;const r=t.headers.get("range");if(!r)throw new e.WorkboxError("no-range-header");const s=function(t){const n=t.trim().toLowerCase();if(!n.startsWith("bytes="))throw new e.WorkboxError("unit-must-be-bytes",{normalizedRangeHeader:n});if(n.includes(","))throw new e.WorkboxError("single-range-only",{normalizedRangeHeader:n});const r=/(\d*)-(\d*)/.exec(n);if(!r||!r[1]&&!r[2])throw new e.WorkboxError("invalid-range-values",{normalizedRangeHeader:n});return{start:""===r[1]?void 0:Number(r[1]),end:""===r[2]?void 0:Number(r[2])}}(r),a=await n.blob(),o=function(t,n,r){const s=t.size;if(r&&r>s||n&&n<0)throw new e.WorkboxError("range-not-satisfiable",{size:s,end:r,start:n});let a,o;return n&&r?(a=n,o=r+1):n&&!r?(a=n,o=s):r&&!n&&(a=s-r,o=s),{start:a,end:o}}(a,s.start,s.end),i=a.slice(o.start,o.end),u=i.size,c=new Response(i,{status:206,statusText:"Partial Content",headers:n.headers});return c.headers.set("Content-Length",String(u)),c.headers.set("Content-Range",`bytes ${o.start}-${o.end-1}/`+a.size),c}catch(t){return new Response("",{status:416,statusText:"Range Not Satisfiable"})}}return t.Plugin=class{constructor(){this.cachedResponseWillBeUsed=(async({request:t,cachedResponse:e})=>e&&t.headers.has("range")?await r(t,e):e)}},t.createPartialResponse=r,t}({},workbox.core._private,workbox.core._private); | ||
//# sourceMappingURL=workbox-range-requests.prod.js.map |
{ | ||
"name": "workbox-range-requests", | ||
"version": "5.0.0-alpha.0", | ||
"version": "5.0.0-alpha.1", | ||
"license": "MIT", | ||
@@ -30,8 +30,9 @@ "author": "Google's Web DevRel Team", | ||
}, | ||
"main": "build/workbox-range-requests.prod.js", | ||
"main": "index.js", | ||
"module": "index.mjs", | ||
"types": "index.d.ts", | ||
"dependencies": { | ||
"workbox-core": "^5.0.0-alpha.0" | ||
"workbox-core": "^5.0.0-alpha.1" | ||
}, | ||
"gitHead": "7f231c04023669bc42d5a939d1359b0867e2efda" | ||
"gitHead": "20d2110ddace710a46af06addd4977cae08f5942" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
87822
33
838
1
Updatedworkbox-core@^5.0.0-alpha.1