1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and 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.  * @flow
    
  8.  */
    
  9. 
    
  10. import 'regenerator-runtime/runtime';
    
  11. 
    
  12. import type {TimelineEvent} from '@elg/speedscope';
    
  13. import type {ImportWorkerOutputData} from './index';
    
  14. 
    
  15. import preprocessData from './preprocessData';
    
  16. import {readInputData} from './readInputData';
    
  17. import InvalidProfileError from './InvalidProfileError';
    
  18. 
    
  19. export async function importFile(file: File): Promise<ImportWorkerOutputData> {
    
  20.   try {
    
  21.     const readFile = await readInputData(file);
    
  22.     const events: TimelineEvent[] = JSON.parse(readFile);
    
  23.     if (events.length === 0) {
    
  24.       throw new InvalidProfileError('No profiling data found in file.');
    
  25.     }
    
  26. 
    
  27.     const processedData = await preprocessData(events);
    
  28. 
    
  29.     return {
    
  30.       status: 'SUCCESS',
    
  31.       processedData,
    
  32.     };
    
  33.   } catch (error) {
    
  34.     if (error instanceof InvalidProfileError) {
    
  35.       return {
    
  36.         status: 'INVALID_PROFILE_ERROR',
    
  37.         error,
    
  38.       };
    
  39.     } else {
    
  40.       return {
    
  41.         status: 'UNEXPECTED_ERROR',
    
  42.         error,
    
  43.       };
    
  44.     }
    
  45.   }
    
  46. }