diff --git a/packages/cli/README.md b/packages/cli/README.md new file mode 100644 index 00000000..03700bad --- /dev/null +++ b/packages/cli/README.md @@ -0,0 +1,36 @@ +# cli + +## chat + +A basic CLI to pass messages between peers using stdin/stdout + +* Install dependencies: + + ```bash + yarn install + ``` + +* Build the peer package: + + ``` + cd packages/peer + yarn build + ``` + +* Run a local signalling server (skip if an already running signalling server is available): + + ```bash + # In packages/peer + yarn signal-server + ``` + +* Start the node: + + ```bash + # In packages/cli + yarn chat --signalServer [SIGNAL_SERVER_URL] + ``` + + * `signalServer`: multiaddr of a signalling server (default: local signalling server multiaddr) + +* The process starts reading from `stdin` and outputs messages from others peers to `stdout`. diff --git a/packages/cli/package.json b/packages/cli/package.json index 805c3b11..e8245076 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,19 +1,23 @@ { "name": "@cerc-io/cli", "version": "0.2.18", - "main": "dist/index.js", + "exports": "./dist/index.js", "license": "AGPL-3.0", + "type": "module", "scripts": { "lint": "eslint .", "build": "yarn clean && tsc && yarn copy-assets", "clean": "rm -rf ./dist", - "copy-assets": "copyfiles -u 1 src/**/*.gql dist/" + "copy-assets": "copyfiles -u 1 src/**/*.gql dist/", + "chat": "node dist/chat.js" }, "dependencies": { + "@cerc-io/peer": "^0.2.18", "@cerc-io/util": "^0.2.18", "@ethersproject/providers": "^5.4.4", "@graphql-tools/utils": "^9.1.1", "@ipld/dag-cbor": "^6.0.12", + "@libp2p/interface-peer-id": "^1.1.2", "apollo-server-express": "^3.11.1", "debug": "^4.3.1", "express": "^4.18.2", @@ -24,6 +28,7 @@ }, "devDependencies": { "@types/express": "^4.17.14", + "@types/node": "16.11.7", "@typescript-eslint/eslint-plugin": "^4.25.0", "@typescript-eslint/parser": "^4.25.0", "eslint-config-semistandard": "^15.0.1", @@ -31,6 +36,7 @@ "eslint-plugin-import": "^2.23.3", "eslint-plugin-node": "^11.1.0", "eslint-plugin-promise": "^5.1.0", - "eslint-plugin-standard": "^5.0.0" + "eslint-plugin-standard": "^5.0.0", + "typescript": "^4.9.4" } } diff --git a/packages/cli/src/chat.ts b/packages/cli/src/chat.ts new file mode 100644 index 00000000..669feeb9 --- /dev/null +++ b/packages/cli/src/chat.ts @@ -0,0 +1,52 @@ +import * as readline from 'readline'; +import { hideBin } from 'yargs/helpers'; +import yargs from 'yargs'; + +import { Peer } from '@cerc-io/peer'; +import { PeerId } from '@libp2p/interface-peer-id'; + +interface Arguments { + signalServer: string; +} + +async function main (): Promise { + const argv: Arguments = _getArgv(); + if (!argv.signalServer) { + console.log('Using default signalling server URL'); + } + + const peer = new Peer(true); + await peer.init(argv.signalServer); + + peer.subscribeMessage((peerId: PeerId, message: string) => { + console.log(`> ${peerId.toString()} > ${message}`); + }); + + console.log(`Peer ID: ${peer.peerId?.toString()}`); + + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + + rl.on('line', (input: string) => { + peer.broadcastMessage(input); + }); + + console.log('Reading input...'); +} + +function _getArgv (): any { + return yargs(hideBin(process.argv)).parserConfiguration({ + 'parse-numbers': false + }).options({ + signalServer: { + type: 'string', + describe: 'Signalling server URL' + } + }).argv; +} + +main().catch(err => { + console.log(err); +}); diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json index d2a0be0c..0f91a3ed 100644 --- a/packages/cli/tsconfig.json +++ b/packages/cli/tsconfig.json @@ -5,7 +5,7 @@ /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */ - "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ + "module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ "lib": [ "ES5", "ES6", "ES2020" ], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ // "checkJs": true, /* Report errors in .js files. */ @@ -49,7 +49,8 @@ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ "typeRoots": [ - "./src/types" + "./src/types", + "node_modules/@types" ], /* List of folders to include type definitions from. */ // "types": [], /* Type declaration files to be included in compilation. */ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ diff --git a/packages/peer/README.md b/packages/peer/README.md index 44774aa8..d6f90374 100644 --- a/packages/peer/README.md +++ b/packages/peer/README.md @@ -7,7 +7,7 @@ Package used for connecting between peers and send messages - [x] Discover peers - [x] Connect between peers and send messages - [x] Use package in browser -- [ ] Use package in server +- [x] Use package in server - [ ] Send messages between systems in different LANs ## Issues diff --git a/packages/peer/package.json b/packages/peer/package.json index 58a55b80..1c3e62ec 100644 --- a/packages/peer/package.json +++ b/packages/peer/package.json @@ -39,6 +39,7 @@ }, "devDependencies": { "@libp2p/webrtc-star-signalling-server": "^2.0.5", + "@types/node": "16.11.7", "@typescript-eslint/eslint-plugin": "^4.25.0", "@typescript-eslint/parser": "^4.25.0", "eslint": "^7.27.0", diff --git a/packages/peer/src/index.ts b/packages/peer/src/index.ts index f88498c5..8abe7ea0 100644 --- a/packages/peer/src/index.ts +++ b/packages/peer/src/index.ts @@ -4,7 +4,7 @@ import { createLibp2p, Libp2p } from 'libp2p'; // For nodejs. -// import wrtc from 'wrtc'; +import wrtc from 'wrtc'; import assert from 'assert'; import { pipe } from 'it-pipe'; import * as lp from 'it-length-prefixed'; @@ -13,7 +13,6 @@ import { pushable, Pushable } from 'it-pushable'; import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'; import { toString as uint8ArrayToString } from 'uint8arrays/to-string'; -import { webSockets } from '@libp2p/websockets'; import { webRTCStar, WebRTCStarTuple } from '@libp2p/webrtc-star'; import { noise } from '@chainsafe/libp2p-noise'; import { mplex } from '@libp2p/mplex'; @@ -32,10 +31,13 @@ export class Peer { _peerStreamMap: Map> = new Map() _messageHandlers: Array<(peerId: PeerId, message: string) => void> = [] - constructor () { + constructor (nodejs?: boolean) { // Instantiation in nodejs. - // this._wrtcStar = webRTCStar({ wrtc }); - this._wrtcStar = webRTCStar(); + if (nodejs) { + this._wrtcStar = webRTCStar({ wrtc }); + } else { + this._wrtcStar = webRTCStar(); + } } get peerId (): PeerId | undefined { diff --git a/packages/peer/tsconfig.json b/packages/peer/tsconfig.json index 441996d5..797133aa 100644 --- a/packages/peer/tsconfig.json +++ b/packages/peer/tsconfig.json @@ -49,7 +49,8 @@ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ "typeRoots": [ - "./src/types" + "./src/types", + "node_modules/@types" ], /* List of folders to include type definitions from. */ // "types": [], /* Type declaration files to be included in compilation. */ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ diff --git a/yarn.lock b/yarn.lock index 1dd5e640..fb21b0ee 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4069,9 +4069,9 @@ "@libp2p/interface-peer-info" "^1.0.0" "@libp2p/interfaces" "^3.0.0" -"@libp2p/interface-peer-id@^1.0.0", "@libp2p/interface-peer-id@^1.0.2", "@libp2p/interface-peer-id@^1.0.4": +"@libp2p/interface-peer-id@^1.0.0", "@libp2p/interface-peer-id@^1.0.2", "@libp2p/interface-peer-id@^1.0.4", "@libp2p/interface-peer-id@^1.1.2": version "1.1.2" - resolved "https://registry.npmjs.org/@libp2p/interface-peer-id/-/interface-peer-id-1.1.2.tgz" + resolved "https://registry.yarnpkg.com/@libp2p/interface-peer-id/-/interface-peer-id-1.1.2.tgz#22cbfb4707949cd49c3271a871172221d6920049" integrity sha512-S5iyVzG2EUgxm4NLe8W4ya9kpKuGfHs7Wbbos0wOUB4GXsbIKgOOxIr4yf+xGFgtEBaoximvlLkpob6dn8VFgA== dependencies: multiformats "^10.0.0" @@ -5702,6 +5702,11 @@ resolved "https://registry.npmjs.org/@types/node/-/node-16.18.10.tgz" integrity sha512-XU1+v7h81p7145ddPfjv7jtWvkSilpcnON3mQ+bDi9Yuf7OI56efOglXRyXWgQ57xH3fEQgh7WOJMncRHVew5w== +"@types/node@16.11.7": + version "16.11.7" + resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.7.tgz#36820945061326978c42a01e56b61cd223dfdc42" + integrity sha512-QB5D2sqfSjCmTuWcBWyJ+/44bcjO7VbjSbOE0ucoVbAsSNQc4Lt6QkgkVXkTDwkL4z/beecZNDvVX15D4P8Jbw== + "@types/node@>=10.0.0": version "18.11.17" resolved "https://registry.npmjs.org/@types/node/-/node-18.11.17.tgz" @@ -22614,7 +22619,7 @@ typeorm@^0.2.32: typescript@^4.3.2, typescript@^4.9.4: version "4.9.4" - resolved "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.4.tgz#a2a3d2756c079abda241d75f149df9d561091e78" integrity sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg== typewise-core@^1.2, typewise-core@^1.2.0: