Skip to content

Commit

Permalink
intial cleanups, lots to do
Browse files Browse the repository at this point in the history
  • Loading branch information
jchris committed Jan 7, 2025
1 parent 09f20c5 commit 6c99ace
Show file tree
Hide file tree
Showing 28 changed files with 113 additions and 110 deletions.
4 changes: 2 additions & 2 deletions src/blockstore/commit-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Future } from "@adviser/cement";
type QueueFunction<T = void> = () => Promise<T>;

export class CommitQueue<T = void> {
readonly queue: QueueFunction<void>[] = [];
readonly queue: QueueFunction[] = [];
processing = false;

readonly _waitIdleItems: Set<Future<void>> = new Set<Future<void>>();
Expand Down Expand Up @@ -48,7 +48,7 @@ export class CommitQueue<T = void> {
if (this.queue.length === 0 && !this.processing) {
const toResolve = Array.from(this._waitIdleItems);
this._waitIdleItems.clear();
toResolve.map((fn) => fn.resolve());
toResolve.map((fn) => { fn.resolve(); });
}
}
}
8 changes: 4 additions & 4 deletions src/blockstore/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ export class Loader implements Loadable {
remoteCarStore?: DataStore;
remoteFileStore?: DataStore;

private getBlockCache = new Map<string, AnyBlock>();
private seenMeta = new Set<string>();
private writeLimit = pLimit(1);
private readonly getBlockCache = new Map<string, AnyBlock>();
private readonly seenMeta = new Set<string>();
private readonly writeLimit = pLimit(1);

private readonly _carStore = new ResolveOnce<DataStore>();
async carStore(): Promise<DataStore> {
Expand Down Expand Up @@ -214,7 +214,7 @@ export class Loader implements Loadable {
carHeader.compact.map((c) => c.toString()).forEach(this.seenCompacted.add, this.seenCompacted);
await this.getMoreReaders(carHeader.cars.flat());
this.carLog = [...uniqueCids([meta.cars, ...this.carLog, ...carHeader.cars], this.seenCompacted)];
await this.ebOpts.applyMeta?.(carHeader.meta);
await this.ebOpts.applyMeta(carHeader.meta);
}

// protected async ingestKeyFromMeta(meta: DbMeta): Promise<void> {
Expand Down
20 changes: 10 additions & 10 deletions src/blockstore/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export abstract class BaseStoreImpl {
const res = await this.gateway.start(this._url, this.loader);
if (res.isErr()) {
this.logger.Error().Result("gw-start", res).Msg("started-gateway");
return res as Result<URI>;
return res;
}
this._url = res.Ok();
// add storekey to url
Expand All @@ -126,7 +126,7 @@ export abstract class BaseStoreImpl {
return storeKeyName.join(":");
});
if (skRes.isErr()) {
return skRes as Result<URI>;
return skRes;
}
this._url = skRes.Ok();
const version = guardVersion(this._url);
Expand All @@ -143,7 +143,7 @@ export abstract class BaseStoreImpl {
return ready as Result<URI>;
}
}
this._onStarted.forEach((fn) => fn());
this._onStarted.forEach((fn) => { fn(); });
this.logger.Debug().Msg("started");
return version;
}
Expand All @@ -154,7 +154,7 @@ export async function createDbMetaEvent(sthis: SuperThis, dbMeta: DbMeta, parent
{
dbMeta: sthis.txt.encode(format(dbMeta)),
},
parents as unknown as Link<EventView<DbMetaBinary>, number, number, 1>[],
parents as unknown as Link<EventView<DbMetaBinary>>[],
);
return {
eventCid: event.cid as CarClockLink,
Expand Down Expand Up @@ -182,7 +182,7 @@ export class MetaStoreImpl extends BaseStoreImpl implements MetaStore {
async ({ payload: dbMetas }: FPEnvelopeMeta) => {
this.logger.Debug().Msg("Received message from gateway");
await Promise.all(
dbMetas.map((dbMeta) => this.loader.taskManager?.handleEvent(dbMeta.eventCid, dbMeta.parents, dbMeta.dbMeta)),
dbMetas.map((dbMeta) => this.loader.taskManager.handleEvent(dbMeta.eventCid, dbMeta.parents, dbMeta.dbMeta)),
);
this.updateParentsFromDbMetas(dbMetas);
},
Expand Down Expand Up @@ -257,7 +257,7 @@ export class MetaStoreImpl extends BaseStoreImpl implements MetaStore {

async close(): Promise<Result<void>> {
await this.gateway.close(this.url(), this.loader);
this._onClosed.forEach((fn) => fn());
this._onClosed.forEach((fn) => { fn(); });
return Result.Ok(undefined);
}
async destroy(): Promise<Result<void>> {
Expand Down Expand Up @@ -322,7 +322,7 @@ export class DataStoreImpl extends BaseStoreImpl implements DataStore {
if (res.isErr()) {
throw this.logger.Error().Err(res.Err()).Msg("got error from gateway.put").AsError();
}
return res.Ok();
res.Ok();
}
async remove(cid: AnyLink): Promise<Result<void>> {
const url = await this.gateway.buildUrl(this.url(), cid.toString(), this.loader);
Expand All @@ -333,7 +333,7 @@ export class DataStoreImpl extends BaseStoreImpl implements DataStore {
}
async close(): Promise<Result<void>> {
await this.gateway.close(this.url(), this.loader);
this._onClosed.forEach((fn) => fn());
this._onClosed.forEach((fn) => { fn(); });
return Result.Ok(undefined);
}
destroy(): Promise<Result<void>> {
Expand All @@ -352,7 +352,7 @@ export class WALStoreImpl extends BaseStoreImpl implements WALStore {

readonly walState: WALState = { operations: [], noLoaderOps: [], fileOperations: [] };
readonly processing: Promise<void> | undefined = undefined;
readonly processQueue: CommitQueue<void> = new CommitQueue<void>();
readonly processQueue: CommitQueue = new CommitQueue();

constructor(sthis: SuperThis, url: URI, opts: StoreOpts) {
// const my = new URL(url.toString());
Expand Down Expand Up @@ -574,7 +574,7 @@ export class WALStoreImpl extends BaseStoreImpl implements WALStore {

async close() {
await this.gateway.close(this.url(), this.loader);
this._onClosed.forEach((fn) => fn());
this._onClosed.forEach((fn) => { fn(); });
return Result.Ok(undefined);
}

Expand Down
2 changes: 1 addition & 1 deletion src/blockstore/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function defaultedBlockstoreRuntime(
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
compact: async (blocks: BlockFetcher) => {
return {} as unknown as TransactionMeta;
return {} as unknown;
},
autoCompact: 100,
public: false,
Expand Down
4 changes: 2 additions & 2 deletions src/blockstore/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface AnyBlock {
}

export interface CIDBlock {
readonly cid: CID<unknown, number, number, Version>;
readonly cid: CID;
readonly bytes: Uint8Array;
}

Expand Down Expand Up @@ -357,7 +357,7 @@ export interface WALStore extends BaseStore {
readonly storeType: "wal";
ready(): Promise<void>;
readonly processing?: Promise<void> | undefined;
readonly processQueue: CommitQueue<void>;
readonly processQueue: CommitQueue;

process(): Promise<void>;
enqueue(dbMeta: DbMeta, opts: CommitOpts): Promise<void>;
Expand Down
6 changes: 3 additions & 3 deletions src/crdt-clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ export class CRDTClock<T extends DocTypes> {
const changes = await clockChangesSince<T>(throwFalsy(this.blockstore), this.head, prevHead, {}, this.logger);
internalUpdates = changes.result;
}
this.zoomers.forEach((fn) => fn());
this.zoomers.forEach((fn) => { fn(); });
this.notifyWatchers(internalUpdates || []);
}

notifyWatchers(updates: DocUpdate<T>[]) {
this.emptyWatchers.forEach((fn) => fn());
this.watchers.forEach((fn) => fn(updates || []));
this.emptyWatchers.forEach((fn) => { fn(); });
this.watchers.forEach((fn) => { fn(updates || []); });
}

onTick(fn: (updates: DocUpdate<T>[]) => void) {
Expand Down
6 changes: 3 additions & 3 deletions src/crdt-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async function processFileset(
// let totalSize = 0
for (const filename in files) {
if (File === files[filename].constructor) {
const file = files[filename] as File;
const file = files[filename];

// totalSize += file.size
const { cid, blocks: fileBlocks } = await store.encodeFile(file);
Expand All @@ -148,7 +148,7 @@ async function processFileset(
if (didPut.length) {
const car = await dbBlockstore.loader.commitFiles(
t,
{ files } as unknown as TransactionMeta /* {
{ files } as unknown /* {
public: publicFiles,
} */,
);
Expand Down Expand Up @@ -243,7 +243,7 @@ export async function clockChangesSince<T extends DocTypes>(
): Promise<{ result: DocUpdate<T>[]; head: ClockHead }> {
const eventsFetcher = (
opts.dirty ? new DirtyEventFetcher<Operation>(logger, blocks) : new EventFetcher<Operation>(blocks)
) as EventFetcher<Operation>;
);
const keys = new Set<string>();
const updates = await gatherUpdates<T>(
blocks,
Expand Down
2 changes: 1 addition & 1 deletion src/crdt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,6 @@ export class CRDT<T extends DocTypes> {

async compact(): Promise<void> {
const blocks = this.blockstore as EncryptedBlockstore;
return await blocks.compact();
await blocks.compact();
}
}
12 changes: 6 additions & 6 deletions src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class LedgerShell<DT extends DocTypes = NonNullable<unknown>> implements
return this.ref.name;
}
onClosed(fn: () => void): void {
return this.ref.onClosed(fn);
this.ref.onClosed(fn);
}
close(): Promise<void> {
return this.ref.shellClose(this);
Expand Down Expand Up @@ -225,7 +225,7 @@ class LedgerImpl<DT extends DocTypes = NonNullable<unknown>> implements Ledger<D
if (this.shells.size === 0) {
await this.ready();
await this.crdt.close();
this._onClosedFns.forEach((fn) => fn());
this._onClosedFns.forEach((fn) => { fn(); });
}
// await this.blockstore.close();
}
Expand Down Expand Up @@ -296,14 +296,14 @@ class LedgerImpl<DT extends DocTypes = NonNullable<unknown>> implements Ledger<D
_id: docId,
},
})) as CRDTMeta;
return { id: docId, clock: result?.head, name: this.name } as DocResponse;
return { id: docId, clock: result.head, name: this.name } as DocResponse;
}

async del(id: string): Promise<DocResponse> {
await this.ready();
this.logger.Debug().Str("id", id).Msg("del");
const result = (await this._writeQueue.push({ id: id, del: true })) as CRDTMeta;
return { id, clock: result?.head, name: this.name } as DocResponse;
return { id, clock: result.head, name: this.name } as DocResponse;
}

async changes<T extends DocTypes>(since: ClockHead = [], opts: ChangesOptions = {}): Promise<ChangesResponse<T>> {
Expand Down Expand Up @@ -386,7 +386,7 @@ class LedgerImpl<DT extends DocTypes = NonNullable<unknown>> implements Ledger<D
if (this._listeners.size) {
const docs: DocWithId<NonNullable<unknown>>[] = updates.map(({ id, value }) => ({ ...value, _id: id }));
for (const listener of this._listeners) {
await (async () => await listener(docs as DocWithId<DT>[]))().catch((e: Error) => {
await (async () => { await listener(docs as DocWithId<DT>[]); })().catch((e: Error) => {
this.logger.Error().Err(e).Msg("subscriber error");
});
}
Expand All @@ -397,7 +397,7 @@ class LedgerImpl<DT extends DocTypes = NonNullable<unknown>> implements Ledger<D
await this.ready();
if (this._noupdate_listeners.size) {
for (const listener of this._noupdate_listeners) {
await (async () => await listener([]))().catch((e: Error) => {
await (async () => { await listener([]); })().catch((e: Error) => {
this.logger.Error().Err(e).Msg("subscriber error");
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/react/useFireproof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export function useFireproof(name: string | Ledger = "useFireproof", config: Con

const refreshRows = useCallback(async () => {
const res = await ledger.query<K, T, R>(mapFn, query);
setResult({ ...res, docs: res.rows.map((r) => r.doc as DocWithId<T>) });
setResult({ ...res, docs: res.rows.map((r) => r.doc!) });
}, [mapFnString, queryString]);

useEffect(() => {
Expand All @@ -276,7 +276,7 @@ export function useFireproof(name: string | Ledger = "useFireproof", config: Con

const refreshRows = useCallback(async () => {
const res = await ledger.allDocs<T>(query);
setResult({ ...res, docs: res.rows.map((r) => r.value as DocWithId<T>) });
setResult({ ...res, docs: res.rows.map((r) => r.value) });
}, [queryString]);

useEffect(() => {
Expand All @@ -296,7 +296,7 @@ export function useFireproof(name: string | Ledger = "useFireproof", config: Con

const refreshRows = useCallback(async () => {
const res = await ledger.changes<T>(since, opts);
setResult({ ...res, docs: res.rows.map((r) => r.value as DocWithId<T>) });
setResult({ ...res, docs: res.rows.map((r) => r.value) });
}, [since, queryString]);

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function createFileEncoderStream(blob: BlobLike) {
}

class UnixFSFileBuilder {
#file;
readonly #file;
readonly name: string;
constructor(name: string, file: BlobLike) {
this.name = name;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/gateways/file/node-filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class NodeFileSystem implements SysFileSystem {
return this.fs?.mkdir(path, options);
}
async readdir(path: PathLike, options?: ObjectEncodingOptions): Promise<string[]> {
return this.fs?.readdir(path, options) as Promise<string[]>;
return this.fs?.readdir(path, options)!;
}
async rm(path: PathLike, options?: MakeDirectoryOptions & { recursive: boolean }): Promise<void> {
return this.fs?.rm(path, options);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/gateways/fp-envelope-serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async function dbMetaEvent2Serialized(sthis: SuperThis, dbEvents: Omit<DbMetaEve
{
dbMeta: sthis.txt.encode(format(dbEvent.dbMeta)),
},
dbEvent.parents as unknown as Link<EventView<DbMetaBinary>, number, number, 1>[],
dbEvent.parents as unknown as Link<EventView<DbMetaBinary>>[],
);
return {
cid: event.cid.toString(),
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/gateways/indexdb/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function ensureVersion(url: URI): URI {
}

interface IDBConn {
readonly db: IDBPDatabase<unknown>;
readonly db: IDBPDatabase;
readonly dbName: DbName;
readonly version: string;
readonly url: URI;
Expand All @@ -40,7 +40,7 @@ async function connectIdb(url: URI, sthis: SuperThis): Promise<IDBConn> {
},
});
const found = await db.get("version", "version");
const version = ensureVersion(url).getParam(PARAM.VERSION) as string;
const version = ensureVersion(url).getParam(PARAM.VERSION)!;
if (!found) {
await db.put("version", { version }, "version");
} else if (found.version !== version) {
Expand Down Expand Up @@ -91,7 +91,7 @@ export class IndexDBGateway implements Gateway {
this.logger = ensureLogger(sthis, "IndexDBGateway");
this.sthis = sthis;
}
_db: IDBPDatabase<unknown> = {} as IDBPDatabase<unknown>;
_db: IDBPDatabase = {} as IDBPDatabase;

async start(baseURL: URI): Promise<Result<URI>> {
return exception2Result(async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/key-bag-indexdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Logger, ResolveOnce, URI } from "@adviser/cement";
import { SuperThis } from "use-fireproof";

export class KeyBagProviderIndexDB implements KeyBagProvider {
readonly _db: ResolveOnce<IDBPDatabase<unknown>> = new ResolveOnce<IDBPDatabase<unknown>>();
readonly _db: ResolveOnce<IDBPDatabase> = new ResolveOnce<IDBPDatabase>();

readonly dbName: string;
readonly url: URI;
Expand All @@ -18,7 +18,7 @@ export class KeyBagProviderIndexDB implements KeyBagProvider {
this.dbName = getPath(this.url, this.sthis);
}

async _prepare(): Promise<IDBPDatabase<unknown>> {
async _prepare(): Promise<IDBPDatabase> {
return this._db.once(async () => {
return await openDB(this.dbName, 1, {
upgrade(db) {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/keyed-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const generateIV: Record<string, GenerateIVFn> = {

function getGenerateIVFn(url: URI, opts: Partial<CodecOpts>): GenerateIVFn {
const ivhash = opts.ivCalc || url.getParam(PARAM.IV_HASH) || "hash";
return generateIV[ivhash] || generateIV["hash"];
return generateIV[ivhash] || generateIV.hash;
}

export class BlockIvKeyIdCodec implements BlockCodec<0x300539, Uint8Array> {
Expand Down Expand Up @@ -85,7 +85,7 @@ export class BlockIvKeyIdCodec implements BlockCodec<0x300539, Uint8Array> {
throw this.ko.logger.Error().Str("fp", fprt).Str("keyId", base58btc.encode(keyId)).Msg("keyId mismatch").AsError();
}
const result = await this.ko._decrypt({ iv: iv, bytes: data });
if (!this.opts?.noIVVerify && !(await getGenerateIVFn(this.ko.url, this.opts).verify(this.ko, this.ko.crypto, iv, result))) {
if (!this.opts.noIVVerify && !(await getGenerateIVFn(this.ko.url, this.opts).verify(this.ko, this.ko.crypto, iv, result))) {
throw this.ko.logger.Error().Msg("iv missmatch").AsError();
}
return result;
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/wait-pr-multiformats/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function decode<T, Code extends number, Alg extends number>({

const value = await Promise.resolve(codec.decode(bytes));
const hash = await hasher.digest(bytes);
const cid = CID.create(1, codec.code, hash) as CID<T, Code, Alg, 1>;
const cid = CID.create(1, codec.code, hash);

return new mfBlock({ value, bytes, cid });
}
Expand All @@ -46,7 +46,7 @@ export async function encode<T, Code extends number, Alg extends number>({

const bytes = await Promise.resolve(codec.encode(value));
const hash = await hasher.digest(bytes);
const cid = CID.create(1, codec.code, hash) as CID<T, Code, Alg, 1>;
const cid = CID.create(1, codec.code, hash);

return new mfBlock({ value, bytes, cid });
}
Expand Down
Loading

0 comments on commit 6c99ace

Please sign in to comment.