mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 23:55:20 +00:00
a1da4f023d
Co-authored-by: Nicolas Dorseuil <nicolas@gitbook.io>
37 lines
1.2 KiB
JavaScript
37 lines
1.2 KiB
JavaScript
import { runWithCloudflareRequestContext } from '../../.open-next/cloudflare/init.js';
|
|
|
|
import { DurableObject } from 'cloudflare:workers';
|
|
|
|
// Only needed to run locally, in prod we'll use the one from do.js
|
|
export class R2WriteBuffer extends DurableObject {
|
|
writePromise;
|
|
|
|
async write(cacheKey, value) {
|
|
// We are already writing to this key
|
|
if (this.writePromise) {
|
|
return;
|
|
}
|
|
|
|
this.writePromise = this.env.NEXT_INC_CACHE_R2_BUCKET.put(cacheKey, value);
|
|
this.ctx.waitUntil(
|
|
this.writePromise.finally(() => {
|
|
this.writePromise = undefined;
|
|
})
|
|
);
|
|
}
|
|
}
|
|
|
|
export default {
|
|
async fetch(request, env, ctx) {
|
|
return runWithCloudflareRequestContext(request, env, ctx, async () => {
|
|
// We can't move the handler import to the top level, otherwise the runtime will not be properly initialized
|
|
const { handler } = await import(
|
|
'../../.open-next/server-functions/default/handler.mjs'
|
|
);
|
|
|
|
// - `Request`s are handled by the Next server
|
|
return handler(request, env, ctx);
|
|
});
|
|
},
|
|
};
|