Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit ab111846eedd98ac8673895fa8815a172c73a386 0 parents
@pofallon authored
Showing with 153 additions and 0 deletions.
  1. +3 −0  .gitignore
  2. +118 −0 index.js
  3. +22 −0 package.json
  4. +10 −0 test/test.js
3  .gitignore
@@ -0,0 +1,3 @@
+node_modules/**/*
+**/*.swp
+*.sublime-*
118 index.js
@@ -0,0 +1,118 @@
+var Stream = require('stream').Stream;
+var util = require('util');
+
+util.inherits(LoremIpStream, Stream);
+
+function LoremIpStream(options) {
+
+ if (!options) {
+ options = {};
+ }
+
+ this.readable = true;
+ this.streamEncoding = null;
+ this.charSent = 0;
+ this.position = 0;
+ this.intervalId = null;
+
+ this.dataInterval = options.dataInterval || 100;
+ this.charLength = options.charLength || lorem.length;
+ this.chunkSize = options.chunkSize || lorem.length * 10;
+
+ //TODO: Add more options?
+
+ this.resume();
+
+}
+
+LoremIpStream.prototype.setEncoding = function(encoding) {
+
+ if (encoding.match(/^utf8|ascii|base64$/)) {
+ this.streamEncoding = encoding;
+ } else {
+ this.emit('error', new Error('Invalid string encoding'));
+ }
+
+};
+
+LoremIpStream.prototype.pause = function() {
+
+ if (this.intervalId) {
+ clearInterval(this.intervalId);
+ this.intervalId = null;
+ }
+
+};
+
+LoremIpStream.prototype.resume = function() {
+
+ var that = this;
+
+ if (!this.intervalId) {
+
+ this.intervalId = setInterval(function() {
+
+ if (that.charSent >= that.charLength) {
+
+ that.emit('end');
+ that.destroy();
+
+ } else {
+
+ var remaining = that.charLength - that.charSent;
+
+ var bufferSize = (remaining > that.chunkSize) ? that.chunkSize : remaining;
+
+ var theBuffer = new Buffer(bufferSize);
+
+ for (var i=0; i<bufferSize; i++) {
+ theBuffer[i] = that._getNextChar();
+ }
+
+ if (that.streamEncoding) {
+ that.emit('data', theBuffer.toString(that.streamEncoding));
+ } else {
+ that.emit('data',theBuffer);
+ }
+
+ that.charSent += bufferSize;
+
+ }
+
+ }, this.dataInterval);
+
+ }
+
+};
+
+LoremIpStream.prototype.destroy = function() {
+
+ if (this.intervalId) {
+ clearInterval(this.intervalId);
+ this.intervalId = null;
+ }
+
+};
+
+LoremIpStream.prototype.destroySoon = function() {
+
+ this.destroy();
+
+};
+
+LoremIpStream.prototype._getNextChar = function() {
+
+ if (this.position > lorem.length) {
+ this.position = 0;
+ }
+
+ return(lorem.charCodeAt(this.position++));
+
+}
+
+module.exports = LoremIpStream;
+
+var lorem = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ';
+ lorem += 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ';
+ lorem += 'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ';
+ lorem += 'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ';
22 package.json
@@ -0,0 +1,22 @@
+{
+ "name": "loremipstream",
+ "description": "A configurable readable stream that generates Lorem ipsum. Helpful in stream testing.",
+ "keywords": ["stream", "testing", "latin"],
+ "author": "Paul O'Fallon <paul@ofallonfamily.com>",
+ "homepage": "https://github.com/pofallon/node-loremipstream",
+ "dependencies": {},
+ "devDependencies": {
+ "mocha": "0.x.x",
+ "should": "0.x.x",
+ "memorystream" : "0.x.x",
+ },
+ "engine": "node >= 0.4.1",
+ "version": "0.0.1",
+ "files": [
+ "index.js",
+ "package.json",
+ "README.md",
+ "test"
+ ],
+ "main": "index"
+}
10 test/test.js
@@ -0,0 +1,10 @@
+var LoremIpStream = require('../index.js');
+
+var lorem = new LoremIpStream();
+
+lorem.on('end', function() {
+ console.log('');
+ console.log(this.charSent + ' characters sent');
+});
+
+lorem.pipe(process.stdout);
Please sign in to comment.
Something went wrong with that request. Please try again.