From 2fdf491aee7a944dbf5c2f56d7fb25fb495cdb53 Mon Sep 17 00:00:00 2001 From: Marty Schoch Date: Thu, 19 Sep 2024 17:23:16 -0400 Subject: [PATCH 1/4] update Connectable to include sthis and export it from fireproof --- src/blockstore/connection-base.ts | 3 ++- src/database.ts | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/blockstore/connection-base.ts b/src/blockstore/connection-base.ts index c34c1aca..6c1fb1f2 100644 --- a/src/blockstore/connection-base.ts +++ b/src/blockstore/connection-base.ts @@ -1,6 +1,6 @@ import { Logger, URI } from "@adviser/cement"; -import { throwFalsy } from "../types.js"; +import {type SuperThis, throwFalsy} from "../types.js"; import { TaskManager } from "./task-manager.js"; import type { BlockstoreRuntime, Connection, Loadable } from "./types.js"; import { type Loader } from "./loader.js"; @@ -14,6 +14,7 @@ export interface Connectable { readonly ebOpts: BlockstoreRuntime; }; readonly name?: string; + readonly sthis: SuperThis; } export abstract class ConnectionBase implements Connection { diff --git a/src/database.ts b/src/database.ts index 5a9ab93e..f4a776bd 100644 --- a/src/database.ts +++ b/src/database.ts @@ -28,6 +28,8 @@ import type { import { BaseBlockstore, Connectable } from "./blockstore/index.js"; import { ensureLogger, ensureSuperThis, NotFoundError } from "./utils.js"; +export { Connectable }; + export class Database
> implements Connectable { static databases = new Map(); From b661e03750e49527b7a6b0992681a3675a769394 Mon Sep 17 00:00:00 2001 From: Marty Schoch Date: Thu, 19 Sep 2024 17:24:40 -0400 Subject: [PATCH 2/4] format --- src/blockstore/connection-base.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/blockstore/connection-base.ts b/src/blockstore/connection-base.ts index 6c1fb1f2..b410bd92 100644 --- a/src/blockstore/connection-base.ts +++ b/src/blockstore/connection-base.ts @@ -1,6 +1,6 @@ import { Logger, URI } from "@adviser/cement"; -import {type SuperThis, throwFalsy} from "../types.js"; +import { type SuperThis, throwFalsy } from "../types.js"; import { TaskManager } from "./task-manager.js"; import type { BlockstoreRuntime, Connection, Loadable } from "./types.js"; import { type Loader } from "./loader.js"; From 8e1517456a181acbb3c5074fddad52e3afc72a83 Mon Sep 17 00:00:00 2001 From: Marty Schoch Date: Fri, 20 Sep 2024 09:31:49 -0400 Subject: [PATCH 3/4] moving it here fixes a test failure --- src/database.ts | 2 -- src/index.ts | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/database.ts b/src/database.ts index f4a776bd..5a9ab93e 100644 --- a/src/database.ts +++ b/src/database.ts @@ -28,8 +28,6 @@ import type { import { BaseBlockstore, Connectable } from "./blockstore/index.js"; import { ensureLogger, ensureSuperThis, NotFoundError } from "./utils.js"; -export { Connectable }; - export class Database
> implements Connectable { static databases = new Map(); diff --git a/src/index.ts b/src/index.ts index 58095209..5d8d4bb9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,9 @@ export * from "./indexer.js"; export * as bs from "./blockstore/index.js"; export * as blockstore from "./blockstore/index.js"; +import { type Connectable } from "./blockstore"; +export { Connectable }; + export * as rt from "./runtime/index.js"; export * as runtime from "./runtime/index.js"; From 9621f5076c8a6d18c5b8ae21ab81c6e5e651523d Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Sun, 22 Sep 2024 05:33:07 -0700 Subject: [PATCH 4/4] fixes for connect --- src/blockstore/connection-base.ts | 8 +++++--- src/blockstore/loader.ts | 2 +- src/blockstore/store-meta.ts | 13 +++++++++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/blockstore/connection-base.ts b/src/blockstore/connection-base.ts index b410bd92..df7f80e8 100644 --- a/src/blockstore/connection-base.ts +++ b/src/blockstore/connection-base.ts @@ -56,8 +56,9 @@ export abstract class ConnectionBase implements Connection { const metaUrl = this.url.build().defParam("store", "meta").URI(); const gateway = await getGatewayFromURL(metaUrl, this.loader.sthis); if (!gateway) throw this.logger.Error().Url(metaUrl).Msg("connectMeta_X: gateway is required").AsError(); - const name = metaUrl.toString(); - const remote = await RemoteMetaStore(loader.sthis, name, metaUrl, { + const dbName = metaUrl.getParam("name"); + if (!dbName) throw this.logger.Error().Url(metaUrl).Msg("connectMeta_X: name is required").AsError(); + const remote = await RemoteMetaStore(loader.sthis, dbName, metaUrl, { gateway: gateway.gateway, keybag: () => getKeyBag(loader.sthis, loader.ebOpts.keyBag), loader, @@ -78,7 +79,8 @@ export abstract class ConnectionBase implements Connection { const dataUrl = this.url.build().defParam("store", "data").URI(); const gateway = await getGatewayFromURL(dataUrl, this.loader.sthis); if (!gateway) throw this.logger.Error().Url(dataUrl).Msg("connectStorage_X: gateway is required").AsError(); - const name = dataUrl.toString(); + const name = dataUrl.getParam("name"); + if (!name) throw this.logger.Error().Url(dataUrl).Msg("connectStorage_X: name is required").AsError; loader.remoteCarStore = await RemoteDataStore(loader.sthis, name, this.url, { gateway: gateway.gateway, keybag: () => getKeyBag(loader.sthis, this.loader?.ebOpts.keyBag), diff --git a/src/blockstore/loader.ts b/src/blockstore/loader.ts index f6d205a8..0ba955b3 100644 --- a/src/blockstore/loader.ts +++ b/src/blockstore/loader.ts @@ -67,7 +67,7 @@ export class Loader implements Loadable { carLog: CarLog = []; // key?: string; // keyId?: string; - // remoteMetaStore?: RemoteMetaStore; + remoteMetaStore?: MetaStore; remoteCarStore?: DataStore; remoteFileStore?: DataStore; diff --git a/src/blockstore/store-meta.ts b/src/blockstore/store-meta.ts index 448f5a54..2da6638b 100644 --- a/src/blockstore/store-meta.ts +++ b/src/blockstore/store-meta.ts @@ -24,7 +24,11 @@ async function decodeGatewayMetaBytesToDbMeta(sthis: SuperThis, byteHeads: Uint8 ); } -export async function setCryptoKeyFromGatewayMetaPayload(uri: URI, sthis: SuperThis, data: Uint8Array): Promise { +export async function setCryptoKeyFromGatewayMetaPayload( + uri: URI, + sthis: SuperThis, + data: Uint8Array, +): Promise { const keyInfo = await decodeGatewayMetaBytesToDbMeta(sthis, data); if (keyInfo.length) { const dbMeta = keyInfo[0].dbMeta; @@ -36,6 +40,7 @@ export async function setCryptoKeyFromGatewayMetaPayload(uri: URI, sthis: SuperT throw res.Err(); } } + return dbMeta; } } @@ -60,7 +65,7 @@ function getStoreKeyName(url: URI): string { if (idx) { storeKeyName.push(idx); } - storeKeyName.push("meta"); + storeKeyName.push("data"); return `@${storeKeyName.join(":")}@`; } @@ -109,7 +114,7 @@ export class MetaStoreImpl extends BaseStoreImpl implements MetaStore { ); if (remote && opts.gateway.subscribe) { this.logger.Debug().Str("url", url.toString()).Msg("Subscribing to the gateway"); - opts.gateway.subscribe(url, (byteHead: Uint8Array) => this.handleByteHeads(byteHead)); + opts.gateway.subscribe(url, (message: Uint8Array) => decodeGatewayMetaBytesToDbMeta(this.sthis, message)); } } @@ -121,7 +126,7 @@ export class MetaStoreImpl extends BaseStoreImpl implements MetaStore { const branch = "main"; const url = await this.gateway.buildUrl(this.url(), branch); if (url.isErr()) { - throw this.logger.Error().Result("buidUrl", url).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError(); + throw this.logger.Error().Result("buildUrl", url).Str("branch", branch).Msg("got error from gateway.buildUrl").AsError(); } const bytes = await this.gateway.get(url.Ok()); if (bytes.isErr()) {