Skip to content

Commit 1ff151d

Browse files
authored
refactor: replace node buffers with uint8arrays (#10)
BREAKING CHANGES: - All use of node Buffers have been replaced with Uint8Arrays - All deps of this module now use Uint8Arrays in place of Buffers
1 parent 2df918d commit 1ff151d

File tree

16 files changed

+49
-75
lines changed

16 files changed

+49
-75
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Convert the passed CID to base 32 CID version 1.
106106

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

111111
#### Returns
112112

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

186186
| Name | Type | Description |
187187
|------|------|-------------|
188-
| cid | [`CID`](https://github.com/ipld/js-cid/)\|`String`\|`Buffer` | CID to format |
188+
| cid | [`CID`](https://github.com/ipld/js-cid/)\|`String`\|`Uint8Array` | CID to format |
189189
| options | `Object` | (optional) options for formatting |
190190
| options.format | `String` | Format string to use, default "%s" |
191191
| options.base | `String` | Multibase name or code to use for output |

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,16 @@
2828
"author": "Alan Shaw",
2929
"license": "MIT",
3030
"dependencies": {
31-
"cids": "~0.8.0",
31+
"cids": "^1.0.0",
3232
"explain-error": "^1.0.4",
33-
"multibase": "~0.7.0",
34-
"multihashes": "~0.4.14",
33+
"multibase": "^3.0.0",
34+
"multihashes": "^3.0.1",
3535
"split2": "^3.1.1",
36+
"uint8arrays": "^1.1.0",
3637
"yargs": "^15.0.2"
3738
},
3839
"devDependencies": {
39-
"aegir": "^21.10.2",
40-
"chai": "^4.1.2",
41-
"dirty-chai": "^2.0.1",
40+
"aegir": "^25.1.0",
4241
"execa": "^4.0.0"
4342
},
4443
"contributors": [

src/cli/commands/bases.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ module.exports = {
2323
handler (argv) {
2424
CIDTool.bases().forEach(({ name, code }) => {
2525
if (argv.prefix && argv.numeric) {
26-
console.log(`${code} ${code.charCodeAt(0)} ${name}`) // eslint-disable-line no-console
26+
console.log(`${code}\t${code.charCodeAt(0)}\t${name}`) // eslint-disable-line no-console
2727
} else if (argv.prefix) {
28-
console.log(`${code} ${name}`) // eslint-disable-line no-console
28+
console.log(`${code}\t${name}`) // eslint-disable-line no-console
2929
} else if (argv.numeric) {
30-
console.log(`${code.charCodeAt(0)} ${name}`) // eslint-disable-line no-console
30+
console.log(`${code.charCodeAt(0)}\t${name}`) // eslint-disable-line no-console
3131
} else {
3232
console.log(name) // eslint-disable-line no-console
3333
}

src/cli/commands/codecs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
handler (argv) {
1919
CIDTool.codecs().forEach(({ name, code }) => {
2020
if (argv.numeric) {
21-
console.log(`${code} ${name}`) // eslint-disable-line no-console
21+
console.log(`${code}\t${name}`) // eslint-disable-line no-console
2222
} else {
2323
console.log(name) // eslint-disable-line no-console
2424
}

src/core/bases.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
const multibase = require('multibase')
44

55
module.exports = function bases () {
6-
return multibase.names.map((name, i) => {
7-
const code = multibase.codes[i]
8-
return { name, code }
6+
return Object.keys(multibase.names).map(name => {
7+
const base = multibase.names[name]
8+
9+
return { name: base.name, code: base.code }
910
})
1011
}

src/core/format.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const codecs = require('./codecs')
66
const explain = require('explain-error')
77
const multibase = require('multibase')
88
const multihash = require('multihashes')
9+
const uint8ArrayToString = require('uint8arrays/to-string')
910

1011
module.exports = function format (cid, options) {
1112
options = options || {}
@@ -87,19 +88,19 @@ function replacer (cid, base, options) {
8788
case 'L': // hash length
8889
return multihash.decode(cid.multihash).length
8990
case 'm': // multihash encoded in base %b
90-
return multibase.encode(base, cid.multihash)
91+
return uint8ArrayToString(multibase.encode(base, cid.multihash))
9192
case 'M': // multihash encoded in base %b without base prefix
92-
return multibase.encode(base, cid.multihash).slice(1)
93+
return uint8ArrayToString(cid.multihash, base)
9394
case 'd': // hash digest encoded in base %b
94-
return multibase.encode(base, multihash.decode(cid.multihash).digest)
95+
return uint8ArrayToString(multibase.encode(base, multihash.decode(cid.multihash).digest))
9596
case 'D': // hash digest encoded in base %b without base prefix
96-
return multibase.encode(base, multihash.decode(cid.multihash).digest).slice(1)
97+
return uint8ArrayToString(multihash.decode(cid.multihash).digest, base)
9798
case 's': // cid string encoded in base %b
98-
return cid.toBaseEncodedString(base)
99+
return cid.toString(base)
99100
case 'S': // cid string without base prefix
100101
return cid.version === 1
101-
? cid.toBaseEncodedString(base).slice(1)
102-
: multibase.encode(base, cid.buffer).toString().slice(1)
102+
? cid.toString(base).slice(1)
103+
: uint8ArrayToString(cid.bytes, base)
103104
case 'P': // prefix
104105
return prefix(cid)
105106
default:

test/cli/base32.spec.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const chai = require('chai')
5-
const dirtyChai = require('dirty-chai')
6-
const expect = chai.expect
7-
chai.use(dirtyChai)
4+
const { expect } = require('aegir/utils/chai')
85
const CIDToolCli = require('./utils/cid-tool-cli')
96
const TestCID = require('../fixtures/test-cid')
107

test/cli/bases.spec.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const chai = require('chai')
5-
const dirtyChai = require('dirty-chai')
6-
const expect = chai.expect
7-
chai.use(dirtyChai)
4+
const { expect } = require('aegir/utils/chai')
85
const multibase = require('multibase')
96
const CIDToolCli = require('./utils/cid-tool-cli')
107

118
describe('cli bases', () => {
129
it('should list multibase names', async () => {
1310
const cli = CIDToolCli()
14-
const expectedOutput = multibase.names.join('\n') + '\n'
11+
const expectedOutput = Object.keys(multibase.names).join('\n') + '\n'
1512
const { stdout } = await cli('bases')
1613
expect(stdout).to.equal(expectedOutput)
1714
})
1815

1916
it('should list multibase codes and names', async () => {
2017
const cli = CIDToolCli()
21-
const expectedOutput = multibase.names
22-
.map((name, i) => `${multibase.codes[i]} ${name}`)
18+
const expectedOutput = Object.keys(multibase.names)
19+
.map(name => multibase.names[name])
20+
.map(base => `${base.code}\t${base.name}`)
2321
.join('\n') + '\n'
2422
const { stdout } = await cli('bases --prefix')
2523
expect(stdout).to.equal(expectedOutput)
2624
})
2725

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

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

test/cli/codecs.spec.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const chai = require('chai')
5-
const dirtyChai = require('dirty-chai')
6-
const expect = chai.expect
7-
chai.use(dirtyChai)
4+
const { expect } = require('aegir/utils/chai')
85
const CID = require('cids')
96
const CIDToolCli = require('./utils/cid-tool-cli')
107

@@ -21,7 +18,7 @@ describe('cli codecs', () => {
2118
const expectedOutput = Object.keys(CID.codecs)
2219
.map(name => {
2320
const code = CID.codecs[name]
24-
return `${code} ${name}`
21+
return `${code}\t${name}`
2522
})
2623
.join('\n') + '\n'
2724
const { stdout } = await cli('codecs --numeric')

test/cli/format.spec.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const chai = require('chai')
5-
const dirtyChai = require('dirty-chai')
6-
const expect = chai.expect
7-
chai.use(dirtyChai)
4+
const { expect } = require('aegir/utils/chai')
85
const CIDToolCli = require('./utils/cid-tool-cli')
96
const TestCID = require('../fixtures/test-cid')
107

0 commit comments

Comments
 (0)