diff --git a/src/crypto/browser/crypto.ts b/src/crypto/browser/crypto.ts index 536e252a..9792557a 100644 --- a/src/crypto/browser/crypto.ts +++ b/src/crypto/browser/crypto.ts @@ -22,7 +22,7 @@ import * as base64js from 'base64-js'; // Not all browsers support `TextEncoder`. The following `require` will // provide a fast UTF8-only replacement for those browsers that don't support // text encoding natively. -if (typeof TextEncoder === 'undefined') { +if (typeof process === 'undefined' && typeof TextEncoder === 'undefined') { require('fast-text-encoding'); } diff --git a/test/test.crypto.ts b/test/test.crypto.ts index 086d9b99..ee0b723f 100644 --- a/test/test.crypto.ts +++ b/test/test.crypto.ts @@ -73,4 +73,10 @@ describe('Node.js crypto tests', () => { const encodedString = crypto.encodeBase64StringUtf8(originalString); assert.strictEqual(encodedString, base64String); }); + + it('should not load fast-text-encoding while running in nodejs', () => { + const loadedModules = Object.keys(require('module')._cache); + const hits = loadedModules.filter(x => x.includes('fast-text-encoding')); + assert.strictEqual(hits.length, 0); + }); });