├── .github ├── FUNDING.yml ├── dependabot.yml └── workflows │ ├── node.js.yml │ └── codeql-analysis.yml ├── index.js ├── tsconfig.json ├── .gitattributes ├── .eslintrc.json ├── LICENSE ├── package.json ├── .gitignore ├── types └── crocket.d.ts ├── README.md ├── lib └── crocket.js └── test └── test.js /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [hexagon] 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require("./lib/crocket.js"); -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "npm" # See documentation for possible values 4 | directory: "/" # Location of package manifests 5 | schedule: 6 | interval: "daily" 7 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "include": ["lib/**/*"], 3 | "compilerOptions": { 4 | "allowJs": true, 5 | "declaration": true, 6 | "emitDeclarationOnly": true, 7 | "esModuleInterop": true, 8 | "outDir": "types" 9 | } 10 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "indent": [ 4 | 2, 5 | "tab" 6 | ], 7 | "quotes": [ 8 | 2, 9 | "double" 10 | ], 11 | "linebreak-style": [ 12 | 2, 13 | "unix" 14 | ], 15 | "semi": [ 16 | 2, 17 | "always" 18 | ], 19 | "complexity": [ 20 | 2, 21 | 15 22 | ] 23 | }, 24 | "globals": { 25 | "define": false, 26 | "describe": false, 27 | "it": false 28 | }, 29 | "env": { 30 | "es6": true, 31 | "node": true, 32 | "browser": true 33 | }, 34 | "extends": "eslint:recommended" 35 | } -------------------------------------------------------------------------------- /.github/workflows/node.js.yml: -------------------------------------------------------------------------------- 1 | # This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions 3 | 4 | name: Node.js CI 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | strategy: 18 | matrix: 19 | node-version: [12.x, 14.x, 16.x] 20 | # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ 21 | 22 | steps: 23 | - uses: actions/checkout@v2 24 | - name: Use Node.js ${{ matrix.node-version }} 25 | uses: actions/setup-node@v2 26 | with: 27 | node-version: ${{ matrix.node-version }} 28 | cache: 'npm' 29 | - run: npm ci 30 | - run: npm run build:ci 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Robin Nilsson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "crocket", 3 | "version": "1.0.15", 4 | "description": "Efficient and simple interprocess communication for unix/windows/macos over tcp or sockets.", 5 | "author": "Hexagon ", 6 | "main": "./lib/crocket.js", 7 | "types": "./types/crocket.d.ts", 8 | "scripts": { 9 | "build": "npm update && npm outdated && npm run test && npm run build:precleanup && npm run build:typings", 10 | "build:ci": "npm run test && npm run build:precleanup && npm run build:typings", 11 | "build:precleanup": "(rm -rf types/* || del /Q types\\*)", 12 | "build:typings": "tsc", 13 | "test": "npm run test:lint && npm run test:coverage", 14 | "test:lint": "eslint ./**/*.js", 15 | "test:suite": "uvu test test", 16 | "test:coverage": "c8 --include=lib npm run test:suite" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/hexagon/crocket" 21 | }, 22 | "bugs": { 23 | "url": "https://github.com/hexagon/crocket/issues" 24 | }, 25 | "keywords": [ 26 | "ipc", 27 | "rpc", 28 | "interprocess", 29 | "communication", 30 | "tcp", 31 | "mediator", 32 | "eventemitter", 33 | "qbus", 34 | "crocket", 35 | "unix", 36 | "windows", 37 | "sockets", 38 | "net" 39 | ], 40 | "dependencies": { 41 | "xpipe": "^1.0.5" 42 | }, 43 | "devDependencies": { 44 | "c8": "^7.10.0", 45 | "eslint": "^8.1.0", 46 | "qbus": "^0.9.7", 47 | "typescript": "^4.4.4", 48 | "uvu": "^0.5.2" 49 | }, 50 | "license": "MIT" 51 | } 52 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directories 27 | node_modules 28 | jspm_packages 29 | 30 | # Optional npm cache directory 31 | .npm 32 | 33 | # Optional REPL history 34 | .node_repl_history 35 | 36 | # ========================= 37 | # Operating System Files 38 | # ========================= 39 | 40 | # OSX 41 | # ========================= 42 | 43 | .DS_Store 44 | .AppleDouble 45 | .LSOverride 46 | 47 | # Thumbnails 48 | ._* 49 | 50 | # Files that might appear in the root of a volume 51 | .DocumentRevisions-V100 52 | .fseventsd 53 | .Spotlight-V100 54 | .TemporaryItems 55 | .Trashes 56 | .VolumeIcon.icns 57 | 58 | # Directories potentially created on remote AFP share 59 | .AppleDB 60 | .AppleDesktop 61 | Network Trash Folder 62 | Temporary Items 63 | .apdisk 64 | 65 | # Windows 66 | # ========================= 67 | 68 | # Windows image file caches 69 | Thumbs.db 70 | ehthumbs.db 71 | 72 | # Folder config file 73 | Desktop.ini 74 | 75 | # Recycle Bin used on file shares 76 | $RECYCLE.BIN/ 77 | 78 | # Windows Installer files 79 | *.cab 80 | *.msi 81 | *.msm 82 | *.msp 83 | 84 | # Windows shortcuts 85 | *.lnk 86 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ master ] 17 | paths: 18 | - 'lib/**' 19 | pull_request: 20 | # The branches below must be a subset of the branches above 21 | branches: [ master ] 22 | paths: 23 | - 'lib/**' 24 | schedule: 25 | - cron: '39 11 * * 2' 26 | 27 | jobs: 28 | analyze: 29 | name: Analyze 30 | runs-on: ubuntu-latest 31 | permissions: 32 | actions: read 33 | contents: read 34 | security-events: write 35 | 36 | strategy: 37 | fail-fast: false 38 | matrix: 39 | language: [ 'javascript' ] 40 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] 41 | # Learn more: 42 | # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed 43 | 44 | steps: 45 | - name: Checkout repository 46 | uses: actions/checkout@v2 47 | 48 | # Initializes the CodeQL tools for scanning. 49 | - name: Initialize CodeQL 50 | uses: github/codeql-action/init@v1 51 | with: 52 | languages: ${{ matrix.language }} 53 | # If you wish to specify custom queries, you can do so here or in a config file. 54 | # By default, queries listed here will override any specified in a config file. 55 | # Prefix the list here with "+" to use these queries and those in the config file. 56 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 57 | 58 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 59 | # If this step fails, then you should remove it and run the build manually (see below) 60 | - name: Autobuild 61 | uses: github/codeql-action/autobuild@v1 62 | 63 | # ℹ️ Command-line programs to run using the OS shell. 64 | # 📚 https://git.io/JvXDl 65 | 66 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 67 | # and modify them (or add more) to build your code if your project 68 | # uses a compiled language 69 | 70 | #- run: | 71 | # make bootstrap 72 | # make release 73 | 74 | - name: Perform CodeQL Analysis 75 | uses: github/codeql-action/analyze@v1 76 | -------------------------------------------------------------------------------- /types/crocket.d.ts: -------------------------------------------------------------------------------- 1 | export = Crocket; 2 | /** 3 | * Crocket constructor 4 | * 5 | * @constructor 6 | * @param {*} [mediator] - Mediator to use, EventEmitter is default 7 | * @returns {Crocket} 8 | */ 9 | declare function Crocket(mediator?: any): Crocket; 10 | declare class Crocket { 11 | /** 12 | * Crocket constructor 13 | * 14 | * @constructor 15 | * @param {*} [mediator] - Mediator to use, EventEmitter is default 16 | * @returns {Crocket} 17 | */ 18 | constructor(mediator?: any); 19 | /** @type {*} */ 20 | mediator: any; 21 | sockets: any[]; 22 | /** @type {CrocketClientOptions|CrocketServerOptions} */ 23 | opts: CrocketClientOptions | CrocketServerOptions; 24 | private _onMessage; 25 | private _onData; 26 | buffer: any; 27 | /** 28 | * Register a callback for a mediator event 29 | * 30 | * @public 31 | * @param {string} event 32 | * @param {Function} callback 33 | * @returns {Crocket} 34 | */ 35 | public on(event: string, callback: Function): Crocket; 36 | /** 37 | * Emit a mediator message 38 | * 39 | * @public 40 | * @param {string} topic 41 | * @param {*} data 42 | * @param {Function} [callback] 43 | * @returns {Crocket} 44 | */ 45 | public emit(topic: string, data: any, callback?: Function): Crocket; 46 | /** 47 | * Close IPC connection, used for both server and client 48 | * 49 | * @public 50 | * @param {Function} [callback] 51 | * @returns {Crocket} 52 | */ 53 | public close(callback?: Function): Crocket; 54 | /** 55 | * Start listening 56 | * 57 | * @public 58 | * @param {CrocketServerOptions} options 59 | * @param {Function} callback 60 | * @returns {Crocket} 61 | */ 62 | public listen(options: CrocketServerOptions, callback: Function): Crocket; 63 | isServer: boolean; 64 | server: any; 65 | /** 66 | * Connect to a Crocket server 67 | * 68 | * @public 69 | * @param {CrocketClientOptions} options 70 | * @param {Function} callback 71 | * @returns {Crocket} 72 | */ 73 | public connect(options: CrocketClientOptions, callback: Function): Crocket; 74 | } 75 | declare namespace Crocket { 76 | export { CrocketServerOptions, CrocketClientOptions }; 77 | } 78 | /** 79 | * - Crocket Options 80 | */ 81 | type CrocketClientOptions = { 82 | /** 83 | * - Path to socket, defaults to /tmp/crocket-ipc.sock 84 | */ 85 | path?: string; 86 | /** 87 | * - Hostname/ip to connect to/listen to 88 | */ 89 | host?: string; 90 | /** 91 | * - Port to connect/listen to, if port is specified, socket path is ignored and tcp is used instead 92 | */ 93 | port?: number; 94 | /** 95 | * - In ms, defaults to 2000 for server and 5000 for client 96 | */ 97 | timeout?: number; 98 | /** 99 | * - How many ms between reconnection attempts, defaults to -1 (disabled) 100 | */ 101 | reconnect?: number; 102 | /** 103 | * - Encoding for transmission, defaults to utf8 104 | */ 105 | encoding?: string; 106 | }; 107 | /** 108 | * - Crocket Options 109 | */ 110 | type CrocketServerOptions = { 111 | /** 112 | * - Path to socket, defaults to /tmp/crocket-ipc.sock 113 | */ 114 | path?: string; 115 | /** 116 | * - Hostname/ip to connect to/listen to 117 | */ 118 | host?: string; 119 | /** 120 | * - Port to connect/listen to, if port is specified, socket path is ignored and tcp is used instead 121 | */ 122 | port?: number; 123 | /** 124 | * - Encoding for transmission, defaults to utf8 125 | */ 126 | encoding?: string; 127 | }; 128 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # crocket 2 | 3 | ![Node.js CI](https://github.com/Hexagon/crocket/workflows/Node.js%20CI/badge.svg?branch=master) [![npm version](https://badge.fury.io/js/crocket.svg)](https://badge.fury.io/js/crocket) [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Hexagon/crocket/blob/master/LICENSE) [![NPM Downloads](https://img.shields.io/npm/dm/crocket.svg)](https://www.npmjs.org/package/crocket) 4 | 5 | Minimal node.js cross platform IPC communication library. 6 | 7 | * Communcates over TCP, unix sockets or windows pipe. 8 | * Works both locally and remotely. 9 | * Works on Linux, Windows AND macOS. 10 | * Pluggable event mediator, uses EventEmitter by default. But can be extended with something like [qbus](https://www.npmjs.com/package/qbus) for extended functionality. 11 | 12 | 13 | # Installation 14 | 15 | ```npm install crocket``` 16 | 17 | # Usage 18 | 19 | ### Host process 20 | 21 | ```javascript 22 | var crocket = require("crocket"), 23 | server = new crocket(); 24 | 25 | // Start listening, this example communicate by file sockets 26 | server.listen({ "path": "/tmp/crocket-test.sock" }, (e) => { 27 | 28 | // Fatal errors are supplied as the first parameter to callback 29 | if(e) throw e; 30 | 31 | // All is well if we got this far 32 | console.log('IPC listening on /tmp/crocket-test.sock'); 33 | 34 | }); 35 | 36 | // Events are handled by EventEmitter by default ... 37 | server.on('/request/food', function (payload) { 38 | 39 | // Respond to the query 40 | server.emit('/response', 'You asked for food and supplied ' + payload); 41 | 42 | }); 43 | 44 | 45 | // React to communication errors 46 | server.on('error', (e) => { console.error('Communication error occurred: ', e); }); 47 | ``` 48 | 49 | Output 50 | 51 | ``` 52 | > node test-server.js 53 | IPC listening on /tmp/crocket-test.sock 54 | ``` 55 | 56 | ### Client process 57 | 58 | ```javascript 59 | var crocket = require("crocket"), 60 | client = new crocket(); 61 | 62 | client.connect({ "path": "/tmp/crocket-test.sock" }, (e) => { 63 | 64 | // Connection errors are supplied as the first parameter to callback 65 | if(e) throw e; 66 | 67 | // Instantly a message to the server 68 | client.emit('/request/food', 'cash'); 69 | 70 | }); 71 | 72 | // Expect a reply on '/response' 73 | client.on('/response', function (what) { 74 | 75 | // Should print 'Server said: You asked for food and supplied cash' 76 | console.log('Server said: ' + what); 77 | 78 | // Work is done now, no need to keep a connection open 79 | client.close(); 80 | 81 | }); 82 | ``` 83 | 84 | Output 85 | 86 | ``` 87 | > node test-client.js 88 | Server said: You asked for food and supplied cash 89 | ``` 90 | 91 | ### Replacing EventEmitter 92 | 93 | ### Host process 94 | 95 | ```javascript 96 | var crocket = require("crocket"), 97 | 98 | // Require the alternative event handler 99 | qbus = require("qbus"), 100 | 101 | // Pass the mediator to the constructor 102 | server = new crocket(qbus); 103 | 104 | // Start listening, this example communicate by file sockets 105 | server.listen({ "path": "/tmp/crocket-test.sock" }, (e) => { 106 | 107 | // Fatal errors are supplied as the first parameter to callback 108 | if(e) throw e; 109 | 110 | // All is well if we got this far 111 | console.log('IPC listening on /tmp/crocket-test.sock'); 112 | 113 | }); 114 | 115 | // Now we're using qbus to handle events 116 | // Documentation: https://github.com/unkelpehr/qbus 117 | // Query tester: http://unkelpehr.github.io/qbus/ 118 | server.on('/request/:what', function (what, payload) { 119 | 120 | // Respond to the query 121 | server.emit('/response', 'You asked for ' + what + ' and supplied ' + payload); 122 | 123 | }); 124 | 125 | // React to communication errors 126 | server.on('error', (e) => { console.error('Communication error occurred: ', e); }); 127 | ``` 128 | 129 | Output 130 | 131 | ``` 132 | > node test-server.js 133 | IPC listening on /tmp/crocket-test.sock 134 | ``` 135 | 136 | 137 | ### Options 138 | 139 | All available options for server.listen 140 | 141 | **Server** 142 | ```json 143 | { 144 | "path": "/tmp/node-crocket.sock", 145 | "host": null, 146 | "port": null, 147 | "encoding": "utf8" 148 | } 149 | ``` 150 | 151 | All available options for client.connect 152 | 153 | **Client** 154 | ```json 155 | { 156 | "path": "/tmp/node-crocket.sock", 157 | "host": null, 158 | "port": null, 159 | "reconnect": -1, 160 | "timeout": 5000, 161 | "encoding": "utf8" 162 | } 163 | ``` 164 | 165 | **Path** is a file-socket path, normalized by [xpipe](https://www.npmjs.com/package/xpipe). As an example, ```/tmp/my.sock``` is unchanged on Linux/OS X, while it is transformed to ```//./pipe/tmp/my.sock``` on Windows. 166 | 167 | **Port** is specified if you want to use TCP instead of file sockets. 168 | 169 | **Host** Only used in TCP mode. For server, ```0.0.0.0``` makes crocket listen on any IPv4-interface. ```::``` Is the equivalent for IPv6. For client, you specify the host address. 170 | 171 | **Reconnect** is the number of milliseconds to wait before reviving a broken listener/connection, or -1 to disable automtic revive. 172 | 173 | **Encoding** the encoding used by the underlaying sockets, in most cases this should be left at default. 174 | 175 | 176 | # License 177 | 178 | MIT 179 | -------------------------------------------------------------------------------- /lib/crocket.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (c) 2016-2021 Hexagon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | 23 | */ 24 | 25 | /** 26 | * @typedef {Object} CrocketServerOptions - Crocket Options 27 | * @property {string} [path] - Path to socket, defaults to /tmp/crocket-ipc.sock 28 | * @property {string} [host] - Hostname/ip to connect to/listen to 29 | * @property {number} [port] - Port to connect/listen to, if port is specified, socket path is ignored and tcp is used instead 30 | * @property {string} [encoding] - Encoding for transmission, defaults to utf8 31 | */ 32 | 33 | /** 34 | * @typedef {Object} CrocketClientOptions - Crocket Options 35 | * @property {string} [path] - Path to socket, defaults to /tmp/crocket-ipc.sock 36 | * @property {string} [host] - Hostname/ip to connect to/listen to 37 | * @property {number} [port] - Port to connect/listen to, if port is specified, socket path is ignored and tcp is used instead 38 | * @property {number} [timeout] - In ms, defaults to 2000 for server and 5000 for client 39 | * @property {number} [reconnect] - How many ms between reconnection attempts, defaults to -1 (disabled) 40 | * @property {string} [encoding] - Encoding for transmission, defaults to utf8 41 | */ 42 | 43 | "use strict"; 44 | 45 | const 46 | xpipe = require("xpipe"), 47 | net = require("net"), 48 | EventEmitter = require("events"), 49 | extend = require("util")._extend; 50 | 51 | // Private properties 52 | const 53 | defaults = { 54 | "server": { 55 | "path": "/tmp/crocket-ipc.sock", 56 | "host": null, 57 | "port": null, 58 | "timeout": 2000, 59 | "encoding": "utf8" 60 | }, 61 | "client": { 62 | "path": "/tmp/crocket-ipc.sock", 63 | "host": null, 64 | "port": null, 65 | "reconnect": -1, 66 | "timeout": 5000, 67 | "encoding": "utf8" 68 | } 69 | }, 70 | separator = "<< {} ); 95 | } 96 | 97 | this.sockets = []; 98 | this.connectTimeout = void 0; 99 | this.buffer = void 0; 100 | 101 | /** @type {CrocketClientOptions|CrocketServerOptions} */ 102 | this.opts = {}; 103 | 104 | return this; 105 | } 106 | 107 | /** 108 | * @private 109 | * @param {*} message 110 | * @param {*} socket 111 | */ 112 | Crocket.prototype._onMessage = function (message, socket) { 113 | try { 114 | var incoming = JSON.parse(message); 115 | if( incoming && incoming.topic ) { 116 | this.mediator.emit(incoming.topic, incoming.data, socket); 117 | } else { 118 | this.mediator.emit("error", new Error("Invalid data received.")); 119 | } 120 | } catch (e) { 121 | this.mediator.emit("error", e); 122 | } 123 | }; 124 | 125 | /** 126 | * @private 127 | * @param {*} data 128 | * @param {*} socket 129 | */ 130 | Crocket.prototype._onData = function (data, socket) { 131 | 132 | // Append to buffer 133 | if ( this.buffer ) { 134 | this.buffer += data; 135 | } else { 136 | this.buffer = data; 137 | } 138 | 139 | // Did we get a separator 140 | if (data.indexOf(separator) !== -1) { 141 | while(this.buffer.indexOf(separator) !== -1) { 142 | var message = this.buffer.substring(0,this.buffer.indexOf(separator)); 143 | this.buffer = this.buffer.substring(this.buffer.indexOf(separator)+separator.length); 144 | if (message) { 145 | this._onMessage(message, socket); 146 | } 147 | } 148 | } 149 | 150 | }; 151 | 152 | 153 | /** 154 | * Register a callback for a mediator event 155 | * 156 | * @public 157 | * @param {string} event 158 | * @param {Function} callback 159 | * @returns {Crocket} 160 | */ 161 | Crocket.prototype.on = function (event, callback) { 162 | this.mediator.on(event, callback); 163 | return this; 164 | }; 165 | 166 | /** 167 | * Emit a mediator message 168 | * 169 | * @public 170 | * @param {string} topic 171 | * @param {*} data 172 | * @param {Function} [callback] 173 | * @returns {Crocket} 174 | */ 175 | Crocket.prototype.emit = function (topic, data, callback) { 176 | try { 177 | var message = JSON.stringify({topic: topic, data: data})+separator; 178 | this.sockets.forEach(function(socket) { 179 | socket.write(message); 180 | }); 181 | callback && callback(); 182 | } catch (e) { 183 | if (callback) { 184 | callback(e); 185 | } else { 186 | this.mediator.emit("error", e); 187 | } 188 | } 189 | return this; 190 | }; 191 | 192 | /** 193 | * Close IPC connection, used for both server and client 194 | * 195 | * @public 196 | * @param {Function} [callback] 197 | * @returns {Crocket} 198 | */ 199 | Crocket.prototype.close = function (callback) { 200 | if (this.isServer) { 201 | this.server.close(); 202 | if (callback) this.server.on("close", callback); 203 | } else { 204 | this.opts.reconnect = -1; 205 | clearTimeout(this.connectTimeout); 206 | this.sockets[0].destroy(callback); 207 | } 208 | return this; 209 | }; 210 | 211 | /** 212 | * Start listening 213 | * 214 | * @public 215 | * @param {CrocketServerOptions} options 216 | * @param {Function} callback 217 | * @returns {Crocket} 218 | */ 219 | Crocket.prototype.listen = function (options, callback) { 220 | 221 | let self = this; 222 | 223 | // ToDo, make options optional 224 | this.opts = extend(extend(this.opts, defaults.server), options); 225 | this.isServer = true; 226 | 227 | this.server = net.createServer(); 228 | 229 | this.server.on("error", (e) => self.mediator.emit("error", e) ); 230 | 231 | // New connection established 232 | this.server.on("connection", (socket) => { 233 | self.sockets.push(socket); 234 | self.mediator.emit("connect", socket); 235 | socket.setEncoding(self.opts.encoding); 236 | socket.on("data", (data) => self._onData(data, socket) ); 237 | socket.on("close", (socket) => { 238 | self.mediator.emit("disconnect", socket); 239 | self.sockets.splice(self.sockets.indexOf(socket), 1); 240 | }); 241 | socket.on("error", (e) => { 242 | self.mediator.emit("error", e); 243 | }); 244 | }); 245 | 246 | this.server.on("close", () => { 247 | self.mediator.emit("close"); 248 | }); 249 | 250 | // Start listening 251 | if (this.opts.port) { 252 | this.server.listen(this.opts.port, this.opts.host, callback); 253 | } else { 254 | this.server.listen(xpipe.eq(this.opts.path), callback); 255 | } 256 | 257 | return this; 258 | }; 259 | 260 | /** 261 | * Connect to a Crocket server 262 | * 263 | * @public 264 | * @param {CrocketClientOptions} options 265 | * @param {Function} callback 266 | * @returns {Crocket} 267 | */ 268 | Crocket.prototype.connect = function (options, callback) { 269 | 270 | var self = this; 271 | 272 | // ToDo, make options optional 273 | 274 | this.opts = extend(extend(this.opts, defaults.client), options); 275 | this.isServer = false; 276 | 277 | var socket = new net.Socket(); 278 | this.sockets = [socket]; 279 | 280 | var 281 | 282 | flagConnected, 283 | 284 | connected = () => { 285 | flagConnected = true; 286 | clearTimeout(self.connectTimeout); 287 | callback && callback(); 288 | }, 289 | 290 | connect = (first) => { 291 | if (self.opts.port) { 292 | socket.connect(self.opts.port, self.opts.host, first ? connected : undefined); 293 | } else { 294 | socket.connect(xpipe.eq(self.opts.path), first ? connected : undefined); 295 | } 296 | self.connectTimeout = setTimeout(() => { 297 | if ( !flagConnected ) { 298 | socket.destroy(); 299 | if (self.opts.reconnect === -1) { 300 | callback(new Error("Connection timeout")); 301 | } 302 | } 303 | }, self.opts.timeout); 304 | }; 305 | 306 | socket.setEncoding(self.opts.encoding); 307 | 308 | socket.on("error", (e) => { 309 | self.mediator.emit("error", e); 310 | }); 311 | 312 | socket.on("data", (data) => { 313 | self._onData(data, socket); 314 | }); 315 | 316 | socket.on("close", () => { 317 | if (self.opts.reconnect > 0) { 318 | setTimeout(() => connect(), self.opts.reconnect); 319 | } else { 320 | self.mediator.emit("close"); 321 | } 322 | }); 323 | 324 | connect(true); 325 | 326 | return this; 327 | }; 328 | 329 | module.exports = Crocket; -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (c) 2016-2021 Hexagon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | 23 | */ 24 | 25 | /* eslint no-unused-vars: 0 */ 26 | 27 | let 28 | suite = require("uvu").suite, 29 | assert = require("uvu/assert"), 30 | ipc = require("../index.js"), 31 | address = { 32 | tcp: { host: "127.0.0.1", port: 31338 }, 33 | socket: { path: "/tmp/crdtest.sock" } 34 | }; 35 | 36 | // Convenience function for asynchronous testing 37 | const timeout = (timeoutMs, fn) => { 38 | return () => { 39 | let to = void 0; 40 | return new Promise((resolve, reject) => { 41 | fn(resolve, reject); 42 | to = setTimeout(() => { reject(new Error("Timeout")); }, timeoutMs); 43 | }).finally(() => { 44 | clearTimeout(to); 45 | }); 46 | }; 47 | }; 48 | 49 | // Create a ~6MB payload 50 | let longpayload = "I am payload"; 51 | for (let i = 0; i < 19;i++) { 52 | longpayload += longpayload; 53 | } 54 | 55 | const optionalNew = suite("new"); 56 | 57 | optionalNew("should be optional", timeout(2000, (resolve) => { 58 | assert.not.throws(() => { 59 | var server = ipc(); 60 | server.listen(address.tcp, (e) => { if(e) throw e; server.close(resolve); } ); 61 | }); 62 | })); 63 | 64 | optionalNew.run(); 65 | 66 | const listen = suite("listen"); 67 | 68 | listen("on tcp should not throw", timeout(2000, (resolve) => { 69 | assert.not.throws(() => { 70 | var server = new ipc(); 71 | server.listen(address.tcp, (e) => { if(e) throw e; server.close(resolve); } ); 72 | }); 73 | })); 74 | 75 | listen("on sockets should not throw", timeout(2000, (resolve) => { 76 | assert.not.throws(() => { 77 | var server = new ipc(); 78 | server.listen(address.socket, (e) => { if(e) throw e; server.close(resolve); } ); 79 | }); 80 | })); 81 | 82 | listen.run(); 83 | 84 | const connect = suite("connect"); 85 | 86 | connect("to tcp should not throw", timeout(2000, (resolve, reject) => { 87 | assert.not.throws(() => { 88 | // Create server 89 | var server = new ipc(); 90 | server.listen(address.tcp, (e1) => { 91 | if(e1) throw e1; 92 | // Create client 93 | var client = new ipc(); 94 | client.connect(address.tcp, (e2) => { 95 | if(e2) throw e2; 96 | client.close(); 97 | server.close(resolve); 98 | }); 99 | }); 100 | }); 101 | })); 102 | 103 | connect("to socket should not throw", timeout(2000, (resolve, reject) => { 104 | assert.not.throws(() => { 105 | 106 | // Create server 107 | var server = new ipc(); 108 | server.listen(address.socket, (e1) => { 109 | if(e1) throw e1; 110 | // Create client 111 | var client = new ipc(); 112 | client.connect(address.socket, (e2) => { 113 | if(e2) throw e2; 114 | client.close(); 115 | server.close(resolve); 116 | }); 117 | }); 118 | 119 | }); 120 | })); 121 | 122 | connect.run(); 123 | 124 | const connectNonExisting = suite("connect to non existing server"); 125 | 126 | 127 | connectNonExisting("with tcp should return error", timeout(2000, (resolve, reject) => { 128 | 129 | assert.not.throws(() => { 130 | 131 | var client = new ipc(); 132 | client.connect({ host: "asdf", port: 1234, timeout: 500}, (e) => { 133 | if(e) { 134 | resolve(e); 135 | } 136 | }); 137 | 138 | }); 139 | })); 140 | 141 | connectNonExisting("with socket should return error", timeout(2000, (resolve, reject) => { 142 | 143 | assert.not.throws(() => { 144 | 145 | var client = new ipc(); 146 | client.connect({ path: "/tmp/__lol-asdf-not-existing", timeout: 500}, (e) => { 147 | if(e) { 148 | resolve(); 149 | } 150 | }); 151 | 152 | }); 153 | })); 154 | 155 | connectNonExisting.run(); 156 | 157 | const reconnect = suite("reconnect"); 158 | 159 | reconnect("with tcp should not throw", timeout(7500, (resolve, reject) => { 160 | 161 | assert.not.throws(() => { 162 | 163 | var qbus = require("qbus"); 164 | var client = new ipc(qbus); 165 | client.connect({ host: "127.0.0.1", port: 51234, timeout: 500, reconnect: 500}, (e) => { 166 | if(!e) { 167 | client.emit("/test/this",{}); 168 | client.close(); 169 | } 170 | }); 171 | 172 | // Start server after 5 seconds 173 | setTimeout(() => { 174 | 175 | var server = new ipc(qbus); 176 | server.listen({ host: "127.0.0.1", port: 51234 } , (e1) => { 177 | if(e1) throw e1; 178 | }); 179 | server.on("/test/:what", (what, payload) => { 180 | server.close(resolve); 181 | }); 182 | server.on("error", (e) => { throw e; }) ; 183 | 184 | },5000); 185 | }); 186 | })); 187 | 188 | reconnect.run(); 189 | 190 | const altMediator = suite("alternative mediator"); 191 | 192 | altMediator("over tcp should complete and not throw", timeout(2000, (resolve, reject) => { 193 | assert.not.throws(() => { 194 | 195 | // Create server 196 | var qbus = require("qbus"); 197 | var server = new ipc(qbus); 198 | server.listen(address.tcp, (e1) => { 199 | if(e1) throw e1; 200 | // Create client 201 | var client = new ipc(qbus); 202 | client.connect(address.tcp, (e2) => { 203 | if(e2) throw e2; 204 | client.emit("/test/send", "I am payload"); 205 | client.close(); 206 | }); 207 | client.on("error", (e) => { throw e; }) ; 208 | }); 209 | server.on("/test/:what", function (what, payload) { 210 | if (what === "send" && payload == "I am payload") { 211 | server.close(resolve); 212 | } 213 | }); 214 | server.on("error", (e) => { throw e; }) ; 215 | 216 | }); 217 | })); 218 | 219 | altMediator("over sockets should complete and not throw (2)", timeout(2000, (resolve, reject) => { 220 | assert.not.throws(() => { 221 | 222 | // Create server 223 | var qbus = require("qbus"), 224 | server = new ipc(qbus); 225 | server.listen(address.socket, (e1) => { 226 | if(e1) throw e1; 227 | // Create client 228 | var client = new ipc(qbus); 229 | client.connect(address.socket, (e2) => { 230 | if(e2) throw e2; 231 | client.emit("/test/send", "I am payload"); 232 | client.close(); 233 | }); 234 | client.on("error", (e) => { throw e; }) ; 235 | }); 236 | server.on("/test/:what", function (what, payload) { 237 | if (what === "send" && payload == "I am payload") { 238 | server.close(resolve); 239 | } 240 | }); 241 | server.on("error", (e) => { throw e; }) ; 242 | 243 | }); 244 | })); 245 | 246 | altMediator.run(); 247 | 248 | const longPayload = suite("long payload"); 249 | 250 | longPayload("over sockets should complete and not throw (3)", timeout(15000, (resolve, reject) => { 251 | assert.not.throws(() => { 252 | 253 | // Create server 254 | var server = new ipc(); 255 | server.listen({ path: "/tmp/crdtest-3.sock" }, (e1) => { 256 | if(e1) throw e1; 257 | // Create client 258 | var client = new ipc(); 259 | client.connect({ path: "/tmp/crdtest-3.sock" }, (e2) => { 260 | if(e2) throw e2; 261 | client.emit("/test/send", longpayload); 262 | client.on("/test/reply", function (payload) { 263 | assert.equal(payload,longpayload); 264 | client.close(); 265 | server.close(resolve); 266 | }); 267 | }); 268 | client.on("error", (e) => { throw e; }) ; 269 | }); 270 | server.on("/test/send", function (payload) { 271 | if ( payload == longpayload) { 272 | server.emit("/test/reply", longpayload); 273 | } 274 | }); 275 | server.on("error", (e) => { throw e; }) ; 276 | 277 | }); 278 | })); 279 | 280 | longPayload("over tcp should complete and not throw", timeout(15000, (resolve, reject) => { 281 | assert.not.throws(() => { 282 | 283 | // Create server 284 | var server = new ipc(); 285 | server.listen(address.tcp, (e1) => { 286 | if(e1) throw e1; 287 | // Create client 288 | var client = new ipc(); 289 | client.connect(address.tcp, (e2) => { 290 | if(e2) throw e2; 291 | client.emit("/test/send", longpayload); 292 | client.on("/test/reply", function (payload) { 293 | assert.equal(payload, longpayload); 294 | client.close(); 295 | server.close(resolve); 296 | }); 297 | }); 298 | client.on("error", (e) => { throw e; }) ; 299 | }); 300 | server.on("/test/send", function (payload) { 301 | if (payload == longpayload) { 302 | server.emit("/test/reply", longpayload); 303 | } 304 | }); 305 | server.on("error", (e) => { throw e; }) ; 306 | 307 | }); 308 | })); 309 | 310 | longPayload.run(); 311 | 312 | const emitCallback = suite("emit callback"); 313 | emitCallback("should execute over socket", timeout(2000, (resolve, reject) => { 314 | assert.not.throws(() => { 315 | 316 | // Create server 317 | var server = new ipc(); 318 | server.listen({ path: "/tmp/crdtest-2.sock" }, (e1) => { 319 | if(e1) throw e1; 320 | // Create client 321 | var client = new ipc(); 322 | client.connect({ path: "/tmp/crdtest-2.sock" }, (e2) => { 323 | if(e2) throw e2; 324 | client.emit("/test/send", longpayload, () => { 325 | client.close(); 326 | server.close(resolve); 327 | }); 328 | }); 329 | client.on("error", (e) => { throw e; }) ; 330 | }); 331 | server.on("/test/send", function (payload) { 332 | if ( payload == "I am payload") { 333 | server.emit("/test/reply"); 334 | } 335 | }); 336 | server.on("error", (e) => { throw e; }) ; 337 | }); 338 | })); 339 | 340 | emitCallback("should execute over tcp", timeout(15000, (resolve, reject) => { 341 | assert.not.throws(() => { 342 | 343 | // Create server 344 | var server = new ipc(); 345 | server.listen(address.tcp, (e1) => { 346 | if(e1) throw e1; 347 | // Create client 348 | var client = new ipc(); 349 | client.connect(address.tcp, (e2) => { 350 | if(e2) throw e2; 351 | client.emit("/test/send", longpayload, () => { 352 | client.close(); 353 | server.close(resolve); 354 | }); 355 | }); 356 | client.on("error", (e) => { throw e; }) ; 357 | }); 358 | server.on("/test/send", function (payload) { 359 | if ( payload == "I am payload") { 360 | server.emit("/test/reply"); 361 | } 362 | }); 363 | server.on("error", (e) => { throw e; }) ; 364 | }); 365 | })); 366 | 367 | emitCallback.run(); --------------------------------------------------------------------------------