Skip to content

Commit 1900490

Browse files
authored
feat: add typescript types + type tests (#105)
* feat: add typescript types * fix: remove duplicate string coersion * feat: add typescript type tests
1 parent 541bf87 commit 1900490

File tree

4 files changed

+229
-1
lines changed

4 files changed

+229
-1
lines changed

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"test": "aegir test",
1010
"test:node": "aegir test -t node",
1111
"test:browser": "aegir test -t browser",
12+
"test:types": "npx tsc",
1213
"build": "aegir build",
1314
"release": "aegir release",
1415
"release-minor": "aegir release --type minor",
@@ -39,6 +40,10 @@
3940
"varint": "^5.0.0"
4041
},
4142
"devDependencies": {
43+
"@types/chai": "^4.2.8",
44+
"@types/dirty-chai": "^2.0.2",
45+
"@types/mocha": "^7.0.1",
46+
"@types/node": "^13.7.0",
4247
"aegir": "^20.0.0",
4348
"bundlesize": "~0.18.0",
4449
"chai": "^4.2.0",

src/convert.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function onion32buf (str) {
183183
function buf2onion (buf) {
184184
const addrBytes = buf.slice(0, buf.length - 2)
185185
const portBytes = buf.slice(buf.length - 2)
186-
const addr = base32.encode(addrBytes).toString('ascii').toLowerCase()
186+
const addr = base32.encode(addrBytes).toLowerCase()
187187
const port = buf2port(portBytes)
188188
return addr + ':' + port
189189
}

src/index.d.ts

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/// <reference types="node" />
2+
3+
declare interface NetOptions {
4+
family: 'ipv4' | 'ipv6';
5+
host: string;
6+
transport: string;
7+
port: number;
8+
}
9+
10+
declare interface Protocol {
11+
code: number;
12+
size: number;
13+
name: string;
14+
resolvable: boolean;
15+
path: boolean;
16+
}
17+
18+
declare interface Protocols {
19+
table: [number, number, string, boolean?, boolean?][];
20+
names: Record<string, Protocol>;
21+
codes: Record<number, Protocol>;
22+
object(code: number, size: number, name: string, resolvable?: any, path?: any): Protocol;
23+
}
24+
25+
declare interface NodeAddress {
26+
family: 4 | 6;
27+
address: string;
28+
port: number;
29+
}
30+
31+
declare type MultiaddrInput = string | Buffer | Multiaddr;
32+
33+
declare class Multiaddr {
34+
/**
35+
* Creates a [multiaddr](https://github.com/multiformats/multiaddr) from
36+
* a Buffer, String or another Multiaddr instance
37+
* public key.
38+
* @param addr - If String or Buffer, needs to adhere
39+
* to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format)
40+
*/
41+
constructor(addr?: MultiaddrInput);
42+
43+
/**
44+
* Returns Multiaddr as a String
45+
*/
46+
toString(): string;
47+
48+
/**
49+
* Returns Multiaddr as a JSON encoded object
50+
*/
51+
toJSON(): string;
52+
53+
/**
54+
* Returns Multiaddr as a convinient options object to be used with net.createConnection
55+
*/
56+
toOptions(): NetOptions;
57+
58+
/**
59+
* Returns Multiaddr as a human-readable string
60+
*/
61+
inspect(): string;
62+
63+
/**
64+
* Returns the protocols the Multiaddr is defined with, as an array of objects, in
65+
* left-to-right order. Each object contains the protocol code, protocol name,
66+
* and the size of its address space in bits.
67+
* [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv)
68+
*/
69+
protos(): Protocol[];
70+
71+
/**
72+
* Returns the codes of the protocols in left-to-right order.
73+
* [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv)
74+
*/
75+
protoCodes(): number[];
76+
77+
/**
78+
* Returns the names of the protocols in left-to-right order.
79+
* [See list of protocols](https://github.com/multiformats/multiaddr/blob/master/protocols.csv)
80+
*/
81+
protoNames(): string[];
82+
83+
/**
84+
* Returns a tuple of parts
85+
*/
86+
tuples(): [number, Buffer][];
87+
88+
/**
89+
* Returns a tuple of string/number parts
90+
*/
91+
stringTuples(): [number, string | number][];
92+
93+
/**
94+
* Encapsulates a Multiaddr in another Multiaddr
95+
*/
96+
encapsulate(addr: MultiaddrInput): Multiaddr;
97+
98+
/**
99+
* Decapsulates a Multiaddr from another Multiaddr
100+
*/
101+
decapsulate(addr: MultiaddrInput): Multiaddr;
102+
103+
/**
104+
* A more reliable version of `decapsulate` if you are targeting a
105+
* specific code, such as 421 (the `p2p` protocol code). The last index of the code
106+
* will be removed from the `Multiaddr`, and a new instance will be returned.
107+
* If the code is not present, the original `Multiaddr` is returned.
108+
*/
109+
decapsulateCode(code: number): Multiaddr;
110+
111+
/**
112+
* Extract the peerId if the multiaddr contains one
113+
*/
114+
getPeerId(): string;
115+
116+
/**
117+
* Extract the path if the multiaddr contains one
118+
*/
119+
getPath(): string | null;
120+
121+
/**
122+
* Checks if two Multiaddrs are the same
123+
*/
124+
equals(addr: Multiaddr): boolean;
125+
126+
/**
127+
* Gets a Multiaddrs node-friendly address object. Note that protocol information
128+
* is left out: in Node (and most network systems) the protocol is unknowable
129+
* given only the address.
130+
*
131+
* Has to be a ThinWaist Address, otherwise throws error
132+
*/
133+
nodeAddress(): NodeAddress;
134+
135+
/**
136+
* Returns if a Multiaddr is a Thin Waist address or not.
137+
*
138+
* Thin Waist is if a Multiaddr adheres to the standard combination of:
139+
*
140+
* `{IPv4, IPv6}/{TCP, UDP}`
141+
*/
142+
isThinWaistAddress(addr?: Multiaddr): boolean;
143+
}
144+
145+
declare interface MultiaddrClass {
146+
/**
147+
* Creates a [multiaddr](https://github.com/multiformats/multiaddr) from
148+
* a Buffer, String or another Multiaddr instance
149+
* public key.
150+
* @param addr - If String or Buffer, needs to adhere
151+
* to the address format of a [multiaddr](https://github.com/multiformats/multiaddr#string-format)
152+
*/
153+
(addr?: MultiaddrInput): Multiaddr;
154+
155+
/**
156+
* Creates a Multiaddr from a node-friendly address object
157+
*/
158+
fromNodeAddress(addr: NodeAddress, transport: string): Multiaddr;
159+
160+
/**
161+
* Object containing table, names and codes of all supported protocols.
162+
* To get the protocol values from a Multiaddr, you can use
163+
* [`.protos()`](#multiaddrprotos),
164+
* [`.protoCodes()`](#multiaddrprotocodes) or
165+
* [`.protoNames()`](#multiaddrprotonames)
166+
*/
167+
protocols: Protocols;
168+
169+
/**
170+
* Returns if something is a Multiaddr
171+
*/
172+
isMultiaddr(addr: unknown): addr is Multiaddr;
173+
174+
/**
175+
* Returns if something is a Multiaddr that is a name
176+
*/
177+
isName(addr: Multiaddr): boolean;
178+
179+
/**
180+
* Returns an array of multiaddrs, by resolving the multiaddr that is a name
181+
*/
182+
resolve(addr: Multiaddr): Promise<Multiaddr[]>
183+
}
184+
185+
export = MultiaddrClass;

tsconfig.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"lib": [
6+
"es6"
7+
],
8+
"target": "ES5",
9+
"noImplicitAny": false,
10+
"noImplicitThis": true,
11+
"strictFunctionTypes": true,
12+
"strictNullChecks": true,
13+
"esModuleInterop": true,
14+
"resolveJsonModule": true,
15+
"allowJs": true,
16+
"checkJs": true,
17+
"baseUrl": ".",
18+
"paths": {
19+
"multiaddr": [
20+
"./src",
21+
"../src",
22+
]
23+
},
24+
"types": [
25+
"node",
26+
"mocha",
27+
"chai"
28+
],
29+
"noEmit": true,
30+
"forceConsistentCasingInFileNames": true
31+
},
32+
"files": [
33+
"./src/index.d.ts",
34+
],
35+
"include": [
36+
"./test/**/*.spec.js"
37+
]
38+
}

0 commit comments

Comments
 (0)