Skip to content

Commit 1fba61c

Browse files
committed
feat: base types
- adds types - removes fake async - fixes tests for async errors that now are sync - adds local types for merge-options
1 parent ff1cd3a commit 1fba61c

21 files changed

+424
-158
lines changed

package.json

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@
4343
"npm": ">=3.0.0"
4444
},
4545
"devDependencies": {
46-
"aegir": "^30.0.1",
46+
"@types/bytes": "^3.1.0",
47+
"@types/debug": "^4.1.5",
48+
"@types/err-code": "^2.0.0",
49+
"@types/memdown": "^3.0.0",
50+
"@types/ncp": "^2.0.4",
51+
"@types/rimraf": "^3.0.0",
52+
"aegir": "^30.3.0",
4753
"it-all": "^1.0.2",
4854
"it-drain": "^1.0.1",
4955
"it-first": "^1.0.2",
@@ -58,24 +64,28 @@
5864
"bignumber.js": "^9.0.0",
5965
"bytes": "^3.1.0",
6066
"cids": "^1.0.0",
61-
"datastore-core": "^2.0.0",
62-
"datastore-fs": "^2.0.0",
63-
"datastore-level": "^2.0.0",
67+
"datastore-core": "ipfs/js-datastore-core#feat/ts-types",
68+
"datastore-fs": "ipfs/js-datastore-fs#feat/ts-types",
69+
"datastore-level": "ipfs/js-datastore-level#feat/ts-types",
6470
"debug": "^4.1.0",
6571
"err-code": "^2.0.0",
66-
"interface-datastore": "^2.0.0",
72+
"interface-datastore": "^3.0.1",
6773
"ipfs-repo-migrations": "^5.0.3",
68-
"ipfs-utils": "^5.0.1",
74+
"ipfs-utils": "^6.0.0",
6975
"ipld-block": "^0.11.0",
7076
"it-map": "^1.0.2",
7177
"it-pushable": "^1.4.0",
7278
"just-safe-get": "^2.0.0",
7379
"just-safe-set": "^2.1.0",
80+
"merge-options": "^3.0.4",
7481
"multibase": "^3.0.0",
7582
"p-queue": "^6.0.0",
7683
"proper-lockfile": "^4.0.0",
7784
"sort-keys": "^4.0.0",
78-
"uint8arrays": "^1.0.0"
85+
"uint8arrays": "^2.0.5"
86+
},
87+
"eslintConfig": {
88+
"extends": "ipfs"
7989
},
8090
"license": "MIT",
8191
"contributors": [

src/api-addr.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const uint8ArrayFromString = require('uint8arrays/from-string')
55

66
const apiFile = new Key('api')
77

8+
/**
9+
*
10+
* @param {import("interface-datastore").Datastore} store
11+
*/
812
module.exports = (store) => {
913
return {
1014
/**
@@ -18,19 +22,17 @@ module.exports = (store) => {
1822
},
1923
/**
2024
* Set the current configuration for this repo.
25+
* TODO: fix find the proper type or remove this API
2126
*
22-
* @param {Object} value - the api address to be written
23-
* @returns {Promise<?>}
27+
* @param {string} value - the api address to be written
2428
*/
25-
async set (value) { // eslint-disable-line require-await
29+
set (value) {
2630
return store.put(apiFile, uint8ArrayFromString(value.toString()))
2731
},
2832
/**
2933
* Deletes api file
30-
*
31-
* @returns {Promise<void>}
3234
*/
33-
async delete () { // eslint-disable-line require-await
35+
delete () {
3436
return store.delete(apiFile)
3537
}
3638
}

src/backends.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
'use strict'
22

3-
exports.create = function createBackend (name, path, options) {
3+
/**
4+
* @typedef {import("interface-datastore").Datastore} Datastore
5+
* @typedef {import("./types").Backends} Backends
6+
* @typedef {import("./types").InternalOptions} Options
7+
*/
8+
9+
/**
10+
*
11+
* @param {Backends} name
12+
* @param {string} path
13+
* @param {Options} options
14+
* @returns {Datastore}
15+
*/
16+
function createBackend (name, path, options) {
417
const Ctor = options.storageBackends[name]
518
const backendOptions = Object.assign({}, options.storageBackendOptions[name] || {})
19+
// @ts-ignore we don't have a signature for the constructor
620
return new Ctor(path, backendOptions)
721
}
22+
23+
module.exports = {
24+
create: createBackend
25+
}

src/blockstore.js

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,50 @@
11
'use strict'
22

3-
const core = require('datastore-core')
4-
const ShardingStore = core.ShardingDatastore
3+
const { shard, ShardingDatastore } = require('datastore-core')
54
const Block = require('ipld-block')
65
const { cidToKey, keyToCid } = require('./blockstore-utils')
76
const map = require('it-map')
87
const drain = require('it-drain')
98
const pushable = require('it-pushable')
10-
9+
/**
10+
* @typedef {import("interface-datastore").Query} Query
11+
* @typedef {import("interface-datastore").Datastore} Datastore
12+
* @typedef {import("interface-datastore").Options} DatastoreOptions
13+
* @typedef {import("cids")} CID
14+
*/
15+
16+
/**
17+
*
18+
* @param {Datastore} filestore
19+
* @param {*} options
20+
*/
1121
module.exports = async (filestore, options) => {
1222
const store = await maybeWithSharding(filestore, options)
1323
return createBaseStore(store)
1424
}
1525

26+
/**
27+
* @param {Datastore} filestore
28+
* @param {{ sharding: any; }} options
29+
*/
1630
function maybeWithSharding (filestore, options) {
1731
if (options.sharding) {
18-
const shard = new core.shard.NextToLast(2)
19-
return ShardingStore.createOrOpen(filestore, shard)
32+
return ShardingDatastore.createOrOpen(filestore, new shard.NextToLast(2))
2033
}
2134
return filestore
2235
}
2336

37+
/**
38+
* @param {Datastore | ShardingDatastore} store
39+
*/
2440
function createBaseStore (store) {
2541
return {
2642
/**
2743
* Query the store
2844
*
29-
* @param {Object} query
30-
* @param {Object} options
31-
* @returns {AsyncIterator<Block|CID>}
45+
* @param {Query} query
46+
* @param {DatastoreOptions} [options]
47+
* @returns {AsyncIterable<Block|CID>}
3248
*/
3349
async * query (query, options) {
3450
for await (const { key, value } of store.query(query, options)) {
@@ -45,7 +61,7 @@ function createBaseStore (store) {
4561
* Get a single block by CID
4662
*
4763
* @param {CID} cid
48-
* @param {Object} options
64+
* @param {DatastoreOptions} [options]
4965
* @returns {Promise<Block>}
5066
*/
5167
async get (cid, options) {
@@ -58,9 +74,9 @@ function createBaseStore (store) {
5874
/**
5975
* Like get, but for more
6076
*
61-
* @param {AsyncIterator<CID>} cids
62-
* @param {Object} options
63-
* @returns {AsyncIterator<Block>}
77+
* @param {Iterable<CID> | AsyncIterable<CID>} cids
78+
* @param {DatastoreOptions} [options]
79+
* @returns {AsyncIterable<Block>}
6480
*/
6581
async * getMany (cids, options) {
6682
for await (const cid of cids) {
@@ -72,7 +88,7 @@ function createBaseStore (store) {
7288
* Write a single block to the store
7389
*
7490
* @param {Block} block
75-
* @param {Object} options
91+
* @param {DatastoreOptions} [options]
7692
* @returns {Promise<Block>}
7793
*/
7894
async put (block, options) {
@@ -94,7 +110,7 @@ function createBaseStore (store) {
94110
* Like put, but for more
95111
*
96112
* @param {AsyncIterable<Block>|Iterable<Block>} blocks
97-
* @param {Object} options
113+
* @param {DatastoreOptions} [options]
98114
* @returns {AsyncIterable<Block>}
99115
*/
100116
async * putMany (blocks, options) { // eslint-disable-line require-await
@@ -142,41 +158,38 @@ function createBaseStore (store) {
142158
* Does the store contain block with this CID?
143159
*
144160
* @param {CID} cid
145-
* @param {Object} options
146-
* @returns {Promise<bool>}
161+
* @param {DatastoreOptions} [options]
147162
*/
148-
async has (cid, options) { // eslint-disable-line require-await
163+
has (cid, options) {
149164
return store.has(cidToKey(cid), options)
150165
},
151166

152167
/**
153168
* Delete a block from the store
154169
*
155170
* @param {CID} cid
156-
* @param {Object} options
171+
* @param {DatastoreOptions} [options]
157172
* @returns {Promise<void>}
158173
*/
159-
async delete (cid, options) { // eslint-disable-line require-await
174+
delete (cid, options) {
160175
return store.delete(cidToKey(cid), options)
161176
},
162177

163178
/**
164179
* Delete a block from the store
165180
*
166-
* @param {AsyncIterable<CID>} cids
167-
* @param {Object} options
168-
* @returns {Promise<void>}
181+
* @param {AsyncIterable<any> | Iterable<any>} cids
182+
* @param {DatastoreOptions} [options]
169183
*/
170-
async * deleteMany (cids, options) { // eslint-disable-line require-await
171-
yield * store.deleteMany(map(cids, cid => cidToKey(cid)), options)
184+
deleteMany (cids, options) {
185+
return store.deleteMany(map(cids, cid => cidToKey(cid)), options)
172186
},
173187

174188
/**
175189
* Close the store
176190
*
177-
* @returns {Promise<void>}
178191
*/
179-
async close () { // eslint-disable-line require-await
192+
close () {
180193
return store.close()
181194
}
182195
}

src/config.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict'
22

3-
const Key = require('interface-datastore').Key
3+
const { Key } = require('interface-datastore')
44
const { default: Queue } = require('p-queue')
5+
// @ts-ignore
56
const _get = require('just-safe-get')
7+
// @ts-ignore
68
const _set = require('just-safe-set')
79
const errcode = require('err-code')
810
const errors = require('./errors')
@@ -11,28 +13,32 @@ const uint8ArrayFromString = require('uint8arrays/from-string')
1113

1214
const configKey = new Key('config')
1315

16+
/**
17+
*
18+
* @param {import("interface-datastore").Datastore} store
19+
*/
1420
module.exports = (store) => {
1521
const setQueue = new Queue({ concurrency: 1 })
1622

1723
const configStore = {
1824
/**
1925
* Get the current configuration from the repo.
2026
*
21-
* @param {Object} options - options
22-
* @param {AbortSignal} options.signal - abort this config read
23-
* @returns {Promise<Object>}
27+
* @param {Object} [options] - options
28+
* @param {AbortSignal} [options.signal] - abort this config read
29+
* @returns {Promise<unknown>}
2430
*/
25-
async getAll (options = {}) { // eslint-disable-line require-await
31+
getAll (options = {}) { // eslint-disable-line require-await
2632
return configStore.get(undefined, options)
2733
},
2834

2935
/**
3036
* Get the value for the passed configuration key from the repo.
3137
*
32-
* @param {string} key - the config key to get
33-
* @param {Object} options - options
34-
* @param {AbortSignal} options.signal - abort this config read
35-
* @returns {Promise<Object>}
38+
* @param {string} [key] - the config key to get
39+
* @param {Object} [options] - options
40+
* @param {AbortSignal} [options.signal] - abort this config read
41+
* @returns {Promise<unknown>}
3642
*/
3743
async get (key, options = {}) {
3844
if (!key) {
@@ -57,13 +63,12 @@ module.exports = (store) => {
5763
/**
5864
* Set the current configuration for this repo.
5965
*
60-
* @param {string} key - the config key to be written
61-
* @param {Object} value - the config value to be written
62-
* @param {Object} options - options
63-
* @param {AbortSignal} options.signal - abort this config write
64-
* @returns {void}
66+
* @param {string | unknown} [key] - the config key to be written
67+
* @param {unknown} [value] - the config value to be written
68+
* @param {Object} [options] - options
69+
* @param {AbortSignal} [options.signal] - abort this config write
6570
*/
66-
async set (key, value, options = {}) { // eslint-disable-line require-await
71+
set (key, value, options = {}) {
6772
if (arguments.length === 1) {
6873
value = key
6974
key = undefined
@@ -84,12 +89,11 @@ module.exports = (store) => {
8489
/**
8590
* Set the current configuration for this repo.
8691
*
87-
* @param {Object} value - the config value to be written
88-
* @param {Object} options - options
89-
* @param {AbortSignal} options.signal - abort this config write
90-
* @returns {void}
92+
* @param {Object} [value] - the config value to be written
93+
* @param {Object} [options] - options
94+
* @param {AbortSignal} [options.signal] - abort this config write
9195
*/
92-
async replace (value, options = {}) { // eslint-disable-line require-await
96+
replace (value, options = {}) {
9397
if (!value || (value instanceof Uint8Array)) {
9498
throw errcode(new Error('Invalid value type: ' + typeof value), 'ERR_INVALID_VALUE')
9599
}
@@ -103,15 +107,18 @@ module.exports = (store) => {
103107
/**
104108
* Check if a config file exists.
105109
*
106-
* @returns {Promise<bool>}
107110
*/
108-
async exists () { // eslint-disable-line require-await
111+
exists () {
109112
return store.has(configKey)
110113
}
111114
}
112115

113116
return configStore
114117

118+
/**
119+
* @param {{ key: any; value: any; }} m
120+
* @param {AbortSignal | undefined} signal
121+
*/
115122
async function _maybeDoSet (m, signal) {
116123
if (signal && signal.aborted) {
117124
return
@@ -127,6 +134,9 @@ module.exports = (store) => {
127134
return _saveAll(value)
128135
}
129136

137+
/**
138+
* @param {unknown} config
139+
*/
130140
function _saveAll (config) {
131141
const buf = uint8ArrayFromString(JSON.stringify(config, null, 2))
132142
return store.put(configKey, buf)

src/default-options.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict'
22

33
// Default configuration for a repo in node.js
4+
5+
/**
6+
* @type {import("./types").InternalOptions}
7+
*/
48
module.exports = {
59
lock: 'fs',
610
storageBackends: {

0 commit comments

Comments
 (0)