Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit 7fd26cf

Browse files
committed
fix: optional fields
1 parent b7f1072 commit 7fd26cf

File tree

7 files changed

+96
-54
lines changed

7 files changed

+96
-54
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"lint": "aegir lint",
1414
"build": "aegir build",
1515
"pregenerate:types": "rimraf './src/**/*.d.ts'",
16-
"generate:types": "tsc",
16+
"generate:types": "tsc --build",
1717
"test": "aegir test",
1818
"test:node": "aegir test --target node",
1919
"test:browser": "aegir test --target browser",
@@ -69,7 +69,7 @@
6969
"aegir": "^25.0.0",
7070
"it-handshake": "^1.0.1",
7171
"rimraf": "^3.0.2",
72-
"typescript": "^4.1.2"
72+
"typescript": "^4.0.5"
7373
},
7474
"contributors": [
7575
"Alan Shaw <alan.shaw@protocol.ai>",

src/pubsub/utils.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ export function msgId(from: string, seqno: Uint8Array): Uint8Array;
33
export function noSignMsgId(data: Uint8Array): Uint8Array;
44
export function anyMatch(a: Set<any> | any[], b: Set<any> | any[]): boolean;
55
export function ensureArray<T>(maybeArray: T | T[]): T[];
6-
export function normalizeInRpcMessage<T extends Object>(message: T, peerId?: string | undefined): T & {
6+
export function normalizeInRpcMessage<T extends unknown>(message: T, peerId?: string | undefined): T & {
77
from?: string | undefined;
88
peerId?: string | undefined;
99
};
10-
export function normalizeOutRpcMessage<T extends Object>(message: T): T & {
10+
export function normalizeOutRpcMessage<T extends unknown>(message: T): T & {
1111
from?: Uint8Array | undefined;
1212
data?: Uint8Array | undefined;
1313
};

src/topology/index.d.ts

+27-16
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,9 @@ declare class Topology {
88
*/
99
static isTopology(other: any): other is Topology;
1010
/**
11-
* @param {Object} props
12-
* @param {number} [props.min] minimum needed connections (default: 0)
13-
* @param {number} [props.max] maximum needed connections (default: Infinity)
14-
* @param {Object} [props.handlers]
15-
* @param {function} [props.handlers.onConnect] protocol "onConnect" handler
16-
* @param {function} [props.handlers.onDisconnect] protocol "onDisconnect" handler
17-
* @constructor
18-
*/
19-
constructor({ min, max, handlers }: {
20-
min: number | undefined;
21-
max: number | undefined;
22-
handlers: {
23-
onConnect?: Function | undefined;
24-
onDisconnect?: Function | undefined;
25-
} | undefined;
26-
});
11+
* @param {Options} options
12+
*/
13+
constructor({ min, max, handlers }: Options);
2714
min: number;
2815
max: number;
2916
_onConnect: Function;
@@ -48,4 +35,28 @@ declare class Topology {
4835
disconnect(peerId: import("peer-id")): void;
4936
get [topologySymbol](): boolean;
5037
}
38+
declare namespace Topology {
39+
export { Options, Handlers };
40+
}
5141
declare const topologySymbol: unique symbol;
42+
type Options = {
43+
/**
44+
* - minimum needed connections.
45+
*/
46+
min?: number | undefined;
47+
/**
48+
* - maximum needed connections.
49+
*/
50+
max?: number | undefined;
51+
handlers?: Handlers | undefined;
52+
};
53+
type Handlers = {
54+
/**
55+
* - protocol "onConnect" handler
56+
*/
57+
onConnect?: Function | undefined;
58+
/**
59+
* - protocol "onDisconnect" handler
60+
*/
61+
onDisconnect?: Function | undefined;
62+
};

src/topology/index.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ const topologySymbol = Symbol.for('@libp2p/js-interfaces/topology')
66

77
class Topology {
88
/**
9-
* @param {Object} props
10-
* @param {number} [props.min] minimum needed connections (default: 0)
11-
* @param {number} [props.max] maximum needed connections (default: Infinity)
12-
* @param {Object} [props.handlers]
13-
* @param {function} [props.handlers.onConnect] protocol "onConnect" handler
14-
* @param {function} [props.handlers.onDisconnect] protocol "onDisconnect" handler
15-
* @constructor
9+
* @param {Options} options
1610
*/
1711
constructor ({
1812
min = 0,
@@ -70,4 +64,15 @@ class Topology {
7064
}
7165
}
7266

67+
/**
68+
* @typedef {Object} Options
69+
* @property {number} [min=0] - minimum needed connections.
70+
* @property {number} [max=Infinity] - maximum needed connections.
71+
* @property {Handlers} [handlers]
72+
*
73+
* @typedef {Object} Handlers
74+
* @property {Function} [onConnect] - protocol "onConnect" handler
75+
* @property {Function} [onDisconnect] - protocol "onDisconnect" handler
76+
*/
77+
7378
module.exports = Topology

src/topology/multicodec-topology.d.ts

+34-19
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,9 @@ declare class MulticodecTopology extends Topology {
88
*/
99
static isMulticodecTopology(other: any): other is MulticodecTopology;
1010
/**
11-
* @param {Object} props
12-
* @param {number} [props.min] minimum needed connections (default: 0)
13-
* @param {number} [props.max] maximum needed connections (default: Infinity)
14-
* @param {Array<string>} props.multicodecs protocol multicodecs
15-
* @param {Object} props.handlers
16-
* @param {function} props.handlers.onConnect protocol "onConnect" handler
17-
* @param {function} props.handlers.onDisconnect protocol "onDisconnect" handler
18-
* @constructor
19-
*/
20-
constructor({ min, max, multicodecs, handlers }: {
21-
min: number | undefined;
22-
max: number | undefined;
23-
multicodecs: Array<string>;
24-
handlers: {
25-
onConnect: Function;
26-
onDisconnect: Function;
27-
};
28-
});
11+
* @param {TopologyOptions & MulticodecOptions} props
12+
*/
13+
constructor({ min, max, multicodecs, handlers }: TopologyOptions & MulticodecOptions);
2914
multicodecs: string[];
3015
/**
3116
* Check if a new peer support the multicodecs for this topology.
@@ -53,11 +38,41 @@ declare class MulticodecTopology extends Topology {
5338
multiaddrs: Array<Multiaddr>;
5439
protocols: Array<string>;
5540
}>): void;
41+
get [multicodecTopologySymbol](): boolean;
5642
}
5743
declare namespace MulticodecTopology {
58-
export { PeerId, Multiaddr, Connection };
44+
export { PeerId, Multiaddr, Connection, TopologyOptions, MulticodecOptions, Handlers };
5945
}
6046
import Topology = require(".");
6147
type PeerId = import("peer-id");
6248
type Connection = typeof import("../connection");
6349
type Multiaddr = import("multiaddr");
50+
declare const multicodecTopologySymbol: unique symbol;
51+
type TopologyOptions = {
52+
/**
53+
* - minimum needed connections.
54+
*/
55+
min?: number | undefined;
56+
/**
57+
* - maximum needed connections.
58+
*/
59+
max?: number | undefined;
60+
handlers?: Topology.Handlers | undefined;
61+
};
62+
type MulticodecOptions = {
63+
/**
64+
* - protocol multicodecs
65+
*/
66+
multicodecs: string[];
67+
handlers: Required<Handlers>;
68+
};
69+
type Handlers = {
70+
/**
71+
* - protocol "onConnect" handler
72+
*/
73+
onConnect?: Function | undefined;
74+
/**
75+
* - protocol "onDisconnect" handler
76+
*/
77+
onDisconnect?: Function | undefined;
78+
};

src/topology/multicodec-topology.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@ const multicodecTopologySymbol = Symbol.for('@libp2p/js-interfaces/topology/mult
66

77
class MulticodecTopology extends Topology {
88
/**
9-
* @param {Object} props
10-
* @param {number} [props.min] minimum needed connections (default: 0)
11-
* @param {number} [props.max] maximum needed connections (default: Infinity)
12-
* @param {Array<string>} props.multicodecs protocol multicodecs
13-
* @param {Object} props.handlers
14-
* @param {function} props.handlers.onConnect protocol "onConnect" handler
15-
* @param {function} props.handlers.onDisconnect protocol "onDisconnect" handler
16-
* @constructor
9+
* @param {TopologyOptions & MulticodecOptions} props
1710
*/
1811
constructor ({
1912
min,
@@ -143,5 +136,10 @@ class MulticodecTopology extends Topology {
143136
* @typedef {import('peer-id')} PeerId
144137
* @typedef {import('multiaddr')} Multiaddr
145138
* @typedef {import('../connection')} Connection
139+
* @typedef {import('.').Options} TopologyOptions
140+
* @typedef {Object} MulticodecOptions
141+
* @property {string[]} multicodecs - protocol multicodecs
142+
* @property {Required<Handlers>} handlers
143+
* @typedef {import('.').Handlers} Handlers
146144
*/
147145
module.exports = MulticodecTopology

tsconfig.json

+13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@
66
// Tells TypeScript to read JS files, as
77
// normally they are ignored as source files
88
"allowJs": true,
9+
"forceConsistentCasingInFileNames": true,
10+
"noImplicitReturns": false,
11+
"noImplicitAny": false,
12+
"noImplicitThis": true,
13+
"noFallthroughCasesInSwitch": true,
14+
"noUnusedLocals": true,
15+
"noUnusedParameters": false,
16+
"strictFunctionTypes": true,
17+
"strictNullChecks": true,
18+
"strictPropertyInitialization": true,
19+
"strictBindCallApply": true,
920
"strict": true,
21+
"alwaysStrict": true,
22+
"stripInternal": true,
1023
// Generate d.ts files
1124
"declaration": true,
1225
// This compiler run should

0 commit comments

Comments
 (0)