Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
The vfile npm package is a virtual file format for text processing systems, which allows for the manipulation and handling of file-related information in a structured and consistent way. It is commonly used in projects involving the processing of markdown, text, and similar content, providing a standardized interface for plugins and utilities.
Creating and modifying virtual files
This feature allows users to create a new virtual file and modify its contents. The example demonstrates creating a vfile with initial content and then appending additional text to it.
const vfile = require('vfile');
const file = vfile({path: './example.txt', contents: 'Hello, world!'});
file.contents += ' Modified content.';
console.log(file.contents);
Accessing file metadata
This feature enables users to access metadata of the file such as the path and basename. The example shows how to create a vfile and retrieve its path and basename properties.
const vfile = require('vfile');
const file = vfile({path: './example.txt', contents: 'Hello, world!'});
console.log(file.path); // Outputs: './example.txt'
console.log(file.basename); // Outputs: 'example.txt'
Handling errors and messages
This feature is useful for adding error or warning messages to a file, which can be particularly helpful in linting tools or compilers. The example demonstrates adding an error message to a vfile and logging all associated messages.
const vfile = require('vfile');
const file = vfile();
file.message('Unknown error', {line: 1, column: 1});
console.log(file.messages);
Vinyl is a virtual file format that is often used in the gulp build system. It is similar to vfile in that it represents file objects in a virtual form, but it is more focused on being used with streams, particularly in the context of gulp's pipelines.
mem-fs provides an in-memory file system which stores file representations similar to vfile. While vfile is designed for text processing with a focus on linting and transformation, mem-fs is geared more towards temporary storage and manipulation of files in memory during tasks like scaffolding or testing.
VFile is a virtual file format used by unified, a text processing umbrella (it powers retext for natural language, remark for markdown, and rehype for HTML). Each processors that parse, transform, and compile text, and need a virtual representation of files and a place to store messages about them. Plus, they work in the browser. VFile provides these requirements at a small size, in IE 9 and up.
VFile is different from the excellent vinyl in that it has a smaller API, a smaller size, and focuses on messages.
npm:
npm install vfile
var vfile = require('vfile');
var file = vfile({path: '~/example.txt', contents: 'Alpha *braavo* charlie.'});
file.path; //=> '~/example.txt'
file.dirname; //=> '~'
file.extname = '.md';
file.basename; //=> 'example.md'
file.basename = 'index.text';
file.history; //=> ['~/example.txt', '~/example.md', '~/index.text']
file.message('`braavo` is misspelt; did you mean `bravo`?', {line: 1, column: 8});
console.log(file.messages);
Yields:
[ { [~/index.text:1:8: `braavo` is misspelt; did you mean `bravo`?]
message: '`braavo` is misspelt; did you mean `bravo`?',
name: '~/index.text:1:8',
file: '~/index.text',
reason: '`braavo` is misspelt; did you mean `bravo`?',
line: 1,
column: 8,
location: { start: [Object], end: [Object] },
ruleId: null,
source: null,
fatal: false } ]
The following list of projects includes tools for working with virtual files. See Unist for projects working with nodes.
convert-vinyl-to-vfile
— Convert from Vinylis-vfile-message
— Check if a value is a VFileMessage
objectto-vfile
— Create a virtual file from a file-path (and optionally read it)vfile-find-down
— Find files by searching the file system downwardsvfile-find-up
— Find files by searching the file system upwardsvfile-location
— Convert between line/column- and range-based locationsvfile-statistics
— Count messages per categoryvfile-messages-to-vscode-diagnostics
— Convert to VS Code diagnosticsvfile-sort
— Sort messages by line/columnvfile-to-eslint
— Convert VFiles to ESLint formatter compatible outputThe following list of projects show linting results for given virtual files.
Reporters must accept Array.<VFile>
as their first argument, and return
string
. Reporters may accept other values too, in which case it’s suggested
to stick to vfile-reporter
s interface.
vfile-reporter
— Stylish reportervfile-reporter-json
— JSON reportervfile-reporter-pretty
— Pretty reporterVFile([options])
Create a new virtual file. If options
is string
or Buffer
, treats
it as {contents: options}
. If options
is a VFile
, returns it.
All other options are set on the newly created vfile
.
Path related properties are set in the following order (least specific
to most specific): history
, path
, basename
, stem
, extname
,
dirname
.
It’s not possible to set either dirname
or extname
without setting
either history
, path
, basename
, or stem
as well.
vfile();
vfile('console.log("alpha");');
vfile(Buffer.from('exit 1'));
vfile({path: path.join(__dirname, 'readme.md')});
vfile({stem: 'readme', extname: '.md', dirname: __dirname});
vfile({other: 'properties', are: 'copied', ov: {e: 'r'}});
vfile.contents
Buffer
, string
, null
— Raw value.
vfile.cwd
string
— Base of path
. Defaults to process.cwd()
.
vfile.path
string?
— Path of vfile
. Cannot be nullified.
vfile.basename
string?
— Current name (including extension) of vfile
. Cannot
contain path separators. Cannot be nullified either (use
file.path = file.dirname
instead).
vfile.stem
string?
— Name (without extension) of vfile
. Cannot be nullified,
and cannot contain path separators.
vfile.extname
string?
— Extension (with dot) of vfile
. Cannot be set if
there’s no path
yet and cannot contain path separators.
vfile.dirname
string?
— Path to parent directory of vfile
. Cannot be set if
there’s no path
yet.
vfile.history
Array.<string>
— List of file-paths the file moved between.
vfile.messages
Array.<VFileMessage>
— List of messages associated with the file.
vfile.data
Object
— Place to store custom information. It’s OK to store custom
data directly on the vfile
, moving it to data
gives a little more
privacy.
VFile#toString([encoding])
Convert contents of vfile
to string. If contents
is a buffer,
encoding
is used to stringify buffers (default: 'utf8'
).
VFile#message(reason[, position[, ruleId]])
Associates a message with the file for reason
at position
. When an
error is passed in as reason
, copies the stack. Each message has a fatal
property which by default is set to false
(ie. warning
).
reason
(string
or Error
)
— Reason for message, uses the stack and message of the error if givenposition
(Node
, Location
, or Position
, optional)
— Place at which the message occurred in vfile
ruleId
(string
, optional)
— Category of messageVFile#info(reason[, position[, ruleId]])
Associates an informational message with the file, where fatal
is set to
null
. Calls #message()
internally.
VFile#fail(reason[, position[, ruleId]])
Associates a fatal message with the file, then immediately throws it.
Note: fatal errors mean a file is no longer processable.
Calls #message()
internally.
VFileMessage
File-related message describing something at certain position (extends
Error
).
file
(string
) — File-path (when the message was triggered)reason
(string
) — Reason for messageruleId
(string?
) — Category of messagesource
(string?
) — Namespace of warningstack
(string?
) — Stack of messagefatal
(boolean?
) — If true
, marks associated file as no longer
processableline
(number?
) — Starting line of errorcolumn
(number?
) — Starting column of errorlocation
(object
) — Full range information, when available. Has
start
and end
properties, both set to an object with line
and
column
, set to number?
FAQs
Virtual file format for text processing
The npm package vfile receives a total of 4,999,513 weekly downloads. As such, vfile popularity was classified as popular.
We found that vfile demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.