All files / src/utils file.ts

61.53% Statements 8/13
75% Branches 6/8
50% Functions 2/4
66.66% Lines 8/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          33x   20x 2x       18x   18x                         33x 1x 1x                    
/**
 * Compatibility functions for working with File API objects
 *
 * https://developer.mozilla.org/en-US/docs/Web/API/File
 */
export function isFile(file: unknown): file is File {
  // browser
  if (typeof File === 'function' && file instanceof File) {
    return true
  }
 
  // node.js
  const f = file as File
 
  return (
    typeof f === 'object' &&
    typeof (f as File).name === 'string' &&
    (typeof (f as File).stream === 'function' || typeof (f as File).arrayBuffer === 'function')
  )
}
 
/**
 * Compatibility helper for browsers where the `arrayBuffer function is
 * missing from `File` objects.
 *
 * @param file A File object
 */
export async function fileArrayBuffer(file: File): Promise<ArrayBuffer> {
  if (file.arrayBuffer) {
    return file.arrayBuffer()
  }
 
  // workaround for Safari where arrayBuffer is not supported on Files
  return new Promise(resolve => {
    const fr = new FileReader()
    fr.onload = () => resolve(fr.result as ArrayBuffer)
    fr.readAsArrayBuffer(file)
  })
}