@thewtex/vtk.js-esm
Version:
Visualization Toolkit for the Web
270 lines (216 loc) • 8.16 kB
JavaScript
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { p as pako } from '../../../vendor/pako/dist/pako.esm.mjs';
import macro from '../../../macro.js';
import Endian from '../../../Common/Core/Endian.js';
import { DataTypeByteSize } from '../../../Common/Core/DataArray/Constants.js';
import { registerType } from '../DataAccessHelper.js';
var vtkErrorMacro = macro.vtkErrorMacro,
vtkDebugMacro = macro.vtkDebugMacro;
/* eslint-disable prefer-promise-reject-errors */
var requestCount = 0;
function openAsyncXHR(method, url) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
if (options.headers) {
Object.entries(options.headers).forEach(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
key = _ref2[0],
value = _ref2[1];
return xhr.setRequestHeader(key, value);
});
}
if (options.progressCallback) {
xhr.addEventListener('progress', options.progressCallback);
}
return xhr;
}
function fetchBinary(url) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new Promise(function (resolve, reject) {
var xhr = openAsyncXHR('GET', url, options);
xhr.onreadystatechange = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200 || xhr.status === 0) {
resolve(xhr.response);
} else {
reject({
xhr: xhr,
e: e
});
}
}
}; // Make request
xhr.responseType = 'arraybuffer';
xhr.send();
});
}
function fetchArray() {
var instance = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var baseURL = arguments.length > 1 ? arguments[1] : undefined;
var array = arguments.length > 2 ? arguments[2] : undefined;
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
if (array.ref && !array.ref.pending) {
return new Promise(function (resolve, reject) {
var url = [baseURL, array.ref.basepath, options.compression ? "".concat(array.ref.id, ".gz") : array.ref.id].join('/');
var xhr = openAsyncXHR('GET', url, options);
xhr.onreadystatechange = function (e) {
if (xhr.readyState === 1) {
array.ref.pending = true;
if (++requestCount === 1 && instance.invokeBusy) {
instance.invokeBusy(true);
}
}
if (xhr.readyState === 4) {
array.ref.pending = false;
if (xhr.status === 200 || xhr.status === 0) {
array.buffer = xhr.response;
if (options.compression) {
if (array.dataType === 'string' || array.dataType === 'JSON') {
array.buffer = pako.inflate(new Uint8Array(array.buffer), {
to: 'string'
});
} else {
array.buffer = pako.inflate(new Uint8Array(array.buffer)).buffer;
}
}
if (array.ref.encode === 'JSON') {
array.values = JSON.parse(array.buffer);
} else {
if (Endian.ENDIANNESS !== array.ref.encode && Endian.ENDIANNESS) {
// Need to swap bytes
vtkDebugMacro("Swap bytes of ".concat(array.name));
Endian.swapBytes(array.buffer, DataTypeByteSize[array.dataType]);
}
array.values = macro.newTypedArray(array.dataType, array.buffer);
}
if (array.values.length !== array.size) {
vtkErrorMacro("Error in FetchArray: ".concat(array.name, ", does not have the proper array size. Got ").concat(array.values.length, ", instead of ").concat(array.size));
} // Done with the ref and work
delete array.ref;
if (--requestCount === 0 && instance.invokeBusy) {
instance.invokeBusy(false);
}
if (instance.modified) {
instance.modified();
}
resolve(array);
} else {
reject({
xhr: xhr,
e: e
});
}
}
}; // Make request
xhr.responseType = options.compression || array.dataType !== 'string' ? 'arraybuffer' : 'text';
xhr.send();
});
}
return Promise.resolve(array);
} // ----------------------------------------------------------------------------
function fetchJSON() {
var instance = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var url = arguments.length > 1 ? arguments[1] : undefined;
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return new Promise(function (resolve, reject) {
var xhr = openAsyncXHR('GET', url, options);
xhr.onreadystatechange = function (e) {
if (xhr.readyState === 1) {
if (++requestCount === 1 && instance.invokeBusy) {
instance.invokeBusy(true);
}
}
if (xhr.readyState === 4) {
if (--requestCount === 0 && instance.invokeBusy) {
instance.invokeBusy(false);
}
if (xhr.status === 200 || xhr.status === 0) {
if (options.compression) {
resolve(JSON.parse(pako.inflate(new Uint8Array(xhr.response), {
to: 'string'
})));
} else {
resolve(JSON.parse(xhr.responseText));
}
} else {
reject({
xhr: xhr,
e: e
});
}
}
}; // Make request
xhr.responseType = options.compression ? 'arraybuffer' : 'text';
xhr.send();
});
} // ----------------------------------------------------------------------------
function fetchText() {
var instance = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var url = arguments.length > 1 ? arguments[1] : undefined;
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (options && options.compression && options.compression !== 'gz') {
vtkErrorMacro('Supported algorithms are: [gz]');
vtkErrorMacro("Unkown compression algorithm: ".concat(options.compression));
}
return new Promise(function (resolve, reject) {
var xhr = openAsyncXHR('GET', url, options);
xhr.onreadystatechange = function (e) {
if (xhr.readyState === 1) {
if (++requestCount === 1 && instance.invokeBusy) {
instance.invokeBusy(true);
}
}
if (xhr.readyState === 4) {
if (--requestCount === 0 && instance.invokeBusy) {
instance.invokeBusy(false);
}
if (xhr.status === 200 || xhr.status === 0) {
if (options.compression) {
resolve(pako.inflate(new Uint8Array(xhr.response), {
to: 'string'
}));
} else {
resolve(xhr.responseText);
}
} else {
reject({
xhr: xhr,
e: e
});
}
}
}; // Make request
xhr.responseType = options.compression ? 'arraybuffer' : 'text';
xhr.send();
});
} // ----------------------------------------------------------------------------
function fetchImage() {
var url = arguments.length > 1 ? arguments[1] : undefined;
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return new Promise(function (resolve, reject) {
var img = new Image();
if (options.crossOrigin) {
img.crossOrigin = options.crossOrigin;
}
img.onload = function () {
return resolve(img);
};
img.onerror = reject;
img.src = url;
});
}
/* eslint-enable prefer-promise-reject-errors */
// ----------------------------------------------------------------------------
var HttpDataAccessHelper = {
fetchArray: fetchArray,
fetchJSON: fetchJSON,
fetchText: fetchText,
fetchBinary: fetchBinary,
// Only for HTTP
fetchImage: fetchImage
};
registerType('http', function (options) {
return HttpDataAccessHelper;
});
export default HttpDataAccessHelper;