2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2015-12-24 01:02:12 +01:00
|
|
|
require('../common');
|
2014-04-15 18:44:46 +02:00
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
var Transform = require('stream').Transform;
|
|
|
|
|
2016-05-05 07:20:27 +02:00
|
|
|
var 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
|
|
|
};
|
|
|
|
|
|
|
|
var parsed;
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-05-05 07:20:27 +02:00
|
|
|
var 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
|
|
|
|
|
|
|
var serialized;
|
|
|
|
|
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
|
|
|
});
|