Skip to content

fix: replace node buffers with uint8arrays #10

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

Merged
merged 1 commit into from
Aug 19, 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Convert the passed CID to base 32 CID version 1.

| Name | Type | Description |
|------|------|-------------|
| cid | [`CID`](https://github.com/ipld/js-cid/)\|`String`\|`Buffer` | CID to convert. |
| cid | [`CID`](https://github.com/ipld/js-cid/)\|`String`\|`Uint8Array` | CID to convert. |

#### Returns

Expand Down Expand Up @@ -185,7 +185,7 @@ Format and convert a CID in various useful ways.

| Name | Type | Description |
|------|------|-------------|
| cid | [`CID`](https://github.com/ipld/js-cid/)\|`String`\|`Buffer` | CID to format |
| cid | [`CID`](https://github.com/ipld/js-cid/)\|`String`\|`Uint8Array` | CID to format |
| options | `Object` | (optional) options for formatting |
| options.format | `String` | Format string to use, default "%s" |
| options.base | `String` | Multibase name or code to use for output |
Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@
"author": "Alan Shaw",
"license": "MIT",
"dependencies": {
"cids": "~0.8.0",
"cids": "^1.0.0",
"explain-error": "^1.0.4",
"multibase": "~0.7.0",
"multihashes": "~0.4.14",
"multibase": "^3.0.0",
"multihashes": "^3.0.1",
"split2": "^3.1.1",
"uint8arrays": "^1.1.0",
"yargs": "^15.0.2"
},
"devDependencies": {
"aegir": "^21.10.2",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"aegir": "^25.1.0",
"execa": "^4.0.0"
},
"contributors": [
Expand Down
6 changes: 3 additions & 3 deletions src/cli/commands/bases.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ module.exports = {
handler (argv) {
CIDTool.bases().forEach(({ name, code }) => {
if (argv.prefix && argv.numeric) {
console.log(`${code} ${code.charCodeAt(0)} ${name}`) // eslint-disable-line no-console
console.log(`${code}\t${code.charCodeAt(0)}\t${name}`) // eslint-disable-line no-console
} else if (argv.prefix) {
console.log(`${code} ${name}`) // eslint-disable-line no-console
console.log(`${code}\t${name}`) // eslint-disable-line no-console
} else if (argv.numeric) {
console.log(`${code.charCodeAt(0)} ${name}`) // eslint-disable-line no-console
console.log(`${code.charCodeAt(0)}\t${name}`) // eslint-disable-line no-console
} else {
console.log(name) // eslint-disable-line no-console
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/codecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
handler (argv) {
CIDTool.codecs().forEach(({ name, code }) => {
if (argv.numeric) {
console.log(`${code} ${name}`) // eslint-disable-line no-console
console.log(`${code}\t${name}`) // eslint-disable-line no-console
} else {
console.log(name) // eslint-disable-line no-console
}
Expand Down
7 changes: 4 additions & 3 deletions src/core/bases.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
const multibase = require('multibase')

module.exports = function bases () {
return multibase.names.map((name, i) => {
const code = multibase.codes[i]
return { name, code }
return Object.keys(multibase.names).map(name => {
const base = multibase.names[name]

return { name: base.name, code: base.code }
})
}
15 changes: 8 additions & 7 deletions src/core/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const codecs = require('./codecs')
const explain = require('explain-error')
const multibase = require('multibase')
const multihash = require('multihashes')
const uint8ArrayToString = require('uint8arrays/to-string')

module.exports = function format (cid, options) {
options = options || {}
Expand Down Expand Up @@ -87,19 +88,19 @@ function replacer (cid, base, options) {
case 'L': // hash length
return multihash.decode(cid.multihash).length
case 'm': // multihash encoded in base %b
return multibase.encode(base, cid.multihash)
return uint8ArrayToString(multibase.encode(base, cid.multihash))
case 'M': // multihash encoded in base %b without base prefix
return multibase.encode(base, cid.multihash).slice(1)
return uint8ArrayToString(cid.multihash, base)
case 'd': // hash digest encoded in base %b
return multibase.encode(base, multihash.decode(cid.multihash).digest)
return uint8ArrayToString(multibase.encode(base, multihash.decode(cid.multihash).digest))
case 'D': // hash digest encoded in base %b without base prefix
return multibase.encode(base, multihash.decode(cid.multihash).digest).slice(1)
return uint8ArrayToString(multihash.decode(cid.multihash).digest, base)
case 's': // cid string encoded in base %b
return cid.toBaseEncodedString(base)
return cid.toString(base)
case 'S': // cid string without base prefix
return cid.version === 1
? cid.toBaseEncodedString(base).slice(1)
: multibase.encode(base, cid.buffer).toString().slice(1)
? cid.toString(base).slice(1)
: uint8ArrayToString(cid.bytes, base)
case 'P': // prefix
return prefix(cid)
default:
Expand Down
5 changes: 1 addition & 4 deletions test/cli/base32.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const CIDToolCli = require('./utils/cid-tool-cli')
const TestCID = require('../fixtures/test-cid')

Expand Down
22 changes: 11 additions & 11 deletions test/cli/bases.spec.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const multibase = require('multibase')
const CIDToolCli = require('./utils/cid-tool-cli')

describe('cli bases', () => {
it('should list multibase names', async () => {
const cli = CIDToolCli()
const expectedOutput = multibase.names.join('\n') + '\n'
const expectedOutput = Object.keys(multibase.names).join('\n') + '\n'
const { stdout } = await cli('bases')
expect(stdout).to.equal(expectedOutput)
})

it('should list multibase codes and names', async () => {
const cli = CIDToolCli()
const expectedOutput = multibase.names
.map((name, i) => `${multibase.codes[i]} ${name}`)
const expectedOutput = Object.keys(multibase.names)
.map(name => multibase.names[name])
.map(base => `${base.code}\t${base.name}`)
.join('\n') + '\n'
const { stdout } = await cli('bases --prefix')
expect(stdout).to.equal(expectedOutput)
})

it('should list multibase numeric codes and names', async () => {
const cli = CIDToolCli()
const expectedOutput = multibase.names
.map((name, i) => `${multibase.codes[i].charCodeAt(0)} ${name}`)
const expectedOutput = Object.keys(multibase.names)
.map(name => multibase.names[name])
.map(base => `${base.code.charCodeAt(0)}\t${base.name}`)
.join('\n') + '\n'
const { stdout } = await cli('bases --numeric')
expect(stdout).to.equal(expectedOutput)
})

it('should list multibase codes, numeric codes and names', async () => {
const cli = CIDToolCli()
const expectedOutput = multibase.names
.map((name, i) => `${multibase.codes[i]} ${multibase.codes[i].charCodeAt(0)} ${name}`)
const expectedOutput = Object.keys(multibase.names)
.map(name => multibase.names[name])
.map(base => `${base.code}\t${base.code.charCodeAt(0)}\t${base.name}`)
.join('\n') + '\n'
const { stdout } = await cli('bases --prefix --numeric')
expect(stdout).to.equal(expectedOutput)
Expand Down
7 changes: 2 additions & 5 deletions test/cli/codecs.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const CID = require('cids')
const CIDToolCli = require('./utils/cid-tool-cli')

Expand All @@ -21,7 +18,7 @@ describe('cli codecs', () => {
const expectedOutput = Object.keys(CID.codecs)
.map(name => {
const code = CID.codecs[name]
return `${code} ${name}`
return `${code}\t${name}`
})
.join('\n') + '\n'
const { stdout } = await cli('codecs --numeric')
Expand Down
5 changes: 1 addition & 4 deletions test/cli/format.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const CIDToolCli = require('./utils/cid-tool-cli')
const TestCID = require('../fixtures/test-cid')

Expand Down
5 changes: 1 addition & 4 deletions test/cli/hashes.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const multihash = require('multihashes')
const CIDToolCli = require('./utils/cid-tool-cli')

Expand Down
13 changes: 5 additions & 8 deletions test/core/base32.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const CID = require('cids')
const CIDTool = require('../../')
const TestCID = require('../fixtures/test-cid')
Expand All @@ -14,16 +11,16 @@ describe('core base32', () => {
const inputs = [
TestCID.v0,
new CID(TestCID.v0),
new CID(TestCID.v0).buffer,
new CID(TestCID.v0).bytes,
TestCID.b32,
new CID(TestCID.b32),
new CID(TestCID.b32).buffer,
new CID(TestCID.b32).bytes,
TestCID.b58,
new CID(TestCID.b58),
new CID(TestCID.b58).buffer,
new CID(TestCID.b58).bytes,
TestCID.b64,
new CID(TestCID.b64),
new CID(TestCID.b64).buffer
new CID(TestCID.b64).bytes
]

inputs.forEach(input => {
Expand Down
7 changes: 2 additions & 5 deletions test/core/bases.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const multibase = require('multibase')
const CIDTool = require('../../')

describe('core bases', () => {
it('should return list of multibase names and codes', () => {
const bases = CIDTool.bases()
expect(multibase.names.every(name => bases.find(b => b.name === name)))
expect(Object.keys(multibase.names).every(name => bases.find(b => b.name === name)))
.to.be.true()
})
})
5 changes: 1 addition & 4 deletions test/core/codecs.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const CID = require('cids')
const CIDTool = require('../../')

Expand Down
5 changes: 1 addition & 4 deletions test/core/format.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const CID = require('cids')
const CIDTool = require('../../')

Expand Down
5 changes: 1 addition & 4 deletions test/core/hashes.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const multihash = require('multihashes')
const CIDTool = require('../../')

Expand Down