const statsig = require('statsig-node');
let requestsHandled = 0;
const initStart = Date.now();
let initEnd = 0;
let initialized = false;
const initializationPromise = statsig.initialize(process.env.sdkKey).then(() => {
initEnd = Date.now();
initialized = true;
});
exports.statsig = async (req, res) => {
const functionStart = Date.now();
requestsHandled++;
if (!initialized) {
await initializationPromise;
}
const alwaysOnGateValue = await statsig.checkGate({userID: 'gcp'}, 'always_on_gate');
const functionEnd = Date.now();
res.status(200).send({
initStart,
initEnd,
functionStart,
functionEnd,
requestsHandled,
initializeTime: initEnd - initStart,
functionTime: functionEnd - functionStart,
alwaysOnGateValue: alwaysOnGateValue,
});
};
```javascript
### Alternatives
If this doesn't work for you, a custom integration may help but would require additional setup.
For example, you could create a background function which fetches your definitions from Statsig servers periodically, and stores them in your database.
Then, when initializing a Statsig server SDK, you can fetch the values from your database, rather than issuing a request to Statsig servers.
[Reach out to us in Slack](https://www.statsig.com/slack) if you want to talk through how to make Statsig play nicely with your architecture and requirements!
### Fastly Implementation
Depending on your Fastly account settings, you may have an extra layer of security enabled which requires a custom `fetch` method to be implemented for any network requests that need to be made to 3rd party domains. You must implement a custom `fetch` method that uses Fastly's required [backend parameter](https://js-compute-reference-docs.edgecompute.app/docs/globals/fetch).
To solve for this, first define a module in its own file that implements a global override of `fetch`:
```javascript
export { };
const ogFetch = fetch.bind(fetch.prototype);
globalThis.fetch = (input, init) => {
const backend: string = input instanceof URL ? input.hostname :
input instanceof Request ? new URL(input.url).hostname :
new URL(input).hostname;
const initWithBackend: typeof init = { ...init, backend };
return ogFetch(input, initWithBackend);
}
(globalThis as any).EdgeRuntime = "fastly";
```javascript
Next, in the main function entrypoint, simply import the fetch override module prior to importing the Statsig SDK:
```javascript
import "./providers/utils/fastly-fetch-setup";
import Statsig from "statsig-node";