From 9d5c6181d7d05035f0a137575b2cb3f2ba8c5771 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 11 Dec 2020 13:06:44 +0000 Subject: [PATCH 01/11] feat: types Adds types support --- .github/workflows/main.yml | 67 ++++++++++++++++++++++++++++++++++++++ .npmignore | 41 ----------------------- .travis.yml | 29 ----------------- package.json | 14 ++++++-- src/index.js | 23 ++++++++++--- test/index.spec.js | 2 ++ tsconfig.json | 10 ++++++ 7 files changed, 108 insertions(+), 78 deletions(-) create mode 100644 .github/workflows/main.yml delete mode 100644 .npmignore delete mode 100644 .travis.yml create mode 100644 tsconfig.json diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..9e81b21 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,67 @@ +name: ci +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: npx aegir lint + - uses: gozala/typescript-error-reporter-action@v1.0.8 + - run: npx aegir build + - run: npx aegir dep-check + - uses: ipfs/aegir/actions/bundle-size@master + name: size + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + test-node: + needs: check + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [windows-latest, ubuntu-latest, macos-latest] + node: [12, 14] + fail-fast: true + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node }} + - run: npm install + - run: npx nyc --reporter=lcov aegir test -t node -- --bail + - uses: codecov/codecov-action@v1 + test-chrome: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: npx aegir test -t browser -t webworker --bail + test-firefox: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless + test-electron-main: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: npx xvfb-maybe aegir test -t electron-main --bail + test-electron-renderer: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: npx xvfb-maybe aegir test -t electron-renderer --bail \ No newline at end of file diff --git a/.npmignore b/.npmignore deleted file mode 100644 index c630e7c..0000000 --- a/.npmignore +++ /dev/null @@ -1,41 +0,0 @@ -yarn.lock -package-lock.json -**/node_modules/ -**/*.log -test/repo-tests* - -# Logs -logs -*.log - -coverage - -# Runtime data -pids -*.pid -*.seed - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage - -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# node-waf configuration -.lock-wscript - -build - -# Dependency directory -# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git -node_modules - -lib -dist -docs -test -.github -.travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 8c47422..0000000 --- a/.travis.yml +++ /dev/null @@ -1,29 +0,0 @@ -language: node_js -cache: npm -stages: - - check - - test - - cov - -node_js: - - 'lts/*' - - 'node' - -os: - - linux - - osx - - windows - -script: npx nyc -s npm run test -- --bail -after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov - -jobs: - include: - - stage: check - script: - - npx aegir commitlint --travis - - npx aegir dep-check - - npm run lint - -notifications: - email: false diff --git a/package.json b/package.json index 970b883..e6aa3c0 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,14 @@ "description": "Datastore implementation with file system backend", "leadMaintainer": "Alex Potsides ", "main": "src/index.js", + "types": "dist/src/index.d.ts", + "typesVersions": { + "*": { "src/*": ["dist/src/*", "dist/src/*/index"] } + }, + "files": [ + "src", + "dist" + ], "scripts": { "test": "aegir test -t node", "build": "aegir build", @@ -32,14 +40,14 @@ }, "homepage": "https://github.com/ipfs/js-datastore-fs#readme", "dependencies": { - "datastore-core": "^2.0.0", + "datastore-core": "ipfs/js-datastore-core#feat/ts-types", "fast-write-atomic": "^0.2.0", - "interface-datastore": "^2.0.0", + "interface-datastore": "ipfs/interface-datastore#feat/ts-types", "it-glob": "0.0.10", "mkdirp": "^1.0.4" }, "devDependencies": { - "aegir": "^28.1.0", + "aegir": "ipfs/aegir#feat/ts-4.1.x", "async-iterator-all": "^1.0.0", "cids": "^1.0.0", "detect-node": "^2.0.4", diff --git a/src/index.js b/src/index.js index ddea56c..60b5e3c 100644 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,17 @@ const fsAccess = promisify(fs.access || noop) const fsReadFile = promisify(fs.readFile || noop) const fsUnlink = promisify(fs.unlink || noop) +/** + * @typedef {import('interface-datastore/src/types').Datastore} Datastore + * @typedef {import('interface-datastore/src/types').Pair} Pair + * @typedef {import('interface-datastore/src/key')} Key + */ + +/** + * Write a file atomically + * @param {string} path + * @param {Uint8Array} contents + */ async function writeFile (path, contents) { try { await writeAtomic(path, contents) @@ -42,6 +53,8 @@ async function writeFile (path, contents) { * * Keys need to be sanitized before use, as they are written * to the file system as is. + * + * @implements {Datastore} */ class FsDatastore extends Adapter { constructor (location, opts) { @@ -55,7 +68,7 @@ class FsDatastore extends Adapter { }, opts) } - open () { + async open () { try { if (!fs.existsSync(this.path)) { throw Errors.notFoundError(new Error(`Datastore directory: ${this.path} does not exist`)) @@ -79,7 +92,7 @@ class FsDatastore extends Adapter { * * @private * @param {Key} key - * @returns {{string, string}} + * @returns {{dir:string, file:string}} */ _encode (key) { const parent = key.parent().toString() @@ -194,7 +207,7 @@ class FsDatastore extends Adapter { * Check for the existence of the given key. * * @param {Key} key - * @returns {Promise} + * @returns {Promise} */ async has (key) { const parts = this._encode(key) @@ -225,7 +238,7 @@ class FsDatastore extends Adapter { } } - async * _all (q) { // eslint-disable-line require-await + async * _all (q) { let prefix = q.prefix || '**' // strip leading slashes @@ -256,7 +269,7 @@ class FsDatastore extends Adapter { } } } else { - yield * map(files, f => ({ key: this._decode(f) })) + yield * map(files, f => /** @type {Pair} */({ key: this._decode(f) })) } } } diff --git a/test/index.spec.js b/test/index.spec.js index 8670415..10fcc26 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -69,6 +69,7 @@ describe('FsDatastore', () => { const fs = new FsStore(dir) expect( + // @ts-ignore fs._encode(new Key('hello/world')) ).to.eql({ dir: path.join(dir, 'hello'), @@ -76,6 +77,7 @@ describe('FsDatastore', () => { }) expect( + // @ts-ignore fs._decode(fs._encode(new Key('hello/world/test:other')).file) ).to.eql( new Key('hello/world/test:other') diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..e605b61 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "./node_modules/aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": [ + "test", // remove this line if you don't want to type-check tests + "src" + ] +} From a8fd7874420cc7bc6bb382f0754277f389e21310 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 11 Dec 2020 13:19:51 +0000 Subject: [PATCH 02/11] fix: add eslint config --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index e6aa3c0..9ee27ac 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,9 @@ "memdown": "^5.1.0", "rimraf": "^3.0.2" }, + "eslintConfig": { + "extends": "ipfs" + }, "contributors": [ "achingbrain ", "David Dias ", From 6b9ab5d0dc56b3b7eb13ecdec999c66546417f04 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 11 Dec 2020 14:50:14 +0000 Subject: [PATCH 03/11] fix: lint --- src/index.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 60b5e3c..a607440 100644 --- a/src/index.js +++ b/src/index.js @@ -25,8 +25,9 @@ const fsUnlink = promisify(fs.unlink || noop) /** * Write a file atomically - * @param {string} path - * @param {Uint8Array} contents + * + * @param {string} path + * @param {Uint8Array} contents */ async function writeFile (path, contents) { try { @@ -53,7 +54,7 @@ async function writeFile (path, contents) { * * Keys need to be sanitized before use, as they are written * to the file system as is. - * + * * @implements {Datastore} */ class FsDatastore extends Adapter { @@ -68,7 +69,7 @@ class FsDatastore extends Adapter { }, opts) } - async open () { + open () { try { if (!fs.existsSync(this.path)) { throw Errors.notFoundError(new Error(`Datastore directory: ${this.path} does not exist`)) @@ -77,10 +78,11 @@ class FsDatastore extends Adapter { if (this.opts.errorIfExists) { throw Errors.dbOpenFailedError(new Error(`Datastore directory: ${this.path} already exists`)) } + return Promise.resolve() } catch (err) { if (err.code === 'ERR_NOT_FOUND' && this.opts.createIfMissing) { mkdirp.sync(this.path, { fs: fs }) - return + return Promise.resolve() } throw err From afccf05a392bac4f2a66d5150c3b1aaffa62e8f6 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Sat, 16 Jan 2021 16:13:09 +0000 Subject: [PATCH 04/11] fix: update aegir and fix types --- package.json | 11 ++++------- src/index.js | 21 ++++++++++++++++++--- test/index.spec.js | 14 +++++++++----- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 9ee27ac..5e98479 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,6 @@ "leadMaintainer": "Alex Potsides ", "main": "src/index.js", "types": "dist/src/index.d.ts", - "typesVersions": { - "*": { "src/*": ["dist/src/*", "dist/src/*/index"] } - }, "files": [ "src", "dist" @@ -42,16 +39,16 @@ "dependencies": { "datastore-core": "ipfs/js-datastore-core#feat/ts-types", "fast-write-atomic": "^0.2.0", - "interface-datastore": "ipfs/interface-datastore#feat/ts-types", + "interface-datastore": "ipfs/interface-datastore#chore/export-types-from-entrypoint", "it-glob": "0.0.10", "mkdirp": "^1.0.4" }, "devDependencies": { - "aegir": "ipfs/aegir#feat/ts-4.1.x", + "aegir": "^30.3.0", "async-iterator-all": "^1.0.0", - "cids": "^1.0.0", + "cids": "^1.1.5", "detect-node": "^2.0.4", - "ipfs-utils": "^4.0.1", + "ipfs-utils": "^6.0.0", "memdown": "^5.1.0", "rimraf": "^3.0.2" }, diff --git a/src/index.js b/src/index.js index a607440..1fdc676 100644 --- a/src/index.js +++ b/src/index.js @@ -2,8 +2,10 @@ const fs = require('fs') const glob = require('it-glob') +// @ts-ignore const mkdirp = require('mkdirp') const promisify = require('util').promisify +// @ts-ignore const writeAtomic = promisify(require('fast-write-atomic')) const path = require('path') const { @@ -18,9 +20,9 @@ const fsReadFile = promisify(fs.readFile || noop) const fsUnlink = promisify(fs.unlink || noop) /** - * @typedef {import('interface-datastore/src/types').Datastore} Datastore - * @typedef {import('interface-datastore/src/types').Pair} Pair - * @typedef {import('interface-datastore/src/key')} Key + * @typedef {import('interface-datastore').Datastore} Datastore + * @typedef {import('interface-datastore').Pair} Pair + * @typedef {import('interface-datastore').Query} Query */ /** @@ -58,6 +60,10 @@ async function writeFile (path, contents) { * @implements {Datastore} */ class FsDatastore extends Adapter { + /** + * @param {string} location + * @param {{ createIfMissing?: boolean; errorIfExists?: boolean; extension?: string; } | undefined} [opts] + */ constructor (location, opts) { super() @@ -89,6 +95,10 @@ class FsDatastore extends Adapter { } } + close () { + return Promise.resolve() + } + /** * Calculate the directory and file name for a given key. * @@ -240,6 +250,11 @@ class FsDatastore extends Adapter { } } + /** + * + * @param {Query} q + * @returns {AsyncIterable} + */ async * _all (q) { let prefix = q.prefix || '**' diff --git a/test/index.spec.js b/test/index.spec.js index 10fcc26..5a5d7dc 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -4,18 +4,22 @@ const { expect } = require('aegir/utils/chai') const path = require('path') const promisify = require('util').promisify -const noop = () => {} +// @ts-ignore const mkdirp = require('mkdirp') +// @ts-ignore const rimraf = promisify(require('rimraf')) const fs = require('fs') +const noop = () => {} const fsReadFile = promisify(require('fs').readFile || noop) const Key = require('interface-datastore').Key const utils = require('interface-datastore').utils const ShardingStore = require('datastore-core').ShardingDatastore const sh = require('datastore-core').shard -const isNode = require('detect-node') const TextEncoder = require('ipfs-utils/src/text-encoder') -const utf8Encoder = new TextEncoder('utf8') +const { isNode } = require('ipfs-utils/src/env') +const utf8Encoder = new TextEncoder() +// @ts-ignore +const tests = require('interface-datastore/src/tests') const FsStore = require('../src') @@ -155,7 +159,7 @@ describe('FsDatastore', () => { describe('interface-datastore', () => { const dir = utils.tmpdir() - require('interface-datastore/src/tests')({ + tests({ setup: () => { return new FsStore(dir) }, @@ -168,7 +172,7 @@ describe('FsDatastore', () => { describe('interface-datastore (sharding(fs))', () => { const dir = utils.tmpdir() - require('interface-datastore/src/tests')({ + tests({ setup: () => { const shard = new sh.NextToLast(2) return ShardingStore.createOrOpen(new FsStore(dir), shard) From 740fe0aac6a52286e3d939fea62636b4efb2db97 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 18 Jan 2021 19:21:13 +0000 Subject: [PATCH 05/11] fix: remove gh url --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5e98479..a14b25e 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "dependencies": { "datastore-core": "ipfs/js-datastore-core#feat/ts-types", "fast-write-atomic": "^0.2.0", - "interface-datastore": "ipfs/interface-datastore#chore/export-types-from-entrypoint", + "interface-datastore": "^3.0.1", "it-glob": "0.0.10", "mkdirp": "^1.0.4" }, From 13fdabe6ebd417eec5c552f2d93915df8b5c70ae Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 19 Jan 2021 12:39:41 +0000 Subject: [PATCH 06/11] fix: add prepare --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index a14b25e..e0a9557 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "dist" ], "scripts": { + "prepare": "aegir build --no-bundle", "test": "aegir test -t node", "build": "aegir build", "lint": "aegir lint", From 7585d523278f595bd1c899d8f09d11dfe9529182 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 21 Jan 2021 19:29:21 +0000 Subject: [PATCH 07/11] fix: fix gh url --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e0a9557..c341039 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ }, "homepage": "https://github.com/ipfs/js-datastore-fs#readme", "dependencies": { - "datastore-core": "ipfs/js-datastore-core#feat/ts-types", + "datastore-core": "ipfs/js-datastore-core#bee45ae", "fast-write-atomic": "^0.2.0", "interface-datastore": "^3.0.1", "it-glob": "0.0.10", From 52824d696f3d99f8e730ee4ccc6fe6c6ef4ef780 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 22 Jan 2021 18:18:55 +0000 Subject: [PATCH 08/11] fix: remove gh url --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c341039..99458be 100644 --- a/package.json +++ b/package.json @@ -38,9 +38,9 @@ }, "homepage": "https://github.com/ipfs/js-datastore-fs#readme", "dependencies": { - "datastore-core": "ipfs/js-datastore-core#bee45ae", + "datastore-core": "^3.0.0", "fast-write-atomic": "^0.2.0", - "interface-datastore": "^3.0.1", + "interface-datastore": "^3.0.3", "it-glob": "0.0.10", "mkdirp": "^1.0.4" }, From 55be3b667cdfe541c3dbbf03829f2be4c229a24d Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 22 Jan 2021 18:27:52 +0000 Subject: [PATCH 09/11] fix: remove bundle from ci --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9e81b21..8971af8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,7 +15,7 @@ jobs: - run: npm install - run: npx aegir lint - uses: gozala/typescript-error-reporter-action@v1.0.8 - - run: npx aegir build + - run: npx aegir build --no-bundle - run: npx aegir dep-check - uses: ipfs/aegir/actions/bundle-size@master name: size From 1669255916cab108e595be353eaa14cf21bcf3e2 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 22 Jan 2021 18:30:26 +0000 Subject: [PATCH 10/11] fix: remove bundle-size --- .github/workflows/main.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8971af8..bc4da7a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,10 +17,6 @@ jobs: - uses: gozala/typescript-error-reporter-action@v1.0.8 - run: npx aegir build --no-bundle - run: npx aegir dep-check - - uses: ipfs/aegir/actions/bundle-size@master - name: size - with: - github_token: ${{ secrets.GITHUB_TOKEN }} test-node: needs: check runs-on: ${{ matrix.os }} From 164aa486dc7291f03bc3c6cc7155aaebc6d6f19f Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 22 Jan 2021 19:00:49 +0000 Subject: [PATCH 11/11] fix: remove ci browser --- .github/workflows/main.yml | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bc4da7a..9ae2650 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -33,20 +33,6 @@ jobs: - run: npm install - run: npx nyc --reporter=lcov aegir test -t node -- --bail - uses: codecov/codecov-action@v1 - test-chrome: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: npx aegir test -t browser -t webworker --bail - test-firefox: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless test-electron-main: needs: check runs-on: ubuntu-latest