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

Issue that arise when using a webpack #27

Open
MichaelKim20 opened this issue Aug 28, 2020 · 3 comments
Open

Issue that arise when using a webpack #27

MichaelKim20 opened this issue Aug 28, 2020 · 3 comments

Comments

@MichaelKim20
Copy link

I love sodium-javascript. And I'm using it so well.
I met a problem like this. Please solve this problem.

Module not found: Error: Can't resolve 'worker_threads' in '/node_modules/sodium-javascript'
@ ./node_modules/sodium-javascript/memory.js 3:50-80
@ ./node_modules/sodium-javascript/index.js

@MichaelKim20 MichaelKim20 changed the title Issues that arise when using a webpack Issue that arise when using a webpack Aug 28, 2020
@christianbundy
Copy link
Contributor

Sorry to hear you're bumping into problems with this. If you're able to apply a small patch to that file, maybe you could check whether an approach like this would work?

diff --git a/memory.js b/memory.js
index 4d8aa4f..25890f8 100644
--- a/memory.js
+++ b/memory.js
@@ -1,6 +1,12 @@
 /* eslint-disable camelcase */
 var MessageChannel = global.MessageChannel
-if (MessageChannel == null) ({ MessageChannel } = require('worker' + '_threads'))
+try {
+  ({ MessageChannel } = require('worker' + '_threads'))
+} catch (e) {
+  if (MessageChannel == null) {
+    throw e
+  }
+}
 
 function sodium_malloc (n) {
   return new Uint8Array(n)

@emilbayes
Copy link
Member

What the funky require line does above is bust browserify's static require resolution, but I guess webpack goes a bit beyond that. That module is not supposed to be loaded in a browser setting, but is a "hack" to get a place to throw Buffers away, in the same way that sodium-native's sodium_free detaches the buffer

@MichaelKim20
Copy link
Author

Sorry to hear you're bumping into problems with this. If you're able to apply a small patch to that file, maybe you could check whether an approach like this would work?

diff --git a/memory.js b/memory.js
index 4d8aa4f..25890f8 100644
--- a/memory.js
+++ b/memory.js
@@ -1,6 +1,12 @@
 /* eslint-disable camelcase */
 var MessageChannel = global.MessageChannel
-if (MessageChannel == null) ({ MessageChannel } = require('worker' + '_threads'))
+try {
+  ({ MessageChannel } = require('worker' + '_threads'))
+} catch (e) {
+  if (MessageChannel == null) {
+    throw e
+  }
+}
 
 function sodium_malloc (n) {
   return new Uint8Array(n)

Yes, it works well.
I have confirmed that it works without any problems with the web browser (Firefox, Safari, Chrome).
And the loading speed is much faster.
Thank you.

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