From 5cc457d78b40541c1ecfa1d6a5c07274a7d48ab6 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 1 Sep 2023 15:38:24 -0400 Subject: [PATCH] feat: add initial fallback registry support --- src/queries.ts | 30 +++++++++++++++++++++++++----- src/registry.ts | 20 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 src/registry.ts diff --git a/src/queries.ts b/src/queries.ts index 942ab57..cc70edc 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -13,10 +13,12 @@ import { import { deriveChildKey, downloadSmallObject, + hexToBytes, verifyCid, } from "@lumeweb/libweb"; import type { moduleQuery, presentKeyData } from "@lumeweb/libkernel/module"; import { readableStreamToUint8Array } from "binconv"; +import { getSavedRegistryEntry } from "./registry.js"; // WorkerLaunchFn is the type signature of the function that launches the // worker to set up for processing a query. @@ -308,16 +310,22 @@ function handleModuleCall( ); return; } + + let isRegistryEntry = false; + try { + isRegistryEntry = hexToBytes(event.data.data.module)?.length === 32; + } catch {} + if ( typeof event.data.data.module !== "string" || - !verifyCid(event.data.data.module) + (!verifyCid(event.data.data.module) && !isRegistryEntry) ) { logErr("moduleCall", "received moduleCall with malformed module"); respondErr( event, messagePortal, isWorker, - "'module' field in moduleCall is expected to be a base58 encoded blake3 hash + filesize", + "'module' field in moduleCall is expected to be a base58 encoded blake3 hash + filesize or a registry entry pubkey", ); return; } @@ -372,10 +380,22 @@ function handleModuleCall( return; } - // TODO: Load any overrides. - const finalModule = event.data.data.module; // Can change with overrides. - const moduleDomain = event.data.data.module; // Does not change with overrides. + let moduleDomain = event.data.data.module; // Can change with overrides. + let finalModule = moduleDomain; // Can change with overrides. + if (isRegistryEntry) { + finalModule = getSavedRegistryEntry(moduleDomain); + if (!finalModule) { + logErr("moduleCall", "received moduleCall with no known registry entry"); + respondErr( + event, + messagePortal, + isWorker, + "registry entry for module is not found", + ); + return; + } + } // Define a helper function to create a new query to the module. It will // both open a query on the module and also send an update message to the // caller with the kernel nonce for this query so that the caller can diff --git a/src/registry.ts b/src/registry.ts new file mode 100644 index 0000000..979fbb6 --- /dev/null +++ b/src/registry.ts @@ -0,0 +1,20 @@ +const DEFAULT_MODULE_REGISTRY = new Map(Object.entries({})); +const REGISTRY_ITEM_ID = "registry"; + +Object.freeze(DEFAULT_MODULE_REGISTRY); + +export function getSavedRegistryEntry(pubkey: string) { + const savedEntries = new Map( + Object.entries(window.localStorage.getItem(REGISTRY_ITEM_ID) ?? {}), + ); + + if (savedEntries.has(pubkey)) { + return savedEntries.get(pubkey) as string; + } + + if (DEFAULT_MODULE_REGISTRY.has(pubkey)) { + DEFAULT_MODULE_REGISTRY.get(pubkey) as string; + } + + return null; +}