|
| 1 | +import Statsig from 'statsig-node-lite'; |
| 2 | + |
| 3 | +declare global { |
| 4 | + var EdgeRuntime: string | undefined; |
| 5 | +} |
| 6 | + |
| 7 | +export const isEdgeRuntime = (): boolean => { |
| 8 | + return EdgeRuntime !== undefined; |
| 9 | +}; |
| 10 | + |
| 11 | +/** |
| 12 | + * The Edge Config Data Adapter is an optional peer dependency that allows |
| 13 | + * the Statsig SDK to retrieve its data from Edge Config instead of over the network. |
| 14 | + */ |
| 15 | +export async function createEdgeConfigDataAdapter(options: { |
| 16 | + edgeConfigItemKey: string; |
| 17 | + edgeConfigConnectionString: string; |
| 18 | +}) { |
| 19 | + // Edge Config adapter requires `@vercel/edge-config` and `statsig-node-vercel` |
| 20 | + // Since it is a peer dependency, we will import it dynamically |
| 21 | + const { EdgeConfigDataAdapter } = await import('statsig-node-vercel'); |
| 22 | + const { createClient } = await import('@vercel/edge-config'); |
| 23 | + return new EdgeConfigDataAdapter({ |
| 24 | + edgeConfigItemKey: options.edgeConfigItemKey, |
| 25 | + edgeConfigClient: createClient(options.edgeConfigConnectionString, { |
| 26 | + // We disable the development cache as Statsig caches for 10 seconds internally, |
| 27 | + // and we want to avoid situations where Statsig tries to read the latest value, |
| 28 | + // but hits the development cache and then caches the outdated value for another 10 seconds, |
| 29 | + // as this would lead to the developer having to wait 20 seconds to see the latest value. |
| 30 | + disableDevelopmentCache: true, |
| 31 | + }), |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Edge runtime does not support timers outside of a request context. |
| 37 | + * |
| 38 | + * Statsig syncs config specs outside of the request context, |
| 39 | + * so we will support it in triggering config spec synchronization in this case. |
| 40 | + */ |
| 41 | +export const createSyncingHandler = (): null | (() => void) => { |
| 42 | + // Syncing both in Edge Runtime and Node.js for now, as the sync is otherwise |
| 43 | + // not working during local development. |
| 44 | + // |
| 45 | + // This needs to be fixed in statsig-node-lite in the future. |
| 46 | + // |
| 47 | + // Ideally the Statsig SDK would not sync at all and instead always read from Edge Config, |
| 48 | + // this would provide two benefits: |
| 49 | + // - changes would propagate immediately instead of being cached for 5s or 10s |
| 50 | + // - the broken syncing due to issues in Date.now in Edge Runtime would be irrelevant |
| 51 | + // |
| 52 | + // if (typeof EdgeRuntime === 'undefined') return null; |
| 53 | + |
| 54 | + const timerInterval = 5_000; |
| 55 | + let isSyncingConfigSpecs = false; |
| 56 | + let nextConfigSpecSyncTime = Date.now() + timerInterval; |
| 57 | + return (): void => { |
| 58 | + if (Date.now() >= nextConfigSpecSyncTime && !isSyncingConfigSpecs) { |
| 59 | + try { |
| 60 | + isSyncingConfigSpecs = true; |
| 61 | + const sync = Statsig.syncConfigSpecs().finally(() => { |
| 62 | + isSyncingConfigSpecs = false; |
| 63 | + nextConfigSpecSyncTime = Date.now() + timerInterval; |
| 64 | + }); |
| 65 | + import('@vercel/functions').then(({ waitUntil }) => { |
| 66 | + waitUntil(sync); |
| 67 | + }); |
| 68 | + } catch (e) { |
| 69 | + // continue |
| 70 | + } |
| 71 | + } |
| 72 | + }; |
| 73 | +}; |
0 commit comments