@@ -10,12 +10,18 @@ import { fromUtf8, toUtf8 } from "@smithy/util-utf8";
10
10
/**
11
11
*
12
12
*/
13
- type CborItemType = undefined | boolean | number | [ CborSliceType , Uint64Imprecise ] | string | CborTagType ;
13
+ type CborItemType =
14
+ | undefined
15
+ | boolean
16
+ | number
17
+ | [ CborUnstructuredByteStringType , Uint64Imprecise ]
18
+ | string
19
+ | CborTagType ;
14
20
type CborTagType = {
15
21
tag : Uint64Imprecise ;
16
22
value : CborValueType ;
17
23
} ;
18
- type CborSliceType = Uint8Array ;
24
+ type CborUnstructuredByteStringType = Uint8Array ;
19
25
type CborListType < T = any > = Array < T > ;
20
26
type CborMapType < T = any > = Record < string , T > ;
21
27
type CborCollectionType < T = any > = CborMapType < T > | CborListType < T > ;
@@ -174,38 +180,43 @@ const decodeNegativeInt = (payload: Uint8Array): [Uint64Imprecise, CborArgumentL
174
180
return [ - 1 - i , offset ] ;
175
181
} ;
176
182
183
+ const decodeUnstructuredByteString = ( payload : Uint8Array ) : [ CborUnstructuredByteStringType , CborOffset ] => {
184
+ return decodeString ( payload , majorUnstructuredByteString ) ;
185
+ } ;
186
+
187
+ const decodeUtf8String = ( payload : Uint8Array ) : [ string , CborOffset ] => {
188
+ return decodeString ( payload , majorUtf8String ) ;
189
+ } ;
190
+
177
191
const decodeString = < M extends typeof majorUtf8String | typeof majorUnstructuredByteString > (
178
192
payload : Uint8Array ,
179
193
inner : M
180
- ) : [ M extends typeof majorUtf8String ? string : CborSliceType , CborOffset ] => {
194
+ ) : [ M extends typeof majorUtf8String ? string : CborUnstructuredByteStringType , CborOffset ] => {
181
195
const minor = peekMinor ( payload ) ;
196
+
182
197
if ( minor === minorIndefinite ) {
183
- return decodeUnstructuredByteStringIndefinite ( payload ) as [
184
- M extends typeof majorUtf8String ? string : CborSliceType ,
198
+ const [ decoded , offset ] = decodeStringIndefinite ( payload , inner ) ;
199
+ return ( inner === majorUnstructuredByteString ? [ decoded , offset ] : [ toUtf8 ( decoded ) , offset ] ) as [
200
+ M extends typeof majorUtf8String ? string : CborUnstructuredByteStringType ,
185
201
CborOffset ,
186
202
] ;
187
203
}
188
204
189
205
const [ length , offset ] = decodeArgument ( payload ) ;
190
206
payload = payload . subarray ( offset ) ;
191
207
if ( payload . length < length ) {
192
- throw new Error ( `slice len ${ length } greater than remaining buf len.` ) ;
208
+ throw new Error ( `unstructured byte string len ${ length } greater than remaining buf len.` ) ;
193
209
}
194
210
195
211
const value =
196
212
inner === majorUnstructuredByteString ? payload . subarray ( 0 , length ) : toUtf8 ( payload . subarray ( 0 , length ) ) ;
197
- return [ value as M extends typeof majorUtf8String ? string : CborSliceType , offset + length ] ;
213
+ return [ value as M extends typeof majorUtf8String ? string : CborUnstructuredByteStringType , offset + length ] ;
198
214
} ;
199
215
200
- const decodeUnstructuredByteString = ( payload : Uint8Array ) : [ CborSliceType , CborOffset ] => {
201
- return decodeString ( payload , majorUnstructuredByteString ) ;
202
- } ;
203
-
204
- const decodeUtf8String = ( payload : Uint8Array ) : [ string , CborOffset ] => {
205
- return decodeString ( payload , majorUtf8String ) ;
206
- } ;
207
-
208
- const decodeUnstructuredByteStringIndefinite = ( payload : Uint8Array ) : [ CborSliceType , CborOffset ] => {
216
+ const decodeStringIndefinite = < I extends typeof majorUtf8String | typeof majorUnstructuredByteString > (
217
+ payload : Uint8Array ,
218
+ inner : I
219
+ ) : [ CborUnstructuredByteStringType , CborOffset ] => {
209
220
payload = payload . subarray ( 1 ) ;
210
221
211
222
const buffer = [ ] ;
@@ -217,18 +228,24 @@ const decodeUnstructuredByteStringIndefinite = (payload: Uint8Array): [CborSlice
217
228
218
229
const major = peekMajor ( payload ) ;
219
230
const minor = peekMinor ( payload ) ;
220
- if ( major !== majorUnstructuredByteString ) {
221
- throw new Error ( `unexpected major type ${ major } in indefinite slice .` ) ;
231
+ if ( major !== inner ) {
232
+ throw new Error ( `unexpected major type ${ major } in indefinite string .` ) ;
222
233
}
223
234
if ( minor === minorIndefinite ) {
224
- throw new Error ( "nested indefinite slice ." ) ;
235
+ throw new Error ( "nested indefinite string ." ) ;
225
236
}
226
237
227
- const [ byteString , length ] = decodeUnstructuredByteString ( payload ) ;
228
- payload = payload . subarray ( length ) ;
229
-
230
- buffer . push ( byteString ) ;
231
- offset += length ;
238
+ if ( inner === majorUtf8String ) {
239
+ const [ str , length ] = decodeUtf8String ( payload ) ;
240
+ payload = payload . subarray ( length ) ;
241
+ buffer . push ( fromUtf8 ( str ) ) ;
242
+ offset += length ;
243
+ } else {
244
+ const [ byteString , length ] = decodeUnstructuredByteString ( payload ) ;
245
+ payload = payload . subarray ( length ) ;
246
+ buffer . push ( byteString ) ;
247
+ offset += length ;
248
+ }
232
249
}
233
250
throw new Error ( "expected break marker." ) ;
234
251
} ;
@@ -530,6 +547,11 @@ const encode = (input: any, buffer: Uint8Array): CborOffset => {
530
547
offset += encode ( vv , buffer . subarray ( offset ) ) ;
531
548
}
532
549
return offset ;
550
+ } else if ( input instanceof Uint8Array ) {
551
+ // serialize as UnstructuredByteString
552
+ const offset = encodeHeader ( majorUnstructuredByteString , input . length , buffer ) ;
553
+ buffer . subarray ( offset ) . set ( input ) ;
554
+ return offset + input . length ;
533
555
} else if ( typeof input === "object" ) {
534
556
const entries = Object . entries ( input ) . filter ( ( [ , v ] ) => {
535
557
return v !== undefined ;
0 commit comments