From 72455941ba80b1a1d814fa2b07e09bc8e7149792 Mon Sep 17 00:00:00 2001 From: Ben Nham Date: Mon, 21 Aug 2017 04:05:14 -0700 Subject: [PATCH] add number of files changed to bundle response Reviewed By: davidaurelio Differential Revision: D5650181 fbshipit-source-id: 16c7842a79bec6cf4fb5bfeffc4bc8a835c7ce83 --- .../src/Server/__tests__/Server-test.js | 9 +++++ packages/metro-bundler/src/Server/index.js | 34 ++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/metro-bundler/src/Server/__tests__/Server-test.js b/packages/metro-bundler/src/Server/__tests__/Server-test.js index 4d143e8a..be602f55 100644 --- a/packages/metro-bundler/src/Server/__tests__/Server-test.js +++ b/packages/metro-bundler/src/Server/__tests__/Server-test.js @@ -118,6 +118,15 @@ describe('processRequest', () => { }); }); + it('returns build info headers on request of *.bundle', () => { + return makeRequest( + requestHandler, + 'mybundle.bundle?runModule=true' + ).then(response => { + expect(response.getHeader('X-Metro-Files-Changed-Count')).toBeDefined(); + }); + }); + it('returns Content-Length header on request of *.bundle', () => { return makeRequest( requestHandler, diff --git a/packages/metro-bundler/src/Server/index.js b/packages/metro-bundler/src/Server/index.js index a846b141..4aac9e94 100644 --- a/packages/metro-bundler/src/Server/index.js +++ b/packages/metro-bundler/src/Server/index.js @@ -126,6 +126,13 @@ type DependencyOptions = {| +recursive: boolean, |}; +type BuildInfo = {| + filesChangedCount: number, +|}; + +const FILES_CHANGED_COUNT_HEADER = 'X-Metro-Files-Changed-Count'; +const FILES_CHANGED_COUNT_REBUILD = -1; + const bundleDeps = new WeakMap(); const NODE_MODULES = `${path.sep}node_modules${path.sep}`; @@ -160,6 +167,7 @@ class Server { }; _projectRoots: $ReadOnlyArray; _bundles: {}; + _bundleBuildInfos: WeakMap; _changeWatchers: Array<{ req: IncomingMessage, res: ServerResponse, @@ -219,6 +227,7 @@ class Server { this._reporter = reporter; this._projectRoots = this._opts.projectRoots; this._bundles = Object.create(null); + this._bundleBuildInfos = new WeakMap(); this._changeWatchers = []; this._fileChangeListeners = []; this._platforms = new Set(this._opts.platforms); @@ -589,7 +598,12 @@ class Server { const bundleFromScratch = () => { const building = this.buildBundle(options); this._bundles[optionsJson] = building; - return building; + return building.then(bundle => { + this._bundleBuildInfos.set(bundle, { + filesChangedCount: FILES_CHANGED_COUNT_REBUILD, + }); + return bundle; + }); }; if (optionsJson in this._bundles) { @@ -678,6 +692,10 @@ class Server { log(createActionEndEntry(updatingExistingBundleLogEntry)); + this._bundleBuildInfos.set(bundle, { + filesChangedCount: outdated.size, + }); + debug('Successfully updated existing bundle'); return bundle; }); @@ -691,6 +709,10 @@ class Server { })); return this._reportBundlePromise(buildID, options, bundlePromise); } else { + this._bundleBuildInfos.set(bundle, { + filesChangedCount: 0, + }); + debug('Using cached bundle'); return bundle; } @@ -792,6 +814,7 @@ class Server { mres.writeHead(304); mres.end(); } else { + setBuildInfoHeaders(mres, this._bundleBuildInfos.get(p)); mres.setHeader('Content-Length', Buffer.byteLength(bundleSource)); mres.end(bundleSource); } @@ -1102,4 +1125,13 @@ function* zip(xs: Iterable, ys: Iterable): Iterable<[X, Y]> { } } +function setBuildInfoHeaders( + resp: MultipartResponse, + buildInfo: ?BuildInfo, +): void { + if (buildInfo) { + resp.setHeader(FILES_CHANGED_COUNT_HEADER, buildInfo.filesChangedCount); + } +} + module.exports = Server;