JezK
Edit File: git-fetcher.js
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; function _regenerator() { var data = _interopRequireDefault(require("@babel/runtime/regenerator")); _regenerator = function _regenerator() { return data; }; return data; } function _slicedToArray2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); _slicedToArray2 = function _slicedToArray2() { return data; }; return data; } function _asyncToGenerator2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); _asyncToGenerator2 = function _asyncToGenerator2() { return data; }; return data; } function _classCallCheck2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); _classCallCheck2 = function _classCallCheck2() { return data; }; return data; } function _createClass2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); _createClass2 = function _createClass2() { return data; }; return data; } function _inherits2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); _inherits2 = function _inherits2() { return data; }; return data; } function _possibleConstructorReturn2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); _possibleConstructorReturn2 = function _possibleConstructorReturn2() { return data; }; return data; } function _getPrototypeOf2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); _getPrototypeOf2 = function _getPrototypeOf2() { return data; }; return data; } function _errors() { var data = require("../errors.js"); _errors = function _errors() { return data; }; return data; } function _baseFetcher() { var data = _interopRequireDefault(require("./base-fetcher.js")); _baseFetcher = function _baseFetcher() { return data; }; return data; } function _git() { var data = _interopRequireDefault(require("../util/git.js")); _git = function _git() { return data; }; return data; } function fsUtil() { var data = _interopRequireWildcard(require("../util/fs.js")); fsUtil = function fsUtil() { return data; }; return data; } function constants() { var data = _interopRequireWildcard(require("../constants.js")); constants = function constants() { return data; }; return data; } function crypto() { var data = _interopRequireWildcard(require("../util/crypto.js")); crypto = function crypto() { return data; }; return data; } function _install() { var data = require("../cli/commands/install.js"); _install = function _install() { return data; }; return data; } function _lockfile() { var data = _interopRequireDefault(require("../lockfile")); _lockfile = function _lockfile() { return data; }; return data; } function _config() { var data = _interopRequireDefault(require("../config.js")); _config = function _config() { return data; }; return data; } function _pack() { var data = require("../cli/commands/pack.js"); _pack = function _pack() { return data; }; return data; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2()["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2()["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2()["default"])(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } var tarFs = require('tar-fs'); var url = require('url'); var path = require('path'); var fs = require('fs'); var invariant = require('invariant'); var PACKED_FLAG = '1'; var GitFetcher = /*#__PURE__*/function (_BaseFetcher) { (0, _inherits2()["default"])(GitFetcher, _BaseFetcher); var _super = _createSuper(GitFetcher); function GitFetcher() { (0, _classCallCheck2()["default"])(this, GitFetcher); return _super.apply(this, arguments); } (0, _createClass2()["default"])(GitFetcher, [{ key: "setupMirrorFromCache", value: function () { var _setupMirrorFromCache = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee() { var tarballMirrorPath, tarballCachePath; return _regenerator()["default"].wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: tarballMirrorPath = this.getTarballMirrorPath(); tarballCachePath = this.getTarballCachePath(); if (!(tarballMirrorPath == null)) { _context.next = 4; break; } return _context.abrupt("return"); case 4: _context.next = 6; return fsUtil().exists(tarballMirrorPath); case 6: _context.t0 = !_context.sent; if (!_context.t0) { _context.next = 11; break; } _context.next = 10; return fsUtil().exists(tarballCachePath); case 10: _context.t0 = _context.sent; case 11: if (!_context.t0) { _context.next = 16; break; } _context.next = 14; return fsUtil().mkdirp(path.dirname(tarballMirrorPath)); case 14: _context.next = 16; return fsUtil().copy(tarballCachePath, tarballMirrorPath, this.reporter); case 16: case "end": return _context.stop(); } }, _callee, this); })); function setupMirrorFromCache() { return _setupMirrorFromCache.apply(this, arguments); } return setupMirrorFromCache; }() }, { key: "getTarballMirrorPath", value: function getTarballMirrorPath() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref$withCommit = _ref.withCommit, withCommit = _ref$withCommit === void 0 ? true : _ref$withCommit; var _url$parse = url.parse(this.reference), pathname = _url$parse.pathname; if (pathname == null) { return null; } var hash = this.hash; var packageFilename = withCommit && hash ? "".concat(path.basename(pathname), "-").concat(hash) : "".concat(path.basename(pathname)); if (packageFilename.startsWith(':')) { packageFilename = packageFilename.substr(1); } return this.config.getOfflineMirrorPath(packageFilename); } }, { key: "getTarballCachePath", value: function getTarballCachePath() { return path.join(this.dest, constants().TARBALL_FILENAME); } }, { key: "getLocalPaths", value: function getLocalPaths(override) { var paths = [override ? path.resolve(this.config.cwd, override) : null, this.getTarballMirrorPath(), this.getTarballMirrorPath({ withCommit: false }), this.getTarballCachePath()]; // $FlowFixMe: https://github.com/facebook/flow/issues/1414 return paths.filter(function (path) { return path != null; }); } }, { key: "fetchFromLocal", value: function () { var _fetchFromLocal = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee2(override) { var _this = this; var tarPaths, stream; return _regenerator()["default"].wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: tarPaths = this.getLocalPaths(override); _context2.next = 3; return fsUtil().readFirstAvailableStream(tarPaths); case 3: stream = _context2.sent; return _context2.abrupt("return", new Promise(function (resolve, reject) { if (!stream) { reject(new (_errors().MessageError)(_this.reporter.lang('tarballNotInNetworkOrCache', _this.reference, tarPaths))); return; } invariant(stream, 'cachedStream should be available at this point'); // $FlowFixMe - This is available https://nodejs.org/api/fs.html#fs_readstream_path var tarballPath = stream.path; var untarStream = _this._createUntarStream(_this.dest); var hashStream = new (crypto().HashStream)(); stream.pipe(hashStream).pipe(untarStream).on('finish', function () { var expectHash = _this.hash; invariant(expectHash, 'Commit hash required'); var actualHash = hashStream.getHash(); // This condition is disabled because "expectHash" actually is the commit hash // This is a design issue that we'll need to fix (https://github.com/yarnpkg/yarn/pull/3449) if (true || !expectHash || expectHash === actualHash) { resolve({ hash: expectHash }); } else { reject(new (_errors().SecurityError)(_this.config.reporter.lang('fetchBadHashWithPath', _this.packageName, _this.remote.reference, expectHash, actualHash))); } }).on('error', function (err) { reject(new (_errors().MessageError)(this.reporter.lang('fetchErrorCorrupt', err.message, tarballPath))); }); })); case 5: case "end": return _context2.stop(); } }, _callee2, this); })); function fetchFromLocal(_x) { return _fetchFromLocal.apply(this, arguments); } return fetchFromLocal; }() }, { key: "hasPrepareScript", value: function () { var _hasPrepareScript = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee3(git) { var manifestFile, scripts, _hasPrepareScript2; return _regenerator()["default"].wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return git.getFile('package.json'); case 2: manifestFile = _context3.sent; if (!manifestFile) { _context3.next = 7; break; } scripts = JSON.parse(manifestFile).scripts; _hasPrepareScript2 = Boolean(scripts && scripts.prepare); return _context3.abrupt("return", _hasPrepareScript2); case 7: return _context3.abrupt("return", false); case 8: case "end": return _context3.stop(); } }, _callee3); })); function hasPrepareScript(_x2) { return _hasPrepareScript.apply(this, arguments); } return hasPrepareScript; }() }, { key: "fetchFromExternal", value: function () { var _fetchFromExternal = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee4() { var hash, gitUrl, git; return _regenerator()["default"].wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: hash = this.hash; invariant(hash, 'Commit hash required'); gitUrl = _git()["default"].npmUrlToGitUrl(this.reference); git = new (_git()["default"])(this.config, gitUrl, hash); _context4.next = 6; return git.init(); case 6: _context4.next = 8; return this.hasPrepareScript(git); case 8: if (!_context4.sent) { _context4.next = 13; break; } _context4.next = 11; return this.fetchFromInstallAndPack(git); case 11: _context4.next = 15; break; case 13: _context4.next = 15; return this.fetchFromGitArchive(git); case 15: return _context4.abrupt("return", { hash: hash }); case 16: case "end": return _context4.stop(); } }, _callee4, this); })); function fetchFromExternal() { return _fetchFromExternal.apply(this, arguments); } return fetchFromExternal; }() }, { key: "fetchFromInstallAndPack", value: function () { var _fetchFromInstallAndPack = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee5(git) { var prepareDirectory, _yield$Promise$all, _yield$Promise$all2, prepareConfig, prepareLockFile, tarballMirrorPath, tarballCachePath; return _regenerator()["default"].wrap(function _callee5$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: prepareDirectory = this.config.getTemp("".concat(crypto().hash(git.gitUrl.repository), ".").concat(git.hash, ".prepare")); _context5.next = 3; return fsUtil().unlink(prepareDirectory); case 3: _context5.next = 5; return git.clone(prepareDirectory); case 5: _context5.next = 7; return Promise.all([_config()["default"].create({ binLinks: true, cwd: prepareDirectory, disablePrepublish: true, production: false }, this.reporter), _lockfile()["default"].fromDirectory(prepareDirectory, this.reporter)]); case 7: _yield$Promise$all = _context5.sent; _yield$Promise$all2 = (0, _slicedToArray2()["default"])(_yield$Promise$all, 2); prepareConfig = _yield$Promise$all2[0]; prepareLockFile = _yield$Promise$all2[1]; _context5.next = 13; return (0, _install().install)(prepareConfig, this.reporter, {}, prepareLockFile); case 13: tarballMirrorPath = this.getTarballMirrorPath(); tarballCachePath = this.getTarballCachePath(); if (!tarballMirrorPath) { _context5.next = 18; break; } _context5.next = 18; return this._packToTarball(prepareConfig, tarballMirrorPath); case 18: if (!tarballCachePath) { _context5.next = 21; break; } _context5.next = 21; return this._packToTarball(prepareConfig, tarballCachePath); case 21: _context5.next = 23; return this._packToDirectory(prepareConfig, this.dest); case 23: _context5.next = 25; return fsUtil().unlink(prepareDirectory); case 25: case "end": return _context5.stop(); } }, _callee5, this); })); function fetchFromInstallAndPack(_x3) { return _fetchFromInstallAndPack.apply(this, arguments); } return fetchFromInstallAndPack; }() }, { key: "_packToTarball", value: function () { var _packToTarball2 = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee6(config, path) { var tarballStream; return _regenerator()["default"].wrap(function _callee6$(_context6) { while (1) switch (_context6.prev = _context6.next) { case 0: _context6.next = 2; return this._createTarballStream(config); case 2: tarballStream = _context6.sent; _context6.next = 5; return new Promise(function (resolve, reject) { var writeStream = fs.createWriteStream(path); tarballStream.on('error', reject); writeStream.on('error', reject); writeStream.on('end', resolve); writeStream.on('open', function () { tarballStream.pipe(writeStream); }); writeStream.once('finish', resolve); }); case 5: case "end": return _context6.stop(); } }, _callee6, this); })); function _packToTarball(_x4, _x5) { return _packToTarball2.apply(this, arguments); } return _packToTarball; }() }, { key: "_packToDirectory", value: function () { var _packToDirectory2 = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee7(config, dest) { var _this2 = this; var tarballStream; return _regenerator()["default"].wrap(function _callee7$(_context7) { while (1) switch (_context7.prev = _context7.next) { case 0: _context7.next = 2; return this._createTarballStream(config); case 2: tarballStream = _context7.sent; _context7.next = 5; return new Promise(function (resolve, reject) { var untarStream = _this2._createUntarStream(dest); tarballStream.on('error', reject); untarStream.on('error', reject); untarStream.on('end', resolve); untarStream.once('finish', resolve); tarballStream.pipe(untarStream); }); case 5: case "end": return _context7.stop(); } }, _callee7, this); })); function _packToDirectory(_x6, _x7) { return _packToDirectory2.apply(this, arguments); } return _packToDirectory; }() }, { key: "_createTarballStream", value: function _createTarballStream(config) { var savedPackedHeader = false; return (0, _pack().packTarball)(config, { mapHeader: function mapHeader(header) { if (!savedPackedHeader) { savedPackedHeader = true; header.pax = header.pax || {}; // add a custom data on the first header // in order to distinguish a tar from "git archive" and a tar from "pack" command header.pax.packed = PACKED_FLAG; } return header; } }); } }, { key: "_createUntarStream", value: function _createUntarStream(dest) { var PREFIX = 'package/'; var isPackedTarball = undefined; return tarFs.extract(dest, { dmode: 365, // all dirs should be readable fmode: 292, // all files should be readable chown: false, // don't chown. just leave as it is map: function map(header) { if (isPackedTarball === undefined) { isPackedTarball = header.pax && header.pax.packed === PACKED_FLAG; } if (isPackedTarball) { header.name = header.name.substr(PREFIX.length); } } }); } }, { key: "fetchFromGitArchive", value: function () { var _fetchFromGitArchive = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee8(git) { var tarballMirrorPath, tarballCachePath; return _regenerator()["default"].wrap(function _callee8$(_context8) { while (1) switch (_context8.prev = _context8.next) { case 0: _context8.next = 2; return git.clone(this.dest); case 2: tarballMirrorPath = this.getTarballMirrorPath(); tarballCachePath = this.getTarballCachePath(); if (!tarballMirrorPath) { _context8.next = 7; break; } _context8.next = 7; return git.archive(tarballMirrorPath); case 7: if (!tarballCachePath) { _context8.next = 10; break; } _context8.next = 10; return git.archive(tarballCachePath); case 10: case "end": return _context8.stop(); } }, _callee8, this); })); function fetchFromGitArchive(_x8) { return _fetchFromGitArchive.apply(this, arguments); } return fetchFromGitArchive; }() }, { key: "_fetch", value: function _fetch() { var _this3 = this; return this.fetchFromLocal()["catch"](function (err) { return _this3.fetchFromExternal(); }); } }]); return GitFetcher; }(_baseFetcher()["default"]); exports["default"] = GitFetcher; //# sourceMappingURL=git-fetcher.js.map