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

Commit de519dd

Browse files
authored
feat: types (#62)
Adds types support
1 parent b7a1099 commit de519dd

File tree

7 files changed

+119
-85
lines changed

7 files changed

+119
-85
lines changed

.github/workflows/main.yml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: ci
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
branches:
8+
- master
9+
10+
jobs:
11+
check:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
- run: npm install
16+
- run: npx aegir lint
17+
- uses: gozala/[email protected]
18+
- run: npx aegir build --no-bundle
19+
- run: npx aegir dep-check
20+
test-node:
21+
needs: check
22+
runs-on: ${{ matrix.os }}
23+
strategy:
24+
matrix:
25+
os: [windows-latest, ubuntu-latest, macos-latest]
26+
node: [12, 14]
27+
fail-fast: true
28+
steps:
29+
- uses: actions/checkout@v2
30+
- uses: actions/setup-node@v1
31+
with:
32+
node-version: ${{ matrix.node }}
33+
- run: npm install
34+
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
35+
- uses: codecov/codecov-action@v1
36+
test-electron-main:
37+
needs: check
38+
runs-on: ubuntu-latest
39+
steps:
40+
- uses: actions/checkout@v2
41+
- run: npm install
42+
- run: npx xvfb-maybe aegir test -t electron-main --bail
43+
test-electron-renderer:
44+
needs: check
45+
runs-on: ubuntu-latest
46+
steps:
47+
- uses: actions/checkout@v2
48+
- run: npm install
49+
- run: npx xvfb-maybe aegir test -t electron-renderer --bail

.npmignore

-41
This file was deleted.

.travis.yml

-29
This file was deleted.

package.json

+14-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
"description": "Datastore implementation with file system backend",
55
"leadMaintainer": "Alex Potsides <[email protected]>",
66
"main": "src/index.js",
7+
"types": "dist/src/index.d.ts",
8+
"files": [
9+
"src",
10+
"dist"
11+
],
712
"scripts": {
13+
"prepare": "aegir build --no-bundle",
814
"test": "aegir test -t node",
915
"build": "aegir build",
1016
"lint": "aegir lint",
@@ -32,21 +38,24 @@
3238
},
3339
"homepage": "https://github.com/ipfs/js-datastore-fs#readme",
3440
"dependencies": {
35-
"datastore-core": "^2.0.0",
41+
"datastore-core": "^3.0.0",
3642
"fast-write-atomic": "^0.2.0",
37-
"interface-datastore": "^2.0.0",
43+
"interface-datastore": "^3.0.3",
3844
"it-glob": "0.0.10",
3945
"mkdirp": "^1.0.4"
4046
},
4147
"devDependencies": {
42-
"aegir": "^28.1.0",
48+
"aegir": "^30.3.0",
4349
"async-iterator-all": "^1.0.0",
44-
"cids": "^1.0.0",
50+
"cids": "^1.1.5",
4551
"detect-node": "^2.0.4",
46-
"ipfs-utils": "^4.0.1",
52+
"ipfs-utils": "^6.0.0",
4753
"memdown": "^5.1.0",
4854
"rimraf": "^3.0.2"
4955
},
56+
"eslintConfig": {
57+
"extends": "ipfs"
58+
},
5059
"contributors": [
5160
"achingbrain <[email protected]>",
5261
"David Dias <[email protected]>",

src/index.js

+35-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
const fs = require('fs')
44
const glob = require('it-glob')
5+
// @ts-ignore
56
const mkdirp = require('mkdirp')
67
const promisify = require('util').promisify
8+
// @ts-ignore
79
const writeAtomic = promisify(require('fast-write-atomic'))
810
const path = require('path')
911
const {
@@ -17,6 +19,18 @@ const fsAccess = promisify(fs.access || noop)
1719
const fsReadFile = promisify(fs.readFile || noop)
1820
const fsUnlink = promisify(fs.unlink || noop)
1921

22+
/**
23+
* @typedef {import('interface-datastore').Datastore} Datastore
24+
* @typedef {import('interface-datastore').Pair} Pair
25+
* @typedef {import('interface-datastore').Query} Query
26+
*/
27+
28+
/**
29+
* Write a file atomically
30+
*
31+
* @param {string} path
32+
* @param {Uint8Array} contents
33+
*/
2034
async function writeFile (path, contents) {
2135
try {
2236
await writeAtomic(path, contents)
@@ -42,8 +56,14 @@ async function writeFile (path, contents) {
4256
*
4357
* Keys need to be sanitized before use, as they are written
4458
* to the file system as is.
59+
*
60+
* @implements {Datastore}
4561
*/
4662
class FsDatastore extends Adapter {
63+
/**
64+
* @param {string} location
65+
* @param {{ createIfMissing?: boolean; errorIfExists?: boolean; extension?: string; } | undefined} [opts]
66+
*/
4767
constructor (location, opts) {
4868
super()
4969

@@ -64,22 +84,27 @@ class FsDatastore extends Adapter {
6484
if (this.opts.errorIfExists) {
6585
throw Errors.dbOpenFailedError(new Error(`Datastore directory: ${this.path} already exists`))
6686
}
87+
return Promise.resolve()
6788
} catch (err) {
6889
if (err.code === 'ERR_NOT_FOUND' && this.opts.createIfMissing) {
6990
mkdirp.sync(this.path, { fs: fs })
70-
return
91+
return Promise.resolve()
7192
}
7293

7394
throw err
7495
}
7596
}
7697

98+
close () {
99+
return Promise.resolve()
100+
}
101+
77102
/**
78103
* Calculate the directory and file name for a given key.
79104
*
80105
* @private
81106
* @param {Key} key
82-
* @returns {{string, string}}
107+
* @returns {{dir:string, file:string}}
83108
*/
84109
_encode (key) {
85110
const parent = key.parent().toString()
@@ -194,7 +219,7 @@ class FsDatastore extends Adapter {
194219
* Check for the existence of the given key.
195220
*
196221
* @param {Key} key
197-
* @returns {Promise<bool>}
222+
* @returns {Promise<boolean>}
198223
*/
199224
async has (key) {
200225
const parts = this._encode(key)
@@ -225,7 +250,12 @@ class FsDatastore extends Adapter {
225250
}
226251
}
227252

228-
async * _all (q) { // eslint-disable-line require-await
253+
/**
254+
*
255+
* @param {Query} q
256+
* @returns {AsyncIterable<Pair>}
257+
*/
258+
async * _all (q) {
229259
let prefix = q.prefix || '**'
230260

231261
// strip leading slashes
@@ -256,7 +286,7 @@ class FsDatastore extends Adapter {
256286
}
257287
}
258288
} else {
259-
yield * map(files, f => ({ key: this._decode(f) }))
289+
yield * map(files, f => /** @type {Pair} */({ key: this._decode(f) }))
260290
}
261291
}
262292
}

test/index.spec.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44
const { expect } = require('aegir/utils/chai')
55
const path = require('path')
66
const promisify = require('util').promisify
7-
const noop = () => {}
7+
// @ts-ignore
88
const mkdirp = require('mkdirp')
9+
// @ts-ignore
910
const rimraf = promisify(require('rimraf'))
1011
const fs = require('fs')
12+
const noop = () => {}
1113
const fsReadFile = promisify(require('fs').readFile || noop)
1214
const Key = require('interface-datastore').Key
1315
const utils = require('interface-datastore').utils
1416
const ShardingStore = require('datastore-core').ShardingDatastore
1517
const sh = require('datastore-core').shard
16-
const isNode = require('detect-node')
1718
const TextEncoder = require('ipfs-utils/src/text-encoder')
18-
const utf8Encoder = new TextEncoder('utf8')
19+
const { isNode } = require('ipfs-utils/src/env')
20+
const utf8Encoder = new TextEncoder()
21+
// @ts-ignore
22+
const tests = require('interface-datastore/src/tests')
1923

2024
const FsStore = require('../src')
2125

@@ -69,13 +73,15 @@ describe('FsDatastore', () => {
6973
const fs = new FsStore(dir)
7074

7175
expect(
76+
// @ts-ignore
7277
fs._encode(new Key('hello/world'))
7378
).to.eql({
7479
dir: path.join(dir, 'hello'),
7580
file: path.join(dir, 'hello', 'world.data')
7681
})
7782

7883
expect(
84+
// @ts-ignore
7985
fs._decode(fs._encode(new Key('hello/world/test:other')).file)
8086
).to.eql(
8187
new Key('hello/world/test:other')
@@ -153,7 +159,7 @@ describe('FsDatastore', () => {
153159
describe('interface-datastore', () => {
154160
const dir = utils.tmpdir()
155161

156-
require('interface-datastore/src/tests')({
162+
tests({
157163
setup: () => {
158164
return new FsStore(dir)
159165
},
@@ -166,7 +172,7 @@ describe('FsDatastore', () => {
166172
describe('interface-datastore (sharding(fs))', () => {
167173
const dir = utils.tmpdir()
168174

169-
require('interface-datastore/src/tests')({
175+
tests({
170176
setup: () => {
171177
const shard = new sh.NextToLast(2)
172178
return ShardingStore.createOrOpen(new FsStore(dir), shard)

tsconfig.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "./node_modules/aegir/src/config/tsconfig.aegir.json",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
},
6+
"include": [
7+
"test", // remove this line if you don't want to type-check tests
8+
"src"
9+
]
10+
}

0 commit comments

Comments
 (0)