stream-mock
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.WritableMock = exports.ReadableMock = undefined; | ||
exports.DuplexMock = exports.WritableMock = exports.ReadableMock = undefined; | ||
@@ -17,2 +17,6 @@ var _readableMock = require('./readable-mock'); | ||
var _duplexMock = require('./duplex-mock'); | ||
var _duplexMock2 = _interopRequireDefault(_duplexMock); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -22,6 +26,8 @@ | ||
var WritableMock = exports.WritableMock = _writableMock2.default; | ||
var DuplexMock = exports.DuplexMock = _duplexMock2.default; | ||
exports.default = { | ||
ReadableMock: _readableMock2.default, | ||
WritableMock: _writableMock2.default | ||
WritableMock: _writableMock2.default, | ||
DuplexMock: _duplexMock2.default | ||
}; |
@@ -7,10 +7,6 @@ 'use strict'; | ||
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); | ||
var _apply = require('babel-runtime/core-js/reflect/apply'); | ||
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); | ||
var _apply2 = _interopRequireDefault(_apply); | ||
var _getIterator2 = require('babel-runtime/core-js/get-iterator'); | ||
var _getIterator3 = _interopRequireDefault(_getIterator2); | ||
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); | ||
@@ -38,2 +34,6 @@ | ||
var _writable = require('./commons/writable'); | ||
var _writable2 = _interopRequireDefault(_writable); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -47,2 +47,6 @@ | ||
*/ | ||
/** | ||
* @module writable-mock | ||
* @requires stream.Writable | ||
*/ | ||
var WritableMock = function (_Writable) { | ||
@@ -81,5 +85,8 @@ (0, _inherits3.default)(WritableMock, _Writable); | ||
key: '_write', | ||
value: function _write(chunk, encoding, callback) { | ||
this.data.push(chunk); | ||
callback(); | ||
value: function _write() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
(0, _apply2.default)(_writable2.default._write, this, args); | ||
} | ||
@@ -97,8 +104,8 @@ | ||
key: '_writev', | ||
value: function _writev(chunks, callback) { | ||
var datas = chunks.map(function (c) { | ||
return c.chunk; | ||
}); | ||
this.data = this.data.concat(datas); | ||
callback(); | ||
value: function _writev() { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
(0, _apply2.default)(_writable2.default._writev, this, args); | ||
} | ||
@@ -115,39 +122,8 @@ | ||
key: '_final', | ||
value: function _final(callback) { | ||
if (!this._writableState.objectMode) { | ||
var length = this.data.reduce(function (acc, curr) { | ||
acc += curr.length; | ||
return acc; | ||
}, 0); | ||
var buf = Buffer.alloc(length); | ||
var offset = 0; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
value: function _final() { | ||
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
args[_key3] = arguments[_key3]; | ||
} | ||
try { | ||
for (var _iterator = (0, _getIterator3.default)(this.data), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var d = _step.value; | ||
d.copy(buf, offset); | ||
offset += d.length; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
this.data = buf; | ||
} | ||
callback(); | ||
(0, _apply2.default)(_writable2.default._final, this, args); | ||
} | ||
@@ -163,18 +139,8 @@ | ||
get: function get() { | ||
var _ref; | ||
if (!this._writableState.objectMode) { | ||
// Buffer mode, alreday flat | ||
return this.data; | ||
} | ||
return (_ref = []).concat.apply(_ref, (0, _toConsumableArray3.default)(this.data)); | ||
return (0, _apply2.default)(_writable2.default.flatData, this, []); | ||
} | ||
}]); | ||
return WritableMock; | ||
}(_stream.Writable); /** | ||
* @module writable-mock | ||
* @requires stream.Writable | ||
*/ | ||
}(_stream.Writable); | ||
exports.default = WritableMock; |
{ | ||
"name": "stream-mock", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Node stream mock module", | ||
@@ -51,3 +51,3 @@ "main": "lib/index.js", | ||
"tui-jsdoc-template": "^1.2.2", | ||
"xo": "^0.20.1" | ||
"xo": "^0.21.0" | ||
}, | ||
@@ -76,3 +76,4 @@ "eslintConfig": { | ||
[ | ||
"istanbul", [ | ||
"istanbul", | ||
[ | ||
"module-resolver", | ||
@@ -79,0 +80,0 @@ { |
@@ -18,2 +18,5 @@ # Stream Mock | ||
that puts its data at your disposal. | ||
- Create a | ||
[duplex stream](https://nodejs.org/api/stream.html#stream_duplex_and_transform_streams) | ||
that combines a readable and writable stream together. | ||
- Can operate both in | ||
@@ -53,3 +56,3 @@ [object](https://nodejs.org/api/stream.html#stream_object_mode) and normal | ||
```javascript | ||
import {ReadableMock, WritableMock } from 'stream-mock'; | ||
import {ReadableMock, WritableMock, DuplexMock } from 'stream-mock'; | ||
import chai from 'chai'; | ||
@@ -75,2 +78,20 @@ | ||
}); | ||
it('mocks a duplex stream', (done) = { | ||
// Given | ||
const stream = new DuplexMock({ | ||
readableObjectMode: true, | ||
writableObjectMode: true | ||
}); | ||
stream.on('data', ({ input }) => { | ||
console.log('Got input', input); | ||
}) | ||
stream.write({ input: 'a'}) | ||
stream.write({ input: 'b'}) | ||
stream.write({ input: 'c'}) | ||
stream.end() | ||
}); | ||
}); | ||
@@ -77,0 +98,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24176
8
380
128