Skip to content

Commit

Permalink
Merge pull request #20 from sivukhin/use-cross-fetch
Browse files Browse the repository at this point in the history
replace @libsql/isomorphic-fetch with cross-fetch
  • Loading branch information
penberg authored Sep 19, 2024
2 parents 91ce1d3 + 2c3bd38 commit 95a1d21
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.8.0 -- 2024-09-16

- Replace isomorphic-fetch dependency with cross-fetch

## 0.7.0 -- 2024-09-16

- Upgrade isomorphic-fetch to v0.3.0 which do not override `fetch` agent and do not have issue with headers in cloud providers (see https://github.com/libsql/isomorphic-ts/pull/17)
Expand Down
65 changes: 54 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@libsql/hrana-client",
"version": "0.7.0",
"version": "0.8.0",
"keywords": [
"hrana",
"libsql",
Expand Down Expand Up @@ -45,8 +45,8 @@
"typedoc": "rm -rf ./docs && typedoc"
},
"dependencies": {
"@libsql/isomorphic-fetch": "^0.3.1",
"@libsql/isomorphic-ws": "^0.1.5",
"cross-fetch": "^4.0.0",
"js-base64": "^3.7.5",
"node-fetch": "^3.3.2"
},
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Response } from "@libsql/isomorphic-fetch";
import { fetch, Request } from "@libsql/isomorphic-fetch";
import type { Response } from "cross-fetch";
import { fetch, Request } from "cross-fetch";

import * as hrana from "..";

Expand Down
2 changes: 1 addition & 1 deletion src/http/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetch, Request } from "@libsql/isomorphic-fetch";
import { fetch, Request } from "cross-fetch";

import type { ProtocolVersion, ProtocolEncoding } from "../client.js";
import { Client } from "../client.js";
Expand Down
19 changes: 11 additions & 8 deletions src/http/cursor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Response, ReadableStreamDefaultReader } from "@libsql/isomorphic-fetch";
import type { Response } from "cross-fetch";

import { ByteQueue } from "../byte_queue.js";
import type { ProtocolEncoding } from "../client.js";
Expand All @@ -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 All @@ -79,7 +82,7 @@ export class HttpCursor extends Cursor {
}

async #nextItem<T>(jsonFun: jsond.ObjectFun<T>, protobufDef: protobufd.MessageDef<T>): Promise<T | undefined> {
for (;;) {
for (; ;) {
if (this.#done) {
return undefined;
} else if (this.#closed !== undefined) {
Expand All @@ -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 Expand Up @@ -135,12 +138,12 @@ export class HttpCursor extends Cursor {

let varintValue = 0;
let varintLength = 0;
for (;;) {
for (; ;) {
if (varintLength >= data.byteLength) {
return undefined;
}
const byte = data[varintLength];
varintValue |= (byte & 0x7f) << (7*varintLength);
varintValue |= (byte & 0x7f) << (7 * varintLength);
varintLength += 1;
if (!(byte & 0x80)) {
break;
Expand Down
4 changes: 2 additions & 2 deletions src/http/stream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { fetch, Response } from "@libsql/isomorphic-fetch";
import { Request, Headers } from "@libsql/isomorphic-fetch";
import type { fetch } from "cross-fetch";
import { Request, Headers } from "cross-fetch";

import type { ProtocolEncoding } from "../client.js";
import type { Cursor } from "../cursor.js";
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { WsClient } from "./ws/client.js";
import { ProtocolVersion } from "./client.js";

export { WebSocket } from "@libsql/isomorphic-ws";
export type { RequestInit, Response } from "@libsql/isomorphic-fetch";
export { fetch, Request, Headers } from "@libsql/isomorphic-fetch";
export type { Response } from "cross-fetch";
export { fetch, Request, Headers } from "cross-fetch";

export type { ProtocolVersion, ProtocolEncoding } from "./client.js";
export { Client } from "./client.js";
Expand Down Expand Up @@ -49,8 +49,8 @@ export function openWs(url: string | URL, jwt?: string, protocolVersion: Protoco
/** Open a Hrana client over HTTP connected to the given `url`.
*
* If the `customFetch` argument is passed and not `undefined`, it is used in place of the `fetch` function
* from `@libsql/isomorphic-fetch`. This function is always called with a `Request` object from
* `@libsql/isomorphic-fetch`.
* from `cross-fetch`. This function is always called with a `Request` object from
* `cross-fetch`.
*/
export function openHttp(url: string | URL, jwt?: string, customFetch?: unknown | undefined, protocolVersion: ProtocolVersion = 2): HttpClient {
return new HttpClient(url instanceof URL ? url : new URL(url), jwt, customFetch, protocolVersion);
Expand Down

0 comments on commit 95a1d21

Please sign in to comment.