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

Commit 418eb5f

Browse files
Gozalavmx
authored andcommitted
feat: add Uint8Array support
1 parent 7059cc8 commit 418eb5f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ const withIs = require('class-is')
1616
*/
1717
class Block {
1818
constructor (data, cid) {
19-
if (!data || !Buffer.isBuffer(data)) {
20-
throw new Error('first argument must be a buffer')
19+
if (!data || !ArrayBuffer.isView(data)) {
20+
throw new Error('first argument must be a buffer or typed array')
21+
} else if (!Buffer.isBuffer(data)) {
22+
data = Buffer.from(data.buffer, data.byteOffset, data.byteLength)
2123
}
2224

2325
if (!cid || !CID.isCID(cid)) {

test/index.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ describe('block', () => {
2727
expect(Block.isBlock(b)).to.eql(true)
2828
})
2929

30+
it('create with Uint8Array', () => {
31+
const b = new Block(
32+
new Uint8Array([104, 101, 108, 108, 111]),
33+
new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
34+
)
35+
36+
expect(Block.isBlock(b)).to.eql(true)
37+
})
38+
3039
it('block stays immutable', () => {
3140
const b = new Block(Buffer.from('hello'), new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'))
3241

0 commit comments

Comments
 (0)