office-text-extractor
Version:
Yet another library to extract text from MS Office and PDF files
27 lines (26 loc) • 758 B
JavaScript
// source/util.ts
// Utility functions to help with the handling of input.
export const readFile = async (filePath) => {
let read;
try {
const fs = await import('node:fs/promises');
read = fs.readFile;
}
catch {
read = undefined;
}
if (!read) {
const message = `text-extractor: could not import node:fs/promises`;
throw new Error(message);
}
return read(filePath);
};
export const fetchUrl = async (url) => {
if (typeof fetch === 'undefined') {
const message = `text-extractor: built-in fetch is undefined`;
throw new Error(message);
}
const response = await fetch(url);
const buffer = await response.arrayBuffer();
return new Uint8Array(buffer);
};