Skip to content
Browse files

code import

  • Loading branch information...
1 parent 09ab5e0 commit eb19fcd1f910fce8262491087ccd0ff96a95b091 @deoxxa committed
Showing with 228 additions and 0 deletions.
  1. +26 −0 LICENSE.md
  2. +68 −0 README.md
  3. +24 −0 example.js
  4. +89 −0 index.js
  5. +21 −0 package.json
View
26 LICENSE.md
@@ -0,0 +1,26 @@
+Copyright (c) 2012, Deoxxa Development
+======================================
+All rights reserved.
+--------------------
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of Deoxxa Development nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY DEOXXA DEVELOPMENT ''AS IS'' AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL DEOXXA DEVELOPMENT BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
68 README.md
@@ -0,0 +1,68 @@
+steez
+=====
+
+The g of a mack and the steez of a geezer.
+
+Overview
+--------
+
+Implementing `pause()`/`resume()` semantics every time you write some code that
+you want to use as a stream is annoying. steez tries to make this a thing of the
+past by providing a properly extendable base class with some transparent magic
+built in to make your stream behave nicely. When you `.emit("data", ...)`, steez
+will trap the event and queue it up for later if your stream is paused. It will
+also handle all the requisite events and methods for proper `pipe()` usage.
+
+Installation
+------------
+
+Available via [npm](http://npmjs.org/):
+
+> $ npm install steez
+
+Or via git:
+
+> $ git clone git://github.com/deoxxa/node-steez.git node_modules/steez
+
+Usage
+-----
+
+```javascript
+#!/usr/bin/env node
+
+var fs = require("fs"),
+ util = require("util");
+
+var Steez = require("./index");
+
+function ChunkReverser() {
+ Steez.call(this);
+}
+util.inherits(ChunkReverser, Steez);
+
+ChunkReverser.prototype.write = function write(data) {
+ this.emit("data", Buffer(data.toString().split("").reverse().join("")));
+
+ return this.writable;
+};
+
+var chunk_reverser = new ChunkReverser();
+
+var a = fs.createReadStream("./data.in"),
+ b = fs.createWriteStream("./data.out");
+
+a.pipe(chunk_reverser).pipe(b);
+```
+
+License
+-------
+
+3-clause BSD. A copy is included with the source.
+
+Contact
+-------
+
+* GitHub ([deoxxa](http://github.com/deoxxa))
+* Twitter ([@deoxxa](http://twitter.com/deoxxa))
+* ADN ([@deoxxa](https://alpha.app.net/deoxxa))
+* Email ([deoxxa@fknsrs.biz](mailto:deoxxa@fknsrs.biz))
View
24 example.js
@@ -0,0 +1,24 @@
+#!/usr/bin/env node
+
+var fs = require("fs"),
+ util = require("util");
+
+var Steez = require("./index");
+
+function ChunkReverser() {
+ Steez.call(this);
+}
+util.inherits(ChunkReverser, Steez);
+
+ChunkReverser.prototype.write = function write(data) {
+ this.emit("data", Buffer(data.toString().split("").reverse().join("")));
+
+ return this.writable;
+};
+
+var chunkreverser = new ChunkReverser();
+
+var a = fs.createReadStream("./data.in"),
+ b = fs.createWriteStream("./data.out");
+
+a.pipe(chunkreverser).pipe(b);
View
89 index.js
@@ -0,0 +1,89 @@
+var stream = require("stream"),
+ util = require("util");
+
+var Steez = module.exports = function Steez() {
+ stream.Stream.call(this);
+
+ this.writable = true;
+ this.readable = true;
+
+ this.paused = false;
+ this.closing = false;
+ this.closed = false;
+
+ this.queue = [];
+
+ this.on("drain", this.flush_queue.bind(this));
+}
+
+util.inherits(Steez, stream.Stream);
+
+// hijacking emit() to re-route "data" events
+Steez.prototype._emit = Steez.prototype.emit;
+Steez.prototype.emit = function emit() {
+ if (arguments[0] === "data") {
+ if (this.closed || this.closing) {
+ return this;
+ }
+
+ if (this.paused) {
+ this.queue.push(arguments[1]);
+ return this;
+ }
+ }
+
+ return this._emit.apply(this, arguments);
+};
+
+Steez.prototype.write = function write(data) {
+ this.emit("data", data);
+
+ return this.writable;
+};
+
+Steez.prototype.end = function end(data) {
+ if (data) {
+ this.write(data);
+ }
+
+ this.writable = false;
+ this.closing = true;
+
+ this.destroySoon();
+};
+
+Steez.prototype.destroySoon = function destroySoon() {
+ if (this.queue.length === 0) {
+ this.destroy();
+ } else {
+ this.once("drain", this.destroy.bind(this));
+ }
+};
+
+Steez.prototype.destroy = function destroy() {
+ this.closing = false;
+ this.closed = true;
+ this.readable = false;
+ this.writable = false;
+
+ this.emit("close");
+};
+
+Steez.prototype.pause = function pause() {
+ this.writable = false;
+ this.paused = true;
+};
+
+Steez.prototype.resume = function resume() {
+ if (!this.closed && !this.closing && this.paused) {
+ this.paused = false;
+ this.emit("drain");
+ this.writable = true;
+ }
+};
+
+Steez.prototype.flush_queue = function flush_queue() {
+ while (this.queue.length && !this.paused) {
+ this._emit("data", this.queue.shift());
+ }
+};
View
21 package.json
@@ -0,0 +1,21 @@
+{
+ "name": "steez",
+ "version": "0.0.0",
+ "description": "The g of a mack and the steez of a geezer.",
+ "main": "index.js",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/deoxxa/steez.git"
+ },
+ "keywords": [
+ "stream",
+ "template",
+ "buffer",
+ "queue",
+ "automatic",
+ "pipe",
+ "wrapper"
+ ],
+ "author": "Conrad Pankoff <deoxxa@fknsrs.biz> (http://www.fknsrs.biz/)",
+ "license": "BSD"
+}

0 comments on commit eb19fcd

Please sign in to comment.
Something went wrong with that request. Please try again.