@warp-drive-mirror/experiments 0.2.6-beta.0 → 0.2.6-beta.2
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 +28 -8
- 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,339 @@
|
|
|
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 };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { createDeferred } from '@warp-drive-mirror/core/request';
|
|
2
|
+
|
|
3
|
+
// @ts-expect-error untyped global
|
|
4
|
+
const isServerEnv = typeof FastBoot !== 'undefined';
|
|
5
|
+
class ImageFetch {
|
|
6
|
+
constructor(worker) {
|
|
7
|
+
this.threadId = isServerEnv ? '' : crypto.randomUUID();
|
|
8
|
+
this.pending = new Map();
|
|
9
|
+
this.cache = new Map();
|
|
10
|
+
this.worker = worker;
|
|
11
|
+
if (!isServerEnv) {
|
|
12
|
+
const fn = event => {
|
|
13
|
+
const {
|
|
14
|
+
type,
|
|
15
|
+
url
|
|
16
|
+
} = event.data;
|
|
17
|
+
const deferred = this.cleanupRequest(url);
|
|
18
|
+
if (!deferred) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (type === 'success-response') {
|
|
22
|
+
deferred.resolve(url);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (type === 'error-response') {
|
|
26
|
+
deferred.reject(null);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
if (worker instanceof SharedWorker) {
|
|
31
|
+
worker.port.postMessage({
|
|
32
|
+
type: 'connect',
|
|
33
|
+
thread: this.threadId
|
|
34
|
+
});
|
|
35
|
+
worker.port.onmessage = fn;
|
|
36
|
+
} else if (worker) {
|
|
37
|
+
this.channel = new MessageChannel();
|
|
38
|
+
worker.postMessage({
|
|
39
|
+
type: 'connect',
|
|
40
|
+
thread: this.threadId
|
|
41
|
+
}, [this.channel.port2]);
|
|
42
|
+
this.channel.port1.onmessage = fn;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
cleanupRequest(url) {
|
|
47
|
+
const deferred = this.pending.get(url);
|
|
48
|
+
this.pending.delete(url);
|
|
49
|
+
return deferred;
|
|
50
|
+
}
|
|
51
|
+
_send(event) {
|
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
53
|
+
this.worker instanceof SharedWorker ? this.worker.port.postMessage(event) : this.channel.port1.postMessage(event);
|
|
54
|
+
}
|
|
55
|
+
load(url) {
|
|
56
|
+
if (isServerEnv) {
|
|
57
|
+
return Promise.resolve(url);
|
|
58
|
+
}
|
|
59
|
+
const objectUrl = this.cache.get(url);
|
|
60
|
+
if (objectUrl) {
|
|
61
|
+
return Promise.resolve(objectUrl);
|
|
62
|
+
}
|
|
63
|
+
const deferred = createDeferred();
|
|
64
|
+
this.pending.set(url, deferred);
|
|
65
|
+
this._send({
|
|
66
|
+
type: 'load',
|
|
67
|
+
thread: this.threadId,
|
|
68
|
+
url
|
|
69
|
+
});
|
|
70
|
+
return deferred.promise;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { ImageFetch };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const WorkerScope = globalThis.SharedWorkerGlobalScope;
|
|
2
|
+
async function loadImage(url) {
|
|
3
|
+
const response = await fetch(url);
|
|
4
|
+
const fileBlob = await response.blob();
|
|
5
|
+
return URL.createObjectURL(fileBlob);
|
|
6
|
+
}
|
|
7
|
+
class ImageWorker {
|
|
8
|
+
constructor(options) {
|
|
9
|
+
// disable if running on main thread
|
|
10
|
+
if (typeof window !== 'undefined') {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
this.threads = new Map();
|
|
14
|
+
this.pendingImages = new Map();
|
|
15
|
+
this.cache = new Map();
|
|
16
|
+
this.options = options || {
|
|
17
|
+
persisted: false
|
|
18
|
+
};
|
|
19
|
+
this.isSharedWorker = WorkerScope && globalThis instanceof WorkerScope;
|
|
20
|
+
this.initialize();
|
|
21
|
+
}
|
|
22
|
+
fetch(url) {
|
|
23
|
+
const objectUrl = this.cache.get(url);
|
|
24
|
+
if (objectUrl) {
|
|
25
|
+
return Promise.resolve(objectUrl);
|
|
26
|
+
}
|
|
27
|
+
const pending = this.pendingImages.get(url);
|
|
28
|
+
if (pending) {
|
|
29
|
+
return pending;
|
|
30
|
+
}
|
|
31
|
+
const promise = loadImage(url);
|
|
32
|
+
this.pendingImages.set(url, promise);
|
|
33
|
+
return promise.finally(() => {
|
|
34
|
+
this.pendingImages.delete(url);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
initialize() {
|
|
38
|
+
if (this.isSharedWorker) {
|
|
39
|
+
globalThis.onconnect = e => {
|
|
40
|
+
const port = e.ports[0];
|
|
41
|
+
port.onmessage = event => {
|
|
42
|
+
const {
|
|
43
|
+
type
|
|
44
|
+
} = event.data;
|
|
45
|
+
switch (type) {
|
|
46
|
+
case 'connect':
|
|
47
|
+
this.setupThread(event.data.thread, port);
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
port.start();
|
|
52
|
+
};
|
|
53
|
+
} else {
|
|
54
|
+
globalThis.onmessage = event => {
|
|
55
|
+
const {
|
|
56
|
+
type
|
|
57
|
+
} = event.data;
|
|
58
|
+
switch (type) {
|
|
59
|
+
case 'connect':
|
|
60
|
+
this.setupThread(event.data.thread, event.ports[0]);
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
setupThread(thread, port) {
|
|
67
|
+
this.threads.set(thread, port);
|
|
68
|
+
port.onmessage = event => {
|
|
69
|
+
if (event.type === 'close') {
|
|
70
|
+
this.threads.delete(thread);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const {
|
|
74
|
+
type
|
|
75
|
+
} = event.data;
|
|
76
|
+
switch (type) {
|
|
77
|
+
case 'load':
|
|
78
|
+
void this.request(event.data);
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
async request(event) {
|
|
84
|
+
const {
|
|
85
|
+
thread,
|
|
86
|
+
url
|
|
87
|
+
} = event;
|
|
88
|
+
const objectUrl = await this.fetch(url);
|
|
89
|
+
const port = this.threads.get(thread);
|
|
90
|
+
port.postMessage({
|
|
91
|
+
type: 'success-response',
|
|
92
|
+
thread,
|
|
93
|
+
url,
|
|
94
|
+
objectUrl
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export { ImageWorker };
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { createDeferred } from '@warp-drive-mirror/core/request';
|
|
2
|
+
|
|
3
|
+
// @ts-expect-error untyped global
|
|
4
|
+
const isServerEnv = typeof FastBoot !== 'undefined';
|
|
5
|
+
function isAggregateError(error) {
|
|
6
|
+
return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
|
|
7
|
+
}
|
|
8
|
+
function stitchTrace(stack, origin) {
|
|
9
|
+
if (origin.startsWith('Error\n')) {
|
|
10
|
+
return origin.slice(6) + '\n' + stack;
|
|
11
|
+
}
|
|
12
|
+
return origin + '\n' + stack;
|
|
13
|
+
}
|
|
14
|
+
function cloneError(error, stack) {
|
|
15
|
+
const isAggregate = isAggregateError(error);
|
|
16
|
+
const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
|
|
17
|
+
cloned.stack = stitchTrace(error.stack || '', stack);
|
|
18
|
+
cloned.error = error.error;
|
|
19
|
+
|
|
20
|
+
// copy over enumerable properties
|
|
21
|
+
Object.assign(cloned, error);
|
|
22
|
+
return cloned;
|
|
23
|
+
}
|
|
24
|
+
class WorkerFetch {
|
|
25
|
+
constructor(worker) {
|
|
26
|
+
this.threadId = isServerEnv ? '' : crypto.randomUUID();
|
|
27
|
+
this.pending = new Map();
|
|
28
|
+
this.worker = worker;
|
|
29
|
+
if (!isServerEnv) {
|
|
30
|
+
const fn = event => {
|
|
31
|
+
const {
|
|
32
|
+
type,
|
|
33
|
+
id,
|
|
34
|
+
data
|
|
35
|
+
} = event.data;
|
|
36
|
+
const info = this.cleanupRequest(id);
|
|
37
|
+
|
|
38
|
+
// typically this means the request was aborted
|
|
39
|
+
if (!info) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (type === 'success-response') {
|
|
43
|
+
const {
|
|
44
|
+
deferred
|
|
45
|
+
} = info;
|
|
46
|
+
const {
|
|
47
|
+
response,
|
|
48
|
+
content
|
|
49
|
+
} = data;
|
|
50
|
+
if (response) {
|
|
51
|
+
response.headers = new Headers(response.headers);
|
|
52
|
+
info.context.setResponse(new Response(null, response));
|
|
53
|
+
}
|
|
54
|
+
deferred.resolve(content);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (type === 'error-response') {
|
|
58
|
+
const {
|
|
59
|
+
deferred,
|
|
60
|
+
stack
|
|
61
|
+
} = info;
|
|
62
|
+
deferred.reject(cloneError(data, stack));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
if (worker instanceof SharedWorker) {
|
|
67
|
+
worker.port.postMessage({
|
|
68
|
+
type: 'connect',
|
|
69
|
+
thread: this.threadId
|
|
70
|
+
});
|
|
71
|
+
worker.port.onmessage = fn;
|
|
72
|
+
} else if (worker) {
|
|
73
|
+
this.channel = new MessageChannel();
|
|
74
|
+
worker.postMessage({
|
|
75
|
+
type: 'connect',
|
|
76
|
+
thread: this.threadId
|
|
77
|
+
}, [this.channel.port2]);
|
|
78
|
+
this.channel.port1.onmessage = fn;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
cleanupRequest(id) {
|
|
83
|
+
const info = this.pending.get(id);
|
|
84
|
+
this.pending.delete(id);
|
|
85
|
+
if (info?.signal) {
|
|
86
|
+
info.signal.removeEventListener('abort', info.abortFn);
|
|
87
|
+
}
|
|
88
|
+
return info;
|
|
89
|
+
}
|
|
90
|
+
send(event) {
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
92
|
+
this.worker instanceof SharedWorker ? this.worker.port.postMessage(event) : this.channel.port1.postMessage(event);
|
|
93
|
+
}
|
|
94
|
+
request(context, next) {
|
|
95
|
+
if (isServerEnv) {
|
|
96
|
+
return next(context.request);
|
|
97
|
+
}
|
|
98
|
+
const deferred = createDeferred();
|
|
99
|
+
const {
|
|
100
|
+
signal,
|
|
101
|
+
request
|
|
102
|
+
} = prepareRequest(context.request);
|
|
103
|
+
const abortFn = signal ? () => {
|
|
104
|
+
deferred.reject(enhanceReason(signal.reason));
|
|
105
|
+
this.send({
|
|
106
|
+
type: 'abort',
|
|
107
|
+
thread: this.threadId,
|
|
108
|
+
id: context.id,
|
|
109
|
+
data: signal.reason
|
|
110
|
+
});
|
|
111
|
+
this.cleanupRequest(context.id);
|
|
112
|
+
} : () => {
|
|
113
|
+
return;
|
|
114
|
+
};
|
|
115
|
+
signal?.addEventListener('abort', abortFn);
|
|
116
|
+
try {
|
|
117
|
+
throw new Error();
|
|
118
|
+
} catch (e) {
|
|
119
|
+
this.pending.set(context.id, {
|
|
120
|
+
context,
|
|
121
|
+
deferred,
|
|
122
|
+
signal,
|
|
123
|
+
abortFn,
|
|
124
|
+
stack: e.stack
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
this.send({
|
|
128
|
+
type: 'request',
|
|
129
|
+
thread: this.threadId,
|
|
130
|
+
id: context.id,
|
|
131
|
+
data: request
|
|
132
|
+
});
|
|
133
|
+
return deferred.promise;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function enhanceReason(reason) {
|
|
137
|
+
return new DOMException(reason || 'The user aborted a request.', 'AbortError');
|
|
138
|
+
}
|
|
139
|
+
function prepareRequest(request) {
|
|
140
|
+
const {
|
|
141
|
+
signal,
|
|
142
|
+
headers
|
|
143
|
+
} = request;
|
|
144
|
+
const requestCopy = Object.assign({}, request);
|
|
145
|
+
delete requestCopy.store;
|
|
146
|
+
if (signal instanceof AbortSignal) {
|
|
147
|
+
delete requestCopy.signal;
|
|
148
|
+
}
|
|
149
|
+
if (headers instanceof Headers) {
|
|
150
|
+
requestCopy.headers = Array.from(headers);
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
signal: signal || null,
|
|
154
|
+
request: requestCopy
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export { WorkerFetch };
|
package/logos/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
# Autogeneration Notice
|
|
2
2
|
|
|
3
|
-
This directory is maintained in the root of the monorepo and sync'd
|
|
4
|
-
repositories by running `bun sync-logos`
|
|
3
|
+
This directory is maintained in the root of the monorepo in `/logos/synced` and sync'd
|
|
4
|
+
to public repositories by running `bun sync-logos`
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="1498" height="1047" fill="none" viewBox="0 0 1498 1047"><g filter="url(#a)"><path fill="url(#b)" d="M132 175.075C132 149.076 153.138 128 179.213 128H1318.23c26.08 0 47.22 21.076 47.22 47.075v688.85c0 25.999-21.14 47.075-47.22 47.075H179.213C153.138 911 132 889.924 132 863.925z"/><path fill="#201328" d="M1043.4 587.312c0-39.962 30.84-72.357 68.87-72.357h53.09c38.04 0 68.88-32.395 68.88-72.357v-92.651c0-39.962-30.84-72.358-68.88-72.358h-53.09c-38.01 0-68.82 32.351-68.87 72.275v92.734c0 39.962-30.83 72.357-68.869 72.357h-53.089c-38.036 0-68.871-32.395-68.871-72.357v-87.409c0-39.962-30.835-72.358-68.872-72.358h-51.654c-38.037 0-68.872 32.396-68.872 72.358v87.409c0 39.962-30.835 72.357-68.872 72.357h-53.088c-38.037 0-68.872-32.395-68.872-72.357v-92.652c0-39.961-30.835-72.357-68.872-72.357h-53.088c-38.037 0-68.872 32.396-68.872 72.357v92.652c0 39.962 30.835 72.357 68.872 72.357h53.088c38.037 0 68.872 32.395 68.872 72.357v101.741c0 39.962 30.835 72.358 68.872 72.358h53.088c38.037 0 68.872-32.396 68.872-72.358V587.312c0-39.962 30.835-72.357 68.872-72.357h51.654c38.037 0 68.872 32.395 68.872 72.357v101.741c0 39.962 30.835 72.358 68.871 72.358h53.089c38.039 0 68.869-32.396 68.869-72.358z"/></g><defs><linearGradient id="b" x1="748.724" x2="1167.5" y1="128" y2="1070.23" gradientUnits="userSpaceOnUse"><stop stop-color="#ffc474"/><stop offset="1" stop-color="#ff9809"/></linearGradient><filter id="a" width="1497.45" height="1047" x="0" y="0" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feColorMatrix in="SourceAlpha" result="hardAlpha" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0"/><feOffset dy="4"/><feGaussianBlur stdDeviation="66"/><feComposite in2="hardAlpha" operator="out"/><feColorMatrix values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1 0"/><feBlend in2="BackgroundImageFix" result="effect1_dropShadow_757_188"/><feColorMatrix in="SourceAlpha" result="hardAlpha" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0"/><feOffset dy="4"/><feGaussianBlur stdDeviation="18"/><feComposite in2="hardAlpha" operator="out"/><feColorMatrix values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.25 0"/><feBlend in2="effect1_dropShadow_757_188" result="effect2_dropShadow_757_188"/><feColorMatrix in="SourceAlpha" result="hardAlpha" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0"/><feOffset dy="4"/><feGaussianBlur stdDeviation="2"/><feComposite in2="hardAlpha" operator="out"/><feColorMatrix values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.25 0"/><feBlend in2="effect2_dropShadow_757_188" result="effect3_dropShadow_757_188"/><feBlend in="SourceGraphic" in2="effect3_dropShadow_757_188" result="shape"/></filter></defs></svg>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="2081" height="200" fill="none" viewBox="0 0 2081 200"><path fill="#000" d="M2064.89 185.359c-15.57 5.16-31.15 8.846-46.72 11.058q-23.22 3.456-47.55 3.456c-20.73 0-39.21-2.212-55.43-6.635q-24.18-6.773-40.77-19.49c-11.06-8.478-19.49-18.844-25.3-31.1-5.8-12.256-8.71-26.125-8.71-41.606 0-14.375 2.91-27.69 8.71-39.947 5.9-12.348 14.19-23.037 24.88-32.068 10.78-9.123 23.78-16.218 38.98-21.286C1928.19 2.58 1945.14 0 1963.85 0c17.23 0 32.99 2.35 47.27 7.05 14.38 4.607 26.68 11.472 36.91 20.595q15.48 13.684 23.91 33.727c5.71 13.361 8.57 28.75 8.57 46.167v12.716h-187.16a48.3 48.3 0 0 0 8.16 16.449c3.87 4.976 9.08 9.215 15.62 12.717 6.54 3.501 14.56 6.22 24.05 8.155 9.58 1.935 21.01 2.903 34.28 2.903 9.4 0 18.61-.553 27.64-1.659 9.04-1.198 17.47-2.719 25.3-4.562q11.745-2.902 21.15-6.358c6.36-2.304 11.47-4.607 15.34-6.911zm-35.38-102.7c-.47-4.7-1.89-9.538-4.29-14.514-2.3-5.069-5.99-9.63-11.06-13.685s-11.7-7.371-19.9-9.952q-12.3-4.008-30.69-4.008-17.28 0-29.85 4.285-12.585 4.284-21.15 10.643c-5.62 4.239-10 8.8-13.13 13.684-3.14 4.884-5.16 9.4-6.09 13.546zm-284.63 112.099h-59.71l-96.06-189.92h57.22l69.11 142.924 68.98-142.924h57.22zm-238.29 0V4.838h51.28v189.92zm-32.63-146.932c-1.38-.461-3.4-1.06-6.08-1.797q-3.87-1.245-9.12-2.35-5.25-1.244-11.61-2.073c-4.15-.553-8.43-.83-12.86-.83-9.21 0-17.83 1.152-25.84 3.456q-11.895 3.317-22.26 8.984a125.3 125.3 0 0 0-19.35 12.717c-5.9 4.7-11.33 9.537-16.31 14.514v114.311h-51.28V4.838h51.28v30.824c6.17-4.608 12.49-9.03 18.94-13.27a163 163 0 0 1 20.32-11.472c7.09-3.318 14.56-5.944 22.39-7.88 7.83-2.026 16.12-3.04 24.88-3.04 3.32 0 6.68.184 10.09.553 3.5.276 6.86.645 10.09 1.106 3.32.46 6.4.967 9.26 1.52s5.35 1.106 7.46 1.659zM1263.71 99.66c0 12.164-1.33 22.991-4 32.483-2.68 9.399-6.45 17.6-11.34 24.604-4.79 7.003-10.6 12.947-17.42 17.831q-10.08 7.325-22.53 11.749c-8.29 2.948-17.23 5.114-26.81 6.496q-14.37 1.935-30 1.935h-120.53V4.838h120.26c10.41 0 20.41.691 29.99 2.073 9.59 1.29 18.52 3.41 26.82 6.359 8.38 2.948 15.99 6.865 22.8 11.749 6.82 4.791 12.63 10.735 17.42 17.83 4.89 7.004 8.66 15.205 11.34 24.605 2.67 9.399 4 20.134 4 32.206m-51.69 0q0-13.685-3.6-23.775c-2.3-6.727-6.08-12.256-11.33-16.587-5.16-4.423-11.93-7.694-20.32-9.814-8.38-2.211-18.61-3.317-30.68-3.317h-63.73v107.262h63.73c12.07 0 22.3-1.06 30.68-3.179 8.39-2.212 15.16-5.529 20.32-9.952 5.25-4.515 9.03-10.137 11.33-16.864q3.6-10.09 3.6-23.774M986.574 63.169q0 14.237-4.699 25.157-4.562 10.92-14.514 18.383-9.952 7.464-25.71 11.335-15.62 3.732-37.735 3.732h-85.285v72.982h-17.278V4.838h102.563q22.116 0 37.735 3.87 15.758 3.732 25.71 11.058t14.514 18.246q4.7 10.92 4.699 25.157m-17.831 0q0-13.546-4.561-21.84-4.423-8.294-13.823-12.716-9.26-4.562-23.636-6.082-14.237-1.52-33.865-1.52h-74.227v84.593h74.227q7.602 0 16.172.138 8.709 0 17.14-.83 8.432-.967 16.034-3.179 7.74-2.349 13.546-7.05 5.945-4.699 9.399-12.301 3.594-7.602 3.594-19.213M766.455 19.49q-4.01-1.244-11.197-2.627-7.049-1.52-18.245-1.52-15.481 0-29.166 3.87-13.545 3.732-25.295 10.229a126.3 126.3 0 0 0-21.563 15.205q-9.951 8.57-17.969 18.245v131.866h-17.278V4.838h17.278V43.54a166 166 0 0 1 20.043-17.555q10.92-8.017 22.945-13.684a125.8 125.8 0 0 1 25.157-8.985Q724.435 0 738.533 0q4.839 0 8.708.276 3.87.14 7.05.553 3.317.277 6.22.691 2.902.416 5.944.968zm-220.33 175.268v-21.01q-9.952 5.39-22.669 9.952-12.579 4.562-26.677 7.879-13.96 3.318-28.751 5.114-14.79 1.797-29.027 1.797-18.522 0-33.312-3.317-14.652-3.318-24.881-9.814-10.228-6.635-15.757-16.31-5.529-9.814-5.529-22.669 0-12.717 6.358-22.393 6.497-9.813 18.384-17.001 11.887-7.326 28.612-12.302 16.864-5.115 37.598-8.57 20.733-3.594 44.784-5.806 24.19-2.349 50.867-3.87V61.925q0-8.846-3.318-15.62-3.317-6.772-9.261-11.749-5.943-5.115-13.96-8.431-8.017-3.456-17.416-5.53-9.262-2.074-19.628-2.902a219 219 0 0 0-20.596-.968q-13.96 0-25.433 1.383-11.473 1.381-21.425 3.87-9.952 2.35-19.075 5.529a563 563 0 0 0-18.384 6.773V14.099q19.213-5.114 40.915-8.846 21.839-3.87 46.305-3.87 20.733 0 38.703 3.593 17.969 3.456 31.238 11.196 13.27 7.74 20.872 20.043 7.603 12.164 7.603 29.718v128.825zm0-102.977q-45.2 2.489-77.268 7.05-31.929 4.561-52.248 11.196-20.32 6.634-29.857 15.481-9.399 8.846-9.399 20.042 0 9.123 4.561 16.173 4.7 7.049 13.408 11.887 8.708 4.7 21.148 7.188 12.44 2.349 27.922 2.349 9.123 0 18.798-.967a276 276 0 0 0 19.49-2.903 319 319 0 0 0 19.075-4.423q9.4-2.627 17.693-5.667t15.066-6.497q6.912-3.455 11.611-7.049zM180.245 4.838l67.73 176.098L318.193 4.838h19.213l-76.438 189.92h-25.71L168.634 23.222l-66.486 171.536h-25.71L0 4.838h19.213l70.218 176.098L157.023 4.838z"/></svg>
|