@netlify/cache 1.4.0 → 1.6.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.
@@ -39,11 +39,34 @@ var __privateSet = (obj, member, value, setter) => {
39
39
  var main_exports = {};
40
40
  __export(main_exports, {
41
41
  NetlifyCache: () => NetlifyCache,
42
- NetlifyCacheStorage: () => NetlifyCacheStorage
42
+ NetlifyCacheStorage: () => NetlifyCacheStorage,
43
+ Operation: () => Operation
43
44
  });
44
45
  module.exports = __toCommonJS(main_exports);
45
46
 
47
+ // src/bootstrap/environment.ts
48
+ var Operation = /* @__PURE__ */ ((Operation2) => {
49
+ Operation2["Delete"] = "delete";
50
+ Operation2["Read"] = "read";
51
+ Operation2["Write"] = "write";
52
+ return Operation2;
53
+ })(Operation || {});
54
+
55
+ // src/bootstrap/errors.ts
56
+ var ERROR_CODES = {
57
+ invalid_vary: "Responses must not use unsupported directives of the `Netlify-Vary` header (https://ntl.fyi/cache_api_invalid_vary).",
58
+ no_cache: "Responses must not set cache control headers with the `private`, `no-cache` or `no-store` directives (https://ntl.fyi/cache_api_no_cache).",
59
+ low_ttl: "Responses must have a cache control header with a `max-age` or `s-maxage` directive (https://ntl.fyi/cache_api_low_ttl).",
60
+ no_directive: "Responses must have a cache control header with caching directives (https://ntl.fyi/cache_api_no_directive).",
61
+ no_ttl: "Responses must have a cache control header with a `max-age` or `s-maxage` directive (https://ntl.fyi/cache_api_no_ttl).",
62
+ no_status: "Responses must specify a status code (https://ntl.fyi/cache_api_no_status).",
63
+ invalid_directive: "Responses must have a cache control header with caching directives (https://ntl.fyi/cache_api_invalid_directive).",
64
+ status: "Responses must have a status code between 200 and 299 (https://ntl.fyi/cache_api_status)."
65
+ };
66
+ var GENERIC_ERROR = "The server has returned an unexpected error (https://ntl.fyi/cache_api_error).";
67
+
46
68
  // src/headers.ts
69
+ var ErrorDetail = "netlify-programmable-error";
47
70
  var ResourceHeaders = "netlify-programmable-headers";
48
71
  var ResourceStatus = "netlify-programmable-status";
49
72
  var ResourceStore = "netlify-programmable-store";
@@ -104,12 +127,14 @@ var NetlifyCache = class {
104
127
  }
105
128
  // eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
106
129
  async delete(request) {
107
- const context = __privateGet(this, _getContext).call(this);
108
- const resourceURL = extractAndValidateURL(request);
109
- await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
110
- headers: this[getInternalHeaders](context),
111
- method: "DELETE"
112
- });
130
+ const context = __privateGet(this, _getContext).call(this, { operation: "delete" /* Delete */ });
131
+ if (context) {
132
+ const resourceURL = extractAndValidateURL(request);
133
+ await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
134
+ headers: this[getInternalHeaders](context),
135
+ method: "DELETE"
136
+ });
137
+ }
113
138
  return true;
114
139
  }
115
140
  // eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
@@ -118,7 +143,10 @@ var NetlifyCache = class {
118
143
  }
119
144
  async match(request) {
120
145
  try {
121
- const context = __privateGet(this, _getContext).call(this);
146
+ const context = __privateGet(this, _getContext).call(this, { operation: "read" /* Read */ });
147
+ if (!context) {
148
+ return;
149
+ }
122
150
  const resourceURL = extractAndValidateURL(request);
123
151
  const cacheURL = `${context.url}/${toCacheKey(resourceURL)}`;
124
152
  const response = await fetch(cacheURL, {
@@ -153,9 +181,12 @@ var NetlifyCache = class {
153
181
  if (response.headers.get("vary")?.includes("*")) {
154
182
  throw new TypeError("Cannot cache response with 'Vary: *' header.");
155
183
  }
156
- const context = __privateGet(this, _getContext).call(this);
184
+ const context = __privateGet(this, _getContext).call(this, { operation: "write" /* Write */ });
185
+ if (!context) {
186
+ return;
187
+ }
157
188
  const resourceURL = extractAndValidateURL(request);
158
- await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
189
+ const cacheResponse = await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
159
190
  body: response.body,
160
191
  headers: {
161
192
  ...this[getInternalHeaders](context),
@@ -166,6 +197,11 @@ var NetlifyCache = class {
166
197
  duplex: "half",
167
198
  method: "POST"
168
199
  });
200
+ if (!cacheResponse.ok) {
201
+ const errorDetail = cacheResponse.headers.get(ErrorDetail) ?? "";
202
+ const errorMessage = ERROR_CODES[errorDetail] || GENERIC_ERROR;
203
+ console.warn(`Failed to write to the cache: ${errorMessage}`);
204
+ }
169
205
  }
170
206
  };
171
207
  _base64Encode = new WeakMap();
@@ -240,5 +276,6 @@ _stores = new WeakMap();
240
276
  // Annotate the CommonJS export names for ESM import in node:
241
277
  0 && (module.exports = {
242
278
  NetlifyCache,
243
- NetlifyCacheStorage
279
+ NetlifyCacheStorage,
280
+ Operation
244
281
  });
@@ -1,5 +1,5 @@
1
- import { E as EnvironmentOptions } from '../cache-854474ad.js';
2
- export { B as Base64Encoder, N as NetlifyCache, R as RequestContextFactory } from '../cache-854474ad.js';
1
+ import { E as EnvironmentOptions } from '../cache-dc160ee0.js';
2
+ export { B as Base64Encoder, N as NetlifyCache, O as Operation, R as RequestContextFactory } from '../cache-dc160ee0.js';
3
3
 
4
4
  declare class NetlifyCacheStorage {
5
5
  #private;
@@ -1,5 +1,5 @@
1
- import { E as EnvironmentOptions } from '../cache-854474ad.js';
2
- export { B as Base64Encoder, N as NetlifyCache, R as RequestContextFactory } from '../cache-854474ad.js';
1
+ import { E as EnvironmentOptions } from '../cache-dc160ee0.js';
2
+ export { B as Base64Encoder, N as NetlifyCache, O as Operation, R as RequestContextFactory } from '../cache-dc160ee0.js';
3
3
 
4
4
  declare class NetlifyCacheStorage {
5
5
  #private;
@@ -1,13 +1,50 @@
1
- import {
2
- NetlifyForwardedHost,
3
- ResourceHeaders,
4
- ResourceStatus,
5
- ResourceStore,
6
- UserAgent,
7
- __privateAdd,
8
- __privateGet,
9
- __privateSet
10
- } from "../chunk-I5FZDZ6V.js";
1
+ var __accessCheck = (obj, member, msg) => {
2
+ if (!member.has(obj))
3
+ throw TypeError("Cannot " + msg);
4
+ };
5
+ var __privateGet = (obj, member, getter) => {
6
+ __accessCheck(obj, member, "read from private field");
7
+ return getter ? getter.call(obj) : member.get(obj);
8
+ };
9
+ var __privateAdd = (obj, member, value) => {
10
+ if (member.has(obj))
11
+ throw TypeError("Cannot add the same private member more than once");
12
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
13
+ };
14
+ var __privateSet = (obj, member, value, setter) => {
15
+ __accessCheck(obj, member, "write to private field");
16
+ setter ? setter.call(obj, value) : member.set(obj, value);
17
+ return value;
18
+ };
19
+
20
+ // src/bootstrap/environment.ts
21
+ var Operation = /* @__PURE__ */ ((Operation2) => {
22
+ Operation2["Delete"] = "delete";
23
+ Operation2["Read"] = "read";
24
+ Operation2["Write"] = "write";
25
+ return Operation2;
26
+ })(Operation || {});
27
+
28
+ // src/bootstrap/errors.ts
29
+ var ERROR_CODES = {
30
+ invalid_vary: "Responses must not use unsupported directives of the `Netlify-Vary` header (https://ntl.fyi/cache_api_invalid_vary).",
31
+ no_cache: "Responses must not set cache control headers with the `private`, `no-cache` or `no-store` directives (https://ntl.fyi/cache_api_no_cache).",
32
+ low_ttl: "Responses must have a cache control header with a `max-age` or `s-maxage` directive (https://ntl.fyi/cache_api_low_ttl).",
33
+ no_directive: "Responses must have a cache control header with caching directives (https://ntl.fyi/cache_api_no_directive).",
34
+ no_ttl: "Responses must have a cache control header with a `max-age` or `s-maxage` directive (https://ntl.fyi/cache_api_no_ttl).",
35
+ no_status: "Responses must specify a status code (https://ntl.fyi/cache_api_no_status).",
36
+ invalid_directive: "Responses must have a cache control header with caching directives (https://ntl.fyi/cache_api_invalid_directive).",
37
+ status: "Responses must have a status code between 200 and 299 (https://ntl.fyi/cache_api_status)."
38
+ };
39
+ var GENERIC_ERROR = "The server has returned an unexpected error (https://ntl.fyi/cache_api_error).";
40
+
41
+ // src/headers.ts
42
+ var ErrorDetail = "netlify-programmable-error";
43
+ var ResourceHeaders = "netlify-programmable-headers";
44
+ var ResourceStatus = "netlify-programmable-status";
45
+ var ResourceStore = "netlify-programmable-store";
46
+ var NetlifyForwardedHost = "netlify-forwarded-host";
47
+ var UserAgent = "user-agent";
11
48
 
12
49
  // src/bootstrap/cache.ts
13
50
  var allowedProtocols = /* @__PURE__ */ new Set(["http:", "https:"]);
@@ -63,12 +100,14 @@ var NetlifyCache = class {
63
100
  }
64
101
  // eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
65
102
  async delete(request) {
66
- const context = __privateGet(this, _getContext).call(this);
67
- const resourceURL = extractAndValidateURL(request);
68
- await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
69
- headers: this[getInternalHeaders](context),
70
- method: "DELETE"
71
- });
103
+ const context = __privateGet(this, _getContext).call(this, { operation: "delete" /* Delete */ });
104
+ if (context) {
105
+ const resourceURL = extractAndValidateURL(request);
106
+ await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
107
+ headers: this[getInternalHeaders](context),
108
+ method: "DELETE"
109
+ });
110
+ }
72
111
  return true;
73
112
  }
74
113
  // eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
@@ -77,7 +116,10 @@ var NetlifyCache = class {
77
116
  }
78
117
  async match(request) {
79
118
  try {
80
- const context = __privateGet(this, _getContext).call(this);
119
+ const context = __privateGet(this, _getContext).call(this, { operation: "read" /* Read */ });
120
+ if (!context) {
121
+ return;
122
+ }
81
123
  const resourceURL = extractAndValidateURL(request);
82
124
  const cacheURL = `${context.url}/${toCacheKey(resourceURL)}`;
83
125
  const response = await fetch(cacheURL, {
@@ -112,9 +154,12 @@ var NetlifyCache = class {
112
154
  if (response.headers.get("vary")?.includes("*")) {
113
155
  throw new TypeError("Cannot cache response with 'Vary: *' header.");
114
156
  }
115
- const context = __privateGet(this, _getContext).call(this);
157
+ const context = __privateGet(this, _getContext).call(this, { operation: "write" /* Write */ });
158
+ if (!context) {
159
+ return;
160
+ }
116
161
  const resourceURL = extractAndValidateURL(request);
117
- await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
162
+ const cacheResponse = await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
118
163
  body: response.body,
119
164
  headers: {
120
165
  ...this[getInternalHeaders](context),
@@ -125,6 +170,11 @@ var NetlifyCache = class {
125
170
  duplex: "half",
126
171
  method: "POST"
127
172
  });
173
+ if (!cacheResponse.ok) {
174
+ const errorDetail = cacheResponse.headers.get(ErrorDetail) ?? "";
175
+ const errorMessage = ERROR_CODES[errorDetail] || GENERIC_ERROR;
176
+ console.warn(`Failed to write to the cache: ${errorMessage}`);
177
+ }
128
178
  }
129
179
  };
130
180
  _base64Encode = new WeakMap();
@@ -198,5 +248,6 @@ _environmentOptions = new WeakMap();
198
248
  _stores = new WeakMap();
199
249
  export {
200
250
  NetlifyCache,
201
- NetlifyCacheStorage
251
+ NetlifyCacheStorage,
252
+ Operation
202
253
  };
@@ -4,7 +4,14 @@ interface EnvironmentOptions {
4
4
  getContext: RequestContextFactory;
5
5
  userAgent?: string;
6
6
  }
7
- type RequestContextFactory = () => RequestContext;
7
+ declare const enum Operation {
8
+ Delete = "delete",
9
+ Read = "read",
10
+ Write = "write"
11
+ }
12
+ type RequestContextFactory = (options: {
13
+ operation: Operation;
14
+ }) => RequestContext | null;
8
15
  interface RequestContext {
9
16
  host: string;
10
17
  token: string;
@@ -30,4 +37,4 @@ declare class NetlifyCache implements Cache {
30
37
  put(request: RequestInfo | URL | string, response: Response): Promise<void>;
31
38
  }
32
39
 
33
- export { Base64Encoder as B, EnvironmentOptions as E, NetlifyCache as N, RequestContextFactory as R };
40
+ export { Base64Encoder as B, EnvironmentOptions as E, NetlifyCache as N, Operation as O, RequestContextFactory as R };
package/dist/main.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { N as NetlifyCache } from './cache-854474ad.js';
1
+ import { N as NetlifyCache } from './cache-dc160ee0.js';
2
2
 
3
3
  interface CacheSettings {
4
4
  /**
package/dist/main.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { N as NetlifyCache } from './cache-854474ad.js';
1
+ import { N as NetlifyCache } from './cache-dc160ee0.js';
2
2
 
3
3
  interface CacheSettings {
4
4
  /**
package/dist/main.js CHANGED
@@ -1,10 +1,9 @@
1
- import {
2
- CacheStatus,
3
- NetlifyCacheId,
4
- NetlifyCacheTag,
5
- NetlifyCdnCacheControl,
6
- NetlifyVary
7
- } from "./chunk-I5FZDZ6V.js";
1
+ // src/headers.ts
2
+ var CacheStatus = "cache-status";
3
+ var NetlifyCacheId = "netlify-cache-id";
4
+ var NetlifyCacheTag = "netlify-cache-tag";
5
+ var NetlifyCdnCacheControl = "netlify-cdn-cache-control";
6
+ var NetlifyVary = "netlify-vary";
8
7
 
9
8
  // src/cache-headers/validation.ts
10
9
  var ensureArray = (value) => Array.isArray(value) ? value : [value];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/cache",
3
- "version": "1.4.0",
3
+ "version": "1.6.0",
4
4
  "description": "TypeScript utilities for interacting with the Netlify cache",
5
5
  "type": "module",
6
6
  "engines": {
@@ -1,46 +0,0 @@
1
- var __accessCheck = (obj, member, msg) => {
2
- if (!member.has(obj))
3
- throw TypeError("Cannot " + msg);
4
- };
5
- var __privateGet = (obj, member, getter) => {
6
- __accessCheck(obj, member, "read from private field");
7
- return getter ? getter.call(obj) : member.get(obj);
8
- };
9
- var __privateAdd = (obj, member, value) => {
10
- if (member.has(obj))
11
- throw TypeError("Cannot add the same private member more than once");
12
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
13
- };
14
- var __privateSet = (obj, member, value, setter) => {
15
- __accessCheck(obj, member, "write to private field");
16
- setter ? setter.call(obj, value) : member.set(obj, value);
17
- return value;
18
- };
19
-
20
- // src/headers.ts
21
- var CacheStatus = "cache-status";
22
- var NetlifyCacheId = "netlify-cache-id";
23
- var NetlifyCacheTag = "netlify-cache-tag";
24
- var NetlifyCdnCacheControl = "netlify-cdn-cache-control";
25
- var NetlifyVary = "netlify-vary";
26
- var ResourceHeaders = "netlify-programmable-headers";
27
- var ResourceStatus = "netlify-programmable-status";
28
- var ResourceStore = "netlify-programmable-store";
29
- var NetlifyForwardedHost = "netlify-forwarded-host";
30
- var UserAgent = "user-agent";
31
-
32
- export {
33
- __privateGet,
34
- __privateAdd,
35
- __privateSet,
36
- CacheStatus,
37
- NetlifyCacheId,
38
- NetlifyCacheTag,
39
- NetlifyCdnCacheControl,
40
- NetlifyVary,
41
- ResourceHeaders,
42
- ResourceStatus,
43
- ResourceStore,
44
- NetlifyForwardedHost,
45
- UserAgent
46
- };