Skip to content

Commit 35d49a3

Browse files
authored
net - fix: making it so fetch is cached (#1292)
* net - fix: making it so fetch is cached * Update cache.test.ts
1 parent 3547d1e commit 35d49a3

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

packages/cacheable-request/test/cache.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,10 @@ test("return with protocol", async () => {
350350

351351
test("hostname over host", async () => {
352352
const options = {
353-
host: "mockhttp.org",
354-
hostname: "cacheable.org",
353+
host: "example.org",
354+
hostname: "mockhttp.org",
355355
};
356-
const expected = "GET:http://cacheable.org";
356+
const expected = "GET:http://mockhttp.org";
357357
const expectKey = `cacheable-request:${expected}`;
358358
const actualKey = await testCacheKeyReturn(options);
359359
expect(actualKey).toBe(expectKey);

packages/net/src/fetch.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,38 @@ export async function fetch(
2929
cache: "no-cache",
3030
};
3131

32-
return options.cache.getOrSet(url, async () => {
32+
// Create a cache key that includes the method
33+
const cacheKey = `${options.method || "GET"}:${url}`;
34+
35+
const cachedData = await options.cache.getOrSet(cacheKey, async () => {
3336
// Perform the fetch operation
3437
const response = await undiciFetch(url, fetchOptions);
3538
/* c8 ignore next 3 */
3639
if (!response.ok) {
3740
throw new Error(`Fetch failed with status ${response.status}`);
3841
}
3942

40-
return response;
41-
}) as Promise<UndiciResponse>;
43+
// Convert response to cacheable format
44+
const body = await response.text();
45+
return {
46+
body,
47+
status: response.status,
48+
statusText: response.statusText,
49+
headers: Object.fromEntries(response.headers.entries()),
50+
};
51+
});
52+
53+
// Reconstruct Response object from cached data
54+
/* c8 ignore next 3 */
55+
if (!cachedData) {
56+
throw new Error("Failed to get or set cache data");
57+
}
58+
59+
return new Response(cachedData.body, {
60+
status: cachedData.status,
61+
statusText: cachedData.statusText,
62+
headers: cachedData.headers,
63+
}) as UndiciResponse;
4264
}
4365

4466
export type Response = UndiciResponse;

packages/net/test/fetch.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ describe("Fetch", () => {
3131
cache,
3232
};
3333
const response = await fetch(url, options);
34+
const response2 = await fetch(url, options);
3435
expect(response).toBeDefined();
36+
expect(response2).toBeDefined();
37+
expect(cache.stats).toBeDefined();
38+
expect(cache.stats.hits).toBe(1);
39+
// Verify that both responses have the same text content
40+
const text1 = await response.text();
41+
const text2 = await response2.text();
42+
expect(text1).toEqual(text2);
3543
},
3644
testTimeout,
3745
);

0 commit comments

Comments
 (0)