fix URLSearchParams and HTTP typing issue

This commit is contained in:
Florian Dold 2022-11-12 19:18:55 +01:00
parent 233a354b47
commit f09a502fe7
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
4 changed files with 47 additions and 8 deletions

View File

@ -82,9 +82,12 @@ export interface URLCtor {
delete Object.prototype.__magic__;
})();
// Use native or pure JS URL implementation?
const useOwnUrlImp = true;
// @ts-ignore
let _URL = globalThis.URL;
if (!_URL) {
if (useOwnUrlImp || !_URL) {
// @ts-ignore
globalThis.URL = _URL = URLImpl;
// @ts-ignore
@ -96,7 +99,7 @@ export const URL: URLCtor = _URL;
// @ts-ignore
let _URLSearchParams = globalThis.URLSearchParams;
if (!_URLSearchParams) {
if (useOwnUrlImp || !_URLSearchParams) {
// @ts-ignore
globalThis.URLSearchParams = URLSearchParamsImpl;
// @ts-ignore

View File

@ -347,8 +347,7 @@ function isASCIIHex(c: number) {
export class URLSearchParamsImpl {
_list: any[];
_url: any;
constructor(constructorArgs: any[], { doNotStripQMark = false }: any) {
let init = constructorArgs[0];
constructor(init: any, { doNotStripQMark = false }: any = {}) {
this._list = [];
this._url = null;
@ -425,6 +424,19 @@ export class URLSearchParamsImpl {
return output;
}
forEach(
callbackfn: (
value: string,
key: string,
parent: URLSearchParamsImpl,
) => void,
thisArg?: any,
): void {
for (const tuple of this._list) {
callbackfn.call(thisArg, tuple[1], tuple[0], this);
}
}
has(name: string) {
for (const tuple of this._list) {
if (tuple[0] === name) {
@ -1916,7 +1928,7 @@ export class URLImpl {
// We cannot invoke the "new URLSearchParams object" algorithm without going through the constructor, which strips
// question mark by default. Therefore the doNotStripQMark hack is used.
this._query = new URLSearchParamsImpl([query], {
this._query = new URLSearchParamsImpl(query, {
doNotStripQMark: true,
});
this._query._url = this;

View File

@ -27,6 +27,7 @@ import {
import {
Logger,
RequestThrottler,
stringToBytes,
TalerErrorCode,
} from "@gnu-taler/taler-util";
@ -70,7 +71,15 @@ export class BrowserHttpLib implements HttpRequestLibrary {
}
myRequest.responseType = "arraybuffer";
if (requestBody) {
if (requestBody instanceof ArrayBuffer) {
myRequest.send(requestBody);
} else if (ArrayBuffer.isView(requestBody)) {
myRequest.send(requestBody);
} else if (typeof requestBody === "string") {
myRequest.send(requestBody);
} else {
myRequest.send(JSON.stringify(requestBody));
}
} else {
myRequest.send();
}

View File

@ -55,9 +55,24 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary {
);
}
let myBody: BodyInit | undefined = undefined;
if (requestBody != null) {
if (typeof requestBody === "string") {
myBody = requestBody;
} else if (requestBody instanceof ArrayBuffer) {
myBody = requestBody;
} else if (ArrayBuffer.isView(requestBody)) {
myBody = requestBody;
} else if (typeof requestBody === "object") {
myBody = JSON.stringify(myBody);
} else {
throw Error("unsupported request body type");
}
}
const response = await fetch(requestUrl, {
headers: requestHeader,
body: requestBody,
body: myBody,
method: requestMethod,
// timeout: options?.timeout
});