-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
333 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
"@comet/cms-api": minor | ||
--- | ||
|
||
Set content scopes in request object | ||
|
||
This allows accessing the affected content scopes inside a block's transformer service. | ||
|
||
**Example** | ||
|
||
```ts | ||
import { Inject, Injectable } from "@nestjs/common"; | ||
import { CONTEXT } from "@nestjs/graphql"; | ||
|
||
/* ... */ | ||
|
||
@Injectable() | ||
export class PixelImageBlockTransformerService implements BlockTransformerServiceInterface<PixelImageBlockData, TransformResponse> { | ||
constructor( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
@Inject(CONTEXT) private readonly context: any, | ||
) {} | ||
|
||
async transformToPlain(block: PixelImageBlockData) { | ||
// Get the affected content scopes | ||
const contentScopes = this.context.req.contentScopes; | ||
|
||
// Do something with the content scopes | ||
|
||
/* ... */ | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@comet/cms-site": minor | ||
--- | ||
|
||
gql: Handle non-string variables in GraphQL documents | ||
|
||
Non-string variables were incorrectly converted to strings, e.g., `'[object Object]'`. This error usually occurred when trying to import a GraphQL fragment from a React Client Component. The `gql` helper now throws an error for non-string variables. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@comet/cms-site": patch | ||
--- | ||
|
||
GraphQLFetch: Correctly report GraphQL schema validation errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* eslint-disable no-console */ | ||
import { Redis } from "ioredis"; | ||
import { LRUCache } from "lru-cache"; | ||
import { CacheHandler as NextCacheHandler } from "next/dist/server/lib/incremental-cache"; | ||
|
||
const REDIS_HOST = process.env.REDIS_HOST; | ||
if (!REDIS_HOST) { | ||
throw new Error("REDIS_HOST is required"); | ||
} | ||
|
||
const REDIS_PORT = parseInt(process.env.REDIS_PORT || "6379", 10); | ||
|
||
const REDIS_PASSWORD = process.env.REDIS_PASSWORD; | ||
if (!REDIS_PASSWORD) { | ||
throw new Error("REDIS_PASSWORD is required"); | ||
} | ||
|
||
const REDIS_KEY_PREFIX = process.env.REDIS_KEY_PREFIX || ""; | ||
|
||
const REDIS_ENABLE_AUTOPIPELINING = process.env.REDIS_ENABLE_AUTOPIPELINING === "true"; | ||
|
||
const CACHE_HANDLER_DEBUG = process.env.CACHE_HANDLER_DEBUG === "true"; | ||
|
||
const CACHE_TTL_IN_S = 24 * 60 * 60; // 1 day | ||
|
||
const redis = new Redis({ | ||
commandTimeout: 1000, | ||
enableOfflineQueue: false, | ||
host: REDIS_HOST, | ||
keyPrefix: REDIS_KEY_PREFIX, | ||
password: REDIS_PASSWORD, | ||
port: REDIS_PORT, | ||
socketTimeout: 1000, | ||
enableAutoPipelining: REDIS_ENABLE_AUTOPIPELINING, // https://github.com/redis/ioredis?tab=readme-ov-file#autopipelining | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const fallbackCache = new LRUCache<string, any>({ | ||
maxSize: 50 * 1024 * 1024, // 50MB | ||
ttl: CACHE_TTL_IN_S * 1000, | ||
ttlAutopurge: true, | ||
}); | ||
|
||
let isFallbackInUse = false; | ||
|
||
function parseBodyForGqlError(body: string) { | ||
try { | ||
const decodedBody = Buffer.from(body, "base64").toString("utf-8"); | ||
if (!decodedBody.startsWith("{")) return null; // Not a JSON response, ignore | ||
return JSON.parse(decodedBody); | ||
} catch (error) { | ||
console.error("CacheHandler.parseBodyForGqlError error", error); | ||
return null; | ||
} | ||
} | ||
|
||
export default class CacheHandler extends NextCacheHandler { | ||
//constructor(_ctx: NextCacheHandlerContext) {} | ||
|
||
async get( | ||
key: string, | ||
//ctx: Parameters<NextCacheHandler["get"]>[1] | ||
): ReturnType<NextCacheHandler["get"]> { | ||
if (redis.status === "ready") { | ||
try { | ||
if (CACHE_HANDLER_DEBUG) { | ||
console.log("CacheHandler.get redis", key); | ||
} | ||
|
||
const redisResponse = await redis.get(key); | ||
if (isFallbackInUse) { | ||
isFallbackInUse = false; | ||
console.info(`${new Date().toISOString()} [${REDIS_HOST} up] Switching back to redis cache`); | ||
} | ||
if (!redisResponse) { | ||
return null; | ||
} | ||
return JSON.parse(redisResponse); | ||
} catch (e) { | ||
console.error("CacheHandler.get error", e); | ||
} | ||
} | ||
|
||
if (CACHE_HANDLER_DEBUG) { | ||
console.log("CacheHandler.get fallbackCache", key); | ||
} | ||
|
||
// fallback to in-memory cache | ||
if (!isFallbackInUse) { | ||
console.warn(`${new Date().toISOString()} | [${REDIS_HOST} down] switching to fallback in-memory cache`); | ||
isFallbackInUse = true; | ||
} | ||
|
||
return fallbackCache.get(key) ?? null; | ||
} | ||
|
||
async set( | ||
key: string, | ||
value: Parameters<NextCacheHandler["set"]>[1], | ||
// ctx: Parameters<NextCacheHandler["set"]>[2], | ||
): Promise<void> { | ||
if (value?.kind === "FETCH") { | ||
const responseBody = parseBodyForGqlError(value.data.body); | ||
if (responseBody?.errors) { | ||
// Must not cache GraphQL errors | ||
console.error("CacheHandler.set GraphQL Error: ", responseBody.error); | ||
return; | ||
} | ||
} | ||
|
||
const stringData = JSON.stringify({ | ||
lastModified: Date.now(), | ||
value, | ||
}); | ||
|
||
if (redis.status === "ready") { | ||
try { | ||
if (CACHE_HANDLER_DEBUG) { | ||
console.log("CacheHandler.set redis", key); | ||
} | ||
await redis.set(key, stringData, "EX", CACHE_TTL_IN_S); | ||
} catch (e) { | ||
console.error("CacheHandler.set error", e); | ||
} | ||
} | ||
if (CACHE_HANDLER_DEBUG) { | ||
console.log("CacheHandler.set fallbackCache", key); | ||
} | ||
fallbackCache.set(key, value, { size: stringData.length }); | ||
} | ||
|
||
async revalidateTag(tags: string | string[]): Promise<void> { | ||
console.log("CacheHandler.revalidateTag", tags); | ||
throw new Error("unsupported"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.