Skip to content

Commit

Permalink
use async iterator explicitly as node-fetch do not fully support WebS…
Browse files Browse the repository at this point in the history
…treams API
  • Loading branch information
sivukhin committed Sep 18, 2024
1 parent c61bac4 commit 4ee9194
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/http/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class HttpCursor extends Cursor {
#stream: HttpStream;
#encoding: ProtocolEncoding;

#reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
#reader: any | undefined;
#queue: ByteQueue;
#closed: Error | undefined;
#done: boolean;
Expand All @@ -42,7 +42,10 @@ export class HttpCursor extends Cursor {
throw new ProtoError("No response body for cursor request");
}

this.#reader = response.body.getReader();
// node-fetch do not fully support WebStream API, especially getReader() function
// see https://github.com/node-fetch/node-fetch/issues/387
// so, we are using async iterator which behaves similarly here instead
this.#reader = (response.body as any)[Symbol.asyncIterator]();
const respBody = await this.#nextItem(json_CursorRespBody, protobuf_CursorRespBody);
if (respBody === undefined) {
throw new ProtoError("Empty response to cursor request");
Expand All @@ -69,7 +72,7 @@ export class HttpCursor extends Cursor {
this.#stream._cursorClosed(this);

if (this.#reader !== undefined) {
this.#reader.cancel();
this.#reader.return();
}
}

Expand Down Expand Up @@ -106,7 +109,7 @@ export class HttpCursor extends Cursor {
throw new InternalError("Attempted to read from HTTP cursor before it was opened");
}

const { value, done } = await this.#reader.read();
const { value, done } = await this.#reader.next();
if (done && this.#queue.length === 0) {
this.#done = true;
} else if (done) {
Expand Down

0 comments on commit 4ee9194

Please sign in to comment.