diff --git a/bun.lockb b/bun.lockb index 088eaa18b..5ee55e896 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 0f0a95ad1..2a6e38a53 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "dependencies": { "@geist-ui/icons": "^1.0.2", - "@gitbook/api": "^0.17.0", + "@gitbook/api": "^0.20.1", "@radix-ui/react-checkbox": "^1.0.4", "@radix-ui/react-popover": "^1.0.7", "@readme/openapi-parser": "^2.5.0", diff --git a/src/lib/api.ts b/src/lib/api.ts index 61650432a..6488f19e6 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -56,7 +56,6 @@ export const getPublishedContentByUrl = cache( // If the request is aborted, we don't need to make the API call // We call it as this logic is wrapped in an asynchronous cache that is not tied to the signal. - console.log('getPublishedContentByUrl', signal?.aborted); signal?.throwIfAborted(); const gitbook = new GitBookAPI({ diff --git a/src/lib/cache.ts b/src/lib/cache.ts index eef9cc5e9..dd5def9d0 100644 --- a/src/lib/cache.ts +++ b/src/lib/cache.ts @@ -12,7 +12,6 @@ const redis = : null; const memoryCache = new Map(); -const pendingOps = new Set>(); export interface CacheResult { data: Result; @@ -76,14 +75,6 @@ export function cacheResponse( }; } -/** - * Wait for all cache operations to be completed. - * This is a workaround until https://github.com/upstash/upstash-redis/issues/778 is fixed. - */ -export async function waitForCache() { - await Promise.all(pendingOps); -} - /** * Create a cache key from a function name and its arguments. */ @@ -100,9 +91,7 @@ async function getCacheValue(key: string) { } if (redis) { - console.log('getCacheValue', key); - const value = await wrapOperation(redis.get(key)); - console.log('done getCacheValue', key); + const value = await redis.get(key); return value; } @@ -116,22 +105,8 @@ async function setCacheValue(key: string, value: any, ttl: number) { memoryCache.set(key, value); if (redis) { - console.log('setCacheValue', key); - await wrapOperation( - redis.set(key, value, { - ex: ttl, - }), - ); - console.log('done setCacheValue', key); + await redis.set(key, value, { + ex: ttl, + }); } } - -/** - * Wrap a cache operation, it can be later awaited with `waitForCache`. - */ -function wrapOperation(op: Promise): Promise { - pendingOps.add(op); - return op.finally(() => { - pendingOps.delete(op); - }); -} diff --git a/src/middleware.ts b/src/middleware.ts index 103f2a704..3f7c5a179 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -235,57 +235,51 @@ async function lookupSpaceByAPI( } alternatives`, ); - const found = await new Promise((resolve, reject) => { - let resolved = false; + console.time('lookupSpaceByAPI'); + try { const abort = new AbortController(); - - Promise.all( + const matches = await Promise.all( lookupAlternatives.map(async (alternative) => { - console.log(`lookup content for url "${alternative.url}"`) - const data = await getPublishedContentByUrl( - alternative.url, - apiEndpoint, - visitorAuthToken, - { - signal: abort.signal, - }, - ); + try { + const data = await getPublishedContentByUrl( + alternative.url, + apiEndpoint, + visitorAuthToken, + { + signal: abort.signal, + }, + ); - if (resolved) { - return; - } - resolved = true; - console.log('aborting'); - abort.abort(); + if ('redirect' in data) { + if (alternative.url === url.toString()) { + return data; + } - if (alternative.url === url.toString()) { - resolve(data); - } else if (!('redirect' in data)) { - resolve({ + return null; + } + + // Cancel all other requests to speed up the lookup + abort.abort(); + return { space: data.space, basePath: data.basePath, pathname: joinPath(data.pathname, alternative.extraPath), apiToken: data.apiToken, - }); + } as PublishedContentLookup; + } catch (error) { + // @ts-ignore + if (error.name === 'AbortError') { + return null; + } + + throw error; } }), - ).catch((error) => { - if (error.name === 'AbortError') { - return; - } - - if (resolved) { - return; - } - - resolved = true; - reject(error); - }); - }); - - await waitForCache(); - - return found; + ); + return matches.find((match) => match !== null) ?? null; + } finally { + console.timeEnd('lookupSpaceByAPI'); + } } function computeLookupAlternatives(url: URL) {