mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-06-17 10:19:44 -07:00
bitview: reorg part 9
This commit is contained in:
@@ -0,0 +1 @@
|
||||
generated
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* @param {VoidFunction} callback
|
||||
* @param {number} [timeout = 1]
|
||||
*/
|
||||
export function runWhenIdle(callback, timeout = 1) {
|
||||
if ("requestIdleCallback" in window) {
|
||||
requestIdleCallback(callback);
|
||||
} else {
|
||||
setTimeout(callback, timeout);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* @import { IndexName } from "./generated/metrics"
|
||||
* @import { Metric } from './metrics'
|
||||
*
|
||||
* @typedef {ReturnType<createClient>} BRK
|
||||
*/
|
||||
|
||||
// client.metrics.catalog.a.b.c() -> string (uncompress inside)
|
||||
|
||||
import { runWhenIdle } from "./idle";
|
||||
|
||||
import { POOL_ID_TO_POOL_NAME } from "./generated/pools";
|
||||
import { INDEXES } from "./generated/metrics";
|
||||
import { hasMetric, getIndexesFromMetric } from "./metrics";
|
||||
import { VERSION } from "./generated/version";
|
||||
|
||||
const CACHE_NAME = "__BRK_CLIENT__";
|
||||
|
||||
/**
|
||||
* @param {string} origin
|
||||
*/
|
||||
export function createClient(origin) {
|
||||
/**
|
||||
* @template T
|
||||
* @param {(value: T) => void} callback
|
||||
* @param {string} url
|
||||
*/
|
||||
async function fetchJson(callback, url) {
|
||||
/** @type {T | null} */
|
||||
let cachedJson = null;
|
||||
|
||||
/** @type {Cache | undefined} */
|
||||
let cache;
|
||||
/** @type {Response | undefined} */
|
||||
let cachedResponse;
|
||||
try {
|
||||
cache = await caches.open(CACHE_NAME);
|
||||
cachedResponse = await cache.match(url);
|
||||
if (cachedResponse) {
|
||||
console.debug(`cache: ${url}`);
|
||||
const json = /** @type {T} */ (await cachedResponse.json());
|
||||
cachedJson = json;
|
||||
callback(json);
|
||||
}
|
||||
} catch {}
|
||||
|
||||
try {
|
||||
if (!navigator.onLine) {
|
||||
throw "Offline";
|
||||
}
|
||||
|
||||
console.debug(`fetch: ${url}`);
|
||||
|
||||
const fetchedResponse = await fetch(url, {
|
||||
signal: AbortSignal.timeout(5000),
|
||||
});
|
||||
if (!fetchedResponse.ok) {
|
||||
throw `Bad response: ${fetchedResponse}`;
|
||||
}
|
||||
|
||||
if (
|
||||
cachedResponse?.headers.get("ETag") ===
|
||||
fetchedResponse.headers.get("ETag")
|
||||
) {
|
||||
return cachedJson;
|
||||
}
|
||||
|
||||
const clonedResponse = fetchedResponse.clone();
|
||||
|
||||
const fetchedJson = /** @type {T} */ (await fetchedResponse.json());
|
||||
if (!fetchedJson) throw `JSON is false`;
|
||||
|
||||
callback(fetchedJson);
|
||||
|
||||
runWhenIdle(async function () {
|
||||
try {
|
||||
await cache?.put(url, clonedResponse);
|
||||
} catch (_) {}
|
||||
});
|
||||
|
||||
return fetchedJson;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return cachedJson;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Metric} metric
|
||||
* @param {IndexName} index
|
||||
* @param {number} [from]
|
||||
* @param {number} [to]
|
||||
*/
|
||||
function genMetricURL(metric, index, from, to) {
|
||||
let path = `${origin}api/metrics/${metric.replaceAll("_", "-")}/${index}?`;
|
||||
|
||||
if (from !== undefined) {
|
||||
path += `from=${from}`;
|
||||
}
|
||||
if (to !== undefined) {
|
||||
if (!path.endsWith("?")) {
|
||||
path += `&`;
|
||||
}
|
||||
path += `to=${to}`;
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param {(v: T[]) => void} callback
|
||||
* @param {IndexName} index
|
||||
* @param {Metric} metric
|
||||
* @param {number} [from]
|
||||
* @param {number} [to]
|
||||
*/
|
||||
function fetchMetric(callback, index, metric, from, to) {
|
||||
return fetchJson(callback, genMetricURL(metric, index, from, to));
|
||||
}
|
||||
|
||||
return {
|
||||
VERSION,
|
||||
POOL_ID_TO_POOL_NAME,
|
||||
INDEXES,
|
||||
|
||||
hasMetric,
|
||||
getIndexesFromMetric,
|
||||
|
||||
genMetricURL,
|
||||
fetchMetric,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"checkJs": true,
|
||||
"strict": true,
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"exclude": ["dist"]
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
import {
|
||||
INDEX_TO_WORD,
|
||||
COMPRESSED_METRIC_TO_INDEXES,
|
||||
} from "./generated/metrics";
|
||||
|
||||
/**
|
||||
* @typedef {typeof import("./generated/metrics")["COMPRESSED_METRIC_TO_INDEXES"]} MetricToIndexes
|
||||
* @typedef {string} Metric
|
||||
*/
|
||||
|
||||
/** @type {Record<string, number>} */
|
||||
const WORD_TO_INDEX = {};
|
||||
|
||||
INDEX_TO_WORD.forEach((word, index) => {
|
||||
WORD_TO_INDEX[word] = index;
|
||||
});
|
||||
|
||||
/**
|
||||
* @param {Metric} metric
|
||||
*/
|
||||
export function getIndexesFromMetric(metric) {
|
||||
return COMPRESSED_METRIC_TO_INDEXES[compressMetric(metric)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Metric} metric
|
||||
*/
|
||||
export function hasMetric(metric) {
|
||||
return compressMetric(metric) in COMPRESSED_METRIC_TO_INDEXES;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} metric
|
||||
*/
|
||||
function compressMetric(metric) {
|
||||
return metric
|
||||
.split("_")
|
||||
.map((word) => {
|
||||
const index = WORD_TO_INDEX[word];
|
||||
return index !== undefined ? indexToLetters(index) : word;
|
||||
})
|
||||
.join("_");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} compressedMetric
|
||||
*/
|
||||
function decompressMetric(compressedMetric) {
|
||||
return compressedMetric
|
||||
.split("_")
|
||||
.map((code) => {
|
||||
const index = lettersToIndex(code);
|
||||
return INDEX_TO_WORD[index] || code; // Fallback to original if not found
|
||||
})
|
||||
.join("_");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} letters
|
||||
*/
|
||||
function lettersToIndex(letters) {
|
||||
let result = 0;
|
||||
for (let i = 0; i < letters.length; i++) {
|
||||
const value = charToIndex(letters.charCodeAt(i));
|
||||
result = result * 52 + value + 1;
|
||||
}
|
||||
return result - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} byte
|
||||
*/
|
||||
function charToIndex(byte) {
|
||||
if (byte >= 65 && byte <= 90) {
|
||||
// 'A' to 'Z'
|
||||
return byte - 65;
|
||||
} else if (byte >= 97 && byte <= 122) {
|
||||
// 'a' to 'z'
|
||||
return byte - 97 + 26;
|
||||
} else {
|
||||
return 255; // Invalid
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} index
|
||||
*/
|
||||
function indexToLetters(index) {
|
||||
if (index < 52) {
|
||||
return indexToChar(index);
|
||||
}
|
||||
let result = [];
|
||||
while (true) {
|
||||
result.push(indexToChar(index % 52));
|
||||
index = Math.floor(index / 52);
|
||||
if (index === 0) break;
|
||||
index -= 1;
|
||||
}
|
||||
return result.reverse().join("");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} index
|
||||
*/
|
||||
function indexToChar(index) {
|
||||
if (index <= 25) {
|
||||
return String.fromCharCode(65 + index); // A-Z
|
||||
} else {
|
||||
return String.fromCharCode(97 + index - 26); // a-z
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"allowJs": true,
|
||||
"checkJs": true,
|
||||
"strict": true,
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"outDir": "/tmp/brk",
|
||||
"lib": ["DOM", "DOM.Iterable", "ESNext", "WebWorker"],
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"exclude": ["dist"]
|
||||
}
|
||||
Reference in New Issue
Block a user