From 141fcc4918466340b6b5b846889772fe49c6c888 Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Tue, 9 Jan 2018 23:11:09 -0800 Subject: [PATCH] Add flow type signatures. --- .flowconfig | 8 ++++++ package.json | 3 ++- src/codec.js.flow | 32 ++++++++++++++++++++++++ src/convert.js.flow | 13 ++++++++++ src/index.js.flow | 49 +++++++++++++++++++++++++++++++++++++ src/protocols-table.js.flow | 21 ++++++++++++++++ 6 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 .flowconfig create mode 100644 src/codec.js.flow create mode 100644 src/convert.js.flow create mode 100644 src/index.js.flow create mode 100644 src/protocols-table.js.flow diff --git a/.flowconfig b/.flowconfig new file mode 100644 index 00000000..ff4ebd4e --- /dev/null +++ b/.flowconfig @@ -0,0 +1,8 @@ +[ignore] +.*/node_modules/documentation/* + +[libs] + +[include] + +[options] diff --git a/package.json b/package.json index f5fc785d..21234451 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,8 @@ "lodash.filter": "^4.6.0", "lodash.map": "^4.6.0", "varint": "^5.0.0", - "xtend": "^4.0.1" + "xtend": "^4.0.1", + "callback.flow": "^1.0.0" }, "devDependencies": { "aegir": "^12.3.0", diff --git a/src/codec.js.flow b/src/codec.js.flow new file mode 100644 index 00000000..cc24f580 --- /dev/null +++ b/src/codec.js.flow @@ -0,0 +1,32 @@ +// @flow + +import type { Protocol } from "./protocols-table.js" + +export type Name$Address = [string, string] +export type Code$Buffer = [number, Buffer] +export type { Protocol } +export class ParseError extends Error {} + +declare export default { + stringToStringTuples(string): Name$Address[], + stringTuplesToString(Name$Address[]): string, + tuplesToStringTuples(Code$Buffer[]): Name$Address[], + stringTuplesToTuples(Name$Address[]): Code$Buffer[], + + bufferToTuples(Buffer): Code$Buffer[], + tuplesToBuffer(Code$Buffer[]): Buffer, + + bufferToString(Buffer): string, + stringToBuffer(string): Buffer, + + fromString(string): Buffer, + fromBuffer(Buffer): Buffer, + validateBuffer(mixed): void, + isValidBuffer(mixed): boolean, + cleanPath(string): string, + + ParseError(string): ParseError, + protoFromTuple(Name$Address | Code$Buffer): Protocol, + + sizeForAddr(Protocol, Buffer): number +} diff --git a/src/convert.js.flow b/src/convert.js.flow new file mode 100644 index 00000000..1b5232de --- /dev/null +++ b/src/convert.js.flow @@ -0,0 +1,13 @@ +// @flow + +import type { Protocol, Code } from "./protocols-table" +export type { Protocol, Code } + +declare export default { + // So this just flips the type it seems, I wish there were two different named + // functions instead. + (Protocol, Buffer): string, + (Protocol, string): Buffer, + toString(Protocol, Buffer): string, + toBuffer(Protocol, string): Buffer +} diff --git a/src/index.js.flow b/src/index.js.flow new file mode 100644 index 00000000..3ad1edfc --- /dev/null +++ b/src/index.js.flow @@ -0,0 +1,49 @@ +// @flow + +import type { Callback } from "callback.flow" +import type { Protocol, Code } from "./protocols-table" +import protocols from "./protocols-table" +export opaque type String: string = string +export type { Protocol, Code, Callback } + +export type Options = { + family: string, + host: string, + transport: string, + port: string +} + +export type NodeAddress = { + family: string, + address: string, + port: string +} + +export interface Multiaddr { + buffer: Buffer; + toString(): String; + toOptions(): Options; + inspect(): string; + protos(): Protocol[]; + protoCodes(): Code[]; + protoNames(): string[]; + tuples(): Array<[Code, Buffer]>; + stringTuples(): Array<[Code, string | number]>; + encapsulate(string | Buffer | Multiaddr): Multiaddr; + decapsulate(string | Buffer | Multiaddr): Multiaddr; + getPeerId(): ?string; + equals(Multiaddr): boolean; + nodeAddress(): NodeAddress; + isThinWaistAddress(Multiaddr): boolean; + fromStupidString(string): empty; +} + +declare export default { + Buffer: typeof Buffer, + protocols: typeof protocols, + (string | Buffer | Multiaddr): Multiaddr, + fromNodeAddress(NodeAddress, transport: string): Multiaddr, + isMultiaddr(mixed): boolean, + isName(mixed): boolean, + resolve(mixed, Callback): void +} diff --git a/src/protocols-table.js.flow b/src/protocols-table.js.flow new file mode 100644 index 00000000..955e435d --- /dev/null +++ b/src/protocols-table.js.flow @@ -0,0 +1,21 @@ +// @flow + +export opaque type Code: number = number +export type Size = number + +export type Protocol = { + code: Code, + size: Size, + name: string, + resolvable: boolean +} + +declare export default { + (string | number): Protocol, + lengthPrefixedVarSize: number, + V: number, + table: Array<[number, number, string]>, + names: { [string]: Protocol }, + codes: { [number]: Protocol }, + object(Code, Size, string, boolean): Protocol +}