@warp-drive-mirror/experiments 0.0.1-alpha.99 → 0.0.1-beta.1

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 (34) hide show
  1. package/README.md +2 -1
  2. package/dist/data-worker.js +124 -31
  3. package/dist/data-worker.js.map +1 -1
  4. package/dist/document-storage.js +1 -243
  5. package/dist/document-storage.js.map +1 -1
  6. package/dist/image-fetch.js +78 -0
  7. package/dist/image-fetch.js.map +1 -0
  8. package/dist/image-worker.js +98 -0
  9. package/dist/image-worker.js.map +1 -0
  10. package/dist/index-Cn3o840t.js +349 -0
  11. package/dist/index-Cn3o840t.js.map +1 -0
  12. package/dist/persisted-cache.js +19 -19
  13. package/dist/persisted-cache.js.map +1 -1
  14. package/dist/worker-fetch.js +24 -11
  15. package/dist/worker-fetch.js.map +1 -1
  16. package/package.json +37 -21
  17. package/unstable-preview-types/data-worker/cache-handler.d.ts.map +1 -1
  18. package/unstable-preview-types/data-worker/fetch.d.ts.map +1 -1
  19. package/unstable-preview-types/data-worker/worker.d.ts +11 -1
  20. package/unstable-preview-types/data-worker/worker.d.ts.map +1 -1
  21. package/unstable-preview-types/document-storage/index.d.ts +4 -1
  22. package/unstable-preview-types/document-storage/index.d.ts.map +1 -1
  23. package/unstable-preview-types/image-fetch.d.ts +4 -0
  24. package/unstable-preview-types/image-fetch.d.ts.map +1 -0
  25. package/unstable-preview-types/image-worker/fetch.d.ts +21 -0
  26. package/unstable-preview-types/image-worker/fetch.d.ts.map +1 -0
  27. package/unstable-preview-types/image-worker/types.d.ts +24 -0
  28. package/unstable-preview-types/image-worker/types.d.ts.map +1 -0
  29. package/unstable-preview-types/image-worker/worker.d.ts +20 -0
  30. package/unstable-preview-types/image-worker/worker.d.ts.map +1 -0
  31. package/unstable-preview-types/image-worker.d.ts +4 -0
  32. package/unstable-preview-types/image-worker.d.ts.map +1 -0
  33. package/unstable-preview-types/index.d.ts +13 -8
  34. package/unstable-preview-types/persisted-cache/cache.d.ts +19 -19
@@ -1 +1 @@
1
- {"version":3,"file":"document-storage.js","sources":["../src/document-storage/index.ts"],"sourcesContent":["import type { StructuredDocument } from '@ember-data-mirror/request';\nimport type { ExistingRecordIdentifier } from '@warp-drive-mirror/core-types/identifier';\nimport type { ResourceDataDocument, ResourceDocument } from '@warp-drive-mirror/core-types/spec/document';\nimport type { ExistingResourceObject } from '@warp-drive-mirror/core-types/spec/json-api-raw';\n\nexport const WARP_DRIVE_STORAGE_FILE_NAME = 'warp-drive_document-storage';\nexport const WARP_DRIVE_STORAGE_VERSION = 1;\n\nexport type DocumentStorageOptions = {\n /**\n * The scope of the storage. This is used to enable multiple distinct\n * storage areas within the same origin.\n *\n * One use case for this is to have a separate storage area for each\n * user credential. So for instance, in applications that allow a single\n * user to have multiple accounts, each account can have its own storage!\n */\n scope: string;\n /**\n * When set to true, if other instances of the storage are created with\n * the same scope, they will not share the same in-memory cache and BroadcastChannel.\n *\n * This is mostly useful for testing purposes to replicate the behavior of\n * multiple tabs or workers.\n */\n isolated: boolean;\n};\n/**\n * DocumentStorage is specifically designed around WarpDrive Cache and Request concepts.\n *\n * CacheFileDocument is a StructuredDocument (request response) whose `content` is\n * the ResourceDocument returned by inserting the request into a Store's Cache.\n */\ntype CacheFileDocument = StructuredDocument<ResourceDocument<ExistingRecordIdentifier>>;\n/**\n * A CacheDocument is a reconstructed request response that rehydrates ResourceDocument\n * with the associated resources based on their identifiers.\n */\ntype CacheDocument = StructuredDocument<ResourceDocument<ExistingResourceObject>>;\ntype CacheResourceEntry = [string, ExistingResourceObject];\ntype CacheFile = {\n documents: [string, CacheFileDocument][];\n resources: CacheResourceEntry[];\n};\ntype DocumentIdentifier = { lid: string };\n\ntype MemCache = {\n documents: Map<string, CacheFileDocument>;\n resources: Map<string, ExistingResourceObject>;\n};\n\nclass InternalDocumentStorage {\n declare readonly options: DocumentStorageOptions;\n declare _fileHandle: Promise<FileSystemFileHandle>;\n declare _channel: BroadcastChannel;\n declare _invalidated: boolean;\n declare _lastModified: number;\n declare _cache: MemCache | null;\n declare _filePromise: Promise<MemCache> | null;\n\n constructor(options: DocumentStorageOptions) {\n this.options = options;\n\n this._lastModified = 0;\n this._invalidated = true;\n this._fileHandle = this._open(options.scope);\n this._channel = Object.assign(new BroadcastChannel(options.scope), {\n onmessage: this._onMessage.bind(this),\n });\n }\n\n _onMessage(_event: MessageEvent) {\n this._invalidated = true;\n }\n\n async _open(scope: string) {\n const directoryHandle = await navigator.storage.getDirectory();\n const fileHandle = await directoryHandle.getFileHandle(scope, { create: true });\n return fileHandle;\n }\n\n async _read(): Promise<MemCache> {\n if (this._filePromise) {\n return this._filePromise;\n }\n\n if (this._invalidated) {\n const updateFile = async () => {\n const fileHandle = await this._fileHandle;\n const file = await fileHandle.getFile();\n\n const lastModified = file.lastModified;\n if (lastModified === this._lastModified && this._cache) {\n return this._cache;\n }\n\n const contents = await file.text();\n const cache = contents ? (JSON.parse(contents) as CacheFile) : ({ documents: [], resources: [] } as CacheFile);\n\n const documents = new Map(cache.documents);\n const resources = new Map(cache.resources);\n\n const cacheMap = { documents, resources };\n this._cache = cacheMap;\n this._invalidated = false;\n this._lastModified = lastModified;\n return cacheMap;\n };\n this._filePromise = updateFile();\n await this._filePromise;\n this._filePromise = null;\n }\n\n return this._cache!;\n }\n\n async _patch(\n documentKey: string,\n document: CacheFileDocument,\n updatedResources: Map<string, ExistingResourceObject>\n ) {\n const fileHandle = await this._fileHandle;\n // secure a lock before getting latest state\n const writable = await fileHandle.createWritable();\n\n const cache = await this._read();\n cache.documents.set(documentKey, document);\n updatedResources.forEach((resource, key) => {\n cache.resources.set(key, resource);\n });\n\n const documents = [...cache.documents.entries()];\n const resources = [...cache.resources.entries()];\n const cacheFile: CacheFile = {\n documents,\n resources,\n };\n\n await writable.write(JSON.stringify(cacheFile));\n await writable.close();\n this._channel.postMessage({ type: 'patch', key: documentKey, resources: [...updatedResources.keys()] });\n }\n\n async getDocument(key: DocumentIdentifier): Promise<CacheDocument | null> {\n const cache = await this._read();\n // clone the document to avoid leaking the internal cache\n const document = structuredClone(cache.documents.get(key.lid));\n\n if (!document) {\n return null;\n }\n\n // expand the document with the resources\n if (document.content) {\n if (docHasData(document.content)) {\n let data: ExistingResourceObject | ExistingResourceObject[] | null = null;\n if (Array.isArray(document.content.data)) {\n data = document.content.data.map((resourceIdentifier) => {\n const resource = cache.resources.get(resourceIdentifier.lid);\n if (!resource) {\n throw new Error(`Resource not found for ${resourceIdentifier.lid}`);\n }\n\n // clone the resource to avoid leaking the internal cache\n return structuredClone(resource);\n });\n } else if (document.content.data) {\n const resource = cache.resources.get(document.content.data.lid);\n if (!resource) {\n throw new Error(`Resource not found for ${document.content.data.lid}`);\n }\n\n // clone the resource to avoid leaking the internal cache\n data = structuredClone(resource);\n }\n\n if (document.content.included) {\n const included = document.content.included.map((resourceIdentifier) => {\n const resource = cache.resources.get(resourceIdentifier.lid);\n if (!resource) {\n throw new Error(`Resource not found for ${resourceIdentifier.lid}`);\n }\n\n // clone the resource to avoid leaking the internal cache\n return structuredClone(resource);\n });\n document.content.included = included as ExistingRecordIdentifier[];\n }\n\n document.content.data = data as unknown as ResourceDataDocument<ExistingRecordIdentifier>['data'];\n }\n }\n\n return document as CacheDocument;\n }\n\n async putDocument(\n document: CacheFileDocument,\n resourceCollector: (resourceIdentifier: ExistingRecordIdentifier) => ExistingResourceObject\n ): Promise<void> {\n const resources = new Map<string, ExistingResourceObject>();\n\n if (!document.content) {\n throw new Error(`Document content is missing, only finalized documents can be stored`);\n }\n\n if (!document.content.lid) {\n throw new Error(`Document content is missing a lid, only documents with a cache-key can be stored`);\n }\n\n if (docHasData(document.content)) {\n if (Array.isArray(document.content.data)) {\n document.content.data.forEach((resourceIdentifier) => {\n const resource = resourceCollector(resourceIdentifier);\n resources.set(resourceIdentifier.lid, structuredClone(resource));\n });\n } else if (document.content.data) {\n const resource = resourceCollector(document.content.data);\n resources.set(document.content.data.lid, structuredClone(resource));\n }\n\n if (document.content.included) {\n document.content.included.forEach((resourceIdentifier) => {\n const resource = resourceCollector(resourceIdentifier);\n resources.set(resourceIdentifier.lid, structuredClone(resource));\n });\n }\n }\n\n await this._patch(document.content.lid, structuredClone(document), resources);\n }\n\n async clear(reset?: boolean) {\n const fileHandle = await this._fileHandle;\n const writable = await fileHandle.createWritable();\n await writable.write('');\n await writable.close();\n\n this._invalidated = true;\n this._lastModified = 0;\n this._cache = null;\n this._filePromise = null;\n this._channel.postMessage({ type: 'clear' });\n\n if (!reset) {\n this._channel.close();\n this._channel = null as unknown as BroadcastChannel;\n\n if (!this.options.isolated) {\n Storages.delete(this.options.scope);\n }\n }\n }\n}\n\nfunction docHasData<T>(doc: ResourceDocument<T>): doc is ResourceDataDocument<T> {\n return 'data' in doc;\n}\n\nconst Storages = new Map<string, WeakRef<InternalDocumentStorage>>();\n\n/**\n * DocumentStorage is a wrapper around the StorageManager API that provides\n * a simple interface for reading and updating documents and requests.\n *\n * Some goals for this experiment:\n *\n * - optimize for storing requests/documents\n * - optimize for storing resources\n * - optimize for looking up resources associated to a document\n * - optimize for notifying cross-tab when data is updated\n *\n * optional features:\n *\n * - support for offline mode\n * - ?? support for relationship based cache traversal\n * - a way to index records by type + another field (e.g updatedAt/createAt/name)\n * such that simple queries can be done without having to scan all records\n */\nexport class DocumentStorage {\n declare readonly _storage: InternalDocumentStorage;\n\n constructor(options: Partial<DocumentStorageOptions> = {}) {\n options.isolated = options.isolated ?? false;\n options.scope = options.scope ?? 'default';\n\n const fileName = `${WARP_DRIVE_STORAGE_FILE_NAME}@version_${WARP_DRIVE_STORAGE_VERSION}:${options.scope}`;\n if (!options.isolated && Storages.has(fileName)) {\n const storage = Storages.get(fileName);\n if (storage) {\n this._storage = storage.deref()!;\n return;\n }\n }\n\n const storage = new InternalDocumentStorage({ scope: fileName, isolated: options.isolated });\n this._storage = storage;\n if (!options.isolated) {\n Storages.set(fileName, new WeakRef(storage));\n }\n }\n\n getDocument(key: DocumentIdentifier): Promise<CacheDocument | null> {\n return this._storage.getDocument(key);\n }\n\n putDocument(\n document: CacheFileDocument,\n resourceCollector: (resourceIdentifier: ExistingRecordIdentifier) => ExistingResourceObject\n ): Promise<void> {\n return this._storage.putDocument(document, resourceCollector);\n }\n\n clear(reset?: boolean) {\n return this._storage.clear(reset);\n }\n}\n"],"names":["WARP_DRIVE_STORAGE_FILE_NAME","WARP_DRIVE_STORAGE_VERSION","InternalDocumentStorage","constructor","options","_lastModified","_invalidated","_fileHandle","_open","scope","_channel","Object","assign","BroadcastChannel","onmessage","_onMessage","bind","_event","directoryHandle","navigator","storage","getDirectory","fileHandle","getFileHandle","create","_read","_filePromise","updateFile","file","getFile","lastModified","_cache","contents","text","cache","JSON","parse","documents","resources","Map","cacheMap","_patch","documentKey","document","updatedResources","writable","createWritable","set","forEach","resource","key","entries","cacheFile","write","stringify","close","postMessage","type","keys","getDocument","structuredClone","get","lid","content","docHasData","data","Array","isArray","map","resourceIdentifier","Error","included","putDocument","resourceCollector","clear","reset","isolated","Storages","delete","doc","DocumentStorage","fileName","has","_storage","deref","WeakRef"],"mappings":"AAKO,MAAMA,4BAA4B,GAAG,6BAA6B,CAAA;AAClE,MAAMC,0BAA0B,GAAG,CAAC,CAAA;;AAqB3C;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAcA,MAAMC,uBAAuB,CAAC;EAS5BC,WAAWA,CAACC,OAA+B,EAAE;IAC3C,IAAI,CAACA,OAAO,GAAGA,OAAO,CAAA;IAEtB,IAAI,CAACC,aAAa,GAAG,CAAC,CAAA;IACtB,IAAI,CAACC,YAAY,GAAG,IAAI,CAAA;IACxB,IAAI,CAACC,WAAW,GAAG,IAAI,CAACC,KAAK,CAACJ,OAAO,CAACK,KAAK,CAAC,CAAA;AAC5C,IAAA,IAAI,CAACC,QAAQ,GAAGC,MAAM,CAACC,MAAM,CAAC,IAAIC,gBAAgB,CAACT,OAAO,CAACK,KAAK,CAAC,EAAE;AACjEK,MAAAA,SAAS,EAAE,I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;AAAS,KAAC,CAAC,CAAA;IAC5F,IAAI,CAACO,QAAQ,GAAG/D,OAAO,CAAA;AACvB,IAAA,IAAI,CAAChB,OAAO,CAACwE,QAAQ,EAAE;MACrBC,QAAQ,CAAC9B,GAAG,CAACkC,QAAQ,EAAE,IAAII,OAAO,CAACjE,OAAO,CAAC,CAAC,CAAA;AAC9C,KAAA;AACF,GAAA;EAEAuC,WAAWA,CAACT,GAAuB,EAAiC;AAClE,IAAA,OAAO,IAAI,CAACiC,QAAQ,CAACxB,WAAW,CAACT,GAAG,CAAC,CAAA;AACvC,GAAA;AAEAsB,EAAAA,WAAWA,CACT7B,QAA2B,EAC3B8B,iBAA2F,EAC5E;IACf,OAAO,IAAI,CAACU,QAAQ,CAACX,WAAW,CAAC7B,QAAQ,EAAE8B,iBAAiB,CAAC,CAAA;AAC/D,GAAA;EAEAC,KAAKA,CAACC,KAAe,EAAE;AACrB,IAAA,OAAO,IAAI,CAACQ,QAAQ,CAACT,KAAK,CAACC,KAAK,CAAC,CAAA;AACnC,GAAA;AACF;;;;"}
1
+ {"version":3,"file":"document-storage.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -0,0 +1,78 @@
1
+ import { createDeferred } from '@ember-data-mirror/request';
2
+ import { macroCondition, getGlobalConfig } from '@embroider/macros';
3
+ const isServerEnv = typeof FastBoot !== 'undefined';
4
+ class ImageFetch {
5
+ constructor(worker) {
6
+ this.threadId = isServerEnv ? '' : crypto.randomUUID();
7
+ this.pending = new Map();
8
+ this.cache = new Map();
9
+ const isTesting = macroCondition(getGlobalConfig().WarpDriveMirror.env.TESTING) ? true : false;
10
+ macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
11
+ if (!test) {
12
+ throw new Error(`Expected a SharedWorker instance`);
13
+ }
14
+ })(isTesting || isServerEnv || worker instanceof SharedWorker) : {};
15
+ this.worker = worker;
16
+ if (!isServerEnv) {
17
+ const fn = event => {
18
+ const {
19
+ type,
20
+ url
21
+ } = event.data;
22
+ const deferred = this.cleanupRequest(url);
23
+ if (!deferred) {
24
+ return;
25
+ }
26
+ if (type === 'success-response') {
27
+ deferred.resolve(url);
28
+ return;
29
+ }
30
+ if (type === 'error-response') {
31
+ deferred.reject(null);
32
+ return;
33
+ }
34
+ };
35
+ if (worker instanceof SharedWorker) {
36
+ worker.port.postMessage({
37
+ type: 'connect',
38
+ thread: this.threadId
39
+ });
40
+ worker.port.onmessage = fn;
41
+ } else if (worker) {
42
+ this.channel = new MessageChannel();
43
+ worker.postMessage({
44
+ type: 'connect',
45
+ thread: this.threadId
46
+ }, [this.channel.port2]);
47
+ this.channel.port1.onmessage = fn;
48
+ }
49
+ }
50
+ }
51
+ cleanupRequest(url) {
52
+ const deferred = this.pending.get(url);
53
+ this.pending.delete(url);
54
+ return deferred;
55
+ }
56
+ _send(event) {
57
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
58
+ this.worker instanceof SharedWorker ? this.worker.port.postMessage(event) : this.channel.port1.postMessage(event);
59
+ }
60
+ load(url) {
61
+ if (isServerEnv) {
62
+ return Promise.resolve(url);
63
+ }
64
+ const objectUrl = this.cache.get(url);
65
+ if (objectUrl) {
66
+ return Promise.resolve(objectUrl);
67
+ }
68
+ const deferred = createDeferred();
69
+ this.pending.set(url, deferred);
70
+ this._send({
71
+ type: 'load',
72
+ thread: this.threadId,
73
+ url
74
+ });
75
+ return deferred.promise;
76
+ }
77
+ }
78
+ export { ImageFetch };
@@ -0,0 +1 @@
1
+ {"version":3,"file":"image-fetch.js","sources":["../src/image-worker/fetch.ts"],"sourcesContent":["import { createDeferred } from '@ember-data-mirror/request';\nimport type { Deferred } from '@ember-data-mirror/request/-private/types';\nimport { TESTING } from '@warp-drive-mirror/build-config/env';\nimport { assert } from '@warp-drive-mirror/build-config/macros';\n\nimport type { MainThreadEvent, RequestEventData } from './types';\n\nexport interface FastBoot {\n require(moduleName: string): unknown;\n isFastBoot: boolean;\n request: Request;\n}\n\nconst isServerEnv = typeof FastBoot !== 'undefined';\n\nexport class ImageFetch {\n declare worker: Worker | SharedWorker;\n declare threadId: string;\n declare pending: Map<string, Deferred<string>>;\n declare channel: MessageChannel;\n declare cache: Map<string, string>;\n\n constructor(worker: Worker | SharedWorker | null) {\n this.threadId = isServerEnv ? '' : crypto.randomUUID();\n this.pending = new Map();\n this.cache = new Map();\n\n const isTesting = TESTING ? true : false;\n assert(`Expected a SharedWorker instance`, isTesting || isServerEnv || worker instanceof SharedWorker);\n this.worker = worker as SharedWorker;\n\n if (!isServerEnv) {\n const fn = (event: MainThreadEvent) => {\n const { type, url } = event.data;\n const deferred = this.cleanupRequest(url);\n if (!deferred) {\n return;\n }\n\n if (type === 'success-response') {\n deferred.resolve(url);\n return;\n }\n\n if (type === 'error-response') {\n deferred.reject(null);\n return;\n }\n };\n\n if (worker instanceof SharedWorker) {\n worker.port.postMessage({ type: 'connect', thread: this.threadId });\n worker.port.onmessage = fn;\n } else if (worker) {\n this.channel = new MessageChannel();\n worker.postMessage({ type: 'connect', thread: this.threadId }, [this.channel.port2]);\n\n this.channel.port1.onmessage = fn;\n }\n }\n }\n\n cleanupRequest(url: string) {\n const deferred = this.pending.get(url);\n this.pending.delete(url);\n\n return deferred;\n }\n\n _send(event: RequestEventData) {\n // eslint-disable-next-line @typescript-eslint/no-unused-expressions\n this.worker instanceof SharedWorker ? this.worker.port.postMessage(event) : this.channel.port1.postMessage(event);\n }\n\n load(url: string) {\n if (isServerEnv) {\n return Promise.resolve(url);\n }\n\n const objectUrl = this.cache.get(url);\n if (objectUrl) {\n return Promise.resolve(objectUrl);\n }\n\n const deferred = createDeferred<string>();\n this.pending.set(url, deferred);\n this._send({ type: 'load', thread: this.threadId, url });\n return deferred.promise;\n }\n}\n"],"names":["isServerEnv","FastBoot","ImageFetch","constructor","worker","threadId","crypto","randomUUID","pending","Map","cache","isTesting","macroCondition","getGlobalConfig","WarpDrive","env","TESTING","DEBUG","test","Error","SharedWorker","fn","event","type","url","data","deferred","cleanupRequest","resolve","reject","port","postMessage","thread","onmessage","channel","MessageChannel","port2","port1","get","delete","_send","load","Promise","objectUrl","createDeferred","set","promise"],"mappings":";;;AAaA,MAAMA,WAAW,GAAG,OAAOC,QAAQ,KAAK,WAAW,CAAA;AAE5C,MAAMC,UAAU,CAAC;EAOtBC,WAAWA,CAACC,MAAoC,EAAE;IAChD,IAAI,CAACC,QAAQ,GAAGL,WAAW,GAAG,EAAE,GAAGM,MAAM,CAACC,UAAU,EAAE,CAAA;AACtD,IAAA,IAAI,CAACC,OAAO,GAAG,IAAIC,GAAG,EAAE,CAAA;AACxB,IAAA,IAAI,CAACC,KAAK,GAAG,IAAID,GAAG,EAAE,CAAA;AAEtB,IAAA,MAAME,SAAS,GAAGC,cAAA,CAAAC,eAAA,EAAAC,CAAAA,SAAA,CAAAC,GAAA,CAAAC,OAAA,CAAU,GAAA,IAAI,GAAG,KAAK,CAAA;IACxCJ,cAAA,CAAAC,eAAA,EAAAC,CAAAA,SAAA,CAAAC,GAAA,CAAAE,KAAA,CAAA,GAAA,CAAAC,IAAA,IAAA;AAAA,MAAA,IAAA,CAAAA,IAAA,EAAA;QAAA,MAAAC,IAAAA,KAAA,CAAO,CAAkC,gCAAA,CAAA,CAAA,CAAA;AAAA,OAAA;AAAA,KAAA,EAAER,SAAS,IAAIX,WAAW,IAAII,MAAM,YAAYgB,YAAY,CAAA,GAAA,EAAA,CAAA;IACrG,IAAI,CAAChB,MAAM,GAAGA,MAAsB,CAAA;IAEpC,IAAI,CAACJ,WAAW,EAAE;MAChB,MAAMqB,EAAE,GAAIC,KAAsB,IAAK;QACrC,MAAM;UAAEC,IAAI;AAAEC,UAAAA,GAAAA;SAAK,GAAGF,KAAK,CAACG,IAAI,CAAA;AAChC,QAAA,MAAMC,QAAQ,GAAG,IAAI,CAACC,cAAc,CAACH,GAAG,CAAC,CAAA;QACzC,IAAI,CAACE,QAAQ,EAAE;AACb,UAAA,OAAA;AACF,SAAA;QAEA,IAAIH,IAAI,KAAK,kBAAkB,E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AACT,KAAK,CAAC,GAAG,IAAI,CAACY,OAAO,CAACG,KAAK,CAACN,WAAW,CAACT,KAAK,CAAC,CAAA;AACnH,GAAA;EAEAmB,IAAIA,CAACjB,GAAW,EAAE;AAChB,IAAA,IAAIxB,WAAW,EAAE;AACf,MAAA,OAAO0C,OAAO,CAACd,OAAO,CAACJ,GAAG,CAAC,CAAA;AAC7B,KAAA;IAEA,MAAMmB,SAAS,GAAG,IAAI,CAACjC,KAAK,CAAC4B,GAAG,CAACd,GAAG,CAAC,CAAA;AACrC,IAAA,IAAImB,SAAS,EAAE;AACb,MAAA,OAAOD,OAAO,CAACd,OAAO,CAACe,SAAS,CAAC,CAAA;AACnC,KAAA;AAEA,IAAA,MAAMjB,QAAQ,GAAGkB,cAAc,EAAU,CAAA;IACzC,IAAI,CAACpC,OAAO,CAACqC,GAAG,CAACrB,GAAG,EAAEE,QAAQ,CAAC,CAAA;IAC/B,IAAI,CAACc,KAAK,CAAC;AAAEjB,MAAAA,IAAI,EAAE,MAAM;MAAES,MAAM,EAAE,IAAI,CAAC3B,QAAQ;AAAEmB,MAAAA,GAAAA;AAAI,KAAC,CAAC,CAAA;IACxD,OAAOE,QAAQ,CAACoB,OAAO,CAAA;AACzB,GAAA;AACF;;;;"}
@@ -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 @@
1
+ {"version":3,"file":"image-worker.js","sources":["../src/image-worker/worker.ts"],"sourcesContent":["import type { RequestEventData, ThreadInitEventData, WorkerThreadEvent } from './types';\n\nconst WorkerScope = (globalThis as unknown as { SharedWorkerGlobalScope: FunctionConstructor }).SharedWorkerGlobalScope;\n\nasync function loadImage(url: string): Promise<string> {\n const response = await fetch(url);\n const fileBlob = await response.blob();\n return URL.createObjectURL(fileBlob);\n}\n\nexport class ImageWorker {\n declare threads: Map<string, MessagePort>;\n declare pendingImages: Map<string, Promise<string>>;\n declare options: { persisted: boolean };\n declare isSharedWorker: boolean;\n declare cache: Map<string, string>;\n\n constructor(options?: { persisted: boolean }) {\n // disable if running on main thread\n if (typeof window !== 'undefined') {\n return;\n }\n this.threads = new Map();\n this.pendingImages = new Map();\n this.cache = new Map();\n this.options = options || { persisted: false };\n this.isSharedWorker = WorkerScope && globalThis instanceof WorkerScope;\n this.initialize();\n }\n\n fetch(url: string): Promise<string> {\n const objectUrl = this.cache.get(url);\n\n if (objectUrl) {\n return Promise.resolve(objectUrl);\n }\n\n const pending = this.pendingImages.get(url);\n if (pending) {\n return pending;\n }\n\n const promise = loadImage(url);\n this.pendingImages.set(url, promise);\n return promise.finally(() => {\n this.pendingImages.delete(url);\n });\n }\n\n initialize() {\n if (this.isSharedWorker) {\n (globalThis as unknown as { onconnect: typeof globalThis.onmessage }).onconnect = (e) => {\n const port = e.ports[0];\n port.onmessage = (event: MessageEvent<ThreadInitEventData>) => {\n const { type } = event.data;\n\n switch (type) {\n case 'connect':\n this.setupThread(event.data.thread, port);\n break;\n }\n };\n port.start();\n };\n } else {\n globalThis.onmessage = (event: MessageEvent<ThreadInitEventData>) => {\n const { type } = event.data;\n\n switch (type) {\n case 'connect':\n this.setupThread(event.data.thread, event.ports[0]);\n break;\n }\n };\n }\n }\n\n setupThread(thread: string, port: MessagePort) {\n this.threads.set(thread, port);\n port.onmessage = (event: WorkerThreadEvent) => {\n if (event.type === 'close') {\n this.threads.delete(thread);\n return;\n }\n\n const { type } = event.data;\n switch (type) {\n case 'load':\n void this.request(event.data);\n break;\n }\n };\n }\n\n async request(event: RequestEventData) {\n const { thread, url } = event;\n\n const objectUrl = await this.fetch(url);\n const port = this.threads.get(thread)!;\n port.postMessage({ type: 'success-response', thread, url, objectUrl });\n }\n}\n"],"names":["WorkerScope","globalThis","SharedWorkerGlobalScope","loadImage","url","response","fetch","fileBlob","blob","URL","createObjectURL","ImageWorker","constructor","options","window","threads","Map","pendingImages","cache","persisted","isSharedWorker","initialize","objectUrl","get","Promise","resolve","pending","promise","set","finally","delete","onconnect","e","port","ports","onmessage","event","type","data","setupThread","thread","start","request","postMessage"],"mappings":"AAEA,MAAMA,WAAW,GAAIC,UAAU,CAAiEC,uBAAuB,CAAA;AAEvH,eAAeC,SAASA,CAACC,GAAW,EAAmB;AACrD,EAAA,MAAMC,QAAQ,GAAG,MAAMC,KAAK,CAACF,GAAG,CAAC,CAAA;AACjC,EAAA,MAAMG,QAAQ,GAAG,MAAMF,QAAQ,CAACG,IAAI,EAAE,CAAA;AACtC,EAAA,OAAOC,GAAG,CAACC,eAAe,CAACH,QAAQ,CAAC,CAAA;AACtC,CAAA;AAEO,MAAMI,WAAW,CAAC;EAOvBC,WAAWA,CAACC,OAAgC,EAAE;AAC5C;AACA,IAAA,IAAI,OAAOC,MAAM,KAAK,WAAW,EAAE;AACjC,MAAA,OAAA;AACF,KAAA;AACA,IAAA,IAAI,CAACC,OAAO,GAAG,IAAIC,GAAG,EAAE,CAAA;AACxB,IAAA,IAAI,CAACC,aAAa,GAAG,IAAID,GAAG,EAAE,CAAA;AAC9B,IAAA,IAAI,CAACE,KAAK,GAAG,IAAIF,GAAG,EAAE,CAAA;AACtB,IAAA,IAAI,CAACH,OAAO,GAAGA,OAAO,IAAI;AAAEM,MAAAA,SAAS,EAAE,KAAA;KAAO,CAAA;AAC9C,IAAA,IAAI,CAACC,cAAc,GAAGpB,WAAW,IAAIC,UAAU,YAAYD,WAAW,CAAA;IACtE,IAAI,CAACqB,UAAU,EAAE,CAAA;AACnB,GAAA;EAEAf,KAAKA,CAACF,GAAW,EAAmB;IAClC,MAAMkB,SAAS,GAAG,IAAI,CAACJ,KAAK,CAACK,GAAG,CAACnB,GAAG,CAAC,CAAA;AAErC,IAAA,IAAIkB,SAAS,EAAE;AACb,MAAA,OAAOE,OAAO,CAACC,OAAO,CAACH,SAAS,CAAC,CAAA;AACnC,KAAA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+ 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().WarpDriveMirror.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.entries());
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.entries());
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().WarpDriveMirror.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 };