Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Buffer more compliant with official implementation #285

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ export class Buffer extends Uint8Array {
write(string: string, offset?: number, length?: number, encoding?: string): number;
toString(encoding?: string, start?: number, end?: number): string;
toJSON(): { type: 'Buffer', data: any[] };
equals(otherBuffer: Buffer): boolean;
equals(otherBuffer: Uint8Array): boolean;
compare(otherBuffer: Uint8Array, targetStart?: number, targetEnd?: number, sourceStart?: number, sourceEnd?: number): number;
copy(targetBuffer: Buffer, targetStart?: number, sourceStart?: number, sourceEnd?: number): number;
copy(targetBuffer: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number): number;
slice(start?: number, end?: number): Buffer;
writeUIntLE(value: number, offset: number, byteLength: number, noAssert?: boolean): number;
writeUIntBE(value: number, offset: number, byteLength: number, noAssert?: boolean): number;
Expand Down Expand Up @@ -56,9 +56,9 @@ export class Buffer extends Uint8Array {
writeDoubleLE(value: number, offset: number, noAssert?: boolean): number;
writeDoubleBE(value: number, offset: number, noAssert?: boolean): number;
fill(value: any, offset?: number, end?: number): this;
indexOf(value: string | number | Buffer, byteOffset?: number, encoding?: string): number;
lastIndexOf(value: string | number | Buffer, byteOffset?: number, encoding?: string): number;
includes(value: string | number | Buffer, byteOffset?: number, encoding?: string): boolean;
indexOf(value: string | number | Uint8Array, byteOffset?: number, encoding?: string): number;
lastIndexOf(value: string | number | Uint8Array, byteOffset?: number, encoding?: string): number;
includes(value: string | number | Uint8Array, byteOffset?: number, encoding?: string): boolean;

/**
* Allocates a new buffer containing the given {str}.
Expand Down Expand Up @@ -122,7 +122,7 @@ export class Buffer extends Uint8Array {
*
* @param buffer
*/
static from(buffer: Buffer | Uint8Array): Buffer;
static from(buffer: Uint8Array): Buffer;
/**
* Creates a new Buffer containing the given JavaScript string {str}.
* If provided, the {encoding} parameter identifies the character encoding.
Expand Down Expand Up @@ -176,7 +176,7 @@ export class Buffer extends Uint8Array {
* If parameter is omitted, buffer will be filled with zeros.
* @param encoding encoding used for call to buf.fill while initializing
*/
static alloc(size: number, fill?: string | Buffer | number, encoding?: string): Buffer;
static alloc(size: number, fill?: string | Uint8Array | number, encoding?: string): Buffer;
/**
* Allocates a new buffer of {size} octets, leaving memory not initialized, so the contents
* of the newly created Buffer are unknown and may contain sensitive data.
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ Buffer.prototype.toString = function toString () {
Buffer.prototype.toLocaleString = Buffer.prototype.toString

Buffer.prototype.equals = function equals (b) {
if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
if (!Buffer.isBuffer(b) && !(b instanceof Uint8Array)) throw new TypeError('Argument must be a Buffer')
if (this === b) return true
return Buffer.compare(this, b) === 0
}
Expand Down Expand Up @@ -1688,7 +1688,7 @@ Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert

// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
Buffer.prototype.copy = function copy (target, targetStart, start, end) {
if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer')
if (!Buffer.isBuffer(target) && !(target instanceof Uint8Array)) throw new TypeError('argument should be a Buffer')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s probably nicer to use Object.prototype.toString.call(target) === '[object Uint8Array]' here instead of instanceof, because instanceof still won’t work across multiple realms (e.g. iframes)

if (!start) start = 0
if (!end && end !== 0) end = this.length
if (targetStart >= target.length) targetStart = target.length
Expand Down
18 changes: 18 additions & 0 deletions test/equals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const B = require('../').Buffer
const test = require('tape')
const { randomBytes } = require('crypto')

test('buffer.equals', function (t) {
const a = B.from(randomBytes(32))
const b = B.from(randomBytes(64))
const c = B.from(randomBytes(128))
const d = B.from(randomBytes(256))
const e = B.from(randomBytes(512))

t.assert(a.equals(new Uint8Array(a.buffer)))
t.assert(b.equals(new Uint8Array(b.buffer)))
t.assert(c.equals(new Uint8Array(c.buffer)))
t.assert(d.equals(new Uint8Array(d.buffer)))
t.assert(e.equals(new Uint8Array(e.buffer)))
t.end()
})
17 changes: 17 additions & 0 deletions test/methods.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const B = require('../').Buffer
const test = require('tape')
const { randomBytes } = require('crypto')

test('buffer.toJSON', function (t) {
const data = [1, 2, 3, 4]
Expand All @@ -26,6 +27,12 @@ test('buffer.copy', function (t) {
buf2.toString('ascii', 0, 25),
'!!!!!!!!qrst!!!!!!!!!!!!!'
)

const buf3 = Buffer.from([1, 2, 3])
const buf4 = new Uint8Array(3)
buf3.copy(buf4)
t.ok(buf3.equals(buf4))

t.end()
})

Expand Down Expand Up @@ -138,3 +145,13 @@ test('buffer.slice out of range', function (t) {
t.equal((new B('hallo')).slice(10, 2).toString(), '')
t.end()
})

test('buffer.includes', function (t) {
const bytes1 = new Uint8Array([54, 12, 21])
t.ok(B.from(bytes1), bytes1)

const size = 512
const bytes2 = randomBytes(size)
t.ok(B.alloc((size * 2) - 1, bytes2), bytes2)
t.end()
})
12 changes: 12 additions & 0 deletions test/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@ test('Buffer.isBuffer', function (t) {
t.equal(B.isBuffer('hey'), false)
t.end()
})

test('Buffer.alloc', function (t) {
t.notStrictEqual(
B.alloc(10, new Uint8Array([1, 2, 3])),
Buffer.from(new Uint8Array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1]))
)
t.notStrictEqual(
B.alloc(1),
Buffer.from(new Uint8Array([0]))
)
t.end()
})