import type { Readable } from "stream" ; export type UploadHandlerArgs = { name : string ; stream : Readable ; filename : string ; encoding : string ; mimetype : string ; } ; export type UploadHandler = ( args : UploadHandlerArgs ) => Promise < string | File | undefined > ; function isBlob ( value ) { return ( typeof value === "object" && ( typeof value . arrayBuffer === "function" || typeof value . size === "number" || typeof value . slice === "function" || typeof value . stream === "function" || typeof value . text === "function" || typeof value . type === "string" ) ) ; } export function isFile ( blob ) { let file = blob as File ; return typeof file . name === "string" ; } class NodeFormData implements FormData { private _fields : Record < string , ( File | string ) [ ] > ; constructor ( form ? ) { if ( typeof form !== "undefined" ) { throw new Error ( "Form data on the server is not supported." ) ; } this . _fields = { } ; } append ( name , value , fileName ? ) { if ( typeof value !== "string" && ! isBlob ( value ) ) { throw new Error ( "formData.append can only accept a string or Blob" ) ; } this . _fields [ name ] = this . _fields [ name ] || [ ] ; if ( typeof value === "string" || isFile ( value ) ) { this . _fields [ name ] . push ( value ) ; } else { this . _fields [ name ] . push ( new File ( [ value ] , fileName || "unknown" ) ) ; } } delete ( name ) { delete this . _fields [ name ] ; } get ( name ) { let arr = this . _fields [ name ] ; return arr ?. slice ( - 1 ) [ 0 ] || null ; } getAll ( name ) { let arr = this . _fields [ name ] ; return arr || [ ] ; } has ( name ) { return name in this . _fields ; } set ( name , value , fileName ? ) { if ( typeof value !== "string" && ! isBlob ( value ) ) { throw new Error ( "formData.set can only accept a string or Blob" ) ; } if ( typeof value === "string" || isFile ( value ) ) { this . _fields [ name ] = [ value ] ; } else { this . _fields [ name ] = [ new File ( [ value ] , fileName || "unknown" ) ] ; } } forEach ( callbackfn , thisArg ? ) { Object . entries ( this . _fields ) . forEach ( ( [ name , values ] ) => { values . forEach ( value => callbackfn ( value , name , thisArg ) , thisArg ) ; } ) ; } entries ( ) { return Object . entries ( this . _fields ) . reduce ( ( entries , [ name , values ] ) => { values . forEach ( value => entries . push ( [ name , value ] ) ) ; return entries ; } , [ ] as [ string , FormDataEntryValue ] [ ] ) . values ( ) ; } keys ( ) { return Object . keys ( this . _fields ) . values ( ) ; } values ( ) { return Object . entries ( this . _fields ) . reduce ( ( results , [ name , values ] ) => { values . forEach ( value => results . push ( value ) ) ; return results ; } , [ ] as FormDataEntryValue [ ] ) . values ( ) ; } * [ Symbol . iterator ] ( ) { yield * this . entries ( ) ; } } export { NodeFormData as FormData } ;