1
1
/**
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`.
6
2
* @typedef {import('vfile').VFileOptions } Options
7
3
* @typedef {import('vfile').VFileValue } Value
8
4
*/
9
5
10
6
/**
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
+ *
11
14
* @typedef ReadOptions
12
15
* Configuration for `fs.readFile`.
13
16
* @property {BufferEncoding | null | undefined } [encoding]
58
61
import fs from 'fs'
59
62
import path from 'path'
60
63
import { URL } from 'url'
61
- import buffer from 'is-buffer'
62
64
import { VFile } from 'vfile'
63
65
64
66
// To do: next major: use `node:` prefix.
@@ -82,8 +84,8 @@ import {VFile} from 'vfile'
82
84
export function toVFile ( description ) {
83
85
if ( typeof description === 'string' || description instanceof URL ) {
84
86
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 ) }
87
89
}
88
90
89
91
// To do: do not return given file.
@@ -304,6 +306,23 @@ function looksLikeAVFile(value) {
304
306
)
305
307
}
306
308
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
+
307
326
// To do: next major: remove?
308
327
toVFile . readSync = readSync
309
328
toVFile . writeSync = writeSync
0 commit comments