Permalink
Browse files

feat(service-worker): introduce the @angular/service-worker package (#…

…19274)

This service worker is a conceptual derivative of the existing @angular/service-worker maintained at github.com/angular/mobile-toolkit, but has been rewritten to support use across a much wider variety of applications.

Entrypoints include:

@angular/service-worker: a library for use within Angular client apps to communicate with the service worker.
@angular/service-worker/gen: a library for generating ngsw.json files from glob-based SW config files.
@angular/service-worker/ngsw-worker.js: the bundled service worker script itself.
@angular/service-worker/ngsw-cli.js: a CLI tool for generating ngsw.json files from glob-based SW config files.
  • Loading branch information...
alxhub authored and vicb committed Sep 28, 2017
1 parent 7c1d3e0 commit d442b6855ff114b59dd3d299110d01be84a20ae3
Showing with 6,722 additions and 8 deletions.
  1. +8 −1 build.sh
  2. +17 −0 packages/service-worker/build.sh
  3. +42 −0 packages/service-worker/cli/filesystem.ts
  4. +29 −0 packages/service-worker/cli/main.ts
  5. +21 −0 packages/service-worker/cli/rollup-cli.config.js
  6. +25 −0 packages/service-worker/cli/tsconfig.json
  7. +14 −0 packages/service-worker/config/index.ts
  8. +7 −0 packages/service-worker/config/package.json
  9. +11 −0 packages/service-worker/config/public_api.ts
  10. +23 −0 packages/service-worker/config/rollup.config.js
  11. +48 −0 packages/service-worker/config/src/duration.ts
  12. +19 −0 packages/service-worker/config/src/filesystem.ts
  13. +134 −0 packages/service-worker/config/src/generator.ts
  14. +33 −0 packages/service-worker/config/src/glob.ts
  15. +55 −0 packages/service-worker/config/src/in.ts
  16. +196 −0 packages/service-worker/config/src/sha1.ts
  17. +86 −0 packages/service-worker/config/test/generator_spec.ts
  18. +25 −0 packages/service-worker/config/testing/mock.ts
  19. +23 −0 packages/service-worker/config/tsconfig-build.json
  20. +24 −0 packages/service-worker/package.json
  21. +16 −0 packages/service-worker/public_api.ts
  22. +46 −0 packages/service-worker/rollup.config.js
  23. +19 −0 packages/service-worker/src/index.ts
  24. +180 −0 packages/service-worker/src/low_level.ts
  25. +66 −0 packages/service-worker/src/module.ts
  26. +82 −0 packages/service-worker/src/push.ts
  27. +42 −0 packages/service-worker/src/update.ts
  28. +28 −0 packages/service-worker/test/async.ts
  29. +137 −0 packages/service-worker/test/comm_spec.ts
  30. +132 −0 packages/service-worker/test/integration_spec.ts
  31. +64 −0 packages/service-worker/testing/mock.ts
  32. +24 −0 packages/service-worker/tsconfig-build.json
  33. +16 −0 packages/service-worker/worker/main.ts
  34. +13 −0 packages/service-worker/worker/rollup-worker.config.js
  35. +68 −0 packages/service-worker/worker/src/adapter.ts
  36. +109 −0 packages/service-worker/worker/src/api.ts
  37. +241 −0 packages/service-worker/worker/src/app-version.ts
  38. +556 −0 packages/service-worker/worker/src/assets.ts
  39. +541 −0 packages/service-worker/worker/src/data.ts
  40. +60 −0 packages/service-worker/worker/src/database.ts
  41. +69 −0 packages/service-worker/worker/src/db-cache.ts
  42. +68 −0 packages/service-worker/worker/src/debug.ts
  43. +858 −0 packages/service-worker/worker/src/driver.ts
  44. +89 −0 packages/service-worker/worker/src/idle.ts
  45. +43 −0 packages/service-worker/worker/src/manifest.ts
  46. +36 −0 packages/service-worker/worker/src/msg.ts
  47. +148 −0 packages/service-worker/worker/src/service-worker.d.ts
  48. +196 −0 packages/service-worker/worker/src/sha1.ts
  49. +40 −0 packages/service-worker/worker/src/util.ts
  50. +28 −0 packages/service-worker/worker/test/async.ts
  51. +265 −0 packages/service-worker/worker/test/data_spec.ts
  52. +601 −0 packages/service-worker/worker/test/happy_spec.ts
  53. +91 −0 packages/service-worker/worker/test/prefetch_spec.ts
  54. +158 −0 packages/service-worker/worker/testing/cache.ts
  55. +128 −0 packages/service-worker/worker/testing/fetch.ts
  56. +196 −0 packages/service-worker/worker/testing/mock.ts
  57. +322 −0 packages/service-worker/worker/testing/scope.ts
  58. +22 −0 packages/service-worker/worker/tsconfig.json
  59. +2 −1 tools/gulp-tasks/public-api.js
  60. +52 −0 tools/public_api_guard/service-worker/config.d.ts
  61. +24 −0 tools/public_api_guard/service-worker/service-worker.d.ts
  62. +1 −1 tools/validate-commit-message/commit-message.json
  63. +5 −5 tools/validate-commit-message/validate-commit-message.spec.js
View
@@ -23,7 +23,8 @@ PACKAGES=(core
router
compiler-cli
language-service
benchpress)
benchpress
service-worker)
TSC_PACKAGES=(compiler-cli
language-service
@@ -235,6 +236,7 @@ compilePackage() {
fi
fi
# Build subpackages
for DIR in ${1}/* ; do
[ -d "${DIR}" ] || continue
BASE_DIR=$(basename "${DIR}")
@@ -465,6 +467,11 @@ do
addBanners ${BUNDLES_DIR}
minify ${BUNDLES_DIR}
if [[ -e ${SRC_DIR}/build.sh ]]; then
echo "====== Custom build for ${PACKAGE}"
cd ${SRC_DIR} && ${SRC_DIR}/build.sh
fi
) 2>&1 | grep -v "as external dependency"
if [[ ${PACKAGE} == "common" ]]; then
@@ -0,0 +1,17 @@
#!/bin/bash
set -u -e -o pipefail
BIN=$(cd .. && npm bin)
$BIN/tsc -p worker/tsconfig.json
$BIN/rollup -c worker/rollup-worker.config.js
$BIN/tsc -p cli/tsconfig.json
$BIN/rollup -c cli/rollup-cli.config.js
echo "#!/usr/bin/env node" > ../../dist/packages-dist/service-worker/ngsw-config.js
cat ../../dist/packages-dist/service-worker/ngsw-config-tmp.js >> ../../dist/packages-dist/service-worker/ngsw-config.js
rm ../../dist/packages-dist/service-worker/ngsw-config-tmp.js
@@ -0,0 +1,42 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Filesystem} from '@angular/service-worker/config';
const fs = require('fs');
const path = require('path');
export class NodeFilesystem implements Filesystem {
constructor(private base: string) {}
async list(_path: string): Promise<string[]> {
const dir = this.canonical(_path);
const entries = fs.readdirSync(dir).map(
(entry: string) => ({entry, stats: fs.statSync(path.join(dir, entry))}));
const files = entries.filter((entry: any) => !entry.stats.isDirectory())
.map((entry: any) => path.join(_path, entry.entry));
return entries.filter((entry: any) => entry.stats.isDirectory())
.map((entry: any) => path.join(_path, entry.entry))
.reduce(
async(list: string[], subdir: string) => (await list).concat(await this.list(subdir)),
Promise.resolve(files));
}
async read(_path: string): Promise<string> {
const file = this.canonical(_path);
return fs.readFileSync(file).toString();
}
async write(_path: string, contents: string): Promise<void> {
const file = this.canonical(_path);
fs.writeFileSync(file, contents);
}
private canonical(_path: string): string { return path.join(this.base, _path); }
}
@@ -0,0 +1,29 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
const {Generator, NgswConfig} = require('@angular/service-worker/config');
const fs = require('fs');
const path = require('path');
import {NodeFilesystem} from './filesystem';
const cwd = process.cwd();
const distDir = path.join(cwd, process.argv[2]);
const config = path.join(cwd, process.argv[3]);
const baseHref = process.argv[4] || '/';
const configParsed = JSON.parse(fs.readFileSync(config).toString());
const filesystem = new NodeFilesystem(distDir);
const gen = new Generator(filesystem, baseHref);
(async() => {
const control = await gen.process(configParsed);
await filesystem.write('/ngsw.json', JSON.stringify(control, null, 2));
})();
@@ -0,0 +1,21 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import resolve from 'rollup-plugin-node-resolve';
export default {
entry: '../../dist/all/@angular/service-worker/cli-custom/main.js',
dest: '../../dist/packages-dist/service-worker/ngsw-config-tmp.js',
format: 'iife',
plugins: [resolve()],
external: [
'fs',
'path',
'@angular/service-worker/config',
],
};
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"baseUrl": ".",
"declaration": false,
"strict": true,
"module": "es2015",
"moduleResolution": "node",
"strictNullChecks": true,
"outDir": "../../../dist/all/@angular/service-worker/cli-custom",
"noImplicitAny": true,
"noFallthroughCasesInSwitch": true,
"rootDir": ".",
"paths": {
"@angular/service-worker/config": ["../../../dist/packages/service-worker/config"]
},
"inlineSourceMap": true,
"lib": ["es2015"],
"target": "es5",
"typeRoots": []
},
"files": [
"main.ts",
"../../../node_modules/@types/node/index.d.ts"
]
}
@@ -0,0 +1,14 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// This file is not used to build this module. It is only used during editing
// by the TypeScript language service and during build for verification. `ngc`
// replaces this file with production index.ts when it rewrites private symbol
// names.
export * from './public_api';
@@ -0,0 +1,7 @@
{
"name": "@angular/service-worker/config",
"typings": "./index.d.ts",
"main": "../bundles/service-worker-config.umd.js",
"module": "../esm5/config/index.js",
"es2015": "../esm15/config/index.js"
}
@@ -0,0 +1,11 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
export {Filesystem} from './src/filesystem';
export {Generator} from './src/generator';
export {AssetGroup, Config, DataGroup, Duration, Glob} from './src/in';
@@ -0,0 +1,23 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import resolve from 'rollup-plugin-node-resolve';
import sourcemaps from 'rollup-plugin-sourcemaps';
const globals = {};
export default {
entry: '../../../dist/packages-dist/service-worker/esm5/config.js',
dest: '../../../dist/packages-dist/service-worker/bundles/service-worker-config.umd.js',
format: 'umd',
exports: 'named',
moduleName: 'ng.serviceWorker.config',
plugins: [resolve(), sourcemaps()],
external: Object.keys(globals),
globals: globals
};
@@ -0,0 +1,48 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
const PARSE_TO_PAIRS = /([0-9]+[^0-9]+)/g;
const PAIR_SPLIT = /^([0-9]+)([dhmsu]+)$/;
export function parseDurationToMs(duration: string): number {
const matches: string[] = [];
let array: RegExpExecArray|null;
while ((array = PARSE_TO_PAIRS.exec(duration)) !== null) {
matches.push(array[0]);
}
return matches
.map(match => {
const res = PAIR_SPLIT.exec(match);
if (res === null) {
throw new Error(`Not a valid duration: ${match}`);
}
let factor: number = 0;
switch (res[2]) {
case 'd':
factor = 86400000;
break;
case 'h':
factor = 3600000;
break;
case 'm':
factor = 60000;
break;
case 's':
factor = 1000;
break;
case 'u':
factor = 1;
break;
default:
throw new Error(`Not a valid duration unit: ${res[2]}`);
}
return parseInt(res[1]) * factor;
})
.reduce((total, value) => total + value, 0);
}
@@ -0,0 +1,19 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* An abstraction over a virtual file system used to enable testing and operation
* of the config generator in different environments.
*
* @experimental
*/
export interface Filesystem {
list(dir: string): Promise<string[]>;
read(file: string): Promise<string>;
write(file: string, contents: string): Promise<void>;
}
Oops, something went wrong.

0 comments on commit d442b68

Please sign in to comment.