Skip to content

Commit e458bdc

Browse files
committed
Update vfile
1 parent 8d484e8 commit e458bdc

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

lib/index.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
/**
2-
* @typedef {import('vfile').BufferEncoding} BufferEncoding
3-
* Encodings supported by the buffer class.
4-
*
5-
* This is a copy of the types from Node and `VFile`.
62
* @typedef {import('vfile').VFileOptions} Options
73
* @typedef {import('vfile').VFileValue} Value
84
*/
95

106
/**
7+
* @typedef {'ascii' | 'base64' | 'base64url' | 'binary' | 'hex' | 'latin1' | 'ucs-2' | 'ucs2' | 'utf-8' | 'utf16le' | 'utf8'} BufferEncoding
8+
* Encodings supported by the buffer class.
9+
*
10+
* This is a copy of the types from Node, copied to prevent Node globals from
11+
* being needed.
12+
* Copied from: <https://github.com/DefinitelyTyped/DefinitelyTyped/blob/1761eec/types/node/buffer.d.ts#L223>.
13+
*
1114
* @typedef ReadOptions
1215
* Configuration for `fs.readFile`.
1316
* @property {BufferEncoding | null | undefined} [encoding]
@@ -58,7 +61,6 @@
5861
import fs from 'fs'
5962
import path from 'path'
6063
import {URL} from 'url'
61-
import buffer from 'is-buffer'
6264
import {VFile} from 'vfile'
6365

6466
// To do: next major: use `node:` prefix.
@@ -82,8 +84,8 @@ import {VFile} from 'vfile'
8284
export function toVFile(description) {
8385
if (typeof description === 'string' || description instanceof URL) {
8486
description = {path: description}
85-
} else if (buffer(description)) {
86-
description = {path: String(description)}
87+
} else if (isUint8Array(description)) {
88+
description = {path: new TextDecoder().decode(description)}
8789
}
8890

8991
// To do: do not return given file.
@@ -304,6 +306,23 @@ function looksLikeAVFile(value) {
304306
)
305307
}
306308

309+
/**
310+
* Check whether `value` is an `Uint8Array`.
311+
*
312+
* @param {unknown} value
313+
* thing.
314+
* @returns {value is Uint8Array}
315+
* Whether `value` is an `Uint8Array`.
316+
*/
317+
function isUint8Array(value) {
318+
return Boolean(
319+
value &&
320+
typeof value === 'object' &&
321+
'byteLength' in value &&
322+
'byteOffset' in value
323+
)
324+
}
325+
307326
// To do: next major: remove?
308327
toVFile.readSync = readSync
309328
toVFile.writeSync = writeSync

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
"index.js"
3636
],
3737
"dependencies": {
38-
"is-buffer": "^2.0.0",
39-
"vfile": "^5.1.0"
38+
"vfile": "^6.0.0"
4039
},
4140
"devDependencies": {
4241
"@types/node": "^20.0.0",

test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ test('toVFile', async function (t) {
4343
assert.equal(file.value, undefined)
4444
})
4545

46+
await t.test('should accept a uint array as `.path`', function () {
47+
const file = toVFile(new Uint8Array([0x61, 0x62, 0x63, 0x2e, 0x6d, 0x64]))
48+
49+
assert.equal(file.path, 'abc.md')
50+
})
51+
4652
await t.test('should accept an object', function () {
4753
const file = toVFile({
4854
dirname: join('foo', 'bar'),

0 commit comments

Comments
 (0)