diff --git a/.github/dependabot.yml b/.github/dependabot.yml index f468993..167aca9 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -7,6 +7,8 @@ updates: commit-message: prefix: 'chore' include: 'scope' + cooldown: + default-days: 5 - package-ecosystem: 'npm' directory: '/' schedule: @@ -14,3 +16,5 @@ updates: commit-message: prefix: 'chore' include: 'scope' + cooldown: + default-days: 5 diff --git a/.github/workflows/test-and-release.yml b/.github/workflows/test-and-release.yml index 489b1c4..b895f1c 100644 --- a/.github/workflows/test-and-release.yml +++ b/.github/workflows/test-and-release.yml @@ -1,11 +1,12 @@ name: Test & Maybe Release on: [push, pull_request] + jobs: test: strategy: fail-fast: false matrix: - node: [18.x, 20.x, lts/*, current] + node: [lts/*, current] os: [macos-latest, ubuntu-latest, windows-latest] runs-on: ${{ matrix.os }} steps: @@ -16,17 +17,26 @@ jobs: with: node-version: ${{ matrix.node }} - name: Install Dependencies + run: npm install --no-progress + - name: Check build is up to date run: | - npm install --no-progress + npm run build + git diff --exit-code || (echo "::error::Build artifacts not committed. Run 'npm run build' and commit the changes." && exit 1) - name: Run tests run: | npm config set script-shell bash npm run test:ci + release: name: Release needs: test runs-on: ubuntu-latest if: github.event_name == 'push' && github.ref == 'refs/heads/master' + permissions: + contents: write + issues: write + pull-requests: write + id-token: write steps: - name: Checkout uses: actions/checkout@v6 @@ -36,26 +46,13 @@ jobs: uses: actions/setup-node@v6.3.0 with: node-version: lts/* + registry-url: 'https://registry.npmjs.org' - name: Install dependencies - run: | - npm install --no-progress --no-package-lock --no-save + run: npm install --no-progress --no-package-lock --no-save - name: Build - run: | - npm run build - - name: Install plugins - run: | - npm install \ - @semantic-release/commit-analyzer \ - conventional-changelog-conventionalcommits \ - @semantic-release/release-notes-generator \ - @semantic-release/npm \ - @semantic-release/github \ - @semantic-release/git \ - @semantic-release/changelog \ - --no-progress --no-package-lock --no-save + run: npm run build - name: Release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + NPM_CONFIG_PROVENANCE: true run: npx semantic-release - diff --git a/.gitignore b/.gitignore index 40b878d..3275623 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -node_modules/ \ No newline at end of file +node_modules +package-lock.jsontypes/tsconfig.tsbuildinfo +package-lock.json diff --git a/BufferList.d.ts b/BufferList.d.ts deleted file mode 100644 index 7487f12..0000000 --- a/BufferList.d.ts +++ /dev/null @@ -1,431 +0,0 @@ -export type BufferListAcceptedTypes = - | Buffer - | BufferList - | Uint8Array - | BufferListAcceptedTypes[] - | string - | number; - -export interface BufferListConstructor { - new (initData?: BufferListAcceptedTypes): BufferList; - (initData?: BufferListAcceptedTypes): BufferList; - - /** - * Determines if the passed object is a BufferList. It will return true - * if the passed object is an instance of BufferList or BufferListStream - * and false otherwise. - * - * N.B. this won't return true for BufferList or BufferListStream instances - * created by versions of this library before this static method was added. - * - * @param other - */ - - isBufferList(other: unknown): boolean; -} - -interface BufferList { - prototype: Object - - /** - * Get the length of the list in bytes. This is the sum of the lengths - * of all of the buffers contained in the list, minus any initial offset - * for a semi-consumed buffer at the beginning. Should accurately - * represent the total number of bytes that can be read from the list. - */ - - length: number; - - /** - * Adds an additional buffer or BufferList to the internal list. - * this is returned so it can be chained. - * - * @param buffer - */ - - append(buffer: BufferListAcceptedTypes): this; - - /** - * Adds an additional buffer or BufferList at the beginning of the internal list. - * this is returned so it can be chained. - * - * @param buffer - */ - prepend(buffer: BufferListAcceptedTypes): this; - - /** - * Will return the byte at the specified index. - * @param index - */ - - get(index: number): number; - - /** - * Returns a new Buffer object containing the bytes within the - * range specified. Both start and end are optional and will - * default to the beginning and end of the list respectively. - * - * If the requested range spans a single internal buffer then a - * slice of that buffer will be returned which shares the original - * memory range of that Buffer. If the range spans multiple buffers - * then copy operations will likely occur to give you a uniform Buffer. - * - * @param start - * @param end - */ - - slice(start?: number, end?: number): Buffer; - - /** - * Returns a new BufferList object containing the bytes within the - * range specified. Both start and end are optional and will default - * to the beginning and end of the list respectively. - * - * No copies will be performed. All buffers in the result share - * memory with the original list. - * - * @param start - * @param end - */ - - shallowSlice(start?: number, end?: number): this; - - /** - * Copies the content of the list in the `dest` buffer, starting from - * `destStart` and containing the bytes within the range specified - * with `srcStart` to `srcEnd`. - * - * `destStart`, `start` and `end` are optional and will default to the - * beginning of the dest buffer, and the beginning and end of the - * list respectively. - * - * @param dest - * @param destStart - * @param srcStart - * @param srcEnd - */ - - copy( - dest: Buffer, - destStart?: number, - srcStart?: number, - srcEnd?: number - ): Buffer; - - /** - * Performs a shallow-copy of the list. The internal Buffers remains the - * same, so if you change the underlying Buffers, the change will be - * reflected in both the original and the duplicate. - * - * This method is needed if you want to call consume() or pipe() and - * still keep the original list. - * - * @example - * - * ```js - * var bl = new BufferListStream(); - * bl.append('hello'); - * bl.append(' world'); - * bl.append('\n'); - * bl.duplicate().pipe(process.stdout, { end: false }); - * - * console.log(bl.toString()) - * ``` - */ - - duplicate(): this; - - /** - * Will shift bytes off the start of the list. The number of bytes - * consumed don't need to line up with the sizes of the internal - * Buffers—initial offsets will be calculated accordingly in order - * to give you a consistent view of the data. - * - * @param bytes - */ - - consume(bytes?: number): void; - - /** - * Will return a string representation of the buffer. The optional - * `start` and `end` arguments are passed on to `slice()`, while - * the encoding is passed on to `toString()` of the resulting Buffer. - * - * See the [`Buffer#toString()`](http://nodejs.org/docs/latest/api/buffer.html#buffer_buf_tostring_encoding_start_end) - * documentation for more information. - * - * @param encoding - * @param start - * @param end - */ - - toString(encoding?: string, start?: number, end?: number): string; - - /** - * Will return the byte at the specified index. indexOf() method - * returns the first index at which a given element can be found - * in the BufferList, or -1 if it is not present. - * - * @param value - * @param byteOffset - * @param encoding - */ - - indexOf( - value: string | number | Uint8Array | BufferList | Buffer, - byteOffset?: number, - encoding?: string - ): number; - - /** - * Will return the internal list of buffers. - */ - getBuffers(): Buffer[]; - - /** - * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. - * - * @param offset - */ - - readDoubleBE: Buffer['readDoubleBE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. - * - * @param offset - */ - - readDoubleLE: Buffer['readDoubleLE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. - * - * @param offset - */ - - readFloatBE: Buffer['readFloatBE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. - * - * @param offset - */ - - readFloatLE: Buffer['readFloatLE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. - * - * @param offset - */ - - readBigInt64BE: Buffer['readBigInt64BE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. - * - * @param offset - */ - - readBigInt64LE: Buffer['readBigInt64LE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. - * - * @param offset - */ - - readBigUInt64BE: Buffer['readBigUInt64BE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. - * - * @param offset - */ - - readBigUInt64LE: Buffer['readBigUInt64LE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. - * - * @param offset - */ - - readInt32BE: Buffer['readInt32BE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. - * - * @param offset - */ - - readInt32LE: Buffer['readInt32LE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. - * - * @param offset - */ - - readUInt32BE: Buffer['readUInt32BE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. - * - * @param offset - */ - - readUInt32LE: Buffer['readUInt32LE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently. - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work. - * - * @param offset - */ - - readInt16BE: Buffer['readInt16BE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are - * implemented and will operate across internal Buffer boundaries transparently. - * - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) - * documentation for how these work. - * - * @param offset - */ - - readInt16LE: Buffer['readInt16LE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are - * implemented and will operate across internal Buffer boundaries transparently. - * - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) - * documentation for how these work. - * - * @param offset - */ - - readUInt16BE: Buffer['readUInt16BE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are - * implemented and will operate across internal Buffer boundaries transparently. - * - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) - * documentation for how these work. - * - * @param offset - */ - - readUInt16LE: Buffer['readUInt16LE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are - * implemented and will operate across internal Buffer boundaries transparently. - * - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) - * documentation for how these work. - * - * @param offset - */ - - readInt8: Buffer['readInt8']; - - /** - * All of the standard byte-reading methods of the Buffer interface are - * implemented and will operate across internal Buffer boundaries transparently. - * - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) - * documentation for how these work. - * - * @param offset - */ - - readUInt8: Buffer['readUInt8']; - - /** - * All of the standard byte-reading methods of the Buffer interface are - * implemented and will operate across internal Buffer boundaries transparently. - * - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) - * documentation for how these work. - * - * @param offset - */ - - readIntBE: Buffer['readIntBE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are - * implemented and will operate across internal Buffer boundaries transparently. - * - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) - * documentation for how these work. - * - * @param offset - */ - - readIntLE: Buffer['readIntLE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are - * implemented and will operate across internal Buffer boundaries transparently. - * - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) - * documentation for how these work. - * - * @param offset - */ - - readUIntBE: Buffer['readUIntBE']; - - /** - * All of the standard byte-reading methods of the Buffer interface are - * implemented and will operate across internal Buffer boundaries transparently. - * - * See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) - * documentation for how these work. - * - * @param offset - */ - - readUIntLE: Buffer['readUIntLE']; -} - -/** - * No arguments are required for the constructor, but you can initialise - * the list by passing in a single Buffer object or an array of Buffer - * objects. - * - * `new` is not strictly required, if you don't instantiate a new object, - * it will be done automatically for you so you can create a new instance - * simply with: - * - * ```js - * const { BufferList } = require('bl') - * const bl = BufferList() - * - * // equivalent to: - * - * const { BufferList } = require('bl') - * const bl = new BufferList() - * ``` - */ - -declare const BufferList: BufferListConstructor; diff --git a/BufferList.js b/BufferList.js index 536ec79..ce02557 100644 --- a/BufferList.js +++ b/BufferList.js @@ -1,421 +1,503 @@ -'use strict' +import { Buffer } from 'node:buffer' + +/** + * @typedef {Buffer | BufferList | Uint8Array | Array | string | number} BufferListAcceptedTypes + */ -const { Buffer } = require('buffer') const symbol = Symbol.for('BufferList') -function BufferList (buf) { - if (!(this instanceof BufferList)) { - return new BufferList(buf) +export class BufferList { + /** @param {BufferListAcceptedTypes} [buf] */ + constructor (buf) { + /** @type {Buffer[]} */ + this._bufs = [] + /** @type {number} */ + this.length = 0 + Object.defineProperty(this, symbol, { value: true }) + if (buf) { + this.append(buf) + } } - BufferList._init.call(this, buf) -} - -BufferList._init = function _init (buf) { - Object.defineProperty(this, symbol, { value: true }) - - this._bufs = [] - this.length = 0 - - if (buf) { - this.append(buf) + /** @param {BufferListAcceptedTypes} [buf] */ + _new (buf) { + return new BufferList(buf) } -} -BufferList.prototype._new = function _new (buf) { - return new BufferList(buf) -} - -BufferList.prototype._offset = function _offset (offset) { - if (offset === 0) { - return [0, 0] - } + /** + * @param {number} offset + * @returns {[number, number]} + */ + _offset (offset) { + if (offset === 0 || this._bufs.length === 0) { + return [0, 0] + } - let tot = 0 + let tot = 0 - for (let i = 0; i < this._bufs.length; i++) { - const _t = tot + this._bufs[i].length - if (offset < _t || i === this._bufs.length - 1) { - return [i, offset - tot] + for (let i = 0; i < this._bufs.length; i++) { + const _t = tot + this._bufs[i].length + if (offset < _t || i === this._bufs.length - 1) { + return [i, offset - tot] + } + tot = _t } - tot = _t - } -} - -BufferList.prototype._reverseOffset = function (blOffset) { - const bufferId = blOffset[0] - let offset = blOffset[1] - for (let i = 0; i < bufferId; i++) { - offset += this._bufs[i].length + return [0, 0] } - return offset -} + /** + * @param {[number, number]} blOffset + * @returns {number} + */ + _reverseOffset (blOffset) { + const bufferId = blOffset[0] + let offset = blOffset[1] -BufferList.prototype.getBuffers = function getBuffers () { - return this._bufs -} + for (let i = 0; i < bufferId; i++) { + offset += this._bufs[i].length + } -BufferList.prototype.get = function get (index) { - if (index > this.length || index < 0) { - return undefined + return offset } - const offset = this._offset(index) - - return this._bufs[offset[0]][offset[1]] -} - -BufferList.prototype.slice = function slice (start, end) { - if (typeof start === 'number' && start < 0) { - start += this.length + /** @returns {Buffer[]} */ + getBuffers () { + return this._bufs } - if (typeof end === 'number' && end < 0) { - end += this.length - } + /** + * @param {number} index + * @returns {number | undefined} + */ + get (index) { + if (index > this.length || index < 0) { + return undefined + } - return this.copy(null, 0, start, end) -} + const offset = this._offset(index) -BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) { - if (typeof srcStart !== 'number' || srcStart < 0) { - srcStart = 0 + return this._bufs[offset[0]][offset[1]] } - if (typeof srcEnd !== 'number' || srcEnd > this.length) { - srcEnd = this.length - } + /** + * @param {number} [start] + * @param {number} [end] + * @returns {Buffer} + */ + slice (start, end) { + if (typeof start === 'number' && start < 0) { + start += this.length + } - if (srcStart >= this.length) { - return dst || Buffer.alloc(0) - } + if (typeof end === 'number' && end < 0) { + end += this.length + } - if (srcEnd <= 0) { - return dst || Buffer.alloc(0) + return this.copy(null, 0, start, end) } - const copy = !!dst - const off = this._offset(srcStart) - const len = srcEnd - srcStart - let bytes = len - let bufoff = (copy && dstStart) || 0 - let start = off[1] - - // copy/slice everything - if (srcStart === 0 && srcEnd === this.length) { - if (!copy) { - // slice, but full concat if multiple buffers - return this._bufs.length === 1 - ? this._bufs[0] - : Buffer.concat(this._bufs, this.length) + /** + * @param {Buffer | null} dst + * @param {number} [dstStart] + * @param {number} [srcStart] + * @param {number} [srcEnd] + * @returns {Buffer} + */ + copy (dst, dstStart, srcStart, srcEnd) { + if (typeof srcStart !== 'number' || srcStart < 0) { + srcStart = 0 } - // copy, need to copy individual buffers - for (let i = 0; i < this._bufs.length; i++) { - this._bufs[i].copy(dst, bufoff) - bufoff += this._bufs[i].length + if (typeof srcEnd !== 'number' || srcEnd > this.length) { + srcEnd = this.length } - return dst - } + if (srcStart >= this.length) { + return dst || Buffer.alloc(0) + } - // easy, cheap case where it's a subset of one of the buffers - if (bytes <= this._bufs[off[0]].length - start) { - return copy - ? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes) - : this._bufs[off[0]].slice(start, start + bytes) - } + if (srcEnd <= 0) { + return dst || Buffer.alloc(0) + } - if (!copy) { - // a slice, we need something to copy in to - dst = Buffer.allocUnsafe(len) - } + const copy = !!dst + const off = this._offset(srcStart) + const len = srcEnd - srcStart + let bytes = len + let bufoff = (copy && dstStart) || 0 + let start = off[1] + + // copy/slice everything + if (srcStart === 0 && srcEnd === this.length) { + if (!copy) { + // slice, but full concat if multiple buffers + return this._bufs.length === 1 + ? this._bufs[0] + : Buffer.concat(/** @type {any} */ (this._bufs), this.length) + } - for (let i = off[0]; i < this._bufs.length; i++) { - const l = this._bufs[i].length - start + // copy, need to copy individual buffers + const d = /** @type {any} */ (dst) + for (let i = 0; i < this._bufs.length; i++) { + this._bufs[i].copy(d, bufoff) + bufoff += this._bufs[i].length + } - if (bytes > l) { - this._bufs[i].copy(dst, bufoff, start) - bufoff += l - } else { - this._bufs[i].copy(dst, bufoff, start, start + bytes) - bufoff += l - break + return d } - bytes -= l + // easy, cheap case where it's a subset of one of the buffers + if (bytes <= this._bufs[off[0]].length - start) { + if (!copy) { + return this._bufs[off[0]].slice(start, start + bytes) + } + this._bufs[off[0]].copy(/** @type {any} */ (dst), dstStart, start, start + bytes) + return /** @type {Buffer} */ (dst) + } - if (start) { - start = 0 + if (!copy) { + // a slice, we need something to copy in to + dst = Buffer.allocUnsafe(len) } - } - // safeguard so that we don't return uninitialized memory - if (dst.length > bufoff) return dst.slice(0, bufoff) + const target = /** @type {any} */ (dst) - return dst -} + for (let i = off[0]; i < this._bufs.length; i++) { + const l = this._bufs[i].length - start -BufferList.prototype.shallowSlice = function shallowSlice (start, end) { - start = start || 0 - end = typeof end !== 'number' ? this.length : end + if (bytes > l) { + this._bufs[i].copy(target, bufoff, start) + bufoff += l + } else { + this._bufs[i].copy(target, bufoff, start, start + bytes) + bufoff += l + break + } - if (start < 0) { - start += this.length - } + bytes -= l - if (end < 0) { - end += this.length - } + if (start) { + start = 0 + } + } + + // safeguard so that we don't return uninitialized memory + if (target.length > bufoff) return target.slice(0, bufoff) - if (start === end) { - return this._new() + return target } - const startOffset = this._offset(start) - const endOffset = this._offset(end) - const buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1) + /** + * @param {number} [start] + * @param {number} [end] + * @returns {BufferList} + */ + shallowSlice (start, end) { + start = start || 0 + end = typeof end !== 'number' ? this.length : end - if (endOffset[1] === 0) { - buffers.pop() - } else { - buffers[buffers.length - 1] = buffers[buffers.length - 1].slice(0, endOffset[1]) - } + if (start < 0) { + start += this.length + } - if (startOffset[1] !== 0) { - buffers[0] = buffers[0].slice(startOffset[1]) - } + if (end < 0) { + end += this.length + } - return this._new(buffers) -} + if (start === end) { + return this._new() + } -BufferList.prototype.toString = function toString (encoding, start, end) { - return this.slice(start, end).toString(encoding) -} + const startOffset = this._offset(start) + const endOffset = this._offset(end) + const buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1) -BufferList.prototype.consume = function consume (bytes) { - // first, normalize the argument, in accordance with how Buffer does it - bytes = Math.trunc(bytes) - // do nothing if not a positive number - if (Number.isNaN(bytes) || bytes <= 0) return this - - while (this._bufs.length) { - if (bytes >= this._bufs[0].length) { - bytes -= this._bufs[0].length - this.length -= this._bufs[0].length - this._bufs.shift() + if (endOffset[1] === 0) { + buffers.pop() } else { - this._bufs[0] = this._bufs[0].slice(bytes) - this.length -= bytes - break + buffers[buffers.length - 1] = buffers[buffers.length - 1].slice(0, endOffset[1]) } - } - return this -} + if (startOffset[1] !== 0) { + buffers[0] = buffers[0].slice(startOffset[1]) + } -BufferList.prototype.duplicate = function duplicate () { - const copy = this._new() + return this._new(buffers) + } + + /** + * @param {BufferEncoding} [encoding] + * @param {number} [start] + * @param {number} [end] + * @returns {string} + */ + toString (encoding, start, end) { + return this.slice(start, end).toString(encoding) + } + + /** + * @param {number} bytes + * @returns {this} + */ + consume (bytes) { + // first, normalize the argument, in accordance with how Buffer does it + bytes = Math.trunc(bytes) + // do nothing if not a positive number + if (Number.isNaN(bytes) || bytes <= 0) return this + + while (this._bufs.length) { + if (bytes >= this._bufs[0].length) { + bytes -= this._bufs[0].length + this.length -= this._bufs[0].length + this._bufs.shift() + } else { + this._bufs[0] = this._bufs[0].slice(bytes) + this.length -= bytes + break + } + } - for (let i = 0; i < this._bufs.length; i++) { - copy.append(this._bufs[i]) + return this } - return copy -} + /** @returns {BufferList} */ + duplicate () { + const copy = this._new() -BufferList.prototype.append = function append (buf) { - return this._attach(buf, BufferList.prototype._appendBuffer) -} + for (let i = 0; i < this._bufs.length; i++) { + copy.append(this._bufs[i]) + } -BufferList.prototype.prepend = function prepend (buf) { - return this._attach(buf, BufferList.prototype._prependBuffer, true) -} + return copy + } -BufferList.prototype._attach = function _attach (buf, attacher, prepend) { - if (buf == null) { - return this + /** + * @param {BufferListAcceptedTypes} buf + * @returns {this} + */ + append (buf) { + return this._attach(buf, BufferList.prototype._appendBuffer) } - if (buf.buffer) { - // append/prepend a view of the underlying ArrayBuffer - attacher.call(this, Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)) - } else if (Array.isArray(buf)) { - const [starting, modifier] = prepend ? [buf.length - 1, -1] : [0, 1] + /** + * @param {BufferListAcceptedTypes} buf + * @returns {this} + */ + prepend (buf) { + return this._attach(buf, BufferList.prototype._prependBuffer, true) + } - for (let i = starting; i >= 0 && i < buf.length; i += modifier) { - this._attach(buf[i], attacher, prepend) + /** + * @param {BufferListAcceptedTypes} buf + * @param {(this: BufferList, buf: Buffer) => void} attacher + * @param {boolean} [prepend] + * @returns {this} + */ + _attach (buf, attacher, prepend) { + if (buf == null) { + return this } - } else if (this._isBufferList(buf)) { - // unwrap argument into individual BufferLists - const [starting, modifier] = prepend ? [buf._bufs.length - 1, -1] : [0, 1] - for (let i = starting; i >= 0 && i < buf._bufs.length; i += modifier) { - this._attach(buf._bufs[i], attacher, prepend) - } - } else { - // coerce number arguments to strings, since Buffer(number) does - // uninitialized memory allocation - if (typeof buf === 'number') { - buf = buf.toString() - } + if (typeof buf === 'object' && 'buffer' in buf) { + // append/prepend a view of the underlying ArrayBuffer + const view = /** @type {Uint8Array} */ (buf) + attacher.call(this, Buffer.from(view.buffer, view.byteOffset, view.byteLength)) + } else if (Array.isArray(buf)) { + const [starting, modifier] = prepend ? [buf.length - 1, -1] : [0, 1] - attacher.call(this, Buffer.from(buf)) - } + for (let i = starting; i >= 0 && i < buf.length; i += modifier) { + this._attach(buf[i], attacher, prepend) + } + } else if (this._isBufferList(buf)) { + // unwrap argument into individual BufferLists + const [starting, modifier] = prepend ? [buf._bufs.length - 1, -1] : [0, 1] - return this -} + for (let i = starting; i >= 0 && i < buf._bufs.length; i += modifier) { + this._attach(buf._bufs[i], attacher, prepend) + } + } else { + // coerce number arguments to strings, since Buffer(number) does + // uninitialized memory allocation + if (typeof buf === 'number') { + buf = buf.toString() + } -BufferList.prototype._appendBuffer = function appendBuffer (buf) { - this._bufs.push(buf) - this.length += buf.length -} + attacher.call(this, Buffer.from(/** @type {string} */ (buf))) + } -BufferList.prototype._prependBuffer = function prependBuffer (buf) { - this._bufs.unshift(buf) - this.length += buf.length -} + return this + } -BufferList.prototype.indexOf = function (search, offset, encoding) { - if (encoding === undefined && typeof offset === 'string') { - encoding = offset - offset = undefined + /** @param {Buffer} buf */ + _appendBuffer (buf) { + this._bufs.push(buf) + this.length += buf.length } - if (typeof search === 'function' || Array.isArray(search)) { - throw new TypeError('The "value" argument must be one of type string, Buffer, BufferList, or Uint8Array.') - } else if (typeof search === 'number') { - search = Buffer.from([search]) - } else if (typeof search === 'string') { - search = Buffer.from(search, encoding) - } else if (this._isBufferList(search)) { - search = search.slice() - } else if (Array.isArray(search.buffer)) { - search = Buffer.from(search.buffer, search.byteOffset, search.byteLength) - } else if (!Buffer.isBuffer(search)) { - search = Buffer.from(search) + /** @param {Buffer} buf */ + _prependBuffer (buf) { + this._bufs.unshift(buf) + this.length += buf.length } - offset = Number(offset || 0) + /** + * @param {string | number | Buffer | BufferList | Uint8Array} search + * @param {number | string} [offset] + * @param {BufferEncoding} [encoding] + * @returns {number} + */ + indexOf (search, offset, encoding) { + if (encoding === undefined && typeof offset === 'string') { + encoding = /** @type {BufferEncoding} */ (offset) + offset = undefined + } - if (isNaN(offset)) { - offset = 0 - } + if (typeof search === 'function' || Array.isArray(search)) { + throw new TypeError('The "value" argument must be one of type string, Buffer, BufferList, or Uint8Array.') + } else if (typeof search === 'number') { + search = Buffer.from([search]) + } else if (typeof search === 'string') { + search = Buffer.from(search, encoding) + } else if (this._isBufferList(search)) { + search = search.slice() + } else if (Array.isArray(/** @type {any} */ (search).buffer)) { + search = Buffer.from(/** @type {any} */ (search).buffer, /** @type {any} */ (search).byteOffset, /** @type {any} */ (search).byteLength) + } else if (!Buffer.isBuffer(search)) { + search = Buffer.from(/** @type {any} */ (search)) + } - if (offset < 0) { - offset = this.length + offset - } + offset = Number(offset || 0) - if (offset < 0) { - offset = 0 - } + if (isNaN(offset)) { + offset = 0 + } - if (search.length === 0) { - return offset > this.length ? this.length : offset - } + if (offset < 0) { + offset = this.length + offset + } - const blOffset = this._offset(offset) - let blIndex = blOffset[0] // index of which internal buffer we're working on - let buffOffset = blOffset[1] // offset of the internal buffer we're working on + if (offset < 0) { + offset = 0 + } - // scan over each buffer - for (; blIndex < this._bufs.length; blIndex++) { - const buff = this._bufs[blIndex] + if (search.length === 0) { + return offset > this.length ? this.length : offset + } - while (buffOffset < buff.length) { - const availableWindow = buff.length - buffOffset + const blOffset = this._offset(offset) + let blIndex = blOffset[0] // index of which internal buffer we're working on + let buffOffset = blOffset[1] // offset of the internal buffer we're working on - if (availableWindow >= search.length) { - const nativeSearchResult = buff.indexOf(search, buffOffset) + // scan over each buffer + for (; blIndex < this._bufs.length; blIndex++) { + const buff = this._bufs[blIndex] - if (nativeSearchResult !== -1) { - return this._reverseOffset([blIndex, nativeSearchResult]) - } + while (buffOffset < buff.length) { + const availableWindow = buff.length - buffOffset - buffOffset = buff.length - search.length + 1 // end of native search window - } else { - const revOffset = this._reverseOffset([blIndex, buffOffset]) + if (availableWindow >= search.length) { + const nativeSearchResult = buff.indexOf(/** @type {any} */ (search), buffOffset) - if (this._match(revOffset, search)) { - return revOffset - } + if (nativeSearchResult !== -1) { + return this._reverseOffset([blIndex, nativeSearchResult]) + } - buffOffset++ - } - } + buffOffset = buff.length - search.length + 1 // end of native search window + } else { + const revOffset = this._reverseOffset([blIndex, buffOffset]) - buffOffset = 0 - } + if (this._match(revOffset, search)) { + return revOffset + } - return -1 -} + buffOffset++ + } + } -BufferList.prototype._match = function (offset, search) { - if (this.length - offset < search.length) { - return false + buffOffset = 0 + } + + return -1 } - for (let searchOffset = 0; searchOffset < search.length; searchOffset++) { - if (this.get(offset + searchOffset) !== search[searchOffset]) { + /** + * @param {number} offset + * @param {Buffer} search + * @returns {boolean} + */ + _match (offset, search) { + if (this.length - offset < search.length) { return false } + + for (let searchOffset = 0; searchOffset < search.length; searchOffset++) { + if (this.get(offset + searchOffset) !== search[searchOffset]) { + return false + } + } + return true } - return true -} -;(function () { - const methods = { - readDoubleBE: 8, - readDoubleLE: 8, - readFloatBE: 4, - readFloatLE: 4, - readBigInt64BE: 8, - readBigInt64LE: 8, - readBigUInt64BE: 8, - readBigUInt64LE: 8, - readInt32BE: 4, - readInt32LE: 4, - readUInt32BE: 4, - readUInt32LE: 4, - readInt16BE: 2, - readInt16LE: 2, - readUInt16BE: 2, - readUInt16LE: 2, - readInt8: 1, - readUInt8: 1, - readIntBE: null, - readIntLE: null, - readUIntBE: null, - readUIntLE: null + // Used internally by the class and also as an indicator of this object being + // a `BufferList`. It's not possible to use `instanceof BufferList` in a browser + // environment because there could be multiple different copies of the + // BufferList class and some `BufferList`s might be `BufferList`s. + /** + * @param {any} b + * @returns {b is BufferList} + */ + _isBufferList (b) { + return b instanceof BufferList || BufferList.isBufferList(b) } - for (const m in methods) { - (function (m) { - if (methods[m] === null) { - BufferList.prototype[m] = function (offset, byteLength) { - return this.slice(offset, offset + byteLength)[m](0, byteLength) - } - } else { - BufferList.prototype[m] = function (offset = 0) { - return this.slice(offset, offset + methods[m])[m](0) - } - } - }(m)) + /** + * @param {any} b + * @returns {boolean} + */ + static isBufferList (b) { + return b != null && b[symbol] } -}()) - -// Used internally by the class and also as an indicator of this object being -// a `BufferList`. It's not possible to use `instanceof BufferList` in a browser -// environment because there could be multiple different copies of the -// BufferList class and some `BufferList`s might be `BufferList`s. -BufferList.prototype._isBufferList = function _isBufferList (b) { - return b instanceof BufferList || BufferList.isBufferList(b) } -BufferList.isBufferList = function isBufferList (b) { - return b != null && b[symbol] +// Add all the Buffer read methods +/** @type {Record} */ +const methods = { + readDoubleBE: 8, + readDoubleLE: 8, + readFloatBE: 4, + readFloatLE: 4, + readBigInt64BE: 8, + readBigInt64LE: 8, + readBigUInt64BE: 8, + readBigUInt64LE: 8, + readInt32BE: 4, + readInt32LE: 4, + readUInt32BE: 4, + readUInt32LE: 4, + readInt16BE: 2, + readInt16LE: 2, + readUInt16BE: 2, + readUInt16LE: 2, + readInt8: 1, + readUInt8: 1, + readIntBE: null, + readIntLE: null, + readUIntBE: null, + readUIntLE: null +} + +for (const m in methods) { + const size = methods[m] + if (size === null) { + /** @type {any} */ (BufferList.prototype)[m] = function (/** @type {number} */ offset, /** @type {number} */ byteLength) { + return /** @type {any} */ (this.slice(offset, offset + byteLength))[m](0, byteLength) + } + } else { + /** @type {any} */ (BufferList.prototype)[m] = function (/** @type {number} */ offset = 0) { + return /** @type {any} */ (this.slice(offset, offset + size))[m](0) + } + } } -module.exports = BufferList +export default BufferList diff --git a/BufferListStream.js b/BufferListStream.js new file mode 100644 index 0000000..ff02203 --- /dev/null +++ b/BufferListStream.js @@ -0,0 +1,133 @@ +import { Duplex } from 'node:stream' +import { BufferList } from './BufferList.js' + +const symbol = Symbol.for('BufferList') + +/** + * @typedef {import('./BufferList.js').BufferListAcceptedTypes} BufferListAcceptedTypes + */ + +export class BufferListStream extends Duplex { + /** + * @param {((err: Error | null, buffer?: Buffer) => void) | BufferListAcceptedTypes} [callback] + */ + constructor (callback) { + super() + + if (typeof callback === 'function') { + this._callback = callback + + /** @param {Error} err */ + const piper = (err) => { + if (this._callback) { + this._callback(err) + this._callback = null + } + } + + this.on('pipe', (src) => { + src.on('error', piper) + }) + this.on('unpipe', (src) => { + src.removeListener('error', piper) + }) + + callback = undefined + } + + Object.defineProperty(this, symbol, { value: true }) + /** @type {Buffer[]} */ + this._bufs = [] + /** @type {number} */ + this.length = 0 + + if (callback) { + /** @type {any} */ (this).append(callback) + } + } + + /** + * @param {any} buf + * @param {string} encoding + * @param {Function} callback + */ + _write (buf, encoding, callback) { + /** @type {any} */ (this)._appendBuffer(buf) + if (typeof callback === 'function') { + callback() + } + } + + /** @param {number} size */ + _read (size) { + if (!this.length) { + return this.push(null) + } + + const self = /** @type {any} */ (this) + size = Math.min(size, this.length) + this.push(self.slice(0, size)) + self.consume(size) + } + + /** + * @param {any} [chunk] + * @param {any} [encoding] + * @param {any} [cb] + * @returns {this} + */ + end (chunk, encoding, cb) { + Duplex.prototype.end.call(this, chunk, encoding, cb) + + if (this._callback) { + this._callback(null, /** @type {any} */ (this).slice()) + this._callback = null + } + + return this + } + + /** + * @param {Error | null} err + * @param {Function} cb + */ + _destroy (err, cb) { + this._bufs.length = 0 + this.length = 0 + cb(err) + } + + /** @param {any} [callback] */ + _new (callback) { + return new BufferListStream(callback) + } + + /** + * @param {any} b + * @returns {b is BufferList} + */ + _isBufferList (b) { + return b instanceof BufferListStream || b instanceof BufferList || BufferListStream.isBufferList(b) + } + + /** + * @param {any} b + * @returns {boolean} + */ + static isBufferList (b) { + return BufferList.isBufferList(b) + } +} + +// Copy BufferList prototype methods to BufferListStream prototype +for (const key of Object.getOwnPropertyNames(BufferList.prototype)) { + if (key === 'constructor' || key === '_new' || key === '_isBufferList') continue + const descriptor = Object.getOwnPropertyDescriptor(BufferList.prototype, key) + if (descriptor) { + Object.defineProperty(BufferListStream.prototype, key, descriptor) + } +} + +export default BufferListStream +export { BufferList } +export const isBufferList = BufferList.isBufferList diff --git a/README.md b/README.md index 55e85aa..d49e43c 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,23 @@ # bl *(BufferList)* -![Build Status](https://img.shields.io/github/actions/workflow/status/rvagg/bl/test-and-release.yml?branch=master) +[![NPM](https://nodei.co/npm/bl.svg?style=flat&data=n,v&color=blue)](https://nodei.co/npm/bl/) **A Node.js Buffer list collector, reader and streamer thingy.** -[![NPM](https://nodei.co/npm/bl.svg)](https://nodei.co/npm/bl/) - **bl** is a storage object for collections of Node Buffers, exposing them with the main Buffer readable API. Also works as a duplex stream so you can collect buffers from a stream that emits them and emit buffers to a stream that consumes them! The original buffers are kept intact and copies are only done as necessary. Any reads that require the use of a single original buffer will return a slice of that buffer only (which references the same memory as the original buffer). Reads that span buffers perform concatenation as required and return the results transparently. +## Installation + +```bash +npm install bl +``` + +## Basic Usage + ```js -const { BufferList } = require('bl') +import { BufferList } from 'bl' const bl = new BufferList() bl.append(Buffer.from('abcd')) @@ -41,47 +47,35 @@ console.log(bl.readUInt16BE(10)) // 0x0304 console.log(bl.readUInt16LE(10)) // 0x0403 ``` -Give it a callback in the constructor and use it just like **[concat-stream](https://github.com/maxogden/node-concat-stream)**: - -```js -const { BufferListStream } = require('bl') -const fs = require('fs') - -fs.createReadStream('README.md') - .pipe(BufferListStream((err, data) => { // note 'new' isn't strictly required - // `data` is a complete Buffer object containing the full data - console.log(data.toString()) - })) -``` - -Note that when you use the *callback* method like this, the resulting `data` parameter is a concatenation of all `Buffer` objects in the list. If you want to avoid the overhead of this concatenation (in cases of extreme performance consciousness), then avoid the *callback* method and just listen to `'end'` instead, like a standard Stream. +### Node.js Streams -Or to fetch a URL using [hyperquest](https://github.com/substack/hyperquest) (should work with [request](http://github.com/mikeal/request) and even plain Node http too!): +Collect the contents of a stream: ```js -const hyperquest = require('hyperquest') -const { BufferListStream } = require('bl') +import { BufferListStream } from 'bl' +import { pipeline } from 'node:stream/promises' +import { Readable } from 'node:stream' -const url = 'https://raw.github.com/rvagg/bl/master/README.md' - -hyperquest(url).pipe(BufferListStream((err, data) => { - console.log(data.toString()) -})) +const response = await fetch('https://raw.githubusercontent.com/rvagg/bl/master/README.md') +const bl = new BufferListStream() +await pipeline(Readable.fromWeb(response.body), bl) +console.log(bl.toString()) ``` -Or, use it as a readable stream to recompose a list of Buffers to an output source: +Or use it as a readable stream to pipe to an output: ```js -const { BufferListStream } = require('bl') -const fs = require('fs') +import BufferListStream from 'bl' +import { pipeline } from 'node:stream/promises' +import fs from 'node:fs' -var bl = new BufferListStream() +const bl = new BufferListStream() bl.append(Buffer.from('abcd')) bl.append(Buffer.from('efg')) bl.append(Buffer.from('hi')) bl.append(Buffer.from('j')) -bl.pipe(fs.createWriteStream('gibberish.txt')) +await pipeline(bl, fs.createWriteStream('gibberish.txt')) ``` ## API @@ -90,7 +84,7 @@ bl.pipe(fs.createWriteStream('gibberish.txt')) * BufferList.isBufferList(obj) * bl.length * bl.append(buffer) - * bl.append(buffer) + * bl.prepend(buffer) * bl.get(index) * bl.indexOf(value[, byteOffset][, encoding]) * bl.slice([ start[, end ] ]) @@ -102,21 +96,14 @@ bl.pipe(fs.createWriteStream('gibberish.txt')) * bl.readDoubleBE(), bl.readDoubleLE(), bl.readFloatBE(), bl.readFloatLE(), bl.readBigInt64BE(), bl.readBigInt64LE(), bl.readBigUInt64BE(), bl.readBigUInt64LE(), bl.readInt32BE(), bl.readInt32LE(), bl.readUInt32BE(), bl.readUInt32LE(), bl.readInt16BE(), bl.readInt16LE(), bl.readUInt16BE(), bl.readUInt16LE(), bl.readInt8(), bl.readUInt8() * new BufferListStream([ callback ]) * bl.getBuffers() - + -------------------------------------------------------- ### new BufferList([ Buffer | Buffer array | BufferList | BufferList array | String ]) No arguments are _required_ for the constructor, but you can initialise the list by passing in a single `Buffer` object or an array of `Buffer` objects. -`new` is not strictly required, if you don't instantiate a new object, it will be done automatically for you so you can create a new instance simply with: - ```js -const { BufferList } = require('bl') -const bl = BufferList() - -// equivalent to: - -const { BufferList } = require('bl') +import { BufferList } from 'bl' const bl = new BufferList() ``` @@ -150,7 +137,6 @@ Get the length of the list in bytes. This is the sum of the lengths of all of th -------------------------------------------------------- ### bl.indexOf(value[, byteOffset][, encoding]) -`get()` will return the byte at the specified index. `indexOf()` method returns the first index at which a given element can be found in the BufferList, or -1 if it is not present. -------------------------------------------------------- @@ -175,10 +161,12 @@ No copies will be performed. All buffers in the result share memory with the ori -------------------------------------------------------- ### bl.duplicate() -`duplicate()` performs a **shallow-copy** of the list. The internal Buffers remains the same, so if you change the underlying Buffers, the change will be reflected in both the original and the duplicate. This method is needed if you want to call `consume()` or `pipe()` and still keep the original list.Example: +`duplicate()` performs a **shallow-copy** of the list. The internal Buffers remains the same, so if you change the underlying Buffers, the change will be reflected in both the original and the duplicate. This method is needed if you want to call `consume()` or `pipe()` and still keep the original list. Example: ```js -var bl = new BufferListStream() +import BufferListStream from 'bl' + +const bl = new BufferListStream() bl.append('hello') bl.append(' world') @@ -192,7 +180,7 @@ console.log(bl.toString()) -------------------------------------------------------- ### bl.consume(bytes) -`consume()` will shift bytes *off the start of the list*. The number of bytes consumed don't need to line up with the sizes of the internal Buffers—initial offsets will be calculated accordingly in order to give you a consistent view of the data. +`consume()` will shift bytes *off the start of the list*. The number of bytes consumed don't need to line up with the sizes of the internal Buffers - initial offsets will be calculated accordingly in order to give you a consistent view of the data. -------------------------------------------------------- @@ -214,26 +202,14 @@ See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html)< The constructor takes an optional callback, if supplied, the callback will be called with an error argument followed by a reference to the **bl** instance, when `bl.end()` is called (i.e. from a piped stream). This is a convenient method of collecting the entire contents of a stream, particularly when the stream is *chunky*, such as a network stream. -Normally, no arguments are required for the constructor, but you can initialise the list by passing in a single `Buffer` object or an array of `Buffer` object. - -`new` is not strictly required, if you don't instantiate a new object, it will be done automatically for you so you can create a new instance simply with: - -```js -const { BufferListStream } = require('bl') -const bl = BufferListStream() - -// equivalent to: - -const { BufferListStream } = require('bl') -const bl = new BufferListStream() -``` +Normally, no arguments are required for the constructor, but you can initialise the list by passing in a single `Buffer` object or an array of `Buffer` objects. -N.B. For backwards compatibility reasons, `BufferListStream` is the **default** export when you `require('bl')`: +N.B. For backwards compatibility reasons, `BufferListStream` is the **default** export when you `import from 'bl'`: ```js -const { BufferListStream } = require('bl') +import { BufferListStream } from 'bl' // equivalent to: -const BufferListStream = require('bl') +import BufferListStream from 'bl' ``` -------------------------------------------------------- @@ -243,6 +219,16 @@ const BufferListStream = require('bl') `getBuffers()` returns the internal list of buffers. +## Migrating from v6 + +v7 is a modernisation release: + + * **ESM-only**: CommonJS `require('bl')` is no longer supported. Use `import` instead. + * **`new` is required**: `BufferListStream()` and `BufferList()` can no longer be called without `new`. + * **Zero runtime dependencies**: `readable-stream`, `buffer`, and `inherits` have been removed. `BufferListStream` now uses Node's built-in `node:stream` Duplex directly. + * **Node.js >= 20**: Browser bundling is no longer a supported use case. If you need buffer accumulation in the browser, consider working with `Uint8Array` directly. + + ## Contributors **bl** is brought to you by the following hackers: @@ -252,8 +238,8 @@ const BufferListStream = require('bl') * [Jarett Cruger](https://github.com/jcrugzz) -## License & copyright +## License & copyright -Copyright (c) 2013-2019 bl contributors (listed above). +Copyright (c) 2013-2026 bl contributors (listed above). bl is licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details. diff --git a/bl.js b/bl.js deleted file mode 100644 index 40228f8..0000000 --- a/bl.js +++ /dev/null @@ -1,84 +0,0 @@ -'use strict' - -const DuplexStream = require('readable-stream').Duplex -const inherits = require('inherits') -const BufferList = require('./BufferList') - -function BufferListStream (callback) { - if (!(this instanceof BufferListStream)) { - return new BufferListStream(callback) - } - - if (typeof callback === 'function') { - this._callback = callback - - const piper = function piper (err) { - if (this._callback) { - this._callback(err) - this._callback = null - } - }.bind(this) - - this.on('pipe', function onPipe (src) { - src.on('error', piper) - }) - this.on('unpipe', function onUnpipe (src) { - src.removeListener('error', piper) - }) - - callback = null - } - - BufferList._init.call(this, callback) - DuplexStream.call(this) -} - -inherits(BufferListStream, DuplexStream) -Object.assign(BufferListStream.prototype, BufferList.prototype) - -BufferListStream.prototype._new = function _new (callback) { - return new BufferListStream(callback) -} - -BufferListStream.prototype._write = function _write (buf, encoding, callback) { - this._appendBuffer(buf) - - if (typeof callback === 'function') { - callback() - } -} - -BufferListStream.prototype._read = function _read (size) { - if (!this.length) { - return this.push(null) - } - - size = Math.min(size, this.length) - this.push(this.slice(0, size)) - this.consume(size) -} - -BufferListStream.prototype.end = function end (chunk) { - DuplexStream.prototype.end.call(this, chunk) - - if (this._callback) { - this._callback(null, this.slice()) - this._callback = null - } -} - -BufferListStream.prototype._destroy = function _destroy (err, cb) { - this._bufs.length = 0 - this.length = 0 - cb(err) -} - -BufferListStream.prototype._isBufferList = function _isBufferList (b) { - return b instanceof BufferListStream || b instanceof BufferList || BufferListStream.isBufferList(b) -} - -BufferListStream.isBufferList = BufferList.isBufferList - -module.exports = BufferListStream -module.exports.BufferListStream = BufferListStream -module.exports.BufferList = BufferList diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 07a8ee3..0000000 --- a/index.d.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { Duplex } from "readable-stream"; -import { - BufferList as BL, - BufferListConstructor, - BufferListAcceptedTypes, -} from "./BufferList"; - -type BufferListStreamInit = - | ((err: Error, buffer: Buffer) => void) - | BufferListAcceptedTypes; - -interface BufferListStreamConstructor { - new (initData?: BufferListStreamInit): BufferListStream; - (callback?: BufferListStreamInit): BufferListStream; - - /** - * Determines if the passed object is a BufferList. It will return true - * if the passed object is an instance of BufferList or BufferListStream - * and false otherwise. - * - * N.B. this won't return true for BufferList or BufferListStream instances - * created by versions of this library before this static method was added. - * - * @param other - */ - - isBufferList(other: unknown): boolean; - - /** - * Rexporting BufferList and BufferListStream to fix - * issue with require/commonjs import and "export = " below. - */ - - BufferList: BufferListConstructor; - BufferListStream: BufferListStreamConstructor; -} - -interface BufferListStream extends Duplex, BL { - prototype: BufferListStream & BL; -} - -/** - * BufferListStream is a Node Duplex Stream, so it can be read from - * and written to like a standard Node stream. You can also pipe() - * to and from a BufferListStream instance. - * - * The constructor takes an optional callback, if supplied, the - * callback will be called with an error argument followed by a - * reference to the bl instance, when bl.end() is called - * (i.e. from a piped stream). - * - * This is a convenient method of collecting the entire contents of - * a stream, particularly when the stream is chunky, such as a network - * stream. - * - * Normally, no arguments are required for the constructor, but you can - * initialise the list by passing in a single Buffer object or an array - * of Buffer object. - * - * `new` is not strictly required, if you don't instantiate a new object, - * it will be done automatically for you so you can create a new instance - * simply with: - * - * ```js - * const { BufferListStream } = require('bl'); - * const bl = BufferListStream(); - * - * // equivalent to: - * - * const { BufferListStream } = require('bl'); - * const bl = new BufferListStream(); - * ``` - * - * N.B. For backwards compatibility reasons, BufferListStream is the default - * export when you `require('bl')`: - * - * ```js - * const { BufferListStream } = require('bl') - * - * // equivalent to: - * - * const BufferListStream = require('bl') - * ``` - */ - -declare const BufferListStream: BufferListStreamConstructor; - -export = BufferListStream; diff --git a/package.json b/package.json index 1e745d6..e39ab72 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,26 @@ { "name": "bl", "version": "6.1.6", - "description": "Buffer List: collect buffers and access with a standard readable Buffer interface, streamable too!", + "description": "Buffer List: collect buffers and access with a standard readable Buffer interface, streamable too", "license": "MIT", - "main": "bl.js", + "type": "module", + "engines": { + "node": ">=20" + }, + "main": "./BufferListStream.js", + "exports": { + ".": { + "import": "./BufferListStream.js", + "types": "./types/index.d.ts" + } + }, "scripts": { - "lint": "standard *.js test/*.js", - "test": "npm run lint && npm run test:types && node test/test.js | faucet", - "test:ci": "npm run lint && node test/test.js && npm run test:types", - "test:types": "tsc --target esnext --moduleResolution node --allowJs --noEmit --skipLibCheck test/test.js", - "build": "true" + "lint": "standard BufferList.js BufferListStream.js test/*.js", + "test": "npm run lint && npm run build:types && npm run test:types && node test/test.js | faucet", + "test:ci": "npm run lint && node test/test.js && npm run build:types && npm run test:types", + "test:types": "tsc --noEmit --strict --module nodenext --moduleResolution nodenext --target esnext --skipLibCheck --types node test/types.ts --ignoreConfig && node test/verify-types.js", + "build:types": "tsc --build", + "build": "npm run build:types" }, "repository": { "type": "git", @@ -27,17 +38,20 @@ "stream", "awesomesauce" ], - "dependencies": { - "@types/readable-stream": "^4.0.0", - "buffer": "^6.0.3", - "inherits": "^2.0.4", - "readable-stream": "^4.2.0" - }, "devDependencies": { - "faucet": "~0.0.1", - "standard": "^17.0.0", - "tape": "^5.2.2", - "typescript": "~5.9.2" + "@types/node": "^25.0.0", + "@semantic-release/changelog": "^6.0.3", + "@semantic-release/commit-analyzer": "^13.0.1", + "@semantic-release/git": "^10.0.1", + "@semantic-release/github": "^12.0.6", + "@semantic-release/npm": "^13.1.5", + "@semantic-release/release-notes-generator": "^14.1.0", + "conventional-changelog-conventionalcommits": "^9.3.0", + "faucet": "^0.0.4", + "semantic-release": "^25.0.3", + "standard": "^17.1.2", + "tape": "^5.9.0", + "typescript": "^6.0.2" }, "release": { "branches": [ diff --git a/test/convert.js b/test/convert.js index 9f3e235..f9c57f0 100644 --- a/test/convert.js +++ b/test/convert.js @@ -1,8 +1,6 @@ -'use strict' - -const tape = require('tape') -const { BufferList, BufferListStream } = require('../') -const { Buffer } = require('buffer') +import tape from 'tape' +import { BufferList, BufferListStream } from '../BufferListStream.js' +import { Buffer } from 'node:buffer' tape('convert from BufferList to BufferListStream', (t) => { const data = Buffer.from(`TEST-${Date.now()}`) diff --git a/test/indexOf.js b/test/indexOf.js index 62dcb01..210d047 100644 --- a/test/indexOf.js +++ b/test/indexOf.js @@ -1,8 +1,6 @@ -'use strict' - -const tape = require('tape') -const BufferList = require('../') -const { Buffer } = require('buffer') +import tape from 'tape' +import BufferList from '../BufferListStream.js' +import { Buffer } from 'node:buffer' tape('indexOf single byte needle', (t) => { const bl = new BufferList(['abcdefg', 'abcdefg', '12345']) diff --git a/test/isBufferList.js b/test/isBufferList.js index 9d895d5..d5b1755 100644 --- a/test/isBufferList.js +++ b/test/isBufferList.js @@ -1,8 +1,6 @@ -'use strict' - -const tape = require('tape') -const { BufferList, BufferListStream } = require('../') -const { Buffer } = require('buffer') +import tape from 'tape' +import { BufferList, BufferListStream } from '../BufferListStream.js' +import { Buffer } from 'node:buffer' tape('isBufferList positives', (t) => { t.ok(BufferList.isBufferList(new BufferList())) diff --git a/test/test.js b/test/test.js index f10326f..3508ac3 100644 --- a/test/test.js +++ b/test/test.js @@ -1,13 +1,17 @@ // @ts-check -'use strict' - -const tape = require('tape') -const crypto = require('crypto') -const fs = require('fs') -const path = require('path') -const os = require('os') -const BufferListStream = require('../') -const { Buffer } = require('buffer') +import tape from 'tape' +import * as crypto from 'node:crypto' +import * as fs from 'node:fs' +import * as path from 'node:path' +import * as os from 'node:os' +import { fileURLToPath } from 'node:url' +import BufferListStream from '../BufferListStream.js' +import { Buffer } from 'node:buffer' +import './indexOf.js' +import './isBufferList.js' +import './convert.js' + +const __dirname = path.dirname(fileURLToPath(import.meta.url)) /** * This typedef allows us to add _bufs to the API without declaring it publicly on types. @@ -38,10 +42,6 @@ if (process.browser) { ) } -require('./indexOf') -require('./isBufferList') -require('./convert') - tape('single bytes from single buffer', function (t) { const bl = new BufferListStream() @@ -685,10 +685,10 @@ tape('uninitialized memory', function (t) { tape('instantiation with Buffer', function (t) { const buf = crypto.randomBytes(1024) const buf2 = crypto.randomBytes(1024) - let b = BufferListStream(buf) + let b = new BufferListStream(buf) t.equal(buf.toString('hex'), b.slice().toString('hex'), 'same buffer') - b = BufferListStream([buf, buf2]) + b = new BufferListStream([buf, buf2]) t.equal(b.slice().toString('hex'), Buffer.concat([buf, buf2]).toString('hex'), 'same buffer') t.end() @@ -729,7 +729,7 @@ tape('test Number appendage', function (t) { tape('write nothing, should get empty buffer', function (t) { t.plan(3) - BufferListStream(function (err, data) { + new BufferListStream(function (err, data) { t.notOk(err, 'no error') t.ok(Buffer.isBuffer(data), 'got a buffer') t.equal(0, data.length, 'got a zero-length buffer') @@ -743,7 +743,7 @@ tape('unicode string', function (t) { const inp1 = '\u2600' const inp2 = '\u2603' const exp = inp1 + ' and ' + inp2 - const bl = BufferListStream() + const bl = new BufferListStream() bl.write(inp1) bl.write(' and ') @@ -753,8 +753,8 @@ tape('unicode string', function (t) { }) tape('should emit finish', function (t) { - const source = BufferListStream() - const dest = BufferListStream() + const source = new BufferListStream() + const dest = new BufferListStream() source.write('hello') source.pipe(dest) @@ -768,7 +768,7 @@ tape('should emit finish', function (t) { tape('basic copy', function (t) { const buf = crypto.randomBytes(1024) const buf2 = Buffer.alloc(1024) - const b = BufferListStream(buf) + const b = new BufferListStream(buf) b.copy(buf2) t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer') @@ -779,7 +779,7 @@ tape('basic copy', function (t) { tape('copy after many appends', function (t) { const buf = crypto.randomBytes(512) const buf2 = Buffer.alloc(1024) - const b = BufferListStream(buf) + const b = new BufferListStream(buf) b.append(buf) b.copy(buf2) @@ -791,7 +791,7 @@ tape('copy after many appends', function (t) { tape('copy at a precise position', function (t) { const buf = crypto.randomBytes(1004) const buf2 = Buffer.alloc(1024) - const b = BufferListStream(buf) + const b = new BufferListStream(buf) b.copy(buf2, 20) t.equal(b.slice().toString('hex'), buf2.slice(20).toString('hex'), 'same buffer') @@ -802,7 +802,7 @@ tape('copy at a precise position', function (t) { tape('copy starting from a precise location', function (t) { const buf = crypto.randomBytes(10) const buf2 = Buffer.alloc(5) - const b = BufferListStream(buf) + const b = new BufferListStream(buf) b.copy(buf2, 0, 5) t.equal(b.slice(5).toString('hex'), buf2.toString('hex'), 'same buffer') @@ -812,7 +812,7 @@ tape('copy starting from a precise location', function (t) { tape('copy in an interval', function (t) { const rnd = crypto.randomBytes(10) - const b = BufferListStream(rnd) // put the random bytes there + const b = new BufferListStream(rnd) // put the random bytes there const actual = Buffer.alloc(3) const expected = Buffer.alloc(3) @@ -827,7 +827,7 @@ tape('copy in an interval', function (t) { tape('copy an interval between two buffers', function (t) { const buf = crypto.randomBytes(10) const buf2 = Buffer.alloc(10) - const b = BufferListStream(buf) + const b = new BufferListStream(buf) b.append(buf) b.copy(buf2, 0, 5, 15) @@ -913,7 +913,7 @@ tape('duplicate', function (t) { const bl = new BufferListStream('abcdefghij\xff\x00') const dup = bl.duplicate() - t.equal(bl.prototype, dup.prototype) + t.equal(Object.getPrototypeOf(bl), Object.getPrototypeOf(dup)) t.equal(bl.toString('hex'), dup.toString('hex')) }) @@ -1025,7 +1025,7 @@ tape('destroy with error', function (t) { !process.browser && tape('handle error', function (t) { t.plan(2) - fs.createReadStream('/does/not/exist').pipe(BufferListStream(function (err, data) { + fs.createReadStream('/does/not/exist').pipe(new BufferListStream(function (err, data) { t.ok(err instanceof Error, 'has error') t.notOk(data, 'no data') })) diff --git a/test/types.ts b/test/types.ts new file mode 100644 index 0000000..33fcf0c --- /dev/null +++ b/test/types.ts @@ -0,0 +1,89 @@ +// Type-level test: verifies augmented types are correct. +// Run with: tsc --noEmit test/types.ts +// If this file fails to compile, the type augmentations are out of sync. + +import { BufferListStream, BufferList } from '../types/index.js' + +// -- Conformance with Buffer read methods -- +// Assert that BufferList's read methods are callable with the same +// signatures as Buffer's. If @types/node changes a signature, this +// section will fail to compile. +type AssertConforms<_Bl extends _Buf, _Buf> = true + +type _FixedRead = AssertConforms< + { [K in + | 'readDoubleBE' | 'readDoubleLE' + | 'readFloatBE' | 'readFloatLE' + | 'readBigInt64BE' | 'readBigInt64LE' + | 'readBigUInt64BE' | 'readBigUInt64LE' + | 'readInt32BE' | 'readInt32LE' + | 'readUInt32BE' | 'readUInt32LE' + | 'readInt16BE' | 'readInt16LE' + | 'readUInt16BE' | 'readUInt16LE' + | 'readInt8' | 'readUInt8' + ]: BufferList[K] }, + { [K in + | 'readDoubleBE' | 'readDoubleLE' + | 'readFloatBE' | 'readFloatLE' + | 'readBigInt64BE' | 'readBigInt64LE' + | 'readBigUInt64BE' | 'readBigUInt64LE' + | 'readInt32BE' | 'readInt32LE' + | 'readUInt32BE' | 'readUInt32LE' + | 'readInt16BE' | 'readInt16LE' + | 'readUInt16BE' | 'readUInt16LE' + | 'readInt8' | 'readUInt8' + ]: Buffer[K] } +> + +type _VariableRead = AssertConforms< + { [K in 'readIntBE' | 'readIntLE' | 'readUIntBE' | 'readUIntLE']: BufferList[K] }, + { [K in 'readIntBE' | 'readIntLE' | 'readUIntBE' | 'readUIntLE']: Buffer[K] } +> + +// Verify return types match Buffer +declare const buf: Buffer +declare const bl: BufferList +const _br: ReturnType = bl.readUInt16BE(0) +const _bbr: ReturnType = bl.readBigInt64BE(0) +const _bfr: ReturnType = bl.readDoubleBE(0) + +// -- BufferList basic API -- +const bl2 = new BufferList() +const _a1: BufferList = bl2.append(Buffer.from('test')) +const _a2: BufferList = bl2.prepend(Buffer.from('test')) +const _g: number | undefined = bl2.get(0) +const _s: Buffer = bl2.slice(0, 2) +const _ss: BufferList = bl2.shallowSlice(0, 2) +const _c: Buffer = bl2.copy(Buffer.alloc(4), 0, 0, 4) +const _d: BufferList = bl2.duplicate() +const _co: BufferList = bl2.consume(2) +const _ts: string = bl2.toString('utf8', 0, 2) +const _io: number = bl2.indexOf('test') +const _io2: number = bl2.indexOf(Buffer.from('x'), 0, 'utf8') +const _gb: Buffer[] = bl2.getBuffers() +const _l: number = bl2.length +const _ib: boolean = BufferList.isBufferList(bl2) + +// -- BufferListStream has both Duplex and BufferList APIs -- +const bls = new BufferListStream() +const _sa: BufferListStream = bls.append(Buffer.from('test')) +const _sp: BufferListStream = bls.prepend(Buffer.from('test')) +const _sg: number | undefined = bls.get(0) +const _sslice: Buffer = bls.slice(0, 2) +const _sss: BufferListStream = bls.shallowSlice(0, 2) +const _sco: BufferListStream = bls.consume(2) +const _sd: BufferListStream = bls.duplicate() +const _sts: string = bls.toString('utf8', 0, 2) +const _sio: number = bls.indexOf('test') +const _sgb: Buffer[] = bls.getBuffers() +const _sru: number = bls.readUInt16BE(0) +const _srb: bigint = bls.readBigInt64BE(0) +const _srib: number = bls.readIntBE(0, 4) + +// Duplex stream methods +bls.pipe(process.stdout) +bls.write(Buffer.from('hello')) +bls.end() + +// isBufferList +const _sib: boolean = BufferListStream.isBufferList(bls) diff --git a/test/verify-types.js b/test/verify-types.js new file mode 100644 index 0000000..3976193 --- /dev/null +++ b/test/verify-types.js @@ -0,0 +1,51 @@ +import tape from 'tape' +import { BufferList, BufferListStream } from '../BufferListStream.js' + +// Methods declared in types/augment.d.ts for BufferList (read methods) +const readMethods = [ + 'readDoubleBE', 'readDoubleLE', + 'readFloatBE', 'readFloatLE', + 'readBigInt64BE', 'readBigInt64LE', + 'readBigUInt64BE', 'readBigUInt64LE', + 'readInt32BE', 'readInt32LE', + 'readUInt32BE', 'readUInt32LE', + 'readInt16BE', 'readInt16LE', + 'readUInt16BE', 'readUInt16LE', + 'readInt8', 'readUInt8', + 'readIntBE', 'readIntLE', + 'readUIntBE', 'readUIntLE' +] + +// Methods declared in types/augment.d.ts for BufferListStream (BufferList methods) +const bufferListMethods = [ + 'append', 'prepend', + 'get', 'indexOf', + 'slice', 'shallowSlice', + 'copy', 'duplicate', + 'consume', 'toString', + 'getBuffers' +] + +tape('BufferList has all augmented read methods', (t) => { + const bl = new BufferList() + for (const method of readMethods) { + t.equal(typeof bl[method], 'function', `BufferList.prototype.${method} exists`) + } + t.end() +}) + +tape('BufferListStream has all augmented read methods', (t) => { + const bls = new BufferListStream() + for (const method of readMethods) { + t.equal(typeof bls[method], 'function', `BufferListStream.prototype.${method} exists`) + } + t.end() +}) + +tape('BufferListStream has all augmented BufferList methods', (t) => { + const bls = new BufferListStream() + for (const method of bufferListMethods) { + t.equal(typeof bls[method], 'function', `BufferListStream.prototype.${method} exists`) + } + t.end() +}) diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..e6a878c --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "allowJs": true, + "checkJs": true, + "strict": true, + "esModuleInterop": true, + "target": "ESNext", + "module": "nodenext", + "moduleResolution": "nodenext", + "skipLibCheck": true, + "types": ["node"], + "declaration": true, + "declarationMap": true, + "emitDeclarationOnly": true, + "outDir": "types" + }, + "include": [ + "BufferList.js", + "BufferListStream.js" + ], + "exclude": [ + "node_modules" + ] +} diff --git a/types/BufferList.d.ts b/types/BufferList.d.ts new file mode 100644 index 0000000..6515638 --- /dev/null +++ b/types/BufferList.d.ts @@ -0,0 +1,109 @@ +export class BufferList { + /** + * @param {any} b + * @returns {boolean} + */ + static isBufferList(b: any): boolean; + /** @param {BufferListAcceptedTypes} [buf] */ + constructor(buf?: BufferListAcceptedTypes); + /** @type {Buffer[]} */ + _bufs: Buffer[]; + /** @type {number} */ + length: number; + /** @param {BufferListAcceptedTypes} [buf] */ + _new(buf?: BufferListAcceptedTypes): BufferList; + /** + * @param {number} offset + * @returns {[number, number]} + */ + _offset(offset: number): [number, number]; + /** + * @param {[number, number]} blOffset + * @returns {number} + */ + _reverseOffset(blOffset: [number, number]): number; + /** @returns {Buffer[]} */ + getBuffers(): Buffer[]; + /** + * @param {number} index + * @returns {number | undefined} + */ + get(index: number): number | undefined; + /** + * @param {number} [start] + * @param {number} [end] + * @returns {Buffer} + */ + slice(start?: number, end?: number): Buffer; + /** + * @param {Buffer | null} dst + * @param {number} [dstStart] + * @param {number} [srcStart] + * @param {number} [srcEnd] + * @returns {Buffer} + */ + copy(dst: Buffer | null, dstStart?: number, srcStart?: number, srcEnd?: number): Buffer; + /** + * @param {number} [start] + * @param {number} [end] + * @returns {BufferList} + */ + shallowSlice(start?: number, end?: number): BufferList; + /** + * @param {BufferEncoding} [encoding] + * @param {number} [start] + * @param {number} [end] + * @returns {string} + */ + toString(encoding?: BufferEncoding, start?: number, end?: number): string; + /** + * @param {number} bytes + * @returns {this} + */ + consume(bytes: number): this; + /** @returns {BufferList} */ + duplicate(): BufferList; + /** + * @param {BufferListAcceptedTypes} buf + * @returns {this} + */ + append(buf: BufferListAcceptedTypes): this; + /** + * @param {BufferListAcceptedTypes} buf + * @returns {this} + */ + prepend(buf: BufferListAcceptedTypes): this; + /** + * @param {BufferListAcceptedTypes} buf + * @param {(this: BufferList, buf: Buffer) => void} attacher + * @param {boolean} [prepend] + * @returns {this} + */ + _attach(buf: BufferListAcceptedTypes, attacher: (this: BufferList, buf: Buffer) => void, prepend?: boolean): this; + /** @param {Buffer} buf */ + _appendBuffer(buf: Buffer): void; + /** @param {Buffer} buf */ + _prependBuffer(buf: Buffer): void; + /** + * @param {string | number | Buffer | BufferList | Uint8Array} search + * @param {number | string} [offset] + * @param {BufferEncoding} [encoding] + * @returns {number} + */ + indexOf(search: string | number | Buffer | BufferList | Uint8Array, offset?: number | string, encoding?: BufferEncoding): number; + /** + * @param {number} offset + * @param {Buffer} search + * @returns {boolean} + */ + _match(offset: number, search: Buffer): boolean; + /** + * @param {any} b + * @returns {b is BufferList} + */ + _isBufferList(b: any): b is BufferList; +} +export default BufferList; +export type BufferListAcceptedTypes = Buffer | BufferList | Uint8Array | Array | string | number; +import { Buffer } from 'node:buffer'; +//# sourceMappingURL=BufferList.d.ts.map \ No newline at end of file diff --git a/types/BufferList.d.ts.map b/types/BufferList.d.ts.map new file mode 100644 index 0000000..afb78e4 --- /dev/null +++ b/types/BufferList.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"BufferList.d.ts","sourceRoot":"","sources":["../BufferList.js"],"names":[],"mappings":"AAQA;IA6bE;;;OAGG;IACH,uBAHW,GAAG,GACD,OAAO,CAInB;IAlcD,6CAA6C;IAC7C,kBADY,uBAAuB,EAUlC;IARC,uBAAuB;IACvB,OADW,MAAM,EAAE,CACJ;IACf,qBAAqB;IACrB,QADW,MAAM,CACF;IAOjB,6CAA6C;IAC7C,WADY,uBAAuB,cAGlC;IAED;;;OAGG;IACH,gBAHW,MAAM,GACJ,CAAC,MAAM,EAAE,MAAM,CAAC,CAkB5B;IAED;;;OAGG;IACH,yBAHW,CAAC,MAAM,EAAE,MAAM,CAAC,GACd,MAAM,CAWlB;IAED,0BAA0B;IAC1B,cADc,MAAM,EAAE,CAGrB;IAED;;;OAGG;IACH,WAHW,MAAM,GACJ,MAAM,GAAG,SAAS,CAU9B;IAED;;;;OAIG;IACH,cAJW,MAAM,QACN,MAAM,GACJ,MAAM,CAYlB;IAED;;;;;;OAMG;IACH,UANW,MAAM,GAAG,IAAI,aACb,MAAM,aACN,MAAM,WACN,MAAM,GACJ,MAAM,CAoFlB;IAED;;;;OAIG;IACH,qBAJW,MAAM,QACN,MAAM,GACJ,UAAU,CAiCtB;IAED;;;;;OAKG;IACH,oBALW,cAAc,UACd,MAAM,QACN,MAAM,GACJ,MAAM,CAIlB;IAED;;;OAGG;IACH,eAHW,MAAM,GACJ,IAAI,CAqBhB;IAED,4BAA4B;IAC5B,aADc,UAAU,CASvB;IAED;;;OAGG;IACH,YAHW,uBAAuB,GACrB,IAAI,CAIhB;IAED;;;OAGG;IACH,aAHW,uBAAuB,GACrB,IAAI,CAIhB;IAED;;;;;OAKG;IACH,aALW,uBAAuB,YACvB,CAAC,IAAI,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,YACvC,OAAO,GACL,IAAI,CAmChB;IAED,0BAA0B;IAC1B,mBADY,MAAM,QAIjB;IAED,0BAA0B;IAC1B,oBADY,MAAM,QAIjB;IAED;;;;;OAKG;IACH,gBALW,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU,GAAG,UAAU,WAClD,MAAM,GAAG,MAAM,aACf,cAAc,GACZ,MAAM,CA0ElB;IAED;;;;OAIG;IACH,eAJW,MAAM,UACN,MAAM,GACJ,OAAO,CAanB;IAMD;;;OAGG;IACH,iBAHW,GAAG,GACD,CAAC,IAAI,UAAU,CAI3B;CASF;;sCAzcY,MAAM,GAAG,UAAU,GAAG,UAAU,GAAG,KAAK,CAAC,MAAM,GAAG,UAAU,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC,GAAG,MAAM,GAAG,MAAM;uBAHpG,aAAa"} \ No newline at end of file diff --git a/types/BufferListStream.d.ts b/types/BufferListStream.d.ts new file mode 100644 index 0000000..53e45a5 --- /dev/null +++ b/types/BufferListStream.d.ts @@ -0,0 +1,53 @@ +/** + * @typedef {import('./BufferList.js').BufferListAcceptedTypes} BufferListAcceptedTypes + */ +export class BufferListStream extends Duplex { + /** + * @param {any} b + * @returns {boolean} + */ + static isBufferList(b: any): boolean; + /** + * @param {((err: Error | null, buffer?: Buffer) => void) | BufferListAcceptedTypes} [callback] + */ + constructor(callback?: ((err: Error | null, buffer?: Buffer) => void) | BufferListAcceptedTypes); + _callback: any; + /** @type {Buffer[]} */ + _bufs: Buffer[]; + /** @type {number} */ + length: number; + /** + * @param {any} buf + * @param {string} encoding + * @param {Function} callback + */ + _write(buf: any, encoding: string, callback: Function): void; + /** @param {number} size */ + _read(size: number): boolean | undefined; + /** + * @param {any} [chunk] + * @param {any} [encoding] + * @param {any} [cb] + * @returns {this} + */ + end(chunk?: any, encoding?: any, cb?: any): this; + /** + * @param {Error | null} err + * @param {Function} cb + */ + _destroy(err: Error | null, cb: Function): void; + /** @param {any} [callback] */ + _new(callback?: any): BufferListStream; + /** + * @param {any} b + * @returns {b is BufferList} + */ + _isBufferList(b: any): b is BufferList; +} +export default BufferListStream; +export { BufferList }; +export const isBufferList: any; +export type BufferListAcceptedTypes = any; +import { Duplex } from 'node:stream'; +import { BufferList } from './BufferList.js'; +//# sourceMappingURL=BufferListStream.d.ts.map \ No newline at end of file diff --git a/types/BufferListStream.d.ts.map b/types/BufferListStream.d.ts.map new file mode 100644 index 0000000..4dd2a12 --- /dev/null +++ b/types/BufferListStream.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"BufferListStream.d.ts","sourceRoot":"","sources":["../BufferListStream.js"],"names":[],"mappings":"AAKA;;GAEG;AAEH;IAuGE;;;OAGG;IACH,uBAHW,GAAG,GACD,OAAO,CAInB;IA5GD;;OAEG;IACH,uBAFW,CAAC,CAAC,GAAG,EAAE,KAAK,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC,GAAG,uBAAuB,EAmClF;IA7BG,eAAyB;IAqB3B,uBAAuB;IACvB,OADW,MAAM,EAAE,CACJ;IACf,qBAAqB;IACrB,QADW,MAAM,CACF;IAOjB;;;;OAIG;IACH,YAJW,GAAG,YACH,MAAM,4BAQhB;IAED,2BAA2B;IAC3B,YADY,MAAM,uBAUjB;IAED;;;;;OAKG;IACH,YALW,GAAG,aACH,GAAG,OACH,GAAG,GACD,IAAI,CAWhB;IAED;;;OAGG;IACH,cAHW,KAAK,GAAG,IAAI,sBAOtB;IAED,8BAA8B;IAC9B,gBADY,GAAG,oBAGd;IAED;;;OAGG;IACH,iBAHW,GAAG,GACD,CAAC,IAAI,UAAU,CAI3B;CASF;;;AAaD,+BAAmD;;uBApI5B,aAAa;2BACT,iBAAiB"} \ No newline at end of file diff --git a/types/augment.d.ts b/types/augment.d.ts new file mode 100644 index 0000000..57d4326 --- /dev/null +++ b/types/augment.d.ts @@ -0,0 +1,50 @@ +import { BufferList, BufferListAcceptedTypes } from './BufferList.js' + +// Buffer read methods dynamically assigned to BufferList.prototype +interface BufferListReadMethods { + readDoubleBE(offset?: number): number + readDoubleLE(offset?: number): number + readFloatBE(offset?: number): number + readFloatLE(offset?: number): number + readBigInt64BE(offset?: number): bigint + readBigInt64LE(offset?: number): bigint + readBigUInt64BE(offset?: number): bigint + readBigUInt64LE(offset?: number): bigint + readInt32BE(offset?: number): number + readInt32LE(offset?: number): number + readUInt32BE(offset?: number): number + readUInt32LE(offset?: number): number + readInt16BE(offset?: number): number + readInt16LE(offset?: number): number + readUInt16BE(offset?: number): number + readUInt16LE(offset?: number): number + readInt8(offset?: number): number + readUInt8(offset?: number): number + readIntBE(offset: number, byteLength: number): number + readIntLE(offset: number, byteLength: number): number + readUIntBE(offset: number, byteLength: number): number + readUIntLE(offset: number, byteLength: number): number +} + +// BufferList methods copied to BufferListStream.prototype at runtime +interface BufferListMethods { + append(buf: BufferListAcceptedTypes): this + prepend(buf: BufferListAcceptedTypes): this + get(index: number): number | undefined + indexOf(search: string | number | Buffer | BufferList | Uint8Array, offset?: number | string, encoding?: BufferEncoding): number + slice(start?: number, end?: number): Buffer + shallowSlice(start?: number, end?: number): this + copy(dst: Buffer | null, dstStart?: number, srcStart?: number, srcEnd?: number): Buffer + duplicate(): this + consume(bytes: number): this + toString(encoding?: BufferEncoding, start?: number, end?: number): string + getBuffers(): Buffer[] +} + +declare module './BufferList.js' { + interface BufferList extends BufferListReadMethods {} +} + +declare module './BufferListStream.js' { + interface BufferListStream extends BufferListMethods, BufferListReadMethods {} +} diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..b5cf0e8 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,4 @@ +/// +export { BufferList, BufferListAcceptedTypes } from './BufferList.js' +export { BufferListStream, isBufferList } from './BufferListStream.js' +export { default } from './BufferListStream.js' diff --git a/types/tsconfig.tsbuildinfo b/types/tsconfig.tsbuildinfo new file mode 100644 index 0000000..cf1094a --- /dev/null +++ b/types/tsconfig.tsbuildinfo @@ -0,0 +1 @@ +{"root":["../BufferList.js","../BufferListStream.js"],"version":"6.0.2"} \ No newline at end of file