Skip to content

Commit fb49556

Browse files
committed
Change to replace Buffer with Uint8Array
Previously, `VFile` supported `Buffer`, which is Node.js-specific. It changed to `Uint8Array`. In most cases, this will be fine, because the Node `Buffer` class subclasses `Uint8Array`. Related-to: vfile/vfile@f4edd0d.
1 parent ad06700 commit fb49556

File tree

6 files changed

+56
-37
lines changed

6 files changed

+56
-37
lines changed

index.d.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export type Processor<
111111
* * If the plugin sets a parser, then this should be the node type that
112112
* the parser yields.
113113
* * If the plugin sets a compiler, then this should be the result that
114-
* the compiler yields (`string`, `Buffer`, or something else).
114+
* the compiler yields (`string`, `Uint8Array`, or something else).
115115
* @param plugin
116116
* Plugin (function) to use.
117117
* Plugins are deduped based on identity: passing a function in twice will
@@ -162,7 +162,7 @@ export type Processor<
162162
* * If the plugin sets a parser, then this should be the node type that
163163
* the parser yields.
164164
* * If the plugin sets a compiler, then this should be the result that
165-
* the compiler yields (`string`, `Buffer`, or something else).
165+
* the compiler yields (`string`, `Uint8Array`, or something else).
166166
* @param tuple
167167
* A tuple where the first item is a plugin (function) to use and other
168168
* items are options.
@@ -260,7 +260,7 @@ export type FrozenProcessor<
260260
* @param file
261261
* `VFile` or anything that can be given to `new VFile()`, optional.
262262
* @returns
263-
* New content: compiled text (`string` or `Buffer`) or something else.
263+
* New content: compiled text (`string` or `Uint8Array`) or something else.
264264
* This depends on which plugins you use: typically text, but could for
265265
* example be a React node.
266266
*/
@@ -347,8 +347,8 @@ export type FrozenProcessor<
347347
*
348348
* The result from the compiler is stored on the file.
349349
* What the result is depends on which plugins you use.
350-
* The result is typically text (`string` or `Buffer`), which can be retrieved
351-
* with `file.toString()` (or `String(file)`).
350+
* The result is typically text (`string` or `Uint8Array`), which can be
351+
* retrieved with `file.toString()` (or `String(file)`).
352352
* In some cases, such as when using `rehypeReact` to create a React node,
353353
* the result is stored on `file.result`.
354354
*
@@ -375,8 +375,8 @@ export type FrozenProcessor<
375375
*
376376
* The result from the compiler is stored on the file.
377377
* What the result is depends on which plugins you use.
378-
* The result is typically text (`string` or `Buffer`), which can be retrieved
379-
* with `file.toString()` (or `String(file)`).
378+
* The result is typically text (`string` or `Uint8Array`), which can be
379+
* retrieved with `file.toString()` (or `String(file)`).
380380
* In some cases, such as when using `rehypeReact` to create a React node,
381381
* the result is stored on `file.result`.
382382
*
@@ -399,8 +399,8 @@ export type FrozenProcessor<
399399
*
400400
* The result from the compiler is stored on the file.
401401
* What the result is depends on which plugins you use.
402-
* The result is typically text (`string` or `Buffer`), which can be retrieved
403-
* with `file.toString()` (or `String(file)`).
402+
* The result is typically text (`string` or `Uint8Array`), which can be
403+
* retrieved with `file.toString()` (or `String(file)`).
404404
* In some cases, such as when using `rehypeReact` to create a React node,
405405
* the result is stored on `file.result`.
406406
*
@@ -502,7 +502,7 @@ export type FrozenProcessor<
502502
* * If the plugin sets a parser, then this should be the node type that
503503
* the parser yields.
504504
* * If the plugin sets a compiler, then this should be the result that
505-
* the compiler yields (`string`, `Buffer`, or something else).
505+
* the compiler yields (`string`, `Uint8Array`, or something else).
506506
* @this
507507
* The current processor.
508508
* Plugins can configure the processor by interacting with `this.Parser` or
@@ -580,7 +580,7 @@ export type Preset = {
580580
* * If the plugin sets a parser, then this should be the node type that
581581
* the parser yields.
582582
* * If the plugin sets a compiler, then this should be the result that
583-
* the compiler yields (`string`, `Buffer`, or something else).
583+
* the compiler yields (`string`, `Uint8Array`, or something else).
584584
*/
585585
export type PluginTuple<
586586
PluginParameters extends any[] = any[],
@@ -780,8 +780,8 @@ export class CompilerClass<Tree extends Node = Node, Result = unknown> {
780780
* Compile a tree.
781781
*
782782
* @returns
783-
* New content: compiled text (`string` or `Buffer`, for `file.value`) or
784-
* something else (for `file.result`).
783+
* New content: compiled text (`string` or `Uint8Array`, for
784+
* `file.value`) or something else (for `file.result`).
785785
*/
786786
compile(): Result
787787
}
@@ -811,7 +811,7 @@ export class CompilerClass<Tree extends Node = Node, Result = unknown> {
811811
* @param file
812812
* File associated with `tree`.
813813
* @returns
814-
* New content: compiled text (`string` or `Buffer`, for `file.value`) or
814+
* New content: compiled text (`string` or `Uint8Array`, for `file.value`) or
815815
* something else (for `file.result`).
816816
*/
817817
export type CompilerFunction<Tree extends Node = Node, Result = unknown> = (

index.test-d.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -524,27 +524,32 @@ expectType<string>(processorWithRehypeStringify.stringify(hastRoot))
524524
processorWithRehypeStringify.stringify(mdastRoot)
525525
expectType<VFile>(processorWithRehypeStringify.processSync(''))
526526

527-
// Compile plugin (to a buffer).
528-
const rehypeStringifyBuffer: Plugin<[], HastRoot, Uint8Array> = function () {
529-
// Empty.
530-
}
527+
// Compile plugin (to an `Uint8Array`).
528+
const rehypeStringifyUint8Array: Plugin<[], HastRoot, Uint8Array> =
529+
function () {
530+
// Empty.
531+
}
531532

532-
const processorWithRehypeStringifyBuffer = unified().use(rehypeStringifyBuffer)
533+
const processorWithRehypeStringifyUint8Array = unified().use(
534+
rehypeStringifyUint8Array
535+
)
533536

534537
// To do: ?
535538
expectType<Processor<HastRoot, HastRoot, HastRoot>>(
536-
processorWithRehypeStringifyBuffer
539+
processorWithRehypeStringifyUint8Array
537540
)
538541
// To do: yield `UnistNode`?
539-
expectType<HastRoot>(processorWithRehypeStringifyBuffer.parse(''))
542+
expectType<HastRoot>(processorWithRehypeStringifyUint8Array.parse(''))
540543
// @ts-expect-error: to do: accept `UnistNode`?
541-
processorWithRehypeStringifyBuffer.runSync(mdastRoot)
544+
processorWithRehypeStringifyUint8Array.runSync(mdastRoot)
542545
// To do: accept, yield `UnistNode`?
543-
expectType<HastRoot>(processorWithRehypeStringifyBuffer.runSync(hastRoot))
544-
expectType<Uint8Array>(processorWithRehypeStringifyBuffer.stringify(hastRoot))
546+
expectType<HastRoot>(processorWithRehypeStringifyUint8Array.runSync(hastRoot))
547+
expectType<Uint8Array>(
548+
processorWithRehypeStringifyUint8Array.stringify(hastRoot)
549+
)
545550
// @ts-expect-error: not the correct node type.
546-
processorWithRehypeStringifyBuffer.stringify(mdastRoot)
547-
expectType<VFile>(processorWithRehypeStringifyBuffer.processSync(''))
551+
processorWithRehypeStringifyUint8Array.stringify(mdastRoot)
552+
expectType<VFile>(processorWithRehypeStringifyUint8Array.processSync(''))
548553

549554
// Compile plugin (to a non-node).
550555
const rehypeReact: Plugin<[], HastRoot, ReactNode> = function () {

lib/index.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import structuredClone from '@ungap/structured-clone'
2020
import {bail} from 'bail'
21-
import isBuffer from 'is-buffer'
2221
import isPlainObj from 'is-plain-obj'
2322
import {trough} from 'trough'
2423
import {VFile} from 'vfile'
@@ -601,5 +600,22 @@ function looksLikeAVFile(value) {
601600
* @returns {value is VFileValue}
602601
*/
603602
function looksLikeAVFileValue(value) {
604-
return typeof value === 'string' || isBuffer(value)
603+
return typeof value === 'string' || isUint8Array(value)
604+
}
605+
606+
/**
607+
* Assert `value` is an `Uint8Array`.
608+
*
609+
* @param {unknown} value
610+
* thing.
611+
* @returns {value is Uint8Array}
612+
* Whether `value` is an `Uint8Array`.
613+
*/
614+
function isUint8Array(value) {
615+
return Boolean(
616+
value &&
617+
typeof value === 'object' &&
618+
'byteLength' in value &&
619+
'byteOffset' in value
620+
)
605621
}

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"@types/unist": "^3.0.0",
4949
"@ungap/structured-clone": "^1.0.0",
5050
"bail": "^2.0.0",
51-
"is-buffer": "^2.0.0",
5251
"is-plain-obj": "^4.0.0",
5352
"trough": "^2.0.0",
5453
"vfile": "^6.0.0"

readme.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,10 @@ Compile a syntax tree.
526526

527527
###### Returns
528528

529-
Textual representation of the tree (`string` or `Buffer`, see note).
529+
Textual representation of the tree (`string` or `Uint8Array`, see note).
530530

531531
> 👉 **Note**: unified typically compiles by serializing: most
532-
> [compilers][compiler] return `string` (or `Buffer`).
532+
> [compilers][compiler] return `string` (or `Uint8Array`).
533533
> Some compilers, such as the one configured with
534534
> [`rehype-react`][rehype-react], return other values (in this case, a React
535535
> tree).
@@ -575,7 +575,7 @@ Instances must have a `compile` method that is called without arguments and
575575
should return a `string`.
576576

577577
> 👉 **Note**: unified typically compiles by serializing: most compilers
578-
> return `string` (or `Buffer`).
578+
> return `string` (or `Uint8Array`).
579579
> Some compilers, such as the one configured with
580580
> [`rehype-react`][rehype-react], return other values (in this case, a React
581581
> tree).
@@ -693,7 +693,7 @@ The parsed, transformed, and compiled value is available at
693693
[`file.value`][vfile-value] (see note).
694694

695695
> 👉 **Note**: unified typically compiles by serializing: most
696-
> [compilers][compiler] return `string` (or `Buffer`).
696+
> [compilers][compiler] return `string` (or `Uint8Array`).
697697
> Some compilers, such as the one configured with
698698
> [`rehype-react`][rehype-react], result in other values (in this case, a React
699699
> tree).
@@ -805,7 +805,7 @@ The parsed, transformed, and compiled value is available at
805805
[`file.value`][vfile-value] (see note).
806806

807807
> 👉 **Note**: unified typically compiles by serializing: most
808-
> [compilers][compiler] return `string` (or `Buffer`).
808+
> [compilers][compiler] return `string` (or `Uint8Array`).
809809
> Some compilers, such as the one configured with
810810
> [`rehype-react`][rehype-react], result in other values (in this case, a React
811811
> tree).

test/process-compilers.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {Buffer} from 'node:buffer'
21
import assert from 'node:assert/strict'
32
import test from 'node:test'
43
import {unified} from 'unified'
@@ -20,9 +19,9 @@ test('process (compilers)', async function (t) {
2019
assert.equal(file.result, undefined)
2120
})
2221

23-
await t.test('should compile `buffer`', async function () {
22+
await t.test('should compile `Uint8Array`', async function () {
2423
const processor = unified()
25-
const result = Buffer.from('bravo')
24+
const result = new Uint8Array([0xef, 0xbb, 0xbf, 0x61, 0x62, 0x63])
2625

2726
processor.Parser = SimpleParser
2827
processor.Compiler = function () {

0 commit comments

Comments
 (0)