Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update types #302

Merged
merged 3 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/blockstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function createBaseStore (store) {
return store.open()
},

// @ts-ignore TODO: how to not have ts freak out about this
// @ts-ignore TODO: ts does not think we will yield only CIDs or only Blocks
async * query (query, options) {
for await (const { key, value } of store.query(query, options)) {
// TODO: we should make this a different method
Expand Down
40 changes: 19 additions & 21 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,52 +34,50 @@ module.exports = (store) => {
* @param {AbortSignal} [options.signal] - abort this config read
* @returns {Promise<Config>}
*/
getAll (options = {}) { // eslint-disable-line require-await
return configStore.get(undefined, options)
async getAll (options = {}) { // eslint-disable-line require-await
// [email protected] cannot read keys from [email protected] dbs so fall back to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an issue to track this? If not, can you create one?

Copy link
Member Author

@achingbrain achingbrain Apr 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do, but it's not something that's ever going to be fixed so the issue will hang around forever.

The level-js 4 -> 5 version bump was because of this breaking change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok makes sense, so maybe not worth

// using IndexedDB API with string keys - only necessary until we do
// the migratiion to v10 or above
const encodedValue = await getWithFallback(configKey, store.get.bind(store), store.has.bind(store), store, {
signal: options.signal
})

return JSON.parse(uint8ArrayToString(encodedValue))
},

/**
* Get the value for the passed configuration key from the repo.
*
* @param {string} [key] - the config key to get
* @param {string} key - the config key to get
* @param {Object} [options] - options
* @param {AbortSignal} [options.signal] - abort this config read
* @returns {Promise<Config | any>}
*/
async get (key, options = {}) {
if (!key) {
key = undefined
if (key == null) {
throw new errors.NotFoundError(`Key ${key} does not exist in config`)
}

// [email protected] cannot read keys from [email protected] dbs so fall back to
// using IndexedDB API with string keys - only necessary until we do
// the migratiion to v10 or above
const encodedValue = await getWithFallback(configKey, store.get.bind(store), store.has.bind(store), store, {
signal: options.signal
})
const config = await this.getAll(options)
const value = _get(config, key)

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

const value = key !== undefined ? _get(config, key) : config
return value
},

/**
* Set the current configuration for this repo.
*
* @param {string} [key] - the config key to be written
* @param {string} key - the config key to be written
* @param {any} [value] - the config value to be written
* @param {Object} [options] - options
* @param {AbortSignal} [options.signal] - abort this config write
*/
set (key, value, options = {}) {
if (arguments.length === 1) {
value = key
key = undefined
} else if (!key || typeof key !== 'string') {
// @ts-ignore ts thinks key will only be a string, but it may not be
if (typeof key !== 'string' && !(key instanceof String)) {
throw errcode(new Error('Invalid key type: ' + typeof key), 'ERR_INVALID_KEY')
}

Expand Down Expand Up @@ -137,7 +135,7 @@ module.exports = (store) => {
const key = m.key
const value = m.value
if (key) {
const config = await configStore.get()
const config = await configStore.getAll()
if (typeof config === 'object' && config !== null) {
_set(config, key, value)
}
Expand Down
2 changes: 1 addition & 1 deletion src/idstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function createIdStore (store) {
return store.open()
},

// @ts-ignore TODO: how to not have ts freak out about this
// @ts-ignore TODO: ts does not think we will yield only CIDs or only Blocks
query (query, options) {
return store.query(query, options)
},
Expand Down
13 changes: 6 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ class IpfsRepo {
/**
* Initialize a new repo.
*
* @param {any} config - config to write into `config`.
* @param {import('./types').Config} config - config to write into `config`.
* @returns {Promise<void>}
*/
async init (config) {
log('initializing at: %s', this.path)
await this._openRoot()
await this.config.set(buildConfig(config))
await this.config.replace(buildConfig(config))
await this.spec.set(buildDatastoreSpec(config))
await this.version.set(constants.repoVersion)
}
Expand Down Expand Up @@ -430,24 +430,23 @@ module.exports.utils = { blockstore: require('./blockstore-utils') }
module.exports.repoVersion = constants.repoVersion
module.exports.errors = ERRORS

// TODO this should come from js-ipfs instead
/**
* @param {any} _config
* @param {import('./types').Config} _config
*/
function buildConfig (_config) {
_config.datastore = Object.assign({}, defaultDatastore, _get(_config, 'datastore'))
_config.Datastore = Object.assign({}, defaultDatastore, _get(_config, 'datastore'))

return _config
}

/**
* @param {any} _config
* @param {import('./types').Config} _config
*/
function buildDatastoreSpec (_config) {
/** @type { {type: string, mounts: Array<{mountpoint: string, type: string, prefix: string, child: {type: string, path: 'string', sync: boolean, shardFunc: string}}>}} */
const spec = {
...defaultDatastore.Spec,
..._get(_config, 'datastore.Spec')
..._get(_config, 'Datastore.Spec')
}

return {
Expand Down
34 changes: 25 additions & 9 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ export interface Blockstore {
}

export interface Config {
Addresses: AddressConfig
Addresses?: AddressConfig
API?: APIConfig,
Profiles?: string
Bootstrap: string[]
Discovery: DiscoveryConfig
Bootstrap?: string[]
Discovery?: DiscoveryConfig
Datastore?: DatastoreConfig
Identity?: IdentityConfig
Keychain?: KeychainConfig
Expand All @@ -130,16 +130,16 @@ export interface AddressConfig {
RPC?: string
Delegates?: string[]
Gateway?: string
Swarm: string[]
Swarm?: string[]
}

export interface APIConfig {
HTTPHeaders?: Record<string, string>
}

export interface DiscoveryConfig {
MDNS: MDNSDiscovery
webRTCStar: WebRTCStarDiscovery
MDNS?: MDNSDiscovery
webRTCStar?: WebRTCStarDiscovery
}

export interface MDNSDiscovery {
Expand All @@ -155,15 +155,31 @@ export interface DatastoreConfig {
Spec?: DatastoreSpec
}

export interface DatastoreSpec {
export interface DatastoreType {
type: string,
path: string,
sync?: boolean,
shardFunc?: string,
compression?: string
}

achingbrain marked this conversation as resolved.
Show resolved Hide resolved
export interface DatastoreMountPoint {
mountpoint: string,
type: string,
prefix: string,
child: DatastoreType
}

export interface DatastoreSpec {
type?: string,
mounts?: DatastoreMountPoint[]
}

export interface IdentityConfig {
/**
* The unique PKI identity label for this configs peer. Set on init and never
* read, its merely here for convenience. IPFS will always generate the peerID
* from its keypair at runtime.
* read, its merely here for convenience. IPFS will always generate the peerID
* from its keypair at runtime.
*/
PeerID: string

Expand Down
7 changes: 4 additions & 3 deletions test/config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = (repo) => {
describe('config', () => {
describe('.set', () => {
it('should throw when invalid key is passed', () => {
// @ts-expect-error key should be a string
return expect(() => repo.config.set(5, 'value'))
.to.throw()
.with.property('code', 'ERR_INVALID_KEY')
Expand All @@ -33,16 +34,16 @@ module.exports = (repo) => {
it('should return the whole conifg', async () => {
const thing = await repo.config.getAll()

expect(thing).to.deep.equal(await repo.config.get())
expect(thing).to.deep.equal(await repo.config.getAll())
})
})
describe('.replace', () => {
it('should replace the whole conifg', async () => {
expect({}).to.not.deep.equal(await repo.config.get())
expect({}).to.not.deep.equal(await repo.config.getAll())

await repo.config.replace({})

expect({}).to.deep.equal(await repo.config.get())
expect({}).to.deep.equal(await repo.config.getAll())
})
})
})
Expand Down
9 changes: 5 additions & 4 deletions test/repo-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ module.exports = (repo) => {

describe('config', () => {
it('get config', async () => {
const config = await repo.config.get()
const config = await repo.config.getAll()
expect(config).to.be.a('object')
})

it('set config', async () => {
await repo.config.set({ a: 'b' })
const config = await repo.config.get()
await repo.config.replace({})
await repo.config.set('a', 'b')
const config = await repo.config.getAll()
expect(config).to.deep.equal({ a: 'b' })
})

Expand All @@ -45,7 +46,7 @@ module.exports = (repo) => {

it('set config key', async () => {
await repo.config.set('c.x', 'd')
const config = await repo.config.get()
const config = await repo.config.getAll()
expect(config).to.deep.equal({ a: 'b', c: { x: 'd' } })
})
})
Expand Down