All files / Sources/IO/XML/XMLReader index.js

26.46% Statements 77/291
19.8% Branches 20/101
29.41% Functions 10/34
26.08% Lines 72/276

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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749                                          4x       4x         2x         2x 2x 2x         1x                             1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           4x 4x 4x 4x 20x 20x         4x 4x 4x                                                                                                                               4x     4x 4x                 4x                         4x     4x 2x     2x 2x         2x 2x 2x     2x 2x 2x 2x 2x 2x   2x   2x         2x         2x           2x 2x 2x 2x     12x 12x                             2x   2x   2x   2x       2x 2x                                               2x 2x                                   2x 2x 2x   2x               2x   2x               2x   2x 2x   2x                                 2x     4x                 1x                 4x     4x 4x 4x 4x     4x            
import { create } from 'xmlbuilder2';
import { decompressSync } from 'fflate';
 
import DataAccessHelper from 'vtk.js/Sources/IO/Core/DataAccessHelper';
import Base64 from 'vtk.js/Sources/Common/Core/Base64';
import macro from 'vtk.js/Sources/macros';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkStringArray from 'vtk.js/Sources/Common/Core/StringArray';
import BinaryHelper from 'vtk.js/Sources/IO/Core/BinaryHelper';
 
// Enable data soure for DataAccessHelper
import 'vtk.js/Sources/IO/Core/DataAccessHelper/LiteHttpDataAccessHelper'; // Just need HTTP
// import 'vtk.js/Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper'; // HTTP + zip
// import 'vtk.js/Sources/IO/Core/DataAccessHelper/HtmlDataAccessHelper'; // html + base64 + zip
// import 'vtk.js/Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper'; // zip
 
// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------
 
export function findAllTags(node, tagName) {
  return [...node.getElementsByTagName(tagName)];
}
 
export function findFirstTag(node, tagName) {
  return findAllTags(node, tagName)[0];
}
 
function parseXML(xmlStr) {
  // see xmlbuilder2 docs on the object format
  return create(xmlStr);
}
 
function extractAppendedData(buffer) {
  // search for appended data tag
  const prefixRegex = /^\s*<AppendedData\s+encoding="raw">\s*_/m;
  const suffixRegex = /\n\s*<\/AppendedData>/m;
  return BinaryHelper.extractBinary(buffer, prefixRegex, suffixRegex);
}
 
// ----------------------------------------------------------------------------
 
const TYPED_ARRAY = {
  Int8: Int8Array,
  UInt8: Uint8Array,
  Int16: Int16Array,
  UInt16: Uint16Array,
  Int32: Int32Array,
  UInt32: Uint32Array,
  Int64: Int32Array, // Not supported with JavaScript will cause error in binary
  UInt64: Uint32Array, // Not supported with JavaScript will cause error in binary
  Float32: Float32Array,
  Float64: Float64Array,
};
 
// ----------------------------------------------------------------------------
 
const TYPED_ARRAY_BYTES = {
  Int8: 1,
  UInt8: 1,
  Int16: 2,
  UInt16: 2,
  Int32: 4,
  UInt32: 4,
  Int64: 8, // Not supported with JavaScript will cause error in binary
  UInt64: 8, // Not supported with JavaScript will cause error in binary
  Float32: 4,
  Float64: 8,
};
 
// ----------------------------------------------------------------------------
 
function integer64to32(array) {
  const maxIdx = array.length - 1; // Skip last
  return array.filter((v, i) => i < maxIdx && i % 2 === 0);
}
 
// ----------------------------------------------------------------------------
 
/**
 * Reads the data of a length-prefixed blob.
 *
 * Int64 headers and values are not supported.
 *
 * @param {Uint8Array} uint8 a uint8 view of the length-prefixed blob.
 * @param {string} dataType
 * @param {string} headerType either UInt64 or UInt32
 */
function readLengthPrefixedData(uint8, dataType, headerType) {
  if (!['UInt64', 'UInt32'].includes(headerType)) {
    throw new Error(`Cannot handle a header type of ${headerType}`);
  }
 
  let HeaderTypedArray = TYPED_ARRAY[headerType];
  let DataTypedArray = TYPED_ARRAY[dataType];
  let dataFallbackTo32 = false;
 
  if (headerType === 'UInt64') {
    HeaderTypedArray = TYPED_ARRAY.UInt32;
  }
 
  if (/^U?Int64$/.test(dataType)) {
    dataFallbackTo32 = true;
    DataTypedArray = TYPED_ARRAY[dataType.replace('64', '32')];
  }
 
  const { byteOffset } = uint8;
  const dataWordSize = TYPED_ARRAY_BYTES[dataType];
  const headerWordSize = TYPED_ARRAY_BYTES[headerType];
 
  // slice if offset is not word aligned
  let header;
  if (byteOffset % headerWordSize === 0) {
    header = new HeaderTypedArray(uint8.buffer, byteOffset, 1);
  } else {
    header = new HeaderTypedArray(
      uint8.buffer.slice(byteOffset, byteOffset + headerWordSize)
    );
  }
 
  const dataByteLength = Number(header[0]);
 
  // read values
  let values;
  let arraySize = dataByteLength / dataWordSize;
 
  if (dataFallbackTo32) {
    // We are reading int64 data with an int32 typed array.
    arraySize *= 2;
  }
 
  // slice if offset is not word aligned
  const dataOffset = byteOffset + headerWordSize;
  if (dataOffset % dataWordSize === 0) {
    values = new DataTypedArray(uint8.buffer, dataOffset, arraySize);
  } else {
    values = new DataTypedArray(
      uint8.buffer.slice(dataOffset, dataOffset + dataByteLength)
    );
  }
 
  if (dataFallbackTo32) {
    // remove higher order 32 bits
    values = integer64to32(values);
  }
 
  return values;
}
 
// ----------------------------------------------------------------------------
 
function readerHeader(uint8, headerType) {
  // We do not handle endianness or if more than 32 bits are needed to encode the data
  if (headerType === 'UInt64') {
    const offset = 8;
    let uint32 = new Uint32Array(uint8.buffer, 0, 6);
    const nbBlocks = uint32[0];
    const s1 = uint32[2];
    const s2 = uint32[4];
    const resultArray = [offset, nbBlocks, s1, s2];
    uint32 = new Uint32Array(uint8.buffer, 3 * 8, nbBlocks * 2);
    for (let i = 0; i < nbBlocks; i++) {
      resultArray.push(uint32[i * 2]);
    }
    return resultArray;
  }
  // UInt32
  let uint32 = new Uint32Array(uint8.buffer, 0, 3);
  const offset = 4;
  const nbBlocks = uint32[0];
  const s1 = uint32[1];
  const s2 = uint32[2];
  const resultArray = [offset, nbBlocks, s1, s2];
  uint32 = new Uint32Array(uint8.buffer, 3 * 4, nbBlocks);
  for (let i = 0; i < nbBlocks; i++) {
    resultArray.push(uint32[i]);
  }
  return resultArray;
}
 
// ----------------------------------------------------------------------------
 
/**
 * Given the return value of readerHeader, return header byte length.
 */
function getHeaderByteLength(header, headerType) {
  // ignore the first number, which isn't actually part of the header.
  const [headerWordSize] = header;
  return (header.length - 1) * headerWordSize;
}
 
// ----------------------------------------------------------------------------
 
function uncompressBlock(compressedUint8, output) {
  const uncompressedBlock = decompressSync(compressedUint8);
  output.uint8.set(uncompressedBlock, output.offset);
  output.offset += uncompressedBlock.length;
}
 
// ----------------------------------------------------------------------------
 
function decompressZLib(bytes, headerType) {
  // ----------------------------------------------------------------------
  // Layout of the data
  // header[N, s1, s2, blockSize1, ..., blockSizeN], [padding???], block[compressedData], ..., block[compressedData]
  // [header] N, s1 and s2 are uint 32 or 64 (defined by header_type="UInt64" attribute on the root node)
  // [header] s1: uncompress size of each block except the last one
  // [header] s2: uncompress size of the last blocks
  // [header] blockSize: size of the block in compressed space that represent to bloc to inflate in zlib. (This also give the offset to the next block)
  // ----------------------------------------------------------------------
 
  // Header reading: [offset, numBlocks, s1, s2]
  const header = readerHeader(bytes, headerType);
  const nbBlocks = header[1];
  const s1 = header[2];
  const s2 = header[3];
  let dataByteLength = 0;
 
  if (nbBlocks > 0) {
    // If the last block's size is labeled as 0, that means the last block
    // really has size header[2].
    if (s2 === 0) {
      dataByteLength = nbBlocks * s1;
    } else {
      dataByteLength = (nbBlocks - 1) * s1 + s2;
    }
  }
 
  const buffer = new ArrayBuffer(dataByteLength);
  const output = {
    offset: 0,
    uint8: new Uint8Array(buffer),
  };
 
  let offset = getHeaderByteLength(header, headerType);
  // Starting from end of the data header, find the zlib header, which starts with 0x78.
  // This accounts for any padding between the header and the compressed data.
  while (offset < bytes.length && bytes[offset] !== 0x78) {
    offset++;
  }
 
  for (let i = 0; i < nbBlocks; i++) {
    const blockSize = header[4 + i];
    const compressedBlock = new Uint8Array(bytes.buffer, offset, blockSize);
    uncompressBlock(compressedBlock, output);
    offset += blockSize;
  }
 
  return output.uint8;
}
 
// ----------------------------------------------------------------------------
 
function processDataArray(
  size,
  dataArrayElem,
  compressor,
  byteOrder,
  headerType,
  binaryBuffer
) {
  const dataType = dataArrayElem.getAttribute('type');
  const name = dataArrayElem.getAttribute('Name');
  const format = dataArrayElem.getAttribute('format'); // binary, ascii, appended
  const numberOfComponents = Number(
    dataArrayElem.getAttribute('NumberOfComponents') || '1'
  );
  let values = null;
 
  if (format === 'ascii') {
    values = new TYPED_ARRAY[dataType](size * numberOfComponents);
    let offset = 0;
    dataArrayElem.firstChild.nodeValue.split(/[\\t \\n]+/).forEach((token) => {
      if (token.trim().length) {
        values[offset++] = Number(token);
      }
    });
  } else if (format === 'binary') {
    const uint8 = new Uint8Array(
      Base64.toArrayBuffer(dataArrayElem.firstChild.nodeValue.trim())
    );
    if (compressor === 'vtkZLibDataCompressor') {
      const data = decompressZLib(uint8, headerType);
      values = new TYPED_ARRAY[dataType](data.buffer);
 
      // Handle (u)int64 hoping for no overflow...
      if (/^U?Int64$/.test(dataType)) {
        values = integer64to32(values);
      }
    } else {
      values = new TYPED_ARRAY[dataType](
        uint8.buffer,
        TYPED_ARRAY_BYTES[headerType]
      );
 
      // Handle (u)int64 hoping no overflow...
      if (dataType.indexOf('Int64') !== -1) {
        values = integer64to32(values);
      }
    }
  } else if (format === 'appended') {
    const offset = Number(dataArrayElem.getAttribute('offset'));
    values = readLengthPrefixedData(
      new Uint8Array(binaryBuffer, offset),
      dataType,
      headerType
    );
  } else {
    console.error('Format not supported', format);
  }
 
  return { name, values, numberOfComponents };
}
 
// ----------------------------------------------------------------------------
 
function decodeStringArrayFromBytes(bytes) {
  const decoder = new TextDecoder();
  const decoded = decoder.decode(bytes);
 
  // strings are null-terminated
  return decoded.split('\x00').slice(0, -1);
}
 
// ----------------------------------------------------------------------------
 
function processStringArray(
  stringArrayElem,
  compressor,
  byteOrder,
  headerType,
  binaryBuffer
) {
  const name = stringArrayElem.getAttribute('Name');
  const format = stringArrayElem.getAttribute('format'); // binary, ascii, appended
  const numberOfComponents = Number(
    stringArrayElem.getAttribute('NumberOfComponents') || '1'
  );
  const numberOfTuples = Number(
    stringArrayElem.getAttribute('NumberOfTuples') || '1'
  );
  const nbStrings = numberOfTuples * numberOfComponents;
  const strings = [];
 
  if (format === 'ascii') {
    const tokens = stringArrayElem.firstChild.nodeValue.trim().split(/\s+/);
    let tokIdx = 0;
    const strChars = [];
 
    while (strings.length < nbStrings) {
      const token = Number(tokens[tokIdx++]);
      if (token === 0) {
        strings.push(strChars.join(''));
        strChars.length = 0;
      } else {
        strChars.push(String.fromCharCode(token));
      }
    }
  } else if (format === 'binary') {
    const uint8 = new Uint8Array(
      Base64.toArrayBuffer(stringArrayElem.firstChild.nodeValue.trim())
    );
    if (compressor === 'vtkZLibDataCompressor') {
      const decompressed = decompressZLib(uint8, headerType);
      strings.push(...decodeStringArrayFromBytes(decompressed));
    } else {
      const strData = readLengthPrefixedData(uint8, 'UInt8', headerType);
      strings.push(...decodeStringArrayFromBytes(strData));
    }
  } else if (format === 'appended') {
    const offset = Number(stringArrayElem.getAttribute('offset'));
    const values = readLengthPrefixedData(
      new Uint8Array(binaryBuffer, offset),
      'UInt8',
      headerType
    );
    strings.push(...decodeStringArrayFromBytes(values));
  } else {
    macro.vtkErrorMacro(`Format not supported: ${format}`);
  }
 
  return { name, values: strings, numberOfComponents };
}
 
// ----------------------------------------------------------------------------
 
function processCells(
  size,
  containerElem,
  compressor,
  byteOrder,
  headerType,
  binaryBuffer
) {
  const arrayElems = {};
  const dataArrayElems = containerElem.getElementsByTagName('DataArray');
  for (let elIdx = 0; elIdx < dataArrayElems.length; elIdx++) {
    const el = dataArrayElems[elIdx];
    arrayElems[el.getAttribute('Name')] = el;
  }
 
  const offsets = processDataArray(
    size,
    arrayElems.offsets,
    compressor,
    byteOrder,
    headerType,
    binaryBuffer
  ).values;
  const connectivitySize = offsets[offsets.length - 1];
  const connectivity = processDataArray(
    connectivitySize,
    arrayElems.connectivity,
    compressor,
    byteOrder,
    headerType,
    binaryBuffer
  ).values;
  const values = new Uint32Array(size + connectivitySize);
  let writeOffset = 0;
  let previousOffset = 0;
  offsets.forEach((v) => {
    const cellSize = v - previousOffset;
    values[writeOffset++] = cellSize;
 
    for (let i = 0; i < cellSize; i++) {
      values[writeOffset++] = connectivity[previousOffset + i];
    }
 
    // save previous offset
    previousOffset = v;
  });
 
  return values;
}
 
// ----------------------------------------------------------------------------
 
function processFieldData(
  size,
  fieldElem,
  fieldContainer,
  compressor,
  byteOrder,
  headerType,
  binaryBuffer
) {
  if (fieldElem) {
    const attributes = ['Scalars', 'Vectors', 'Normals', 'Tensors', 'TCoords'];
    const nameBinding = {};
    attributes.forEach((attrName) => {
      const arrayName = fieldElem.getAttribute(attrName);
      Iif (arrayName) {
        nameBinding[arrayName] = fieldContainer[`set${attrName}`];
      }
    });
 
    const dataArrayElems = fieldElem.getElementsByTagName('DataArray');
    const nbArrays = dataArrayElems.length;
    for (let idx = 0; idx < nbArrays; idx++) {
      const array = dataArrayElems[idx];
      const dataArray = vtkDataArray.newInstance(
        processDataArray(
          size,
          array,
          compressor,
          byteOrder,
          headerType,
          binaryBuffer
        )
      );
      const name = dataArray.getName();
      (nameBinding[name] || fieldContainer.addArray)(dataArray);
    }
  }
}
 
// ----------------------------------------------------------------------------
function handleFieldDataArrays(
  fieldDataElem,
  compressor,
  byteOrder,
  headerType,
  binaryBuffer
) {
  const dataArrays = [...fieldDataElem.getElementsByTagName('DataArray')].map(
    (daElem) =>
      vtkDataArray.newInstance(
        processDataArray(
          Number(daElem.getAttribute('NumberOfTuples')),
          daElem,
          compressor,
          byteOrder,
          headerType,
          binaryBuffer
        )
      )
  );
 
  const stringArrays = [...fieldDataElem.getElementsByTagName('Array')]
    .filter((elem) => elem.getAttribute('type') === 'String')
    .map((arrElem) => {
      const sa = vtkStringArray.newInstance(
        processStringArray(
          arrElem,
          compressor,
          byteOrder,
          headerType,
          binaryBuffer
        )
      );
      return sa;
    });
 
  return [...dataArrays, ...stringArrays];
}
 
// ----------------------------------------------------------------------------
// vtkXMLReader methods
// ----------------------------------------------------------------------------
 
function vtkXMLReader(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkXMLReader');
 
  // Create default dataAccessHelper if not available
  if (!model.dataAccessHelper) {
    model.dataAccessHelper = DataAccessHelper.get('http');
  }
 
  // Internal method to fetch Array
  function fetchData(url, option = {}) {
    return model.dataAccessHelper.fetchBinary(url, option);
  }
 
  // Set DataSet url
  publicAPI.setUrl = (url, option = {}) => {
    model.url = url;
 
    // Remove the file in the URL
    const path = url.split('/');
    path.pop();
    model.baseURL = path.join('/');
 
    // Fetch metadata
    return publicAPI.loadData(option);
  };
 
  // Fetch the actual data arrays
  publicAPI.loadData = (option = {}) =>
    fetchData(model.url, option).then(publicAPI.parseAsArrayBuffer);
 
  publicAPI.parseAsArrayBuffer = (arrayBuffer) => {
    Iif (!arrayBuffer) {
      return false;
    }
    if (arrayBuffer !== model.rawDataBuffer) {
      publicAPI.modified();
    } else E{
      return true;
    }
 
    const { text: content, binaryBuffer } = extractAppendedData(arrayBuffer);
    model.rawDataBuffer = arrayBuffer;
    model.binaryBuffer = binaryBuffer;
 
    // Parse data here...
    const doc = parseXML(content);
    const root = doc.root();
    const rootElem = root.node;
    const type = rootElem.getAttribute('type');
    const compressor = rootElem.getAttribute('compressor');
    const byteOrder = rootElem.getAttribute('byte_order');
    // default to UInt32. I think version 0.1 vtp/vti files default to UInt32.
    const headerType = rootElem.getAttribute('header_type') || 'UInt32';
 
    Iif (compressor && compressor !== 'vtkZLibDataCompressor') {
      console.error('Invalid compressor', compressor);
      return false;
    }
 
    Iif (byteOrder && byteOrder !== 'LittleEndian') {
      console.error('Only LittleEndian encoding is supported');
      return false;
    }
 
    Iif (type !== model.dataType) {
      console.error('Invalid data type', type, 'expecting', model.dataType);
      return false;
    }
 
    // appended format
    if (findFirstTag(rootElem, 'AppendedData')) {
      const appendedDataElem = findFirstTag(rootElem, 'AppendedData');
      const encoding = appendedDataElem.getAttribute('encoding');
      const arrays = root
        .filter(
          (xmlNode) => {
            const { node } = xmlNode;
            return (
              node.nodeType === Node.ELEMENT_NODE &&
              node.getAttribute('format') === 'appended' &&
              node.hasAttribute('offset')
            );
          },
          false,
          true
        )
        .map((xmlNode) => ({
          node: xmlNode.node,
          offset: Number(xmlNode.node.getAttribute('offset')),
        }));
 
      // sort dataElems by increasing offset
      arrays.sort((a, b) => a.offset - b.offset);
 
      let appendedBuffer = model.binaryBuffer;
 
      if (encoding === 'base64') {
        // substr(1) is to remove the '_' prefix
        appendedBuffer = appendedDataElem.textContent.trim().substr(1);
      }
 
      // get data array chunks
      const dataArrays = [];
      for (let i = 0; i < arrays.length; ++i) {
        const offset = arrays[i].offset;
        let nextOffset = 0;
        if (i === arrays.length - 1) {
          nextOffset = appendedBuffer.length || appendedBuffer.byteLength;
        } else {
          nextOffset = arrays[i + 1].offset;
        }
 
        if (encoding === 'base64') {
          dataArrays.push(
            new Uint8Array(
              Base64.toArrayBuffer(appendedBuffer.substring(offset, nextOffset))
            )
          );
        } else {
          // encoding === 'raw'
          // Need to slice the ArrayBuffer so readerHeader() works properly
          dataArrays.push(
            new Uint8Array(appendedBuffer.slice(offset, nextOffset))
          );
        }
      }
 
      if (compressor === 'vtkZLibDataCompressor') {
        for (let arrayidx = 0; arrayidx < dataArrays.length; ++arrayidx) {
          const dataArray = dataArrays[arrayidx];
 
          const uncompressed = decompressZLib(dataArray, headerType);
 
          const data = new Uint8Array(
            uncompressed.length + TYPED_ARRAY_BYTES[headerType]
          );
          // set length header
          // TODO this does not work for lengths that are greater than the max Uint32 value.
          new TYPED_ARRAY[headerType](data.buffer, 0, 1)[0] =
            uncompressed.length;
          data.set(uncompressed, TYPED_ARRAY_BYTES[headerType]);
 
          dataArrays[arrayidx] = data;
        }
      }
 
      const bufferLength = dataArrays.reduce((acc, arr) => acc + arr.length, 0);
      const buffer = new ArrayBuffer(bufferLength);
      const view = new Uint8Array(buffer);
 
      for (let i = 0, offset = 0; i < dataArrays.length; ++i) {
        // set correct offsets
        arrays[i].node.setAttribute('offset', offset);
        // set final buffer data
        view.set(dataArrays[i], offset);
        offset += dataArrays[i].length;
      }
 
      model.binaryBuffer = buffer;
 
      Iif (!model.binaryBuffer) {
        console.error(
          'Processing appended data format: requires binaryBuffer to parse'
        );
        return false;
      }
    }
 
    publicAPI.parseXML(rootElem, type, compressor, byteOrder, headerType);
 
    const datasetElem = rootElem.getElementsByTagName(type)[0];
    const fieldDataElem = datasetElem.getElementsByTagName('FieldData')[0];
 
    Iif (fieldDataElem) {
      const fieldDataArrays = handleFieldDataArrays(
        fieldDataElem,
        compressor,
        byteOrder,
        headerType,
        model.binaryBuffer
      );
 
      for (let i = 0; i < model.output.length; i++) {
        const fieldData = model.output[i].getFieldData();
        for (let j = 0; j < fieldDataArrays.length; j++) {
          fieldData.addArray(fieldDataArrays[j]);
        }
      }
    }
 
    return true;
  };
 
  publicAPI.requestData = (inData, outData) => {
    publicAPI.parseAsArrayBuffer(model.rawDataBuffer);
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  // baseURL: null,
  // dataAccessHelper: null,
  // url: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.get(publicAPI, model, ['url', 'baseURL']);
  macro.setGet(publicAPI, model, ['dataAccessHelper']);
  macro.algo(publicAPI, model, 0, 1);
 
  // vtkXMLReader methods
  vtkXMLReader(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export default { extend, processDataArray, processFieldData, processCells };