Skip to content

Commit ea405b5

Browse files
authored
fix: swap node buffers for uint8arrays (#249)
Upgrades all the datastores to get/put `Uint8Array`s instead of node `Buffer`s. BREAKING CHANGES: - Swaps out node `Buffer`s for `Uint8Array`s
1 parent 7c16781 commit ea405b5

15 files changed

+73
-67
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ This is the implementation of the [IPFS repo spec](https://github.com/ipfs/specs
3636
- [`Promise<boolean> repo.exists()`](#promiseboolean-repoexists)
3737
- [`Promise<Boolean> repo.isInitialized()`](#promiseboolean-repoisinitialized)
3838
- [Repos](#repos)
39-
- [`Promise repo.put(key, value:Buffer)`](#promise-repoputkey-valuebuffer)
40-
- [`Promise<Buffer> repo.get(key)`](#promisebuffer-repogetkey)
39+
- [`Promise repo.put(key, value:Uint8Array)`](#promise-repoputkey-valueuint8array)
40+
- [`Promise<Uint8Array> repo.get(key)`](#promiseuint8array-repogetkey)
4141
- [Blocks](#blocks)
4242
- [`Promise<Block> repo.blocks.put(block:Block)`](#promiseblock-repoblocksputblockblock)
4343
- [`AsyncIterator<Block> repo.blocks.putMany(source:AsyncIterable<Block>)`](#asynciteratorblock-repoblocksputmanysourceasynciterableblock)
@@ -214,17 +214,17 @@ The returned promise resolves to `false` if the repo has not been initialized an
214214

215215
Root repo:
216216

217-
#### `Promise repo.put(key, value:Buffer)`
217+
#### `Promise repo.put(key, value:Uint8Array)`
218218

219219
Put a value at the root of the repo
220220

221-
* `key` can be a buffer, a string or a [Key][]
221+
* `key` can be a Uint8Array, a string or a [Key][]
222222

223-
#### `Promise<Buffer> repo.get(key)`
223+
#### `Promise<Uint8Array> repo.get(key)`
224224

225225
Get a value at the root of the repo
226226

227-
* `key` can be a buffer, a string or a [Key][]
227+
* `key` can be a Uint8Array, a string or a [Key][]
228228

229229
### Blocks
230230

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,33 @@
5252
"it-first": "^1.0.2",
5353
"just-range": "^2.1.0",
5454
"memdown": "^5.1.0",
55-
"multihashing-async": "^1.0.0",
55+
"multihashing-async": "^2.0.0",
5656
"ncp": "^2.0.0",
5757
"rimraf": "^3.0.0",
5858
"sinon": "^9.0.2"
5959
},
6060
"dependencies": {
6161
"bignumber.js": "^9.0.0",
62-
"buffer": "^5.6.0",
6362
"bytes": "^3.1.0",
64-
"cids": "^0.8.0",
65-
"datastore-core": "^1.1.0",
66-
"datastore-fs": "^1.1.0",
67-
"datastore-level": "^1.1.0",
63+
"cids": "^1.0.0",
64+
"datastore-core": "^2.0.0",
65+
"datastore-fs": "^2.0.0",
66+
"datastore-level": "^2.0.0",
6867
"debug": "^4.1.0",
6968
"err-code": "^2.0.0",
70-
"interface-datastore": "^1.0.2",
71-
"ipfs-repo-migrations": "^2.0.0",
72-
"ipfs-utils": "^2.2.0",
73-
"ipld-block": "^0.9.1",
69+
"interface-datastore": "^2.0.0",
70+
"ipfs-repo-migrations": "^3.0.0",
71+
"ipfs-utils": "^2.3.1",
72+
"ipld-block": "^0.10.0",
7473
"it-map": "^1.0.2",
7574
"it-pushable": "^1.4.0",
7675
"just-safe-get": "^2.0.0",
7776
"just-safe-set": "^2.1.0",
78-
"multibase": "^1.0.1",
77+
"multibase": "^3.0.0",
7978
"p-queue": "^6.0.0",
8079
"proper-lockfile": "^4.0.0",
81-
"sort-keys": "^4.0.0"
80+
"sort-keys": "^4.0.0",
81+
"uint8arrays": "^1.0.0"
8282
},
8383
"license": "MIT",
8484
"contributors": [

src/api-addr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const Key = require('interface-datastore').Key
4+
const uint8ArrayFromString = require('uint8arrays/from-string')
55

66
const apiFile = new Key('api')
77

@@ -23,7 +23,7 @@ module.exports = (store) => {
2323
* @returns {Promise<?>}
2424
*/
2525
async set (value) { // eslint-disable-line require-await
26-
return store.put(apiFile, Buffer.from(value.toString()))
26+
return store.put(apiFile, uint8ArrayFromString(value.toString()))
2727
},
2828
/**
2929
* Deletes api file

src/blockstore-utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { Key } = require('interface-datastore')
44
const CID = require('cids')
55
const multibase = require('multibase')
66
const errcode = require('err-code')
7+
const uint8ArrayToString = require('uint8arrays/to-string')
78

89
/**
910
* Transform a cid to the appropriate datastore key.
@@ -16,7 +17,7 @@ exports.cidToKey = cid => {
1617
throw errcode(new Error('Not a valid cid'), 'ERR_INVALID_CID')
1718
}
1819

19-
return new Key('/' + multibase.encode('base32', cid.multihash).toString().slice(1).toUpperCase(), false)
20+
return new Key('/' + uint8ArrayToString(multibase.encode('base32', cid.multihash)).slice(1).toUpperCase(), false)
2021
}
2122

2223
/**

src/config.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const Key = require('interface-datastore').Key
54
const { default: Queue } = require('p-queue')
65
const _get = require('just-safe-get')
76
const _set = require('just-safe-set')
87
const errcode = require('err-code')
98
const errors = require('./errors')
9+
const uint8ArrayToString = require('uint8arrays/to-string')
10+
const uint8ArrayFromString = require('uint8arrays/from-string')
1011

1112
const configKey = new Key('config')
1213

@@ -44,7 +45,7 @@ module.exports = (store) => {
4445
return
4546
}
4647

47-
const config = JSON.parse(encodedValue.toString())
48+
const config = JSON.parse(uint8ArrayToString(encodedValue))
4849
if (key !== undefined && _get(config, key) === undefined) {
4950
throw new errors.NotFoundError(`Key ${key} does not exist in config`)
5051
}
@@ -70,7 +71,7 @@ module.exports = (store) => {
7071
throw errcode(new Error('Invalid key type: ' + typeof key), 'ERR_INVALID_KEY')
7172
}
7273

73-
if (value === undefined || Buffer.isBuffer(value)) {
74+
if (value === undefined || (value instanceof Uint8Array)) {
7475
throw errcode(new Error('Invalid value type: ' + typeof value), 'ERR_INVALID_VALUE')
7576
}
7677

@@ -89,7 +90,7 @@ module.exports = (store) => {
8990
* @returns {void}
9091
*/
9192
async replace (value, options = {}) { // eslint-disable-line require-await
92-
if (!value || Buffer.isBuffer(value)) {
93+
if (!value || (value instanceof Uint8Array)) {
9394
throw errcode(new Error('Invalid value type: ' + typeof value), 'ERR_INVALID_VALUE')
9495
}
9596

@@ -127,7 +128,7 @@ module.exports = (store) => {
127128
}
128129

129130
function _saveAll (config) {
130-
const buf = Buffer.from(JSON.stringify(config, null, 2))
131+
const buf = uint8ArrayFromString(JSON.stringify(config, null, 2))
131132
return store.put(configKey, buf)
132133
}
133134
}

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ class IpfsRepo {
358358
count = count.plus(1)
359359
size = size
360360
.plus(block.data.byteLength)
361-
.plus(block.cid.buffer.byteLength)
361+
.plus(block.cid.bytes.byteLength)
362362
}
363363

364364
return { count, size }
@@ -369,7 +369,7 @@ async function getSize (queryFn) {
369369
const sum = new Big(0)
370370
for await (const block of queryFn.query({})) {
371371
sum.plus(block.value.byteLength)
372-
.plus(block.key.toBuffer().byteLength)
372+
.plus(block.key.uint8Array().byteLength)
373373
}
374374
return sum
375375
}

src/spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const Key = require('interface-datastore').Key
54
const sortKeys = require('sort-keys')
5+
const uint8ArrayToString = require('uint8arrays/to-string')
6+
const uint8ArrayFromString = require('uint8arrays/from-string')
67

78
const specKey = new Key('datastore_spec')
89

@@ -19,11 +20,11 @@ module.exports = (store) => {
1920
/**
2021
* Get the current datastore spec.
2122
*
22-
* @returns {Promise<Buffer>}
23+
* @returns {Promise<Uint8Array>}
2324
*/
2425
async get () {
2526
const buf = await store.get(specKey)
26-
return JSON.parse(buf.toString())
27+
return JSON.parse(uint8ArrayToString(buf))
2728
},
2829
/**
2930
* Set the datastore spec of the repo, writing it to the underlying store.
@@ -32,7 +33,7 @@ module.exports = (store) => {
3233
* @returns {Promise<void>}
3334
*/
3435
async set (spec) { // eslint-disable-line require-await
35-
return store.put(specKey, Buffer.from(JSON.stringify(sortKeys(spec, { deep: true }))))
36+
return store.put(specKey, uint8ArrayFromString(JSON.stringify(sortKeys(spec, { deep: true }))))
3637
}
3738
}
3839
}

src/version.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const Key = require('interface-datastore').Key
54
const debug = require('debug')
65
const log = debug('ipfs:repo:version')
6+
const uint8ArrayToString = require('uint8arrays/to-string')
7+
const uint8ArrayFromString = require('uint8arrays/from-string')
78

89
const versionKey = new Key('version')
910

@@ -24,7 +25,7 @@ module.exports = (store) => {
2425
*/
2526
async get () {
2627
const buf = await store.get(versionKey)
27-
return parseInt(buf.toString().trim(), 10)
28+
return parseInt(uint8ArrayToString(buf), 10)
2829
},
2930
/**
3031
* Set the version of the repo, writing it to the underlying store.
@@ -33,7 +34,7 @@ module.exports = (store) => {
3334
* @returns {Promise<void>}
3435
*/
3536
async set (version) { // eslint-disable-line require-await
36-
return store.put(versionKey, Buffer.from(String(version)))
37+
return store.put(versionKey, uint8ArrayFromString(String(version)))
3738
},
3839
/**
3940
* Check the current version, and returns true if versions matches

test/api-addr-test.js

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

4-
const { Buffer } = require('buffer')
54
const { expect } = require('./utils/chai')
65
const apiAddr = require('../src/api-addr')
6+
const uint8ArrayFromString = require('uint8arrays/from-string')
77

88
module.exports = () => {
99
describe('api-addr', () => {
@@ -31,7 +31,7 @@ module.exports = () => {
3131

3232
await api.set('0')
3333

34-
expect(val).to.deep.equal(Buffer.from('0'))
34+
expect(val).to.deep.equal(uint8ArrayFromString('0'))
3535
})
3636
})
3737
})

0 commit comments

Comments
 (0)