mirror of
https://github.com/status-im/metro.git
synced 2025-02-28 02:30:29 +00:00
Use async/await in AssetServer methods
Reviewed By: davidaurelio Differential Revision: D6435849 fbshipit-source-id: c665dfd6737e636f06c7056a89609730a5041a53
This commit is contained in:
parent
e19f82d408
commit
8cf97e332f
@ -49,6 +49,7 @@ export type AssetData = {|
|
|||||||
const stat = denodeify(fs.stat);
|
const stat = denodeify(fs.stat);
|
||||||
const readDir = denodeify(fs.readdir);
|
const readDir = denodeify(fs.readdir);
|
||||||
const readFile = denodeify(fs.readFile);
|
const readFile = denodeify(fs.readFile);
|
||||||
|
const hashFiles = denodeify(hashFilesCb);
|
||||||
|
|
||||||
class AssetServer {
|
class AssetServer {
|
||||||
_roots: $ReadOnlyArray<string>;
|
_roots: $ReadOnlyArray<string>;
|
||||||
@ -77,7 +78,7 @@ class AssetServer {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_getAssetInfo(
|
async _getAssetInfo(
|
||||||
assetPath: string,
|
assetPath: string,
|
||||||
platform: ?string = null,
|
platform: ?string = null,
|
||||||
): Promise<AssetInfo> {
|
): Promise<AssetInfo> {
|
||||||
@ -87,28 +88,23 @@ class AssetServer {
|
|||||||
);
|
);
|
||||||
const {name, type} = nameData;
|
const {name, type} = nameData;
|
||||||
|
|
||||||
return this._getAssetRecord(assetPath, platform).then(record => {
|
const {scales, files} = await this._getAssetRecord(assetPath, platform);
|
||||||
const {scales, files} = record;
|
|
||||||
|
|
||||||
const hash = this._hashes.get(assetPath);
|
const hash = this._hashes.get(assetPath);
|
||||||
if (hash != null) {
|
if (hash != null) {
|
||||||
return {files, hash, name, scales, type};
|
return {files, hash, name, scales, type};
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
const hasher = crypto.createHash('md5');
|
||||||
const hasher = crypto.createHash('md5');
|
|
||||||
hashFiles(files.slice(), hasher, error => {
|
if (files.length > 0) {
|
||||||
if (error) {
|
await hashFiles(files.slice(), hasher);
|
||||||
reject(error);
|
}
|
||||||
} else {
|
|
||||||
const freshHash = hasher.digest('hex');
|
const freshHash = hasher.digest('hex');
|
||||||
this._hashes.set(assetPath, freshHash);
|
this._hashes.set(assetPath, freshHash);
|
||||||
files.forEach(f => this._files.set(f, assetPath));
|
files.forEach(f => this._files.set(f, assetPath));
|
||||||
resolve({files, hash: freshHash, name, scales, type});
|
return {files, hash: freshHash, name, scales, type};
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAssetData(
|
async getAssetData(
|
||||||
@ -157,7 +153,7 @@ class AssetServer {
|
|||||||
* 4. Then try to pick platform-specific asset records
|
* 4. Then try to pick platform-specific asset records
|
||||||
* 5. Then pick the closest resolution (rounding up) to the requested one
|
* 5. Then pick the closest resolution (rounding up) to the requested one
|
||||||
*/
|
*/
|
||||||
_getAssetRecord(
|
async _getAssetRecord(
|
||||||
assetPath: string,
|
assetPath: string,
|
||||||
platform: ?string = null,
|
platform: ?string = null,
|
||||||
): Promise<{|
|
): Promise<{|
|
||||||
@ -166,77 +162,77 @@ class AssetServer {
|
|||||||
|}> {
|
|}> {
|
||||||
const filename = path.basename(assetPath);
|
const filename = path.basename(assetPath);
|
||||||
|
|
||||||
return this._findRoot(this._roots, path.dirname(assetPath), assetPath)
|
const dir = await this._findRoot(
|
||||||
.then(dir => Promise.all([dir, readDir(dir)]))
|
this._roots,
|
||||||
.then(res => {
|
path.dirname(assetPath),
|
||||||
const dir = res[0];
|
assetPath,
|
||||||
const files = res[1];
|
);
|
||||||
const assetData = AssetPaths.parse(
|
|
||||||
filename,
|
|
||||||
new Set(platform != null ? [platform] : []),
|
|
||||||
);
|
|
||||||
|
|
||||||
const map = this._buildAssetMap(dir, files, platform);
|
const files = await readDir(dir);
|
||||||
|
|
||||||
let record;
|
const assetData = AssetPaths.parse(
|
||||||
if (platform != null) {
|
filename,
|
||||||
record =
|
new Set(platform != null ? [platform] : []),
|
||||||
map.get(getAssetKey(assetData.assetName, platform)) ||
|
);
|
||||||
map.get(assetData.assetName);
|
|
||||||
} else {
|
|
||||||
record = map.get(assetData.assetName);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!record) {
|
const map = this._buildAssetMap(dir, files, platform);
|
||||||
throw new Error(
|
|
||||||
/* $FlowFixMe: platform can be null */
|
|
||||||
`Asset not found: ${assetPath} for platform: ${platform}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return record;
|
let record;
|
||||||
});
|
if (platform != null) {
|
||||||
|
record =
|
||||||
|
map.get(getAssetKey(assetData.assetName, platform)) ||
|
||||||
|
map.get(assetData.assetName);
|
||||||
|
} else {
|
||||||
|
record = map.get(assetData.assetName);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!record) {
|
||||||
|
throw new Error(
|
||||||
|
/* $FlowFixMe: platform can be null */
|
||||||
|
`Asset not found: ${assetPath} for platform: ${platform}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return record;
|
||||||
}
|
}
|
||||||
|
|
||||||
_findRoot(
|
async _findRoot(
|
||||||
roots: $ReadOnlyArray<string>,
|
roots: $ReadOnlyArray<string>,
|
||||||
dir: string,
|
dir: string,
|
||||||
debugInfoFile: string,
|
debugInfoFile: string,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
return Promise.all(
|
const stats = await Promise.all(
|
||||||
roots.map(root => {
|
roots.map(async root => {
|
||||||
const absRoot = path.resolve(root);
|
|
||||||
// important: we want to resolve root + dir
|
// important: we want to resolve root + dir
|
||||||
// to ensure the requested path doesn't traverse beyond root
|
// to ensure the requested path doesn't traverse beyond root
|
||||||
const absPath = path.resolve(root, dir);
|
const absPath = path.resolve(root, dir);
|
||||||
|
|
||||||
return stat(absPath).then(
|
try {
|
||||||
fstat => {
|
const fstat = await stat(absPath);
|
||||||
// keep asset requests from traversing files
|
|
||||||
// up from the root (e.g. ../../../etc/hosts)
|
|
||||||
if (!absPath.startsWith(absRoot)) {
|
|
||||||
return {path: absPath, isValid: false};
|
|
||||||
}
|
|
||||||
return {path: absPath, isValid: fstat.isDirectory()};
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
return {path: absPath, isValid: false};
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
).then(stats => {
|
|
||||||
for (let i = 0; i < stats.length; i++) {
|
|
||||||
if (stats[i].isValid) {
|
|
||||||
return stats[i].path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const rootsString = roots.map(s => `'${s}'`).join(', ');
|
// keep asset requests from traversing files
|
||||||
throw new Error(
|
// up from the root (e.g. ../../../etc/hosts)
|
||||||
`'${debugInfoFile}' could not be found, because '${dir}' is not a ` +
|
if (!absPath.startsWith(path.resolve(root))) {
|
||||||
`subdirectory of any of the roots (${rootsString})`,
|
return {path: absPath, isValid: false};
|
||||||
);
|
}
|
||||||
});
|
return {path: absPath, isValid: fstat.isDirectory()};
|
||||||
|
} catch (_) {
|
||||||
|
return {path: absPath, isValid: false};
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
for (let i = 0; i < stats.length; i++) {
|
||||||
|
if (stats[i].isValid) {
|
||||||
|
return stats[i].path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const rootsString = roots.map(s => `'${s}'`).join(', ');
|
||||||
|
throw new Error(
|
||||||
|
`'${debugInfoFile}' could not be found, because '${dir}' is not a ` +
|
||||||
|
`subdirectory of any of the roots (${rootsString})`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_buildAssetMap(
|
_buildAssetMap(
|
||||||
@ -296,7 +292,7 @@ function getAssetKey(assetName, platform) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function hashFiles(files, hash, callback) {
|
function hashFilesCb(files, hash, callback) {
|
||||||
if (!files.length) {
|
if (!files.length) {
|
||||||
callback(null);
|
callback(null);
|
||||||
return;
|
return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user