All files / src index.ts

92.86% Statements 26/28
76.92% Branches 10/13
100% Functions 4/4
96.15% Lines 25/26

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 551x   1x 1x               1x   4x 4x 4x         4x 1x 1x   1x     1x 1x 1x 1x     1x     3x 1x     3x 2x     3x 3x     4x 4x   4x      
import * as fs from 'fs'
 
import { parseCubeLut } from './formats/cube'
import { parseLutFormat } from './lutFormat'
import { Lut } from './typings'
 
/**
 * Parses the input LUT. It will try to detect the format, and will default
 * to Adobe's .cube
 * @param input
 */
export const parseLut = async (
  input: string | Uint8Array | ArrayBuffer | Buffer
): Promise<Lut> => {
  const node = typeof window === 'undefined'
  const options: { filePath?: string; data: Uint8Array } = {
    data: new Uint8Array()
  }
 
  // We are given a file path, read file
  if ((input as string).toLowerCase) {
    const filePath = input as string
    Iif (!node)
      throw new Error('Parsing lut from file path is only supported in node')
    options.filePath = filePath
 
    // const fs = require('fs')
    const buff = await new Promise<Buffer>((resolve, reject) => {
      fs.readFile(filePath, (err, data) => {
        Iif (err) return reject(err)
        return resolve(data)
      })
    })
    options.data = new Uint8Array(buff.buffer)
  } else {
    // Convert input to Uint8Array
    if ((input as Buffer).writeBigInt64BE) {
      options.data = new Uint8Array((input as Buffer).buffer)
    }
 
    if ((input as Uint8Array).buffer) {
      options.data = input as Uint8Array
    }
 
    Eif ((input as ArrayBuffer).byteLength) {
      options.data = new Uint8Array(input as ArrayBuffer)
    }
  }
  const format = parseLutFormat(options)
  switch (format) {
    case 'cube':
      return parseCubeLut(options.data)
  }
}