From 9e3e042e1fdee2d75dfa8d0e0e7ad66119a89ef4 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Sat, 15 Aug 2020 08:50:14 +0100 Subject: [PATCH] fix: only remove extension from path when it is non-empty (#47) The default config for root datastores is to have an empty string extension, but if you use putRaw or getRaw as the sharded datastore does, stripping the extension can cause the final file path to be an empty string, so guard on the extention not having a valid length. --- package.json | 6 ++---- src/index.js | 13 +++++++++++-- test/index.spec.js | 19 ++++++++++++++++--- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 4899ab1..45ed051 100644 --- a/package.json +++ b/package.json @@ -39,12 +39,10 @@ "mkdirp": "^1.0.4" }, "devDependencies": { - "aegir": "^25.0.0", + "aegir": "^26.0.0", "async-iterator-all": "^1.0.0", - "chai": "^4.2.0", - "cids": "^0.8.3", + "cids": "^1.0.0", "detect-node": "^2.0.4", - "dirty-chai": "^2.0.1", "ipfs-utils": "^2.3.1", "memdown": "^5.1.0", "rimraf": "^3.0.2" diff --git a/src/index.js b/src/index.js index 6644ea1..ddea56c 100644 --- a/src/index.js +++ b/src/index.js @@ -122,7 +122,12 @@ class FsDatastore extends Adapter { */ async putRaw (key, val) { const parts = this._encode(key) - const file = parts.file.slice(0, -this.opts.extension.length) + let file = parts.file + + if (this.opts.extension.length) { + file = parts.file.slice(0, -this.opts.extension.length) + } + await mkdirp(parts.dir, { fs: fs }) await writeFile(file, val) } @@ -154,7 +159,11 @@ class FsDatastore extends Adapter { async getRaw (key) { const parts = this._encode(key) let file = parts.file - file = file.slice(0, -this.opts.extension.length) + + if (this.opts.extension.length) { + file = file.slice(0, -this.opts.extension.length) + } + let data try { data = await fsReadFile(file) diff --git a/test/index.spec.js b/test/index.spec.js index 8e18a30..8670415 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -1,9 +1,7 @@ /* eslint-env mocha */ 'use strict' -const chai = require('chai') -chai.use(require('dirty-chai')) -const expect = chai.expect +const { expect } = require('aegir/utils/chai') const path = require('path') const promisify = require('util').promisify const noop = () => {} @@ -193,4 +191,19 @@ describe('FsDatastore', () => { expect(res).to.deep.equal(value) }) + + it('can survive putRaw and getRaw with an empty extension', async () => { + const dir = utils.tmpdir() + const fstore = new FsStore(dir, { + extension: '' + }) + const key = new Key('CIQGFTQ7FSI2COUXWWLOQ45VUM2GUZCGAXLWCTOKKPGTUWPXHBNIVOY') + const value = utf8Encoder.encode('Hello world') + + await fstore.putRaw(key, value) + + const res = await fstore.getRaw(key) + + expect(res).to.deep.equal(value) + }) })