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

feat: Aadd Uint8Array support #53

Merged
merged 1 commit into from
Jun 12, 2020
Merged
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
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