All files / Sources/IO/Core/DataAccessHelper HtmlDataAccessHelper.js

7.14% Statements 4/56
0% Branches 0/42
0% Functions 0/12
7.27% Lines 4/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                1x   1x                                                                                                                                                                                                                                                       1x             1x        
import { decompressSync, strFromU8 } from 'fflate';
 
import macro from 'vtk.js/Sources/macros';
import Base64 from 'vtk.js/Sources/Common/Core/Base64';
import Endian from 'vtk.js/Sources/Common/Core/Endian';
import { DataTypeByteSize } from 'vtk.js/Sources/Common/Core/DataArray/Constants';
import { registerType } from 'vtk.js/Sources/IO/Core/DataAccessHelper';
 
const { vtkErrorMacro, vtkDebugMacro } = macro;
 
let requestCount = 0;
 
function getContent(url) {
  const el = document.querySelector(`.webResource[data-url="${url}"]`);
  return el ? el.innerHTML : null;
}
 
function getElement(url) {
  return document.querySelector(`.webResource[data-url="${url}"]`);
}
 
function removeLeadingSlash(str) {
  return str[0] === '/' ? str.substr(1) : str;
}
 
function fetchText(instance, url, options = {}) {
  return new Promise((resolve, reject) => {
    const txt = getContent(url);
    if (txt === null) {
      reject(new Error(`No such text ${url}`));
    } else {
      resolve(txt);
    }
  });
}
 
function fetchJSON(instance, url, options = {}) {
  return new Promise((resolve, reject) => {
    const txt = getContent(removeLeadingSlash(url));
    if (txt === null) {
      reject(new Error(`No such JSON ${url}`));
    } else {
      resolve(JSON.parse(txt));
    }
  });
}
 
function fetchArray(instance, baseURL, array, options = {}) {
  return new Promise((resolve, reject) => {
    const url = removeLeadingSlash(
      [
        baseURL,
        array.ref.basepath,
        options.compression ? `${array.ref.id}.gz` : array.ref.id,
      ].join('/')
    );
 
    const txt = getContent(url);
    if (txt === null) {
      reject(new Error(`No such array ${url}`));
    } else {
      if (array.dataType === 'string') {
        let bText = atob(txt);
        if (options.compression) {
          bText = strFromU8(decompressSync(bText));
        }
        array.values = JSON.parse(bText);
      } else {
        const uint8array = new Uint8Array(Base64.toArrayBuffer(txt));
 
        array.buffer = new ArrayBuffer(uint8array.length);
 
        // copy uint8array to buffer
        const view = new Uint8Array(array.buffer);
        view.set(uint8array);
 
        if (options.compression) {
          if (array.dataType === 'string' || array.dataType === 'JSON') {
            array.buffer = strFromU8(
              decompressSync(new Uint8Array(array.buffer))
            );
          } else {
            array.buffer = decompressSync(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 ${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: ${array.name} does not have the proper array size. Got ${array.values.length}, instead of ${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);
    }
  });
}
 
// ----------------------------------------------------------------------------
 
function fetchImage(instance, url, options = {}) {
  return new Promise((resolve, reject) => {
    const img = getElement(url);
    if (img) {
      resolve(img);
    } else {
      reject(new Error(`No such image ${url}`));
    }
  });
}
 
// ----------------------------------------------------------------------------
 
const HtmlDataAccessHelper = {
  fetchJSON,
  fetchText,
  fetchArray,
  fetchImage,
};
 
registerType('html', (options) => HtmlDataAccessHelper);
 
// Export fetch methods
export default HtmlDataAccessHelper;