fix: optional fields

This commit is contained in:
Irakli Gozalishvili 2020-11-30 22:22:28 -08:00
parent b7f10727d6
commit 7fd26cf6b9
No known key found for this signature in database
GPG Key ID: C80F9B292FB470DE
7 changed files with 94 additions and 52 deletions

View File

@ -13,7 +13,7 @@
"lint": "aegir lint", "lint": "aegir lint",
"build": "aegir build", "build": "aegir build",
"pregenerate:types": "rimraf './src/**/*.d.ts'", "pregenerate:types": "rimraf './src/**/*.d.ts'",
"generate:types": "tsc", "generate:types": "tsc --build",
"test": "aegir test", "test": "aegir test",
"test:node": "aegir test --target node", "test:node": "aegir test --target node",
"test:browser": "aegir test --target browser", "test:browser": "aegir test --target browser",
@ -69,7 +69,7 @@
"aegir": "^25.0.0", "aegir": "^25.0.0",
"it-handshake": "^1.0.1", "it-handshake": "^1.0.1",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"typescript": "^4.1.2" "typescript": "^4.0.5"
}, },
"contributors": [ "contributors": [
"Alan Shaw <alan.shaw@protocol.ai>", "Alan Shaw <alan.shaw@protocol.ai>",

View File

@ -3,11 +3,11 @@ export function msgId(from: string, seqno: Uint8Array): Uint8Array;
export function noSignMsgId(data: Uint8Array): Uint8Array; export function noSignMsgId(data: Uint8Array): Uint8Array;
export function anyMatch(a: Set<any> | any[], b: Set<any> | any[]): boolean; export function anyMatch(a: Set<any> | any[], b: Set<any> | any[]): boolean;
export function ensureArray<T>(maybeArray: T | T[]): T[]; export function ensureArray<T>(maybeArray: T | T[]): T[];
export function normalizeInRpcMessage<T extends Object>(message: T, peerId?: string | undefined): T & { export function normalizeInRpcMessage<T extends unknown>(message: T, peerId?: string | undefined): T & {
from?: string | undefined; from?: string | undefined;
peerId?: string | undefined; peerId?: string | undefined;
}; };
export function normalizeOutRpcMessage<T extends Object>(message: T): T & { export function normalizeOutRpcMessage<T extends unknown>(message: T): T & {
from?: Uint8Array | undefined; from?: Uint8Array | undefined;
data?: Uint8Array | undefined; data?: Uint8Array | undefined;
}; };

View File

@ -8,22 +8,9 @@ declare class Topology {
*/ */
static isTopology(other: any): other is Topology; static isTopology(other: any): other is Topology;
/** /**
* @param {Object} props * @param {Options} options
* @param {number} [props.min] minimum needed connections (default: 0)
* @param {number} [props.max] maximum needed connections (default: Infinity)
* @param {Object} [props.handlers]
* @param {function} [props.handlers.onConnect] protocol "onConnect" handler
* @param {function} [props.handlers.onDisconnect] protocol "onDisconnect" handler
* @constructor
*/ */
constructor({ min, max, handlers }: { constructor({ min, max, handlers }: Options);
min: number | undefined;
max: number | undefined;
handlers: {
onConnect?: Function | undefined;
onDisconnect?: Function | undefined;
} | undefined;
});
min: number; min: number;
max: number; max: number;
_onConnect: Function; _onConnect: Function;
@ -48,4 +35,28 @@ declare class Topology {
disconnect(peerId: import("peer-id")): void; disconnect(peerId: import("peer-id")): void;
get [topologySymbol](): boolean; get [topologySymbol](): boolean;
} }
declare namespace Topology {
export { Options, Handlers };
}
declare const topologySymbol: unique symbol; declare const topologySymbol: unique symbol;
type Options = {
/**
* - minimum needed connections.
*/
min?: number | undefined;
/**
* - maximum needed connections.
*/
max?: number | undefined;
handlers?: Handlers | undefined;
};
type Handlers = {
/**
* - protocol "onConnect" handler
*/
onConnect?: Function | undefined;
/**
* - protocol "onDisconnect" handler
*/
onDisconnect?: Function | undefined;
};

View File

@ -6,13 +6,7 @@ const topologySymbol = Symbol.for('@libp2p/js-interfaces/topology')
class Topology { class Topology {
/** /**
* @param {Object} props * @param {Options} options
* @param {number} [props.min] minimum needed connections (default: 0)
* @param {number} [props.max] maximum needed connections (default: Infinity)
* @param {Object} [props.handlers]
* @param {function} [props.handlers.onConnect] protocol "onConnect" handler
* @param {function} [props.handlers.onDisconnect] protocol "onDisconnect" handler
* @constructor
*/ */
constructor ({ constructor ({
min = 0, min = 0,
@ -70,4 +64,15 @@ class Topology {
} }
} }
/**
* @typedef {Object} Options
* @property {number} [min=0] - minimum needed connections.
* @property {number} [max=Infinity] - maximum needed connections.
* @property {Handlers} [handlers]
*
* @typedef {Object} Handlers
* @property {Function} [onConnect] - protocol "onConnect" handler
* @property {Function} [onDisconnect] - protocol "onDisconnect" handler
*/
module.exports = Topology module.exports = Topology

View File

@ -8,24 +8,9 @@ declare class MulticodecTopology extends Topology {
*/ */
static isMulticodecTopology(other: any): other is MulticodecTopology; static isMulticodecTopology(other: any): other is MulticodecTopology;
/** /**
* @param {Object} props * @param {TopologyOptions & MulticodecOptions} props
* @param {number} [props.min] minimum needed connections (default: 0)
* @param {number} [props.max] maximum needed connections (default: Infinity)
* @param {Array<string>} props.multicodecs protocol multicodecs
* @param {Object} props.handlers
* @param {function} props.handlers.onConnect protocol "onConnect" handler
* @param {function} props.handlers.onDisconnect protocol "onDisconnect" handler
* @constructor
*/ */
constructor({ min, max, multicodecs, handlers }: { constructor({ min, max, multicodecs, handlers }: TopologyOptions & MulticodecOptions);
min: number | undefined;
max: number | undefined;
multicodecs: Array<string>;
handlers: {
onConnect: Function;
onDisconnect: Function;
};
});
multicodecs: string[]; multicodecs: string[];
/** /**
* Check if a new peer support the multicodecs for this topology. * Check if a new peer support the multicodecs for this topology.
@ -53,11 +38,41 @@ declare class MulticodecTopology extends Topology {
multiaddrs: Array<Multiaddr>; multiaddrs: Array<Multiaddr>;
protocols: Array<string>; protocols: Array<string>;
}>): void; }>): void;
get [multicodecTopologySymbol](): boolean;
} }
declare namespace MulticodecTopology { declare namespace MulticodecTopology {
export { PeerId, Multiaddr, Connection }; export { PeerId, Multiaddr, Connection, TopologyOptions, MulticodecOptions, Handlers };
} }
import Topology = require("."); import Topology = require(".");
type PeerId = import("peer-id"); type PeerId = import("peer-id");
type Connection = typeof import("../connection"); type Connection = typeof import("../connection");
type Multiaddr = import("multiaddr"); type Multiaddr = import("multiaddr");
declare const multicodecTopologySymbol: unique symbol;
type TopologyOptions = {
/**
* - minimum needed connections.
*/
min?: number | undefined;
/**
* - maximum needed connections.
*/
max?: number | undefined;
handlers?: Topology.Handlers | undefined;
};
type MulticodecOptions = {
/**
* - protocol multicodecs
*/
multicodecs: string[];
handlers: Required<Handlers>;
};
type Handlers = {
/**
* - protocol "onConnect" handler
*/
onConnect?: Function | undefined;
/**
* - protocol "onDisconnect" handler
*/
onDisconnect?: Function | undefined;
};

View File

@ -6,14 +6,7 @@ const multicodecTopologySymbol = Symbol.for('@libp2p/js-interfaces/topology/mult
class MulticodecTopology extends Topology { class MulticodecTopology extends Topology {
/** /**
* @param {Object} props * @param {TopologyOptions & MulticodecOptions} props
* @param {number} [props.min] minimum needed connections (default: 0)
* @param {number} [props.max] maximum needed connections (default: Infinity)
* @param {Array<string>} props.multicodecs protocol multicodecs
* @param {Object} props.handlers
* @param {function} props.handlers.onConnect protocol "onConnect" handler
* @param {function} props.handlers.onDisconnect protocol "onDisconnect" handler
* @constructor
*/ */
constructor ({ constructor ({
min, min,
@ -143,5 +136,10 @@ class MulticodecTopology extends Topology {
* @typedef {import('peer-id')} PeerId * @typedef {import('peer-id')} PeerId
* @typedef {import('multiaddr')} Multiaddr * @typedef {import('multiaddr')} Multiaddr
* @typedef {import('../connection')} Connection * @typedef {import('../connection')} Connection
* @typedef {import('.').Options} TopologyOptions
* @typedef {Object} MulticodecOptions
* @property {string[]} multicodecs - protocol multicodecs
* @property {Required<Handlers>} handlers
* @typedef {import('.').Handlers} Handlers
*/ */
module.exports = MulticodecTopology module.exports = MulticodecTopology

View File

@ -6,7 +6,20 @@
// Tells TypeScript to read JS files, as // Tells TypeScript to read JS files, as
// normally they are ignored as source files // normally they are ignored as source files
"allowJs": true, "allowJs": true,
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": false,
"noImplicitAny": false,
"noImplicitThis": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": false,
"strictFunctionTypes": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"strict": true, "strict": true,
"alwaysStrict": true,
"stripInternal": true,
// Generate d.ts files // Generate d.ts files
"declaration": true, "declaration": true,
// This compiler run should // This compiler run should