@netlify/cache 1.8.0 → 1.8.2
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/main.cjs +39 -230
- package/dist/main.d.cts +10 -3
- package/dist/main.d.ts +10 -3
- package/dist/main.js +39 -231
- package/package.json +3 -6
package/dist/main.cjs
CHANGED
|
@@ -16,24 +16,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var __accessCheck = (obj, member, msg) => {
|
|
20
|
-
if (!member.has(obj))
|
|
21
|
-
throw TypeError("Cannot " + msg);
|
|
22
|
-
};
|
|
23
|
-
var __privateGet = (obj, member, getter) => {
|
|
24
|
-
__accessCheck(obj, member, "read from private field");
|
|
25
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
26
|
-
};
|
|
27
|
-
var __privateAdd = (obj, member, value) => {
|
|
28
|
-
if (member.has(obj))
|
|
29
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
30
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
31
|
-
};
|
|
32
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
33
|
-
__accessCheck(obj, member, "write to private field");
|
|
34
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
35
|
-
return value;
|
|
36
|
-
};
|
|
37
19
|
|
|
38
20
|
// src/main.ts
|
|
39
21
|
var main_exports = {};
|
|
@@ -50,238 +32,65 @@ __export(main_exports, {
|
|
|
50
32
|
});
|
|
51
33
|
module.exports = __toCommonJS(main_exports);
|
|
52
34
|
|
|
53
|
-
// src/
|
|
54
|
-
var
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
no_directive: "Responses must have a cache control header with caching directives (https://ntl.fyi/cache_api_no_directive).",
|
|
59
|
-
no_ttl: "Responses must have a cache control header with a `max-age` or `s-maxage` directive (https://ntl.fyi/cache_api_no_ttl).",
|
|
60
|
-
no_status: "Responses must specify a status code (https://ntl.fyi/cache_api_no_status).",
|
|
61
|
-
invalid_directive: "Responses must have a cache control header with caching directives (https://ntl.fyi/cache_api_invalid_directive).",
|
|
62
|
-
status: "Responses must have a status code between 200 and 299 (https://ntl.fyi/cache_api_status)."
|
|
63
|
-
};
|
|
64
|
-
var GENERIC_ERROR = "The server has returned an unexpected error (https://ntl.fyi/cache_api_error).";
|
|
65
|
-
|
|
66
|
-
// src/headers.ts
|
|
67
|
-
var CacheStatus = "cache-status";
|
|
68
|
-
var NetlifyCacheId = "netlify-cache-id";
|
|
69
|
-
var NetlifyCacheTag = "netlify-cache-tag";
|
|
70
|
-
var NetlifyCdnCacheControl = "netlify-cdn-cache-control";
|
|
71
|
-
var NetlifyVary = "netlify-vary";
|
|
72
|
-
var ErrorDetail = "netlify-programmable-error";
|
|
73
|
-
var ResourceHeaders = "netlify-programmable-headers";
|
|
74
|
-
var ResourceStatus = "netlify-programmable-status";
|
|
75
|
-
var ResourceStore = "netlify-programmable-store";
|
|
76
|
-
var NetlifyForwardedHost = "netlify-forwarded-host";
|
|
77
|
-
var UserAgent = "user-agent";
|
|
78
|
-
|
|
79
|
-
// src/bootstrap/cache.ts
|
|
80
|
-
var allowedProtocols = /* @__PURE__ */ new Set(["http:", "https:"]);
|
|
81
|
-
var discardedHeaders = /* @__PURE__ */ new Set(["cookie", "content-encoding", "content-length"]);
|
|
82
|
-
var getInternalHeaders = Symbol("getInternalHeaders");
|
|
83
|
-
var serializeResourceHeaders = Symbol("serializeResourceHeaders");
|
|
84
|
-
var _base64Encode, _getContext, _name, _userAgent;
|
|
85
|
-
var NetlifyCache = class {
|
|
86
|
-
constructor({ base64Encode, getContext, name, userAgent }) {
|
|
87
|
-
__privateAdd(this, _base64Encode, void 0);
|
|
88
|
-
__privateAdd(this, _getContext, void 0);
|
|
89
|
-
__privateAdd(this, _name, void 0);
|
|
90
|
-
__privateAdd(this, _userAgent, void 0);
|
|
91
|
-
__privateSet(this, _base64Encode, base64Encode);
|
|
92
|
-
__privateSet(this, _getContext, getContext);
|
|
93
|
-
__privateSet(this, _name, name);
|
|
94
|
-
__privateSet(this, _userAgent, userAgent);
|
|
95
|
-
}
|
|
96
|
-
[getInternalHeaders](requestContext) {
|
|
97
|
-
const { host, token } = requestContext;
|
|
98
|
-
const headers = {
|
|
99
|
-
Authorization: `Bearer ${token}`,
|
|
100
|
-
[ResourceStore]: __privateGet(this, _name)
|
|
101
|
-
};
|
|
102
|
-
if (host) {
|
|
103
|
-
headers[NetlifyForwardedHost] = host;
|
|
104
|
-
}
|
|
105
|
-
if (__privateGet(this, _userAgent)) {
|
|
106
|
-
headers[UserAgent] = __privateGet(this, _userAgent);
|
|
35
|
+
// src/polyfill.ts
|
|
36
|
+
var NetlifyCacheStorageProxy = class {
|
|
37
|
+
async delete(name) {
|
|
38
|
+
if (globalThis.caches) {
|
|
39
|
+
return globalThis.caches.delete(name);
|
|
107
40
|
}
|
|
108
|
-
return
|
|
41
|
+
return false;
|
|
109
42
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (discardedHeaders.has(key)) {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
if (key === "set-cookie") {
|
|
117
|
-
headersMap[key] = headersMap[key] || [];
|
|
118
|
-
headersMap[key].push(value);
|
|
119
|
-
} else {
|
|
120
|
-
headersMap[key] = value.split(",");
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
return __privateGet(this, _base64Encode).call(this, JSON.stringify(headersMap));
|
|
124
|
-
}
|
|
125
|
-
async add(request) {
|
|
126
|
-
await this.put(new Request(request), await fetch(request));
|
|
127
|
-
}
|
|
128
|
-
async addAll(requests) {
|
|
129
|
-
await Promise.allSettled(requests.map((request) => this.add(request)));
|
|
130
|
-
}
|
|
131
|
-
// eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
|
|
132
|
-
async delete(request) {
|
|
133
|
-
const context = __privateGet(this, _getContext).call(this, { operation: "delete" /* Delete */ });
|
|
134
|
-
if (context) {
|
|
135
|
-
const resourceURL = extractAndValidateURL(request);
|
|
136
|
-
await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
|
|
137
|
-
headers: this[getInternalHeaders](context),
|
|
138
|
-
method: "DELETE"
|
|
139
|
-
});
|
|
43
|
+
async has(name) {
|
|
44
|
+
if (globalThis.caches) {
|
|
45
|
+
return globalThis.caches.has(name);
|
|
140
46
|
}
|
|
141
|
-
return
|
|
142
|
-
}
|
|
143
|
-
// eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
|
|
144
|
-
async keys(_request) {
|
|
145
|
-
return [];
|
|
47
|
+
return false;
|
|
146
48
|
}
|
|
147
|
-
async
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (!context) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
const resourceURL = extractAndValidateURL(request);
|
|
154
|
-
const cacheURL = `${context.url}/${toCacheKey(resourceURL)}`;
|
|
155
|
-
const response = await fetch(cacheURL, {
|
|
156
|
-
headers: this[getInternalHeaders](context),
|
|
157
|
-
method: "GET"
|
|
158
|
-
});
|
|
159
|
-
if (!response.ok) {
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
return response;
|
|
163
|
-
} catch {
|
|
49
|
+
async keys() {
|
|
50
|
+
if (globalThis.caches) {
|
|
51
|
+
return globalThis.caches.keys();
|
|
164
52
|
}
|
|
53
|
+
return [];
|
|
165
54
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
return [];
|
|
55
|
+
async match(request, options) {
|
|
56
|
+
if (globalThis.caches) {
|
|
57
|
+
return globalThis.caches.match(request, options);
|
|
170
58
|
}
|
|
171
|
-
const res = await this.match(request);
|
|
172
|
-
return res ? [res] : [];
|
|
173
59
|
}
|
|
174
|
-
async
|
|
175
|
-
if (
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
if (request instanceof Request && request.method !== "GET") {
|
|
179
|
-
throw new TypeError(`Cannot cache response to ${request.method} request.`);
|
|
180
|
-
}
|
|
181
|
-
if (response.status === 206) {
|
|
182
|
-
throw new TypeError("Cannot cache response to a range request (206 Partial Content).");
|
|
183
|
-
}
|
|
184
|
-
if (response.headers.get("vary")?.includes("*")) {
|
|
185
|
-
throw new TypeError("Cannot cache response with 'Vary: *' header.");
|
|
186
|
-
}
|
|
187
|
-
const context = __privateGet(this, _getContext).call(this, { operation: "write" /* Write */ });
|
|
188
|
-
if (!context) {
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
const resourceURL = extractAndValidateURL(request);
|
|
192
|
-
const cacheResponse = await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
|
|
193
|
-
body: response.body,
|
|
194
|
-
headers: {
|
|
195
|
-
...this[getInternalHeaders](context),
|
|
196
|
-
[ResourceHeaders]: this[serializeResourceHeaders](response.headers),
|
|
197
|
-
[ResourceStatus]: response.status.toString()
|
|
198
|
-
},
|
|
199
|
-
// @ts-expect-error https://github.com/whatwg/fetch/pull/1457
|
|
200
|
-
duplex: "half",
|
|
201
|
-
method: "POST"
|
|
202
|
-
});
|
|
203
|
-
if (!cacheResponse.ok) {
|
|
204
|
-
const errorDetail = cacheResponse.headers.get(ErrorDetail) ?? "";
|
|
205
|
-
const errorMessage = ERROR_CODES[errorDetail] || GENERIC_ERROR;
|
|
206
|
-
context.logger?.(`Failed to write to the cache: ${errorMessage}`);
|
|
60
|
+
async open(cacheName) {
|
|
61
|
+
if (globalThis.caches) {
|
|
62
|
+
return globalThis.caches.open(cacheName);
|
|
207
63
|
}
|
|
64
|
+
return new NetlifyNoopCache();
|
|
208
65
|
}
|
|
209
66
|
};
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
_name = new WeakMap();
|
|
213
|
-
_userAgent = new WeakMap();
|
|
214
|
-
var extractAndValidateURL = (input) => {
|
|
215
|
-
let url;
|
|
216
|
-
if (input instanceof Request) {
|
|
217
|
-
url = new URL(input.url);
|
|
218
|
-
} else {
|
|
219
|
-
try {
|
|
220
|
-
url = new URL(String(input));
|
|
221
|
-
} catch {
|
|
222
|
-
throw new TypeError(`${input} is not a valid URL.`);
|
|
223
|
-
}
|
|
67
|
+
var NetlifyNoopCache = class {
|
|
68
|
+
async add(_) {
|
|
224
69
|
}
|
|
225
|
-
|
|
226
|
-
throw new TypeError(
|
|
227
|
-
`Cannot cache response for URL with unsupported protocol (${url.protocol}). Supported protocols are ${[
|
|
228
|
-
...allowedProtocols
|
|
229
|
-
].join(", ")}.`
|
|
230
|
-
);
|
|
70
|
+
async addAll(_) {
|
|
231
71
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
var toCacheKey = (url) => encodeURIComponent(url.toString());
|
|
235
|
-
|
|
236
|
-
// src/bootstrap/cachestorage.ts
|
|
237
|
-
var _environmentOptions, _stores;
|
|
238
|
-
var NetlifyCacheStorage = class {
|
|
239
|
-
constructor(environmentOptions) {
|
|
240
|
-
__privateAdd(this, _environmentOptions, void 0);
|
|
241
|
-
__privateAdd(this, _stores, void 0);
|
|
242
|
-
__privateSet(this, _environmentOptions, environmentOptions);
|
|
243
|
-
__privateSet(this, _stores, /* @__PURE__ */ new Map());
|
|
244
|
-
}
|
|
245
|
-
open(name) {
|
|
246
|
-
let store = __privateGet(this, _stores).get(name);
|
|
247
|
-
if (!store) {
|
|
248
|
-
store = new NetlifyCache({
|
|
249
|
-
...__privateGet(this, _environmentOptions),
|
|
250
|
-
name
|
|
251
|
-
});
|
|
252
|
-
__privateGet(this, _stores).set(name, store);
|
|
253
|
-
}
|
|
254
|
-
return Promise.resolve(store);
|
|
72
|
+
async delete(_) {
|
|
73
|
+
return true;
|
|
255
74
|
}
|
|
256
|
-
|
|
257
|
-
return
|
|
75
|
+
async keys(_) {
|
|
76
|
+
return [];
|
|
258
77
|
}
|
|
259
|
-
|
|
260
|
-
return Promise.resolve(__privateGet(this, _stores).delete(name));
|
|
78
|
+
async match(_) {
|
|
261
79
|
}
|
|
262
|
-
|
|
263
|
-
return
|
|
80
|
+
async matchAll(_) {
|
|
81
|
+
return [];
|
|
264
82
|
}
|
|
265
|
-
async
|
|
266
|
-
if (options?.cacheName) {
|
|
267
|
-
return __privateGet(this, _stores).get(options.cacheName)?.match(request);
|
|
268
|
-
}
|
|
269
|
-
for (const store of __privateGet(this, _stores).values()) {
|
|
270
|
-
const response = await store.match(request);
|
|
271
|
-
if (response === void 0) {
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
83
|
+
async put(_, __) {
|
|
275
84
|
}
|
|
276
85
|
};
|
|
277
|
-
|
|
278
|
-
_stores = new WeakMap();
|
|
86
|
+
var caches = new NetlifyCacheStorageProxy();
|
|
279
87
|
|
|
280
|
-
// src/
|
|
281
|
-
var
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
88
|
+
// src/headers.ts
|
|
89
|
+
var CacheStatus = "cache-status";
|
|
90
|
+
var NetlifyCacheId = "netlify-cache-id";
|
|
91
|
+
var NetlifyCacheTag = "netlify-cache-tag";
|
|
92
|
+
var NetlifyCdnCacheControl = "netlify-cdn-cache-control";
|
|
93
|
+
var NetlifyVary = "netlify-vary";
|
|
285
94
|
|
|
286
95
|
// src/cache-headers/validation.ts
|
|
287
96
|
var ensureArray = (value) => Array.isArray(value) ? value : [value];
|
package/dist/main.d.cts
CHANGED
|
@@ -2,10 +2,17 @@ import { N as NetlifyCache } from './cache-7af07baa.js';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Polyfill for local development environments where `globalThis.caches` is not
|
|
5
|
-
* available.
|
|
6
|
-
*
|
|
5
|
+
* available. It's a no-op cache. In production it will use the real one that
|
|
6
|
+
* has been set up in the environment by the bootstrap layer.
|
|
7
7
|
*/
|
|
8
|
-
declare
|
|
8
|
+
declare class NetlifyCacheStorageProxy implements CacheStorage {
|
|
9
|
+
delete(name: string): Promise<boolean>;
|
|
10
|
+
has(name: string): Promise<boolean>;
|
|
11
|
+
keys(): Promise<string[]>;
|
|
12
|
+
match(request: RequestInfo, options?: MultiCacheQueryOptions): Promise<Response | undefined>;
|
|
13
|
+
open(cacheName: string): Promise<Cache>;
|
|
14
|
+
}
|
|
15
|
+
declare const caches: NetlifyCacheStorageProxy;
|
|
9
16
|
|
|
10
17
|
interface CacheSettings {
|
|
11
18
|
/**
|
package/dist/main.d.ts
CHANGED
|
@@ -2,10 +2,17 @@ import { N as NetlifyCache } from './cache-7af07baa.js';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Polyfill for local development environments where `globalThis.caches` is not
|
|
5
|
-
* available.
|
|
6
|
-
*
|
|
5
|
+
* available. It's a no-op cache. In production it will use the real one that
|
|
6
|
+
* has been set up in the environment by the bootstrap layer.
|
|
7
7
|
*/
|
|
8
|
-
declare
|
|
8
|
+
declare class NetlifyCacheStorageProxy implements CacheStorage {
|
|
9
|
+
delete(name: string): Promise<boolean>;
|
|
10
|
+
has(name: string): Promise<boolean>;
|
|
11
|
+
keys(): Promise<string[]>;
|
|
12
|
+
match(request: RequestInfo, options?: MultiCacheQueryOptions): Promise<Response | undefined>;
|
|
13
|
+
open(cacheName: string): Promise<Cache>;
|
|
14
|
+
}
|
|
15
|
+
declare const caches: NetlifyCacheStorageProxy;
|
|
9
16
|
|
|
10
17
|
interface CacheSettings {
|
|
11
18
|
/**
|
package/dist/main.js
CHANGED
|
@@ -1,254 +1,62 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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/errors.ts
|
|
21
|
-
var ERROR_CODES = {
|
|
22
|
-
invalid_vary: "Responses must not use unsupported directives of the `Netlify-Vary` header (https://ntl.fyi/cache_api_invalid_vary).",
|
|
23
|
-
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).",
|
|
24
|
-
low_ttl: "Responses must have a cache control header with a `max-age` or `s-maxage` directive (https://ntl.fyi/cache_api_low_ttl).",
|
|
25
|
-
no_directive: "Responses must have a cache control header with caching directives (https://ntl.fyi/cache_api_no_directive).",
|
|
26
|
-
no_ttl: "Responses must have a cache control header with a `max-age` or `s-maxage` directive (https://ntl.fyi/cache_api_no_ttl).",
|
|
27
|
-
no_status: "Responses must specify a status code (https://ntl.fyi/cache_api_no_status).",
|
|
28
|
-
invalid_directive: "Responses must have a cache control header with caching directives (https://ntl.fyi/cache_api_invalid_directive).",
|
|
29
|
-
status: "Responses must have a status code between 200 and 299 (https://ntl.fyi/cache_api_status)."
|
|
30
|
-
};
|
|
31
|
-
var GENERIC_ERROR = "The server has returned an unexpected error (https://ntl.fyi/cache_api_error).";
|
|
32
|
-
|
|
33
|
-
// src/headers.ts
|
|
34
|
-
var CacheStatus = "cache-status";
|
|
35
|
-
var NetlifyCacheId = "netlify-cache-id";
|
|
36
|
-
var NetlifyCacheTag = "netlify-cache-tag";
|
|
37
|
-
var NetlifyCdnCacheControl = "netlify-cdn-cache-control";
|
|
38
|
-
var NetlifyVary = "netlify-vary";
|
|
39
|
-
var ErrorDetail = "netlify-programmable-error";
|
|
40
|
-
var ResourceHeaders = "netlify-programmable-headers";
|
|
41
|
-
var ResourceStatus = "netlify-programmable-status";
|
|
42
|
-
var ResourceStore = "netlify-programmable-store";
|
|
43
|
-
var NetlifyForwardedHost = "netlify-forwarded-host";
|
|
44
|
-
var UserAgent = "user-agent";
|
|
45
|
-
|
|
46
|
-
// src/bootstrap/cache.ts
|
|
47
|
-
var allowedProtocols = /* @__PURE__ */ new Set(["http:", "https:"]);
|
|
48
|
-
var discardedHeaders = /* @__PURE__ */ new Set(["cookie", "content-encoding", "content-length"]);
|
|
49
|
-
var getInternalHeaders = Symbol("getInternalHeaders");
|
|
50
|
-
var serializeResourceHeaders = Symbol("serializeResourceHeaders");
|
|
51
|
-
var _base64Encode, _getContext, _name, _userAgent;
|
|
52
|
-
var NetlifyCache = class {
|
|
53
|
-
constructor({ base64Encode, getContext, name, userAgent }) {
|
|
54
|
-
__privateAdd(this, _base64Encode, void 0);
|
|
55
|
-
__privateAdd(this, _getContext, void 0);
|
|
56
|
-
__privateAdd(this, _name, void 0);
|
|
57
|
-
__privateAdd(this, _userAgent, void 0);
|
|
58
|
-
__privateSet(this, _base64Encode, base64Encode);
|
|
59
|
-
__privateSet(this, _getContext, getContext);
|
|
60
|
-
__privateSet(this, _name, name);
|
|
61
|
-
__privateSet(this, _userAgent, userAgent);
|
|
62
|
-
}
|
|
63
|
-
[getInternalHeaders](requestContext) {
|
|
64
|
-
const { host, token } = requestContext;
|
|
65
|
-
const headers = {
|
|
66
|
-
Authorization: `Bearer ${token}`,
|
|
67
|
-
[ResourceStore]: __privateGet(this, _name)
|
|
68
|
-
};
|
|
69
|
-
if (host) {
|
|
70
|
-
headers[NetlifyForwardedHost] = host;
|
|
71
|
-
}
|
|
72
|
-
if (__privateGet(this, _userAgent)) {
|
|
73
|
-
headers[UserAgent] = __privateGet(this, _userAgent);
|
|
1
|
+
// src/polyfill.ts
|
|
2
|
+
var NetlifyCacheStorageProxy = class {
|
|
3
|
+
async delete(name) {
|
|
4
|
+
if (globalThis.caches) {
|
|
5
|
+
return globalThis.caches.delete(name);
|
|
74
6
|
}
|
|
75
|
-
return
|
|
7
|
+
return false;
|
|
76
8
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (discardedHeaders.has(key)) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
if (key === "set-cookie") {
|
|
84
|
-
headersMap[key] = headersMap[key] || [];
|
|
85
|
-
headersMap[key].push(value);
|
|
86
|
-
} else {
|
|
87
|
-
headersMap[key] = value.split(",");
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
return __privateGet(this, _base64Encode).call(this, JSON.stringify(headersMap));
|
|
91
|
-
}
|
|
92
|
-
async add(request) {
|
|
93
|
-
await this.put(new Request(request), await fetch(request));
|
|
94
|
-
}
|
|
95
|
-
async addAll(requests) {
|
|
96
|
-
await Promise.allSettled(requests.map((request) => this.add(request)));
|
|
97
|
-
}
|
|
98
|
-
// eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
|
|
99
|
-
async delete(request) {
|
|
100
|
-
const context = __privateGet(this, _getContext).call(this, { operation: "delete" /* Delete */ });
|
|
101
|
-
if (context) {
|
|
102
|
-
const resourceURL = extractAndValidateURL(request);
|
|
103
|
-
await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
|
|
104
|
-
headers: this[getInternalHeaders](context),
|
|
105
|
-
method: "DELETE"
|
|
106
|
-
});
|
|
9
|
+
async has(name) {
|
|
10
|
+
if (globalThis.caches) {
|
|
11
|
+
return globalThis.caches.has(name);
|
|
107
12
|
}
|
|
108
|
-
return
|
|
109
|
-
}
|
|
110
|
-
// eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
|
|
111
|
-
async keys(_request) {
|
|
112
|
-
return [];
|
|
13
|
+
return false;
|
|
113
14
|
}
|
|
114
|
-
async
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (!context) {
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
const resourceURL = extractAndValidateURL(request);
|
|
121
|
-
const cacheURL = `${context.url}/${toCacheKey(resourceURL)}`;
|
|
122
|
-
const response = await fetch(cacheURL, {
|
|
123
|
-
headers: this[getInternalHeaders](context),
|
|
124
|
-
method: "GET"
|
|
125
|
-
});
|
|
126
|
-
if (!response.ok) {
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
return response;
|
|
130
|
-
} catch {
|
|
15
|
+
async keys() {
|
|
16
|
+
if (globalThis.caches) {
|
|
17
|
+
return globalThis.caches.keys();
|
|
131
18
|
}
|
|
19
|
+
return [];
|
|
132
20
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return [];
|
|
21
|
+
async match(request, options) {
|
|
22
|
+
if (globalThis.caches) {
|
|
23
|
+
return globalThis.caches.match(request, options);
|
|
137
24
|
}
|
|
138
|
-
const res = await this.match(request);
|
|
139
|
-
return res ? [res] : [];
|
|
140
25
|
}
|
|
141
|
-
async
|
|
142
|
-
if (
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
if (request instanceof Request && request.method !== "GET") {
|
|
146
|
-
throw new TypeError(`Cannot cache response to ${request.method} request.`);
|
|
147
|
-
}
|
|
148
|
-
if (response.status === 206) {
|
|
149
|
-
throw new TypeError("Cannot cache response to a range request (206 Partial Content).");
|
|
150
|
-
}
|
|
151
|
-
if (response.headers.get("vary")?.includes("*")) {
|
|
152
|
-
throw new TypeError("Cannot cache response with 'Vary: *' header.");
|
|
153
|
-
}
|
|
154
|
-
const context = __privateGet(this, _getContext).call(this, { operation: "write" /* Write */ });
|
|
155
|
-
if (!context) {
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
const resourceURL = extractAndValidateURL(request);
|
|
159
|
-
const cacheResponse = await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
|
|
160
|
-
body: response.body,
|
|
161
|
-
headers: {
|
|
162
|
-
...this[getInternalHeaders](context),
|
|
163
|
-
[ResourceHeaders]: this[serializeResourceHeaders](response.headers),
|
|
164
|
-
[ResourceStatus]: response.status.toString()
|
|
165
|
-
},
|
|
166
|
-
// @ts-expect-error https://github.com/whatwg/fetch/pull/1457
|
|
167
|
-
duplex: "half",
|
|
168
|
-
method: "POST"
|
|
169
|
-
});
|
|
170
|
-
if (!cacheResponse.ok) {
|
|
171
|
-
const errorDetail = cacheResponse.headers.get(ErrorDetail) ?? "";
|
|
172
|
-
const errorMessage = ERROR_CODES[errorDetail] || GENERIC_ERROR;
|
|
173
|
-
context.logger?.(`Failed to write to the cache: ${errorMessage}`);
|
|
26
|
+
async open(cacheName) {
|
|
27
|
+
if (globalThis.caches) {
|
|
28
|
+
return globalThis.caches.open(cacheName);
|
|
174
29
|
}
|
|
30
|
+
return new NetlifyNoopCache();
|
|
175
31
|
}
|
|
176
32
|
};
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
_name = new WeakMap();
|
|
180
|
-
_userAgent = new WeakMap();
|
|
181
|
-
var extractAndValidateURL = (input) => {
|
|
182
|
-
let url;
|
|
183
|
-
if (input instanceof Request) {
|
|
184
|
-
url = new URL(input.url);
|
|
185
|
-
} else {
|
|
186
|
-
try {
|
|
187
|
-
url = new URL(String(input));
|
|
188
|
-
} catch {
|
|
189
|
-
throw new TypeError(`${input} is not a valid URL.`);
|
|
190
|
-
}
|
|
33
|
+
var NetlifyNoopCache = class {
|
|
34
|
+
async add(_) {
|
|
191
35
|
}
|
|
192
|
-
|
|
193
|
-
throw new TypeError(
|
|
194
|
-
`Cannot cache response for URL with unsupported protocol (${url.protocol}). Supported protocols are ${[
|
|
195
|
-
...allowedProtocols
|
|
196
|
-
].join(", ")}.`
|
|
197
|
-
);
|
|
36
|
+
async addAll(_) {
|
|
198
37
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
var toCacheKey = (url) => encodeURIComponent(url.toString());
|
|
202
|
-
|
|
203
|
-
// src/bootstrap/cachestorage.ts
|
|
204
|
-
var _environmentOptions, _stores;
|
|
205
|
-
var NetlifyCacheStorage = class {
|
|
206
|
-
constructor(environmentOptions) {
|
|
207
|
-
__privateAdd(this, _environmentOptions, void 0);
|
|
208
|
-
__privateAdd(this, _stores, void 0);
|
|
209
|
-
__privateSet(this, _environmentOptions, environmentOptions);
|
|
210
|
-
__privateSet(this, _stores, /* @__PURE__ */ new Map());
|
|
211
|
-
}
|
|
212
|
-
open(name) {
|
|
213
|
-
let store = __privateGet(this, _stores).get(name);
|
|
214
|
-
if (!store) {
|
|
215
|
-
store = new NetlifyCache({
|
|
216
|
-
...__privateGet(this, _environmentOptions),
|
|
217
|
-
name
|
|
218
|
-
});
|
|
219
|
-
__privateGet(this, _stores).set(name, store);
|
|
220
|
-
}
|
|
221
|
-
return Promise.resolve(store);
|
|
38
|
+
async delete(_) {
|
|
39
|
+
return true;
|
|
222
40
|
}
|
|
223
|
-
|
|
224
|
-
return
|
|
41
|
+
async keys(_) {
|
|
42
|
+
return [];
|
|
225
43
|
}
|
|
226
|
-
|
|
227
|
-
return Promise.resolve(__privateGet(this, _stores).delete(name));
|
|
44
|
+
async match(_) {
|
|
228
45
|
}
|
|
229
|
-
|
|
230
|
-
return
|
|
46
|
+
async matchAll(_) {
|
|
47
|
+
return [];
|
|
231
48
|
}
|
|
232
|
-
async
|
|
233
|
-
if (options?.cacheName) {
|
|
234
|
-
return __privateGet(this, _stores).get(options.cacheName)?.match(request);
|
|
235
|
-
}
|
|
236
|
-
for (const store of __privateGet(this, _stores).values()) {
|
|
237
|
-
const response = await store.match(request);
|
|
238
|
-
if (response === void 0) {
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
49
|
+
async put(_, __) {
|
|
242
50
|
}
|
|
243
51
|
};
|
|
244
|
-
|
|
245
|
-
_stores = new WeakMap();
|
|
52
|
+
var caches = new NetlifyCacheStorageProxy();
|
|
246
53
|
|
|
247
|
-
// src/
|
|
248
|
-
var
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
54
|
+
// src/headers.ts
|
|
55
|
+
var CacheStatus = "cache-status";
|
|
56
|
+
var NetlifyCacheId = "netlify-cache-id";
|
|
57
|
+
var NetlifyCacheTag = "netlify-cache-tag";
|
|
58
|
+
var NetlifyCdnCacheControl = "netlify-cdn-cache-control";
|
|
59
|
+
var NetlifyVary = "netlify-vary";
|
|
252
60
|
|
|
253
61
|
// src/cache-headers/validation.ts
|
|
254
62
|
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.8.
|
|
3
|
+
"version": "1.8.2",
|
|
4
4
|
"description": "TypeScript utilities for interacting with the Netlify cache",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -44,10 +44,8 @@
|
|
|
44
44
|
"dist/**/*"
|
|
45
45
|
],
|
|
46
46
|
"scripts": {
|
|
47
|
-
"build": "
|
|
48
|
-
"
|
|
49
|
-
"build:transpile": "node build.mjs",
|
|
50
|
-
"dev": "node build.mjs --watch",
|
|
47
|
+
"build": "tsup-node",
|
|
48
|
+
"dev": "tsup-node --watch",
|
|
51
49
|
"prepack": "npm run build",
|
|
52
50
|
"test": "run-s build test:dev",
|
|
53
51
|
"test:dev": "run-s build test:dev:*",
|
|
@@ -73,7 +71,6 @@
|
|
|
73
71
|
"semver": "^7.5.3",
|
|
74
72
|
"tmp-promise": "^3.0.3",
|
|
75
73
|
"tsup": "^7.2.0",
|
|
76
|
-
"typescript": "^5.0.0",
|
|
77
74
|
"vitest": "^0.34.0"
|
|
78
75
|
}
|
|
79
76
|
}
|