-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Tim Diekmann <21277928+TimDiekmann@users.noreply.github.com>
- Loading branch information
1 parent
99ac6db
commit 134b75f
Showing
8 changed files
with
499 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
libs/@local/harpc/client/typescript/src/net/internal/multiaddr.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { | ||
isMultiaddr, | ||
type Multiaddr, | ||
type MultiaddrInput, | ||
type ResolveOptions, | ||
} from "@multiformats/multiaddr"; | ||
import { Equal, Hash, pipe, Predicate } from "effect"; | ||
|
||
import { createProto, hashUint8Array } from "../../utils.js"; | ||
|
||
const MultiaddrSymbol = Symbol.for("@multiformats/js-multiaddr/multiaddr"); | ||
type MultiaddrSymbol = typeof MultiaddrSymbol; | ||
|
||
const TypeId: unique symbol = Symbol( | ||
"@local/harpc-client/net/internal/HashableMultiaddr", | ||
); | ||
type TypeId = typeof TypeId; | ||
|
||
/** @internal */ | ||
export interface HashableMultiaddr extends Multiaddr, Equal.Equal { | ||
readonly [TypeId]: TypeId; | ||
readonly [MultiaddrSymbol]: true; | ||
readonly inner: Multiaddr; | ||
} | ||
|
||
const HashableMultiaddrProto: Omit<HashableMultiaddr, "inner"> = { | ||
[TypeId]: TypeId, | ||
[MultiaddrSymbol]: true, | ||
|
||
get bytes() { | ||
return (this as HashableMultiaddr).inner.bytes; | ||
}, | ||
|
||
toString(this: HashableMultiaddr) { | ||
return this.inner.toString(); | ||
}, | ||
|
||
toJSON(this: HashableMultiaddr) { | ||
return this.inner.toString(); | ||
}, | ||
|
||
toOptions(this: HashableMultiaddr) { | ||
return this.inner.toOptions(); | ||
}, | ||
|
||
protos(this: HashableMultiaddr) { | ||
return this.inner.protos(); | ||
}, | ||
|
||
protoCodes(this: HashableMultiaddr) { | ||
return this.inner.protoCodes(); | ||
}, | ||
|
||
protoNames(this: HashableMultiaddr) { | ||
return this.inner.protoNames(); | ||
}, | ||
|
||
tuples(this: HashableMultiaddr) { | ||
return this.inner.tuples(); | ||
}, | ||
|
||
stringTuples(this: HashableMultiaddr) { | ||
return this.inner.stringTuples(); | ||
}, | ||
|
||
encapsulate(this: HashableMultiaddr, addr: MultiaddrInput) { | ||
return this.inner.encapsulate(addr); | ||
}, | ||
|
||
decapsulate(this: HashableMultiaddr, addr: Multiaddr | string) { | ||
return this.inner.decapsulate(addr); | ||
}, | ||
|
||
decapsulateCode(this: HashableMultiaddr, code: number) { | ||
return this.inner.decapsulateCode(code); | ||
}, | ||
|
||
getPeerId(this: HashableMultiaddr) { | ||
return this.inner.getPeerId(); | ||
}, | ||
|
||
getPath(this: HashableMultiaddr) { | ||
return this.inner.getPath(); | ||
}, | ||
|
||
equals(this: HashableMultiaddr, other: HashableMultiaddr) { | ||
return this.inner.equals(other.inner); | ||
}, | ||
|
||
resolve(this: HashableMultiaddr, options?: ResolveOptions) { | ||
return this.inner.resolve(options); | ||
}, | ||
|
||
nodeAddress(this: HashableMultiaddr) { | ||
return this.inner.nodeAddress(); | ||
}, | ||
|
||
isThinWaistAddress(this: HashableMultiaddr) { | ||
return this.inner.isThinWaistAddress(); | ||
}, | ||
|
||
[Equal.symbol](this: HashableMultiaddr, other: unknown) { | ||
return isMultiaddr(other) && this.inner.equals(other); | ||
}, | ||
|
||
[Hash.symbol](this: HashableMultiaddr) { | ||
return pipe( | ||
Hash.hash(MultiaddrSymbol), | ||
Hash.combine(hashUint8Array(this.bytes)), | ||
Hash.cached(this), | ||
); | ||
}, | ||
}; | ||
|
||
const isHashableMultiaddr = (value: unknown): value is HashableMultiaddr => | ||
Predicate.hasProperty(value, TypeId); | ||
|
||
/** @internal */ | ||
export const make = (inner: Multiaddr): HashableMultiaddr => | ||
isHashableMultiaddr(inner) | ||
? inner | ||
: (createProto(HashableMultiaddrProto, { | ||
inner, | ||
}) satisfies HashableMultiaddr); |
Oops, something went wrong.