Skip to content

Commit bea57d8

Browse files
Alexandre Kirszenbergfacebook-github-bot
Alexandre Kirszenberg
authored andcommitted
DeltaPatcher: better support for the new Delta format
Summary: Adds support for the `deleted` key to remove modules from the bundle. Without this, source maps would break after removing a module, since it would still end up in the patched bundle but not in the source map. Reviewed By: mjesun Differential Revision: D12874011 fbshipit-source-id: 79239756854cb2c02f14ec8b0bb2b649766393fe
1 parent 0436bfc commit bea57d8

File tree

3 files changed

+131
-27
lines changed

3 files changed

+131
-27
lines changed

local-cli/server/util/debugger-ui/DeltaPatcher.js

+19-20
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,33 @@
6363

6464
// Reset the current bundle when we receive a base bundle.
6565
if (bundle.base) {
66+
this._lastNumModifiedFiles = bundle.modules.length;
67+
6668
this._lastBundle = {
67-
revisionId: undefined,
69+
revisionId: bundle.revisionId,
6870
pre: bundle.pre,
6971
post: bundle.post,
70-
modules: new Map(),
72+
modules: new Map(bundle.modules),
7173
};
72-
}
74+
} else {
75+
this._lastNumModifiedFiles =
76+
bundle.modules.length + bundle.deleted.length;
7377

74-
this._lastNumModifiedFiles = bundle.modules.size;
78+
this._lastBundle.revisionId = bundle.revisionId;
79+
80+
for (const [key, value] of bundle.modules) {
81+
this._lastBundle.modules.set(key, value);
82+
}
83+
84+
for (const id of bundle.deleted) {
85+
this._lastBundle.modules.delete(id);
86+
}
87+
}
7588

7689
if (this._lastNumModifiedFiles > 0) {
7790
this._lastModifiedDate = new Date();
7891
}
7992

80-
this._patchMap(this._lastBundle.modules, bundle.modules);
81-
82-
this._lastBundle.revisionId = bundle.revisionId;
83-
8493
return this;
8594
}
8695

@@ -104,21 +113,11 @@
104113

105114
getAllModules() {
106115
return [].concat(
107-
this._lastBundle.pre,
116+
[this._lastBundle.pre],
108117
Array.from(this._lastBundle.modules.values()),
109-
this._lastBundle.post,
118+
[this._lastBundle.post],
110119
);
111120
}
112-
113-
_patchMap(original, patch) {
114-
for (const [key, value] of patch.entries()) {
115-
if (value == null) {
116-
original.delete(key);
117-
} else {
118-
original.set(key, value);
119-
}
120-
}
121-
}
122121
}
123122

124123
DeltaPatcher._deltaPatchers = new Map();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @emails oncall+javascript_foundation
9+
*/
10+
11+
'use strict';
12+
13+
describe('DeltaPatcher', () => {
14+
const window = (global.window = {});
15+
require('../DeltaPatcher');
16+
17+
it('should initialize to an empty bundle', () => {
18+
global.Date = jest.fn();
19+
const dp = new window.DeltaPatcher();
20+
expect(dp.getLastRevisionId()).toBe(undefined);
21+
expect(dp.getLastModifiedDate()).toBe(global.Date.mock.instances[0]);
22+
expect(dp.getLastNumModifiedFiles()).toBe(0);
23+
// Empty pre and post.
24+
expect(dp.getAllModules()).toEqual(['', '']);
25+
});
26+
27+
it('should expect a base bundle at initialization', () => {
28+
const dp = new window.DeltaPatcher();
29+
expect(() => {
30+
dp.applyDelta({
31+
base: false,
32+
revisionId: 'hello',
33+
modules: [],
34+
deleted: [],
35+
});
36+
}).toThrow();
37+
});
38+
39+
it('should accept a base bundle at initialization', () => {
40+
const dp = new window.DeltaPatcher();
41+
global.Date = jest.fn();
42+
dp.applyDelta({
43+
base: true,
44+
revisionId: 'rev0',
45+
pre: 'pre0',
46+
post: 'post0',
47+
modules: [[0, '__d(0);']],
48+
});
49+
expect(dp.getLastRevisionId()).toBe('rev0');
50+
expect(dp.getLastModifiedDate()).toBe(global.Date.mock.instances[0]);
51+
expect(dp.getLastNumModifiedFiles()).toBe(1);
52+
expect(dp.getAllModules()).toEqual(['pre0', '__d(0);', 'post0']);
53+
});
54+
55+
it('should accept a delta bundle after a base bundle', () => {
56+
const dp = new window.DeltaPatcher();
57+
dp.applyDelta({
58+
base: true,
59+
revisionId: 'rev0',
60+
pre: 'pre0',
61+
post: 'post0',
62+
modules: [[0, '__d(0);'], [1, '__d(1);'], [2, '__d(2);']],
63+
});
64+
global.Date = jest.fn();
65+
dp.applyDelta({
66+
base: false,
67+
revisionId: 'rev1',
68+
modules: [[1, '__d(1.1);'], [3, '__d(3);']],
69+
deleted: [0],
70+
});
71+
expect(dp.getLastRevisionId()).toBe('rev1');
72+
expect(dp.getLastModifiedDate()).toBe(global.Date.mock.instances[0]);
73+
expect(dp.getLastNumModifiedFiles()).toBe(3);
74+
expect(dp.getAllModules()).toEqual([
75+
'pre0',
76+
'__d(1.1);',
77+
'__d(2);',
78+
'__d(3);',
79+
'post0',
80+
]);
81+
});
82+
83+
it('should accept a base bundle after initialization', () => {
84+
const dp = new window.DeltaPatcher();
85+
dp.applyDelta({
86+
base: true,
87+
revisionId: 'rev0',
88+
pre: 'pre0',
89+
post: 'post0',
90+
modules: [[0, '__d(0);'], [1, '__d(1);'], [2, '__d(2);']],
91+
});
92+
dp.applyDelta({
93+
base: false,
94+
revisionId: 'rev1',
95+
modules: [[1, '__d(1.1);'], [3, '__d(3);']],
96+
deleted: [0],
97+
});
98+
global.Date = jest.fn();
99+
dp.applyDelta({
100+
base: true,
101+
revisionId: 'rev2',
102+
pre: 'pre2',
103+
post: 'post2',
104+
modules: [[4, '__d(4);'], [5, '__d(5);']],
105+
});
106+
expect(dp.getLastRevisionId()).toBe('rev2');
107+
expect(dp.getLastModifiedDate()).toBe(global.Date.mock.instances[0]);
108+
expect(dp.getLastNumModifiedFiles()).toBe(2);
109+
expect(dp.getAllModules()).toEqual(['pre2', '__d(4);', '__d(5);', 'post2']);
110+
});
111+
});

local-cli/server/util/debugger-ui/deltaUrlToBlobUrl.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@
2929
const data = await fetch(deltaUrl + revisionId);
3030
const bundle = await data.json();
3131

32-
const deltaPatcher = client.applyDelta({
33-
base: bundle.base,
34-
revisionId: bundle.revisionId,
35-
pre: bundle.pre,
36-
post: bundle.post,
37-
modules: new Map(bundle.modules),
38-
});
32+
const deltaPatcher = client.applyDelta(bundle);
3933

4034
let cachedBundle = cachedBundleUrls.get(deltaUrl);
4135

0 commit comments

Comments
 (0)