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