Suite of AT Protocol TypeScript libraries built on web standards
21
fork

Configure Feed

Select the types of activity you want to include in your feed.

some more explicit repo typing

+92 -67
+1 -1
repo/cid-set.ts
··· 23 23 return this; 24 24 } 25 25 26 - delete(cid: CID) { 26 + delete(cid: CID): CidSet { 27 27 this.set.delete(cid.toString()); 28 28 return this; 29 29 }
+9 -9
repo/error.ts
··· 1 - import { CID } from 'multiformats/cid' 1 + import type { CID } from "multiformats/cid"; 2 2 3 3 export class MissingBlockError extends Error { 4 4 constructor( 5 5 public cid: CID, 6 6 def?: string, 7 7 ) { 8 - let msg = `block not found: ${cid.toString()}` 8 + let msg = `block not found: ${cid.toString()}`; 9 9 if (def) { 10 - msg += `, expected type: ${def}` 10 + msg += `, expected type: ${def}`; 11 11 } 12 - super(msg) 12 + super(msg); 13 13 } 14 14 } 15 15 ··· 18 18 public context: string, 19 19 public cids: CID[], 20 20 ) { 21 - const cidStr = cids.map((c) => c.toString()) 22 - super(`missing ${context} blocks: ${cidStr}`) 21 + const cidStr = cids.map((c) => c.toString()); 22 + super(`missing ${context} blocks: ${cidStr}`); 23 23 } 24 24 } 25 25 ··· 28 28 public commit: CID, 29 29 public cids: CID[], 30 30 ) { 31 - const cidStr = cids.map((c) => c.toString()) 32 - super(`missing blocks for commit ${commit.toString()}: ${cidStr}`) 31 + const cidStr = cids.map((c) => c.toString()); 32 + super(`missing blocks for commit ${commit.toString()}: ${cidStr}`); 33 33 } 34 34 } 35 35 ··· 38 38 public cid: CID, 39 39 public def: string, 40 40 ) { 41 - super(`unexpected object at ${cid.toString()}, expected: ${def}`) 41 + super(`unexpected object at ${cid.toString()}, expected: ${def}`); 42 42 } 43 43 }
+16 -5
repo/mst/mst.ts
··· 42 42 * Then the first will be described as `prefix: 0, key: 'bsky/posts/abcdefg'`, 43 43 * and the second will be described as `prefix: 16, key: 'hi'.` 44 44 */ 45 - const subTreePointer = z.nullable(common.cid); 46 - const treeEntry = z.object({ 45 + const subTreePointer: SubTreePointerType = z.nullable(common.cid); 46 + type SubTreePointerType = z.ZodNullable<typeof common.cid>; 47 + const treeEntry: TreeEntryType = z.object({ 47 48 p: z.number(), // prefix count of ascii chars that this key shares with the prev key 48 49 k: common.bytes, // the rest of the key outside the shared prefix 49 50 v: common.cid, // value 50 51 t: subTreePointer, // next subtree (to the right of leaf) 51 52 }); 52 - const nodeData = z.object({ 53 + type TreeEntryType = z.ZodObject<{ 54 + p: z.ZodNumber; 55 + k: typeof common.bytes; 56 + v: typeof common.cid; 57 + t: SubTreePointerType; 58 + }, z.core.$strip>; 59 + const nodeData: NodeDataType = z.object({ 53 60 l: subTreePointer, // left-most subtree 54 61 e: z.array(treeEntry), //entries 55 62 }); 56 - export type NodeData = z.infer<typeof nodeData>; 63 + type NodeDataType = z.ZodObject<{ 64 + l: SubTreePointerType; 65 + e: z.ZodArray<TreeEntryType>; 66 + }, z.core.$strip>; 67 + export type NodeData = z.infer<NodeDataType>; 57 68 58 69 export const nodeDataDef = { 59 70 name: "mst node", ··· 675 686 } 676 687 677 688 // Walks tree & returns all leaves 678 - async leaves() { 689 + async leaves(): Promise<Leaf[]> { 679 690 const leaves: Leaf[] = []; 680 691 for await (const entry of this.walk()) { 681 692 if (entry.isLeaf()) leaves.push(entry);
+2 -2
repo/mst/util.ts
··· 11 11 type NodeEntry, 12 12 } from "./mst.ts"; 13 13 14 - export const leadingZerosOnHash = (key: string | Uint8Array) => { 14 + export const leadingZerosOnHash = (key: string | Uint8Array): number => { 15 15 const hash = sha256(key); 16 16 let leadingZeros = 0; 17 17 for (let i = 0; i < hash.length; i++) { ··· 138 138 return str.match(validCharsRegex) !== null; 139 139 }; 140 140 141 - export const ensureValidMstKey = (str: string) => { 141 + export const ensureValidMstKey = (str: string): void => { 142 142 if (!isValidMstKey(str)) { 143 143 throw new InvalidMstKeyError(str); 144 144 }
+4 -4
repo/parse.ts
··· 1 - import { CID } from "multiformats/cid"; 2 - import { cborDecode, check } from "@atp/common"; 3 - import { RepoRecord } from "@atp/lexicon"; 4 - import { BlockMap } from "./block-map.ts"; 1 + import type { CID } from "multiformats/cid"; 2 + import { cborDecode, type check } from "@atp/common"; 3 + import type { RepoRecord } from "@atp/lexicon"; 4 + import type { BlockMap } from "./block-map.ts"; 5 5 import { MissingBlockError, UnexpectedObjectError } from "./error.ts"; 6 6 import { cborToLexRecord } from "./util.ts"; 7 7
+5 -5
repo/readable-repo.ts
··· 1 - import { CID } from "multiformats/cid"; 2 - import { RepoRecord } from "@atp/lexicon"; 1 + import type { CID } from "multiformats/cid"; 2 + import type { RepoRecord } from "@atp/lexicon"; 3 3 import { MissingBlocksError } from "./error.ts"; 4 4 import log from "./logger.ts"; 5 5 import { MST } from "./mst/index.ts"; 6 6 import * as parse from "./parse.ts"; 7 - import { ReadableBlockstore } from "./storage/index.ts"; 8 - import { Commit, def, RepoContents } from "./types.ts"; 7 + import type { ReadableBlockstore } from "./storage/index.ts"; 8 + import { type Commit, def, type RepoContents } from "./types.ts"; 9 9 import * as util from "./util.ts"; 10 10 11 11 type Params = { ··· 28 28 this.cid = params.cid; 29 29 } 30 30 31 - static load(storage: ReadableBlockstore, commitCid: CID) { 31 + static load(storage: ReadableBlockstore, commitCid: CID): ReadableRepo { 32 32 const commit = storage.readObj(commitCid, def.versionedCommit); 33 33 const data = MST.load(storage, (commit as { data: CID }).data); 34 34 log.info("loaded repo for", { did: commit.did });
+27 -18
repo/repo.ts
··· 1 - import { CID } from "multiformats/cid"; 1 + import type { CID } from "multiformats/cid"; 2 2 import { dataToCborBlock, TID } from "@atp/common"; 3 - import * as crypto from "@atp/crypto"; 3 + import type * as crypto from "@atp/crypto"; 4 4 import { lexToIpld } from "@atp/lexicon"; 5 5 import { BlockMap } from "./block-map.ts"; 6 6 import { CidSet } from "./cid-set.ts"; ··· 8 8 import log from "./logger.ts"; 9 9 import { MST } from "./mst/index.ts"; 10 10 import { ReadableRepo } from "./readable-repo.ts"; 11 - import { RepoStorage } from "./storage/index.ts"; 11 + import type { RepoStorage } from "./storage/index.ts"; 12 12 import { 13 - Commit, 14 - CommitData, 13 + type Commit, 14 + type CommitData, 15 15 def, 16 - RecordCreateOp, 17 - RecordWriteOp, 16 + type RecordCreateOp, 17 + type RecordWriteOp, 18 18 WriteOpAction, 19 19 } from "./types.ts"; 20 20 import * as util from "./util.ts"; 21 + import type { Version } from "multiformats/link/interface"; 21 22 22 23 type Params = { 23 24 storage: RepoStorage; ··· 54 55 newBlocks.addMap(diff.newMstBlocks); 55 56 56 57 const rev = revOverride ?? TID.nextStr(); 57 - const commit = await util.signCommit( 58 + const commit = util.signCommit( 58 59 { 59 60 did, 60 61 version: 3, ··· 76 77 }; 77 78 } 78 79 79 - static async createFromCommit( 80 + static createFromCommit( 80 81 storage: RepoStorage, 81 82 commit: CommitData, 82 - ): Promise<Repo> { 83 - await storage.applyCommit(commit); 83 + ): Repo { 84 + storage.applyCommit(commit); 84 85 return Repo.load(storage, commit.cid); 85 86 } 86 87 ··· 99 100 return Repo.createFromCommit(storage, commit); 100 101 } 101 102 102 - static override load(storage: RepoStorage, cid?: CID) { 103 + static override load(storage: RepoStorage, cid?: CID): Repo { 103 104 const commitCid = cid || (storage.getRoot()); 104 105 if (!commitCid) { 105 106 throw new Error("No cid provided and none in storage"); ··· 159 160 relevantBlocks.addMap(addedLeaves.blocks); 160 161 161 162 const rev = TID.nextStr(this.commit.rev); 162 - const commit = await util.signCommit( 163 + const commit = util.signCommit( 163 164 { 164 165 did: this.did, 165 166 version: 3, ··· 187 188 }; 188 189 } 189 190 190 - async applyCommit(commitData: CommitData): Promise<Repo> { 191 - await this.storage.applyCommit(commitData); 191 + applyCommit(commitData: CommitData): Repo { 192 + this.storage.applyCommit(commitData); 192 193 return Repo.load(this.storage, commitData.cid); 193 194 } 194 195 ··· 200 201 return this.applyCommit(commit); 201 202 } 202 203 203 - async formatResignCommit(rev: string, keypair: crypto.Keypair) { 204 - const commit = await util.signCommit( 204 + async formatResignCommit(rev: string, keypair: crypto.Keypair): Promise<{ 205 + cid: CID<unknown, number, number, Version>; 206 + rev: string; 207 + since: null; 208 + prev: null; 209 + newBlocks: BlockMap; 210 + relevantBlocks: BlockMap; 211 + removedCids: CidSet; 212 + }> { 213 + const commit = util.signCommit( 205 214 { 206 215 did: this.did, 207 216 version: 3, ··· 224 233 }; 225 234 } 226 235 227 - async resignCommit(rev: string, keypair: crypto.Keypair) { 236 + async resignCommit(rev: string, keypair: crypto.Keypair): Promise<Repo> { 228 237 const formatted = await this.formatResignCommit(rev, keypair); 229 238 return this.applyCommit(formatted); 230 239 }
+2 -3
repo/tests/_util.ts
··· 207 207 const commitCid = await newBlocks.add(commit); 208 208 209 209 // @ts-expect-error FIXME remove this comment (and fix the TS error) 210 - await repo.storage.applyCommit({ 210 + repo.storage.applyCommit({ 211 211 cid: commitCid, 212 212 rev, 213 - prev: repo.cid, 214 213 newBlocks, 215 214 removedCids: diff.removedCids, 216 215 }); ··· 227 226 const layer = await entry.getLayer(); 228 227 log += `Layer ${layer}: ${entry.pointer}\n`; 229 228 log += "--------------\n"; 230 - const entries = await entry.getEntries(); 229 + const entries = entry.getEntries(); 231 230 for (const e of entries) { 232 231 if (e.isLeaf()) { 233 232 log += `Key: ${e.key} (${e.value})\n`;
+4 -4
repo/types.ts
··· 1 - import { CID } from "multiformats"; 1 + import type { CID } from "multiformats"; 2 2 import { z } from "zod"; 3 3 import { schema as common } from "@atp/common"; 4 4 import { def as commonDef } from "@atp/common"; 5 - import { RepoRecord } from "@atp/lexicon"; 6 - import { BlockMap } from "./block-map.ts"; 7 - import { CidSet } from "./cid-set.ts"; 5 + import type { RepoRecord } from "@atp/lexicon"; 6 + import type { BlockMap } from "./block-map.ts"; 7 + import type { CidSet } from "./cid-set.ts"; 8 8 9 9 // Repo nodes 10 10 // ---------------
+22 -16
repo/util.ts
··· 1 1 import * as cbor from "@ipld/dag-cbor"; 2 2 import { cborDecode, check, cidForCbor, schema, TID } from "@atp/common"; 3 3 import * as crypto from "@atp/crypto"; 4 - import { Keypair } from "@atp/crypto"; 5 - import { ipldToLex, lexToIpld, LexValue, RepoRecord } from "@atp/lexicon"; 6 - import { DataDiff } from "./data-diff.ts"; 4 + import type { Keypair } from "@atp/crypto"; 7 5 import { 8 - Commit, 9 - LegacyV2Commit, 10 - RecordCreateDescript, 11 - RecordDeleteDescript, 12 - RecordPath, 13 - RecordUpdateDescript, 14 - RecordWriteDescript, 15 - UnsignedCommit, 6 + ipldToLex, 7 + lexToIpld, 8 + type LexValue, 9 + type RepoRecord, 10 + } from "@atp/lexicon"; 11 + import type { DataDiff } from "./data-diff.ts"; 12 + import { 13 + type Commit, 14 + type LegacyV2Commit, 15 + type RecordCreateDescript, 16 + type RecordDeleteDescript, 17 + type RecordPath, 18 + type RecordUpdateDescript, 19 + type RecordWriteDescript, 20 + type UnsignedCommit, 16 21 WriteOpAction, 17 22 } from "./types.ts"; 23 + import type { CID } from "multiformats/basics"; 18 24 19 25 export const diffToWriteDescripts = ( 20 26 diff: DataDiff, ··· 79 85 return a.did === b.did && a.version === b.version; 80 86 }; 81 87 82 - export const signCommit = async ( 88 + export const signCommit = ( 83 89 unsigned: UnsignedCommit, 84 90 keypair: Keypair, 85 - ): Promise<Commit> => { 91 + ): Commit => { 86 92 const encoded = cbor.encode(unsigned); 87 - const sig = await keypair.sign(encoded); 93 + const sig = keypair.sign(encoded); 88 94 return { 89 95 ...unsigned, 90 96 sig, ··· 112 118 return parsed as RepoRecord; 113 119 }; 114 120 115 - export const cidForRecord = (val: LexValue) => { 116 - return cidForCbor(lexToIpld(val)); 121 + export const cidForRecord = async (val: LexValue): Promise<CID> => { 122 + return await cidForCbor(lexToIpld(val)); 117 123 }; 118 124 119 125 export const ensureV3Commit = (commit: LegacyV2Commit | Commit): Commit => {