npm.io
3.0.0 • Published 4 months ago

read-file-safe

Licence
MIT
Version
3.0.0
Deps
0
Size
11 kB
Vulns
0
Weekly
0
Stars
1

read-file-safe

NPM TypeScript Coverage Status

Read files without try catch.

This package handles filesystem errors for you. If a file does not exist or cannot be accessed, undefined is returned instead of throwing an error.

Installation

NPM
npm install read-file-safe
Yarn
yarn add read-file-safe
PNPM
pnpm add read-file-safe
Bun
bun add read-file-safe

Usage

By default, readFile and readFileSync return a string:

import { readFile, readFileSync } from "read-file-safe";

const path = "./path/to/file.ext";

const file = readFile(path); // Promise<string | undefined>

const file = readFileSync(path); // string | undefined
Return Type

Setting the buffer option to true will return a Buffer instead of a string:

import { readFile, readFileSync } from "read-file-safe";

const file = readFile(path, { buffer: true }); // Promise<Buffer | undefined>

const file = readFileSync(path, { buffer: true }); // Buffer | undefined
Decompression

Setting the compression option will decompress the file before returning it. Both gzip and brotli are supported:

import { readFile, readFileSync } from "read-file-safe";

const file = readFile(path, { compression: "gzip" }); // Promise<string | undefined>

const file = readFile(path, { compression: "brotli" }); // Promise<string | undefined>

License license

MIT - MIT License


Keywords