11import { decodeUtf8 } from '../../util/buffers/utf8/decodeUtf8' ;
22import { Reader } from '../../util/buffers/Reader' ;
33import { fromBase64Bin } from '../../util/base64/fromBase64Bin' ;
4+ import { findEndingQuote } from './util' ;
45import type { BinaryJsonDecoder , PackValue } from '../types' ;
56
67const REGEX_REPLACE_ESCAPED_CHARS = / \\ ( b | f | n | r | t | " | \/ | \\ ) / g;
@@ -104,20 +105,6 @@ const isUndefined = (u8: Uint8Array, x: number) =>
104105 u8 [ x ++ ] === 0x3d && // =
105106 u8 [ x ++ ] === 0x22 ; // "
106107
107- const findEndingQuote = ( uint8 : Uint8Array , x : number ) : number => {
108- const len = uint8 . length ;
109- let char = uint8 [ x ] ;
110- let prev = 0 ;
111- while ( x < len ) {
112- if ( char === 34 && prev !== 92 ) break ;
113- if ( char === 92 && prev === 92 ) prev = 0 ;
114- else prev = char ;
115- char = uint8 [ ++ x ] ;
116- }
117- if ( x === len ) throw new Error ( 'Invalid JSON' ) ;
118- return x ;
119- } ;
120-
121108const fromCharCode = String . fromCharCode ;
122109
123110const readShortUtf8StrAndUnescape = ( reader : Reader ) : string => {
@@ -198,7 +185,7 @@ const readShortUtf8StrAndUnescape = (reader: Reader): string => {
198185export class JsonDecoder implements BinaryJsonDecoder {
199186 public reader = new Reader ( ) ;
200187
201- public read ( uint8 : Uint8Array ) : PackValue {
188+ public read ( uint8 : Uint8Array ) : unknown {
202189 this . reader . reset ( uint8 ) ;
203190 return this . readAny ( ) ;
204191 }
@@ -208,7 +195,7 @@ export class JsonDecoder implements BinaryJsonDecoder {
208195 return this . readAny ( ) ;
209196 }
210197
211- public readAny ( ) : PackValue {
198+ public readAny ( ) : unknown {
212199 this . skipWhitespace ( ) ;
213200 const reader = this . reader ;
214201 const x = reader . x ;
@@ -653,10 +640,10 @@ export class JsonDecoder implements BinaryJsonDecoder {
653640 return bin ;
654641 }
655642
656- public readArr ( ) : PackValue [ ] {
643+ public readArr ( ) : unknown [ ] {
657644 const reader = this . reader ;
658645 if ( reader . u8 ( ) !== 0x5b ) throw new Error ( 'Invalid JSON' ) ;
659- const arr : PackValue [ ] = [ ] ;
646+ const arr : unknown [ ] = [ ] ;
660647 const uint8 = reader . uint8 ;
661648 while ( true ) {
662649 this . skipWhitespace ( ) ;
@@ -670,10 +657,10 @@ export class JsonDecoder implements BinaryJsonDecoder {
670657 }
671658 }
672659
673- public readObj ( ) : Record < string , PackValue > {
660+ public readObj ( ) : PackValue | Record < string , unknown > | unknown {
674661 const reader = this . reader ;
675662 if ( reader . u8 ( ) !== 0x7b ) throw new Error ( 'Invalid JSON' ) ;
676- const obj : Record < string , PackValue > = { } ;
663+ const obj : Record < string , unknown > = { } ;
677664 const uint8 = reader . uint8 ;
678665 while ( true ) {
679666 this . skipWhitespace ( ) ;
0 commit comments