@netlify/cache 1.2.0 → 1.4.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.
@@ -55,29 +55,24 @@ var allowedProtocols = /* @__PURE__ */ new Set(["http:", "https:"]);
55
55
  var discardedHeaders = /* @__PURE__ */ new Set(["cookie", "content-encoding", "content-length"]);
56
56
  var getInternalHeaders = Symbol("getInternalHeaders");
57
57
  var serializeResourceHeaders = Symbol("serializeResourceHeaders");
58
- var _base64Encode, _getHost, _getToken, _getURL, _name, _userAgent;
58
+ var _base64Encode, _getContext, _name, _userAgent;
59
59
  var NetlifyCache = class {
60
- constructor({ base64Encode, getHost, getToken, getURL, name, userAgent }) {
60
+ constructor({ base64Encode, getContext, name, userAgent }) {
61
61
  __privateAdd(this, _base64Encode, void 0);
62
- __privateAdd(this, _getHost, void 0);
63
- __privateAdd(this, _getToken, void 0);
64
- __privateAdd(this, _getURL, void 0);
62
+ __privateAdd(this, _getContext, void 0);
65
63
  __privateAdd(this, _name, void 0);
66
64
  __privateAdd(this, _userAgent, void 0);
67
65
  __privateSet(this, _base64Encode, base64Encode);
68
- __privateSet(this, _getHost, getHost);
69
- __privateSet(this, _getToken, getToken);
70
- __privateSet(this, _getURL, getURL);
66
+ __privateSet(this, _getContext, getContext);
71
67
  __privateSet(this, _name, name);
72
68
  __privateSet(this, _userAgent, userAgent);
73
69
  }
74
- [getInternalHeaders]() {
75
- var _a;
70
+ [getInternalHeaders](requestContext) {
71
+ const { host, token } = requestContext;
76
72
  const headers = {
77
- Authorization: `Bearer ${__privateGet(this, _getToken).call(this)}`,
73
+ Authorization: `Bearer ${token}`,
78
74
  [ResourceStore]: __privateGet(this, _name)
79
75
  };
80
- const host = (_a = __privateGet(this, _getHost)) == null ? void 0 : _a.call(this);
81
76
  if (host) {
82
77
  headers[NetlifyForwardedHost] = host;
83
78
  }
@@ -109,9 +104,10 @@ var NetlifyCache = class {
109
104
  }
110
105
  // eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
111
106
  async delete(request) {
107
+ const context = __privateGet(this, _getContext).call(this);
112
108
  const resourceURL = extractAndValidateURL(request);
113
- await fetch(`${__privateGet(this, _getURL).call(this)}/${toCacheKey(resourceURL)}`, {
114
- headers: this[getInternalHeaders](),
109
+ await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
110
+ headers: this[getInternalHeaders](context),
115
111
  method: "DELETE"
116
112
  });
117
113
  return true;
@@ -122,10 +118,11 @@ var NetlifyCache = class {
122
118
  }
123
119
  async match(request) {
124
120
  try {
121
+ const context = __privateGet(this, _getContext).call(this);
125
122
  const resourceURL = extractAndValidateURL(request);
126
- const cacheURL = `${__privateGet(this, _getURL).call(this)}/${toCacheKey(resourceURL)}`;
123
+ const cacheURL = `${context.url}/${toCacheKey(resourceURL)}`;
127
124
  const response = await fetch(cacheURL, {
128
- headers: this[getInternalHeaders](),
125
+ headers: this[getInternalHeaders](context),
129
126
  method: "GET"
130
127
  });
131
128
  if (!response.ok) {
@@ -156,11 +153,12 @@ var NetlifyCache = class {
156
153
  if (response.headers.get("vary")?.includes("*")) {
157
154
  throw new TypeError("Cannot cache response with 'Vary: *' header.");
158
155
  }
156
+ const context = __privateGet(this, _getContext).call(this);
159
157
  const resourceURL = extractAndValidateURL(request);
160
- await fetch(`${__privateGet(this, _getURL).call(this)}/${toCacheKey(resourceURL)}`, {
158
+ await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
161
159
  body: response.body,
162
160
  headers: {
163
- ...this[getInternalHeaders](),
161
+ ...this[getInternalHeaders](context),
164
162
  [ResourceHeaders]: this[serializeResourceHeaders](response.headers),
165
163
  [ResourceStatus]: response.status.toString()
166
164
  },
@@ -171,9 +169,7 @@ var NetlifyCache = class {
171
169
  }
172
170
  };
173
171
  _base64Encode = new WeakMap();
174
- _getHost = new WeakMap();
175
- _getToken = new WeakMap();
176
- _getURL = new WeakMap();
172
+ _getContext = new WeakMap();
177
173
  _name = new WeakMap();
178
174
  _userAgent = new WeakMap();
179
175
  var extractAndValidateURL = (input) => {
@@ -1,5 +1,5 @@
1
- import { E as EnvironmentOptions, F as Factory } from '../cache-7ef2f22d.js';
2
- export { B as Base64Encoder, N as NetlifyCache } from '../cache-7ef2f22d.js';
1
+ import { E as EnvironmentOptions } from '../cache-854474ad.js';
2
+ export { B as Base64Encoder, N as NetlifyCache, R as RequestContextFactory } from '../cache-854474ad.js';
3
3
 
4
4
  declare class NetlifyCacheStorage {
5
5
  #private;
@@ -11,8 +11,4 @@ declare class NetlifyCacheStorage {
11
11
  match(request: RequestInfo, options?: MultiCacheQueryOptions): Promise<Response | undefined>;
12
12
  }
13
13
 
14
- type HostFactory = Factory<string>;
15
- type TokenFactory = Factory<string>;
16
- type URLFactory = Factory<string>;
17
-
18
- export { HostFactory, NetlifyCacheStorage, TokenFactory, URLFactory };
14
+ export { NetlifyCacheStorage };
@@ -1,5 +1,5 @@
1
- import { E as EnvironmentOptions, F as Factory } from '../cache-7ef2f22d.js';
2
- export { B as Base64Encoder, N as NetlifyCache } from '../cache-7ef2f22d.js';
1
+ import { E as EnvironmentOptions } from '../cache-854474ad.js';
2
+ export { B as Base64Encoder, N as NetlifyCache, R as RequestContextFactory } from '../cache-854474ad.js';
3
3
 
4
4
  declare class NetlifyCacheStorage {
5
5
  #private;
@@ -11,8 +11,4 @@ declare class NetlifyCacheStorage {
11
11
  match(request: RequestInfo, options?: MultiCacheQueryOptions): Promise<Response | undefined>;
12
12
  }
13
13
 
14
- type HostFactory = Factory<string>;
15
- type TokenFactory = Factory<string>;
16
- type URLFactory = Factory<string>;
17
-
18
- export { HostFactory, NetlifyCacheStorage, TokenFactory, URLFactory };
14
+ export { NetlifyCacheStorage };
@@ -14,29 +14,24 @@ var allowedProtocols = /* @__PURE__ */ new Set(["http:", "https:"]);
14
14
  var discardedHeaders = /* @__PURE__ */ new Set(["cookie", "content-encoding", "content-length"]);
15
15
  var getInternalHeaders = Symbol("getInternalHeaders");
16
16
  var serializeResourceHeaders = Symbol("serializeResourceHeaders");
17
- var _base64Encode, _getHost, _getToken, _getURL, _name, _userAgent;
17
+ var _base64Encode, _getContext, _name, _userAgent;
18
18
  var NetlifyCache = class {
19
- constructor({ base64Encode, getHost, getToken, getURL, name, userAgent }) {
19
+ constructor({ base64Encode, getContext, name, userAgent }) {
20
20
  __privateAdd(this, _base64Encode, void 0);
21
- __privateAdd(this, _getHost, void 0);
22
- __privateAdd(this, _getToken, void 0);
23
- __privateAdd(this, _getURL, void 0);
21
+ __privateAdd(this, _getContext, void 0);
24
22
  __privateAdd(this, _name, void 0);
25
23
  __privateAdd(this, _userAgent, void 0);
26
24
  __privateSet(this, _base64Encode, base64Encode);
27
- __privateSet(this, _getHost, getHost);
28
- __privateSet(this, _getToken, getToken);
29
- __privateSet(this, _getURL, getURL);
25
+ __privateSet(this, _getContext, getContext);
30
26
  __privateSet(this, _name, name);
31
27
  __privateSet(this, _userAgent, userAgent);
32
28
  }
33
- [getInternalHeaders]() {
34
- var _a;
29
+ [getInternalHeaders](requestContext) {
30
+ const { host, token } = requestContext;
35
31
  const headers = {
36
- Authorization: `Bearer ${__privateGet(this, _getToken).call(this)}`,
32
+ Authorization: `Bearer ${token}`,
37
33
  [ResourceStore]: __privateGet(this, _name)
38
34
  };
39
- const host = (_a = __privateGet(this, _getHost)) == null ? void 0 : _a.call(this);
40
35
  if (host) {
41
36
  headers[NetlifyForwardedHost] = host;
42
37
  }
@@ -68,9 +63,10 @@ var NetlifyCache = class {
68
63
  }
69
64
  // eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
70
65
  async delete(request) {
66
+ const context = __privateGet(this, _getContext).call(this);
71
67
  const resourceURL = extractAndValidateURL(request);
72
- await fetch(`${__privateGet(this, _getURL).call(this)}/${toCacheKey(resourceURL)}`, {
73
- headers: this[getInternalHeaders](),
68
+ await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
69
+ headers: this[getInternalHeaders](context),
74
70
  method: "DELETE"
75
71
  });
76
72
  return true;
@@ -81,10 +77,11 @@ var NetlifyCache = class {
81
77
  }
82
78
  async match(request) {
83
79
  try {
80
+ const context = __privateGet(this, _getContext).call(this);
84
81
  const resourceURL = extractAndValidateURL(request);
85
- const cacheURL = `${__privateGet(this, _getURL).call(this)}/${toCacheKey(resourceURL)}`;
82
+ const cacheURL = `${context.url}/${toCacheKey(resourceURL)}`;
86
83
  const response = await fetch(cacheURL, {
87
- headers: this[getInternalHeaders](),
84
+ headers: this[getInternalHeaders](context),
88
85
  method: "GET"
89
86
  });
90
87
  if (!response.ok) {
@@ -115,11 +112,12 @@ var NetlifyCache = class {
115
112
  if (response.headers.get("vary")?.includes("*")) {
116
113
  throw new TypeError("Cannot cache response with 'Vary: *' header.");
117
114
  }
115
+ const context = __privateGet(this, _getContext).call(this);
118
116
  const resourceURL = extractAndValidateURL(request);
119
- await fetch(`${__privateGet(this, _getURL).call(this)}/${toCacheKey(resourceURL)}`, {
117
+ await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
120
118
  body: response.body,
121
119
  headers: {
122
- ...this[getInternalHeaders](),
120
+ ...this[getInternalHeaders](context),
123
121
  [ResourceHeaders]: this[serializeResourceHeaders](response.headers),
124
122
  [ResourceStatus]: response.status.toString()
125
123
  },
@@ -130,9 +128,7 @@ var NetlifyCache = class {
130
128
  }
131
129
  };
132
130
  _base64Encode = new WeakMap();
133
- _getHost = new WeakMap();
134
- _getToken = new WeakMap();
135
- _getURL = new WeakMap();
131
+ _getContext = new WeakMap();
136
132
  _name = new WeakMap();
137
133
  _userAgent = new WeakMap();
138
134
  var extractAndValidateURL = (input) => {
@@ -1,12 +1,15 @@
1
1
  type Base64Encoder = (input: string) => string;
2
- type Factory<T> = () => T;
3
2
  interface EnvironmentOptions {
4
3
  base64Encode: Base64Encoder;
5
- getHost: Factory<string>;
6
- getToken: Factory<string>;
7
- getURL: Factory<string>;
4
+ getContext: RequestContextFactory;
8
5
  userAgent?: string;
9
6
  }
7
+ type RequestContextFactory = () => RequestContext;
8
+ interface RequestContext {
9
+ host: string;
10
+ token: string;
11
+ url: string;
12
+ }
10
13
 
11
14
  type NetlifyCacheOptions = EnvironmentOptions & {
12
15
  name: string;
@@ -15,7 +18,7 @@ declare const getInternalHeaders: unique symbol;
15
18
  declare const serializeResourceHeaders: unique symbol;
16
19
  declare class NetlifyCache implements Cache {
17
20
  #private;
18
- constructor({ base64Encode, getHost, getToken, getURL, name, userAgent }: NetlifyCacheOptions);
21
+ constructor({ base64Encode, getContext, name, userAgent }: NetlifyCacheOptions);
19
22
  private [getInternalHeaders];
20
23
  private [serializeResourceHeaders];
21
24
  add(request: RequestInfo): Promise<void>;
@@ -27,4 +30,4 @@ declare class NetlifyCache implements Cache {
27
30
  put(request: RequestInfo | URL | string, response: Response): Promise<void>;
28
31
  }
29
32
 
30
- export { Base64Encoder as B, EnvironmentOptions as E, Factory as F, NetlifyCache as N };
33
+ export { Base64Encoder as B, EnvironmentOptions as E, NetlifyCache as N, RequestContextFactory as R };
package/dist/main.cjs CHANGED
@@ -20,6 +20,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/main.ts
21
21
  var main_exports = {};
22
22
  __export(main_exports, {
23
+ DAY: () => DAY,
24
+ HOUR: () => HOUR,
25
+ MINUTE: () => MINUTE,
26
+ WEEK: () => WEEK,
27
+ YEAR: () => YEAR,
23
28
  fetchWithCache: () => fetchWithCache,
24
29
  getCacheStatus: () => getCacheStatus,
25
30
  setCacheHeaders: () => setCacheHeaders
@@ -290,8 +295,20 @@ var fetchWithCache = async (request, optionsOrCacheSettings, cacheOptionsParam)
290
295
  }
291
296
  return fresh;
292
297
  };
298
+
299
+ // src/constants.ts
300
+ var MINUTE = 60;
301
+ var HOUR = 60 * MINUTE;
302
+ var DAY = 24 * HOUR;
303
+ var WEEK = 7 * DAY;
304
+ var YEAR = 365 * DAY;
293
305
  // Annotate the CommonJS export names for ESM import in node:
294
306
  0 && (module.exports = {
307
+ DAY,
308
+ HOUR,
309
+ MINUTE,
310
+ WEEK,
311
+ YEAR,
295
312
  fetchWithCache,
296
313
  getCacheStatus,
297
314
  setCacheHeaders
package/dist/main.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { N as NetlifyCache } from './cache-7ef2f22d.js';
1
+ import { N as NetlifyCache } from './cache-854474ad.js';
2
2
 
3
3
  interface CacheSettings {
4
4
  /**
@@ -153,4 +153,25 @@ type FetchWithCache = {
153
153
  */
154
154
  declare const fetchWithCache: FetchWithCache;
155
155
 
156
- export { fetchWithCache, getCacheStatus, setCacheHeaders };
156
+ /**
157
+ * Number of seconds in one minute.
158
+ */
159
+ declare const MINUTE = 60;
160
+ /**
161
+ * Number of seconds in one hour.
162
+ */
163
+ declare const HOUR: number;
164
+ /**
165
+ * Number of seconds in one day.
166
+ */
167
+ declare const DAY: number;
168
+ /**
169
+ * Number of seconds in one week.
170
+ */
171
+ declare const WEEK: number;
172
+ /**
173
+ * Number of seconds in one year.
174
+ */
175
+ declare const YEAR: number;
176
+
177
+ export { DAY, HOUR, MINUTE, WEEK, YEAR, fetchWithCache, getCacheStatus, setCacheHeaders };
package/dist/main.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { N as NetlifyCache } from './cache-7ef2f22d.js';
1
+ import { N as NetlifyCache } from './cache-854474ad.js';
2
2
 
3
3
  interface CacheSettings {
4
4
  /**
@@ -153,4 +153,25 @@ type FetchWithCache = {
153
153
  */
154
154
  declare const fetchWithCache: FetchWithCache;
155
155
 
156
- export { fetchWithCache, getCacheStatus, setCacheHeaders };
156
+ /**
157
+ * Number of seconds in one minute.
158
+ */
159
+ declare const MINUTE = 60;
160
+ /**
161
+ * Number of seconds in one hour.
162
+ */
163
+ declare const HOUR: number;
164
+ /**
165
+ * Number of seconds in one day.
166
+ */
167
+ declare const DAY: number;
168
+ /**
169
+ * Number of seconds in one week.
170
+ */
171
+ declare const WEEK: number;
172
+ /**
173
+ * Number of seconds in one year.
174
+ */
175
+ declare const YEAR: number;
176
+
177
+ export { DAY, HOUR, MINUTE, WEEK, YEAR, fetchWithCache, getCacheStatus, setCacheHeaders };
package/dist/main.js CHANGED
@@ -263,7 +263,19 @@ var fetchWithCache = async (request, optionsOrCacheSettings, cacheOptionsParam)
263
263
  }
264
264
  return fresh;
265
265
  };
266
+
267
+ // src/constants.ts
268
+ var MINUTE = 60;
269
+ var HOUR = 60 * MINUTE;
270
+ var DAY = 24 * HOUR;
271
+ var WEEK = 7 * DAY;
272
+ var YEAR = 365 * DAY;
266
273
  export {
274
+ DAY,
275
+ HOUR,
276
+ MINUTE,
277
+ WEEK,
278
+ YEAR,
267
279
  fetchWithCache,
268
280
  getCacheStatus,
269
281
  setCacheHeaders
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/cache",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "TypeScript utilities for interacting with the Netlify cache",
5
5
  "type": "module",
6
6
  "engines": {