From b0db31907640403ac9e68ba6e425af91510aef97 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Mon, 29 Mar 2021 17:54:35 +0200 Subject: [PATCH] chore: add ts tests --- .github/workflows/examples.yml | 102 ++++++++++++ .github/workflows/main.yml | 93 +---------- package.json | 8 +- src/circuit/circuit/stop.js | 3 +- src/types.d.ts | 230 ++++++++------------------- test/nat-manager/nat-manager.node.js | 1 - test/pnet/index.spec.js | 2 +- test/ts-use/package.json | 24 +++ test/ts-use/src/main.ts | 192 ++++++++++++++++++++++ test/ts-use/tsconfig.json | 7 + 10 files changed, 403 insertions(+), 259 deletions(-) create mode 100644 .github/workflows/examples.yml create mode 100644 test/ts-use/package.json create mode 100644 test/ts-use/src/main.ts create mode 100644 test/ts-use/tsconfig.json diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml new file mode 100644 index 0000000000..b2b0aa37f6 --- /dev/null +++ b/.github/workflows/examples.yml @@ -0,0 +1,102 @@ +name: examples +on: + push: + branches: + - master + pull_request: + branches: + - '**' + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: npx aegir lint + - run: npx aegir ts -p check + - run: npx aegir build + test-auto-relay-example: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: cd examples && yarn && npm run test -- auto-relay + test-chat-example: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: cd examples && yarn && npm run test -- chat + test-connection-encryption-example: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: cd examples && yarn && npm run test -- connection-encryption + test-discovery-mechanisms-example: + needs: check + runs-on: macos-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: cd examples && yarn && npm run test -- discovery-mechanisms + test-echo-example: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: cd examples && yarn && npm run test -- echo + test-libp2p-in-the-browser-example: + needs: check + runs-on: macos-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: cd examples && yarn && npm run test -- libp2p-in-the-browser + test-peer-and-content-routing-example: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: cd examples && yarn && npm run test -- peer-and-content-routing + test-pnet-example: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: cd examples && yarn && npm run test -- pnet + test-protocol-and-stream-muxing-example: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: cd examples && yarn && npm run test -- protocol-and-stream-muxing + test-pubsub-example: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: cd examples && yarn && npm run test -- pubsub + test-transports-example: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: cd examples && yarn && npm run test -- transports + test-webrtc-direct-example: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: npm install + - run: cd examples && yarn && npm run test -- webrtc-direct \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 033cdc9e24..3d9b5969e4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,10 +13,10 @@ jobs: steps: - uses: actions/checkout@v2 - run: npm install - - run: npm run lint - - uses: gozala/typescript-error-reporter-action@v1.0.8 - - run: npm run build - - run: yarn aegir dep-check + - run: npx aegir lint + - run: npx aegir ts -p check + - run: npx aegir build + - run: npx aegir dep-check - uses: ipfs/aegir/actions/bundle-size@master name: size with: @@ -51,94 +51,17 @@ jobs: - uses: actions/checkout@v2 - run: npm install - run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless - test-interop: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: cd node_modules/interop-libp2p && yarn && LIBP2P_JS=${GITHUB_WORKSPACE}/src/index.js npx aegir test -t node --bail - test-auto-relay-example: + test-ts: needs: check runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- auto-relay - test-chat-example: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: cd examples && yarn && npm run test -- chat - test-connection-encryption-example: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: cd examples && yarn && npm run test -- connection-encryption - test-discovery-mechanisms-example: - needs: check - runs-on: macos-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: cd examples && yarn && npm run test -- discovery-mechanisms - test-echo-example: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: cd examples && yarn && npm run test -- echo - test-libp2p-in-the-browser-example: - needs: check - runs-on: macos-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: cd examples && yarn && npm run test -- libp2p-in-the-browser - test-peer-and-content-routing-example: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: cd examples && yarn && npm run test -- peer-and-content-routing - test-pnet-example: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: cd examples && yarn && npm run test -- pnet - test-protocol-and-stream-muxing-example: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: cd examples && yarn && npm run test -- protocol-and-stream-muxing - test-pubsub-example: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: cd examples && yarn && npm run test -- pubsub - test-transports-example: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: cd examples && yarn && npm run test -- transports - test-webrtc-direct-example: + - run: npm run test:ts + test-interop: needs: check runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm install - - run: cd examples && yarn && npm run test -- webrtc-direct + - run: cd node_modules/interop-libp2p && yarn && LIBP2P_JS=${GITHUB_WORKSPACE}/src/index.js npx aegir test -t node --bail diff --git a/package.json b/package.json index 072e1a4967..3ce5228c2b 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "prepare": "aegir build --no-bundle", "lint": "aegir lint", "build": "aegir build", - "test": "npm run test:node && npm run test:browser", + "test": "aegir test", + "test:ts": "aegir build --no-bundle && npm run test --prefix test/ts-use", "test:node": "aegir test -t node -f \"./test/**/*.{node,spec}.js\"", "test:browser": "aegir test -t browser", "test:examples": "cd examples && npm run test:all", @@ -57,12 +58,12 @@ "eslintConfig": { "extends": "ipfs", "ignorePatterns": [ - "!.aegir.js" + "!.aegir.js", + "test/ts-use" ] }, "dependencies": { "@motrix/nat-api": "^0.3.1", - "@types/varint": "^6.0.0", "abort-controller": "^3.0.0", "aggregate-error": "^3.1.0", "any-signal": "^2.1.1", @@ -119,6 +120,7 @@ "@nodeutils/defaults-deep": "^1.1.0", "@types/es6-promisify": "^6.0.0", "@types/node-forge": "^0.9.7", + "@types/varint": "^6.0.0", "abortable-iterator": "^3.0.0", "aegir": "^32.1.0", "chai-bytes": "^0.1.2", diff --git a/src/circuit/circuit/stop.js b/src/circuit/circuit/stop.js index a7af0eaad0..00c4d3c517 100644 --- a/src/circuit/circuit/stop.js +++ b/src/circuit/circuit/stop.js @@ -14,7 +14,6 @@ const { validateAddrs } = require('./utils') * @typedef {import('libp2p-interfaces/src/connection').Connection} Connection * @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream * @typedef {import('../../types').CircuitRequest} CircuitRequest - * @typedef {import('./stream-handler')} StreamHandlerT */ /** @@ -24,7 +23,7 @@ const { validateAddrs } = require('./utils') * @param {Object} options * @param {Connection} options.connection * @param {CircuitRequest} options.request - The CircuitRelay protobuf request (unencoded) - * @param {StreamHandlerT} options.streamHandler + * @param {StreamHandler} options.streamHandler * @returns {Promise|void} Resolves a duplex iterable */ module.exports.handleStop = function handleStop ({ diff --git a/src/types.d.ts b/src/types.d.ts index 0ba21da15f..4ce294aa50 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,4 +1,3 @@ - // Insecure Message types export enum KeyType { RSA = 0, @@ -8,202 +7,99 @@ export enum KeyType { } // Protobufs -export type MessageProto = { - encode(value: any): Uint8Array - decode(bytes: Uint8Array): any +export interface MessageProto { + encode: (value: any) => Uint8Array + decode: (bytes: Uint8Array) => any } -export type SUCCESS = 100; -export type HOP_SRC_ADDR_TOO_LONG = 220; -export type HOP_DST_ADDR_TOO_LONG = 221; -export type HOP_SRC_MULTIADDR_INVALID = 250; -export type HOP_DST_MULTIADDR_INVALID = 251; -export type HOP_NO_CONN_TO_DST = 260; -export type HOP_CANT_DIAL_DST = 261; -export type HOP_CANT_OPEN_DST_STREAM = 262; -export type HOP_CANT_SPEAK_RELAY = 270; -export type HOP_CANT_RELAY_TO_SELF = 280; -export type STOP_SRC_ADDR_TOO_LONG = 320; -export type STOP_DST_ADDR_TOO_LONG = 321; -export type STOP_SRC_MULTIADDR_INVALID = 350; -export type STOP_DST_MULTIADDR_INVALID = 351; -export type STOP_RELAY_REFUSED = 390; -export type MALFORMED_MESSAGE = 400; +export type SUCCESS = 100 +export type HOP_SRC_ADDR_TOO_LONG = 220 +export type HOP_DST_ADDR_TOO_LONG = 221 +export type HOP_SRC_MULTIADDR_INVALID = 250 +export type HOP_DST_MULTIADDR_INVALID = 251 +export type HOP_NO_CONN_TO_DST = 260 +export type HOP_CANT_DIAL_DST = 261 +export type HOP_CANT_OPEN_DST_STREAM = 262 +export type HOP_CANT_SPEAK_RELAY = 270 +export type HOP_CANT_RELAY_TO_SELF = 280 +export type STOP_SRC_ADDR_TOO_LONG = 320 +export type STOP_DST_ADDR_TOO_LONG = 321 +export type STOP_SRC_MULTIADDR_INVALID = 350 +export type STOP_DST_MULTIADDR_INVALID = 351 +export type STOP_RELAY_REFUSED = 390 +export type MALFORMED_MESSAGE = 400 export type CircuitStatus = SUCCESS | HOP_SRC_ADDR_TOO_LONG | HOP_DST_ADDR_TOO_LONG - | HOP_SRC_MULTIADDR_INVALID | HOP_DST_MULTIADDR_INVALID | HOP_NO_CONN_TO_DST - | HOP_CANT_DIAL_DST | HOP_CANT_OPEN_DST_STREAM | HOP_CANT_SPEAK_RELAY | HOP_CANT_RELAY_TO_SELF - | STOP_SRC_ADDR_TOO_LONG | STOP_DST_ADDR_TOO_LONG | STOP_SRC_MULTIADDR_INVALID - | STOP_DST_MULTIADDR_INVALID | STOP_RELAY_REFUSED | MALFORMED_MESSAGE +| HOP_SRC_MULTIADDR_INVALID | HOP_DST_MULTIADDR_INVALID | HOP_NO_CONN_TO_DST +| HOP_CANT_DIAL_DST | HOP_CANT_OPEN_DST_STREAM | HOP_CANT_SPEAK_RELAY | HOP_CANT_RELAY_TO_SELF +| STOP_SRC_ADDR_TOO_LONG | STOP_DST_ADDR_TOO_LONG | STOP_SRC_MULTIADDR_INVALID +| STOP_DST_MULTIADDR_INVALID | STOP_RELAY_REFUSED | MALFORMED_MESSAGE -export type HOP = 1; -export type STOP = 2; -export type STATUS = 3; -export type CAN_HOP = 4; +export type HOP = 1 +export type STOP = 2 +export type STATUS = 3 +export type CAN_HOP = 4 export type CircuitType = HOP | STOP | STATUS | CAN_HOP -export type CircuitPeer = { +export interface CircuitPeer { id: Uint8Array addrs: Uint8Array[] } -export type CircuitRequest = { +export interface CircuitRequest { type?: CircuitType code?: CircuitStatus dstPeer?: CircuitPeer srcPeer?: CircuitPeer } -export type CircuitMessageProto = { - encode(value: CircuitRequest): Uint8Array - decode(bytes: Uint8Array): any +export interface CircuitMessageProto { + encode: (value: CircuitRequest) => Uint8Array + decode: (bytes: Uint8Array) => any status: number type: number Status: { - SUCCESS: SUCCESS, - HOP_SRC_ADDR_TOO_LONG: HOP_SRC_ADDR_TOO_LONG, - HOP_DST_ADDR_TOO_LONG: HOP_DST_ADDR_TOO_LONG, - HOP_SRC_MULTIADDR_INVALID: HOP_SRC_MULTIADDR_INVALID, - HOP_DST_MULTIADDR_INVALID: HOP_DST_MULTIADDR_INVALID, - HOP_NO_CONN_TO_DST: HOP_NO_CONN_TO_DST, - HOP_CANT_DIAL_DST: HOP_CANT_DIAL_DST, - HOP_CANT_OPEN_DST_STREAM: HOP_CANT_OPEN_DST_STREAM, - HOP_CANT_SPEAK_RELAY: HOP_CANT_SPEAK_RELAY, - HOP_CANT_RELAY_TO_SELF: HOP_CANT_RELAY_TO_SELF, - STOP_SRC_ADDR_TOO_LONG: STOP_SRC_ADDR_TOO_LONG, - STOP_DST_ADDR_TOO_LONG: STOP_DST_ADDR_TOO_LONG, - STOP_SRC_MULTIADDR_INVALID: STOP_SRC_MULTIADDR_INVALID, - STOP_DST_MULTIADDR_INVALID: STOP_DST_MULTIADDR_INVALID, - STOP_RELAY_REFUSED: STOP_RELAY_REFUSED, + SUCCESS: SUCCESS + HOP_SRC_ADDR_TOO_LONG: HOP_SRC_ADDR_TOO_LONG + HOP_DST_ADDR_TOO_LONG: HOP_DST_ADDR_TOO_LONG + HOP_SRC_MULTIADDR_INVALID: HOP_SRC_MULTIADDR_INVALID + HOP_DST_MULTIADDR_INVALID: HOP_DST_MULTIADDR_INVALID + HOP_NO_CONN_TO_DST: HOP_NO_CONN_TO_DST + HOP_CANT_DIAL_DST: HOP_CANT_DIAL_DST + HOP_CANT_OPEN_DST_STREAM: HOP_CANT_OPEN_DST_STREAM + HOP_CANT_SPEAK_RELAY: HOP_CANT_SPEAK_RELAY + HOP_CANT_RELAY_TO_SELF: HOP_CANT_RELAY_TO_SELF + STOP_SRC_ADDR_TOO_LONG: STOP_SRC_ADDR_TOO_LONG + STOP_DST_ADDR_TOO_LONG: STOP_DST_ADDR_TOO_LONG + STOP_SRC_MULTIADDR_INVALID: STOP_SRC_MULTIADDR_INVALID + STOP_DST_MULTIADDR_INVALID: STOP_DST_MULTIADDR_INVALID + STOP_RELAY_REFUSED: STOP_RELAY_REFUSED MALFORMED_MESSAGE: MALFORMED_MESSAGE } Type: { - HOP: HOP, - STOP: STOP, - STATUS: STATUS, + HOP: HOP + STOP: STOP + STATUS: STATUS CAN_HOP: CAN_HOP } } export interface EventEmitterFactory { - new(): EventEmitter; + new(): EventEmitter } export interface EventEmitter { - addListener(event: string | symbol, listener: (...args: any[]) => void): EventEmitter; - on(event: string | symbol, listener: (...args: any[]) => void): EventEmitter; - once(event: string | symbol, listener: (...args: any[]) => void): EventEmitter; - removeListener(event: string | symbol, listener: (...args: any[]) => void): EventEmitter; - off(event: string | symbol, listener: (...args: any[]) => void): EventEmitter; - removeAllListeners(event?: string | symbol): EventEmitter; - setMaxListeners(n: number): EventEmitter; - getMaxListeners(): number; - listeners(event: string | symbol): Function[]; // eslint-disable-line @typescript-eslint/ban-types - rawListeners(event: string | symbol): Function[]; // eslint-disable-line @typescript-eslint/ban-types - emit(event: string | symbol, ...args: any[]): boolean; - listenerCount(event: string | symbol): number; + addListener: (event: string | symbol, listener: (...args: any[]) => void) => EventEmitter + on: (event: string | symbol, listener: (...args: any[]) => void) => EventEmitter + once: (event: string | symbol, listener: (...args: any[]) => void) => EventEmitter + removeListener: (event: string | symbol, listener: (...args: any[]) => void) => EventEmitter + off: (event: string | symbol, listener: (...args: any[]) => void) => EventEmitter + removeAllListeners: (event?: string | symbol) => EventEmitter + setMaxListeners: (n: number) => EventEmitter + getMaxListeners: () => number + listeners: (event: string | symbol) => Function[] // eslint-disable-line @typescript-eslint/ban-types + rawListeners: (event: string | symbol) => Function[] // eslint-disable-line @typescript-eslint/ban-types + emit(event: string | symbol, ...args: any[]): boolean // eslint-disable-line @typescript-eslint/method-signature-style + listenerCount: (event: string | symbol) => number } - -// // Insecure Message types -// export enum KeyType { -// RSA = 0, -// Ed25519 = 1, -// Secp256k1 = 2, -// ECDSA = 3 -// } - -// // Protobufs -// export interface MessageProto { -// encode: (value: any) => Uint8Array -// decode: (bytes: Uint8Array) => any -// } - -// export type SUCCESS = 100 -// export type HOP_SRC_ADDR_TOO_LONG = 220 -// export type HOP_DST_ADDR_TOO_LONG = 221 -// export type HOP_SRC_MULTIADDR_INVALID = 250 -// export type HOP_DST_MULTIADDR_INVALID = 251 -// export type HOP_NO_CONN_TO_DST = 260 -// export type HOP_CANT_DIAL_DST = 261 -// export type HOP_CANT_OPEN_DST_STREAM = 262 -// export type HOP_CANT_SPEAK_RELAY = 270 -// export type HOP_CANT_RELAY_TO_SELF = 280 -// export type STOP_SRC_ADDR_TOO_LONG = 320 -// export type STOP_DST_ADDR_TOO_LONG = 321 -// export type STOP_SRC_MULTIADDR_INVALID = 350 -// export type STOP_DST_MULTIADDR_INVALID = 351 -// export type STOP_RELAY_REFUSED = 390 -// export type MALFORMED_MESSAGE = 400 - -// export type CircuitStatus = SUCCESS | HOP_SRC_ADDR_TOO_LONG | HOP_DST_ADDR_TOO_LONG -// | HOP_SRC_MULTIADDR_INVALID | HOP_DST_MULTIADDR_INVALID | HOP_NO_CONN_TO_DST -// | HOP_CANT_DIAL_DST | HOP_CANT_OPEN_DST_STREAM | HOP_CANT_SPEAK_RELAY | HOP_CANT_RELAY_TO_SELF -// | STOP_SRC_ADDR_TOO_LONG | STOP_DST_ADDR_TOO_LONG | STOP_SRC_MULTIADDR_INVALID -// | STOP_DST_MULTIADDR_INVALID | STOP_RELAY_REFUSED | MALFORMED_MESSAGE - -// export type HOP = 1 -// export type STOP = 2 -// export type STATUS = 3 -// export type CAN_HOP = 4 - -// export type CircuitType = HOP | STOP | STATUS | CAN_HOP - -// export interface CircuitPeer { -// id: Uint8Array -// addrs: Uint8Array[] -// } - -// export interface CircuitRequest { -// type: CircuitType -// dstPeer: CircuitPeer -// srcPeer: CircuitPeer -// } - -// export interface CircuitMessageProto { -// encode: (value: any) => Uint8Array -// decode: (bytes: Uint8Array) => any -// status: { -// SUCCESS: SUCCESS -// HOP_SRC_ADDR_TOO_LONG: HOP_SRC_ADDR_TOO_LONG -// HOP_DST_ADDR_TOO_LONG: HOP_DST_ADDR_TOO_LONG -// HOP_SRC_MULTIADDR_INVALID: HOP_SRC_MULTIADDR_INVALID -// HOP_DST_MULTIADDR_INVALID: HOP_DST_MULTIADDR_INVALID -// HOP_NO_CONN_TO_DST: HOP_NO_CONN_TO_DST -// HOP_CANT_DIAL_DST: HOP_CANT_DIAL_DST -// HOP_CANT_OPEN_DST_STREAM: HOP_CANT_OPEN_DST_STREAM -// HOP_CANT_SPEAK_RELAY: HOP_CANT_SPEAK_RELAY -// HOP_CANT_RELAY_TO_SELF: HOP_CANT_RELAY_TO_SELF -// STOP_SRC_ADDR_TOO_LONG: STOP_SRC_ADDR_TOO_LONG -// STOP_DST_ADDR_TOO_LONG: STOP_DST_ADDR_TOO_LONG -// STOP_SRC_MULTIADDR_INVALID: STOP_SRC_MULTIADDR_INVALID -// STOP_DST_MULTIADDR_INVALID: STOP_DST_MULTIADDR_INVALID -// STOP_RELAY_REFUSED: STOP_RELAY_REFUSED -// MALFORMED_MESSAGE: MALFORMED_MESSAGE -// } -// type: { -// HOP: HOP -// STOP: STOP -// STATUS: STATUS -// CAN_HOP: CAN_HOP -// } -// } - -// export interface EventEmitterFactory { -// new(): EventEmitter -// } - -// export interface EventEmitter { -// addListener: (event: string | symbol, listener: (...args: any[]) => void) => EventEmitter -// on: (event: string | symbol, listener: (...args: any[]) => void) => EventEmitter -// once: (event: string | symbol, listener: (...args: any[]) => void) => EventEmitter -// removeListener: (event: string | symbol, listener: (...args: any[]) => void) => EventEmitter -// off: (event: string | symbol, listener: (...args: any[]) => void) => EventEmitter -// removeAllListeners: (event?: string | symbol) => EventEmitter -// setMaxListeners: (n: number) => EventEmitter -// getMaxListeners: () => number -// listeners: (event: string | symbol) => Function[] // eslint-disable-line @typescript-eslint/ban-types -// rawListeners: (event: string | symbol) => Function[] // eslint-disable-line @typescript-eslint/ban-types -// emit: (event: string | symbol, ...args: any[]) => boolean -// listenerCount: (event: string | symbol) => number -// } diff --git a/test/nat-manager/nat-manager.node.js b/test/nat-manager/nat-manager.node.js index 9333920ef7..5f5562e0b0 100644 --- a/test/nat-manager/nat-manager.node.js +++ b/test/nat-manager/nat-manager.node.js @@ -60,7 +60,6 @@ describe('Nat Manager (TCP)', () => { teardown.push(async () => { await natManager.stop() - // await transportManager.close() await transportManager.removeAll() expect(transportManager._transports.size).to.equal(0) }) diff --git a/test/pnet/index.spec.js b/test/pnet/index.spec.js index bdfcc15549..98327146d8 100644 --- a/test/pnet/index.spec.js +++ b/test/pnet/index.spec.js @@ -18,7 +18,7 @@ const wrongSwarmKeyBuffer = new Uint8Array(95) generate(swarmKeyBuffer) generate(wrongSwarmKeyBuffer) -describe.skip('private network', () => { +describe('private network', () => { it('should accept a valid psk buffer', () => { const protector = new Protector(swarmKeyBuffer) diff --git a/test/ts-use/package.json b/test/ts-use/package.json new file mode 100644 index 0000000000..c3a06e4a81 --- /dev/null +++ b/test/ts-use/package.json @@ -0,0 +1,24 @@ +{ + "name": "ts-use", + "private": true, + "dependencies": { + "datastore-level": "^4.0.0", + "ipfs-http-client": "^49.0.4", + "libp2p": "file:../..", + "libp2p-bootstrap": "^0.12.2", + "libp2p-delegated-content-routing": "^0.9.0", + "libp2p-delegated-peer-routing": "^0.8.2", + "libp2p-gossipsub": "^0.8.0", + "libp2p-interfaces": "^0.8.4", + "libp2p-kad-dht": "^0.21.0", + "libp2p-mplex": "^0.10.2", + "libp2p-noise": "^2.0.5", + "libp2p-tcp": "^0.15.3", + "libp2p-websockets": "^0.15.3", + "peer-id": "^0.14.3" + }, + "scripts": { + "build": "npx tsc", + "test": "npm install && npx -p typescript tsc --noEmit" + } +} diff --git a/test/ts-use/src/main.ts b/test/ts-use/src/main.ts new file mode 100644 index 0000000000..4dc4efe4cb --- /dev/null +++ b/test/ts-use/src/main.ts @@ -0,0 +1,192 @@ +import Libp2p = require('libp2p') + +const TCP = require('libp2p-tcp') +const WEBSOCKETS = require('libp2p-websockets') +const NOISE = require('libp2p-noise') +const MPLEX = require('libp2p-mplex') +const Gossipsub = require('libp2p-gossipsub') +const DHT = require('libp2p-kad-dht') + +const { dnsaddrResolver } = require('multiaddr/src/resolvers') +const { publicAddressesFirst } = require('libp2p-utils/src/address-sort') + +const { SignaturePolicy } = require('libp2p-interfaces/src/pubsub/signature-policy') +const { FaultTolerance } = require('libp2p/src/transport-manager') +const filters = require('libp2p-websockets/src/filters') + +const Bootstrap = require('libp2p-bootstrap') +const LevelStore = require('datastore-level') + +const ipfsHttpClient = require('ipfs-http-client') +const DelegatedPeerRouter = require('libp2p-delegated-peer-routing') +const DelegatedContentRouter = require('libp2p-delegated-content-routing') +const PeerId = require('peer-id') + +// Known peers addresses +const bootstrapMultiaddrs = [ + '/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb', + '/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN' +] +const transportKey = WEBSOCKETS.prototype[Symbol.toStringTag] + +async function main() { + // create a peerId + const peerId = await PeerId.create() + + const delegatedPeerRouting = new DelegatedPeerRouter(ipfsHttpClient({ + host: 'node0.delegate.ipfs.io', // In production you should setup your own delegates + protocol: 'https', + port: 443 + })) + + const delegatedContentRouting = new DelegatedContentRouter(peerId, ipfsHttpClient({ + host: 'node0.delegate.ipfs.io', // In production you should setup your own delegates + protocol: 'https', + port: 443 + })) + + const libp2p = await Libp2p.create({ + peerId, + addresses: { + listen: ['/ip4/127.0.0.1/tcp/8000', '/ip4/127.0.0.1/tcp/8001/ws'] + }, + modules: { + transport: [TCP, WEBSOCKETS], + streamMuxer: [MPLEX], + connEncryption: [NOISE], + peerDiscovery: [Bootstrap], + pubsub: Gossipsub, + dht: DHT, + contentRouting: [delegatedContentRouting], + peerRouting: [delegatedPeerRouting] + }, + peerRouting: { + refreshManager: { + enabled: true, + interval: 1000, + bootDelay: 11111 + } + }, + dialer: { + maxParallelDials: 100, + maxDialsPerPeer: 4, + dialTimeout: 30e3, + resolvers: { + dnsaddr: dnsaddrResolver + }, + addressSorter: publicAddressesFirst + }, + connectionManager: { + maxConnections: Infinity, + minConnections: 0, + pollInterval: 2000, + defaultPeerValue: 1, + maxData: Infinity, + maxSentData: Infinity, + maxReceivedData: Infinity, + maxEventLoopDelay: Infinity, + movingAverageInterval: 60000 + }, + transportManager: { + faultTolerance: FaultTolerance.NO_FATAL + }, + metrics: { + enabled: true, + computeThrottleMaxQueueSize: 1000, + computeThrottleTimeout: 2000, + movingAverageIntervals: [ + 60 * 1000, // 1 minute + 5 * 60 * 1000, // 5 minutes + 15 * 60 * 1000 // 15 minutes + ], + maxOldPeersRetention: 50 + }, + datastore: new LevelStore('path/to/store'), + peerStore: { + persistence: false, + threshold: 5 + }, + keychain: { + pass: 'notsafepassword123456789', + datastore: new LevelStore('path/to/store-keys') + }, + config: { + peerDiscovery: { + autoDial: true, + [Bootstrap.tag]: { + enabled: true, + list: bootstrapMultiaddrs // provide array of multiaddrs + } + }, + dht: { + enabled: true, + kBucketSize: 20, + randomWalk: { + enabled: true, // Allows to disable discovery (enabled by default) + interval: 300e3, + timeout: 10e3 + } + }, + nat: { + description: 'my-node', // set as the port mapping description on the router, defaults the current libp2p version and your peer id + enabled: true, // defaults to true + gateway: '192.168.1.1', // leave unset to auto-discover + externalIp: '80.1.1.1', // leave unset to auto-discover + ttl: 7200, // TTL for port mappings (min 20 minutes) + keepAlive: true, // Refresh port mapping after TTL expires + pmp: { + enabled: false, // defaults to false + } + }, + relay: { + enabled: true, // Allows you to dial and accept relayed connections. Does not make you a relay. + hop: { + enabled: true, // Allows you to be a relay for other peers + active: true // You will attempt to dial destination peers if you are not connected to them + }, + advertise: { + bootDelay: 15 * 60 * 1000, // Delay before HOP relay service is advertised on the network + enabled: true, // Allows you to disable the advertise of the Hop service + ttl: 30 * 60 * 1000 // Delay Between HOP relay service advertisements on the network + }, + autoRelay: { + enabled: true, // Allows you to bind to relays with HOP enabled for improving node dialability + maxListeners: 2 // Configure maximum number of HOP relays to use + } + }, + transport: { + [transportKey]: { + filter: filters.all + } + }, + pubsub: { // The pubsub options (and defaults) can be found in the pubsub router documentation + enabled: true, + emitSelf: false, // whether the node should emit to self on publish + globalSignaturePolicy: SignaturePolicy.StrictSign // message signing policy + } + } + }) + + libp2p.connectionManager.on('peer:connect', (connection) => { + console.log(`Connected to ${connection.remotePeer.toB58String()}`) + }) + + + + // Listen for new connections to peers + libp2p.connectionManager.on('peer:connect', (connection) => { + console.log(`Connected to ${connection.remotePeer.toB58String()}`) + }) + + // Listen for peers disconnecting + libp2p.connectionManager.on('peer:disconnect', (connection) => { + console.log(`Disconnected from ${connection.remotePeer.toB58String()}`) + }) + + + await libp2p.start() + console.log('started') + await libp2p.stop() +} + +main() diff --git a/test/ts-use/tsconfig.json b/test/ts-use/tsconfig.json new file mode 100644 index 0000000000..8cd47e1e86 --- /dev/null +++ b/test/ts-use/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "strict": true, + "noImplicitAny": true, + "skipLibCheck": true + } +} \ No newline at end of file