| title | Runtime |
|---|---|
| description | The runtime APIs available on the bunny.net platform. |
The bunny.net EdgeScript Runtime is based on Deno, so you can use a subset of what is available from Deno or Node. On top of that, we provide functions that change how the script behaves in our environment or bind it to other bunny.net services.
The waitUntil function extends the life of the isolate running a request. Use
it when a script needs to keep working after the request it answered has
finished. Even when no other requests are routed to the script, the invocation
stays alive.
It is useful for holding WebSocket connections open, refreshing a cache entry in the background, or firing off telemetry once the response has gone back to the client.
Bunny.v1.waitUntil(promise: Promise<unknown>): void;void. waitUntil does not return a value.
Return the response to the client immediately while a slower task, in this case populating the cache, finishes in the background.
import * as BunnySDK from "@bunny.net/edgescript-sdk";
BunnySDK.net.http.serve(async (request: Request): Promise<Response> => {
const url = new URL(request.url);
const cache = caches.default;
const cacheKey = new Request(url.toString(), { method: "GET" });
const hit = await cache.match(cacheKey);
if (hit) {
return hit;
}
const fresh = Response.json(
{ generatedAt: new Date().toISOString(), random: Math.random() },
{ headers: { "Cache-Control": "s-maxage=60" } },
);
// Don't block the response on the cache write. Let it finish after we
// return; the isolate stays alive until cache.put() resolves.
Bunny.v1.waitUntil(cache.put(cacheKey, fresh.clone()));
return fresh;
});