extract.on('finish', () => { resolve() })
downloadExpandNodeSource() { const url = `https://nodejs.org/dist/v${this.version}/node-v${this.version}.tar.gz`; if(fs.existsSync(this.nodePath('configure'))) { log(`node version=${this.version} already downloaded and expanded, using it`); return Promise.resolve(); } return download(url, this.nodeSrcFile) .then(() => new Promise((resolve, reject) => { log(`expanding node source, file=${this.nodeSrcFile} ...`); fs.createReadStream(this.nodeSrcFile) .pipe(createGunzip()) .pipe(tar.extract(dirname(this.nodeSrcFile))) .on('error', reject) .on('finish', resolve) }) ); }
/** * Unpack the tarball into given directory. * * Used from https://github.com/ywangii/await-targz under MIT. * * @param {String} tarball Path for tarball to be extracted * @param {String} dir Path for the file to be extracted * @returns {Promise} A promise represents if unpack tarball succeeds or fails * @public */ async unpack(tarball, dir) { return { then: (fulfill, reject) => { const logOpts = { tarball, dir }; const readableStream = fs.createReadStream(tarball).once('error', this._logError(`fs.createReadStream`, logOpts)); const unzip = zlib.createUnzip().once('error', this._logError(`zlib.createUnzip`, logOpts)); const extract = tar.extract(dir).once('error', this._logError(`tar.extract`, logOpts)); pump(readableStream, unzip, extract, err => { if (err) return reject(err); fulfill(); }); } }; }
extract.on('entry', (header, stream, cb) => { const file = header.name.split('/').pop() stream.on('data', (chunk) => { if (file === 'package.json') data += chunk }) stream.on('end', () => { if (data) { try { const pkgJSON = JSON.parse(data) resolve({ type: 'remote', version: pkgJSON.version, dependencies: pkgJSON.dependencies, url: spec }) } catch (e) { reject(e) } } else { cb() } }) stream.resume() })
/** * Find the list of file names in the specified chaincode deployment specification. * @private * @param {ChaincodeDeploymentSpec} chaincodeDeploymentSpec The chaincode deployment specification. * @returns {string[]} The list of file names. */ static async _findFileNames(chaincodeDeploymentSpec) { const codePackage = chaincodeDeploymentSpec.getCodePackage().toBuffer(); const gunzip = zlib.createGunzip(); const extract = tar.extract(); return new Promise((resolve) => { const fileNames = []; extract.on('entry', (header, stream, next) => { logger.debug('Package._findFileNames - found entry %s', header.name); fileNames.push(header.name); stream.on('end', () => { next(); }); stream.resume(); }); extract.on('finish', () => { resolve(fileNames.sort()); }); gunzip.pipe(extract); gunzip.end(codePackage); }); }
extract.on('finish', () => { resolve() })
/** * Unpack the tarball into given directory. * * Used from https://github.com/ywangii/await-targz under MIT. * * @param {String} tarball Path for tarball to be extracted * @param {String} dir Path for the file to be extracted * @returns {Promise} A promise represents if unpack tarball succeeds or fails * @public */ async unpack(tarball, dir) { return { then: (fulfill, reject) => { const logOpts = { tarball, dir }; const readableStream = fs.createReadStream(tarball).once('error', this._logError(`fs.createReadStream`, logOpts)); const unzip = zlib.createUnzip().once('error', this._logError(`zlib.createUnzip`, logOpts)); const extract = tar.extract(dir).once('error', this._logError(`tar.extract`, logOpts)); pump(readableStream, unzip, extract, err => { if (err) return reject(err); fulfill(); }); } }; }
/** * Find the list of file names in the specified chaincode deployment specification. * @private * @param {ChaincodeDeploymentSpec} chaincodeDeploymentSpec The chaincode deployment specification. * @returns {string[]} The list of file names. */ static async _findFileNames(chaincodeDeploymentSpec) { const codePackage = chaincodeDeploymentSpec.getCodePackage().toBuffer(); const gunzip = zlib.createGunzip(); const extract = tar.extract(); return new Promise((resolve) => { const fileNames = []; extract.on('entry', (header, stream, next) => { logger.debug('Package._findFileNames - found entry %s', header.name); fileNames.push(header.name); stream.on('end', () => { next(); }); stream.resume(); }); extract.on('finish', () => { resolve(fileNames.sort()); }); gunzip.pipe(extract); gunzip.end(codePackage); }); }