Commit 014d8921fce193c2007afab069201a9633d3d87a
more consistent use of Address class
Reto Gmür committed on 8/15/2021, 7:47:41 PMParent: e3d8dd1eab204f6c1866d7a27a38e632281c2df1
Files changed
Procedures.ts | changed |
feedSubscriptions.ts | changed |
get-followees.ts | changed |
play.ts | changed |
run.ts | changed |
util.ts | changed |
Procedures.ts | ||
---|---|---|
@@ -1,7 +1,7 @@ | ||
1 | 1 … | import { RequestHandler } from "./RPCConnection.ts"; |
2 | 2 … | import * as FSStorage from "./fsStorage.ts"; |
3 | -import { log, path, parseFeedId } from "./util.ts"; | |
3 … | +import { log, parseFeedId, path } from "./util.ts"; | |
4 | 4 … | |
5 | 5 … | type sourceProcedure = ( |
6 | 6 … | args: Record<string, string>[], |
7 | 7 … | ) => AsyncIterator<Record<string, unknown> | string | Uint8Array>; |
feedSubscriptions.ts | ||
---|---|---|
@@ -1,10 +1,10 @@ | ||
1 | 1 … | import * as FSStorage from "./fsStorage.ts"; |
2 | 2 … | import { |
3 | 3 … | computeMsgHash, |
4 | 4 … | FeedId, |
5 … | + log, | |
5 | 6 … | parseFeedId, |
6 | - log, | |
7 | 7 … | path, |
8 | 8 … | toBase64, |
9 | 9 … | verifySignature, |
10 | 10 … | } from "./util.ts"; |
get-followees.ts | ||
---|---|---|
@@ -1,6 +1,6 @@ | ||
1 | 1 … | import * as FSStorage from "./fsStorage.ts"; |
2 | -import { log, path, parseFeedId } from "./util.ts"; | |
2 … | +import { log, parseFeedId, path } from "./util.ts"; | |
3 | 3 … | import config from "./config.ts"; |
4 | 4 … | |
5 | 5 … | if (Deno.args.length < 1) { |
6 | 6 … | throw new Error("expecting at least one argument"); |
play.ts | ||
---|---|---|
@@ -22,9 +22,8 @@ | ||
22 | 22 … | const address = parseAddress( |
23 | 23 … | addressString, |
24 | 24 … | ); |
25 | 25 … | |
26 | - | |
27 | 26 … | const feedKey = Deno.args.length > 1 ? parseFeedId(Deno.args[1]) : address.key; |
28 | 27 … | |
29 | 28 … | const boxConnection: BoxConnection = await host.connect( |
30 | 29 … | address, |
run.ts | ||
---|---|---|
@@ -1,15 +1,15 @@ | ||
1 | 1 … | import ScuttlebuttPeer from "./ScuttlebuttPeer.ts"; |
2 | 2 … | import BoxConnection from "./BoxConnection.ts"; |
3 | 3 … | import Procedures from "./Procedures.ts"; |
4 | 4 … | import { updateFeeds } from "./feedSubscriptions.ts"; |
5 | -import { delay, log, parseAddress, path } from "./util.ts"; | |
5 … | +import { Address, delay, log, parseAddress, path } from "./util.ts"; | |
6 | 6 … | import RPCConnection from "./RPCConnection.ts"; |
7 | 7 … | import config from "./config.ts"; |
8 | 8 … | |
9 | 9 … | const peersFile = path.join(config.baseDir, "peers.json"); |
10 | 10 … | |
11 | -function getPeers() { | |
11 … | +function getPeersFromFile() { | |
12 | 12 … | try { |
13 | 13 … | return JSON.parse(Deno.readTextFileSync(peersFile)); |
14 | 14 … | } catch (error) { |
15 | 15 … | if (error instanceof Deno.errors.NotFound) { |
@@ -17,10 +17,15 @@ | ||
17 | 17 … | } |
18 | 18 … | throw error; |
19 | 19 … | } |
20 | 20 … | } |
21 | -const peers: string[] = getPeers(); | |
22 | 21 … | |
22 … | +function getPeers() { | |
23 … | + return getPeersFromFile().map(parseAddress); | |
24 … | +} | |
25 … | + | |
26 … | +const peers: Address[] = getPeers(); | |
27 … | + | |
23 | 28 … | const host = new ScuttlebuttPeer(); |
24 | 29 … | |
25 | 30 … | host.listen(); |
26 | 31 … | |
@@ -43,9 +48,9 @@ | ||
43 | 48 … | } else { |
44 | 49 … | log.info( |
45 | 50 … | `${host.connections.length} connections open, connecting to ${address}`, |
46 | 51 … | ); |
47 | - await host.connect(parseAddress(address)); | |
52 … | + await host.connect(address); | |
48 | 53 … | } |
49 | 54 … | } catch (error) { |
50 | 55 … | log.error( |
51 | 56 … | `In connection with ${address}: ${error}, now having ${host.connections.length} connections left`, |
util.ts | ||
---|---|---|
@@ -9,9 +9,8 @@ | ||
9 | 9 … | |
10 | 10 … | const textEncoder = new TextEncoder(); |
11 | 11 … | |
12 | 12 … | export class FeedId extends Uint8Array { |
13 | - | |
14 | 13 … | constructor(publicKey: Uint8Array) { |
15 | 14 … | super(publicKey); |
16 | 15 … | } |
17 | 16 … | |
@@ -19,9 +18,9 @@ | ||
19 | 18 … | return toBase64(this); |
20 | 19 … | } |
21 | 20 … | |
22 | 21 … | get base64FilenameSafe() { |
23 | - return filenameSafeAlphabetRFC3548(this.base64Key) | |
22 … | + return filenameSafeAlphabetRFC3548(this.base64Key); | |
24 | 23 … | } |
25 | 24 … | |
26 | 25 … | toString(): string { |
27 | 26 … | return `@${this.base64Key}.ed25519`; |
@@ -32,21 +31,35 @@ | ||
32 | 31 … | protocol: string; |
33 | 32 … | host: string; |
34 | 33 … | port: number; |
35 | 34 … | key: FeedId; |
35 … | + toString: () => string; | |
36 | 36 … | } |
37 | 37 … | |
38 | 38 … | 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 … | + } | |
43 | 55 … | } |
44 | 56 … | |
45 | 57 … | 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; | |
49 | 62 … | return new FeedId(fromBase64(base64Key)); |
50 | 63 … | } |
51 | 64 … | |
52 | 65 … | export function bytes2NumberUnsigned(bytes: Uint8Array): number { |
Built with git-ssb-web