AxiosTransformStream.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. 'use strict';
  2. import stream from 'stream';
  3. import utils from '../utils.js';
  4. const kInternals = Symbol('internals');
  5. class AxiosTransformStream extends stream.Transform {
  6. constructor(options) {
  7. options = utils.toFlatObject(
  8. options,
  9. {
  10. maxRate: 0,
  11. chunkSize: 64 * 1024,
  12. minChunkSize: 100,
  13. timeWindow: 500,
  14. ticksRate: 2,
  15. samplesCount: 15,
  16. },
  17. null,
  18. (prop, source) => {
  19. return !utils.isUndefined(source[prop]);
  20. }
  21. );
  22. super({
  23. readableHighWaterMark: options.chunkSize,
  24. });
  25. const internals = (this[kInternals] = {
  26. timeWindow: options.timeWindow,
  27. chunkSize: options.chunkSize,
  28. maxRate: options.maxRate,
  29. minChunkSize: options.minChunkSize,
  30. bytesSeen: 0,
  31. isCaptured: false,
  32. notifiedBytesLoaded: 0,
  33. ts: Date.now(),
  34. bytes: 0,
  35. onReadCallback: null,
  36. });
  37. this.on('newListener', (event) => {
  38. if (event === 'progress') {
  39. if (!internals.isCaptured) {
  40. internals.isCaptured = true;
  41. }
  42. }
  43. });
  44. }
  45. _read(size) {
  46. const internals = this[kInternals];
  47. if (internals.onReadCallback) {
  48. internals.onReadCallback();
  49. }
  50. return super._read(size);
  51. }
  52. _transform(chunk, encoding, callback) {
  53. const internals = this[kInternals];
  54. const maxRate = internals.maxRate;
  55. const readableHighWaterMark = this.readableHighWaterMark;
  56. const timeWindow = internals.timeWindow;
  57. const divider = 1000 / timeWindow;
  58. const bytesThreshold = maxRate / divider;
  59. let minChunkSize =
  60. internals.minChunkSize !== false
  61. ? Math.max(internals.minChunkSize, bytesThreshold * 0.01)
  62. : 0;
  63. if (maxRate) {
  64. minChunkSize = Math.min(bytesThreshold, minChunkSize);
  65. }
  66. const pushChunk = (_chunk, _callback) => {
  67. const bytes = Buffer.byteLength(_chunk);
  68. internals.bytesSeen += bytes;
  69. internals.bytes += bytes;
  70. internals.isCaptured && this.emit('progress', internals.bytesSeen);
  71. if (this.push(_chunk)) {
  72. process.nextTick(_callback);
  73. } else {
  74. internals.onReadCallback = () => {
  75. internals.onReadCallback = null;
  76. process.nextTick(_callback);
  77. };
  78. }
  79. };
  80. const transformChunk = (_chunk, _callback) => {
  81. if (this.destroyed) {
  82. return _callback();
  83. }
  84. const chunkSize = Buffer.byteLength(_chunk);
  85. let chunkRemainder = null;
  86. let maxChunkSize = readableHighWaterMark;
  87. let bytesLeft;
  88. let passed = 0;
  89. if (maxRate) {
  90. const now = Date.now();
  91. if (!internals.ts || (passed = now - internals.ts) >= timeWindow) {
  92. internals.ts = now;
  93. bytesLeft = bytesThreshold - internals.bytes;
  94. internals.bytes = bytesLeft < 0 ? -bytesLeft : 0;
  95. passed = 0;
  96. }
  97. bytesLeft = bytesThreshold - internals.bytes;
  98. }
  99. if (maxRate) {
  100. if (bytesLeft <= 0) {
  101. // next time window
  102. return setTimeout(() => {
  103. _callback(null, _chunk);
  104. }, timeWindow - passed);
  105. }
  106. if (bytesLeft < maxChunkSize) {
  107. // A buffer boundary must consume at least one byte. Carry any
  108. // fractional allowance forward in the next accounting window.
  109. maxChunkSize = Math.max(1, Math.floor(bytesLeft));
  110. }
  111. }
  112. if (maxChunkSize && chunkSize > maxChunkSize && chunkSize - maxChunkSize > minChunkSize) {
  113. chunkRemainder = _chunk.subarray(maxChunkSize);
  114. _chunk = _chunk.subarray(0, maxChunkSize);
  115. }
  116. pushChunk(
  117. _chunk,
  118. chunkRemainder
  119. ? () => {
  120. process.nextTick(_callback, null, chunkRemainder);
  121. }
  122. : _callback
  123. );
  124. };
  125. transformChunk(chunk, function transformNextChunk(err, _chunk) {
  126. if (err) {
  127. return callback(err);
  128. }
  129. if (_chunk) {
  130. transformChunk(_chunk, transformNextChunk);
  131. } else {
  132. callback(null);
  133. }
  134. });
  135. }
  136. }
  137. export default AxiosTransformStream;