Skip to content

Commit

Permalink
fix: clean up repo and bundle size reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias authored and daviddias committed Jan 8, 2019
1 parent c6f5f6a commit 136315a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 70 deletions.
29 changes: 0 additions & 29 deletions .npmignore

This file was deleted.

26 changes: 12 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,33 @@
"release-major": "aegir release --type major",
"coverage": "aegir coverage",
"coverage-publish": "aegir coverage publish",
"docs": "aegir docs"
},
"repository": {
"type": "git",
"url": "https://github.com/multiformats/js-multiaddr.git"
"docs": "aegir docs",
"size": "bundlesize -f dist/index.min.js -s 39kB"
},
"files": [
"src",
"dist"
],
"repository": "github:multiformats/js-multiaddr",
"keywords": [
"multiaddr",
"binary",
"string"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/multiformats/js-multiaddr/issues"
},
"bugs": "https://github.com/multiformats/js-multiaddr/issues",
"homepage": "https://github.com/multiformats/js-multiaddr",
"dependencies": {
"bs58": "^4.0.1",
"class-is": "^1.1.0",
"ip": "^1.1.5",
"ip-address": "^5.8.9",
"lodash.filter": "^4.6.0",
"lodash.map": "^4.6.0",
"varint": "^5.0.0",
"xtend": "^4.0.1"
"is-ip": "^2.0.0",
"varint": "^5.0.0"
},
"devDependencies": {
"aegir": "^17.1.1",
"aegir": "ipfs/aegir#fix/babel",
"buffer-loader": "0.0.1",
"bundlesize": "^0.17.0",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"pre-commit": "^1.2.2"
Expand Down
12 changes: 5 additions & 7 deletions src/codec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const map = require('lodash.map')
const filter = require('lodash.filter')
const convert = require('./convert')
const protocols = require('./protocols-table')
const varint = require('varint')
Expand Down Expand Up @@ -63,7 +61,7 @@ function stringToStringTuples (str) {
// [[str name, str addr]... ] -> string
function stringTuplesToString (tuples) {
const parts = []
map(tuples, function (tup) {
tuples.map(tup => {
const proto = protoFromTuple(tup)
parts.push(proto.name)
if (tup.length > 1) {
Expand All @@ -76,7 +74,7 @@ function stringTuplesToString (tuples) {

// [[str name, str addr]... ] -> [[int code, Buffer]... ]
function stringTuplesToTuples (tuples) {
return map(tuples, function (tup) {
return tuples.map(tup => {
if (!Array.isArray(tup)) {
tup = [tup]
}
Expand All @@ -90,7 +88,7 @@ function stringTuplesToTuples (tuples) {

// [[int code, Buffer]... ] -> [[str name, str addr]... ]
function tuplesToStringTuples (tuples) {
return map(tuples, function (tup) {
return tuples.map(tup => {
const proto = protoFromTuple(tup)
if (tup.length > 1) {
return [proto.code, convert.toString(proto.code, tup[1])]
Expand All @@ -101,7 +99,7 @@ function tuplesToStringTuples (tuples) {

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

Expand Down Expand Up @@ -198,7 +196,7 @@ function isValidBuffer (buf) {
}

function cleanPath (str) {
return '/' + filter(str.trim().split('/')).join('/')
return '/' + str.trim().split('/').filter(a => a).join('/')
}

function ParseError (str) {
Expand Down
24 changes: 17 additions & 7 deletions src/convert.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const ip = require('ip')
const ipAddress = require('ip-address')
const isIp = require('is-ip')
const protocols = require('./protocols-table')
const bs58 = require('bs58')
const varint = require('varint')
Expand All @@ -22,7 +22,7 @@ Convert.toString = function convertToString (proto, buf) {
switch (proto.code) {
case 4: // ipv4
case 41: // ipv6
return ip.toString(buf)
return buf2ip(buf)

case 6: // tcp
case 273: // udp
Expand All @@ -46,9 +46,9 @@ Convert.toBuffer = function convertToBuffer (proto, str) {
proto = protocols(proto)
switch (proto.code) {
case 4: // ipv4
return ip2buf(new ipAddress.Address4(str))
return ip2buf(str)
case 41: // ipv6
return ip2buf(new ipAddress.Address6(str))
return ip2buf(str)

case 6: // tcp
case 273: // udp
Expand All @@ -68,9 +68,19 @@ Convert.toBuffer = function convertToBuffer (proto, str) {
}
}

function ip2buf (ipaddr) {
if (!ipaddr.isValid()) throw new Error('invalid ip address')
return ip.toBuffer(ipaddr.address)
function ip2buf (ipString) {
if (!isIp(ipString)) {
throw new Error('invalid ip address')
}
return ip.toBuffer(ipString)
}

function buf2ip (ipBuff) {
const ipString = ip.toString(ipBuff)
if (!isIp(ipString)) {
throw new Error('invalid ip address')
}
return ipString
}

function port2buf (port) {
Expand Down
13 changes: 3 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const map = require('lodash.map')
const extend = require('xtend')
const codec = require('./codec')
const protocols = require('./protocols-table')
const varint = require('varint')
Expand Down Expand Up @@ -108,10 +106,7 @@ Multiaddr.prototype.inspect = function inspect () {
* // { code: 6, size: 16, name: 'tcp' } ]
*/
Multiaddr.prototype.protos = function protos () {
return map(this.protoCodes(), function (code) {
return extend(protocols(code))
// copy to prevent users from modifying the internal objs.
})
return this.protoCodes().map(code => Object.assign({}, protocols(code)))
}

/**
Expand Down Expand Up @@ -151,9 +146,7 @@ Multiaddr.prototype.protoCodes = function protoCodes () {
* // [ 'ip4', 'tcp' ]
*/
Multiaddr.prototype.protoNames = function protoNames () {
return map(this.protos(), function (proto) {
return proto.name
})
return this.protos().map(proto => proto.name)
}

/**
Expand Down Expand Up @@ -251,7 +244,7 @@ Multiaddr.prototype.getPeerId = function getPeerId () {
let b58str = null
try {
b58str = this.stringTuples().filter((tuple) => {
if (tuple[0] === protocols.names['ipfs'].code) {
if (tuple[0] === protocols.names.ipfs.code) {
return true
}
})[0][1]
Expand Down
4 changes: 1 addition & 3 deletions src/protocols-table.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const map = require('lodash.map')

function Protocols (proto) {
if (typeof (proto) === 'number') {
if (Protocols.codes[proto]) {
Expand Down Expand Up @@ -58,7 +56,7 @@ Protocols.names = {}
Protocols.codes = {}

// populate tables
map(Protocols.table, function (row) {
Protocols.table.map(row => {
const proto = p.apply(null, row)
Protocols.codes[proto.code] = proto
Protocols.names[proto.name] = proto
Expand Down

0 comments on commit 136315a

Please sign in to comment.