sodium-friends / sodium-javascript

Pure Javascript version of sodium-native
MIT License
92 stars 24 forks source link

worker_threads dependency breaks on Node 10 #47

Open christianbundy opened 4 years ago

christianbundy commented 4 years ago

https://nodejs.org/docs/latest-v10.x/api/worker_threads.html#worker_threads_worker_threads

We need to run Node with an extra experimental flag for this. Are there any good alternatives to keep Node 10 compat?

See-also: https://github.com/ssb-js/chloride/pull/21

emilbayes commented 4 years ago

All that is required is some way of detaching the buffer

christianbundy commented 4 years ago

Right, but are there ways to detach the buffer cleanly without any postMessage() API?

emilbayes commented 4 years ago

MesaageChannels was the only way I could find, but I didn’t consider node 10 at the time

mafintosh commented 4 years ago

@emilbayes could just make it a noop if that dep doesn't exist

glowkeeper commented 4 years ago

I'm seeing something similar on a build using node v14.15.0, npm 6.14.8 and webpack 4.43.0. It happens after installing dat-sdk:

WARNING in ./node_modules/sodium-javascript/randombytes.js 23:6-13
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
 @ ./node_modules/sodium-javascript/index.js
 @ ./node_modules/sodium-universal/index.js
 @ ./node_modules/hypercore-crypto/index.js
 @ ./node_modules/dat-sdk/index.js
 @ ./src/containers/pages/settings.tsx
 @ ./src/containers/pages/index.ts
 @ ./src/components/content.tsx
 @ ./src/components/pages/main.tsx
 @ ./src/components/root.tsx
 @ ./src/components/index.tsx
 @ multi @babel/polyfill ./src/components/index.tsx

ERROR in ./node_modules/sodium-javascript/memory.js
Module not found: Error: Can't resolve 'worker_threads' in '/Users/sh601/gitRepos/Storefront-minima/node_modules/sodium-javascript'
 @ ./node_modules/sodium-javascript/memory.js 3:50-80
 @ ./node_modules/sodium-javascript/index.js
 @ ./node_modules/sodium-universal/index.js
 @ ./node_modules/hypercore-crypto/index.js
 @ ./node_modules/dat-sdk/index.js
 @ ./src/containers/pages/settings.tsx
 @ ./src/containers/pages/index.ts
 @ ./src/components/content.tsx
 @ ./src/components/pages/main.tsx
 @ ./src/components/root.tsx
 @ ./src/components/index.tsx
 @ multi @babel/polyfill ./src/components/index.tsx

I've managed to get my project building by hacking a couple of your files:

In memory.js, I commented out if (MessageChannel == null) ({ MessageChannel } = require('worker' + '_threads')) on line 3.

And in randombytes.js, I commented out if (require != null) { (and its associated curly brace) on line 23.

But I know 'me bad' :( Is this an easy fix?