Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
The 'to-vfile' npm package is a utility for working with virtual files in Node.js. It allows you to create, read, write, and manipulate file objects in memory, which can be particularly useful for processing files in a pipeline without needing to interact with the filesystem directly.
Creating a virtual file
This feature allows you to create a virtual file object with specified path and contents. The file object can then be manipulated in memory.
const vfile = require('to-vfile');
const file = vfile({ path: 'example.txt', contents: 'Hello, world!' });
console.log(file);
Reading a file from the filesystem
This feature allows you to read a file from the filesystem into a virtual file object. The contents of the file are loaded into memory, and you can manipulate the file object as needed.
const vfile = require('to-vfile');
vfile.read('example.txt', 'utf8', (err, file) => {
if (err) throw err;
console.log(file);
});
Writing a virtual file to the filesystem
This feature allows you to write a virtual file object to the filesystem. The contents of the virtual file are saved to the specified path.
const vfile = require('to-vfile');
const file = vfile({ path: 'example.txt', contents: 'Hello, world!' });
vfile.write(file, err => {
if (err) throw err;
console.log('File written!');
});
Vinyl is a similar package that provides a virtual file format. It is commonly used in the Gulp ecosystem for handling file transformations in memory. Compared to 'to-vfile', Vinyl is more focused on stream-based workflows and integrates tightly with Gulp.
Memfs is a package that provides an in-memory filesystem. It allows you to perform filesystem operations in memory, which can be useful for testing and mocking. While 'to-vfile' focuses on individual file objects, Memfs provides a broader filesystem abstraction.
vfile utility to read and write to the file system.
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.
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.
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'
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'
}
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()
.
description
(Compatible
, optional)
— fath to file, file options, or file itselfGiven file or new file (VFile
).
read(description[, options][, callback])
Create a virtual file and read it in, async.
(description[, options], Callback): undefined
(description[, options]): Promise<VFile>
description
(Compatible
)
— path to file, file options, or file itselfoptions
(BufferEncoding
,
ReadOptions
, optional)callback
(Callback
, optional)
— callback called when doneNothing 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.
description
(Compatible
)
— path to file, file options, or file itselfoptions
(BufferEncoding
,
ReadOptions
, optional)Given file or new file (VFile
).
write(description[, options][, callback])
Create a virtual file and write it, async.
(description[, options], Callback): undefined
(description[, options]): Promise<VFile>
description
(Compatible
)
— path to file, file options, or file itselfoptions
(BufferEncoding
,
WriteOptions
, optional)callback
(Callback
, optional)
— callback called when doneNothing 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.
description
(Compatible
)
— path to file, file options, or file itselfoptions
(BufferEncoding
,
WriteOptions
, optional)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 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).
error
(Error
, optional)
— error when reading or writing was not successfulfile
(VFile
, optional)
— file when reading or writing was successfulNothing (undefined
).
Compatible
URL to file, path to file, options for file, or actual file (TypeScript type).
type Compatible = Uint8Array | URL | VFile | VFileOptions | string
See VFileOptions
and VFile
.
ReadOptions
Configuration for fs.readFile
(TypeScript type).
encoding
(BufferEncoding
, optional)
— encoding to read file as, will turn file.value
into a string
if passedflag
(string
, optional)
— file system flags to useWriteOptions
Configuration for fs.writeFile
(TypeScript type).
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 createdflag
(string
, optional)
— file system flags to useThis package is fully typed with TypeScript.
It exports the additional types
BufferEncoding
,
Callback
,
Compatible
,
ReadOptions
, and
WriteOptions
.
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.
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.
FAQs
vfile utility to read and write to the file system
The npm package to-vfile receives a total of 194,760 weekly downloads. As such, to-vfile popularity was classified as popular.
We found that to-vfile demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.