You can add your own data to `req.cf`, then detect that.
You can add your own data to
req.cf, then detect that.req.cfcontext.waitUntil on the global to improve it's ergonomics or does it need to be coupled with a specific request? e.g.
ctx or waitUntil / env into various modules, creating a lot of boilerplate for call sites
itty-durableitty-durable is not needed, it just makes the code way shorter).fetch on a Service Worker binding do anything with the cache-control header?
wrangler dev), that goes to Worker #2 in remote mode (wrangler dev --remote)?context.waitUntil// worker.js
import { myFetch } from './my-fetch';
export default {
async fetch(request, env, context) {
global.waitUntil = context.waitUntil;
return fetch(request);
},
};
// my-fetch.js
export const fetch = async (request) => {
const response = await fetch(request);
response.headers.append("Cache-Control", "s-maxage=10");
waitUntil(caches.default.put(request, response.clone()));
return response;
}ctxwaitUntilenvAsyncLocalStorageitty-durableitty-durable.fetchcache-controlawait ctx.env.IMAGE_SERVICE.fetch(
"https://image-service.worker/v1/avatars/test",
{
cf: {
// Both properties not in the request cf output in the image service worker.
serviceBinding: "api-service",
isInternal: true,
},
headers: {
// Does show up in
"api-key": ctx.env.IMAGE_SERVICE_API_KEY
},
},
);export default {
fetch(req) {
const connectingIp = req.headers.get("cf-connecting-ip");
return new Response("Connecting from: " + connectingIp, {
headers: {
"Content-Type": "text/plain"
}
});
}
}import { createDurable } from "itty-durable";
export class Scheduler extends createDurable({ autoPersist: true }) {
constructor(state, env) {
super(state, env);
}
create(name, doEverySeconds = 30) {
this.name = name;
this.doEverySeconds = doEverySeconds;
this.setAlarm(Date.now());
}
async alarm() {
console.log(`Doing ${this.name}...`);
this.setAlarm(Date.now() + this.doEverySeconds * 1000);
// do the thing...
}
}