Remove getAssetData() method from AssetServer

Reviewed By: mjesun

Differential Revision: D6488537

fbshipit-source-id: 48c27e9cf5802c62fb25aac592ac1c5bcd6cc87f
This commit is contained in:
Rafael Oleza 2017-12-06 07:21:56 -08:00 committed by Facebook Github Bot
parent 3721464107
commit 5bff35f09e
11 changed files with 196 additions and 171 deletions

View File

@ -16,7 +16,6 @@ jest.mock('fs');
jest.mock('image-size'); jest.mock('image-size');
const AssetServer = require('../'); const AssetServer = require('../');
const crypto = require('crypto');
const fs = require('fs'); const fs = require('fs');
require('image-size').mockReturnValue({ require('image-size').mockReturnValue({
@ -24,8 +23,6 @@ require('image-size').mockReturnValue({
height: 200, height: 200,
}); });
const {objectContaining} = jasmine;
describe('AssetServer', () => { describe('AssetServer', () => {
describe('assetServer.get', () => { describe('assetServer.get', () => {
it('should work for the simple case', () => { it('should work for the simple case', () => {
@ -177,123 +174,4 @@ describe('AssetServer', () => {
.then(data => expect(data).toBe('b1 image')); .then(data => expect(data).toBe('b1 image'));
}); });
}); });
describe('assetServer.getAssetData', () => {
it('should get assetData', () => {
const server = new AssetServer({
projectRoots: ['/root'],
});
fs.__setMockFilesystem({
root: {
imgs: {
'b@1x.png': 'b1 image',
'b@2x.png': 'b2 image',
'b@4x.png': 'b4 image',
'b@4.5x.png': 'b4.5 image',
},
},
});
return server.getAssetData('/root/imgs/b.png').then(data => {
expect(data).toEqual(
objectContaining({
__packager_asset: true,
type: 'png',
name: 'b',
scales: [1, 2, 4, 4.5],
fileSystemLocation: '/root/imgs',
httpServerLocation: '/assets/imgs',
files: [
'/root/imgs/b@1x.png',
'/root/imgs/b@2x.png',
'/root/imgs/b@4x.png',
'/root/imgs/b@4.5x.png',
],
}),
);
});
});
it('should get assetData for non-png images', () => {
const server = new AssetServer({
projectRoots: ['/root'],
});
fs.__setMockFilesystem({
root: {
imgs: {
'b@1x.jpg': 'b1 image',
'b@2x.jpg': 'b2 image',
'b@4x.jpg': 'b4 image',
'b@4.5x.jpg': 'b4.5 image',
},
},
});
return server.getAssetData('/root/imgs/b.jpg').then(data => {
expect(data).toEqual(
objectContaining({
__packager_asset: true,
type: 'jpg',
name: 'b',
scales: [1, 2, 4, 4.5],
fileSystemLocation: '/root/imgs',
httpServerLocation: '/assets/imgs',
files: [
'/root/imgs/b@1x.jpg',
'/root/imgs/b@2x.jpg',
'/root/imgs/b@4x.jpg',
'/root/imgs/b@4.5x.jpg',
],
}),
);
});
});
describe('hash:', () => {
let server, mockFS;
beforeEach(() => {
server = new AssetServer({
projectRoots: ['/root'],
});
mockFS = {
root: {
imgs: {
'b@1x.jpg': 'b1 image',
'b@2x.jpg': 'b2 image',
'b@4x.jpg': 'b4 image',
'b@4.5x.jpg': 'b4.5 image',
},
},
};
fs.__setMockFilesystem(mockFS);
});
it('uses the file contents to build the hash', () => {
const hash = crypto.createHash('md5');
for (const name in mockFS.root.imgs) {
hash.update(mockFS.root.imgs[name]);
}
return server
.getAssetData('/root/imgs/b.jpg')
.then(data =>
expect(data).toEqual(objectContaining({hash: hash.digest('hex')})),
);
});
it('changes the hash when the passed-in file watcher emits an `all` event', () => {
return server.getAssetData('/root/imgs/b.jpg').then(initialData => {
mockFS.root.imgs['b@4x.jpg'] = 'updated data';
return server
.getAssetData('/root/imgs/b.jpg')
.then(data => expect(data.hash).not.toEqual(initialData.hash));
});
});
});
});
}); });

View File

@ -0,0 +1,131 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+javascript_foundation
* @format
*/
'use strict';
jest.mock('fs');
jest.mock('image-size');
const {getAssetData} = require('../util');
const crypto = require('crypto');
const fs = require('fs');
require('image-size').mockReturnValue({
width: 300,
height: 200,
});
describe('getAssetData', () => {
it('should get assetData', () => {
fs.__setMockFilesystem({
root: {
imgs: {
'b@1x.png': 'b1 image',
'b@2x.png': 'b2 image',
'b@4x.png': 'b4 image',
'b@4.5x.png': 'b4.5 image',
},
},
});
return getAssetData('/root/imgs/b.png', 'imgs/b.png').then(data => {
expect(data).toEqual(
expect.objectContaining({
__packager_asset: true,
type: 'png',
name: 'b',
scales: [1, 2, 4, 4.5],
fileSystemLocation: '/root/imgs',
httpServerLocation: '/assets/imgs',
files: [
'/root/imgs/b@1x.png',
'/root/imgs/b@2x.png',
'/root/imgs/b@4x.png',
'/root/imgs/b@4.5x.png',
],
}),
);
});
});
it('should get assetData for non-png images', async () => {
fs.__setMockFilesystem({
root: {
imgs: {
'b@1x.jpg': 'b1 image',
'b@2x.jpg': 'b2 image',
'b@4x.jpg': 'b4 image',
'b@4.5x.jpg': 'b4.5 image',
},
},
});
const data = await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg');
expect(data).toEqual(
expect.objectContaining({
__packager_asset: true,
type: 'jpg',
name: 'b',
scales: [1, 2, 4, 4.5],
fileSystemLocation: '/root/imgs',
httpServerLocation: '/assets/imgs',
files: [
'/root/imgs/b@1x.jpg',
'/root/imgs/b@2x.jpg',
'/root/imgs/b@4x.jpg',
'/root/imgs/b@4.5x.jpg',
],
}),
);
});
describe('hash:', () => {
let mockFS;
beforeEach(() => {
mockFS = {
root: {
imgs: {
'b@1x.jpg': 'b1 image',
'b@2x.jpg': 'b2 image',
'b@4x.jpg': 'b4 image',
'b@4.5x.jpg': 'b4.5 image',
},
},
};
fs.__setMockFilesystem(mockFS);
});
it('uses the file contents to build the hash', async () => {
const hash = crypto.createHash('md5');
for (const name in mockFS.root.imgs) {
hash.update(mockFS.root.imgs[name]);
}
expect(await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg')).toEqual(
expect.objectContaining({hash: hash.digest('hex')}),
);
});
it('changes the hash when the passed-in file watcher emits an `all` event', async () => {
const initialData = await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg');
mockFS.root.imgs['b@4x.jpg'] = 'updated data';
const data = await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg');
expect(data.hash).not.toEqual(initialData.hash);
});
});
});

View File

@ -17,9 +17,8 @@ const AssetPaths = require('../node-haste/lib/AssetPaths');
const denodeify = require('denodeify'); const denodeify = require('denodeify');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const toLocalPath = require('../node-haste/lib/toLocalPath');
const {findRoot, getAbsoluteAssetRecord, getAssetData} = require('./util'); const {findRoot, getAbsoluteAssetRecord} = require('./util');
export type AssetData = {| export type AssetData = {|
__packager_asset: boolean, __packager_asset: boolean,
@ -59,15 +58,6 @@ class AssetServer {
}); });
} }
async getAssetData(
assetPath: string,
platform: ?string = null,
): Promise<AssetData> {
const localPath = toLocalPath(this._roots, assetPath);
return getAssetData(assetPath, localPath, platform);
}
/** /**
* Given a request for an image by path. That could contain a resolution * Given a request for an image by path. That could contain a resolution
* postfix, we need to find that image (or the closest one to it's resolution) * postfix, we need to find that image (or the closest one to it's resolution)

View File

@ -235,9 +235,18 @@ async function getAssetData(
}; };
} }
async function getAssetFiles(
assetPath: string,
platform: ?string = null,
): Promise<Array<string>> {
const assetData = await getAbsoluteAssetInfo(assetPath, platform);
return assetData.files;
}
module.exports = { module.exports = {
findRoot, findRoot,
getAbsoluteAssetInfo,
getAbsoluteAssetRecord, getAbsoluteAssetRecord,
getAssetData, getAssetData,
getAssetFiles,
}; };

View File

@ -89,7 +89,7 @@ export type PostProcessBundleSourcemap = ({
outFileName: string, outFileName: string,
}) => {code: Buffer | string, map: SourceMap | string}; }) => {code: Buffer | string, map: SourceMap | string};
type Options = {| export type Options = {|
+assetExts: Array<string>, +assetExts: Array<string>,
+assetRegistryPath: string, +assetRegistryPath: string,
+assetServer: AssetServer, +assetServer: AssetServer,
@ -226,8 +226,8 @@ class Bundler {
this._getTransformOptions = opts.getTransformOptions; this._getTransformOptions = opts.getTransformOptions;
} }
getAssetServer(): AssetServer { getOptions(): Options {
return this._assetServer; return this._opts;
} }
async end() { async end() {

View File

@ -14,6 +14,9 @@
const DeltaPatcher = require('./DeltaPatcher'); const DeltaPatcher = require('./DeltaPatcher');
const toLocalPath = require('../node-haste/lib/toLocalPath');
const {getAssetData} = require('../AssetServer/util');
const {fromRawMappings} = require('../Bundler/source-map'); const {fromRawMappings} = require('../Bundler/source-map');
const {createRamBundleGroups} = require('../Bundler/util'); const {createRamBundleGroups} = require('../Bundler/util');
@ -230,9 +233,12 @@ async function getAssets(
const assets = await Promise.all( const assets = await Promise.all(
modules.map(async module => { modules.map(async module => {
if (module.type === 'asset') { if (module.type === 'asset') {
return await deltaBundler const localPath = toLocalPath(
.getAssetServer() deltaBundler.getOptions().projectRoots,
.getAssetData(module.path, options.platform); module.path,
);
return getAssetData(module.path, localPath, options.platform);
} }
return null; return null;
}), }),

View File

@ -12,6 +12,12 @@
'use strict'; 'use strict';
jest.mock('../../node-haste/lib/toLocalPath');
jest.mock('../../AssetServer/util');
const {getAssetData} = require('../../AssetServer/util');
const toLocalPath = require('../../node-haste/lib/toLocalPath');
const CURRENT_TIME = 1482363367000; const CURRENT_TIME = 1482363367000;
describe('Serializers', () => { describe('Serializers', () => {
@ -62,18 +68,24 @@ describe('Serializers', () => {
}, },
}; };
}, },
getAssetServer() {
return {
async getAssetData(path, platform) {
return {path, platform, assetData: true};
},
};
},
getPostProcessModulesFn() { getPostProcessModulesFn() {
return postProcessModules; return postProcessModules;
}, },
getOptions() {
return {
projectRoots: ['/foo'],
};
},
}; };
getAssetData.mockImplementation((path, localPath, platform) => ({
path,
platform,
assetData: true,
}));
toLocalPath.mockImplementation((roots, path) => path.replace(roots[0], ''));
setCurrentTime(CURRENT_TIME); setCurrentTime(CURRENT_TIME);
}); });

View File

@ -14,7 +14,7 @@
const DeltaTransformer = require('./DeltaTransformer'); const DeltaTransformer = require('./DeltaTransformer');
import type Bundler from '../Bundler'; import type Bundler, {Options as ServerOptions} from '../Bundler';
import type {BundleOptions} from '../shared/types.flow'; import type {BundleOptions} from '../shared/types.flow';
import type {DeltaEntry} from './DeltaTransformer'; import type {DeltaEntry} from './DeltaTransformer';
@ -56,8 +56,8 @@ class DeltaBundler {
this._deltaTransformers = new Map(); this._deltaTransformers = new Map();
} }
getAssetServer() { getOptions(): ServerOptions {
return this._bundler.getAssetServer(); return this._bundler.getOptions();
} }
async getDeltaTransformer( async getDeltaTransformer(

View File

@ -268,11 +268,7 @@ class Server {
).platform; ).platform;
} }
return await getOrderedDependencyPaths( return await getOrderedDependencyPaths(this._deltaBundler, bundleOptions);
this._deltaBundler,
this._assetServer,
bundleOptions,
);
} }
onFileChange(type: string, filePath: string) { onFileChange(type: string, filePath: string) {

View File

@ -13,20 +13,21 @@
'use strict'; 'use strict';
jest.mock('../../DeltaBundler/Serializers'); jest.mock('../../DeltaBundler/Serializers');
jest.mock('../../AssetServer/util');
const getOrderedDependencyPaths = require('../getOrderedDependencyPaths'); const getOrderedDependencyPaths = require('../getOrderedDependencyPaths');
const Serializers = require('../../DeltaBundler/Serializers'); const Serializers = require('../../DeltaBundler/Serializers');
const {getAssetFiles} = require('../../AssetServer/util');
describe('getOrderedDependencyPaths', () => { describe('getOrderedDependencyPaths', () => {
const assetsServer = {
getAssetData: jest.fn(),
};
const deltaBundler = {}; const deltaBundler = {};
beforeEach(() => { beforeEach(() => {
assetsServer.getAssetData.mockImplementation(async path => ({ getAssetFiles.mockImplementation(async path => [
files: [`${path}@2x`, `${path}@3x`], `${path}@2x`,
})); `${path}@3x`,
]);
}); });
it('Should return all module dependencies correctly', async () => { it('Should return all module dependencies correctly', async () => {
@ -42,11 +43,13 @@ describe('getOrderedDependencyPaths', () => {
); );
expect( expect(
await getOrderedDependencyPaths(deltaBundler, assetsServer, ['/tmp'], {}), await getOrderedDependencyPaths(deltaBundler, ['/tmp'], {}),
).toEqual(['/tmp/1.js', '/tmp/2.js', '/tmp/3.js', '/tmp/4.js']); ).toEqual(['/tmp/1.js', '/tmp/2.js', '/tmp/3.js', '/tmp/4.js']);
}); });
it('Should add assets data dependencies correctly', async () => { it('Should add assets data dependencies correctly', async () => {
deltaBundler.getOptions = () => ({projectRoots: ['/root']});
Serializers.getAllModules.mockReturnValue( Serializers.getAllModules.mockReturnValue(
Promise.resolve( Promise.resolve(
new Map([ new Map([
@ -60,7 +63,7 @@ describe('getOrderedDependencyPaths', () => {
); );
expect( expect(
await getOrderedDependencyPaths(deltaBundler, assetsServer, ['/tmp'], {}), await getOrderedDependencyPaths(deltaBundler, ['/tmp'], {}),
).toEqual([ ).toEqual([
'/tmp/1.js', '/tmp/1.js',
'/tmp/2.png@2x', '/tmp/2.png@2x',
@ -70,5 +73,10 @@ describe('getOrderedDependencyPaths', () => {
'/tmp/4.png@3x', '/tmp/4.png@3x',
'/tmp/5.js', '/tmp/5.js',
]); ]);
expect(getAssetFiles.mock.calls).toEqual([
['/tmp/2.png', undefined],
['/tmp/4.png', undefined],
]);
}); });
}); });

View File

@ -14,13 +14,13 @@
const Serializers = require('../DeltaBundler/Serializers'); const Serializers = require('../DeltaBundler/Serializers');
import type AssetsServer from '../AssetServer'; const {getAssetFiles} = require('../AssetServer/util');
import type {Options} from '../DeltaBundler/Serializers'; import type {Options} from '../DeltaBundler/Serializers';
import type DeltaBundler from '../DeltaBundler'; import type DeltaBundler from '../DeltaBundler';
async function getOrderedDependencyPaths( async function getOrderedDependencyPaths(
deltaBundler: DeltaBundler, deltaBundler: DeltaBundler,
assetsServer: AssetsServer,
options: Options, options: Options,
): Promise<Array<string>> { ): Promise<Array<string>> {
const modules = await Serializers.getAllModules(deltaBundler, options); const modules = await Serializers.getAllModules(deltaBundler, options);
@ -30,12 +30,7 @@ async function getOrderedDependencyPaths(
if (module.type !== 'asset') { if (module.type !== 'asset') {
return [module.path]; return [module.path];
} else { } else {
const assetData = await assetsServer.getAssetData( return await getAssetFiles(module.path, options.platform);
module.path,
options.platform,
);
return assetData.files;
} }
}), }),
); );