testground / sdk-js

Other
1 stars 3 forks source link

first baby steps towards native browser support for the JS sdk of testground #25

Closed GlenDC closed 1 year ago

GlenDC commented 1 year ago

I am by far no NodeJS expert, so please do help me adapt to this culture where you see fit.

With this PR I distinguish in some first starter locations between the browser and node environment. Currently I do this using process.title:

Seems to me like a clean way, but if there's a more natural way, do let me know.

Currently there are the following changes where we differentiate between browser and NodeJS:

This is just the beginning though, I imagine there is even more we can do and want to do if native browser support for sdk-js is what we want. My current aim is to make it as easy to use as in NodeJS, meaning that the code can just be used and it will pull all it needs in the background (e.g. env variables).

Things to keep in mind...

  1. for now we still need to polyfill a lot when web-packing, e.g. this is what I have for a testground plan in my webpack config file:
resolve: {
    extensions: ['.js'],
    fallback: {
      fs: false,
      os: require.resolve('os-browserify'),
      path: require.resolve('path-browserify'),
      https: require.resolve('https-browserify'),
      crypto: require.resolve('crypto-browserify'),
      http: require.resolve('stream-http'),
      zlib: require.resolve('browserify-zlib'),
      buffer: require.resolve('buffer/'),
      url: require.resolve('url/'),
      'assert/': require.resolve('assert/'),
      stream: require.resolve('stream-browserify')
    }
  },
  plugins: [
    new webpack.ProvidePlugin({
      Buffer: ['buffer', 'Buffer']
    }),
    new webpack.ProvidePlugin({
      process: 'process/browser'
    })
  ],

Could be great if out of the box, the sdk-js already does a lot of that for us, or ensures that what it uses is more cross-platform and thus does not need to be shimmed?

  1. I am no expert of this library, so there might be even more places where we need to add some different logic for clean browser support. Please do point me to these locations if you know of any.

Finally we might also want to add some documentation on pointers for some gotcha's, hints, etc. All related to usage in browser.

GlenDC commented 1 year ago

Replaced this PR with https://github.com/testground/sdk-js/pull/26.