Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More β†’
Socket
Sign inDemoInstall
Socket

vfile

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vfile - npm Package Compare versions

Comparing version 4.2.1 to 5.0.0

index.d.ts

12

index.js

@@ -1,3 +0,11 @@

'use strict'
/**
* @typedef {import('./lib/index.js').BufferEncoding} BufferEncoding
* @typedef {import('./lib/index.js').VFileValue} VFileValue
* @typedef {import('./lib/index.js').VFileOptions} VFileOptions
* @typedef {import('./lib/index.js').VFileCompatible} VFileCompatible
*
* @typedef {import('./lib/index.js').VFileReporterSettings} VFileReporterSettings
* @typedef {import('./lib/index.js').VFileReporter} VFileReporter
*/
module.exports = require('./lib')
export {VFile} from './lib/index.js'

@@ -1,46 +0,350 @@

'use strict'
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Position} Position
* @typedef {import('unist').Point} Point
*
* @typedef {'ascii'|'utf8'|'utf-8'|'utf16le'|'ucs2'|'ucs-2'|'base64'|'latin1'|'binary'|'hex'} BufferEncoding
* Encodings supported by the buffer class.
* This is a copy of the typing from Node, copied to prevent Node globals from
* being needed.
* Copied from: <https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a2bc1d8/types/node/globals.d.ts#L174>
*
* @typedef {string|Uint8Array} VFileValue Contents of the file.
* Can either be text, or a Buffer like structure.
* This does not directly use type `Buffer`, because it can also be used in a
* browser context.
* Instead this leverages `Uint8Array` which is the base type for `Buffer`,
* and a native JavaScript construct.
*
* @typedef {VFileValue|VFileOptions|VFile} VFileCompatible Things that can be
* passed to the constructor.
*
* @typedef VFileCoreOptions
* @property {VFileValue} [value]
* @property {string} [cwd]
* @property {Array.<string>} [history]
* @property {string} [path]
* @property {string} [basename]
* @property {string} [stem]
* @property {string} [extname]
* @property {string} [dirname]
* @property {Object.<string, unknown>} [data]
*
* @typedef {{[key: string]: unknown} & VFileCoreOptions} VFileOptions
* Configuration: a bunch of keys that will be shallow copied over to the new
* file.
*
* @typedef {Object.<string, unknown>} VFileReporterSettings
* @typedef {<T = VFileReporterSettings>(files: VFile[], options: T) => string} VFileReporter
*/
var VMessage = require('vfile-message')
var VFile = require('./core.js')
import buffer from 'is-buffer'
import {path} from './minpath.js'
import {proc} from './minproc.js'
import {VFileMessage} from 'vfile-message'
module.exports = VFile
// Order of setting (least specific to most), we need this because otherwise
// `{stem: 'a', path: '~/b.js'}` would throw, as a path is needed before a
// stem can be set.
var order = ['history', 'path', 'basename', 'stem', 'extname', 'dirname']
VFile.prototype.message = message
VFile.prototype.info = info
VFile.prototype.fail = fail
export class VFile {
/**
* Create a new virtual file.
*
* If `options` is `string` or `Buffer`, treats it as `{value: options}`.
* If `options` is a `VFile`, shallow copies its data over to the new file.
* All other given fields 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.
*
* @param {VFileCompatible} [value]
*/
constructor(value) {
var index = -1
/** @type {VFileOptions} */
var options
/** @type {string} */
var prop
// Create a message with `reason` at `position`.
// When an error is passed in as `reason`, copies the stack.
function message(reason, position, origin) {
var message = new VMessage(reason, position, origin)
if (!value) {
options = {}
} else if (typeof value === 'string' || buffer(value)) {
// @ts-ignore Looks like a buffer.
options = {value}
} else {
// @ts-ignore Looks like file or options.
options = value
}
if (this.path) {
message.name = this.path + ':' + message.name
message.file = this.path
/**
* Place to store custom information.
* It’s OK to store custom data directly on the file, moving it to `data`
* gives a little more privacy.
* @type {Object.<string, unknown>}
*/
this.data = {}
/**
* List of messages associated with the file.
* @type {Array.<VFileMessage>}
*/
this.messages = []
/**
* List of file paths the file moved between.
* @type {Array.<string>}
*/
this.history = []
/**
* Base of `path`.
* Defaults to `process.cwd()` (`/` in browsers).
* @type {string}
*/
this.cwd = proc.cwd()
/* eslint-disable no-unused-expressions */
/**
* Raw value.
* @type {VFileValue}
*/
this.value
/* eslint-enable no-unused-expressions */
// Set path related properties in the correct order.
while (++index < order.length) {
prop = order[index]
// Note: we specifically use `in` instead of `hasOwnProperty` to accept
// `vfile`s too.
if (prop in options && options[prop] !== undefined) {
this[prop] = prop === 'history' ? options[prop].concat() : options[prop]
}
}
// Set non-path related properties.
for (prop in options) {
if (!order.includes(prop)) {
this[prop] = options[prop]
}
}
}
message.fatal = false
/**
* Access full path (`~/index.min.js`).
*/
get path() {
return this.history[this.history.length - 1]
}
this.messages.push(message)
/**
* Set full path (`~/index.min.js`).
* Cannot be nullified.
*/
set path(path) {
assertNonEmpty(path, 'path')
return message
}
if (this.path !== path) {
this.history.push(path)
}
}
// Fail: creates a vmessage, associates it with the file, and throws it.
function fail() {
var message = this.message.apply(this, arguments)
/**
* Access parent path (`~`).
*/
get dirname() {
return typeof this.path === 'string' ? path.dirname(this.path) : undefined
}
message.fatal = true
/**
* Set parent path (`~`).
* Cannot be set if there's no `path` yet.
*/
set dirname(dirname) {
assertPath(this.path, 'dirname')
this.path = path.join(dirname || '', this.basename)
}
throw message
/**
* Access basename (including extname) (`index.min.js`).
*/
get basename() {
return typeof this.path === 'string' ? path.basename(this.path) : undefined
}
/**
* Set basename (`index.min.js`).
* Cannot contain path separators.
* Cannot be nullified either (use `file.path = file.dirname` instead).
*/
set basename(basename) {
assertNonEmpty(basename, 'basename')
assertPart(basename, 'basename')
this.path = path.join(this.dirname || '', basename)
}
/**
* Access extname (including dot) (`.js`).
*/
get extname() {
return typeof this.path === 'string' ? path.extname(this.path) : undefined
}
/**
* Set extname (including dot) (`.js`).
* Cannot be set if there's no `path` yet and cannot contain path separators.
*/
set extname(extname) {
assertPart(extname, 'extname')
assertPath(this.path, 'extname')
if (extname) {
if (extname.charCodeAt(0) !== 46 /* `.` */) {
throw new Error('`extname` must start with `.`')
}
if (extname.includes('.', 1)) {
throw new Error('`extname` cannot contain multiple dots')
}
}
this.path = path.join(this.dirname, this.stem + (extname || ''))
}
/**
* Access stem (w/o extname) (`index.min`).
*/
get stem() {
return typeof this.path === 'string'
? path.basename(this.path, this.extname)
: undefined
}
/**
* Set stem (w/o extname) (`index.min`).
* Cannot be nullified, and cannot contain path separators.
*/
set stem(stem) {
assertNonEmpty(stem, 'stem')
assertPart(stem, 'stem')
this.path = path.join(this.dirname || '', stem + (this.extname || ''))
}
/**
* Serialize the file.
*
* @param {BufferEncoding} [encoding='utf8'] If `file.value` is a buffer, `encoding` is used to serialize buffers.
* @returns {string}
*/
toString(encoding) {
// @ts-ignore string’s don’t accept the parameter, but buffers do.
return (this.value || '').toString(encoding)
}
/**
* Create a message and associates it w/ the file.
*
* @param {string|Error} reason Reason for message (`string` or `Error`). Uses the stack and message of the error if given.
* @param {Node|Position|Point} [place] Place at which the message occurred in a file (`Node`, `Position`, or `Point`, optional).
* @param {string} [origin] Place in code the message originates from (`string`, optional).
* @returns {VFileMessage}
*/
message(reason, place, origin) {
var message = new VFileMessage(reason, place, origin)
if (this.path) {
message.name = this.path + ':' + message.name
message.file = this.path
}
message.fatal = false
this.messages.push(message)
return message
}
/**
* Info: create a message, associate it with the file, and mark the fatality
* as `null`.
* Calls `message()` internally.
*
* @param {string|Error} reason Reason for message (`string` or `Error`). Uses the stack and message of the error if given.
* @param {Node|Position|Point} [place] Place at which the message occurred in a file (`Node`, `Position`, or `Point`, optional).
* @param {string} [origin] Place in code the message originates from (`string`, optional).
* @returns {VFileMessage}
*/
info(reason, place, origin) {
var message = this.message(reason, place, origin)
message.fatal = null
return message
}
/**
* Fail: create a message, associate it with the file, mark the fatality as
* `true`.
* Note: fatal errors mean a file is no longer processable.
* Calls `message()` internally.
*
* @param {string|Error} reason Reason for message (`string` or `Error`). Uses the stack and message of the error if given.
* @param {Node|Position|Point} [place] Place at which the message occurred in a file (`Node`, `Position`, or `Point`, optional).
* @param {string} [origin] Place in code the message originates from (`string`, optional).
* @returns {never}
*/
fail(reason, place, origin) {
var message = this.message(reason, place, origin)
message.fatal = true
throw message
}
}
// Info: creates a vmessage, associates it with the file, and marks the fatality
// as null.
function info() {
var message = this.message.apply(this, arguments)
/**
* Assert that `part` is not a path (as in, does not contain `path.sep`).
*
* @param {string} part
* @param {string} name
* @returns {void}
*/
function assertPart(part, name) {
if (part && part.includes(path.sep)) {
throw new Error(
'`' + name + '` cannot be a path: did not expect `' + path.sep + '`'
)
}
}
message.fatal = null
/**
* Assert that `part` is not empty.
*
* @param {string} part
* @param {string} name
* @returns {void}
*/
function assertNonEmpty(part, name) {
if (!part) {
throw new Error('`' + name + '` cannot be empty')
}
}
return message
/**
* Assert `path` exists.
*
* @param {string} path
* @param {string} name
* @returns {void}
*/
function assertPath(path, name) {
if (!path) {
throw new Error('Setting `' + name + '` requires `path` to be set too')
}
}

87

lib/minpath.browser.js

@@ -1,3 +0,1 @@

'use strict'
// A derivative work based on:

@@ -54,14 +52,19 @@ // <https://github.com/browserify/path-browserify>.

exports.basename = basename
exports.dirname = dirname
exports.extname = extname
exports.join = join
exports.sep = '/'
export const path = {basename, dirname, extname, join, sep: '/'}
/**
* @param {string} path
* @param {string} [ext]
* @returns {string}
*/
function basename(path, ext) {
var start = 0
var end = -1
/** @type {number} */
var index
/** @type {number} */
var firstNonSlashEnd
/** @type {boolean} */
var seenNonSlash
/** @type {number} */
var extIndex

@@ -76,3 +79,3 @@

if (ext === undefined || !ext.length || ext.length > path.length) {
if (ext === undefined || ext.length === 0 || ext.length > path.length) {
while (index--) {

@@ -147,5 +150,12 @@ if (path.charCodeAt(index) === 47 /* `/` */) {

/**
* @param {string} path
* @returns {string}
*/
function dirname(path) {
/** @type {number} */
var end
/** @type {boolean} */
var unmatchedSlash
/** @type {number} */
var index

@@ -155,3 +165,3 @@

if (!path.length) {
if (path.length === 0) {
return '.'

@@ -185,2 +195,6 @@ }

/**
* @param {string} path
* @returns {string}
*/
function extname(path) {

@@ -193,4 +207,7 @@ var startDot = -1

var preDotState = 0
/** @type {boolean} */
var unmatchedSlash
/** @type {number} */
var code
/** @type {number} */
var index

@@ -251,14 +268,17 @@

function join() {
/**
* @param {Array.<string>} segments
* @returns {string}
*/
function join(...segments) {
var index = -1
/** @type {string} */
var joined
while (++index < arguments.length) {
assertPath(arguments[index])
while (++index < segments.length) {
assertPath(segments[index])
if (arguments[index]) {
if (segments[index]) {
joined =
joined === undefined
? arguments[index]
: joined + '/' + arguments[index]
joined === undefined ? segments[index] : joined + '/' + segments[index]
}

@@ -270,6 +290,13 @@ }

// Note: `normalize` is not exposed as `path.normalize`, so some code is
// manually removed from it.
/**
* Note: `normalize` is not exposed as `path.normalize`, so some code is
* manually removed from it.
*
* @param {string} path
* @returns {string}
*/
function normalize(path) {
/** @type {boolean} */
var absolute
/** @type {string} */
var value

@@ -284,7 +311,7 @@

if (!value.length && !absolute) {
if (value.length === 0 && !absolute) {
value = '.'
}
if (value.length && path.charCodeAt(path.length - 1) === 47 /* / */) {
if (value.length > 0 && path.charCodeAt(path.length - 1) === 47 /* / */) {
value += '/'

@@ -296,3 +323,9 @@ }

// Resolve `.` and `..` elements in a path with directory names.
/**
* Resolve `.` and `..` elements in a path with directory names.
*
* @param {string} path
* @param {boolean} allowAboveRoot
* @returns {string}
*/
function normalizeString(path, allowAboveRoot) {

@@ -304,3 +337,5 @@ var result = ''

var index = -1
/** @type {number} */
var code
/** @type {number} */
var lastSlashIndex

@@ -330,3 +365,2 @@

/* istanbul ignore else - No clue how to cover it. */
if (lastSlashIndex !== result.length - 1) {

@@ -345,3 +379,3 @@ if (lastSlashIndex < 0) {

}
} else if (result.length) {
} else if (result.length > 0) {
result = ''

@@ -356,7 +390,7 @@ lastSegmentLength = 0

if (allowAboveRoot) {
result = result.length ? result + '/..' : '..'
result = result.length > 0 ? result + '/..' : '..'
lastSegmentLength = 2
}
} else {
if (result.length) {
if (result.length > 0) {
result += '/' + path.slice(lastSlash + 1, index)

@@ -382,2 +416,5 @@ } else {

/**
* @param {string} path
*/
function assertPath(path) {

@@ -384,0 +421,0 @@ if (typeof path !== 'string') {

@@ -1,3 +0,3 @@

'use strict'
import path from 'path'
module.exports = require('path')
export {path}

@@ -1,7 +0,5 @@

'use strict'
// Somewhat based on:
// <https://github.com/defunctzombie/node-process/blob/master/browser.js>.
// But I don’t think one tiny line of code can be copyrighted. πŸ˜…
exports.cwd = cwd
export const proc = {cwd}

@@ -8,0 +6,0 @@ function cwd() {

@@ -1,3 +0,1 @@

'use strict'
module.exports = process
export const proc = process
{
"name": "vfile",
"version": "4.2.1",
"version": "5.0.0",
"description": "Virtual file format for text processing",

@@ -34,3 +34,6 @@ "license": "MIT",

],
"types": "types/index.d.ts",
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"browser": {

@@ -46,5 +49,4 @@ "./lib/minpath.js": "./lib/minpath.browser.js",

"lib/",
"types/index.d.ts",
"index.js",
"core.js"
"index.d.ts",
"index.js"
],

@@ -54,32 +56,25 @@ "dependencies": {

"is-buffer": "^2.0.0",
"unist-util-stringify-position": "^2.0.0",
"vfile-message": "^2.0.0"
"unist-util-stringify-position": "^3.0.0",
"vfile-message": "^3.0.0"
},
"devDependencies": {
"browserify": "^17.0.0",
"dtslint": "^4.0.0",
"nyc": "^15.0.0",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"xo": "^0.35.0"
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . --write && xo --fix",
"build-bundle": "browserify . -s VFile -o vfile.js",
"build-mangle": "browserify . -s VFile -o vfile.min.js -p tinyify",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test-types": "dtslint types",
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
"prepack": "npm run build && npm run format",
"build": "rimraf \"{lib/**,}*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run build && npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {

@@ -95,14 +90,8 @@ "tabWidth": 2,

"prettier": true,
"esnext": false,
"ignores": [
"types",
"vfile.js"
],
"rules": {
"unicorn/explicit-length-check": "off",
"unicorn/prefer-includes": "off",
"unicorn/prefer-reflect-apply": "off",
"unicorn/prefer-number-properties": "off",
"unicorn/no-array-for-each": "off",
"max-depth": "off",
"complexity": "off"
"complexity": "off",
"no-var": "off",
"prefer-arrow-callback": "off"
}

@@ -118,3 +107,9 @@ },

]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}
<h1>
<img src="https://raw.githubusercontent.com/vfile/vfile/7e1e6a6/logo.svg?sanitize=true" alt="vfile" width="400" />
<img src="https://raw.githubusercontent.com/vfile/vfile/fc8164b/logo.svg?sanitize=true" alt="vfile" />
</h1>

@@ -14,3 +14,3 @@

**vfile** is a small and browser friendly virtual file format that tracks
metadata (such as a file’s `path` and `contents`) and [messages][].
metadata (such as a file’s `path` and `value`) and [messages][].

@@ -38,3 +38,3 @@ It was made specifically for **[unified][]** and generally for the common task

* [`VFile(options?)`](#vfileoptions)
* [`vfile.contents`](#vfilecontents)
* [`vfile.value`](#vfilevalue)
* [`vfile.cwd`](#vfilecwd)

@@ -62,2 +62,5 @@ * [`vfile.path`](#vfilepath)

This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
[npm][]:

@@ -72,5 +75,5 @@

```js
var vfile = require('vfile')
import {VFile} from 'vfile'
var file = vfile({path: '~/example.txt', contents: 'Alpha *braavo* charlie.'})
var file = new VFile({path: '~/example.txt', value: 'Alpha *braavo* charlie.'})

@@ -88,6 +91,3 @@ file.path // => '~/example.txt'

file.message('`braavo` is misspelt; did you mean `bravo`?', {
line: 1,
column: 8
})
file.message('`braavo` is misspelt; did you mean `bravo`?', {line: 1, column: 8})

@@ -99,14 +99,15 @@ console.log(file.messages)

```js
[ { [~/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',
```txt
[
[~/index.text:1:8: `braavo` is misspelt; did you mean `bravo`?] {
reason: '`braavo` is misspelt; did you mean `bravo`?',
line: 1,
column: 8,
location: { start: [Object], end: [Object] },
source: null,
ruleId: null,
source: null,
fatal: false } ]
position: {start: [Object], end: [Object]},
file: '~/index.text',
fatal: false
}
]
```

@@ -116,8 +117,11 @@

This package exports the following identifiers: `VFile`.
There is no default export.
### `VFile(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`.
If `options` is `string` or `Buffer`, treats it as `{value: options}`.
If `options` is a `VFile`, shallow copies its data over to the new file.
All other given fields are set on the newly created `VFile`.

@@ -133,11 +137,11 @@ Path related properties are set in the following order (least specific to most

```js
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'}})
new VFile()
new VFile('console.log("alpha");')
new VFile(Buffer.from('exit 1'))
new VFile({path: path.join('path', 'to', 'readme.md')})
new VFile({stem: 'readme', extname: '.md', dirname: path.join('path', 'to')})
new VFile({other: 'properties', are: 'copied', ov: {e: 'r'}})
```
### `vfile.contents`
### `vfile.value`

@@ -193,5 +197,5 @@ `Buffer`, `string`, `null` β€” Raw value.

Convert contents of `vfile` to string.
When `contents` is a [`Buffer`][buffer], `encoding` is a
[character encoding][encoding] to understand `doc` as (`string`, default:
Convert value of `vfile` to string.
When `value` is a [`Buffer`][buffer], `encoding` is a
[character encoding][encoding] to understand it as (`string`, default:
`'utf8'`).

@@ -198,0 +202,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc