zlib-1.3.1.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. (function () {
  3. let ret;
  4. async function initialize() {
  5. if (ret) return ret;
  6. const COMPRESSION_LEVEL = 7;
  7. const NO_ZLIB_HEADER = -1;
  8. const CHUNK_SIZE = 32 * 1024;
  9. const map = {};
  10. const memory = new WebAssembly.Memory({
  11. initial: 1,
  12. maximum: 1024, // 64MB
  13. });
  14. const env = {
  15. memory,
  16. writeToJs(ptr, size) {
  17. const o = map[ptr];
  18. o.onData(new Uint8Array(memory.buffer, dstPtr, size));
  19. },
  20. _abort: errno => { console.error(`Error: ${errno}`) },
  21. _grow: () => { },
  22. };
  23. const ins = (await WebAssembly.instantiateStreaming(fetch('js/zlib-1.3.1.wasm'), { env })).instance;
  24. const srcPtr = ins.exports._malloc(CHUNK_SIZE);
  25. const dstPtr = ins.exports._malloc(CHUNK_SIZE);
  26. class RawDef {
  27. constructor() {
  28. this.zstreamPtr = ins.exports._createDeflateContext(COMPRESSION_LEVEL, NO_ZLIB_HEADER);
  29. map[this.zstreamPtr] = this;
  30. this.offset = 0;
  31. this.buff = new Uint8Array(CHUNK_SIZE);
  32. }
  33. deflate(chunk, flush) {
  34. const src = new Uint8Array(memory.buffer, srcPtr, chunk.length);
  35. src.set(chunk);
  36. ins.exports._deflate(this.zstreamPtr, srcPtr, dstPtr, chunk.length, CHUNK_SIZE, flush);
  37. }
  38. onData(chunk) {
  39. if (this.buff.length < this.offset + chunk.length) {
  40. const buff = this.buff;
  41. this.buff = new Uint8Array(this.buff.length * 2);
  42. this.buff.set(buff);
  43. }
  44. this.buff.set(chunk, this.offset);
  45. this.offset += chunk.length;
  46. }
  47. destroy() {
  48. ins.exports._freeDeflateContext(this.zstreamPtr);
  49. delete map[this.zstreamPtr];
  50. this.buff = null;
  51. }
  52. getBuffer() {
  53. const res = new Uint8Array(this.offset);
  54. for (let i = 0; i < this.offset; ++i) {
  55. res[i] = this.buff[i];
  56. }
  57. return res;
  58. }
  59. }
  60. class RawInf {
  61. constructor() {
  62. this.zstreamPtr = ins.exports._createInflateContext(NO_ZLIB_HEADER);
  63. map[this.zstreamPtr] = this;
  64. this.offset = 0;
  65. this.buff = new Uint8Array(CHUNK_SIZE);
  66. }
  67. inflate(chunk) {
  68. const src = new Uint8Array(memory.buffer, srcPtr, chunk.length);
  69. src.set(chunk);
  70. ins.exports._inflate(this.zstreamPtr, srcPtr, dstPtr, chunk.length, CHUNK_SIZE);
  71. }
  72. onData(chunk) {
  73. if (this.buff.length < this.offset + chunk.length) {
  74. const buff = this.buff;
  75. this.buff = new Uint8Array(this.buff.length * 2);
  76. this.buff.set(buff);
  77. }
  78. this.buff.set(chunk, this.offset);
  79. this.offset += chunk.length;
  80. }
  81. destroy() {
  82. ins.exports._freeInflateContext(this.zstreamPtr);
  83. delete map[this.zstreamPtr];
  84. this.buff = null;
  85. }
  86. getBuffer() {
  87. const res = new Uint8Array(this.offset);
  88. for (let i = 0; i < this.offset; ++i) {
  89. res[i] = this.buff[i];
  90. }
  91. return res;
  92. }
  93. }
  94. ret = {
  95. inflate(rawDeflateBuffer) {
  96. const rawInf = new RawInf();
  97. for (let offset = 0; offset < rawDeflateBuffer.length; offset += CHUNK_SIZE) {
  98. const end = Math.min(offset + CHUNK_SIZE, rawDeflateBuffer.length);
  99. const chunk = rawDeflateBuffer.subarray(offset, end);
  100. rawInf.inflate(chunk);
  101. }
  102. const ret = rawInf.getBuffer();
  103. rawInf.destroy();
  104. return ret;
  105. },
  106. deflate(rawInflateBuffer) {
  107. const rawDef = new RawDef();
  108. for (let offset = 0; offset < rawInflateBuffer.length; offset += CHUNK_SIZE) {
  109. const end = Math.min(offset + CHUNK_SIZE, rawInflateBuffer.length);
  110. const chunk = rawInflateBuffer.subarray(offset, end);
  111. rawDef.deflate(chunk, rawInflateBuffer.length <= offset + CHUNK_SIZE);
  112. }
  113. const ret = rawDef.getBuffer();
  114. rawDef.destroy();
  115. return ret;
  116. },
  117. }
  118. return ret;
  119. }
  120. this.zlib = initialize();
  121. }).call(this);