diff options
author | Florian Dold <florian@dold.me> | 2022-11-11 20:52:45 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2022-11-11 20:52:53 +0100 |
commit | 233a354b4728b5efc76d19a530ad85a3a3e2a3d2 (patch) | |
tree | 7e426da749ba8f97bc8a5f4767f31fbc254675a9 /packages/taler-wallet-embedded/src | |
parent | 22290020fba91253c9885eb9fa193a0dd98fa72f (diff) | |
download | wallet-core-233a354b4728b5efc76d19a530ad85a3a3e2a3d2.tar.xz |
fixes and polyfills for quickjs
Diffstat (limited to 'packages/taler-wallet-embedded/src')
-rw-r--r-- | packages/taler-wallet-embedded/src/wallet-qjs.ts | 112 |
1 files changed, 108 insertions, 4 deletions
diff --git a/packages/taler-wallet-embedded/src/wallet-qjs.ts b/packages/taler-wallet-embedded/src/wallet-qjs.ts index 69cecb828..21796870a 100644 --- a/packages/taler-wallet-embedded/src/wallet-qjs.ts +++ b/packages/taler-wallet-embedded/src/wallet-qjs.ts @@ -23,6 +23,7 @@ import { getErrorDetailFromException, handleWorkerError, handleWorkerMessage, + Headers, HttpRequestLibrary, HttpRequestOptions, HttpResponse, @@ -31,13 +32,16 @@ import { SetTimeoutTimerAPI, SynchronousCryptoWorkerFactoryPlain, Wallet, + WalletApiOperation, } from "@gnu-taler/taler-wallet-core"; import { CoreApiEnvelope, CoreApiResponse, CoreApiResponseSuccess, + j2s, Logger, + setGlobalLogLevelFromString, WalletNotification, } from "@gnu-taler/taler-util"; import { BridgeIDBFactory } from "@gnu-taler/idb-bridge"; @@ -45,6 +49,31 @@ import { MemoryBackend } from "@gnu-taler/idb-bridge"; import { shimIndexedDB } from "@gnu-taler/idb-bridge"; import { IDBFactory } from "@gnu-taler/idb-bridge"; +import * as _qjsOsImp from "os"; + +const textDecoder = new TextDecoder(); +const textEncoder = new TextEncoder(); + +export interface QjsHttpResp { + status: number; + data: ArrayBuffer; +} + +export interface QjsHttpOptions { + method: string; + debug?: boolean; + data?: ArrayBuffer; + headers?: string[]; +} + +export interface QjsOsLib { + // Not async! + fetchHttp(url: string, options?: QjsHttpOptions): QjsHttpResp; +} + +// This is not the nodejs "os" module, but the qjs "os" module. +const qjsOs: QjsOsLib = _qjsOsImp as any; + export { handleWorkerError, handleWorkerMessage }; const logger = new Logger("taler-wallet-embedded/index.ts"); @@ -54,20 +83,73 @@ export class NativeHttpLib implements HttpRequestLibrary { url: string, opt?: HttpRequestOptions | undefined, ): Promise<HttpResponse> { - throw new Error("Method not implemented."); + return this.fetch(url, { + method: "GET", + ...opt, + }); } postJson( url: string, body: any, opt?: HttpRequestOptions | undefined, ): Promise<HttpResponse> { - throw new Error("Method not implemented."); + return this.fetch(url, { + method: "POST", + body, + ...opt, + }); } - fetch( + async fetch( url: string, opt?: HttpRequestOptions | undefined, ): Promise<HttpResponse> { - throw new Error("Method not implemented."); + const method = opt?.method ?? "GET"; + let data: ArrayBuffer | undefined = undefined; + let headers: string[] = []; + if (opt?.headers) { + for (let headerName of Object.keys(opt.headers)) { + headers.push(`${headerName}: ${opt.headers[headerName]}`); + } + } + if (method.toUpperCase() === "POST") { + if (opt?.body) { + if (typeof opt.body === "string") { + data = textEncoder.encode(opt.body).buffer; + } else if (ArrayBuffer.isView(opt.body)) { + data = opt.body.buffer; + } else if (opt.body instanceof ArrayBuffer) { + data = opt.body; + } else if (typeof opt.body === "object") { + data = textEncoder.encode(JSON.stringify(opt.body)).buffer; + } + } else { + data = new ArrayBuffer(0); + } + } + console.log(`data type ${data?.constructor.name}`); + console.log(`data: ${j2s(data)}`); + const res = qjsOs.fetchHttp(url, { + method, + data, + headers, + }); + return { + requestMethod: method, + headers: new Headers(), + async bytes() { + return res.data; + }, + json() { + const text = textDecoder.decode(res.data); + return JSON.parse(text); + }, + async text() { + const text = textDecoder.decode(res.data); + return text; + }, + requestUrl: url, + status: res.status, + }; } } @@ -268,6 +350,7 @@ class NativeWalletMessageHandler { } export function installNativeWalletListener(): void { + setGlobalLogLevelFromString("trace"); const handler = new NativeWalletMessageHandler(); const onMessage = async (msgStr: any): Promise<void> => { if (typeof msgStr !== "string") { @@ -311,3 +394,24 @@ export function installNativeWalletListener(): void { // @ts-ignore globalThis.installNativeWalletListener = installNativeWalletListener; + +// @ts-ignore +globalThis.makeWallet = getWallet; + +export async function testWithGv() { + const w = await getWallet(); + await w.wallet.client.call(WalletApiOperation.InitWallet, {}); + await w.wallet.client.call(WalletApiOperation.RunIntegrationTest, { + amountToSpend: "KUDOS:1", + amountToWithdraw: "KUDOS:3", + bankBaseUrl: "https://bank.demo.taler.net/demobanks/default/access-api/", + exchangeBaseUrl: "https://exchange.demo.taler.net/", + merchantBaseUrl: "https://backend.demo.taler.net/", + }); + await w.wallet.runTaskLoop({ + stopWhenDone: true, + }); +} + +// @ts-ignore +globalThis.testWithGv = testWithGv; |