From 91cc2466da71b820b65823e383b9932e4dee1189 Mon Sep 17 00:00:00 2001 From: Johan Preynat Date: Mon, 24 Jun 2024 14:21:35 +0200 Subject: [PATCH] Make reading global context sync to test --- 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 3e99ddff2..58ada602c 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 = await getRequestContext(); + const ctx = 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 = await getRequestContext(); + const ctx = 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 = await getRequestContext(); + const ctx = 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 ad62ee452..82bcddcc1 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 = await getMemoryCache(); + const memoryCache = 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 = await getMemoryCache(); + const memoryCache = 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 = await getMemoryCache(); + const memoryCache = getMemoryCache(); keys.forEach((key) => memoryCache.delete(key)); }, async revalidateTags(tags) { - const memoryCache = await getMemoryCache(); + const memoryCache = getMemoryCache(); const keys: string[] = []; memoryCache.forEach((entry, key) => { @@ -67,10 +67,10 @@ export const memoryCache: CacheBackend = { /** * In memory cache shared globally. */ -async function getMemoryCache(): Promise> { - const ctx: Awaited> & { +function getMemoryCache(): Map { + const ctx: ReturnType & { gitbookMemoryCache?: Map; - } = await getGlobalContext(); + } = getGlobalContext(); if (ctx.gitbookMemoryCache) { console.log('getMemoryCache - cache already exists'); diff --git a/src/lib/waitUntil.ts b/src/lib/waitUntil.ts index 6e2940f17..08fa5a330 100644 --- a/src/lib/waitUntil.ts +++ b/src/lib/waitUntil.ts @@ -1,3 +1,4 @@ +import { getOptionalRequestContext } from '@cloudflare/next-on-pages'; import type { ExecutionContext, IncomingRequestCfProperties } from '@cloudflare/workers-types'; let pendings: Array> = []; @@ -7,14 +8,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 async function getGlobalContext(): Promise { +export function getGlobalContext(): ExecutionContext | object { 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; } @@ -22,14 +23,13 @@ export async function getGlobalContext(): Promise { * 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 async function getRequestContext(): Promise { +export function getRequestContext(): IncomingRequestCfProperties | object { 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 = await getGlobalContext(); + const cloudflareContext = getGlobalContext(); if ('waitUntil' in cloudflareContext) { cloudflareContext.waitUntil(promise); } else {