Add TextDecoder polyfill

This commit is contained in:
Daniel Huigens 2018-06-19 17:17:24 +02:00
parent ad92ac7c3a
commit 0cabf72682
5 changed files with 56 additions and 37 deletions

View File

@ -69,6 +69,7 @@
"istanbul": "^0.4.5", "istanbul": "^0.4.5",
"mocha": "^5.0.0", "mocha": "^5.0.0",
"sinon": "^4.3.0", "sinon": "^4.3.0",
"text-encoder-lite": "^1.0.1",
"whatwg-fetch": "^2.0.3" "whatwg-fetch": "^2.0.3"
}, },
"dependencies": { "dependencies": {

View File

@ -44,15 +44,11 @@ import { CleartextMessage } from './cleartext';
import { generate, reformat } from './key'; import { generate, reformat } from './key';
import config from './config/config'; import config from './config/config';
import enums from './enums'; import enums from './enums';
import './polyfills';
import stream from './stream'; import stream from './stream';
import util from './util'; import util from './util';
import AsyncProxy from './worker/async_proxy'; import AsyncProxy from './worker/async_proxy';
// Old browser polyfills
if (typeof window !== 'undefined') {
require('./polyfills');
}
////////////////////////// //////////////////////////
// // // //
// Web Worker setup // // Web Worker setup //

View File

@ -1,8 +1,16 @@
/* eslint-disable import/no-extraneous-dependencies */ /**
// Old browser polyfills * @fileoverview Old browser polyfills
// All are listed as dev dependencies because Node does not need them * All are listed as dev dependencies because Node does not need them
// and for browser babel will take care of it * and for browser babel will take care of it
* @requires util
* @module polyfills
*/
import util from './util';
/* eslint-disable import/no-extraneous-dependencies */
if (typeof window !== 'undefined') {
if (typeof window.fetch === 'undefined') { if (typeof window.fetch === 'undefined') {
require('whatwg-fetch'); require('whatwg-fetch');
} }
@ -31,3 +39,14 @@ if (typeof Symbol === 'undefined') {
if (typeof Object.assign === 'undefined') { if (typeof Object.assign === 'undefined') {
require('core-js/fn/object/assign'); require('core-js/fn/object/assign');
} }
}
if (typeof TransformStream === 'undefined') {
require('@mattiasbuelens/web-streams-polyfill');
}
if (typeof TextDecoder === 'undefined') {
global.TextDecoder = util.getNodeTextDecoder();
}
if (typeof TextDecoder === 'undefined') {
global.TextDecoder = require('text-encoder-lite');
}

View File

@ -1,9 +1,5 @@
import util from './util'; import util from './util';
if (typeof TransformStream === 'undefined') {
require('@mattiasbuelens/web-streams-polyfill');
}
const nodeStream = util.getNodeStream(); const nodeStream = util.getNodeStream();
function toStream(input) { function toStream(input) {

View File

@ -664,12 +664,19 @@ export default {
return; return;
} }
// This "hack" allows us to access the native node buffer module.
// otherwise, it gets replaced with the browserified version
// eslint-disable-next-line no-useless-concat, import/no-dynamic-require // eslint-disable-next-line no-useless-concat, import/no-dynamic-require
return require('stream'+''); return require('stream'+'');
}, },
getNodeTextDecoder: function() {
if (!util.detectNode()) {
return;
}
// eslint-disable-next-line no-useless-concat, import/no-dynamic-require
return require('util'+'').TextDecoder;
},
isEmailAddress: function(data) { isEmailAddress: function(data) {
if (!util.isString(data)) { if (!util.isString(data)) {
return false; return false;