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

View File

@ -347,8 +347,7 @@ function isASCIIHex(c: number) {
export class URLSearchParamsImpl { export class URLSearchParamsImpl {
_list: any[]; _list: any[];
_url: any; _url: any;
constructor(constructorArgs: any[], { doNotStripQMark = false }: any) { constructor(init: any, { doNotStripQMark = false }: any = {}) {
let init = constructorArgs[0];
this._list = []; this._list = [];
this._url = null; this._url = null;
@ -425,6 +424,19 @@ export class URLSearchParamsImpl {
return output; 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) { has(name: string) {
for (const tuple of this._list) { for (const tuple of this._list) {
if (tuple[0] === name) { 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 // 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. // question mark by default. Therefore the doNotStripQMark hack is used.
this._query = new URLSearchParamsImpl([query], { this._query = new URLSearchParamsImpl(query, {
doNotStripQMark: true, doNotStripQMark: true,
}); });
this._query._url = this; this._query._url = this;

View File

@ -27,6 +27,7 @@ import {
import { import {
Logger, Logger,
RequestThrottler, RequestThrottler,
stringToBytes,
TalerErrorCode, TalerErrorCode,
} from "@gnu-taler/taler-util"; } from "@gnu-taler/taler-util";
@ -70,7 +71,15 @@ export class BrowserHttpLib implements HttpRequestLibrary {
} }
myRequest.responseType = "arraybuffer"; myRequest.responseType = "arraybuffer";
if (requestBody) { 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 { } else {
myRequest.send(); 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, { const response = await fetch(requestUrl, {
headers: requestHeader, headers: requestHeader,
body: requestBody, body: myBody,
method: requestMethod, method: requestMethod,
// timeout: options?.timeout // timeout: options?.timeout
}); });