statsig-io / node-js-server-sdk

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

Impossible to use cloudflare worker nodejs-compatibility mode due to transitive imports #37

Open dsumac opened 11 months ago

dsumac commented 11 months ago

Context

Bug

image

Analysis

You use sha.js which use safe-buffer which uses native node Buffer api without node prefix import node:buffer

image

kenny-statsig commented 11 months ago

Hey @dsumac, thanks for sharing these details. We are aware of this incompatibility and plan to replace the sha.js dependency. Until then are you able to workaround this by using polyfills instead of compatibility mode?

dsumac commented 11 months ago

Thanks @kenny-statsig. We use node_compat = true(in our wrangler.toml config file) option to use polyfill. But it doesn't work. Error at deployement.

cpreid commented 11 months ago

@dsumac Can you try adding compatibility_date. This works fine for me:

compatibility_date = "2023-01-20"
node_compat = true
dsumac commented 11 months ago

@cpreid , my explicate was maybe not clear.

Effectively this config it works. Even with an older compat mode. And this is this kind of config which works.

But I would like use native nodejs api in the same time, in the same worker code. Ex:

import { createHash } from "node:crypto";

const hash = createHash("valueToHash");

....

To directly use node api like this without polyfill we must activate nodejs compatibility flag: https://developers.cloudflare.com/workers/configuration/compatibility-dates/#nodejs-compatibility-flag

And when we activate this config, it doesn't work:

compatibility_date = "2023-01-20"
compatibility_flags = [ "nodejs_compat" ]

# we must disable node_compat to use compatibility_flags
# node_compat = true