Skip to content

Commit

Permalink
Simplify readFirstAvailableStream() (#6022)
Browse files Browse the repository at this point in the history
* The calling function already knows which paths we tried

* This generic function isn't specific to tarballs

* Return only non-null paths, and return them as an array

This makes the function more convenient to use

* Only use real paths in readFirstAvailableStream()

* Return stream directly
  • Loading branch information
sth authored and arcanis committed Jun 26, 2018
1 parent c53d039 commit eb2b565
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
23 changes: 12 additions & 11 deletions src/fetchers/git-fetcher.js
Expand Up @@ -59,23 +59,24 @@ export default class GitFetcher extends BaseFetcher {
return path.join(this.dest, constants.TARBALL_FILENAME);
}

*getLocalPaths(override: ?string): Generator<?string, void, void> {
if (override) {
yield path.resolve(this.config.cwd, override);
}
yield this.getTarballMirrorPath();
yield this.getTarballMirrorPath({
withCommit: false,
});
yield this.getTarballCachePath();
getLocalPaths(override: ?string): Array<string> {
const paths: Array<?string> = [
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(path => path != null);
}

async fetchFromLocal(override: ?string): Promise<FetchedOverride> {
const {stream, triedPaths} = await fsUtil.readFirstAvailableStream(this.getLocalPaths(override));
const tarPaths = this.getLocalPaths(override);
const stream = await fsUtil.readFirstAvailableStream(tarPaths);

return new Promise((resolve, reject) => {
if (!stream) {
reject(new MessageError(this.reporter.lang('tarballNotInNetworkOrCache', this.reference, triedPaths)));
reject(new MessageError(this.reporter.lang('tarballNotInNetworkOrCache', this.reference, tarPaths)));
return;
}
invariant(stream, 'cachedStream should be available at this point');
Expand Down
19 changes: 11 additions & 8 deletions src/fetchers/tarball-fetcher.js
Expand Up @@ -118,20 +118,23 @@ export default class TarballFetcher extends BaseFetcher {
return {validateStream, extractorStream};
}

*getLocalPaths(override: ?string): Generator<?string, void, void> {
if (override) {
yield path.resolve(this.config.cwd, override);
}
yield this.getTarballMirrorPath();
yield this.getTarballCachePath();
getLocalPaths(override: ?string): Array<string> {
const paths: Array<?string> = [
override ? path.resolve(this.config.cwd, override) : null,
this.getTarballMirrorPath(),
this.getTarballCachePath(),
];
// $FlowFixMe: https://github.com/facebook/flow/issues/1414
return paths.filter(path => path != null);
}

async fetchFromLocal(override: ?string): Promise<FetchedOverride> {
const {stream, triedPaths} = await fsUtil.readFirstAvailableStream(this.getLocalPaths(override));
const tarPaths = this.getLocalPaths(override);
const stream = await fsUtil.readFirstAvailableStream(tarPaths);

return new Promise((resolve, reject) => {
if (!stream) {
reject(new MessageError(this.reporter.lang('tarballNotInNetworkOrCache', this.reference, triedPaths)));
reject(new MessageError(this.reporter.lang('tarballNotInNetworkOrCache', this.reference, tarPaths)));
return;
}
invariant(stream, 'stream should be available at this point');
Expand Down
24 changes: 8 additions & 16 deletions src/util/fs.js
Expand Up @@ -814,24 +814,16 @@ export async function makeTempDir(prefix?: string): Promise<string> {
return dir;
}

export async function readFirstAvailableStream(
paths: Iterable<?string>,
): Promise<{stream: ?ReadStream, triedPaths: Array<string>}> {
let stream: ?ReadStream;
const triedPaths = [];
for (const tarballPath of paths) {
if (tarballPath) {
try {
const fd = await open(tarballPath, 'r');
stream = fs.createReadStream(tarballPath, {fd});
break;
} catch (err) {
// Try the next one
triedPaths.push(tarballPath);
}
export async function readFirstAvailableStream(paths: Iterable<string>): Promise<?ReadStream> {
for (const path of paths) {
try {
const fd = await open(path, 'r');
return fs.createReadStream(path, {fd});
} catch (err) {
// Try the next one
}
}
return {stream, triedPaths};
return null;
}

export async function getFirstSuitableFolder(
Expand Down

0 comments on commit eb2b565

Please sign in to comment.