add number of files changed to bundle response

Reviewed By: davidaurelio

Differential Revision: D5650181

fbshipit-source-id: 16c7842a79bec6cf4fb5bfeffc4bc8a835c7ce83
This commit is contained in:
Ben Nham 2017-08-21 04:05:14 -07:00 committed by Facebook Github Bot
parent 6d4b8e0c8d
commit 72455941ba
2 changed files with 42 additions and 1 deletions

View File

@ -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', () => { it('returns Content-Length header on request of *.bundle', () => {
return makeRequest( return makeRequest(
requestHandler, requestHandler,

View File

@ -126,6 +126,13 @@ type DependencyOptions = {|
+recursive: boolean, +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 bundleDeps = new WeakMap();
const NODE_MODULES = `${path.sep}node_modules${path.sep}`; const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
@ -160,6 +167,7 @@ class Server {
}; };
_projectRoots: $ReadOnlyArray<string>; _projectRoots: $ReadOnlyArray<string>;
_bundles: {}; _bundles: {};
_bundleBuildInfos: WeakMap<Bundle, BuildInfo>;
_changeWatchers: Array<{ _changeWatchers: Array<{
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
@ -219,6 +227,7 @@ class Server {
this._reporter = reporter; this._reporter = reporter;
this._projectRoots = this._opts.projectRoots; this._projectRoots = this._opts.projectRoots;
this._bundles = Object.create(null); this._bundles = Object.create(null);
this._bundleBuildInfos = new WeakMap();
this._changeWatchers = []; this._changeWatchers = [];
this._fileChangeListeners = []; this._fileChangeListeners = [];
this._platforms = new Set(this._opts.platforms); this._platforms = new Set(this._opts.platforms);
@ -589,7 +598,12 @@ class Server {
const bundleFromScratch = () => { const bundleFromScratch = () => {
const building = this.buildBundle(options); const building = this.buildBundle(options);
this._bundles[optionsJson] = building; 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) { if (optionsJson in this._bundles) {
@ -678,6 +692,10 @@ class Server {
log(createActionEndEntry(updatingExistingBundleLogEntry)); log(createActionEndEntry(updatingExistingBundleLogEntry));
this._bundleBuildInfos.set(bundle, {
filesChangedCount: outdated.size,
});
debug('Successfully updated existing bundle'); debug('Successfully updated existing bundle');
return bundle; return bundle;
}); });
@ -691,6 +709,10 @@ class Server {
})); }));
return this._reportBundlePromise(buildID, options, bundlePromise); return this._reportBundlePromise(buildID, options, bundlePromise);
} else { } else {
this._bundleBuildInfos.set(bundle, {
filesChangedCount: 0,
});
debug('Using cached bundle'); debug('Using cached bundle');
return bundle; return bundle;
} }
@ -792,6 +814,7 @@ class Server {
mres.writeHead(304); mres.writeHead(304);
mres.end(); mres.end();
} else { } else {
setBuildInfoHeaders(mres, this._bundleBuildInfos.get(p));
mres.setHeader('Content-Length', Buffer.byteLength(bundleSource)); mres.setHeader('Content-Length', Buffer.byteLength(bundleSource));
mres.end(bundleSource); mres.end(bundleSource);
} }
@ -1102,4 +1125,13 @@ function* zip<X, Y>(xs: Iterable<X>, ys: Iterable<Y>): Iterable<[X, Y]> {
} }
} }
function setBuildInfoHeaders(
resp: MultipartResponse,
buildInfo: ?BuildInfo,
): void {
if (buildInfo) {
resp.setHeader(FILES_CHANGED_COUNT_HEADER, buildInfo.filesChangedCount);
}
}
module.exports = Server; module.exports = Server;