@netlify/cache 1.1.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/LICENSE +22 -0
- package/README.md +14 -0
- package/dist/bootstrap/main.cjs +225 -0
- package/dist/bootstrap/main.d.cts +37 -0
- package/dist/bootstrap/main.d.ts +37 -0
- package/dist/bootstrap/main.js +198 -0
- package/dist/main.cjs +18 -0
- package/dist/main.d.cts +2 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +0 -0
- package/package.json +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2022 Netlify <team@netlify.com>
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
[](https://github.com/netlify/cache/actions)
|
|
2
|
+
[](https://www.npmjs.com/package/@netlify/cache)
|
|
3
|
+
|
|
4
|
+
# @netlify/cache
|
|
5
|
+
|
|
6
|
+
TypeScript utilities for interacting with the Netlify cache.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
You can install `@netlify/cache` via npm:
|
|
11
|
+
|
|
12
|
+
```shell
|
|
13
|
+
npm install @netlify/cache
|
|
14
|
+
```
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
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
|
+
|
|
38
|
+
// src/bootstrap/main.ts
|
|
39
|
+
var main_exports = {};
|
|
40
|
+
__export(main_exports, {
|
|
41
|
+
NetlifyCache: () => NetlifyCache,
|
|
42
|
+
NetlifyCacheStorage: () => NetlifyCacheStorage
|
|
43
|
+
});
|
|
44
|
+
module.exports = __toCommonJS(main_exports);
|
|
45
|
+
|
|
46
|
+
// src/bootstrap/cache.ts
|
|
47
|
+
var import_node_buffer = require("buffer");
|
|
48
|
+
var HEADERS_HEADER = "Netlify-Programmable-Headers";
|
|
49
|
+
var STATUS_HEADER = "Netlify-Programmable-Status";
|
|
50
|
+
var STORE_HEADER = "Netlify-Programmable-Store";
|
|
51
|
+
var allowedProtocols = /* @__PURE__ */ new Set(["http:", "https:"]);
|
|
52
|
+
var discardedHeaders = /* @__PURE__ */ new Set(["cookie", "content-encoding", "content-length"]);
|
|
53
|
+
var _getToken, _getURL, _name;
|
|
54
|
+
var NetlifyCache = class {
|
|
55
|
+
constructor({ getToken, getURL, name }) {
|
|
56
|
+
__privateAdd(this, _getToken, void 0);
|
|
57
|
+
__privateAdd(this, _getURL, void 0);
|
|
58
|
+
__privateAdd(this, _name, void 0);
|
|
59
|
+
__privateSet(this, _getToken, getToken);
|
|
60
|
+
__privateSet(this, _getURL, getURL);
|
|
61
|
+
__privateSet(this, _name, name);
|
|
62
|
+
}
|
|
63
|
+
async add(request) {
|
|
64
|
+
await this.put(new Request(request), await fetch(request));
|
|
65
|
+
}
|
|
66
|
+
async addAll(requests) {
|
|
67
|
+
await Promise.allSettled(requests.map((request) => this.add(request)));
|
|
68
|
+
}
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
70
|
+
async matchAll(request, _options) {
|
|
71
|
+
if (!request) {
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
const res = await this.match(request);
|
|
75
|
+
return res ? [res] : [];
|
|
76
|
+
}
|
|
77
|
+
async put(request, response) {
|
|
78
|
+
if (!response.ok) {
|
|
79
|
+
throw new TypeError(`Cannot cache response with status ${response.status}.`);
|
|
80
|
+
}
|
|
81
|
+
if (request instanceof Request && request.method !== "GET") {
|
|
82
|
+
throw new TypeError(`Cannot cache response to ${request.method} request.`);
|
|
83
|
+
}
|
|
84
|
+
if (response.status === 206) {
|
|
85
|
+
throw new TypeError("Cannot cache response to a range request (206 Partial Content).");
|
|
86
|
+
}
|
|
87
|
+
if (response.headers.get("vary")?.includes("*")) {
|
|
88
|
+
throw new TypeError("Cannot cache response with 'Vary: *' header.");
|
|
89
|
+
}
|
|
90
|
+
const resourceURL = extractAndValidateURL(request);
|
|
91
|
+
await fetch(`${__privateGet(this, _getURL).call(this)}/${toCacheKey(resourceURL)}`, {
|
|
92
|
+
body: response.body,
|
|
93
|
+
headers: {
|
|
94
|
+
Authorization: `Bearer ${__privateGet(this, _getToken).call(this)}`,
|
|
95
|
+
[HEADERS_HEADER]: getEncodedHeaders(response.headers),
|
|
96
|
+
[STATUS_HEADER]: response.status.toString(),
|
|
97
|
+
[STORE_HEADER]: __privateGet(this, _name)
|
|
98
|
+
},
|
|
99
|
+
// @ts-expect-error https://github.com/whatwg/fetch/pull/1457
|
|
100
|
+
duplex: "half",
|
|
101
|
+
method: "POST"
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
async match(request) {
|
|
105
|
+
try {
|
|
106
|
+
const resourceURL = extractAndValidateURL(request);
|
|
107
|
+
const cacheURL = `${__privateGet(this, _getURL).call(this)}/${toCacheKey(resourceURL)}`;
|
|
108
|
+
const response = await fetch(cacheURL, {
|
|
109
|
+
headers: {
|
|
110
|
+
Authorization: `Bearer ${__privateGet(this, _getToken).call(this)}`
|
|
111
|
+
},
|
|
112
|
+
method: "GET"
|
|
113
|
+
});
|
|
114
|
+
if (!response.ok) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
return response;
|
|
118
|
+
} catch {
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
// eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
|
|
122
|
+
async delete(request) {
|
|
123
|
+
const resourceURL = extractAndValidateURL(request);
|
|
124
|
+
await fetch(`${__privateGet(this, _getURL).call(this)}/${toCacheKey(resourceURL)}`, {
|
|
125
|
+
headers: {
|
|
126
|
+
Authorization: `Bearer ${__privateGet(this, _getToken).call(this)}`,
|
|
127
|
+
[STORE_HEADER]: __privateGet(this, _name)
|
|
128
|
+
},
|
|
129
|
+
method: "DELETE"
|
|
130
|
+
});
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
// eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
|
|
134
|
+
async keys(_request) {
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
_getToken = new WeakMap();
|
|
139
|
+
_getURL = new WeakMap();
|
|
140
|
+
_name = new WeakMap();
|
|
141
|
+
var getEncodedHeaders = (headers) => {
|
|
142
|
+
const headersMap = {};
|
|
143
|
+
headers.forEach((value, key) => {
|
|
144
|
+
if (discardedHeaders.has(key)) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (key === "set-cookie") {
|
|
148
|
+
headersMap[key] = headersMap[key] || [];
|
|
149
|
+
headersMap[key].push(value);
|
|
150
|
+
} else {
|
|
151
|
+
headersMap[key] = value.split(",");
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
return import_node_buffer.Buffer.from(JSON.stringify(headersMap), "utf8").toString("base64");
|
|
155
|
+
};
|
|
156
|
+
var extractAndValidateURL = (input) => {
|
|
157
|
+
let url;
|
|
158
|
+
if (input instanceof Request) {
|
|
159
|
+
url = new URL(input.url);
|
|
160
|
+
} else {
|
|
161
|
+
try {
|
|
162
|
+
url = new URL(String(input));
|
|
163
|
+
} catch {
|
|
164
|
+
throw new TypeError(`${input} is not a valid URL.`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (!allowedProtocols.has(url.protocol)) {
|
|
168
|
+
throw new TypeError(
|
|
169
|
+
`Cannot cache response for URL with unsupported protocol (${url.protocol}). Supported protocols are ${[
|
|
170
|
+
...allowedProtocols
|
|
171
|
+
].join(", ")}.`
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
return url;
|
|
175
|
+
};
|
|
176
|
+
var toCacheKey = (url) => encodeURIComponent(url.toString());
|
|
177
|
+
|
|
178
|
+
// src/bootstrap/cachestorage.ts
|
|
179
|
+
var _getToken2, _getURL2, _stores;
|
|
180
|
+
var NetlifyCacheStorage = class {
|
|
181
|
+
constructor({ getToken, getURL }) {
|
|
182
|
+
__privateAdd(this, _getToken2, void 0);
|
|
183
|
+
__privateAdd(this, _getURL2, void 0);
|
|
184
|
+
__privateAdd(this, _stores, void 0);
|
|
185
|
+
__privateSet(this, _getToken2, getToken);
|
|
186
|
+
__privateSet(this, _getURL2, getURL);
|
|
187
|
+
__privateSet(this, _stores, /* @__PURE__ */ new Map());
|
|
188
|
+
}
|
|
189
|
+
open(name) {
|
|
190
|
+
let store = __privateGet(this, _stores).get(name);
|
|
191
|
+
if (!store) {
|
|
192
|
+
store = new NetlifyCache({ getToken: __privateGet(this, _getToken2), getURL: __privateGet(this, _getURL2), name });
|
|
193
|
+
__privateGet(this, _stores).set(name, store);
|
|
194
|
+
}
|
|
195
|
+
return Promise.resolve(store);
|
|
196
|
+
}
|
|
197
|
+
has(name) {
|
|
198
|
+
return Promise.resolve(__privateGet(this, _stores).has(name));
|
|
199
|
+
}
|
|
200
|
+
delete(name) {
|
|
201
|
+
return Promise.resolve(__privateGet(this, _stores).delete(name));
|
|
202
|
+
}
|
|
203
|
+
keys() {
|
|
204
|
+
return Promise.resolve([...__privateGet(this, _stores).keys()]);
|
|
205
|
+
}
|
|
206
|
+
async match(request, options) {
|
|
207
|
+
if (options?.cacheName) {
|
|
208
|
+
return __privateGet(this, _stores).get(options.cacheName)?.match(request);
|
|
209
|
+
}
|
|
210
|
+
for (const store of __privateGet(this, _stores).values()) {
|
|
211
|
+
const response = await store.match(request);
|
|
212
|
+
if (response === void 0) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
_getToken2 = new WeakMap();
|
|
219
|
+
_getURL2 = new WeakMap();
|
|
220
|
+
_stores = new WeakMap();
|
|
221
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
222
|
+
0 && (module.exports = {
|
|
223
|
+
NetlifyCache,
|
|
224
|
+
NetlifyCacheStorage
|
|
225
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
type Factory<T> = () => T;
|
|
2
|
+
|
|
3
|
+
interface NetlifyCacheOptions {
|
|
4
|
+
getToken: Factory<string>;
|
|
5
|
+
getURL: Factory<string>;
|
|
6
|
+
name: string;
|
|
7
|
+
}
|
|
8
|
+
declare class NetlifyCache implements Cache {
|
|
9
|
+
#private;
|
|
10
|
+
constructor({ getToken, getURL, name }: NetlifyCacheOptions);
|
|
11
|
+
add(request: RequestInfo): Promise<void>;
|
|
12
|
+
addAll(requests: RequestInfo[]): Promise<void>;
|
|
13
|
+
matchAll(request?: RequestInfo, _options?: CacheQueryOptions): Promise<readonly Response[]>;
|
|
14
|
+
put(request: RequestInfo | URL | string, response: Response): Promise<void>;
|
|
15
|
+
match(request: RequestInfo): Promise<Response | undefined>;
|
|
16
|
+
delete(request: RequestInfo): Promise<boolean>;
|
|
17
|
+
keys(_request?: Request): Promise<never[]>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface NetlifyCacheStorageOptions {
|
|
21
|
+
getToken: Factory<string>;
|
|
22
|
+
getURL: Factory<string>;
|
|
23
|
+
}
|
|
24
|
+
declare class NetlifyCacheStorage {
|
|
25
|
+
#private;
|
|
26
|
+
constructor({ getToken, getURL }: NetlifyCacheStorageOptions);
|
|
27
|
+
open(name: string): Promise<Cache>;
|
|
28
|
+
has(name: string): Promise<boolean>;
|
|
29
|
+
delete(name: string): Promise<boolean>;
|
|
30
|
+
keys(): Promise<string[]>;
|
|
31
|
+
match(request: RequestInfo, options?: MultiCacheQueryOptions): Promise<Response | undefined>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type TokenFactory = Factory<string>;
|
|
35
|
+
type URLFactory = Factory<string>;
|
|
36
|
+
|
|
37
|
+
export { NetlifyCache, NetlifyCacheStorage, TokenFactory, URLFactory };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
type Factory<T> = () => T;
|
|
2
|
+
|
|
3
|
+
interface NetlifyCacheOptions {
|
|
4
|
+
getToken: Factory<string>;
|
|
5
|
+
getURL: Factory<string>;
|
|
6
|
+
name: string;
|
|
7
|
+
}
|
|
8
|
+
declare class NetlifyCache implements Cache {
|
|
9
|
+
#private;
|
|
10
|
+
constructor({ getToken, getURL, name }: NetlifyCacheOptions);
|
|
11
|
+
add(request: RequestInfo): Promise<void>;
|
|
12
|
+
addAll(requests: RequestInfo[]): Promise<void>;
|
|
13
|
+
matchAll(request?: RequestInfo, _options?: CacheQueryOptions): Promise<readonly Response[]>;
|
|
14
|
+
put(request: RequestInfo | URL | string, response: Response): Promise<void>;
|
|
15
|
+
match(request: RequestInfo): Promise<Response | undefined>;
|
|
16
|
+
delete(request: RequestInfo): Promise<boolean>;
|
|
17
|
+
keys(_request?: Request): Promise<never[]>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface NetlifyCacheStorageOptions {
|
|
21
|
+
getToken: Factory<string>;
|
|
22
|
+
getURL: Factory<string>;
|
|
23
|
+
}
|
|
24
|
+
declare class NetlifyCacheStorage {
|
|
25
|
+
#private;
|
|
26
|
+
constructor({ getToken, getURL }: NetlifyCacheStorageOptions);
|
|
27
|
+
open(name: string): Promise<Cache>;
|
|
28
|
+
has(name: string): Promise<boolean>;
|
|
29
|
+
delete(name: string): Promise<boolean>;
|
|
30
|
+
keys(): Promise<string[]>;
|
|
31
|
+
match(request: RequestInfo, options?: MultiCacheQueryOptions): Promise<Response | undefined>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type TokenFactory = Factory<string>;
|
|
35
|
+
type URLFactory = Factory<string>;
|
|
36
|
+
|
|
37
|
+
export { NetlifyCache, NetlifyCacheStorage, TokenFactory, URLFactory };
|
|
@@ -0,0 +1,198 @@
|
|
|
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/cache.ts
|
|
21
|
+
import { Buffer as Buffer2 } from "buffer";
|
|
22
|
+
var HEADERS_HEADER = "Netlify-Programmable-Headers";
|
|
23
|
+
var STATUS_HEADER = "Netlify-Programmable-Status";
|
|
24
|
+
var STORE_HEADER = "Netlify-Programmable-Store";
|
|
25
|
+
var allowedProtocols = /* @__PURE__ */ new Set(["http:", "https:"]);
|
|
26
|
+
var discardedHeaders = /* @__PURE__ */ new Set(["cookie", "content-encoding", "content-length"]);
|
|
27
|
+
var _getToken, _getURL, _name;
|
|
28
|
+
var NetlifyCache = class {
|
|
29
|
+
constructor({ getToken, getURL, name }) {
|
|
30
|
+
__privateAdd(this, _getToken, void 0);
|
|
31
|
+
__privateAdd(this, _getURL, void 0);
|
|
32
|
+
__privateAdd(this, _name, void 0);
|
|
33
|
+
__privateSet(this, _getToken, getToken);
|
|
34
|
+
__privateSet(this, _getURL, getURL);
|
|
35
|
+
__privateSet(this, _name, name);
|
|
36
|
+
}
|
|
37
|
+
async add(request) {
|
|
38
|
+
await this.put(new Request(request), await fetch(request));
|
|
39
|
+
}
|
|
40
|
+
async addAll(requests) {
|
|
41
|
+
await Promise.allSettled(requests.map((request) => this.add(request)));
|
|
42
|
+
}
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
44
|
+
async matchAll(request, _options) {
|
|
45
|
+
if (!request) {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
const res = await this.match(request);
|
|
49
|
+
return res ? [res] : [];
|
|
50
|
+
}
|
|
51
|
+
async put(request, response) {
|
|
52
|
+
if (!response.ok) {
|
|
53
|
+
throw new TypeError(`Cannot cache response with status ${response.status}.`);
|
|
54
|
+
}
|
|
55
|
+
if (request instanceof Request && request.method !== "GET") {
|
|
56
|
+
throw new TypeError(`Cannot cache response to ${request.method} request.`);
|
|
57
|
+
}
|
|
58
|
+
if (response.status === 206) {
|
|
59
|
+
throw new TypeError("Cannot cache response to a range request (206 Partial Content).");
|
|
60
|
+
}
|
|
61
|
+
if (response.headers.get("vary")?.includes("*")) {
|
|
62
|
+
throw new TypeError("Cannot cache response with 'Vary: *' header.");
|
|
63
|
+
}
|
|
64
|
+
const resourceURL = extractAndValidateURL(request);
|
|
65
|
+
await fetch(`${__privateGet(this, _getURL).call(this)}/${toCacheKey(resourceURL)}`, {
|
|
66
|
+
body: response.body,
|
|
67
|
+
headers: {
|
|
68
|
+
Authorization: `Bearer ${__privateGet(this, _getToken).call(this)}`,
|
|
69
|
+
[HEADERS_HEADER]: getEncodedHeaders(response.headers),
|
|
70
|
+
[STATUS_HEADER]: response.status.toString(),
|
|
71
|
+
[STORE_HEADER]: __privateGet(this, _name)
|
|
72
|
+
},
|
|
73
|
+
// @ts-expect-error https://github.com/whatwg/fetch/pull/1457
|
|
74
|
+
duplex: "half",
|
|
75
|
+
method: "POST"
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async match(request) {
|
|
79
|
+
try {
|
|
80
|
+
const resourceURL = extractAndValidateURL(request);
|
|
81
|
+
const cacheURL = `${__privateGet(this, _getURL).call(this)}/${toCacheKey(resourceURL)}`;
|
|
82
|
+
const response = await fetch(cacheURL, {
|
|
83
|
+
headers: {
|
|
84
|
+
Authorization: `Bearer ${__privateGet(this, _getToken).call(this)}`
|
|
85
|
+
},
|
|
86
|
+
method: "GET"
|
|
87
|
+
});
|
|
88
|
+
if (!response.ok) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
return response;
|
|
92
|
+
} catch {
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
|
|
96
|
+
async delete(request) {
|
|
97
|
+
const resourceURL = extractAndValidateURL(request);
|
|
98
|
+
await fetch(`${__privateGet(this, _getURL).call(this)}/${toCacheKey(resourceURL)}`, {
|
|
99
|
+
headers: {
|
|
100
|
+
Authorization: `Bearer ${__privateGet(this, _getToken).call(this)}`,
|
|
101
|
+
[STORE_HEADER]: __privateGet(this, _name)
|
|
102
|
+
},
|
|
103
|
+
method: "DELETE"
|
|
104
|
+
});
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
// eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
|
|
108
|
+
async keys(_request) {
|
|
109
|
+
return [];
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
_getToken = new WeakMap();
|
|
113
|
+
_getURL = new WeakMap();
|
|
114
|
+
_name = new WeakMap();
|
|
115
|
+
var getEncodedHeaders = (headers) => {
|
|
116
|
+
const headersMap = {};
|
|
117
|
+
headers.forEach((value, key) => {
|
|
118
|
+
if (discardedHeaders.has(key)) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (key === "set-cookie") {
|
|
122
|
+
headersMap[key] = headersMap[key] || [];
|
|
123
|
+
headersMap[key].push(value);
|
|
124
|
+
} else {
|
|
125
|
+
headersMap[key] = value.split(",");
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
return Buffer2.from(JSON.stringify(headersMap), "utf8").toString("base64");
|
|
129
|
+
};
|
|
130
|
+
var extractAndValidateURL = (input) => {
|
|
131
|
+
let url;
|
|
132
|
+
if (input instanceof Request) {
|
|
133
|
+
url = new URL(input.url);
|
|
134
|
+
} else {
|
|
135
|
+
try {
|
|
136
|
+
url = new URL(String(input));
|
|
137
|
+
} catch {
|
|
138
|
+
throw new TypeError(`${input} is not a valid URL.`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (!allowedProtocols.has(url.protocol)) {
|
|
142
|
+
throw new TypeError(
|
|
143
|
+
`Cannot cache response for URL with unsupported protocol (${url.protocol}). Supported protocols are ${[
|
|
144
|
+
...allowedProtocols
|
|
145
|
+
].join(", ")}.`
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
return url;
|
|
149
|
+
};
|
|
150
|
+
var toCacheKey = (url) => encodeURIComponent(url.toString());
|
|
151
|
+
|
|
152
|
+
// src/bootstrap/cachestorage.ts
|
|
153
|
+
var _getToken2, _getURL2, _stores;
|
|
154
|
+
var NetlifyCacheStorage = class {
|
|
155
|
+
constructor({ getToken, getURL }) {
|
|
156
|
+
__privateAdd(this, _getToken2, void 0);
|
|
157
|
+
__privateAdd(this, _getURL2, void 0);
|
|
158
|
+
__privateAdd(this, _stores, void 0);
|
|
159
|
+
__privateSet(this, _getToken2, getToken);
|
|
160
|
+
__privateSet(this, _getURL2, getURL);
|
|
161
|
+
__privateSet(this, _stores, /* @__PURE__ */ new Map());
|
|
162
|
+
}
|
|
163
|
+
open(name) {
|
|
164
|
+
let store = __privateGet(this, _stores).get(name);
|
|
165
|
+
if (!store) {
|
|
166
|
+
store = new NetlifyCache({ getToken: __privateGet(this, _getToken2), getURL: __privateGet(this, _getURL2), name });
|
|
167
|
+
__privateGet(this, _stores).set(name, store);
|
|
168
|
+
}
|
|
169
|
+
return Promise.resolve(store);
|
|
170
|
+
}
|
|
171
|
+
has(name) {
|
|
172
|
+
return Promise.resolve(__privateGet(this, _stores).has(name));
|
|
173
|
+
}
|
|
174
|
+
delete(name) {
|
|
175
|
+
return Promise.resolve(__privateGet(this, _stores).delete(name));
|
|
176
|
+
}
|
|
177
|
+
keys() {
|
|
178
|
+
return Promise.resolve([...__privateGet(this, _stores).keys()]);
|
|
179
|
+
}
|
|
180
|
+
async match(request, options) {
|
|
181
|
+
if (options?.cacheName) {
|
|
182
|
+
return __privateGet(this, _stores).get(options.cacheName)?.match(request);
|
|
183
|
+
}
|
|
184
|
+
for (const store of __privateGet(this, _stores).values()) {
|
|
185
|
+
const response = await store.match(request);
|
|
186
|
+
if (response === void 0) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
_getToken2 = new WeakMap();
|
|
193
|
+
_getURL2 = new WeakMap();
|
|
194
|
+
_stores = new WeakMap();
|
|
195
|
+
export {
|
|
196
|
+
NetlifyCache,
|
|
197
|
+
NetlifyCacheStorage
|
|
198
|
+
};
|
package/dist/main.cjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// src/main.ts
|
|
17
|
+
var main_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(main_exports);
|
package/dist/main.d.cts
ADDED
package/dist/main.d.ts
ADDED
package/dist/main.js
ADDED
|
File without changes
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@netlify/cache",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "TypeScript utilities for interacting with the Netlify cache",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": "^14.16.0 || >=16.0.0"
|
|
8
|
+
},
|
|
9
|
+
"main": "./dist/main.cjs",
|
|
10
|
+
"module": "./dist/main.js",
|
|
11
|
+
"types": "./dist/main.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/main.d.cts",
|
|
16
|
+
"default": "./dist/main.cjs"
|
|
17
|
+
},
|
|
18
|
+
"import": {
|
|
19
|
+
"types": "./dist/main.d.ts",
|
|
20
|
+
"default": "./dist/main.js"
|
|
21
|
+
},
|
|
22
|
+
"default": {
|
|
23
|
+
"types": "./dist/main.d.ts",
|
|
24
|
+
"default": "./dist/main.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json",
|
|
28
|
+
"./bootstrap": {
|
|
29
|
+
"require": {
|
|
30
|
+
"types": "./dist/bootstrap/main.d.cts",
|
|
31
|
+
"default": "./dist/bootstrap/main.cjs"
|
|
32
|
+
},
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./dist/bootstrap/main.d.ts",
|
|
35
|
+
"default": "./dist/bootstrap/main.js"
|
|
36
|
+
},
|
|
37
|
+
"default": {
|
|
38
|
+
"types": "./dist/bootstrap/main.d.ts",
|
|
39
|
+
"default": "./dist/bootstrap/main.js"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"dist/**/*"
|
|
45
|
+
],
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "run-s build:*",
|
|
48
|
+
"build:check": "tsc",
|
|
49
|
+
"build:transpile": "node build.mjs",
|
|
50
|
+
"dev": "node build.mjs --watch",
|
|
51
|
+
"prepack": "npm run build",
|
|
52
|
+
"test": "run-s build test:dev",
|
|
53
|
+
"test:dev": "run-s build test:dev:*",
|
|
54
|
+
"test:ci": "run-s build test:ci:*",
|
|
55
|
+
"test:dev:vitest": "vitest run",
|
|
56
|
+
"test:dev:vitest:watch": "vitest watch",
|
|
57
|
+
"test:ci:vitest": "vitest run"
|
|
58
|
+
},
|
|
59
|
+
"keywords": [],
|
|
60
|
+
"license": "MIT",
|
|
61
|
+
"repository": "netlify/cache",
|
|
62
|
+
"bugs": {
|
|
63
|
+
"url": "https://github.com/netlify/cache/issues"
|
|
64
|
+
},
|
|
65
|
+
"author": "Netlify Inc.",
|
|
66
|
+
"directories": {
|
|
67
|
+
"test": "test"
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"npm-run-all2": "^7.0.2",
|
|
71
|
+
"semver": "^7.5.3",
|
|
72
|
+
"tmp-promise": "^3.0.3",
|
|
73
|
+
"tsup": "^7.2.0",
|
|
74
|
+
"typescript": "^5.0.0",
|
|
75
|
+
"vitest": "^0.34.0"
|
|
76
|
+
}
|
|
77
|
+
}
|