116 lines
3.4 KiB
JavaScript
116 lines
3.4 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
|
|
var bufferFrom = _interopDefault(require('buffer-from'));
|
|
|
|
function thisAtob(str) {
|
|
var base64 = new Buffer(str, 'base64');
|
|
// Node.js will just skip the characters it can't decode instead of
|
|
// throwing an exception
|
|
if (base64.toString('base64') !== str) {
|
|
throw new Error("attachment is not a valid base64 string");
|
|
}
|
|
return base64.toString('binary');
|
|
}
|
|
|
|
function thisBtoa(str) {
|
|
return bufferFrom(str, 'binary').toString('base64');
|
|
}
|
|
|
|
function typedBuffer(binString, buffType, type) {
|
|
// buffType is either 'binary' or 'base64'
|
|
var buff = bufferFrom(binString, buffType);
|
|
buff.type = type; // non-standard, but used for consistency with the browser
|
|
return buff;
|
|
}
|
|
|
|
function b64ToBluffer(b64, type) {
|
|
return typedBuffer(b64, 'base64', type);
|
|
}
|
|
|
|
// From http://stackoverflow.com/questions/14967647/ (continues on next line)
|
|
// encode-decode-image-with-base64-breaks-image (2013-04-21)
|
|
function binaryStringToArrayBuffer(bin) {
|
|
var length = bin.length;
|
|
var buf = new ArrayBuffer(length);
|
|
var arr = new Uint8Array(buf);
|
|
for (var i = 0; i < length; i++) {
|
|
arr[i] = bin.charCodeAt(i);
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
function binStringToBluffer(binString, type) {
|
|
return typedBuffer(binString, 'binary', type);
|
|
}
|
|
|
|
// This function is unused in Node
|
|
/* istanbul ignore next */
|
|
function createBlob() {
|
|
}
|
|
|
|
function blobToBase64(blobOrBuffer, callback) {
|
|
callback(blobOrBuffer.toString('base64'));
|
|
}
|
|
|
|
// not used in Node, but here for completeness
|
|
function blobToBase64$1(blobOrBuffer, callback) {
|
|
callback(blobOrBuffer.toString('binary'));
|
|
}
|
|
|
|
// simplified API. universal browser support is assumed
|
|
function readAsArrayBuffer(blob, callback) {
|
|
var reader = new FileReader();
|
|
reader.onloadend = function (e) {
|
|
var result = e.target.result || new ArrayBuffer(0);
|
|
callback(result);
|
|
};
|
|
reader.readAsArrayBuffer(blob);
|
|
}
|
|
|
|
//Can't find original post, but this is close
|
|
//http://stackoverflow.com/questions/6965107/ (continues on next line)
|
|
//converting-between-strings-and-arraybuffers
|
|
function arrayBufferToBinaryString(buffer) {
|
|
var binary = '';
|
|
var bytes = new Uint8Array(buffer);
|
|
var length = bytes.byteLength;
|
|
for (var i = 0; i < length; i++) {
|
|
binary += String.fromCharCode(bytes[i]);
|
|
}
|
|
return binary;
|
|
}
|
|
|
|
// shim for browsers that don't support it
|
|
function readAsBinaryString(blob, callback) {
|
|
var reader = new FileReader();
|
|
var hasBinaryString = typeof reader.readAsBinaryString === 'function';
|
|
reader.onloadend = function (e) {
|
|
var result = e.target.result || '';
|
|
if (hasBinaryString) {
|
|
return callback(result);
|
|
}
|
|
callback(arrayBufferToBinaryString(result));
|
|
};
|
|
if (hasBinaryString) {
|
|
reader.readAsBinaryString(blob);
|
|
} else {
|
|
reader.readAsArrayBuffer(blob);
|
|
}
|
|
}
|
|
|
|
exports.atob = thisAtob;
|
|
exports.btoa = thisBtoa;
|
|
exports.base64StringToBlobOrBuffer = b64ToBluffer;
|
|
exports.binaryStringToArrayBuffer = binaryStringToArrayBuffer;
|
|
exports.binaryStringToBlobOrBuffer = binStringToBluffer;
|
|
exports.blob = createBlob;
|
|
exports.blobOrBufferToBase64 = blobToBase64;
|
|
exports.blobOrBufferToBinaryString = blobToBase64$1;
|
|
exports.readAsArrayBuffer = readAsArrayBuffer;
|
|
exports.readAsBinaryString = readAsBinaryString;
|
|
exports.typedBuffer = typedBuffer;
|