@serwist/background-sync 8.0.0
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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/BackgroundSyncPlugin.d.ts +23 -0
- package/dist/BackgroundSyncPlugin.d.ts.map +1 -0
- package/dist/Queue.d.ts +170 -0
- package/dist/Queue.d.ts.map +1 -0
- package/dist/QueueDb.d.ts +90 -0
- package/dist/QueueDb.d.ts.map +1 -0
- package/dist/QueueStore.d.ts +75 -0
- package/dist/QueueStore.d.ts.map +1 -0
- package/dist/StorableRequest.d.ts +52 -0
- package/dist/StorableRequest.d.ts.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +707 -0
- package/dist/index.old.cjs +712 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Google LLC, 2019 ShadowWalker w@weiw.io https://weiw.io, 2023 Serwist
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This module's documentation can be found at https://developers.google.com/web/tools/workbox/modules/workbox-background-sync
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { SerwistPlugin } from "@serwist/core";
|
|
2
|
+
import type { QueueOptions } from "./Queue.js";
|
|
3
|
+
/**
|
|
4
|
+
* A class implementing the `fetchDidFail` lifecycle callback. This makes it
|
|
5
|
+
* easier to add failed requests to a background sync Queue.
|
|
6
|
+
*/
|
|
7
|
+
declare class BackgroundSyncPlugin implements SerwistPlugin {
|
|
8
|
+
private readonly _queue;
|
|
9
|
+
/**
|
|
10
|
+
* @param name See the `@serwist/background-sync.Queue`
|
|
11
|
+
* documentation for parameter details.
|
|
12
|
+
* @param options See the `@serwist/background-sync.Queue`
|
|
13
|
+
* documentation for parameter details.
|
|
14
|
+
*/
|
|
15
|
+
constructor(name: string, options?: QueueOptions);
|
|
16
|
+
/**
|
|
17
|
+
* @param options
|
|
18
|
+
* @private
|
|
19
|
+
*/
|
|
20
|
+
fetchDidFail: SerwistPlugin["fetchDidFail"];
|
|
21
|
+
}
|
|
22
|
+
export { BackgroundSyncPlugin };
|
|
23
|
+
//# sourceMappingURL=BackgroundSyncPlugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BackgroundSyncPlugin.d.ts","sourceRoot":"","sources":["../src/BackgroundSyncPlugin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAG/C;;;GAGG;AACH,cAAM,oBAAqB,YAAW,aAAa;IACjD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;IAE/B;;;;;OAKG;gBACS,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;IAIhD;;;OAGG;IACH,YAAY,EAAE,aAAa,CAAC,cAAc,CAAC,CAEzC;CACH;AAED,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
|
package/dist/Queue.d.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
interface OnSyncCallbackOptions {
|
|
2
|
+
queue: Queue;
|
|
3
|
+
}
|
|
4
|
+
interface OnSyncCallback {
|
|
5
|
+
(options: OnSyncCallbackOptions): void | Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export interface QueueOptions {
|
|
8
|
+
/**
|
|
9
|
+
* If `true`, instead of attempting to use background sync events, always attempt
|
|
10
|
+
* to replay queued request at service worker startup. Most folks will not need
|
|
11
|
+
* this, unless you explicitly target a runtime like Electron that exposes the
|
|
12
|
+
* interfaces for background sync, but does not have a working implementation.
|
|
13
|
+
*
|
|
14
|
+
* @default false
|
|
15
|
+
*/
|
|
16
|
+
forceSyncFallback?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* The amount of time (in minutes) a request may be retried. After this amount
|
|
19
|
+
* of time has passed, the request will be deleted from the queue.
|
|
20
|
+
*
|
|
21
|
+
* @default 60 * 24 * 7
|
|
22
|
+
*/
|
|
23
|
+
maxRetentionTime?: number;
|
|
24
|
+
/**
|
|
25
|
+
* A function that gets invoked whenever the 'sync' event fires. The function
|
|
26
|
+
* is invoked with an object containing the `queue` property (referencing this
|
|
27
|
+
* instance), and you can use the callback to customize the replay behavior of
|
|
28
|
+
* the queue. When not set the `replayRequests()` method is called. Note: if the
|
|
29
|
+
* replay fails after a sync event, make sure you throw an error, so the browser
|
|
30
|
+
* knows to retry the sync event later.
|
|
31
|
+
*/
|
|
32
|
+
onSync?: OnSyncCallback;
|
|
33
|
+
}
|
|
34
|
+
interface QueueEntry {
|
|
35
|
+
/**
|
|
36
|
+
* The request to store in the queue.
|
|
37
|
+
*/
|
|
38
|
+
request: Request;
|
|
39
|
+
/**
|
|
40
|
+
* The timestamp (Epoch time in milliseconds) when the request was first added
|
|
41
|
+
* to the queue. This is used along with `maxRetentionTime` to remove outdated
|
|
42
|
+
* requests. In general you don't need to set this value, as it's automatically
|
|
43
|
+
* set for you (defaulting to `Date.now()`), but you can update it if you don't
|
|
44
|
+
* want particular requests to expire.
|
|
45
|
+
*/
|
|
46
|
+
timestamp?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Any metadata you want associated with the stored request. When requests are
|
|
49
|
+
* replayed you'll have access to this metadata object in case you need to modify
|
|
50
|
+
* the request beforehand.
|
|
51
|
+
*/
|
|
52
|
+
metadata?: Record<string, unknown>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* A class to manage storing failed requests in IndexedDB and retrying them
|
|
56
|
+
* later. All parts of the storing and replaying process are observable via
|
|
57
|
+
* callbacks.
|
|
58
|
+
*/
|
|
59
|
+
declare class Queue {
|
|
60
|
+
private readonly _name;
|
|
61
|
+
private readonly _onSync;
|
|
62
|
+
private readonly _maxRetentionTime;
|
|
63
|
+
private readonly _queueStore;
|
|
64
|
+
private readonly _forceSyncFallback;
|
|
65
|
+
private _syncInProgress;
|
|
66
|
+
private _requestsAddedDuringSync;
|
|
67
|
+
/**
|
|
68
|
+
* Creates an instance of Queue with the given options
|
|
69
|
+
*
|
|
70
|
+
* @param name The unique name for this queue. This name must be
|
|
71
|
+
* unique as it's used to register sync events and store requests
|
|
72
|
+
* in IndexedDB specific to this instance. An error will be thrown if
|
|
73
|
+
* a duplicate name is detected.
|
|
74
|
+
* @param options
|
|
75
|
+
*/
|
|
76
|
+
constructor(name: string, { forceSyncFallback, onSync, maxRetentionTime }?: QueueOptions);
|
|
77
|
+
/**
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
get name(): string;
|
|
81
|
+
/**
|
|
82
|
+
* Stores the passed request in IndexedDB (with its timestamp and any
|
|
83
|
+
* metadata) at the end of the queue.
|
|
84
|
+
*
|
|
85
|
+
* @param entry
|
|
86
|
+
*/
|
|
87
|
+
pushRequest(entry: QueueEntry): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Stores the passed request in IndexedDB (with its timestamp and any
|
|
90
|
+
* metadata) at the beginning of the queue.
|
|
91
|
+
*
|
|
92
|
+
* @param entry
|
|
93
|
+
*/
|
|
94
|
+
unshiftRequest(entry: QueueEntry): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Removes and returns the last request in the queue (along with its
|
|
97
|
+
* timestamp and any metadata). The returned object takes the form:
|
|
98
|
+
* `{request, timestamp, metadata}`.
|
|
99
|
+
*
|
|
100
|
+
* @returns
|
|
101
|
+
*/
|
|
102
|
+
popRequest(): Promise<QueueEntry | undefined>;
|
|
103
|
+
/**
|
|
104
|
+
* Removes and returns the first request in the queue (along with its
|
|
105
|
+
* timestamp and any metadata). The returned object takes the form:
|
|
106
|
+
* `{request, timestamp, metadata}`.
|
|
107
|
+
*
|
|
108
|
+
* @returns
|
|
109
|
+
*/
|
|
110
|
+
shiftRequest(): Promise<QueueEntry | undefined>;
|
|
111
|
+
/**
|
|
112
|
+
* Returns all the entries that have not expired (per `maxRetentionTime`).
|
|
113
|
+
* Any expired entries are removed from the queue.
|
|
114
|
+
*
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
getAll(): Promise<Array<QueueEntry>>;
|
|
118
|
+
/**
|
|
119
|
+
* Returns the number of entries present in the queue.
|
|
120
|
+
* Note that expired entries (per `maxRetentionTime`) are also included in this count.
|
|
121
|
+
*
|
|
122
|
+
* @returns
|
|
123
|
+
*/
|
|
124
|
+
size(): Promise<number>;
|
|
125
|
+
/**
|
|
126
|
+
* Adds the entry to the QueueStore and registers for a sync event.
|
|
127
|
+
*
|
|
128
|
+
* @param entry
|
|
129
|
+
* @param operation
|
|
130
|
+
* @private
|
|
131
|
+
*/
|
|
132
|
+
_addRequest({ request, metadata, timestamp }: QueueEntry, operation: "push" | "unshift"): Promise<void>;
|
|
133
|
+
/**
|
|
134
|
+
* Removes and returns the first or last (depending on `operation`) entry
|
|
135
|
+
* from the QueueStore that's not older than the `maxRetentionTime`.
|
|
136
|
+
*
|
|
137
|
+
* @param operation
|
|
138
|
+
* @returns
|
|
139
|
+
* @private
|
|
140
|
+
*/
|
|
141
|
+
_removeRequest(operation: "pop" | "shift"): Promise<QueueEntry | undefined>;
|
|
142
|
+
/**
|
|
143
|
+
* Loops through each request in the queue and attempts to re-fetch it.
|
|
144
|
+
* If any request fails to re-fetch, it's put back in the same position in
|
|
145
|
+
* the queue (which registers a retry for the next sync event).
|
|
146
|
+
*/
|
|
147
|
+
replayRequests(): Promise<void>;
|
|
148
|
+
/**
|
|
149
|
+
* Registers a sync event with a tag unique to this instance.
|
|
150
|
+
*/
|
|
151
|
+
registerSync(): Promise<void>;
|
|
152
|
+
/**
|
|
153
|
+
* In sync-supporting browsers, this adds a listener for the sync event.
|
|
154
|
+
* In non-sync-supporting browsers, or if _forceSyncFallback is true, this
|
|
155
|
+
* will retry the queue on service worker startup.
|
|
156
|
+
*
|
|
157
|
+
* @private
|
|
158
|
+
*/
|
|
159
|
+
private _addSyncListener;
|
|
160
|
+
/**
|
|
161
|
+
* Returns the set of queue names. This is primarily used to reset the list
|
|
162
|
+
* of queue names in tests.
|
|
163
|
+
*
|
|
164
|
+
* @returns
|
|
165
|
+
* @private
|
|
166
|
+
*/
|
|
167
|
+
static get _queueNames(): Set<string>;
|
|
168
|
+
}
|
|
169
|
+
export { Queue };
|
|
170
|
+
//# sourceMappingURL=Queue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Queue.d.ts","sourceRoot":"","sources":["../src/Queue.ts"],"names":[],"mappings":"AAgBA,UAAU,qBAAqB;IAC7B,KAAK,EAAE,KAAK,CAAC;CACd;AAED,UAAU,cAAc;IACtB,CAAC,OAAO,EAAE,qBAAqB,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACxD;AAED,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,UAAU,UAAU;IAClB;;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,CAAC,CAAC;CACpC;AA2BD;;;;GAIG;AACH,cAAM,KAAK;IACT,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAS;IAC3C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAa;IACzC,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAU;IAC7C,OAAO,CAAC,eAAe,CAAS;IAChC,OAAO,CAAC,wBAAwB,CAAS;IAEzC;;;;;;;;OAQG;gBACS,IAAI,EAAE,MAAM,EAAE,EAAE,iBAAiB,EAAE,MAAM,EAAE,gBAAgB,EAAE,GAAE,YAAiB;IAiB5F;;OAEG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;;;;OAKG;IACG,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAmBnD;;;;;OAKG;IACG,cAAc,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAmBtD;;;;;;OAMG;IACG,UAAU,IAAI,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAInD;;;;;;OAMG;IACG,YAAY,IAAI,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IAIrD;;;;;OAKG;IACG,MAAM,IAAI,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IAmB1C;;;;;OAKG;IACG,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC;IAI7B;;;;;;OAMG;IACG,WAAW,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAsB,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;IAmC1H;;;;;;;OAOG;IACG,cAAc,CAAC,SAAS,EAAE,KAAK,GAAG,OAAO,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IA0BjF;;;;OAIG;IACG,cAAc,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;AAED,OAAO,EAAE,KAAK,EAAE,CAAC"}
|
|
@@ -0,0 +1,90 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
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
|
+
export { StorableRequest };
|
|
52
|
+
//# sourceMappingURL=StorableRequest.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,cAAM,eAAe;IACnB,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;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BackgroundSyncPlugin } from "./BackgroundSyncPlugin.js";
|
|
2
|
+
import type { QueueOptions } from "./Queue.js";
|
|
3
|
+
import { Queue } from "./Queue.js";
|
|
4
|
+
import { QueueStore } from "./QueueStore.js";
|
|
5
|
+
import { StorableRequest } from "./StorableRequest.js";
|
|
6
|
+
interface SyncManager {
|
|
7
|
+
getTags(): Promise<string[]>;
|
|
8
|
+
register(tag: string): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
declare global {
|
|
11
|
+
interface ServiceWorkerRegistration {
|
|
12
|
+
readonly sync: SyncManager;
|
|
13
|
+
}
|
|
14
|
+
interface SyncEvent extends ExtendableEvent {
|
|
15
|
+
readonly lastChance: boolean;
|
|
16
|
+
readonly tag: string;
|
|
17
|
+
}
|
|
18
|
+
interface ServiceWorkerGlobalScopeEventMap {
|
|
19
|
+
sync: SyncEvent;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export { BackgroundSyncPlugin, Queue, QueueStore, StorableRequest };
|
|
23
|
+
export type { QueueOptions };
|
|
24
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAGvD,UAAU,WAAW;IACnB,OAAO,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAC7B,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACtC;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,yBAAyB;QACjC,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;KAC5B;IAED,UAAU,SAAU,SAAQ,eAAe;QACzC,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;QAC7B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;KACtB;IAED,UAAU,gCAAgC;QACxC,IAAI,EAAE,SAAS,CAAC;KACjB;CACF;AAED,OAAO,EAAE,oBAAoB,EAAE,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,CAAC;AAEpE,YAAY,EAAE,YAAY,EAAE,CAAC"}
|