1
1
'use strict'
2
2
3
- const { Buffer } = require ( 'buffer' )
4
3
const Key = require ( 'interface-datastore' ) . Key
5
4
const { default : Queue } = require ( 'p-queue' )
6
5
const _get = require ( 'just-safe-get' )
7
6
const _set = require ( 'just-safe-set' )
8
7
const errcode = require ( 'err-code' )
9
8
const errors = require ( './errors' )
9
+ const uint8ArrayToString = require ( 'uint8arrays/to-string' )
10
+ const uint8ArrayFromString = require ( 'uint8arrays/from-string' )
10
11
11
12
const configKey = new Key ( 'config' )
12
13
@@ -44,7 +45,7 @@ module.exports = (store) => {
44
45
return
45
46
}
46
47
47
- const config = JSON . parse ( encodedValue . toString ( ) )
48
+ const config = JSON . parse ( uint8ArrayToString ( encodedValue ) )
48
49
if ( key !== undefined && _get ( config , key ) === undefined ) {
49
50
throw new errors . NotFoundError ( `Key ${ key } does not exist in config` )
50
51
}
@@ -70,7 +71,7 @@ module.exports = (store) => {
70
71
throw errcode ( new Error ( 'Invalid key type: ' + typeof key ) , 'ERR_INVALID_KEY' )
71
72
}
72
73
73
- if ( value === undefined || Buffer . isBuffer ( value ) ) {
74
+ if ( value === undefined || ( value instanceof Uint8Array ) ) {
74
75
throw errcode ( new Error ( 'Invalid value type: ' + typeof value ) , 'ERR_INVALID_VALUE' )
75
76
}
76
77
@@ -89,7 +90,7 @@ module.exports = (store) => {
89
90
* @returns {void }
90
91
*/
91
92
async replace ( value , options = { } ) { // eslint-disable-line require-await
92
- if ( ! value || Buffer . isBuffer ( value ) ) {
93
+ if ( ! value || ( value instanceof Uint8Array ) ) {
93
94
throw errcode ( new Error ( 'Invalid value type: ' + typeof value ) , 'ERR_INVALID_VALUE' )
94
95
}
95
96
@@ -127,7 +128,7 @@ module.exports = (store) => {
127
128
}
128
129
129
130
function _saveAll ( config ) {
130
- const buf = Buffer . from ( JSON . stringify ( config , null , 2 ) )
131
+ const buf = uint8ArrayFromString ( JSON . stringify ( config , null , 2 ) )
131
132
return store . put ( configKey , buf )
132
133
}
133
134
}
0 commit comments