ts-service-worker 1.1.1 → 1.2.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/README.md +1 -1
- package/dist/cli.mjs +45 -16
- package/dist/index.js +45 -16
- package/dist/index.js.map +3 -3
- package/package.json +1 -1
- package/types/{fileSpecsToPaths.d.mts → filesSpecToPaths.d.mts} +1 -1
- package/types/organizeStrategies.d.mts +6 -0
- package/types/strategies.d.mts +3 -1
- package/types/utils.d.mts +1 -0
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
- provide declarative support for the most common caching strategies
|
|
4
4
|
- supports easy integration of multiple caching strategies
|
|
5
|
-
- produces a `service-worker.js` that requires no additional
|
|
5
|
+
- produces a `service-worker.js` that requires no additional transcompilation
|
|
6
6
|
- avoids nesting and promise chains
|
|
7
7
|
- is written in Typescript and fully typed
|
|
8
8
|
- can scan your disk for resources to cache
|
package/dist/cli.mjs
CHANGED
|
@@ -6449,8 +6449,8 @@ var glob = Object.assign(glob_, {
|
|
|
6449
6449
|
});
|
|
6450
6450
|
glob.glob = glob;
|
|
6451
6451
|
|
|
6452
|
-
// src/
|
|
6453
|
-
function
|
|
6452
|
+
// src/filesSpecToPaths.mts
|
|
6453
|
+
function filesSpecToPaths(files) {
|
|
6454
6454
|
let prefix = "";
|
|
6455
6455
|
let dir = "";
|
|
6456
6456
|
let globs;
|
|
@@ -6488,7 +6488,7 @@ function isCachePathOnInstall(s) {
|
|
|
6488
6488
|
return s.strategy === "cache-on-install" && "paths" in s;
|
|
6489
6489
|
}
|
|
6490
6490
|
function isStaticOfflineBackup(s) {
|
|
6491
|
-
return s.strategy === "staticOfflineBackup";
|
|
6491
|
+
return s.strategy === "staticOfflineBackup" && !!s.paths && !!s.backup;
|
|
6492
6492
|
}
|
|
6493
6493
|
var OriginAndBelow = Symbol.for("origin-and-below");
|
|
6494
6494
|
var ScopeAndBelow = Symbol.for("scope-and-below");
|
|
@@ -6496,7 +6496,7 @@ function isRoutableStrategy(x) {
|
|
|
6496
6496
|
return x.strategy === "cacheFirst" || x.strategy === "networkFirst" || x.strategy === "networkOnly" || x.strategy === "staleWhileRevalidate" || x.strategy === "staticOfflineBackup";
|
|
6497
6497
|
}
|
|
6498
6498
|
function convertPreloadFilesToPaths(strategy) {
|
|
6499
|
-
const paths =
|
|
6499
|
+
const paths = filesSpecToPaths(strategy.files);
|
|
6500
6500
|
return {
|
|
6501
6501
|
strategy: "cache-on-install",
|
|
6502
6502
|
paths
|
|
@@ -6551,7 +6551,9 @@ self.addEventListener('activate', function (event) {
|
|
|
6551
6551
|
}))));
|
|
6552
6552
|
});
|
|
6553
6553
|
self.addEventListener('fetch', function (event) {
|
|
6554
|
-
|
|
6554
|
+
|
|
6555
|
+
const url = event.request.url.replace(/#.*$/, '');
|
|
6556
|
+
|
|
6555
6557
|
// Cache http and https only, skip unsupported chrome-extension:// and file://...
|
|
6556
6558
|
if (!url.startsWith('http'))
|
|
6557
6559
|
return;
|
|
@@ -6568,7 +6570,7 @@ self.addEventListener('fetch', function (event) {
|
|
|
6568
6570
|
function matches(paths) {
|
|
6569
6571
|
return paths &&
|
|
6570
6572
|
paths.some(path => path instanceof RegExp ?
|
|
6571
|
-
path.exec(url) : url
|
|
6573
|
+
path.exec(url) : url === path);
|
|
6572
6574
|
}
|
|
6573
6575
|
});
|
|
6574
6576
|
// Caching strategies
|
|
@@ -6616,11 +6618,12 @@ function fetchRequest(event) {
|
|
|
6616
6618
|
function fetchAndCache(event) {
|
|
6617
6619
|
return fetchRequest(event)
|
|
6618
6620
|
.then(response => {
|
|
6619
|
-
return toCache(event, response)
|
|
6620
|
-
|
|
6621
|
-
|
|
6622
|
-
})
|
|
6623
|
-
|
|
6621
|
+
return toCache(event, response)
|
|
6622
|
+
})
|
|
6623
|
+
.catch(e => {
|
|
6624
|
+
log(\` - unable to fetch \${event.request.url} \${e}\`);
|
|
6625
|
+
throw e
|
|
6626
|
+
});
|
|
6624
6627
|
}
|
|
6625
6628
|
function fromCache(event) {
|
|
6626
6629
|
return caches.open(CACHE_NAME).then(cache => cache.match(event.request));
|
|
@@ -6630,7 +6633,7 @@ function fileFromCache(name) {
|
|
|
6630
6633
|
}
|
|
6631
6634
|
function toCache(event, response) {
|
|
6632
6635
|
if (!response.ok) {
|
|
6633
|
-
log(\`request
|
|
6636
|
+
log(\`skipping cache of failed request \${event.request.url} with status \${response.status} \${response.statusText}\`);
|
|
6634
6637
|
return response;
|
|
6635
6638
|
}
|
|
6636
6639
|
return caches.open(CACHE_NAME)
|
|
@@ -6649,8 +6652,36 @@ function log(s) {
|
|
|
6649
6652
|
function regexEscape(s) {
|
|
6650
6653
|
return s.replace(/([/.])/g, '\\\\$&');
|
|
6651
6654
|
}
|
|
6655
|
+
|
|
6656
|
+
function withOrigin(path) {
|
|
6657
|
+
if (path.match(/https?:\\/\\//)) return path
|
|
6658
|
+
if (path.startsWith('/')) return self.origin + path
|
|
6659
|
+
return self.origin + '/' + path
|
|
6660
|
+
}
|
|
6652
6661
|
`;
|
|
6653
6662
|
|
|
6663
|
+
// src/utils.mts
|
|
6664
|
+
function deDup(paths) {
|
|
6665
|
+
if (!Array.isArray(paths))
|
|
6666
|
+
return paths;
|
|
6667
|
+
return [...new Set(paths)].sort();
|
|
6668
|
+
}
|
|
6669
|
+
|
|
6670
|
+
// src/organizeStrategies.mts
|
|
6671
|
+
function organizeStrategies(inputPlan) {
|
|
6672
|
+
const spec = convertAllFilesToPaths(inputPlan);
|
|
6673
|
+
const preloadPaths = deDup(extractAllPreloadPaths(spec));
|
|
6674
|
+
const specWithoutPreloads = convertPreloadPathsToCacheFirst(spec);
|
|
6675
|
+
const routable = removeDuplicatePaths(specWithoutPreloads);
|
|
6676
|
+
return { preloadPaths, routable };
|
|
6677
|
+
}
|
|
6678
|
+
function removeDuplicatePaths(specs) {
|
|
6679
|
+
return specs.map((spec) => ({
|
|
6680
|
+
...spec,
|
|
6681
|
+
paths: deDup(spec.paths)
|
|
6682
|
+
}));
|
|
6683
|
+
}
|
|
6684
|
+
|
|
6654
6685
|
// src/generate.mts
|
|
6655
6686
|
var defaultOptions = {
|
|
6656
6687
|
version: "0.0",
|
|
@@ -6662,9 +6693,7 @@ function generateServiceWorker(inputSpec, optionsIn = {}) {
|
|
|
6662
6693
|
...defaultOptions,
|
|
6663
6694
|
...optionsIn
|
|
6664
6695
|
};
|
|
6665
|
-
const
|
|
6666
|
-
const preloadPaths = extractAllPreloadPaths(spec);
|
|
6667
|
-
const routable = convertPreloadPathsToCacheFirst(spec);
|
|
6696
|
+
const { preloadPaths, routable } = organizeStrategies(inputSpec);
|
|
6668
6697
|
const varsBlock = generateFlag("DEBUG", options.debug) + generateFlag("SKIP_WAITING", options.skipWaiting) + generateVersion(options.version) + generatePreloadCode(preloadPaths) + generateRoutes(routable);
|
|
6669
6698
|
return evalTemplate(varsBlock);
|
|
6670
6699
|
}
|
|
@@ -6690,7 +6719,7 @@ function evalTemplate(variables) {
|
|
|
6690
6719
|
}
|
|
6691
6720
|
function pathsToJS(paths) {
|
|
6692
6721
|
const asArray = Array.isArray(paths) ? paths : [paths];
|
|
6693
|
-
return `[${asArray.map(pathToJS).join(",")}]`;
|
|
6722
|
+
return `[${asArray.map(pathToJS).join(",")}].map(withOrigin)`;
|
|
6694
6723
|
}
|
|
6695
6724
|
function pathToJS(path3) {
|
|
6696
6725
|
switch (typeof path3) {
|
package/dist/index.js
CHANGED
|
@@ -6461,8 +6461,8 @@ var glob = Object.assign(glob_, {
|
|
|
6461
6461
|
});
|
|
6462
6462
|
glob.glob = glob;
|
|
6463
6463
|
|
|
6464
|
-
// src/
|
|
6465
|
-
function
|
|
6464
|
+
// src/filesSpecToPaths.mts
|
|
6465
|
+
function filesSpecToPaths(files) {
|
|
6466
6466
|
let prefix = "";
|
|
6467
6467
|
let dir = "";
|
|
6468
6468
|
let globs;
|
|
@@ -6500,7 +6500,7 @@ function isCachePathOnInstall(s) {
|
|
|
6500
6500
|
return s.strategy === "cache-on-install" && "paths" in s;
|
|
6501
6501
|
}
|
|
6502
6502
|
function isStaticOfflineBackup(s) {
|
|
6503
|
-
return s.strategy === "staticOfflineBackup";
|
|
6503
|
+
return s.strategy === "staticOfflineBackup" && !!s.paths && !!s.backup;
|
|
6504
6504
|
}
|
|
6505
6505
|
var OriginAndBelow = Symbol.for("origin-and-below");
|
|
6506
6506
|
var ScopeAndBelow = Symbol.for("scope-and-below");
|
|
@@ -6508,7 +6508,7 @@ function isRoutableStrategy(x) {
|
|
|
6508
6508
|
return x.strategy === "cacheFirst" || x.strategy === "networkFirst" || x.strategy === "networkOnly" || x.strategy === "staleWhileRevalidate" || x.strategy === "staticOfflineBackup";
|
|
6509
6509
|
}
|
|
6510
6510
|
function convertPreloadFilesToPaths(strategy) {
|
|
6511
|
-
const paths =
|
|
6511
|
+
const paths = filesSpecToPaths(strategy.files);
|
|
6512
6512
|
return {
|
|
6513
6513
|
strategy: "cache-on-install",
|
|
6514
6514
|
paths
|
|
@@ -6563,7 +6563,9 @@ self.addEventListener('activate', function (event) {
|
|
|
6563
6563
|
}))));
|
|
6564
6564
|
});
|
|
6565
6565
|
self.addEventListener('fetch', function (event) {
|
|
6566
|
-
|
|
6566
|
+
|
|
6567
|
+
const url = event.request.url.replace(/#.*$/, '');
|
|
6568
|
+
|
|
6567
6569
|
// Cache http and https only, skip unsupported chrome-extension:// and file://...
|
|
6568
6570
|
if (!url.startsWith('http'))
|
|
6569
6571
|
return;
|
|
@@ -6580,7 +6582,7 @@ self.addEventListener('fetch', function (event) {
|
|
|
6580
6582
|
function matches(paths) {
|
|
6581
6583
|
return paths &&
|
|
6582
6584
|
paths.some(path => path instanceof RegExp ?
|
|
6583
|
-
path.exec(url) : url
|
|
6585
|
+
path.exec(url) : url === path);
|
|
6584
6586
|
}
|
|
6585
6587
|
});
|
|
6586
6588
|
// Caching strategies
|
|
@@ -6628,11 +6630,12 @@ function fetchRequest(event) {
|
|
|
6628
6630
|
function fetchAndCache(event) {
|
|
6629
6631
|
return fetchRequest(event)
|
|
6630
6632
|
.then(response => {
|
|
6631
|
-
return toCache(event, response)
|
|
6632
|
-
|
|
6633
|
-
|
|
6634
|
-
})
|
|
6635
|
-
|
|
6633
|
+
return toCache(event, response)
|
|
6634
|
+
})
|
|
6635
|
+
.catch(e => {
|
|
6636
|
+
log(\` - unable to fetch \${event.request.url} \${e}\`);
|
|
6637
|
+
throw e
|
|
6638
|
+
});
|
|
6636
6639
|
}
|
|
6637
6640
|
function fromCache(event) {
|
|
6638
6641
|
return caches.open(CACHE_NAME).then(cache => cache.match(event.request));
|
|
@@ -6642,7 +6645,7 @@ function fileFromCache(name) {
|
|
|
6642
6645
|
}
|
|
6643
6646
|
function toCache(event, response) {
|
|
6644
6647
|
if (!response.ok) {
|
|
6645
|
-
log(\`request
|
|
6648
|
+
log(\`skipping cache of failed request \${event.request.url} with status \${response.status} \${response.statusText}\`);
|
|
6646
6649
|
return response;
|
|
6647
6650
|
}
|
|
6648
6651
|
return caches.open(CACHE_NAME)
|
|
@@ -6661,8 +6664,36 @@ function log(s) {
|
|
|
6661
6664
|
function regexEscape(s) {
|
|
6662
6665
|
return s.replace(/([/.])/g, '\\\\$&');
|
|
6663
6666
|
}
|
|
6667
|
+
|
|
6668
|
+
function withOrigin(path) {
|
|
6669
|
+
if (path.match(/https?:\\/\\//)) return path
|
|
6670
|
+
if (path.startsWith('/')) return self.origin + path
|
|
6671
|
+
return self.origin + '/' + path
|
|
6672
|
+
}
|
|
6664
6673
|
`;
|
|
6665
6674
|
|
|
6675
|
+
// src/utils.mts
|
|
6676
|
+
function deDup(paths) {
|
|
6677
|
+
if (!Array.isArray(paths))
|
|
6678
|
+
return paths;
|
|
6679
|
+
return [...new Set(paths)].sort();
|
|
6680
|
+
}
|
|
6681
|
+
|
|
6682
|
+
// src/organizeStrategies.mts
|
|
6683
|
+
function organizeStrategies(inputPlan) {
|
|
6684
|
+
const spec = convertAllFilesToPaths(inputPlan);
|
|
6685
|
+
const preloadPaths = deDup(extractAllPreloadPaths(spec));
|
|
6686
|
+
const specWithoutPreloads = convertPreloadPathsToCacheFirst(spec);
|
|
6687
|
+
const routable = removeDuplicatePaths(specWithoutPreloads);
|
|
6688
|
+
return { preloadPaths, routable };
|
|
6689
|
+
}
|
|
6690
|
+
function removeDuplicatePaths(specs) {
|
|
6691
|
+
return specs.map((spec) => ({
|
|
6692
|
+
...spec,
|
|
6693
|
+
paths: deDup(spec.paths)
|
|
6694
|
+
}));
|
|
6695
|
+
}
|
|
6696
|
+
|
|
6666
6697
|
// src/generate.mts
|
|
6667
6698
|
var defaultOptions = {
|
|
6668
6699
|
version: "0.0",
|
|
@@ -6674,9 +6705,7 @@ function generateServiceWorker(inputSpec, optionsIn = {}) {
|
|
|
6674
6705
|
...defaultOptions,
|
|
6675
6706
|
...optionsIn
|
|
6676
6707
|
};
|
|
6677
|
-
const
|
|
6678
|
-
const preloadPaths = extractAllPreloadPaths(spec);
|
|
6679
|
-
const routable = convertPreloadPathsToCacheFirst(spec);
|
|
6708
|
+
const { preloadPaths, routable } = organizeStrategies(inputSpec);
|
|
6680
6709
|
const varsBlock = generateFlag("DEBUG", options.debug) + generateFlag("SKIP_WAITING", options.skipWaiting) + generateVersion(options.version) + generatePreloadCode(preloadPaths) + generateRoutes(routable);
|
|
6681
6710
|
return evalTemplate(varsBlock);
|
|
6682
6711
|
}
|
|
@@ -6702,7 +6731,7 @@ function evalTemplate(variables) {
|
|
|
6702
6731
|
}
|
|
6703
6732
|
function pathsToJS(paths) {
|
|
6704
6733
|
const asArray = Array.isArray(paths) ? paths : [paths];
|
|
6705
|
-
return `[${asArray.map(pathToJS).join(",")}]`;
|
|
6734
|
+
return `[${asArray.map(pathToJS).join(",")}].map(withOrigin)`;
|
|
6706
6735
|
}
|
|
6707
6736
|
function pathToJS(path2) {
|
|
6708
6737
|
switch (typeof path2) {
|