From 01599ed55ebcf25c2a41a256f3b32c107effb8ec Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Thu, 29 Mar 2018 09:07:52 -0700 Subject: [PATCH] Send `X-Metro-Delta-ID` header Summary: Adds additional headers to delta responses: - The next delta ID. This helps to defer parsing the delta payload to a different layer. - The size of the delta. This can be used as a hint for memory allocation Reviewed By: jeanlauliac Differential Revision: D7428603 fbshipit-source-id: 1d5e840991ec8cb490d33b3bc97444b0d7a3a0f3 --- packages/metro/src/Server/__tests__/Server-test.js | 4 ++++ packages/metro/src/Server/index.js | 11 ++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/metro/src/Server/__tests__/Server-test.js b/packages/metro/src/Server/__tests__/Server-test.js index e1cd3199..7b161ac8 100644 --- a/packages/metro/src/Server/__tests__/Server-test.js +++ b/packages/metro/src/Server/__tests__/Server-test.js @@ -433,6 +433,8 @@ describe('processRequest', () => { ], reset: true, }); + + expect(response.headers['X-Metro-Delta-ID']).toEqual('XXXXX-0'); }); it('should generate an incremental delta correctly', async () => { @@ -469,6 +471,8 @@ describe('processRequest', () => { reset: false, }); + expect(response.headers['X-Metro-Delta-ID']).toEqual('XXXXX-1'); + expect(DeltaBundler.prototype.getDelta.mock.calls[0][1]).toEqual({ reset: false, }); diff --git a/packages/metro/src/Server/index.js b/packages/metro/src/Server/index.js index e0a1ddcc..7bf67944 100644 --- a/packages/metro/src/Server/index.js +++ b/packages/metro/src/Server/index.js @@ -84,6 +84,7 @@ function debounceAndBatch(fn, delay) { }; } +const DELTA_ID_HEADER = 'X-Metro-Delta-ID'; const FILES_CHANGED_COUNT_HEADER = 'X-Metro-Files-Changed-Count'; class Server { @@ -687,12 +688,11 @@ class Server { }), ); - let output; + let output, sequenceId; try { - const {delta, graph, prepend, sequenceId} = await this._getDeltaInfo( - options, - ); + let delta, graph, prepend; + ({delta, graph, prepend, sequenceId} = await this._getDeltaInfo(options)); output = { bundle: deltaJSBundle( @@ -724,7 +724,8 @@ class Server { } mres.setHeader(FILES_CHANGED_COUNT_HEADER, String(output.numModifiedFiles)); - mres.setHeader('Content-Type', 'application/javascript'); + mres.setHeader(DELTA_ID_HEADER, String(sequenceId)); + mres.setHeader('Content-Type', 'application/json'); mres.setHeader('Content-Length', String(Buffer.byteLength(output.bundle))); mres.end(output.bundle);