feat: rawRequestBodySize

This commit is contained in:
orion 2023-11-28 11:15:00 -06:00
parent 01f505d05b
commit deb995015d
Signed by: orion
GPG Key ID: 6D4165AE4C928719
2 changed files with 20 additions and 4 deletions

View File

@ -1,2 +1,16 @@
/** @type {(_: Blob) => () => Promise<ArrayBuffer>} */
export const blobArrayBufferImpl = b => () => b.arrayBuffer()
/** @type {(_: FormDataEntryValue) => number} */
const formDataValueSize = v => (typeof v === 'string' ? v.length : v.size)
/** @type {(_: ArrayBuffer | FormData) => () => number} */
export const rawRequestBodySize = body => () =>
body instanceof ArrayBuffer
? body.byteLength
: body instanceof FormData
? Array.from(body.entries()).reduce(
(size, [k, v]) => size + k.length + formDataValueSize(v),
0,
)
: 0

View File

@ -12,21 +12,21 @@ module HTTP.Request
, requestHeaders
, requestUrl
, requestMethod
, rawRequestBodySize
) where
import Prelude
import Control.Promise (Promise)
import Control.Promise as Promise
import Data.ArrayBuffer.ArrayBuffer as ArrayBuffer
import Data.ArrayBuffer.Types (ArrayBuffer)
import Data.Map (Map)
import Data.Map as Map
import Data.Maybe (Maybe(..))
import Data.Newtype (unwrap)
import Data.Nullable as Nullable
import Data.Tuple.Containing (class TupleContaining, extract)
import Data.Tuple.Nested (type (/\), (/\))
import Data.Tuple.Containing (extract)
import Data.Tuple.Nested (type (/\))
import Data.URL (URL)
import Effect (Effect)
import Effect.Aff.Class (class MonadAff, liftAff)
@ -45,9 +45,11 @@ import Unsafe.Coerce (unsafeCoerce)
import Web.File.Blob (Blob)
import Web.File.Blob as Blob
foreign import blobArrayBufferImpl :: Blob -> Effect (Promise ArrayBuffer)
foreign import data RawRequestBody :: Type
foreign import blobArrayBufferImpl :: Blob -> Effect (Promise ArrayBuffer)
foreign import rawRequestBodySize :: RawRequestBody -> Effect Int
unsafeEmptyRawRequestBody :: RawRequestBody
unsafeEmptyRawRequestBody = unsafeCoerce Nullable.null