This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathPackagerLogsStream.ts
540 lines (492 loc) · 15.6 KB
/
PackagerLogsStream.ts
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import { JSONObject } from '@expo/json-file';
import chalk from 'chalk';
import getenv from 'getenv';
import path from 'path';
import Logger from '../Logger';
import * as ProjectUtils from '../project/ProjectUtils';
type BuildEventType =
| 'METRO_INITIALIZE_STARTED'
| 'BUILD_STARTED'
| 'BUILD_PROGRESS'
| 'BUILD_FAILED'
| 'BUILD_DONE';
type MetroLogRecord = {
tag: 'metro';
id: string;
shouldHide: boolean;
msg: ReportableEvent | string;
level: number;
_metroEventType?: BuildEventType;
};
type ExpoLogRecord = {
tag: 'expo';
id: string;
shouldHide: boolean;
msg: any;
level: number;
};
type DeviceLogRecord = {
tag: 'device';
id: string;
shouldHide: boolean;
msg: any;
level: number;
};
export type LogRecord = (MetroLogRecord | ExpoLogRecord | DeviceLogRecord) & ProjectUtils.LogFields;
export type LogUpdater = (logState: Array<LogRecord>) => Array<LogRecord>;
type ErrorObject = {
name?: string;
stack?: string;
message?: string;
code?: string;
} & JSONObject;
type MetroError =
| ({
originModulePath: string;
message: string;
errors: Array<Object>;
} & ErrorObject)
| ({
type: 'TransformError';
snippet: string;
lineNumber: number;
column: number;
filename: string;
errors: Array<Object>;
} & ErrorObject)
| ErrorObject;
// Metro reporter types
// https://github.com/facebook/metro/blob/2a327fb19dd62169ab3ae9069011d8a599cfcf3e/packages/metro/src/lib/reporting.js
type GlobalCacheDisabledReason = 'too_many_errors' | 'too_many_misses';
type BundleDetails = {
entryFile: string;
platform: string;
dev: boolean;
minify: boolean;
bundleType: string;
};
type ReportableEvent =
| {
port: number | undefined;
projectRoots: ReadonlyArray<string>;
type: 'initialize_started';
}
| {
type: 'initialize_done';
}
| {
type: 'initialize_failed';
port: number;
error: MetroError;
}
| {
buildID: string;
type: 'bundle_build_done';
}
| {
buildID: string;
type: 'bundle_build_failed';
}
| {
buildID: string;
bundleDetails: BundleDetails;
type: 'bundle_build_started';
}
| {
error: MetroError;
type: 'bundling_error';
}
| {
type: 'dep_graph_loading';
}
| {
type: 'dep_graph_loaded';
}
| {
buildID: string;
type: 'bundle_transform_progressed';
transformedFileCount: number;
totalFileCount: number;
}
| {
type: 'global_cache_error';
error: MetroError;
}
| {
type: 'global_cache_disabled';
reason: GlobalCacheDisabledReason;
}
| {
type: 'transform_cache_reset';
}
| {
type: 'worker_stdout_chunk';
chunk: string;
}
| {
type: 'worker_stderr_chunk';
chunk: string;
}
| {
type: 'hmr_client_error';
error: MetroError;
};
type StartBuildBundleCallback = (chunk: LogRecord) => void;
type ProgressBuildBundleCallback = (progress: number, start: Date | null, chunk: any) => void;
type FinishBuildBundleCallback = (
error: string | null,
start: Date,
end: Date,
chunk: MetroLogRecord
) => void;
export default class PackagerLogsStream {
_projectRoot: string;
_getCurrentOpenProjectId: () => any;
_updateLogs: (updater: LogUpdater) => void;
_logsToAdd: Array<LogRecord> = [];
_bundleBuildChunkID: string | null = null;
_onStartBuildBundle?: StartBuildBundleCallback;
_onProgressBuildBundle?: ProgressBuildBundleCallback;
_onFinishBuildBundle?: FinishBuildBundleCallback;
_bundleBuildStart: Date | null = null;
_getSnippetForError?: (error: MetroError) => string | null;
constructor({
projectRoot,
getCurrentOpenProjectId,
updateLogs,
onStartBuildBundle,
onProgressBuildBundle,
onFinishBuildBundle,
getSnippetForError,
}: {
projectRoot: string;
getCurrentOpenProjectId?: () => any;
updateLogs: (updater: LogUpdater) => void;
onStartBuildBundle?: StartBuildBundleCallback;
onProgressBuildBundle?: ProgressBuildBundleCallback;
onFinishBuildBundle?: FinishBuildBundleCallback;
getSnippetForError?: (error: MetroError) => string | null;
}) {
this._projectRoot = projectRoot;
this._getCurrentOpenProjectId = getCurrentOpenProjectId || (() => 1);
this._updateLogs = updateLogs;
// Optional properties in case the consumer wants to handle updates on
// its own, eg: for a progress bar
this._onStartBuildBundle = onStartBuildBundle;
this._onProgressBuildBundle = onProgressBuildBundle;
this._onFinishBuildBundle = onFinishBuildBundle;
// Optional function for creating custom code frame snippet
// (e.g. with terminal colors) from a syntax error.
this._getSnippetForError = getSnippetForError;
this._attachLoggerStream();
}
_attachLoggerStream() {
let projectId = this._getCurrentOpenProjectId();
ProjectUtils.attachLoggerStream(this._projectRoot, {
stream: {
write: (chunk: LogRecord) => {
if (chunk.tag !== 'metro' && chunk.tag !== 'expo') {
return;
} else if (this._getCurrentOpenProjectId() !== projectId) {
// TODO: We should be confident that we are properly unsubscribing
// from the stream rather than doing a defensive check like this.
return;
}
chunk = this._maybeParseMsgJSON(chunk);
chunk = this._cleanUpNodeErrors(chunk);
if (chunk.tag === 'metro') {
this._handleMetroEvent(chunk);
} else if (
typeof chunk.msg === 'string' &&
chunk.msg.match(/\w/) &&
chunk.msg[0] !== '{'
) {
this._enqueueAppendLogChunk(chunk);
}
},
},
type: 'raw',
});
}
_handleMetroEvent(originalChunk: MetroLogRecord) {
const chunk = { ...originalChunk };
let { msg } = chunk;
if (typeof msg === 'string') {
if ((msg as string).includes('HTTP/1.1') && !getenv.boolish('EXPO_DEBUG', false)) {
// Do nothing with this message - we want to filter out network requests logged by Metro.
} else {
// If Metro crashes for some reason, it may log an error message as a plain string to stderr.
this._enqueueAppendLogChunk(chunk);
}
return;
}
switch (msg.type) {
// Bundle transform events
case 'bundle_build_started':
case 'bundle_transform_progressed':
case 'bundle_build_failed':
case 'bundle_build_done':
this._handleBundleTransformEvent(chunk);
return;
case 'initialize_started':
chunk._metroEventType = 'METRO_INITIALIZE_STARTED';
chunk.msg = msg.port
? `Starting Metro Bundler on port ${msg.port}.`
: 'Starting Metro Bundler.';
break;
case 'initialize_done':
chunk.msg = `Metro Bundler ready.`;
break;
case 'initialize_failed': {
// SDK <=22
let code = msg.error.code;
chunk.msg =
code === 'EADDRINUSE'
? `Metro Bundler can't listen on port ${msg.port}. The port is in use.`
: `Metro Bundler failed to start. (code: ${code})`;
break;
}
case 'bundling_error':
chunk.msg =
this._formatModuleResolutionError(msg.error) ||
this._formatBundlingError(msg.error) ||
msg;
chunk.level = Logger.ERROR;
break;
case 'transform_cache_reset':
chunk.msg =
'Your JavaScript transform cache is empty, rebuilding (this may take a minute).';
break;
case 'hmr_client_error':
chunk.msg = `A WebSocket client got a connection error. Please reload your device to get HMR working again.`;
break;
case 'global_cache_disabled':
if (msg.reason === 'too_many_errors') {
chunk.msg =
'The global cache is now disabled because it has been failing too many times.';
} else if (msg.reason === 'too_many_misses') {
chunk.msg = `The global cache is now disabled because it has been missing too many consecutive keys.`;
} else {
chunk.msg = `The global cache is now disabled. Reason: ${msg.reason}`;
}
break;
case 'worker_stdout_chunk':
chunk.msg = this._formatWorkerChunk('stdout', msg.chunk);
break;
case 'worker_stderr_chunk':
chunk.msg = this._formatWorkerChunk('stderr', msg.chunk);
break;
// Ignored events.
case 'dep_graph_loading':
case 'dep_graph_loaded':
case 'global_cache_error':
return;
default:
chunk.msg = `Unrecognized event: ${JSON.stringify(msg)}`;
break;
}
this._enqueueAppendLogChunk(chunk);
}
// Any event related to bundle building is handled here
_handleBundleTransformEvent = (chunk: MetroLogRecord) => {
const msg = chunk.msg as ReportableEvent;
if (msg.type === 'bundle_build_started') {
chunk._metroEventType = 'BUILD_STARTED';
this._handleNewBundleTransformStarted(chunk);
} else if (msg.type === 'bundle_transform_progressed' && this._bundleBuildChunkID) {
chunk._metroEventType = 'BUILD_PROGRESS';
this._handleUpdateBundleTransformProgress(chunk);
} else if (msg.type === 'bundle_build_failed' && this._bundleBuildChunkID) {
chunk._metroEventType = 'BUILD_FAILED';
this._handleUpdateBundleTransformProgress(chunk);
} else if (msg.type === 'bundle_build_done' && this._bundleBuildChunkID) {
chunk._metroEventType = 'BUILD_DONE';
this._handleUpdateBundleTransformProgress(chunk);
}
};
_handleNewBundleTransformStarted(chunk: MetroLogRecord) {
if (this._bundleBuildChunkID) {
return;
}
this._bundleBuildChunkID = chunk.id;
this._bundleBuildStart = new Date();
chunk.msg = 'Building JavaScript bundle';
if (this._onStartBuildBundle) {
this._onStartBuildBundle(chunk);
} else {
this._enqueueAppendLogChunk(chunk);
}
}
_handleUpdateBundleTransformProgress(progressChunk: MetroLogRecord) {
const msg = progressChunk.msg as ReportableEvent;
let percentProgress;
let bundleComplete = false;
if (msg.type === 'bundle_build_done') {
percentProgress = 100;
bundleComplete = true;
if (this._bundleBuildStart) {
const duration = new Date().getTime() - this._bundleBuildStart.getTime();
progressChunk.msg = `Building JavaScript bundle: finished in ${duration}ms.`;
} else {
progressChunk.msg = `Building JavaScript bundle: finished.`;
}
} else if (msg.type === 'bundle_build_failed') {
percentProgress = -1;
bundleComplete = true;
progressChunk.msg = `Building JavaScript bundle: error`;
progressChunk.level = Logger.ERROR;
} else if (msg.type === 'bundle_transform_progressed') {
percentProgress = Math.floor((msg.transformedFileCount / msg.totalFileCount) * 100);
progressChunk.msg = `Building JavaScript bundle: ${percentProgress}%`;
} else {
return;
}
if (this._bundleBuildChunkID) {
progressChunk.id = this._bundleBuildChunkID;
}
if (this._onProgressBuildBundle) {
this._onProgressBuildBundle(percentProgress, this._bundleBuildStart, progressChunk);
if (bundleComplete) {
if (this._onFinishBuildBundle && this._bundleBuildStart) {
const error = msg.type === 'bundle_build_failed' ? 'Build failed' : null;
this._onFinishBuildBundle(error, this._bundleBuildStart, new Date(), progressChunk);
}
this._bundleBuildStart = null;
this._bundleBuildChunkID = null;
}
} else {
this._updateLogs(logs => {
if (!logs || !logs.length) {
return [];
}
logs.forEach(log => {
if (log.id === this._bundleBuildChunkID) {
log.msg = progressChunk.msg;
}
});
if (bundleComplete) {
this._bundleBuildChunkID = null;
}
return [...logs];
});
}
}
_formatModuleResolutionError(error: MetroError): string | null {
if (!error.message) {
return null;
}
const match = /^Unable to resolve module `(.+?)`/.exec(error.message);
const originModulePath = error.originModulePath as string | null;
if (!match || !originModulePath) {
return null;
}
const moduleName = match[1];
const relativePath = path.relative(this._projectRoot, originModulePath);
const DOCS_PAGE_URL =
'https://docs.expo.io/versions/latest/introduction/faq/#can-i-use-nodejs-packages-with-expo';
if (NODE_STDLIB_MODULES.includes(moduleName)) {
if (originModulePath.includes('node_modules')) {
return `The package at "${relativePath}" attempted to import the Node standard library module "${moduleName}". It failed because React Native does not include the Node standard library. Read more at ${DOCS_PAGE_URL}`;
} else {
return `You attempted attempted to import the Node standard library module "${moduleName}" from "${relativePath}". It failed because React Native does not include the Node standard library. Read more at ${DOCS_PAGE_URL}`;
}
}
return `Unable to resolve "${moduleName}" from "${relativePath}"`;
}
_formatBundlingError(error: MetroError): string | null {
let message = error.message;
if (!message && Array.isArray(error.errors) && error.errors.length) {
message = (error.errors[0] as any).description;
}
if (!message) {
return null;
}
message = chalk.red(message);
let snippet = (this._getSnippetForError && this._getSnippetForError(error)) || error.snippet;
if (snippet) {
message += `\n${snippet}`;
}
return message;
}
_formatWorkerChunk(origin: 'stdout' | 'stderr', chunk: string) {
const lines = chunk.split('\n');
if (lines.length >= 1 && lines[lines.length - 1] === '') {
lines.splice(lines.length - 1, 1);
}
return lines.map(line => `transform[${origin}]: ${line}`).join('\n');
}
_enqueueAppendLogChunk(chunk: LogRecord) {
if (!chunk.shouldHide) {
this._logsToAdd.push(chunk);
this._enqueueFlushLogsToAdd();
}
}
_enqueueFlushLogsToAdd = () => {
this._updateLogs(logs => {
if (this._logsToAdd.length === 0) {
return logs;
}
let nextLogs = logs.concat(this._logsToAdd);
this._logsToAdd = [];
return nextLogs;
});
};
_maybeParseMsgJSON(chunk: LogRecord) {
try {
let parsedMsg = JSON.parse(chunk.msg);
chunk.msg = parsedMsg;
} catch (e) {
// non-JSON message
}
return chunk;
}
_cleanUpNodeErrors = (chunk: LogRecord) => {
if (typeof chunk.msg === 'object') {
return chunk;
}
if (chunk.msg.match(/\(node:.\d*\)/)) {
// Example: (node:13817) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): SyntaxError: SyntaxError /Users/brent/universe/apps/new-project-template/main.js: Unexpected token (10:6)
// The first part of this is totally useless, so let's remove it.
if (chunk.msg.match(/UnhandledPromiseRejectionWarning/)) {
chunk.msg = chunk.msg.replace(/\(node:.*\(rejection .*\):/, '');
if (chunk.msg.match(/SyntaxError: SyntaxError/)) {
chunk.msg = chunk.msg.replace('SyntaxError: ', '');
}
} else if (chunk.msg.match(/DeprecationWarning/)) {
chunk.msg = '';
}
}
return chunk;
};
}
const NODE_STDLIB_MODULES = [
'assert',
'async_hooks',
'buffer',
'child_process',
'cluster',
'crypto',
'dgram',
'dns',
'domain',
'events',
'fs',
'http',
'https',
'net',
'os',
'path',
'punycode',
'querystring',
'readline',
'repl',
'stream',
'string_decoder',
'tls',
'tty',
'url',
'util',
'v8',
'vm',
'zlib',
];