cf-workers-query
Version:
Automatically cache and revalidate data in Cloudflare Workers. Using the Cache API and Execution Context
35 lines (33 loc) • 867 B
JavaScript
import {
__name
} from "../chunk-SHUYVCID.js";
// src/lib/durable-object-deduper.ts
import { DurableObject } from "cloudflare:workers";
var QueryDeduper = class extends DurableObject {
static {
__name(this, "QueryDeduper");
}
inflight = /* @__PURE__ */ new Map();
async dedupe(request) {
const key = new URL(request.url).searchParams.get("key");
if (!key) {
return new Response('Missing "key" query parameter', {
status: 400
});
}
const existing = this.inflight.get(key);
if (existing) return existing;
const promise = this.executeQuery(request).finally(() => {
this.inflight.delete(key);
});
this.inflight.set(key, promise);
return promise;
}
async executeQuery(request) {
const body = await request.json();
return fetch(body.url, body.init);
}
};
export {
QueryDeduper
};