Skip to content

Commit 53398f5

Browse files
authored
fix: replace node buffers with uint8arrays (#140)
* fix: replace node buffers with uint8arrays All uses of node Buffers have been replaced with Uint8Arrays. Also redundant modules have been removed from package.json BREAKING CHANGES: - Where node Buffers were returned, now Uint8Arrays are - The `.buffer` property has been renamed `.bytes` similar to cid@1.0.0 * chore: downgrade aegir
1 parent e197c3d commit 53398f5

14 files changed

+246
-245
lines changed

.aegir.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
module.exports = {
4+
bundlesize: {
5+
maxSize: '14kB'
6+
}
7+
}

README.md

+16-18
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ js-multiaddr
1818

1919
## Table of Contents
2020

21-
- [Background](#background)
22-
- [What is multiaddr?](#what-is-multiaddr)
23-
- [Install](#install)
24-
- [Setup](#setup)
25-
- [Node.js](#nodejs)
26-
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
27-
- [Browser: `<script>` Tag](#browser-script-tag)
28-
- [Usage](#usage)
29-
- [API](#api) - https://multiformats.github.io/js-multiaddr/
30-
- [Maintainers](#maintainers)
31-
- [Contribute](#contribute)
32-
- [License](#license)
21+
- [js-multiaddr](#js-multiaddr)
22+
- [Lead Maintainer](#lead-maintainer)
23+
- [Table of Contents](#table-of-contents)
24+
- [Background](#background)
25+
- [What is multiaddr?](#what-is-multiaddr)
26+
- [Install](#install)
27+
- [Setup](#setup)
28+
- [Node.js](#nodejs)
29+
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
30+
- [Browser: `<script>` Tag](#browser-script-tag)
31+
- [Usage](#usage)
32+
- [API](#api)
33+
- [Contribute](#contribute)
34+
- [License](#license)
3335

3436
## Background
3537

@@ -79,10 +81,6 @@ the global namespace.
7981
<script src="https://unpkg.com/multiaddr/dist/index.js"></script>
8082
```
8183

82-
**NOTE**: You will need access to the Node.js `Buffer` API. If you are running
83-
in the browser, you can access it with `multiaddr.Buffer` or you can install
84-
[feross/buffer](https://github.com/feross/buffer).
85-
8684
## Usage
8785

8886
```js
@@ -93,8 +91,8 @@ $ node
9391
> const addr = multiaddr("/ip4/127.0.0.1/udp/1234")
9492
<Multiaddr /ip4/127.0.0.1/udp/1234>
9593

96-
> addr.buffer
97-
<Buffer 04 7f 00 00 01 11 04 d2>
94+
> addr.bytes
95+
<Uint8Array 04 7f 00 00 01 11 04 d2>
9896

9997
> addr.toString()
10098
'/ip4/127.0.0.1/udp/1234'

examples/try.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ var log = console.log
55

66
var addr = multiaddr('/ip4/127.0.0.1/udp/1234')
77
log(addr)
8-
log(addr.buffer)
8+
log(addr.bytes)
99
log(addr.toString())
10-
log(multiaddr(addr.buffer))
10+
log(multiaddr(addr.bytes))
1111

1212
log(addr.protoCodes())
1313
log(addr.protoNames())

intro.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ JavaScript implementation of [Multiaddr](https://github.com/multiformats/multiad
22

33
## What is multiaddr?
44

5-
Multiaddr is a standard way to represent addresses that:
5+
Multiaddr is a standard way to represent addresses that:
66
- Support any standard network protocols.
77
- Self-describe (include protocols).
88
- Have a binary packed format.
99
- Have a nice string representation.
1010
- Encapsulate well.
1111

12-
You can read more about what Multiaddr is in the language-independent Github repository:
12+
You can read more about what Multiaddr is in the language-independent Github repository:
1313
https://github.com/multiformats/multiaddr
1414

1515
Multiaddr is a part of a group of values called [Multiformats](https://github.com/multiformats/multiformats)
@@ -22,8 +22,8 @@ var Multiaddr = require('multiaddr')
2222
var home = new Multiaddr('/ip4/127.0.0.1/tcp/80')
2323
// <Multiaddr 047f000001060050 - /ip4/127.0.0.1/tcp/80>
2424

25-
home.buffer
26-
// <Buffer 04 7f 00 00 01 06 00 50>
25+
home.bytes
26+
// <Uint8Array 04 7f 00 00 01 06 00 50>
2727

2828
home.toString()
2929
// '/ip4/127.0.0.1/tcp/80'

package.json

+5-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"release-minor": "aegir release --type minor",
1717
"release-major": "aegir release --type major",
1818
"docs": "aegir docs",
19-
"size": "bundlesize -f dist/index.min.js -s 14kB"
19+
"size": "aegir build -b"
2020
},
2121
"files": [
2222
"src",
@@ -32,22 +32,19 @@
3232
"bugs": "https://github.com/multiformats/js-multiaddr/issues",
3333
"homepage": "https://github.com/multiformats/js-multiaddr",
3434
"dependencies": {
35-
"buffer": "^5.5.0",
36-
"cids": "~0.8.0",
35+
"cids": "^1.0.0",
3736
"class-is": "^1.1.0",
3837
"is-ip": "^3.1.0",
39-
"multibase": "^0.7.0",
38+
"multibase": "^3.0.0",
39+
"uint8arrays": "^1.1.0",
4040
"varint": "^5.0.0"
4141
},
4242
"devDependencies": {
4343
"@types/chai": "^4.2.8",
4444
"@types/dirty-chai": "^2.0.2",
45-
"@types/mocha": "^7.0.1",
45+
"@types/mocha": "^8.0.1",
4646
"@types/node": "^14.0.11",
4747
"aegir": "^22.0.0",
48-
"bundlesize": "~0.18.0",
49-
"chai": "^4.2.0",
50-
"dirty-chai": "^2.0.1",
5148
"typescript": "^3.9.5"
5249
},
5350
"contributors": [

src/codec.js

+36-35
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const convert = require('./convert')
54
const protocols = require('./protocols-table')
65
const varint = require('varint')
6+
const uint8ArrayConcat = require('uint8arrays/concat')
7+
const uint8ArrayToString = require('uint8arrays/to-string')
78

89
// export codec
910
module.exports = {
@@ -13,16 +14,16 @@ module.exports = {
1314
tuplesToStringTuples,
1415
stringTuplesToTuples,
1516

16-
bufferToTuples,
17-
tuplesToBuffer,
17+
bytesToTuples,
18+
tuplesToBytes,
1819

19-
bufferToString,
20-
stringToBuffer,
20+
bytesToString,
21+
stringToBytes,
2122

2223
fromString,
23-
fromBuffer,
24-
validateBuffer,
25-
isValidBuffer,
24+
fromBytes,
25+
validateBytes,
26+
isValidBytes,
2627
cleanPath,
2728

2829
ParseError,
@@ -85,21 +86,21 @@ function stringTuplesToString (tuples) {
8586
return cleanPath(parts.join('/'))
8687
}
8788

88-
// [[str name, str addr]... ] -> [[int code, Buffer]... ]
89+
// [[str name, str addr]... ] -> [[int code, Uint8Array]... ]
8990
function stringTuplesToTuples (tuples) {
9091
return tuples.map(tup => {
9192
if (!Array.isArray(tup)) {
9293
tup = [tup]
9394
}
9495
const proto = protoFromTuple(tup)
9596
if (tup.length > 1) {
96-
return [proto.code, convert.toBuffer(proto.code, tup[1])]
97+
return [proto.code, convert.toBytes(proto.code, tup[1])]
9798
}
9899
return [proto.code]
99100
})
100101
}
101102

102-
// [[int code, Buffer]... ] -> [[str name, str addr]... ]
103+
// [[int code, Uint8Array]... ] -> [[str name, str addr]... ]
103104
function tuplesToStringTuples (tuples) {
104105
return tuples.map(tup => {
105106
const proto = protoFromTuple(tup)
@@ -110,14 +111,14 @@ function tuplesToStringTuples (tuples) {
110111
})
111112
}
112113

113-
// [[int code, Buffer ]... ] -> Buffer
114-
function tuplesToBuffer (tuples) {
115-
return fromBuffer(Buffer.concat(tuples.map(tup => {
114+
// [[int code, Uint8Array ]... ] -> Uint8Array
115+
function tuplesToBytes (tuples) {
116+
return fromBytes(uint8ArrayConcat(tuples.map(tup => {
116117
const proto = protoFromTuple(tup)
117-
let buf = Buffer.from(varint.encode(proto.code))
118+
let buf = Uint8Array.from(varint.encode(proto.code))
118119

119120
if (tup.length > 1) {
120-
buf = Buffer.concat([buf, tup[1]]) // add address buffer
121+
buf = uint8ArrayConcat([buf, tup[1]]) // add address buffer
121122
}
122123

123124
return buf
@@ -135,8 +136,8 @@ function sizeForAddr (p, addr) {
135136
}
136137
}
137138

138-
// Buffer -> [[int code, Buffer ]... ]
139-
function bufferToTuples (buf) {
139+
// Uint8Array -> [[int code, Uint8Array ]... ]
140+
function bytesToTuples (buf) {
140141
const tuples = []
141142
let i = 0
142143
while (i < buf.length) {
@@ -158,7 +159,7 @@ function bufferToTuples (buf) {
158159
i += (size + n)
159160

160161
if (i > buf.length) { // did not end _exactly_ at buffer.length
161-
throw ParseError('Invalid address buffer: ' + buf.toString('hex'))
162+
throw ParseError('Invalid address Uint8Array: ' + uint8ArrayToString(buf, 'base16'))
162163
}
163164

164165
// ok, tuple seems good.
@@ -168,44 +169,44 @@ function bufferToTuples (buf) {
168169
return tuples
169170
}
170171

171-
// Buffer -> String
172-
function bufferToString (buf) {
173-
const a = bufferToTuples(buf)
172+
// Uint8Array -> String
173+
function bytesToString (buf) {
174+
const a = bytesToTuples(buf)
174175
const b = tuplesToStringTuples(a)
175176
return stringTuplesToString(b)
176177
}
177178

178-
// String -> Buffer
179-
function stringToBuffer (str) {
179+
// String -> Uint8Array
180+
function stringToBytes (str) {
180181
str = cleanPath(str)
181182
const a = stringToStringTuples(str)
182183
const b = stringTuplesToTuples(a)
183184

184-
return tuplesToBuffer(b)
185+
return tuplesToBytes(b)
185186
}
186187

187-
// String -> Buffer
188+
// String -> Uint8Array
188189
function fromString (str) {
189-
return stringToBuffer(str)
190+
return stringToBytes(str)
190191
}
191192

192-
// Buffer -> Buffer
193-
function fromBuffer (buf) {
194-
const err = validateBuffer(buf)
193+
// Uint8Array -> Uint8Array
194+
function fromBytes (buf) {
195+
const err = validateBytes(buf)
195196
if (err) throw err
196-
return Buffer.from(buf) // copy
197+
return Uint8Array.from(buf) // copy
197198
}
198199

199-
function validateBuffer (buf) {
200+
function validateBytes (buf) {
200201
try {
201-
bufferToTuples(buf) // try to parse. will throw if breaks
202+
bytesToTuples(buf) // try to parse. will throw if breaks
202203
} catch (err) {
203204
return err
204205
}
205206
}
206207

207-
function isValidBuffer (buf) {
208-
return validateBuffer(buf) === undefined
208+
function isValidBytes (buf) {
209+
return validateBytes(buf) === undefined
209210
}
210211

211212
function cleanPath (str) {

0 commit comments

Comments
 (0)