wip: add wasm to `upend_js` lib

feat/type-attributes
Tomáš Mládek 2023-06-28 14:26:11 +02:00
parent 0fef002c0b
commit 27a1ee1051
6 changed files with 101 additions and 17 deletions

View File

@ -1,5 +1,6 @@
*.js
node_modules
*.wasm
.pnp.*
.yarn/*

View File

@ -12,23 +12,40 @@ import type {
StoreInfo,
VaultInfo,
} from "./types";
import init_wasm from "upend_wasm";
import {
AddressComponents,
addr_to_components,
components_to_addr,
} from "upend_wasm";
import debug from "debug";
const dbg = debug("upend:api");
export { AddressComponents };
export class UpEndApi {
private instanceUrl = "";
private wasmPath: string | undefined;
private wasmInitialized = false;
private queryOnceLRU = new LRU<string, UpListing>({ max: 128 });
private inFlightRequests: { [key: string]: Promise<UpListing> | null } = {};
constructor(instanceUrl = "") {
constructor(instanceUrl = "", wasmPath?: string) {
this.setInstanceUrl(instanceUrl);
if (wasmPath) {
this.setWasmPath(wasmPath);
}
}
public setInstanceUrl(apiUrl: string) {
this.instanceUrl = apiUrl.replace(/\/+$/g, "");
}
public setWasmPath(wasmPath: string) {
this.wasmPath = wasmPath;
}
public get apiUrl() {
return this.instanceUrl + "/api";
}
@ -181,24 +198,61 @@ export class UpEndApi {
}
public async getAddress(
input: { attribute: string } | { url: string } | { urlContent: string }
input:
| { attribute: string }
| { url: string }
| { urlContent: string }
| "Hash"
| "Uuid"
| "Attribute"
| "Url"
): Promise<string> {
let response;
if ("attribute" in input) {
response = await fetch(
`${this.apiUrl}/address?attribute=${input.attribute}`
);
} else if ("url" in input) {
response = await fetch(`${this.apiUrl}/address?url=${input.url}`);
} else if ("urlContent" in input) {
response = await fetch(
`${this.apiUrl}/address?url_content=${input.urlContent}`
);
if (typeof input === "string") {
response = await fetch(`${this.apiUrl}/address?type=${input}`);
} else {
throw new Error("Input cannot be empty.");
if ("attribute" in input) {
response = await fetch(
`${this.apiUrl}/address?attribute=${input.attribute}`
);
} else if ("url" in input) {
response = await fetch(`${this.apiUrl}/address?url=${input.url}`);
} else if ("urlContent" in input) {
response = await fetch(
`${this.apiUrl}/address?url_content=${input.urlContent}`
);
} else {
throw new Error("Input cannot be empty.");
}
}
const result = await response.json();
dbg("Address for %o = %s", input, result);
return result;
}
public async addressToComponents(
address: string
): Promise<AddressComponents> {
await this.initWasm();
return addr_to_components(address);
}
public async componentsToAddress(
components: AddressComponents
): Promise<string> {
await this.initWasm();
return components_to_addr(components);
}
private async initWasm(): Promise<void> {
if (!this.wasmInitialized) {
if (!this.wasmPath) {
throw new Error(
"Path to WASM file not specified, cannot initialize WASM extensions."
);
}
await init_wasm(this.wasmPath);
this.wasmInitialized = true;
}
}
}

View File

@ -3,7 +3,7 @@
"version": "0.0.1",
"description": "Client library to interact with the UpEnd system.",
"scripts": {
"build": "tsc",
"build": "tsc && cp -v node_modules/upend_wasm/*.wasm .",
"test": "ava",
"lint": "eslint ."
},
@ -20,6 +20,7 @@
"packageManager": "yarn@3.1.1",
"dependencies": {
"debug": "^4.3.4",
"lru-cache": "^7.0.0"
"lru-cache": "^7.0.0",
"upend_wasm": "file:../upend_wasm/pkg"
}
}

View File

@ -3632,9 +3632,17 @@ __metadata:
eslint: ^8.7.0
lru-cache: ^7.0.0
typescript: ^4.4.4
upend_wasm: "file:../upend_wasm/pkg"
languageName: unknown
linkType: soft
"upend_wasm@file:../upend_wasm/pkg::locator=upend%40workspace%3A.":
version: 0.1.0
resolution: "upend_wasm@file:../upend_wasm/pkg#../upend_wasm/pkg::hash=10772a&locator=upend%40workspace%3A."
checksum: 26c11aa145ba9a5209dac83cbf6c4bc6236d88364a9797030afe9cc3452f1f5daf23014926dfe2f1aea5289494ff166f8c897e766c808af939f1d953ddc0d483
languageName: node
linkType: hard
"uri-js@npm:^4.2.2":
version: 4.4.1
resolution: "uri-js@npm:4.4.1"

View File

@ -4364,7 +4364,19 @@ update-notifier@6.0.2:
upend@../tools/upend_js:
version "0.0.1"
dependencies:
debug "^4.3.4"
lru-cache "^7.0.0"
upend_wasm "file:../../../.cache/yarn/v6/npm-upend-0.0.1-97749447-daec-4450-84bd-3a9efd2d337b-1687955082927/node_modules/upend_wasm/pkg"
"upend@file:../tools/upend_js":
version "0.0.1"
dependencies:
debug "^4.3.4"
lru-cache "^7.0.0"
upend_wasm "file:../../../.cache/yarn/v6/npm-upend-0.0.1-b03bbb87-3597-4b59-9114-fd6071909861-1687955082756/node_modules/upend_wasm/pkg"
"upend_wasm@file:../tools/upend_wasm/pkg":
version "0.1.0"
uri-js@^4.2.2:
version "4.4.1"

View File

@ -12585,11 +12585,19 @@ __metadata:
"upend@file:../tools/upend_js::locator=upend-kestrel%40workspace%3A.":
version: 0.0.1
resolution: "upend@file:../tools/upend_js#../tools/upend_js::hash=cda57f&locator=upend-kestrel%40workspace%3A."
resolution: "upend@file:../tools/upend_js#../tools/upend_js::hash=2d3140&locator=upend-kestrel%40workspace%3A."
dependencies:
debug: ^4.3.4
lru-cache: ^7.0.0
checksum: 551abb5f6c2d07e1350993d27ca835fea005172ad66889e41fa5a9793ad414788ecfce2c966f526d3347d08e1731f1945c695e95aae15dd334ca17b1bc1fd195
upend_wasm: "file:../upend_wasm/pkg"
checksum: 3ac07a91311fe2fbf25d1e95dd8231b87d82457457c242c6095ee6419f3f5997a037a6f0eaa826e90ba16f70985750da2f7af2a8f4da25f421370e42fbb2def1
languageName: node
linkType: hard
"upend_wasm@file:../upend_wasm/pkg::locator=upend%40file%3A..%2Ftools%2Fupend_js%23..%2Ftools%2Fupend_js%3A%3Ahash%3D2d3140%26locator%3Dupend-kestrel%2540workspace%253A.":
version: 0.1.0
resolution: "upend_wasm@file:../upend_wasm/pkg#../upend_wasm/pkg::hash=10772a&locator=upend%40file%3A..%2Ftools%2Fupend_js%23..%2Ftools%2Fupend_js%3A%3Ahash%3D2d3140%26locator%3Dupend-kestrel%2540workspace%253A."
checksum: 26c11aa145ba9a5209dac83cbf6c4bc6236d88364a9797030afe9cc3452f1f5daf23014926dfe2f1aea5289494ff166f8c897e766c808af939f1d953ddc0d483
languageName: node
linkType: hard