Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing on Cloudflare Workers #212

Closed
tomvardasca opened this issue Sep 13, 2019 · 6 comments
Closed

Failing on Cloudflare Workers #212

tomvardasca opened this issue Sep 13, 2019 · 6 comments

Comments

@tomvardasca
Copy link

Hi

When I try to use libsodium with Cloudflare workers, with libsodium-sumo module, I get TypeError: Cannot read property 'href' of undefined.
I think it has to do with it trying to use the worker sandbox as a browser.
Do you plan to ship .wasm files on this project? that would work!

Kind Regards,
Tomé

@jedisct1
Copy link
Owner

Hi Tomé,

There isn't any href in that code. Not sure where it comes from.

The .wasm file used to be shipped separately, but that caused a lot of trouble. People didn't know how to use it, there were issues with CORS, etc. So it's now embedded in the Javascript module.

But if you only need the WebAssembly version, you can compile a version leveraging WASI with the dist-build/wasm32-wasi.sh script. This is the version I use for WebAssembly benchmarks.

@tomvardasca
Copy link
Author

Thank you Denis,

There are some self.location.href:document.currentScripton libsodium.js, maybe the error happens there, the Worker sandbox is very limited.
I am compiling it right now, when I am able to test it I will let you know. Not sure it will run because of WASI.

@jedisct1
Copy link
Owner

This is the code emscripten generates to check the platform the module is running on:

var ENVIRONMENT_IS_WEB = false;
var ENVIRONMENT_IS_WORKER = false;
var ENVIRONMENT_IS_NODE = false;
var ENVIRONMENT_IS_SHELL = false;
ENVIRONMENT_IS_WEB = typeof window === 'object';
ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof require === 'function' && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER;
ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;

You may be able to trick it by undefining window and importScripts, and defining process.

@tomvardasca
Copy link
Author

Thank you again!

Was able to use it as a shell.
Added the following configuration to the Webpack config on my Cloudflare Worker project

module: {
    rules: [
      {
        test: require.resolve('libsodium-sumo'),
        use:
          'imports-loader?window=>undefined,importScripts=>undefined,process=>undefined',
      },
    ],
  },
  plugins: [
    new webpack.ProvidePlugin({
      TextEncoder: ['text-encoding', 'TextEncoder'],
      TextDecoder: ['text-encoding', 'TextDecoder'],
    }),
  ],

I had to also add the text-encoding npm package for encodings other than UTF-8 (Workers only have UTF-8 available), and also installed imports-loader npm package to override window, importScripts and process.

Tested a couple of functions and works very well.

Thank you again for the work you have been doing on libsodium.

@jedisct1
Copy link
Owner

Wow, thanks for the update! I didn't know that something like that was possible. Great!

@praswicaksono
Copy link

If someone needed here is fix when you are using TS

make binding.d.ts in src folder, you will need this for binding KV anyway

export {};

declare global {
  const USERS: KVNamespace;
  var window: any;
  var importScripts: any;
}

and set window and importScripts global var to undefined as @jedisct1 mentioned before libsodium.ready

import libsodium from 'libsodium-wrappers';

globalThis.window = undefined;
globalThis.importScripts = undefined;

await libsodium.ready;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants