@warp-drive/core 5.8.0-alpha.5 → 5.8.0-alpha.7
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/declarations/build-config.d.ts +18 -1
- package/declarations/index.d.ts +80 -3
- package/declarations/reactive/-private/document.d.ts +58 -46
- package/declarations/reactive/-private/schema.d.ts +1 -1
- package/declarations/reactive.d.ts +3 -2
- package/declarations/request.d.ts +25 -0
- package/declarations/store/-private/caches/instance-cache.d.ts +1 -1
- package/declarations/store/-private/default-cache-policy.d.ts +74 -73
- package/declarations/store/-private/managers/cache-capabilities-manager.d.ts +1 -1
- package/declarations/store/-private/managers/cache-manager.d.ts +6 -4
- package/declarations/store/-private/new-core-tmp/promise-state.d.ts +1 -0
- package/declarations/store/-private/store-service.d.ts +1 -1
- package/declarations/store/-types/q/cache-capabilities-manager.d.ts +1 -1
- package/declarations/store/deprecated/store.d.ts +1 -1
- package/declarations/types/cache.d.ts +6 -4
- package/declarations/types/request.d.ts +4 -6
- package/declarations/types/schema/fields.d.ts +11 -0
- package/declarations/types/symbols.d.ts +2 -2
- package/dist/build-config.js +1 -1
- package/dist/default-cache-policy-D7_u4YRH.js +572 -0
- package/dist/graph/-private.js +1 -1
- package/dist/{index-MiSBsI57.js → index-BMCk_UD5.js} +10453 -10426
- package/dist/index.js +5 -4
- package/dist/reactive.js +7 -42
- package/dist/{context-C_7OLieY.js → request-CN-dNlEY.js} +193 -173
- package/dist/request.js +1 -1
- package/dist/store/-private.js +1 -1
- package/dist/store.js +1 -570
- package/dist/types/-private.js +1 -1
- package/dist/types/request.js +3 -5
- package/dist/types/schema/fields.js +14 -0
- package/dist/types/symbols.js +2 -2
- package/package.json +3 -3
package/dist/store.js
CHANGED
|
@@ -1,570 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { LRUCache } from './utils/string.js';
|
|
3
|
-
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Interface of a parsed Cache-Control header value.
|
|
7
|
-
*
|
|
8
|
-
* - [MDN Cache-Control Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control)
|
|
9
|
-
*/
|
|
10
|
-
const NUMERIC_KEYS = new Set(['max-age', 's-maxage', 'stale-if-error', 'stale-while-revalidate']);
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Parses a string Cache-Control header value into an object with the following structure:
|
|
14
|
-
*
|
|
15
|
-
* ```ts
|
|
16
|
-
* interface CacheControlValue {
|
|
17
|
-
* immutable?: boolean;
|
|
18
|
-
* 'max-age'?: number;
|
|
19
|
-
* 'must-revalidate'?: boolean;
|
|
20
|
-
* 'must-understand'?: boolean;
|
|
21
|
-
* 'no-cache'?: boolean;
|
|
22
|
-
* 'no-store'?: boolean;
|
|
23
|
-
* 'no-transform'?: boolean;
|
|
24
|
-
* 'only-if-cached'?: boolean;
|
|
25
|
-
* private?: boolean;
|
|
26
|
-
* 'proxy-revalidate'?: boolean;
|
|
27
|
-
* public?: boolean;
|
|
28
|
-
* 's-maxage'?: number;
|
|
29
|
-
* 'stale-if-error'?: number;
|
|
30
|
-
* 'stale-while-revalidate'?: number;
|
|
31
|
-
* }
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* See also {@link CacheControlValue} and [Response Directives](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control#response_directives)
|
|
35
|
-
*
|
|
36
|
-
* @public
|
|
37
|
-
* @param {String} header
|
|
38
|
-
* @return {CacheControlValue}
|
|
39
|
-
*/
|
|
40
|
-
function parseCacheControl(header) {
|
|
41
|
-
return CACHE_CONTROL_CACHE.get(header);
|
|
42
|
-
}
|
|
43
|
-
const CACHE_CONTROL_CACHE = new LRUCache(header => {
|
|
44
|
-
let key = '';
|
|
45
|
-
let value = '';
|
|
46
|
-
let isParsingKey = true;
|
|
47
|
-
const cacheControlValue = {};
|
|
48
|
-
for (let i = 0; i < header.length; i++) {
|
|
49
|
-
const char = header.charAt(i);
|
|
50
|
-
if (char === ',') {
|
|
51
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
52
|
-
if (!test) {
|
|
53
|
-
throw new Error(`Invalid Cache-Control value, expected a value`);
|
|
54
|
-
}
|
|
55
|
-
})(!isParsingKey || !NUMERIC_KEYS.has(key)) : {};
|
|
56
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
57
|
-
if (!test) {
|
|
58
|
-
throw new Error(`Invalid Cache-Control value, expected a value after "=" but got ","`);
|
|
59
|
-
}
|
|
60
|
-
})(i === 0 || header.charAt(i - 1) !== '=') : {};
|
|
61
|
-
isParsingKey = true;
|
|
62
|
-
// @ts-expect-error TS incorrectly thinks that optional keys must have a type that includes undefined
|
|
63
|
-
cacheControlValue[key] = NUMERIC_KEYS.has(key) ? parseCacheControlValue(value) : true;
|
|
64
|
-
key = '';
|
|
65
|
-
value = '';
|
|
66
|
-
continue;
|
|
67
|
-
} else if (char === '=') {
|
|
68
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
69
|
-
if (!test) {
|
|
70
|
-
throw new Error(`Invalid Cache-Control value, expected a value after "="`);
|
|
71
|
-
}
|
|
72
|
-
})(i + 1 !== header.length) : {};
|
|
73
|
-
isParsingKey = false;
|
|
74
|
-
} else if (char === ' ' || char === `\t` || char === `\n`) {
|
|
75
|
-
continue;
|
|
76
|
-
} else if (isParsingKey) {
|
|
77
|
-
key += char;
|
|
78
|
-
} else {
|
|
79
|
-
value += char;
|
|
80
|
-
}
|
|
81
|
-
if (i === header.length - 1) {
|
|
82
|
-
// @ts-expect-error TS incorrectly thinks that optional keys must have a type that includes undefined
|
|
83
|
-
cacheControlValue[key] = NUMERIC_KEYS.has(key) ? parseCacheControlValue(value) : true;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return cacheControlValue;
|
|
87
|
-
}, 200);
|
|
88
|
-
function parseCacheControlValue(stringToParse) {
|
|
89
|
-
const parsedValue = Number.parseInt(stringToParse);
|
|
90
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
91
|
-
if (!test) {
|
|
92
|
-
throw new Error(`Invalid Cache-Control value, expected a number but got - ${stringToParse}`);
|
|
93
|
-
}
|
|
94
|
-
})(!Number.isNaN(parsedValue)) : {};
|
|
95
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
96
|
-
if (!test) {
|
|
97
|
-
throw new Error(`Invalid Cache-Control value, expected a number greater than 0 but got - ${stringToParse}`);
|
|
98
|
-
}
|
|
99
|
-
})(parsedValue >= 0) : {};
|
|
100
|
-
if (Number.isNaN(parsedValue) || parsedValue < 0) {
|
|
101
|
-
return 0;
|
|
102
|
-
}
|
|
103
|
-
return parsedValue;
|
|
104
|
-
}
|
|
105
|
-
function isExpired(cacheKey, request, config) {
|
|
106
|
-
const {
|
|
107
|
-
constraints
|
|
108
|
-
} = config;
|
|
109
|
-
if (constraints?.isExpired) {
|
|
110
|
-
const result = constraints.isExpired(request);
|
|
111
|
-
if (result !== null) {
|
|
112
|
-
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
113
|
-
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
114
|
-
// eslint-disable-next-line no-console
|
|
115
|
-
console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because constraints.isExpired returned ${result}`);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return result;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
const {
|
|
122
|
-
headers
|
|
123
|
-
} = request.response;
|
|
124
|
-
if (!headers) {
|
|
125
|
-
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
126
|
-
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
127
|
-
// eslint-disable-next-line no-console
|
|
128
|
-
console.log(`CachePolicy: ${cacheKey.lid} is EXPIRED because no headers were provided`);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// if we have no headers then both the headers based expiration
|
|
133
|
-
// and the time based expiration will be considered expired
|
|
134
|
-
return true;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// check for X-WarpDrive-Expires
|
|
138
|
-
const now = Date.now();
|
|
139
|
-
const date = headers.get('Date');
|
|
140
|
-
if (constraints?.headers) {
|
|
141
|
-
if (constraints.headers['X-WarpDrive-Expires']) {
|
|
142
|
-
const xWarpDriveExpires = headers.get('X-WarpDrive-Expires');
|
|
143
|
-
if (xWarpDriveExpires) {
|
|
144
|
-
const expirationTime = new Date(xWarpDriveExpires).getTime();
|
|
145
|
-
const result = now >= expirationTime;
|
|
146
|
-
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
147
|
-
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
148
|
-
// eslint-disable-next-line no-console
|
|
149
|
-
console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because the time set by X-WarpDrive-Expires header is ${result ? 'in the past' : 'in the future'}`);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return result;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// check for Cache-Control
|
|
157
|
-
if (constraints.headers['Cache-Control']) {
|
|
158
|
-
const cacheControl = headers.get('Cache-Control');
|
|
159
|
-
const age = headers.get('Age');
|
|
160
|
-
if (cacheControl && age && date) {
|
|
161
|
-
const cacheControlValue = parseCacheControl(cacheControl);
|
|
162
|
-
|
|
163
|
-
// max-age and s-maxage are stored in
|
|
164
|
-
const maxAge = cacheControlValue['max-age'] || cacheControlValue['s-maxage'];
|
|
165
|
-
if (maxAge) {
|
|
166
|
-
// age is stored in seconds
|
|
167
|
-
const ageValue = parseInt(age, 10);
|
|
168
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
169
|
-
if (!test) {
|
|
170
|
-
throw new Error(`Invalid Cache-Control value, expected a number but got - ${age}`);
|
|
171
|
-
}
|
|
172
|
-
})(!Number.isNaN(ageValue)) : {};
|
|
173
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
174
|
-
if (!test) {
|
|
175
|
-
throw new Error(`Invalid Cache-Control value, expected a number greater than 0 but got - ${age}`);
|
|
176
|
-
}
|
|
177
|
-
})(ageValue >= 0) : {};
|
|
178
|
-
if (!Number.isNaN(ageValue) && ageValue >= 0) {
|
|
179
|
-
const dateValue = new Date(date).getTime();
|
|
180
|
-
const expirationTime = dateValue + (maxAge - ageValue) * 1000;
|
|
181
|
-
const result = now >= expirationTime;
|
|
182
|
-
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
183
|
-
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
184
|
-
// eslint-disable-next-line no-console
|
|
185
|
-
console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because the time set by Cache-Control header is ${result ? 'in the past' : 'in the future'}`);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return result;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// check for Expires
|
|
195
|
-
if (constraints.headers.Expires) {
|
|
196
|
-
const expires = headers.get('Expires');
|
|
197
|
-
if (expires) {
|
|
198
|
-
const expirationTime = new Date(expires).getTime();
|
|
199
|
-
const result = now >= expirationTime;
|
|
200
|
-
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
201
|
-
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
202
|
-
// eslint-disable-next-line no-console
|
|
203
|
-
console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because the time set by Expires header is ${result ? 'in the past' : 'in the future'}`);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
return result;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// check for Date
|
|
212
|
-
if (!date) {
|
|
213
|
-
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
214
|
-
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
215
|
-
// eslint-disable-next-line no-console
|
|
216
|
-
console.log(`CachePolicy: ${cacheKey.lid} is EXPIRED because no Date header was provided`);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
return true;
|
|
220
|
-
}
|
|
221
|
-
let expirationTime = config.apiCacheHardExpires;
|
|
222
|
-
if (macroCondition(getGlobalConfig().WarpDrive.env.TESTING)) {
|
|
223
|
-
if (!config.disableTestOptimization) {
|
|
224
|
-
expirationTime = config.apiCacheSoftExpires;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
const time = new Date(date).getTime();
|
|
228
|
-
const deadline = time + expirationTime;
|
|
229
|
-
const result = now >= deadline;
|
|
230
|
-
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
231
|
-
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
232
|
-
// eslint-disable-next-line no-console
|
|
233
|
-
console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because the apiCacheHardExpires time since the response's Date header is ${result ? 'in the past' : 'in the future'}`);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
return result;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* The configuration options for the {@link DefaultCachePolicy}
|
|
241
|
-
*
|
|
242
|
-
* ```ts [app/services/store.ts]
|
|
243
|
-
* import { Store } from '@warp-drive/core';
|
|
244
|
-
* import { DefaultCachePolicy } from '@warp-drive/core/store'; // [!code focus]
|
|
245
|
-
*
|
|
246
|
-
* export default class AppStore extends Store {
|
|
247
|
-
* lifetimes = new DefaultCachePolicy({ // [!code focus:3]
|
|
248
|
-
* // ... PolicyConfig Settings ... //
|
|
249
|
-
* });
|
|
250
|
-
* }
|
|
251
|
-
* ```
|
|
252
|
-
*
|
|
253
|
-
*/
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* A basic {@link CachePolicy} that can be added to the Store service.
|
|
257
|
-
*
|
|
258
|
-
* ```ts [app/services/store.ts]
|
|
259
|
-
* import { Store } from '@warp-drive/core';
|
|
260
|
-
* import { DefaultCachePolicy } from '@warp-drive/core/store'; // [!code focus]
|
|
261
|
-
*
|
|
262
|
-
* export default class AppStore extends Store {
|
|
263
|
-
* lifetimes = new DefaultCachePolicy({ // [!code focus:5]
|
|
264
|
-
* apiCacheSoftExpires: 30_000,
|
|
265
|
-
* apiCacheHardExpires: 60_000,
|
|
266
|
-
* // ... Other PolicyConfig Settings ... //
|
|
267
|
-
* });
|
|
268
|
-
* }
|
|
269
|
-
* ```
|
|
270
|
-
*
|
|
271
|
-
* :::tip 💡 TIP
|
|
272
|
-
* Date headers do not have millisecond precision, so expiration times should
|
|
273
|
-
* generally be larger than 1000ms.
|
|
274
|
-
* :::
|
|
275
|
-
*
|
|
276
|
-
* See also {@link PolicyConfig} for configuration options.
|
|
277
|
-
*
|
|
278
|
-
* ### The Mechanics
|
|
279
|
-
*
|
|
280
|
-
* This policy determines staleness based on various configurable constraints falling back to a simple
|
|
281
|
-
* check of the time elapsed since the request was last received from the API using the `date` header
|
|
282
|
-
* from the last response.
|
|
283
|
-
*
|
|
284
|
-
* :::tip 💡 TIP
|
|
285
|
-
* The {@link Fetch} handler provided by `@warp-drive/core` will automatically
|
|
286
|
-
* add the `date` header to responses if it is not present.
|
|
287
|
-
* :::
|
|
288
|
-
*
|
|
289
|
-
* - For manual override of reload see {@link RequestInfo.cacheOptions.reload | cacheOptions.reload}
|
|
290
|
-
* - For manual override of background reload see {@link RequestInfo.cacheOptions.backgroundReload | cacheOptions.backgroundReload}
|
|
291
|
-
*
|
|
292
|
-
* In order expiration is determined by:
|
|
293
|
-
*
|
|
294
|
-
* ```md
|
|
295
|
-
* Is explicitly invalidated by `cacheOptions.reload`
|
|
296
|
-
* ↳ (if falsey) if the request has been explicitly invalidated
|
|
297
|
-
* since the last request (see Automatic Invalidation below)
|
|
298
|
-
* ↳ (if false) (If Active) isExpired function
|
|
299
|
-
* ↳ (if null) (If Active) X-WarpDrive-Expires header
|
|
300
|
-
* ↳ (if null) (If Active) Cache-Control header
|
|
301
|
-
* ↳ (if null) (If Active) Expires header
|
|
302
|
-
* ↳ (if null) Date header + apiCacheHardExpires < current time
|
|
303
|
-
*
|
|
304
|
-
* -- <if above is false, a background request is issued if> --
|
|
305
|
-
*
|
|
306
|
-
* ↳ is invalidated by `cacheOptions.backgroundReload`
|
|
307
|
-
* ↳ (if falsey) Date header + apiCacheSoftExpires < current time
|
|
308
|
-
* ```
|
|
309
|
-
*
|
|
310
|
-
* ### Automatic Invalidation / Entanglement
|
|
311
|
-
*
|
|
312
|
-
* It also invalidates any request with an {@link RequestInfo.op | OpCode} of `"query"`
|
|
313
|
-
* for which {@link RequestInfo.cacheOptions.types | cacheOptions.types} was provided
|
|
314
|
-
* when a request with an `OpCode` of `"createRecord"` is successful and also includes
|
|
315
|
-
* a matching type in its own `cacheOptions.types` array.
|
|
316
|
-
|
|
317
|
-
* :::tip 💡 TIP
|
|
318
|
-
* Abstracting this behavior via builders is recommended to ensure consistency.
|
|
319
|
-
* :::
|
|
320
|
-
*
|
|
321
|
-
* ### Testing
|
|
322
|
-
*
|
|
323
|
-
* In Testing environments:
|
|
324
|
-
*
|
|
325
|
-
* - `apiCacheSoftExpires` will always be `false`
|
|
326
|
-
* - `apiCacheHardExpires` will use the `apiCacheSoftExpires` value.
|
|
327
|
-
*
|
|
328
|
-
* This helps reduce flakiness and produce predictably rendered results in test suites.
|
|
329
|
-
*
|
|
330
|
-
* Requests that specifically set `cacheOptions.backgroundReload = true` will
|
|
331
|
-
* still be background reloaded in tests.
|
|
332
|
-
*
|
|
333
|
-
* This behavior can be opted out of by setting `disableTestOptimization = true`
|
|
334
|
-
* in the policy config.
|
|
335
|
-
*
|
|
336
|
-
* @public
|
|
337
|
-
*/
|
|
338
|
-
class DefaultCachePolicy {
|
|
339
|
-
/**
|
|
340
|
-
* @internal
|
|
341
|
-
*/
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* @internal
|
|
345
|
-
*/
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
* @internal
|
|
349
|
-
*/
|
|
350
|
-
_getStore(store) {
|
|
351
|
-
let set = this._stores.get(store);
|
|
352
|
-
if (!set) {
|
|
353
|
-
set = {
|
|
354
|
-
invalidated: new Set(),
|
|
355
|
-
types: new Map()
|
|
356
|
-
};
|
|
357
|
-
this._stores.set(store, set);
|
|
358
|
-
}
|
|
359
|
-
return set;
|
|
360
|
-
}
|
|
361
|
-
constructor(config) {
|
|
362
|
-
this._stores = new WeakMap();
|
|
363
|
-
const _config = arguments.length === 1 ? config : arguments[1];
|
|
364
|
-
deprecate(`Passing a Store to the CachePolicy is deprecated, please pass only a config instead.`, arguments.length === 1, {
|
|
365
|
-
id: 'ember-data:request-utils:lifetimes-service-store-arg',
|
|
366
|
-
since: {
|
|
367
|
-
enabled: '5.4',
|
|
368
|
-
available: '4.13'
|
|
369
|
-
},
|
|
370
|
-
for: '@ember-data/request-utils',
|
|
371
|
-
until: '6.0'
|
|
372
|
-
});
|
|
373
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
374
|
-
if (!test) {
|
|
375
|
-
throw new Error(`You must pass a config to the CachePolicy`);
|
|
376
|
-
}
|
|
377
|
-
})(_config) : {};
|
|
378
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
379
|
-
if (!test) {
|
|
380
|
-
throw new Error(`You must pass a apiCacheSoftExpires to the CachePolicy`);
|
|
381
|
-
}
|
|
382
|
-
})(typeof _config.apiCacheSoftExpires === 'number') : {};
|
|
383
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
384
|
-
if (!test) {
|
|
385
|
-
throw new Error(`You must pass a apiCacheHardExpires to the CachePolicy`);
|
|
386
|
-
}
|
|
387
|
-
})(typeof _config.apiCacheHardExpires === 'number') : {};
|
|
388
|
-
this.config = _config;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
* Invalidate a request by its CacheKey for the given store instance.
|
|
393
|
-
*
|
|
394
|
-
* While the store argument may seem redundant, the CachePolicy
|
|
395
|
-
* is designed to be shared across multiple stores / forks
|
|
396
|
-
* of the store.
|
|
397
|
-
*
|
|
398
|
-
* ```ts
|
|
399
|
-
* store.lifetimes.invalidateRequest(store, cacheKey);
|
|
400
|
-
* ```
|
|
401
|
-
*
|
|
402
|
-
* @public
|
|
403
|
-
*/
|
|
404
|
-
invalidateRequest(cacheKey, store) {
|
|
405
|
-
this._getStore(store).invalidated.add(cacheKey);
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
/**
|
|
409
|
-
* Invalidate all requests associated to a specific type
|
|
410
|
-
* for a given store instance.
|
|
411
|
-
*
|
|
412
|
-
* While the store argument may seem redundant, the CachePolicy
|
|
413
|
-
* is designed to be shared across multiple stores / forks
|
|
414
|
-
* of the store.
|
|
415
|
-
*
|
|
416
|
-
* This invalidation is done automatically when using this service
|
|
417
|
-
* for both the CacheHandler and the LegacyNetworkHandler.
|
|
418
|
-
*
|
|
419
|
-
* ```ts
|
|
420
|
-
* store.lifetimes.invalidateRequestsForType(store, 'person');
|
|
421
|
-
* ```
|
|
422
|
-
*
|
|
423
|
-
* @public
|
|
424
|
-
*/
|
|
425
|
-
invalidateRequestsForType(type, store) {
|
|
426
|
-
const storeCache = this._getStore(store);
|
|
427
|
-
const set = storeCache.types.get(type);
|
|
428
|
-
const notifications = store.notifications;
|
|
429
|
-
if (set) {
|
|
430
|
-
// TODO batch notifications
|
|
431
|
-
set.forEach(id => {
|
|
432
|
-
storeCache.invalidated.add(id);
|
|
433
|
-
// @ts-expect-error
|
|
434
|
-
notifications.notify(id, 'invalidated', null);
|
|
435
|
-
});
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
/**
|
|
440
|
-
* Invoked when a request has been fulfilled from the configured request handlers.
|
|
441
|
-
* This is invoked by the CacheHandler for both foreground and background requests
|
|
442
|
-
* once the cache has been updated.
|
|
443
|
-
*
|
|
444
|
-
* Note, this is invoked by the CacheHandler regardless of whether
|
|
445
|
-
* the request has a cache-key.
|
|
446
|
-
*
|
|
447
|
-
* This method should not be invoked directly by consumers.
|
|
448
|
-
*
|
|
449
|
-
* @public
|
|
450
|
-
*/
|
|
451
|
-
didRequest(request, response, cacheKey, store) {
|
|
452
|
-
// if this is a successful createRecord request, invalidate the cacheKey for the type
|
|
453
|
-
if (request.op === 'createRecord') {
|
|
454
|
-
const statusNumber = response?.status ?? 0;
|
|
455
|
-
if (statusNumber >= 200 && statusNumber < 400) {
|
|
456
|
-
const types = new Set(request.records?.map(r => r.type));
|
|
457
|
-
const additionalTypes = request.cacheOptions?.types;
|
|
458
|
-
additionalTypes?.forEach(type => {
|
|
459
|
-
types.add(type);
|
|
460
|
-
});
|
|
461
|
-
types.forEach(type => {
|
|
462
|
-
this.invalidateRequestsForType(type, store);
|
|
463
|
-
});
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
// add this document's cacheKey to a map for all associated types
|
|
467
|
-
// it is recommended to only use this for queries
|
|
468
|
-
} else if (cacheKey && request.cacheOptions?.types?.length) {
|
|
469
|
-
const storeCache = this._getStore(store);
|
|
470
|
-
request.cacheOptions?.types.forEach(type => {
|
|
471
|
-
const set = storeCache.types.get(type);
|
|
472
|
-
if (set) {
|
|
473
|
-
set.add(cacheKey);
|
|
474
|
-
storeCache.invalidated.delete(cacheKey);
|
|
475
|
-
} else {
|
|
476
|
-
storeCache.types.set(type, new Set([cacheKey]));
|
|
477
|
-
}
|
|
478
|
-
});
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
/**
|
|
483
|
-
* Invoked to determine if the request may be fulfilled from cache
|
|
484
|
-
* if possible.
|
|
485
|
-
*
|
|
486
|
-
* Note, this is only invoked by the CacheHandler if the request has
|
|
487
|
-
* a cache-key.
|
|
488
|
-
*
|
|
489
|
-
* If no cache entry is found or the entry is hard expired,
|
|
490
|
-
* the request will be fulfilled from the configured request handlers
|
|
491
|
-
* and the cache will be updated before returning the response.
|
|
492
|
-
*
|
|
493
|
-
* @public
|
|
494
|
-
* @return true if the request is considered hard expired
|
|
495
|
-
*/
|
|
496
|
-
isHardExpired(cacheKey, store) {
|
|
497
|
-
// if we are explicitly invalidated, we are hard expired
|
|
498
|
-
const storeCache = this._getStore(store);
|
|
499
|
-
if (storeCache.invalidated.has(cacheKey)) {
|
|
500
|
-
return true;
|
|
501
|
-
}
|
|
502
|
-
const cache = store.cache;
|
|
503
|
-
const cached = cache.peekRequest(cacheKey);
|
|
504
|
-
if (!cached?.response) {
|
|
505
|
-
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
506
|
-
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
507
|
-
// eslint-disable-next-line no-console
|
|
508
|
-
console.log(`CachePolicy: ${cacheKey.lid} is EXPIRED because no cache entry was found`);
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
return true;
|
|
512
|
-
}
|
|
513
|
-
return isExpired(cacheKey, cached, this.config);
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
/**
|
|
517
|
-
* Invoked if `isHardExpired` is false to determine if the request
|
|
518
|
-
* should be update behind the scenes if cache data is already available.
|
|
519
|
-
*
|
|
520
|
-
* Note, this is only invoked by the CacheHandler if the request has
|
|
521
|
-
* a cache-key.
|
|
522
|
-
*
|
|
523
|
-
* If true, the request will be fulfilled from cache while a backgrounded
|
|
524
|
-
* request is made to update the cache via the configured request handlers.
|
|
525
|
-
*
|
|
526
|
-
* @public
|
|
527
|
-
* @return true if the request is considered soft expired
|
|
528
|
-
*/
|
|
529
|
-
isSoftExpired(cacheKey, store) {
|
|
530
|
-
if (macroCondition(getGlobalConfig().WarpDrive.env.TESTING)) {
|
|
531
|
-
if (!this.config.disableTestOptimization) {
|
|
532
|
-
return false;
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
const cache = store.cache;
|
|
536
|
-
const cached = cache.peekRequest(cacheKey);
|
|
537
|
-
if (cached?.response) {
|
|
538
|
-
const date = cached.response.headers.get('date');
|
|
539
|
-
if (!date) {
|
|
540
|
-
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
541
|
-
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
542
|
-
// eslint-disable-next-line no-console
|
|
543
|
-
console.log(`CachePolicy: ${cacheKey.lid} is STALE because no date header was found`);
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
return true;
|
|
547
|
-
} else {
|
|
548
|
-
const time = new Date(date).getTime();
|
|
549
|
-
const now = Date.now();
|
|
550
|
-
const deadline = time + this.config.apiCacheSoftExpires;
|
|
551
|
-
const result = now >= deadline;
|
|
552
|
-
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
553
|
-
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
554
|
-
// eslint-disable-next-line no-console
|
|
555
|
-
console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'STALE' : 'NOT stale'}. Expiration time: ${deadline}, now: ${now}`);
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
return result;
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
562
|
-
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
563
|
-
// eslint-disable-next-line no-console
|
|
564
|
-
console.log(`CachePolicy: ${cacheKey.lid} is STALE because no cache entry was found`);
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
return true;
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
export { DefaultCachePolicy, parseCacheControl };
|
|
1
|
+
export { D as DefaultCachePolicy, p as parseCacheControl } from "./default-cache-policy-D7_u4YRH.js";
|
package/dist/types/-private.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
2
2
|
const name = "@warp-drive/core";
|
|
3
|
-
const version = "5.8.0-alpha.
|
|
3
|
+
const version = "5.8.0-alpha.7";
|
|
4
4
|
|
|
5
5
|
// in testing mode, we utilize globals to ensure only one copy exists of
|
|
6
6
|
// these maps, due to bugs in ember-auto-import
|
package/dist/types/request.js
CHANGED
|
@@ -8,7 +8,8 @@ const IS_FUTURE = getOrSetGlobal('IS_FUTURE', Symbol('IS_FUTURE'));
|
|
|
8
8
|
const STRUCTURED = getOrSetGlobal('DOC', Symbol('DOC'));
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* Use these options to adjust CacheHandler behavior for a request
|
|
11
|
+
* Use these options to adjust {@link CacheHandler} behavior for a request
|
|
12
|
+
* via {@link RequestInfo.cacheOptions}.
|
|
12
13
|
*
|
|
13
14
|
*/
|
|
14
15
|
|
|
@@ -69,7 +70,4 @@ const STRUCTURED = getOrSetGlobal('DOC', Symbol('DOC'));
|
|
|
69
70
|
*
|
|
70
71
|
*/
|
|
71
72
|
|
|
72
|
-
|
|
73
|
-
return obj;
|
|
74
|
-
}
|
|
75
|
-
export { EnableHydration, IS_FUTURE, STRUCTURED, SkipCache, withBrand };
|
|
73
|
+
export { EnableHydration, IS_FUTURE, STRUCTURED, SkipCache };
|
|
@@ -443,6 +443,20 @@
|
|
|
443
443
|
* @public
|
|
444
444
|
*/
|
|
445
445
|
|
|
446
|
+
/**
|
|
447
|
+
* A trait for use on a PolarisMode record
|
|
448
|
+
*/
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* A trait for use on a LegacyMode record
|
|
452
|
+
*/
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* A union of
|
|
456
|
+
* - {@link LegacyTrait}
|
|
457
|
+
* - {@link PolarisTrait}
|
|
458
|
+
*/
|
|
459
|
+
|
|
446
460
|
/**
|
|
447
461
|
* A no-op type utility that enables type-checking resource schema
|
|
448
462
|
* definitions.
|
package/dist/types/symbols.js
CHANGED
|
@@ -11,7 +11,7 @@ const RecordStore = getOrSetGlobal('Store', Symbol('Store'));
|
|
|
11
11
|
* record implementations to provide a typescript
|
|
12
12
|
* hint for the type of the resource.
|
|
13
13
|
*
|
|
14
|
-
* When used,
|
|
14
|
+
* When used, WarpDrive APIs can
|
|
15
15
|
* take advantage of this to provide better type
|
|
16
16
|
* safety and intellisense.
|
|
17
17
|
*
|
|
@@ -48,7 +48,7 @@ const Type = getOrSetGlobal('$type', Symbol('$type'));
|
|
|
48
48
|
* record implementations to provide a typescript
|
|
49
49
|
* hint for the type of the resource.
|
|
50
50
|
*
|
|
51
|
-
* When used,
|
|
51
|
+
* When used, WarpDrive APIs can
|
|
52
52
|
* take advantage of this to provide better type
|
|
53
53
|
* safety and intellisense.
|
|
54
54
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@warp-drive/core",
|
|
3
|
-
"version": "5.8.0-alpha.
|
|
3
|
+
"version": "5.8.0-alpha.7",
|
|
4
4
|
"description": "Core package for WarpDrive | All the Universal Basics",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ember-addon"
|
|
@@ -37,13 +37,13 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@embroider/macros": "^1.18.1",
|
|
40
|
-
"@warp-drive/build-config": "5.8.0-alpha.
|
|
40
|
+
"@warp-drive/build-config": "5.8.0-alpha.7"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@babel/core": "^7.28.3",
|
|
44
44
|
"@babel/plugin-transform-typescript": "^7.28.0",
|
|
45
45
|
"@babel/preset-typescript": "^7.27.1",
|
|
46
|
-
"@warp-drive/internal-config": "5.8.0-alpha.
|
|
46
|
+
"@warp-drive/internal-config": "5.8.0-alpha.7",
|
|
47
47
|
"decorator-transforms": "^2.3.0",
|
|
48
48
|
"ember-source": "~6.6.0",
|
|
49
49
|
"expect-type": "^1.2.2",
|