Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace node buffers with uint8arrays #140

Merged
merged 2 commits into from
Aug 7, 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
7 changes: 7 additions & 0 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = {
bundlesize: {
maxSize: '14kB'
}
}
34 changes: 16 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ js-multiaddr

## Table of Contents

- [Background](#background)
- [What is multiaddr?](#what-is-multiaddr)
- [Install](#install)
- [Setup](#setup)
- [Node.js](#nodejs)
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
- [Browser: `<script>` Tag](#browser-script-tag)
- [Usage](#usage)
- [API](#api) - https://multiformats.github.io/js-multiaddr/
- [Maintainers](#maintainers)
- [Contribute](#contribute)
- [License](#license)
- [js-multiaddr](#js-multiaddr)
- [Lead Maintainer](#lead-maintainer)
- [Table of Contents](#table-of-contents)
- [Background](#background)
- [What is multiaddr?](#what-is-multiaddr)
- [Install](#install)
- [Setup](#setup)
- [Node.js](#nodejs)
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
- [Browser: `<script>` Tag](#browser-script-tag)
- [Usage](#usage)
- [API](#api)
- [Contribute](#contribute)
- [License](#license)

## Background

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

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

## Usage

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

> addr.buffer
<Buffer 04 7f 00 00 01 11 04 d2>
> addr.bytes
<Uint8Array 04 7f 00 00 01 11 04 d2>

> addr.toString()
'/ip4/127.0.0.1/udp/1234'
Expand Down
4 changes: 2 additions & 2 deletions examples/try.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ var log = console.log

var addr = multiaddr('/ip4/127.0.0.1/udp/1234')
log(addr)
log(addr.buffer)
log(addr.bytes)
log(addr.toString())
log(multiaddr(addr.buffer))
log(multiaddr(addr.bytes))

log(addr.protoCodes())
log(addr.protoNames())
Expand Down
8 changes: 4 additions & 4 deletions intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ JavaScript implementation of [Multiaddr](https://github.com/multiformats/multiad

## What is multiaddr?

Multiaddr is a standard way to represent addresses that:
Multiaddr is a standard way to represent addresses that:
- Support any standard network protocols.
- Self-describe (include protocols).
- Have a binary packed format.
- Have a nice string representation.
- Encapsulate well.

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

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

home.buffer
// <Buffer 04 7f 00 00 01 06 00 50>
home.bytes
// <Uint8Array 04 7f 00 00 01 06 00 50>

home.toString()
// '/ip4/127.0.0.1/tcp/80'
Expand Down
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
"docs": "aegir docs",
"size": "bundlesize -f dist/index.min.js -s 14kB"
"size": "aegir build -b"
},
"files": [
"src",
Expand All @@ -32,22 +32,19 @@
"bugs": "https://github.com/multiformats/js-multiaddr/issues",
"homepage": "https://github.com/multiformats/js-multiaddr",
"dependencies": {
"buffer": "^5.5.0",
"cids": "~0.8.0",
"cids": "^1.0.0",
"class-is": "^1.1.0",
"is-ip": "^3.1.0",
"multibase": "^0.7.0",
"multibase": "^3.0.0",
"uint8arrays": "^1.1.0",
"varint": "^5.0.0"
},
"devDependencies": {
"@types/chai": "^4.2.8",
"@types/dirty-chai": "^2.0.2",
"@types/mocha": "^7.0.1",
"@types/mocha": "^8.0.1",
"@types/node": "^14.0.11",
"aegir": "^22.0.0",
"bundlesize": "~0.18.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"typescript": "^3.9.5"
},
"contributors": [
Expand Down
71 changes: 36 additions & 35 deletions src/codec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

const { Buffer } = require('buffer')
const convert = require('./convert')
const protocols = require('./protocols-table')
const varint = require('varint')
const uint8ArrayConcat = require('uint8arrays/concat')
const uint8ArrayToString = require('uint8arrays/to-string')

// export codec
module.exports = {
Expand All @@ -13,16 +14,16 @@ module.exports = {
tuplesToStringTuples,
stringTuplesToTuples,

bufferToTuples,
tuplesToBuffer,
bytesToTuples,
tuplesToBytes,

bufferToString,
stringToBuffer,
bytesToString,
stringToBytes,

fromString,
fromBuffer,
validateBuffer,
isValidBuffer,
fromBytes,
validateBytes,
isValidBytes,
cleanPath,

ParseError,
Expand Down Expand Up @@ -85,21 +86,21 @@ function stringTuplesToString (tuples) {
return cleanPath(parts.join('/'))
}

// [[str name, str addr]... ] -> [[int code, Buffer]... ]
// [[str name, str addr]... ] -> [[int code, Uint8Array]... ]
function stringTuplesToTuples (tuples) {
return tuples.map(tup => {
if (!Array.isArray(tup)) {
tup = [tup]
}
const proto = protoFromTuple(tup)
if (tup.length > 1) {
return [proto.code, convert.toBuffer(proto.code, tup[1])]
return [proto.code, convert.toBytes(proto.code, tup[1])]
}
return [proto.code]
})
}

// [[int code, Buffer]... ] -> [[str name, str addr]... ]
// [[int code, Uint8Array]... ] -> [[str name, str addr]... ]
function tuplesToStringTuples (tuples) {
return tuples.map(tup => {
const proto = protoFromTuple(tup)
Expand All @@ -110,14 +111,14 @@ function tuplesToStringTuples (tuples) {
})
}

// [[int code, Buffer ]... ] -> Buffer
function tuplesToBuffer (tuples) {
return fromBuffer(Buffer.concat(tuples.map(tup => {
// [[int code, Uint8Array ]... ] -> Uint8Array
function tuplesToBytes (tuples) {
return fromBytes(uint8ArrayConcat(tuples.map(tup => {
const proto = protoFromTuple(tup)
let buf = Buffer.from(varint.encode(proto.code))
let buf = Uint8Array.from(varint.encode(proto.code))

if (tup.length > 1) {
buf = Buffer.concat([buf, tup[1]]) // add address buffer
buf = uint8ArrayConcat([buf, tup[1]]) // add address buffer
}

return buf
Expand All @@ -135,8 +136,8 @@ function sizeForAddr (p, addr) {
}
}

// Buffer -> [[int code, Buffer ]... ]
function bufferToTuples (buf) {
// Uint8Array -> [[int code, Uint8Array ]... ]
function bytesToTuples (buf) {
const tuples = []
let i = 0
while (i < buf.length) {
Expand All @@ -158,7 +159,7 @@ function bufferToTuples (buf) {
i += (size + n)

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

// ok, tuple seems good.
Expand All @@ -168,44 +169,44 @@ function bufferToTuples (buf) {
return tuples
}

// Buffer -> String
function bufferToString (buf) {
const a = bufferToTuples(buf)
// Uint8Array -> String
function bytesToString (buf) {
const a = bytesToTuples(buf)
const b = tuplesToStringTuples(a)
return stringTuplesToString(b)
}

// String -> Buffer
function stringToBuffer (str) {
// String -> Uint8Array
function stringToBytes (str) {
str = cleanPath(str)
const a = stringToStringTuples(str)
const b = stringTuplesToTuples(a)

return tuplesToBuffer(b)
return tuplesToBytes(b)
}

// String -> Buffer
// String -> Uint8Array
function fromString (str) {
return stringToBuffer(str)
return stringToBytes(str)
}

// Buffer -> Buffer
function fromBuffer (buf) {
const err = validateBuffer(buf)
// Uint8Array -> Uint8Array
function fromBytes (buf) {
const err = validateBytes(buf)
if (err) throw err
return Buffer.from(buf) // copy
return Uint8Array.from(buf) // copy
}

function validateBuffer (buf) {
function validateBytes (buf) {
try {
bufferToTuples(buf) // try to parse. will throw if breaks
bytesToTuples(buf) // try to parse. will throw if breaks
} catch (err) {
return err
}
}

function isValidBuffer (buf) {
return validateBuffer(buf) === undefined
function isValidBytes (buf) {
return validateBytes(buf) === undefined
}

function cleanPath (str) {
Expand Down
Loading