git ssb

0+

farewellutopia-dev / deno-ssb-experiments



Commit 014d8921fce193c2007afab069201a9633d3d87a

more consistent use of Address class

Reto Gmür committed on 8/15/2021, 7:47:41 PM
Parent: e3d8dd1eab204f6c1866d7a27a38e632281c2df1

Files changed

Procedures.tschanged
feedSubscriptions.tschanged
get-followees.tschanged
play.tschanged
run.tschanged
util.tschanged
Procedures.tsView
@@ -1,7 +1,7 @@
11 import { RequestHandler } from "./RPCConnection.ts";
22 import * as FSStorage from "./fsStorage.ts";
3-import { log, path, parseFeedId } from "./util.ts";
3 +import { log, parseFeedId, path } from "./util.ts";
44
55 type sourceProcedure = (
66 args: Record<string, string>[],
77 ) => AsyncIterator<Record<string, unknown> | string | Uint8Array>;
feedSubscriptions.tsView
@@ -1,10 +1,10 @@
11 import * as FSStorage from "./fsStorage.ts";
22 import {
33 computeMsgHash,
44 FeedId,
5 + log,
56 parseFeedId,
6- log,
77 path,
88 toBase64,
99 verifySignature,
1010 } from "./util.ts";
get-followees.tsView
@@ -1,6 +1,6 @@
11 import * as FSStorage from "./fsStorage.ts";
2-import { log, path, parseFeedId } from "./util.ts";
2 +import { log, parseFeedId, path } from "./util.ts";
33 import config from "./config.ts";
44
55 if (Deno.args.length < 1) {
66 throw new Error("expecting at least one argument");
play.tsView
@@ -22,9 +22,8 @@
2222 const address = parseAddress(
2323 addressString,
2424 );
2525
26-
2726 const feedKey = Deno.args.length > 1 ? parseFeedId(Deno.args[1]) : address.key;
2827
2928 const boxConnection: BoxConnection = await host.connect(
3029 address,
run.tsView
@@ -1,15 +1,15 @@
11 import ScuttlebuttPeer from "./ScuttlebuttPeer.ts";
22 import BoxConnection from "./BoxConnection.ts";
33 import Procedures from "./Procedures.ts";
44 import { updateFeeds } from "./feedSubscriptions.ts";
5-import { delay, log, parseAddress, path } from "./util.ts";
5 +import { Address, delay, log, parseAddress, path } from "./util.ts";
66 import RPCConnection from "./RPCConnection.ts";
77 import config from "./config.ts";
88
99 const peersFile = path.join(config.baseDir, "peers.json");
1010
11-function getPeers() {
11 +function getPeersFromFile() {
1212 try {
1313 return JSON.parse(Deno.readTextFileSync(peersFile));
1414 } catch (error) {
1515 if (error instanceof Deno.errors.NotFound) {
@@ -17,10 +17,15 @@
1717 }
1818 throw error;
1919 }
2020 }
21-const peers: string[] = getPeers();
2221
22 +function getPeers() {
23 + return getPeersFromFile().map(parseAddress);
24 +}
25 +
26 +const peers: Address[] = getPeers();
27 +
2328 const host = new ScuttlebuttPeer();
2429
2530 host.listen();
2631
@@ -43,9 +48,9 @@
4348 } else {
4449 log.info(
4550 `${host.connections.length} connections open, connecting to ${address}`,
4651 );
47- await host.connect(parseAddress(address));
52 + await host.connect(address);
4853 }
4954 } catch (error) {
5055 log.error(
5156 `In connection with ${address}: ${error}, now having ${host.connections.length} connections left`,
util.tsView
@@ -9,9 +9,8 @@
99
1010 const textEncoder = new TextEncoder();
1111
1212 export class FeedId extends Uint8Array {
13-
1413 constructor(publicKey: Uint8Array) {
1514 super(publicKey);
1615 }
1716
@@ -19,9 +18,9 @@
1918 return toBase64(this);
2019 }
2120
2221 get base64FilenameSafe() {
23- return filenameSafeAlphabetRFC3548(this.base64Key)
22 + return filenameSafeAlphabetRFC3548(this.base64Key);
2423 }
2524
2625 toString(): string {
2726 return `@${this.base64Key}.ed25519`;
@@ -32,21 +31,35 @@
3231 protocol: string;
3332 host: string;
3433 port: number;
3534 key: FeedId;
35 + toString: () => string;
3636 }
3737
3838 export function parseAddress(addr: string): Address {
39- const sections = addr.split(":");
40- const [protocol, host, portshs, keyString] = sections;
41- const port = parseInt(portshs.split("~")[0]);
42- return { protocol, host, port, key: new FeedId(fromBase64(keyString)) };
39 + try {
40 + const sections = addr.split(":");
41 + const [protocol, host, portshs, keyString] = sections;
42 + const port = parseInt(portshs.split("~")[0]);
43 + return {
44 + protocol,
45 + host,
46 + port,
47 + key: new FeedId(fromBase64(keyString)),
48 + toString: () => {
49 + return addr;
50 + },
51 + };
52 + } catch (error) {
53 + throw new Error(`Error parsing ${addr}: ${error}`);
54 + }
4355 }
4456
4557 export function parseFeedId(feedIdString: string) {
46- const base64Key = feedIdString.startsWith("@") && feedIdString.endsWith(".ed25519")?
47- feedIdString.substring(1, feedIdString.length - 8) :
48- feedIdString;
58 + const base64Key =
59 + feedIdString.startsWith("@") && feedIdString.endsWith(".ed25519")
60 + ? feedIdString.substring(1, feedIdString.length - 8)
61 + : feedIdString;
4962 return new FeedId(fromBase64(base64Key));
5063 }
5164
5265 export function bytes2NumberUnsigned(bytes: Uint8Array): number {

Built with git-ssb-web