From 4caabae895948379afc1f9540260401c64457af9 Mon Sep 17 00:00:00 2001 From: Bruno Windels <274386+bwindels@users.noreply.github.com> Date: Wed, 15 Jun 2022 10:15:15 +0200 Subject: [PATCH] extract map -> formdata conversion and also suppor this for xhr --- src/platform/web/dom/request/common.js | 15 +++++++++++++++ src/platform/web/dom/request/fetch.js | 15 ++------------- src/platform/web/dom/request/xhr.js | 5 ++++- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/platform/web/dom/request/common.js b/src/platform/web/dom/request/common.js index d97456aa..c073cf6b 100644 --- a/src/platform/web/dom/request/common.js +++ b/src/platform/web/dom/request/common.js @@ -27,6 +27,21 @@ export function addCacheBuster(urlStr, random = Math.random) { return urlStr + `_cacheBuster=${Math.ceil(random() * Number.MAX_SAFE_INTEGER)}`; } +export function mapAsFormData(map) { + const formData = new FormData(); + for (const [name, value] of map) { + let filename; + // Special case {name: string, blob: BlobHandle} to set a filename. + // This is the format returned by platform.openFile + if (value.blob?.nativeBlob && value.name) { + formData.set(name, value.blob.nativeBlob, value.name); + } else { + formData.set(name, value); + } + } + return formData; +} + export function tests() { return { "add cache buster": assert => { diff --git a/src/platform/web/dom/request/fetch.js b/src/platform/web/dom/request/fetch.js index 1d2ef3c0..d50f6194 100644 --- a/src/platform/web/dom/request/fetch.js +++ b/src/platform/web/dom/request/fetch.js @@ -20,7 +20,7 @@ import { ConnectionError } from "../../../../matrix/error.js"; import {abortOnTimeout} from "../../../../utils/timeout"; -import {addCacheBuster} from "./common.js"; +import {addCacheBuster, mapAsFormData} from "./common.js"; import {xhrRequest} from "./xhr.js"; class RequestResult { @@ -71,18 +71,7 @@ export function createFetchRequest(createTimeout, serviceWorkerHandler) { body = body.nativeBlob; } if (body instanceof Map) { - const formData = new FormData(); - for (const [name, value] of body) { - let filename; - // Special case {name: string, blob: BlobHandle} to set a filename. - // This is the format returned by platform.openFile - if (value.blob?.nativeBlob && value.name) { - formData.set(name, value.blob.nativeBlob, value.name); - } else { - formData.set(name, value); - } - } - body = formData; + body = mapAsFormData(body); } let options = {method, body}; if (controller) { diff --git a/src/platform/web/dom/request/xhr.js b/src/platform/web/dom/request/xhr.js index c3ad8ae8..fba7123e 100644 --- a/src/platform/web/dom/request/xhr.js +++ b/src/platform/web/dom/request/xhr.js @@ -18,7 +18,7 @@ import { AbortError, ConnectionError } from "../../../../matrix/error.js"; -import {addCacheBuster} from "./common.js"; +import {addCacheBuster, mapAsFormData} from "./common.js"; class RequestResult { constructor(promise, xhr) { @@ -94,6 +94,9 @@ export function xhrRequest(url, options) { if (body?.nativeBlob) { body = body.nativeBlob; } + if (body instanceof Map) { + body = mapAsFormData(body); + } xhr.send(body || null); return new RequestResult(promise, xhr);