2023-05-21 21:37:29 +02:00
|
|
|
import LRU from "lru-cache";
|
2023-10-09 21:53:44 +02:00
|
|
|
import type { Query, UpObject } from ".";
|
2023-10-07 13:13:00 +02:00
|
|
|
import { UpListing } from ".";
|
2023-05-21 21:37:29 +02:00
|
|
|
import type {
|
2023-06-28 18:44:08 +02:00
|
|
|
ADDRESS_TYPE,
|
2023-05-21 21:37:29 +02:00
|
|
|
Address,
|
|
|
|
AttributeListingResult,
|
|
|
|
EntityListing,
|
|
|
|
IJob,
|
|
|
|
IValue,
|
|
|
|
ListingResult,
|
|
|
|
PutInput,
|
|
|
|
PutResult,
|
|
|
|
StoreInfo,
|
|
|
|
VaultInfo,
|
|
|
|
} from "./types";
|
2023-10-07 13:00:34 +02:00
|
|
|
import type { UpEndWasmExtensions, AddressComponents } from "./wasm";
|
2023-06-05 12:56:33 +02:00
|
|
|
import debug from "debug";
|
|
|
|
const dbg = debug("upend:api");
|
2023-05-21 21:37:29 +02:00
|
|
|
|
2023-10-07 13:13:00 +02:00
|
|
|
export type { AddressComponents };
|
2023-06-28 14:26:11 +02:00
|
|
|
|
2023-05-21 21:37:29 +02:00
|
|
|
export class UpEndApi {
|
|
|
|
private instanceUrl = "";
|
2023-10-07 13:00:34 +02:00
|
|
|
private wasmExtensions: UpEndWasmExtensions | undefined = undefined;
|
2023-05-21 21:37:29 +02:00
|
|
|
|
|
|
|
private queryOnceLRU = new LRU<string, UpListing>({ max: 128 });
|
|
|
|
private inFlightRequests: { [key: string]: Promise<UpListing> | null } = {};
|
|
|
|
|
2023-10-07 17:28:26 +02:00
|
|
|
constructor(config: {
|
|
|
|
instanceUrl?: string;
|
|
|
|
wasmExtensions?: UpEndWasmExtensions;
|
|
|
|
}) {
|
|
|
|
this.setInstanceUrl(config.instanceUrl || "http://localhost:8093");
|
|
|
|
this.wasmExtensions = config.wasmExtensions;
|
2023-05-21 21:37:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public setInstanceUrl(apiUrl: string) {
|
|
|
|
this.instanceUrl = apiUrl.replace(/\/+$/g, "");
|
|
|
|
}
|
|
|
|
|
2023-05-22 20:57:06 +02:00
|
|
|
public get apiUrl() {
|
2023-05-21 21:37:29 +02:00
|
|
|
return this.instanceUrl + "/api";
|
|
|
|
}
|
|
|
|
|
|
|
|
public async fetchEntity(address: string): Promise<UpObject> {
|
2023-06-05 12:56:33 +02:00
|
|
|
dbg("Fetching Entity %s", address);
|
2023-05-21 21:37:29 +02:00
|
|
|
const entityFetch = await fetch(`${this.apiUrl}/obj/${address}`);
|
|
|
|
const entityResult = (await entityFetch.json()) as EntityListing;
|
|
|
|
const entityListing = new UpListing(entityResult.entries);
|
|
|
|
return entityListing.getObject(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async fetchEntry(address: string) {
|
2023-06-05 12:56:33 +02:00
|
|
|
dbg("Fetching entry %s", address);
|
2023-05-21 21:37:29 +02:00
|
|
|
const response = await fetch(`${this.apiUrl}/raw/${address}`);
|
|
|
|
const data = await response.json();
|
|
|
|
const listing = new UpListing({ address: data });
|
|
|
|
return listing.entries[0];
|
|
|
|
}
|
|
|
|
|
2023-10-09 21:53:44 +02:00
|
|
|
public async query(query: string | Query): Promise<UpListing> {
|
|
|
|
const queryStr = query.toString();
|
|
|
|
|
|
|
|
const cacheResult = this.queryOnceLRU.get(queryStr);
|
2023-05-21 21:37:29 +02:00
|
|
|
if (!cacheResult) {
|
2023-10-09 21:53:44 +02:00
|
|
|
if (!this.inFlightRequests[queryStr]) {
|
2023-06-05 12:56:33 +02:00
|
|
|
dbg(`Querying: ${query}`);
|
2023-10-09 21:53:44 +02:00
|
|
|
this.inFlightRequests[queryStr] = new Promise((resolve, reject) => {
|
2023-05-21 21:37:29 +02:00
|
|
|
fetch(`${this.apiUrl}/query`, {
|
|
|
|
method: "POST",
|
2023-10-09 21:53:44 +02:00
|
|
|
body: queryStr,
|
2023-05-21 21:37:29 +02:00
|
|
|
keepalive: true,
|
|
|
|
})
|
|
|
|
.then(async (response) => {
|
2023-10-09 22:13:11 +02:00
|
|
|
if (!response.ok) {
|
|
|
|
reject(
|
|
|
|
`Query ${queryStr} failed: ${response.status} ${
|
|
|
|
response.statusText
|
|
|
|
}: ${await response.text()}}`
|
|
|
|
);
|
|
|
|
}
|
2023-05-21 21:37:29 +02:00
|
|
|
resolve(new UpListing(await response.json()));
|
2023-10-09 21:53:44 +02:00
|
|
|
this.inFlightRequests[queryStr] = null;
|
2023-05-21 21:37:29 +02:00
|
|
|
})
|
|
|
|
.catch((err) => reject(err));
|
|
|
|
});
|
|
|
|
} else {
|
2023-10-09 21:53:44 +02:00
|
|
|
dbg(`Chaining request for ${queryStr}...`);
|
2023-05-21 21:37:29 +02:00
|
|
|
}
|
2023-10-09 21:53:44 +02:00
|
|
|
return await (this.inFlightRequests[queryStr] as Promise<UpListing>); // TODO?
|
2023-05-21 21:37:29 +02:00
|
|
|
} else {
|
2023-10-09 21:53:44 +02:00
|
|
|
dbg(`Returning cached: ${queryStr}`);
|
2023-05-21 21:37:29 +02:00
|
|
|
return cacheResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async putEntry(input: PutInput): Promise<PutResult> {
|
2023-06-05 12:56:33 +02:00
|
|
|
dbg("Putting %O", input);
|
2023-05-21 21:37:29 +02:00
|
|
|
const response = await fetch(`${this.apiUrl}/obj`, {
|
|
|
|
method: "PUT",
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
body: JSON.stringify(input),
|
|
|
|
});
|
|
|
|
|
|
|
|
return await response.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async putEntityAttribute(
|
|
|
|
entity: Address,
|
|
|
|
attribute: string,
|
2023-06-19 16:45:55 +02:00
|
|
|
value: IValue,
|
|
|
|
provenance?: string
|
2023-05-21 21:37:29 +02:00
|
|
|
): Promise<Address> {
|
2023-09-23 17:59:52 +02:00
|
|
|
dbg("Putting %s = %o for %s (%s)", attribute, value, entity, provenance);
|
2023-06-19 16:45:55 +02:00
|
|
|
let url = `${this.apiUrl}/obj/${entity}/${attribute}`;
|
|
|
|
if (provenance) {
|
|
|
|
url += `?provenance=${provenance}`;
|
|
|
|
}
|
|
|
|
const response = await fetch(url, {
|
2023-05-21 21:37:29 +02:00
|
|
|
method: "PUT",
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
body: JSON.stringify(value),
|
|
|
|
});
|
|
|
|
|
|
|
|
return await response.json();
|
|
|
|
}
|
|
|
|
|
2023-05-27 15:52:18 +02:00
|
|
|
public async putBlob(fileOrUrl: File | URL): Promise<PutResult> {
|
2023-06-05 12:56:33 +02:00
|
|
|
dbg("Putting Blob: %O", fileOrUrl);
|
2023-05-21 21:37:29 +02:00
|
|
|
const formData = new FormData();
|
2023-05-27 15:52:18 +02:00
|
|
|
if (fileOrUrl instanceof File) {
|
|
|
|
formData.append(fileOrUrl.name, fileOrUrl);
|
|
|
|
} else {
|
|
|
|
formData.append("@url", fileOrUrl.toString());
|
|
|
|
}
|
2023-05-21 21:37:29 +02:00
|
|
|
|
|
|
|
const response = await fetch(`${this.apiUrl}/blob`, {
|
|
|
|
method: "PUT",
|
|
|
|
body: formData,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw Error(await response.text());
|
|
|
|
}
|
|
|
|
|
|
|
|
return await response.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async deleteEntry(address: Address): Promise<void> {
|
2023-06-05 12:56:33 +02:00
|
|
|
dbg("Deleting entry %s", address);
|
2023-05-21 21:37:29 +02:00
|
|
|
await fetch(`${this.apiUrl}/obj/${address}`, { method: "DELETE" });
|
|
|
|
}
|
|
|
|
|
2023-10-20 20:55:10 +02:00
|
|
|
public getRaw(address: Address, preview = false) {
|
|
|
|
return `${this.apiUrl}/${preview ? "thumb" : "raw"}/${address}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async fetchRaw(address: Address, preview = false) {
|
2023-06-05 12:56:33 +02:00
|
|
|
dbg("Getting %s raw (preview = %s)", address, preview);
|
2023-10-20 20:55:10 +02:00
|
|
|
return await fetch(this.getRaw(address, preview));
|
2023-05-21 21:37:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async refreshVault() {
|
2023-06-05 12:56:33 +02:00
|
|
|
dbg("Triggering vault refresh");
|
2023-05-21 21:37:29 +02:00
|
|
|
return await fetch(`${this.apiUrl}/refresh`, { method: "POST" });
|
|
|
|
}
|
|
|
|
|
|
|
|
public async nativeOpen(address: Address) {
|
2023-06-05 12:56:33 +02:00
|
|
|
dbg("Opening %s natively", address);
|
2023-05-21 21:37:29 +02:00
|
|
|
return fetch(`${this.apiUrl}/raw/${address}?native=1`);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async fetchRoots(): Promise<ListingResult> {
|
2023-06-05 12:56:33 +02:00
|
|
|
dbg("Fetching hierarchical roots...");
|
2023-05-21 21:37:29 +02:00
|
|
|
const response = await fetch(`${this.apiUrl}/hier_roots`);
|
2023-06-05 12:56:33 +02:00
|
|
|
const roots = await response.json();
|
|
|
|
dbg("Hierarchical roots: %O", roots);
|
|
|
|
return roots;
|
2023-05-21 21:37:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async fetchJobs(): Promise<IJob[]> {
|
|
|
|
const response = await fetch(`${this.apiUrl}/jobs`);
|
|
|
|
return await response.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async fetchAllAttributes(): Promise<AttributeListingResult> {
|
2023-06-05 12:56:33 +02:00
|
|
|
dbg("Fetching all attributes...");
|
2023-05-21 21:37:29 +02:00
|
|
|
const response = await fetch(`${this.apiUrl}/all/attributes`);
|
2023-06-05 12:56:33 +02:00
|
|
|
const result = await response.json();
|
|
|
|
dbg("All attributes: %O", result);
|
|
|
|
return await result;
|
2023-05-21 21:37:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async fetchInfo(): Promise<VaultInfo> {
|
|
|
|
const response = await fetch(`${this.apiUrl}/info`);
|
2023-06-05 12:56:33 +02:00
|
|
|
const result = await response.json();
|
|
|
|
dbg("Vault info: %O", result);
|
|
|
|
return result;
|
2023-05-21 21:37:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async fetchStoreInfo(): Promise<{ [key: string]: StoreInfo }> {
|
2023-06-19 18:51:06 +02:00
|
|
|
const response = await fetch(`${this.apiUrl}/stats/store`);
|
2023-06-05 12:56:33 +02:00
|
|
|
const result = await response.json();
|
|
|
|
dbg("Store info: %O");
|
|
|
|
return await result;
|
2023-05-21 21:37:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async getAddress(
|
2023-11-29 14:10:44 +01:00
|
|
|
input: { urlContent: string } | ADDRESS_TYPE
|
2023-05-21 21:37:29 +02:00
|
|
|
): Promise<string> {
|
2023-08-25 23:35:29 +02:00
|
|
|
let response: Response;
|
2023-06-28 14:26:11 +02:00
|
|
|
if (typeof input === "string") {
|
2023-10-07 13:00:34 +02:00
|
|
|
if (this.wasmExtensions) {
|
|
|
|
await this.wasmExtensions.init();
|
|
|
|
return this.wasmExtensions.AddressTypeConstants[input];
|
2023-06-28 18:36:47 +02:00
|
|
|
}
|
2023-06-28 14:26:11 +02:00
|
|
|
response = await fetch(`${this.apiUrl}/address?type=${input}`);
|
2023-05-21 21:37:29 +02:00
|
|
|
} else {
|
2023-11-29 14:42:31 +01:00
|
|
|
if ("urlContent" in input) {
|
2023-06-28 14:26:11 +02:00
|
|
|
response = await fetch(
|
|
|
|
`${this.apiUrl}/address?url_content=${input.urlContent}`
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
throw new Error("Input cannot be empty.");
|
|
|
|
}
|
2023-05-21 21:37:29 +02:00
|
|
|
}
|
2023-06-05 12:56:33 +02:00
|
|
|
const result = await response.json();
|
|
|
|
dbg("Address for %o = %s", input, result);
|
|
|
|
return result;
|
2023-05-21 21:37:29 +02:00
|
|
|
}
|
2023-06-28 14:26:11 +02:00
|
|
|
|
|
|
|
public async addressToComponents(
|
|
|
|
address: string
|
|
|
|
): Promise<AddressComponents> {
|
2023-10-07 13:00:34 +02:00
|
|
|
if (!this.wasmExtensions) {
|
|
|
|
throw new Error("WASM extensions not supplied.");
|
|
|
|
}
|
|
|
|
await this.wasmExtensions.init();
|
|
|
|
return this.wasmExtensions.addr_to_components(address);
|
2023-06-28 14:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async componentsToAddress(
|
|
|
|
components: AddressComponents
|
|
|
|
): Promise<string> {
|
2023-10-07 13:00:34 +02:00
|
|
|
if (!this.wasmExtensions) {
|
|
|
|
throw new Error("WASM extensions not initialized.");
|
2023-06-28 14:26:11 +02:00
|
|
|
}
|
2023-10-07 13:00:34 +02:00
|
|
|
await this.wasmExtensions.init();
|
|
|
|
return this.wasmExtensions.components_to_addr(components);
|
2023-06-28 14:26:11 +02:00
|
|
|
}
|
2023-11-08 22:14:41 +01:00
|
|
|
|
|
|
|
public async getVaultOptions(): Promise<VaultOptions> {
|
|
|
|
const response = await fetch(`${this.apiUrl}/options`);
|
|
|
|
return await response.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async setVaultOptions(options: VaultOptions): Promise<void> {
|
|
|
|
const payload: Record<string, unknown> = {};
|
|
|
|
|
|
|
|
if (options.blob_mode) {
|
|
|
|
const blob_mode: Record<string, unknown> = {};
|
|
|
|
blob_mode[options.blob_mode] = null;
|
|
|
|
payload["blob_mode"] = blob_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await fetch(`${this.apiUrl}/options`, {
|
|
|
|
method: "PUT",
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
body: JSON.stringify(payload),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw Error(await response.text());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type VaultBlobMode = "Flat" | "Mirror" | "Incoming";
|
|
|
|
export interface VaultOptions {
|
|
|
|
blob_mode: VaultBlobMode;
|
2023-05-21 21:37:29 +02:00
|
|
|
}
|