@serwist/range-requests 8.0.0
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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/RangeRequestsPlugin.d.ts +20 -0
- package/dist/RangeRequestsPlugin.d.ts.map +1 -0
- package/dist/createPartialResponse.d.ts +19 -0
- package/dist/createPartialResponse.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +181 -0
- package/dist/index.old.cjs +184 -0
- package/dist/utils/calculateEffectiveBoundaries.d.ts +15 -0
- package/dist/utils/calculateEffectiveBoundaries.d.ts.map +1 -0
- package/dist/utils/parseRangeHeader.d.ts +13 -0
- package/dist/utils/parseRangeHeader.d.ts.map +1 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Google LLC, 2019 ShadowWalker w@weiw.io https://weiw.io, 2023 Serwist
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This module's documentation can be found at https://developers.google.com/web/tools/workbox/modules/workbox-range-requests
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { SerwistPlugin } from "@serwist/core";
|
|
2
|
+
/**
|
|
3
|
+
* The range request plugin makes it easy for a request with a 'Range' header to
|
|
4
|
+
* be fulfilled by a cached response.
|
|
5
|
+
*
|
|
6
|
+
* It does this by intercepting the `cachedResponseWillBeUsed` plugin callback
|
|
7
|
+
* and returning the appropriate subset of the cached response body.
|
|
8
|
+
*/
|
|
9
|
+
declare class RangeRequestsPlugin implements SerwistPlugin {
|
|
10
|
+
/**
|
|
11
|
+
* @param options
|
|
12
|
+
* @returns If request contains a 'Range' header, then a
|
|
13
|
+
* new response with status 206 whose body is a subset of `cachedResponse` is
|
|
14
|
+
* returned. Otherwise, `cachedResponse` is returned as-is.
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
cachedResponseWillBeUsed: SerwistPlugin["cachedResponseWillBeUsed"];
|
|
18
|
+
}
|
|
19
|
+
export { RangeRequestsPlugin };
|
|
20
|
+
//# sourceMappingURL=RangeRequestsPlugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RangeRequestsPlugin.d.ts","sourceRoot":"","sources":["../src/RangeRequestsPlugin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAInD;;;;;;GAMG;AACH,cAAM,mBAAoB,YAAW,aAAa;IAChD;;;;;;OAMG;IACH,wBAAwB,EAAE,aAAa,CAAC,0BAA0B,CAAC,CAUjE;CACH;AAED,OAAO,EAAE,mBAAmB,EAAE,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Given a `Request` and `Response` objects as input, this will return a
|
|
3
|
+
* promise for a new `Response`.
|
|
4
|
+
*
|
|
5
|
+
* If the original `Response` already contains partial content (i.e. it has
|
|
6
|
+
* a status of 206), then this assumes it already fulfills the `Range:`
|
|
7
|
+
* requirements, and will return it as-is.
|
|
8
|
+
*
|
|
9
|
+
* @param request A request, which should contain a Range:
|
|
10
|
+
* header.
|
|
11
|
+
* @param originalResponse A response.
|
|
12
|
+
* @returns Either a `206 Partial Content` response, with
|
|
13
|
+
* the response body set to the slice of content specified by the request's
|
|
14
|
+
* `Range:` header, or a `416 Range Not Satisfiable` response if the
|
|
15
|
+
* conditions of the `Range:` header can't be met.
|
|
16
|
+
*/
|
|
17
|
+
declare function createPartialResponse(request: Request, originalResponse: Response): Promise<Response>;
|
|
18
|
+
export { createPartialResponse };
|
|
19
|
+
//# sourceMappingURL=createPartialResponse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createPartialResponse.d.ts","sourceRoot":"","sources":["../src/createPartialResponse.ts"],"names":[],"mappings":"AAaA;;;;;;;;;;;;;;;GAeG;AACH,iBAAe,qBAAqB,CAAC,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,CA8DpG;AAED,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAE/D,OAAO,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { assert, SerwistError, logger } from '@serwist/core/internal';
|
|
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) {
|
|
12
|
+
if (process.env.NODE_ENV !== "production") {
|
|
13
|
+
assert.isInstance(blob, Blob, {
|
|
14
|
+
moduleName: "@serwist/range-requests",
|
|
15
|
+
funcName: "calculateEffectiveBoundaries",
|
|
16
|
+
paramName: "blob"
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
const blobSize = blob.size;
|
|
20
|
+
if (end && end > blobSize || start && start < 0) {
|
|
21
|
+
throw new SerwistError("range-not-satisfiable", {
|
|
22
|
+
size: blobSize,
|
|
23
|
+
end,
|
|
24
|
+
start
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
let effectiveStart;
|
|
28
|
+
let effectiveEnd;
|
|
29
|
+
if (start !== undefined && end !== undefined) {
|
|
30
|
+
effectiveStart = start;
|
|
31
|
+
// Range values are inclusive, so add 1 to the value.
|
|
32
|
+
effectiveEnd = end + 1;
|
|
33
|
+
} else if (start !== undefined && end === undefined) {
|
|
34
|
+
effectiveStart = start;
|
|
35
|
+
effectiveEnd = blobSize;
|
|
36
|
+
} else if (end !== undefined && start === undefined) {
|
|
37
|
+
effectiveStart = blobSize - end;
|
|
38
|
+
effectiveEnd = blobSize;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
start: effectiveStart,
|
|
42
|
+
end: effectiveEnd
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
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) {
|
|
53
|
+
if (process.env.NODE_ENV !== "production") {
|
|
54
|
+
assert.isType(rangeHeader, "string", {
|
|
55
|
+
moduleName: "@serwist/range-requests",
|
|
56
|
+
funcName: "parseRangeHeader",
|
|
57
|
+
paramName: "rangeHeader"
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
const normalizedRangeHeader = rangeHeader.trim().toLowerCase();
|
|
61
|
+
if (!normalizedRangeHeader.startsWith("bytes=")) {
|
|
62
|
+
throw new SerwistError("unit-must-be-bytes", {
|
|
63
|
+
normalizedRangeHeader
|
|
64
|
+
});
|
|
65
|
+
}
|
|
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
|
+
if (normalizedRangeHeader.includes(",")) {
|
|
70
|
+
throw new SerwistError("single-range-only", {
|
|
71
|
+
normalizedRangeHeader
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const rangeParts = /(\d*)-(\d*)/.exec(normalizedRangeHeader);
|
|
75
|
+
// We need either at least one of the start or end values.
|
|
76
|
+
if (!rangeParts || !(rangeParts[1] || rangeParts[2])) {
|
|
77
|
+
throw new SerwistError("invalid-range-values", {
|
|
78
|
+
normalizedRangeHeader
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
start: rangeParts[1] === "" ? undefined : Number(rangeParts[1]),
|
|
83
|
+
end: rangeParts[2] === "" ? undefined : Number(rangeParts[2])
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
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) {
|
|
103
|
+
try {
|
|
104
|
+
if (process.env.NODE_ENV !== "production") {
|
|
105
|
+
assert.isInstance(request, Request, {
|
|
106
|
+
moduleName: "@serwist/range-requests",
|
|
107
|
+
funcName: "createPartialResponse",
|
|
108
|
+
paramName: "request"
|
|
109
|
+
});
|
|
110
|
+
assert.isInstance(originalResponse, Response, {
|
|
111
|
+
moduleName: "@serwist/range-requests",
|
|
112
|
+
funcName: "createPartialResponse",
|
|
113
|
+
paramName: "originalResponse"
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
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
|
+
return originalResponse;
|
|
120
|
+
}
|
|
121
|
+
const rangeHeader = request.headers.get("range");
|
|
122
|
+
if (!rangeHeader) {
|
|
123
|
+
throw new SerwistError("no-range-header");
|
|
124
|
+
}
|
|
125
|
+
const boundaries = parseRangeHeader(rangeHeader);
|
|
126
|
+
const originalBlob = await originalResponse.blob();
|
|
127
|
+
const effectiveBoundaries = calculateEffectiveBoundaries(originalBlob, boundaries.start, boundaries.end);
|
|
128
|
+
const slicedBlob = originalBlob.slice(effectiveBoundaries.start, effectiveBoundaries.end);
|
|
129
|
+
const slicedBlobSize = slicedBlob.size;
|
|
130
|
+
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
|
+
status: 206,
|
|
134
|
+
statusText: "Partial Content",
|
|
135
|
+
headers: originalResponse.headers
|
|
136
|
+
});
|
|
137
|
+
slicedResponse.headers.set("Content-Length", String(slicedBlobSize));
|
|
138
|
+
slicedResponse.headers.set("Content-Range", `bytes ${effectiveBoundaries.start}-${effectiveBoundaries.end - 1}/` + `${originalBlob.size}`);
|
|
139
|
+
return slicedResponse;
|
|
140
|
+
} catch (error) {
|
|
141
|
+
if (process.env.NODE_ENV !== "production") {
|
|
142
|
+
logger.warn(`Unable to construct a partial response; returning a ` + `416 Range Not Satisfiable response instead.`);
|
|
143
|
+
logger.groupCollapsed(`View details here.`);
|
|
144
|
+
logger.log(error);
|
|
145
|
+
logger.log(request);
|
|
146
|
+
logger.log(originalResponse);
|
|
147
|
+
logger.groupEnd();
|
|
148
|
+
}
|
|
149
|
+
return new Response("", {
|
|
150
|
+
status: 416,
|
|
151
|
+
statusText: "Range Not Satisfiable"
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The range request plugin makes it easy for a request with a 'Range' header to
|
|
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.
|
|
172
|
+
if (cachedResponse && request.headers.has("range")) {
|
|
173
|
+
return await createPartialResponse(request, cachedResponse);
|
|
174
|
+
}
|
|
175
|
+
// If there was no Range: header, or if cachedResponse wasn't valid, just
|
|
176
|
+
// pass it through as-is.
|
|
177
|
+
return cachedResponse;
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export { RangeRequestsPlugin, createPartialResponse };
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var internal = require('@serwist/core/internal');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param blob A source blob.
|
|
7
|
+
* @param start The offset to use as the start of the
|
|
8
|
+
* slice.
|
|
9
|
+
* @param end The offset to use as the end of the slice.
|
|
10
|
+
* @returns An object with `start` and `end` properties, reflecting
|
|
11
|
+
* the effective boundaries to use given the size of the blob.
|
|
12
|
+
* @private
|
|
13
|
+
*/ function calculateEffectiveBoundaries(blob, start, end) {
|
|
14
|
+
if (process.env.NODE_ENV !== "production") {
|
|
15
|
+
internal.assert.isInstance(blob, Blob, {
|
|
16
|
+
moduleName: "@serwist/range-requests",
|
|
17
|
+
funcName: "calculateEffectiveBoundaries",
|
|
18
|
+
paramName: "blob"
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
const blobSize = blob.size;
|
|
22
|
+
if (end && end > blobSize || start && start < 0) {
|
|
23
|
+
throw new internal.SerwistError("range-not-satisfiable", {
|
|
24
|
+
size: blobSize,
|
|
25
|
+
end,
|
|
26
|
+
start
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
let effectiveStart;
|
|
30
|
+
let effectiveEnd;
|
|
31
|
+
if (start !== undefined && end !== undefined) {
|
|
32
|
+
effectiveStart = start;
|
|
33
|
+
// Range values are inclusive, so add 1 to the value.
|
|
34
|
+
effectiveEnd = end + 1;
|
|
35
|
+
} else if (start !== undefined && end === undefined) {
|
|
36
|
+
effectiveStart = start;
|
|
37
|
+
effectiveEnd = blobSize;
|
|
38
|
+
} else if (end !== undefined && start === undefined) {
|
|
39
|
+
effectiveStart = blobSize - end;
|
|
40
|
+
effectiveEnd = blobSize;
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
start: effectiveStart,
|
|
44
|
+
end: effectiveEnd
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @param rangeHeader A Range: header value.
|
|
50
|
+
* @returns An object with `start` and `end` properties, reflecting
|
|
51
|
+
* the parsed value of the Range: header. If either the `start` or `end` are
|
|
52
|
+
* omitted, then `null` will be returned.
|
|
53
|
+
* @private
|
|
54
|
+
*/ function parseRangeHeader(rangeHeader) {
|
|
55
|
+
if (process.env.NODE_ENV !== "production") {
|
|
56
|
+
internal.assert.isType(rangeHeader, "string", {
|
|
57
|
+
moduleName: "@serwist/range-requests",
|
|
58
|
+
funcName: "parseRangeHeader",
|
|
59
|
+
paramName: "rangeHeader"
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const normalizedRangeHeader = rangeHeader.trim().toLowerCase();
|
|
63
|
+
if (!normalizedRangeHeader.startsWith("bytes=")) {
|
|
64
|
+
throw new internal.SerwistError("unit-must-be-bytes", {
|
|
65
|
+
normalizedRangeHeader
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
// Specifying multiple ranges separate by commas is valid syntax, but this
|
|
69
|
+
// library only attempts to handle a single, contiguous sequence of bytes.
|
|
70
|
+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range#Syntax
|
|
71
|
+
if (normalizedRangeHeader.includes(",")) {
|
|
72
|
+
throw new internal.SerwistError("single-range-only", {
|
|
73
|
+
normalizedRangeHeader
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
const rangeParts = /(\d*)-(\d*)/.exec(normalizedRangeHeader);
|
|
77
|
+
// We need either at least one of the start or end values.
|
|
78
|
+
if (!rangeParts || !(rangeParts[1] || rangeParts[2])) {
|
|
79
|
+
throw new internal.SerwistError("invalid-range-values", {
|
|
80
|
+
normalizedRangeHeader
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
start: rangeParts[1] === "" ? undefined : Number(rangeParts[1]),
|
|
85
|
+
end: rangeParts[2] === "" ? undefined : Number(rangeParts[2])
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Given a `Request` and `Response` objects as input, this will return a
|
|
91
|
+
* promise for a new `Response`.
|
|
92
|
+
*
|
|
93
|
+
* If the original `Response` already contains partial content (i.e. it has
|
|
94
|
+
* a status of 206), then this assumes it already fulfills the `Range:`
|
|
95
|
+
* requirements, and will return it as-is.
|
|
96
|
+
*
|
|
97
|
+
* @param request A request, which should contain a Range:
|
|
98
|
+
* header.
|
|
99
|
+
* @param originalResponse A response.
|
|
100
|
+
* @returns Either a `206 Partial Content` response, with
|
|
101
|
+
* the response body set to the slice of content specified by the request's
|
|
102
|
+
* `Range:` header, or a `416 Range Not Satisfiable` response if the
|
|
103
|
+
* conditions of the `Range:` header can't be met.
|
|
104
|
+
*/ async function createPartialResponse(request, originalResponse) {
|
|
105
|
+
try {
|
|
106
|
+
if (process.env.NODE_ENV !== "production") {
|
|
107
|
+
internal.assert.isInstance(request, Request, {
|
|
108
|
+
moduleName: "@serwist/range-requests",
|
|
109
|
+
funcName: "createPartialResponse",
|
|
110
|
+
paramName: "request"
|
|
111
|
+
});
|
|
112
|
+
internal.assert.isInstance(originalResponse, Response, {
|
|
113
|
+
moduleName: "@serwist/range-requests",
|
|
114
|
+
funcName: "createPartialResponse",
|
|
115
|
+
paramName: "originalResponse"
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
if (originalResponse.status === 206) {
|
|
119
|
+
// If we already have a 206, then just pass it through as-is;
|
|
120
|
+
// see https://github.com/GoogleChrome/workbox/issues/1720
|
|
121
|
+
return originalResponse;
|
|
122
|
+
}
|
|
123
|
+
const rangeHeader = request.headers.get("range");
|
|
124
|
+
if (!rangeHeader) {
|
|
125
|
+
throw new internal.SerwistError("no-range-header");
|
|
126
|
+
}
|
|
127
|
+
const boundaries = parseRangeHeader(rangeHeader);
|
|
128
|
+
const originalBlob = await originalResponse.blob();
|
|
129
|
+
const effectiveBoundaries = calculateEffectiveBoundaries(originalBlob, boundaries.start, boundaries.end);
|
|
130
|
+
const slicedBlob = originalBlob.slice(effectiveBoundaries.start, effectiveBoundaries.end);
|
|
131
|
+
const slicedBlobSize = slicedBlob.size;
|
|
132
|
+
const slicedResponse = new Response(slicedBlob, {
|
|
133
|
+
// Status code 206 is for a Partial Content response.
|
|
134
|
+
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206
|
|
135
|
+
status: 206,
|
|
136
|
+
statusText: "Partial Content",
|
|
137
|
+
headers: originalResponse.headers
|
|
138
|
+
});
|
|
139
|
+
slicedResponse.headers.set("Content-Length", String(slicedBlobSize));
|
|
140
|
+
slicedResponse.headers.set("Content-Range", `bytes ${effectiveBoundaries.start}-${effectiveBoundaries.end - 1}/` + `${originalBlob.size}`);
|
|
141
|
+
return slicedResponse;
|
|
142
|
+
} catch (error) {
|
|
143
|
+
if (process.env.NODE_ENV !== "production") {
|
|
144
|
+
internal.logger.warn(`Unable to construct a partial response; returning a ` + `416 Range Not Satisfiable response instead.`);
|
|
145
|
+
internal.logger.groupCollapsed(`View details here.`);
|
|
146
|
+
internal.logger.log(error);
|
|
147
|
+
internal.logger.log(request);
|
|
148
|
+
internal.logger.log(originalResponse);
|
|
149
|
+
internal.logger.groupEnd();
|
|
150
|
+
}
|
|
151
|
+
return new Response("", {
|
|
152
|
+
status: 416,
|
|
153
|
+
statusText: "Range Not Satisfiable"
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The range request plugin makes it easy for a request with a 'Range' header to
|
|
160
|
+
* be fulfilled by a cached response.
|
|
161
|
+
*
|
|
162
|
+
* It does this by intercepting the `cachedResponseWillBeUsed` plugin callback
|
|
163
|
+
* and returning the appropriate subset of the cached response body.
|
|
164
|
+
*/ class RangeRequestsPlugin {
|
|
165
|
+
/**
|
|
166
|
+
* @param options
|
|
167
|
+
* @returns If request contains a 'Range' header, then a
|
|
168
|
+
* new response with status 206 whose body is a subset of `cachedResponse` is
|
|
169
|
+
* returned. Otherwise, `cachedResponse` is returned as-is.
|
|
170
|
+
* @private
|
|
171
|
+
*/ cachedResponseWillBeUsed = async ({ request, cachedResponse })=>{
|
|
172
|
+
// Only return a sliced response if there's something valid in the cache,
|
|
173
|
+
// and there's a Range: header in the request.
|
|
174
|
+
if (cachedResponse && request.headers.has("range")) {
|
|
175
|
+
return await createPartialResponse(request, cachedResponse);
|
|
176
|
+
}
|
|
177
|
+
// If there was no Range: header, or if cachedResponse wasn't valid, just
|
|
178
|
+
// pass it through as-is.
|
|
179
|
+
return cachedResponse;
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
exports.RangeRequestsPlugin = RangeRequestsPlugin;
|
|
184
|
+
exports.createPartialResponse = createPartialResponse;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param blob A source blob.
|
|
3
|
+
* @param start The offset to use as the start of the
|
|
4
|
+
* slice.
|
|
5
|
+
* @param end The offset to use as the end of the slice.
|
|
6
|
+
* @returns An object with `start` and `end` properties, reflecting
|
|
7
|
+
* the effective boundaries to use given the size of the blob.
|
|
8
|
+
* @private
|
|
9
|
+
*/
|
|
10
|
+
declare function calculateEffectiveBoundaries(blob: Blob, start?: number, end?: number): {
|
|
11
|
+
start: number;
|
|
12
|
+
end: number;
|
|
13
|
+
};
|
|
14
|
+
export { calculateEffectiveBoundaries };
|
|
15
|
+
//# sourceMappingURL=calculateEffectiveBoundaries.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculateEffectiveBoundaries.d.ts","sourceRoot":"","sources":["../../src/utils/calculateEffectiveBoundaries.ts"],"names":[],"mappings":"AAUA;;;;;;;;GAQG;AACH,iBAAS,4BAA4B,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAsC9G;AAED,OAAO,EAAE,4BAA4B,EAAE,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param rangeHeader A Range: header value.
|
|
3
|
+
* @returns An object with `start` and `end` properties, reflecting
|
|
4
|
+
* the parsed value of the Range: header. If either the `start` or `end` are
|
|
5
|
+
* omitted, then `null` will be returned.
|
|
6
|
+
* @private
|
|
7
|
+
*/
|
|
8
|
+
declare function parseRangeHeader(rangeHeader: string): {
|
|
9
|
+
start?: number;
|
|
10
|
+
end?: number;
|
|
11
|
+
};
|
|
12
|
+
export { parseRangeHeader };
|
|
13
|
+
//# sourceMappingURL=parseRangeHeader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseRangeHeader.d.ts","sourceRoot":"","sources":["../../src/utils/parseRangeHeader.ts"],"names":[],"mappings":"AAUA;;;;;;GAMG;AACH,iBAAS,gBAAgB,CAAC,WAAW,EAAE,MAAM,GAAG;IAC9C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;CACd,CA+BA;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@serwist/range-requests",
|
|
3
|
+
"version": "8.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "This library creates a new Response, given a source Response and a Range header value.",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist",
|
|
8
|
+
"!dist/**/dts"
|
|
9
|
+
],
|
|
10
|
+
"keywords": [
|
|
11
|
+
"serwist",
|
|
12
|
+
"serwistjs",
|
|
13
|
+
"service worker",
|
|
14
|
+
"sw",
|
|
15
|
+
"caching",
|
|
16
|
+
"cache",
|
|
17
|
+
"range",
|
|
18
|
+
"media",
|
|
19
|
+
"serwist-plugin"
|
|
20
|
+
],
|
|
21
|
+
"author": "Google's Web DevRel Team, Serwist's Team",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"repository": "serwist/serwist",
|
|
24
|
+
"bugs": "https://github.com/serwist/serwist/issues",
|
|
25
|
+
"homepage": "https://ducanh-next-pwa.vercel.app",
|
|
26
|
+
"module": "./dist/index.js",
|
|
27
|
+
"main": "./dist/index.old.cjs",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"require": "./dist/index.old.cjs",
|
|
33
|
+
"types": "./dist/index.d.ts"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@serwist/core": "8.0.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"rollup": "3.28.1",
|
|
42
|
+
"@serwist/constants": "8.0.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
|
|
46
|
+
"lint": "eslint src --ext ts,tsx,js,jsx,cjs,mjs",
|
|
47
|
+
"typecheck": "tsc"
|
|
48
|
+
}
|
|
49
|
+
}
|