1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23require('../common'); 24const assert = require('assert'); 25 26const Transform = require('stream').Transform; 27 28const parser = new Transform({ readableObjectMode: true }); 29 30assert(parser._readableState.objectMode); 31assert(!parser._writableState.objectMode); 32assert.strictEqual(parser.readableHighWaterMark, 16); 33assert.strictEqual(parser.writableHighWaterMark, 16 * 1024); 34assert.strictEqual(parser.readableHighWaterMark, 35 parser._readableState.highWaterMark); 36assert.strictEqual(parser.writableHighWaterMark, 37 parser._writableState.highWaterMark); 38 39parser._transform = function(chunk, enc, callback) { 40 callback(null, { val: chunk[0] }); 41}; 42 43let parsed; 44 45parser.on('data', function(obj) { 46 parsed = obj; 47}); 48 49parser.end(Buffer.from([42])); 50 51process.on('exit', function() { 52 assert.strictEqual(parsed.val, 42); 53}); 54 55 56const serializer = new Transform({ writableObjectMode: true }); 57 58assert(!serializer._readableState.objectMode); 59assert(serializer._writableState.objectMode); 60assert.strictEqual(serializer.readableHighWaterMark, 16 * 1024); 61assert.strictEqual(serializer.writableHighWaterMark, 16); 62assert.strictEqual(parser.readableHighWaterMark, 63 parser._readableState.highWaterMark); 64assert.strictEqual(parser.writableHighWaterMark, 65 parser._writableState.highWaterMark); 66 67serializer._transform = function(obj, _, callback) { 68 callback(null, Buffer.from([obj.val])); 69}; 70 71let serialized; 72 73serializer.on('data', function(chunk) { 74 serialized = chunk; 75}); 76 77serializer.write({ val: 42 }); 78 79process.on('exit', function() { 80 assert.strictEqual(serialized[0], 42); 81}); 82