loaders.gl
Version:
Framework-independent loaders for 3D graphics formats
63 lines (50 loc) • 1.65 kB
JavaScript
/* global FileReader, Blob, ArrayBuffer, Buffer */
import assert from './assert';
export function toArrayBuffer(binaryData) {
if (binaryData instanceof ArrayBuffer) {
return binaryData;
}
if (typeof Blob !== 'undefined' && binaryData instanceof Blob) {
return blobToArrayBuffer(binaryData);
} // if (ArrayBuffer.isView(binaryData)) {
// return binaryData.buffer;
// }
return nodeBufferToArrayBuffer(binaryData); // assert(false);
// return null;
} // Convert (copy) ArrayBuffer to Buffer
export function toBuffer(binaryData) {
if (ArrayBuffer.isView(binaryData)) {
binaryData = binaryData.buffer;
}
if (typeof Buffer !== 'undefined' && binaryData instanceof ArrayBuffer) {
/* global Buffer */
var buffer = new Buffer(binaryData.byteLength);
var view = new Uint8Array(binaryData);
for (var i = 0; i < buffer.length; ++i) {
buffer[i] = view[i];
}
return buffer;
}
assert(false);
return null;
} // Helper functions
export function blobToArrayBuffer(blob) {
return new Promise(function (resolve, reject) {
var arrayBuffer;
var fileReader = new FileReader();
fileReader.onload = function (event) {
arrayBuffer = event.target.result;
};
fileReader.onloadend = function (event) {
return resolve(arrayBuffer);
};
fileReader.onerror = reject;
fileReader.readAsArrayBuffer(blob);
});
}
function nodeBufferToArrayBuffer(buffer) {
// TODO - per docs we should just be able to call buffer.buffer, but there are issues
var typedArray = new Uint8Array(buffer);
return typedArray.buffer;
}
//# sourceMappingURL=binary-utils.js.map