@serwist/cacheable-response 9.0.0-preview.17 → 9.0.0-preview.19

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,6 +1,3 @@
1
- import type { CacheableResponseOptions } from "./CacheableResponse.js";
2
- import { CacheableResponse } from "./CacheableResponse.js";
3
- import { CacheableResponsePlugin } from "./CacheableResponsePlugin.js";
4
- export { CacheableResponse, CacheableResponsePlugin };
5
- export type { CacheableResponseOptions };
1
+ export { CacheableResponse, CacheableResponsePlugin } from "@serwist/sw/plugins";
2
+ export type { CacheableResponseOptions } from "@serwist/sw/plugins";
6
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAEvE,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,CAAC;AAEtD,YAAY,EAAE,wBAAwB,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AACjF,YAAY,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC"}
package/dist/index.js CHANGED
@@ -1,97 +1 @@
1
- import { SerwistError, assert, logger, getFriendlyURL } from '@serwist/core/internal';
2
-
3
- class CacheableResponse {
4
- _statuses;
5
- _headers;
6
- constructor(config = {}){
7
- if (process.env.NODE_ENV !== "production") {
8
- if (!(config.statuses || config.headers)) {
9
- throw new SerwistError("statuses-or-headers-required", {
10
- moduleName: "@serwist/cacheable-response",
11
- className: "CacheableResponse",
12
- funcName: "constructor"
13
- });
14
- }
15
- if (config.statuses) {
16
- assert.isArray(config.statuses, {
17
- moduleName: "@serwist/cacheable-response",
18
- className: "CacheableResponse",
19
- funcName: "constructor",
20
- paramName: "config.statuses"
21
- });
22
- }
23
- if (config.headers) {
24
- assert.isType(config.headers, "object", {
25
- moduleName: "@serwist/cacheable-response",
26
- className: "CacheableResponse",
27
- funcName: "constructor",
28
- paramName: "config.headers"
29
- });
30
- }
31
- }
32
- this._statuses = config.statuses;
33
- if (config.headers) {
34
- this._headers = new Headers(config.headers);
35
- }
36
- }
37
- isResponseCacheable(response) {
38
- if (process.env.NODE_ENV !== "production") {
39
- assert.isInstance(response, Response, {
40
- moduleName: "@serwist/cacheable-response",
41
- className: "CacheableResponse",
42
- funcName: "isResponseCacheable",
43
- paramName: "response"
44
- });
45
- }
46
- let cacheable = true;
47
- if (this._statuses) {
48
- cacheable = this._statuses.includes(response.status);
49
- }
50
- if (this._headers && cacheable) {
51
- for (const [headerName, headerValue] of this._headers.entries()){
52
- if (response.headers.get(headerName) !== headerValue) {
53
- cacheable = false;
54
- break;
55
- }
56
- }
57
- }
58
- if (process.env.NODE_ENV !== "production") {
59
- if (!cacheable) {
60
- logger.groupCollapsed(`The request for '${getFriendlyURL(response.url)}' returned a response that does not meet the criteria for being cached.`);
61
- logger.groupCollapsed("View cacheability criteria here.");
62
- logger.log(`Cacheable statuses: ${JSON.stringify(this._statuses)}`);
63
- logger.log(`Cacheable headers: ${JSON.stringify(this._headers, null, 2)}`);
64
- logger.groupEnd();
65
- const logFriendlyHeaders = {};
66
- response.headers.forEach((value, key)=>{
67
- logFriendlyHeaders[key] = value;
68
- });
69
- logger.groupCollapsed("View response status and headers here.");
70
- logger.log(`Response status: ${response.status}`);
71
- logger.log(`Response headers: ${JSON.stringify(logFriendlyHeaders, null, 2)}`);
72
- logger.groupEnd();
73
- logger.groupCollapsed("View full response details here.");
74
- logger.log(response.headers);
75
- logger.log(response);
76
- logger.groupEnd();
77
- logger.groupEnd();
78
- }
79
- }
80
- return cacheable;
81
- }
82
- }
83
-
84
- class CacheableResponsePlugin {
85
- _cacheableResponse;
86
- constructor(config){
87
- this._cacheableResponse = new CacheableResponse(config);
88
- }
89
- cacheWillUpdate = async ({ response })=>{
90
- if (this._cacheableResponse.isResponseCacheable(response)) {
91
- return response;
92
- }
93
- return null;
94
- };
95
- }
96
-
97
- export { CacheableResponse, CacheableResponsePlugin };
1
+ export { CacheableResponse, CacheableResponsePlugin } from '@serwist/sw/plugins';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/cacheable-response",
3
- "version": "9.0.0-preview.17",
3
+ "version": "9.0.0-preview.19",
4
4
  "type": "module",
5
5
  "description": "A module that takes a Response object and determines whether it's cacheable based on a specific configuration.",
6
6
  "files": [
@@ -16,8 +16,8 @@
16
16
  ],
17
17
  "author": "Google's Web DevRel Team, Serwist's Team",
18
18
  "license": "MIT",
19
- "repository": "serwist/serwist",
20
- "bugs": "https://github.com/serwist/serwist/issues",
19
+ "repository": "https://gitlab.com/serwist/serwist",
20
+ "bugs": "https://gitlab.com/serwist/serwist/issues",
21
21
  "homepage": "https://serwist.pages.dev",
22
22
  "main": "./dist/index.js",
23
23
  "types": "./dist/index.d.ts",
@@ -29,12 +29,12 @@
29
29
  "./package.json": "./package.json"
30
30
  },
31
31
  "dependencies": {
32
- "@serwist/core": "9.0.0-preview.17"
32
+ "@serwist/sw": "9.0.0-preview.19"
33
33
  },
34
34
  "devDependencies": {
35
35
  "rollup": "4.13.0",
36
36
  "typescript": "5.5.0-dev.20240323",
37
- "@serwist/constants": "9.0.0-preview.17"
37
+ "@serwist/constants": "9.0.0-preview.19"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "typescript": ">=5.0.0"
@@ -45,8 +45,7 @@
45
45
  }
46
46
  },
47
47
  "scripts": {
48
- "build": "cross-env NODE_ENV=production rollup --config rollup.config.js",
49
- "dev": "rollup --config rollup.config.js --watch",
48
+ "build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
50
49
  "lint": "biome lint ./src",
51
50
  "typecheck": "tsc"
52
51
  }
package/src/index.ts CHANGED
@@ -1,15 +1,2 @@
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 { CacheableResponseOptions } from "./CacheableResponse.js";
10
- import { CacheableResponse } from "./CacheableResponse.js";
11
- import { CacheableResponsePlugin } from "./CacheableResponsePlugin.js";
12
-
13
- export { CacheableResponse, CacheableResponsePlugin };
14
-
15
- export type { CacheableResponseOptions };
1
+ export { CacheableResponse, CacheableResponsePlugin } from "@serwist/sw/plugins";
2
+ export type { CacheableResponseOptions } from "@serwist/sw/plugins";
@@ -1,40 +0,0 @@
1
- export interface CacheableResponseOptions {
2
- /**
3
- * One or more status codes that a `Response` can have to be considered cacheable.
4
- */
5
- statuses?: number[];
6
- /**
7
- * A mapping of header names and expected values that a `Response` can have and be
8
- * considered cacheable. If multiple headers are provided, only one needs to be present.
9
- */
10
- headers?: HeadersInit;
11
- }
12
- /**
13
- * Allows you to set up rules determining what status codes and/or headers need
14
- * to be present in order for a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
15
- * to be considered cacheable.
16
- */
17
- export declare class CacheableResponse {
18
- private readonly _statuses?;
19
- private readonly _headers?;
20
- /**
21
- * To construct a new CacheableResponse instance you must provide at least
22
- * one of the `config` properties.
23
- *
24
- * If both `statuses` and `headers` are specified, then both conditions must
25
- * be met for the `Response` to be considered cacheable.
26
- *
27
- * @param config
28
- */
29
- constructor(config?: CacheableResponseOptions);
30
- /**
31
- * Checks a response to see whether it's cacheable or not.
32
- *
33
- * @param response The response whose cacheability is being
34
- * checked.
35
- * @returns `true` if the `Response` is cacheable, and `false`
36
- * otherwise.
37
- */
38
- isResponseCacheable(response: Response): boolean;
39
- }
40
- //# sourceMappingURL=CacheableResponse.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"CacheableResponse.d.ts","sourceRoot":"","sources":["../src/CacheableResponse.ts"],"names":[],"mappings":"AAUA,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB;;;OAGG;IACH,OAAO,CAAC,EAAE,WAAW,CAAC;CACvB;AAED;;;;GAIG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAuC;IAClE,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAU;IAEpC;;;;;;;;OAQG;gBACS,MAAM,GAAE,wBAA6B;IAmCjD;;;;;;;OAOG;IACH,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO;CAyDjD"}
@@ -1,27 +0,0 @@
1
- import type { SerwistPlugin } from "@serwist/core";
2
- import type { CacheableResponseOptions } from "./CacheableResponse.js";
3
- /**
4
- * A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
5
- * easier to add in cacheability checks to requests made via Serwist's built-in
6
- * strategies.
7
- */
8
- export declare class CacheableResponsePlugin implements SerwistPlugin {
9
- private readonly _cacheableResponse;
10
- /**
11
- * To construct a new CacheableResponsePlugin instance you must provide at
12
- * least one of the `config` properties.
13
- *
14
- * If both `statuses` and `headers` are specified, then both conditions must
15
- * be met for the `Response` to be considered cacheable.
16
- *
17
- * @param config
18
- */
19
- constructor(config: CacheableResponseOptions);
20
- /**
21
- * @param options
22
- * @returns
23
- * @private
24
- */
25
- cacheWillUpdate: SerwistPlugin["cacheWillUpdate"];
26
- }
27
- //# sourceMappingURL=CacheableResponsePlugin.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"CacheableResponsePlugin.d.ts","sourceRoot":"","sources":["../src/CacheableResponsePlugin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAGvE;;;;GAIG;AACH,qBAAa,uBAAwB,YAAW,aAAa;IAC3D,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAoB;IAEvD;;;;;;;;OAQG;gBACS,MAAM,EAAE,wBAAwB;IAI5C;;;;OAIG;IACH,eAAe,EAAE,aAAa,CAAC,iBAAiB,CAAC,CAK/C;CACH"}
@@ -1,141 +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, getFriendlyURL, logger } from "@serwist/core/internal";
10
-
11
- export interface CacheableResponseOptions {
12
- /**
13
- * One or more status codes that a `Response` can have to be considered cacheable.
14
- */
15
- statuses?: number[];
16
- /**
17
- * A mapping of header names and expected values that a `Response` can have and be
18
- * considered cacheable. If multiple headers are provided, only one needs to be present.
19
- */
20
- headers?: HeadersInit;
21
- }
22
-
23
- /**
24
- * Allows you to set up rules determining what status codes and/or headers need
25
- * to be present in order for a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
26
- * to be considered cacheable.
27
- */
28
- export class CacheableResponse {
29
- private readonly _statuses?: CacheableResponseOptions["statuses"];
30
- private readonly _headers?: Headers;
31
-
32
- /**
33
- * To construct a new CacheableResponse instance you must provide at least
34
- * one of the `config` properties.
35
- *
36
- * If both `statuses` and `headers` are specified, then both conditions must
37
- * be met for the `Response` to be considered cacheable.
38
- *
39
- * @param config
40
- */
41
- constructor(config: CacheableResponseOptions = {}) {
42
- if (process.env.NODE_ENV !== "production") {
43
- if (!(config.statuses || config.headers)) {
44
- throw new SerwistError("statuses-or-headers-required", {
45
- moduleName: "@serwist/cacheable-response",
46
- className: "CacheableResponse",
47
- funcName: "constructor",
48
- });
49
- }
50
-
51
- if (config.statuses) {
52
- assert!.isArray(config.statuses, {
53
- moduleName: "@serwist/cacheable-response",
54
- className: "CacheableResponse",
55
- funcName: "constructor",
56
- paramName: "config.statuses",
57
- });
58
- }
59
-
60
- if (config.headers) {
61
- assert!.isType(config.headers, "object", {
62
- moduleName: "@serwist/cacheable-response",
63
- className: "CacheableResponse",
64
- funcName: "constructor",
65
- paramName: "config.headers",
66
- });
67
- }
68
- }
69
-
70
- this._statuses = config.statuses;
71
- if (config.headers) {
72
- this._headers = new Headers(config.headers);
73
- }
74
- }
75
-
76
- /**
77
- * Checks a response to see whether it's cacheable or not.
78
- *
79
- * @param response The response whose cacheability is being
80
- * checked.
81
- * @returns `true` if the `Response` is cacheable, and `false`
82
- * otherwise.
83
- */
84
- isResponseCacheable(response: Response): boolean {
85
- if (process.env.NODE_ENV !== "production") {
86
- assert!.isInstance(response, Response, {
87
- moduleName: "@serwist/cacheable-response",
88
- className: "CacheableResponse",
89
- funcName: "isResponseCacheable",
90
- paramName: "response",
91
- });
92
- }
93
-
94
- let cacheable = true;
95
-
96
- if (this._statuses) {
97
- cacheable = this._statuses.includes(response.status);
98
- }
99
-
100
- if (this._headers && cacheable) {
101
- for (const [headerName, headerValue] of this._headers.entries()) {
102
- if (response.headers.get(headerName) !== headerValue) {
103
- cacheable = false;
104
- break;
105
- }
106
- }
107
- }
108
-
109
- if (process.env.NODE_ENV !== "production") {
110
- if (!cacheable) {
111
- logger.groupCollapsed(
112
- `The request for '${getFriendlyURL(response.url)}' returned a response that does not meet the criteria for being cached.`,
113
- );
114
-
115
- logger.groupCollapsed("View cacheability criteria here.");
116
- logger.log(`Cacheable statuses: ${JSON.stringify(this._statuses)}`);
117
- logger.log(`Cacheable headers: ${JSON.stringify(this._headers, null, 2)}`);
118
- logger.groupEnd();
119
-
120
- const logFriendlyHeaders: { [key: string]: string } = {};
121
- response.headers.forEach((value, key) => {
122
- logFriendlyHeaders[key] = value;
123
- });
124
-
125
- logger.groupCollapsed("View response status and headers here.");
126
- logger.log(`Response status: ${response.status}`);
127
- logger.log(`Response headers: ${JSON.stringify(logFriendlyHeaders, null, 2)}`);
128
- logger.groupEnd();
129
-
130
- logger.groupCollapsed("View full response details here.");
131
- logger.log(response.headers);
132
- logger.log(response);
133
- logger.groupEnd();
134
-
135
- logger.groupEnd();
136
- }
137
- }
138
-
139
- return cacheable;
140
- }
141
- }
@@ -1,46 +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 type { CacheableResponseOptions } from "./CacheableResponse.js";
12
- import { CacheableResponse } from "./CacheableResponse.js";
13
-
14
- /**
15
- * A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
16
- * easier to add in cacheability checks to requests made via Serwist's built-in
17
- * strategies.
18
- */
19
- export class CacheableResponsePlugin implements SerwistPlugin {
20
- private readonly _cacheableResponse: CacheableResponse;
21
-
22
- /**
23
- * To construct a new CacheableResponsePlugin instance you must provide at
24
- * least one of the `config` properties.
25
- *
26
- * If both `statuses` and `headers` are specified, then both conditions must
27
- * be met for the `Response` to be considered cacheable.
28
- *
29
- * @param config
30
- */
31
- constructor(config: CacheableResponseOptions) {
32
- this._cacheableResponse = new CacheableResponse(config);
33
- }
34
-
35
- /**
36
- * @param options
37
- * @returns
38
- * @private
39
- */
40
- cacheWillUpdate: SerwistPlugin["cacheWillUpdate"] = async ({ response }) => {
41
- if (this._cacheableResponse.isResponseCacheable(response)) {
42
- return response;
43
- }
44
- return null;
45
- };
46
- }