Skip to content

Commit 0ec1c0b

Browse files
authored
chore: update types (#302)
- Updates deps - Changes Blockstore.query return type based on query arg - Exports Blockstore type from index.js - Moves Config types from ipfs-core-types to ipfs-repo - Removes bignumber.js dep in favour of using `BigInt`s - Normalises type import style - Removes ts-ignores from modules with types
1 parent bbcdb12 commit 0ec1c0b

26 files changed

+227
-123
lines changed

package.json

+8-9
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,33 @@
5151
"@types/ncp": "^2.0.4",
5252
"@types/proper-lockfile": "^4.1.1",
5353
"@types/rimraf": "^3.0.0",
54-
"aegir": "^31.0.1",
54+
"aegir": "^33.1.0",
5555
"assert": "^2.0.0",
5656
"events": "^3.3.0",
5757
"it-all": "^1.0.2",
5858
"it-drain": "^1.0.1",
5959
"it-first": "^1.0.2",
6060
"just-range": "^2.1.0",
61-
"memdown": "^5.1.0",
61+
"memdown": "^6.0.0",
6262
"multihashing-async": "^2.1.0",
6363
"ncp": "^2.0.0",
6464
"process": "^0.11.10",
6565
"readable-stream": "^3.6.0",
6666
"rimraf": "^3.0.0",
67-
"sinon": "^9.0.2",
67+
"sinon": "^10.0.0",
6868
"url": "^0.11.0",
6969
"util": "^0.12.3"
7070
},
7171
"dependencies": {
72-
"bignumber.js": "^9.0.0",
7372
"bytes": "^3.1.0",
7473
"cids": "^1.1.6",
75-
"datastore-core": "^3.0.0",
76-
"datastore-fs": "^3.0.0",
77-
"datastore-level": "^4.0.0",
74+
"datastore-core": "^4.0.0",
75+
"datastore-fs": "^4.0.0",
76+
"datastore-level": "^5.0.0",
7877
"debug": "^4.1.0",
7978
"err-code": "^3.0.1",
80-
"interface-datastore": "^3.0.3",
81-
"ipfs-repo-migrations": "^7.0.1",
79+
"interface-datastore": "^4.0.0",
80+
"ipfs-repo-migrations": "^8.0.0",
8281
"ipfs-utils": "^6.0.0",
8382
"ipld-block": "^0.11.0",
8483
"it-filter": "^1.0.2",

src/api-addr.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const apiFile = new Key('api')
77

88
/**
99
*
10-
* @param {import("interface-datastore").Datastore} store
10+
* @param {import('interface-datastore').Datastore} store
1111
*/
1212
module.exports = (store) => {
1313
return {

src/backends.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

33
/**
4-
* @typedef {import("interface-datastore").Datastore} Datastore
5-
* @typedef {import("./types").Backends} Backends
6-
* @typedef {Required<import("./types").Options>} Options
4+
* @typedef {import('interface-datastore').Datastore} Datastore
5+
* @typedef {import('./types').Backends} Backends
6+
* @typedef {Required<import('./types').Options>} Options
77
*/
88

99
/**

src/blockstore.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const map = require('it-map')
77
const drain = require('it-drain')
88
const pushable = require('it-pushable')
99
/**
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
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
1414
* @typedef {import('./types').Blockstore} Blockstore
1515
*/
1616

@@ -45,6 +45,7 @@ function createBaseStore (store) {
4545
return store.open()
4646
},
4747

48+
// @ts-ignore TODO: ts does not think we will yield only CIDs or only Blocks
4849
async * query (query, options) {
4950
for await (const { key, value } of store.query(query, options)) {
5051
// TODO: we should make this a different method

src/config.js

+27-30
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
const { Key } = require('interface-datastore')
44
const { default: Queue } = require('p-queue')
5-
// @ts-ignore
65
const _get = require('just-safe-get')
7-
// @ts-ignore
86
const _set = require('just-safe-set')
97
const errcode = require('err-code')
108
const errors = require('./errors')
@@ -19,8 +17,11 @@ const {
1917
const configKey = new Key('config')
2018

2119
/**
22-
*
23-
* @param {import("interface-datastore").Datastore} store
20+
* @typedef {import('./types').Config} Config
21+
*/
22+
23+
/**
24+
* @param {import('interface-datastore').Datastore} store
2425
*/
2526
module.exports = (store) => {
2627
const setQueue = new Queue({ concurrency: 1 })
@@ -31,56 +32,52 @@ module.exports = (store) => {
3132
*
3233
* @param {Object} [options] - options
3334
* @param {AbortSignal} [options.signal] - abort this config read
34-
* @returns {Promise<unknown>}
35+
* @returns {Promise<Config>}
3536
*/
36-
getAll (options = {}) { // eslint-disable-line require-await
37-
return configStore.get(undefined, options)
37+
async getAll (options = {}) { // eslint-disable-line require-await
38+
// [email protected] cannot read keys from [email protected] dbs so fall back to
39+
// using IndexedDB API with string keys - only necessary until we do
40+
// the migratiion to v10 or above
41+
const encodedValue = await getWithFallback(configKey, store.get.bind(store), store.has.bind(store), store, {
42+
signal: options.signal
43+
})
44+
45+
return JSON.parse(uint8ArrayToString(encodedValue))
3846
},
3947

4048
/**
4149
* Get the value for the passed configuration key from the repo.
4250
*
43-
* @param {string} [key] - the config key to get
51+
* @param {string} key - the config key to get
4452
* @param {Object} [options] - options
4553
* @param {AbortSignal} [options.signal] - abort this config read
46-
* @returns {Promise<unknown>}
4754
*/
4855
async get (key, options = {}) {
49-
if (!key) {
50-
key = undefined
56+
if (key == null) {
57+
throw new errors.NotFoundError(`Key ${key} does not exist in config`)
5158
}
5259

53-
// [email protected] cannot read keys from [email protected] dbs so fall back to
54-
// using IndexedDB API with string keys - only necessary until we do
55-
// the migratiion to v10 or above
56-
const encodedValue = await getWithFallback(configKey, store.get.bind(store), store.has.bind(store), store)
57-
58-
if (options.signal && options.signal.aborted) {
59-
return
60-
}
60+
const config = await this.getAll(options)
61+
const value = _get(config, key)
6162

62-
const config = JSON.parse(uint8ArrayToString(encodedValue))
63-
if (key !== undefined && _get(config, key) === undefined) {
63+
if (value === undefined) {
6464
throw new errors.NotFoundError(`Key ${key} does not exist in config`)
6565
}
6666

67-
const value = key !== undefined ? _get(config, key) : config
6867
return value
6968
},
7069

7170
/**
7271
* Set the current configuration for this repo.
7372
*
74-
* @param {string | unknown} [key] - the config key to be written
75-
* @param {unknown} [value] - the config value to be written
73+
* @param {string} key - the config key to be written
74+
* @param {any} [value] - the config value to be written
7675
* @param {Object} [options] - options
7776
* @param {AbortSignal} [options.signal] - abort this config write
7877
*/
7978
set (key, value, options = {}) {
80-
if (arguments.length === 1) {
81-
value = key
82-
key = undefined
83-
} else if (!key || typeof key !== 'string') {
79+
// @ts-ignore ts thinks key will only be a string, but it may not be
80+
if (typeof key !== 'string' && !(key instanceof String)) {
8481
throw errcode(new Error('Invalid key type: ' + typeof key), 'ERR_INVALID_KEY')
8582
}
8683

@@ -97,7 +94,7 @@ module.exports = (store) => {
9794
/**
9895
* Set the current configuration for this repo.
9996
*
100-
* @param {Object} [value] - the config value to be written
97+
* @param {Config} [value] - the config value to be written
10198
* @param {Object} [options] - options
10299
* @param {AbortSignal} [options.signal] - abort this config write
103100
*/
@@ -138,7 +135,7 @@ module.exports = (store) => {
138135
const key = m.key
139136
const value = m.value
140137
if (key) {
141-
const config = await configStore.get()
138+
const config = await configStore.getAll()
142139
if (typeof config === 'object' && config !== null) {
143140
_set(config, key, value)
144141
}

src/default-options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Default configuration for a repo in node.js
44

55
/**
6-
* @type {Required<import("./types").Options>}
6+
* @type {Required<import('./types').Options>}
77
*/
88
module.exports = {
99
autoMigrate: true,

src/idstore.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const CID = require('cids')
99
const errcode = require('err-code')
1010

1111
/**
12-
* @typedef {import("interface-datastore").Query} Query
13-
* @typedef {import("interface-datastore").Datastore} Datastore
14-
* @typedef {import("interface-datastore").Options} DatastoreOptions
12+
* @typedef {import('interface-datastore').Query} Query
13+
* @typedef {import('interface-datastore').Datastore} Datastore
14+
* @typedef {import('interface-datastore').Options} DatastoreOptions
1515
* @typedef {import('./types').Blockstore} Blockstore
1616
*/
1717

@@ -30,6 +30,8 @@ function createIdStore (store) {
3030
open () {
3131
return store.open()
3232
},
33+
34+
// @ts-ignore TODO: ts does not think we will yield only CIDs or only Blocks
3335
query (query, options) {
3436
return store.query(query, options)
3537
},

src/index.js

+25-29
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
'use strict'
22

3-
// @ts-ignore
43
const _get = require('just-safe-get')
54
const debug = require('debug')
6-
const Big = require('bignumber.js').BigNumber
75
const errcode = require('err-code')
86
const migrator = require('ipfs-repo-migrations')
97
const bytes = require('bytes')
@@ -33,12 +31,14 @@ const lockers = {
3331
}
3432

3533
/**
36-
* @typedef {import("./types").Options} Options
37-
* @typedef {import("./types").Lock} Lock
38-
* @typedef {import("./types").LockCloser} LockCloser
39-
* @typedef {import("./types").Stat} Stat
40-
* @typedef {import("ipld-block")} Block
41-
* @typedef {import("interface-datastore").Datastore} Datastore
34+
* @typedef {import('./types').Options} Options
35+
* @typedef {import('./types').Lock} Lock
36+
* @typedef {import('./types').LockCloser} LockCloser
37+
* @typedef {import('./types').Stat} Stat
38+
* @typedef {import('./types').Blockstore} Blockstore
39+
* @typedef {import('./types').Config} Config
40+
* @typedef {import('ipld-block')} Block
41+
* @typedef {import('interface-datastore').Datastore} Datastore
4242
*/
4343

4444
/**
@@ -78,13 +78,13 @@ class IpfsRepo {
7878
/**
7979
* Initialize a new repo.
8080
*
81-
* @param {any} config - config to write into `config`.
81+
* @param {import('./types').Config} config - config to write into `config`.
8282
* @returns {Promise<void>}
8383
*/
8484
async init (config) {
8585
log('initializing at: %s', this.path)
8686
await this._openRoot()
87-
await this.config.set(buildConfig(config))
87+
await this.config.replace(buildConfig(config))
8888
await this.spec.set(buildDatastoreSpec(config))
8989
await this.version.set(constants.repoVersion)
9090
}
@@ -319,9 +319,7 @@ class IpfsRepo {
319319
getSize(this.datastore),
320320
getSize(this.keys)
321321
])
322-
const size = blocks.size
323-
.plus(datastore)
324-
.plus(keys)
322+
const size = blocks.size + datastore + keys
325323

326324
return {
327325
repoPath: this.path,
@@ -389,26 +387,25 @@ class IpfsRepo {
389387
async _storageMaxStat () {
390388
try {
391389
const max = /** @type {number} */(await this.config.get('Datastore.StorageMax'))
392-
return new Big(bytes(max))
390+
return BigInt(bytes(max))
393391
} catch (err) {
394-
return new Big(noLimit)
392+
return BigInt(noLimit)
395393
}
396394
}
397395

398396
/**
399397
* @private
400398
*/
401399
async _blockStat () {
402-
let count = new Big(0)
403-
let size = new Big(0)
400+
let count = BigInt(0)
401+
let size = BigInt(0)
404402

405403
if (this.blocks) {
406404
for await (const blockOrCid of this.blocks.query({})) {
407405
const block = /** @type {Block} */(blockOrCid)
408-
count = count.plus(1)
409-
size = size
410-
.plus(block.data.byteLength)
411-
.plus(block.cid.bytes.byteLength)
406+
count += BigInt(1)
407+
size += BigInt(block.data.byteLength)
408+
size += BigInt(block.cid.bytes.byteLength)
412409
}
413410
}
414411

@@ -420,10 +417,10 @@ class IpfsRepo {
420417
* @param {Datastore} datastore
421418
*/
422419
async function getSize (datastore) {
423-
const sum = new Big(0)
420+
let sum = BigInt(0)
424421
for await (const block of datastore.query({})) {
425-
sum.plus(block.value.byteLength)
426-
.plus(block.key.uint8Array().byteLength)
422+
sum += BigInt(block.value.byteLength)
423+
sum += BigInt(block.key.uint8Array().byteLength)
427424
}
428425
return sum
429426
}
@@ -433,24 +430,23 @@ module.exports.utils = { blockstore: require('./blockstore-utils') }
433430
module.exports.repoVersion = constants.repoVersion
434431
module.exports.errors = ERRORS
435432

436-
// TODO this should come from js-ipfs instead
437433
/**
438-
* @param {any} _config
434+
* @param {import('./types').Config} _config
439435
*/
440436
function buildConfig (_config) {
441-
_config.datastore = Object.assign({}, defaultDatastore, _get(_config, 'datastore'))
437+
_config.Datastore = Object.assign({}, defaultDatastore, _get(_config, 'datastore'))
442438

443439
return _config
444440
}
445441

446442
/**
447-
* @param {any} _config
443+
* @param {import('./types').Config} _config
448444
*/
449445
function buildDatastoreSpec (_config) {
450446
/** @type { {type: string, mounts: Array<{mountpoint: string, type: string, prefix: string, child: {type: string, path: 'string', sync: boolean, shardFunc: string}}>}} */
451447
const spec = {
452448
...defaultDatastore.Spec,
453-
..._get(_config, 'datastore.Spec')
449+
..._get(_config, 'Datastore.Spec')
454450
}
455451

456452
return {

src/lock-memory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const lockFile = 'repo.lock'
1111
const LOCKS = {}
1212

1313
/**
14-
* @typedef {import("./types").LockCloser} LockCloser
14+
* @typedef {import('./types').LockCloser} LockCloser
1515
*/
1616

1717
/**

src/lock.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const log = debug('ipfs:repo:lock')
99
const lockFile = 'repo.lock'
1010

1111
/**
12-
* @typedef {import("./types").LockCloser} LockCloser
12+
* @typedef {import('./types').LockCloser} LockCloser
1313
*/
1414

1515
/**

src/spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const specKey = new Key('datastore_spec')
99

1010
/**
1111
*
12-
* @param {import("interface-datastore").Datastore} store
12+
* @param {import('interface-datastore').Datastore} store
1313
*/
1414
module.exports = (store) => {
1515
return {

0 commit comments

Comments
 (0)