@serwist/broadcast-update 9.0.0-preview.7 → 9.0.0-preview.8
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/BroadcastCacheUpdate.d.ts +2 -26
- package/dist/BroadcastCacheUpdate.d.ts.map +1 -1
- package/dist/BroadcastUpdatePlugin.d.ts +4 -5
- package/dist/BroadcastUpdatePlugin.d.ts.map +1 -1
- package/dist/constants.d.ts +5 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/index.d.ts +3 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -18
- package/dist/types.d.ts +36 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +4 -4
- package/src/BroadcastCacheUpdate.ts +9 -36
- package/src/BroadcastUpdatePlugin.ts +6 -9
- package/src/{utils/constants.ts → constants.ts} +2 -2
- package/src/index.ts +10 -7
- package/src/types.ts +39 -0
- package/dist/utils/constants.d.ts +0 -5
- package/dist/utils/constants.d.ts.map +0 -1
|
@@ -1,28 +1,5 @@
|
|
|
1
1
|
import type { CacheDidUpdateCallbackParam } from "@serwist/core";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A list of headers that will be used to determine whether the responses
|
|
5
|
-
* differ.
|
|
6
|
-
*
|
|
7
|
-
* @default ['content-length', 'etag', 'last-modified']
|
|
8
|
-
*/
|
|
9
|
-
headersToCheck?: string[];
|
|
10
|
-
/**
|
|
11
|
-
* A function whose return value
|
|
12
|
-
* will be used as the `payload` field in any cache update messages sent
|
|
13
|
-
* to the window clients.
|
|
14
|
-
* @param options
|
|
15
|
-
* @returns
|
|
16
|
-
*/
|
|
17
|
-
generatePayload?: (options: CacheDidUpdateCallbackParam) => Record<string, any>;
|
|
18
|
-
/**
|
|
19
|
-
* If true (the default) then all open clients will receive a message. If false,
|
|
20
|
-
* then only the client that make the original request will be notified of the update.
|
|
21
|
-
*
|
|
22
|
-
* @default true
|
|
23
|
-
*/
|
|
24
|
-
notifyAllClients?: boolean;
|
|
25
|
-
}
|
|
2
|
+
import type { BroadcastCacheUpdateOptions } from "./types.js";
|
|
26
3
|
/**
|
|
27
4
|
* Uses the `postMessage()` API to inform any open windows/tabs when a cached
|
|
28
5
|
* response has been updated.
|
|
@@ -30,7 +7,7 @@ export interface BroadcastCacheUpdateOptions {
|
|
|
30
7
|
* For efficiency's sake, the underlying response bodies are not compared;
|
|
31
8
|
* only specific response headers are checked.
|
|
32
9
|
*/
|
|
33
|
-
declare class BroadcastCacheUpdate {
|
|
10
|
+
export declare class BroadcastCacheUpdate {
|
|
34
11
|
private readonly _headersToCheck;
|
|
35
12
|
private readonly _generatePayload;
|
|
36
13
|
private readonly _notifyAllClients;
|
|
@@ -67,5 +44,4 @@ declare class BroadcastCacheUpdate {
|
|
|
67
44
|
*/
|
|
68
45
|
notifyIfUpdated(options: CacheDidUpdateCallbackParam): Promise<void>;
|
|
69
46
|
}
|
|
70
|
-
export { BroadcastCacheUpdate };
|
|
71
47
|
//# sourceMappingURL=BroadcastCacheUpdate.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BroadcastCacheUpdate.d.ts","sourceRoot":"","sources":["../src/BroadcastCacheUpdate.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"BroadcastCacheUpdate.d.ts","sourceRoot":"","sources":["../src/BroadcastCacheUpdate.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;AAKjE,OAAO,KAAK,EAAE,2BAA2B,EAAiE,MAAM,YAAY,CAAC;AAuB7H;;;;;;GAMG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAW;IAC3C,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA4B;IAC7D,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAU;IAE5C;;;;;OAKG;gBACS,EAAE,eAAe,EAAE,cAAc,EAAE,gBAAgB,EAAE,GAAE,2BAAgC;IAMnG;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACG,eAAe,CAAC,OAAO,EAAE,2BAA2B,GAAG,OAAO,CAAC,IAAI,CAAC;CA4E3E"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { SerwistPlugin } from "@serwist/core";
|
|
2
|
-
import type { BroadcastCacheUpdateOptions } from "./
|
|
1
|
+
import type { CacheDidUpdateCallbackParam, SerwistPlugin } from "@serwist/core";
|
|
2
|
+
import type { BroadcastCacheUpdateOptions } from "./types.js";
|
|
3
3
|
/**
|
|
4
4
|
* This plugin will automatically broadcast a message whenever a cached response
|
|
5
5
|
* is updated.
|
|
6
6
|
*/
|
|
7
|
-
declare class BroadcastUpdatePlugin implements SerwistPlugin {
|
|
7
|
+
export declare class BroadcastUpdatePlugin implements SerwistPlugin {
|
|
8
8
|
private readonly _broadcastUpdate;
|
|
9
9
|
/**
|
|
10
10
|
* Construct a `@serwist/broadcast-update.BroadcastCacheUpdate` instance with
|
|
@@ -22,7 +22,6 @@ declare class BroadcastUpdatePlugin implements SerwistPlugin {
|
|
|
22
22
|
* @private
|
|
23
23
|
* @param options The input object to this function.
|
|
24
24
|
*/
|
|
25
|
-
cacheDidUpdate:
|
|
25
|
+
cacheDidUpdate(options: CacheDidUpdateCallbackParam): void;
|
|
26
26
|
}
|
|
27
|
-
export { BroadcastUpdatePlugin };
|
|
28
27
|
//# sourceMappingURL=BroadcastUpdatePlugin.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BroadcastUpdatePlugin.d.ts","sourceRoot":"","sources":["../src/BroadcastUpdatePlugin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"BroadcastUpdatePlugin.d.ts","sourceRoot":"","sources":["../src/BroadcastUpdatePlugin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,2BAA2B,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAGhF,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAC;AAE9D;;;GAGG;AACH,qBAAa,qBAAsB,YAAW,aAAa;IACzD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAuB;IAExD;;;;;;OAMG;gBACS,OAAO,CAAC,EAAE,2BAA2B;IAIjD;;;;;;;OAOG;IACH,cAAc,CAAC,OAAO,EAAE,2BAA2B;CAGpD"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const CACHE_UPDATED_MESSAGE_TYPE = "CACHE_UPDATED";
|
|
2
|
+
export declare const CACHE_UPDATED_MESSAGE_META = "serwist-broadcast-update";
|
|
3
|
+
export declare const defaultNotifyAllClients = true;
|
|
4
|
+
export declare const defaultHeadersToCheck: string[];
|
|
5
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,0BAA0B,kBAAkB,CAAC;AAC1D,eAAO,MAAM,0BAA0B,6BAA6B,CAAC;AACrE,eAAO,MAAM,uBAAuB,OAAO,CAAC;AAC5C,eAAO,MAAM,qBAAqB,UAA8C,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import type { BroadcastCacheUpdateOptions } from "./BroadcastCacheUpdate.js";
|
|
2
1
|
import { BroadcastCacheUpdate } from "./BroadcastCacheUpdate.js";
|
|
3
2
|
import { BroadcastUpdatePlugin } from "./BroadcastUpdatePlugin.js";
|
|
3
|
+
import { CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE, defaultHeadersToCheck } from "./constants.js";
|
|
4
4
|
import { responsesAreSame } from "./responsesAreSame.js";
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
*/
|
|
8
|
-
export { BroadcastCacheUpdate, BroadcastUpdatePlugin, responsesAreSame };
|
|
9
|
-
export type { BroadcastCacheUpdateOptions };
|
|
5
|
+
export { BroadcastCacheUpdate, BroadcastUpdatePlugin, responsesAreSame, CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE, defaultHeadersToCheck, };
|
|
6
|
+
export type * from "./types.js";
|
|
10
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,EAAE,0BAA0B,EAAE,0BAA0B,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAC/G,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,gBAAgB,EAChB,0BAA0B,EAC1B,0BAA0B,EAC1B,qBAAqB,GACtB,CAAC;AAEF,mBAAmB,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
import { SerwistError, logger, assert, resultingClientExists, timeout
|
|
1
|
+
import { SerwistError, logger, assert, resultingClientExists, timeout } from '@serwist/core/internal';
|
|
2
|
+
|
|
3
|
+
const CACHE_UPDATED_MESSAGE_TYPE = "CACHE_UPDATED";
|
|
4
|
+
const CACHE_UPDATED_MESSAGE_META = "serwist-broadcast-update";
|
|
5
|
+
const defaultNotifyAllClients = true;
|
|
6
|
+
const defaultHeadersToCheck = [
|
|
7
|
+
"content-length",
|
|
8
|
+
"etag",
|
|
9
|
+
"last-modified"
|
|
10
|
+
];
|
|
2
11
|
|
|
3
12
|
const responsesAreSame = (firstResponse, secondResponse, headersToCheck)=>{
|
|
4
13
|
if (process.env.NODE_ENV !== "production") {
|
|
@@ -23,30 +32,21 @@ const responsesAreSame = (firstResponse, secondResponse, headersToCheck)=>{
|
|
|
23
32
|
});
|
|
24
33
|
};
|
|
25
34
|
|
|
26
|
-
const CACHE_UPDATED_MESSAGE_TYPE = "CACHE_UPDATED";
|
|
27
|
-
const CACHE_UPDATED_MESSAGE_META = "serwist-broadcast-update";
|
|
28
|
-
const NOTIFY_ALL_CLIENTS = true;
|
|
29
|
-
const DEFAULT_HEADERS_TO_CHECK = [
|
|
30
|
-
"content-length",
|
|
31
|
-
"etag",
|
|
32
|
-
"last-modified"
|
|
33
|
-
];
|
|
34
|
-
|
|
35
35
|
const isSafari = typeof navigator !== "undefined" && /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
|
36
|
-
|
|
36
|
+
const defaultPayloadGenerator = (data)=>{
|
|
37
37
|
return {
|
|
38
38
|
cacheName: data.cacheName,
|
|
39
39
|
updatedURL: data.request.url
|
|
40
40
|
};
|
|
41
|
-
}
|
|
41
|
+
};
|
|
42
42
|
class BroadcastCacheUpdate {
|
|
43
43
|
_headersToCheck;
|
|
44
44
|
_generatePayload;
|
|
45
45
|
_notifyAllClients;
|
|
46
46
|
constructor({ generatePayload, headersToCheck, notifyAllClients } = {}){
|
|
47
|
-
this._headersToCheck = headersToCheck ||
|
|
47
|
+
this._headersToCheck = headersToCheck || defaultHeadersToCheck;
|
|
48
48
|
this._generatePayload = generatePayload || defaultPayloadGenerator;
|
|
49
|
-
this._notifyAllClients = notifyAllClients ??
|
|
49
|
+
this._notifyAllClients = notifyAllClients ?? defaultNotifyAllClients;
|
|
50
50
|
}
|
|
51
51
|
async notifyIfUpdated(options) {
|
|
52
52
|
if (process.env.NODE_ENV !== "production") {
|
|
@@ -113,9 +113,9 @@ class BroadcastUpdatePlugin {
|
|
|
113
113
|
constructor(options){
|
|
114
114
|
this._broadcastUpdate = new BroadcastCacheUpdate(options);
|
|
115
115
|
}
|
|
116
|
-
cacheDidUpdate
|
|
117
|
-
|
|
118
|
-
}
|
|
116
|
+
cacheDidUpdate(options) {
|
|
117
|
+
void this._broadcastUpdate.notifyIfUpdated(options);
|
|
118
|
+
}
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
export { BroadcastCacheUpdate, BroadcastUpdatePlugin, responsesAreSame };
|
|
121
|
+
export { BroadcastCacheUpdate, BroadcastUpdatePlugin, CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE, defaultHeadersToCheck, responsesAreSame };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { CacheDidUpdateCallbackParam } from "@serwist/core";
|
|
2
|
+
import type { CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE } from "./constants.js";
|
|
3
|
+
export interface BroadcastCacheUpdateOptions {
|
|
4
|
+
/**
|
|
5
|
+
* A list of headers that will be used to determine whether the responses
|
|
6
|
+
* differ.
|
|
7
|
+
*
|
|
8
|
+
* @default ['content-length', 'etag', 'last-modified']
|
|
9
|
+
*/
|
|
10
|
+
headersToCheck?: string[];
|
|
11
|
+
/**
|
|
12
|
+
* A function whose return value
|
|
13
|
+
* will be used as the `payload` field in any cache update messages sent
|
|
14
|
+
* to the window clients.
|
|
15
|
+
* @param options
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
generatePayload?: (options: CacheDidUpdateCallbackParam) => Record<string, any>;
|
|
19
|
+
/**
|
|
20
|
+
* If true (the default) then all open clients will receive a message. If false,
|
|
21
|
+
* then only the client that make the original request will be notified of the update.
|
|
22
|
+
*
|
|
23
|
+
* @default true
|
|
24
|
+
*/
|
|
25
|
+
notifyAllClients?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export type BroadcastPayload = Record<string, any>;
|
|
28
|
+
export interface BroadcastPayloadGenerator {
|
|
29
|
+
(options: CacheDidUpdateCallbackParam): BroadcastPayload;
|
|
30
|
+
}
|
|
31
|
+
export interface BroadcastMessage {
|
|
32
|
+
type: typeof CACHE_UPDATED_MESSAGE_TYPE;
|
|
33
|
+
meta: typeof CACHE_UPDATED_MESSAGE_META;
|
|
34
|
+
payload: BroadcastPayload;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,KAAK,EAAE,0BAA0B,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAC;AAE7F,MAAM,WAAW,2BAA2B;IAC1C;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,2BAA2B,KAAK,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAChF;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B;AAED,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAEnD,MAAM,WAAW,yBAAyB;IACxC,CAAC,OAAO,EAAE,2BAA2B,GAAG,gBAAgB,CAAC;CAC1D;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,OAAO,0BAA0B,CAAC;IACxC,IAAI,EAAE,OAAO,0BAA0B,CAAC;IACxC,OAAO,EAAE,gBAAgB,CAAC;CAC3B"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/broadcast-update",
|
|
3
|
-
"version": "9.0.0-preview.
|
|
3
|
+
"version": "9.0.0-preview.8",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "A
|
|
5
|
+
"description": "A module that uses the Broadcast Channel API to announce when a cached response has updated",
|
|
6
6
|
"files": [
|
|
7
7
|
"src",
|
|
8
8
|
"dist"
|
|
@@ -29,12 +29,12 @@
|
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@serwist/core": "9.0.0-preview.
|
|
32
|
+
"@serwist/core": "9.0.0-preview.8"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"rollup": "4.9.6",
|
|
36
36
|
"typescript": "5.4.0-dev.20240206",
|
|
37
|
-
"@serwist/constants": "9.0.0-preview.
|
|
37
|
+
"@serwist/constants": "9.0.0-preview.8"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"typescript": ">=5.0.0"
|
|
@@ -9,8 +9,9 @@
|
|
|
9
9
|
import type { CacheDidUpdateCallbackParam } from "@serwist/core";
|
|
10
10
|
import { assert, logger, resultingClientExists, timeout } from "@serwist/core/internal";
|
|
11
11
|
|
|
12
|
+
import { CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE, defaultHeadersToCheck, defaultNotifyAllClients } from "./constants.js";
|
|
12
13
|
import { responsesAreSame } from "./responsesAreSame.js";
|
|
13
|
-
import {
|
|
14
|
+
import type { BroadcastCacheUpdateOptions, BroadcastMessage, BroadcastPayload, BroadcastPayloadGenerator } from "./types.js";
|
|
14
15
|
|
|
15
16
|
// UA-sniff Safari: https://stackoverflow.com/questions/7944460/detect-safari-browser
|
|
16
17
|
// TODO(philipwalton): remove once this Safari bug fix has been released.
|
|
@@ -19,32 +20,6 @@ const isSafari = typeof navigator !== "undefined" && /^((?!chrome|android).)*saf
|
|
|
19
20
|
|
|
20
21
|
// Give TypeScript the correct global.
|
|
21
22
|
declare let self: ServiceWorkerGlobalScope;
|
|
22
|
-
|
|
23
|
-
export interface BroadcastCacheUpdateOptions {
|
|
24
|
-
/**
|
|
25
|
-
* A list of headers that will be used to determine whether the responses
|
|
26
|
-
* differ.
|
|
27
|
-
*
|
|
28
|
-
* @default ['content-length', 'etag', 'last-modified']
|
|
29
|
-
*/
|
|
30
|
-
headersToCheck?: string[];
|
|
31
|
-
/**
|
|
32
|
-
* A function whose return value
|
|
33
|
-
* will be used as the `payload` field in any cache update messages sent
|
|
34
|
-
* to the window clients.
|
|
35
|
-
* @param options
|
|
36
|
-
* @returns
|
|
37
|
-
*/
|
|
38
|
-
generatePayload?: (options: CacheDidUpdateCallbackParam) => Record<string, any>;
|
|
39
|
-
/**
|
|
40
|
-
* If true (the default) then all open clients will receive a message. If false,
|
|
41
|
-
* then only the client that make the original request will be notified of the update.
|
|
42
|
-
*
|
|
43
|
-
* @default true
|
|
44
|
-
*/
|
|
45
|
-
notifyAllClients?: boolean;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
23
|
/**
|
|
49
24
|
* Generates the default payload used in update messages. By default the
|
|
50
25
|
* payload includes the `cacheName` and `updatedURL` fields.
|
|
@@ -52,12 +27,12 @@ export interface BroadcastCacheUpdateOptions {
|
|
|
52
27
|
* @returns
|
|
53
28
|
* @private
|
|
54
29
|
*/
|
|
55
|
-
|
|
30
|
+
const defaultPayloadGenerator = (data: CacheDidUpdateCallbackParam): BroadcastPayload => {
|
|
56
31
|
return {
|
|
57
32
|
cacheName: data.cacheName,
|
|
58
33
|
updatedURL: data.request.url,
|
|
59
34
|
};
|
|
60
|
-
}
|
|
35
|
+
};
|
|
61
36
|
|
|
62
37
|
/**
|
|
63
38
|
* Uses the `postMessage()` API to inform any open windows/tabs when a cached
|
|
@@ -66,9 +41,9 @@ function defaultPayloadGenerator(data: CacheDidUpdateCallbackParam): Record<stri
|
|
|
66
41
|
* For efficiency's sake, the underlying response bodies are not compared;
|
|
67
42
|
* only specific response headers are checked.
|
|
68
43
|
*/
|
|
69
|
-
class BroadcastCacheUpdate {
|
|
44
|
+
export class BroadcastCacheUpdate {
|
|
70
45
|
private readonly _headersToCheck: string[];
|
|
71
|
-
private readonly _generatePayload:
|
|
46
|
+
private readonly _generatePayload: BroadcastPayloadGenerator;
|
|
72
47
|
private readonly _notifyAllClients: boolean;
|
|
73
48
|
|
|
74
49
|
/**
|
|
@@ -78,9 +53,9 @@ class BroadcastCacheUpdate {
|
|
|
78
53
|
* @param options
|
|
79
54
|
*/
|
|
80
55
|
constructor({ generatePayload, headersToCheck, notifyAllClients }: BroadcastCacheUpdateOptions = {}) {
|
|
81
|
-
this._headersToCheck = headersToCheck ||
|
|
56
|
+
this._headersToCheck = headersToCheck || defaultHeadersToCheck;
|
|
82
57
|
this._generatePayload = generatePayload || defaultPayloadGenerator;
|
|
83
|
-
this._notifyAllClients = notifyAllClients ??
|
|
58
|
+
this._notifyAllClients = notifyAllClients ?? defaultNotifyAllClients;
|
|
84
59
|
}
|
|
85
60
|
|
|
86
61
|
/**
|
|
@@ -143,7 +118,7 @@ class BroadcastCacheUpdate {
|
|
|
143
118
|
type: CACHE_UPDATED_MESSAGE_TYPE,
|
|
144
119
|
meta: CACHE_UPDATED_MESSAGE_META,
|
|
145
120
|
payload: this._generatePayload(options),
|
|
146
|
-
};
|
|
121
|
+
} satisfies BroadcastMessage;
|
|
147
122
|
|
|
148
123
|
// For navigation requests, wait until the new window client exists
|
|
149
124
|
// before sending the message
|
|
@@ -184,5 +159,3 @@ class BroadcastCacheUpdate {
|
|
|
184
159
|
}
|
|
185
160
|
}
|
|
186
161
|
}
|
|
187
|
-
|
|
188
|
-
export { BroadcastCacheUpdate };
|
|
@@ -6,17 +6,16 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type { SerwistPlugin } from "@serwist/core";
|
|
10
|
-
import { dontWaitFor } from "@serwist/core/internal";
|
|
9
|
+
import type { CacheDidUpdateCallbackParam, SerwistPlugin } from "@serwist/core";
|
|
11
10
|
|
|
12
|
-
import type { BroadcastCacheUpdateOptions } from "./BroadcastCacheUpdate.js";
|
|
13
11
|
import { BroadcastCacheUpdate } from "./BroadcastCacheUpdate.js";
|
|
12
|
+
import type { BroadcastCacheUpdateOptions } from "./types.js";
|
|
14
13
|
|
|
15
14
|
/**
|
|
16
15
|
* This plugin will automatically broadcast a message whenever a cached response
|
|
17
16
|
* is updated.
|
|
18
17
|
*/
|
|
19
|
-
class BroadcastUpdatePlugin implements SerwistPlugin {
|
|
18
|
+
export class BroadcastUpdatePlugin implements SerwistPlugin {
|
|
20
19
|
private readonly _broadcastUpdate: BroadcastCacheUpdate;
|
|
21
20
|
|
|
22
21
|
/**
|
|
@@ -38,9 +37,7 @@ class BroadcastUpdatePlugin implements SerwistPlugin {
|
|
|
38
37
|
* @private
|
|
39
38
|
* @param options The input object to this function.
|
|
40
39
|
*/
|
|
41
|
-
cacheDidUpdate:
|
|
42
|
-
|
|
43
|
-
}
|
|
40
|
+
cacheDidUpdate(options: CacheDidUpdateCallbackParam) {
|
|
41
|
+
void this._broadcastUpdate.notifyIfUpdated(options);
|
|
42
|
+
}
|
|
44
43
|
}
|
|
45
|
-
|
|
46
|
-
export { BroadcastUpdatePlugin };
|
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
|
|
9
9
|
export const CACHE_UPDATED_MESSAGE_TYPE = "CACHE_UPDATED";
|
|
10
10
|
export const CACHE_UPDATED_MESSAGE_META = "serwist-broadcast-update";
|
|
11
|
-
export const
|
|
12
|
-
export const
|
|
11
|
+
export const defaultNotifyAllClients = true;
|
|
12
|
+
export const defaultHeadersToCheck = ["content-length", "etag", "last-modified"];
|
package/src/index.ts
CHANGED
|
@@ -6,15 +6,18 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type { BroadcastCacheUpdateOptions } from "./BroadcastCacheUpdate.js";
|
|
10
9
|
import { BroadcastCacheUpdate } from "./BroadcastCacheUpdate.js";
|
|
11
10
|
import { BroadcastUpdatePlugin } from "./BroadcastUpdatePlugin.js";
|
|
11
|
+
import { CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE, defaultHeadersToCheck } from "./constants.js";
|
|
12
12
|
import { responsesAreSame } from "./responsesAreSame.js";
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
export {
|
|
15
|
+
BroadcastCacheUpdate,
|
|
16
|
+
BroadcastUpdatePlugin,
|
|
17
|
+
responsesAreSame,
|
|
18
|
+
CACHE_UPDATED_MESSAGE_META,
|
|
19
|
+
CACHE_UPDATED_MESSAGE_TYPE,
|
|
20
|
+
defaultHeadersToCheck,
|
|
21
|
+
};
|
|
17
22
|
|
|
18
|
-
export
|
|
19
|
-
|
|
20
|
-
export type { BroadcastCacheUpdateOptions };
|
|
23
|
+
export type * from "./types.js";
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { CacheDidUpdateCallbackParam } from "@serwist/core";
|
|
2
|
+
import type { CACHE_UPDATED_MESSAGE_META, CACHE_UPDATED_MESSAGE_TYPE } from "./constants.js";
|
|
3
|
+
|
|
4
|
+
export interface BroadcastCacheUpdateOptions {
|
|
5
|
+
/**
|
|
6
|
+
* A list of headers that will be used to determine whether the responses
|
|
7
|
+
* differ.
|
|
8
|
+
*
|
|
9
|
+
* @default ['content-length', 'etag', 'last-modified']
|
|
10
|
+
*/
|
|
11
|
+
headersToCheck?: string[];
|
|
12
|
+
/**
|
|
13
|
+
* A function whose return value
|
|
14
|
+
* will be used as the `payload` field in any cache update messages sent
|
|
15
|
+
* to the window clients.
|
|
16
|
+
* @param options
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
generatePayload?: (options: CacheDidUpdateCallbackParam) => Record<string, any>;
|
|
20
|
+
/**
|
|
21
|
+
* If true (the default) then all open clients will receive a message. If false,
|
|
22
|
+
* then only the client that make the original request will be notified of the update.
|
|
23
|
+
*
|
|
24
|
+
* @default true
|
|
25
|
+
*/
|
|
26
|
+
notifyAllClients?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type BroadcastPayload = Record<string, any>;
|
|
30
|
+
|
|
31
|
+
export interface BroadcastPayloadGenerator {
|
|
32
|
+
(options: CacheDidUpdateCallbackParam): BroadcastPayload;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface BroadcastMessage {
|
|
36
|
+
type: typeof CACHE_UPDATED_MESSAGE_TYPE;
|
|
37
|
+
meta: typeof CACHE_UPDATED_MESSAGE_META;
|
|
38
|
+
payload: BroadcastPayload;
|
|
39
|
+
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export declare const CACHE_UPDATED_MESSAGE_TYPE = "CACHE_UPDATED";
|
|
2
|
-
export declare const CACHE_UPDATED_MESSAGE_META = "serwist-broadcast-update";
|
|
3
|
-
export declare const NOTIFY_ALL_CLIENTS = true;
|
|
4
|
-
export declare const DEFAULT_HEADERS_TO_CHECK: string[];
|
|
5
|
-
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/utils/constants.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,0BAA0B,kBAAkB,CAAC;AAC1D,eAAO,MAAM,0BAA0B,6BAA6B,CAAC;AACrE,eAAO,MAAM,kBAAkB,OAAO,CAAC;AACvC,eAAO,MAAM,wBAAwB,EAAE,MAAM,EAAgD,CAAC"}
|