npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

to-vfile

v8.0.0

Published

vfile utility to read and write to the file system

Downloads

1,260,221

Readme

to-vfile

Build Coverage Downloads Sponsors Backers Chat

vfile utility to read and write to the file system.

Contents

What is this?

This utility puts the file system first. Where vfile itself focusses on file values (the file contents), this instead focuses on the file system, which is a common case when working with actual files from Node.js.

When should I use this?

Use this if you know there’s a file system and want to use it. Use vfile if there might not be a file system.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install to-vfile

In Deno with esm.sh:

import {toVFile, read, readSync, write, writeSync} from 'https://esm.sh/to-vfile@8'

Use

import {toVFile, read} from 'to-vfile'

console.log(toVFile('readme.md'))
console.log(toVFile(new URL('readme.md', import.meta.url)))
console.log(await read('.git/HEAD'))
console.log(await read('.git/HEAD', 'utf8'))

Yields:

VFile {
  cwd: '/Users/tilde/Projects/oss/to-vfile',
  data: {},
  history: [ 'readme.md' ],
  messages: []
}
VFile {
  cwd: '/Users/tilde/Projects/oss/to-vfile',
  data: {},
  history: [ '/Users/tilde/Projects/oss/to-vfile/readme.md' ],
  messages: []
}
VFile {
  cwd: '/Users/tilde/Projects/oss/to-vfile',
  data: {},
  history: [ '.git/HEAD' ],
  messages: [],
  value: <Buffer 72 65 66 3a 20 72 65 66 73 2f 68 65 61 64 73 2f 6d 61 69 6e 0a>
}
VFile {
  cwd: '/Users/tilde/Projects/oss/to-vfile',
  data: {},
  history: [ '.git/HEAD' ],
  messages: [],
  value: 'ref: refs/heads/main\n'
}

API

This package exports the identifiers read, readSync, toVFile, write, and writeSync. There is no default export.

toVFile(description)

Create a virtual file from a description.

This is like VFile, but it accepts a file path instead of file cotnents.

If options is a string, URL, or buffer, it’s used as the path. Otherwise, if it’s a file, that’s returned instead. Otherwise, the options are passed through to new VFile().

Parameters
  • description (Compatible, optional) — fath to file, file options, or file itself
Returns

Given file or new file (VFile).

read(description[, options][, callback])

Create a virtual file and read it in, async.

Signatures
  • (description[, options], Callback): undefined
  • (description[, options]): Promise<VFile>
Parameters
Returns

Nothing when a callback is given, otherwise promise that resolves to given file or new file (VFile).

readSync(description[, options])

Create a virtual file and read it in, synchronously.

Parameters
Returns

Given file or new file (VFile).

write(description[, options][, callback])

Create a virtual file and write it, async.

Signatures
  • (description[, options], Callback): undefined
  • (description[, options]): Promise<VFile>
Parameters
Returns

Nothing when a callback is given, otherwise promise that resolves to given file or new file (VFile).

writeSync(description[, options])

Create a virtual file and write it, synchronously.

Parameters
Returns

Given file or new file (VFile).

BufferEncoding

Encodings supported by the buffer class (TypeScript type).

This is a copy of the types from Node.

Type
type BufferEncoding =
  | 'ascii'
  | 'base64'
  | 'base64url'
  | 'binary'
  | 'hex'
  | 'latin1'
  | 'ucs-2'
  | 'ucs2'
  | 'utf-8'
  | 'utf16le'
  | 'utf8'

Callback

Callback called after reading or writing a file (TypeScript type).

Parameters
  • error (Error, optional) — error when reading or writing was not successful
  • file (VFile, optional) — file when reading or writing was successful
Returns

Nothing (undefined).

Compatible

URL to file, path to file, options for file, or actual file (TypeScript type).

Type
type Compatible = Uint8Array | URL | VFile | VFileOptions | string

See VFileOptions and VFile.

ReadOptions

Configuration for fs.readFile (TypeScript type).

Fields
  • encoding (BufferEncoding, optional) — encoding to read file as, will turn file.value into a string if passed
  • flag (string, optional) — file system flags to use

WriteOptions

Configuration for fs.writeFile (TypeScript type).

Fields
  • encoding (BufferEncoding, optional) — encoding to write file as when file.value is a string
  • mode (number | string, optional) — file mode (permission and sticky bits) if the file was newly created
  • flag (string, optional) — file system flags to use

Types

This package is fully typed with TypeScript. It exports the additional types BufferEncoding, Callback, Compatible, ReadOptions, and WriteOptions.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, vfile@^8, compatible with Node.js 16.

Contribute

See contributing.md in vfile/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer