@ethersphere/bee-js
Version:
Javascript client for Bee
39 lines (38 loc) • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fileArrayBuffer = exports.isFile = void 0;
/**
* Compatibility functions for working with File API objects
*
* https://developer.mozilla.org/en-US/docs/Web/API/File
*/
function isFile(file) {
// browser
if (typeof File === 'function' && file instanceof File) {
return true;
}
// node.js
const f = file;
return (typeof f === 'object' &&
typeof f.name === 'string' &&
(typeof f.stream === 'function' || typeof f.arrayBuffer === 'function'));
}
exports.isFile = isFile;
/**
* Compatibility helper for browsers where the `arrayBuffer function is
* missing from `File` objects.
*
* @param file A File object
*/
async function fileArrayBuffer(file) {
if (file.arrayBuffer) {
return file.arrayBuffer();
}
// workaround for Safari where arrayBuffer is not supported on Files
return new Promise(resolve => {
const fr = new FileReader();
fr.onload = () => resolve(fr.result);
fr.readAsArrayBuffer(file);
});
}
exports.fileArrayBuffer = fileArrayBuffer;