Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions modules/cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import process from 'node:process'
import { defineNuxtModule } from 'nuxt/kit'
import { defineNuxtModule, useRuntimeConfig } from 'nuxt/kit'
import { provider } from 'std-env'

// Storage key for fetch cache - must match shared/utils/fetch-cache-config.ts
Expand All @@ -14,9 +14,17 @@ export default defineNuxtModule({
return
}

const config = useRuntimeConfig()

nuxt.hook('nitro:config', nitroConfig => {
nitroConfig.storage = nitroConfig.storage || {}

const upstash = {
driver: 'upstash' as const,
url: config.upstash.redisRestUrl,
token: config.upstash.redisRestToken,
}
Comment on lines +17 to +26
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Missing null check when accessing config.upstash properties.

Accessing config.upstash.redisRestUrl and config.upstash.redisRestToken without verifying that config.upstash exists could throw a runtime error if the runtime configuration is not properly defined.

🛡️ Proposed fix to add defensive check
     const config = useRuntimeConfig()
 
     nuxt.hook('nitro:config', nitroConfig => {
       nitroConfig.storage = nitroConfig.storage || {}
 
-      const upstash = {
-        driver: 'upstash' as const,
-        url: config.upstash.redisRestUrl,
-        token: config.upstash.redisRestToken,
-      }
+      const upstash = config.upstash?.redisRestUrl && config.upstash?.redisRestToken
+        ? {
+            driver: 'upstash' as const,
+            url: config.upstash.redisRestUrl,
+            token: config.upstash.redisRestToken,
+          }
+        : undefined

Then update the storage assignment accordingly:

       const env = process.env.VERCEL_ENV
       nitroConfig.storage.atproto =
-        env === 'production' ? upstash : { driver: 'vercel-runtime-cache' }
+        env === 'production' && upstash ? upstash : { driver: 'vercel-runtime-cache' }

As per coding guidelines: "Ensure you write strictly type-safe code, for example by ensuring you always check when accessing an array value by index" — this principle extends to checking nested object properties.


// Main cache storage (for defineCachedFunction, etc.)
nitroConfig.storage.cache = {
...nitroConfig.storage.cache,
Expand All @@ -30,9 +38,8 @@ export default defineNuxtModule({
}

const env = process.env.VERCEL_ENV
nitroConfig.storage.atproto = {
driver: env === 'production' ? 'vercel-kv' : 'vercel-runtime-cache',
}
nitroConfig.storage.atproto =
env === 'production' ? upstash : { driver: 'vercel-runtime-cache' }
})
},
})
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"@unocss/nuxt": "66.6.0",
"@unocss/preset-wind4": "66.6.0",
"@upstash/redis": "1.36.1",
"@vercel/kv": "3.0.0",
"@vite-pwa/assets-generator": "1.0.2",
"@vite-pwa/nuxt": "1.1.0",
"@voidzero-dev/vite-plus-core": "0.0.0-833c515fa25cef20905a7f9affb156dfa6f151ab",
Expand Down
4 changes: 1 addition & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading