Skip to content

Commit

Permalink
Merge pull request #236 from fireproof-storage/update-connectable-and…
Browse files Browse the repository at this point in the history
…-export

changes for making the sync possible
  • Loading branch information
jchris committed Sep 22, 2024
2 parents 5ef2f4b + 9621f50 commit 0189433
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
11 changes: 7 additions & 4 deletions src/blockstore/connection-base.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -14,6 +14,7 @@ export interface Connectable {
readonly ebOpts: BlockstoreRuntime;
};
readonly name?: string;
readonly sthis: SuperThis;
}

export abstract class ConnectionBase implements Connection {
Expand Down Expand Up @@ -55,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,
Expand All @@ -77,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),
Expand Down
2 changes: 1 addition & 1 deletion src/blockstore/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class Loader implements Loadable {
carLog: CarLog = [];
// key?: string;
// keyId?: string;
// remoteMetaStore?: RemoteMetaStore;
remoteMetaStore?: MetaStore;
remoteCarStore?: DataStore;
remoteFileStore?: DataStore;

Expand Down
13 changes: 9 additions & 4 deletions src/blockstore/store-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ async function decodeGatewayMetaBytesToDbMeta(sthis: SuperThis, byteHeads: Uint8
);
}

export async function setCryptoKeyFromGatewayMetaPayload(uri: URI, sthis: SuperThis, data: Uint8Array): Promise<void> {
export async function setCryptoKeyFromGatewayMetaPayload(
uri: URI,
sthis: SuperThis,
data: Uint8Array,
): Promise<DbMeta | undefined> {
const keyInfo = await decodeGatewayMetaBytesToDbMeta(sthis, data);
if (keyInfo.length) {
const dbMeta = keyInfo[0].dbMeta;
Expand All @@ -36,6 +40,7 @@ export async function setCryptoKeyFromGatewayMetaPayload(uri: URI, sthis: SuperT
throw res.Err();
}
}
return dbMeta;
}
}

Expand All @@ -60,7 +65,7 @@ function getStoreKeyName(url: URI): string {
if (idx) {
storeKeyName.push(idx);
}
storeKeyName.push("meta");
storeKeyName.push("data");
return `@${storeKeyName.join(":")}@`;
}

Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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()) {
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down

0 comments on commit 0189433

Please sign in to comment.