パッケージの詳細

read-file-safe

bconnorwhite53.2kMIT2.1.1

Read files without try catch

read, file, safe, try

readme

read-file-safe

NPM TypeScript Coverage Status


Read files without try catch.


If I should maintain this repo, please ⭐️ GitHub stars

DM me on Twitter if you have questions or suggestions. Twitter


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

yarn add read-file-safe
npm install read-file-safe
pnpm 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>


Dev Dependencies

  • autorepo: Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.


License license

MIT - MIT License


Related Packages

更新履歴

2.1.1 (2023-09-19)

Bug Fixes

  • avoid destructing zlib which doesn't exist in bun (746b440)

2.1.0 (2023-01-12)

Features

  • added support for decompression (a385bb0)

2.0.0 (2023-01-11)

1.0.10 (2020-10-05)

Bug Fixes

  • export Args and ReturnValue (ef71108)

1.0.9 (2020-10-04)

1.0.8 (2020-10-04)

Bug Fixes

1.0.7 (2020-10-03)

1.0.6 (2020-10-03)

1.0.5 (2020-09-02)

1.0.4 (2020-09-02)

1.0.3 (2020-08-28)

1.0.2 (2020-08-28)

1.0.1 (2020-08-16)