Skip to content

Commit 507f8c4

Browse files
fix: replace node buffers with uint8arrays (#730)
* fix: replace node buffers with uint8arrays Upgrades all deps and replaces all use of node Buffers with Uint8Arrays BREAKING CHANGES: - All deps used by this module now use Uint8Arrays in place of node Buffers * chore: browser fixes * chore: remove .only * chore: stringify uint8array before parsing * chore: update interop suite * chore: remove ts from build command * chore: update deps * fix: update records to use uint8array * chore: fix lint * chore: update deps Co-authored-by: Jacob Heun <jacobheun@gmail.com>
1 parent 02b6248 commit 507f8c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+283
-270
lines changed

.aegir.js

+6
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,11 @@ module.exports = {
4949
hooks: {
5050
pre: before,
5151
post: after
52+
},
53+
webpack: {
54+
node: {
55+
// needed by bcrypto
56+
Buffer: true
57+
}
5258
}
5359
}

doc/API.md

+20-20
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ Writes a value to a key in the DHT.
585585
| Name | Type | Description |
586586
|------|------|-------------|
587587
| key | `string` | key to add to the dht |
588-
| value | `Buffer` | value to add to the dht |
588+
| value | `Uint8Array` | value to add to the dht |
589589
| [options] | `object` | put options |
590590
| [options.minPeers] | `number` | minimum number of peers required to successfully put (default: closestPeers.length) |
591591

@@ -600,7 +600,7 @@ Writes a value to a key in the DHT.
600600
```js
601601
// ...
602602
const key = '/key'
603-
const value = Buffer.from('oh hello there')
603+
const value = uint8ArrayFromString('oh hello there')
604604

605605
await libp2p.contentRouting.put(key, value)
606606
```
@@ -623,7 +623,7 @@ Queries the DHT for a value stored for a given key.
623623

624624
| Type | Description |
625625
|------|-------------|
626-
| `Promise<Buffer>` | Value obtained from the DHT |
626+
| `Promise<Uint8Array>` | Value obtained from the DHT |
627627

628628
#### Example
629629

@@ -653,7 +653,7 @@ Queries the DHT for the n values stored for the given key (without sorting).
653653

654654
| Type | Description |
655655
|------|-------------|
656-
| `Promise<Array<{from: PeerId, val: Buffer}>>` | Array of records obtained from the DHT |
656+
| `Promise<Array<{from: PeerId, val: Uint8Array}>>` | Array of records obtained from the DHT |
657657

658658
#### Example
659659

@@ -970,7 +970,7 @@ Delete the provided peer from the book.
970970
```js
971971
peerStore.metadataBook.delete(peerId)
972972
// false
973-
peerStore.metadataBook.set(peerId, 'nickname', Buffer.from('homePeer'))
973+
peerStore.metadataBook.set(peerId, 'nickname', uint8ArrayFromString('homePeer'))
974974
peerStore.metadataBook.delete(peerId)
975975
// true
976976
```
@@ -999,7 +999,7 @@ Deletes the provided peer metadata key-value pair from the book.
999999
```js
10001000
peerStore.metadataBook.deleteValue(peerId, 'location')
10011001
// false
1002-
peerStore.metadataBook.set(peerId, 'location', Buffer.from('Berlin'))
1002+
peerStore.metadataBook.set(peerId, 'location', uint8ArrayFromString('Berlin'))
10031003
peerStore.metadataBook.deleteValue(peerId, 'location')
10041004
// true
10051005
```
@@ -1020,14 +1020,14 @@ Get the known metadata of a provided peer.
10201020

10211021
| Type | Description |
10221022
|------|-------------|
1023-
| `Map<string, Buffer>` | Peer Metadata |
1023+
| `Map<string, Uint8Array>` | Peer Metadata |
10241024

10251025
#### Example
10261026

10271027
```js
10281028
peerStore.metadataBook.get(peerId)
10291029
// undefined
1030-
peerStore.metadataBook.set(peerId, 'location', Buffer.from('Berlin'))
1030+
peerStore.metadataBook.set(peerId, 'location', uint8ArrayFromString('Berlin'))
10311031
peerStore.metadataBook.get(peerId)
10321032
// Metadata Map
10331033
```
@@ -1049,14 +1049,14 @@ Get specific metadata of a provided peer.
10491049

10501050
| Type | Description |
10511051
|------|-------------|
1052-
| `Map<string, Buffer>` | Peer Metadata |
1052+
| `Map<string, Uint8Array>` | Peer Metadata |
10531053

10541054
#### Example
10551055

10561056
```js
10571057
peerStore.metadataBook.getValue(peerId, 'location')
10581058
// undefined
1059-
peerStore.metadataBook.set(peerId, 'location', Buffer.from('Berlin'))
1059+
peerStore.metadataBook.set(peerId, 'location', uint8ArrayFromString('Berlin'))
10601060
peerStore.metadataBook.getValue(peerId, 'location')
10611061
// Metadata Map
10621062
```
@@ -1073,7 +1073,7 @@ Set known metadata of a given `peerId`.
10731073
|------|------|-------------|
10741074
| peerId | [`PeerId`][peer-id] | peerId to set |
10751075
| key | `string` | key of the metadata value to store |
1076-
| value | `Buffer` | metadata value to store |
1076+
| value | `Uint8Array` | metadata value to store |
10771077

10781078
#### Returns
10791079

@@ -1084,7 +1084,7 @@ Set known metadata of a given `peerId`.
10841084
#### Example
10851085

10861086
```js
1087-
peerStore.metadataBook.set(peerId, 'location', Buffer.from('Berlin'))
1087+
peerStore.metadataBook.set(peerId, 'location', uint8ArrayFromString('Berlin'))
10881088
```
10891089

10901090
### peerStore.protoBook.delete
@@ -1308,7 +1308,7 @@ Publishes messages to the given topics.
13081308
| Name | Type | Description |
13091309
|------|------|-------------|
13101310
| topic | `string` | topic to publish |
1311-
| data | `Buffer` | data to publish |
1311+
| data | `Uint8Array` | data to publish |
13121312

13131313
#### Returns
13141314

@@ -1320,7 +1320,7 @@ Publishes messages to the given topics.
13201320

13211321
```js
13221322
const topic = 'topic'
1323-
const data = Buffer.from('data')
1323+
const data = uint8ArrayFromString('data')
13241324

13251325
await libp2p.pubsub.publish(topic, data)
13261326
```
@@ -1336,7 +1336,7 @@ Subscribes the given handler to a pubsub topic.
13361336
| Name | Type | Description |
13371337
|------|------|-------------|
13381338
| topic | `string` | topic to subscribe |
1339-
| handler | `function({ from: string, data: Buffer, seqno: Buffer, topicIDs: Array<string>, signature: Buffer, key: Buffer })` | handler for new data on topic |
1339+
| handler | `function({ from: string, data: Uint8Array, seqno: Uint8Array, topicIDs: Array<string>, signature: Uint8Array, key: Uint8Array })` | handler for new data on topic |
13401340

13411341
#### Returns
13421342

@@ -1679,19 +1679,19 @@ Encrypt protected data using the Cryptographic Message Syntax (CMS).
16791679
| Name | Type | Description |
16801680
|------|------|-------------|
16811681
| name | `string` | The local key name. |
1682-
| data | `Buffer` | The data to encrypt. |
1682+
| data | `Uint8Array` | The data to encrypt. |
16831683

16841684
#### Returns
16851685

16861686
| Type | Description |
16871687
|------|-------------|
1688-
| `Promise<Buffer>` | Encrypted data as a PKCS #7 message in DER. |
1688+
| `Promise<Uint8Array>` | Encrypted data as a PKCS #7 message in DER. |
16891689

16901690
#### Example
16911691

16921692
```js
16931693
const keyInfo = await libp2p.keychain.createKey('keyTest', 'rsa', 4096)
1694-
const enc = await libp2p.keychain.cms.encrypt('keyTest', Buffer.from('data'))
1694+
const enc = await libp2p.keychain.cms.encrypt('keyTest', uint8ArrayFromString('data'))
16951695
```
16961696

16971697
### keychain.cms.decrypt
@@ -1711,13 +1711,13 @@ The keychain must contain one of the keys used to encrypt the data. If none of
17111711

17121712
| Type | Description |
17131713
|------|-------------|
1714-
| `Promise<Buffer>` | Decrypted data. |
1714+
| `Promise<Uint8Array>` | Decrypted data. |
17151715

17161716
#### Example
17171717

17181718
```js
17191719
const keyInfo = await libp2p.keychain.createKey('keyTest', 'rsa', 4096)
1720-
const enc = await libp2p.keychain.cms.encrypt('keyTest', Buffer.from('data'))
1720+
const enc = await libp2p.keychain.cms.encrypt('keyTest', uint8ArrayFromString('data'))
17211721
const decData = await libp2p.keychain.cms.decrypt(enc)
17221722
```
17231723

examples/chat/src/stream.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const pipe = require('it-pipe')
55
const lp = require('it-length-prefixed')
6+
const uint8ArrayToString = require('uint8arrays/to-string')
67

78
function stdinToStream(stream) {
89
// Read utf-8 from stdin
@@ -28,7 +29,7 @@ function streamToConsole(stream) {
2829
// For each chunk of data
2930
for await (const msg of source) {
3031
// Output the data as a utf8 string
31-
console.log('> ' + msg.toString('utf8').replace('\n', ''))
32+
console.log('> ' + uint8ArrayToString(msg).replace('\n', ''))
3233
}
3334
}
3435
)

examples/pnet/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
/* eslint no-console: ["off"] */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
54
const { generate } = require('libp2p/src/pnet')
65
const privateLibp2pNode = require('./libp2p-node')
76

87
const pipe = require('it-pipe')
98

10-
// Create a buffer and write the swarm key to it
11-
const swarmKey = Buffer.alloc(95)
9+
// Create a Uint8Array and write the swarm key to it
10+
const swarmKey = new Uint8Array(95)
1211
generate(swarmKey)
1312

1413
// This key is for testing a different key not working
15-
const otherSwarmKey = Buffer.alloc(95)
14+
const otherSwarmKey = new Uint8Array(95)
1615
generate(otherSwarmKey)
1716

1817
;(async () => {

examples/pnet/libp2p-node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const Protector = require('libp2p/src/pnet')
1111
* privateLibp2pNode returns a libp2p node function that will use the swarm
1212
* key with the given `swarmKey` to create the Protector
1313
*
14-
* @param {Buffer} swarmKey
14+
* @param {Uint8Array} swarmKey
1515
* @returns {Promise<libp2p>} Returns a libp2pNode function for use in IPFS creation
1616
*/
1717
const privateLibp2pNode = async (swarmKey) => {

examples/pubsub/1.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* eslint-disable no-console */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
54
const Libp2p = require('../../')
65
const TCP = require('libp2p-tcp')
76
const Mplex = require('libp2p-mplex')
87
const { NOISE } = require('libp2p-noise')
98
const SECIO = require('libp2p-secio')
109
const Gossipsub = require('libp2p-gossipsub')
10+
const uint8ArrayFromString = require('uint8arrays/from-string')
1111

1212
const createNode = async () => {
1313
const node = await Libp2p.create({
@@ -48,6 +48,6 @@ const createNode = async () => {
4848

4949
// node2 publishes "news" every second
5050
setInterval(() => {
51-
node2.pubsub.publish(topic, Buffer.from('Bird bird bird, bird is the word!'))
51+
node2.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!'))
5252
}, 1000)
5353
})()

examples/pubsub/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ await node2.pubsub.subscribe(topic, (msg) => {
5757

5858
// node2 publishes "news" every second
5959
setInterval(() => {
60-
node2.pubsub.publish(topic, Buffer.from('Bird bird bird, bird is the word!'))
60+
node2.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!'))
6161
}, 1000)
6262
```
6363

examples/pubsub/message-filtering/1.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* eslint-disable no-console */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
54
const Libp2p = require('../../../')
65
const TCP = require('libp2p-tcp')
76
const Mplex = require('libp2p-mplex')
87
const { NOISE } = require('libp2p-noise')
98
const SECIO = require('libp2p-secio')
109
const Gossipsub = require('libp2p-gossipsub')
10+
const uint8ArrayFromString = require('uint8arrays/from-string')
1111

1212
const createNode = async () => {
1313
const node = await Libp2p.create({
@@ -73,7 +73,7 @@ const createNode = async () => {
7373
// car is not a fruit !
7474
setInterval(() => {
7575
console.log('############## fruit ' + myFruits[count] + ' ##############')
76-
node1.pubsub.publish(topic, Buffer.from(myFruits[count]))
76+
node1.pubsub.publish(topic, uint8ArrayFromString(myFruits[count]))
7777
count++
7878
if (count == myFruits.length) {
7979
count = 0

examples/pubsub/message-filtering/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const myFruits = ['banana', 'apple', 'car', 'orange'];
7979

8080
setInterval(() => {
8181
console.log('############## fruit ' + myFruits[count] + ' ##############')
82-
node1.pubsub.publish(topic, Buffer.from(myFruits[count]))
82+
node1.pubsub.publish(topic, new TextEncoder().encode(myFruits[count]))
8383
count++
8484
if (count == myFruits.length) {
8585
count = 0

0 commit comments

Comments
 (0)