Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit a9786b9

Browse files
authored
fix: remove node buffers (#27)
Updates to the latest interface-datastore that swaps out Buffers for Uint8Arrays. BREAKING CHANGE: no longer uses node Buffers, only Uint8Arrays
1 parent f144604 commit a9786b9

9 files changed

+35
-28
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
},
3939
"homepage": "https://github.com/ipfs/js-datastore-core#readme",
4040
"devDependencies": {
41-
"aegir": "^22.0.0",
41+
"aegir": "^25.0.0",
4242
"async-iterator-all": "^1.0.0",
4343
"chai": "^4.2.0",
4444
"dirty-chai": "^2.0.1"
4545
},
4646
"dependencies": {
47-
"buffer": "^5.5.0",
4847
"debug": "^4.1.1",
49-
"interface-datastore": "^1.0.2"
48+
"interface-datastore": "^2.0.0",
49+
"ipfs-utils": "^2.3.1"
5050
},
5151
"engines": {
5252
"node": ">=6.0.0",

src/shard.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict'
33

44
const Key = require('interface-datastore').Key
5+
const { utf8Decoder } = require('../src/utils')
56

67
const readme = require('./shard-readme')
78

@@ -125,7 +126,7 @@ exports.readShardFun = async (path /* : string */, store) /* : Promise<ShardV1>
125126
const key = new Key(path).child(new Key(SHARDING_FN))
126127
const get = typeof store.getRaw === 'function' ? store.getRaw.bind(store) : store.get.bind(store)
127128
const res = await get(key)
128-
return parseShardFun((res || '').toString().trim())
129+
return parseShardFun(utf8Decoder.decode(res || '').trim())
129130
}
130131

131132
exports.readme = readme

src/sharding.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const { Adapter, Key } = require('interface-datastore')
54
const sh = require('./shard')
65
const KeytransformStore = require('./keytransform')
6+
const { utf8Encoder } = require('../src/utils')
77

88
const shardKey = new Key(sh.SHARDING_FN)
99
const shardReadmeKey = new Key(sh.README_FN)
@@ -65,8 +65,8 @@ class ShardingDatastore extends Adapter {
6565
const exists = await store.has(shardKey)
6666
if (!exists) {
6767
const put = typeof store.putRaw === 'function' ? store.putRaw.bind(store) : store.put.bind(store)
68-
return Promise.all([put(shardKey, Buffer.from(shard.toString() + '\n')),
69-
put(shardReadmeKey, Buffer.from(sh.readme))])
68+
return Promise.all([put(shardKey, utf8Encoder.encode(shard.toString() + '\n')),
69+
put(shardReadmeKey, utf8Encoder.encode(sh.readme))])
7070
}
7171

7272
const diskShard = await sh.readShardFun('/', store)

src/utils.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const TextEncoder = require('ipfs-utils/src/text-encoder')
4+
const TextDecoder = require('ipfs-utils/src/text-decoder')
5+
6+
module.exports.utf8Encoder = new TextEncoder('utf8')
7+
module.exports.utf8Decoder = new TextDecoder('utf8')

test/keytransform.spec.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
54
const chai = require('chai')
65
chai.use(require('dirty-chai'))
76
const expect = chai.expect
@@ -37,7 +36,7 @@ describe('KeyTransformDatastore', () => {
3736
'foo/bar/bazb',
3837
'foo/bar/baz/barb'
3938
].map((s) => new Key(s))
40-
await Promise.all(keys.map((key) => kStore.put(key, Buffer.from(key.toString()))))
39+
await Promise.all(keys.map((key) => kStore.put(key, key.uint8Array())))
4140
const kResults = Promise.all(keys.map((key) => kStore.get(key)))
4241
const mResults = Promise.all(keys.map((key) => mStore.get(new Key('abc').child(key))))
4342
const results = await Promise.all([kResults, mResults])

test/mount.spec.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
/* eslint max-nested-callbacks: ["error", 8] */
33
'use strict'
44

5-
const { Buffer } = require('buffer')
65
const chai = require('chai')
76
chai.use(require('dirty-chai'))
87
const assert = chai.assert
98
const expect = chai.expect
109
const all = require('async-iterator-all')
10+
const { utf8Encoder } = require('../src/utils')
1111

1212
const Key = require('interface-datastore').Key
1313
const MemoryStore = require('interface-datastore').MemoryDatastore
@@ -18,7 +18,7 @@ describe('MountStore', () => {
1818
it('put - no mount', async () => {
1919
const m = new MountStore([])
2020
try {
21-
await m.put(new Key('hello'), Buffer.from('foo'))
21+
await m.put(new Key('hello'), utf8Encoder.encode('foo'))
2222
assert(false, 'Failed to throw error on no mount')
2323
} catch (err) {
2424
expect(err).to.be.an('Error')
@@ -31,7 +31,7 @@ describe('MountStore', () => {
3131
prefix: new Key('cool')
3232
}])
3333
try {
34-
await m.put(new Key('/fail/hello'), Buffer.from('foo'))
34+
await m.put(new Key('/fail/hello'), utf8Encoder.encode('foo'))
3535
assert(false, 'Failed to throw error on wrong mount')
3636
} catch (err) {
3737
expect(err).to.be.an('Error')
@@ -45,7 +45,7 @@ describe('MountStore', () => {
4545
prefix: new Key('cool')
4646
}])
4747

48-
const val = Buffer.from('hello')
48+
const val = utf8Encoder.encode('hello')
4949
await m.put(new Key('/cool/hello'), val)
5050
const res = await mds.get(new Key('/hello'))
5151
expect(res).to.eql(val)
@@ -58,7 +58,7 @@ describe('MountStore', () => {
5858
prefix: new Key('cool')
5959
}])
6060

61-
const val = Buffer.from('hello')
61+
const val = utf8Encoder.encode('hello')
6262
await mds.put(new Key('/hello'), val)
6363
const res = await m.get(new Key('/cool/hello'))
6464
expect(res).to.eql(val)
@@ -71,7 +71,7 @@ describe('MountStore', () => {
7171
prefix: new Key('cool')
7272
}])
7373

74-
const val = Buffer.from('hello')
74+
const val = utf8Encoder.encode('hello')
7575
await mds.put(new Key('/hello'), val)
7676
const exists = await m.has(new Key('/cool/hello'))
7777
expect(exists).to.eql(true)
@@ -84,7 +84,7 @@ describe('MountStore', () => {
8484
prefix: new Key('cool')
8585
}])
8686

87-
const val = Buffer.from('hello')
87+
const val = utf8Encoder.encode('hello')
8888
await m.put(new Key('/cool/hello'), val)
8989
await m.delete(new Key('/cool/hello'))
9090
let exists = await m.has(new Key('/cool/hello'))
@@ -100,7 +100,7 @@ describe('MountStore', () => {
100100
prefix: new Key('cool')
101101
}])
102102

103-
const val = Buffer.from('hello')
103+
const val = utf8Encoder.encode('hello')
104104
await m.put(new Key('/cool/hello'), val)
105105
const res = await all(m.query({ prefix: '/cool' }))
106106
expect(res).to.eql([{ key: new Key('/cool/hello'), value: val }])

test/namespace.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
54
const chai = require('chai')
65
chai.use(require('dirty-chai'))
76
const expect = chai.expect
@@ -11,6 +10,7 @@ const MemoryStore = require('interface-datastore').MemoryDatastore
1110

1211
const NamespaceStore = require('../src/').NamespaceDatastore
1312
const all = require('async-iterator-all')
13+
const { utf8Encoder } = require('../src/utils')
1414

1515
describe('KeyTransformDatastore', () => {
1616
const prefixes = [
@@ -30,7 +30,7 @@ describe('KeyTransformDatastore', () => {
3030
'foo/bar/baz/barb'
3131
].map((s) => new Key(s))
3232

33-
await Promise.all(keys.map(key => store.put(key, Buffer.from(key.toString()))))
33+
await Promise.all(keys.map(key => store.put(key, utf8Encoder.encode(key.toString()))))
3434
const nResults = Promise.all(keys.map((key) => store.get(key)))
3535
const mResults = Promise.all(keys.map((key) => mStore.get(new Key(prefix).child(key))))
3636
const results = await Promise.all([nResults, mResults])

test/sharding.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
54
const chai = require('chai')
65
chai.use(require('dirty-chai'))
76
const expect = chai.expect
@@ -12,15 +11,16 @@ const MemoryStore = require('interface-datastore').MemoryDatastore
1211

1312
const ShardingStore = require('../src').ShardingDatastore
1413
const sh = require('../src').shard
14+
const { utf8Encoder, utf8Decoder } = require('../src/utils')
1515

1616
describe('ShardingStore', () => {
1717
it('create', async () => {
1818
const ms = new MemoryStore()
1919
const shard = new sh.NextToLast(2)
2020
await ShardingStore.create(ms, shard)
2121
const res = await Promise.all([ms.get(new Key(sh.SHARDING_FN)), ms.get(new Key(sh.README_FN))])
22-
expect(res[0].toString()).to.eql(shard.toString() + '\n')
23-
expect(res[1].toString()).to.eql(sh.readme)
22+
expect(utf8Decoder.decode(res[0])).to.eql(shard.toString() + '\n')
23+
expect(utf8Decoder.decode(res[1])).to.eql(sh.readme)
2424
})
2525

2626
it('open - empty', async () => {
@@ -47,9 +47,9 @@ describe('ShardingStore', () => {
4747
const store = await ShardingStore.createOrOpen(ms, shard)
4848
expect(store).to.exist()
4949
await ShardingStore.createOrOpen(ms, shard)
50-
await store.put(new Key('hello'), Buffer.from('test'))
50+
await store.put(new Key('hello'), utf8Encoder.encode('test'))
5151
const res = await ms.get(new Key('ll').child(new Key('hello')))
52-
expect(res).to.eql(Buffer.from('test'))
52+
expect(res).to.eql(utf8Encoder.encode('test'))
5353
})
5454

5555
describe('interface-datastore', () => {

test/tiered.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
54
const chai = require('chai')
65
chai.use(require('dirty-chai'))
76
const expect = chai.expect
@@ -10,6 +9,7 @@ const Key = require('interface-datastore').Key
109
const MemoryStore = require('interface-datastore').MemoryDatastore
1110

1211
const TieredStore = require('../src').TieredDatastore
12+
const { utf8Encoder } = require('../src/utils')
1313

1414
describe('Tiered', () => {
1515
describe('all stores', () => {
@@ -23,7 +23,7 @@ describe('Tiered', () => {
2323

2424
it('put', async () => {
2525
const k = new Key('hello')
26-
const v = Buffer.from('world')
26+
const v = utf8Encoder.encode('world')
2727
await store.put(k, v)
2828
const res = await Promise.all([ms[0].get(k), ms[1].get(k)])
2929
res.forEach((val) => {
@@ -33,7 +33,7 @@ describe('Tiered', () => {
3333

3434
it('get and has, where available', async () => {
3535
const k = new Key('hello')
36-
const v = Buffer.from('world')
36+
const v = utf8Encoder.encode('world')
3737
await ms[1].put(k, v)
3838
const val = await store.get(k)
3939
expect(val).to.be.eql(v)
@@ -47,7 +47,7 @@ describe('Tiered', () => {
4747

4848
it('has and delete', async () => {
4949
const k = new Key('hello')
50-
const v = Buffer.from('world')
50+
const v = utf8Encoder.encode('world')
5151
await store.put(k, v)
5252
let res = await Promise.all([ms[0].has(k), ms[1].has(k)])
5353
expect(res).to.be.eql([true, true])

0 commit comments

Comments
 (0)