Remix
Remix

fs

Lazy, streaming filesystem utilities for JavaScript. This package provides utilities for working with files on the local filesystem using the LazyFile/ native File API.

Features

  • Web Standards - Uses LazyFile which matches the native File API and provides .stream(), .toFile(), and .toBlob() for converting to native types.
  • Seamless Node.js Compat - Works seamlessly with Node.js file descriptors and handles

Installation

npm i remix

Usage

Opening Lazy Files

import { openLazyFile } from 'remix/fs'

// Open a file from the filesystem
let lazyFile = openLazyFile('./path/to/file.json')

// The file is lazy - no data is read until you call lazyFile.text(), lazyFile.bytes(), etc.
let json = JSON.parse(await lazyFile.text())

// You can override file metadata
let customLazyFile = openLazyFile('./image.jpg', {
  name: 'custom-name.jpg',
  type: 'image/jpeg',
  lastModified: Date.now(),
})

Writing Files

import { openLazyFile, writeFile } from 'remix/fs'

// Read a file and write it elsewhere
let lazyFile = openLazyFile('./source.txt')
await writeFile('./destination.txt', lazyFile)

// Write to an open file handle
import * as fsp from 'node:fs/promises'
let handle = await fsp.open('./destination.txt', 'w')
await writeFile(handle, lazyFile)
await handle.close()

Related Packages

License

See LICENSE