rs-utils.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. 'use strict';
  2. (function () {
  3. // Fake setInterval-like functionality in environments that don't have it
  4. class IntervalHandle {
  5. constructor(callback, delayMs) {
  6. this.callback = callback;
  7. this.delayMs = delayMs;
  8. this.cancelled = false;
  9. Promise.resolve().then(() => this.check());
  10. }
  11. async check() {
  12. while (true) {
  13. await new Promise(resolve => step_timeout(resolve, this.delayMs));
  14. if (this.cancelled) {
  15. return;
  16. }
  17. this.callback();
  18. }
  19. }
  20. cancel() {
  21. this.cancelled = true;
  22. }
  23. }
  24. let localSetInterval, localClearInterval;
  25. if (typeof globalThis.setInterval !== "undefined" &&
  26. typeof globalThis.clearInterval !== "undefined") {
  27. localSetInterval = globalThis.setInterval;
  28. localClearInterval = globalThis.clearInterval;
  29. } else {
  30. localSetInterval = function setInterval(callback, delayMs) {
  31. return new IntervalHandle(callback, delayMs);
  32. }
  33. localClearInterval = function clearInterval(handle) {
  34. handle.cancel();
  35. }
  36. }
  37. class RandomPushSource {
  38. constructor(toPush) {
  39. this.pushed = 0;
  40. this.toPush = toPush;
  41. this.started = false;
  42. this.paused = false;
  43. this.closed = false;
  44. this._intervalHandle = null;
  45. }
  46. readStart() {
  47. if (this.closed) {
  48. return;
  49. }
  50. if (!this.started) {
  51. this._intervalHandle = localSetInterval(writeChunk, 2);
  52. this.started = true;
  53. }
  54. if (this.paused) {
  55. this._intervalHandle = localSetInterval(writeChunk, 2);
  56. this.paused = false;
  57. }
  58. const source = this;
  59. function writeChunk() {
  60. if (source.paused) {
  61. return;
  62. }
  63. source.pushed++;
  64. if (source.toPush > 0 && source.pushed > source.toPush) {
  65. if (source._intervalHandle) {
  66. localClearInterval(source._intervalHandle);
  67. source._intervalHandle = undefined;
  68. }
  69. source.closed = true;
  70. source.onend();
  71. } else {
  72. source.ondata(randomChunk(128));
  73. }
  74. }
  75. }
  76. readStop() {
  77. if (this.paused) {
  78. return;
  79. }
  80. if (this.started) {
  81. this.paused = true;
  82. localClearInterval(this._intervalHandle);
  83. this._intervalHandle = undefined;
  84. } else {
  85. throw new Error('Can\'t pause reading an unstarted source.');
  86. }
  87. }
  88. }
  89. function randomChunk(size) {
  90. let chunk = '';
  91. for (let i = 0; i < size; ++i) {
  92. // Add a random character from the basic printable ASCII set.
  93. chunk += String.fromCharCode(Math.round(Math.random() * 84) + 32);
  94. }
  95. return chunk;
  96. }
  97. function readableStreamToArray(readable, reader) {
  98. if (reader === undefined) {
  99. reader = readable.getReader();
  100. }
  101. const chunks = [];
  102. return pump();
  103. function pump() {
  104. return reader.read().then(result => {
  105. if (result.done) {
  106. return chunks;
  107. }
  108. chunks.push(result.value);
  109. return pump();
  110. });
  111. }
  112. }
  113. class SequentialPullSource {
  114. constructor(limit, options) {
  115. const async = options && options.async;
  116. this.current = 0;
  117. this.limit = limit;
  118. this.opened = false;
  119. this.closed = false;
  120. this._exec = f => f();
  121. if (async) {
  122. this._exec = f => step_timeout(f, 0);
  123. }
  124. }
  125. open(cb) {
  126. this._exec(() => {
  127. this.opened = true;
  128. cb();
  129. });
  130. }
  131. read(cb) {
  132. this._exec(() => {
  133. if (++this.current <= this.limit) {
  134. cb(null, false, this.current);
  135. } else {
  136. cb(null, true, null);
  137. }
  138. });
  139. }
  140. close(cb) {
  141. this._exec(() => {
  142. this.closed = true;
  143. cb();
  144. });
  145. }
  146. }
  147. function sequentialReadableStream(limit, options) {
  148. const sequentialSource = new SequentialPullSource(limit, options);
  149. const stream = new ReadableStream({
  150. start() {
  151. return new Promise((resolve, reject) => {
  152. sequentialSource.open(err => {
  153. if (err) {
  154. reject(err);
  155. }
  156. resolve();
  157. });
  158. });
  159. },
  160. pull(c) {
  161. return new Promise((resolve, reject) => {
  162. sequentialSource.read((err, done, chunk) => {
  163. if (err) {
  164. reject(err);
  165. } else if (done) {
  166. sequentialSource.close(err2 => {
  167. if (err2) {
  168. reject(err2);
  169. }
  170. c.close();
  171. resolve();
  172. });
  173. } else {
  174. c.enqueue(chunk);
  175. resolve();
  176. }
  177. });
  178. });
  179. }
  180. });
  181. stream.source = sequentialSource;
  182. return stream;
  183. }
  184. function transferArrayBufferView(view) {
  185. const noopByteStream = new ReadableStream({
  186. type: 'bytes',
  187. pull(c) {
  188. c.byobRequest.respond(c.byobRequest.view.byteLength);
  189. c.close();
  190. }
  191. });
  192. const reader = noopByteStream.getReader({ mode: 'byob' });
  193. return reader.read(view).then((result) => result.value);
  194. }
  195. self.RandomPushSource = RandomPushSource;
  196. self.readableStreamToArray = readableStreamToArray;
  197. self.sequentialReadableStream = sequentialReadableStream;
  198. self.transferArrayBufferView = transferArrayBufferView;
  199. }());