22
33const { Key } = require ( 'interface-datastore' )
44const { default : Queue } = require ( 'p-queue' )
5- // @ts -ignore
65const _get = require ( 'just-safe-get' )
7- // @ts -ignore
86const _set = require ( 'just-safe-set' )
97const errcode = require ( 'err-code' )
108const errors = require ( './errors' )
@@ -19,8 +17,11 @@ const {
1917const 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 */
2526module . 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 }
0 commit comments