stream.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stream_exports = {};
  19. __export(stream_exports, {
  20. StreamingApi: () => StreamingApi
  21. });
  22. module.exports = __toCommonJS(stream_exports);
  23. class StreamingApi {
  24. writer;
  25. encoder;
  26. writable;
  27. abortSubscribers = [];
  28. responseReadable;
  29. /**
  30. * Whether the stream has been aborted.
  31. */
  32. aborted = false;
  33. /**
  34. * Whether the stream has been closed normally.
  35. */
  36. closed = false;
  37. constructor(writable, _readable) {
  38. this.writable = writable;
  39. this.writer = writable.getWriter();
  40. this.encoder = new TextEncoder();
  41. const reader = _readable.getReader();
  42. this.abortSubscribers.push(async () => {
  43. await reader.cancel();
  44. });
  45. this.responseReadable = new ReadableStream({
  46. async pull(controller) {
  47. const { done, value } = await reader.read();
  48. done ? controller.close() : controller.enqueue(value);
  49. },
  50. cancel: () => {
  51. this.abort();
  52. }
  53. });
  54. }
  55. async write(input) {
  56. try {
  57. if (typeof input === "string") {
  58. input = this.encoder.encode(input);
  59. }
  60. await this.writer.write(input);
  61. } catch {
  62. }
  63. return this;
  64. }
  65. async writeln(input) {
  66. await this.write(input + "\n");
  67. return this;
  68. }
  69. sleep(ms) {
  70. return new Promise((res) => setTimeout(res, ms));
  71. }
  72. async close() {
  73. try {
  74. await this.writer.close();
  75. } catch {
  76. }
  77. this.closed = true;
  78. }
  79. async pipe(body) {
  80. this.writer.releaseLock();
  81. await body.pipeTo(this.writable, { preventClose: true });
  82. this.writer = this.writable.getWriter();
  83. }
  84. onAbort(listener) {
  85. this.abortSubscribers.push(listener);
  86. }
  87. /**
  88. * Abort the stream.
  89. * You can call this method when stream is aborted by external event.
  90. */
  91. abort() {
  92. if (!this.aborted) {
  93. this.aborted = true;
  94. this.abortSubscribers.forEach((subscriber) => subscriber());
  95. }
  96. }
  97. }
  98. // Annotate the CommonJS export names for ESM import in node:
  99. 0 && (module.exports = {
  100. StreamingApi
  101. });