@serwist/sw 9.0.0-preview.20 → 9.0.0-preview.24
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/index.d.ts +4 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -156
- package/package.json +5 -40
- package/src/index.ts +5 -22
- package/README.md +0 -1
- package/dist/abstractions/Serwist.d.ts +0 -82
- package/dist/abstractions/Serwist.d.ts.map +0 -1
- package/dist/abstractions/disableDevLogs.d.ts +0 -7
- package/dist/abstractions/disableDevLogs.d.ts.map +0 -1
- package/dist/abstractions/fallbacks.d.ts +0 -39
- package/dist/abstractions/fallbacks.d.ts.map +0 -1
- package/dist/abstractions/handlePrecaching.d.ts +0 -39
- package/dist/abstractions/handlePrecaching.d.ts.map +0 -1
- package/dist/abstractions/installSerwist.d.ts +0 -15
- package/dist/abstractions/installSerwist.d.ts.map +0 -1
- package/dist/abstractions/navigationPreload.d.ts +0 -20
- package/dist/abstractions/navigationPreload.d.ts.map +0 -1
- package/dist/abstractions/registerRuntimeCaching.d.ts +0 -11
- package/dist/abstractions/registerRuntimeCaching.d.ts.map +0 -1
- package/dist/abstractions/types.d.ts +0 -29
- package/dist/abstractions/types.d.ts.map +0 -1
- package/dist/chunks/NavigationRoute.js +0 -54
- package/dist/chunks/NetworkOnly.js +0 -193
- package/dist/chunks/PrecacheFallbackPlugin.js +0 -573
- package/dist/chunks/Strategy.js +0 -410
- package/dist/chunks/precacheAndRoute.js +0 -113
- package/dist/chunks/registerRoute.js +0 -7
- package/dist/chunks/singletonPrecacheController.js +0 -433
- package/dist/chunks/singletonRouter.js +0 -435
- package/dist/index.plugins.d.ts +0 -41
- package/dist/index.plugins.d.ts.map +0 -1
- package/dist/index.plugins.js +0 -671
- package/dist/index.precaching.d.ts +0 -25
- package/dist/index.precaching.d.ts.map +0 -1
- package/dist/index.precaching.js +0 -24
- package/dist/index.routing.d.ts +0 -15
- package/dist/index.routing.d.ts.map +0 -1
- package/dist/index.routing.js +0 -19
- package/dist/index.strategies.d.ts +0 -22
- package/dist/index.strategies.d.ts.map +0 -1
- package/dist/index.strategies.js +0 -146
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts +0 -23
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts.map +0 -1
- package/dist/plugins/backgroundSync/Queue.d.ts +0 -166
- package/dist/plugins/backgroundSync/Queue.d.ts.map +0 -1
- package/dist/plugins/backgroundSync/QueueDb.d.ts +0 -90
- package/dist/plugins/backgroundSync/QueueDb.d.ts.map +0 -1
- package/dist/plugins/backgroundSync/QueueStore.d.ts +0 -75
- package/dist/plugins/backgroundSync/QueueStore.d.ts.map +0 -1
- package/dist/plugins/backgroundSync/StorableRequest.d.ts +0 -51
- package/dist/plugins/backgroundSync/StorableRequest.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts +0 -45
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts +0 -27
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/constants.d.ts +0 -5
- package/dist/plugins/broadcastUpdate/constants.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts +0 -11
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts.map +0 -1
- package/dist/plugins/broadcastUpdate/types.d.ts +0 -34
- package/dist/plugins/broadcastUpdate/types.d.ts.map +0 -1
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts +0 -40
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts.map +0 -1
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts +0 -27
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts.map +0 -1
- package/dist/plugins/expiration/CacheExpiration.d.ts +0 -66
- package/dist/plugins/expiration/CacheExpiration.d.ts.map +0 -1
- package/dist/plugins/expiration/ExpirationPlugin.d.ts +0 -116
- package/dist/plugins/expiration/ExpirationPlugin.d.ts.map +0 -1
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts +0 -73
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts.map +0 -1
- package/dist/plugins/googleAnalytics/constants.d.ts +0 -10
- package/dist/plugins/googleAnalytics/constants.d.ts.map +0 -1
- package/dist/plugins/googleAnalytics/initialize.d.ts +0 -34
- package/dist/plugins/googleAnalytics/initialize.d.ts.map +0 -1
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts +0 -54
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts.map +0 -1
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts +0 -19
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts.map +0 -1
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts +0 -18
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts.map +0 -1
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts +0 -14
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +0 -1
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts +0 -12
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts.map +0 -1
- package/dist/precaching/PrecacheController.d.ts +0 -145
- package/dist/precaching/PrecacheController.d.ts.map +0 -1
- package/dist/precaching/PrecacheRoute.d.ts +0 -20
- package/dist/precaching/PrecacheRoute.d.ts.map +0 -1
- package/dist/precaching/PrecacheStrategy.d.ts +0 -68
- package/dist/precaching/PrecacheStrategy.d.ts.map +0 -1
- package/dist/precaching/addPlugins.d.ts +0 -8
- package/dist/precaching/addPlugins.d.ts.map +0 -1
- package/dist/precaching/addRoute.d.ts +0 -15
- package/dist/precaching/addRoute.d.ts.map +0 -1
- package/dist/precaching/cleanupOutdatedCaches.d.ts +0 -6
- package/dist/precaching/cleanupOutdatedCaches.d.ts.map +0 -1
- package/dist/precaching/createHandlerBoundToURL.d.ts +0 -17
- package/dist/precaching/createHandlerBoundToURL.d.ts.map +0 -1
- package/dist/precaching/getCacheKeyForURL.d.ts +0 -20
- package/dist/precaching/getCacheKeyForURL.d.ts.map +0 -1
- package/dist/precaching/matchPrecache.d.ts +0 -14
- package/dist/precaching/matchPrecache.d.ts.map +0 -1
- package/dist/precaching/precache.d.ts +0 -19
- package/dist/precaching/precache.d.ts.map +0 -1
- package/dist/precaching/precacheAndRoute.d.ts +0 -14
- package/dist/precaching/precacheAndRoute.d.ts.map +0 -1
- package/dist/precaching/singletonPrecacheController.d.ts +0 -38
- package/dist/precaching/singletonPrecacheController.d.ts.map +0 -1
- package/dist/precaching/types.d.ts +0 -37
- package/dist/precaching/types.d.ts.map +0 -1
- package/dist/precaching/utils/PrecacheCacheKeyPlugin.d.ts +0 -17
- package/dist/precaching/utils/PrecacheCacheKeyPlugin.d.ts.map +0 -1
- package/dist/precaching/utils/PrecacheInstallReportPlugin.d.ts +0 -15
- package/dist/precaching/utils/PrecacheInstallReportPlugin.d.ts.map +0 -1
- package/dist/precaching/utils/createCacheKey.d.ts +0 -16
- package/dist/precaching/utils/createCacheKey.d.ts.map +0 -1
- package/dist/precaching/utils/deleteOutdatedCaches.d.ts +0 -18
- package/dist/precaching/utils/deleteOutdatedCaches.d.ts.map +0 -1
- package/dist/precaching/utils/generateURLVariations.d.ts +0 -12
- package/dist/precaching/utils/generateURLVariations.d.ts.map +0 -1
- package/dist/precaching/utils/getCacheKeyForURL.d.ts +0 -14
- package/dist/precaching/utils/getCacheKeyForURL.d.ts.map +0 -1
- package/dist/precaching/utils/printCleanupDetails.d.ts +0 -6
- package/dist/precaching/utils/printCleanupDetails.d.ts.map +0 -1
- package/dist/precaching/utils/printInstallDetails.d.ts +0 -7
- package/dist/precaching/utils/printInstallDetails.d.ts.map +0 -1
- package/dist/precaching/utils/removeIgnoredSearchParams.d.ts +0 -12
- package/dist/precaching/utils/removeIgnoredSearchParams.d.ts.map +0 -1
- package/dist/routing/NavigationRoute.d.ts +0 -57
- package/dist/routing/NavigationRoute.d.ts.map +0 -1
- package/dist/routing/RegExpRoute.d.ts +0 -24
- package/dist/routing/RegExpRoute.d.ts.map +0 -1
- package/dist/routing/Route.d.ts +0 -33
- package/dist/routing/Route.d.ts.map +0 -1
- package/dist/routing/Router.d.ts +0 -150
- package/dist/routing/Router.d.ts.map +0 -1
- package/dist/routing/parseRoute.d.ts +0 -16
- package/dist/routing/parseRoute.d.ts.map +0 -1
- package/dist/routing/registerRoute.d.ts +0 -15
- package/dist/routing/registerRoute.d.ts.map +0 -1
- package/dist/routing/setCatchHandler.d.ts +0 -9
- package/dist/routing/setCatchHandler.d.ts.map +0 -1
- package/dist/routing/setDefaultHandler.d.ts +0 -12
- package/dist/routing/setDefaultHandler.d.ts.map +0 -1
- package/dist/routing/singletonRouter.d.ts +0 -47
- package/dist/routing/singletonRouter.d.ts.map +0 -1
- package/dist/routing/unregisterRoute.d.ts +0 -8
- package/dist/routing/unregisterRoute.d.ts.map +0 -1
- package/dist/routing/utils/constants.d.ts +0 -15
- package/dist/routing/utils/constants.d.ts.map +0 -1
- package/dist/routing/utils/normalizeHandler.d.ts +0 -10
- package/dist/routing/utils/normalizeHandler.d.ts.map +0 -1
- package/dist/strategies/CacheFirst.d.ts +0 -23
- package/dist/strategies/CacheFirst.d.ts.map +0 -1
- package/dist/strategies/CacheOnly.d.ts +0 -20
- package/dist/strategies/CacheOnly.d.ts.map +0 -1
- package/dist/strategies/NetworkFirst.d.ts +0 -61
- package/dist/strategies/NetworkFirst.d.ts.map +0 -1
- package/dist/strategies/NetworkOnly.d.ts +0 -32
- package/dist/strategies/NetworkOnly.d.ts.map +0 -1
- package/dist/strategies/StaleWhileRevalidate.d.ts +0 -35
- package/dist/strategies/StaleWhileRevalidate.d.ts.map +0 -1
- package/dist/strategies/Strategy.d.ts +0 -83
- package/dist/strategies/Strategy.d.ts.map +0 -1
- package/dist/strategies/StrategyHandler.d.ts +0 -189
- package/dist/strategies/StrategyHandler.d.ts.map +0 -1
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +0 -3
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +0 -1
- package/dist/strategies/utils/messages.d.ts +0 -5
- package/dist/strategies/utils/messages.d.ts.map +0 -1
- package/src/abstractions/Serwist.ts +0 -177
- package/src/abstractions/disableDevLogs.ts +0 -10
- package/src/abstractions/fallbacks.ts +0 -65
- package/src/abstractions/handlePrecaching.ts +0 -65
- package/src/abstractions/installSerwist.ts +0 -28
- package/src/abstractions/navigationPreload.ts +0 -64
- package/src/abstractions/registerRuntimeCaching.ts +0 -17
- package/src/abstractions/types.ts +0 -29
- package/src/index.plugins.ts +0 -95
- package/src/index.precaching.ts +0 -41
- package/src/index.routing.ts +0 -28
- package/src/index.strategies.ts +0 -26
- package/src/plugins/backgroundSync/BackgroundSyncPlugin.ts +0 -39
- package/src/plugins/backgroundSync/Queue.ts +0 -438
- package/src/plugins/backgroundSync/QueueDb.ts +0 -176
- package/src/plugins/backgroundSync/QueueStore.ts +0 -161
- package/src/plugins/backgroundSync/StorableRequest.ts +0 -142
- package/src/plugins/broadcastUpdate/BroadcastCacheUpdate.ts +0 -159
- package/src/plugins/broadcastUpdate/BroadcastUpdatePlugin.ts +0 -43
- package/src/plugins/broadcastUpdate/constants.ts +0 -12
- package/src/plugins/broadcastUpdate/responsesAreSame.ts +0 -48
- package/src/plugins/broadcastUpdate/types.ts +0 -37
- package/src/plugins/cacheableResponse/CacheableResponse.ts +0 -141
- package/src/plugins/cacheableResponse/CacheableResponsePlugin.ts +0 -46
- package/src/plugins/expiration/CacheExpiration.ts +0 -192
- package/src/plugins/expiration/ExpirationPlugin.ts +0 -297
- package/src/plugins/expiration/models/CacheTimestampsModel.ts +0 -184
- package/src/plugins/googleAnalytics/constants.ts +0 -22
- package/src/plugins/googleAnalytics/initialize.ts +0 -213
- package/src/plugins/precaching/PrecacheFallbackPlugin.ts +0 -86
- package/src/plugins/rangeRequests/RangeRequestsPlugin.ts +0 -39
- package/src/plugins/rangeRequests/createPartialResponse.ts +0 -92
- package/src/plugins/rangeRequests/utils/calculateEffectiveBoundaries.ts +0 -58
- package/src/plugins/rangeRequests/utils/parseRangeHeader.ts +0 -54
- package/src/precaching/PrecacheController.ts +0 -332
- package/src/precaching/PrecacheRoute.ts +0 -50
- package/src/precaching/PrecacheStrategy.ts +0 -238
- package/src/precaching/addPlugins.ts +0 -21
- package/src/precaching/addRoute.ts +0 -30
- package/src/precaching/cleanupOutdatedCaches.ts +0 -33
- package/src/precaching/createHandlerBoundToURL.ts +0 -30
- package/src/precaching/getCacheKeyForURL.ts +0 -33
- package/src/precaching/matchPrecache.ts +0 -25
- package/src/precaching/precache.ts +0 -31
- package/src/precaching/precacheAndRoute.ts +0 -27
- package/src/precaching/singletonPrecacheController.ts +0 -57
- package/src/precaching/types.ts +0 -46
- package/src/precaching/utils/PrecacheCacheKeyPlugin.ts +0 -36
- package/src/precaching/utils/PrecacheInstallReportPlugin.ts +0 -49
- package/src/precaching/utils/createCacheKey.ts +0 -68
- package/src/precaching/utils/deleteOutdatedCaches.ts +0 -40
- package/src/precaching/utils/generateURLVariations.ts +0 -55
- package/src/precaching/utils/getCacheKeyForURL.ts +0 -36
- package/src/precaching/utils/printCleanupDetails.ts +0 -38
- package/src/precaching/utils/printInstallDetails.ts +0 -53
- package/src/precaching/utils/removeIgnoredSearchParams.ts +0 -29
- package/src/routing/NavigationRoute.ts +0 -119
- package/src/routing/RegExpRoute.ts +0 -74
- package/src/routing/Route.ts +0 -68
- package/src/routing/Router.ts +0 -481
- package/src/routing/parseRoute.ts +0 -78
- package/src/routing/registerRoute.ts +0 -27
- package/src/routing/setCatchHandler.ts +0 -21
- package/src/routing/setDefaultHandler.ts +0 -24
- package/src/routing/singletonRouter.ts +0 -76
- package/src/routing/unregisterRoute.ts +0 -11
- package/src/routing/utils/constants.ts +0 -24
- package/src/routing/utils/normalizeHandler.ts +0 -40
- package/src/strategies/CacheFirst.ts +0 -87
- package/src/strategies/CacheOnly.ts +0 -58
- package/src/strategies/NetworkFirst.ts +0 -228
- package/src/strategies/NetworkOnly.ts +0 -96
- package/src/strategies/StaleWhileRevalidate.ts +0 -109
- package/src/strategies/Strategy.ts +0 -202
- package/src/strategies/StrategyHandler.ts +0 -557
- package/src/strategies/plugins/cacheOkAndOpaquePlugin.ts +0 -26
- package/src/strategies/utils/messages.ts +0 -20
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import { getFriendlyURL, logger, assert, SerwistError, timeout } from '@serwist/core/internal';
|
|
2
|
-
import { S as Strategy } from './Strategy.js';
|
|
3
|
-
|
|
4
|
-
const cacheOkAndOpaquePlugin = {
|
|
5
|
-
cacheWillUpdate: async ({ response })=>{
|
|
6
|
-
if (response.status === 200 || response.status === 0) {
|
|
7
|
-
return response;
|
|
8
|
-
}
|
|
9
|
-
return null;
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const messages = {
|
|
14
|
-
strategyStart: (strategyName, request)=>`Using ${strategyName} to respond to '${getFriendlyURL(request.url)}'`,
|
|
15
|
-
printFinalResponse: (response)=>{
|
|
16
|
-
if (response) {
|
|
17
|
-
logger.groupCollapsed("View the final response here.");
|
|
18
|
-
logger.log(response || "[No response returned]");
|
|
19
|
-
logger.groupEnd();
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
class NetworkFirst extends Strategy {
|
|
25
|
-
_networkTimeoutSeconds;
|
|
26
|
-
constructor(options = {}){
|
|
27
|
-
super(options);
|
|
28
|
-
if (!this.plugins.some((p)=>"cacheWillUpdate" in p)) {
|
|
29
|
-
this.plugins.unshift(cacheOkAndOpaquePlugin);
|
|
30
|
-
}
|
|
31
|
-
this._networkTimeoutSeconds = options.networkTimeoutSeconds || 0;
|
|
32
|
-
if (process.env.NODE_ENV !== "production") {
|
|
33
|
-
if (this._networkTimeoutSeconds) {
|
|
34
|
-
assert.isType(this._networkTimeoutSeconds, "number", {
|
|
35
|
-
moduleName: "@serwist/strategies",
|
|
36
|
-
className: this.constructor.name,
|
|
37
|
-
funcName: "constructor",
|
|
38
|
-
paramName: "networkTimeoutSeconds"
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
async _handle(request, handler) {
|
|
44
|
-
const logs = [];
|
|
45
|
-
if (process.env.NODE_ENV !== "production") {
|
|
46
|
-
assert.isInstance(request, Request, {
|
|
47
|
-
moduleName: "@serwist/strategies",
|
|
48
|
-
className: this.constructor.name,
|
|
49
|
-
funcName: "handle",
|
|
50
|
-
paramName: "makeRequest"
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
const promises = [];
|
|
54
|
-
let timeoutId;
|
|
55
|
-
if (this._networkTimeoutSeconds) {
|
|
56
|
-
const { id, promise } = this._getTimeoutPromise({
|
|
57
|
-
request,
|
|
58
|
-
logs,
|
|
59
|
-
handler
|
|
60
|
-
});
|
|
61
|
-
timeoutId = id;
|
|
62
|
-
promises.push(promise);
|
|
63
|
-
}
|
|
64
|
-
const networkPromise = this._getNetworkPromise({
|
|
65
|
-
timeoutId,
|
|
66
|
-
request,
|
|
67
|
-
logs,
|
|
68
|
-
handler
|
|
69
|
-
});
|
|
70
|
-
promises.push(networkPromise);
|
|
71
|
-
const response = await handler.waitUntil((async ()=>{
|
|
72
|
-
return await handler.waitUntil(Promise.race(promises)) || await networkPromise;
|
|
73
|
-
})());
|
|
74
|
-
if (process.env.NODE_ENV !== "production") {
|
|
75
|
-
logger.groupCollapsed(messages.strategyStart(this.constructor.name, request));
|
|
76
|
-
for (const log of logs){
|
|
77
|
-
logger.log(log);
|
|
78
|
-
}
|
|
79
|
-
messages.printFinalResponse(response);
|
|
80
|
-
logger.groupEnd();
|
|
81
|
-
}
|
|
82
|
-
if (!response) {
|
|
83
|
-
throw new SerwistError("no-response", {
|
|
84
|
-
url: request.url
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
return response;
|
|
88
|
-
}
|
|
89
|
-
_getTimeoutPromise({ request, logs, handler }) {
|
|
90
|
-
let timeoutId;
|
|
91
|
-
const timeoutPromise = new Promise((resolve)=>{
|
|
92
|
-
const onNetworkTimeout = async ()=>{
|
|
93
|
-
if (process.env.NODE_ENV !== "production") {
|
|
94
|
-
logs.push(`Timing out the network response at ${this._networkTimeoutSeconds} seconds.`);
|
|
95
|
-
}
|
|
96
|
-
resolve(await handler.cacheMatch(request));
|
|
97
|
-
};
|
|
98
|
-
timeoutId = setTimeout(onNetworkTimeout, this._networkTimeoutSeconds * 1000);
|
|
99
|
-
});
|
|
100
|
-
return {
|
|
101
|
-
promise: timeoutPromise,
|
|
102
|
-
id: timeoutId
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
async _getNetworkPromise({ timeoutId, request, logs, handler }) {
|
|
106
|
-
let error = undefined;
|
|
107
|
-
let response = undefined;
|
|
108
|
-
try {
|
|
109
|
-
response = await handler.fetchAndCachePut(request);
|
|
110
|
-
} catch (fetchError) {
|
|
111
|
-
if (fetchError instanceof Error) {
|
|
112
|
-
error = fetchError;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
if (timeoutId) {
|
|
116
|
-
clearTimeout(timeoutId);
|
|
117
|
-
}
|
|
118
|
-
if (process.env.NODE_ENV !== "production") {
|
|
119
|
-
if (response) {
|
|
120
|
-
logs.push("Got response from network.");
|
|
121
|
-
} else {
|
|
122
|
-
logs.push("Unable to get a response from the network. Will respond " + "with a cached response.");
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
if (error || !response) {
|
|
126
|
-
response = await handler.cacheMatch(request);
|
|
127
|
-
if (process.env.NODE_ENV !== "production") {
|
|
128
|
-
if (response) {
|
|
129
|
-
logs.push(`Found a cached response in the '${this.cacheName}' cache.`);
|
|
130
|
-
} else {
|
|
131
|
-
logs.push(`No response found in the '${this.cacheName}' cache.`);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return response;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
class NetworkOnly extends Strategy {
|
|
140
|
-
_networkTimeoutSeconds;
|
|
141
|
-
constructor(options = {}){
|
|
142
|
-
super(options);
|
|
143
|
-
this._networkTimeoutSeconds = options.networkTimeoutSeconds || 0;
|
|
144
|
-
}
|
|
145
|
-
async _handle(request, handler) {
|
|
146
|
-
if (process.env.NODE_ENV !== "production") {
|
|
147
|
-
assert.isInstance(request, Request, {
|
|
148
|
-
moduleName: "@serwist/strategies",
|
|
149
|
-
className: this.constructor.name,
|
|
150
|
-
funcName: "_handle",
|
|
151
|
-
paramName: "request"
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
let error = undefined;
|
|
155
|
-
let response;
|
|
156
|
-
try {
|
|
157
|
-
const promises = [
|
|
158
|
-
handler.fetch(request)
|
|
159
|
-
];
|
|
160
|
-
if (this._networkTimeoutSeconds) {
|
|
161
|
-
const timeoutPromise = timeout(this._networkTimeoutSeconds * 1000);
|
|
162
|
-
promises.push(timeoutPromise);
|
|
163
|
-
}
|
|
164
|
-
response = await Promise.race(promises);
|
|
165
|
-
if (!response) {
|
|
166
|
-
throw new Error(`Timed out the network response after ${this._networkTimeoutSeconds} seconds.`);
|
|
167
|
-
}
|
|
168
|
-
} catch (err) {
|
|
169
|
-
if (err instanceof Error) {
|
|
170
|
-
error = err;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
if (process.env.NODE_ENV !== "production") {
|
|
174
|
-
logger.groupCollapsed(messages.strategyStart(this.constructor.name, request));
|
|
175
|
-
if (response) {
|
|
176
|
-
logger.log("Got response from network.");
|
|
177
|
-
} else {
|
|
178
|
-
logger.log("Unable to get a response from the network.");
|
|
179
|
-
}
|
|
180
|
-
messages.printFinalResponse(response);
|
|
181
|
-
logger.groupEnd();
|
|
182
|
-
}
|
|
183
|
-
if (!response) {
|
|
184
|
-
throw new SerwistError("no-response", {
|
|
185
|
-
url: request.url,
|
|
186
|
-
error
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
return response;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
export { NetworkOnly as N, NetworkFirst as a, cacheOkAndOpaquePlugin as c, messages as m };
|