1
0

zlib.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 createModule = (await import('./zlib-1.3.2.js')).default;
  10. const Module = await createModule({map: {}});
  11. const srcPtr = Module.__malloc(CHUNK_SIZE);
  12. const dstPtr = Module.__malloc(CHUNK_SIZE);
  13. class RawDef {
  14. constructor() {
  15. this.zstreamPtr = Module.__createDeflateContext(COMPRESSION_LEVEL, NO_ZLIB_HEADER);
  16. Module.map[this.zstreamPtr] = this;
  17. this.offset = 0;
  18. this.buff = new Uint8Array(CHUNK_SIZE);
  19. }
  20. deflate(chunk, flush) {
  21. Module.HEAPU8.set(chunk, srcPtr);
  22. Module.__deflate(this.zstreamPtr, srcPtr, dstPtr, chunk.length, CHUNK_SIZE, flush);
  23. }
  24. onData(chunk) {
  25. if (this.buff.length < this.offset + chunk.length) {
  26. const buff = this.buff;
  27. this.buff = new Uint8Array(this.buff.length * 2);
  28. this.buff.set(buff);
  29. }
  30. this.buff.set(chunk, this.offset);
  31. this.offset += chunk.length;
  32. }
  33. destroy() {
  34. Module.__freeDeflateContext(this.zstreamPtr);
  35. delete Module.map[this.zstreamPtr];
  36. this.buff = null;
  37. }
  38. getBuffer() {
  39. return this.buff.slice(0, this.offset);
  40. }
  41. }
  42. class RawInf {
  43. constructor() {
  44. this.zstreamPtr = Module.__createInflateContext(NO_ZLIB_HEADER);
  45. Module.map[this.zstreamPtr] = this;
  46. this.offset = 0;
  47. this.buff = new Uint8Array(CHUNK_SIZE);
  48. }
  49. inflate(chunk) {
  50. Module.HEAPU8.set(chunk, srcPtr);
  51. Module.__inflate(this.zstreamPtr, srcPtr, dstPtr, chunk.length, CHUNK_SIZE);
  52. }
  53. onData(chunk) {
  54. if (this.buff.length < this.offset + chunk.length) {
  55. const buff = this.buff;
  56. this.buff = new Uint8Array(this.buff.length * 2);
  57. this.buff.set(buff);
  58. }
  59. this.buff.set(chunk, this.offset);
  60. this.offset += chunk.length;
  61. }
  62. destroy() {
  63. Module.__freeInflateContext(this.zstreamPtr);
  64. delete Module.map[this.zstreamPtr];
  65. this.buff = null;
  66. }
  67. getBuffer() {
  68. return this.buff.slice(0, this.offset);
  69. }
  70. }
  71. ret = {
  72. inflate(rawDeflateBuffer) {
  73. const rawInf = new RawInf();
  74. for (let offset = 0; offset < rawDeflateBuffer.length; offset += CHUNK_SIZE) {
  75. const end = Math.min(offset + CHUNK_SIZE, rawDeflateBuffer.length);
  76. const chunk = rawDeflateBuffer.subarray(offset, end);
  77. rawInf.inflate(chunk);
  78. }
  79. const ret = rawInf.getBuffer();
  80. rawInf.destroy();
  81. return ret;
  82. },
  83. deflate(rawInflateBuffer) {
  84. const rawDef = new RawDef();
  85. for (let offset = 0; offset < rawInflateBuffer.length; offset += CHUNK_SIZE) {
  86. const end = Math.min(offset + CHUNK_SIZE, rawInflateBuffer.length);
  87. const chunk = rawInflateBuffer.subarray(offset, end);
  88. rawDef.deflate(chunk, rawInflateBuffer.length <= offset + CHUNK_SIZE);
  89. }
  90. const ret = rawDef.getBuffer();
  91. rawDef.destroy();
  92. return ret;
  93. },
  94. };
  95. return ret;
  96. }
  97. this.zlib = initialize();
  98. }).call(this);