From 0608332cb935f794a3b3e79bfefd0a0fa7500571 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Thu, 12 Oct 2017 09:46:54 -0700 Subject: [PATCH] metro-bundler/src/ModuleGraph: throw on empty asset images Reviewed By: fkgozali Differential Revision: D6030788 fbshipit-source-id: 000757096f7af68c077e80829f48d52814752599 --- .../transform-module-test.js.snap | 3 +++ .../worker/__tests__/transform-module-test.js | 8 ++++++++ .../src/ModuleGraph/worker/transform-module.js | 18 ++++++++++++------ 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 packages/metro-bundler/src/ModuleGraph/worker/__tests__/__snapshots__/transform-module-test.js.snap diff --git a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/__snapshots__/transform-module-test.js.snap b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/__snapshots__/transform-module-test.js.snap new file mode 100644 index 00000000..028db6fa --- /dev/null +++ b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/__snapshots__/transform-module-test.js.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`transforming JS modules: assets throws on empty images 1`] = `"Image asset \`foo.png\` cannot be an empty file."`; diff --git a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js index 6757e09e..31e96044 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js @@ -210,6 +210,14 @@ describe('transforming JS modules:', () => { }); expect(result.details.package).toEqual(pkg); }); + + describe('assets', () => { + it('throws on empty images', () => { + expect(() => + transformModule(new Buffer(0), {...options(), filename: 'foo.png'}), + ).toThrowErrorMatchingSnapshot(); + }); + }); }); function createTestData() { diff --git a/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js b/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js index 07f3b83a..77ad3802 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js @@ -64,7 +64,7 @@ function transformModule( options: TransformOptions<{+retainLines?: boolean}>, ): TransformedSourceFile { if (ASSET_EXTENSIONS.has(path.extname(options.filename).substr(1))) { - return transformAsset(content, options); + return transformAsset(content, options.filename); } const code = content.toString('utf8'); @@ -148,11 +148,10 @@ function transformJSON(json, options): TransformedSourceFile { return {type: 'code', details: result}; } -function transformAsset( +function transformAsset( content: Buffer, - options: TransformOptions, + filePath: string, ): TransformedSourceFile { - const filePath = options.filename; const assetData = AssetPaths.parse(filePath, Platforms.VALID_PLATFORMS); const contentType = path.extname(filePath).slice(1); const details = { @@ -160,17 +159,24 @@ function transformAsset( contentBase64: content.toString('base64'), contentType, filePath, - physicalSize: getAssetSize(contentType, content), + physicalSize: getAssetSize(contentType, content, filePath), platform: assetData.platform, scale: assetData.resolution, }; return {details, type: 'asset'}; } -function getAssetSize(type: string, content: Buffer): ?ImageSize { +function getAssetSize( + type: string, + content: Buffer, + filePath: string, +): ?ImageSize { if (!isAssetTypeAnImage(type)) { return null; } + if (content.length === 0) { + throw new Error(`Image asset \`${filePath}\` cannot be an empty file.`); + } const {width, height} = getImageSize(content); return {width, height}; }