Support environments without MessageChannel or worker_threads

This commit is contained in:
mauve 2020-10-15 19:26:21 -04:00
parent 7febc97986
commit 90308dacd3
2 changed files with 11 additions and 3 deletions

View File

@ -1,13 +1,20 @@
/* eslint-disable camelcase */ /* eslint-disable camelcase */
var MessageChannel = global.MessageChannel var MessageChannel = global.MessageChannel
if (MessageChannel == null) ({ MessageChannel } = require('worker' + '_threads')) if (MessageChannel == null) {
try {
({ MessageChannel } = require('worker' + '_threads'))
} catch (e) {
// Must not be supported
}
}
function sodium_malloc (n) { function sodium_malloc (n) {
return new Uint8Array(n) return new Uint8Array(n)
} }
const sink = new MessageChannel() const sink = MessageChannel ? new MessageChannel() : null
function sodium_free (n) { function sodium_free (n) {
if (!sink) return
sodium_memzero(n) sodium_memzero(n)
sink.port1.postMessage(n.buffer, [n.buffer]) sink.port1.postMessage(n.buffer, [n.buffer])
} }

View File

@ -24,7 +24,8 @@
] ]
}, },
"browser": { "browser": {
"crypto": false "crypto": false,
"worker_threads": false
}, },
"react-native": { "react-native": {
"crypto": "crypto" "crypto": "crypto"