1// LazyTransform is a special type of Transform stream that is lazily loaded. 2// This is used for performance with bi-API-ship: when two APIs are available 3// for the stream, one conventional and one non-conventional. 4'use strict'; 5 6const { 7 ObjectDefineProperties, 8 ObjectDefineProperty, 9 ObjectSetPrototypeOf, 10} = primordials; 11 12const stream = require('stream'); 13 14const { 15 getDefaultEncoding 16} = require('internal/crypto/util'); 17 18module.exports = LazyTransform; 19 20function LazyTransform(options) { 21 this._options = options; 22} 23ObjectSetPrototypeOf(LazyTransform.prototype, stream.Transform.prototype); 24ObjectSetPrototypeOf(LazyTransform, stream.Transform); 25 26function makeGetter(name) { 27 return function() { 28 stream.Transform.call(this, this._options); 29 this._writableState.decodeStrings = false; 30 31 if (!this._options || !this._options.defaultEncoding) { 32 this._writableState.defaultEncoding = getDefaultEncoding(); 33 } 34 35 return this[name]; 36 }; 37} 38 39function makeSetter(name) { 40 return function(val) { 41 ObjectDefineProperty(this, name, { 42 value: val, 43 enumerable: true, 44 configurable: true, 45 writable: true 46 }); 47 }; 48} 49 50ObjectDefineProperties(LazyTransform.prototype, { 51 _readableState: { 52 get: makeGetter('_readableState'), 53 set: makeSetter('_readableState'), 54 configurable: true, 55 enumerable: true 56 }, 57 _writableState: { 58 get: makeGetter('_writableState'), 59 set: makeSetter('_writableState'), 60 configurable: true, 61 enumerable: true 62 }, 63 _transformState: { 64 get: makeGetter('_transformState'), 65 set: makeSetter('_transformState'), 66 configurable: true, 67 enumerable: true 68 } 69}); 70