Skip to content

Commit

Permalink
Merge branch 'main' into readme-logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jrodewig authored Dec 18, 2024
2 parents b44a3f6 + 018f4cc commit cbacb1c
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 87 deletions.
6 changes: 0 additions & 6 deletions __tests__/functional/client-configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ an environmental variable named FAUNA_SECRET or pass it to the Client constructo
},

close() {},
getURL(): string {
return "http://foo.com/bar";
},
};

const client = getClient(
Expand Down Expand Up @@ -150,9 +147,6 @@ an environmental variable named FAUNA_SECRET or pass it to the Client constructo
},

close() {},
getURL(): string {
return "https://foo.com/bar";
},
};

const client = getClient(
Expand Down
1 change: 0 additions & 1 deletion __tests__/functional/feed-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const mockHttpClient = {
.fn()
.mockImplementation(() => Promise.resolve({ ...mockHttpResponse })),
close: jest.fn(),
getURL: jest.fn(() => "bar"),
};

const defaultConfig: FeedClientConfiguration = {
Expand Down
6 changes: 0 additions & 6 deletions __tests__/integration/client-last-txn-tracking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ describe("last_txn_ts tracking in client", () => {
},

close() {},
getURL(): string {
return "http://foo.com/bar";
},
};

const myClient = getClient(
Expand Down Expand Up @@ -66,9 +63,6 @@ describe("last_txn_ts tracking in client", () => {
},

close() {},
getURL(): string {
return "http://foo.com/bar";
},
};

const myClient = getClient(
Expand Down
15 changes: 0 additions & 15 deletions __tests__/integration/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ describe("query", () => {
return dummyResponse;
},
close() {},
getURL(): string {
return "http://foo.com/bar";
},
};
const clientConfiguration: Partial<ClientConfiguration> = {
linearized: true,
Expand All @@ -166,9 +163,6 @@ describe("query", () => {
return dummyResponse;
},
close() {},
getURL() {
return "http://foo.com/bar";
},
};

let clientConfiguration: Partial<ClientConfiguration> = {
Expand Down Expand Up @@ -277,9 +271,6 @@ describe("query", () => {
);
},
close() {},
getURL() {
return "http://foo.com/bar";
},
};
badClient = getClient({}, httpClient);
await badClient.query(fql`"dummy"`);
Expand Down Expand Up @@ -388,9 +379,6 @@ describe("query", () => {
return httpClient.request(badRequest);
},
close() {},
getURL() {
return "http://foo.com/bar";
},
};

const badClient = getClient({}, badHTTPClient);
Expand All @@ -412,9 +400,6 @@ describe("query", () => {
throw new Error("boom!");
},
close() {},
getURL(): string {
return "http://foo.com/bar";
},
};
const badClient = getClient(
{
Expand Down
6 changes: 0 additions & 6 deletions __tests__/integration/set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@ describe("SetIterator", () => {
close() {
return;
},
getURL(): string {
return "http://foo.com/bar";
},
};
const testClient = getClient({}, httpClient);

Expand Down Expand Up @@ -251,9 +248,6 @@ describe("SetIterator", () => {
close() {
return;
},
getURL(): string {
return "http://foo.com/bar";
},
};
const testClient = getClient({}, httpClient);

Expand Down
20 changes: 20 additions & 0 deletions __tests__/unit/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AuthorizationError,
FetchClient,
fql,
ProtocolError,
QueryTimeoutError,
ServiceError,
ServiceInternalError,
Expand Down Expand Up @@ -123,6 +124,25 @@ describe("query", () => {
expect(actual.summary).toEqual("the summary");
});

it("Throws ProtocolError on an empty 200 response", async () => {
expect.assertions(2);
fetchMock.mockResponse("", {
status: 200,
headers: [["content-length", "0"]],
});
try {
const result = await client.query(fql`'foo'.length`);
console.log("result", result);
} catch (e) {
if (e instanceof ProtocolError) {
expect(e.message).toEqual(
"There was an issue communicating with Fauna. Response is empty. Please try again.",
);
expect(e.httpStatus).toEqual(500);
}
}
});

// it("throws an NetworkError on a timeout", async () => {
// expect.assertions(2);
// // axios mock adapater currently has a bug that cannot match
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fauna",
"version": "2.4.0",
"version": "2.4.1",
"description": "A driver to query Fauna databases in browsers, Node.js, and other Javascript runtimes",
"homepage": "https://fauna.com",
"bugs": {
Expand Down
93 changes: 55 additions & 38 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,63 @@
import {
ClientConfiguration,
FeedClientConfiguration,
StreamClientConfiguration,
endpoints,
type ClientConfiguration,
type FeedClientConfiguration,
type StreamClientConfiguration,
} from "./client-configuration";
import {
ClientClosedError,
ClientError,
FaunaError,
getServiceError,
NetworkError,
ProtocolError,
ServiceError,
ThrottlingError,
getServiceError,
} from "./errors";
import {
FaunaAPIPaths,
HTTPRequest,
HTTPStreamClient,
StreamAdapter,
getDefaultHTTPClient,
isHTTPResponse,
isStreamClient,
type HTTPClient,
HTTPResponse,
type HTTPRequest,
type HTTPResponse,
type HTTPStreamRequest,
type HTTPStreamClient,
} from "./http-client";
import { Query } from "./query-builder";
import { TaggedTypeFormat } from "./tagged-type";
import { getDriverEnv } from "./util/environment";
import { withRetries } from "./util/retryable";
import {
EmbeddedSet,
EventSource,
FeedPage,
isEventSource,
Page,
SetIterator,
isEventSource,
type EmbeddedSet,
type EventSource,
} from "./values";
import {
EncodedObject,
FeedError,
FeedRequest,
FeedSuccess,
QueryOptions,
QueryRequest,
StreamEvent,
StreamEventData,
StreamEventStatus,
isQueryFailure,
isQuerySuccess,
type EncodedObject,
type FeedError,
type FeedRequest,
type FeedSuccess,
type QueryOptions,
type QueryRequest,
type QuerySuccess,
type QueryValue,
type StreamEvent,
type StreamEventData,
type StreamEventStatus,
} from "./wire-protocol";
import {
ConsoleLogHandler,
parseDebugLevel,
LogHandler,
LOG_LEVELS,
type LogHandler,
} from "./util/logging";

type RequiredClientConfig = ClientConfiguration &
Expand Down Expand Up @@ -617,10 +618,9 @@ in an environmental variable named FAUNA_SECRET or pass it to the Client\
this.#clientConfiguration.client_timeout_buffer_ms;
const method = "POST";
this.#clientConfiguration.logger.debug(
"Fauna HTTP %s Request to %s (timeout: %s), headers: %s",
"Fauna HTTP %s request to %s (timeout: %s), headers: %s",
method,
this.#httpClient.getURL(),
this.#clientConfiguration.endpoint.toString(),
FaunaAPIPaths.QUERY,
client_timeout_ms.toString(),
JSON.stringify(headers),
);
Expand All @@ -633,12 +633,25 @@ in an environmental variable named FAUNA_SECRET or pass it to the Client\
});

this.#clientConfiguration.logger.debug(
"Fauna HTTP Response %s from %s, headers: %s",
"Fauna HTTP response %s from %s, headers: %s",
response.status,
this.#httpClient.getURL(),
FaunaAPIPaths.QUERY,
JSON.stringify(response.headers),
);

// Receiving a 200 with no body/content indicates an issue with core router
if (
response.status === 200 &&
(response.body.length === 0 ||
response.headers["content-length"] === "0")
) {
throw new ProtocolError({
message:
"There was an issue communicating with Fauna. Response is empty. Please try again.",
httpStatus: 500,
});
}

let parsedResponse;
try {
parsedResponse = {
Expand Down Expand Up @@ -915,18 +928,25 @@ export class StreamClient<T extends QueryValue = any> {
const headers = {
Authorization: `Bearer ${this.#clientConfiguration.secret}`,
};

const streamAdapter = this.#clientConfiguration.httpStreamClient.stream({
const request: HTTPStreamRequest = {
data: {
token: eventSource.token,
cursor: this.#last_cursor || this.#clientConfiguration.cursor,
},
headers,
method: "POST",
});

};
const streamAdapter =
this.#clientConfiguration.httpStreamClient.stream(request);
this.#streamAdapter = streamAdapter;

this.#clientConfiguration.logger.debug(
"Fauna HTTP %s request to '%s', headers: %s",
request.method,
FaunaAPIPaths.STREAM,
JSON.stringify(request.headers),
);

for await (const event of streamAdapter.read) {
// stream events are always tagged
const deserializedEvent: StreamEvent<T> = TaggedTypeFormat.decode(event, {
Expand Down Expand Up @@ -996,8 +1016,6 @@ export class FeedClient<T extends QueryValue = any> {
#query: () => Promise<EventSource>;
/** The event feed's client options */
#clientConfiguration: FeedClientConfiguration;
/** A LogHandler instance */
#logger: LogHandler;
/** The last `cursor` value received for the current page */
#lastCursor?: string;
/** A saved copy of the EventSource once received */
Expand Down Expand Up @@ -1026,7 +1044,6 @@ export class FeedClient<T extends QueryValue = any> {

this.#clientConfiguration = clientConfiguration;
this.#lastCursor = clientConfiguration.cursor;
this.#logger = clientConfiguration.logger;

this.#validateConfiguration();
}
Expand Down Expand Up @@ -1098,10 +1115,10 @@ export class FeedClient<T extends QueryValue = any> {

const request: HTTPRequest<FeedRequest> = await this.#nextPageHttpRequest();

this.#logger.debug(
"Fauna HTTP %s Request to %s (timeout: %s), headers: %s",
this.#clientConfiguration.logger.debug(
"Fauna HTTP %s request to '%s' (timeout: %s), headers: %s",
request.method,
httpClient.getURL(),
FaunaAPIPaths.EVENT_FEED,
request.client_timeout_ms,
JSON.stringify(request.headers),
);
Expand All @@ -1110,10 +1127,10 @@ export class FeedClient<T extends QueryValue = any> {
maxBackoff: this.#clientConfiguration.max_backoff,
shouldRetry: (error) => error instanceof ThrottlingError,
});
this.#logger.debug(
"Fauna HTTP Response %s from %s, headers: %s",
this.#clientConfiguration.logger.debug(
"Fauna HTTP response '%s' from %s, headers: %s",
response.status,
httpClient.getURL(),
FaunaAPIPaths.EVENT_FEED,
JSON.stringify(response.headers),
);

Expand Down
4 changes: 0 additions & 4 deletions src/http-client/fetch-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ export class FetchClient implements HTTPClient, HTTPStreamClient {
return new URL(path, this.#baseUrl).toString();
}

getURL(): string {
return this.#resolveURL(this.#defaultRequestPath);
}

/** {@inheritDoc HTTPClient.request} */
async request<T = QueryRequest>({
data,
Expand Down
5 changes: 0 additions & 5 deletions src/http-client/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ export interface HTTPClient {
* is a no-op as there is no shared resource to close.
*/
close(): void;

/**
* Return the full URL (path and endpoint) for the query endpoint for this client.
*/
getURL(): string;
}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/http-client/node-http2-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ export class NodeHTTP2Client implements HTTPClient, HTTPStreamClient {
this.#session = null;
}

getURL(): string {
return this.#url;
}

/**
* Gets a {@link NodeHTTP2Client} matching the {@link HTTPClientOptions}
* @param httpClientOptions - the {@link HTTPClientOptions}
Expand Down
2 changes: 1 addition & 1 deletion src/util/package-version.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//THIS FILE IS AUTOGENERATED. DO NOT EDIT. SEE .husky/pre-commit

/** The current package version. */
export const packageVersion = "2.4.0";
export const packageVersion = "2.4.1";

0 comments on commit cbacb1c

Please sign in to comment.