diff --git a/lib/exceljs.bare.js b/lib/exceljs.bare.js index 9f00471f8..af6941ba6 100644 --- a/lib/exceljs.bare.js +++ b/lib/exceljs.bare.js @@ -1,4 +1,6 @@ // this bundle is built without polyfill leaving apps the freedom to add their own +require('./utils/browser-patch-buffer'); + const ExcelJS = { Workbook: require('./doc/workbook'), }; diff --git a/lib/exceljs.browser.js b/lib/exceljs.browser.js index 3a3d631dc..0e19fbe25 100644 --- a/lib/exceljs.browser.js +++ b/lib/exceljs.browser.js @@ -5,6 +5,7 @@ require('core-js/modules/es.object.keys'); require('core-js/modules/es.symbol'); require('core-js/modules/es.symbol.async-iterator'); require('regenerator-runtime/runtime'); +require('./utils/browser-patch-buffer'); const ExcelJS = { Workbook: require('./doc/workbook'), diff --git a/lib/utils/browser-patch-buffer.js b/lib/utils/browser-patch-buffer.js new file mode 100644 index 000000000..ce8bee2bd --- /dev/null +++ b/lib/utils/browser-patch-buffer.js @@ -0,0 +1,34 @@ +/* eslint-disable node/no-unsupported-features/node-builtins */ +// Note: this is included only in browser +if (typeof TextDecoder !== 'undefined' || typeof TextEncoder !== 'undefined') { + const {Buffer} = require('buffer'); + if (typeof TextEncoder !== 'undefined') { + const textEncoder = new TextEncoder('utf-8'); + const {from} = Buffer; + Buffer.from = function(str, encoding) { + if (typeof str === 'string') { + if (encoding) { + encoding = encoding.toLowerCase(); + } + if (!encoding || encoding === 'utf8' || encoding === 'utf-8') { + return from.call(this, textEncoder.encode(str).buffer); + } + } + return from.apply(this, arguments); + }; + } + if (typeof TextDecoder !== 'undefined') { + const textDecoder = new TextDecoder('utf-8'); + const {toString} = Buffer.prototype; + Buffer.prototype.toString = function(encoding, start, end) { + if (encoding) { + encoding = encoding.toLowerCase(); + } + if (!start && end === undefined && + (!encoding || encoding === 'utf8' || encoding === 'utf-8')) { + return textDecoder.decode(this); + } + return toString.apply(this, arguments); + }; + } +}