@warp-drive-mirror/experiments 0.2.6-alpha.37 → 0.2.6-alpha.40
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 +10 -8
- package/dist/unpkg/dev/document-storage.js +349 -1
- package/dist/unpkg/dev/image-fetch.js +2 -8
- package/dist/unpkg/dev/image-worker.js +2 -1
- package/dist/unpkg/dev/worker-fetch.js +2 -8
- package/dist/unpkg/dev-deprecated/data-worker.js +10 -8
- package/dist/unpkg/dev-deprecated/document-storage.js +349 -1
- package/dist/unpkg/dev-deprecated/image-fetch.js +2 -8
- package/dist/unpkg/dev-deprecated/image-worker.js +2 -1
- package/dist/unpkg/dev-deprecated/worker-fetch.js +2 -8
- package/dist/unpkg/prod/data-worker.js +5 -17
- package/dist/unpkg/prod/document-storage.js +339 -1
- package/dist/unpkg/prod/image-fetch.js +2 -8
- package/dist/unpkg/prod/image-worker.js +2 -1
- package/dist/unpkg/prod/worker-fetch.js +2 -8
- package/dist/unpkg/prod-deprecated/data-worker.js +5 -17
- package/dist/unpkg/prod-deprecated/document-storage.js +339 -1
- package/dist/unpkg/prod-deprecated/image-fetch.js +2 -8
- package/dist/unpkg/prod-deprecated/image-worker.js +2 -1
- package/dist/unpkg/prod-deprecated/worker-fetch.js +2 -8
- package/package.json +24 -44
- package/dist/unpkg/dev/declarations/data-worker/cache-handler.d.ts +0 -7
- package/dist/unpkg/dev/declarations/data-worker/fetch.d.ts +0 -21
- package/dist/unpkg/dev/declarations/data-worker/types.d.ts +0 -31
- package/dist/unpkg/dev/declarations/data-worker/utils.d.ts +0 -12
- package/dist/unpkg/dev/declarations/data-worker/worker.d.ts +0 -23
- package/dist/unpkg/dev/declarations/data-worker.d.ts +0 -2
- package/dist/unpkg/dev/declarations/document-storage/index.d.ts +0 -90
- package/dist/unpkg/dev/declarations/document-storage.d.ts +0 -1
- package/dist/unpkg/dev/declarations/image-fetch.d.ts +0 -1
- package/dist/unpkg/dev/declarations/image-worker/fetch.d.ts +0 -18
- package/dist/unpkg/dev/declarations/image-worker/types.d.ts +0 -21
- package/dist/unpkg/dev/declarations/image-worker/worker.d.ts +0 -14
- package/dist/unpkg/dev/declarations/image-worker.d.ts +0 -1
- package/dist/unpkg/dev/declarations/worker-fetch.d.ts +0 -1
- package/dist/unpkg/dev/index-CGCX7hY2.js +0 -349
- package/dist/unpkg/dev-deprecated/declarations/data-worker/cache-handler.d.ts +0 -7
- package/dist/unpkg/dev-deprecated/declarations/data-worker/fetch.d.ts +0 -21
- package/dist/unpkg/dev-deprecated/declarations/data-worker/types.d.ts +0 -31
- package/dist/unpkg/dev-deprecated/declarations/data-worker/utils.d.ts +0 -12
- package/dist/unpkg/dev-deprecated/declarations/data-worker/worker.d.ts +0 -23
- package/dist/unpkg/dev-deprecated/declarations/data-worker.d.ts +0 -2
- package/dist/unpkg/dev-deprecated/declarations/document-storage/index.d.ts +0 -90
- package/dist/unpkg/dev-deprecated/declarations/document-storage.d.ts +0 -1
- package/dist/unpkg/dev-deprecated/declarations/image-fetch.d.ts +0 -1
- package/dist/unpkg/dev-deprecated/declarations/image-worker/fetch.d.ts +0 -18
- package/dist/unpkg/dev-deprecated/declarations/image-worker/types.d.ts +0 -21
- package/dist/unpkg/dev-deprecated/declarations/image-worker/worker.d.ts +0 -14
- package/dist/unpkg/dev-deprecated/declarations/image-worker.d.ts +0 -1
- package/dist/unpkg/dev-deprecated/declarations/worker-fetch.d.ts +0 -1
- package/dist/unpkg/dev-deprecated/index-CGCX7hY2.js +0 -349
- package/dist/unpkg/prod/declarations/data-worker/cache-handler.d.ts +0 -7
- package/dist/unpkg/prod/declarations/data-worker/fetch.d.ts +0 -21
- package/dist/unpkg/prod/declarations/data-worker/types.d.ts +0 -31
- package/dist/unpkg/prod/declarations/data-worker/utils.d.ts +0 -12
- package/dist/unpkg/prod/declarations/data-worker/worker.d.ts +0 -23
- package/dist/unpkg/prod/declarations/data-worker.d.ts +0 -2
- package/dist/unpkg/prod/declarations/document-storage/index.d.ts +0 -90
- package/dist/unpkg/prod/declarations/document-storage.d.ts +0 -1
- package/dist/unpkg/prod/declarations/image-fetch.d.ts +0 -1
- package/dist/unpkg/prod/declarations/image-worker/fetch.d.ts +0 -18
- package/dist/unpkg/prod/declarations/image-worker/types.d.ts +0 -21
- package/dist/unpkg/prod/declarations/image-worker/worker.d.ts +0 -14
- package/dist/unpkg/prod/declarations/image-worker.d.ts +0 -1
- package/dist/unpkg/prod/declarations/worker-fetch.d.ts +0 -1
- package/dist/unpkg/prod/index-CGCX7hY2.js +0 -349
- package/dist/unpkg/prod-deprecated/declarations/data-worker/cache-handler.d.ts +0 -7
- package/dist/unpkg/prod-deprecated/declarations/data-worker/fetch.d.ts +0 -21
- package/dist/unpkg/prod-deprecated/declarations/data-worker/types.d.ts +0 -31
- package/dist/unpkg/prod-deprecated/declarations/data-worker/utils.d.ts +0 -12
- package/dist/unpkg/prod-deprecated/declarations/data-worker/worker.d.ts +0 -23
- package/dist/unpkg/prod-deprecated/declarations/data-worker.d.ts +0 -2
- package/dist/unpkg/prod-deprecated/declarations/document-storage/index.d.ts +0 -90
- package/dist/unpkg/prod-deprecated/declarations/document-storage.d.ts +0 -1
- package/dist/unpkg/prod-deprecated/declarations/image-fetch.d.ts +0 -1
- package/dist/unpkg/prod-deprecated/declarations/image-worker/fetch.d.ts +0 -18
- package/dist/unpkg/prod-deprecated/declarations/image-worker/types.d.ts +0 -21
- package/dist/unpkg/prod-deprecated/declarations/image-worker/worker.d.ts +0 -14
- package/dist/unpkg/prod-deprecated/declarations/image-worker.d.ts +0 -1
- package/dist/unpkg/prod-deprecated/declarations/worker-fetch.d.ts +0 -1
- package/dist/unpkg/prod-deprecated/index-CGCX7hY2.js +0 -349
|
@@ -1 +1,339 @@
|
|
|
1
|
-
|
|
1
|
+
const WARP_DRIVE_STORAGE_FILE_NAME = 'warp-drive_document-storage';
|
|
2
|
+
const WARP_DRIVE_STORAGE_VERSION = 1;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* DocumentStorage is specifically designed around WarpDrive Cache and Request concepts.
|
|
6
|
+
*
|
|
7
|
+
* CacheFileDocument is a StructuredDocument (request response) whose `content` is
|
|
8
|
+
* the ResourceDocument returned by inserting the request into a Store's Cache.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A CacheDocument is a reconstructed request response that rehydrates ResourceDocument
|
|
13
|
+
* with the associated resources based on their identifiers.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
class InternalDocumentStorage {
|
|
17
|
+
constructor(options) {
|
|
18
|
+
this.options = options;
|
|
19
|
+
this._lastModified = 0;
|
|
20
|
+
this._invalidated = true;
|
|
21
|
+
this._fileHandle = this._open(options.scope);
|
|
22
|
+
this._channel = Object.assign(new BroadcastChannel(options.scope), {
|
|
23
|
+
onmessage: this._onMessage.bind(this)
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
_onMessage(_event) {
|
|
27
|
+
this._invalidated = true;
|
|
28
|
+
}
|
|
29
|
+
async _open(scope) {
|
|
30
|
+
const directoryHandle = await navigator.storage.getDirectory();
|
|
31
|
+
const fileHandle = await directoryHandle.getFileHandle(scope, {
|
|
32
|
+
create: true
|
|
33
|
+
});
|
|
34
|
+
return fileHandle;
|
|
35
|
+
}
|
|
36
|
+
async _read() {
|
|
37
|
+
if (this._filePromise) {
|
|
38
|
+
return this._filePromise;
|
|
39
|
+
}
|
|
40
|
+
if (this._invalidated) {
|
|
41
|
+
const updateFile = async () => {
|
|
42
|
+
const fileHandle = await this._fileHandle;
|
|
43
|
+
const file = await fileHandle.getFile();
|
|
44
|
+
const lastModified = file.lastModified;
|
|
45
|
+
if (lastModified === this._lastModified && this._cache) {
|
|
46
|
+
return this._cache;
|
|
47
|
+
}
|
|
48
|
+
const contents = await file.text();
|
|
49
|
+
const cache = contents ? JSON.parse(contents) : {
|
|
50
|
+
documents: [],
|
|
51
|
+
resources: []
|
|
52
|
+
};
|
|
53
|
+
const documents = new Map(cache.documents);
|
|
54
|
+
const resources = new Map(cache.resources);
|
|
55
|
+
const cacheMap = {
|
|
56
|
+
documents,
|
|
57
|
+
resources
|
|
58
|
+
};
|
|
59
|
+
this._cache = cacheMap;
|
|
60
|
+
this._invalidated = false;
|
|
61
|
+
this._lastModified = lastModified;
|
|
62
|
+
return cacheMap;
|
|
63
|
+
};
|
|
64
|
+
this._filePromise = updateFile();
|
|
65
|
+
await this._filePromise;
|
|
66
|
+
this._filePromise = null;
|
|
67
|
+
}
|
|
68
|
+
return this._cache;
|
|
69
|
+
}
|
|
70
|
+
async _patch(documentKey, document, updatedResources) {
|
|
71
|
+
const fileHandle = await this._fileHandle;
|
|
72
|
+
// secure a lock before getting latest state
|
|
73
|
+
const writable = await fileHandle.createWritable();
|
|
74
|
+
const cache = await this._read();
|
|
75
|
+
cache.documents.set(documentKey, document);
|
|
76
|
+
updatedResources.forEach((resource, key) => {
|
|
77
|
+
cache.resources.set(key, resource);
|
|
78
|
+
});
|
|
79
|
+
const documents = [...cache.documents.entries()];
|
|
80
|
+
const resources = [...cache.resources.entries()];
|
|
81
|
+
const cacheFile = {
|
|
82
|
+
documents,
|
|
83
|
+
resources
|
|
84
|
+
};
|
|
85
|
+
await writable.write(JSON.stringify(cacheFile));
|
|
86
|
+
await writable.close();
|
|
87
|
+
this._channel.postMessage({
|
|
88
|
+
type: 'patch',
|
|
89
|
+
key: documentKey,
|
|
90
|
+
resources: [...updatedResources.keys()]
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
async getDocument(key) {
|
|
94
|
+
const cache = await this._read();
|
|
95
|
+
// clone the document to avoid leaking the internal cache
|
|
96
|
+
const document = safeDocumentHydrate(cache.documents.get(key.lid));
|
|
97
|
+
if (!document) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// expand the document with the resources
|
|
102
|
+
if (document.content) {
|
|
103
|
+
if (docHasData(document.content)) {
|
|
104
|
+
let data = null;
|
|
105
|
+
if (Array.isArray(document.content.data)) {
|
|
106
|
+
data = document.content.data.map(resourceIdentifier => {
|
|
107
|
+
const resource = cache.resources.get(resourceIdentifier.lid);
|
|
108
|
+
if (!resource) {
|
|
109
|
+
throw new Error(`Resource not found for ${resourceIdentifier.lid}`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// clone the resource to avoid leaking the internal cache
|
|
113
|
+
return structuredClone(resource);
|
|
114
|
+
});
|
|
115
|
+
} else if (document.content.data) {
|
|
116
|
+
const resource = cache.resources.get(document.content.data.lid);
|
|
117
|
+
if (!resource) {
|
|
118
|
+
throw new Error(`Resource not found for ${document.content.data.lid}`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// clone the resource to avoid leaking the internal cache
|
|
122
|
+
data = structuredClone(resource);
|
|
123
|
+
}
|
|
124
|
+
if (document.content.included) {
|
|
125
|
+
const included = document.content.included.map(resourceIdentifier => {
|
|
126
|
+
const resource = cache.resources.get(resourceIdentifier.lid);
|
|
127
|
+
if (!resource) {
|
|
128
|
+
throw new Error(`Resource not found for ${resourceIdentifier.lid}`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// clone the resource to avoid leaking the internal cache
|
|
132
|
+
return structuredClone(resource);
|
|
133
|
+
});
|
|
134
|
+
document.content.included = included;
|
|
135
|
+
}
|
|
136
|
+
document.content.data = data;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return document;
|
|
140
|
+
}
|
|
141
|
+
async putDocument(document, resourceCollector) {
|
|
142
|
+
const resources = new Map();
|
|
143
|
+
if (!document.content) {
|
|
144
|
+
throw new Error(`Document content is missing, only finalized documents can be stored`);
|
|
145
|
+
}
|
|
146
|
+
if (!document.content.lid) {
|
|
147
|
+
throw new Error(`Document content is missing a lid, only documents with a cache-key can be stored`);
|
|
148
|
+
}
|
|
149
|
+
if (docHasData(document.content)) {
|
|
150
|
+
this._getResources(document.content, resourceCollector, resources);
|
|
151
|
+
}
|
|
152
|
+
await this._patch(document.content.lid, safeDocumentSerialize(document), resources);
|
|
153
|
+
}
|
|
154
|
+
_getResources(document, resourceCollector, resources = new Map()) {
|
|
155
|
+
if (Array.isArray(document.data)) {
|
|
156
|
+
document.data.forEach(resourceIdentifier => {
|
|
157
|
+
const resource = resourceCollector(resourceIdentifier);
|
|
158
|
+
resources.set(resourceIdentifier.lid, structuredClone(resource));
|
|
159
|
+
});
|
|
160
|
+
} else if (document.data) {
|
|
161
|
+
const resource = resourceCollector(document.data);
|
|
162
|
+
resources.set(document.data.lid, structuredClone(resource));
|
|
163
|
+
}
|
|
164
|
+
if (document.included) {
|
|
165
|
+
document.included.forEach(resourceIdentifier => {
|
|
166
|
+
const resource = resourceCollector(resourceIdentifier);
|
|
167
|
+
resources.set(resourceIdentifier.lid, structuredClone(resource));
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return resources;
|
|
171
|
+
}
|
|
172
|
+
async putResources(document, resourceCollector) {
|
|
173
|
+
const fileHandle = await this._fileHandle;
|
|
174
|
+
// secure a lock before getting latest state
|
|
175
|
+
const writable = await fileHandle.createWritable();
|
|
176
|
+
const cache = await this._read();
|
|
177
|
+
const updatedResources = this._getResources(document, resourceCollector);
|
|
178
|
+
updatedResources.forEach((resource, key) => {
|
|
179
|
+
cache.resources.set(key, resource);
|
|
180
|
+
});
|
|
181
|
+
const documents = [...cache.documents.entries()];
|
|
182
|
+
const resources = [...cache.resources.entries()];
|
|
183
|
+
const cacheFile = {
|
|
184
|
+
documents,
|
|
185
|
+
resources
|
|
186
|
+
};
|
|
187
|
+
await writable.write(JSON.stringify(cacheFile));
|
|
188
|
+
await writable.close();
|
|
189
|
+
this._channel.postMessage({
|
|
190
|
+
type: 'patch',
|
|
191
|
+
key: null,
|
|
192
|
+
resources: [...updatedResources.keys()]
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
async clear(reset) {
|
|
196
|
+
const fileHandle = await this._fileHandle;
|
|
197
|
+
const writable = await fileHandle.createWritable();
|
|
198
|
+
await writable.write('');
|
|
199
|
+
await writable.close();
|
|
200
|
+
this._invalidated = true;
|
|
201
|
+
this._lastModified = 0;
|
|
202
|
+
this._cache = null;
|
|
203
|
+
this._filePromise = null;
|
|
204
|
+
this._channel.postMessage({
|
|
205
|
+
type: 'clear'
|
|
206
|
+
});
|
|
207
|
+
if (!reset) {
|
|
208
|
+
this._channel.close();
|
|
209
|
+
this._channel = null;
|
|
210
|
+
if (!this.options.isolated) {
|
|
211
|
+
Storages.delete(this.options.scope);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function safeDocumentSerialize(document) {
|
|
217
|
+
const doc = document;
|
|
218
|
+
const newDoc = {};
|
|
219
|
+
if ('request' in doc) {
|
|
220
|
+
newDoc.request = prepareRequest(doc.request);
|
|
221
|
+
}
|
|
222
|
+
if ('response' in doc) {
|
|
223
|
+
newDoc.response = prepareResponse(doc.response);
|
|
224
|
+
}
|
|
225
|
+
if ('content' in doc) {
|
|
226
|
+
newDoc.content = structuredClone(doc.content);
|
|
227
|
+
}
|
|
228
|
+
return newDoc;
|
|
229
|
+
}
|
|
230
|
+
function prepareRequest(request) {
|
|
231
|
+
const {
|
|
232
|
+
signal,
|
|
233
|
+
headers
|
|
234
|
+
} = request;
|
|
235
|
+
const requestCopy = Object.assign({}, request);
|
|
236
|
+
delete requestCopy.store;
|
|
237
|
+
if (signal instanceof AbortSignal) {
|
|
238
|
+
delete requestCopy.signal;
|
|
239
|
+
}
|
|
240
|
+
if (headers instanceof Headers) {
|
|
241
|
+
requestCopy.headers = Array.from(headers);
|
|
242
|
+
}
|
|
243
|
+
return requestCopy;
|
|
244
|
+
}
|
|
245
|
+
function prepareResponse(response) {
|
|
246
|
+
if (!response) return null;
|
|
247
|
+
const clone = {};
|
|
248
|
+
if (response.headers) {
|
|
249
|
+
clone.headers = Array.from(response.headers);
|
|
250
|
+
}
|
|
251
|
+
clone.ok = response.ok;
|
|
252
|
+
clone.redirected = response.redirected;
|
|
253
|
+
clone.status = response.status;
|
|
254
|
+
clone.statusText = response.statusText;
|
|
255
|
+
clone.type = response.type;
|
|
256
|
+
clone.url = response.url;
|
|
257
|
+
return clone;
|
|
258
|
+
}
|
|
259
|
+
function safeDocumentHydrate(document) {
|
|
260
|
+
const doc = document;
|
|
261
|
+
const newDoc = {};
|
|
262
|
+
if ('request' in doc) {
|
|
263
|
+
const headers = new Headers(doc.request.headers);
|
|
264
|
+
const req = Object.assign({}, doc.request, {
|
|
265
|
+
headers
|
|
266
|
+
});
|
|
267
|
+
newDoc.request = new Request(doc.request.url ?? '', req);
|
|
268
|
+
}
|
|
269
|
+
if ('response' in doc) {
|
|
270
|
+
const headers = new Headers(doc.response.headers);
|
|
271
|
+
const resp = Object.assign({}, doc.response, {
|
|
272
|
+
headers
|
|
273
|
+
});
|
|
274
|
+
newDoc.response = new Response(null, resp);
|
|
275
|
+
}
|
|
276
|
+
if ('content' in doc) {
|
|
277
|
+
newDoc.content = structuredClone(doc.content);
|
|
278
|
+
}
|
|
279
|
+
return newDoc;
|
|
280
|
+
}
|
|
281
|
+
function docHasData(doc) {
|
|
282
|
+
return 'data' in doc;
|
|
283
|
+
}
|
|
284
|
+
const Storages = new Map();
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* DocumentStorage is a wrapper around the StorageManager API that provides
|
|
288
|
+
* a simple interface for reading and updating documents and requests.
|
|
289
|
+
*
|
|
290
|
+
* Some goals for this experiment:
|
|
291
|
+
*
|
|
292
|
+
* - optimize for storing requests/documents
|
|
293
|
+
* - optimize for storing resources
|
|
294
|
+
* - optimize for looking up resources associated to a document
|
|
295
|
+
* - optimize for notifying cross-tab when data is updated
|
|
296
|
+
*
|
|
297
|
+
* optional features:
|
|
298
|
+
*
|
|
299
|
+
* - support for offline mode
|
|
300
|
+
* - ?? support for relationship based cache traversal
|
|
301
|
+
* - a way to index records by type + another field (e.g updatedAt/createAt/name)
|
|
302
|
+
* such that simple queries can be done without having to scan all records
|
|
303
|
+
*/
|
|
304
|
+
class DocumentStorage {
|
|
305
|
+
constructor(options = {}) {
|
|
306
|
+
options.isolated = options.isolated ?? false;
|
|
307
|
+
options.scope = options.scope ?? 'default';
|
|
308
|
+
const fileName = `${WARP_DRIVE_STORAGE_FILE_NAME}@version_${WARP_DRIVE_STORAGE_VERSION}:${options.scope}`;
|
|
309
|
+
if (!options.isolated && Storages.has(fileName)) {
|
|
310
|
+
const storage = Storages.get(fileName);
|
|
311
|
+
if (storage) {
|
|
312
|
+
this._storage = storage.deref();
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
const storage = new InternalDocumentStorage({
|
|
317
|
+
scope: fileName,
|
|
318
|
+
isolated: options.isolated
|
|
319
|
+
});
|
|
320
|
+
this._storage = storage;
|
|
321
|
+
if (!options.isolated) {
|
|
322
|
+
Storages.set(fileName, new WeakRef(storage));
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
getDocument(key) {
|
|
326
|
+
return this._storage.getDocument(key);
|
|
327
|
+
}
|
|
328
|
+
putDocument(document, resourceCollector) {
|
|
329
|
+
return this._storage.putDocument(document, resourceCollector);
|
|
330
|
+
}
|
|
331
|
+
putResources(document, resourceCollector) {
|
|
332
|
+
return this._storage.putResources(document, resourceCollector);
|
|
333
|
+
}
|
|
334
|
+
clear(reset) {
|
|
335
|
+
return this._storage.clear(reset);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export { DocumentStorage };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createDeferred } from '@warp-drive-mirror/core/request';
|
|
2
|
-
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
3
2
|
|
|
4
3
|
// @ts-expect-error untyped global
|
|
5
4
|
const isServerEnv = typeof FastBoot !== 'undefined';
|
|
@@ -8,12 +7,6 @@ class ImageFetch {
|
|
|
8
7
|
this.threadId = isServerEnv ? '' : crypto.randomUUID();
|
|
9
8
|
this.pending = new Map();
|
|
10
9
|
this.cache = new Map();
|
|
11
|
-
const isTesting = macroCondition(getGlobalConfig().WarpDriveMirror.env.TESTING) ? true : false;
|
|
12
|
-
macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
|
|
13
|
-
if (!test) {
|
|
14
|
-
throw new Error(`Expected a SharedWorker instance`);
|
|
15
|
-
}
|
|
16
|
-
})(isTesting || isServerEnv || worker instanceof SharedWorker) : {};
|
|
17
10
|
this.worker = worker;
|
|
18
11
|
if (!isServerEnv) {
|
|
19
12
|
const fn = event => {
|
|
@@ -77,4 +70,5 @@ class ImageFetch {
|
|
|
77
70
|
return deferred.promise;
|
|
78
71
|
}
|
|
79
72
|
}
|
|
80
|
-
|
|
73
|
+
|
|
74
|
+
export { ImageFetch };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createDeferred } from '@warp-drive-mirror/core/request';
|
|
2
|
-
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
3
2
|
|
|
4
3
|
// @ts-expect-error untyped global
|
|
5
4
|
const isServerEnv = typeof FastBoot !== 'undefined';
|
|
@@ -26,12 +25,6 @@ class WorkerFetch {
|
|
|
26
25
|
constructor(worker) {
|
|
27
26
|
this.threadId = isServerEnv ? '' : crypto.randomUUID();
|
|
28
27
|
this.pending = new Map();
|
|
29
|
-
const isTesting = macroCondition(getGlobalConfig().WarpDriveMirror.env.TESTING) ? true : false;
|
|
30
|
-
macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
|
|
31
|
-
if (!test) {
|
|
32
|
-
throw new Error(`Expected a SharedWorker instance`);
|
|
33
|
-
}
|
|
34
|
-
})(isTesting || isServerEnv || worker instanceof SharedWorker) : {};
|
|
35
28
|
this.worker = worker;
|
|
36
29
|
if (!isServerEnv) {
|
|
37
30
|
const fn = event => {
|
|
@@ -161,4 +154,5 @@ function prepareRequest(request) {
|
|
|
161
154
|
request: requestCopy
|
|
162
155
|
};
|
|
163
156
|
}
|
|
164
|
-
|
|
157
|
+
|
|
158
|
+
export { WorkerFetch };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@warp-drive-mirror/experiments",
|
|
3
3
|
"description": "Experimental features for WarpDrive",
|
|
4
|
-
"version": "0.2.6-alpha.
|
|
4
|
+
"version": "0.2.6-alpha.40",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Chris Thoburn <runspired@users.noreply.github.com>",
|
|
7
7
|
"repository": {
|
|
@@ -13,62 +13,42 @@
|
|
|
13
13
|
"bugs": "https://github.com/warp-drive-data/warp-drive/issues",
|
|
14
14
|
"exports": {
|
|
15
15
|
"./document-storage": {
|
|
16
|
-
"unpkg":
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
},
|
|
21
|
-
"deprecations": "./dist/unpkg/prod-deprecated/document-storage.js",
|
|
22
|
-
"default": "./dist/unpkg/prod/document-storage.js"
|
|
23
|
-
},
|
|
16
|
+
"unpkg-dev-deprecated": "./dist/unpkg/dev-deprecated/document-storage.js",
|
|
17
|
+
"unpkg-dev": "./dist/unpkg/dev/document-storage.js",
|
|
18
|
+
"unpkg-deprecated": "./dist/unpkg/prod-deprecated/document-storage.js",
|
|
19
|
+
"unpkg": "./dist/unpkg/prod/document-storage.js",
|
|
24
20
|
"types": "./declarations/document-storage.d.ts",
|
|
25
21
|
"default": "./dist/document-storage.js"
|
|
26
22
|
},
|
|
27
23
|
"./data-worker": {
|
|
28
|
-
"unpkg":
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
},
|
|
33
|
-
"deprecations": "./dist/unpkg/prod-deprecated/data-worker.js",
|
|
34
|
-
"default": "./dist/unpkg/prod/data-worker.js"
|
|
35
|
-
},
|
|
24
|
+
"unpkg-dev-deprecated": "./dist/unpkg/dev-deprecated/data-worker.js",
|
|
25
|
+
"unpkg-dev": "./dist/unpkg/dev/data-worker.js",
|
|
26
|
+
"unpkg-deprecated": "./dist/unpkg/prod-deprecated/data-worker.js",
|
|
27
|
+
"unpkg": "./dist/unpkg/prod/data-worker.js",
|
|
36
28
|
"types": "./declarations/data-worker.d.ts",
|
|
37
29
|
"default": "./dist/data-worker.js"
|
|
38
30
|
},
|
|
39
31
|
"./worker-fetch": {
|
|
40
|
-
"unpkg":
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
},
|
|
45
|
-
"deprecations": "./dist/unpkg/prod-deprecated/worker-fetch.js",
|
|
46
|
-
"default": "./dist/unpkg/prod/worker-fetch.js"
|
|
47
|
-
},
|
|
32
|
+
"unpkg-dev-deprecated": "./dist/unpkg/dev-deprecated/worker-fetch.js",
|
|
33
|
+
"unpkg-dev": "./dist/unpkg/dev/worker-fetch.js",
|
|
34
|
+
"unpkg-deprecated": "./dist/unpkg/prod-deprecated/worker-fetch.js",
|
|
35
|
+
"unpkg": "./dist/unpkg/prod/worker-fetch.js",
|
|
48
36
|
"types": "./declarations/worker-fetch.d.ts",
|
|
49
37
|
"default": "./dist/worker-fetch.js"
|
|
50
38
|
},
|
|
51
39
|
"./image-worker": {
|
|
52
|
-
"unpkg":
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
},
|
|
57
|
-
"deprecations": "./dist/unpkg/prod-deprecated/image-worker.js",
|
|
58
|
-
"default": "./dist/unpkg/prod/image-worker.js"
|
|
59
|
-
},
|
|
40
|
+
"unpkg-dev-deprecated": "./dist/unpkg/dev-deprecated/image-worker.js",
|
|
41
|
+
"unpkg-dev": "./dist/unpkg/dev/image-worker.js",
|
|
42
|
+
"unpkg-deprecated": "./dist/unpkg/prod-deprecated/image-worker.js",
|
|
43
|
+
"unpkg": "./dist/unpkg/prod/image-worker.js",
|
|
60
44
|
"types": "./declarations/image-worker.d.ts",
|
|
61
45
|
"default": "./dist/image-worker.js"
|
|
62
46
|
},
|
|
63
47
|
"./image-fetch": {
|
|
64
|
-
"unpkg":
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
},
|
|
69
|
-
"deprecations": "./dist/unpkg/prod-deprecated/image-fetch.js",
|
|
70
|
-
"default": "./dist/unpkg/prod/image-fetch.js"
|
|
71
|
-
},
|
|
48
|
+
"unpkg-dev-deprecated": "./dist/unpkg/dev-deprecated/image-fetch.js",
|
|
49
|
+
"unpkg-dev": "./dist/unpkg/dev/image-fetch.js",
|
|
50
|
+
"unpkg-deprecated": "./dist/unpkg/prod-deprecated/image-fetch.js",
|
|
51
|
+
"unpkg": "./dist/unpkg/prod/image-fetch.js",
|
|
72
52
|
"types": "./declarations/image-fetch.d.ts",
|
|
73
53
|
"default": "./dist/image-fetch.js"
|
|
74
54
|
}
|
|
@@ -84,7 +64,7 @@
|
|
|
84
64
|
],
|
|
85
65
|
"peerDependencies": {
|
|
86
66
|
"@sqlite.org/sqlite-wasm": "3.46.0-build2",
|
|
87
|
-
"@warp-drive-mirror/core": "5.8.0-alpha.
|
|
67
|
+
"@warp-drive-mirror/core": "5.8.0-alpha.40"
|
|
88
68
|
},
|
|
89
69
|
"peerDependenciesMeta": {
|
|
90
70
|
"@sqlite.org/sqlite-wasm": {
|
|
@@ -99,8 +79,8 @@
|
|
|
99
79
|
"@babel/plugin-transform-typescript": "^7.28.0",
|
|
100
80
|
"@babel/preset-env": "^7.28.3",
|
|
101
81
|
"@babel/preset-typescript": "^7.27.1",
|
|
102
|
-
"@warp-drive/internal-config": "5.8.0-alpha.
|
|
103
|
-
"@warp-drive-mirror/core": "5.8.0-alpha.
|
|
82
|
+
"@warp-drive/internal-config": "5.8.0-alpha.40",
|
|
83
|
+
"@warp-drive-mirror/core": "5.8.0-alpha.40",
|
|
104
84
|
"@sqlite.org/sqlite-wasm": "3.46.0-build2",
|
|
105
85
|
"typescript": "^5.9.2",
|
|
106
86
|
"vite": "^7.1.3"
|
|
@@ -1,7 +0,0 @@
|
|
|
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;
|
|
@@ -1,21 +0,0 @@
|
|
|
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 {};
|
|
@@ -1,31 +0,0 @@
|
|
|
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>;
|
|
@@ -1,12 +0,0 @@
|
|
|
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 {};
|
|
@@ -1,23 +0,0 @@
|
|
|
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
|
-
}
|