From 04fcd77f5fc1470c81f1d245c22d70f2f2cc57da Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Tue, 25 Jun 2019 10:28:00 -0700 Subject: [PATCH] fix: only require fast-text-encoding when needed (#740) --- src/crypto/browser/crypto.ts | 2 +- test/test.crypto.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) 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); + }); });