Skip to content

Commit

Permalink
fix(agent): add method to computeURI (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken authored Sep 7, 2023
1 parent e6eca6d commit 6e6590b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
14 changes: 9 additions & 5 deletions src/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Agent, ProxyAgent, MockAgent } from "undici";
import { LRUCache } from "lru-cache";

// Import Internal Dependencies
import { InlineCallbackAction } from "./request";
import { InlineCallbackAction, HttpMethod, WebDavMethod } from "./request";
import { getCurrentEnv } from "./utils";

// CONSTANTS
Expand Down Expand Up @@ -97,9 +97,13 @@ export function detectAgentFromURI(uri: URL): CustomHttpAgent | null {
* @description Compute a given URI (format string or WHATWG URL) and return a fully build URL and paired agent.
* Under the hood it use a LRU cache
*/
export function computeURI(uri: string | URL): computedUrlAndAgent {
if (URICache.has(uri)) {
return URICache.get(uri)!;
export function computeURI(
method: HttpMethod | WebDavMethod,
uri: string | URL
): computedUrlAndAgent {
const uriStr = method.toUpperCase() + uri.toString();
if (URICache.has(uriStr)) {
return URICache.get(uriStr)!;
}

let response: computedUrlAndAgent;
Expand All @@ -111,7 +115,7 @@ export function computeURI(uri: string | URL): computedUrlAndAgent {

response = { url: uri, agent: agent?.agent ?? null, limit: agent?.limit };
}
URICache.set(uri, response);
URICache.set(uriStr, response);

return response;
}
2 changes: 1 addition & 1 deletion src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function request<T>(
): Promise<RequestResponse<T>> {
const { maxRedirections = 0 } = options;

const computedURI = computeURI(uri);
const computedURI = computeURI(method, uri);
if (typeof options.querystring !== "undefined") {
const qs = typeof options.querystring === "string" ? new URLSearchParams(options.querystring) : options.querystring;
for (const [key, value] of qs.entries()) {
Expand Down
4 changes: 2 additions & 2 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function pipeline(
): Duplex {
const { maxRedirections = 0 } = options;

const computedURI = computeURI(uri);
const computedURI = computeURI(method, uri);
if (typeof options.querystring !== "undefined") {
const qs = typeof options.querystring === "string" ? new URLSearchParams(options.querystring) : options.querystring;
for (const [key, value] of qs.entries()) {
Expand All @@ -43,7 +43,7 @@ export function stream(
options: StreamOptions = {}
): WritableStreamCallback {
const { maxRedirections = 0 } = options;
const computedURI = computeURI(uri);
const computedURI = computeURI(method, uri);

const dispatcher = options.agent ?? computedURI.agent ?? void 0;
const headers = Utils.createHeaders({ headers: options.headers, authorization: options.authorization });
Expand Down
24 changes: 17 additions & 7 deletions test/agents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,44 @@ describe("computeURI", () => {
});

it("should compute a windev URI (as string)", () => {
const result = Agents.computeURI(kWindevMonitoringURL);
const result = Agents.computeURI("GET", kWindevMonitoringURL);

expect(result.url.href).toStrictEqual(kWindevMonitoringURL);
expect(result.agent).toStrictEqual(windev.agent);

expect(Agents.URICache.has(kWindevMonitoringURL)).toStrictEqual(true);
expect(Agents.URICache.has("GET" + kWindevMonitoringURL)).toStrictEqual(true);
});

it("should compute a windev URI (as WHATWG URL)", () => {
const localURL = new URL(kWindevMonitoringURL);
const result = Agents.computeURI(localURL);
const result = Agents.computeURI("POST", localURL);

expect(result.url.href).toStrictEqual(kWindevMonitoringURL);
expect(result.agent).toStrictEqual(windev.agent);

expect(Agents.URICache.has(localURL)).toStrictEqual(true);
expect(Agents.URICache.has("POST" + localURL.toString())).toStrictEqual(true);
});

it("should return cached entry", () => {
Agents.URICache.set(kWindevMonitoringURL, true as any);
const result = Agents.computeURI(kWindevMonitoringURL) as unknown as boolean;
Agents.URICache.set("GET" + kWindevMonitoringURL, true as any);
const result = Agents.computeURI("GET", kWindevMonitoringURL) as unknown as boolean;

expect(result).toStrictEqual(true);
});

it("should not return cached entry because method doesn't match", () => {
Agents.URICache.set("POST" + kWindevMonitoringURL, true as any);
const result = Agents.computeURI("GET", kWindevMonitoringURL);

expect(result.url.href).toStrictEqual(kWindevMonitoringURL);
expect(result.agent).toStrictEqual(windev.agent);

expect(Agents.URICache.has("GET" + kWindevMonitoringURL)).toStrictEqual(true);
});

it("should compute an URL not related to any local agents", () => {
const stringURL = "https://www.linkedin.com/feed/";
const result = Agents.computeURI(new URL("", stringURL));
const result = Agents.computeURI("GET", new URL("", stringURL));

expect(result.url.href).toStrictEqual(stringURL);
expect(result.agent).toStrictEqual(null);
Expand Down

0 comments on commit 6e6590b

Please sign in to comment.