Skip to content

Commit

Permalink
fix: replace node buffers with uint8arrays (#34)
Browse files Browse the repository at this point in the history
Swaps use of node Buffers for Uint8Arrays

BREAKING CHANGE: this module now only has deps that use Uint8Arrays and not Buffers

Co-authored-by: Marcin Rataj <[email protected]>
  • Loading branch information
achingbrain and lidel authored Aug 10, 2020
1 parent b568dc6 commit ac5ec19
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 69 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ stages:
- cov

node_js:
- '12'
- '10'
- 'lts/*'
- 'node'

os:
- linux
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ Complex validation of multiaddr can be built using `isIPFS.multiaddr` and [`maf

### `isIPFS.multiaddr(addr)`

Returns `true` if the provided `string`, [`Multiaddr`](https://github.com/multiformats/js-multiaddr) or `Buffer` represents a valid multiaddr or `false` otherwise.
Returns `true` if the provided `string`, [`Multiaddr`](https://github.com/multiformats/js-multiaddr) or `Uint8Array` represents a valid multiaddr or `false` otherwise.

### `isIPFS.peerMultiaddr(addr)`

Returns `true` if the provided `string`, [`Multiaddr`](https://github.com/multiformats/js-multiaddr) or `Buffer` represents a valid "IPFS Peer" multiaddr (matching [`IPFS` format from `mafmt`](https://github.com/multiformats/js-mafmt#api)) or `false` otherwise.
Returns `true` if the provided `string`, [`Multiaddr`](https://github.com/multiformats/js-multiaddr) or `Uint8Array` represents a valid "IPFS Peer" multiaddr (matching [`IPFS` format from `mafmt`](https://github.com/multiformats/js-mafmt#api)) or `false` otherwise.

# License

Expand Down
16 changes: 7 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@
"coverage-publish": "aegir coverage --upload"
},
"dependencies": {
"buffer": "^5.6.0",
"cids": "~0.8.0",
"cids": "^1.0.0",
"iso-url": "~0.4.7",
"mafmt": "^7.1.0",
"multiaddr": "^7.4.3",
"multibase": "~0.7.0",
"multihashes": "~0.4.19"
"mafmt": "^8.0.0",
"multiaddr": "^8.0.0",
"multibase": "^3.0.0",
"multihashes": "^3.0.1",
"uint8arrays": "^1.1.0"
},
"devDependencies": {
"aegir": "^21.10.1",
"bs58": "^4.0.1",
"chai": "^4.2.0",
"aegir": "^25.0.0",
"pre-commit": "^1.2.2"
},
"engines": {
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

const { Buffer } = require('buffer')
const multihash = require('multihashes')
const multibase = require('multibase')
const Multiaddr = require('multiaddr')
const mafmt = require('mafmt')
const CID = require('cids')
const { URL } = require('iso-url')
const uint8ArrayToString = require('uint8arrays/to-string')

const pathGatewayPattern = /^https?:\/\/[^/]+\/(ip[fn]s)\/([^/?#]+)/
const pathPattern = /^\/(ip[fn]s)\/([^/?#]+)/
Expand Down Expand Up @@ -132,8 +132,8 @@ function isString (input) {
}

function convertToString (input) {
if (Buffer.isBuffer(input)) {
return multibase.encode('base58btc', input).toString().slice(1)
if (input instanceof Uint8Array) {
return uint8ArrayToString(input, 'base58btc')
}

if (isString(input)) {
Expand Down
13 changes: 6 additions & 7 deletions test/test-cid.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const base58 = require('bs58')
const expect = require('chai').expect
const { expect } = require('aegir/utils/chai')
const isIPFS = require('../src/index')
const CID = require('cids')
const uint8ArrayFromString = require('uint8arrays/from-string')

describe('ipfs cid', () => {
it('isIPFS.cid should match a valid CID instance', (done) => {
Expand All @@ -21,14 +20,14 @@ describe('ipfs cid', () => {
done()
})

it('isIPFS.cid should match a valid CIDv0 (multihash) buffer', (done) => {
const actual = isIPFS.cid(Buffer.from(base58.decode('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')))
it('isIPFS.cid should match a valid CIDv0 (multihash) Uint8Array', (done) => {
const actual = isIPFS.cid(uint8ArrayFromString('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o', 'base58btc'))
expect(actual).to.equal(true)
done()
})

it('isIPFS.cid should not match a broken CIDv0 buffer', (done) => {
const actual = isIPFS.cid(Buffer.from('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE70'))
it('isIPFS.cid should not match a broken CIDv0 Uint8Array', (done) => {
const actual = isIPFS.cid(uint8ArrayFromString('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE70'))
expect(actual).to.equal(false)
done()
})
Expand Down
20 changes: 10 additions & 10 deletions test/test-multiaddr.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const expect = require('chai').expect
const { expect } = require('aegir/utils/chai')
const Multiaddr = require('multiaddr')
const isIPFS = require('../src/index')
const uint8ArrayFromString = require('uint8arrays/from-string')

describe('ipfs multiaddr', () => {
it('isIPFS.multiaddr should match a string with valid ip4 multiaddr', (done) => {
Expand All @@ -26,15 +26,15 @@ describe('ipfs multiaddr', () => {
done()
})

it('isIPFS.multiaddr should match a Buffer with multiaddr', (done) => {
it('isIPFS.multiaddr should match a Uint8Array with multiaddr', (done) => {
const ma = new Multiaddr('/ip6/::1/udp/1234/http')
const actual = isIPFS.multiaddr(Buffer.from(ma.buffer))
const actual = isIPFS.multiaddr(ma.bytes)
expect(actual).to.equal(true)
done()
})

it('isIPFS.multiaddr should not match random Buffer', (done) => {
const actual = isIPFS.multiaddr(Buffer.from('randombuffer'))
it('isIPFS.multiaddr should not match random Uint8Array', (done) => {
const actual = isIPFS.multiaddr(uint8ArrayFromString('randomUint8Array'))
expect(actual).to.equal(false)
done()
})
Expand Down Expand Up @@ -111,17 +111,17 @@ describe('ipfs peerMultiaddr', () => {
done()
})

it('isIPFS.peerMultiaddr should match a Buffer with multiaddr', (done) => {
it('isIPFS.peerMultiaddr should match a Uint8Array with multiaddr', (done) => {
for (const addr of validPeerMultiaddrs) {
const ma = new Multiaddr(addr)
const actual = isIPFS.peerMultiaddr((Buffer.from(ma.buffer)))
const actual = isIPFS.peerMultiaddr(ma.bytes)
expect(actual, `isIPFS.peerMultiaddr(${addr})`).to.equal(true)
}
done()
})

it('isIPFS.peerMultiaddr should not match random Buffer', (done) => {
const actual = isIPFS.peerMultiaddr(Buffer.from('randombuffer'))
it('isIPFS.peerMultiaddr should not match random Uint8Array', (done) => {
const actual = isIPFS.peerMultiaddr(uint8ArrayFromString('randomUint8Array'))
expect(actual).to.equal(false)
done()
})
Expand Down
13 changes: 6 additions & 7 deletions test/test-multihash.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const base58 = require('bs58')
const expect = require('chai').expect
const { expect } = require('aegir/utils/chai')
const isIPFS = require('../src/index')
const uint8ArrayFromString = require('uint8arrays/from-string')

describe('ipfs multihash', () => {
it('isIPFS.multihash should match a valid multihash', (done) => {
Expand All @@ -13,14 +12,14 @@ describe('ipfs multihash', () => {
done()
})

it('isIPFS.multihash should match a valid multihash buffer', (done) => {
const actual = isIPFS.multihash(Buffer.from(base58.decode('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')))
it('isIPFS.multihash should match a valid multihash Uint8Array', (done) => {
const actual = isIPFS.multihash(uint8ArrayFromString('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o', 'base58btc'))
expect(actual).to.equal(true)
done()
})

it('isIPFS.multihash should not match a buffer', (done) => {
const actual = isIPFS.multihash(Buffer.from('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE70'))
it('isIPFS.multihash should not match a Uint8Array', (done) => {
const actual = isIPFS.multihash(uint8ArrayFromString('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE70'))
expect(actual).to.equal(false)
done()
})
Expand Down
21 changes: 10 additions & 11 deletions test/test-path.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const base58 = require('bs58')
const isIPFS = require('../src/index')
const expect = require('chai').expect
const { expect } = require('aegir/utils/chai')
const uint8ArrayFromString = require('uint8arrays/from-string')

describe('ipfs path', () => {
it('isIPFS.ipfsPath should match an ipfs path', (done) => {
Expand Down Expand Up @@ -37,8 +36,8 @@ describe('ipfs path', () => {
done()
})

it('isIPFS.ipfsPath should not match a buffer data type', (done) => {
const actual = isIPFS.ipfsPath(Buffer.from(base58.decode('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')))
it('isIPFS.ipfsPath should not match a Uint8Array data type', (done) => {
const actual = isIPFS.ipfsPath(uint8ArrayFromString('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o', 'base58btc'))
expect(actual).to.equal(false)
done()
})
Expand Down Expand Up @@ -67,8 +66,8 @@ describe('ipfs path', () => {
done()
})

it('isIPFS.ipnsPath should not match a buffer data type', (done) => {
const actual = isIPFS.ipnsPath(Buffer.from(base58.decode('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')))
it('isIPFS.ipnsPath should not match a Uint8Array data type', (done) => {
const actual = isIPFS.ipnsPath(uint8ArrayFromString('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o', 'base58btc'))
expect(actual).to.equal(false)
done()
})
Expand Down Expand Up @@ -97,8 +96,8 @@ describe('ipfs path', () => {
done()
})

it('isIPFS.path should not match a buffer data type', (done) => {
const actual = isIPFS.path(Buffer.from(base58.decode('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')))
it('isIPFS.path should not match a Uint8Array data type', (done) => {
const actual = isIPFS.path(uint8ArrayFromString('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o', 'base58btc'))
expect(actual).to.equal(false)
done()
})
Expand Down Expand Up @@ -127,8 +126,8 @@ describe('ipfs path', () => {
done()
})

it('isIPFS.urlOrPath should not match a buffer data type', (done) => {
const actual = isIPFS.ipfsPath(Buffer.from(base58.decode('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')))
it('isIPFS.urlOrPath should not match a Uint8Array data type', (done) => {
const actual = isIPFS.ipfsPath(uint8ArrayFromString('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o', 'base58btc'))
expect(actual).to.equal(false)
done()
})
Expand Down
17 changes: 8 additions & 9 deletions test/test-subdomain.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const base58 = require('bs58')
const isIPFS = require('../src/index')
const expect = require('chai').expect
const { expect } = require('aegir/utils/chai')
const uint8ArrayFromString = require('uint8arrays/from-string')

describe('ipfs subdomain', () => {
it('isIPFS.ipfsSubdomain should match a cidv1b32', (done) => {
Expand Down Expand Up @@ -48,8 +47,8 @@ describe('ipfs subdomain', () => {
done()
})

it('isIPFS.ipfsSubdomain should not match a buffer data type', (done) => {
const actual = isIPFS.ipfsSubdomain(Buffer.from(base58.decode('QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR')))
it('isIPFS.ipfsSubdomain should not match a Uint8Array data type', (done) => {
const actual = isIPFS.ipfsSubdomain(uint8ArrayFromString('QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR', 'base58btc'))
expect(actual).to.equal(false)
done()
})
Expand Down Expand Up @@ -81,8 +80,8 @@ describe('ipfs subdomain', () => {
done()
})

it('isIPFS.ipnsSubdomain should not match a buffer data type', (done) => {
const actual = isIPFS.ipnsSubdomain(Buffer.from(base58.decode('QmNQuBJ8tg4QN6mSLXHekxBbcToPwKxamWNrDdEugxMTDd')))
it('isIPFS.ipnsSubdomain should not match a Uint8Array data type', (done) => {
const actual = isIPFS.ipnsSubdomain(uint8ArrayFromString('QmNQuBJ8tg4QN6mSLXHekxBbcToPwKxamWNrDdEugxMTDd', 'base58btc'))
expect(actual).to.equal(false)
done()
})
Expand Down Expand Up @@ -154,8 +153,8 @@ describe('ipfs subdomain', () => {
done()
})

it('isIPFS.subdomain should not match a buffer data type', (done) => {
const actual = isIPFS.subdomain(Buffer.from(base58.decode('QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR')))
it('isIPFS.subdomain should not match a Uint8Array data type', (done) => {
const actual = isIPFS.subdomain(uint8ArrayFromString('QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR', 'base58btc'))
expect(actual).to.equal(false)
done()
})
Expand Down
17 changes: 8 additions & 9 deletions test/test-url.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const base58 = require('bs58')
const expect = require('chai').expect
const { expect } = require('aegir/utils/chai')
const isIPFS = require('../src/index')
const uint8ArrayFromString = require('uint8arrays/from-string')

describe('ipfs url', () => {
it('isIPFS.ipfsUrl should match an ipfs url', (done) => {
Expand Down Expand Up @@ -37,8 +36,8 @@ describe('ipfs url', () => {
done()
})

it('isIPFS.ipfsUrl should not match a buffer input', (done) => {
const actual = isIPFS.ipfsUrl(Buffer.from(base58.decode('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')))
it('isIPFS.ipfsUrl should not match a Uint8Array input', (done) => {
const actual = isIPFS.ipfsUrl(uint8ArrayFromString('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o', 'base58btc'))
expect(actual).to.equal(false)
done()
})
Expand Down Expand Up @@ -67,8 +66,8 @@ describe('ipfs url', () => {
done()
})

it('isIPFS.ipnsUrl should not match a buffer input', (done) => {
const actual = isIPFS.ipnsUrl(Buffer.from(base58.decode('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')))
it('isIPFS.ipnsUrl should not match a Uint8Array input', (done) => {
const actual = isIPFS.ipnsUrl(uint8ArrayFromString('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o', 'base58btc'))
expect(actual).to.equal(false)
done()
})
Expand Down Expand Up @@ -97,8 +96,8 @@ describe('ipfs url', () => {
done()
})

it('isIPFS.url should not match a buffer input', (done) => {
const actual = isIPFS.url(Buffer.from(base58.decode('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o')))
it('isIPFS.url should not match a Uint8Array input', (done) => {
const actual = isIPFS.url(uint8ArrayFromString('QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o', 'base58btc'))
expect(actual).to.equal(false)
done()
})
Expand Down

0 comments on commit ac5ec19

Please sign in to comment.