Permalink
Browse files

first

  • Loading branch information...
0 parents commit 42ef2733aa615de16d05502042b7c98114f0e056 @Raynos committed Sep 8, 2012
Showing with 170 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +19 −0 LICENCE
  3. 0 Makefile
  4. +15 −0 README.md
  5. +49 −0 examples/to.js
  6. +54 −0 index.js
  7. +30 −0 package.json
3 .gitignore
@@ -0,0 +1,3 @@
+node_modules
+*.log
+*.err
19 LICENCE
@@ -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 Makefile
No changes.
15 README.md
@@ -0,0 +1,15 @@
+# write-stream
+
+Base class for writable streams
+
+## Example
+
+## Installation
+
+`npm install write-stream`
+
+## Contributors
+
+ - Raynos
+
+## MIT Licenced
49 examples/to.js
@@ -0,0 +1,49 @@
+var to = require("..")
+ , Stream = require("readable-stream")
+
+var out1 = to([], function (list) {
+ console.log("out", list)
+})
+
+createInput().pipe(out1)
+
+var buffer2 = []
+var out2 = to(function write(chunk) {
+ buffer2.push(chunk)
+}, function end() {
+ console.log("out", buffer2)
+})
+
+createInput().pipe(out2)
+
+var buffer3 = []
+var out3 = to(function write(chunk) {
+ var stream = this
+ buffer3.push(chunk)
+ setTimeout(function () {
+ stream.emit("drain")
+ }, 500)
+ return false
+}, function end() {
+ console.log("out", buffer3)
+})
+
+createInput().pipe(out3)
+
+function createInput() {
+ var s = new Stream()
+ , count = 0
+
+ s.read = function () {
+ if (++count < 5) {
+ return count
+ } else {
+ s.end()
+ }
+ }
+ s.end = function () {
+ s.emit("end")
+ }
+
+ return s
+}
54 index.js
@@ -0,0 +1,54 @@
+"use strict";
+
+var Stream = require("stream")
+
+module.exports = to
+
+function to(write, end) {
+ if (Array.isArray(write)) {
+ return to(writeArray, endArray)
+ }
+
+ var stream = new Stream()
+ , ended = false
+
+ end = end || defaultEnd
+
+ stream.readable = false
+ stream.writable = true
+
+ stream.write = handleWrite
+ stream.end = handleEnd
+
+ return stream
+
+ function writeArray(chunk) {
+ write.push(chunk)
+ }
+
+ function endArray() {
+ end.call(this, write)
+ this.emit("end")
+ }
+
+ function handleWrite(chunk) {
+ var result = write.call(stream, chunk)
+ return result === false ? false : true
+ }
+
+ function handleEnd(chunk) {
+ if (ended) {
+ return
+ }
+ ended = true
+ if (arguments.length) {
+ stream.write(chunk)
+ }
+ this.writable = false
+ end.call(stream)
+ }
+}
+
+function defaultEnd() {
+ this.emit("end")
+}
30 package.json
@@ -0,0 +1,30 @@
+{
+ "name": "write-stream",
+ "version": "0.0.1",
+ "description": "Base class for writable streams",
+ "keywords": [],
+ "author": "Raynos <raynos2@gmail.com>",
+ "repository": "git://github.com/Raynos/write-stream.git",
+ "main": "index",
+ "homepage": "https://github.com/Raynos/write-stream",
+ "contributors": [
+ {
+ "name": "Jake Verbaten"
+ }
+ ],
+ "bugs": {
+ "url": "https://github.com/Raynos/write-stream/issues",
+ "email": "raynos2@gmail.com"
+ },
+ "dependencies": {
+ "readable-stream": "0.0.1"
+ },
+ "devDependencies": {},
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "http://github.com/Raynos/write-stream/raw/master/LICENSE"
+ }
+ ],
+ "scripts": {}
+}

0 comments on commit 42ef273

Please sign in to comment.