Skip to content

Commit

Permalink
Add static buffersEqual and streamsEqual methods
Browse files Browse the repository at this point in the history
  • Loading branch information
extremeheat committed May 5, 2024
1 parent 040788a commit a3b860c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ declare module "bytewriter" {
readBuffer(length: number): Buffer

// Varints

// Write a signed varint
writeVarInt(value: number): void

Expand Down Expand Up @@ -120,6 +120,9 @@ declare module "bytewriter" {
peek(): number

getBuffer(): Buffer

static buffersEqual(a: ArrayBuffer, b: ArrayBuffer): boolean
static streamsEqual(a: ByteStream, b: ByteStream): boolean
}
export = ByteStream
}
12 changes: 12 additions & 0 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,18 @@ class ByteStream {
getBuffer () {
return this.buffer.subarray(0, this.writeOffset)
}

static buffersEqual (a, b) {
if (a.length !== b.length) return false
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false
}
return true
}

static streamsEqual (a, b) {
return ByteStream.buffersEqual(a.getBuffer(), b.getBuffer())
}
}

module.exports = ByteStream
12 changes: 12 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,18 @@ class ByteStream {
getBuffer () {
return this.buffer.slice(0, this.writeOffset)
}

static buffersEqual (a, b) {
if (a.length !== b.length) return false
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false
}
return true
}

static streamsEqual (a, b) {
return ByteStream.buffersEqual(a.getBuffer(), b.getBuffer())
}
}

module.exports = ByteStream
5 changes: 4 additions & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('basic tests', () => {
stream.readUInt8(0x01)
stream.readUInt16BE(0x0203)
assert.strictEqual(stream.getBuffer().toString('hex'), '878c94208f9cb4e0b0c1c28408a6c8888102b6e8c881e3858b95283f9df3b63ff3c0ca4283de1b68656c6c6f20776f726c64210028292a2b2c')
assert(ByteWriter.streamsEqual(stream, stream))
})
})

Expand All @@ -27,6 +28,7 @@ describe('basic tests - node', () => {
const stream = new ByteStream()
stream.writeStringNT('hello world!')
assert(stream.getBuffer().equals(Buffer.from('hello world!\0')))
assert(ByteWriter.buffersEqual(stream.getBuffer(), Buffer.from('hello world!\0')))
})
it('Numbers with i64', () => {
const stream = new ByteStream()
Expand Down Expand Up @@ -72,7 +74,8 @@ describe('basic tests - browser', () => {
it('NT string writing', () => {
const stream = new ByteStream()
stream.writeStringNT('hello world!')
assert(stream.getBuffer().equals(Uint8Array.from('hello world!\0'.split('').map(c => c.charCodeAt(0)))))
const expected = new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33, 0])
assert(ByteWriter.buffersEqual(stream.getBuffer(), expected))
})
it('Numbers with i64', () => {
const stream = new ByteStream()
Expand Down

0 comments on commit a3b860c

Please sign in to comment.