@@ -22,6 +22,7 @@ import {
2222 validatePutOptions ,
2323} from "./validator.worker" ;
2424
25+ const MAX_BULK_GET_KEYS = 100 ;
2526interface KVParams {
2627 key : string ;
2728}
@@ -81,17 +82,22 @@ async function processKeyValue(
8182 withMetadata : boolean = false
8283) {
8384 const decoder = new TextDecoder ( ) ;
84- let r = "" ;
85+ let decodedValue = "" ;
8586 if ( obj ?. value ) {
8687 for await ( const chunk of obj ?. value ) {
87- r += decoder . decode ( chunk , { stream : true } ) ;
88+ decodedValue += decoder . decode ( chunk , { stream : true } ) ;
8889 }
89- r += decoder . decode ( ) ;
90+ decodedValue += decoder . decode ( ) ;
9091 }
9192
9293 let val = null ;
9394 try {
94- val = obj ?. value == null ? null : type === "json" ? JSON . parse ( r ) : r ;
95+ val =
96+ obj ?. value == null
97+ ? null
98+ : type === "json"
99+ ? JSON . parse ( decodedValue )
100+ : decodedValue ;
95101 } catch ( err : any ) {
96102 throw new HttpError (
97103 400 ,
@@ -104,7 +110,7 @@ async function processKeyValue(
104110 if ( withMetadata ) {
105111 return {
106112 value : val ,
107- metadata : obj ?. metadata ? JSON . stringify ( obj ?. metadata ) : null ,
113+ metadata : obj ?. metadata ,
108114 } ;
109115 }
110116 return val ;
@@ -121,20 +127,20 @@ export class KVNamespaceObject extends MiniflareDurableObject {
121127 @POST ( "/bulk/get" )
122128 get : RouteHandler < KVParams > = async ( req , params , url ) => {
123129 if ( req . method === "POST" && req . body != null ) {
124- let r = "" ;
130+ let decodedBody = "" ;
125131 const decoder = new TextDecoder ( ) ;
126132 for await ( const chunk of req . body ) {
127- r += decoder . decode ( chunk , { stream : true } ) ;
133+ decodedBody += decoder . decode ( chunk , { stream : true } ) ;
128134 }
129- r += decoder . decode ( ) ;
130- const parsedBody = JSON . parse ( r ) ;
135+ decodedBody += decoder . decode ( ) ;
136+ const parsedBody = JSON . parse ( decodedBody ) ;
131137 const keys : string [ ] = parsedBody . keys ;
132138 const type = parsedBody ?. type ;
133139 if ( type && type !== "text" && type !== "json" ) {
134140 return new Response ( "" , { status : 400 } ) ;
135141 }
136142 const obj : { [ key : string ] : any } = { } ;
137- if ( keys . length > 100 ) {
143+ if ( keys . length > MAX_BULK_GET_KEYS ) {
138144 return new Response ( "" , { status : 400 } ) ;
139145 }
140146 for ( const key of keys ) {
0 commit comments