diff --git a/package.json b/package.json index 637dae53..bc709274 100644 --- a/package.json +++ b/package.json @@ -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 ", @@ -66,7 +66,7 @@ "Richard Littauer ", "Dmitriy Ryajov ", "Marcin Rataj ", - "Robert Kiel ", + "Robert Kiel ", "Maciej Krüger ", "Oli Evans ", "Alan Shaw ", diff --git a/src/index.d.ts b/src/index.d.ts index 0fca50e3..ceb22821 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -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; @@ -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 @@ -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 @@ -156,11 +156,11 @@ declare class Multiaddr { /** * Resolve multiaddr if containing resolvable hostname. */ - resolve(): Promise> + resolve(): Promise } declare namespace Multiaddr { - const resolvers: Map < string, (addr: Multiaddr) => Promise < Array < string >>> + const resolvers: Map Promise> /** * Creates a Multiaddr from a node-friendly address object diff --git a/src/index.js b/src/index.js index fc464bd6..9e9d454c 100644 --- a/src/index.js +++ b/src/index.js @@ -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]) @@ -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('/')) } diff --git a/src/resolvers/dns.d.ts b/src/resolvers/dns.d.ts new file mode 100644 index 00000000..7f3da17b --- /dev/null +++ b/src/resolvers/dns.d.ts @@ -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 } \ No newline at end of file diff --git a/test/index.spec.js b/test/index.spec.js index c154eb4a..27f9682b 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -486,6 +486,9 @@ describe('helpers', () => { transport: 'tcp', port: 1234 }) + + const errRegex = /Invalid addr family/ + expect(() => multiaddr('/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC').toOptions()).to.throw(errRegex) }) }) @@ -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' diff --git a/test/types.spec.ts b/test/types.spec.ts index ba9129c9..72ee02e8 100644 --- a/test/types.spec.ts +++ b/test/types.spec.ts @@ -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() } diff --git a/tsconfig.json b/tsconfig.json index 60e2c5b1..7a2adc79 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,9 @@ { "compilerOptions": { "module": "commonjs", - "lib": ["es6"], + "lib": [ + "es6" + ], "target": "ES5", "noImplicitAny": false, "noImplicitThis": true, @@ -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" + ] +} \ No newline at end of file