Skip to content

Commit

Permalink
Support URLs as cache keys, fixes cloudflare#33
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot committed Aug 22, 2021
1 parent 633fedd commit e3f5d8a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/kv/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Headers, Request, Response } from "@mrbbot/node-fetch";
import { Headers, Request, RequestInfo, Response } from "@mrbbot/node-fetch";
import CachePolicy from "http-cache-semantics";
import { KVClock, defaultClock } from "./helpers";
import { KVStorageNamespace } from "./namespace";
Expand All @@ -15,8 +15,8 @@ export interface CachedResponse {
body: string;
}

function normaliseRequest(req: string | Request): Request {
return typeof req === "string" ? new Request(req) : req;
function normaliseRequest(req: RequestInfo): Request {
return req instanceof Request ? req : new Request(req);
}

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

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

// Cloudflare ignores request Cache-Control
Expand Down Expand Up @@ -124,7 +124,7 @@ export class Cache implements CacheInterface {
}

async match(
req: string | Request,
req: RequestInfo,
options?: CacheMatchOptions
): Promise<Response | undefined> {
req = normaliseRequest(req);
Expand All @@ -145,7 +145,7 @@ export class Cache implements CacheInterface {
}

async delete(
req: string | Request,
req: RequestInfo,
options?: CacheMatchOptions
): Promise<boolean> {
req = normaliseRequest(req);
Expand Down
11 changes: 7 additions & 4 deletions test/kv/cache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { HeadersInit } from "@mrbbot/node-fetch";
import { HeadersInit, RequestInfo } from "@mrbbot/node-fetch";
import anyTest, { Macro, TestInterface } from "ava";
import {
Cache,
Expand Down Expand Up @@ -34,7 +34,7 @@ const testResponse = new Response("value", {

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

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

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

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

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

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

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

0 comments on commit e3f5d8a

Please sign in to comment.