@warp-drive-mirror/experiments 0.2.4-beta.0 → 0.2.4-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/addon-main.cjs +1 -1
- package/declarations/data-worker/cache-handler.d.ts +7 -0
- package/declarations/data-worker/fetch.d.ts +21 -0
- package/declarations/data-worker/types.d.ts +31 -0
- package/declarations/data-worker/utils.d.ts +21 -0
- package/declarations/data-worker/worker.d.ts +23 -0
- package/declarations/data-worker.d.ts +2 -0
- package/declarations/document-storage/index.d.ts +90 -0
- package/declarations/document-storage.d.ts +1 -0
- package/declarations/image-fetch.d.ts +1 -0
- package/declarations/image-worker/fetch.d.ts +18 -0
- package/declarations/image-worker/types.d.ts +21 -0
- package/declarations/image-worker/worker.d.ts +14 -0
- package/declarations/image-worker.d.ts +1 -0
- package/declarations/persisted-cache/cache.d.ts +465 -0
- package/declarations/persisted-cache.d.ts +1 -0
- package/declarations/worker-fetch.d.ts +1 -0
- package/dist/data-worker.js +1 -2
- package/dist/image-fetch.js +3 -1
- package/dist/persisted-cache.js +2 -36
- package/dist/worker-fetch.js +3 -1
- package/package.json +14 -19
- package/dist/data-worker.js.map +0 -1
- package/dist/document-storage.js.map +0 -1
- package/dist/image-fetch.js.map +0 -1
- package/dist/image-worker.js.map +0 -1
- package/dist/index-CGCX7hY2.js.map +0 -1
- package/dist/persisted-cache.js.map +0 -1
- package/dist/worker-fetch.js.map +0 -1
package/addon-main.cjs
CHANGED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { CacheHandler as CacheHandlerType } from "@warp-drive-mirror/core/request";
|
|
2
|
+
/**
|
|
3
|
+
* A simplified CacheHandler that hydrates ResourceDataDocuments from the cache
|
|
4
|
+
* with their referenced resources.
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
export declare const CacheHandler: CacheHandlerType;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Context, Deferred, Future, NextFn } from "@warp-drive-mirror/core/request";
|
|
2
|
+
import type { AbortEventData, RequestEventData } from "./types.js";
|
|
3
|
+
interface PendingItem {
|
|
4
|
+
context: Context;
|
|
5
|
+
signal: AbortSignal | null;
|
|
6
|
+
abortFn: () => void;
|
|
7
|
+
deferred: Deferred<unknown>;
|
|
8
|
+
stack: string;
|
|
9
|
+
}
|
|
10
|
+
export declare class WorkerFetch {
|
|
11
|
+
worker: Worker | SharedWorker;
|
|
12
|
+
threadId: string;
|
|
13
|
+
pending: Map<number, PendingItem>;
|
|
14
|
+
channel: MessageChannel;
|
|
15
|
+
constructor(worker: Worker | SharedWorker | null);
|
|
16
|
+
cleanupRequest(id: number): PendingItem | undefined;
|
|
17
|
+
send(event: RequestEventData | AbortEventData): void;
|
|
18
|
+
request<T>(context: Context, next: NextFn<T>): Promise<T> | Future<T>;
|
|
19
|
+
}
|
|
20
|
+
export declare function enhanceReason(reason?: string): DOMException;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { RequestInfo, StructuredDataDocument, StructuredErrorDocument } from "@warp-drive-mirror/core/types/request";
|
|
2
|
+
export type SuccessResponseEventData<T> = {
|
|
3
|
+
type: "success-response";
|
|
4
|
+
thread: string;
|
|
5
|
+
id: number;
|
|
6
|
+
data: StructuredDataDocument<T>;
|
|
7
|
+
};
|
|
8
|
+
export type ErrorResponseEventData<T> = {
|
|
9
|
+
type: "error-response";
|
|
10
|
+
thread: string;
|
|
11
|
+
id: number;
|
|
12
|
+
data: StructuredErrorDocument<T>;
|
|
13
|
+
};
|
|
14
|
+
export type RequestEventData = {
|
|
15
|
+
type: "request";
|
|
16
|
+
thread: string;
|
|
17
|
+
id: number;
|
|
18
|
+
data: RequestInfo;
|
|
19
|
+
};
|
|
20
|
+
export type AbortEventData = {
|
|
21
|
+
type: "abort";
|
|
22
|
+
thread: string;
|
|
23
|
+
id: number;
|
|
24
|
+
data: string;
|
|
25
|
+
};
|
|
26
|
+
export type ThreadInitEventData = {
|
|
27
|
+
type: "connect";
|
|
28
|
+
thread: string;
|
|
29
|
+
};
|
|
30
|
+
export type MainThreadEvent<T> = MessageEvent<SuccessResponseEventData<T> | ErrorResponseEventData<T>>;
|
|
31
|
+
export type WorkerThreadEvent = MessageEvent<RequestEventData> | MessageEvent<ThreadInitEventData> | MessageEvent<AbortEventData>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Store } from "@warp-drive-mirror/core";
|
|
2
|
+
import type { StableDocumentIdentifier } from "@warp-drive-mirror/core/types/identifier";
|
|
3
|
+
import type { ImmutableCreateRequestOptions, ImmutableDeleteRequestOptions, ImmutableRequestInfo, ImmutableUpdateRequestOptions, StructuredDataDocument } from "@warp-drive-mirror/core/types/request";
|
|
4
|
+
import type { ApiError } from "@warp-drive-mirror/core/types/spec/error";
|
|
5
|
+
export declare const MUTATION_OPS: Set<string>;
|
|
6
|
+
/**
|
|
7
|
+
* In a Worker, any time we are asked to make a request, data needs to be returned.
|
|
8
|
+
* background requests are ergo no different than foreground requests.
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export declare function calcShouldFetch(store: Store, request: ImmutableRequestInfo, hasCachedValue: boolean, identifier: StableDocumentIdentifier | null): boolean;
|
|
12
|
+
export declare function isMutation(request: Partial<ImmutableRequestInfo>): request is ImmutableUpdateRequestOptions | ImmutableCreateRequestOptions | ImmutableDeleteRequestOptions;
|
|
13
|
+
export declare function isCacheAffecting<T>(document: StructuredDataDocument<T>): boolean;
|
|
14
|
+
type RobustError = Error & {
|
|
15
|
+
error: string | object;
|
|
16
|
+
errors?: ApiError[];
|
|
17
|
+
content?: unknown;
|
|
18
|
+
};
|
|
19
|
+
// TODO @runspired, consider if we should deep freeze errors (potentially only in debug) vs cloning them
|
|
20
|
+
export declare function cloneError(error: RobustError): RobustError;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Store } from "@warp-drive-mirror/core";
|
|
2
|
+
import type { Future } from "@warp-drive-mirror/core/request";
|
|
3
|
+
import { DocumentStorage } from "../document-storage.js";
|
|
4
|
+
import type { AbortEventData, RequestEventData } from "./types.js";
|
|
5
|
+
export declare class DataWorker {
|
|
6
|
+
store: Store;
|
|
7
|
+
threads: Map<string, MessagePort>;
|
|
8
|
+
pending: Map<string, Map<number, Future<unknown>>>;
|
|
9
|
+
isSharedWorker: boolean;
|
|
10
|
+
options: {
|
|
11
|
+
persisted: boolean;
|
|
12
|
+
scope?: string;
|
|
13
|
+
};
|
|
14
|
+
storage: DocumentStorage;
|
|
15
|
+
constructor(UserStore: typeof Store, options?: {
|
|
16
|
+
persisted: boolean;
|
|
17
|
+
scope?: string;
|
|
18
|
+
});
|
|
19
|
+
initialize(): void;
|
|
20
|
+
setupThread(thread: string, port: MessagePort): void;
|
|
21
|
+
abortRequest(event: AbortEventData): void;
|
|
22
|
+
request(event: RequestEventData): Promise<void>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { ExistingRecordIdentifier } from "@warp-drive-mirror/core/types/identifier";
|
|
2
|
+
import type { StructuredDocument } from "@warp-drive-mirror/core/types/request";
|
|
3
|
+
import type { ResourceDataDocument, ResourceDocument } from "@warp-drive-mirror/core/types/spec/document";
|
|
4
|
+
import type { ExistingResourceObject } from "@warp-drive-mirror/core/types/spec/json-api-raw";
|
|
5
|
+
export declare const WARP_DRIVE_STORAGE_FILE_NAME = "warp-drive_document-storage";
|
|
6
|
+
export declare const WARP_DRIVE_STORAGE_VERSION = 1;
|
|
7
|
+
export type DocumentStorageOptions = {
|
|
8
|
+
/**
|
|
9
|
+
* The scope of the storage. This is used to enable multiple distinct
|
|
10
|
+
* storage areas within the same origin.
|
|
11
|
+
*
|
|
12
|
+
* One use case for this is to have a separate storage area for each
|
|
13
|
+
* user credential. So for instance, in applications that allow a single
|
|
14
|
+
* user to have multiple accounts, each account can have its own storage!
|
|
15
|
+
*/
|
|
16
|
+
scope: string;
|
|
17
|
+
/**
|
|
18
|
+
* When set to true, if other instances of the storage are created with
|
|
19
|
+
* the same scope, they will not share the same in-memory cache and BroadcastChannel.
|
|
20
|
+
*
|
|
21
|
+
* This is mostly useful for testing purposes to replicate the behavior of
|
|
22
|
+
* multiple tabs or workers.
|
|
23
|
+
*/
|
|
24
|
+
isolated: boolean;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* DocumentStorage is specifically designed around WarpDrive Cache and Request concepts.
|
|
28
|
+
*
|
|
29
|
+
* CacheFileDocument is a StructuredDocument (request response) whose `content` is
|
|
30
|
+
* the ResourceDocument returned by inserting the request into a Store's Cache.
|
|
31
|
+
*/
|
|
32
|
+
type CacheFileDocument = StructuredDocument<ResourceDocument<ExistingRecordIdentifier>>;
|
|
33
|
+
/**
|
|
34
|
+
* A CacheDocument is a reconstructed request response that rehydrates ResourceDocument
|
|
35
|
+
* with the associated resources based on their identifiers.
|
|
36
|
+
*/
|
|
37
|
+
type CacheDocument = StructuredDocument<ResourceDocument<ExistingResourceObject>>;
|
|
38
|
+
type DocumentIdentifier = {
|
|
39
|
+
lid: string;
|
|
40
|
+
};
|
|
41
|
+
type MemCache = {
|
|
42
|
+
documents: Map<string, CacheFileDocument>;
|
|
43
|
+
resources: Map<string, ExistingResourceObject>;
|
|
44
|
+
};
|
|
45
|
+
declare class InternalDocumentStorage {
|
|
46
|
+
readonly options: DocumentStorageOptions;
|
|
47
|
+
_fileHandle: Promise<FileSystemFileHandle>;
|
|
48
|
+
_channel: BroadcastChannel;
|
|
49
|
+
_invalidated: boolean;
|
|
50
|
+
_lastModified: number;
|
|
51
|
+
_cache: MemCache | null;
|
|
52
|
+
_filePromise: Promise<MemCache> | null;
|
|
53
|
+
constructor(options: DocumentStorageOptions);
|
|
54
|
+
_onMessage(_event: MessageEvent): void;
|
|
55
|
+
_open(scope: string): Promise<FileSystemFileHandle>;
|
|
56
|
+
_read(): Promise<MemCache>;
|
|
57
|
+
_patch(documentKey: string, document: CacheFileDocument, updatedResources: Map<string, ExistingResourceObject>): Promise<void>;
|
|
58
|
+
getDocument(key: DocumentIdentifier): Promise<CacheDocument | null>;
|
|
59
|
+
putDocument(document: CacheFileDocument, resourceCollector: (resourceIdentifier: ExistingRecordIdentifier) => ExistingResourceObject): Promise<void>;
|
|
60
|
+
_getResources(document: ResourceDataDocument<ExistingRecordIdentifier>, resourceCollector: (resourceIdentifier: ExistingRecordIdentifier) => ExistingResourceObject, resources?: Map<string, ExistingResourceObject>): Map<string, ExistingResourceObject>;
|
|
61
|
+
putResources(document: ResourceDataDocument<ExistingRecordIdentifier>, resourceCollector: (resourceIdentifier: ExistingRecordIdentifier) => ExistingResourceObject): Promise<void>;
|
|
62
|
+
clear(reset?: boolean): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* DocumentStorage is a wrapper around the StorageManager API that provides
|
|
66
|
+
* a simple interface for reading and updating documents and requests.
|
|
67
|
+
*
|
|
68
|
+
* Some goals for this experiment:
|
|
69
|
+
*
|
|
70
|
+
* - optimize for storing requests/documents
|
|
71
|
+
* - optimize for storing resources
|
|
72
|
+
* - optimize for looking up resources associated to a document
|
|
73
|
+
* - optimize for notifying cross-tab when data is updated
|
|
74
|
+
*
|
|
75
|
+
* optional features:
|
|
76
|
+
*
|
|
77
|
+
* - support for offline mode
|
|
78
|
+
* - ?? support for relationship based cache traversal
|
|
79
|
+
* - a way to index records by type + another field (e.g updatedAt/createAt/name)
|
|
80
|
+
* such that simple queries can be done without having to scan all records
|
|
81
|
+
*/
|
|
82
|
+
export declare class DocumentStorage {
|
|
83
|
+
readonly _storage: InternalDocumentStorage;
|
|
84
|
+
constructor(options?: Partial<DocumentStorageOptions>);
|
|
85
|
+
getDocument(key: DocumentIdentifier): Promise<CacheDocument | null>;
|
|
86
|
+
putDocument(document: CacheFileDocument, resourceCollector: (resourceIdentifier: ExistingRecordIdentifier) => ExistingResourceObject): Promise<void>;
|
|
87
|
+
putResources(document: ResourceDataDocument<ExistingRecordIdentifier>, resourceCollector: (resourceIdentifier: ExistingRecordIdentifier) => ExistingResourceObject): Promise<void>;
|
|
88
|
+
clear(reset?: boolean): Promise<void>;
|
|
89
|
+
}
|
|
90
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DocumentStorage } from "./document-storage/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ImageFetch } from "./image-worker/fetch.js";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Deferred } from "@warp-drive-mirror/core/request";
|
|
2
|
+
import type { RequestEventData } from "./types.js";
|
|
3
|
+
export interface FastBoot {
|
|
4
|
+
require(moduleName: string): unknown;
|
|
5
|
+
isFastBoot: boolean;
|
|
6
|
+
request: Request;
|
|
7
|
+
}
|
|
8
|
+
export declare class ImageFetch {
|
|
9
|
+
worker: Worker | SharedWorker;
|
|
10
|
+
threadId: string;
|
|
11
|
+
pending: Map<string, Deferred<string>>;
|
|
12
|
+
channel: MessageChannel;
|
|
13
|
+
cache: Map<string, string>;
|
|
14
|
+
constructor(worker: Worker | SharedWorker | null);
|
|
15
|
+
cleanupRequest(url: string): Deferred<string> | undefined;
|
|
16
|
+
_send(event: RequestEventData): void;
|
|
17
|
+
load(url: string): Promise<string>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type SuccessResponseEventData = {
|
|
2
|
+
type: "success-response";
|
|
3
|
+
thread: string;
|
|
4
|
+
url: string;
|
|
5
|
+
};
|
|
6
|
+
export type ErrorResponseEventData = {
|
|
7
|
+
type: "error-response";
|
|
8
|
+
thread: string;
|
|
9
|
+
url: string;
|
|
10
|
+
};
|
|
11
|
+
export type RequestEventData = {
|
|
12
|
+
type: "load";
|
|
13
|
+
thread: string;
|
|
14
|
+
url: string;
|
|
15
|
+
};
|
|
16
|
+
export type ThreadInitEventData = {
|
|
17
|
+
type: "connect";
|
|
18
|
+
thread: string;
|
|
19
|
+
};
|
|
20
|
+
export type MainThreadEvent = MessageEvent<SuccessResponseEventData | ErrorResponseEventData>;
|
|
21
|
+
export type WorkerThreadEvent = MessageEvent<RequestEventData> | MessageEvent<ThreadInitEventData>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare class ImageWorker {
|
|
2
|
+
private threads;
|
|
3
|
+
private pendingImages;
|
|
4
|
+
private options;
|
|
5
|
+
private isSharedWorker;
|
|
6
|
+
private cache;
|
|
7
|
+
constructor(options?: {
|
|
8
|
+
persisted: boolean;
|
|
9
|
+
});
|
|
10
|
+
private fetch;
|
|
11
|
+
private initialize;
|
|
12
|
+
private setupThread;
|
|
13
|
+
private request;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ImageWorker } from "./image-worker/worker.js";
|