From 6aa9b372d595b0249ffe411257067a165058f461 Mon Sep 17 00:00:00 2001 From: Sergio Moya <1083296+smoya@users.noreply.github.com> Date: Wed, 2 Aug 2023 16:20:54 +0200 Subject: [PATCH] feat: enable manual caching for Netlify edge function that serves schemas (#2020) --- netlify.toml | 2 ++ netlify/edge-functions/serve-definitions.ts | 30 ++++++++++----------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/netlify.toml b/netlify.toml index 3f3d2beb535..ebcbcd3b212 100644 --- a/netlify.toml +++ b/netlify.toml @@ -23,6 +23,7 @@ [[edge_functions]] function = "serve-definitions" path = "/definitions/*" +cache = "manual" # Used by JSON Schema definitions fetched from schemastore.org [[redirects]] @@ -33,6 +34,7 @@ path = "/definitions/*" [[edge_functions]] function = "serve-definitions" path = "/schema-store/*" +cache = "manual" [[plugins]] package = "@netlify/plugin-nextjs" diff --git a/netlify/edge-functions/serve-definitions.ts b/netlify/edge-functions/serve-definitions.ts index 7e8abadbe86..e50747be643 100644 --- a/netlify/edge-functions/serve-definitions.ts +++ b/netlify/edge-functions/serve-definitions.ts @@ -7,10 +7,10 @@ const NR_METRICS_ENDPOINT = Deno.env.get("NR_METRICS_ENDPOINT") || "https://metr const URL_DEST_SCHEMAS = "https://raw.githubusercontent.com/asyncapi/spec-json-schemas/master/schemas"; const URL_DEST_DEFINITIONS = "https://raw.githubusercontent.com/asyncapi/spec-json-schemas/master/definitions"; -// Legitimate request: +// Legitimate request: // Patterns: / OR // OR /// // Examples: /definitions OR /schema-store/2.5.0-without-$id.json OR /definitions/2.4.0/info.json -// Non-legitimate request: +// Non-legitimate request: // Patterns: ///* // Examples: /definitions/asyncapi.yaml OR /schema-store/2.4.0.JSON (uppercase) // @@ -32,6 +32,7 @@ export default async (request: Request, context: Context) => { const isRequestingAFile = request.url.endsWith('.json'); if (isRequestingAFile) { + var metricName: string if (response.ok) { // Manually cloning the response so we can modify the headers as they are immutable response = new Response(response.body, response); @@ -40,20 +41,19 @@ export default async (request: Request, context: Context) => { // This lets tooling fetch the schemas directly from their URL. response.headers.set("Content-Type", "application/schema+json"); - // Sending metrics to NR. - const metric = newNRMetricCount("asyncapi.jsonschema.download.success", request, rewriteRequest) - - await sendMetricToNR(context, metric); + metricName = "asyncapi.jsonschema.download.success"; } else { // Notifying NR of the error. - const attributes = { - "responseStatus": response.status, - "responseStatusText": response.statusText, - }; - const metric = newNRMetricCount("asyncapi.jsonschema.download.error", request, rewriteRequest, attributes); - - await sendMetricToNR(context, metric); + metricName = "asyncapi.jsonschema.download.error"; } + + const metricAttributes = { + "responseStatus": response.status, + "responseStatusText": response.statusText, + }; + + // Sending metrics to NR. + await sendMetricToNR(context, newNRMetricCount(metricName, request, rewriteRequest, metricAttributes)); } return response; @@ -129,7 +129,7 @@ function newNRMetricCount(name: string, originalRequest: Request, rewriteRequest metric["interval.ms"] = 1; const splitPath = new URL(originalRequest.url).pathname.split("/"); - // Examples: + // Examples: // /definitions/2.4.0/info.json => file = info.json // /definitions/2.4.0.json => file = 2.4.0.json const file = splitPath.slice(-1).pop(); @@ -170,4 +170,4 @@ class NRMetric { this.value = value; this.timestamp = timestamp; } -} \ No newline at end of file +}