@serwist/range-requests 9.0.0-preview.0 → 9.0.0-preview.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +5 -59
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
import { assert, SerwistError, logger } from '@serwist/core/internal';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
* @param blob A source blob.
|
|
5
|
-
* @param start The offset to use as the start of the
|
|
6
|
-
* slice.
|
|
7
|
-
* @param end The offset to use as the end of the slice.
|
|
8
|
-
* @returns An object with `start` and `end` properties, reflecting
|
|
9
|
-
* the effective boundaries to use given the size of the blob.
|
|
10
|
-
* @private
|
|
11
|
-
*/ function calculateEffectiveBoundaries(blob, start, end) {
|
|
3
|
+
function calculateEffectiveBoundaries(blob, start, end) {
|
|
12
4
|
if (process.env.NODE_ENV !== "production") {
|
|
13
5
|
assert.isInstance(blob, Blob, {
|
|
14
6
|
moduleName: "@serwist/range-requests",
|
|
@@ -28,7 +20,6 @@ import { assert, SerwistError, logger } from '@serwist/core/internal';
|
|
|
28
20
|
let effectiveEnd;
|
|
29
21
|
if (start !== undefined && end !== undefined) {
|
|
30
22
|
effectiveStart = start;
|
|
31
|
-
// Range values are inclusive, so add 1 to the value.
|
|
32
23
|
effectiveEnd = end + 1;
|
|
33
24
|
} else if (start !== undefined && end === undefined) {
|
|
34
25
|
effectiveStart = start;
|
|
@@ -43,13 +34,7 @@ import { assert, SerwistError, logger } from '@serwist/core/internal';
|
|
|
43
34
|
};
|
|
44
35
|
}
|
|
45
36
|
|
|
46
|
-
|
|
47
|
-
* @param rangeHeader A Range: header value.
|
|
48
|
-
* @returns An object with `start` and `end` properties, reflecting
|
|
49
|
-
* the parsed value of the Range: header. If either the `start` or `end` are
|
|
50
|
-
* omitted, then `null` will be returned.
|
|
51
|
-
* @private
|
|
52
|
-
*/ function parseRangeHeader(rangeHeader) {
|
|
37
|
+
function parseRangeHeader(rangeHeader) {
|
|
53
38
|
if (process.env.NODE_ENV !== "production") {
|
|
54
39
|
assert.isType(rangeHeader, "string", {
|
|
55
40
|
moduleName: "@serwist/range-requests",
|
|
@@ -63,16 +48,12 @@ import { assert, SerwistError, logger } from '@serwist/core/internal';
|
|
|
63
48
|
normalizedRangeHeader
|
|
64
49
|
});
|
|
65
50
|
}
|
|
66
|
-
// Specifying multiple ranges separate by commas is valid syntax, but this
|
|
67
|
-
// library only attempts to handle a single, contiguous sequence of bytes.
|
|
68
|
-
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range#Syntax
|
|
69
51
|
if (normalizedRangeHeader.includes(",")) {
|
|
70
52
|
throw new SerwistError("single-range-only", {
|
|
71
53
|
normalizedRangeHeader
|
|
72
54
|
});
|
|
73
55
|
}
|
|
74
56
|
const rangeParts = /(\d*)-(\d*)/.exec(normalizedRangeHeader);
|
|
75
|
-
// We need either at least one of the start or end values.
|
|
76
57
|
if (!rangeParts || !(rangeParts[1] || rangeParts[2])) {
|
|
77
58
|
throw new SerwistError("invalid-range-values", {
|
|
78
59
|
normalizedRangeHeader
|
|
@@ -84,22 +65,7 @@ import { assert, SerwistError, logger } from '@serwist/core/internal';
|
|
|
84
65
|
};
|
|
85
66
|
}
|
|
86
67
|
|
|
87
|
-
|
|
88
|
-
* Given a `Request` and `Response` objects as input, this will return a
|
|
89
|
-
* promise for a new `Response`.
|
|
90
|
-
*
|
|
91
|
-
* If the original `Response` already contains partial content (i.e. it has
|
|
92
|
-
* a status of 206), then this assumes it already fulfills the `Range:`
|
|
93
|
-
* requirements, and will return it as-is.
|
|
94
|
-
*
|
|
95
|
-
* @param request A request, which should contain a Range:
|
|
96
|
-
* header.
|
|
97
|
-
* @param originalResponse A response.
|
|
98
|
-
* @returns Either a `206 Partial Content` response, with
|
|
99
|
-
* the response body set to the slice of content specified by the request's
|
|
100
|
-
* `Range:` header, or a `416 Range Not Satisfiable` response if the
|
|
101
|
-
* conditions of the `Range:` header can't be met.
|
|
102
|
-
*/ async function createPartialResponse(request, originalResponse) {
|
|
68
|
+
async function createPartialResponse(request, originalResponse) {
|
|
103
69
|
try {
|
|
104
70
|
if (process.env.NODE_ENV !== "production") {
|
|
105
71
|
assert.isInstance(request, Request, {
|
|
@@ -114,8 +80,6 @@ import { assert, SerwistError, logger } from '@serwist/core/internal';
|
|
|
114
80
|
});
|
|
115
81
|
}
|
|
116
82
|
if (originalResponse.status === 206) {
|
|
117
|
-
// If we already have a 206, then just pass it through as-is;
|
|
118
|
-
// see https://github.com/GoogleChrome/workbox/issues/1720
|
|
119
83
|
return originalResponse;
|
|
120
84
|
}
|
|
121
85
|
const rangeHeader = request.headers.get("range");
|
|
@@ -128,8 +92,6 @@ import { assert, SerwistError, logger } from '@serwist/core/internal';
|
|
|
128
92
|
const slicedBlob = originalBlob.slice(effectiveBoundaries.start, effectiveBoundaries.end);
|
|
129
93
|
const slicedBlobSize = slicedBlob.size;
|
|
130
94
|
const slicedResponse = new Response(slicedBlob, {
|
|
131
|
-
// Status code 206 is for a Partial Content response.
|
|
132
|
-
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206
|
|
133
95
|
status: 206,
|
|
134
96
|
statusText: "Partial Content",
|
|
135
97
|
headers: originalResponse.headers
|
|
@@ -153,27 +115,11 @@ import { assert, SerwistError, logger } from '@serwist/core/internal';
|
|
|
153
115
|
}
|
|
154
116
|
}
|
|
155
117
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
* be fulfilled by a cached response.
|
|
159
|
-
*
|
|
160
|
-
* It does this by intercepting the `cachedResponseWillBeUsed` plugin callback
|
|
161
|
-
* and returning the appropriate subset of the cached response body.
|
|
162
|
-
*/ class RangeRequestsPlugin {
|
|
163
|
-
/**
|
|
164
|
-
* @param options
|
|
165
|
-
* @returns If request contains a 'Range' header, then a
|
|
166
|
-
* new response with status 206 whose body is a subset of `cachedResponse` is
|
|
167
|
-
* returned. Otherwise, `cachedResponse` is returned as-is.
|
|
168
|
-
* @private
|
|
169
|
-
*/ cachedResponseWillBeUsed = async ({ request, cachedResponse })=>{
|
|
170
|
-
// Only return a sliced response if there's something valid in the cache,
|
|
171
|
-
// and there's a Range: header in the request.
|
|
118
|
+
class RangeRequestsPlugin {
|
|
119
|
+
cachedResponseWillBeUsed = async ({ request, cachedResponse })=>{
|
|
172
120
|
if (cachedResponse && request.headers.has("range")) {
|
|
173
121
|
return await createPartialResponse(request, cachedResponse);
|
|
174
122
|
}
|
|
175
|
-
// If there was no Range: header, or if cachedResponse wasn't valid, just
|
|
176
|
-
// pass it through as-is.
|
|
177
123
|
return cachedResponse;
|
|
178
124
|
};
|
|
179
125
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/range-requests",
|
|
3
|
-
"version": "9.0.0-preview.
|
|
3
|
+
"version": "9.0.0-preview.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "This library creates a new Response, given a source Response and a Range header value.",
|
|
6
6
|
"files": [
|
|
@@ -33,12 +33,12 @@
|
|
|
33
33
|
"./package.json": "./package.json"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@serwist/core": "9.0.0-preview.
|
|
36
|
+
"@serwist/core": "9.0.0-preview.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"rollup": "4.9.6",
|
|
40
40
|
"typescript": "5.4.0-dev.20240203",
|
|
41
|
-
"@serwist/constants": "9.0.0-preview.
|
|
41
|
+
"@serwist/constants": "9.0.0-preview.1"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"typescript": ">=5.0.0"
|