From f09a502fe7f1cb7583265c91ff95bbb4740720b0 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sat, 12 Nov 2022 19:18:55 +0100 Subject: fix URLSearchParams and HTTP typing issue --- packages/taler-util/src/url.ts | 7 +++++-- packages/taler-util/src/whatwg-url.ts | 20 ++++++++++++++++---- .../taler-wallet-webextension/src/browserHttpLib.ts | 11 ++++++++++- .../src/serviceWorkerHttpLib.ts | 17 ++++++++++++++++- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/packages/taler-util/src/url.ts b/packages/taler-util/src/url.ts index eb7248b7b..149997f3f 100644 --- a/packages/taler-util/src/url.ts +++ b/packages/taler-util/src/url.ts @@ -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 diff --git a/packages/taler-util/src/whatwg-url.ts b/packages/taler-util/src/whatwg-url.ts index a0fe55d8f..991528ae6 100644 --- a/packages/taler-util/src/whatwg-url.ts +++ b/packages/taler-util/src/whatwg-url.ts @@ -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; @@ -2104,4 +2116,4 @@ export class URLImpl { // FIXME: type! _url: any; _query: any; -} \ No newline at end of file +} diff --git a/packages/taler-wallet-webextension/src/browserHttpLib.ts b/packages/taler-wallet-webextension/src/browserHttpLib.ts index d318e8201..26fa8eb11 100644 --- a/packages/taler-wallet-webextension/src/browserHttpLib.ts +++ b/packages/taler-wallet-webextension/src/browserHttpLib.ts @@ -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) { - myRequest.send(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(); } diff --git a/packages/taler-wallet-webextension/src/serviceWorkerHttpLib.ts b/packages/taler-wallet-webextension/src/serviceWorkerHttpLib.ts index b9648ced8..74c7f161d 100644 --- a/packages/taler-wallet-webextension/src/serviceWorkerHttpLib.ts +++ b/packages/taler-wallet-webextension/src/serviceWorkerHttpLib.ts @@ -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 }); -- cgit v1.2.3