2017-01-03 22:16:48 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2015-12-24 01:02:12 +01:00
|
|
|
require('../common');
|
2016-12-31 00:38:06 +01:00
|
|
|
const assert = require('assert');
|
2014-04-15 18:44:46 +02:00
|
|
|
|
2016-12-31 00:38:06 +01:00
|
|
|
const Transform = require('stream').Transform;
|
2014-04-15 18:44:46 +02:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const parser = new Transform({ readableObjectMode: true });
|
2014-04-15 18:44:46 +02:00
|
|
|
|
|
|
|
assert(parser._readableState.objectMode);
|
|
|
|
assert(!parser._writableState.objectMode);
|
2016-09-17 12:32:33 +02:00
|
|
|
assert.strictEqual(parser._readableState.highWaterMark, 16);
|
|
|
|
assert.strictEqual(parser._writableState.highWaterMark, 16 * 1024);
|
2014-04-15 18:44:46 +02:00
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
parser._transform = function(chunk, enc, callback) {
|
2016-05-05 07:20:27 +02:00
|
|
|
callback(null, { val: chunk[0] });
|
2014-04-15 18:44:46 +02:00
|
|
|
};
|
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
let parsed;
|
2014-04-15 18:44:46 +02:00
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
parser.on('data', function(obj) {
|
2014-04-15 18:44:46 +02:00
|
|
|
parsed = obj;
|
|
|
|
});
|
|
|
|
|
2016-01-26 00:00:06 +01:00
|
|
|
parser.end(Buffer.from([42]));
|
2014-04-15 18:44:46 +02:00
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
process.on('exit', function() {
|
2016-09-17 12:32:33 +02:00
|
|
|
assert.strictEqual(parsed.val, 42);
|
2014-04-15 18:44:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
const serializer = new Transform({ writableObjectMode: true });
|
2014-04-15 18:44:46 +02:00
|
|
|
|
|
|
|
assert(!serializer._readableState.objectMode);
|
|
|
|
assert(serializer._writableState.objectMode);
|
2016-09-17 12:32:33 +02:00
|
|
|
assert.strictEqual(serializer._readableState.highWaterMark, 16 * 1024);
|
|
|
|
assert.strictEqual(serializer._writableState.highWaterMark, 16);
|
2014-04-15 18:44:46 +02:00
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
serializer._transform = function(obj, _, callback) {
|
2016-01-26 00:00:06 +01:00
|
|
|
callback(null, Buffer.from([obj.val]));
|
2014-07-09 00:06:40 +02:00
|
|
|
};
|
2014-04-15 18:44:46 +02:00
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
let serialized;
|
2014-04-15 18:44:46 +02:00
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
serializer.on('data', function(chunk) {
|
2014-04-15 18:44:46 +02:00
|
|
|
serialized = chunk;
|
|
|
|
});
|
|
|
|
|
2016-05-05 07:20:27 +02:00
|
|
|
serializer.write({ val: 42 });
|
2014-04-15 18:44:46 +02:00
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
process.on('exit', function() {
|
2016-09-17 12:32:33 +02:00
|
|
|
assert.strictEqual(serialized[0], 42);
|
2014-04-15 18:44:46 +02:00
|
|
|
});
|