UNPKG

8.68 kBJavaScriptView Raw
1#!/usr/bin/env node
2(function () {
3 'use strict';
4
5 /**
6 * @license
7 * Copyright Google LLC All Rights Reserved.
8 *
9 * Use of this source code is governed by an MIT-style license that can be
10 * found in the LICENSE file at https://angular.io/license
11 */
12 function sha1Binary(buffer) {
13 const words32 = arrayBufferToWords32(buffer, Endian.Big);
14 return _sha1(words32, buffer.byteLength * 8);
15 }
16 function _sha1(words32, len) {
17 const w = [];
18 let [a, b, c, d, e] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
19 words32[len >> 5] |= 0x80 << (24 - len % 32);
20 words32[((len + 64 >> 9) << 4) + 15] = len;
21 for (let i = 0; i < words32.length; i += 16) {
22 const [h0, h1, h2, h3, h4] = [a, b, c, d, e];
23 for (let j = 0; j < 80; j++) {
24 if (j < 16) {
25 w[j] = words32[i + j];
26 }
27 else {
28 w[j] = rol32(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
29 }
30 const [f, k] = fk(j, b, c, d);
31 const temp = [rol32(a, 5), f, e, k, w[j]].reduce(add32);
32 [e, d, c, b, a] = [d, c, rol32(b, 30), a, temp];
33 }
34 [a, b, c, d, e] = [add32(a, h0), add32(b, h1), add32(c, h2), add32(d, h3), add32(e, h4)];
35 }
36 return byteStringToHexString(words32ToByteString([a, b, c, d, e]));
37 }
38 function add32(a, b) {
39 return add32to64(a, b)[1];
40 }
41 function add32to64(a, b) {
42 const low = (a & 0xffff) + (b & 0xffff);
43 const high = (a >>> 16) + (b >>> 16) + (low >>> 16);
44 return [high >>> 16, (high << 16) | (low & 0xffff)];
45 }
46 // Rotate a 32b number left `count` position
47 function rol32(a, count) {
48 return (a << count) | (a >>> (32 - count));
49 }
50 var Endian = /*@__PURE__*/ (function (Endian) {
51 Endian[Endian["Little"] = 0] = "Little";
52 Endian[Endian["Big"] = 1] = "Big";
53 return Endian;
54 })({});
55 function fk(index, b, c, d) {
56 if (index < 20) {
57 return [(b & c) | (~b & d), 0x5a827999];
58 }
59 if (index < 40) {
60 return [b ^ c ^ d, 0x6ed9eba1];
61 }
62 if (index < 60) {
63 return [(b & c) | (b & d) | (c & d), 0x8f1bbcdc];
64 }
65 return [b ^ c ^ d, 0xca62c1d6];
66 }
67 function arrayBufferToWords32(buffer, endian) {
68 const size = (buffer.byteLength + 3) >>> 2;
69 const words32 = [];
70 const view = new Uint8Array(buffer);
71 for (let i = 0; i < size; i++) {
72 words32[i] = wordAt(view, i * 4, endian);
73 }
74 return words32;
75 }
76 function byteAt(str, index) {
77 if (typeof str === 'string') {
78 return index >= str.length ? 0 : str.charCodeAt(index) & 0xff;
79 }
80 else {
81 return index >= str.byteLength ? 0 : str[index] & 0xff;
82 }
83 }
84 function wordAt(str, index, endian) {
85 let word = 0;
86 if (endian === Endian.Big) {
87 for (let i = 0; i < 4; i++) {
88 word += byteAt(str, index + i) << (24 - 8 * i);
89 }
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)) & 0xff);
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 & 0x0f).toString(16);
113 }
114 return hex.toLowerCase();
115 }
116
117 /**
118 * @license
119 * Copyright Google LLC All Rights Reserved.
120 *
121 * Use of this source code is governed by an MIT-style license that can be
122 * found in the LICENSE file at https://angular.io/license
123 */
124 var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
125 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
126 return new (P || (P = Promise))(function (resolve, reject) {
127 function fulfilled(value) { try {
128 step(generator.next(value));
129 }
130 catch (e) {
131 reject(e);
132 } }
133 function rejected(value) { try {
134 step(generator["throw"](value));
135 }
136 catch (e) {
137 reject(e);
138 } }
139 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
140 step((generator = generator.apply(thisArg, _arguments || [])).next());
141 });
142 };
143 const fs = require('fs');
144 const path = require('path');
145 class NodeFilesystem {
146 constructor(base) {
147 this.base = base;
148 }
149 list(_path) {
150 return __awaiter(this, void 0, void 0, function* () {
151 const dir = this.canonical(_path);
152 const entries = fs.readdirSync(dir).map((entry) => ({ entry, stats: fs.statSync(path.join(dir, entry)) }));
153 const files = entries.filter((entry) => !entry.stats.isDirectory())
154 .map((entry) => path.posix.join(_path, entry.entry));
155 return entries.filter((entry) => entry.stats.isDirectory())
156 .map((entry) => path.posix.join(_path, entry.entry))
157 .reduce((list, subdir) => __awaiter(this, void 0, void 0, function* () { return (yield list).concat(yield this.list(subdir)); }), Promise.resolve(files));
158 });
159 }
160 read(_path) {
161 return __awaiter(this, void 0, void 0, function* () {
162 const file = this.canonical(_path);
163 return fs.readFileSync(file).toString();
164 });
165 }
166 hash(_path) {
167 return __awaiter(this, void 0, void 0, function* () {
168 const file = this.canonical(_path);
169 const contents = fs.readFileSync(file);
170 return sha1Binary(contents);
171 });
172 }
173 write(_path, contents) {
174 return __awaiter(this, void 0, void 0, function* () {
175 const file = this.canonical(_path);
176 fs.writeFileSync(file, contents);
177 });
178 }
179 canonical(_path) {
180 return path.posix.join(this.base, _path);
181 }
182 }
183
184 /**
185 * @license
186 * Copyright Google LLC All Rights Reserved.
187 *
188 * Use of this source code is governed by an MIT-style license that can be
189 * found in the LICENSE file at https://angular.io/license
190 */
191 var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
192 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
193 return new (P || (P = Promise))(function (resolve, reject) {
194 function fulfilled(value) { try {
195 step(generator.next(value));
196 }
197 catch (e) {
198 reject(e);
199 } }
200 function rejected(value) { try {
201 step(generator["throw"](value));
202 }
203 catch (e) {
204 reject(e);
205 } }
206 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
207 step((generator = generator.apply(thisArg, _arguments || [])).next());
208 });
209 };
210 const { Generator, NgswConfig } = require('@angular/service-worker/config');
211 const fs$1 = require('fs');
212 const path$1 = require('path');
213 const cwd = process.cwd();
214 const distDir = path$1.join(cwd, process.argv[2]);
215 const config = path$1.join(cwd, process.argv[3]);
216 const baseHref = process.argv[4] || '/';
217 const configParsed = JSON.parse(fs$1.readFileSync(config).toString());
218 const filesystem = new NodeFilesystem(distDir);
219 const gen = new Generator(filesystem, baseHref);
220 (() => __awaiter$1(void 0, void 0, void 0, function* () {
221 const control = yield gen.process(configParsed);
222 yield filesystem.write('/ngsw.json', JSON.stringify(control, null, 2));
223 }))();
224
225}());
226//# sourceMappingURL=ngsw_config.js.map