optimizely / javascript-sdk

JavaScript SDK for Optimizely Feature Experimentation and Optimizely Full Stack (legacy)
https://www.optimizely.com/products/experiment/feature-experimentation/
Apache License 2.0
78 stars 80 forks source link

[BUG] Akamai EW doesnt have TextEncoder and crypto as Globals #925

Open dsafonov-grid opened 5 months ago

dsafonov-grid commented 5 months ago

Is there an existing issue for this?

SDK Version

5.3.0

Current Behavior

Running EW version as is, causing runtime Errors e.g.

Error crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported Error: crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported%0A at m (main.js:1:2041)%0A at C (main.js:1:2439)%0A at Object.uuid (main.js:1:7729)%0A at a.decisionObj (main.js:1:70858)%0A at e.sendImpressionEvent (main.js:1:71159)%0A at e.decide (main.js:1:86430)%0A at main.js:1:87419%0A at Array.forEach ()%0A at e.decideForKeys (main.js:1:87390)%0A at e.decideAll (main.js:$

and similar about TextEncoder

Expected Behavior

Everything happens without errors

Steps To Reproduce

  1. In https://github.com/optimizely/akamai-edgeworker-starter-kit with 5.3.0
  2. Use approach suggested in https://github.com/optimizely/javascript-sdk/issues/924 to proceed to next errors

SDK Type

Edge/Lite

Node Version

No response

Browsers impacted

No response

Link

No response

Logs

No response

Severity

No response

Workaround/Solution

Hi Optimizely Team,

As Per: https://techdocs.akamai.com/edgeworkers/docs/specifications 1) the env in Akamai EW doesn't have TextEncoder used by murmurhash https://github.com/perezd/node-murmurhash/blob/master/murmurhash.js#L4 2) the env in Akamai EW doesn't have crypto used by uuid https://github.com/uuidjs/uuid/blob/bc46e198ab06311a9d82d3c9c6222062dd27f760/src/rng-browser.js#L17

Would you consider a proper workaround doing this in main.ts

import { TextEncoder } from 'encoding';
import { crypto } from 'crypto';
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error, @typescript-eslint/ban-ts-comment
// @ts-ignore
globalThis.TextEncoder = TextEncoder;
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error, @typescript-eslint/ban-ts-comment
// @ts-ignore
globalThis.crypto = crypto;

Because for me this seem to be working fine

Recent Change

No response

Conflicts

No response

mikechu-optimizely commented 5 months ago

Thanks. We're looking into a similar report at https://github.com/optimizely/react-sdk/issues/232