Use async/await in AssetServer methods

Reviewed By: davidaurelio

Differential Revision: D6435849

fbshipit-source-id: c665dfd6737e636f06c7056a89609730a5041a53
This commit is contained in:
Rafael Oleza 2017-11-29 15:41:11 -08:00 committed by Facebook Github Bot
parent e19f82d408
commit 8cf97e332f
1 changed files with 75 additions and 79 deletions

View File

@ -49,6 +49,7 @@ export type AssetData = {|
const stat = denodeify(fs.stat);
const readDir = denodeify(fs.readdir);
const readFile = denodeify(fs.readFile);
const hashFiles = denodeify(hashFilesCb);
class AssetServer {
_roots: $ReadOnlyArray<string>;
@ -77,7 +78,7 @@ class AssetServer {
});
}
_getAssetInfo(
async _getAssetInfo(
assetPath: string,
platform: ?string = null,
): Promise<AssetInfo> {
@ -87,28 +88,23 @@ class AssetServer {
);
const {name, type} = nameData;
return this._getAssetRecord(assetPath, platform).then(record => {
const {scales, files} = record;
const {scales, files} = await this._getAssetRecord(assetPath, platform);
const hash = this._hashes.get(assetPath);
if (hash != null) {
return {files, hash, name, scales, type};
}
return new Promise((resolve, reject) => {
const hasher = crypto.createHash('md5');
hashFiles(files.slice(), hasher, error => {
if (error) {
reject(error);
} else {
if (files.length > 0) {
await hashFiles(files.slice(), hasher);
}
const freshHash = hasher.digest('hex');
this._hashes.set(assetPath, freshHash);
files.forEach(f => this._files.set(f, assetPath));
resolve({files, hash: freshHash, name, scales, type});
}
});
});
});
return {files, hash: freshHash, name, scales, type};
}
async getAssetData(
@ -157,7 +153,7 @@ class AssetServer {
* 4. Then try to pick platform-specific asset records
* 5. Then pick the closest resolution (rounding up) to the requested one
*/
_getAssetRecord(
async _getAssetRecord(
assetPath: string,
platform: ?string = null,
): Promise<{|
@ -166,11 +162,14 @@ class AssetServer {
|}> {
const filename = path.basename(assetPath);
return this._findRoot(this._roots, path.dirname(assetPath), assetPath)
.then(dir => Promise.all([dir, readDir(dir)]))
.then(res => {
const dir = res[0];
const files = res[1];
const dir = await this._findRoot(
this._roots,
path.dirname(assetPath),
assetPath,
);
const files = await readDir(dir);
const assetData = AssetPaths.parse(
filename,
new Set(platform != null ? [platform] : []),
@ -195,36 +194,34 @@ class AssetServer {
}
return record;
});
}
_findRoot(
async _findRoot(
roots: $ReadOnlyArray<string>,
dir: string,
debugInfoFile: string,
): Promise<string> {
return Promise.all(
roots.map(root => {
const absRoot = path.resolve(root);
const stats = await Promise.all(
roots.map(async root => {
// important: we want to resolve root + dir
// to ensure the requested path doesn't traverse beyond root
const absPath = path.resolve(root, dir);
return stat(absPath).then(
fstat => {
try {
const fstat = await stat(absPath);
// keep asset requests from traversing files
// up from the root (e.g. ../../../etc/hosts)
if (!absPath.startsWith(absRoot)) {
if (!absPath.startsWith(path.resolve(root))) {
return {path: absPath, isValid: false};
}
return {path: absPath, isValid: fstat.isDirectory()};
},
_ => {
} catch (_) {
return {path: absPath, isValid: false};
},
);
}
}),
).then(stats => {
);
for (let i = 0; i < stats.length; i++) {
if (stats[i].isValid) {
return stats[i].path;
@ -236,7 +233,6 @@ class AssetServer {
`'${debugInfoFile}' could not be found, because '${dir}' is not a ` +
`subdirectory of any of the roots (${rootsString})`,
);
});
}
_buildAssetMap(
@ -296,7 +292,7 @@ function getAssetKey(assetName, platform) {
}
}
function hashFiles(files, hash, callback) {
function hashFilesCb(files, hash, callback) {
if (!files.length) {
callback(null);
return;