fix: type incompatibilities (#75)

This commit is contained in:
Irakli Gozalishvili 2020-12-02 01:33:13 -08:00 committed by GitHub
parent bac57b05dc
commit 67a5f51805
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 180 additions and 94 deletions

27
.github/workflows/typecheck.yml vendored Normal file
View File

@ -0,0 +1,27 @@
on:
push:
branches:
- master
- main
- default
pull_request:
branches:
- '**'
name: Typecheck
jobs:
check:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
- name: Typecheck
uses: gozala/typescript-error-reporter-action@v1.0.8

View File

@ -9,6 +9,18 @@
"types",
"dist"
],
"types": "dist/src/index.d.ts",
"typesVersions": {
"*": {
"src/*": [
"dist/src/*",
"dist/src/*/index"
]
}
},
"eslintConfig": {
"extends": "ipfs"
},
"scripts": {
"lint": "aegir lint",
"build": "aegir build",

View File

@ -1,10 +1,9 @@
'use strict'
/* eslint-disable valid-jsdoc */
const PeerId = require('peer-id')
const multiaddr = require('multiaddr')
const errCode = require('err-code')
const Status = require('./status')
const { OPEN, CLOSING, CLOSED } = require('./status')
const connectionSymbol = Symbol.for('@libp2p/interface-connection/connection')
@ -51,7 +50,7 @@ class Connection {
/**
* Connection identifier.
*/
this.id = (parseInt(Math.random() * 1e9)).toString(36) + Date.now()
this.id = (parseInt(String(Math.random() * 1e9))).toString(36) + Date.now()
/**
* Observed multiaddr of the local peer
@ -75,10 +74,12 @@ class Connection {
/**
* Connection metadata.
*
* @type {Stat & {status: Status}}
*/
this._stat = {
...stat,
status: Status.OPEN
status: OPEN
}
/**
@ -152,11 +153,11 @@ class Connection {
* @returns {Promise<{stream: MuxedStream, protocol: string}>} with muxed+multistream-selected stream and selected protocol
*/
async newStream (protocols) {
if (this.stat.status === Status.CLOSING) {
if (this.stat.status === CLOSING) {
throw errCode(new Error('the connection is being closed'), 'ERR_CONNECTION_BEING_CLOSED')
}
if (this.stat.status === Status.CLOSED) {
if (this.stat.status === CLOSED) {
throw errCode(new Error('the connection is closed'), 'ERR_CONNECTION_CLOSED')
}
@ -178,7 +179,7 @@ class Connection {
* @param {MuxedStream} muxedStream - a muxed stream
* @param {object} properties - the stream properties to be registered
* @param {string} properties.protocol - the protocol used by the stream
* @param {object} properties.metadata - metadata of the stream
* @param {object} [properties.metadata] - metadata of the stream
* @returns {void}
*/
addStream (muxedStream, { protocol, metadata = {} }) {
@ -204,7 +205,7 @@ class Connection {
* @returns {Promise<void>}
*/
async close () {
if (this.stat.status === Status.CLOSED) {
if (this.stat.status === CLOSED) {
return
}
@ -212,16 +213,31 @@ class Connection {
return this._closing
}
this.stat.status = Status.CLOSING
this.stat.status = CLOSING
// Close raw connection
this._closing = await this._close()
this._stat.timeline.close = Date.now()
this.stat.status = Status.CLOSED
this.stat.status = CLOSED
}
}
/**
* @typedef {Object} Stat
* @property {string} direction - connection establishment direction ("inbound" or "outbound").
* @property {Timeline} timeline - connection relevant events timestamp.
* @property {string} [multiplexer] - connection multiplexing identifier.
* @property {string} [encryption] - connection encryption method identifier.
*
* @typedef {Object} Timeline
* @property {number} open - connection opening timestamp.
* @property {number} [upgraded] - connection upgraded timestamp.
* @property {number} [close]
*
* @typedef {import('./status').Status} Status
*/
module.exports = Connection
function validateArgs (localAddr, localPeer, remotePeer, newStream, close, getStreams, stat) {

View File

@ -1,7 +1,3 @@
'use strict'
/**
* @module connection/index
* @type {typeof import('./connection')}
*/
exports.Connection = require('./connection')

View File

@ -1,7 +1,12 @@
'use strict'
module.exports = {
const STATUS = {
OPEN: /** @type {'open'} */('open'),
CLOSING: /** @type {'closing'} */('closing'),
CLOSED: /** @type {'closed'} */('closed')
}
module.exports = STATUS
/**
* @typedef {STATUS[keyof STATUS]} Status
*/

View File

@ -74,6 +74,7 @@ module.exports = (test) => {
let timelineProxy
const proxyHandler = {
set () {
// @ts-ignore - TS fails to infer here
return Reflect.set(...arguments)
}
}
@ -138,7 +139,9 @@ module.exports = (test) => {
expect(connection.stat.timeline.close).to.not.exist()
await connection.close()
// @ts-ignore - fails to infer callCount
expect(proxyHandler.set.callCount).to.equal(1)
// @ts-ignore - fails to infer getCall
const [obj, key, value] = proxyHandler.set.getCall(0).args
expect(obj).to.eql(connection.stat.timeline)
expect(key).to.equal('close')

View File

@ -6,7 +6,7 @@ const expect = chai.expect
chai.use(require('dirty-chai'))
const duplexPair = require('it-pair/duplex')
const pipe = require('it-pipe')
const { pipe } = require('it-pipe')
const PeerId = require('peer-id')
const { collect } = require('streaming-iterables')
const uint8arrayFromString = require('uint8arrays/from-string')

View File

@ -1,11 +1,10 @@
'use strict'
/* eslint-disable valid-jsdoc */
const debug = require('debug')
const EventEmitter = require('events')
const { EventEmitter } = require('events')
const errcode = require('err-code')
const pipe = require('it-pipe')
const { pipe } = require('it-pipe')
const MulticodecTopology = require('../topology/multicodec-topology')
const { codes } = require('./errors')
@ -46,7 +45,7 @@ class PubsubBaseProtocol extends EventEmitter {
* @param {string} props.debugName - log namespace
* @param {Array<string>|string} props.multicodecs - protocol identificers to connect
* @param {Libp2p} props.libp2p
* @param {SignaturePolicy} [props.globalSignaturePolicy = SignaturePolicy.StrictSign] - defines how signatures should be handled
* @param {SignaturePolicyType} [props.globalSignaturePolicy = SignaturePolicy.StrictSign] - defines how signatures should be handled
* @param {boolean} [props.canRelayMessage = false] - if can relay messages not subscribed
* @param {boolean} [props.emitSelf = false] - if publish should emit to self, if subscribed
* @abstract
@ -226,6 +225,7 @@ class PubsubBaseProtocol extends EventEmitter {
const peer = this._addPeer(peerId, protocol)
peer.attachInboundStream(stream)
// @ts-ignore - peer.inboundStream maybe null
this._processMessages(idB58Str, peer.inboundStream, peer)
}
@ -243,6 +243,7 @@ class PubsubBaseProtocol extends EventEmitter {
try {
const { stream, protocol } = await conn.newStream(this.multicodecs)
const peer = this._addPeer(peerId, protocol)
// @ts-ignore MuxedStream is not DuplexIterableStream
await peer.attachOutboundStream(stream)
} catch (err) {
this.log.err(err)
@ -257,7 +258,7 @@ class PubsubBaseProtocol extends EventEmitter {
*
* @private
* @param {PeerId} peerId - peerId
* @param {Error} err - error for connection end
* @param {Error} [err] - error for connection end
*/
_onPeerDisconnected (peerId, err) {
const idB58Str = peerId.toB58String()
@ -341,6 +342,7 @@ class PubsubBaseProtocol extends EventEmitter {
await pipe(
stream,
async (source) => {
// @ts-ignore - DuplexIterableStream isn't defined as iterable
for await (const data of source) {
const rpcBytes = data instanceof Uint8Array ? data : data.slice()
const rpcMsg = this._decodeRpc(rpcBytes)
@ -395,7 +397,7 @@ class PubsubBaseProtocol extends EventEmitter {
* Handles a subscription change from a peer
*
* @param {string} id
* @param {RPC.SubOpt} subOpt
* @param {RPCSubOpts} subOpt
*/
_processRpcSubOpt (id, subOpt) {
const t = subOpt.topicID
@ -457,7 +459,7 @@ class PubsubBaseProtocol extends EventEmitter {
* The default msgID implementation
* Child class can override this.
*
* @param {RPC.Message} msg - the message object
* @param {RPCMessage} msg - the message object
* @returns {Uint8Array} message id as bytes
*/
getMsgId (msg) {
@ -590,8 +592,8 @@ class PubsubBaseProtocol extends EventEmitter {
* Should be used by the routers to create the message to send.
*
* @private
* @param {Message} message
* @returns {Promise<Message>}
* @param {RPCMessage} message
* @returns {Promise<RPCMessage>}
*/
_buildMessage (message) {
const signaturePolicy = this.globalSignaturePolicy
@ -728,6 +730,16 @@ class PubsubBaseProtocol extends EventEmitter {
}
}
/**
* @typedef {any} Libp2p
* @typedef {import('./peer-streams').DuplexIterableStream} DuplexIterableStream
* @typedef {import('../connection/connection')} Connection
* @typedef {import('./message').RPC} RPC
* @typedef {import('./message').SubOpts} RPCSubOpts
* @typedef {import('./message').Message} RPCMessage
* @typedef {import('./signature-policy').SignaturePolicyType} SignaturePolicyType
*/
module.exports = PubsubBaseProtocol
module.exports.message = message
module.exports.utils = utils

View File

@ -40,6 +40,7 @@ async function verifySignature (message) {
const baseMessage = { ...message }
delete baseMessage.signature
delete baseMessage.key
// @ts-ignore - from is optional
baseMessage.from = PeerId.createFromCID(baseMessage.from).toBytes()
const bytes = uint8ArrayConcat([
SignPrefix,
@ -50,6 +51,7 @@ async function verifySignature (message) {
const pubKey = await messagePublicKey(message)
// verify the base message
// @ts-ignore - may not have signature
return pubKey.verify(bytes, message.signature)
}
@ -62,6 +64,7 @@ async function verifySignature (message) {
*/
async function messagePublicKey (message) {
// should be available in the from property of the message (peer id)
// @ts-ignore - from is optional
const from = PeerId.createFromCID(message.from)
if (message.key) {
@ -78,6 +81,11 @@ async function messagePublicKey (message) {
}
}
/**
* @typedef {import('..').InMessage} InMessage
* @typedef {import('libp2p-crypto').PublicKey} PublicKey
*/
module.exports = {
messagePublicKey,
signMessage,

View File

@ -1,12 +1,15 @@
'use strict'
const EventEmitter = require('events')
const { EventEmitter } = require('events')
const lp = require('it-length-prefixed')
/** @type {typeof import('it-pushable').default} */
// @ts-ignore
const pushable = require('it-pushable')
const pipe = require('it-pipe')
const abortable = require('abortable-iterator')
const AbortController = require('abort-controller')
const { pipe } = require('it-pipe')
const { source: abortable } = require('abortable-iterator')
const AbortController = require('abort-controller').default
const debug = require('debug')
const log = debug('libp2p-pubsub:peer-streams')
@ -19,7 +22,7 @@ log.error = debug('libp2p-pubsub:peer-streams:error')
*
* @typedef {object} DuplexIterableStream
* @property {Sink} sink
* @property {() AsyncIterator<Uint8Array>} source
* @property {AsyncIterator<Uint8Array>} source
*
* @typedef PeerId
* @type import('peer-id')
@ -51,33 +54,33 @@ class PeerStreams extends EventEmitter {
* The raw outbound stream, as retrieved from conn.newStream
*
* @private
* @type {DuplexIterableStream}
* @type {null|DuplexIterableStream}
*/
this._rawOutboundStream = null
/**
* The raw inbound stream, as retrieved from the callback from libp2p.handle
*
* @private
* @type {DuplexIterableStream}
* @type {null|DuplexIterableStream}
*/
this._rawInboundStream = null
/**
* An AbortController for controlled shutdown of the inbound stream
*
* @private
* @type {typeof AbortController}
* @type {null|AbortController}
*/
this._inboundAbortController = null
/**
* Write stream -- its preferable to use the write method
*
* @type {import('it-pushable').Pushable<Uint8Array>>}
* @type {null|import('it-pushable').Pushable<Uint8Array>}
*/
this.outboundStream = null
/**
* Read stream
*
* @type {DuplexIterableStream}
* @type {null|DuplexIterableStream}
*/
this.inboundStream = null
}
@ -113,6 +116,7 @@ class PeerStreams extends EventEmitter {
throw new Error('No writable connection to ' + id)
}
// @ts-ignore - this.outboundStream could be null
this.outboundStream.push(data)
}
@ -129,11 +133,13 @@ class PeerStreams extends EventEmitter {
// - transformed with length-prefix transform
this._inboundAbortController = new AbortController()
this._rawInboundStream = stream
// @ts-ignore - abortable returns AsyncIterable and not a DuplexIterableStream
this.inboundStream = abortable(
pipe(
this._rawInboundStream,
lp.decode()
),
// @ts-ignore - possibly null
this._inboundAbortController.signal,
{ returnOnAbort: true }
)
@ -144,7 +150,7 @@ class PeerStreams extends EventEmitter {
/**
* Attach a raw outbound stream and setup a write stream
*
* @param {Stream} stream
* @param {DuplexIterableStream} stream
* @returns {Promise<void>}
*/
async attachOutboundStream (stream) {
@ -153,6 +159,7 @@ class PeerStreams extends EventEmitter {
const _prevStream = this.outboundStream
if (_prevStream) {
// End the stream without emitting a close event
// @ts-ignore - outboundStream may be null
await this.outboundStream.end(false)
}
@ -160,9 +167,12 @@ class PeerStreams extends EventEmitter {
this.outboundStream = pushable({
onEnd: (shouldEmit) => {
// close writable side of the stream
this._rawOutboundStream.reset && this._rawOutboundStream.reset()
// @ts-ignore - DuplexIterableStream does not define reset
this._rawOutboundStream && this._rawOutboundStream.reset && this._rawOutboundStream.reset()
this._rawOutboundStream = null
this.outboundStream = null
// @ts-ignore - shouldEmit is `Error | undefined` so condition is
// always false
if (shouldEmit !== false) {
this.emit('close')
}
@ -195,6 +205,7 @@ class PeerStreams extends EventEmitter {
}
// End the inbound stream
if (this.inboundStream) {
// @ts-ignore - possibly null
this._inboundAbortController.abort()
}

View File

@ -4,7 +4,7 @@
* Enum for Signature Policy
* Details how message signatures are produced/consumed
*/
exports.SignaturePolicy = {
const SignaturePolicy = {
/**
* On the producing side:
* * Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
@ -24,5 +24,10 @@ exports.SignaturePolicy = {
* * Propagate only if the fields are absent, reject otherwise.
* * A message_id function will not be able to use the above fields, and should instead rely on the data field. A commonplace strategy is to calculate a hash.
*/
StrictNoSign: /** @type {'StrictNoSign'} */ 'StrictNoSign'
StrictNoSign: /** @type {'StrictNoSign'} */ ('StrictNoSign')
}
exports.SignaturePolicy = SignaturePolicy
/**
* @typedef {SignaturePolicy[keyof SignaturePolicy]} SignaturePolicyType
*/

View File

@ -62,7 +62,7 @@ module.exports = (common) => {
pubsub.publish(topic, data)
// Wait 1 second to guarantee that self is not noticed
return new Promise((resolve) => setTimeout(() => resolve(), 1000))
return new Promise((resolve) => setTimeout(resolve, 1000))
})
})
})

View File

@ -50,7 +50,10 @@ module.exports = (common) => {
sinon.spy(pubsub, '_publish')
sinon.spy(pubsub, 'validate')
const peerStream = new PeerStreams({ id: await PeerId.create() })
const peerStream = new PeerStreams({
id: await PeerId.create(),
protocol: 'test'
})
const rpc = {
subscriptions: [],
msgs: [{
@ -82,7 +85,11 @@ module.exports = (common) => {
sinon.spy(pubsub, '_publish')
sinon.spy(pubsub, 'validate')
const peerStream = new PeerStreams({ id: await PeerId.create() })
const peerStream = new PeerStreams({
id: await PeerId.create(),
protocol: 'test'
})
const rpc = {
subscriptions: [],
msgs: [{

View File

@ -113,9 +113,9 @@ module.exports = (common) => {
// await subscription change
await Promise.all([
new Promise(resolve => psA.once('pubsub:subscription-change', () => resolve())),
new Promise(resolve => psB.once('pubsub:subscription-change', () => resolve())),
new Promise(resolve => psC.once('pubsub:subscription-change', () => resolve()))
new Promise(resolve => psA.once('pubsub:subscription-change', () => resolve(null))),
new Promise(resolve => psB.once('pubsub:subscription-change', () => resolve(null))),
new Promise(resolve => psC.once('pubsub:subscription-change', () => resolve(null)))
])
// await a cycle
@ -166,9 +166,9 @@ module.exports = (common) => {
// await subscription change
await Promise.all([
new Promise(resolve => psA.once('pubsub:subscription-change', () => resolve())),
new Promise(resolve => psB.once('pubsub:subscription-change', () => resolve())),
new Promise(resolve => psC.once('pubsub:subscription-change', () => resolve()))
new Promise(resolve => psA.once('pubsub:subscription-change', () => resolve(null))),
new Promise(resolve => psB.once('pubsub:subscription-change', () => resolve(null))),
new Promise(resolve => psC.once('pubsub:subscription-change', () => resolve(null)))
])
psA.on(topic, incMsg)

View File

@ -1,5 +1,4 @@
'use strict'
/* eslint-disable valid-jsdoc */
const randomBytes = require('libp2p-crypto/src/random-bytes')
const uint8ArrayToString = require('uint8arrays/to-string')
@ -88,8 +87,8 @@ exports.ensureArray = (maybeArray) => {
/**
* Ensures `message.from` is base58 encoded
*
* @template {Object} T
* @param {T} message
* @template {{from?:any}} T
* @param {T & {from?:string, receivedFrom:string}} message
* @param {string} [peerId]
* @returns {T & {from?: string, peerId?: string }}
*/
@ -105,7 +104,7 @@ exports.normalizeInRpcMessage = (message, peerId) => {
}
/**
* @template {Object} T
* @template {{from?:any, data?:any}} T
*
* @param {T} message
* @returns {T & {from?: Uint8Array, data?: Uint8Array}}

View File

@ -5,7 +5,7 @@ const chai = require('chai')
chai.use(require('chai-checkmark'))
const { expect } = chai
const pair = require('it-pair/duplex')
const pipe = require('it-pipe')
const { pipe } = require('it-pipe')
const { collect, map, consume } = require('streaming-iterables')
function close (stream) {

View File

@ -3,12 +3,12 @@
'use strict'
const pair = require('it-pair/duplex')
const pipe = require('it-pipe')
const { pipe } = require('it-pipe')
const { consume } = require('streaming-iterables')
const Tcp = require('libp2p-tcp')
const multiaddr = require('multiaddr')
const abortable = require('abortable-iterator')
const AbortController = require('abort-controller')
const { source: abortable } = require('abortable-iterator')
const AbortController = require('abort-controller').default
const uint8arrayFromString = require('uint8arrays/from-string')
const mh = multiaddr('/ip4/127.0.0.1/tcp/0')

View File

@ -2,7 +2,10 @@
const { expect } = require('chai')
const pair = require('it-pair/duplex')
const pipe = require('it-pipe')
const { pipe } = require('it-pipe')
/** @type {typeof import('p-limit').default} */
// @ts-ignore - wrong type defs
const pLimit = require('p-limit')
const { collect, tap, consume } = require('streaming-iterables')
@ -61,8 +64,11 @@ module.exports = async (Muxer, nStreams, nMsg, limit) => {
}
function marker (n) {
/** @type {Function} */
let check
let i = 0
/** @type {Promise<void>} */
const done = new Promise((resolve, reject) => {
check = err => {
i++
@ -78,5 +84,7 @@ function marker (n) {
}
}
})
// @ts-ignore - TS can't see that assignement occured
return { check, done }
}

View File

@ -1,5 +1,4 @@
'use strict'
/* eslint-disable valid-jsdoc */
const noop = () => {}
const topologySymbol = Symbol.for('@libp2p/js-interfaces/topology')
@ -15,8 +14,10 @@ const topologySymbol = Symbol.for('@libp2p/js-interfaces/topology')
* @property {Handlers} [handlers]
*
* @typedef {Object} Handlers
* @property {(peerId: PeerId, conn: import('../connection')) => void} [onConnect] - protocol "onConnect" handler
* @property {(peerId: PeerId) => void} [onDisconnect] - protocol "onDisconnect" handler
* @property {(peerId: PeerId, conn: Connection) => void} [onConnect] - protocol "onConnect" handler
* @property {(peerId: PeerId, error?:Error) => void} [onDisconnect] - protocol "onDisconnect" handler
*
* @typedef {import('../connection/connection')} Connection
*/
class Topology {

View File

@ -1,5 +1,4 @@
'use strict'
/* eslint-disable valid-jsdoc */
const Topology = require('./index')
const multicodecTopologySymbol = Symbol.for('@libp2p/js-interfaces/topology/multicodec-topology')
@ -138,7 +137,7 @@ class MulticodecTopology extends Topology {
/**
* @typedef {import('peer-id')} PeerId
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('../connection')} Connection
* @typedef {import('../connection/connection')} Connection
* @typedef {import('.').Options} TopologyOptions
* @typedef {Object} MulticodecOptions
* @property {string[]} multicodecs - protocol multicodecs

View File

@ -9,8 +9,8 @@ chai.use(dirtyChai)
const { isValidTick } = require('./utils')
const goodbye = require('it-goodbye')
const { collect } = require('streaming-iterables')
const pipe = require('it-pipe')
const AbortController = require('abort-controller')
const { pipe } = require('it-pipe')
const AbortController = require('abort-controller').default
const AbortError = require('../errors').AbortError
const sinon = require('sinon')

View File

@ -9,7 +9,7 @@ chai.use(dirtyChai)
const sinon = require('sinon')
const pWaitFor = require('p-wait-for')
const pipe = require('it-pipe')
const { pipe } = require('it-pipe')
const uint8arrayFromString = require('uint8arrays/from-string')
const { isValidTick } = require('./utils')

View File

@ -1,32 +1,9 @@
{
"include": ["src/**/*.js"],
"exclude": ["src/**/tests/*", "src/utils"],
"extends": "./node_modules/aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
// Tells TypeScript to read JS files, as
// normally they are ignored as source files
"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,
"alwaysStrict": true,
"stripInternal": true,
// Generate d.ts files
"declaration": true,
// This compiler run should
// only output d.ts files
"emitDeclarationOnly": true,
"esModuleInterop": true,
"rootDir": "./src",
"outDir": "./src"
}
"outDir": "dist"
},
"include": [
"src"
]
}