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

Commit 887b762

Browse files
authored
fix: remove node buffers (#44)
- Updates to the latest changes from interface-datastore and datastore-core - Uses it-glob to not buffer datastore paths during queries BREAKING CHANGE: only uses Uint8Arrays internally
1 parent 9963b95 commit 887b762

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

package.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
"leadMaintainer": "Alex Potsides <[email protected]>",
66
"main": "src/index.js",
77
"scripts": {
8-
"test": "aegir test",
9-
"test:node": "aegir test -t node",
10-
"test:browser": "aegir test -t browser",
11-
"test:webworker": "aegir test -t webworker",
8+
"test": "aegir test -t node",
129
"build": "aegir build",
1310
"lint": "aegir lint",
1411
"release": "aegir release",
@@ -35,19 +32,20 @@
3532
},
3633
"homepage": "https://github.com/ipfs/js-datastore-fs#readme",
3734
"dependencies": {
38-
"datastore-core": "^1.1.0",
35+
"datastore-core": "^2.0.0",
3936
"fast-write-atomic": "^0.2.0",
40-
"glob": "^7.1.3",
41-
"interface-datastore": "^1.0.2",
37+
"interface-datastore": "^2.0.0",
38+
"it-glob": "0.0.8",
4239
"mkdirp": "^1.0.4"
4340
},
4441
"devDependencies": {
45-
"aegir": "^22.0.0",
42+
"aegir": "^25.0.0",
4643
"async-iterator-all": "^1.0.0",
4744
"chai": "^4.2.0",
48-
"cids": "~0.8.0",
45+
"cids": "^0.8.3",
4946
"detect-node": "^2.0.4",
5047
"dirty-chai": "^2.0.1",
48+
"ipfs-utils": "^2.3.1",
5149
"memdown": "^5.1.0",
5250
"rimraf": "^3.0.2"
5351
},

src/index.js

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

33
const fs = require('fs')
4-
const glob = require('glob')
4+
const glob = require('it-glob')
55
const mkdirp = require('mkdirp')
66
const promisify = require('util').promisify
77
const writeAtomic = promisify(require('fast-write-atomic'))
@@ -117,7 +117,7 @@ class FsDatastore extends Adapter {
117117
* Write to the file system without extension.
118118
*
119119
* @param {Key} key
120-
* @param {Buffer} val
120+
* @param {Uint8Array} val
121121
* @returns {Promise<void>}
122122
*/
123123
async putRaw (key, val) {
@@ -131,11 +131,12 @@ class FsDatastore extends Adapter {
131131
* Store the given value under the key
132132
*
133133
* @param {Key} key
134-
* @param {Buffer} val
134+
* @param {Uint8Array} val
135135
* @returns {Promise<void>}
136136
*/
137137
async put (key, val) {
138138
const parts = this._encode(key)
139+
139140
try {
140141
await mkdirp(parts.dir, { fs: fs })
141142
await writeFile(parts.file, val)
@@ -148,7 +149,7 @@ class FsDatastore extends Adapter {
148149
* Read from the file system without extension.
149150
*
150151
* @param {Key} key
151-
* @returns {Promise<Buffer>}
152+
* @returns {Promise<Uint8Array>}
152153
*/
153154
async getRaw (key) {
154155
const parts = this._encode(key)
@@ -167,7 +168,7 @@ class FsDatastore extends Adapter {
167168
* Read from the file system.
168169
*
169170
* @param {Key} key
170-
* @returns {Promise<Buffer>}
171+
* @returns {Promise<Uint8Array>}
171172
*/
172173
async get (key) {
173174
const parts = this._encode(key)
@@ -216,22 +217,35 @@ class FsDatastore extends Adapter {
216217
}
217218

218219
async * _all (q) { // eslint-disable-line require-await
219-
// glob expects a POSIX path
220-
const prefix = q.prefix || '**'
221-
const pattern = path
222-
.join(this.path, prefix, '*' + this.opts.extension)
220+
let prefix = q.prefix || '**'
221+
222+
// strip leading slashes
223+
prefix = prefix.replace(/^\/+/, '')
224+
225+
const pattern = `${prefix}/*${this.opts.extension}`
223226
.split(path.sep)
224227
.join('/')
225-
const files = glob.sync(pattern)
228+
const files = glob(this.path, pattern, {
229+
absolute: true
230+
})
226231

227232
if (!q.keysOnly) {
228-
yield * map(files, async (f) => {
229-
const buf = await fsReadFile(f)
230-
return {
231-
key: this._decode(f),
232-
value: buf
233+
for await (const file of files) {
234+
try {
235+
const buf = await fsReadFile(file)
236+
237+
yield {
238+
key: this._decode(file),
239+
value: buf
240+
}
241+
} catch (err) {
242+
// if keys are removed from the datastore while the query is
243+
// running, we may encounter missing files.
244+
if (err.code !== 'ENOENT') {
245+
throw err
246+
}
233247
}
234-
})
248+
}
235249
} else {
236250
yield * map(files, f => ({ key: this._decode(f) }))
237251
}

test/index.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const utils = require('interface-datastore').utils
1616
const ShardingStore = require('datastore-core').ShardingDatastore
1717
const sh = require('datastore-core').shard
1818
const isNode = require('detect-node')
19+
const TextEncoder = require('ipfs-utils/src/text-encoder')
20+
const utf8Encoder = new TextEncoder('utf8')
1921

2022
const FsStore = require('../src')
2123

@@ -87,7 +89,7 @@ describe('FsDatastore', () => {
8789
const fs = new FsStore(dir)
8890
const key = new Key('1234')
8991

90-
await fs.put(key, Buffer.from([0, 1, 2, 3]))
92+
await fs.put(key, Uint8Array.from([0, 1, 2, 3]))
9193
await fs.delete(key)
9294

9395
try {
@@ -181,7 +183,7 @@ describe('FsDatastore', () => {
181183
const dir = utils.tmpdir()
182184
const fstore = new FsStore(dir)
183185
const key = new Key('CIQGFTQ7FSI2COUXWWLOQ45VUM2GUZCGAXLWCTOKKPGTUWPXHBNIVOY')
184-
const value = Buffer.from('Hello world')
186+
const value = utf8Encoder.encode('Hello world')
185187

186188
await Promise.all(
187189
new Array(100).fill(0).map(() => fstore.put(key, value))

0 commit comments

Comments
 (0)