Merge pull request #1229 from waku-org/chore/remove-code-dupe-enr

This commit is contained in:
fryorcraken.eth 2023-03-14 11:14:27 +11:00 committed by GitHub
commit e359ab8616
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 70 additions and 78 deletions

View File

@ -46,105 +46,51 @@ export class RawEnr extends Map<ENRKey, ENRValue> {
} }
get ip(): string | undefined { get ip(): string | undefined {
const raw = this.get("ip"); return getStringValue(this, "ip", "ip4");
if (raw) {
return convertToString("ip4", raw) as string;
} else {
return undefined;
}
} }
set ip(ip: string | undefined) { set ip(ip: string | undefined) {
if (ip) { setStringValue(this, "ip", "ip4", ip);
this.set("ip", convertToBytes("ip4", ip));
} else {
this.delete("ip");
}
} }
get tcp(): number | undefined { get tcp(): number | undefined {
const raw = this.get("tcp"); return getNumberAsStringValue(this, "tcp", "tcp");
if (raw) {
return Number(convertToString("tcp", raw));
} else {
return undefined;
}
} }
set tcp(port: number | undefined) { set tcp(port: number | undefined) {
if (port === undefined) { setNumberAsStringValue(this, "tcp", "tcp", port);
this.delete("tcp");
} else {
this.set("tcp", convertToBytes("tcp", port.toString(10)));
}
} }
get udp(): number | undefined { get udp(): number | undefined {
const raw = this.get("udp"); return getNumberAsStringValue(this, "udp", "udp");
if (raw) {
return Number(convertToString("udp", raw));
} else {
return undefined;
}
} }
set udp(port: number | undefined) { set udp(port: number | undefined) {
if (port === undefined) { setNumberAsStringValue(this, "udp", "udp", port);
this.delete("udp");
} else {
this.set("udp", convertToBytes("udp", port.toString(10)));
}
} }
get ip6(): string | undefined { get ip6(): string | undefined {
const raw = this.get("ip6"); return getStringValue(this, "ip6", "ip6");
if (raw) {
return convertToString("ip6", raw) as string;
} else {
return undefined;
}
} }
set ip6(ip: string | undefined) { set ip6(ip: string | undefined) {
if (ip) { setStringValue(this, "ip6", "ip6", ip);
this.set("ip6", convertToBytes("ip6", ip));
} else {
this.delete("ip6");
}
} }
get tcp6(): number | undefined { get tcp6(): number | undefined {
const raw = this.get("tcp6"); return getNumberAsStringValue(this, "tcp6", "tcp");
if (raw) {
return Number(convertToString("tcp", raw));
} else {
return undefined;
}
} }
set tcp6(port: number | undefined) { set tcp6(port: number | undefined) {
if (port === undefined) { setNumberAsStringValue(this, "tcp6", "tcp", port);
this.delete("tcp6");
} else {
this.set("tcp6", convertToBytes("tcp", port.toString(10)));
}
} }
get udp6(): number | undefined { get udp6(): number | undefined {
const raw = this.get("udp6"); return getNumberAsStringValue(this, "udp6", "udp");
if (raw) {
return Number(convertToString("udp", raw));
} else {
return undefined;
}
} }
set udp6(port: number | undefined) { set udp6(port: number | undefined) {
if (port === undefined) { setNumberAsStringValue(this, "udp6", "udp", port);
this.delete("udp6");
} else {
this.set("udp6", convertToBytes("udp", port.toString(10)));
}
} }
/** /**
@ -180,12 +126,7 @@ export class RawEnr extends Map<ENRKey, ENRValue> {
* ie, without a peer id. * ie, without a peer id.
*/ */
set multiaddrs(multiaddrs: Multiaddr[] | undefined) { set multiaddrs(multiaddrs: Multiaddr[] | undefined) {
if (multiaddrs === undefined) { deleteUndefined(this, "multiaddrs", multiaddrs, encodeMultiaddrs);
this.delete("multiaddrs");
} else {
const multiaddrsBuf = encodeMultiaddrs(multiaddrs);
this.set("multiaddrs", multiaddrsBuf);
}
} }
/** /**
@ -202,11 +143,62 @@ export class RawEnr extends Map<ENRKey, ENRValue> {
* Set the `waku2` field on the ENR. * Set the `waku2` field on the ENR.
*/ */
set waku2(waku2: Waku2 | undefined) { set waku2(waku2: Waku2 | undefined) {
if (waku2 === undefined) { deleteUndefined(
this.delete("waku2"); this,
} else { "waku2",
const byte = encodeWaku2(waku2); waku2,
this.set("waku2", new Uint8Array([byte])); (w) => new Uint8Array([encodeWaku2(w)])
} );
}
}
function getStringValue(
map: Map<ENRKey, ENRValue>,
key: ENRKey,
proto: string
): string | undefined {
const raw = map.get(key);
if (!raw) return;
return convertToString(proto, raw);
}
function getNumberAsStringValue(
map: Map<ENRKey, ENRValue>,
key: ENRKey,
proto: string
): number | undefined {
const raw = map.get(key);
if (!raw) return;
return Number(convertToString(proto, raw));
}
function setStringValue(
map: Map<ENRKey, ENRValue>,
key: ENRKey,
proto: string,
value: string | undefined
): void {
deleteUndefined(map, key, value, convertToBytes.bind({}, proto));
}
function setNumberAsStringValue(
map: Map<ENRKey, ENRValue>,
key: ENRKey,
proto: string,
value: number | undefined
): void {
setStringValue(map, key, proto, value?.toString(10));
}
function deleteUndefined<K, V, W>(
map: Map<K, W>,
key: K,
value: V | undefined,
transform: (v: V) => W
): void {
if (value !== undefined) {
map.set(key, transform(value));
} else {
map.delete(key);
} }
} }