@serwist/range-requests 9.0.0-preview.8 → 9.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/dist/index.d.ts CHANGED
@@ -1,4 +1,2 @@
1
- import { RangeRequestsPlugin } from "./RangeRequestsPlugin.js";
2
- import { createPartialResponse } from "./createPartialResponse.js";
3
- export { createPartialResponse, RangeRequestsPlugin };
1
+ export { createPartialResponse, RangeRequestsPlugin } from "serwist";
4
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAEnE,OAAO,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -1,127 +1 @@
1
- import { assert, SerwistError, logger } from '@serwist/core/internal';
2
-
3
- function calculateEffectiveBoundaries(blob, start, end) {
4
- if (process.env.NODE_ENV !== "production") {
5
- assert.isInstance(blob, Blob, {
6
- moduleName: "@serwist/range-requests",
7
- funcName: "calculateEffectiveBoundaries",
8
- paramName: "blob"
9
- });
10
- }
11
- const blobSize = blob.size;
12
- if (end && end > blobSize || start && start < 0) {
13
- throw new SerwistError("range-not-satisfiable", {
14
- size: blobSize,
15
- end,
16
- start
17
- });
18
- }
19
- let effectiveStart;
20
- let effectiveEnd;
21
- if (start !== undefined && end !== undefined) {
22
- effectiveStart = start;
23
- effectiveEnd = end + 1;
24
- } else if (start !== undefined && end === undefined) {
25
- effectiveStart = start;
26
- effectiveEnd = blobSize;
27
- } else if (end !== undefined && start === undefined) {
28
- effectiveStart = blobSize - end;
29
- effectiveEnd = blobSize;
30
- }
31
- return {
32
- start: effectiveStart,
33
- end: effectiveEnd
34
- };
35
- }
36
-
37
- function parseRangeHeader(rangeHeader) {
38
- if (process.env.NODE_ENV !== "production") {
39
- assert.isType(rangeHeader, "string", {
40
- moduleName: "@serwist/range-requests",
41
- funcName: "parseRangeHeader",
42
- paramName: "rangeHeader"
43
- });
44
- }
45
- const normalizedRangeHeader = rangeHeader.trim().toLowerCase();
46
- if (!normalizedRangeHeader.startsWith("bytes=")) {
47
- throw new SerwistError("unit-must-be-bytes", {
48
- normalizedRangeHeader
49
- });
50
- }
51
- if (normalizedRangeHeader.includes(",")) {
52
- throw new SerwistError("single-range-only", {
53
- normalizedRangeHeader
54
- });
55
- }
56
- const rangeParts = /(\d*)-(\d*)/.exec(normalizedRangeHeader);
57
- if (!rangeParts || !(rangeParts[1] || rangeParts[2])) {
58
- throw new SerwistError("invalid-range-values", {
59
- normalizedRangeHeader
60
- });
61
- }
62
- return {
63
- start: rangeParts[1] === "" ? undefined : Number(rangeParts[1]),
64
- end: rangeParts[2] === "" ? undefined : Number(rangeParts[2])
65
- };
66
- }
67
-
68
- async function createPartialResponse(request, originalResponse) {
69
- try {
70
- if (process.env.NODE_ENV !== "production") {
71
- assert.isInstance(request, Request, {
72
- moduleName: "@serwist/range-requests",
73
- funcName: "createPartialResponse",
74
- paramName: "request"
75
- });
76
- assert.isInstance(originalResponse, Response, {
77
- moduleName: "@serwist/range-requests",
78
- funcName: "createPartialResponse",
79
- paramName: "originalResponse"
80
- });
81
- }
82
- if (originalResponse.status === 206) {
83
- return originalResponse;
84
- }
85
- const rangeHeader = request.headers.get("range");
86
- if (!rangeHeader) {
87
- throw new SerwistError("no-range-header");
88
- }
89
- const boundaries = parseRangeHeader(rangeHeader);
90
- const originalBlob = await originalResponse.blob();
91
- const effectiveBoundaries = calculateEffectiveBoundaries(originalBlob, boundaries.start, boundaries.end);
92
- const slicedBlob = originalBlob.slice(effectiveBoundaries.start, effectiveBoundaries.end);
93
- const slicedBlobSize = slicedBlob.size;
94
- const slicedResponse = new Response(slicedBlob, {
95
- status: 206,
96
- statusText: "Partial Content",
97
- headers: originalResponse.headers
98
- });
99
- slicedResponse.headers.set("Content-Length", String(slicedBlobSize));
100
- slicedResponse.headers.set("Content-Range", `bytes ${effectiveBoundaries.start}-${effectiveBoundaries.end - 1}/` + `${originalBlob.size}`);
101
- return slicedResponse;
102
- } catch (error) {
103
- if (process.env.NODE_ENV !== "production") {
104
- logger.warn("Unable to construct a partial response; returning a " + "416 Range Not Satisfiable response instead.");
105
- logger.groupCollapsed("View details here.");
106
- logger.log(error);
107
- logger.log(request);
108
- logger.log(originalResponse);
109
- logger.groupEnd();
110
- }
111
- return new Response("", {
112
- status: 416,
113
- statusText: "Range Not Satisfiable"
114
- });
115
- }
116
- }
117
-
118
- class RangeRequestsPlugin {
119
- cachedResponseWillBeUsed = async ({ request, cachedResponse })=>{
120
- if (cachedResponse && request.headers.has("range")) {
121
- return await createPartialResponse(request, cachedResponse);
122
- }
123
- return cachedResponse;
124
- };
125
- }
126
-
127
- export { RangeRequestsPlugin, createPartialResponse };
1
+ export { RangeRequestsPlugin, createPartialResponse } from 'serwist';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/range-requests",
3
- "version": "9.0.0-preview.8",
3
+ "version": "9.0.0",
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": [
@@ -20,7 +20,7 @@
20
20
  ],
21
21
  "author": "Google's Web DevRel Team, Serwist's Team",
22
22
  "license": "MIT",
23
- "repository": "serwist/serwist",
23
+ "repository": "https://github.com/serwist/serwist",
24
24
  "bugs": "https://github.com/serwist/serwist/issues",
25
25
  "homepage": "https://serwist.pages.dev",
26
26
  "main": "./dist/index.js",
@@ -33,12 +33,12 @@
33
33
  "./package.json": "./package.json"
34
34
  },
35
35
  "dependencies": {
36
- "@serwist/core": "9.0.0-preview.8"
36
+ "serwist": "9.0.0"
37
37
  },
38
38
  "devDependencies": {
39
- "rollup": "4.9.6",
40
- "typescript": "5.4.0-dev.20240206",
41
- "@serwist/constants": "9.0.0-preview.8"
39
+ "rollup": "4.14.3",
40
+ "typescript": "5.5.0-dev.20240415",
41
+ "@serwist/configs": "9.0.0"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "typescript": ">=5.0.0"
@@ -50,7 +50,6 @@
50
50
  },
51
51
  "scripts": {
52
52
  "build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
53
- "dev": "rollup --config rollup.config.js --watch",
54
53
  "lint": "biome lint ./src",
55
54
  "typecheck": "tsc"
56
55
  }
package/src/index.ts CHANGED
@@ -1,12 +1 @@
1
- /*
2
- Copyright 2018 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import { RangeRequestsPlugin } from "./RangeRequestsPlugin.js";
10
- import { createPartialResponse } from "./createPartialResponse.js";
11
-
12
- export { createPartialResponse, RangeRequestsPlugin };
1
+ export { createPartialResponse, RangeRequestsPlugin } from "serwist";
@@ -1,20 +0,0 @@
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
@@ -1 +0,0 @@
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"}
@@ -1,19 +0,0 @@
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
@@ -1 +0,0 @@
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"}
@@ -1,15 +0,0 @@
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
@@ -1 +0,0 @@
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"}
@@ -1,13 +0,0 @@
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
@@ -1 +0,0 @@
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"}
@@ -1,41 +0,0 @@
1
- /*
2
- Copyright 2018 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import type { SerwistPlugin } from "@serwist/core";
10
-
11
- import { createPartialResponse } from "./createPartialResponse.js";
12
-
13
- /**
14
- * The range request plugin makes it easy for a request with a 'Range' header to
15
- * be fulfilled by a cached response.
16
- *
17
- * It does this by intercepting the `cachedResponseWillBeUsed` plugin callback
18
- * and returning the appropriate subset of the cached response body.
19
- */
20
- class RangeRequestsPlugin implements SerwistPlugin {
21
- /**
22
- * @param options
23
- * @returns If request contains a 'Range' header, then a
24
- * new response with status 206 whose body is a subset of `cachedResponse` is
25
- * returned. Otherwise, `cachedResponse` is returned as-is.
26
- * @private
27
- */
28
- cachedResponseWillBeUsed: SerwistPlugin["cachedResponseWillBeUsed"] = async ({ request, cachedResponse }) => {
29
- // Only return a sliced response if there's something valid in the cache,
30
- // and there's a Range: header in the request.
31
- if (cachedResponse && request.headers.has("range")) {
32
- return await createPartialResponse(request, cachedResponse);
33
- }
34
-
35
- // If there was no Range: header, or if cachedResponse wasn't valid, just
36
- // pass it through as-is.
37
- return cachedResponse;
38
- };
39
- }
40
-
41
- export { RangeRequestsPlugin };
@@ -1,94 +0,0 @@
1
- /*
2
- Copyright 2018 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import { assert, SerwistError, logger } from "@serwist/core/internal";
10
-
11
- import { calculateEffectiveBoundaries } from "./utils/calculateEffectiveBoundaries.js";
12
- import { parseRangeHeader } from "./utils/parseRangeHeader.js";
13
-
14
- /**
15
- * Given a `Request` and `Response` objects as input, this will return a
16
- * promise for a new `Response`.
17
- *
18
- * If the original `Response` already contains partial content (i.e. it has
19
- * a status of 206), then this assumes it already fulfills the `Range:`
20
- * requirements, and will return it as-is.
21
- *
22
- * @param request A request, which should contain a Range:
23
- * header.
24
- * @param originalResponse A response.
25
- * @returns Either a `206 Partial Content` response, with
26
- * the response body set to the slice of content specified by the request's
27
- * `Range:` header, or a `416 Range Not Satisfiable` response if the
28
- * conditions of the `Range:` header can't be met.
29
- */
30
- async function createPartialResponse(request: Request, originalResponse: Response): Promise<Response> {
31
- try {
32
- if (process.env.NODE_ENV !== "production") {
33
- assert!.isInstance(request, Request, {
34
- moduleName: "@serwist/range-requests",
35
- funcName: "createPartialResponse",
36
- paramName: "request",
37
- });
38
-
39
- assert!.isInstance(originalResponse, Response, {
40
- moduleName: "@serwist/range-requests",
41
- funcName: "createPartialResponse",
42
- paramName: "originalResponse",
43
- });
44
- }
45
-
46
- if (originalResponse.status === 206) {
47
- // If we already have a 206, then just pass it through as-is;
48
- // see https://github.com/GoogleChrome/workbox/issues/1720
49
- return originalResponse;
50
- }
51
-
52
- const rangeHeader = request.headers.get("range");
53
- if (!rangeHeader) {
54
- throw new SerwistError("no-range-header");
55
- }
56
-
57
- const boundaries = parseRangeHeader(rangeHeader);
58
- const originalBlob = await originalResponse.blob();
59
-
60
- const effectiveBoundaries = calculateEffectiveBoundaries(originalBlob, boundaries.start, boundaries.end);
61
-
62
- const slicedBlob = originalBlob.slice(effectiveBoundaries.start, effectiveBoundaries.end);
63
- const slicedBlobSize = slicedBlob.size;
64
-
65
- const slicedResponse = new Response(slicedBlob, {
66
- // Status code 206 is for a Partial Content response.
67
- // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206
68
- status: 206,
69
- statusText: "Partial Content",
70
- headers: originalResponse.headers,
71
- });
72
-
73
- slicedResponse.headers.set("Content-Length", String(slicedBlobSize));
74
- slicedResponse.headers.set("Content-Range", `bytes ${effectiveBoundaries.start}-${effectiveBoundaries.end - 1}/` + `${originalBlob.size}`);
75
-
76
- return slicedResponse;
77
- } catch (error) {
78
- if (process.env.NODE_ENV !== "production") {
79
- logger.warn("Unable to construct a partial response; returning a " + "416 Range Not Satisfiable response instead.");
80
- logger.groupCollapsed("View details here.");
81
- logger.log(error);
82
- logger.log(request);
83
- logger.log(originalResponse);
84
- logger.groupEnd();
85
- }
86
-
87
- return new Response("", {
88
- status: 416,
89
- statusText: "Range Not Satisfiable",
90
- });
91
- }
92
- }
93
-
94
- export { createPartialResponse };
@@ -1,60 +0,0 @@
1
- /*
2
- Copyright 2018 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import { assert, SerwistError } from "@serwist/core/internal";
10
-
11
- /**
12
- * @param blob A source blob.
13
- * @param start The offset to use as the start of the
14
- * slice.
15
- * @param end The offset to use as the end of the slice.
16
- * @returns An object with `start` and `end` properties, reflecting
17
- * the effective boundaries to use given the size of the blob.
18
- * @private
19
- */
20
- function calculateEffectiveBoundaries(blob: Blob, start?: number, end?: number): { start: number; end: number } {
21
- if (process.env.NODE_ENV !== "production") {
22
- assert!.isInstance(blob, Blob, {
23
- moduleName: "@serwist/range-requests",
24
- funcName: "calculateEffectiveBoundaries",
25
- paramName: "blob",
26
- });
27
- }
28
-
29
- const blobSize = blob.size;
30
-
31
- if ((end && end > blobSize) || (start && start < 0)) {
32
- throw new SerwistError("range-not-satisfiable", {
33
- size: blobSize,
34
- end,
35
- start,
36
- });
37
- }
38
-
39
- let effectiveStart: number;
40
- let effectiveEnd: number;
41
-
42
- if (start !== undefined && end !== undefined) {
43
- effectiveStart = start;
44
- // Range values are inclusive, so add 1 to the value.
45
- effectiveEnd = end + 1;
46
- } else if (start !== undefined && end === undefined) {
47
- effectiveStart = start;
48
- effectiveEnd = blobSize;
49
- } else if (end !== undefined && start === undefined) {
50
- effectiveStart = blobSize - end;
51
- effectiveEnd = blobSize;
52
- }
53
-
54
- return {
55
- start: effectiveStart!,
56
- end: effectiveEnd!,
57
- };
58
- }
59
-
60
- export { calculateEffectiveBoundaries };
@@ -1,54 +0,0 @@
1
- /*
2
- Copyright 2018 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import { assert, SerwistError } from "@serwist/core/internal";
10
-
11
- /**
12
- * @param rangeHeader A Range: header value.
13
- * @returns An object with `start` and `end` properties, reflecting
14
- * the parsed value of the Range: header. If either the `start` or `end` are
15
- * omitted, then `null` will be returned.
16
- * @private
17
- */
18
- function parseRangeHeader(rangeHeader: string): {
19
- start?: number;
20
- end?: number;
21
- } {
22
- if (process.env.NODE_ENV !== "production") {
23
- assert!.isType(rangeHeader, "string", {
24
- moduleName: "@serwist/range-requests",
25
- funcName: "parseRangeHeader",
26
- paramName: "rangeHeader",
27
- });
28
- }
29
-
30
- const normalizedRangeHeader = rangeHeader.trim().toLowerCase();
31
- if (!normalizedRangeHeader.startsWith("bytes=")) {
32
- throw new SerwistError("unit-must-be-bytes", { normalizedRangeHeader });
33
- }
34
-
35
- // Specifying multiple ranges separate by commas is valid syntax, but this
36
- // library only attempts to handle a single, contiguous sequence of bytes.
37
- // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range#Syntax
38
- if (normalizedRangeHeader.includes(",")) {
39
- throw new SerwistError("single-range-only", { normalizedRangeHeader });
40
- }
41
-
42
- const rangeParts = /(\d*)-(\d*)/.exec(normalizedRangeHeader);
43
- // We need either at least one of the start or end values.
44
- if (!rangeParts || !(rangeParts[1] || rangeParts[2])) {
45
- throw new SerwistError("invalid-range-values", { normalizedRangeHeader });
46
- }
47
-
48
- return {
49
- start: rangeParts[1] === "" ? undefined : Number(rangeParts[1]),
50
- end: rangeParts[2] === "" ? undefined : Number(rangeParts[2]),
51
- };
52
- }
53
-
54
- export { parseRangeHeader };