Skip to content

Minor fixes (types and validation) #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@types/node": "^14.0.11",
"aegir": "^29.0.1",
"sinon": "^9.2.0",
"typescript": "^3.9.5"
"typescript": "^4.1"
},
"contributors": [
"David Dias <[email protected]>",
Expand All @@ -66,7 +66,7 @@
"Richard Littauer <[email protected]>",
"Dmitriy Ryajov <[email protected]>",
"Marcin Rataj <[email protected]>",
"Robert Kiel <robert.kiel@validitylabs.org>",
"Robert Kiel <robert.kiel@hoprnet.org>",
"Maciej Krüger <[email protected]>",
"Oli Evans <[email protected]>",
"Alan Shaw <[email protected]>",
Expand Down
12 changes: 6 additions & 6 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ declare interface Protocols {
declare type NodeAddress = {
family: "IPv4" | "IPv6";
address: string;
port: string;
port: number;
};

declare type MultiaddrInput = string | Uint8Array | Multiaddr | null;
Expand Down Expand Up @@ -95,12 +95,12 @@ declare class Multiaddr {
/**
* Returns a tuple of parts
*/
tuples(): [number, Uint8Array][];
tuples(): [code: number, value: Uint8Array][];

/**
* Returns a tuple of string/number parts
*/
stringTuples(): [number, string | number][];
stringTuples(): [code: number, value: string | number][];

/**
* Encapsulates a Multiaddr in another Multiaddr
Expand All @@ -123,7 +123,7 @@ declare class Multiaddr {
/**
* Extract the peerId if the multiaddr contains one
*/
getPeerId(): string;
getPeerId(): string | null;

/**
* Extract the path if the multiaddr contains one
Expand Down Expand Up @@ -156,11 +156,11 @@ declare class Multiaddr {
/**
* Resolve multiaddr if containing resolvable hostname.
*/
resolve(): Promise<Array<Multiaddr>>
resolve(): Promise<Multiaddr[]>
}

declare namespace Multiaddr {
const resolvers: Map < string, (addr: Multiaddr) => Promise < Array < string >>>
const resolvers: Map<string, (addr: Multiaddr) => Promise<string[]>>

/**
* Creates a Multiaddr from a node-friendly address object
Expand Down
13 changes: 11 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,16 @@ Multiaddr.prototype.toJSON = Multiaddr.prototype.toString
Multiaddr.prototype.toOptions = function toOptions () {
const opts = {}
const parsed = this.toString().split('/')
opts.family = parsed[1] === 'ip4' ? 'ipv4' : 'ipv6'
switch (parsed[1]) {
case 'ip4':
opts.family = 'ipv4'
break
case 'ip6':
opts.family = 'ipv6'
break
default:
throw new Error(`Invalid addr family. Got '${parsed[1]}' instead of 'ip4' or 'ip6'`)
}
opts.host = parsed[2]
opts.transport = parsed[3]
opts.port = parseInt(parsed[4])
Expand Down Expand Up @@ -462,7 +471,7 @@ Multiaddr.fromNodeAddress = function fromNodeAddress (addr, transport) {
ip = 'ip6'
break
default:
throw Error(`Invalid addr family. Got '${addr.family}' instead of 'IPv4' or 'IPv6'`)
throw new Error(`Invalid addr family. Got '${addr.family}' instead of 'IPv4' or 'IPv6'`)
}
return Multiaddr('/' + [ip, addr.address, transport, addr.port].join('/'))
}
Expand Down
7 changes: 7 additions & 0 deletions src/resolvers/dns.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Explains to Typescript that 'dns.js' exports
// a class that has the same properties as Resolver
type DNSResolver = typeof import('dns').promises.Resolver

declare var Resolver: DNSResolver

export { Resolver }
5 changes: 4 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ describe('helpers', () => {
transport: 'tcp',
port: 1234
})

const errRegex = /Invalid addr family/
expect(() => multiaddr('/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC').toOptions()).to.throw(errRegex)
})
})

Expand Down Expand Up @@ -745,7 +748,7 @@ describe('helpers', () => {
multiaddr.fromNodeAddress({
address: '192.168.0.1',
family: 'IPv4',
port: '1234'
port: 1234
}, 'tcp').toString()
).to.be.eql(
'/ip4/192.168.0.1/tcp/1234'
Expand Down
4 changes: 2 additions & 2 deletions test/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export const maFromConstructorFunction: Multiaddr = Multiaddr.fromNodeAddress(
{
family: 'IPv4',
address: '127.0.0.1',
port: '12345'
port: 12345
},
'udp'
)

export function doSthWithMa (ma: Multiaddr): void {
export function doSthWithMa(ma: Multiaddr): void {
ma.toOptions()
}
12 changes: 9 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"lib": [
"es6"
],
"target": "ES5",
"noImplicitAny": false,
"noImplicitThis": true,
Expand All @@ -14,5 +16,9 @@
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/index.d.ts", "test/**/*.spec.js", "test/**/*.spec.ts"]
}
"include": [
"src/index.d.ts",
"test/**/*.spec.js",
"test/**/*.spec.ts"
]
}