diff --git a/.eslintrc.cjs b/.eslintrc.cjs index a57974b12..3e0895a90 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -15,7 +15,7 @@ module.exports = { ], "sonarjs/elseif-without-else": "warn", "sonarjs/no-duplicate-string": "warn", - "sonarjs/cognitive-complexity": "warn", + "sonarjs/cognitive-complexity": ["warn", 35], // We add some typescript rules - The recommended rules breaks too much stuff // TODO: We should add more rules, especially around typescript types @@ -29,8 +29,6 @@ module.exports = { ], "@typescript-eslint/unbound-method": "error", - - "@typescript-eslint/no-non-null-assertion": "warn", }, overrides: [ { diff --git a/packages/open-next/src/adapters/cache.ts b/packages/open-next/src/adapters/cache.ts index 716c0d926..124649791 100644 --- a/packages/open-next/src/adapters/cache.ts +++ b/packages/open-next/src/adapters/cache.ts @@ -284,80 +284,95 @@ export default class S3Cache { .getStore() ?.pendingPromiseRunner.withResolvers(); try { - if (data?.kind === "ROUTE" || data?.kind === "APP_ROUTE") { - const { body, status, headers } = data; - await globalThis.incrementalCache.set( - key, - { - type: "route", - body: body.toString( - isBinaryContentType(String(headers["content-type"])) - ? "base64" - : "utf8", - ), - meta: { - status, - headers, - }, - }, - false, - ); - } else if (data?.kind === "PAGE" || data?.kind === "PAGES") { - const { html, pageData, status, headers } = data; - const isAppPath = typeof pageData === "string"; - if (isAppPath) { - await globalThis.incrementalCache.set( - key, - { - type: "app", - html, - rsc: pageData, - meta: { - status, - headers, + if (data === null || data === undefined) { + await globalThis.incrementalCache.delete(key); + } else { + switch (data.kind) { + case "ROUTE": + case "APP_ROUTE": + const { body, status, headers } = data; + await globalThis.incrementalCache.set( + key, + { + type: "route", + body: body.toString( + isBinaryContentType(String(headers["content-type"])) + ? "base64" + : "utf8", + ), + meta: { + status, + headers, + }, }, - }, - false, - ); - } else { - await globalThis.incrementalCache.set( - key, - { - type: "page", - html, - json: pageData, - }, - false, - ); + false, + ); + break; + case "PAGE": + case "PAGES": { + const { html, pageData, status, headers } = data; + const isAppPath = typeof pageData === "string"; + if (isAppPath) { + await globalThis.incrementalCache.set( + key, + { + type: "app", + html, + rsc: pageData, + meta: { + status, + headers, + }, + }, + false, + ); + } else { + await globalThis.incrementalCache.set( + key, + { + type: "page", + html, + json: pageData, + }, + false, + ); + } + break; + } + case "APP_PAGE": { + const { html, rscData, headers, status } = data; + await globalThis.incrementalCache.set( + key, + { + type: "app", + html, + rsc: rscData.toString("utf8"), + meta: { + status, + headers, + }, + }, + false, + ); + break; + } + case "FETCH": + await globalThis.incrementalCache.set(key, data, true); + break; + case "REDIRECT": + await globalThis.incrementalCache.set( + key, + { + type: "redirect", + props: data.props, + }, + false, + ); + break; + case "IMAGE": + // Not implemented + break; } - } else if (data?.kind === "APP_PAGE") { - const { html, rscData, headers, status } = data; - await globalThis.incrementalCache.set( - key, - { - type: "app", - html, - rsc: rscData.toString("utf8"), - meta: { - status, - headers, - }, - }, - false, - ); - } else if (data?.kind === "FETCH") { - await globalThis.incrementalCache.set(key, data, true); - } else if (data?.kind === "REDIRECT") { - await globalThis.incrementalCache.set( - key, - { - type: "redirect", - props: data.props, - }, - false, - ); - } else if (data === null || data === undefined) { - await globalThis.incrementalCache.delete(key); } // Write derivedTags to dynamodb // If we use an in house version of getDerivedTags in build we should use it here instead of next's one diff --git a/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts b/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts index 8d4776688..326a6a3f7 100644 --- a/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts +++ b/packages/open-next/src/adapters/plugins/without-routing/requestHandler.ts @@ -1,3 +1,4 @@ +/* eslint-disable unused-imports/no-unused-vars */ import { InternalEvent } from "types/open-next"; import { MiddlewareOutputEvent } from "../../../core/routingHandler"; diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts index 9723424ce..1a0a26020 100644 --- a/packages/open-next/src/build/copyTracedFiles.ts +++ b/packages/open-next/src/build/copyTracedFiles.ts @@ -24,6 +24,7 @@ function copyPatchFile(outputDir: string) { copyFileSync(patchFile, outputPatchFile); } +// eslint-disable-next-line sonarjs/cognitive-complexity export async function copyTracedFiles( buildOutputPath: string, packagePath: string, diff --git a/packages/open-next/src/build/generateOutput.ts b/packages/open-next/src/build/generateOutput.ts index c02d1c8e1..2cf97e3b7 100644 --- a/packages/open-next/src/build/generateOutput.ts +++ b/packages/open-next/src/build/generateOutput.ts @@ -92,6 +92,8 @@ interface OpenNextOutput { }; } +const indexHandler = "index.hander"; + async function canStream(opts: FunctionOptions) { if (!opts.override?.wrapper) { return false; @@ -161,6 +163,7 @@ function prefixPattern(basePath: string) { }; } +// eslint-disable-next-line sonarjs/cognitive-complexity export async function generateOutput( outputPath: string, config: OpenNextConfig, @@ -183,7 +186,7 @@ export async function generateOutput( if (value.placement === "global") { edgeFunctions[key] = { bundle: `.open-next/functions/${key}`, - handler: "index.handler", + handler: indexHandler, ...(await extractOverrideFn(value.override)), }; } @@ -228,7 +231,7 @@ export async function generateOutput( }, imageOptimizer: { type: "function", - handler: "index.handler", + handler: indexHandler, bundle: ".open-next/image-optimization-function", streaming: false, imageLoader: await extractOverrideName( @@ -247,7 +250,7 @@ export async function generateOutput( } : { type: "function", - handler: "index.handler", + handler: indexHandler, bundle: ".open-next/server-functions/default", streaming: defaultOriginCanstream, ...(await extractOverrideFn(config.default.override)), @@ -274,7 +277,7 @@ export async function generateOutput( const streaming = await canStream(value); origins[key] = { type: "function", - handler: "index.handler", + handler: indexHandler, bundle: `.open-next/server-functions/${key}`, streaming, ...(await extractOverrideFn(value.override)), @@ -348,19 +351,19 @@ export async function generateOutput( disableIncrementalCache: config.dangerous?.disableIncrementalCache, disableTagCache: config.dangerous?.disableTagCache, warmer: { - handler: "index.handler", + handler: indexHandler, bundle: ".open-next/warmer-function", }, initializationFunction: isTagCacheDisabled ? undefined : { - handler: "index.handler", + handler: indexHandler, bundle: ".open-next/dynamodb-provider", }, revalidationFunction: config.dangerous?.disableIncrementalCache ? undefined : { - handler: "index.handler", + handler: indexHandler, bundle: ".open-next/revalidation-function", }, }, diff --git a/packages/open-next/src/core/routing/i18n/accept-header.ts b/packages/open-next/src/core/routing/i18n/accept-header.ts index fe0dd903f..aa2a181b4 100644 --- a/packages/open-next/src/core/routing/i18n/accept-header.ts +++ b/packages/open-next/src/core/routing/i18n/accept-header.ts @@ -13,6 +13,7 @@ interface Options { type: "accept-language"; } +// eslint-disable-next-line sonarjs/cognitive-complexity function parse( raw: string, preferences: string[] | undefined, diff --git a/packages/open-next/src/core/routingHandler.ts b/packages/open-next/src/core/routingHandler.ts index 1c4e72638..6641f9d84 100644 --- a/packages/open-next/src/core/routingHandler.ts +++ b/packages/open-next/src/core/routingHandler.ts @@ -26,7 +26,7 @@ export interface MiddlewareOutputEvent { // Add the locale prefix to the regex so we correctly match the rawPath const optionalLocalePrefixRegex = !!RoutesManifest.locales.length - ? `^/(?:${RoutesManifest.locales.map((locale) => `${locale}/?`).join("|")})?` + ? `^/(?:${RoutesManifest.locales.map((locale) => locale + "/?").join("|")})?` : "^/"; // Add the basepath prefix to the regex so we correctly match the rawPath diff --git a/packages/open-next/src/index.ts b/packages/open-next/src/index.ts index f8e933cba..748bb4c8d 100755 --- a/packages/open-next/src/index.ts +++ b/packages/open-next/src/index.ts @@ -18,6 +18,7 @@ function parseArgs() { acc[key] = undefined; } else if (self[ind + 1]) { acc[key] = self[ind + 1]; + // eslint-disable-next-line sonarjs/elseif-without-else } else if (!self[ind + 1]) { acc[key] = undefined; } diff --git a/packages/open-next/src/minimize-js.ts b/packages/open-next/src/minimize-js.ts index 9879ad503..899c46a31 100644 --- a/packages/open-next/src/minimize-js.ts +++ b/packages/open-next/src/minimize-js.ts @@ -1,3 +1,4 @@ +/* eslint-disable */ // Copied and modified from node-minify-all-js by Adones Pitogo // https://github.com/adonespitogo/node-minify-all-js/blob/master/index.js diff --git a/packages/open-next/src/plugins/edge.ts b/packages/open-next/src/plugins/edge.ts index 98861a7fa..bd376636d 100644 --- a/packages/open-next/src/plugins/edge.ts +++ b/packages/open-next/src/plugins/edge.ts @@ -57,7 +57,7 @@ export function openNextEdgePlugins({ }); } - build.onResolve({ filter: /\.(mjs|wasm)$/g }, (args) => { + build.onResolve({ filter: /\.(mjs|wasm)$/g }, () => { return { external: true, };