Back

Version 11

2/28/2024
import { html } from "https://esm.town/v/postpostscript/html";
import { getValEndpointFromUrl, getValNameFromUrl } from "https://esm.town/v/postpostscript/meta";
import { blob } from "https://esm.town/v/std/blob";

export const URL = getValEndpointFromUrl(import.meta.url);
export const DEFAULT_PREFIX = `${getValNameFromUrl(import.meta.url)}:`;

export async function poll(url = URL, tries = 10, retryMs = 1000) {
for (let i = 0; i < tries; i++) {
try {
const res = fetch(url);
} catch (e) {
console.log("failed", url);
}
await new Promise((resolve) => setTimeout(resolve, retryMs));
}
}

export function provideBlob(getValue: (() => Promise<any>) | Promise<any>, prefix = DEFAULT_PREFIX) {
const id = crypto.randomUUID();

(async () => {
let value;
try {
value = getValue instanceof Promise
? getValue
: await getValue();
} catch (e) {
value = {
"error": e.message,
};
}
await blob.set(`${prefix}${id}`, value);
})();

const url = `${URL}/${id}`;
postpostscript-provideblob.web.val.run
Updated: February 29, 2024