1/** @jsxImportSource https://esm.sh/hono@latest/jsx **/
2
3import { modifyFetchHandler } from "https://esm.town/v/andreterron/codeOnValTown?v=50";
4import { iframeHandler } from "https://esm.town/v/nbbaier/iframeHandler";
5import { resetStyle } from "https://esm.town/v/nbbaier/resetStyle";
16import { verifyToken } from "https://esm.town/v/pomdtr/verifyToken";
17import { ResultSet, sqlite } from "https://esm.town/v/std/sqlite";
18import { reloadOnSaveFetchMiddleware } from "https://esm.town/v/stevekrouse/reloadOnSave";
19import { Hono } from "npm:hono";
20import type { FC } from "npm:hono/jsx";
175});
176
177export const handler = app.fetch;
178export default iframeHandler(modifyFetchHandler(passwordAuth(handler, { verifyPassword: verifyToken })));
1import { fetch } from "https://esm.town/v/std/fetch";
2
3export let duckdbExample = (async () => {
4 async function createWorker(url: string) {
5 const workerScript = await fetch(url);
6 const workerURL = URL.createObjectURL(await workerScript.blob());
7 return new Worker(workerURL, { type: "module" });
1# DuckDB
2
3[DuckDB](https://duckdb.org/) works on Val Town, with only one small tweak! We're basically using DuckDB in the same way you'd use it with a browser - using the WASM package with its dependencies fetched from [jsdelivr](https://www.jsdelivr.com/).
4
5The only trick is to create the worker ourselves rather than using `duckdb.createWorker`. DuckDB's built-in createWorker method doesn't specify a worker type, which causes `type` to default to `classic`, and Deno (our runtime) doesn't support classic workers.
13async function measurePerformance(url) {
14 const start = Date.now();
15 const response = await fetch(url);
16 const ttfb = Date.now() - start;
17
1import { normalizeURL } from "https://esm.town/v/stevekrouse/normalizeURL";
2
3export const fetchJSON = async (
4 url: string | URL,
5 options?: RequestInit & {
6 bearer?: string;
7 fetch?: typeof fetch;
8 },
9) => {
17 headers.set("traceparent", traceparent);
18 }
19 let fetch = options?.fetch ?? globalThis.fetch;
20 let resp = await fetch(normalizeURL(url), {
21 redirect: "follow",
22 ...options,
28 }
29 catch (e) {
30 throw new Error(`fetchJSON error: ${e.message} in ${url}\n\n"${text}"`);
31 }
32};
1import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
2
3export const nasaImageDetails = async () => {
4 const nasaAPOD = await fetchJSON("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY");
5 let nasaImageHtml = nasaAPOD.hdurl
6 ? `<img width="100%" src="${nasaAPOD.hdurl}"/>`
1import { blob } from "https://esm.town/v/std/blob";
2import { fetch } from "https://esm.town/v/std/fetch";
3import xml2js from "npm:xml2js";
4
5async function fetchHistoricData() {
6 const currentDate = new Date();
7 const promises = [];
14 `https://www.floatrates.com/historical-exchange-rates.html?operation=rates&pb_id=1462&page=historical¤cy_date=${formattedDate}&base_currency_code=JPY&format_type=xml`;
15
16 const fetchPromise = fetch(url)
17 .then((response) => {
18 if (!response.ok) {
40 .catch((error) => {
41 console.error(
42 "There has been a problem with your fetch operation:",
43 error,
44 );
46 });
47
48 promises.push(fetchPromise);
49 }
50
61
62export default async function(interval: Interval) {
63 const history = await fetchHistoricData();
64
65 await blob.setJSON("daily", history);
30 const qdrantUrl = `https://${cluster_id}.cloud.qdrant.io${path}`;
31
32 const response = await fetch(qdrantUrl, {
33 method: req.method,
34 headers: req.headers,
1import { normalizeURL } from "https://esm.town/v/stevekrouse/normalizeURL";
2
3export const fetchJSON = async (
4 url: string | URL,
5 options?: RequestInit & {
6 bearer?: string;
7 fetch?: typeof fetch;
8 },
9) => {
17 headers.set("traceparent", traceparent);
18 }
19 let fetch = options?.fetch ?? globalThis.fetch;
20 let resp = await fetch(normalizeURL(url), {
21 redirect: "follow",
22 ...options,
28 }
29 catch (e) {
30 throw new Error(`fetchJSON error: ${e.message} in ${url}\n\n"${text}"`);
31 }
32};
11 const start = performance.now();
12 try {
13 const res = await fetch(url);
14 end = performance.now();
15 status = res.status;
22 } catch (e) {
23 end = performance.now();
24 reason = `couldn't fetch: ${e}`;
25 ok = false;
26 console.log(`Website down (${url}): ${reason} (${end - start}ms)`);