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 nullthrows from 'nullthrows';
    
  11. import InvalidProfileError from './InvalidProfileError';
    
  12. 
    
  13. export const readInputData = (file: File): Promise<string> => {
    
  14.   if (!file.name.endsWith('.json')) {
    
  15.     throw new InvalidProfileError(
    
  16.       'Invalid file type. Only JSON performance profiles are supported',
    
  17.     );
    
  18.   }
    
  19. 
    
  20.   const fileReader = new FileReader();
    
  21. 
    
  22.   return new Promise((resolve, reject) => {
    
  23.     fileReader.onload = () => {
    
  24.       const result = nullthrows(fileReader.result);
    
  25.       if (typeof result === 'string') {
    
  26.         resolve(result);
    
  27.       }
    
  28.       reject(new InvalidProfileError('Input file was not read as a string'));
    
  29.     };
    
  30. 
    
  31.     fileReader.onerror = () => reject(fileReader.error);
    
  32. 
    
  33.     fileReader.readAsText(file);
    
  34.   });
    
  35. };