From ea5b9327f43e43316db17f75b38cba084e915566 Mon Sep 17 00:00:00 2001 From: Johan Preynat Date: Mon, 24 Jun 2024 15:24:15 +0200 Subject: [PATCH] Revert "Make reading global context sync to test" This reverts commit 91cc2466da71b820b65823e383b9932e4dee1189. --- src/lib/async.ts | 6 +++--- src/lib/cache/memory.ts | 14 +++++++------- src/lib/waitUntil.ts | 10 +++++----- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/lib/async.ts b/src/lib/async.ts index 58ada602c..3e99ddff2 100644 --- a/src/lib/async.ts +++ b/src/lib/async.ts @@ -238,7 +238,7 @@ export function singleton(execute: () => Promise): () => Promise { } // Promises are not shared between requests in Cloudflare Workers - const ctx = getRequestContext(); + const ctx = await getRequestContext(); const current = states.get(ctx); if (current) { return current; @@ -269,7 +269,7 @@ export function singletonMap( const states = new WeakMap>>(); const fn: SingletonFunction = async (key, ...args) => { - const ctx = getRequestContext(); + const ctx = await getRequestContext(); let current = states.get(ctx); if (current) { const existing = current.get(key); @@ -292,7 +292,7 @@ export function singletonMap( }; fn.isRunning = async (key: string) => { - const ctx = getRequestContext(); + const ctx = await getRequestContext(); const current = states.get(ctx); return current?.has(key) ?? false; }; diff --git a/src/lib/cache/memory.ts b/src/lib/cache/memory.ts index 82bcddcc1..ad62ee452 100644 --- a/src/lib/cache/memory.ts +++ b/src/lib/cache/memory.ts @@ -6,7 +6,7 @@ export const memoryCache: CacheBackend = { name: 'memory', replication: 'local', async get(key) { - const memoryCache = getMemoryCache(); + const memoryCache = await getMemoryCache(); const memoryEntry = memoryCache.get(key); console.log(`${memoryEntry ? 'memory hit' : 'memory miss'} for key: ${key}`) @@ -23,7 +23,7 @@ export const memoryCache: CacheBackend = { return null; }, async set(key, entry) { - const memoryCache = getMemoryCache(); + const memoryCache = await getMemoryCache(); // When the entry is immutable, we can cache it for the entire duration. // Else we cache it for a very short time. const expiresAt = @@ -43,11 +43,11 @@ export const memoryCache: CacheBackend = { memoryCache.set(key, { ...entry, meta }); }, async del(keys) { - const memoryCache = getMemoryCache(); + const memoryCache = await getMemoryCache(); keys.forEach((key) => memoryCache.delete(key)); }, async revalidateTags(tags) { - const memoryCache = getMemoryCache(); + const memoryCache = await getMemoryCache(); const keys: string[] = []; memoryCache.forEach((entry, key) => { @@ -67,10 +67,10 @@ export const memoryCache: CacheBackend = { /** * In memory cache shared globally. */ -function getMemoryCache(): Map { - const ctx: ReturnType & { +async function getMemoryCache(): Promise> { + const ctx: Awaited> & { gitbookMemoryCache?: Map; - } = getGlobalContext(); + } = await getGlobalContext(); if (ctx.gitbookMemoryCache) { console.log('getMemoryCache - cache already exists'); diff --git a/src/lib/waitUntil.ts b/src/lib/waitUntil.ts index 08fa5a330..6e2940f17 100644 --- a/src/lib/waitUntil.ts +++ b/src/lib/waitUntil.ts @@ -1,4 +1,3 @@ -import { getOptionalRequestContext } from '@cloudflare/next-on-pages'; import type { ExecutionContext, IncomingRequestCfProperties } from '@cloudflare/workers-types'; let pendings: Array> = []; @@ -8,14 +7,14 @@ let pendings: Array> = []; * This object can be used to store data that should be shared between the middleware and the handler * and re-used for all requests. */ -export function getGlobalContext(): ExecutionContext | object { +export async function getGlobalContext(): Promise { if (process.env.NODE_ENV === 'test') { // Do not try loading the next-on-pages package in tests as it'll fail return globalThis; } // We lazy-load the next-on-pages package to avoid errors when running tests because of 'server-only'. - // const { getOptionalRequestContext } = await import('@cloudflare/next-on-pages'); + const { getOptionalRequestContext } = await import('@cloudflare/next-on-pages'); return getOptionalRequestContext()?.ctx ?? globalThis; } @@ -23,13 +22,14 @@ export function getGlobalContext(): ExecutionContext | object { * Get a global context object for the current request. * This object can be used as a key to store request-specific data in a WeakMap. */ -export function getRequestContext(): IncomingRequestCfProperties | object { +export async function getRequestContext(): Promise { if (process.env.NODE_ENV === 'test') { // Do not try loading the next-on-pages package in tests as it'll fail return globalThis; } // We lazy-load the next-on-pages package to avoid errors when running tests because of 'server-only'. + const { getOptionalRequestContext } = await import('@cloudflare/next-on-pages'); return getOptionalRequestContext()?.cf ?? globalThis; } @@ -47,7 +47,7 @@ export async function waitUntil(promise: Promise) { return; } - const cloudflareContext = getGlobalContext(); + const cloudflareContext = await getGlobalContext(); if ('waitUntil' in cloudflareContext) { cloudflareContext.waitUntil(promise); } else {