diff --git a/index.d.ts b/index.d.ts index 6f881a87..4b0f61cc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 @@ -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. @@ -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. */ @@ -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`. * @@ -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`. * @@ -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`. * @@ -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 @@ -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[], @@ -780,8 +780,8 @@ export class CompilerClass { * 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 } @@ -811,7 +811,7 @@ export class CompilerClass { * @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 = ( diff --git a/index.test-d.ts b/index.test-d.ts index 9d71acb0..b51f9fb1 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -524,27 +524,32 @@ expectType(processorWithRehypeStringify.stringify(hastRoot)) processorWithRehypeStringify.stringify(mdastRoot) expectType(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>( - processorWithRehypeStringifyBuffer + processorWithRehypeStringifyUint8Array ) // To do: yield `UnistNode`? -expectType(processorWithRehypeStringifyBuffer.parse('')) +expectType(processorWithRehypeStringifyUint8Array.parse('')) // @ts-expect-error: to do: accept `UnistNode`? -processorWithRehypeStringifyBuffer.runSync(mdastRoot) +processorWithRehypeStringifyUint8Array.runSync(mdastRoot) // To do: accept, yield `UnistNode`? -expectType(processorWithRehypeStringifyBuffer.runSync(hastRoot)) -expectType(processorWithRehypeStringifyBuffer.stringify(hastRoot)) +expectType(processorWithRehypeStringifyUint8Array.runSync(hastRoot)) +expectType( + processorWithRehypeStringifyUint8Array.stringify(hastRoot) +) // @ts-expect-error: not the correct node type. -processorWithRehypeStringifyBuffer.stringify(mdastRoot) -expectType(processorWithRehypeStringifyBuffer.processSync('')) +processorWithRehypeStringifyUint8Array.stringify(mdastRoot) +expectType(processorWithRehypeStringifyUint8Array.processSync('')) // Compile plugin (to a non-node). const rehypeReact: Plugin<[], HastRoot, ReactNode> = function () { diff --git a/lib/index.js b/lib/index.js index f02abd1a..3516154c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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' @@ -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 + ) } diff --git a/package.json b/package.json index 3135cd1e..b448ae3d 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/readme.md b/readme.md index c3c8e53c..003a4ad4 100644 --- a/readme.md +++ b/readme.md @@ -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). @@ -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). @@ -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). @@ -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). diff --git a/test/process-compilers.js b/test/process-compilers.js index 46285de5..c71a4093 100644 --- a/test/process-compilers.js +++ b/test/process-compilers.js @@ -1,4 +1,3 @@ -import {Buffer} from 'node:buffer' import assert from 'node:assert/strict' import test from 'node:test' import {unified} from 'unified' @@ -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 () {