lazy_transform.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. const { ObjectDefineProperties, ObjectDefineProperty, ObjectSetPrototypeOf } = require('../../ours/primordials')
  6. const stream = require('../../stream')
  7. const { getDefaultEncoding } = require('../crypto/util')
  8. module.exports = LazyTransform
  9. function LazyTransform(options) {
  10. this._options = options
  11. }
  12. ObjectSetPrototypeOf(LazyTransform.prototype, stream.Transform.prototype)
  13. ObjectSetPrototypeOf(LazyTransform, stream.Transform)
  14. function makeGetter(name) {
  15. return function () {
  16. stream.Transform.call(this, this._options)
  17. this._writableState.decodeStrings = false
  18. if (!this._options || !this._options.defaultEncoding) {
  19. this._writableState.defaultEncoding = getDefaultEncoding()
  20. }
  21. return this[name]
  22. }
  23. }
  24. function makeSetter(name) {
  25. return function (val) {
  26. ObjectDefineProperty(this, name, {
  27. __proto__: null,
  28. value: val,
  29. enumerable: true,
  30. configurable: true,
  31. writable: true
  32. })
  33. }
  34. }
  35. ObjectDefineProperties(LazyTransform.prototype, {
  36. _readableState: {
  37. __proto__: null,
  38. get: makeGetter('_readableState'),
  39. set: makeSetter('_readableState'),
  40. configurable: true,
  41. enumerable: true
  42. },
  43. _writableState: {
  44. __proto__: null,
  45. get: makeGetter('_writableState'),
  46. set: makeSetter('_writableState'),
  47. configurable: true,
  48. enumerable: true
  49. }
  50. })