test-parallel-reads.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* global Blob, FileReader */
  2. import * as zip from "../../index.js";
  3. const KB = 1024;
  4. const ENTRIES_DATA = [
  5. { name: "entry #1", blob: getBlob(8.5 * KB) }, { name: "entry #2", blob: getBlob(5.2 * KB) }, { name: "entry #3", blob: getBlob(4.7 * KB) },
  6. { name: "entry #4", blob: getBlob(2.8 * KB) }, { name: "entry #5", blob: getBlob(1.9 * KB) }, { name: "entry #6", blob: getBlob(2.2 * KB) },
  7. { name: "entry #7", blob: getBlob(5.1 * KB) }, { name: "entry #8", blob: getBlob(2.6 * KB) }, { name: "entry #9", blob: getBlob(3.1 * KB) }];
  8. export { test };
  9. async function test() {
  10. zip.configure({ chunkSize: 128 });
  11. const blobWriter = new zip.BlobWriter("application/zip");
  12. const zipWriter = new zip.ZipWriter(blobWriter);
  13. for (const entryData of ENTRIES_DATA) {
  14. await zipWriter.add(entryData.name, new zip.BlobReader(entryData.blob));
  15. }
  16. await zipWriter.close();
  17. const zipReader = new zip.ZipReader(new zip.BlobReader(blobWriter.getData()));
  18. const entries = await zipReader.getEntries();
  19. const results = await Promise.all(entries.map(async (entry, indexEntry) => {
  20. const blob = await entry.getData(new zip.BlobWriter("application/octet-stream"));
  21. return compareResult(blob, indexEntry);
  22. }));
  23. zip.terminateWorkers();
  24. return !results.includes(false);
  25. }
  26. function compareResult(result, index) {
  27. return new Promise(resolve => {
  28. const fileReaderInput = new FileReader();
  29. const fileReaderOutput = new FileReader();
  30. let loadCount = 0;
  31. fileReaderInput.readAsArrayBuffer(ENTRIES_DATA[index].blob);
  32. fileReaderOutput.readAsArrayBuffer(result);
  33. fileReaderInput.onload = fileReaderOutput.onload = () => {
  34. loadCount++;
  35. if (loadCount == 2) {
  36. const valueInput = new Float64Array(fileReaderInput.result);
  37. const valueOutput = new Float64Array(fileReaderOutput.result);
  38. if (valueInput.length != valueOutput.length) {
  39. resolve(false);
  40. return;
  41. }
  42. for (let indexValue = 0, n = valueInput.length; indexValue < n; indexValue++) {
  43. if (valueInput[indexValue] != valueOutput[indexValue]) {
  44. resolve(false);
  45. return;
  46. }
  47. }
  48. resolve(true);
  49. }
  50. };
  51. });
  52. }
  53. function getBlob(size) {
  54. const data = new Float64Array(Math.floor(size / 8));
  55. for (let indexData = 0; indexData < data.length; indexData++) {
  56. data[indexData] = Math.random();
  57. }
  58. return new Blob([data]);
  59. }