11'use strict'
22
3- const { Buffer } = require ( 'buffer' )
43const Key = require ( 'interface-datastore' ) . Key
54const { default : Queue } = require ( 'p-queue' )
65const _get = require ( 'just-safe-get' )
76const _set = require ( 'just-safe-set' )
87const errcode = require ( 'err-code' )
98const errors = require ( './errors' )
9+ const uint8ArrayToString = require ( 'uint8arrays/to-string' )
10+ const uint8ArrayFromString = require ( 'uint8arrays/from-string' )
1011
1112const configKey = new Key ( 'config' )
1213
@@ -44,7 +45,7 @@ module.exports = (store) => {
4445 return
4546 }
4647
47- const config = JSON . parse ( encodedValue . toString ( ) )
48+ const config = JSON . parse ( uint8ArrayToString ( encodedValue ) )
4849 if ( key !== undefined && _get ( config , key ) === undefined ) {
4950 throw new errors . NotFoundError ( `Key ${ key } does not exist in config` )
5051 }
@@ -70,7 +71,7 @@ module.exports = (store) => {
7071 throw errcode ( new Error ( 'Invalid key type: ' + typeof key ) , 'ERR_INVALID_KEY' )
7172 }
7273
73- if ( value === undefined || Buffer . isBuffer ( value ) ) {
74+ if ( value === undefined || ( value instanceof Uint8Array ) ) {
7475 throw errcode ( new Error ( 'Invalid value type: ' + typeof value ) , 'ERR_INVALID_VALUE' )
7576 }
7677
@@ -89,7 +90,7 @@ module.exports = (store) => {
8990 * @returns {void }
9091 */
9192 async replace ( value , options = { } ) { // eslint-disable-line require-await
92- if ( ! value || Buffer . isBuffer ( value ) ) {
93+ if ( ! value || ( value instanceof Uint8Array ) ) {
9394 throw errcode ( new Error ( 'Invalid value type: ' + typeof value ) , 'ERR_INVALID_VALUE' )
9495 }
9596
@@ -127,7 +128,7 @@ module.exports = (store) => {
127128 }
128129
129130 function _saveAll ( config ) {
130- const buf = Buffer . from ( JSON . stringify ( config , null , 2 ) )
131+ const buf = uint8ArrayFromString ( JSON . stringify ( config , null , 2 ) )
131132 return store . put ( configKey , buf )
132133 }
133134}
0 commit comments