statsig-io / node-js-server-sdk

Statsig's SDK for server-side Node.js applications.
ISC License
20 stars 15 forks source link

Errors logged in Next.js middleware #27

Closed alvarlagerlof closed 11 months ago

alvarlagerlof commented 1 year ago

I noticed that running there are some errors being logged in Vercel when running Statsig on their middleware. I think this happens because the sdk uses Node.js specific APIs might not be available on the Vercel edge runtime. This confused me because Vercel has an example using Statsig in middleware.

The errors do not seem to affect the function of the SDK, and the errors seem to be handled, but I wonder if this should to be considered anyways.

Screenshot from Vercel

image

Excerpt of next build

../node_modules/node-forge/lib/util.js
A Node.js API is used (process.nextTick at line: 19) which is not supported in the Edge Runtime.
Learn more: https://nextjs.org/docs/api-reference/edge-runtime

Import trace for requested module:
../node_modules/node-forge/lib/util.js
../node_modules/node-forge/lib/index.js
../node_modules/statsig-node/dist/Evaluator.js
../node_modules/statsig-node/dist/StatsigServer.js
../node_modules/statsig-node/dist/index.js
./src/utils/statsigUtils.ts

(also on line 20)

../node_modules/node-forge/lib/util.js
A Node.js API is used (setImmediate at line: 21) which is not supported in the Edge Runtime.
Learn more: https://nextjs.org/docs/api-reference/edge-runtime

Import trace for requested module:
../node_modules/node-forge/lib/util.js
../node_modules/node-forge/lib/index.js
../node_modules/statsig-node/dist/Evaluator.js
../node_modules/statsig-node/dist/StatsigServer.js
../node_modules/statsig-node/dist/index.js
./src/utils/statsigUtils.ts

(also on line 22, 32, 33, 35)

../node_modules/node-forge/lib/util.js
A Node.js API is used (process.versions at line: 111) which is not supported in the Edge Runtime.
Learn more: https://nextjs.org/docs/api-reference/edge-runtime

Import trace for requested module:
../node_modules/node-forge/lib/util.js
../node_modules/node-forge/lib/index.js
../node_modules/statsig-node/dist/Evaluator.js
../node_modules/statsig-node/dist/StatsigServer.js
../node_modules/statsig-node/dist/index.js
./src/utils/statsigUtils.ts

../node_modules/statsig-node/dist/utils/core.js
A Node.js API is used (process.version at line: 96) which is not supported in the Edge Runtime.
Learn more: https://nextjs.org/docs/api-reference/edge-runtime

Import trace for requested module:
../node_modules/statsig-node/dist/utils/core.js
../node_modules/statsig-node/dist/DynamicConfig.js
../node_modules/statsig-node/dist/index.js
./src/utils/statsigUtils.ts

(also on line 97, 98, 

Versions

Node: 18.16.0 Next.js: 13.4.2 statsig-node: 5.5.0 statsig-node-vercel: 0.1.0

Code sample

This runs in middleware.ts

 const dataAdapter = new EdgeConfigDataAdapter(
    process.env.STATSIG_EDGE_CONFIG_ITEM_KEY,
    process.env.EDGE_CONFIG_STATSIG,
  )

  await Statsig.initialize(String(process.env.STATSIG_SERVER_API_KEY), {
    ...dataAdapter,
    environment: { tier: process.env. NEXT_PUBLIC_VERCEL_ENV },
    initTimeoutMs: 300,
  })

  Statsig.checkGate(...)

  await Statsig.flush()
kenny-statsig commented 1 year ago

Hey there, we have plans to remove the node forge dependency. I think that should resolve all the warnings you are seeing. @daniel-statsig

alvarlagerlof commented 1 year ago

Looking at our logs again, #31 seems to have reduced it to just one type of error:

TypeError: setTimeout(...).unref is not a function
    at (../node_modules/statsig-node/dist/utils/StatsigFetcher.js:97:24)
    at (../node_modules/statsig-node/dist/utils/StatsigFetcher.js:92:15)
    at (../node_modules/statsig-node/dist/utils/StatsigFetcher.js:71:29)
    at (src/utils/statsigUtils.ts:83:2)
    at (src/middleware.ts:62:6)
    at (../node_modules/@sentry/nextjs/esm/edge/utils/edgeWrapperUtils.js:53:28)
    at (../node_modules/next/dist/esm/server/web/adapter.js:160:19)

It doesn't break the site site, but it would be great if that one could be resolved as well so that we can have clean logs again.

alvarlagerlof commented 1 year ago

Bump.

kenny-statsig commented 11 months ago

Hey @alvarlagerlof, closing this out as it should be resolved as of v5.9.3 commit: https://github.com/statsig-io/node-js-server-sdk/commit/894f7d291a9d0391ba2d62c09e1fedecc252467f