Socket
Socket
Sign inDemoInstall

string-stream

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.4 to 0.0.5

66

index.js
var Stream = require('stream'),
inherits = require('util').inherits,
Buffer = require('buffer').Buffer;
function StringStream(init) {
Stream.super_.call(this);
this._data = init || '';
}
inherits(StringStream, Stream);
/**
* Implements a readable stream interface to a string or a buffer.
* @param {String} data A initial string to fill the buffer.
*/
function Readable(data) {
Readable.super_.call(this);
/*! The implementation works with a buffer internally. */
if (!Buffer.isBuffer(data)) {
/*! In case that we don't get a buffer as argument we convert it into a
* unicode string. */
this._data = new Buffer(data);
} else {
this._data = data;
}
};
inherits(Readable, Stream);
/**
* Reads the given n bytes from the stream.

@@ -32,13 +15,10 @@ * @param {Number} n Number of bytes to read.

Readable.prototype.read = function (n) {
StringStream.prototype.read = function (n) {
var chunk;
n = n || -1;
n = (n == null || n === -1) ? undefined : n;
chunk = this._data.slice(0, n);
/*! All read bytes are removed from the buffer. */
/*! We must take care: The buffer doesn't like .slice(0, -1) */
if (n === -1) {
chunk = this._data;
} else {
chunk = this._data.slice(0, n);
}
this._data = this._data.slice(n);
if (n >= this._data.length || n === -1) this.emit('end');

@@ -54,27 +34,15 @@ return chunk;

Readable.prototype.pipe = function (dest) {
dest.write(this.read());
dest.end();
StringStream.prototype.pipe = function (dest) {
dest.end(this.read());
};
/**
* The write end to a stream buffer.
*/
function Writable() {
Writable.super_.call(this);
this._data = '';
}
inherits(Writable, Stream);
Writable.prototype.write = function (data) {
StringStream.prototype.write = function (data) {
this._data += data;
};
Writable.prototype.end = function () {
StringStream.prototype.end = function () {
this.write.apply(this, arguments);
this.emit('end');
};
Writable.prototype.toString = function () {
StringStream.prototype.toString = function () {
return this._data;
};
exports.Readable = Readable;
exports.Writable = Writable;
module.exports = StringStream;
{
"name": "string-stream",
"version": "0.0.4",
"version": "0.0.5",
"description": "A stream that works on a string.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -1,32 +0,15 @@

var Readable = require('..').Readable,
Writable = require('..').Writable,
var Stream = require('..'),
Pipe = require('pipette').Pipe;
require('should');
describe('Readable stream', function () {
describe('Stream', function () {
it('should work', function (done) {
var stream = new Readable('Test buffer'),
pipe = new Pipe();
stream.on('end', function (data) {
var readStream = new Stream('Test buffer'),
writeStream = new Stream();
writeStream.on('end', function () {
writeStream.toString().should.equal('Test Test buffer');
done();
});
pipe.reader.on('error', done);
pipe.reader.on('data', function (data) {
try {
data.toString().should.equal('Test buffer');
} catch (x) {
done(x);
}
});
stream.pipe(pipe.writer);
writeStream.write('Test ');
readStream.pipe(writeStream);
});
});
describe('Writable stream', function () {
it('should write the given data', function (done) {
var stream = new Writable();
stream.on('end', function () {
stream.toString().should.equal('Test content');
done();
});
stream.end('Test content');
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc