Skip to content

Commit

Permalink
Merge branch 'master' into feat/lockfile_parser_return_valid_name
Browse files Browse the repository at this point in the history
  • Loading branch information
yuliabaron authored Dec 2, 2018
2 parents 557fb6b + 152e352 commit b2aae05
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
13 changes: 13 additions & 0 deletions lib/event-loop-spinner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class EventLoopSpinner {
private lastSpin: number;
constructor(private thresholdMs: number = 100) {
this.lastSpin = Date.now();
}
public isStarving(): boolean {
return (Date.now() - this.lastSpin) > this.thresholdMs;
}
public async spin() {
this.lastSpin = Date.now();
return new Promise((resolve) => setImmediate(resolve));
}
}
28 changes: 19 additions & 9 deletions lib/parsers/yarn-lock-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as _ from 'lodash';
import {LockfileParser, PkgTree, Dep, DepType, ManifestFile,
getTopLevelDeps, Lockfile, LockfileType, createPkgTreeFromDep} from './';
import getRuntimeVersion from '../get-node-runtime-version';
import {setImmediatePromise} from '../set-immediate-promise';
import { EventLoopSpinner } from '../event-loop-spinner';
import {
InvalidUserInputError,
UnsupportedRuntimeError,
Expand Down Expand Up @@ -32,6 +34,7 @@ export interface YarnLockDep {
export class YarnLockParser implements LockfileParser {

private yarnLockfileParser;
private eventLoop: EventLoopSpinner;

constructor() {
// @yarnpkg/lockfile doesn't work with Node.js < 6 and crashes just after
Expand All @@ -42,6 +45,10 @@ export class YarnLockParser implements LockfileParser {
'Node.js v6 and higher.');
}
this.yarnLockfileParser = require('@yarnpkg/lockfile');
// 200ms is an arbitrary value based on on testing "average request", which is
// processed in ~150ms. Idea is to let those average requests through in one
// tick and split only bigger ones.
this.eventLoop = new EventLoopSpinner(200);
}

public parseLockFile(lockFileContents: string): YarnLock {
Expand Down Expand Up @@ -79,14 +86,14 @@ export class YarnLockParser implements LockfileParser {
return depTree;
}

await Promise.all(topLevelDeps.map(async (dep) => {
if (/^file:/.test(dep.version)) {
depTree.dependencies[dep.name] = createPkgTreeFromDep(dep);
} else {
depTree.dependencies[dep.name] = await this.buildSubTreeRecursiveFromYarnLock(
dep, yarnLock, [], strict);
for (const dep of topLevelDeps) {
if (/^file:/.test(dep.version)) {
depTree.dependencies[dep.name] = createPkgTreeFromDep(dep);
} else {
depTree.dependencies[dep.name] = await this.buildSubTreeRecursiveFromYarnLock(
dep, yarnLock, [], strict);
}
}));
}

return depTree;
}
Expand Down Expand Up @@ -123,17 +130,20 @@ export class YarnLockParser implements LockfileParser {
depPath.push(depKey);
const newDeps = _.entries({...dep.dependencies, ...dep.optionalDependencies});

await Promise.all(newDeps.map(async ([name, version]) => {
for (const [name, version] of newDeps) {
const newDep: Dep = {
dev: searchedDep.dev,
name,
version,
};
depSubTree.dependencies[name] = await this.buildSubTreeRecursiveFromYarnLock(
newDep, lockFile, [...depPath]);
}));
}
}

if (this.eventLoop.isStarving()) {
await this.eventLoop.spin();
}
return depSubTree;
}
}

0 comments on commit b2aae05

Please sign in to comment.