Permalink
Browse files

first

  • Loading branch information...
0 parents commit 3db189382d537f011c7aff79e418756dacff7cea @Raynos committed Aug 10, 2012
Showing with 203 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +3 −0 .travis.yml
  3. +19 −0 LICENCE
  4. +4 −0 Makefile
  5. +36 −0 README.md
  6. +80 −0 index.js
  7. +32 −0 package.json
  8. +26 −0 test.js
@@ -0,0 +1,3 @@
+node_modules
+*.log
+*.err
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - 0.8
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Raynos.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,4 @@
+test:
+ node test.js
+
+.PHONY: test
@@ -0,0 +1,36 @@
+# duplexer [![build status][1]][2]
+
+Creates a duplex stream
+
+Taken from [event-stream][3]
+
+## duplex (writeStream, readStream)
+
+Takes a writable stream and a readable stream and makes them appear as a readable writable stream.
+
+It is assumed that the two streams are connected to each other in some way.
+
+## Example
+
+ var grep = cp.exec('grep Stream')
+
+ duplex(grep.stdin, grep.stdout)
+
+## Installation
+
+`npm install duplexer`
+
+## Tests
+
+`make test`
+
+## Contributors
+
+ - Dominictarr
+ - Raynos
+
+## MIT Licenced
+
+ [1]: https://secure.travis-ci.org/Raynos/duplexer.png
+ [2]: http://travis-ci.org/Raynos/duplexer
+ [3]: https://github.com/dominictarr/event-stream#duplex-writestream-readstream
@@ -0,0 +1,80 @@
+var Stream = require("stream")
+ , writeMethods = ["write", "end", "destroy"]
+ , readMethods = ["resume", "pause"]
+ , readEvents = ["data", "close"]
+ , slice = Array.prototype.slice
+
+module.exports = duplex
+
+function duplex(writer, reader) {
+ var stream = new Stream()
+ , ended = false
+
+ Object.defineProperties(stream, {
+ writable: {
+ get: getWritable
+ }
+ , readable: {
+ get: getReadable
+ }
+ })
+
+ writeMethods.forEach(proxyWriter)
+
+ readMethods.forEach(proxyReader)
+
+ readEvents.forEach(proxyStream)
+
+ reader.on("end", handleEnd)
+
+ return stream
+
+ function getWritable() {
+ return writer.writable
+ }
+
+ function getReadable() {
+ return reader.readable
+ }
+
+ function proxyWriter(methodName) {
+ stream[methodName] = method
+
+ function method() {
+ return writer[methodName].apply(writer, arguments)
+ }
+ }
+
+ function proxyReader(methodName) {
+ stream[methodName] = method
+
+ function method() {
+ stream.emit(methodName)
+ var func = reader[methodName]
+ if (func) {
+ return func.apply(reader, arguments)
+ }
+ reader.emit(methodName)
+ }
+ }
+
+ function proxyStream(methodName) {
+ reader.on(methodName, reemit)
+
+ function reemit() {
+ var args = slice.call(arguments)
+ args.unshift(methodName)
+ stream.emit.apply(stream, args)
+ }
+ }
+
+ function handleEnd() {
+ if (ended) {
+ return
+ }
+ ended = true
+ var args = slice.call(arguments)
+ args.unshift("end")
+ stream.emit.apply(stream, args)
+ }
+}
@@ -0,0 +1,32 @@
+{
+ "name": "duplexer",
+ "version": "0.0.1",
+ "description": "Creates a duplex stream",
+ "keywords": [],
+ "author": "Raynos <raynos2@gmail.com>",
+ "repository": "git://github.com/Raynos/duplexer.git",
+ "main": "index",
+ "homepage": "https://github.com/Raynos/duplexer",
+ "contributors": [
+ {
+ "name": "Jake Verbaten"
+ }
+ ],
+ "bugs": {
+ "url": "https://github.com/Raynos/duplexer/issues",
+ "email": "raynos2@gmail.com"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "through": "~0.1.4"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "http://github.com/Raynos/duplexer/raw/master/LICENSE"
+ }
+ ],
+ "scripts": {
+ "test": "make node-test"
+ }
+}
@@ -0,0 +1,26 @@
+var duplex = require("./index")
+ , assert = require("assert")
+ , through = require("through")
+
+var readable = through()
+ , writable = through(write)
+ , written = 0
+ , data = 0
+
+var stream = duplex(writable, readable)
+
+function write() {
+ written++
+}
+
+stream.on("data", ondata)
+
+function ondata() {
+ data++
+}
+
+stream.write()
+readable.emit("data")
+
+assert.equal(written, 1)
+assert.equal(data, 1)

0 comments on commit 3db1893

Please sign in to comment.