1 | #!/usr/bin/env node
|
2 |
|
3 | import {createRequire as __cjsCompatRequire} from 'module';
|
4 | const require = __cjsCompatRequire(import.meta.url);
|
5 |
|
6 |
|
7 | import { Generator } from "@angular/service-worker/config";
|
8 | import * as fs2 from "fs";
|
9 | import * as path2 from "path";
|
10 |
|
11 |
|
12 | import * as fs from "fs";
|
13 | import * as path from "path";
|
14 |
|
15 |
|
16 | function sha1Binary(buffer) {
|
17 | const words32 = arrayBufferToWords32(buffer, Endian.Big);
|
18 | return _sha1(words32, buffer.byteLength * 8);
|
19 | }
|
20 | function _sha1(words32, len) {
|
21 | const w = [];
|
22 | let [a, b, c, d, e] = [1732584193, 4023233417, 2562383102, 271733878, 3285377520];
|
23 | words32[len >> 5] |= 128 << 24 - len % 32;
|
24 | words32[(len + 64 >> 9 << 4) + 15] = len;
|
25 | for (let i = 0; i < words32.length; i += 16) {
|
26 | const [h0, h1, h2, h3, h4] = [a, b, c, d, e];
|
27 | for (let j = 0; j < 80; j++) {
|
28 | if (j < 16) {
|
29 | w[j] = words32[i + j];
|
30 | } else {
|
31 | w[j] = rol32(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
|
32 | }
|
33 | const [f, k] = fk(j, b, c, d);
|
34 | const temp = [rol32(a, 5), f, e, k, w[j]].reduce(add32);
|
35 | [e, d, c, b, a] = [d, c, rol32(b, 30), a, temp];
|
36 | }
|
37 | [a, b, c, d, e] = [add32(a, h0), add32(b, h1), add32(c, h2), add32(d, h3), add32(e, h4)];
|
38 | }
|
39 | return byteStringToHexString(words32ToByteString([a, b, c, d, e]));
|
40 | }
|
41 | function add32(a, b) {
|
42 | return add32to64(a, b)[1];
|
43 | }
|
44 | function add32to64(a, b) {
|
45 | const low = (a & 65535) + (b & 65535);
|
46 | const high = (a >>> 16) + (b >>> 16) + (low >>> 16);
|
47 | return [high >>> 16, high << 16 | low & 65535];
|
48 | }
|
49 | function rol32(a, count) {
|
50 | return a << count | a >>> 32 - count;
|
51 | }
|
52 | var Endian;
|
53 | (function(Endian2) {
|
54 | Endian2[Endian2["Little"] = 0] = "Little";
|
55 | Endian2[Endian2["Big"] = 1] = "Big";
|
56 | })(Endian || (Endian = {}));
|
57 | function fk(index, b, c, d) {
|
58 | if (index < 20) {
|
59 | return [b & c | ~b & d, 1518500249];
|
60 | }
|
61 | if (index < 40) {
|
62 | return [b ^ c ^ d, 1859775393];
|
63 | }
|
64 | if (index < 60) {
|
65 | return [b & c | b & d | c & d, 2400959708];
|
66 | }
|
67 | return [b ^ c ^ d, 3395469782];
|
68 | }
|
69 | function arrayBufferToWords32(buffer, endian) {
|
70 | const size = buffer.byteLength + 3 >>> 2;
|
71 | const words32 = [];
|
72 | const view = new Uint8Array(buffer);
|
73 | for (let i = 0; i < size; i++) {
|
74 | words32[i] = wordAt(view, i * 4, endian);
|
75 | }
|
76 | return words32;
|
77 | }
|
78 | function byteAt(str, index) {
|
79 | if (typeof str === "string") {
|
80 | return index >= str.length ? 0 : str.charCodeAt(index) & 255;
|
81 | } else {
|
82 | return index >= str.byteLength ? 0 : str[index] & 255;
|
83 | }
|
84 | }
|
85 | function wordAt(str, index, endian) {
|
86 | let word = 0;
|
87 | if (endian === Endian.Big) {
|
88 | for (let i = 0; i < 4; i++) {
|
89 | word += byteAt(str, index + i) << 24 - 8 * i;
|
90 | }
|
91 | } else {
|
92 | for (let i = 0; i < 4; i++) {
|
93 | word += byteAt(str, index + i) << 8 * i;
|
94 | }
|
95 | }
|
96 | return word;
|
97 | }
|
98 | function words32ToByteString(words32) {
|
99 | return words32.reduce((str, word) => str + word32ToByteString(word), "");
|
100 | }
|
101 | function word32ToByteString(word) {
|
102 | let str = "";
|
103 | for (let i = 0; i < 4; i++) {
|
104 | str += String.fromCharCode(word >>> 8 * (3 - i) & 255);
|
105 | }
|
106 | return str;
|
107 | }
|
108 | function byteStringToHexString(str) {
|
109 | let hex = "";
|
110 | for (let i = 0; i < str.length; i++) {
|
111 | const b = byteAt(str, i);
|
112 | hex += (b >>> 4).toString(16) + (b & 15).toString(16);
|
113 | }
|
114 | return hex.toLowerCase();
|
115 | }
|
116 |
|
117 |
|
118 | var NodeFilesystem = class {
|
119 | constructor(base) {
|
120 | this.base = base;
|
121 | }
|
122 | async list(_path) {
|
123 | const dir = this.canonical(_path);
|
124 | const entries = fs.readdirSync(dir).map((entry) => ({ entry, stats: fs.statSync(path.join(dir, entry)) }));
|
125 | const files = entries.filter((entry) => !entry.stats.isDirectory()).map((entry) => path.posix.join(_path, entry.entry));
|
126 | return entries.filter((entry) => entry.stats.isDirectory()).map((entry) => path.posix.join(_path, entry.entry)).reduce(async (list, subdir) => (await list).concat(await this.list(subdir)), Promise.resolve(files));
|
127 | }
|
128 | async read(_path) {
|
129 | const file = this.canonical(_path);
|
130 | return fs.readFileSync(file).toString();
|
131 | }
|
132 | async hash(_path) {
|
133 | const file = this.canonical(_path);
|
134 | const contents = fs.readFileSync(file);
|
135 | return sha1Binary(contents);
|
136 | }
|
137 | async write(_path, contents) {
|
138 | const file = this.canonical(_path);
|
139 | fs.writeFileSync(file, contents);
|
140 | }
|
141 | canonical(_path) {
|
142 | return path.posix.join(this.base, _path);
|
143 | }
|
144 | };
|
145 |
|
146 |
|
147 | var cwd = process.cwd();
|
148 | var distDir = path2.join(cwd, process.argv[2]);
|
149 | var config = path2.join(cwd, process.argv[3]);
|
150 | var baseHref = process.argv[4] || "/";
|
151 | var configParsed = JSON.parse(fs2.readFileSync(config).toString());
|
152 | var filesystem = new NodeFilesystem(distDir);
|
153 | var gen = new Generator(filesystem, baseHref);
|
154 | (async () => {
|
155 | const control = await gen.process(configParsed);
|
156 | await filesystem.write("/ngsw.json", JSON.stringify(control, null, 2));
|
157 | })();
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|