@warp-drive/core 5.8.0-alpha.3 → 5.8.0-alpha.30
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/README.md +22 -38
- package/declarations/build-config.d.ts +18 -1
- package/declarations/graph/-private/-edge-definition.d.ts +12 -2
- package/declarations/index.d.ts +82 -3
- package/declarations/reactive/-private/document.d.ts +58 -46
- package/declarations/reactive/-private/record.d.ts +10 -1
- package/declarations/reactive/-private/schema.d.ts +77 -4
- package/declarations/reactive/-private.d.ts +1 -0
- package/declarations/reactive.d.ts +13 -7
- package/declarations/request/-private/types.d.ts +1 -1
- package/declarations/request.d.ts +47 -0
- package/declarations/store/-private/caches/instance-cache.d.ts +4 -5
- package/declarations/store/-private/default-cache-policy.d.ts +147 -129
- package/declarations/store/-private/managers/cache-capabilities-manager.d.ts +1 -1
- package/declarations/store/-private/managers/cache-key-manager.d.ts +26 -8
- package/declarations/store/-private/managers/cache-manager.d.ts +6 -4
- package/declarations/store/-private/managers/notification-manager.d.ts +1 -1
- package/declarations/store/-private/new-core-tmp/promise-state.d.ts +1 -0
- package/declarations/store/-private/new-core-tmp/request-state.d.ts +1 -1
- package/declarations/store/-private/store-service.d.ts +43 -64
- package/declarations/store/-private.d.ts +0 -1
- package/declarations/store/-types/q/cache-capabilities-manager.d.ts +1 -1
- package/declarations/store/deprecated/store.d.ts +33 -32
- package/declarations/store.d.ts +1 -0
- package/declarations/types/cache.d.ts +8 -6
- package/declarations/types/record.d.ts +132 -0
- package/declarations/types/request.d.ts +26 -14
- package/declarations/types/schema/fields.d.ts +30 -6
- package/declarations/{store/-types/q → types/schema}/schema-service.d.ts +11 -9
- package/declarations/types/spec/document.d.ts +34 -0
- package/declarations/types/symbols.d.ts +2 -2
- package/declarations/types.d.ts +1 -1
- package/dist/build-config.js +1 -1
- package/dist/default-cache-policy-D7_u4YRH.js +572 -0
- package/dist/graph/-private.js +13 -4
- package/dist/{request-state-CUuZzgvE.js → index-CHrZ1B2X.js} +10081 -8924
- package/dist/index.js +6 -382
- package/dist/reactive.js +4 -778
- package/dist/{context-C_7OLieY.js → request-oqoLC9rz.js} +219 -172
- package/dist/request.js +1 -1
- package/dist/store/-private.js +1 -1
- package/dist/store.js +1 -533
- package/dist/types/-private.js +1 -1
- package/dist/types/record.js +127 -0
- package/dist/types/request.js +14 -12
- package/dist/types/schema/fields.js +14 -0
- package/dist/types/schema/schema-service.js +0 -0
- package/dist/types/symbols.js +2 -2
- package/logos/README.md +2 -2
- package/logos/logo-yellow-slab.svg +1 -0
- package/logos/word-mark-black.svg +1 -0
- package/logos/word-mark-white.svg +1 -0
- package/package.json +3 -3
- package/logos/NCC-1701-a-blue.svg +0 -4
- package/logos/NCC-1701-a-gold.svg +0 -4
- package/logos/NCC-1701-a-gold_100.svg +0 -1
- package/logos/NCC-1701-a-gold_base-64.txt +0 -1
- package/logos/NCC-1701-a.svg +0 -4
- package/logos/docs-badge.svg +0 -2
- package/logos/ember-data-logo-dark.svg +0 -12
- package/logos/ember-data-logo-light.svg +0 -12
- package/logos/social1.png +0 -0
- package/logos/social2.png +0 -0
- package/logos/warp-drive-logo-dark.svg +0 -4
- package/logos/warp-drive-logo-gold.svg +0 -4
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
import { deprecate } from '@ember/debug';
|
|
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
|
+
*/
|
|
38
|
+
function parseCacheControl(header) {
|
|
39
|
+
return CACHE_CONTROL_CACHE.get(header);
|
|
40
|
+
}
|
|
41
|
+
const CACHE_CONTROL_CACHE = new LRUCache(header => {
|
|
42
|
+
let key = '';
|
|
43
|
+
let value = '';
|
|
44
|
+
let isParsingKey = true;
|
|
45
|
+
const cacheControlValue = {};
|
|
46
|
+
for (let i = 0; i < header.length; i++) {
|
|
47
|
+
const char = header.charAt(i);
|
|
48
|
+
if (char === ',') {
|
|
49
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
50
|
+
if (!test) {
|
|
51
|
+
throw new Error(`Invalid Cache-Control value, expected a value`);
|
|
52
|
+
}
|
|
53
|
+
})(!isParsingKey || !NUMERIC_KEYS.has(key)) : {};
|
|
54
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
55
|
+
if (!test) {
|
|
56
|
+
throw new Error(`Invalid Cache-Control value, expected a value after "=" but got ","`);
|
|
57
|
+
}
|
|
58
|
+
})(i === 0 || header.charAt(i - 1) !== '=') : {};
|
|
59
|
+
isParsingKey = true;
|
|
60
|
+
// @ts-expect-error TS incorrectly thinks that optional keys must have a type that includes undefined
|
|
61
|
+
cacheControlValue[key] = NUMERIC_KEYS.has(key) ? parseCacheControlValue(value) : true;
|
|
62
|
+
key = '';
|
|
63
|
+
value = '';
|
|
64
|
+
continue;
|
|
65
|
+
} else if (char === '=') {
|
|
66
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
67
|
+
if (!test) {
|
|
68
|
+
throw new Error(`Invalid Cache-Control value, expected a value after "="`);
|
|
69
|
+
}
|
|
70
|
+
})(i + 1 !== header.length) : {};
|
|
71
|
+
isParsingKey = false;
|
|
72
|
+
} else if (char === ' ' || char === `\t` || char === `\n`) {
|
|
73
|
+
continue;
|
|
74
|
+
} else if (isParsingKey) {
|
|
75
|
+
key += char;
|
|
76
|
+
} else {
|
|
77
|
+
value += char;
|
|
78
|
+
}
|
|
79
|
+
if (i === header.length - 1) {
|
|
80
|
+
// @ts-expect-error TS incorrectly thinks that optional keys must have a type that includes undefined
|
|
81
|
+
cacheControlValue[key] = NUMERIC_KEYS.has(key) ? parseCacheControlValue(value) : true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return cacheControlValue;
|
|
85
|
+
}, 200);
|
|
86
|
+
function parseCacheControlValue(stringToParse) {
|
|
87
|
+
const parsedValue = Number.parseInt(stringToParse);
|
|
88
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
89
|
+
if (!test) {
|
|
90
|
+
throw new Error(`Invalid Cache-Control value, expected a number but got - ${stringToParse}`);
|
|
91
|
+
}
|
|
92
|
+
})(!Number.isNaN(parsedValue)) : {};
|
|
93
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
94
|
+
if (!test) {
|
|
95
|
+
throw new Error(`Invalid Cache-Control value, expected a number greater than 0 but got - ${stringToParse}`);
|
|
96
|
+
}
|
|
97
|
+
})(parsedValue >= 0) : {};
|
|
98
|
+
if (Number.isNaN(parsedValue) || parsedValue < 0) {
|
|
99
|
+
return 0;
|
|
100
|
+
}
|
|
101
|
+
return parsedValue;
|
|
102
|
+
}
|
|
103
|
+
function isExpired(cacheKey, request, config) {
|
|
104
|
+
const {
|
|
105
|
+
constraints
|
|
106
|
+
} = config;
|
|
107
|
+
if (constraints?.isExpired) {
|
|
108
|
+
const result = constraints.isExpired(request);
|
|
109
|
+
if (result !== null) {
|
|
110
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
111
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
112
|
+
// eslint-disable-next-line no-console
|
|
113
|
+
console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because constraints.isExpired returned ${result}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const {
|
|
120
|
+
headers
|
|
121
|
+
} = request.response;
|
|
122
|
+
if (!headers) {
|
|
123
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
124
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
125
|
+
// eslint-disable-next-line no-console
|
|
126
|
+
console.log(`CachePolicy: ${cacheKey.lid} is EXPIRED because no headers were provided`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// if we have no headers then both the headers based expiration
|
|
131
|
+
// and the time based expiration will be considered expired
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// check for X-WarpDrive-Expires
|
|
136
|
+
const now = Date.now();
|
|
137
|
+
const date = headers.get('Date');
|
|
138
|
+
if (constraints?.headers) {
|
|
139
|
+
if (constraints.headers['X-WarpDrive-Expires']) {
|
|
140
|
+
const xWarpDriveExpires = headers.get('X-WarpDrive-Expires');
|
|
141
|
+
if (xWarpDriveExpires) {
|
|
142
|
+
const expirationTime = new Date(xWarpDriveExpires).getTime();
|
|
143
|
+
const result = now >= expirationTime;
|
|
144
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
145
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
146
|
+
// eslint-disable-next-line no-console
|
|
147
|
+
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'}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// check for Cache-Control
|
|
155
|
+
if (constraints.headers['Cache-Control']) {
|
|
156
|
+
const cacheControl = headers.get('Cache-Control');
|
|
157
|
+
const age = headers.get('Age');
|
|
158
|
+
if (cacheControl && age && date) {
|
|
159
|
+
const cacheControlValue = parseCacheControl(cacheControl);
|
|
160
|
+
|
|
161
|
+
// max-age and s-maxage are stored in
|
|
162
|
+
const maxAge = cacheControlValue['max-age'] || cacheControlValue['s-maxage'];
|
|
163
|
+
if (maxAge) {
|
|
164
|
+
// age is stored in seconds
|
|
165
|
+
const ageValue = parseInt(age, 10);
|
|
166
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
167
|
+
if (!test) {
|
|
168
|
+
throw new Error(`Invalid Cache-Control value, expected a number but got - ${age}`);
|
|
169
|
+
}
|
|
170
|
+
})(!Number.isNaN(ageValue)) : {};
|
|
171
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
172
|
+
if (!test) {
|
|
173
|
+
throw new Error(`Invalid Cache-Control value, expected a number greater than 0 but got - ${age}`);
|
|
174
|
+
}
|
|
175
|
+
})(ageValue >= 0) : {};
|
|
176
|
+
if (!Number.isNaN(ageValue) && ageValue >= 0) {
|
|
177
|
+
const dateValue = new Date(date).getTime();
|
|
178
|
+
const expirationTime = dateValue + (maxAge - ageValue) * 1000;
|
|
179
|
+
const result = now >= expirationTime;
|
|
180
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
181
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
182
|
+
// eslint-disable-next-line no-console
|
|
183
|
+
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'}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// check for Expires
|
|
193
|
+
if (constraints.headers.Expires) {
|
|
194
|
+
const expires = headers.get('Expires');
|
|
195
|
+
if (expires) {
|
|
196
|
+
const expirationTime = new Date(expires).getTime();
|
|
197
|
+
const result = now >= expirationTime;
|
|
198
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
199
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
200
|
+
// eslint-disable-next-line no-console
|
|
201
|
+
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'}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// check for Date
|
|
210
|
+
if (!date) {
|
|
211
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
212
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
213
|
+
// eslint-disable-next-line no-console
|
|
214
|
+
console.log(`CachePolicy: ${cacheKey.lid} is EXPIRED because no Date header was provided`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
let expirationTime = config.apiCacheHardExpires;
|
|
220
|
+
if (macroCondition(getGlobalConfig().WarpDrive.env.TESTING)) {
|
|
221
|
+
if (!config.disableTestOptimization) {
|
|
222
|
+
expirationTime = config.apiCacheSoftExpires;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const time = new Date(date).getTime();
|
|
226
|
+
const deadline = time + expirationTime;
|
|
227
|
+
const result = now >= deadline;
|
|
228
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
229
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
230
|
+
// eslint-disable-next-line no-console
|
|
231
|
+
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'}`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return result;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* The constraint options for {@link PolicyConfig.constraints}
|
|
239
|
+
*/
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* The configuration options for the {@link DefaultCachePolicy}
|
|
243
|
+
*
|
|
244
|
+
* ```ts [app/services/store.ts]
|
|
245
|
+
* import { Store } from '@warp-drive/core';
|
|
246
|
+
* import { DefaultCachePolicy } from '@warp-drive/core/store'; // [!code focus]
|
|
247
|
+
*
|
|
248
|
+
* export default class AppStore extends Store {
|
|
249
|
+
* lifetimes = new DefaultCachePolicy({ // [!code focus:3]
|
|
250
|
+
* // ... PolicyConfig Settings ... //
|
|
251
|
+
* });
|
|
252
|
+
* }
|
|
253
|
+
* ```
|
|
254
|
+
*
|
|
255
|
+
*/
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* A basic {@link CachePolicy} that can be added to the Store service.
|
|
259
|
+
*
|
|
260
|
+
* ```ts [app/services/store.ts]
|
|
261
|
+
* import { Store } from '@warp-drive/core';
|
|
262
|
+
* import { DefaultCachePolicy } from '@warp-drive/core/store'; // [!code focus]
|
|
263
|
+
*
|
|
264
|
+
* export default class AppStore extends Store {
|
|
265
|
+
* lifetimes = new DefaultCachePolicy({ // [!code focus:5]
|
|
266
|
+
* apiCacheSoftExpires: 30_000,
|
|
267
|
+
* apiCacheHardExpires: 60_000,
|
|
268
|
+
* // ... Other PolicyConfig Settings ... //
|
|
269
|
+
* });
|
|
270
|
+
* }
|
|
271
|
+
* ```
|
|
272
|
+
*
|
|
273
|
+
* :::tip 💡 TIP
|
|
274
|
+
* Date headers do not have millisecond precision, so expiration times should
|
|
275
|
+
* generally be larger than 1000ms.
|
|
276
|
+
* :::
|
|
277
|
+
*
|
|
278
|
+
* See also {@link PolicyConfig} for configuration options.
|
|
279
|
+
*
|
|
280
|
+
* ### The Mechanics
|
|
281
|
+
*
|
|
282
|
+
* This policy determines staleness based on various configurable constraints falling back to a simple
|
|
283
|
+
* check of the time elapsed since the request was last received from the API using the `date` header
|
|
284
|
+
* from the last response.
|
|
285
|
+
*
|
|
286
|
+
* :::tip 💡 TIP
|
|
287
|
+
* The {@link Fetch} handler provided by `@warp-drive/core` will automatically
|
|
288
|
+
* add the `date` header to responses if it is not present.
|
|
289
|
+
* :::
|
|
290
|
+
*
|
|
291
|
+
* - For manual override of reload see {@link CacheOptions.reload | cacheOptions.reload}
|
|
292
|
+
* - For manual override of background reload see {@link CacheOptions.backgroundReload | cacheOptions.backgroundReload}
|
|
293
|
+
*
|
|
294
|
+
* In order expiration is determined by:
|
|
295
|
+
*
|
|
296
|
+
* ```md
|
|
297
|
+
* Is explicitly invalidated by `cacheOptions.reload`
|
|
298
|
+
* ↳ (if falsey) if the request has been explicitly invalidated
|
|
299
|
+
* since the last request (see Automatic Invalidation below)
|
|
300
|
+
* ↳ (if false) (If Active) isExpired function
|
|
301
|
+
* ↳ (if null) (If Active) X-WarpDrive-Expires header
|
|
302
|
+
* ↳ (if null) (If Active) Cache-Control header
|
|
303
|
+
* ↳ (if null) (If Active) Expires header
|
|
304
|
+
* ↳ (if null) Date header + apiCacheHardExpires < current time
|
|
305
|
+
*
|
|
306
|
+
* -- <if above is false, a background request is issued if> --
|
|
307
|
+
*
|
|
308
|
+
* ↳ is invalidated by `cacheOptions.backgroundReload`
|
|
309
|
+
* ↳ (if falsey) Date header + apiCacheSoftExpires < current time
|
|
310
|
+
* ```
|
|
311
|
+
*
|
|
312
|
+
* ### Automatic Invalidation / Entanglement
|
|
313
|
+
*
|
|
314
|
+
* It also invalidates any request with an {@link RequestInfo.op | OpCode} of `"query"`
|
|
315
|
+
* for which {@link CacheOptions.types | cacheOptions.types} was provided
|
|
316
|
+
* when a request with an `OpCode` of `"createRecord"` is successful and also includes
|
|
317
|
+
* a matching type in its own `cacheOptions.types` array.
|
|
318
|
+
|
|
319
|
+
* :::tip 💡 TIP
|
|
320
|
+
* Abstracting this behavior via builders is recommended to ensure consistency.
|
|
321
|
+
* :::
|
|
322
|
+
*
|
|
323
|
+
* ### Testing
|
|
324
|
+
*
|
|
325
|
+
* In Testing environments:
|
|
326
|
+
*
|
|
327
|
+
* - `apiCacheSoftExpires` will always be `false`
|
|
328
|
+
* - `apiCacheHardExpires` will use the `apiCacheSoftExpires` value.
|
|
329
|
+
*
|
|
330
|
+
* This helps reduce flakiness and produce predictably rendered results in test suites.
|
|
331
|
+
*
|
|
332
|
+
* Requests that specifically set `cacheOptions.backgroundReload = true` will
|
|
333
|
+
* still be background reloaded in tests.
|
|
334
|
+
*
|
|
335
|
+
* This behavior can be opted out of by setting `disableTestOptimization = true`
|
|
336
|
+
* in the policy config.
|
|
337
|
+
*
|
|
338
|
+
* @public
|
|
339
|
+
*/
|
|
340
|
+
class DefaultCachePolicy {
|
|
341
|
+
/**
|
|
342
|
+
* @internal
|
|
343
|
+
*/
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* @internal
|
|
347
|
+
*/
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* @internal
|
|
351
|
+
*/
|
|
352
|
+
_getStore(store) {
|
|
353
|
+
let set = this._stores.get(store);
|
|
354
|
+
if (!set) {
|
|
355
|
+
set = {
|
|
356
|
+
invalidated: new Set(),
|
|
357
|
+
types: new Map()
|
|
358
|
+
};
|
|
359
|
+
this._stores.set(store, set);
|
|
360
|
+
}
|
|
361
|
+
return set;
|
|
362
|
+
}
|
|
363
|
+
constructor(config) {
|
|
364
|
+
this._stores = new WeakMap();
|
|
365
|
+
const _config = arguments.length === 1 ? config : arguments[1];
|
|
366
|
+
deprecate(`Passing a Store to the CachePolicy is deprecated, please pass only a config instead.`, arguments.length === 1, {
|
|
367
|
+
id: 'ember-data:request-utils:lifetimes-service-store-arg',
|
|
368
|
+
since: {
|
|
369
|
+
enabled: '5.4',
|
|
370
|
+
available: '4.13'
|
|
371
|
+
},
|
|
372
|
+
for: '@ember-data/request-utils',
|
|
373
|
+
until: '6.0'
|
|
374
|
+
});
|
|
375
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
376
|
+
if (!test) {
|
|
377
|
+
throw new Error(`You must pass a config to the CachePolicy`);
|
|
378
|
+
}
|
|
379
|
+
})(_config) : {};
|
|
380
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
381
|
+
if (!test) {
|
|
382
|
+
throw new Error(`You must pass a apiCacheSoftExpires to the CachePolicy`);
|
|
383
|
+
}
|
|
384
|
+
})(typeof _config.apiCacheSoftExpires === 'number') : {};
|
|
385
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
386
|
+
if (!test) {
|
|
387
|
+
throw new Error(`You must pass a apiCacheHardExpires to the CachePolicy`);
|
|
388
|
+
}
|
|
389
|
+
})(typeof _config.apiCacheHardExpires === 'number') : {};
|
|
390
|
+
this.config = _config;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Invalidate a request by its CacheKey for the given store instance.
|
|
395
|
+
*
|
|
396
|
+
* While the store argument may seem redundant, the CachePolicy
|
|
397
|
+
* is designed to be shared across multiple stores / forks
|
|
398
|
+
* of the store.
|
|
399
|
+
*
|
|
400
|
+
* ```ts
|
|
401
|
+
* store.lifetimes.invalidateRequest(store, cacheKey);
|
|
402
|
+
* ```
|
|
403
|
+
*
|
|
404
|
+
* @public
|
|
405
|
+
*/
|
|
406
|
+
invalidateRequest(cacheKey, store) {
|
|
407
|
+
this._getStore(store).invalidated.add(cacheKey);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Invalidate all requests associated to a specific type
|
|
412
|
+
* for a given store instance.
|
|
413
|
+
*
|
|
414
|
+
* While the store argument may seem redundant, the CachePolicy
|
|
415
|
+
* is designed to be shared across multiple stores / forks
|
|
416
|
+
* of the store.
|
|
417
|
+
*
|
|
418
|
+
* This invalidation is done automatically when using this service
|
|
419
|
+
* for both the {@link CacheHandler} and the [NetworkHandler](/api/@warp-drive/legacy/compat/variables/LegacyNetworkHandler).
|
|
420
|
+
*
|
|
421
|
+
* ```ts
|
|
422
|
+
* store.lifetimes.invalidateRequestsForType(store, 'person');
|
|
423
|
+
* ```
|
|
424
|
+
*
|
|
425
|
+
* @public
|
|
426
|
+
*/
|
|
427
|
+
invalidateRequestsForType(type, store) {
|
|
428
|
+
const storeCache = this._getStore(store);
|
|
429
|
+
const set = storeCache.types.get(type);
|
|
430
|
+
const notifications = store.notifications;
|
|
431
|
+
if (set) {
|
|
432
|
+
// TODO batch notifications
|
|
433
|
+
set.forEach(id => {
|
|
434
|
+
storeCache.invalidated.add(id);
|
|
435
|
+
// @ts-expect-error
|
|
436
|
+
notifications.notify(id, 'invalidated', null);
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Invoked when a request has been fulfilled from the configured request handlers.
|
|
443
|
+
* This is invoked by the CacheHandler for both foreground and background requests
|
|
444
|
+
* once the cache has been updated.
|
|
445
|
+
*
|
|
446
|
+
* Note, this is invoked by the {@link CacheHandler} regardless of whether
|
|
447
|
+
* the request has a cache-key.
|
|
448
|
+
*
|
|
449
|
+
* This method should not be invoked directly by consumers.
|
|
450
|
+
*
|
|
451
|
+
* @public
|
|
452
|
+
*/
|
|
453
|
+
didRequest(request, response, cacheKey, store) {
|
|
454
|
+
// if this is a successful createRecord request, invalidate the cacheKey for the type
|
|
455
|
+
if (request.op === 'createRecord') {
|
|
456
|
+
const statusNumber = response?.status ?? 0;
|
|
457
|
+
if (statusNumber >= 200 && statusNumber < 400) {
|
|
458
|
+
const types = new Set(request.records?.map(r => r.type));
|
|
459
|
+
const additionalTypes = request.cacheOptions?.types;
|
|
460
|
+
additionalTypes?.forEach(type => {
|
|
461
|
+
types.add(type);
|
|
462
|
+
});
|
|
463
|
+
types.forEach(type => {
|
|
464
|
+
this.invalidateRequestsForType(type, store);
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// add this document's cacheKey to a map for all associated types
|
|
469
|
+
// it is recommended to only use this for queries
|
|
470
|
+
} else if (cacheKey && request.cacheOptions?.types?.length) {
|
|
471
|
+
const storeCache = this._getStore(store);
|
|
472
|
+
request.cacheOptions?.types.forEach(type => {
|
|
473
|
+
const set = storeCache.types.get(type);
|
|
474
|
+
if (set) {
|
|
475
|
+
set.add(cacheKey);
|
|
476
|
+
storeCache.invalidated.delete(cacheKey);
|
|
477
|
+
} else {
|
|
478
|
+
storeCache.types.set(type, new Set([cacheKey]));
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Invoked to determine if the request may be fulfilled from cache
|
|
486
|
+
* if possible.
|
|
487
|
+
*
|
|
488
|
+
* Note, this is only invoked by the {@link CacheHandler} if the request has
|
|
489
|
+
* a cache-key.
|
|
490
|
+
*
|
|
491
|
+
* If no cache entry is found or the entry is hard expired,
|
|
492
|
+
* the request will be fulfilled from the configured request handlers
|
|
493
|
+
* and the cache will be updated before returning the response.
|
|
494
|
+
*
|
|
495
|
+
* @public
|
|
496
|
+
* @return true if the request is considered hard expired
|
|
497
|
+
*/
|
|
498
|
+
isHardExpired(cacheKey, store) {
|
|
499
|
+
// if we are explicitly invalidated, we are hard expired
|
|
500
|
+
const storeCache = this._getStore(store);
|
|
501
|
+
if (storeCache.invalidated.has(cacheKey)) {
|
|
502
|
+
return true;
|
|
503
|
+
}
|
|
504
|
+
const cache = store.cache;
|
|
505
|
+
const cached = cache.peekRequest(cacheKey);
|
|
506
|
+
if (!cached?.response) {
|
|
507
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
508
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
509
|
+
// eslint-disable-next-line no-console
|
|
510
|
+
console.log(`CachePolicy: ${cacheKey.lid} is EXPIRED because no cache entry was found`);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return true;
|
|
514
|
+
}
|
|
515
|
+
return isExpired(cacheKey, cached, this.config);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Invoked if `isHardExpired` is false to determine if the request
|
|
520
|
+
* should be update behind the scenes if cache data is already available.
|
|
521
|
+
*
|
|
522
|
+
* Note, this is only invoked by the {@link CacheHandler} if the request has
|
|
523
|
+
* a cache-key.
|
|
524
|
+
*
|
|
525
|
+
* If true, the request will be fulfilled from cache while a backgrounded
|
|
526
|
+
* request is made to update the cache via the configured request handlers.
|
|
527
|
+
*
|
|
528
|
+
* @public
|
|
529
|
+
* @return true if the request is considered soft expired
|
|
530
|
+
*/
|
|
531
|
+
isSoftExpired(cacheKey, store) {
|
|
532
|
+
if (macroCondition(getGlobalConfig().WarpDrive.env.TESTING)) {
|
|
533
|
+
if (!this.config.disableTestOptimization) {
|
|
534
|
+
return false;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
const cache = store.cache;
|
|
538
|
+
const cached = cache.peekRequest(cacheKey);
|
|
539
|
+
if (cached?.response) {
|
|
540
|
+
const date = cached.response.headers.get('date');
|
|
541
|
+
if (!date) {
|
|
542
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
543
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
544
|
+
// eslint-disable-next-line no-console
|
|
545
|
+
console.log(`CachePolicy: ${cacheKey.lid} is STALE because no date header was found`);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
return true;
|
|
549
|
+
} else {
|
|
550
|
+
const time = new Date(date).getTime();
|
|
551
|
+
const now = Date.now();
|
|
552
|
+
const deadline = time + this.config.apiCacheSoftExpires;
|
|
553
|
+
const result = now >= deadline;
|
|
554
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
555
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
556
|
+
// eslint-disable-next-line no-console
|
|
557
|
+
console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'STALE' : 'NOT stale'}. Expiration time: ${deadline}, now: ${now}`);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
return result;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
|
|
564
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
|
|
565
|
+
// eslint-disable-next-line no-console
|
|
566
|
+
console.log(`CachePolicy: ${cacheKey.lid} is STALE because no cache entry was found`);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
return true;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
export { DefaultCachePolicy as D, parseCacheControl as p };
|
package/dist/graph/-private.js
CHANGED
|
@@ -349,6 +349,7 @@ function implicitKeyFor(type, key) {
|
|
|
349
349
|
function syncMeta(definition, inverseDefinition) {
|
|
350
350
|
definition.inverseKind = inverseDefinition.kind;
|
|
351
351
|
definition.inverseKey = inverseDefinition.key;
|
|
352
|
+
definition.inverseName = inverseDefinition.name;
|
|
352
353
|
definition.inverseType = inverseDefinition.type;
|
|
353
354
|
definition.inverseIsAsync = inverseDefinition.isAsync;
|
|
354
355
|
definition.inverseIsCollection = inverseDefinition.isCollection;
|
|
@@ -366,7 +367,8 @@ function upgradeMeta(meta) {
|
|
|
366
367
|
const niceMeta = {};
|
|
367
368
|
const options = meta.options;
|
|
368
369
|
niceMeta.kind = meta.kind;
|
|
369
|
-
niceMeta.key = meta.name;
|
|
370
|
+
niceMeta.key = meta.sourceKey ?? meta.name;
|
|
371
|
+
niceMeta.name = meta.name;
|
|
370
372
|
niceMeta.type = meta.type;
|
|
371
373
|
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
372
374
|
if (!test) {
|
|
@@ -379,6 +381,7 @@ function upgradeMeta(meta) {
|
|
|
379
381
|
niceMeta.isPolymorphic = options && !!options.polymorphic;
|
|
380
382
|
niceMeta.isLinksMode = options.linksMode ?? false;
|
|
381
383
|
niceMeta.inverseKey = options && options.inverse || STR_LATER;
|
|
384
|
+
niceMeta.inverseName = options && options.inverse || STR_LATER;
|
|
382
385
|
niceMeta.inverseType = STR_LATER;
|
|
383
386
|
niceMeta.inverseIsAsync = BOOL_LATER;
|
|
384
387
|
niceMeta.inverseIsImplicit = options && options.inverse === null || BOOL_LATER;
|
|
@@ -502,12 +505,13 @@ function upgradeDefinition(graph, key, propertyName, isImplicit = false) {
|
|
|
502
505
|
}
|
|
503
506
|
})(!isImplicit) : {};
|
|
504
507
|
const relationships = storeWrapper.schema.fields(key);
|
|
508
|
+
const relationshipsBySourceKey = storeWrapper.schema.cacheFields?.(key) ?? relationships;
|
|
505
509
|
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
506
510
|
if (!test) {
|
|
507
511
|
throw new Error(`Expected to have a relationship definition for ${type} but none was found.`);
|
|
508
512
|
}
|
|
509
513
|
})(relationships) : {};
|
|
510
|
-
const meta =
|
|
514
|
+
const meta = relationshipsBySourceKey.get(propertyName);
|
|
511
515
|
if (!meta) {
|
|
512
516
|
// TODO potentially we should just be permissive here since this is an implicit relationship
|
|
513
517
|
// and not require the lookup table to be populated
|
|
@@ -564,6 +568,7 @@ function upgradeDefinition(graph, key, propertyName, isImplicit = false) {
|
|
|
564
568
|
kind: 'belongsTo',
|
|
565
569
|
// this must be updated when we find the first belongsTo or hasMany definition that matches
|
|
566
570
|
key: definition.inverseKey,
|
|
571
|
+
name: definition.inverseName,
|
|
567
572
|
type: type,
|
|
568
573
|
isAsync: false,
|
|
569
574
|
// this must be updated when we find the first belongsTo or hasMany definition that matches
|
|
@@ -578,6 +583,7 @@ function upgradeDefinition(graph, key, propertyName, isImplicit = false) {
|
|
|
578
583
|
inverseDefinition = null;
|
|
579
584
|
} else {
|
|
580
585
|
// CASE: We have an explicit inverse or were able to resolve one
|
|
586
|
+
// for the inverse we use "name" for lookup not "sourceKey"
|
|
581
587
|
const inverseDefinitions = storeWrapper.schema.fields({
|
|
582
588
|
type: inverseType
|
|
583
589
|
});
|
|
@@ -710,7 +716,8 @@ function upgradeDefinition(graph, key, propertyName, isImplicit = false) {
|
|
|
710
716
|
return info;
|
|
711
717
|
}
|
|
712
718
|
function inverseForRelationship(store, resourceKey, key) {
|
|
713
|
-
const
|
|
719
|
+
const fields = store.schema.fields(resourceKey);
|
|
720
|
+
const definition = fields.get(key);
|
|
714
721
|
if (!definition) {
|
|
715
722
|
return null;
|
|
716
723
|
}
|
|
@@ -819,6 +826,7 @@ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
|
|
|
819
826
|
function inverseDefinition(definition) {
|
|
820
827
|
return {
|
|
821
828
|
key: definition.inverseKey,
|
|
829
|
+
name: definition.inverseName,
|
|
822
830
|
type: definition.inverseType,
|
|
823
831
|
kind: definition.inverseKind,
|
|
824
832
|
isAsync: definition.inverseIsAsync,
|
|
@@ -827,6 +835,7 @@ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
|
|
|
827
835
|
isCollection: definition.inverseIsCollection,
|
|
828
836
|
isImplicit: definition.inverseIsImplicit,
|
|
829
837
|
inverseKey: definition.key,
|
|
838
|
+
inverseName: definition.name,
|
|
830
839
|
inverseType: definition.type,
|
|
831
840
|
inverseKind: definition.kind,
|
|
832
841
|
inverseIsAsync: definition.isAsync,
|
|
@@ -3331,7 +3340,7 @@ function isReordered(relationship) {
|
|
|
3331
3340
|
/**
|
|
3332
3341
|
Provides a performance tuned normalized graph for intelligently managing relationships between resources based on identity
|
|
3333
3342
|
|
|
3334
|
-
While this Graph is abstract, it currently is a private implementation required as a peer-dependency by the {
|
|
3343
|
+
While this Graph is abstract, it currently is a private implementation required as a peer-dependency by the {json:api} Cache Implementation.
|
|
3335
3344
|
|
|
3336
3345
|
We intend to make this Graph public API after some additional iteration during the 5.x timeframe, until then all APIs should be considered experimental and unstable, not fit for direct application or 3rd party library usage.
|
|
3337
3346
|
|