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

2.54% Statements 3/118
0% Branches 0/75
0% Functions 0/25
2.63% Lines 3/114

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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240                1x                                                                                                                                                                                                                                                                                                                                                                                                                                                               1x       1x      
import { decompressSync, strFromU8, strToU8, unzipSync } from 'fflate';
 
import macro from 'vtk.js/Sources/macros';
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';
import { fromArrayBuffer } from 'vtk.js/Sources/Common/Core/Base64';
 
const { vtkErrorMacro, vtkDebugMacro } = macro;
 
function toMimeType(url) {
  const ext = url.split('.').pop().toLowerCase();
  if (ext === 'jpg') {
    return 'jpeg';
  }
  return ext;
}
 
function handleUint8Array(array, compression, done) {
  return (uint8array) => {
    array.buffer = new ArrayBuffer(uint8array.length);
 
    // copy uint8array to buffer
    const view = new Uint8Array(array.buffer);
    view.set(uint8array);
 
    if (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();
  };
}
 
function handleString(array, compression, done) {
  return (string) => {
    if (compression) {
      array.values = JSON.parse(strFromU8(decompressSync(string)));
    } else {
      array.values = JSON.parse(string);
    }
    done();
  };
}
 
function removeLeadingSlash(str) {
  return str[0] === '/' ? str.substr(1) : str;
}
 
function normalizePath(str) {
  return new URL(str, 'http://any').pathname;
}
 
function cleanUpPath(str) {
  return removeLeadingSlash(normalizePath(str));
}
 
function unpack(zipContent) {
  return new Promise((resolve, reject) => {
    if (typeof zipContent === 'string') {
      resolve(strToU8(zipContent));
    } else if (zipContent instanceof Blob) {
      resolve(zipContent.arrayBuffer().then((ab) => new Uint8Array(ab)));
    } else if (zipContent instanceof ArrayBuffer) {
      resolve(new Uint8Array(zipContent));
    } else if (zipContent?.buffer instanceof ArrayBuffer) {
      resolve(new Uint8Array(zipContent.buffer));
    } else {
      reject(new Error('Invalid datatype to unpack.'));
    }
  });
}
 
function create(createOptions) {
  let ready = false;
  let requestCount = 0;
  let decompressedFiles = null;
  let fullRootPath = '';
 
  unpack(createOptions.zipContent).then((zipFileData) => {
    decompressedFiles = unzipSync(zipFileData);
    ready = true;
 
    // Find root index.json
    const metaFiles = [];
    Object.keys(decompressedFiles).forEach((relativePath) => {
      if (relativePath.endsWith('index.json')) {
        metaFiles.push(relativePath);
      }
    });
    metaFiles.sort((a, b) => a.length - b.length);
    // if not empty, then fullRootPath will have a forward slash suffix
    fullRootPath = metaFiles[0].replace(/index\.json$/, '');
 
    if (createOptions.callback) {
      createOptions.callback(decompressedFiles);
    }
  });
 
  return {
    fetchArray(instance, baseURL, array, options = {}) {
      return new Promise((resolve, reject) => {
        if (!ready) {
          vtkErrorMacro('ERROR!!! zip not ready...');
        }
        const url = cleanUpPath(
          [
            baseURL,
            array.ref.basepath,
            options.compression ? `${array.ref.id}.gz` : array.ref.id,
          ].join('/')
        );
 
        if (++requestCount === 1 && instance?.invokeBusy) {
          instance.invokeBusy(true);
        }
 
        function doneCleanUp() {
          // Done with the ref and work
          delete array.ref;
          if (--requestCount === 0 && instance?.invokeBusy) {
            instance.invokeBusy(false);
          }
          if (instance?.modified) {
            instance.modified();
          }
          resolve(array);
        }
 
        const fileData = decompressedFiles[`${fullRootPath}${url}`];
 
        if (array.dataType === 'string' && !options.compression) {
          // string
          const handler = handleString(array, options.compression, doneCleanUp);
          handler(strFromU8(fileData));
        } else {
          // uint8array
          const handler = handleUint8Array(
            array,
            options.compression,
            doneCleanUp
          );
          handler(fileData);
        }
      });
    },
 
    fetchJSON(instance, url, options = {}) {
      const path = cleanUpPath(url);
      if (!ready) {
        vtkErrorMacro('ERROR!!! zip not ready...');
      }
      const fileData = decompressedFiles[`${fullRootPath}${path}`];
      if (options.compression) {
        if (options.compression === 'gz') {
          const str = strFromU8(decompressSync(fileData));
          return Promise.resolve(JSON.parse(str));
        }
        return Promise.reject(new Error('Invalid compression'));
      }
      return Promise.resolve(JSON.parse(strFromU8(fileData)));
    },
 
    fetchText(instance, url, options = {}) {
      const path = cleanUpPath(url);
      if (!ready) {
        vtkErrorMacro('ERROR!!! zip not ready...');
      }
      const fileData = decompressedFiles[`${fullRootPath}${path}`];
      if (options.compression) {
        if (options.compression === 'gz') {
          return Promise.resolve(strFromU8(unzipSync(fileData)));
        }
        return Promise.reject(new Error('Invalid compression'));
      }
      return Promise.resolve(strFromU8(fileData));
    },
 
    fetchImage(instance, url, options = {}) {
      const path = cleanUpPath(url);
      if (!ready) {
        vtkErrorMacro('ERROR!!! zip not ready...');
      }
      const fileData = decompressedFiles[`${fullRootPath}${path}`];
      return new Promise((resolve, reject) => {
        const img = new Image();
        img.onload = () => resolve(img);
        img.onerror = reject;
        const str = fromArrayBuffer(fileData.buffer);
        img.src = `data:image/${toMimeType(path)};base64,${str}`;
      });
    },
 
    fetchBinary(instance, url, options = {}) {
      const path = cleanUpPath(url);
      if (!ready) {
        vtkErrorMacro('ERROR!!! zip not ready...');
      }
      const fileData = decompressedFiles[`${fullRootPath}${path}`];
      if (options.compression) {
        if (options.compression === 'gz') {
          return Promise.resolve(decompressSync(fileData).buffer);
        }
        return Promise.reject(new Error('Invalid compression'));
      }
      return Promise.resolve(fileData.buffer);
    },
  };
}
 
const JSZipDataAccessHelper = {
  create,
};
 
registerType('zip', (options) => JSZipDataAccessHelper.create(options));
 
export default JSZipDataAccessHelper;