@@ -90,6 +90,7 @@ async function processKeyValue(
9090 }
9191
9292 let val = null ;
93+ const size = decodedValue . length ;
9394 try {
9495 val = ! obj ?. value
9596 ? null
@@ -99,16 +100,16 @@ async function processKeyValue(
99100 } catch ( err : any ) {
100101 throw new HttpError (
101102 400 ,
102- " At least one of the requested keys corresponds to a non-JSON value"
103+ ` At least one of the requested keys corresponds to a non-${ type } value`
103104 ) ;
104105 }
105106 if ( val && withMetadata ) {
106- return {
107+ return [ {
107108 value : val ,
108109 metadata : obj ?. metadata ?? null ,
109- } ;
110+ } , size ] ;
110111 }
111- return val ;
112+ return [ val , size ] ;
112113}
113114
114115export class KVNamespaceObject extends MiniflareDurableObject {
@@ -132,23 +133,35 @@ export class KVNamespaceObject extends MiniflareDurableObject {
132133 const keys : string [ ] = parsedBody . keys ;
133134 const type = parsedBody ?. type ;
134135 if ( type && type !== "text" && type !== "json" ) {
135- return new Response ( `Type ${ type } is invalid` , { status : 400 } ) ;
136+ return new Response ( "Bad Request" , { status : 400 } ) ;
136137 }
137138 const obj : { [ key : string ] : any } = { } ;
138139 if ( keys . length > MAX_BULK_GET_KEYS ) {
139- return new Response ( `Accepting a max of 100 keys, got ${ keys . length } ` , {
140+ return new Response ( "Bad Request" , {
140141 status : 400 ,
141142 } ) ;
142143 }
144+ let totalBytes = 0 ;
143145 for ( const key of keys ) {
144146 validateGetOptions ( key , { cacheTtl : parsedBody ?. cacheTtl } ) ;
145147 const entry = await this . storage . get ( key ) ;
146- const value = await processKeyValue (
148+ const [ value , size ] = await processKeyValue (
147149 entry ,
148150 parsedBody ?. type ,
149151 parsedBody ?. withMetadata
150152 ) ;
153+ totalBytes += size ;
151154 obj [ key ] = value ;
155+
156+ }
157+ const maxValueSize = this . beingTested
158+ ? KVLimits . MAX_VALUE_SIZE_TEST
159+ : KVLimits . MAX_BULK_SIZE ;
160+ if ( totalBytes > maxValueSize ) {
161+ throw new HttpError (
162+ 413 ,
163+ `Total size of request exceeds the limit of ${ maxValueSize / 1024 / 1024 } MB`
164+ ) ;
152165 }
153166
154167 return new Response ( JSON . stringify ( obj ) ) ;
0 commit comments