Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit e3f5d8a

Browse files
committed
Support URLs as cache keys, fixes #33
1 parent 633fedd commit e3f5d8a

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/kv/cache.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Headers, Request, Response } from "@mrbbot/node-fetch";
1+
import { Headers, Request, RequestInfo, Response } from "@mrbbot/node-fetch";
22
import CachePolicy from "http-cache-semantics";
33
import { KVClock, defaultClock } from "./helpers";
44
import { KVStorageNamespace } from "./namespace";
@@ -15,8 +15,8 @@ export interface CachedResponse {
1515
body: string;
1616
}
1717

18-
function normaliseRequest(req: string | Request): Request {
19-
return typeof req === "string" ? new Request(req) : req;
18+
function normaliseRequest(req: RequestInfo): Request {
19+
return req instanceof Request ? req : new Request(req);
2020
}
2121

2222
// Normalises headers to object mapping lower-case names to single values.
@@ -55,7 +55,7 @@ export class Cache implements CacheInterface {
5555
this.#namespace = new KVStorageNamespace(storage, clock);
5656
}
5757

58-
async put(req: string | Request, res: Response): Promise<undefined> {
58+
async put(req: RequestInfo, res: Response): Promise<undefined> {
5959
req = normaliseRequest(req);
6060

6161
// Cloudflare ignores request Cache-Control
@@ -124,7 +124,7 @@ export class Cache implements CacheInterface {
124124
}
125125

126126
async match(
127-
req: string | Request,
127+
req: RequestInfo,
128128
options?: CacheMatchOptions
129129
): Promise<Response | undefined> {
130130
req = normaliseRequest(req);
@@ -145,7 +145,7 @@ export class Cache implements CacheInterface {
145145
}
146146

147147
async delete(
148-
req: string | Request,
148+
req: RequestInfo,
149149
options?: CacheMatchOptions
150150
): Promise<boolean> {
151151
req = normaliseRequest(req);

test/kv/cache.spec.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "assert";
2-
import { HeadersInit } from "@mrbbot/node-fetch";
2+
import { HeadersInit, RequestInfo } from "@mrbbot/node-fetch";
33
import anyTest, { Macro, TestInterface } from "ava";
44
import {
55
Cache,
@@ -34,7 +34,7 @@ const testResponse = new Response("value", {
3434

3535
// Cache:* tests adapted from Cloudworker:
3636
// https://github.com/dollarshaveclub/cloudworker/blob/master/lib/runtime/cache/__tests__/cache.test.js
37-
const putMacro: Macro<[string | Request], Context> = async (t, req) => {
37+
const putMacro: Macro<[RequestInfo], Context> = async (t, req) => {
3838
const { storage, cache } = t.context;
3939
await cache.put(req, testResponse.clone());
4040

@@ -54,6 +54,7 @@ const putMacro: Macro<[string | Request], Context> = async (t, req) => {
5454
putMacro.title = (providedTitle) => `Cache: puts ${providedTitle}`;
5555
test("request", putMacro, new Request("http://localhost:8787/test"));
5656
test("string request", putMacro, "http://localhost:8787/test");
57+
test("url request", putMacro, new URL("http://localhost:8787/test"));
5758

5859
test("Cache: only puts GET requests", async (t) => {
5960
const { storage, cache } = t.context;
@@ -69,7 +70,7 @@ test("Cache: only puts GET requests", async (t) => {
6970
);
7071
});
7172

72-
const matchMacro: Macro<[string | Request], Context> = async (t, req) => {
73+
const matchMacro: Macro<[RequestInfo], Context> = async (t, req) => {
7374
const { cache } = t.context;
7475
await cache.put(
7576
new Request("http://localhost:8787/test"),
@@ -87,6 +88,7 @@ const matchMacro: Macro<[string | Request], Context> = async (t, req) => {
8788
matchMacro.title = (providedTitle) => `Cache: matches ${providedTitle}`;
8889
test("request", matchMacro, new Request("http://localhost:8787/test"));
8990
test("string request", matchMacro, "http://localhost:8787/test");
91+
test("url request", matchMacro, new URL("http://localhost:8787/test"));
9092

9193
test("Cache: only matches non-GET requests when ignoring method", async (t) => {
9294
const { cache } = t.context;
@@ -99,7 +101,7 @@ test("Cache: only matches non-GET requests when ignoring method", async (t) => {
99101
t.not(await cache.match(req, { ignoreMethod: true }), undefined);
100102
});
101103

102-
const deleteMacro: Macro<[string | Request], Context> = async (t, req) => {
104+
const deleteMacro: Macro<[RequestInfo], Context> = async (t, req) => {
103105
const { storage, cache } = t.context;
104106
await cache.put(
105107
new Request("http://localhost:8787/test"),
@@ -113,6 +115,7 @@ const deleteMacro: Macro<[string | Request], Context> = async (t, req) => {
113115
deleteMacro.title = (providedTitle) => `Cache: deletes ${providedTitle}`;
114116
test("request", deleteMacro, new Request("http://localhost:8787/test"));
115117
test("string request", deleteMacro, "http://localhost:8787/test");
118+
test("url request", deleteMacro, new URL("http://localhost:8787/test"));
116119

117120
test("Cache: only deletes non-GET requests when ignoring method", async (t) => {
118121
const { cache } = t.context;

0 commit comments

Comments
 (0)