UNPKG

11.6 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.AsarPackager = void 0;
7
8function _bluebirdLst() {
9 const data = require("bluebird-lst");
10
11 _bluebirdLst = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _builderUtil() {
19 const data = require("builder-util");
20
21 _builderUtil = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _fs() {
29 const data = require("builder-util/out/fs");
30
31 _fs = function () {
32 return data;
33 };
34
35 return data;
36}
37
38function _fs2() {
39 const data = require("fs");
40
41 _fs2 = function () {
42 return data;
43 };
44
45 return data;
46}
47
48function _fsExtraP() {
49 const data = require("fs-extra-p");
50
51 _fsExtraP = function () {
52 return data;
53 };
54
55 return data;
56}
57
58var path = _interopRequireWildcard(require("path"));
59
60function _appFileCopier() {
61 const data = require("../util/appFileCopier");
62
63 _appFileCopier = function () {
64 return data;
65 };
66
67 return data;
68}
69
70function _asar() {
71 const data = require("./asar");
72
73 _asar = function () {
74 return data;
75 };
76
77 return data;
78}
79
80function _unpackDetector() {
81 const data = require("./unpackDetector");
82
83 _unpackDetector = function () {
84 return data;
85 };
86
87 return data;
88}
89
90let order = (() => {
91 var _ref2 = (0, _bluebirdLst().coroutine)(function* (filenames, orderingFile, src) {
92 const orderingFiles = (yield (0, _fsExtraP().readFile)(orderingFile, "utf8")).split("\n").map(line => {
93 if (line.indexOf(":") !== -1) {
94 line = line.split(":").pop();
95 }
96
97 line = line.trim();
98
99 if (line[0] === "/") {
100 line = line.slice(1);
101 }
102
103 return line;
104 });
105 const ordering = [];
106
107 for (const file of orderingFiles) {
108 const pathComponents = file.split(path.sep);
109
110 for (const pathComponent of pathComponents) {
111 ordering.push(path.join(src, pathComponent));
112 }
113 }
114
115 const sortedFiles = [];
116 let missing = 0;
117 const total = filenames.length;
118
119 for (const file of ordering) {
120 if (!sortedFiles.includes(file) && filenames.includes(file)) {
121 sortedFiles.push(file);
122 }
123 }
124
125 for (const file of filenames) {
126 if (!sortedFiles.includes(file)) {
127 sortedFiles.push(file);
128 missing += 1;
129 }
130 }
131
132 _builderUtil().log.info({
133 coverage: (total - missing) / total * 100
134 }, "ordering files in ASAR archive");
135
136 return sortedFiles;
137 });
138
139 return function order(_x3, _x4, _x5) {
140 return _ref2.apply(this, arguments);
141 };
142})();
143
144function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
145
146const pickle = require("chromium-pickle-js");
147/** @internal */
148
149
150class AsarPackager {
151 constructor(src, destination, options, unpackPattern) {
152 this.src = src;
153 this.destination = destination;
154 this.options = options;
155 this.unpackPattern = unpackPattern;
156 this.fs = new (_asar().AsarFilesystem)(this.src);
157 this.outFile = path.join(destination, "app.asar");
158 this.unpackedDest = `${this.outFile}.unpacked`;
159 } // sort files to minimize file change (i.e. asar file is not changed dramatically on small change)
160
161
162 pack(fileSets, packager) {
163 var _this = this;
164
165 return (0, _bluebirdLst().coroutine)(function* () {
166 if (_this.options.ordering != null) {
167 // ordering doesn't support transformed files, but ordering is not used functionality - wait user report to fix it
168 yield order(fileSets[0].files, _this.options.ordering, fileSets[0].src);
169 }
170
171 yield (0, _fsExtraP().ensureDir)(path.dirname(_this.outFile));
172 const unpackedFileIndexMap = new Map();
173
174 for (const fileSet of fileSets) {
175 unpackedFileIndexMap.set(fileSet, (yield _this.createPackageFromFiles(fileSet, packager.info)));
176 }
177
178 yield _this.writeAsarFile(fileSets, unpackedFileIndexMap);
179 })();
180 }
181
182 createPackageFromFiles(fileSet, packager) {
183 var _this2 = this;
184
185 return (0, _bluebirdLst().coroutine)(function* () {
186 const metadata = fileSet.metadata; // search auto unpacked dir
187
188 const unpackedDirs = new Set();
189 const rootForAppFilesWithoutAsar = path.join(_this2.destination, "app");
190
191 if (_this2.options.smartUnpack !== false) {
192 yield (0, _unpackDetector().detectUnpackedDirs)(fileSet, unpackedDirs, _this2.unpackedDest, rootForAppFilesWithoutAsar);
193 }
194
195 const dirToCreateForUnpackedFiles = new Set(unpackedDirs);
196
197 const correctDirNodeUnpackedFlag = (() => {
198 var _ref = (0, _bluebirdLst().coroutine)(function* (filePathInArchive, dirNode) {
199 for (const dir of unpackedDirs) {
200 if (filePathInArchive.length > dir.length + 2 && filePathInArchive[dir.length] === path.sep && filePathInArchive.startsWith(dir)) {
201 dirNode.unpacked = true;
202 unpackedDirs.add(filePathInArchive); // not all dirs marked as unpacked after first iteration - because node module dir can be marked as unpacked after processing node module dir content
203 // e.g. node-notifier/example/advanced.js processed, but only on process vendor/terminal-notifier.app module will be marked as unpacked
204
205 yield (0, _fsExtraP().ensureDir)(path.join(_this2.unpackedDest, filePathInArchive));
206 break;
207 }
208 }
209 });
210
211 return function correctDirNodeUnpackedFlag(_x, _x2) {
212 return _ref.apply(this, arguments);
213 };
214 })();
215
216 const transformedFiles = fileSet.transformedFiles;
217 const taskManager = new (_builderUtil().AsyncTaskManager)(packager.cancellationToken);
218 const fileCopier = new (_fs().FileCopier)();
219 let currentDirNode = null;
220 let currentDirPath = null;
221 const unpackedFileIndexSet = new Set();
222
223 for (let i = 0, n = fileSet.files.length; i < n; i++) {
224 const file = fileSet.files[i];
225 const stat = metadata.get(file);
226
227 if (stat == null) {
228 continue;
229 }
230
231 const pathInArchive = path.relative(rootForAppFilesWithoutAsar, (0, _appFileCopier().getDestinationPath)(file, fileSet));
232
233 if (stat.isSymbolicLink()) {
234 const s = stat;
235 _this2.fs.getOrCreateNode(pathInArchive).link = s.relativeLink;
236 s.pathInArchive = pathInArchive;
237 unpackedFileIndexSet.add(i);
238 continue;
239 }
240
241 let fileParent = path.dirname(pathInArchive);
242
243 if (fileParent === ".") {
244 fileParent = "";
245 }
246
247 if (currentDirPath !== fileParent) {
248 if (fileParent.startsWith("..")) {
249 throw new Error(`Internal error: path must not start with "..": ${fileParent}`);
250 }
251
252 currentDirPath = fileParent;
253 currentDirNode = _this2.fs.getOrCreateNode(fileParent); // do not check for root
254
255 if (fileParent !== "" && !currentDirNode.unpacked) {
256 if (unpackedDirs.has(fileParent)) {
257 currentDirNode.unpacked = true;
258 } else {
259 yield correctDirNodeUnpackedFlag(fileParent, currentDirNode);
260 }
261 }
262 }
263
264 const dirNode = currentDirNode;
265 const newData = transformedFiles == null ? null : transformedFiles.get(i);
266
267 const isUnpacked = dirNode.unpacked || _this2.unpackPattern != null && _this2.unpackPattern(file, stat);
268
269 _this2.fs.addFileNode(file, dirNode, newData == null ? stat.size : Buffer.byteLength(newData), isUnpacked, stat);
270
271 if (isUnpacked) {
272 if (!dirNode.unpacked && !dirToCreateForUnpackedFiles.has(fileParent)) {
273 dirToCreateForUnpackedFiles.add(fileParent);
274 yield (0, _fsExtraP().ensureDir)(path.join(_this2.unpackedDest, fileParent));
275 }
276
277 const unpackedFile = path.join(_this2.unpackedDest, pathInArchive);
278 taskManager.addTask(copyFileOrData(fileCopier, newData, file, unpackedFile, stat));
279
280 if (taskManager.tasks.length > _fs().MAX_FILE_REQUESTS) {
281 yield taskManager.awaitTasks();
282 }
283
284 unpackedFileIndexSet.add(i);
285 }
286 }
287
288 if (taskManager.tasks.length > 0) {
289 yield taskManager.awaitTasks();
290 }
291
292 return unpackedFileIndexSet;
293 })();
294 }
295
296 writeAsarFile(fileSets, unpackedFileIndexMap) {
297 return new Promise((resolve, reject) => {
298 const headerPickle = pickle.createEmpty();
299 headerPickle.writeString(JSON.stringify(this.fs.header));
300 const headerBuf = headerPickle.toBuffer();
301 const sizePickle = pickle.createEmpty();
302 sizePickle.writeUInt32(headerBuf.length);
303 const sizeBuf = sizePickle.toBuffer();
304 const writeStream = (0, _fsExtraP().createWriteStream)(this.outFile);
305 writeStream.on("error", reject);
306 writeStream.on("close", resolve);
307 writeStream.write(sizeBuf);
308 let fileSetIndex = 0;
309 let files = fileSets[0].files;
310 let metadata = fileSets[0].metadata;
311 let transformedFiles = fileSets[0].transformedFiles;
312 let unpackedFileIndexSet = unpackedFileIndexMap.get(fileSets[0]);
313
314 const w = index => {
315 while (true) {
316 if (index >= files.length) {
317 if (++fileSetIndex >= fileSets.length) {
318 writeStream.end();
319 return;
320 } else {
321 files = fileSets[fileSetIndex].files;
322 metadata = fileSets[fileSetIndex].metadata;
323 transformedFiles = fileSets[fileSetIndex].transformedFiles;
324 unpackedFileIndexSet = unpackedFileIndexMap.get(fileSets[fileSetIndex]);
325 index = 0;
326 }
327 }
328
329 if (!unpackedFileIndexSet.has(index)) {
330 break;
331 } else {
332 const stat = metadata.get(files[index]);
333
334 if (stat != null && stat.isSymbolicLink()) {
335 (0, _fs2().symlink)(stat.linkRelativeToFile, path.join(this.unpackedDest, stat.pathInArchive), () => w(index + 1));
336 return;
337 }
338 }
339
340 index++;
341 }
342
343 const data = transformedFiles == null ? null : transformedFiles.get(index);
344 const file = files[index];
345
346 if (data !== null && data !== undefined) {
347 writeStream.write(data, () => w(index + 1));
348 return;
349 } // https://github.com/yarnpkg/yarn/pull/3539
350
351
352 const stat = metadata.get(file);
353
354 if (stat != null && stat.size < 2 * 1024 * 1024) {
355 (0, _fsExtraP().readFile)(file).then(it => {
356 writeStream.write(it, () => w(index + 1));
357 }).catch(e => reject(`Cannot read file ${file}: ${e.stack || e}`));
358 } else {
359 const readStream = (0, _fsExtraP().createReadStream)(file);
360 readStream.on("error", reject);
361 readStream.once("end", () => w(index + 1));
362 readStream.pipe(writeStream, {
363 end: false
364 });
365 }
366 };
367
368 writeStream.write(headerBuf, () => w(0));
369 });
370 }
371
372}
373
374exports.AsarPackager = AsarPackager;
375
376function copyFileOrData(fileCopier, data, source, destination, stats) {
377 if (data == null) {
378 return fileCopier.copy(source, destination, stats);
379 } else {
380 return (0, _fsExtraP().writeFile)(destination, data);
381 }
382}
383//# sourceMappingURL=asarUtil.js.map
\No newline at end of file