@warp-drive/experiments 0.2.6-alpha.3 → 0.2.6-alpha.32

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