|
| 1 | +import { zValidator } from "@hono/zod-validator"; |
| 2 | +import { |
| 3 | + ABBY_WINDOW_KEY, |
| 4 | + type AbbyData, |
| 5 | + hashStringToInt32, |
| 6 | + serializeAbbyData, |
| 7 | +} from "@tryabby/core"; |
| 8 | +import { type Context, Hono } from "hono"; |
| 9 | +import { cors } from "hono/cors"; |
| 10 | +import { endTime, startTime, timing } from "hono/timing"; |
| 11 | +import { transformFlagValue } from "lib/flags"; |
| 12 | +import { ConfigCache } from "server/common/config-cache"; |
| 13 | +import { prisma } from "server/db/client"; |
| 14 | +import { afterDataRequestQueue } from "server/queue/queues"; |
| 15 | +import { z } from "zod"; |
| 16 | + |
| 17 | +export const X_ABBY_CACHE_HEADER = "X-Abby-Cache"; |
| 18 | + |
| 19 | +async function getAbbyResponseWithCache({ |
| 20 | + environment, |
| 21 | + projectId, |
| 22 | + c, |
| 23 | +}: { |
| 24 | + environment: string; |
| 25 | + projectId: string; |
| 26 | + c: Context; |
| 27 | +}) { |
| 28 | + startTime(c, "readCache"); |
| 29 | + const cachedConfig = ConfigCache.getConfig({ |
| 30 | + environment, |
| 31 | + projectId, |
| 32 | + apiVersion: "v2", |
| 33 | + }); |
| 34 | + endTime(c, "readCache"); |
| 35 | + |
| 36 | + c.header(X_ABBY_CACHE_HEADER, cachedConfig !== undefined ? "HIT" : "MISS"); |
| 37 | + if (cachedConfig) { |
| 38 | + return serializeAbbyData(cachedConfig as AbbyData); |
| 39 | + } |
| 40 | + |
| 41 | + startTime(c, "db"); |
| 42 | + const [dbTests, dbFlags] = await Promise.all([ |
| 43 | + prisma.test.findMany({ |
| 44 | + where: { |
| 45 | + projectId, |
| 46 | + }, |
| 47 | + include: { options: { select: { chance: true } } }, |
| 48 | + }), |
| 49 | + prisma.featureFlagValue.findMany({ |
| 50 | + where: { |
| 51 | + environment: { |
| 52 | + name: environment, |
| 53 | + projectId, |
| 54 | + }, |
| 55 | + }, |
| 56 | + include: { flag: { select: { name: true, type: true } } }, |
| 57 | + }), |
| 58 | + ]); |
| 59 | + endTime(c, "db"); |
| 60 | + |
| 61 | + const flags = dbFlags.filter(({ flag }) => flag.type === "BOOLEAN"); |
| 62 | + |
| 63 | + const remoteConfigs = dbFlags.filter(({ flag }) => flag.type !== "BOOLEAN"); |
| 64 | + |
| 65 | + const response = { |
| 66 | + tests: dbTests.map((test) => ({ |
| 67 | + name: hashStringToInt32(test.name).toString(), |
| 68 | + weights: test.options.map((o) => o.chance.toNumber()), |
| 69 | + })), |
| 70 | + flags: flags.map((flagValue) => { |
| 71 | + return { |
| 72 | + name: hashStringToInt32(flagValue.flag.name).toString(), |
| 73 | + value: transformFlagValue(flagValue.value, flagValue.flag.type), |
| 74 | + }; |
| 75 | + }), |
| 76 | + remoteConfig: remoteConfigs.map((flagValue) => { |
| 77 | + return { |
| 78 | + name: hashStringToInt32(flagValue.flag.name).toString(), |
| 79 | + value: transformFlagValue(flagValue.value, flagValue.flag.type), |
| 80 | + }; |
| 81 | + }), |
| 82 | + } satisfies AbbyData; |
| 83 | + |
| 84 | + ConfigCache.setConfig({ |
| 85 | + environment, |
| 86 | + projectId, |
| 87 | + value: response, |
| 88 | + apiVersion: "v2", |
| 89 | + }); |
| 90 | + return serializeAbbyData(response); |
| 91 | +} |
| 92 | + |
| 93 | +export function makeV2ProjectDataRoute() { |
| 94 | + const app = new Hono() |
| 95 | + .get( |
| 96 | + "/:projectId", |
| 97 | + cors({ |
| 98 | + origin: "*", |
| 99 | + maxAge: 86400, |
| 100 | + }), |
| 101 | + zValidator( |
| 102 | + "query", |
| 103 | + z.object({ |
| 104 | + environment: z.string(), |
| 105 | + }) |
| 106 | + ), |
| 107 | + timing(), |
| 108 | + async (c) => { |
| 109 | + const projectId = c.req.param("projectId"); |
| 110 | + const { environment } = c.req.valid("query"); |
| 111 | + |
| 112 | + const now = performance.now(); |
| 113 | + |
| 114 | + try { |
| 115 | + startTime(c, "getAbbyResponseWithCache"); |
| 116 | + const response = await getAbbyResponseWithCache({ |
| 117 | + projectId, |
| 118 | + environment, |
| 119 | + c, |
| 120 | + }); |
| 121 | + endTime(c, "getAbbyResponseWithCache"); |
| 122 | + |
| 123 | + const duration = performance.now() - now; |
| 124 | + |
| 125 | + afterDataRequestQueue.add("after-data-request", { |
| 126 | + apiVersion: "V2", |
| 127 | + functionDuration: duration, |
| 128 | + projectId, |
| 129 | + }); |
| 130 | + |
| 131 | + return c.json(response); |
| 132 | + } catch (e) { |
| 133 | + console.error(e); |
| 134 | + return c.json({ error: "Internal server error" }, { status: 500 }); |
| 135 | + } |
| 136 | + } |
| 137 | + ) |
| 138 | + .get( |
| 139 | + "/:projectId/script.js", |
| 140 | + cors({ |
| 141 | + origin: "*", |
| 142 | + maxAge: 86400, |
| 143 | + }), |
| 144 | + zValidator( |
| 145 | + "query", |
| 146 | + z.object({ |
| 147 | + environment: z.string(), |
| 148 | + }) |
| 149 | + ), |
| 150 | + timing(), |
| 151 | + async (c) => { |
| 152 | + const projectId = c.req.param("projectId"); |
| 153 | + const { environment } = c.req.valid("query"); |
| 154 | + |
| 155 | + const now = performance.now(); |
| 156 | + |
| 157 | + try { |
| 158 | + startTime(c, "getAbbyResponseWithCache"); |
| 159 | + const response = await getAbbyResponseWithCache({ |
| 160 | + projectId, |
| 161 | + environment, |
| 162 | + c, |
| 163 | + }); |
| 164 | + endTime(c, "getAbbyResponseWithCache"); |
| 165 | + |
| 166 | + const jsContent = `window.${ABBY_WINDOW_KEY} = ${JSON.stringify( |
| 167 | + response |
| 168 | + )}`; |
| 169 | + |
| 170 | + const duration = performance.now() - now; |
| 171 | + |
| 172 | + afterDataRequestQueue.add("after-data-request", { |
| 173 | + apiVersion: "V2", |
| 174 | + functionDuration: duration, |
| 175 | + projectId, |
| 176 | + }); |
| 177 | + |
| 178 | + return c.text(jsContent, { |
| 179 | + headers: { |
| 180 | + "Content-Type": "application/javascript", |
| 181 | + }, |
| 182 | + }); |
| 183 | + } catch (e) { |
| 184 | + console.error(e); |
| 185 | + return c.json({ error: "Internal server error" }, { status: 500 }); |
| 186 | + } |
| 187 | + } |
| 188 | + ); |
| 189 | + return app; |
| 190 | +} |
0 commit comments