├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | sandbox.js 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 Mathias Buus 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # binary-message-stream 2 | 3 | Simple duplex stream that allows you to send messages, including binary messages. 4 | 5 | ``` 6 | npm install binary-message-stream 7 | ``` 8 | 9 | ## Usage 10 | 11 | ``` js 12 | const Messenger = require('binary-message-stream') 13 | 14 | const a = new Messenger() 15 | const b = new Messenger() 16 | 17 | a.pipe(b).pipe(a) 18 | 19 | a.send('hi') 20 | a.send(Buffer.alloc(2)) 21 | a.send({ yes: Buffer.from('yes') }) 22 | 23 | b.on('message', function (message) { 24 | // prints: 25 | // 'hi' 26 | // 27 | // { yes: } 28 | console.log(message) 29 | }) 30 | ``` 31 | 32 | ## License 33 | 34 | MIT 35 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const borc = require('borc') 2 | const streamx = require('streamx') 3 | 4 | module.exports = class Messenger extends streamx.Duplex { 5 | constructor () { 6 | super() 7 | 8 | this.missing = 3 9 | this.state = 0 10 | this.buffer = null 11 | } 12 | 13 | _write (data, cb) { 14 | while (data.length && !this.destroying) { 15 | if (this.buffer) { 16 | const offset = this.buffer.length - this.missing 17 | data.copy(this.buffer, offset) 18 | if (data.length >= this.missing) { 19 | data = data.slice(this.missing) 20 | this.missing = 0 21 | } else { 22 | this.missing -= data.length 23 | } 24 | } else { 25 | if (this.missing <= data.length) { 26 | this.buffer = data.slice(0, this.missing) 27 | data = data.slice(this.missing) 28 | this.missing = 0 29 | } else { 30 | this.buffer = Buffer.allocUnsafe(this.missing) 31 | continue 32 | } 33 | } 34 | 35 | if (this.missing !== 0) return cb(null) 36 | 37 | if (this.state === 0) { 38 | this.state = 1 39 | this.missing = this.buffer[0] + 256 * this.buffer[1] + 65536 * this.buffer[2] 40 | this.buffer = null 41 | } else { 42 | let message 43 | try { 44 | message = borc.decodeFirst(this.buffer) 45 | } catch (err) { 46 | return cb(err) 47 | } 48 | 49 | this.state = 0 50 | this.missing = 3 51 | this.buffer = null 52 | this.emit('message', message) 53 | } 54 | } 55 | 56 | cb(null) 57 | } 58 | 59 | send (msg) { 60 | const header = Buffer.alloc(3) 61 | const buf = borc.encode(msg) 62 | header[0] = buf.length & 0x0000ff 63 | header[1] = (buf.length & 0x00ff00) >>> 8 64 | header[2] = (buf.length & 0xff0000) >>> 16 65 | this.push(Buffer.concat([header, buf])) 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "binary-message-stream", 3 | "version": "1.0.2", 4 | "description": "Duplex stream that allows you to send messages, including binary messages", 5 | "main": "index.js", 6 | "dependencies": { 7 | "borc": "^2.1.2", 8 | "streamx": "^2.7.2" 9 | }, 10 | "devDependencies": {}, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/mafintosh/binary-message-stream.git" 14 | }, 15 | "author": "Mathias Buus (@mafintosh)", 16 | "license": "MIT", 17 | "bugs": { 18 | "url": "https://github.com/mafintosh/binary-message-stream/issues" 19 | }, 20 | "homepage": "https://github.com/mafintosh/binary-message-stream" 21 | } 22 | --------------------------------------------------------------------------------