├── .gitignore ├── .travis.yml ├── test └── api-test.js ├── package.json ├── lib └── stream-pair.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.12" 4 | - "iojs" 5 | -------------------------------------------------------------------------------- /test/api-test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var streamPair = require('../'); 4 | 5 | describe('StreamPair', function() { 6 | var pair = null; 7 | 8 | beforeEach(function() { 9 | pair = streamPair.create(); 10 | }); 11 | 12 | it('should send data to other side', function(done) { 13 | pair.write('hello'); 14 | assert.equal(pair.other.read().toString(), 'hello'); 15 | 16 | pair.other.on('end', done); 17 | pair.end(function() { 18 | pair.other.resume(); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stream-pair", 3 | "version": "1.0.3", 4 | "description": "Create two coupled streams", 5 | "main": "lib/stream-pair.js", 6 | "scripts": { 7 | "test": "mocha --reporter=spec test/*-test.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+ssh://git@github.com/indutny/stream-pair.git" 12 | }, 13 | "keywords": [ 14 | "Stream", 15 | "Socketpair", 16 | "Pair", 17 | "Coupled" 18 | ], 19 | "author": "Fedor Indutny ", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/indutny/stream-pair/issues" 23 | }, 24 | "homepage": "https://github.com/indutny/stream-pair#readme", 25 | "devDependencies": { 26 | "mocha": "^2.2.5" 27 | }, 28 | "optionalDependencies": { 29 | "readable-stream": "^2.0.1" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /lib/stream-pair.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var util = require('util'); 4 | var stream; 5 | try { 6 | stream = require('readable-stream'); 7 | } catch (e) { 8 | stream = require('stream'); 9 | } 10 | var Duplex = stream.Duplex; 11 | var PassThrough = stream.PassThrough; 12 | 13 | function Side() { 14 | Duplex.call(this); 15 | 16 | this.buffer = new PassThrough(); 17 | 18 | this.other = null; 19 | this.once('finish', function() { 20 | this.other.buffer.end(); 21 | }); 22 | 23 | var self = this; 24 | this.buffer.once('end', function() { 25 | self.push(null); 26 | }); 27 | } 28 | util.inherits(Side, Duplex); 29 | 30 | Side.prototype._read = function _read() { 31 | var chunk = this.buffer.read(); 32 | if (chunk) 33 | return this.push(chunk); 34 | 35 | // Retry once there will be data on other stream 36 | var self = this; 37 | this.buffer.once('readable', function() { 38 | self._read(); 39 | }); 40 | }; 41 | 42 | Side.prototype._write = function _write(data, enc, cb) { 43 | this.other.buffer.write(data, enc, cb); 44 | }; 45 | 46 | exports.create = function create() { 47 | var a = new Side(); 48 | var b = new Side(); 49 | 50 | a.name = 'self'; 51 | b.name = 'other'; 52 | a.other = b; 53 | b.other = a; 54 | 55 | return a; 56 | }; 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # StreamPair 2 | [![Build Status](https://secure.travis-ci.org/indutny/stream-pair.png)](http://travis-ci.org/indutny/stream-pair) 3 | [![NPM version](https://badge.fury.io/js/stream-pair.svg)](http://badge.fury.io/js/stream-pair) 4 | 5 | A pair of coupled streams 6 | 7 | ## Usage 8 | 9 | ```javascript 10 | var streamPair = require('stream-pair'); 11 | 12 | var pair = streamPair.create(); 13 | pair.write('123'); 14 | console.log(pair.other.read()); 15 | ``` 16 | 17 | ## LICENSE 18 | 19 | This software is licensed under the MIT License. 20 | 21 | Copyright Fedor Indutny, 2013. 22 | 23 | Permission is hereby granted, free of charge, to any person obtaining a 24 | copy of this software and associated documentation files (the 25 | "Software"), to deal in the Software without restriction, including 26 | without limitation the rights to use, copy, modify, merge, publish, 27 | distribute, sublicense, and/or sell copies of the Software, and to permit 28 | persons to whom the Software is furnished to do so, subject to the 29 | following conditions: 30 | 31 | The above copyright notice and this permission notice shall be included 32 | in all copies or substantial portions of the Software. 33 | 34 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 35 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 36 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 37 | NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 38 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 39 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 40 | USE OR OTHER DEALINGS IN THE SOFTWARE. 41 | 42 | [0]: http://json.org/ 43 | [1]: http://github.com/indutny/bud-backend 44 | [2]: https://github.com/nodejs/io.js 45 | [3]: https://github.com/libuv/libuv 46 | [4]: http://openssl.org/ 47 | --------------------------------------------------------------------------------