Beispiel #1
0
function doDownload(url, destination, redirectCount, options, callback) {
    const ensureDirPromise = options.skipDirCreation ? bluebird_1.Promise.resolve() : fs_extra_p_1.ensureDir(path.dirname(destination));
    const parsedUrl = url_1.parse(url);
    // user-agent must be specified, otherwise some host can return 401 unauthorised
    const request = https.request({
        hostname: parsedUrl.hostname,
        path: parsedUrl.path,
        headers: {
            "User-Agent": "electron-builder"
        }
    }, response => {
        if (response.statusCode >= 400) {
            callback(new Error(`Cannot download "${ url }", status ${ response.statusCode }: ${ response.statusMessage }`));
            return;
        }
        const redirectUrl = response.headers.location;
        if (redirectUrl != null) {
            if (redirectCount < maxRedirects) {
                doDownload(redirectUrl, destination, redirectCount++, options, callback);
            } else {
                callback(new Error("Too many redirects (> " + maxRedirects + ")"));
            }
            return;
        }
        const sha2Header = response.headers["X-Checksum-Sha2"];
        if (sha2Header != null && options.sha2 != null) {
            // todo why bintray doesn't send this header always
            if (sha2Header == null) {
                throw new Error("checksum is required, but server response doesn't contain X-Checksum-Sha2 header");
            } else if (sha2Header !== options.sha2) {
                throw new Error(`checksum mismatch: expected ${ options.sha2 } but got ${ sha2Header } (X-Checksum-Sha2 header)`);
            }
        }
        ensureDirPromise.then(() => {
            const fileOut = fs_extra_p_1.createWriteStream(destination);
            if (options.sha2 == null) {
                response.pipe(fileOut);
            } else {
                response.pipe(new DigestTransform(options.sha2)).pipe(fileOut);
            }
            fileOut.on("finish", callback);
        }).catch(callback);
        let ended = false;
        response.on("end", () => {
            ended = true;
        });
        response.on("close", () => {
            if (!ended) {
                callback(new Error("Request aborted"));
            }
        });
    });
    addTimeOutHandler(request, callback);
    request.on("error", callback);
    request.end();
}
Beispiel #2
0
 return __awaiter(this, void 0, void 0, function* () {
     // search auto unpacked dir
     const autoUnpackDirs = new Set();
     const createDirPromises = [fs_extra_p_1.ensureDir(path.dirname(dest))];
     const unpackedDest = `${ dest }.unpacked`;
     const changedFiles = new Map();
     const fileIndexToModulePackageData = new Array(files.length);
     if (options.smartUnpack !== false) {
         yield detectUnpackedDirs(src, files, metadata, autoUnpackDirs, createDirPromises, unpackedDest, fileIndexToModulePackageData);
     }
     const unpackDir = options.unpackDir == null ? null : new minimatch_1.Minimatch(options.unpackDir);
     const unpack = options.unpack == null ? null : new minimatch_1.Minimatch(options.unpack, {
         matchBase: true
     });
     const toPack = [];
     const filesystem = new Filesystem(src);
     const copyPromises = [];
     const mainPackageJson = path.join(src, "package.json");
     for (let i = 0, n = files.length; i < n; i++) {
         const file = files[i];
         const stat = metadata.get(file);
         if (stat.isFile()) {
             const fileParent = path.dirname(file);
             const dirNode = filesystem.searchNodeFromPath(fileParent);
             if (dirNode.unpacked && createDirPromises.length > 0) {
                 yield bluebird_1.Promise.all(createDirPromises);
                 createDirPromises.length = 0;
             }
             const packageDataPromise = fileIndexToModulePackageData[i];
             let newData = null;
             if (packageDataPromise == null) {
                 if (options.extraMetadata != null && file === mainPackageJson) {
                     newData = JSON.stringify(deepAssign_1.deepAssign((yield fs_extra_p_1.readJson(file)), options.extraMetadata), null, 2);
                 }
             } else {
                 newData = cleanupPackageJson(packageDataPromise.value());
             }
             const fileSize = newData == null ? stat.size : Buffer.byteLength(newData);
             const node = filesystem.searchNodeFromPath(file);
             node.size = fileSize;
             if (dirNode.unpacked || unpack != null && unpack.match(file)) {
                 node.unpacked = true;
                 if (!dirNode.unpacked) {
                     createDirPromises.push(fs_extra_p_1.ensureDir(path.join(unpackedDest, path.relative(src, fileParent))));
                     yield bluebird_1.Promise.all(createDirPromises);
                     createDirPromises.length = 0;
                 }
                 const unpackedFile = path.join(unpackedDest, path.relative(src, file));
                 copyPromises.push(newData == null ? copyFile(file, unpackedFile, stat) : fs_extra_p_1.writeFile(unpackedFile, newData));
                 if (copyPromises.length > MAX_FILE_REQUESTS) {
                     yield bluebird_1.Promise.all(copyPromises);
                     copyPromises.length = 0;
                 }
             } else {
                 if (newData != null) {
                     changedFiles.set(file, newData);
                 }
                 if (fileSize > 4294967295) {
                     throw new Error(`${ file }: file size can not be larger than 4.2GB`);
                 }
                 node.offset = filesystem.offset.toString();
                 //noinspection JSBitwiseOperatorUsage
                 if (process.platform !== "win32" && stat.mode & 0x40) {
                     node.executable = true;
                 }
                 toPack.push(file);
                 filesystem.offset.add(UINT64(fileSize));
             }
         } else if (stat.isDirectory()) {
             let unpacked = false;
             if (autoUnpackDirs.has(file)) {
                 unpacked = true;
             } else {
                 unpacked = unpackDir != null && isUnpackDir(path.relative(src, file), unpackDir, options.unpackDir);
                 if (unpacked) {
                     createDirPromises.push(fs_extra_p_1.ensureDir(path.join(unpackedDest, path.relative(src, file))));
                 } else {
                     for (let d of autoUnpackDirs) {
                         if (file.length > d.length + 2 && file[d.length] === path.sep && file.startsWith(d)) {
                             unpacked = true;
                             autoUnpackDirs.add(file);
                             // not all dirs marked as unpacked after first iteration - because node module dir can be marked as unpacked after processing node module dir content
                             // e.g. node-notifier/example/advanced.js processed, but only on process vendor/terminal-notifier.app module will be marked as unpacked
                             createDirPromises.push(fs_extra_p_1.ensureDir(path.join(unpackedDest, path.relative(src, file))));
                             break;
                         }
                     }
                 }
             }
             filesystem.insertDirectory(file, unpacked);
         } else if (stat.isSymbolicLink()) {
             filesystem.insertLink(file, stat);
         }
     }
     yield bluebird_1.Promise.all(copyPromises);
     yield writeAsarFile(filesystem, dest, toPack, changedFiles);
 });
Beispiel #3
0
 return __awaiter(this, void 0, void 0, function* () {
     const packageJsonStringLength = "package.json".length;
     const readPackageJsonPromises = [];
     for (let i = 0, n = files.length; i < n; i++) {
         const file = files[i];
         const index = file.lastIndexOf(NODE_MODULES_PATTERN);
         if (index < 0) {
             continue;
         }
         const nextSlashIndex = file.indexOf(path.sep, index + NODE_MODULES_PATTERN.length + 1);
         if (nextSlashIndex < 0) {
             continue;
         }
         if (!metadata.get(file).isFile()) {
             continue;
         }
         const nodeModuleDir = file.substring(0, nextSlashIndex);
         if (file.length === nodeModuleDir.length + 1 + packageJsonStringLength && file.endsWith("package.json")) {
             const promise = fs_extra_p_1.readJson(file);
             if (readPackageJsonPromises.length > MAX_FILE_REQUESTS) {
                 yield bluebird_1.Promise.all(readPackageJsonPromises);
                 readPackageJsonPromises.length = 0;
             }
             readPackageJsonPromises.push(promise);
             fileIndexToModulePackageData[i] = promise;
         }
         if (autoUnpackDirs.has(nodeModuleDir)) {
             const fileParent = path.dirname(file);
             if (fileParent !== nodeModuleDir && !autoUnpackDirs.has(fileParent)) {
                 autoUnpackDirs.add(fileParent);
                 if (createDirPromises.length > MAX_FILE_REQUESTS) {
                     yield bluebird_1.Promise.all(createDirPromises);
                     createDirPromises.length = 0;
                 }
                 createDirPromises.push(fs_extra_p_1.ensureDir(path.join(unpackedDest, path.relative(src, fileParent))));
             }
             continue;
         }
         const ext = path.extname(file);
         let shouldUnpack = false;
         if (ext === ".dll" || ext === ".exe") {
             shouldUnpack = true;
         } else if (ext === "") {
             shouldUnpack = yield isBinaryFile(file);
         }
         if (!shouldUnpack) {
             continue;
         }
         log_1.log(`${ path.relative(src, nodeModuleDir) } is not packed into asar archive - contains executable code`);
         autoUnpackDirs.add(nodeModuleDir);
         const fileParent = path.dirname(file);
         if (fileParent !== nodeModuleDir) {
             autoUnpackDirs.add(fileParent);
             // create parent dir to be able to copy file later without directory existence check
             createDirPromises.push(fs_extra_p_1.ensureDir(path.join(unpackedDest, path.relative(src, fileParent))));
         }
     }
     if (readPackageJsonPromises.length > 0) {
         yield bluebird_1.Promise.all(readPackageJsonPromises);
     }
     if (createDirPromises.length > 0) {
         yield bluebird_1.Promise.all(createDirPromises);
         createDirPromises.length = 0;
     }
 });
	this.dirInit = Q.async(function*(){
		const dir = this.options.dir;
		yield pfs.ensureDir(dir);
		this.options.dir = yield pfs.realpath(dir);
	}).call(this);