()
| 85 | return this.open() |
| 86 | } |
| 87 | async *open() { |
| 88 | let value = undefined |
| 89 | let done = undefined |
| 90 | do { |
| 91 | if (this.#response) { |
| 92 | await asyncDelay(RETRY_DELAY_IF_BUFFER_IS_EMPTY) |
| 93 | } |
| 94 | this.#response = await fetch(this.#url, this.#fetchOptions) |
| 95 | if (!this.#response.ok) { |
| 96 | if (this.#response.status === 425) { |
| 97 | continue |
| 98 | } |
| 99 | // Request status indicate failure |
| 100 | console.warn("Stream %o stopped unexpectedly.", this.#response) |
| 101 | value = await Promise.resolve(this.onError(this.#response)) |
| 102 | if (typeof value === "boolean" && value) { |
| 103 | continue |
| 104 | } |
| 105 | return value |
| 106 | } |
| 107 | const reader = this.#response.body.getReader() |
| 108 | done = false |
| 109 | do { |
| 110 | const readState = await reader.read() |
| 111 | value = this.parse(readState.value) |
| 112 | if (value) { |
| 113 | for (let sVal of value) { |
| 114 | ; ({ value: sVal, done } = await Promise.resolve( |
| 115 | this.onNext({ value: sVal, done: readState.done }) |
| 116 | )) |
| 117 | yield sVal |
| 118 | if (done) { |
| 119 | return this.onComplete(sVal) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | if (done) { |
| 124 | return |
| 125 | } |
| 126 | } while (value && !done) |
| 127 | } while (!done && (this.#response.ok || this.#response.status === 425)) |
| 128 | } |
| 129 | *readStreamAsJSON(jsonStr, throwOnError) { |
| 130 | if (typeof jsonStr !== "string") { |
| 131 | throw new Error("jsonStr is not a string.") |
no test coverage detected