|
| 1 | +import macro from 'vtk.js/Sources/macro'; |
| 2 | +import Endian from 'vtk.js/Sources/Common/Core/Endian'; |
| 3 | +import { DataTypeByteSize } from 'vtk.js/Sources/Common/Core/DataArray/Constants'; |
| 4 | +import { registerType } from 'vtk.js/Sources/IO/Core/DataAccessHelper'; |
| 5 | + |
| 6 | +const { vtkErrorMacro, vtkDebugMacro } = macro; |
| 7 | + |
| 8 | +const REJECT_COMPRESSION = () => { |
| 9 | + vtkErrorMacro( |
| 10 | + 'LiteHttpDataAccessHelper does not support compression. Need to register HttpDataAccessHelper instead.' |
| 11 | + ); |
| 12 | + return Promise.reject( |
| 13 | + new Error( |
| 14 | + 'LiteHttpDataAccessHelper does not support compression. Need to register HttpDataAccessHelper instead.' |
| 15 | + ) |
| 16 | + ); |
| 17 | +}; |
| 18 | + |
| 19 | +/* eslint-disable prefer-promise-reject-errors */ |
| 20 | +let requestCount = 0; |
| 21 | + |
| 22 | +function openAsyncXHR(method, url, options = {}) { |
| 23 | + const xhr = new XMLHttpRequest(); |
| 24 | + xhr.open(method, url, true); |
| 25 | + |
| 26 | + if (options.headers) { |
| 27 | + Object.entries(options.headers).forEach(([key, value]) => |
| 28 | + xhr.setRequestHeader(key, value) |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + if (options.progressCallback) { |
| 33 | + xhr.addEventListener('progress', options.progressCallback); |
| 34 | + } |
| 35 | + |
| 36 | + return xhr; |
| 37 | +} |
| 38 | + |
| 39 | +function fetchBinary(url, options = {}) { |
| 40 | + return new Promise((resolve, reject) => { |
| 41 | + const xhr = openAsyncXHR('GET', url, options); |
| 42 | + |
| 43 | + xhr.onreadystatechange = (e) => { |
| 44 | + if (xhr.readyState === 4) { |
| 45 | + if (xhr.status === 200 || xhr.status === 0) { |
| 46 | + resolve(xhr.response); |
| 47 | + } else { |
| 48 | + reject({ xhr, e }); |
| 49 | + } |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + // Make request |
| 54 | + xhr.responseType = 'arraybuffer'; |
| 55 | + xhr.send(); |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +function fetchArray(instance = {}, baseURL, array, options = {}) { |
| 60 | + if (options && options.compression) { |
| 61 | + return REJECT_COMPRESSION(); |
| 62 | + } |
| 63 | + |
| 64 | + if (array.ref && !array.ref.pending) { |
| 65 | + return new Promise((resolve, reject) => { |
| 66 | + const url = [baseURL, array.ref.basepath, array.ref.id].join('/'); |
| 67 | + const xhr = openAsyncXHR('GET', url, options); |
| 68 | + |
| 69 | + xhr.onreadystatechange = (e) => { |
| 70 | + if (xhr.readyState === 1) { |
| 71 | + array.ref.pending = true; |
| 72 | + if (++requestCount === 1 && instance.invokeBusy) { |
| 73 | + instance.invokeBusy(true); |
| 74 | + } |
| 75 | + } |
| 76 | + if (xhr.readyState === 4) { |
| 77 | + array.ref.pending = false; |
| 78 | + if (xhr.status === 200 || xhr.status === 0) { |
| 79 | + array.buffer = xhr.response; |
| 80 | + |
| 81 | + if (array.ref.encode === 'JSON') { |
| 82 | + array.values = JSON.parse(array.buffer); |
| 83 | + } else { |
| 84 | + if (Endian.ENDIANNESS !== array.ref.encode && Endian.ENDIANNESS) { |
| 85 | + // Need to swap bytes |
| 86 | + vtkDebugMacro(`Swap bytes of ${array.name}`); |
| 87 | + Endian.swapBytes( |
| 88 | + array.buffer, |
| 89 | + DataTypeByteSize[array.dataType] |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + array.values = new window[array.dataType](array.buffer); |
| 94 | + } |
| 95 | + |
| 96 | + if (array.values.length !== array.size) { |
| 97 | + vtkErrorMacro( |
| 98 | + `Error in FetchArray: ${array.name}, does not have the proper array size. Got ${array.values.length}, instead of ${array.size}` |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + // Done with the ref and work |
| 103 | + delete array.ref; |
| 104 | + if (--requestCount === 0 && instance.invokeBusy) { |
| 105 | + instance.invokeBusy(false); |
| 106 | + } |
| 107 | + if (instance.modified) { |
| 108 | + instance.modified(); |
| 109 | + } |
| 110 | + resolve(array); |
| 111 | + } else { |
| 112 | + reject({ xhr, e }); |
| 113 | + } |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + // Make request |
| 118 | + xhr.responseType = array.dataType !== 'string' ? 'arraybuffer' : 'text'; |
| 119 | + xhr.send(); |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + return Promise.resolve(array); |
| 124 | +} |
| 125 | + |
| 126 | +// ---------------------------------------------------------------------------- |
| 127 | + |
| 128 | +function fetchJSON(instance = {}, url, options = {}) { |
| 129 | + if (options && options.compression) { |
| 130 | + return REJECT_COMPRESSION(); |
| 131 | + } |
| 132 | + |
| 133 | + return new Promise((resolve, reject) => { |
| 134 | + const xhr = openAsyncXHR('GET', url, options); |
| 135 | + |
| 136 | + xhr.onreadystatechange = (e) => { |
| 137 | + if (xhr.readyState === 1) { |
| 138 | + if (++requestCount === 1 && instance.invokeBusy) { |
| 139 | + instance.invokeBusy(true); |
| 140 | + } |
| 141 | + } |
| 142 | + if (xhr.readyState === 4) { |
| 143 | + if (--requestCount === 0 && instance.invokeBusy) { |
| 144 | + instance.invokeBusy(false); |
| 145 | + } |
| 146 | + if (xhr.status === 200 || xhr.status === 0) { |
| 147 | + resolve(JSON.parse(xhr.responseText)); |
| 148 | + } else { |
| 149 | + reject({ xhr, e }); |
| 150 | + } |
| 151 | + } |
| 152 | + }; |
| 153 | + |
| 154 | + // Make request |
| 155 | + xhr.responseType = 'text'; |
| 156 | + xhr.send(); |
| 157 | + }); |
| 158 | +} |
| 159 | + |
| 160 | +// ---------------------------------------------------------------------------- |
| 161 | + |
| 162 | +function fetchText(instance = {}, url, options = {}) { |
| 163 | + if (options && options.compression) { |
| 164 | + return REJECT_COMPRESSION(); |
| 165 | + } |
| 166 | + |
| 167 | + return new Promise((resolve, reject) => { |
| 168 | + const xhr = openAsyncXHR('GET', url, options); |
| 169 | + |
| 170 | + xhr.onreadystatechange = (e) => { |
| 171 | + if (xhr.readyState === 1) { |
| 172 | + if (++requestCount === 1 && instance.invokeBusy) { |
| 173 | + instance.invokeBusy(true); |
| 174 | + } |
| 175 | + } |
| 176 | + if (xhr.readyState === 4) { |
| 177 | + if (--requestCount === 0 && instance.invokeBusy) { |
| 178 | + instance.invokeBusy(false); |
| 179 | + } |
| 180 | + if (xhr.status === 200 || xhr.status === 0) { |
| 181 | + resolve(xhr.responseText); |
| 182 | + } else { |
| 183 | + reject({ xhr, e }); |
| 184 | + } |
| 185 | + } |
| 186 | + }; |
| 187 | + |
| 188 | + // Make request |
| 189 | + xhr.responseType = 'text'; |
| 190 | + xhr.send(); |
| 191 | + }); |
| 192 | +} |
| 193 | + |
| 194 | +// ---------------------------------------------------------------------------- |
| 195 | + |
| 196 | +function fetchImage(instance = {}, url, options = {}) { |
| 197 | + return new Promise((resolve, reject) => { |
| 198 | + const img = new Image(); |
| 199 | + if (options.crossOrigin) { |
| 200 | + img.crossOrigin = options.crossOrigin; |
| 201 | + } |
| 202 | + img.onload = () => resolve(img); |
| 203 | + img.onerror = reject; |
| 204 | + img.src = url; |
| 205 | + }); |
| 206 | +} |
| 207 | +/* eslint-enable prefer-promise-reject-errors */ |
| 208 | + |
| 209 | +// ---------------------------------------------------------------------------- |
| 210 | + |
| 211 | +const LiteHttpDataAccessHelper = { |
| 212 | + fetchArray, |
| 213 | + fetchJSON, |
| 214 | + fetchText, |
| 215 | + fetchBinary, // Only for HTTP |
| 216 | + fetchImage, |
| 217 | +}; |
| 218 | + |
| 219 | +registerType('http', (options) => LiteHttpDataAccessHelper); |
| 220 | + |
| 221 | +export default LiteHttpDataAccessHelper; |
0 commit comments