{"size": 7917, "ext": "js", "max_stars_repo_path": "node_modules/snyk-nodejs-lockfile-parser/dist/parsers/yarn-lock-parse.js", "max_stars_repo_name": "Hoaitt-GG/demo-html", "max_stars_repo_stars_event_min_datetime": "2019-04-13T15:31:29.000Z", "max_stars_repo_stars_event_max_datetime": "2019-04-13T15:32:55.000Z", "max_issues_repo_path": "node_modules/snyk-nodejs-lockfile-parser/dist/parsers/yarn-lock-parse.js", "max_issues_repo_name": "Hoaitt-GG/demo-html", "max_issues_repo_issues_event_min_datetime": "2021-05-09T11:33:11.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T10:54:22.000Z", "max_forks_repo_path": "node_modules/snyk-nodejs-lockfile-parser/dist/parsers/yarn-lock-parse.js", "max_forks_repo_name": "Hoaitt-GG/demo-html", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 4, "max_forks_count": null, "avg_line_length": 50.4267515924, "max_line_length": 133, "alphanum_fraction": 0.4597701149}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var _ = require("lodash");
var _1 = require("./");
var get_node_runtime_version_1 = require("../get-node-runtime-version");
var set_immediate_promise_1 = require("../set-immediate-promise");
var errors_1 = require("../errors");
var YarnLockParser = /** @class */ (function () {
    function YarnLockParser() {
        this.eventLoopSpinRate = 100;
        // @yarnpkg/lockfile doesn't work with Node.js < 6 and crashes just after
        // the import, so it has to be required conditionally
        // more details at https://github.com/yarnpkg/yarn/issues/6304
        if (get_node_runtime_version_1.default() < 6) {
            throw new errors_1.UnsupportedRuntimeError('yarn.lock parsing is supported for ' +
                'Node.js v6 and higher.');
        }
        this.yarnLockfileParser = require('@yarnpkg/lockfile');
        // Number of dependencies including root one.
        this.treeSize = 1;
    }
    YarnLockParser.prototype.parseLockFile = function (lockFileContents) {
        try {
            var yarnLock = this.yarnLockfileParser.parse(lockFileContents);
            yarnLock.dependencies = yarnLock.object;
            yarnLock.type = _1.LockfileType.yarn;
            return yarnLock;
        }
        catch (e) {
            throw new errors_1.InvalidUserInputError('yarn.lock parsing failed with an ' +
                ("error: " + e.message));
        }
    };
    YarnLockParser.prototype.getDependencyTree = function (manifestFile, lockfile, includeDev, strict) {
        if (includeDev === void 0) { includeDev = false; }
        if (strict === void 0) { strict = true; }
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var yarnLock, depTree, topLevelDeps, _i, topLevelDeps_1, dep, _a, _b;
            return tslib_1.__generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        if (lockfile.type !== _1.LockfileType.yarn) {
                            throw new errors_1.InvalidUserInputError('Unsupported lockfile provided. ' +
                                'Please provide `package-lock.json`.');
                        }
                        yarnLock = lockfile;
                        depTree = {
                            dependencies: {},
                            hasDevDependencies: !_.isEmpty(manifestFile.devDependencies),
                            name: manifestFile.name,
                            size: 1,
                            version: manifestFile.version || '',
                        };
                        topLevelDeps = _1.getTopLevelDeps(manifestFile, includeDev);
                        // asked to process empty deps
                        if (_.isEmpty(manifestFile.dependencies) && !includeDev) {
                            return [2 /*return*/, depTree];
                        }
                        _i = 0, topLevelDeps_1 = topLevelDeps;
                        _c.label = 1;
                    case 1:
                        if (!(_i < topLevelDeps_1.length)) return [3 /*break*/, 7];
                        dep = topLevelDeps_1[_i];
                        if (!/^file:/.test(dep.version)) return [3 /*break*/, 2];
                        depTree.dependencies[dep.name] = _1.createPkgTreeFromDep(dep);
                        return [3 /*break*/, 4];
                    case 2:
                        _a = depTree.dependencies;
                        _b = dep.name;
                        return [4 /*yield*/, this.buildSubTree(yarnLock, _1.createPkgTreeFromDep(dep), strict)];
                    case 3:
                        _a[_b] = _c.sent();
                        _c.label = 4;
                    case 4:
                        this.treeSize++;
                        if (!(this.treeSize % this.eventLoopSpinRate === 0)) return [3 /*break*/, 6];
                        // Spin event loop every X dependencies.
                        return [4 /*yield*/, set_immediate_promise_1.setImmediatePromise()];
                    case 5:
                        // Spin event loop every X dependencies.
                        _c.sent();
                        _c.label = 6;
                    case 6:
                        _i++;
                        return [3 /*break*/, 1];
                    case 7:
                        depTree.size = this.treeSize;
                        return [2 /*return*/, depTree];
                }
            });
        });
    };
    YarnLockParser.prototype.buildSubTree = function (lockFile, tree, strict) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var queue, queueItem, depKey, dependency, subDependencies, _i, subDependencies_1, _a, subName, subVersion, subDependency;
            return tslib_1.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        queue = [{ path: [], tree: tree }];
                        _b.label = 1;
                    case 1:
                        if (!(queue.length > 0)) return [3 /*break*/, 6];
                        queueItem = queue.pop();
                        depKey = queueItem.tree.name + "@" + queueItem.tree.version;
                        dependency = lockFile.object[depKey];
                        if (!dependency) {
                            if (strict) {
                                throw new errors_1.OutOfSyncError(queueItem.tree.name, 'yarn');
                            }
                            queueItem.tree.missingLockFileEntry = true;
                            return [3 /*break*/, 1];
                        }
                        // Overwrite version pattern with exact version.
                        queueItem.tree.version = dependency.version;
                        if (queueItem.path.indexOf(depKey) >= 0) {
                            queueItem.tree.cyclic = true;
                            return [3 /*break*/, 1];
                        }
                        subDependencies = _.entries(tslib_1.__assign({}, dependency.dependencies, dependency.optionalDependencies));
                        _i = 0, subDependencies_1 = subDependencies;
                        _b.label = 2;
                    case 2:
                        if (!(_i < subDependencies_1.length)) return [3 /*break*/, 5];
                        _a = subDependencies_1[_i], subName = _a[0], subVersion = _a[1];
                        subDependency = {
                            depType: tree.depType,
                            dependencies: {},
                            name: subName,
                            version: subVersion,
                        };
                        queueItem.tree.dependencies[subName] = subDependency;
                        queue.push({
                            path: queueItem.path.concat([depKey]),
                            tree: subDependency,
                        });
                        this.treeSize++;
                        if (!(this.treeSize % this.eventLoopSpinRate === 0)) return [3 /*break*/, 4];
                        // Spin event loop every X dependencies.
                        return [4 /*yield*/, set_immediate_promise_1.setImmediatePromise()];
                    case 3:
                        // Spin event loop every X dependencies.
                        _b.sent();
                        _b.label = 4;
                    case 4:
                        _i++;
                        return [3 /*break*/, 2];
                    case 5: return [3 /*break*/, 1];
                    case 6: return [2 /*return*/, tree];
                }
            });
        });
    };
    return YarnLockParser;
}());
exports.YarnLockParser = YarnLockParser;
//# sourceMappingURL=yarn-lock-parse.js.map