@@ -3,14 +3,14 @@ import {
33 DeferredPromise ,
44 DELETE ,
55 GET ,
6- POST ,
76 HttpError ,
7+ KeyValueEntry ,
88 KeyValueStorage ,
99 maybeApply ,
1010 MiniflareDurableObject ,
11+ POST ,
1112 PUT ,
1213 RouteHandler ,
13- KeyValueEntry ,
1414} from "miniflare:shared" ;
1515import { KVHeaders , KVLimits , KVParams } from "./constants" ;
1616import {
@@ -75,10 +75,14 @@ function secondsToMillis(seconds: number): number {
7575 return seconds * 1000 ;
7676}
7777
78- async function processKeyValue ( obj : KeyValueEntry < unknown > | null , type : string = "text" , withMetadata : boolean = false ) {
78+ async function processKeyValue (
79+ obj : KeyValueEntry < unknown > | null ,
80+ type : string = "text" ,
81+ withMetadata : boolean = false
82+ ) {
7983 const decoder = new TextDecoder ( ) ;
8084 let r = "" ;
81- if ( obj ?. value ) {
85+ if ( obj ?. value ) {
8286 for await ( const chunk of obj ?. value ) {
8387 r += decoder . decode ( chunk , { stream : true } ) ;
8488 }
@@ -89,16 +93,22 @@ async function processKeyValue(obj: KeyValueEntry<unknown> | null, type: string
8993 try {
9094 val = obj ?. value == null ? null : type === "json" ? JSON . parse ( r ) : r ;
9195 } catch ( err : any ) {
92- throw new HttpError ( 400 , "At least of of the requested keys corresponds to a non-JSON value" ) ;
96+ throw new HttpError (
97+ 400 ,
98+ "At least of of the requested keys corresponds to a non-JSON value"
99+ ) ;
93100 }
94101 if ( val == null ) {
95102 return null ;
96103 }
97104 if ( withMetadata ) {
98- return { value : val , metadata : obj ?. metadata ? JSON . stringify ( obj ?. metadata ) : null } ;
105+ return {
106+ value : val ,
107+ metadata : obj ?. metadata ? JSON . stringify ( obj ?. metadata ) : null ,
108+ } ;
99109 }
100110 return val ;
101- }
111+ }
102112
103113export class KVNamespaceObject extends MiniflareDurableObject {
104114 #storage?: KeyValueStorage ;
@@ -115,7 +125,8 @@ export class KVNamespaceObject extends MiniflareDurableObject {
115125 const cacheTtlParam = url . searchParams . get ( KVParams . CACHE_TTL ) ;
116126 const cacheTtl =
117127 cacheTtlParam === null ? undefined : parseInt ( cacheTtlParam ) ;
118- if ( req . body != null ) { // get bulk
128+ if ( req . method === "POST" && req . body != null ) {
129+ // get bulk
119130 // get bulk
120131 let r = "" ;
121132 const decoder = new TextDecoder ( ) ;
@@ -125,11 +136,15 @@ export class KVNamespaceObject extends MiniflareDurableObject {
125136 r += decoder . decode ( ) ;
126137 const parsedBody = JSON . parse ( r ) ;
127138 const keys : string [ ] = parsedBody . keys ;
128- const obj : { [ key : string ] : any } = { } ;
129- for ( const key of keys ) {
139+ const obj : { [ key : string ] : any } = { } ;
140+ for ( const key of keys ) {
130141 validateGetOptions ( key , { cacheTtl : parsedBody ?. cacheTtl } ) ;
131142 const entry = await this . storage . get ( key ) ;
132- obj [ key ] = await processKeyValue ( entry , parsedBody ?. type , parsedBody ?. withMetadata ) ;
143+ obj [ key ] = await processKeyValue (
144+ entry ,
145+ parsedBody ?. type ,
146+ parsedBody ?. withMetadata
147+ ) ;
133148 }
134149 return new Response ( JSON . stringify ( obj ) ) ;
135150 }
0 commit comments