@warp-drive-mirror/experiments 0.2.6-alpha.4 → 0.2.6-alpha.41
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 +14 -17
- package/dist/data-worker.js +1 -0
- package/dist/unpkg/dev/data-worker.js +380 -0
- package/dist/unpkg/dev/document-storage.js +349 -0
- package/dist/unpkg/dev/image-fetch.js +74 -0
- package/dist/unpkg/dev/image-worker.js +99 -0
- package/dist/unpkg/dev/worker-fetch.js +158 -0
- package/dist/unpkg/dev-deprecated/data-worker.js +380 -0
- package/dist/unpkg/dev-deprecated/document-storage.js +349 -0
- package/dist/unpkg/dev-deprecated/image-fetch.js +74 -0
- package/dist/unpkg/dev-deprecated/image-worker.js +99 -0
- package/dist/unpkg/dev-deprecated/worker-fetch.js +158 -0
- package/dist/unpkg/prod/data-worker.js +366 -0
- package/dist/unpkg/prod/document-storage.js +339 -0
- package/dist/unpkg/prod/image-fetch.js +74 -0
- package/dist/unpkg/prod/image-worker.js +99 -0
- package/dist/unpkg/prod/worker-fetch.js +158 -0
- package/dist/unpkg/prod-deprecated/data-worker.js +366 -0
- package/dist/unpkg/prod-deprecated/document-storage.js +339 -0
- package/dist/unpkg/prod-deprecated/image-fetch.js +74 -0
- package/dist/unpkg/prod-deprecated/image-worker.js +99 -0
- package/dist/unpkg/prod-deprecated/worker-fetch.js +158 -0
- 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 +25 -5
- 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,380 @@
|
|
|
1
|
+
import { DocumentStorage } from './document-storage.js';
|
|
2
|
+
import { assertPrivateStore } from '@warp-drive-mirror/core/store/-private';
|
|
3
|
+
import { SkipCache } from '@warp-drive-mirror/core/types/request';
|
|
4
|
+
|
|
5
|
+
const WorkerScope = globalThis.SharedWorkerGlobalScope;
|
|
6
|
+
class DataWorker {
|
|
7
|
+
constructor(UserStore, options) {
|
|
8
|
+
// disable if running on main thread
|
|
9
|
+
if (typeof window !== 'undefined') {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
this.store = new UserStore();
|
|
13
|
+
this.threads = new Map();
|
|
14
|
+
this.pending = new Map();
|
|
15
|
+
this.options = Object.assign({
|
|
16
|
+
persisted: false,
|
|
17
|
+
scope: ''
|
|
18
|
+
}, options);
|
|
19
|
+
this.isSharedWorker = WorkerScope && globalThis instanceof WorkerScope;
|
|
20
|
+
this.initialize();
|
|
21
|
+
}
|
|
22
|
+
initialize() {
|
|
23
|
+
// enable the CacheHandler to access the worker
|
|
24
|
+
this.store._worker = this;
|
|
25
|
+
if (this.options.persisted) {
|
|
26
|
+
// will be accessed by the worker's CacheHandler off of store
|
|
27
|
+
this.storage = new DocumentStorage({
|
|
28
|
+
scope: this.options.scope
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (this.isSharedWorker) {
|
|
32
|
+
globalThis.onconnect = e => {
|
|
33
|
+
const port = e.ports[0];
|
|
34
|
+
port.onmessage = event => {
|
|
35
|
+
const {
|
|
36
|
+
type
|
|
37
|
+
} = event.data;
|
|
38
|
+
switch (type) {
|
|
39
|
+
case 'connect':
|
|
40
|
+
this.setupThread(event.data.thread, port);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
port.start();
|
|
45
|
+
};
|
|
46
|
+
} else {
|
|
47
|
+
globalThis.onmessage = event => {
|
|
48
|
+
const {
|
|
49
|
+
type
|
|
50
|
+
} = event.data;
|
|
51
|
+
switch (type) {
|
|
52
|
+
case 'connect':
|
|
53
|
+
this.setupThread(event.data.thread, event.ports[0]);
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
setupThread(thread, port) {
|
|
60
|
+
this.threads.set(thread, port);
|
|
61
|
+
this.pending.set(thread, new Map());
|
|
62
|
+
port.onmessage = event => {
|
|
63
|
+
if (event.type === 'close') {
|
|
64
|
+
this.threads.delete(thread);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const {
|
|
68
|
+
type
|
|
69
|
+
} = event.data;
|
|
70
|
+
switch (type) {
|
|
71
|
+
case 'abort':
|
|
72
|
+
this.abortRequest(event.data);
|
|
73
|
+
break;
|
|
74
|
+
case 'request':
|
|
75
|
+
void this.request(prepareRequest(event.data));
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
abortRequest(event) {
|
|
81
|
+
const {
|
|
82
|
+
thread,
|
|
83
|
+
id
|
|
84
|
+
} = event;
|
|
85
|
+
const future = this.pending.get(thread).get(id);
|
|
86
|
+
if (future) {
|
|
87
|
+
future.abort();
|
|
88
|
+
this.pending.get(thread).delete(id);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async request(event) {
|
|
92
|
+
const {
|
|
93
|
+
thread,
|
|
94
|
+
id,
|
|
95
|
+
data
|
|
96
|
+
} = event;
|
|
97
|
+
try {
|
|
98
|
+
const future = this.store.request(data);
|
|
99
|
+
this.pending.get(thread).set(id, future);
|
|
100
|
+
const result = await future;
|
|
101
|
+
this.threads.get(thread)?.postMessage({
|
|
102
|
+
type: 'success-response',
|
|
103
|
+
id,
|
|
104
|
+
thread,
|
|
105
|
+
data: prepareResponse(result)
|
|
106
|
+
});
|
|
107
|
+
} catch (error) {
|
|
108
|
+
if (isAbortError(error)) return;
|
|
109
|
+
this.threads.get(thread)?.postMessage({
|
|
110
|
+
type: 'error-response',
|
|
111
|
+
id,
|
|
112
|
+
thread,
|
|
113
|
+
data: error
|
|
114
|
+
});
|
|
115
|
+
} finally {
|
|
116
|
+
this.pending.get(thread).delete(id);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function softCloneResponse(response) {
|
|
121
|
+
if (!response) return null;
|
|
122
|
+
const clone = {};
|
|
123
|
+
if (response.headers) {
|
|
124
|
+
clone.headers = Array.from(response.headers);
|
|
125
|
+
}
|
|
126
|
+
clone.ok = response.ok;
|
|
127
|
+
clone.redirected = response.redirected;
|
|
128
|
+
clone.status = response.status;
|
|
129
|
+
clone.statusText = response.statusText;
|
|
130
|
+
clone.type = response.type;
|
|
131
|
+
clone.url = response.url;
|
|
132
|
+
return clone;
|
|
133
|
+
}
|
|
134
|
+
function isAbortError(error) {
|
|
135
|
+
return error instanceof Error && error.name === 'AbortError';
|
|
136
|
+
}
|
|
137
|
+
function prepareResponse(result) {
|
|
138
|
+
const newResponse = {
|
|
139
|
+
response: softCloneResponse(result.response),
|
|
140
|
+
content: result.content
|
|
141
|
+
};
|
|
142
|
+
return newResponse;
|
|
143
|
+
}
|
|
144
|
+
function prepareRequest(event) {
|
|
145
|
+
if (event.data.headers) {
|
|
146
|
+
event.data.headers = new Headers(event.data.headers);
|
|
147
|
+
}
|
|
148
|
+
return event;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* In a Worker, any time we are asked to make a request, data needs to be returned.
|
|
155
|
+
* background requests are ergo no different than foreground requests.
|
|
156
|
+
* @internal
|
|
157
|
+
*/
|
|
158
|
+
function calcShouldFetch(store, request, hasCachedValue, identifier) {
|
|
159
|
+
const {
|
|
160
|
+
cacheOptions
|
|
161
|
+
} = request;
|
|
162
|
+
return request.op && MUTATION_OPS.has(request.op) || cacheOptions?.reload || cacheOptions?.backgroundReload || !hasCachedValue || (store.lifetimes && identifier ? store.lifetimes.isHardExpired(identifier, store) || store.lifetimes.isSoftExpired(identifier, store) : false);
|
|
163
|
+
}
|
|
164
|
+
function isMutation(request) {
|
|
165
|
+
return Boolean(request.op && MUTATION_OPS.has(request.op));
|
|
166
|
+
}
|
|
167
|
+
function isCacheAffecting(document) {
|
|
168
|
+
if (!isMutation(document.request)) {
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
// a mutation combined with a 204 has no cache impact when no known records were involved
|
|
172
|
+
// a createRecord with a 201 with an empty response and no known records should similarly
|
|
173
|
+
// have no cache impact
|
|
174
|
+
|
|
175
|
+
if (document.request.op === 'createRecord' && document.response?.status === 201) {
|
|
176
|
+
return document.content ? Object.keys(document.content).length > 0 : false;
|
|
177
|
+
}
|
|
178
|
+
return document.response?.status !== 204;
|
|
179
|
+
}
|
|
180
|
+
function isAggregateError(error) {
|
|
181
|
+
return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
|
|
182
|
+
}
|
|
183
|
+
// TODO @runspired, consider if we should deep freeze errors (potentially only in debug) vs cloning them
|
|
184
|
+
function cloneError(error) {
|
|
185
|
+
const isAggregate = isAggregateError(error);
|
|
186
|
+
const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
|
|
187
|
+
cloned.stack = error.stack;
|
|
188
|
+
cloned.error = error.error;
|
|
189
|
+
|
|
190
|
+
// copy over enumerable properties
|
|
191
|
+
Object.assign(cloned, error);
|
|
192
|
+
return cloned;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* A simplified CacheHandler that hydrates ResourceDataDocuments from the cache
|
|
197
|
+
* with their referenced resources.
|
|
198
|
+
*
|
|
199
|
+
*/
|
|
200
|
+
const CacheHandler = {
|
|
201
|
+
request(context, next) {
|
|
202
|
+
// if we have no cache or no cache-key skip cache handling
|
|
203
|
+
if (!context.request.store || context.request.cacheOptions?.[SkipCache]) {
|
|
204
|
+
return next(context.request);
|
|
205
|
+
}
|
|
206
|
+
const {
|
|
207
|
+
store
|
|
208
|
+
} = context.request;
|
|
209
|
+
const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(context.request);
|
|
210
|
+
const peeked = identifier ? store.cache.peekRequest(identifier) : null;
|
|
211
|
+
if (identifier && !peeked) {
|
|
212
|
+
// if we are using persisted cache, we should attempt to populate the in-memory cache now
|
|
213
|
+
const worker = store._worker;
|
|
214
|
+
if (worker?.storage) {
|
|
215
|
+
return worker.storage.getDocument(identifier).then(document => {
|
|
216
|
+
if (document) {
|
|
217
|
+
store.cache.put(document);
|
|
218
|
+
}
|
|
219
|
+
return completeRequest(identifier, store, context, next);
|
|
220
|
+
}).catch(e => {
|
|
221
|
+
{
|
|
222
|
+
// eslint-disable-next-line no-console
|
|
223
|
+
console.log('Unable to retrieve document from persisted storage', e);
|
|
224
|
+
}
|
|
225
|
+
return completeRequest(identifier, store, context, next);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return completeRequest(identifier, store, context, next);
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
function completeRequest(identifier, store, context, next) {
|
|
233
|
+
const peeked = identifier ? store.cache.peekRequest(identifier) : null;
|
|
234
|
+
// In a Worker, any time we are asked to make a request, data needs to be returned.
|
|
235
|
+
// background requests are ergo no different than foreground requests.
|
|
236
|
+
if (calcShouldFetch(store, context.request, !!peeked, identifier)) {
|
|
237
|
+
return fetchContentAndHydrate(next, context, identifier);
|
|
238
|
+
}
|
|
239
|
+
(test => {
|
|
240
|
+
if (!test) {
|
|
241
|
+
throw new Error(`Expected a peeked request to be present`);
|
|
242
|
+
}
|
|
243
|
+
})(peeked);
|
|
244
|
+
context.setResponse(peeked.response);
|
|
245
|
+
if ('error' in peeked) {
|
|
246
|
+
throw peeked;
|
|
247
|
+
}
|
|
248
|
+
return maybeUpdateObjects(store, peeked.content);
|
|
249
|
+
}
|
|
250
|
+
function maybeUpdateObjects(store, document) {
|
|
251
|
+
if (!document) {
|
|
252
|
+
return document;
|
|
253
|
+
}
|
|
254
|
+
if (Array.isArray(document.data)) {
|
|
255
|
+
const data = document.data.map(identifier => {
|
|
256
|
+
return store.cache.peek(identifier);
|
|
257
|
+
});
|
|
258
|
+
return Object.assign({}, document, {
|
|
259
|
+
data
|
|
260
|
+
});
|
|
261
|
+
} else {
|
|
262
|
+
// @ts-expect-error FIXME investigate why document.data won't accept the signature
|
|
263
|
+
const data = document.data ? store.cache.peek(document.data) : null;
|
|
264
|
+
return Object.assign({}, document, {
|
|
265
|
+
data
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function maybeUpdatePersistedCache(store, document, resourceDocument) {
|
|
270
|
+
const worker = store._worker;
|
|
271
|
+
if (!worker?.storage) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (!document && resourceDocument) {
|
|
275
|
+
// we have resources to update but not a full request to cache
|
|
276
|
+
void worker.storage.putResources(resourceDocument, resourceIdentifier => {
|
|
277
|
+
return store.cache.peek(resourceIdentifier);
|
|
278
|
+
});
|
|
279
|
+
} else if (document) {
|
|
280
|
+
void worker.storage.putDocument(document, resourceIdentifier => {
|
|
281
|
+
return store.cache.peek(resourceIdentifier);
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
function updateCacheForSuccess(store, request, document) {
|
|
286
|
+
let response = null;
|
|
287
|
+
if (isMutation(request)) {
|
|
288
|
+
const record = request.data?.record || request.records?.[0];
|
|
289
|
+
if (record) {
|
|
290
|
+
// @ts-expect-error while this is valid, we should update the CacheHandler for transactional saves
|
|
291
|
+
response = store.cache.didCommit(record, document);
|
|
292
|
+
|
|
293
|
+
// a mutation combined with a 204 has no cache impact when no known records were involved
|
|
294
|
+
// a createRecord with a 201 with an empty response and no known records should similarly
|
|
295
|
+
// have no cache impact
|
|
296
|
+
} else if (isCacheAffecting(document)) {
|
|
297
|
+
response = store.cache.put(document);
|
|
298
|
+
maybeUpdatePersistedCache(store, null, response);
|
|
299
|
+
}
|
|
300
|
+
} else {
|
|
301
|
+
response = store.cache.put(document);
|
|
302
|
+
if (response.lid) {
|
|
303
|
+
const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(request);
|
|
304
|
+
const full = store.cache.peekRequest(identifier);
|
|
305
|
+
maybeUpdatePersistedCache(store, full);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return maybeUpdateObjects(store, response);
|
|
309
|
+
}
|
|
310
|
+
function handleFetchSuccess(store, request, identifier, document) {
|
|
311
|
+
let response;
|
|
312
|
+
assertPrivateStore(store);
|
|
313
|
+
store._join(() => {
|
|
314
|
+
response = updateCacheForSuccess(store, request, document);
|
|
315
|
+
});
|
|
316
|
+
if (store.lifetimes?.didRequest) {
|
|
317
|
+
store.lifetimes.didRequest(request, document.response, identifier, store);
|
|
318
|
+
}
|
|
319
|
+
return response;
|
|
320
|
+
}
|
|
321
|
+
function updateCacheForError(store, request, error) {
|
|
322
|
+
if (isMutation(request)) {
|
|
323
|
+
// TODO similar to didCommit we should spec this to be similar to cache.put for handling full response
|
|
324
|
+
// currently we let the response remain undefiend.
|
|
325
|
+
const errors = error && error.content && typeof error.content === 'object' && 'errors' in error.content && Array.isArray(error.content.errors) ? error.content.errors : undefined;
|
|
326
|
+
const record = request.data?.record || request.records?.[0];
|
|
327
|
+
store.cache.commitWasRejected(record, errors);
|
|
328
|
+
} else {
|
|
329
|
+
const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(request);
|
|
330
|
+
if (identifier) {
|
|
331
|
+
maybeUpdatePersistedCache(store, error);
|
|
332
|
+
}
|
|
333
|
+
return store.cache.put(error);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
function handleFetchError(store, request, identifier, error) {
|
|
337
|
+
if (request.signal?.aborted) {
|
|
338
|
+
throw error;
|
|
339
|
+
}
|
|
340
|
+
assertPrivateStore(store);
|
|
341
|
+
let response;
|
|
342
|
+
store._join(() => {
|
|
343
|
+
response = updateCacheForError(store, request, error);
|
|
344
|
+
});
|
|
345
|
+
if (identifier && store.lifetimes?.didRequest) {
|
|
346
|
+
store.lifetimes.didRequest(request, error.response, identifier, store);
|
|
347
|
+
}
|
|
348
|
+
if (isMutation(request)) {
|
|
349
|
+
throw error;
|
|
350
|
+
}
|
|
351
|
+
const newError = cloneError(error);
|
|
352
|
+
newError.content = response;
|
|
353
|
+
throw newError;
|
|
354
|
+
}
|
|
355
|
+
function fetchContentAndHydrate(next, context, identifier) {
|
|
356
|
+
const {
|
|
357
|
+
request
|
|
358
|
+
} = context;
|
|
359
|
+
const {
|
|
360
|
+
store
|
|
361
|
+
} = context.request;
|
|
362
|
+
if (isMutation(request)) {
|
|
363
|
+
// TODO should we handle multiple records in request.records by iteratively calling willCommit for each
|
|
364
|
+
const record = request.data?.record || request.records?.[0];
|
|
365
|
+
(test => {
|
|
366
|
+
if (!test) {
|
|
367
|
+
throw new Error(`Expected to receive a list of records included in the ${request.op} request`);
|
|
368
|
+
}
|
|
369
|
+
})(record);
|
|
370
|
+
if (record) {
|
|
371
|
+
store.cache.willCommit(record, context);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (store.lifetimes?.willRequest) {
|
|
375
|
+
store.lifetimes.willRequest(request, identifier, store);
|
|
376
|
+
}
|
|
377
|
+
return next(request).then(document => handleFetchSuccess(store, request, identifier, document), error => handleFetchError(store, request, identifier, error));
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export { CacheHandler, DataWorker };
|