@warp-drive-mirror/experiments 0.2.6-alpha.30 → 0.2.6-alpha.34
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/dist/unpkg/dev/data-worker.js +378 -0
- package/dist/unpkg/dev/declarations/data-worker/cache-handler.d.ts +7 -0
- package/dist/unpkg/dev/declarations/data-worker/fetch.d.ts +21 -0
- package/dist/unpkg/dev/declarations/data-worker/types.d.ts +31 -0
- package/dist/unpkg/dev/declarations/data-worker/utils.d.ts +12 -0
- package/dist/unpkg/dev/declarations/data-worker/worker.d.ts +23 -0
- package/dist/unpkg/dev/declarations/data-worker.d.ts +2 -0
- package/dist/unpkg/dev/declarations/document-storage/index.d.ts +90 -0
- package/dist/unpkg/dev/declarations/document-storage.d.ts +1 -0
- package/dist/unpkg/dev/declarations/image-fetch.d.ts +1 -0
- package/dist/unpkg/dev/declarations/image-worker/fetch.d.ts +18 -0
- package/dist/unpkg/dev/declarations/image-worker/types.d.ts +21 -0
- package/dist/unpkg/dev/declarations/image-worker/worker.d.ts +14 -0
- package/dist/unpkg/dev/declarations/image-worker.d.ts +1 -0
- package/dist/unpkg/dev/declarations/worker-fetch.d.ts +1 -0
- package/dist/unpkg/dev/document-storage.js +1 -0
- package/dist/unpkg/dev/image-fetch.js +80 -0
- package/dist/unpkg/dev/image-worker.js +98 -0
- package/dist/unpkg/dev/index-CGCX7hY2.js +349 -0
- package/dist/unpkg/dev/worker-fetch.js +164 -0
- package/dist/unpkg/dev-deprecated/data-worker.js +378 -0
- package/dist/unpkg/dev-deprecated/declarations/data-worker/cache-handler.d.ts +7 -0
- package/dist/unpkg/dev-deprecated/declarations/data-worker/fetch.d.ts +21 -0
- package/dist/unpkg/dev-deprecated/declarations/data-worker/types.d.ts +31 -0
- package/dist/unpkg/dev-deprecated/declarations/data-worker/utils.d.ts +12 -0
- package/dist/unpkg/dev-deprecated/declarations/data-worker/worker.d.ts +23 -0
- package/dist/unpkg/dev-deprecated/declarations/data-worker.d.ts +2 -0
- package/dist/unpkg/dev-deprecated/declarations/document-storage/index.d.ts +90 -0
- package/dist/unpkg/dev-deprecated/declarations/document-storage.d.ts +1 -0
- package/dist/unpkg/dev-deprecated/declarations/image-fetch.d.ts +1 -0
- package/dist/unpkg/dev-deprecated/declarations/image-worker/fetch.d.ts +18 -0
- package/dist/unpkg/dev-deprecated/declarations/image-worker/types.d.ts +21 -0
- package/dist/unpkg/dev-deprecated/declarations/image-worker/worker.d.ts +14 -0
- package/dist/unpkg/dev-deprecated/declarations/image-worker.d.ts +1 -0
- package/dist/unpkg/dev-deprecated/declarations/worker-fetch.d.ts +1 -0
- package/dist/unpkg/dev-deprecated/document-storage.js +1 -0
- package/dist/unpkg/dev-deprecated/image-fetch.js +80 -0
- package/dist/unpkg/dev-deprecated/image-worker.js +98 -0
- package/dist/unpkg/dev-deprecated/index-CGCX7hY2.js +349 -0
- package/dist/unpkg/dev-deprecated/worker-fetch.js +164 -0
- package/dist/unpkg/prod/data-worker.js +378 -0
- package/dist/unpkg/prod/declarations/data-worker/cache-handler.d.ts +7 -0
- package/dist/unpkg/prod/declarations/data-worker/fetch.d.ts +21 -0
- package/dist/unpkg/prod/declarations/data-worker/types.d.ts +31 -0
- package/dist/unpkg/prod/declarations/data-worker/utils.d.ts +12 -0
- package/dist/unpkg/prod/declarations/data-worker/worker.d.ts +23 -0
- package/dist/unpkg/prod/declarations/data-worker.d.ts +2 -0
- package/dist/unpkg/prod/declarations/document-storage/index.d.ts +90 -0
- package/dist/unpkg/prod/declarations/document-storage.d.ts +1 -0
- package/dist/unpkg/prod/declarations/image-fetch.d.ts +1 -0
- package/dist/unpkg/prod/declarations/image-worker/fetch.d.ts +18 -0
- package/dist/unpkg/prod/declarations/image-worker/types.d.ts +21 -0
- package/dist/unpkg/prod/declarations/image-worker/worker.d.ts +14 -0
- package/dist/unpkg/prod/declarations/image-worker.d.ts +1 -0
- package/dist/unpkg/prod/declarations/worker-fetch.d.ts +1 -0
- package/dist/unpkg/prod/document-storage.js +1 -0
- package/dist/unpkg/prod/image-fetch.js +80 -0
- package/dist/unpkg/prod/image-worker.js +98 -0
- package/dist/unpkg/prod/index-CGCX7hY2.js +349 -0
- package/dist/unpkg/prod/worker-fetch.js +164 -0
- package/dist/unpkg/prod-deprecated/data-worker.js +378 -0
- package/dist/unpkg/prod-deprecated/declarations/data-worker/cache-handler.d.ts +7 -0
- package/dist/unpkg/prod-deprecated/declarations/data-worker/fetch.d.ts +21 -0
- package/dist/unpkg/prod-deprecated/declarations/data-worker/types.d.ts +31 -0
- package/dist/unpkg/prod-deprecated/declarations/data-worker/utils.d.ts +12 -0
- package/dist/unpkg/prod-deprecated/declarations/data-worker/worker.d.ts +23 -0
- package/dist/unpkg/prod-deprecated/declarations/data-worker.d.ts +2 -0
- package/dist/unpkg/prod-deprecated/declarations/document-storage/index.d.ts +90 -0
- package/dist/unpkg/prod-deprecated/declarations/document-storage.d.ts +1 -0
- package/dist/unpkg/prod-deprecated/declarations/image-fetch.d.ts +1 -0
- package/dist/unpkg/prod-deprecated/declarations/image-worker/fetch.d.ts +18 -0
- package/dist/unpkg/prod-deprecated/declarations/image-worker/types.d.ts +21 -0
- package/dist/unpkg/prod-deprecated/declarations/image-worker/worker.d.ts +14 -0
- package/dist/unpkg/prod-deprecated/declarations/image-worker.d.ts +1 -0
- package/dist/unpkg/prod-deprecated/declarations/worker-fetch.d.ts +1 -0
- package/dist/unpkg/prod-deprecated/document-storage.js +1 -0
- package/dist/unpkg/prod-deprecated/image-fetch.js +80 -0
- package/dist/unpkg/prod-deprecated/image-worker.js +98 -0
- package/dist/unpkg/prod-deprecated/index-CGCX7hY2.js +349 -0
- package/dist/unpkg/prod-deprecated/worker-fetch.js +164 -0
- package/package.json +64 -4
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import { D as DocumentStorage } from "./index-CGCX7hY2.js";
|
|
2
|
+
import { assertPrivateStore } from '@warp-drive-mirror/core/store/-private';
|
|
3
|
+
import { SkipCache } from '@warp-drive-mirror/core/types/request';
|
|
4
|
+
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
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
|
+
const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* In a Worker, any time we are asked to make a request, data needs to be returned.
|
|
154
|
+
* background requests are ergo no different than foreground requests.
|
|
155
|
+
* @internal
|
|
156
|
+
*/
|
|
157
|
+
function calcShouldFetch(store, request, hasCachedValue, identifier) {
|
|
158
|
+
const {
|
|
159
|
+
cacheOptions
|
|
160
|
+
} = request;
|
|
161
|
+
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);
|
|
162
|
+
}
|
|
163
|
+
function isMutation(request) {
|
|
164
|
+
return Boolean(request.op && MUTATION_OPS.has(request.op));
|
|
165
|
+
}
|
|
166
|
+
function isCacheAffecting(document) {
|
|
167
|
+
if (!isMutation(document.request)) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
// a mutation combined with a 204 has no cache impact when no known records were involved
|
|
171
|
+
// a createRecord with a 201 with an empty response and no known records should similarly
|
|
172
|
+
// have no cache impact
|
|
173
|
+
|
|
174
|
+
if (document.request.op === 'createRecord' && document.response?.status === 201) {
|
|
175
|
+
return document.content ? Object.keys(document.content).length > 0 : false;
|
|
176
|
+
}
|
|
177
|
+
return document.response?.status !== 204;
|
|
178
|
+
}
|
|
179
|
+
function isAggregateError(error) {
|
|
180
|
+
return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
|
|
181
|
+
}
|
|
182
|
+
// TODO @runspired, consider if we should deep freeze errors (potentially only in debug) vs cloning them
|
|
183
|
+
function cloneError(error) {
|
|
184
|
+
const isAggregate = isAggregateError(error);
|
|
185
|
+
const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
|
|
186
|
+
cloned.stack = error.stack;
|
|
187
|
+
cloned.error = error.error;
|
|
188
|
+
|
|
189
|
+
// copy over enumerable properties
|
|
190
|
+
Object.assign(cloned, error);
|
|
191
|
+
return cloned;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* A simplified CacheHandler that hydrates ResourceDataDocuments from the cache
|
|
196
|
+
* with their referenced resources.
|
|
197
|
+
*
|
|
198
|
+
*/
|
|
199
|
+
const CacheHandler = {
|
|
200
|
+
request(context, next) {
|
|
201
|
+
// if we have no cache or no cache-key skip cache handling
|
|
202
|
+
if (!context.request.store || context.request.cacheOptions?.[SkipCache]) {
|
|
203
|
+
return next(context.request);
|
|
204
|
+
}
|
|
205
|
+
const {
|
|
206
|
+
store
|
|
207
|
+
} = context.request;
|
|
208
|
+
const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(context.request);
|
|
209
|
+
const peeked = identifier ? store.cache.peekRequest(identifier) : null;
|
|
210
|
+
if (identifier && !peeked) {
|
|
211
|
+
// if we are using persisted cache, we should attempt to populate the in-memory cache now
|
|
212
|
+
const worker = store._worker;
|
|
213
|
+
if (worker?.storage) {
|
|
214
|
+
return worker.storage.getDocument(identifier).then(document => {
|
|
215
|
+
if (document) {
|
|
216
|
+
store.cache.put(document);
|
|
217
|
+
}
|
|
218
|
+
return completeRequest(identifier, store, context, next);
|
|
219
|
+
}).catch(e => {
|
|
220
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG)) {
|
|
221
|
+
// eslint-disable-next-line no-console
|
|
222
|
+
console.log('Unable to retrieve document from persisted storage', e);
|
|
223
|
+
}
|
|
224
|
+
return completeRequest(identifier, store, context, next);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return completeRequest(identifier, store, context, next);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
function completeRequest(identifier, store, context, next) {
|
|
232
|
+
const peeked = identifier ? store.cache.peekRequest(identifier) : null;
|
|
233
|
+
// In a Worker, any time we are asked to make a request, data needs to be returned.
|
|
234
|
+
// background requests are ergo no different than foreground requests.
|
|
235
|
+
if (calcShouldFetch(store, context.request, !!peeked, identifier)) {
|
|
236
|
+
return fetchContentAndHydrate(next, context, identifier);
|
|
237
|
+
}
|
|
238
|
+
macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
|
|
239
|
+
if (!test) {
|
|
240
|
+
throw new Error(`Expected a peeked request to be present`);
|
|
241
|
+
}
|
|
242
|
+
})(peeked) : {};
|
|
243
|
+
context.setResponse(peeked.response);
|
|
244
|
+
if ('error' in peeked) {
|
|
245
|
+
throw peeked;
|
|
246
|
+
}
|
|
247
|
+
return maybeUpdateObjects(store, peeked.content);
|
|
248
|
+
}
|
|
249
|
+
function maybeUpdateObjects(store, document) {
|
|
250
|
+
if (!document) {
|
|
251
|
+
return document;
|
|
252
|
+
}
|
|
253
|
+
if (Array.isArray(document.data)) {
|
|
254
|
+
const data = document.data.map(identifier => {
|
|
255
|
+
return store.cache.peek(identifier);
|
|
256
|
+
});
|
|
257
|
+
return Object.assign({}, document, {
|
|
258
|
+
data
|
|
259
|
+
});
|
|
260
|
+
} else {
|
|
261
|
+
// @ts-expect-error FIXME investigate why document.data won't accept the signature
|
|
262
|
+
const data = document.data ? store.cache.peek(document.data) : null;
|
|
263
|
+
return Object.assign({}, document, {
|
|
264
|
+
data
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
function maybeUpdatePersistedCache(store, document, resourceDocument) {
|
|
269
|
+
const worker = store._worker;
|
|
270
|
+
if (!worker?.storage) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (!document && resourceDocument) {
|
|
274
|
+
// we have resources to update but not a full request to cache
|
|
275
|
+
void worker.storage.putResources(resourceDocument, resourceIdentifier => {
|
|
276
|
+
return store.cache.peek(resourceIdentifier);
|
|
277
|
+
});
|
|
278
|
+
} else if (document) {
|
|
279
|
+
void worker.storage.putDocument(document, resourceIdentifier => {
|
|
280
|
+
return store.cache.peek(resourceIdentifier);
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
function updateCacheForSuccess(store, request, document) {
|
|
285
|
+
let response = null;
|
|
286
|
+
if (isMutation(request)) {
|
|
287
|
+
const record = request.data?.record || request.records?.[0];
|
|
288
|
+
if (record) {
|
|
289
|
+
// @ts-expect-error while this is valid, we should update the CacheHandler for transactional saves
|
|
290
|
+
response = store.cache.didCommit(record, document);
|
|
291
|
+
|
|
292
|
+
// a mutation combined with a 204 has no cache impact when no known records were involved
|
|
293
|
+
// a createRecord with a 201 with an empty response and no known records should similarly
|
|
294
|
+
// have no cache impact
|
|
295
|
+
} else if (isCacheAffecting(document)) {
|
|
296
|
+
response = store.cache.put(document);
|
|
297
|
+
maybeUpdatePersistedCache(store, null, response);
|
|
298
|
+
}
|
|
299
|
+
} else {
|
|
300
|
+
response = store.cache.put(document);
|
|
301
|
+
if (response.lid) {
|
|
302
|
+
const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(request);
|
|
303
|
+
const full = store.cache.peekRequest(identifier);
|
|
304
|
+
maybeUpdatePersistedCache(store, full);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return maybeUpdateObjects(store, response);
|
|
308
|
+
}
|
|
309
|
+
function handleFetchSuccess(store, request, identifier, document) {
|
|
310
|
+
let response;
|
|
311
|
+
assertPrivateStore(store);
|
|
312
|
+
store._join(() => {
|
|
313
|
+
response = updateCacheForSuccess(store, request, document);
|
|
314
|
+
});
|
|
315
|
+
if (store.lifetimes?.didRequest) {
|
|
316
|
+
store.lifetimes.didRequest(request, document.response, identifier, store);
|
|
317
|
+
}
|
|
318
|
+
return response;
|
|
319
|
+
}
|
|
320
|
+
function updateCacheForError(store, request, error) {
|
|
321
|
+
if (isMutation(request)) {
|
|
322
|
+
// TODO similar to didCommit we should spec this to be similar to cache.put for handling full response
|
|
323
|
+
// currently we let the response remain undefiend.
|
|
324
|
+
const errors = error && error.content && typeof error.content === 'object' && 'errors' in error.content && Array.isArray(error.content.errors) ? error.content.errors : undefined;
|
|
325
|
+
const record = request.data?.record || request.records?.[0];
|
|
326
|
+
store.cache.commitWasRejected(record, errors);
|
|
327
|
+
} else {
|
|
328
|
+
const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(request);
|
|
329
|
+
if (identifier) {
|
|
330
|
+
maybeUpdatePersistedCache(store, error);
|
|
331
|
+
}
|
|
332
|
+
return store.cache.put(error);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function handleFetchError(store, request, identifier, error) {
|
|
336
|
+
if (request.signal?.aborted) {
|
|
337
|
+
throw error;
|
|
338
|
+
}
|
|
339
|
+
assertPrivateStore(store);
|
|
340
|
+
let response;
|
|
341
|
+
store._join(() => {
|
|
342
|
+
response = updateCacheForError(store, request, error);
|
|
343
|
+
});
|
|
344
|
+
if (identifier && store.lifetimes?.didRequest) {
|
|
345
|
+
store.lifetimes.didRequest(request, error.response, identifier, store);
|
|
346
|
+
}
|
|
347
|
+
if (isMutation(request)) {
|
|
348
|
+
throw error;
|
|
349
|
+
}
|
|
350
|
+
const newError = cloneError(error);
|
|
351
|
+
newError.content = response;
|
|
352
|
+
throw newError;
|
|
353
|
+
}
|
|
354
|
+
function fetchContentAndHydrate(next, context, identifier) {
|
|
355
|
+
const {
|
|
356
|
+
request
|
|
357
|
+
} = context;
|
|
358
|
+
const {
|
|
359
|
+
store
|
|
360
|
+
} = context.request;
|
|
361
|
+
if (isMutation(request)) {
|
|
362
|
+
// TODO should we handle multiple records in request.records by iteratively calling willCommit for each
|
|
363
|
+
const record = request.data?.record || request.records?.[0];
|
|
364
|
+
macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
|
|
365
|
+
if (!test) {
|
|
366
|
+
throw new Error(`Expected to receive a list of records included in the ${request.op} request`);
|
|
367
|
+
}
|
|
368
|
+
})(record) : {};
|
|
369
|
+
if (record) {
|
|
370
|
+
store.cache.willCommit(record, context);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
if (store.lifetimes?.willRequest) {
|
|
374
|
+
store.lifetimes.willRequest(request, identifier, store);
|
|
375
|
+
}
|
|
376
|
+
return next(request).then(document => handleFetchSuccess(store, request, identifier, document), error => handleFetchError(store, request, identifier, error));
|
|
377
|
+
}
|
|
378
|
+
export { CacheHandler, DataWorker };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { CacheHandler as CacheHandlerType } from "@warp-drive-mirror/core/request";
|
|
2
|
+
/**
|
|
3
|
+
* A simplified CacheHandler that hydrates ResourceDataDocuments from the cache
|
|
4
|
+
* with their referenced resources.
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
export declare const CacheHandler: CacheHandlerType;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Context, Deferred, Future, NextFn } from "@warp-drive-mirror/core/request";
|
|
2
|
+
import type { AbortEventData, RequestEventData } from "./types.js";
|
|
3
|
+
interface PendingItem {
|
|
4
|
+
context: Context;
|
|
5
|
+
signal: AbortSignal | null;
|
|
6
|
+
abortFn: () => void;
|
|
7
|
+
deferred: Deferred<unknown>;
|
|
8
|
+
stack: string;
|
|
9
|
+
}
|
|
10
|
+
export declare class WorkerFetch {
|
|
11
|
+
worker: Worker | SharedWorker;
|
|
12
|
+
threadId: string;
|
|
13
|
+
pending: Map<number, PendingItem>;
|
|
14
|
+
channel: MessageChannel;
|
|
15
|
+
constructor(worker: Worker | SharedWorker | null);
|
|
16
|
+
cleanupRequest(id: number): PendingItem | undefined;
|
|
17
|
+
send(event: RequestEventData | AbortEventData): void;
|
|
18
|
+
request<T>(context: Context, next: NextFn<T>): Promise<T> | Future<T>;
|
|
19
|
+
}
|
|
20
|
+
export declare function enhanceReason(reason?: string): DOMException;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { RequestInfo, StructuredDataDocument, StructuredErrorDocument } from "@warp-drive-mirror/core/types/request";
|
|
2
|
+
export type SuccessResponseEventData<T> = {
|
|
3
|
+
type: "success-response";
|
|
4
|
+
thread: string;
|
|
5
|
+
id: number;
|
|
6
|
+
data: StructuredDataDocument<T>;
|
|
7
|
+
};
|
|
8
|
+
export type ErrorResponseEventData<T> = {
|
|
9
|
+
type: "error-response";
|
|
10
|
+
thread: string;
|
|
11
|
+
id: number;
|
|
12
|
+
data: StructuredErrorDocument<T>;
|
|
13
|
+
};
|
|
14
|
+
export type RequestEventData = {
|
|
15
|
+
type: "request";
|
|
16
|
+
thread: string;
|
|
17
|
+
id: number;
|
|
18
|
+
data: RequestInfo;
|
|
19
|
+
};
|
|
20
|
+
export type AbortEventData = {
|
|
21
|
+
type: "abort";
|
|
22
|
+
thread: string;
|
|
23
|
+
id: number;
|
|
24
|
+
data: string;
|
|
25
|
+
};
|
|
26
|
+
export type ThreadInitEventData = {
|
|
27
|
+
type: "connect";
|
|
28
|
+
thread: string;
|
|
29
|
+
};
|
|
30
|
+
export type MainThreadEvent<T> = MessageEvent<SuccessResponseEventData<T> | ErrorResponseEventData<T>>;
|
|
31
|
+
export type WorkerThreadEvent = MessageEvent<RequestEventData> | MessageEvent<ThreadInitEventData> | MessageEvent<AbortEventData>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ImmutableCreateRequestOptions, ImmutableDeleteRequestOptions, ImmutableRequestInfo, ImmutableUpdateRequestOptions, StructuredDataDocument } from "@warp-drive-mirror/core/types/request";
|
|
2
|
+
import type { ApiError } from "@warp-drive-mirror/core/types/spec/error";
|
|
3
|
+
export declare const MUTATION_OPS: Set<string>;
|
|
4
|
+
export declare function isMutation(request: Partial<ImmutableRequestInfo>): request is ImmutableUpdateRequestOptions | ImmutableCreateRequestOptions | ImmutableDeleteRequestOptions;
|
|
5
|
+
export declare function isCacheAffecting<T>(document: StructuredDataDocument<T>): boolean;
|
|
6
|
+
type RobustError = Error & {
|
|
7
|
+
error: string | object;
|
|
8
|
+
errors?: ApiError[];
|
|
9
|
+
content?: unknown;
|
|
10
|
+
};
|
|
11
|
+
export declare function cloneError(error: RobustError): RobustError;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Store } from "@warp-drive-mirror/core";
|
|
2
|
+
import type { Future } from "@warp-drive-mirror/core/request";
|
|
3
|
+
import { DocumentStorage } from "../document-storage.js";
|
|
4
|
+
import type { AbortEventData, RequestEventData } from "./types.js";
|
|
5
|
+
export declare class DataWorker {
|
|
6
|
+
store: Store;
|
|
7
|
+
threads: Map<string, MessagePort>;
|
|
8
|
+
pending: Map<string, Map<number, Future<unknown>>>;
|
|
9
|
+
isSharedWorker: boolean;
|
|
10
|
+
options: {
|
|
11
|
+
persisted: boolean;
|
|
12
|
+
scope?: string;
|
|
13
|
+
};
|
|
14
|
+
storage: DocumentStorage;
|
|
15
|
+
constructor(UserStore: typeof Store, options?: {
|
|
16
|
+
persisted: boolean;
|
|
17
|
+
scope?: string;
|
|
18
|
+
});
|
|
19
|
+
initialize(): void;
|
|
20
|
+
setupThread(thread: string, port: MessagePort): void;
|
|
21
|
+
abortRequest(event: AbortEventData): void;
|
|
22
|
+
request(event: RequestEventData): Promise<void>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { PersistedResourceKey } from "@warp-drive-mirror/core/types/identifier";
|
|
2
|
+
import type { StructuredDocument } from "@warp-drive-mirror/core/types/request";
|
|
3
|
+
import type { ResourceDataDocument, ResourceDocument } from "@warp-drive-mirror/core/types/spec/document";
|
|
4
|
+
import type { ExistingResourceObject } from "@warp-drive-mirror/core/types/spec/json-api-raw";
|
|
5
|
+
export declare const WARP_DRIVE_STORAGE_FILE_NAME = "warp-drive_document-storage";
|
|
6
|
+
export declare const WARP_DRIVE_STORAGE_VERSION = 1;
|
|
7
|
+
export type DocumentStorageOptions = {
|
|
8
|
+
/**
|
|
9
|
+
* The scope of the storage. This is used to enable multiple distinct
|
|
10
|
+
* storage areas within the same origin.
|
|
11
|
+
*
|
|
12
|
+
* One use case for this is to have a separate storage area for each
|
|
13
|
+
* user credential. So for instance, in applications that allow a single
|
|
14
|
+
* user to have multiple accounts, each account can have its own storage!
|
|
15
|
+
*/
|
|
16
|
+
scope: string;
|
|
17
|
+
/**
|
|
18
|
+
* When set to true, if other instances of the storage are created with
|
|
19
|
+
* the same scope, they will not share the same in-memory cache and BroadcastChannel.
|
|
20
|
+
*
|
|
21
|
+
* This is mostly useful for testing purposes to replicate the behavior of
|
|
22
|
+
* multiple tabs or workers.
|
|
23
|
+
*/
|
|
24
|
+
isolated: boolean;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* DocumentStorage is specifically designed around WarpDrive Cache and Request concepts.
|
|
28
|
+
*
|
|
29
|
+
* CacheFileDocument is a StructuredDocument (request response) whose `content` is
|
|
30
|
+
* the ResourceDocument returned by inserting the request into a Store's Cache.
|
|
31
|
+
*/
|
|
32
|
+
type CacheFileDocument = StructuredDocument<ResourceDocument>;
|
|
33
|
+
/**
|
|
34
|
+
* A CacheDocument is a reconstructed request response that rehydrates ResourceDocument
|
|
35
|
+
* with the associated resources based on their identifiers.
|
|
36
|
+
*/
|
|
37
|
+
type CacheDocument = StructuredDocument<ResourceDocument<ExistingResourceObject>>;
|
|
38
|
+
type DocumentIdentifier = {
|
|
39
|
+
lid: string;
|
|
40
|
+
};
|
|
41
|
+
type MemCache = {
|
|
42
|
+
documents: Map<string, CacheFileDocument>;
|
|
43
|
+
resources: Map<string, ExistingResourceObject>;
|
|
44
|
+
};
|
|
45
|
+
declare class InternalDocumentStorage {
|
|
46
|
+
readonly options: DocumentStorageOptions;
|
|
47
|
+
_fileHandle: Promise<FileSystemFileHandle>;
|
|
48
|
+
_channel: BroadcastChannel;
|
|
49
|
+
_invalidated: boolean;
|
|
50
|
+
_lastModified: number;
|
|
51
|
+
_cache: MemCache | null;
|
|
52
|
+
_filePromise: Promise<MemCache> | null;
|
|
53
|
+
constructor(options: DocumentStorageOptions);
|
|
54
|
+
_onMessage(_event: MessageEvent): void;
|
|
55
|
+
_open(scope: string): Promise<FileSystemFileHandle>;
|
|
56
|
+
_read(): Promise<MemCache>;
|
|
57
|
+
_patch(documentKey: string, document: CacheFileDocument, updatedResources: Map<string, ExistingResourceObject>): Promise<void>;
|
|
58
|
+
getDocument(key: DocumentIdentifier): Promise<CacheDocument | null>;
|
|
59
|
+
putDocument(document: CacheFileDocument, resourceCollector: (resourceIdentifier: PersistedResourceKey) => ExistingResourceObject): Promise<void>;
|
|
60
|
+
_getResources(document: ResourceDataDocument, resourceCollector: (resourceIdentifier: PersistedResourceKey) => ExistingResourceObject, resources?: Map<string, ExistingResourceObject>): Map<string, ExistingResourceObject>;
|
|
61
|
+
putResources(document: ResourceDataDocument, resourceCollector: (resourceIdentifier: PersistedResourceKey) => ExistingResourceObject): Promise<void>;
|
|
62
|
+
clear(reset?: boolean): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* DocumentStorage is a wrapper around the StorageManager API that provides
|
|
66
|
+
* a simple interface for reading and updating documents and requests.
|
|
67
|
+
*
|
|
68
|
+
* Some goals for this experiment:
|
|
69
|
+
*
|
|
70
|
+
* - optimize for storing requests/documents
|
|
71
|
+
* - optimize for storing resources
|
|
72
|
+
* - optimize for looking up resources associated to a document
|
|
73
|
+
* - optimize for notifying cross-tab when data is updated
|
|
74
|
+
*
|
|
75
|
+
* optional features:
|
|
76
|
+
*
|
|
77
|
+
* - support for offline mode
|
|
78
|
+
* - ?? support for relationship based cache traversal
|
|
79
|
+
* - a way to index records by type + another field (e.g updatedAt/createAt/name)
|
|
80
|
+
* such that simple queries can be done without having to scan all records
|
|
81
|
+
*/
|
|
82
|
+
export declare class DocumentStorage {
|
|
83
|
+
readonly _storage: InternalDocumentStorage;
|
|
84
|
+
constructor(options?: Partial<DocumentStorageOptions>);
|
|
85
|
+
getDocument(key: DocumentIdentifier): Promise<CacheDocument | null>;
|
|
86
|
+
putDocument(document: CacheFileDocument, resourceCollector: (resourceIdentifier: PersistedResourceKey) => ExistingResourceObject): Promise<void>;
|
|
87
|
+
putResources(document: ResourceDataDocument, resourceCollector: (resourceIdentifier: PersistedResourceKey) => ExistingResourceObject): Promise<void>;
|
|
88
|
+
clear(reset?: boolean): Promise<void>;
|
|
89
|
+
}
|
|
90
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DocumentStorage } from "./document-storage/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ImageFetch } from "./image-worker/fetch.js";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Deferred } from "@warp-drive-mirror/core/request";
|
|
2
|
+
import type { RequestEventData } from "./types.js";
|
|
3
|
+
export interface FastBoot {
|
|
4
|
+
require(moduleName: string): unknown;
|
|
5
|
+
isFastBoot: boolean;
|
|
6
|
+
request: Request;
|
|
7
|
+
}
|
|
8
|
+
export declare class ImageFetch {
|
|
9
|
+
worker: Worker | SharedWorker;
|
|
10
|
+
threadId: string;
|
|
11
|
+
pending: Map<string, Deferred<string>>;
|
|
12
|
+
channel: MessageChannel;
|
|
13
|
+
cache: Map<string, string>;
|
|
14
|
+
constructor(worker: Worker | SharedWorker | null);
|
|
15
|
+
cleanupRequest(url: string): Deferred<string> | undefined;
|
|
16
|
+
_send(event: RequestEventData): void;
|
|
17
|
+
load(url: string): Promise<string>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type SuccessResponseEventData = {
|
|
2
|
+
type: "success-response";
|
|
3
|
+
thread: string;
|
|
4
|
+
url: string;
|
|
5
|
+
};
|
|
6
|
+
export type ErrorResponseEventData = {
|
|
7
|
+
type: "error-response";
|
|
8
|
+
thread: string;
|
|
9
|
+
url: string;
|
|
10
|
+
};
|
|
11
|
+
export type RequestEventData = {
|
|
12
|
+
type: "load";
|
|
13
|
+
thread: string;
|
|
14
|
+
url: string;
|
|
15
|
+
};
|
|
16
|
+
export type ThreadInitEventData = {
|
|
17
|
+
type: "connect";
|
|
18
|
+
thread: string;
|
|
19
|
+
};
|
|
20
|
+
export type MainThreadEvent = MessageEvent<SuccessResponseEventData | ErrorResponseEventData>;
|
|
21
|
+
export type WorkerThreadEvent = MessageEvent<RequestEventData> | MessageEvent<ThreadInitEventData>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare class ImageWorker {
|
|
2
|
+
private threads;
|
|
3
|
+
private pendingImages;
|
|
4
|
+
private options;
|
|
5
|
+
private isSharedWorker;
|
|
6
|
+
private cache;
|
|
7
|
+
constructor(options?: {
|
|
8
|
+
persisted: boolean;
|
|
9
|
+
});
|
|
10
|
+
private fetch;
|
|
11
|
+
private initialize;
|
|
12
|
+
private setupThread;
|
|
13
|
+
private request;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ImageWorker } from "./image-worker/worker.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { WorkerFetch } from "./data-worker/fetch.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { D as DocumentStorage } from "./index-CGCX7hY2.js";
|