Merged in feat/sw-3596-console (pull request #3100)
feat(SW-3596): added lint rule for no console.log. Use logger instead. * feat(SW-3596): added lint rule for no console.log. Use logger instead. Approved-by: Joakim Jäderberg
This commit is contained in:
@@ -1,23 +1,26 @@
|
||||
import crypto from "crypto"
|
||||
import jwt from "jsonwebtoken"
|
||||
|
||||
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
||||
import { safeTry } from "@scandic-hotels/common/utils/safeTry"
|
||||
|
||||
import {
|
||||
type WarmupFunctionsKey,
|
||||
warmupKeys,
|
||||
} from "@/services/warmup/warmupKeys"
|
||||
import { safeTry } from "@scandic-hotels/common/utils/safeTry"
|
||||
import { timeout } from "@/utils/timeout"
|
||||
|
||||
import type { Config, Context } from "@netlify/functions"
|
||||
|
||||
async function WarmupHandler(request: Request, context: Context) {
|
||||
const warmupLogger = createLogger("warmup")
|
||||
const [_, error] = await safeTry(validateRequest(request, context))
|
||||
|
||||
if (error) {
|
||||
if (error instanceof Error) {
|
||||
switch (error.message as ErrorCode) {
|
||||
case ErrorCodes.WARMUP_DISABLED:
|
||||
console.warn("[WARMUP] Warmup is disabled")
|
||||
warmupLogger.warn("Warmup is disabled")
|
||||
return
|
||||
case ErrorCodes.REQUEST_NOT_FOR_CURRENT_CONTEXT:
|
||||
// This is expected, this webhook will be called for all deployments
|
||||
@@ -26,19 +29,20 @@ async function WarmupHandler(request: Request, context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
console.error("[WARMUP] Warmup failed", error)
|
||||
warmupLogger.error("Warmup failed", error)
|
||||
return
|
||||
}
|
||||
|
||||
console.log("[WARMUP] Request is valid, starting warmup")
|
||||
warmupLogger.info("Request is valid, starting warmup")
|
||||
await performWarmup(context)
|
||||
console.log("[WARMUP] Warmup completed")
|
||||
warmupLogger.info("Warmup completed")
|
||||
}
|
||||
|
||||
async function validateRequest(
|
||||
request: Request,
|
||||
context: Context
|
||||
): Promise<true> {
|
||||
const warmupLogger = createLogger("warmup")
|
||||
if (request.method !== "POST") {
|
||||
throw new Error(ErrorCodes.METHOD_NOT_ALLOWED)
|
||||
}
|
||||
@@ -57,7 +61,7 @@ async function validateRequest(
|
||||
}
|
||||
|
||||
const body = await request.text()
|
||||
console.log("[WARMUP] Warmup body", body)
|
||||
warmupLogger.info("Warmup body", body)
|
||||
const deployment = JSON.parse(body) as DeploymentInfo
|
||||
if (!deployment) {
|
||||
throw new Error(ErrorCodes.UNABLE_TO_PARSE_DEPLOYMENT_INFO)
|
||||
@@ -72,7 +76,7 @@ async function validateRequest(
|
||||
throw new Error(ErrorCodes.REQUEST_NOT_FOR_CURRENT_CONTEXT)
|
||||
}
|
||||
|
||||
console.log("[WARMUP] Warmup request", deployment)
|
||||
warmupLogger.info("Warmup request", deployment)
|
||||
let signature: string
|
||||
try {
|
||||
const headerValue = request.headers.get("x-webhook-signature")
|
||||
@@ -81,7 +85,7 @@ async function validateRequest(
|
||||
}
|
||||
signature = headerValue
|
||||
} catch (e) {
|
||||
console.warn("[WARMUP] Failed to parse signature", e)
|
||||
warmupLogger.warn("Failed to parse signature", e)
|
||||
throw new Error(ErrorCodes.FAILED_TO_PARSE_SIGNATURE)
|
||||
}
|
||||
|
||||
@@ -99,8 +103,9 @@ async function validateRequest(
|
||||
}
|
||||
|
||||
export async function performWarmup(context: Context) {
|
||||
const warmupLogger = createLogger("warmup")
|
||||
for (const key of warmupKeys) {
|
||||
console.log("[WARMUP] Warming up cache", key)
|
||||
warmupLogger.info("Warming up cache", key)
|
||||
await callWarmup(key, context)
|
||||
// allow api to catch up
|
||||
await timeout(1000)
|
||||
@@ -108,6 +113,7 @@ export async function performWarmup(context: Context) {
|
||||
}
|
||||
|
||||
async function callWarmup(key: WarmupFunctionsKey, context: Context) {
|
||||
const warmupLogger = createLogger("warmup")
|
||||
const baseUrl = context.url.origin
|
||||
|
||||
const url = new URL("/api/web/warmup", baseUrl)
|
||||
@@ -126,13 +132,13 @@ async function callWarmup(key: WarmupFunctionsKey, context: Context) {
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
console.error(
|
||||
`[WARMUP] Warmup failed '${url.href}' with error: ${response.status}: ${response.statusText}`
|
||||
warmupLogger.error(
|
||||
`Warmup failed '${url.href}' with error: ${response.status}: ${response.statusText}`
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`[WARMUP] Warmup failed '${url.href}' with error: ${error instanceof Error ? error.message : error}`
|
||||
warmupLogger.error(
|
||||
`Warmup failed '${url.href}' with error: ${error instanceof Error ? error.message : error}`
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -143,6 +149,7 @@ export const config: Config = {
|
||||
}
|
||||
|
||||
async function validateSignature(token: string, buffer: string) {
|
||||
const warmupLogger = createLogger("warmup")
|
||||
try {
|
||||
const secret =
|
||||
process.env.WARMUP_SIGNATURE_SECRET ||
|
||||
@@ -160,15 +167,15 @@ async function validateSignature(token: string, buffer: string) {
|
||||
const hashedBody = crypto.createHash("sha256").update(buffer).digest("hex")
|
||||
|
||||
if (!hasSha256(decoded)) {
|
||||
console.error(
|
||||
"[WARMUP] Decoded jwt does not contain sha256, unable to verify signature"
|
||||
warmupLogger.error(
|
||||
"Decoded jwt does not contain sha256, unable to verify signature"
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
return decoded.sha256 === hashedBody
|
||||
} catch (error) {
|
||||
console.error("[WARMUP] Failed to validate signature", error)
|
||||
warmupLogger.error("Failed to validate signature", error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user