Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
feat: add Uint8Array support
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala authored and vmx committed Jun 12, 2020
1 parent 7059cc8 commit 418eb5f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ const withIs = require('class-is')
*/
class Block {
constructor (data, cid) {
if (!data || !Buffer.isBuffer(data)) {
throw new Error('first argument must be a buffer')
if (!data || !ArrayBuffer.isView(data)) {
throw new Error('first argument must be a buffer or typed array')
} else if (!Buffer.isBuffer(data)) {
data = Buffer.from(data.buffer, data.byteOffset, data.byteLength)
}

if (!cid || !CID.isCID(cid)) {
Expand Down
9 changes: 9 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ describe('block', () => {
expect(Block.isBlock(b)).to.eql(true)
})

it('create with Uint8Array', () => {
const b = new Block(
new Uint8Array([104, 101, 108, 108, 111]),
new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
)

expect(Block.isBlock(b)).to.eql(true)
})

it('block stays immutable', () => {
const b = new Block(Buffer.from('hello'), new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'))

Expand Down

0 comments on commit 418eb5f

Please sign in to comment.