41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
"use strict";
|
|
/**
|
|
* this module handles the difference between window (browser) and node js for specific functions and libraries.
|
|
* env.js
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getEnvBtoa = function () {
|
|
if (typeof window !== 'undefined')
|
|
return window.btoa; // browser
|
|
else
|
|
return nodeBtoa; // node
|
|
};
|
|
exports.getEnvAtob = function () {
|
|
if (typeof window !== 'undefined')
|
|
return window.atob; // browser
|
|
else
|
|
return nodeAtob; // node
|
|
};
|
|
var nodeBtoa = function (str) {
|
|
if (typeof Buffer === 'undefined')
|
|
throw new Error('UnsupportedEnvironment');
|
|
var buffer;
|
|
var type = Object.prototype.toString.call(str).slice(8, -1);
|
|
var typedArrays = ['ArrayBuffer', 'TypedArray', 'Uint8Array', 'Int8Array', 'Uint8ClampedArray', 'Int16Array', 'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array', 'Float64Array'];
|
|
if (Buffer.isBuffer(str)) {
|
|
buffer = str;
|
|
}
|
|
else if (typedArrays.indexOf(type) >= 0) {
|
|
buffer = Buffer.from(str);
|
|
}
|
|
else {
|
|
buffer = Buffer.from(str.toString(), 'binary');
|
|
}
|
|
return buffer.toString('base64');
|
|
};
|
|
var nodeAtob = function (str) {
|
|
if (typeof Buffer === 'undefined')
|
|
throw new Error('UnsupportedEnvironment');
|
|
return Buffer.from(str, 'base64').toString('binary');
|
|
};
|