-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
client.js
417 lines (344 loc) · 11.1 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// TODO: add an api to Reify to update cached exports for a module
const ReifyEntry = require('/node_modules/meteor/modules/node_modules/reify/lib/runtime/entry.js')
const SOURCE_URL_PREFIX = "meteor://\u{1f4bb}app";
// Due to the bundler and proxy running in the same node process
// this could possibly be ran after the next build finished
// TODO: the builder should inject a build timestamp in the bundle
let lastUpdated = Date.now();
let appliedChangeSets = [];
let reloadId = 0;
let arch = __meteor_runtime_config__.isModern ? 'web.browser' : 'web.browser.legacy';
let enabled = arch === 'web.browser';
if (!enabled) {
console.log(`HMR is not supported in ${arch}`);
}
let pendingReload = () => Reload._reload({ immediateMigration: true });
let mustReload = false;
function handleMessage(message) {
if (message.type === 'register-failed') {
if (message.reason === 'wrong-app') {
console.log('HMR: A different app is running on', Meteor.absoluteUrl());
console.log('HMR: Once you start this app again reload the page to re-enable HMR');
} else if (message.reason === 'wrong-secret') {
// TODO: we could wait until the first update to use hot code push
// instead of reloading the page immediately in case the user has any
// client state they want to keep for now.
console.log('HMR: Have the wrong secret, possibly because Meteor was restarted');
console.log('HMR: Reloading page to get new secret');
mustReload = true;
pendingReload();
} else {
console.log(`HMR: Register failed for unknown reason`, message);
}
return;
}
if (message.type !== 'changes') {
throw new Error(`Unknown HMR message type ${message.type}`);
}
const hasUnreloadable = message.changeSets.find(changeSet => {
return !changeSet.reloadable;
});
if (
pendingReload &&
hasUnreloadable ||
message.changeSets.length === 0
) {
if (message.eager) {
// This was an attempt to reload before the build finishes
// If we can't, we will wait until the build finishes to properly handle it
return;
}
console.log('HMR: Unable to do HMR. Falling back to hot code push.')
// Complete hot code push if we can not do hot module reload
mustReload = true;
return pendingReload();
}
// In case the user changed how a module works with HMR
// in one of the earlier change sets, we want to apply each
// change set one at a time in order.
const succeeded = message.changeSets.filter(changeSet => {
return !appliedChangeSets.includes(changeSet.id)
}).every(changeSet => {
const applied = applyChangeset(changeSet, message.eager);
// We don't record if a module is unreplaceable
// during an eager update so we can retry and
// handle the failure after the build finishes
if (applied || !message.eager) {
appliedChangeSets.push(changeSet.id);
}
return applied;
});
if (message.eager) {
// We will ignore any failures at this time
// and wait to handle them until the build finishes
return;
}
if (!succeeded) {
if (pendingReload) {
mustReload = true;
return pendingReload();
}
throw new Error('HMR failed and unable to fallback to hot code push?');
}
if (message.changeSets.length > 0) {
lastUpdated = message.changeSets[message.changeSets.length - 1].linkedAt;
}
}
let socket;
let pendingMessages = [];
function send(message) {
if (socket) {
socket.send(JSON.stringify(message));
} else {
pendingMessages.push(message);
}
}
function connect() {
if (mustReload) {
// The page will reload, no reason to
// connect and show more logs in the console
return;
}
const wsUrl = Meteor.absoluteUrl('/__meteor__hmr__/websocket').replace(/^.+\/\//, 'ws://');
socket = new WebSocket(wsUrl);
socket.addEventListener('close', function () {
socket = null;
console.log('HMR: websocket closed');
setTimeout(connect, 2000);
});
socket.addEventListener('open', function () {
console.log('HMR: connected');
socket.send(JSON.stringify({
type: 'register',
arch,
secret: __meteor_runtime_config__._hmrSecret,
appId: __meteor_runtime_config__.appId,
}));
const toSend = pendingMessages.slice();
pendingMessages = [];
toSend.forEach(message => {
send(message);
});
});
socket.addEventListener('message', function (event) {
handleMessage(JSON.parse(event.data));
});
socket.addEventListener('error', console.error);
}
connect();
function requestChanges() {
send({
type: 'request-changes',
arch,
after: lastUpdated
});
}
function walkTree(pathParts, tree) {
const part = pathParts.shift();
const _module = tree.contents[part];
if (!_module) {
console.log(part, pathParts, _module, tree);
throw new Error('not-exist');
}
if (pathParts.length === 0) {
return _module;
}
return walkTree(pathParts, _module);
}
// btoa with unicode support
function utoa(data) {
return btoa(unescape(encodeURIComponent(data)));
}
function createInlineSourceMap(map) {
return "//# sourceMappingURL=data:application/json;base64," + utoa(JSON.stringify(map));
}
function createModuleContent (code, map) {
return function () {
return eval(
// Wrap the function(require,exports,module){...} expression in
// parentheses to force it to be parsed as an expression.
// The sourceURL is treated as a prefix for the sources array
// in the source map
"(" + code + ")\n//# sourceURL=" + SOURCE_URL_PREFIX +
"\n" + createInlineSourceMap(map)
).apply(this, arguments);
}
}
function replaceFileContent(file, contents) {
console.log('HMR: replacing module:', file.module.id);
// TODO: to replace content in packages, we need an eval function that runs
// within the package scope, like dynamic imports does.
const moduleFunction = createModuleContent(contents.code, contents.map, file.module.id);
file.contents = moduleFunction;
}
function rerunFile(file) {
delete file.module.exports;
file.module.loaded = false;
console.log('HMR: rerunning', file.module.id);
// re-eveluate the file
require(file.module.id);
}
const oldLink = module.constructor.prototype.link;
module.constructor.prototype.link = function (path) {
if (this._recordImport) {
this._recordImport(path);
};
return oldLink.apply(this, arguments);
}
function findReloadableParents(importedBy) {
return Object.values(importedBy).map(parentFile => {
if (parentFile.module.id === '/') {
// Have reached the install tree's root. None of the modules have accepted this change
return false;
}
// Force module to be rerun when we complete applying the changeset
parentFile.module.replaceModule();
const canAccept = parentFile.module.hot && parentFile.module.hot._canAcceptUpdate();
if (canAccept === true) {
return parentFile;
} else if (
canAccept === null && Object.keys(parentFile.importedBy).length > 0
) {
// When canAccept is null, whether it is reloadable or not depends on
// if its parents can accept changes.
return findReloadableParents(parentFile.importedBy);
} else {
return false;
}
}).flat(Infinity);
}
function addFiles(addedFiles) {
console.log('HMR: Added files', addedFiles.map(file => file.path));
addedFiles.forEach(file => {
const tree = {};
const segments = file.path.split('/').slice(1);
const fileName = segments.pop();
let previous = tree;
segments.forEach(segment => {
previous[segment] = previous[segment] || {}
previous = previous[segment]
});
previous[fileName] = createModuleContent(file.content.code, file.content.map, file.path);
meteorInstall(tree, file.meteorInstallOptions);
});
}
module.constructor.prototype.replaceModule = function (id, contents) {
const moduleId = id || this.id;
const root = this._getRoot();
let file;
try {
file = walkTree(moduleId.split('/').slice(1), root);
} catch (e) {
if (e.message === 'not-exist') {
return null;
}
throw e;
}
if (!file.contents) {
// File is a dynamic import that hasn't been loaded
return null;
}
const hotState = file.module._hotState;
if (file._reloadedAt !== reloadId && hotState) {
file._reloadedAt = reloadId;
const hotData = {};
hotState._disposeHandlers.forEach(cb => {
cb(hotData);
});
hotState._disposeHandlers = [];
hotState.data = hotData;
}
if (contents) {
replaceFileContent(file, contents);
}
if (!file.module.exports) {
// File hasn't been imported.
return null;
}
// Clear cached exports
// TODO: check how this affects live bindings for ecmascript modules
delete file.module.exports;
const entry = ReifyEntry.getOrCreate(moduleId);
entry.getters = {};
entry.setters = {};
entry.module = null;
Object.keys(entry.namespace).forEach(key => {
if (key !== '__esModule') {
delete entry.namespace[key];
}
});
return file;
}
function applyChangeset({
changedFiles,
addedFiles
}) {
const reloadableParents = [];
let hasImportedModules = false;
changedFiles.forEach(({ content, path }) => {
const file = module.replaceModule(path, content);
if (file) {
hasImportedModules = true;
reloadableParents.push(...findReloadableParents({ self: file }));
} else {
console.log(`HMR: Updated file that hasn't been imported: ${path}`);
}
});
if (addedFiles.length > 0) {
addFiles(addedFiles);
}
// Check if some of the module's parents are not reloadable
// In that case, we have to do a full reload
// TODO: record which parents cause this
if (
hasImportedModules &&
reloadableParents.length === 0 ||
reloadableParents.some(parent => !parent)
) {
return false;
}
reloadId += 1;
// TODO: handle errors
const evaluated = [];
reloadableParents.forEach(parent => {
if (evaluated.includes(parent.module.id)) {
return;
}
evaluated.push(parent.module.id);
rerunFile(parent);
});
console.log('HMR: finished updating');
return true;
}
const initialVersions = (__meteor_runtime_config__.autoupdate.versions || {})['web.browser'];
let nonRefreshableVersion = initialVersions.versionNonRefreshable;
let replaceableVersion = initialVersions.versionReplaceable;
Meteor.startup(() => {
if (!enabled) {
return;
}
Package['autoupdate'].Autoupdate._clientVersions.watch((doc) => {
if (doc._id !== 'web.browser') {
return;
}
if (nonRefreshableVersion !== doc.versionNonRefreshable) {
nonRefreshableVersion = doc.versionNonRefreshable;
console.log('HMR: Some changes can not be applied with HMR. Using hot code push.')
mustReload = true;
pendingReload();
} else if (doc.versionReplaceable !== replaceableVersion) {
replaceableVersion = doc.versionReplaceable;
requestChanges();
}
});
// We disable hot code push for js until there were
// changes that can not be applied through HMR.
Package['reload'].Reload._onMigrate((tryReload) => {
if (mustReload) {
return [true];
}
pendingReload = tryReload;
requestChanges();
return [false];
});
});