From 90308dacd34b1a75d9d28ff4423b03093c03d681 Mon Sep 17 00:00:00 2001 From: mauve Date: Thu, 15 Oct 2020 19:26:21 -0400 Subject: [PATCH] Support environments without MessageChannel or worker_threads --- memory.js | 11 +++++++++-- package.json | 3 ++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/memory.js b/memory.js index 4d8aa4f..4d8fefb 100644 --- a/memory.js +++ b/memory.js @@ -1,13 +1,20 @@ /* eslint-disable camelcase */ 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) { return new Uint8Array(n) } -const sink = new MessageChannel() +const sink = MessageChannel ? new MessageChannel() : null function sodium_free (n) { + if (!sink) return sodium_memzero(n) sink.port1.postMessage(n.buffer, [n.buffer]) } diff --git a/package.json b/package.json index d9a3b4e..518d11a 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ ] }, "browser": { - "crypto": false + "crypto": false, + "worker_threads": false }, "react-native": { "crypto": "crypto"