Skip to content

Commit

Permalink
Support buffer with any byteOffset values instead of 0 (#201)
Browse files Browse the repository at this point in the history
* Support buffer with any byteOffset values instead of 0

* chore: add unit test

Co-authored-by: Tuyen Nguyen <[email protected]>
  • Loading branch information
Gozala and twoeths authored Jun 23, 2021
1 parent a95b991 commit 2201344
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function port2bytes (port) {
*/
function bytes2port (buf) {
const view = new DataView(buf.buffer)
return view.getUint16(0)
return view.getUint16(buf.byteOffset)
}

/**
Expand Down
7 changes: 7 additions & 0 deletions test/convert.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,12 @@ describe('convert', () => {
'c0a80001'
)
})

it('respects byteoffset during conversion', () => {
const bytes = convert.toBytes('sctp', '1234')
const buffer = new Uint8Array(bytes.byteLength + 5)
buffer.set(bytes, 5)
expect(convert.toString('sctp', buffer.subarray(5))).to.equal('1234')
})
})
})

0 comments on commit 2201344

Please sign in to comment.