All files / lib/util file.ts

40% Statements 6/15
100% Branches 0/0
0% Functions 0/6
40% Lines 6/15

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 321x   1x 1x   1x                   1x                             1x  
import { createReadStream } from "fs";
import { Readable } from "stream";
import { RootLogger } from "./log.js";
import { FileLocation, isLocalPath } from "../models";
 
const log = RootLogger.getChildCategory("CommonUtil");
 
export function getFile(location: FileLocation): Promise<Readable> {
  if (isLocalPath(location)) {
    return Promise.resolve(createReadStream(location.localPath));
  } else {
    throw `Unsupported location type: ${location}`;
  }
}
 
export const readFile = async (readable: Readable) =>
  new Promise<Buffer>((resolve, reject) => {
    try {
      const buffs: Uint8Array[] = [];
      readable
        .on("data", (it: Uint8Array) => buffs.push(it))
        .on("close", () => {
          resolve(Buffer.concat(buffs));
        })
        .on("error", (e) => {
          log.error("Failed to read file", e);
        });
    } catch (e) {
      reject(e);
    }
  });