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 __proto__: null, 43 value: val, 44 enumerable: true, 45 configurable: true, 46 writable: true, 47 }); 48 }; 49} 50 51ObjectDefineProperties(LazyTransform.prototype, { 52 _readableState: { 53 __proto__: null, 54 get: makeGetter('_readableState'), 55 set: makeSetter('_readableState'), 56 configurable: true, 57 enumerable: true, 58 }, 59 _writableState: { 60 __proto__: null, 61 get: makeGetter('_writableState'), 62 set: makeSetter('_writableState'), 63 configurable: true, 64 enumerable: true, 65 }, 66}); 67