This commit is contained in:
2024-11-03 17:16:20 +01:00
commit fd6412d6f2
8090 changed files with 778406 additions and 0 deletions

8
node_modules/memorystream/.npmignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
.project
.settings
.settings/*
.gitignore
node_modules/
.travis.yml
test-case/
.git/

23
node_modules/memorystream/Gruntfile.js generated vendored Normal file
View File

@@ -0,0 +1,23 @@
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
jshint: {
options: {
node: true
},
main : ["index.js"]
},
mochaTest: {
options: {
reporter: 'spec'
},
src: ['test/*.test.js']
}
});
grunt.registerTask('default', ['jshint:main', 'mochaTest']);
};

19
node_modules/memorystream/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,19 @@
Copyright (C) 2011 Dmitry Nizovtsev
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.

93
node_modules/memorystream/README.md generated vendored Normal file
View File

@@ -0,0 +1,93 @@
[![Build Status](https://travis-ci.org/JSBizon/node-memorystream.svg?branch=master)](https://travis-ci.org/JSBizon/node-memorystream)
# Introduction
node-memorystream - this module allow create streams in memory. It can be used for emulating file streams, filtering/mutating data between one stream and another, buffering incoming data, being the gap between two data/network streams of variable rates, etc. MemoryStream support read/write states or only read state or only write state. The API is meant to follow node's Stream implementation.
Module supports streams for node > 0.10 now.
Original module is here git://github.com/ollym/memstream.git was remade and improved.
## Installation
If you have npm installed, you can simply type:
npm install memorystream
Or you can clone this repository using the git command:
git clone git://github.com/JSBizon/node-memorystream.git
## Usage
Some examples how to use memorystream module.
#### Basic I/O Operation
In this example I illustrate the basic I/O operations of the memory stream.
var MemoryStream = require('memorystream');
var memStream = new MemoryStream(['Hello',' ']);
var data = '';
memStream.on('data', function(chunk) {
data += chunk.toString();
});
memStream.write('World');
memStream.on('end', function() {
// outputs 'Hello World!'
console.log(data);
});
memStream.end('!');
#### Piping
In this example I'm piping all data from the memory stream to the process's stdout stream.
var MemoryStream = require('memorystream');
var memStream = new MemoryStream();
memStream.pipe(process.stdout, { end: false });
memStream.write('Hello World!');
In this example I'm piping all data from the response stream to the memory stream.
var http = require('http'),
MemoryStream = require('memorystream');
var options = {
host: 'google.com'
};
var memStream = new MemoryStream(null, {
readable : false
});
var req = http.get(options, function(res) {
res.pipe(memStream);
res.on('end', function() {
console.log(memStream.toString());
});
});
#### Delayed Response
In the example below, we first pause the stream before writing the data to it. The stream is then resumed after 1 second, and the data is written to the console.
var MemoryStream = require('memorystream');
var memStream = new MemoryStream('Hello');
var data = '';
memStream.on('data', function(chunk) {
data += chunk;
});
memStream.pause();
memStream.write('World!');
setTimeout(function() {
memStream.resume();
}, 1000);
## Documentation
The memory stream adopts all the same methods and events as node's Stream implementation.
Documentation is [available here](http://github.com/JSBizon/node-memorystream/wiki/API/ "Documentation").

209
node_modules/memorystream/index.js generated vendored Normal file
View File

@@ -0,0 +1,209 @@
'use strict';
var STREAM = require('stream'),
UTIL = require('util'),
StringDecoder = require('string_decoder').StringDecoder;
function MemoryReadableStream(data, options) {
if (!(this instanceof MemoryReadableStream))
return new MemoryReadableStream(data, options);
MemoryReadableStream.super_.call(this, options);
this.init(data, options);
}
UTIL.inherits(MemoryReadableStream, STREAM.Readable);
function MemoryWritableStream(data, options) {
if (!(this instanceof MemoryWritableStream))
return new MemoryWritableStream(data, options);
MemoryWritableStream.super_.call(this, options);
this.init(data, options);
}
UTIL.inherits(MemoryWritableStream, STREAM.Writable);
function MemoryDuplexStream(data, options) {
if (!(this instanceof MemoryDuplexStream))
return new MemoryDuplexStream(data, options);
MemoryDuplexStream.super_.call(this, options);
this.init(data, options);
}
UTIL.inherits(MemoryDuplexStream, STREAM.Duplex);
MemoryReadableStream.prototype.init =
MemoryWritableStream.prototype.init =
MemoryDuplexStream.prototype.init = function init (data, options) {
var self = this;
this.queue = [];
if (data) {
if (!Array.isArray(data)) {
data = [ data ];
}
data.forEach(function (chunk) {
if (!(chunk instanceof Buffer)) {
chunk = new Buffer(chunk);
}
self.queue.push(chunk);
});
}
options = options || {};
this.maxbufsize = options.hasOwnProperty('maxbufsize') ? options.maxbufsize
: null;
this.bufoverflow = options.hasOwnProperty('bufoverflow') ? options.bufoverflow
: null;
this.frequence = options.hasOwnProperty('frequence') ? options.frequence
: null;
};
function MemoryStream (data, options) {
if (!(this instanceof MemoryStream))
return new MemoryStream(data, options);
options = options || {};
var readable = options.hasOwnProperty('readable') ? options.readable : true,
writable = options.hasOwnProperty('writable') ? options.writable : true;
if (readable && writable) {
return new MemoryDuplexStream(data, options);
} else if (readable) {
return new MemoryReadableStream(data, options);
} else if (writable) {
return new MemoryWritableStream(data, options);
} else {
throw new Error("Unknown stream type Readable, Writable or Duplex ");
}
}
MemoryStream.createReadStream = function (data, options) {
options = options || {};
options.readable = true;
options.writable = false;
return new MemoryStream(data, options);
};
MemoryStream.createWriteStream = function (data, options) {
options = options || {};
options.readable = false;
options.writable = true;
return new MemoryStream(data, options);
};
MemoryReadableStream.prototype._read =
MemoryDuplexStream.prototype._read = function _read (n) {
var self = this,
frequence = self.frequence || 0,
wait_data = this instanceof STREAM.Duplex && ! this._writableState.finished ? true : false;
if ( ! this.queue.length && ! wait_data) {
this.push(null);// finish stream
} else if (this.queue.length) {
setTimeout(function () {
if (self.queue.length) {
var chunk = self.queue.shift();
if (chunk && ! self._readableState.ended) {
if ( ! self.push(chunk) ) {
self.queue.unshift(chunk);
}
}
}
}, frequence);
}
};
MemoryWritableStream.prototype._write =
MemoryDuplexStream.prototype._write = function _write (chunk, encoding, cb) {
var decoder = null;
try {
decoder = this.decodeStrings && encoding ? new StringDecoder(encoding) : null;
} catch (err){
return cb(err);
}
var decoded_chunk = decoder ? decoder.write(chunk) : chunk,
queue_size = this._getQueueSize(),
chunk_size = decoded_chunk.length;
if (this.maxbufsize && (queue_size + chunk_size) > this.maxbufsize ) {
if (this.bufoverflow) {
return cb("Buffer overflowed (" + this.bufoverflow + "/" + queue_size + ")");
} else {
return cb();
}
}
if (this instanceof STREAM.Duplex) {
while (this.queue.length) {
this.push(this.queue.shift());
}
this.push(decoded_chunk);
} else {
this.queue.push(decoded_chunk);
}
cb();
};
MemoryDuplexStream.prototype.end = function (chunk, encoding, cb) {
var self = this;
return MemoryDuplexStream.super_.prototype.end.call(this, chunk, encoding, function () {
self.push(null);//finish readble stream too
if (cb) cb();
});
};
MemoryReadableStream.prototype._getQueueSize =
MemoryWritableStream.prototype._getQueueSize =
MemoryDuplexStream.prototype._getQueueSize = function () {
var queuesize = 0, i;
for (i = 0; i < this.queue.length; i++) {
queuesize += Array.isArray(this.queue[i]) ? this.queue[i][0].length
: this.queue[i].length;
}
return queuesize;
};
MemoryWritableStream.prototype.toString =
MemoryDuplexStream.prototype.toString =
MemoryReadableStream.prototype.toString =
MemoryWritableStream.prototype.getAll =
MemoryDuplexStream.prototype.getAll =
MemoryReadableStream.prototype.getAll = function () {
var self = this,
ret = '';
this.queue.forEach(function (data) {
ret += data;
});
return ret;
};
MemoryWritableStream.prototype.toBuffer =
MemoryDuplexStream.prototype.toBuffer =
MemoryReadableStream.prototype.toBuffer = function () {
var buffer = new Buffer(this._getQueueSize()),
currentOffset = 0;
this.queue.forEach(function (data) {
var data_buffer = data instanceof Buffer ? data : new Buffer(data);
data_buffer.copy(buffer, currentOffset);
currentOffset += data.length;
});
return buffer;
};
module.exports = MemoryStream;

39
node_modules/memorystream/package.json generated vendored Normal file
View File

@@ -0,0 +1,39 @@
{
"name": "memorystream",
"description": "This is lightweight memory stream module for node.js.",
"version": "0.3.1",
"keywords": [
"memory",
"test",
"stream",
"tools",
"streams",
"buffer"
],
"scripts": {
"test": "grunt"
},
"devDependencies" : {
"expect.js" : "~0.2.0",
"mocha" : "~1.20.0",
"grunt": "~0.4",
"grunt-cli": "~0.1.13",
"grunt-mocha-test" : "~0.12.2",
"grunt-contrib-jshint" : "~0.10.0",
"q": "~1.0.1"
},
"author": "Dmitry Nizovtsev (https://github.com/JSBizon)",
"contributors": [
{ "name": "Dmitry Nizovtsev", "email": "dmitryp3@gmail.com" }
],
"repository" : {
"type" : "git",
"url" : "https://github.com/JSBizon/node-memorystream.git"
},
"homepage": "https://github.com/JSBizon/node-memorystream",
"engines": { "node": ">= 0.10.0" },
"licenses": [ {
"type": "MIT",
"url" : "http://github.com/JSBizon/node-memorystream/raw/master/LICENSE"
} ]
}

18
node_modules/memorystream/test/example.js generated vendored Normal file
View File

@@ -0,0 +1,18 @@
var http = require('http'),
MemoryStream = require('../index'),
util = require('util');
var options = {
host: 'google.com'
};
var memStream = new MemoryStream(null,{
readable : false
});
var req = http.request(options, function(res) {
util.pump(res, memStream);
res.on('end',function(){
console.log(memStream.toString());
});
});
req.end();

333
node_modules/memorystream/test/memorystream.test.js generated vendored Normal file
View File

@@ -0,0 +1,333 @@
var MemoryStream = require('../index.js'),
expect = require('expect.js'),
STREAM = require('stream'),
Q = require('q'),
FS = require('fs');
describe('Test memory streams', function() {
var writeToStream = function (mem_stream, test_data, frequency) {
var result = Q(),
i = 0;
frequency = frequency || 0;
test_data.forEach(function (chunk) {
var f = Q.nfbind(function (chunk,n, cb) {
setTimeout(function () {
if (n >= (test_data.length - 1) ) {
mem_stream.end(chunk);
} else {
mem_stream.write(chunk, cb);
}
}, frequency);
}, chunk ,i++);
result = result.then(function() { return f(); });
});
result.done();
};
var writeToStream2 = function (mem_stream, test_data) {
var i;
for (i = 0; i < test_data.length ; i++) {
setTimeout((function(n) {
return function () {
if (n >= (test_data.length - 1) ) {
mem_stream.end(test_data[n]);
} else {
mem_stream.write(test_data[n]);
}
}
})(i), i * 2);
}
};
describe("constructor", function() {
it('should have a MemoryStream class', function () {
expect(MemoryStream).to.be.ok();
});
it('should create Readable stream', function () {
var memory_stream = new MemoryStream([], {writable : false});
expect(memory_stream).to.be.ok();
expect(memory_stream).to.be.a(STREAM.Readable);
memory_stream = MemoryStream.createReadStream([]);
expect(memory_stream).to.be.a(STREAM.Readable);
});
it('should create Writable stream', function () {
var memory_stream = new MemoryStream([], {readable : false});
expect(memory_stream).to.be.ok();
expect(memory_stream).to.be.a(STREAM.Writable);
memory_stream = MemoryStream.createWriteStream([]);
expect(memory_stream).to.be.a(STREAM.Writable);
});
it('should create Duplex stream', function () {
var memory_stream = new MemoryStream([]);
expect(memory_stream).to.be.ok();
expect(memory_stream).to.be.a(STREAM.Duplex);
});
});
describe("readable stream", function () {
var test_data = 'abcdefghijklmnopqrstuvwxyz',
frequence = 50;
it("should read data from stream", function (done) {
var mem_stream = MemoryStream.createReadStream(test_data.split(''));
var data = '', chunks = 0;
mem_stream.on('data',function(chunk){
data += chunk;
++chunks;
});
mem_stream.on('end',function () {
expect(chunks).to.be(test_data.length);
expect(data).to.be(test_data);
done();
});
});
it("should read data from stream with frequency", function (done) {
var mem_stream = new MemoryStream(test_data.split(''), {
writable : false,
frequence: frequence
});
var start_time = Date.now();
var data = '';
mem_stream.on('data',function(chunk){
data += chunk;
});
mem_stream.on('end',function(){
var execution_time = Date.now() - start_time;
expect(data).to.be(test_data);
expect(execution_time >= frequence * test_data.length).to.be(true);
done();
});
});
it("should read data pause/resume", function (done) {
var mem_stream = MemoryStream.createReadStream(test_data.split(''));
var start_time = Date.now();
var data = '', chunks = 0;
mem_stream.on('data',function(chunk){
data += chunk;
++chunks;
if (! (chunks % 10) ) {
mem_stream.pause();
setTimeout(function () {
mem_stream.resume();
},frequence);
}
});
mem_stream.on('end',function() {
var execution_time = Date.now() - start_time;
expect(data).to.be(test_data);
expect(execution_time >= frequence).to.be(true);
done();
});
});
});
describe("writable stream", function () {
var test_data = 'abcdefghijklmnopqrstuvwxyz';
it("should write data to Writable", function (done) {
var mem_stream = MemoryStream.createWriteStream(),
i = 0;
writeToStream(mem_stream, test_data.split(''));
mem_stream.on('finish',function () {
expect(mem_stream.toString()).to.be(test_data);
done();
});
});
it("should not write data to readable stream", function (done) {
var mem_stream = new MemoryStream([], {writable : false});
expect(function () {
mem_stream.write("test");
}).to.throwError();
expect(function () {
mem_stream.end("test");
}).to.throwError();
done();
});
it("#toString", function (done) {
var mem_stream = new MemoryStream(null, {readable : false});
writeToStream(mem_stream, test_data.split(''));
mem_stream.on('finish',function () {
expect(mem_stream.toString()).to.be(test_data);
done();
});
});
it("#toBuffer", function (done) {
var mem_stream = new MemoryStream(null, {readable : false});
writeToStream(mem_stream, test_data.split(''));
mem_stream.on('finish',function () {
expect(mem_stream.toBuffer().toString('utf-8')).to.be(test_data);
done();
});
});
it("#toBuffer all data in one buffer", function (done) {
var i = 0,
mem_stream = new MemoryStream(null, {readable : false}),
arr_test_data = [],
str_test_data = '';
for (i = 0; i < 20; i++) {
var b = new Buffer([i]);
arr_test_data.push(b);
str_test_data += b.toString('hex');
}
writeToStream(mem_stream, arr_test_data, 10);
mem_stream.on('finish',function () {
expect(mem_stream.toBuffer().toString('hex')).to.be(str_test_data);
done();
});
});
it("not write data to the overflowed buffer", function (done) {
var mem_stream = new MemoryStream('data1'.split(''), {
readable : false,
maxbufsize : 10
});
mem_stream.write('data2', function (err) {
expect(err).to.not.be.ok();
expect(mem_stream.toString()).to.be('data1data2');
mem_stream.write('data3', function (err) {
expect(err).to.not.be.ok();
expect(mem_stream.toString()).to.be('data1data2');
done();
});
});
});
it("should process error for overflowed buffer", function (done) {
var mem_stream = new MemoryStream('data1'.split(''), {
readable : false,
maxbufsize : 10,
bufoverflow : true
});
mem_stream.write('data2', function (err) {
expect(err).to.not.be.ok();
expect(mem_stream.toString()).to.be('data1data2');
mem_stream.write('data3', function (err) {
expect(err).to.be.ok();
expect(mem_stream.toString()).to.be('data1data2');
done();
});
});
mem_stream.on('error', function () {
});
});
});
describe("duplex stream", function () {
var test_data = 'abcdefghijklmnopqrstuvwxyz';
it("should write/read",function (done) {
var mem_stream = new MemoryStream();
var data = '';
mem_stream.on('data',function(chunk){
data += chunk;
});
writeToStream(mem_stream, test_data.split(''));
mem_stream.on('end', function () {
expect(data).to.be(test_data);
done();
});
});
it("should write/read data with init buffer", function (done) {
var l = Math.floor(test_data.length / 2);
var test_data1 = test_data.substr(0, l),
test_data2 = test_data.substr(l);
var mem_stream = new MemoryStream(test_data1.split(''));
var data = '';
mem_stream.on('data',function(chunk){
data += chunk;
});
writeToStream2(mem_stream, test_data2);
mem_stream.on('end', function() {
expect(data).to.be(test_data);
done();
});
});
it("should piping data", function (done) {
var src_mem_stream = MemoryStream.createReadStream(test_data.split(''), {frequency : 25});
var dst_mem_stream = MemoryStream.createWriteStream();
src_mem_stream.pipe(dst_mem_stream);
dst_mem_stream.on('finish',function(){
expect(dst_mem_stream.toString()).to.be(test_data);
done();
});
});
it("should readable/piping data", function (done) {
var src_mem_stream = MemoryStream.createReadStream(test_data.split(''), {frequency : 25});
var dst_mem_stream = MemoryStream.createWriteStream();
src_mem_stream.once('readable', function () {
src_mem_stream.pipe(dst_mem_stream);
});
dst_mem_stream.on('finish',function(){
expect(dst_mem_stream.toString()).to.be(test_data);
done();
});
});
});
});