8000 fix: replace node buffers with uint8arrays · ipld/js-ipld-block@0696e4d · GitHub
[go: up one dir, main page]

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

Commit 0696e4d

Browse files
achingbrainvmx
authored andcommitted
fix: replace node buffers with uint8arrays
Removes use of node Buffers in favour of Uint8Arrays. BREAKING CHANGE: - The `.data` property used to be a Buffer, now it is a Uint8Array
1 parent ccd70a8 commit 0696e4d

File tree

4 files changed

+33
-41
lines changed

4 files changed

+33
-41
lines changed

README.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@
2121

2222
## Table of Contents
2323

24-
- [Install](#install)
25-
- [npm](#npm)
26-
- [Usage](#usage)
27-
- [Node.js](#nodejs)
28-
- [Example](#example)
29-
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
30-
- [Browser: `<script>` Tag](#browser-script-tag)
31-
- [API](#api)
32-
- [Block](#block)
33-
- [`new Block(data, cid)`](#new-blockdata-cid)
34-
- [`block.data`](#blockdata)
35-
- [`block.cid`](#blockcid)
36-
- [Contribute](#contribute)
37-
- [License](#license)
24+
- [IPLD Block JavaScript Implementation](#ipld-block-javascript-implementation)
25+
- [Lead Maintainer](#lead-maintainer)
26+
- [Table of Contents](#table-of-contents)
27+
- [Install](#install)
28+
- [npm](#npm)
29+
- [Usage](#usage)
30+
- [Node.js](#nodejs)
31+
- [Example](#example)
32+
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
33+
- [Browser: `<script>` Tag](#browser-script-tag)
34+
- [API](#api)
35+
- [Block](#block)
36+
- [`new Block(data, cid)`](#new-blockdata-cid)
37+
- [`block.data`](#blockdata)
38+
- [`block.cid`](#blockcid)
39+
- [Contribute](#contribute)
40+
- [License](#license)
3841

3942
## Install
4043

@@ -56,9 +59,10 @@ const Block = require('ipld-block')
5659

5760
```js
5861
const Block = require('ipld-block')
62+
const encoder = new TextEncoder('utf8')
5963

6064
// create a block
61-
const block = new Block(new Buffer('hello world'), cid)
65+
const block = new Block(encoder.encode('hello world'), cid)
6266
console.log(block.data.toString())
6367
```
6468

@@ -94,7 +98,7 @@ const Block = require('ipld-block')
9498

9599
#### `new Block(data, cid)`
96100

97-
- `data: Buffer`
101+
- `data: Uint8Array`
98102

99103
Creates a new block with raw data `data`.
100104

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@
3434
},
3535
"homepage": "https://github.com/ipld/js-ipld-block#readme",
3636
"devDependencies": {
37-
"aegir": "^21.4.5",
38-
"chai": "^4.2.0"
37+
"aegir": "^25.0.0",
38+
"uint8arrays": "^1.0.0"
3939
},
4040
"dependencies": {
41-
"buffer": "^5.5.0",
42-
"cids": "~0.8.0",
41+
"cids": "^1.0.0",
4342
"class-is": "^1.1.0"
4443
},
4544
"engines": {

src/index.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const CID = require('cids')
54
const withIs = require('class-is')
65

76
/**
87
* Represents an immutable block of data that is uniquely referenced with a cid.
98
*
109
* @constructor
11-
* @param {Buffer} data - The data to be stored in the block as a buffer.
10+
* @param {Uint8Array} data - The data to be stored in the block as a Uint8Array.
1211
* @param {CID} cid - The cid of the data
1312
*
1413
* @example
15-
* const block = new Block(new Buffer('a012d83b20f9371...'))
14+
* const block = new Block(Uint8Array.from([0, 1, 2, 3]), new CID('...'))
1615
*/
1716
class Block {
1817
constructor (data, cid) {
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)
18+
if (!data || !(data instanceof Uint8Array)) {
19+
throw new Error('first argument must be a Uint8Array')
2320
}
2421

2522
if (!cid || !CID.isCID(cid)) {
@@ -33,7 +30,7 @@ class Block {
3330
/**
3431
* The data of this block.
3532
*
36-
* @type {Buffer}
33+
* @type {Uint8Array}
3734
*/
3835
get data () {
3936
return this._data

test/index.spec.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const expect = require('chai').expect
4+
const { expect } = require('aegir/utils/chai')
55
const CID = require('cids')
6+
const uint8ArrayFromString = require('uint8arrays/from-string')
67

78
const Block = require('../src')
89

@@ -13,7 +14,7 @@ describe('block', () => {
1314
).to.throw()
1415

1516
expect(
16-
() => new Block(Buffer.from('hello'), 'cid')
17+
() => new Block(uint8ArrayFromString('hello'), 'cid')
1718
).to.throw()
1819

1920
expect(
@@ -22,22 +23,13 @@ describe('block', () => {
2223
})
2324

2425
it('create', () => {
25-
const b = new Block(Buffer.from('hello'), new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'))
26-
27-
expect(Block.isBlock(b)).to.eql(true)
28-
})
29-
30-
it('create with Uint8Array', () => {
31-
const b = new Block(
32-
new Uint8Array([104, 101, 108, 108, 111]),
33-
new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
34-
)
26+
const b = new Block(uint8ArrayFromString('hello'), new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'))
3527

3628
expect(Block.isBlock(b)).to.eql(true)
3729
})
3830

3931
it('block stays immutable', () => {
40-
const b = new Block(Buffer.from('hello'), new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'))
32+
const b = new Block(uint8ArrayFromString('hello'), new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'))
4133

4234
expect(
4335
() => { b.data = 'fail' }

0 commit comments

Comments
 (0)
0