@serwist/range-requests
Advanced tools
Comparing version 9.0.0-preview.0 to 9.0.0-preview.1
import { assert, SerwistError, logger } from '@serwist/core/internal'; | ||
/** | ||
* @param blob A source blob. | ||
* @param start The offset to use as the start of the | ||
* slice. | ||
* @param end The offset to use as the end of the slice. | ||
* @returns 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) { | ||
function calculateEffectiveBoundaries(blob, start, end) { | ||
if (process.env.NODE_ENV !== "production") { | ||
@@ -31,3 +23,2 @@ assert.isInstance(blob, Blob, { | ||
effectiveStart = start; | ||
// Range values are inclusive, so add 1 to the value. | ||
effectiveEnd = end + 1; | ||
@@ -47,9 +38,3 @@ } else if (start !== undefined && end === undefined) { | ||
/** | ||
* @param rangeHeader A Range: header value. | ||
* @returns 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) { | ||
function parseRangeHeader(rangeHeader) { | ||
if (process.env.NODE_ENV !== "production") { | ||
@@ -68,5 +53,2 @@ assert.isType(rangeHeader, "string", { | ||
} | ||
// 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(",")) { | ||
@@ -78,3 +60,2 @@ throw new SerwistError("single-range-only", { | ||
const rangeParts = /(\d*)-(\d*)/.exec(normalizedRangeHeader); | ||
// We need either at least one of the start or end values. | ||
if (!rangeParts || !(rangeParts[1] || rangeParts[2])) { | ||
@@ -91,18 +72,3 @@ throw new SerwistError("invalid-range-values", { | ||
/** | ||
* 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 A request, which should contain a Range: | ||
* header. | ||
* @param originalResponse A response. | ||
* @returns 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. | ||
*/ async function createPartialResponse(request, originalResponse) { | ||
async function createPartialResponse(request, originalResponse) { | ||
try { | ||
@@ -122,4 +88,2 @@ if (process.env.NODE_ENV !== "production") { | ||
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; | ||
@@ -137,4 +101,2 @@ } | ||
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, | ||
@@ -163,23 +125,7 @@ statusText: "Partial Content", | ||
/** | ||
* 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. | ||
*/ class RangeRequestsPlugin { | ||
/** | ||
* @param options | ||
* @returns 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 | ||
*/ 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. | ||
class RangeRequestsPlugin { | ||
cachedResponseWillBeUsed = async ({ request, cachedResponse })=>{ | ||
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; | ||
@@ -186,0 +132,0 @@ }; |
{ | ||
"name": "@serwist/range-requests", | ||
"version": "9.0.0-preview.0", | ||
"version": "9.0.0-preview.1", | ||
"type": "module", | ||
@@ -36,3 +36,3 @@ "description": "This library creates a new Response, given a source Response and a Range header value.", | ||
"dependencies": { | ||
"@serwist/core": "9.0.0-preview.0" | ||
"@serwist/core": "9.0.0-preview.1" | ||
}, | ||
@@ -42,3 +42,3 @@ "devDependencies": { | ||
"typescript": "5.4.0-dev.20240203", | ||
"@serwist/constants": "9.0.0-preview.0" | ||
"@serwist/constants": "9.0.0-preview.1" | ||
}, | ||
@@ -45,0 +45,0 @@ "peerDependencies": { |
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
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
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
0
20306
408
+ Added@serwist/core@9.0.0-preview.1(transitive)
- Removed@serwist/core@9.0.0-preview.0(transitive)