binding.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. var msg = require('pako/lib/zlib/messages');
  2. var zstream = require('pako/lib/zlib/zstream');
  3. var zlib_deflate = require('pako/lib/zlib/deflate.js');
  4. var zlib_inflate = require('pako/lib/zlib/inflate.js');
  5. var constants = require('pako/lib/zlib/constants');
  6. for (var key in constants) {
  7. exports[key] = constants[key];
  8. }
  9. // zlib modes
  10. exports.NONE = 0;
  11. exports.DEFLATE = 1;
  12. exports.INFLATE = 2;
  13. exports.GZIP = 3;
  14. exports.GUNZIP = 4;
  15. exports.DEFLATERAW = 5;
  16. exports.INFLATERAW = 6;
  17. exports.UNZIP = 7;
  18. /**
  19. * Emulate Node's zlib C++ layer for use by the JS layer in index.js
  20. */
  21. function Zlib(mode) {
  22. if (mode < exports.DEFLATE || mode > exports.UNZIP)
  23. throw new TypeError("Bad argument");
  24. this.mode = mode;
  25. this.init_done = false;
  26. this.write_in_progress = false;
  27. this.pending_close = false;
  28. this.windowBits = 0;
  29. this.level = 0;
  30. this.memLevel = 0;
  31. this.strategy = 0;
  32. this.dictionary = null;
  33. }
  34. Zlib.prototype.init = function(windowBits, level, memLevel, strategy, dictionary) {
  35. this.windowBits = windowBits;
  36. this.level = level;
  37. this.memLevel = memLevel;
  38. this.strategy = strategy;
  39. // dictionary not supported.
  40. if (this.mode === exports.GZIP || this.mode === exports.GUNZIP)
  41. this.windowBits += 16;
  42. if (this.mode === exports.UNZIP)
  43. this.windowBits += 32;
  44. if (this.mode === exports.DEFLATERAW || this.mode === exports.INFLATERAW)
  45. this.windowBits = -this.windowBits;
  46. this.strm = new zstream();
  47. switch (this.mode) {
  48. case exports.DEFLATE:
  49. case exports.GZIP:
  50. case exports.DEFLATERAW:
  51. var status = zlib_deflate.deflateInit2(
  52. this.strm,
  53. this.level,
  54. exports.Z_DEFLATED,
  55. this.windowBits,
  56. this.memLevel,
  57. this.strategy
  58. );
  59. break;
  60. case exports.INFLATE:
  61. case exports.GUNZIP:
  62. case exports.INFLATERAW:
  63. case exports.UNZIP:
  64. var status = zlib_inflate.inflateInit2(
  65. this.strm,
  66. this.windowBits
  67. );
  68. break;
  69. default:
  70. throw new Error("Unknown mode " + this.mode);
  71. }
  72. if (status !== exports.Z_OK) {
  73. this._error(status);
  74. return;
  75. }
  76. this.write_in_progress = false;
  77. this.init_done = true;
  78. };
  79. Zlib.prototype.params = function() {
  80. throw new Error("deflateParams Not supported");
  81. };
  82. Zlib.prototype._writeCheck = function() {
  83. if (!this.init_done)
  84. throw new Error("write before init");
  85. if (this.mode === exports.NONE)
  86. throw new Error("already finalized");
  87. if (this.write_in_progress)
  88. throw new Error("write already in progress");
  89. if (this.pending_close)
  90. throw new Error("close is pending");
  91. };
  92. Zlib.prototype.write = function(flush, input, in_off, in_len, out, out_off, out_len) {
  93. this._writeCheck();
  94. this.write_in_progress = true;
  95. var self = this;
  96. process.nextTick(function() {
  97. self.write_in_progress = false;
  98. var res = self._write(flush, input, in_off, in_len, out, out_off, out_len);
  99. self.callback(res[0], res[1]);
  100. if (self.pending_close)
  101. self.close();
  102. });
  103. return this;
  104. };
  105. // set method for Node buffers, used by pako
  106. function bufferSet(data, offset) {
  107. for (var i = 0; i < data.length; i++) {
  108. this[offset + i] = data[i];
  109. }
  110. }
  111. Zlib.prototype.writeSync = function(flush, input, in_off, in_len, out, out_off, out_len) {
  112. this._writeCheck();
  113. return this._write(flush, input, in_off, in_len, out, out_off, out_len);
  114. };
  115. Zlib.prototype._write = function(flush, input, in_off, in_len, out, out_off, out_len) {
  116. this.write_in_progress = true;
  117. if (flush !== exports.Z_NO_FLUSH &&
  118. flush !== exports.Z_PARTIAL_FLUSH &&
  119. flush !== exports.Z_SYNC_FLUSH &&
  120. flush !== exports.Z_FULL_FLUSH &&
  121. flush !== exports.Z_FINISH &&
  122. flush !== exports.Z_BLOCK) {
  123. throw new Error("Invalid flush value");
  124. }
  125. if (input == null) {
  126. input = new Buffer(0);
  127. in_len = 0;
  128. in_off = 0;
  129. }
  130. if (out._set)
  131. out.set = out._set;
  132. else
  133. out.set = bufferSet;
  134. var strm = this.strm;
  135. strm.avail_in = in_len;
  136. strm.input = input;
  137. strm.next_in = in_off;
  138. strm.avail_out = out_len;
  139. strm.output = out;
  140. strm.next_out = out_off;
  141. switch (this.mode) {
  142. case exports.DEFLATE:
  143. case exports.GZIP:
  144. case exports.DEFLATERAW:
  145. var status = zlib_deflate.deflate(strm, flush);
  146. break;
  147. case exports.UNZIP:
  148. case exports.INFLATE:
  149. case exports.GUNZIP:
  150. case exports.INFLATERAW:
  151. var status = zlib_inflate.inflate(strm, flush);
  152. break;
  153. default:
  154. throw new Error("Unknown mode " + this.mode);
  155. }
  156. if (status !== exports.Z_STREAM_END && status !== exports.Z_OK) {
  157. this._error(status);
  158. }
  159. this.write_in_progress = false;
  160. return [strm.avail_in, strm.avail_out];
  161. };
  162. Zlib.prototype.close = function() {
  163. if (this.write_in_progress) {
  164. this.pending_close = true;
  165. return;
  166. }
  167. this.pending_close = false;
  168. if (this.mode === exports.DEFLATE || this.mode === exports.GZIP || this.mode === exports.DEFLATERAW) {
  169. zlib_deflate.deflateEnd(this.strm);
  170. } else {
  171. zlib_inflate.inflateEnd(this.strm);
  172. }
  173. this.mode = exports.NONE;
  174. };
  175. Zlib.prototype.reset = function() {
  176. switch (this.mode) {
  177. case exports.DEFLATE:
  178. case exports.DEFLATERAW:
  179. var status = zlib_deflate.deflateReset(this.strm);
  180. break;
  181. case exports.INFLATE:
  182. case exports.INFLATERAW:
  183. var status = zlib_inflate.inflateReset(this.strm);
  184. break;
  185. }
  186. if (status !== exports.Z_OK) {
  187. this._error(status);
  188. }
  189. };
  190. Zlib.prototype._error = function(status) {
  191. this.onerror(msg[status] + ': ' + this.strm.msg, status);
  192. this.write_in_progress = false;
  193. if (this.pending_close)
  194. this.close();
  195. };
  196. exports.Zlib = Zlib;