Skip to content

Commit

Permalink
Change to replace Buffer with Uint8Array
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wooorm committed Aug 10, 2023
1 parent ad06700 commit fb49556
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 37 deletions.
28 changes: 14 additions & 14 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export type Processor<
* * If the plugin sets a parser, then this should be the node type that
* the parser yields.
* * If the plugin sets a compiler, then this should be the result that
* the compiler yields (`string`, `Buffer`, or something else).
* the compiler yields (`string`, `Uint8Array`, or something else).
* @param plugin
* Plugin (function) to use.
* Plugins are deduped based on identity: passing a function in twice will
Expand Down Expand Up @@ -162,7 +162,7 @@ export type Processor<
* * If the plugin sets a parser, then this should be the node type that
* the parser yields.
* * If the plugin sets a compiler, then this should be the result that
* the compiler yields (`string`, `Buffer`, or something else).
* the compiler yields (`string`, `Uint8Array`, or something else).
* @param tuple
* A tuple where the first item is a plugin (function) to use and other
* items are options.
Expand Down Expand Up @@ -260,7 +260,7 @@ export type FrozenProcessor<
* @param file
* `VFile` or anything that can be given to `new VFile()`, optional.
* @returns
* New content: compiled text (`string` or `Buffer`) or something else.
* New content: compiled text (`string` or `Uint8Array`) or something else.
* This depends on which plugins you use: typically text, but could for
* example be a React node.
*/
Expand Down Expand Up @@ -347,8 +347,8 @@ export type FrozenProcessor<
*
* The result from the compiler is stored on the file.
* What the result is depends on which plugins you use.
* The result is typically text (`string` or `Buffer`), which can be retrieved
* with `file.toString()` (or `String(file)`).
* The result is typically text (`string` or `Uint8Array`), which can be
* retrieved with `file.toString()` (or `String(file)`).
* In some cases, such as when using `rehypeReact` to create a React node,
* the result is stored on `file.result`.
*
Expand All @@ -375,8 +375,8 @@ export type FrozenProcessor<
*
* The result from the compiler is stored on the file.
* What the result is depends on which plugins you use.
* The result is typically text (`string` or `Buffer`), which can be retrieved
* with `file.toString()` (or `String(file)`).
* The result is typically text (`string` or `Uint8Array`), which can be
* retrieved with `file.toString()` (or `String(file)`).
* In some cases, such as when using `rehypeReact` to create a React node,
* the result is stored on `file.result`.
*
Expand All @@ -399,8 +399,8 @@ export type FrozenProcessor<
*
* The result from the compiler is stored on the file.
* What the result is depends on which plugins you use.
* The result is typically text (`string` or `Buffer`), which can be retrieved
* with `file.toString()` (or `String(file)`).
* The result is typically text (`string` or `Uint8Array`), which can be
* retrieved with `file.toString()` (or `String(file)`).
* In some cases, such as when using `rehypeReact` to create a React node,
* the result is stored on `file.result`.
*
Expand Down Expand Up @@ -502,7 +502,7 @@ export type FrozenProcessor<
* * If the plugin sets a parser, then this should be the node type that
* the parser yields.
* * If the plugin sets a compiler, then this should be the result that
* the compiler yields (`string`, `Buffer`, or something else).
* the compiler yields (`string`, `Uint8Array`, or something else).
* @this
* The current processor.
* Plugins can configure the processor by interacting with `this.Parser` or
Expand Down Expand Up @@ -580,7 +580,7 @@ export type Preset = {
* * If the plugin sets a parser, then this should be the node type that
* the parser yields.
* * If the plugin sets a compiler, then this should be the result that
* the compiler yields (`string`, `Buffer`, or something else).
* the compiler yields (`string`, `Uint8Array`, or something else).
*/
export type PluginTuple<
PluginParameters extends any[] = any[],
Expand Down Expand Up @@ -780,8 +780,8 @@ export class CompilerClass<Tree extends Node = Node, Result = unknown> {
* Compile a tree.
*
* @returns
* New content: compiled text (`string` or `Buffer`, for `file.value`) or
* something else (for `file.result`).
* New content: compiled text (`string` or `Uint8Array`, for
* `file.value`) or something else (for `file.result`).
*/
compile(): Result
}
Expand Down Expand Up @@ -811,7 +811,7 @@ export class CompilerClass<Tree extends Node = Node, Result = unknown> {
* @param file
* File associated with `tree`.
* @returns
* New content: compiled text (`string` or `Buffer`, for `file.value`) or
* New content: compiled text (`string` or `Uint8Array`, for `file.value`) or
* something else (for `file.result`).
*/
export type CompilerFunction<Tree extends Node = Node, Result = unknown> = (
Expand Down
29 changes: 17 additions & 12 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,27 +524,32 @@ expectType<string>(processorWithRehypeStringify.stringify(hastRoot))
processorWithRehypeStringify.stringify(mdastRoot)
expectType<VFile>(processorWithRehypeStringify.processSync(''))

// Compile plugin (to a buffer).
const rehypeStringifyBuffer: Plugin<[], HastRoot, Uint8Array> = function () {
// Empty.
}
// Compile plugin (to an `Uint8Array`).
const rehypeStringifyUint8Array: Plugin<[], HastRoot, Uint8Array> =
function () {
// Empty.
}

const processorWithRehypeStringifyBuffer = unified().use(rehypeStringifyBuffer)
const processorWithRehypeStringifyUint8Array = unified().use(
rehypeStringifyUint8Array
)

// To do: ?
expectType<Processor<HastRoot, HastRoot, HastRoot>>(
processorWithRehypeStringifyBuffer
processorWithRehypeStringifyUint8Array
)
// To do: yield `UnistNode`?
expectType<HastRoot>(processorWithRehypeStringifyBuffer.parse(''))
expectType<HastRoot>(processorWithRehypeStringifyUint8Array.parse(''))
// @ts-expect-error: to do: accept `UnistNode`?
processorWithRehypeStringifyBuffer.runSync(mdastRoot)
processorWithRehypeStringifyUint8Array.runSync(mdastRoot)
// To do: accept, yield `UnistNode`?
expectType<HastRoot>(processorWithRehypeStringifyBuffer.runSync(hastRoot))
expectType<Uint8Array>(processorWithRehypeStringifyBuffer.stringify(hastRoot))
expectType<HastRoot>(processorWithRehypeStringifyUint8Array.runSync(hastRoot))
expectType<Uint8Array>(
processorWithRehypeStringifyUint8Array.stringify(hastRoot)
)
// @ts-expect-error: not the correct node type.
processorWithRehypeStringifyBuffer.stringify(mdastRoot)
expectType<VFile>(processorWithRehypeStringifyBuffer.processSync(''))
processorWithRehypeStringifyUint8Array.stringify(mdastRoot)
expectType<VFile>(processorWithRehypeStringifyUint8Array.processSync(''))

// Compile plugin (to a non-node).
const rehypeReact: Plugin<[], HastRoot, ReactNode> = function () {
Expand Down
20 changes: 18 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import structuredClone from '@ungap/structured-clone'
import {bail} from 'bail'
import isBuffer from 'is-buffer'
import isPlainObj from 'is-plain-obj'
import {trough} from 'trough'
import {VFile} from 'vfile'
Expand Down Expand Up @@ -601,5 +600,22 @@ function looksLikeAVFile(value) {
* @returns {value is VFileValue}
*/
function looksLikeAVFileValue(value) {
return typeof value === 'string' || isBuffer(value)
return typeof value === 'string' || isUint8Array(value)
}

/**
* Assert `value` is an `Uint8Array`.
*
* @param {unknown} value
* thing.
* @returns {value is Uint8Array}
* Whether `value` is an `Uint8Array`.
*/
function isUint8Array(value) {
return Boolean(
value &&
typeof value === 'object' &&
'byteLength' in value &&
'byteOffset' in value
)
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@types/unist": "^3.0.0",
"@ungap/structured-clone": "^1.0.0",
"bail": "^2.0.0",
"is-buffer": "^2.0.0",
"is-plain-obj": "^4.0.0",
"trough": "^2.0.0",
"vfile": "^6.0.0"
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,10 @@ Compile a syntax tree.

###### Returns

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

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

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

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

> 👉 **Note**: unified typically compiles by serializing: most
> [compilers][compiler] return `string` (or `Buffer`).
> [compilers][compiler] return `string` (or `Uint8Array`).
> Some compilers, such as the one configured with
> [`rehype-react`][rehype-react], result in other values (in this case, a React
> tree).
Expand Down
5 changes: 2 additions & 3 deletions test/process-compilers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {Buffer} from 'node:buffer'
import assert from 'node:assert/strict'
import test from 'node:test'
import {unified} from 'unified'
Expand All @@ -20,9 +19,9 @@ test('process (compilers)', async function (t) {
assert.equal(file.result, undefined)
})

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

processor.Parser = SimpleParser
processor.Compiler = function () {
Expand Down

0 comments on commit fb49556

Please sign in to comment.