From e324b229bb28308bed4b4211ffabf2ce82bbe2da Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Mon, 4 Dec 2017 16:36:38 -0800 Subject: [PATCH] Make getAssetData() method stateless Reviewed By: davidaurelio Differential Revision: D6436349 fbshipit-source-id: 3544ae2824ada191f8ad8909f3b6cef13f208975 --- packages/metro/src/AssetServer/index.js | 45 ++----------------------- packages/metro/src/AssetServer/util.js | 38 ++++++++++++++++++++- 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/packages/metro/src/AssetServer/index.js b/packages/metro/src/AssetServer/index.js index 5f1e9c17..2c731ade 100644 --- a/packages/metro/src/AssetServer/index.js +++ b/packages/metro/src/AssetServer/index.js @@ -16,18 +16,10 @@ const AssetPaths = require('../node-haste/lib/AssetPaths'); const denodeify = require('denodeify'); const fs = require('fs'); -const imageSize = require('image-size'); const path = require('path'); const toLocalPath = require('../node-haste/lib/toLocalPath'); -const {isAssetTypeAnImage} = require('../Bundler/util'); -const { - findRoot, - getAbsoluteAssetInfo, - getAbsoluteAssetRecord, -} = require('./util'); - -import type {AssetInfo} from './util'; +const {findRoot, getAbsoluteAssetRecord, getAssetData} = require('./util'); export type AssetData = {| __packager_asset: boolean, @@ -67,46 +59,13 @@ class AssetServer { }); } - async _getAssetInfo( - assetPath: string, - platform: ?string = null, - ): Promise { - const dir = await findRoot(this._roots, path.dirname(assetPath), assetPath); - - const assetAbsolutePath = path.join(dir, path.basename(assetPath)); - - return await getAbsoluteAssetInfo(assetAbsolutePath, platform); - } - async getAssetData( assetPath: string, platform: ?string = null, ): Promise { const localPath = toLocalPath(this._roots, assetPath); - var assetUrlPath = path.join('/assets', path.dirname(localPath)); - // On Windows, change backslashes to slashes to get proper URL path from file path. - if (path.sep === '\\') { - assetUrlPath = assetUrlPath.replace(/\\/g, '/'); - } - - const isImage = isAssetTypeAnImage(path.extname(assetPath).slice(1)); - const assetData = await getAbsoluteAssetInfo(assetPath, platform); - const dimensions = isImage ? imageSize(assetData.files[0]) : null; - const scale = assetData.scales[0]; - - return { - __packager_asset: true, - fileSystemLocation: path.dirname(assetPath), - httpServerLocation: assetUrlPath, - width: dimensions ? dimensions.width / scale : undefined, - height: dimensions ? dimensions.height / scale : undefined, - scales: assetData.scales, - files: assetData.files, - hash: assetData.hash, - name: assetData.name, - type: assetData.type, - }; + return getAssetData(assetPath, localPath, platform); } /** diff --git a/packages/metro/src/AssetServer/util.js b/packages/metro/src/AssetServer/util.js index bc998dd7..93aafcc7 100644 --- a/packages/metro/src/AssetServer/util.js +++ b/packages/metro/src/AssetServer/util.js @@ -17,12 +17,16 @@ const AssetPaths = require('../node-haste/lib/AssetPaths'); const crypto = require('crypto'); const denodeify = require('denodeify'); const fs = require('fs'); +const imageSize = require('image-size'); const path = require('path'); +const {isAssetTypeAnImage} = require('../Bundler/util'); + const stat = denodeify(fs.stat); const readDir = denodeify(fs.readdir); import type {AssetPath} from '../node-haste/lib/AssetPaths'; +import type {AssetData} from './'; export type AssetInfo = {| files: Array, @@ -194,14 +198,46 @@ async function getAbsoluteAssetInfo( const hasher = crypto.createHash('md5'); if (files.length > 0) { - await hashFiles(files.slice(), hasher); + await hashFiles(Array.from(files), hasher); } return {files, hash: hasher.digest('hex'), name, scales, type}; } +async function getAssetData( + assetPath: string, + localPath: string, + platform: ?string = null, +): Promise { + let assetUrlPath = path.join('/assets', path.dirname(localPath)); + + // On Windows, change backslashes to slashes to get proper URL path from file path. + if (path.sep === '\\') { + assetUrlPath = assetUrlPath.replace(/\\/g, '/'); + } + + const isImage = isAssetTypeAnImage(path.extname(assetPath).slice(1)); + const assetData = await getAbsoluteAssetInfo(assetPath, platform); + const dimensions = isImage ? imageSize(assetData.files[0]) : null; + const scale = assetData.scales[0]; + + return { + __packager_asset: true, + fileSystemLocation: path.dirname(assetPath), + httpServerLocation: assetUrlPath, + width: dimensions ? dimensions.width / scale : undefined, + height: dimensions ? dimensions.height / scale : undefined, + scales: assetData.scales, + files: assetData.files, + hash: assetData.hash, + name: assetData.name, + type: assetData.type, + }; +} + module.exports = { findRoot, getAbsoluteAssetInfo, getAbsoluteAssetRecord, + getAssetData, };