forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive-profile.js
804 lines (730 loc) · 22.9 KB
/
receive-profile.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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import { oneLine } from 'common-tags';
import {
processProfile,
unserializeProfileOfArbitraryFormat,
} from '../profile-logic/process-profile';
import { SymbolStore } from '../profile-logic/symbol-store';
import { symbolicateProfile } from '../profile-logic/symbolication';
import * as MozillaSymbolicationAPI from '../profile-logic/mozilla-symbolication-api';
import { decompress } from '../utils/gz';
import { TemporaryError } from '../utils/errors';
import JSZip from 'jszip';
import {
getDataSource,
getSelectedThreadIndexOrNull,
getGlobalTrackOrder,
getHiddenGlobalTracks,
getHiddenLocalTracksByPid,
getLocalTrackOrderByPid,
getLegacyThreadOrder,
getLegacyHiddenThreads,
} from '../selectors/url-state';
import {
initializeLocalTrackOrderByPid,
initializeHiddenLocalTracksByPid,
computeLocalTracksByPid,
computeGlobalTracks,
initializeGlobalTrackOrder,
initializeSelectedThreadIndex,
initializeHiddenGlobalTracks,
getVisibleThreads,
} from '../profile-logic/tracks';
import type {
FunctionsUpdatePerThread,
FuncToFuncMap,
RequestedLib,
} from '../types/actions';
import type { Action, ThunkAction, Dispatch } from '../types/store';
import type {
Profile,
ThreadIndex,
IndexIntoFuncTable,
} from '../types/profile';
/**
* This file collects all the actions that are used for receiving the profile in the
* client and getting it into the processed format.
*/
export function triggerLoadingFromUrl(profileUrl: string): Action {
return {
type: 'TRIGGER_LOADING_FROM_URL',
profileUrl,
};
}
export function waitingForProfileFromAddon(): Action {
return {
type: 'WAITING_FOR_PROFILE_FROM_ADDON',
};
}
/**
* Call this function once the profile has been fetched and pre-processed from whatever
* source (url, addon, file, etc). This function will take the view information from the
* URL, such as hiding and sorting information, and it will validate it against the
* profile. If there is no pre-existing view information, this function will compute the
* defaults. There is a decent amount of complexity to making all of these decisions,
* which has been collected in a bunch of functions in the src/profile-logic/tracks.js
* file.
*/
export function viewProfile(
profile: Profile,
pathInZipFile: ?string
): ThunkAction<void> {
return (dispatch, getState) => {
if (profile.threads.length === 0) {
console.error('This profile has no threads.', profile);
dispatch(
fatalError(
new Error(
'No threads were captured in this profile, there is nothing to display.'
)
)
);
return;
}
// The selectedThreadIndex is only null for new profiles that haven't
// been seen before. If it's non-null, then there is profile view information
// encoded into the URL.
let selectedThreadIndex = getSelectedThreadIndexOrNull(getState());
const hasUrlInfo = selectedThreadIndex !== null;
const globalTracks = computeGlobalTracks(profile);
const globalTrackOrder = initializeGlobalTrackOrder(
globalTracks,
hasUrlInfo ? getGlobalTrackOrder(getState()) : null,
getLegacyThreadOrder(getState())
);
let hiddenGlobalTracks = initializeHiddenGlobalTracks(
globalTracks,
profile,
globalTrackOrder,
hasUrlInfo ? getHiddenGlobalTracks(getState()) : null,
getLegacyHiddenThreads(getState())
);
const localTracksByPid = computeLocalTracksByPid(profile);
const localTrackOrderByPid = initializeLocalTrackOrderByPid(
hasUrlInfo ? getLocalTrackOrderByPid(getState()) : null,
localTracksByPid,
getLegacyThreadOrder(getState())
);
let hiddenLocalTracksByPid = initializeHiddenLocalTracksByPid(
hasUrlInfo ? getHiddenLocalTracksByPid(getState()) : null,
localTracksByPid,
profile,
getLegacyHiddenThreads(getState())
);
let visibleThreadIndexes = getVisibleThreads(
globalTracks,
hiddenGlobalTracks,
localTracksByPid,
hiddenLocalTracksByPid
);
// This validity check can't be extracted into a separate function, as it needs
// to update a lot of the local variables in this function.
if (visibleThreadIndexes.length === 0) {
// All threads are hidden, since this can't happen normally, revert them all.
visibleThreadIndexes = profile.threads.map(
(_, threadIndex) => threadIndex
);
hiddenGlobalTracks = new Set();
const newHiddenTracksByPid = new Map();
for (const [pid] of hiddenLocalTracksByPid) {
newHiddenTracksByPid.set(pid, new Set());
}
hiddenLocalTracksByPid = newHiddenTracksByPid;
}
selectedThreadIndex = initializeSelectedThreadIndex(
selectedThreadIndex,
visibleThreadIndexes,
profile
);
dispatch({
type: 'VIEW_PROFILE',
profile,
selectedThreadIndex,
globalTracks,
globalTrackOrder,
hiddenGlobalTracks,
localTracksByPid,
hiddenLocalTracksByPid,
localTrackOrderByPid,
pathInZipFile,
dataSource: getDataSource(getState()),
});
};
}
export function requestingSymbolTable(requestedLib: RequestedLib): Action {
return {
type: 'REQUESTING_SYMBOL_TABLE',
requestedLib,
};
}
export function receivedSymbolTableReply(requestedLib: RequestedLib): Action {
return {
type: 'RECEIVED_SYMBOL_TABLE_REPLY',
requestedLib,
};
}
export function startSymbolicating(): Action {
return {
type: 'START_SYMBOLICATING',
};
}
export function doneSymbolicating(): Action {
return { type: 'DONE_SYMBOLICATING' };
}
export function coalescedFunctionsUpdate(
functionsUpdatePerThread: FunctionsUpdatePerThread
): Action {
return {
type: 'COALESCED_FUNCTIONS_UPDATE',
functionsUpdatePerThread,
};
}
let requestIdleCallbackPolyfill: (
callback: () => void,
_opts?: { timeout: number }
) => mixed;
if (typeof window === 'object' && window.requestIdleCallback) {
requestIdleCallbackPolyfill = window.requestIdleCallback;
} else if (typeof process === 'object' && process.nextTick) {
// Node environment
requestIdleCallbackPolyfill = process.nextTick;
} else {
requestIdleCallbackPolyfill = callback => setTimeout(callback, 0);
}
class ColascedFunctionsUpdateDispatcher {
_updates: FunctionsUpdatePerThread;
_requestedUpdate: boolean;
_requestIdleTimeout: { timeout: number };
scheduledUpdatesDone: Promise<void>;
constructor() {
this._updates = {};
this._requestedUpdate = false;
this._requestIdleTimeout = { timeout: 2000 };
this.scheduledUpdatesDone = Promise.resolve();
}
_scheduleUpdate(dispatch) {
// Only request an update if one hasn't already been schedule.
if (!this._requestedUpdate) {
// Let any consumers of this class be able to know when all scheduled updates
// are done.
this.scheduledUpdatesDone = new Promise(resolve => {
requestIdleCallbackPolyfill(() => {
this._dispatchUpdate(dispatch);
resolve();
}, this._requestIdleTimeout);
});
this._requestedUpdate = true;
}
}
_dispatchUpdate(dispatch) {
const updates = this._updates;
this._updates = {};
this._requestedUpdate = false;
dispatch(coalescedFunctionsUpdate(updates));
}
mergeFunctions(
dispatch: Dispatch,
threadIndex: ThreadIndex,
oldFuncToNewFuncMap: FuncToFuncMap
) {
this._scheduleUpdate(dispatch);
if (!this._updates[threadIndex]) {
this._updates[threadIndex] = {
oldFuncToNewFuncMap,
funcIndices: [],
funcNames: [],
};
} else {
for (const oldFunc of oldFuncToNewFuncMap.keys()) {
const funcIndex = oldFuncToNewFuncMap.get(oldFunc);
if (funcIndex === undefined) {
throw new Error(
'Unable to merge functions together, an undefined funcIndex was returned.'
);
}
this._updates[threadIndex].oldFuncToNewFuncMap.set(oldFunc, funcIndex);
}
}
}
assignFunctionNames(dispatch, threadIndex, funcIndices, funcNames) {
this._scheduleUpdate(dispatch);
if (!this._updates[threadIndex]) {
this._updates[threadIndex] = {
funcIndices,
funcNames,
oldFuncToNewFuncMap: new Map(),
};
} else {
this._updates[threadIndex].funcIndices = this._updates[
threadIndex
].funcIndices.concat(funcIndices);
this._updates[threadIndex].funcNames = this._updates[
threadIndex
].funcNames.concat(funcNames);
}
}
}
const gCoalescedFunctionsUpdateDispatcher = new ColascedFunctionsUpdateDispatcher();
export function mergeFunctions(
threadIndex: ThreadIndex,
oldFuncToNewFuncMap: FuncToFuncMap
): ThunkAction<void> {
return dispatch => {
gCoalescedFunctionsUpdateDispatcher.mergeFunctions(
dispatch,
threadIndex,
oldFuncToNewFuncMap
);
};
}
export function assignFunctionNames(
threadIndex: ThreadIndex,
funcIndices: IndexIntoFuncTable[],
funcNames: string[]
): ThunkAction<void> {
return dispatch => {
gCoalescedFunctionsUpdateDispatcher.assignFunctionNames(
dispatch,
threadIndex,
funcIndices,
funcNames
);
};
}
/**
* If the profile object we got from the add-on is an ArrayBuffer, convert it
* to a gecko profile object by parsing the JSON.
*/
function _unpackGeckoProfileFromAddon(profile) {
if (profile instanceof ArrayBuffer) {
const textDecoder = new TextDecoder();
return JSON.parse(textDecoder.decode(profile));
}
return profile;
}
async function getProfileFromAddon(dispatch, geckoProfiler) {
dispatch(waitingForProfileFromAddon());
// XXX update state to show that we're connected to the profiler addon
const rawGeckoProfile = await geckoProfiler.getProfile();
const profile = processProfile(_unpackGeckoProfileFromAddon(rawGeckoProfile));
dispatch(viewProfile(profile));
return profile;
}
async function getSymbolStore(dispatch, geckoProfiler) {
const symbolStore = new SymbolStore('perf-html-async-storage', {
requestSymbolsFromServer: requests => {
for (const { lib } of requests) {
dispatch(requestingSymbolTable(lib));
}
return MozillaSymbolicationAPI.requestSymbols(requests).map(
async (libPromise, i) => {
try {
const result = libPromise;
dispatch(receivedSymbolTableReply(requests[i].lib));
return result;
} catch (error) {
dispatch(receivedSymbolTableReply(requests[i].lib));
throw error;
}
}
);
},
requestSymbolTableFromAddon: async lib => {
const { debugName, breakpadId } = lib;
dispatch(requestingSymbolTable(lib));
try {
const symbolTable = await geckoProfiler.getSymbolTable(
debugName,
breakpadId
);
dispatch(receivedSymbolTableReply(lib));
return symbolTable;
} catch (error) {
dispatch(receivedSymbolTableReply(lib));
throw error;
}
},
});
return symbolStore;
}
export async function doSymbolicateProfile(
dispatch: Dispatch,
profile: Profile,
symbolStore: SymbolStore
) {
dispatch(startSymbolicating());
await symbolicateProfile(profile, symbolStore, {
onMergeFunctions: (
threadIndex: ThreadIndex,
oldFuncToNewFuncMap: FuncToFuncMap
) => {
dispatch(mergeFunctions(threadIndex, oldFuncToNewFuncMap));
},
onGotFuncNames: (
threadIndex: ThreadIndex,
funcIndices: IndexIntoFuncTable[],
funcNames: string[]
) => {
dispatch(assignFunctionNames(threadIndex, funcIndices, funcNames));
},
});
await gCoalescedFunctionsUpdateDispatcher.scheduledUpdatesDone;
dispatch(doneSymbolicating());
}
export function fatalError(error: Error) {
return {
type: 'FATAL_ERROR',
error,
};
}
export function retrieveProfileFromAddon(): ThunkAction<Promise<void>> {
return async dispatch => {
try {
const timeoutId = setTimeout(() => {
dispatch(
temporaryError(
new TemporaryError(oneLine`
We were unable to connect to the Gecko profiler add-on within thirty seconds.
This might be because the profile is big or your machine is slower than usual.
Still waiting...
`)
)
);
}, 30000);
const geckoProfiler = await window.geckoProfilerPromise;
clearTimeout(timeoutId);
const [profile, symbolStore] = await Promise.all([
getProfileFromAddon(dispatch, geckoProfiler),
getSymbolStore(dispatch, geckoProfiler),
]);
await doSymbolicateProfile(dispatch, profile, symbolStore);
} catch (error) {
dispatch(fatalError(error));
throw error;
}
};
}
export function waitingForProfileFromStore(): Action {
return {
type: 'WAITING_FOR_PROFILE_FROM_STORE',
};
}
export function waitingForProfileFromUrl(): Action {
return {
type: 'WAITING_FOR_PROFILE_FROM_URL',
};
}
export function receiveZipFile(zip: JSZip): Action {
return {
type: 'RECEIVE_ZIP_FILE',
zip,
};
}
export function temporaryError(error: TemporaryError): Action {
return {
type: 'TEMPORARY_ERROR',
error,
};
}
function _wait(delayMs) {
return new Promise(resolve => setTimeout(resolve, delayMs));
}
type FetchProfileArgs = {
url: string,
onTemporaryError: TemporaryError => void,
// Allow tests to capture the reported error, but normally use console.error.
reportError?: Function,
};
type ProfileOrZip = {
profile?: any,
zip?: JSZip,
};
/**
* Tries to fetch a profile on `url`. If the profile is not found,
* `onTemporaryError` is called with an appropriate error, we wait 1 second, and
* then tries again. If we still can't find the profile after 11 tries, the
* returned promise is rejected with a fatal error.
* If we can retrieve the profile properly, the returned promise is resolved
* with the JSON.parsed profile.
*/
export async function _fetchProfile(
args: FetchProfileArgs
): Promise<ProfileOrZip> {
const MAX_WAIT_SECONDS = 10;
let i = 0;
const { url, onTemporaryError } = args;
// Allow tests to capture the reported error, but normally use console.error.
const reportError = args.reportError || console.error;
while (true) {
const response = await fetch(url);
// Case 1: successful answer.
if (response.ok) {
return _extractProfileOrZipFromResponse(url, response, reportError);
}
// case 2: unrecoverable error.
if (response.status !== 403) {
throw new Error(oneLine`
Could not fetch the profile on remote server.
Response was: ${response.status} ${response.statusText}.
`);
}
// case 3: 403 errors can be transient while a profile is uploaded.
if (i++ === MAX_WAIT_SECONDS) {
// In the last iteration we don't send a temporary error because we'll
// throw an error right after the while loop.
break;
}
onTemporaryError(
new TemporaryError(
'Profile not found on remote server.',
{ count: i, total: MAX_WAIT_SECONDS + 1 } // 11 tries during 10 seconds
)
);
await _wait(1000);
}
throw new Error(oneLine`
Could not fetch the profile on remote server:
still not found after ${MAX_WAIT_SECONDS} seconds.
`);
}
/**
* Deduce the file type from a url and content type. Third parties can give us
* arbitrary information, so make sure that we try out best to extract the proper
* information about it.
*/
function _deduceContentType(
url: string,
contentType: string
): 'application/json' | 'application/zip' | null {
if (contentType === 'application/zip' || contentType === 'application/json') {
return contentType;
}
if (url.match(/\.zip$/)) {
return 'application/zip';
}
if (url.match(/\.json/)) {
return 'application/json';
}
return null;
}
/**
* This function guesses the correct content-type (even if one isn't sent) and then
* attempts to use the proper method to extract the response.
*/
async function _extractProfileOrZipFromResponse(
url: string,
response: Response,
reportError: Function
): Promise<ProfileOrZip> {
const contentType = _deduceContentType(
url,
response.headers.get('content-type')
);
switch (contentType) {
case 'application/zip':
return {
zip: await _extractZipFromResponse(response, reportError),
};
case 'application/json':
case null:
// The content type is null if it is unknown, or an unsupported type. Go ahead
// and try to process it as a profile.
return {
profile: await _extractJsonFromResponse(
response,
reportError,
contentType
),
};
default:
throw new Error(`Unhandled file type: ${(contentType: empty)}`);
}
}
/**
* Attempt to load a zip file from a third party. This process can fail, so make sure
* to handle and report the error if it does.
*/
async function _extractZipFromResponse(
response: Response,
reportError: Function
): Promise<JSZip> {
const buffer = await response.arrayBuffer();
try {
const zip = await JSZip.loadAsync(buffer);
// Catch the error if unable to load the zip.
return zip;
} catch (error) {
const message = 'Unable to unzip the zip file.';
reportError(message);
reportError('Error:', error);
reportError('Fetch response:', response);
throw new Error(
`${message} The full error information has been printed out to the DevTool’s console.`
);
}
}
/**
* Don't trust third party responses, try and handle a variety of responses gracefully.
*/
async function _extractJsonFromResponse(
response: Response,
reportError: Function,
fileType: 'application/json' | null
): Promise<any> {
try {
// Don't check the content-type, but attempt to parse the response as JSON.
const json = await response.json();
// Catch the error if unable to parse the JSON.
return json;
} catch (error) {
// Change the error message depending on the circumstance:
let message;
if (fileType === 'application/json') {
message = 'The profile’s JSON could not be decoded.';
} else {
message = oneLine`
The profile could not be decoded. This does not look like a supported file
type.
`;
}
// Provide helpful debugging information to the console.
reportError(message);
reportError('JSON parsing error:', error);
reportError('Fetch response:', response);
throw new Error(
`${message} The full error information has been printed out to the DevTool’s console.`
);
}
}
export function retrieveProfileFromStore(
hash: string
): ThunkAction<Promise<void>> {
return retrieveProfileOrZipFromUrl(
`https://profile-store.commondatastorage.googleapis.com/${hash}`
);
}
/**
* Runs a fetch on a URL, and downloads the file. If it's JSON, then it attempts
* to process the profile. If it's a zip file, it tries to unzip it, and save it
* into the store so that the user can then choose which file to load.
*/
export function retrieveProfileOrZipFromUrl(
profileUrl: string
): ThunkAction<Promise<void>> {
return async function(dispatch) {
dispatch(waitingForProfileFromUrl());
try {
const response = await _fetchProfile({
url: profileUrl,
onTemporaryError: (e: TemporaryError) => {
dispatch(temporaryError(e));
},
});
const serializedProfile = response.profile;
const zip = response.zip;
if (serializedProfile) {
const profile = await unserializeProfileOfArbitraryFormat(
serializedProfile
);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
dispatch(viewProfile(profile));
} else if (zip) {
dispatch(receiveZipFile(zip));
} else {
throw new Error(
'Expected to receive a zip file or profile from _fetchProfile.'
);
}
} catch (error) {
dispatch(fatalError(error));
}
};
}
export function waitingForProfileFromFile(): Action {
return {
type: 'WAITING_FOR_PROFILE_FROM_FILE',
};
}
function _fileReader(input: File): * {
const reader = new FileReader();
const promise = new Promise((resolve, reject) => {
// Flow's definition for FileReader doesn't handle the polymorphic nature of
// reader.result very well, as its definition is <string | ArrayBuffer>.
// Here we ensure type safety by returning the proper Promise type from the
// methods below.
reader.onload = () => resolve((reader.result: any));
reader.onerror = () => reject(reader.error);
});
return {
asText(): Promise<string> {
reader.readAsText(input);
return promise;
},
asArrayBuffer(): Promise<ArrayBuffer> {
reader.readAsArrayBuffer(input);
return promise;
},
};
}
/**
* Multiple file formats are supported. Look at the file type and try and
* parse the contents according to its type.
*/
export function retrieveProfileFromFile(
file: File,
// Allow tests to inject a custom file reader to bypass the DOM APIs.
fileReader: typeof _fileReader = _fileReader
): ThunkAction<Promise<void>> {
return async dispatch => {
// Notify the UI that we are loading and parsing a profile. This can take
// a little bit of time.
dispatch(waitingForProfileFromFile());
try {
switch (file.type) {
case 'application/gzip':
case 'application/x-gzip':
// Parse a single profile that has been gzipped.
{
const buffer = await fileReader(file).asArrayBuffer();
const arrayBuffer = new Uint8Array(buffer);
const decompressedArrayBuffer = await decompress(arrayBuffer);
const textDecoder = new TextDecoder();
const text = await textDecoder.decode(decompressedArrayBuffer);
const profile = await unserializeProfileOfArbitraryFormat(text);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
dispatch(viewProfile(profile));
}
break;
case 'application/zip':
// Open a zip file in the zip file viewer
{
const buffer = await fileReader(file).asArrayBuffer();
const zip = await JSZip.loadAsync(buffer);
dispatch(receiveZipFile(zip));
}
break;
default: {
// Plain uncompressed profile files can have file names with uncommon
// extensions (eg .profile). So we can't rely on the mime type to
// decide how to handle them. We'll try to parse them as a plain JSON
// file.
const text = await fileReader(file).asText();
const profile = await unserializeProfileOfArbitraryFormat(text);
if (profile === undefined) {
throw new Error('Unable to parse the profile.');
}
dispatch(viewProfile(profile));
}
}
} catch (error) {
dispatch(fatalError(error));
}
};
}