Created
February 27, 2022 14:44
-
-
Save mnixry/39a800063b7fda2b4d966a43ea65318e to your computer and use it in GitHub Desktop.
Polyfill for multipart-form Blob/File read and creation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import '@ssttevee/cfw-formdata-polyfill'; | |
import * as poly from '@ssttevee/blob-ponyfill'; | |
export const BuiltinBlob = Blob, | |
BuiltinFile = File, | |
BuiltinFormData = FormData; | |
class OverrideFormData extends BuiltinFormData { | |
public append(name: string, value: string | Blob, filename?: string): void { | |
const reader = new poly.FileReaderSync(); | |
if (value instanceof poly.File) { | |
value = new BuiltinFile( | |
[reader.readAsArrayBuffer(value)], | |
filename ?? value.name, | |
{ | |
type: value.type, | |
lastModified: value.lastModified, | |
} | |
); | |
} else if (value instanceof poly.Blob) { | |
value = new BuiltinBlob([reader.readAsArrayBuffer(value)]); | |
} | |
return filename | |
? super.append(name, value, filename) | |
: super.append(name, value); | |
} | |
} | |
globalThis.File = poly.File as typeof File; | |
globalThis.Blob = poly.Blob as typeof Blob; | |
globalThis.FormData = OverrideFormData as typeof FormData; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment