@serwist/cacheable-response 8.4.3 → 9.0.0-preview.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.
@@ -17,7 +17,7 @@ export interface CacheableResponseOptions {
17
17
  * [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
18
18
  * to be considered cacheable.
19
19
  */
20
- declare class CacheableResponse {
20
+ export declare class CacheableResponse {
21
21
  private readonly _statuses?;
22
22
  private readonly _headers?;
23
23
  /**
@@ -41,4 +41,4 @@ declare class CacheableResponse {
41
41
  */
42
42
  isResponseCacheable(response: Response): boolean;
43
43
  }
44
- export { CacheableResponse };
44
+ //# sourceMappingURL=CacheableResponse.d.ts.map
@@ -0,0 +1 @@
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;QAAE,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;CAC5C;AAED;;;;;GAKG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAuC;IAClE,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAsC;IAEhE;;;;;;;;OAQG;gBACS,MAAM,GAAE,wBAA6B;IAiCjD;;;;;;;;OAQG;IACH,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO;CAsDjD"}
@@ -5,7 +5,7 @@ import type { CacheableResponseOptions } from "./CacheableResponse.js";
5
5
  * easier to add in cacheability checks to requests made via Serwist's built-in
6
6
  * strategies.
7
7
  */
8
- declare class CacheableResponsePlugin implements SerwistPlugin {
8
+ export declare class CacheableResponsePlugin implements SerwistPlugin {
9
9
  private readonly _cacheableResponse;
10
10
  /**
11
11
  * To construct a new CacheableResponsePlugin instance you must provide at
@@ -24,4 +24,4 @@ declare class CacheableResponsePlugin implements SerwistPlugin {
24
24
  */
25
25
  cacheWillUpdate: SerwistPlugin["cacheWillUpdate"];
26
26
  }
27
- export { CacheableResponsePlugin };
27
+ //# sourceMappingURL=CacheableResponsePlugin.d.ts.map
@@ -0,0 +1 @@
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"}
package/dist/index.d.ts CHANGED
@@ -3,3 +3,4 @@ import { CacheableResponse } from "./CacheableResponse.js";
3
3
  import { CacheableResponsePlugin } from "./CacheableResponsePlugin.js";
4
4
  export { CacheableResponse, CacheableResponsePlugin };
5
5
  export type { CacheableResponseOptions };
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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"}
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@serwist/cacheable-response",
3
- "version": "8.4.3",
3
+ "version": "9.0.0-preview.0",
4
4
  "type": "module",
5
5
  "description": "This library takes a Response object and determines whether it's cacheable based on a specific configuration.",
6
6
  "files": [
7
- "dist",
8
- "!dist/**/dts"
7
+ "src",
8
+ "dist"
9
9
  ],
10
10
  "keywords": [
11
11
  "serwist",
@@ -19,31 +19,33 @@
19
19
  "repository": "serwist/serwist",
20
20
  "bugs": "https://github.com/serwist/serwist/issues",
21
21
  "homepage": "https://serwist.pages.dev",
22
- "module": "./dist/index.js",
23
- "main": "./dist/index.cjs",
22
+ "main": "./dist/index.js",
24
23
  "types": "./dist/index.d.ts",
25
24
  "exports": {
26
25
  ".": {
27
- "import": {
28
- "types": "./dist/index.d.ts",
29
- "default": "./dist/index.js"
30
- },
31
- "require": {
32
- "types": "./dist/index.d.cts",
33
- "default": "./dist/index.cjs"
34
- }
26
+ "types": "./dist/index.d.ts",
27
+ "default": "./dist/index.js"
35
28
  },
36
29
  "./package.json": "./package.json"
37
30
  },
38
31
  "dependencies": {
39
- "@serwist/core": "8.4.3"
32
+ "@serwist/core": "9.0.0-preview.0"
40
33
  },
41
34
  "devDependencies": {
42
- "rollup": "4.9.1",
43
- "@serwist/constants": "8.4.3"
35
+ "rollup": "4.9.6",
36
+ "typescript": "5.4.0-dev.20240203",
37
+ "@serwist/constants": "9.0.0-preview.0"
38
+ },
39
+ "peerDependencies": {
40
+ "typescript": ">=5.0.0"
41
+ },
42
+ "peerDependenciesMeta": {
43
+ "typescript": {
44
+ "optional": true
45
+ }
44
46
  },
45
47
  "scripts": {
46
- "build": "cross-env NODE_ENV=production rollup --waitForBundleInput --config rollup.config.js",
48
+ "build": "cross-env NODE_ENV=production rollup --config rollup.config.js",
47
49
  "dev": "rollup --config rollup.config.js --watch",
48
50
  "lint": "biome lint ./src",
49
51
  "typecheck": "tsc"
@@ -0,0 +1,138 @@
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?: { [headerName: string]: string };
21
+ }
22
+
23
+ /**
24
+ * This class allows you to set up rules determining what
25
+ * status codes and/or headers need to be present in order for a
26
+ * [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
27
+ * to be considered cacheable.
28
+ */
29
+ export class CacheableResponse {
30
+ private readonly _statuses?: CacheableResponseOptions["statuses"];
31
+ private readonly _headers?: CacheableResponseOptions["headers"];
32
+
33
+ /**
34
+ * To construct a new CacheableResponse instance you must provide at least
35
+ * one of the `config` properties.
36
+ *
37
+ * If both `statuses` and `headers` are specified, then both conditions must
38
+ * be met for the `Response` to be considered cacheable.
39
+ *
40
+ * @param config
41
+ */
42
+ constructor(config: CacheableResponseOptions = {}) {
43
+ if (process.env.NODE_ENV !== "production") {
44
+ if (!(config.statuses || config.headers)) {
45
+ throw new SerwistError("statuses-or-headers-required", {
46
+ moduleName: "@serwist/cacheable-response",
47
+ className: "CacheableResponse",
48
+ funcName: "constructor",
49
+ });
50
+ }
51
+
52
+ if (config.statuses) {
53
+ assert!.isArray(config.statuses, {
54
+ moduleName: "@serwist/cacheable-response",
55
+ className: "CacheableResponse",
56
+ funcName: "constructor",
57
+ paramName: "config.statuses",
58
+ });
59
+ }
60
+
61
+ if (config.headers) {
62
+ assert!.isType(config.headers, "object", {
63
+ moduleName: "@serwist/cacheable-response",
64
+ className: "CacheableResponse",
65
+ funcName: "constructor",
66
+ paramName: "config.headers",
67
+ });
68
+ }
69
+ }
70
+
71
+ this._statuses = config.statuses;
72
+ this._headers = config.headers;
73
+ }
74
+
75
+ /**
76
+ * Checks a response to see whether it's cacheable or not, based on this
77
+ * object's configuration.
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
+ cacheable = Object.keys(this._headers).some((headerName) => {
102
+ return response.headers.get(headerName) === this._headers![headerName];
103
+ });
104
+ }
105
+
106
+ if (process.env.NODE_ENV !== "production") {
107
+ if (!cacheable) {
108
+ logger.groupCollapsed(
109
+ `The request for '${getFriendlyURL(response.url)}' returned a response that does not meet the criteria for being cached.`,
110
+ );
111
+
112
+ logger.groupCollapsed("View cacheability criteria here.");
113
+ logger.log(`Cacheable statuses: ${JSON.stringify(this._statuses)}`);
114
+ logger.log(`Cacheable headers: ${JSON.stringify(this._headers, null, 2)}`);
115
+ logger.groupEnd();
116
+
117
+ const logFriendlyHeaders: { [key: string]: string } = {};
118
+ response.headers.forEach((value, key) => {
119
+ logFriendlyHeaders[key] = value;
120
+ });
121
+
122
+ logger.groupCollapsed("View response status and headers here.");
123
+ logger.log(`Response status: ${response.status}`);
124
+ logger.log(`Response headers: ${JSON.stringify(logFriendlyHeaders, null, 2)}`);
125
+ logger.groupEnd();
126
+
127
+ logger.groupCollapsed("View full response details here.");
128
+ logger.log(response.headers);
129
+ logger.log(response);
130
+ logger.groupEnd();
131
+
132
+ logger.groupEnd();
133
+ }
134
+ }
135
+
136
+ return cacheable;
137
+ }
138
+ }
@@ -0,0 +1,46 @@
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
+ }
@@ -1,5 +1,15 @@
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
+
1
9
  import type { CacheableResponseOptions } from "./CacheableResponse.js";
2
10
  import { CacheableResponse } from "./CacheableResponse.js";
3
11
  import { CacheableResponsePlugin } from "./CacheableResponsePlugin.js";
12
+
4
13
  export { CacheableResponse, CacheableResponsePlugin };
14
+
5
15
  export type { CacheableResponseOptions };
package/dist/index.cjs DELETED
@@ -1,132 +0,0 @@
1
- 'use strict';
2
-
3
- var internal = require('@serwist/core/internal');
4
-
5
- /**
6
- * This class allows you to set up rules determining what
7
- * status codes and/or headers need to be present in order for a
8
- * [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
9
- * to be considered cacheable.
10
- */ class CacheableResponse {
11
- _statuses;
12
- _headers;
13
- /**
14
- * To construct a new CacheableResponse instance you must provide at least
15
- * one of the `config` properties.
16
- *
17
- * If both `statuses` and `headers` are specified, then both conditions must
18
- * be met for the `Response` to be considered cacheable.
19
- *
20
- * @param config
21
- */ constructor(config = {}){
22
- if (process.env.NODE_ENV !== "production") {
23
- if (!(config.statuses || config.headers)) {
24
- throw new internal.SerwistError("statuses-or-headers-required", {
25
- moduleName: "@serwist/cacheable-response",
26
- className: "CacheableResponse",
27
- funcName: "constructor"
28
- });
29
- }
30
- if (config.statuses) {
31
- internal.assert.isArray(config.statuses, {
32
- moduleName: "@serwist/cacheable-response",
33
- className: "CacheableResponse",
34
- funcName: "constructor",
35
- paramName: "config.statuses"
36
- });
37
- }
38
- if (config.headers) {
39
- internal.assert.isType(config.headers, "object", {
40
- moduleName: "@serwist/cacheable-response",
41
- className: "CacheableResponse",
42
- funcName: "constructor",
43
- paramName: "config.headers"
44
- });
45
- }
46
- }
47
- this._statuses = config.statuses;
48
- this._headers = config.headers;
49
- }
50
- /**
51
- * Checks a response to see whether it's cacheable or not, based on this
52
- * object's configuration.
53
- *
54
- * @param response The response whose cacheability is being
55
- * checked.
56
- * @returns `true` if the `Response` is cacheable, and `false`
57
- * otherwise.
58
- */ isResponseCacheable(response) {
59
- if (process.env.NODE_ENV !== "production") {
60
- internal.assert.isInstance(response, Response, {
61
- moduleName: "@serwist/cacheable-response",
62
- className: "CacheableResponse",
63
- funcName: "isResponseCacheable",
64
- paramName: "response"
65
- });
66
- }
67
- let cacheable = true;
68
- if (this._statuses) {
69
- cacheable = this._statuses.includes(response.status);
70
- }
71
- if (this._headers && cacheable) {
72
- cacheable = Object.keys(this._headers).some((headerName)=>{
73
- return response.headers.get(headerName) === this._headers[headerName];
74
- });
75
- }
76
- if (process.env.NODE_ENV !== "production") {
77
- if (!cacheable) {
78
- internal.logger.groupCollapsed(`The request for '${internal.getFriendlyURL(response.url)}' returned a response that does not meet the criteria for being cached.`);
79
- internal.logger.groupCollapsed("View cacheability criteria here.");
80
- internal.logger.log(`Cacheable statuses: ${JSON.stringify(this._statuses)}`);
81
- internal.logger.log(`Cacheable headers: ${JSON.stringify(this._headers, null, 2)}`);
82
- internal.logger.groupEnd();
83
- const logFriendlyHeaders = {};
84
- response.headers.forEach((value, key)=>{
85
- logFriendlyHeaders[key] = value;
86
- });
87
- internal.logger.groupCollapsed("View response status and headers here.");
88
- internal.logger.log(`Response status: ${response.status}`);
89
- internal.logger.log(`Response headers: ${JSON.stringify(logFriendlyHeaders, null, 2)}`);
90
- internal.logger.groupEnd();
91
- internal.logger.groupCollapsed("View full response details here.");
92
- internal.logger.log(response.headers);
93
- internal.logger.log(response);
94
- internal.logger.groupEnd();
95
- internal.logger.groupEnd();
96
- }
97
- }
98
- return cacheable;
99
- }
100
- }
101
-
102
- /**
103
- * A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
104
- * easier to add in cacheability checks to requests made via Serwist's built-in
105
- * strategies.
106
- */ class CacheableResponsePlugin {
107
- _cacheableResponse;
108
- /**
109
- * To construct a new CacheableResponsePlugin instance you must provide at
110
- * least one of the `config` properties.
111
- *
112
- * If both `statuses` and `headers` are specified, then both conditions must
113
- * be met for the `Response` to be considered cacheable.
114
- *
115
- * @param config
116
- */ constructor(config){
117
- this._cacheableResponse = new CacheableResponse(config);
118
- }
119
- /**
120
- * @param options
121
- * @returns
122
- * @private
123
- */ cacheWillUpdate = async ({ response })=>{
124
- if (this._cacheableResponse.isResponseCacheable(response)) {
125
- return response;
126
- }
127
- return null;
128
- };
129
- }
130
-
131
- exports.CacheableResponse = CacheableResponse;
132
- exports.CacheableResponsePlugin = CacheableResponsePlugin;