From e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d Mon Sep 17 00:00:00 2001 From: Piotr Russ Date: Mon, 16 Nov 2020 00:10:28 +0100 Subject: api, login, auth --- node_modules/memorystream/.npmignore | 8 + node_modules/memorystream/Gruntfile.js | 23 ++ node_modules/memorystream/LICENSE | 19 ++ node_modules/memorystream/README.md | 93 ++++++ node_modules/memorystream/index.js | 209 +++++++++++++ node_modules/memorystream/package.json | 77 +++++ node_modules/memorystream/test/example.js | 18 ++ .../memorystream/test/memorystream.test.js | 333 +++++++++++++++++++++ 8 files changed, 780 insertions(+) create mode 100644 node_modules/memorystream/.npmignore create mode 100644 node_modules/memorystream/Gruntfile.js create mode 100644 node_modules/memorystream/LICENSE create mode 100644 node_modules/memorystream/README.md create mode 100644 node_modules/memorystream/index.js create mode 100644 node_modules/memorystream/package.json create mode 100644 node_modules/memorystream/test/example.js create mode 100644 node_modules/memorystream/test/memorystream.test.js (limited to 'node_modules/memorystream') diff --git a/node_modules/memorystream/.npmignore b/node_modules/memorystream/.npmignore new file mode 100644 index 0000000..dba2495 --- /dev/null +++ b/node_modules/memorystream/.npmignore @@ -0,0 +1,8 @@ +.project +.settings +.settings/* +.gitignore +node_modules/ +.travis.yml +test-case/ +.git/ \ No newline at end of file diff --git a/node_modules/memorystream/Gruntfile.js b/node_modules/memorystream/Gruntfile.js new file mode 100644 index 0000000..2f6fa43 --- /dev/null +++ b/node_modules/memorystream/Gruntfile.js @@ -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']); +}; \ No newline at end of file diff --git a/node_modules/memorystream/LICENSE b/node_modules/memorystream/LICENSE new file mode 100644 index 0000000..5860a88 --- /dev/null +++ b/node_modules/memorystream/LICENSE @@ -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. \ No newline at end of file diff --git a/node_modules/memorystream/README.md b/node_modules/memorystream/README.md new file mode 100644 index 0000000..1abe8b6 --- /dev/null +++ b/node_modules/memorystream/README.md @@ -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"). + + + + \ No newline at end of file diff --git a/node_modules/memorystream/index.js b/node_modules/memorystream/index.js new file mode 100644 index 0000000..38d46ca --- /dev/null +++ b/node_modules/memorystream/index.js @@ -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; diff --git a/node_modules/memorystream/package.json b/node_modules/memorystream/package.json new file mode 100644 index 0000000..e961781 --- /dev/null +++ b/node_modules/memorystream/package.json @@ -0,0 +1,77 @@ +{ + "_from": "memorystream@^0.3.1", + "_id": "memorystream@0.3.1", + "_inBundle": false, + "_integrity": "sha1-htcJCzDORV1j+64S3aUaR93K+bI=", + "_location": "/memorystream", + "_phantomChildren": {}, + "_requested": { + "type": "range", + "registry": true, + "raw": "memorystream@^0.3.1", + "name": "memorystream", + "escapedName": "memorystream", + "rawSpec": "^0.3.1", + "saveSpec": null, + "fetchSpec": "^0.3.1" + }, + "_requiredBy": [ + "/npm-run-all" + ], + "_resolved": "https://registry.npmjs.org/memorystream/-/memorystream-0.3.1.tgz", + "_shasum": "86d7090b30ce455d63fbae12dda51a47ddcaf9b2", + "_spec": "memorystream@^0.3.1", + "_where": "/home/pruss/Dev/3-minute-website/node_modules/npm-run-all", + "author": { + "name": "Dmitry Nizovtsev", + "url": "https://github.com/JSBizon" + }, + "bugs": { + "url": "https://github.com/JSBizon/node-memorystream/issues" + }, + "bundleDependencies": false, + "contributors": [ + { + "name": "Dmitry Nizovtsev", + "email": "dmitryp3@gmail.com" + } + ], + "deprecated": false, + "description": "This is lightweight memory stream module for node.js.", + "devDependencies": { + "expect.js": "~0.2.0", + "grunt": "~0.4", + "grunt-cli": "~0.1.13", + "grunt-contrib-jshint": "~0.10.0", + "grunt-mocha-test": "~0.12.2", + "mocha": "~1.20.0", + "q": "~1.0.1" + }, + "engines": { + "node": ">= 0.10.0" + }, + "homepage": "https://github.com/JSBizon/node-memorystream", + "keywords": [ + "memory", + "test", + "stream", + "tools", + "streams", + "buffer" + ], + "licenses": [ + { + "type": "MIT", + "url": "http://github.com/JSBizon/node-memorystream/raw/master/LICENSE" + } + ], + "name": "memorystream", + "repository": { + "type": "git", + "url": "git+https://github.com/JSBizon/node-memorystream.git" + }, + "scripts": { + "test": "grunt" + }, + "version": "0.3.1" +} diff --git a/node_modules/memorystream/test/example.js b/node_modules/memorystream/test/example.js new file mode 100644 index 0000000..7d94369 --- /dev/null +++ b/node_modules/memorystream/test/example.js @@ -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(); diff --git a/node_modules/memorystream/test/memorystream.test.js b/node_modules/memorystream/test/memorystream.test.js new file mode 100644 index 0000000..2b80b50 --- /dev/null +++ b/node_modules/memorystream/test/memorystream.test.js @@ -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(); + }); + }); + + }); +}); -- cgit v1.2.3