@serwist/background-sync 9.0.0-preview.16 → 9.0.0-preview.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Queue.d.ts DELETED
@@ -1,166 +0,0 @@
1
- interface OnSyncCallbackOptions {
2
- queue: Queue;
3
- }
4
- type OnSyncCallback = (options: OnSyncCallbackOptions) => void | Promise<void>;
5
- export interface QueueOptions {
6
- /**
7
- * If `true`, instead of attempting to use background sync events, always attempt
8
- * to replay queued request at service worker startup. Most folks will not need
9
- * this, unless you explicitly target a runtime like Electron that exposes the
10
- * interfaces for background sync, but does not have a working implementation.
11
- *
12
- * @default false
13
- */
14
- forceSyncFallback?: boolean;
15
- /**
16
- * The amount of time (in minutes) a request may be retried. After this amount
17
- * of time has passed, the request will be deleted from the queue.
18
- *
19
- * @default 60 * 24 * 7
20
- */
21
- maxRetentionTime?: number;
22
- /**
23
- * A function that gets invoked whenever the 'sync' event fires. The function
24
- * is invoked with an object containing the `queue` property (referencing this
25
- * instance), and you can use the callback to customize the replay behavior of
26
- * the queue. When not set the `replayRequests()` method is called. Note: if the
27
- * replay fails after a sync event, make sure you throw an error, so the browser
28
- * knows to retry the sync event later.
29
- */
30
- onSync?: OnSyncCallback;
31
- }
32
- export interface QueueEntry {
33
- /**
34
- * The request to store in the queue.
35
- */
36
- request: Request;
37
- /**
38
- * The timestamp (Epoch time in milliseconds) when the request was first added
39
- * to the queue. This is used along with `maxRetentionTime` to remove outdated
40
- * requests. In general you don't need to set this value, as it's automatically
41
- * set for you (defaulting to `Date.now()`), but you can update it if you don't
42
- * want particular requests to expire.
43
- */
44
- timestamp?: number;
45
- /**
46
- * Any metadata you want associated with the stored request. When requests are
47
- * replayed you'll have access to this metadata object in case you need to modify
48
- * the request beforehand.
49
- */
50
- metadata?: Record<string, unknown>;
51
- }
52
- /**
53
- * A class to manage storing failed requests in IndexedDB and retrying them
54
- * later. All parts of the storing and replaying process are observable via
55
- * callbacks.
56
- */
57
- export declare class Queue {
58
- private readonly _name;
59
- private readonly _onSync;
60
- private readonly _maxRetentionTime;
61
- private readonly _queueStore;
62
- private readonly _forceSyncFallback;
63
- private _syncInProgress;
64
- private _requestsAddedDuringSync;
65
- /**
66
- * Creates an instance of Queue with the given options
67
- *
68
- * @param name The unique name for this queue. This name must be
69
- * unique as it's used to register sync events and store requests
70
- * in IndexedDB specific to this instance. An error will be thrown if
71
- * a duplicate name is detected.
72
- * @param options
73
- */
74
- constructor(name: string, { forceSyncFallback, onSync, maxRetentionTime }?: QueueOptions);
75
- /**
76
- * @returns
77
- */
78
- get name(): string;
79
- /**
80
- * Stores the passed request in IndexedDB (with its timestamp and any
81
- * metadata) at the end of the queue.
82
- *
83
- * @param entry
84
- */
85
- pushRequest(entry: QueueEntry): Promise<void>;
86
- /**
87
- * Stores the passed request in IndexedDB (with its timestamp and any
88
- * metadata) at the beginning of the queue.
89
- *
90
- * @param entry
91
- */
92
- unshiftRequest(entry: QueueEntry): Promise<void>;
93
- /**
94
- * Removes and returns the last request in the queue (along with its
95
- * timestamp and any metadata).
96
- *
97
- * @returns
98
- */
99
- popRequest(): Promise<QueueEntry | undefined>;
100
- /**
101
- * Removes and returns the first request in the queue (along with its
102
- * timestamp and any metadata).
103
- *
104
- * @returns
105
- */
106
- shiftRequest(): Promise<QueueEntry | undefined>;
107
- /**
108
- * Returns all the entries that have not expired (per `maxRetentionTime`).
109
- * Any expired entries are removed from the queue.
110
- *
111
- * @returns
112
- */
113
- getAll(): Promise<QueueEntry[]>;
114
- /**
115
- * Returns the number of entries present in the queue.
116
- * Note that expired entries (per `maxRetentionTime`) are also included in this count.
117
- *
118
- * @returns
119
- */
120
- size(): Promise<number>;
121
- /**
122
- * Adds the entry to the QueueStore and registers for a sync event.
123
- *
124
- * @param entry
125
- * @param operation
126
- * @private
127
- */
128
- _addRequest({ request, metadata, timestamp }: QueueEntry, operation: "push" | "unshift"): Promise<void>;
129
- /**
130
- * Removes and returns the first or last (depending on `operation`) entry
131
- * from the QueueStore that's not older than the `maxRetentionTime`.
132
- *
133
- * @param operation
134
- * @returns
135
- * @private
136
- */
137
- _removeRequest(operation: "pop" | "shift"): Promise<QueueEntry | undefined>;
138
- /**
139
- * Loops through each request in the queue and attempts to re-fetch it.
140
- * If any request fails to re-fetch, it's put back in the same position in
141
- * the queue (which registers a retry for the next sync event).
142
- */
143
- replayRequests(): Promise<void>;
144
- /**
145
- * Registers a sync event with a tag unique to this instance.
146
- */
147
- registerSync(): Promise<void>;
148
- /**
149
- * In sync-supporting browsers, this adds a listener for the sync event.
150
- * In non-sync-supporting browsers, or if _forceSyncFallback is true, this
151
- * will retry the queue on service worker startup.
152
- *
153
- * @private
154
- */
155
- private _addSyncListener;
156
- /**
157
- * Returns the set of queue names. This is primarily used to reset the list
158
- * of queue names in tests.
159
- *
160
- * @returns
161
- * @private
162
- */
163
- static get _queueNames(): Set<string>;
164
- }
165
- export {};
166
- //# sourceMappingURL=Queue.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Queue.d.ts","sourceRoot":"","sources":["../src/Queue.ts"],"names":[],"mappings":"AAgBA,UAAU,qBAAqB;IAC7B,KAAK,EAAE,KAAK,CAAC;CACd;AAED,KAAK,cAAc,GAAG,CAAC,OAAO,EAAE,qBAAqB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE/E,MAAM,WAAW,YAAY;IAC3B;;;;;;;OAOG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;;;;;OAOG;IACH,MAAM,CAAC,EAAE,cAAc,CAAC;CACzB;AAED,MAAM,WAAW,UAAU;IACzB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO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AAc,IAAI,OAAO,CAAC,IAAI,CAAC;IAuBrC;;OAEG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IAenC;;;;;;OAMG;IACH,OAAO,CAAC,gBAAgB;IAmDxB;;;;;;OAMG;IACH,MAAM,KAAK,WAAW,IAAI,GAAG,CAAC,MAAM,CAAC,CAEpC;CACF"}
package/dist/QueueDb.d.ts DELETED
@@ -1,90 +0,0 @@
1
- import type { RequestData } from "./StorableRequest.js";
2
- export interface UnidentifiedQueueStoreEntry {
3
- requestData: RequestData;
4
- timestamp: number;
5
- id?: number;
6
- queueName?: string;
7
- metadata?: Record<string, unknown>;
8
- }
9
- export interface QueueStoreEntry extends UnidentifiedQueueStoreEntry {
10
- id: number;
11
- }
12
- /**
13
- * A class to interact directly an IndexedDB created specifically to save and
14
- * retrieve QueueStoreEntries. This class encapsulates all the schema details
15
- * to store the representation of a Queue.
16
- *
17
- * @private
18
- */
19
- export declare class QueueDb {
20
- private _db;
21
- /**
22
- * Add QueueStoreEntry to underlying db.
23
- *
24
- * @param entry
25
- */
26
- addEntry(entry: UnidentifiedQueueStoreEntry): Promise<void>;
27
- /**
28
- * Returns the first entry id in the ObjectStore.
29
- *
30
- * @returns
31
- */
32
- getFirstEntryId(): Promise<number | undefined>;
33
- /**
34
- * Get all the entries filtered by index
35
- *
36
- * @param queueName
37
- * @returns
38
- */
39
- getAllEntriesByQueueName(queueName: string): Promise<QueueStoreEntry[]>;
40
- /**
41
- * Returns the number of entries filtered by index
42
- *
43
- * @param queueName
44
- * @returns
45
- */
46
- getEntryCountByQueueName(queueName: string): Promise<number>;
47
- /**
48
- * Deletes a single entry by id.
49
- *
50
- * @param id the id of the entry to be deleted
51
- */
52
- deleteEntry(id: number): Promise<void>;
53
- /**
54
- *
55
- * @param queueName
56
- * @returns
57
- */
58
- getFirstEntryByQueueName(queueName: string): Promise<QueueStoreEntry | undefined>;
59
- /**
60
- *
61
- * @param queueName
62
- * @returns
63
- */
64
- getLastEntryByQueueName(queueName: string): Promise<QueueStoreEntry | undefined>;
65
- /**
66
- * Returns either the first or the last entries, depending on direction.
67
- * Filtered by index.
68
- *
69
- * @param direction
70
- * @param query
71
- * @returns
72
- * @private
73
- */
74
- getEndEntryFromIndex(query: IDBKeyRange, direction: IDBCursorDirection): Promise<QueueStoreEntry | undefined>;
75
- /**
76
- * Returns an open connection to the database.
77
- *
78
- * @private
79
- */
80
- private getDb;
81
- /**
82
- * Upgrades QueueDB
83
- *
84
- * @param db
85
- * @param oldVersion
86
- * @private
87
- */
88
- private _upgradeDb;
89
- }
90
- //# sourceMappingURL=QueueDb.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"QueueDb.d.ts","sourceRoot":"","sources":["../src/QueueDb.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAexD,MAAM,WAAW,2BAA2B;IAC1C,WAAW,EAAE,WAAW,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,eAAgB,SAAQ,2BAA2B;IAClE,EAAE,EAAE,MAAM,CAAC;CACZ;AAED;;;;;;GAMG;AAEH,qBAAa,OAAO;IAClB,OAAO,CAAC,GAAG,CAA4C;IAEvD;;;;OAIG;IACG,QAAQ,CAAC,KAAK,EAAE,2BAA2B,GAAG,OAAO,CAAC,IAAI,CAAC;IASjE;;;;OAIG;IACG,eAAe,IAAI,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAMpD;;;;;OAKG;IACG,wBAAwB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAM7E;;;;;OAKG;IACG,wBAAwB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAKlE;;;;OAIG;IACG,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAK5C;;;;OAIG;IACG,wBAAwB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,SAAS,CAAC;IAIvF;;;;OAIG;IACG,uBAAuB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,SAAS,CAAC;IAItF;;;;;;;;OAQG;IACG,oBAAoB,CAAC,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,kBAAkB,GAAG,OAAO,CAAC,eAAe,GAAG,SAAS,CAAC;IAOnH;;;;OAIG;YACW,KAAK;IASnB;;;;;;OAMG;IACH,OAAO,CAAC,UAAU;CAanB"}
@@ -1,75 +0,0 @@
1
- import type { QueueStoreEntry, UnidentifiedQueueStoreEntry } from "./QueueDb.js";
2
- /**
3
- * A class to manage storing requests from a Queue in IndexedDB,
4
- * indexed by their queue name for easier access.
5
- *
6
- * Most developers will not need to access this class directly;
7
- * it is exposed for advanced use cases.
8
- */
9
- export declare class QueueStore {
10
- private readonly _queueName;
11
- private readonly _queueDb;
12
- /**
13
- * Associates this instance with a Queue instance, so entries added can be
14
- * identified by their queue name.
15
- *
16
- * @param queueName
17
- */
18
- constructor(queueName: string);
19
- /**
20
- * Append an entry last in the queue.
21
- *
22
- * @param entry
23
- */
24
- pushEntry(entry: UnidentifiedQueueStoreEntry): Promise<void>;
25
- /**
26
- * Prepend an entry first in the queue.
27
- *
28
- * @param entry
29
- */
30
- unshiftEntry(entry: UnidentifiedQueueStoreEntry): Promise<void>;
31
- /**
32
- * Removes and returns the last entry in the queue matching the `queueName`.
33
- *
34
- * @returns
35
- */
36
- popEntry(): Promise<QueueStoreEntry | undefined>;
37
- /**
38
- * Removes and returns the first entry in the queue matching the `queueName`.
39
- *
40
- * @returns
41
- */
42
- shiftEntry(): Promise<QueueStoreEntry | undefined>;
43
- /**
44
- * Returns all entries in the store matching the `queueName`.
45
- *
46
- * @returns
47
- */
48
- getAll(): Promise<QueueStoreEntry[]>;
49
- /**
50
- * Returns the number of entries in the store matching the `queueName`.
51
- *
52
- * @returns
53
- */
54
- size(): Promise<number>;
55
- /**
56
- * Deletes the entry for the given ID.
57
- *
58
- * WARNING: this method does not ensure the deleted entry belongs to this
59
- * queue (i.e. matches the `queueName`). But this limitation is acceptable
60
- * as this class is not publicly exposed. An additional check would make
61
- * this method slower than it needs to be.
62
- *
63
- * @param id
64
- */
65
- deleteEntry(id: number): Promise<void>;
66
- /**
67
- * Removes and returns the first or last entry in the queue (based on the
68
- * `direction` argument) matching the `queueName`.
69
- *
70
- * @returns
71
- * @private
72
- */
73
- _removeEntry(entry?: QueueStoreEntry): Promise<QueueStoreEntry | undefined>;
74
- }
75
- //# sourceMappingURL=QueueStore.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"QueueStore.d.ts","sourceRoot":"","sources":["../src/QueueStore.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,eAAe,EAAE,2BAA2B,EAAE,MAAM,cAAc,CAAC;AAGjF;;;;;;GAMG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAU;IAEnC;;;;;OAKG;gBACS,SAAS,EAAE,MAAM;IAK7B;;;;OAIG;IACG,SAAS,CAAC,KAAK,EAAE,2BAA2B,GAAG,OAAO,CAAC,IAAI,CAAC;IAuBlE;;;;OAIG;IACG,YAAY,CAAC,KAAK,EAAE,2BAA2B,GAAG,OAAO,CAAC,IAAI,CAAC;IA8BrE;;;;OAIG;IACG,QAAQ,IAAI,OAAO,CAAC,eAAe,GAAG,SAAS,CAAC;IAItD;;;;OAIG;IACG,UAAU,IAAI,OAAO,CAAC,eAAe,GAAG,SAAS,CAAC;IAIxD;;;;OAIG;IACG,MAAM,IAAI,OAAO,CAAC,eAAe,EAAE,CAAC;IAI1C;;;;OAIG;IACG,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC;IAI7B;;;;;;;;;OASG;IACG,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI5C;;;;;;OAMG;IACG,YAAY,CAAC,KAAK,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,eAAe,GAAG,SAAS,CAAC;CAMlF"}
@@ -1,51 +0,0 @@
1
- import type { MapLikeObject } from "@serwist/core";
2
- export interface RequestData extends MapLikeObject {
3
- url: string;
4
- headers: MapLikeObject;
5
- body?: ArrayBuffer;
6
- }
7
- /**
8
- * A class to make it easier to serialize and de-serialize requests so they
9
- * can be stored in IndexedDB.
10
- *
11
- * Most developers will not need to access this class directly;
12
- * it is exposed for advanced use cases.
13
- */
14
- export declare class StorableRequest {
15
- private readonly _requestData;
16
- /**
17
- * Converts a Request object to a plain object that can be structured
18
- * cloned or JSON-stringified.
19
- *
20
- * @param request
21
- * @returns
22
- */
23
- static fromRequest(request: Request): Promise<StorableRequest>;
24
- /**
25
- * Accepts an object of request data that can be used to construct a
26
- * `Request` but can also be stored in IndexedDB.
27
- *
28
- * @param requestData An object of request data that includes the `url` plus any relevant properties of
29
- * [requestInit](https://fetch.spec.whatwg.org/#requestinit).
30
- */
31
- constructor(requestData: RequestData);
32
- /**
33
- * Returns a deep clone of the instances `_requestData` object.
34
- *
35
- * @returns
36
- */
37
- toObject(): RequestData;
38
- /**
39
- * Converts this instance to a Request.
40
- *
41
- * @returns
42
- */
43
- toRequest(): Request;
44
- /**
45
- * Creates and returns a deep clone of the instance.
46
- *
47
- * @returns
48
- */
49
- clone(): StorableRequest;
50
- }
51
- //# sourceMappingURL=StorableRequest.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"StorableRequest.d.ts","sourceRoot":"","sources":["../src/StorableRequest.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAiBnD,MAAM,WAAW,WAAY,SAAQ,aAAa;IAChD,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,aAAa,CAAC;IACvB,IAAI,CAAC,EAAE,WAAW,CAAC;CACpB;AAED;;;;;;GAMG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAc;IAE3C;;;;;;OAMG;WACU,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,eAAe,CAAC;IA6BpE;;;;;;OAMG;gBACS,WAAW,EAAE,WAAW;IAyBpC;;;;OAIG;IACH,QAAQ,IAAI,WAAW;IAUvB;;;;OAIG;IACH,SAAS,IAAI,OAAO;IAIpB;;;;OAIG;IACH,KAAK,IAAI,eAAe;CAGzB"}
@@ -1,39 +0,0 @@
1
- /*
2
- Copyright 2018 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import type { FetchDidFailCallbackParam, SerwistPlugin } from "@serwist/core";
10
-
11
- import type { QueueOptions } from "./Queue.js";
12
- import { Queue } from "./Queue.js";
13
-
14
- /**
15
- * A class implementing the `fetchDidFail` lifecycle callback. This makes it
16
- * easier to add failed requests to a background sync Queue.
17
- */
18
- export class BackgroundSyncPlugin implements SerwistPlugin {
19
- private readonly _queue: Queue;
20
-
21
- /**
22
- * @param name See the `@serwist/background-sync.Queue`
23
- * documentation for parameter details.
24
- * @param options See the `@serwist/background-sync.Queue`
25
- * documentation for parameter details.
26
- * @see https://serwist.pages.dev/docs/background-sync/queue
27
- */
28
- constructor(name: string, options?: QueueOptions) {
29
- this._queue = new Queue(name, options);
30
- }
31
-
32
- /**
33
- * @param options
34
- * @private
35
- */
36
- async fetchDidFail({ request }: FetchDidFailCallbackParam) {
37
- await this._queue.pushRequest({ request });
38
- }
39
- }