binding.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import msg from './messages';
  2. import zstream from './zstream';
  3. import {deflateInit2, deflateEnd, deflateReset, deflate} from './deflate';
  4. import {inflateInit2, inflate, inflateEnd, inflateReset} from './inflate';
  5. // import constants from './constants';
  6. // zlib modes
  7. export var NONE = 0;
  8. export var DEFLATE = 1;
  9. export var INFLATE = 2;
  10. export var GZIP = 3;
  11. export var GUNZIP = 4;
  12. export var DEFLATERAW = 5;
  13. export var INFLATERAW = 6;
  14. export var UNZIP = 7;
  15. export var Z_NO_FLUSH= 0,
  16. Z_PARTIAL_FLUSH= 1,
  17. Z_SYNC_FLUSH= 2,
  18. Z_FULL_FLUSH= 3,
  19. Z_FINISH= 4,
  20. Z_BLOCK= 5,
  21. Z_TREES= 6,
  22. /* Return codes for the compression/decompression functions. Negative values
  23. * are errors, positive values are used for special but normal events.
  24. */
  25. Z_OK= 0,
  26. Z_STREAM_END= 1,
  27. Z_NEED_DICT= 2,
  28. Z_ERRNO= -1,
  29. Z_STREAM_ERROR= -2,
  30. Z_DATA_ERROR= -3,
  31. //Z_MEM_ERROR: -4,
  32. Z_BUF_ERROR= -5,
  33. //Z_VERSION_ERROR: -6,
  34. /* compression levels */
  35. Z_NO_COMPRESSION= 0,
  36. Z_BEST_SPEED= 1,
  37. Z_BEST_COMPRESSION= 9,
  38. Z_DEFAULT_COMPRESSION= -1,
  39. Z_FILTERED= 1,
  40. Z_HUFFMAN_ONLY= 2,
  41. Z_RLE= 3,
  42. Z_FIXED= 4,
  43. Z_DEFAULT_STRATEGY= 0,
  44. /* Possible values of the data_type field (though see inflate()) */
  45. Z_BINARY= 0,
  46. Z_TEXT= 1,
  47. //Z_ASCII: 1, // = Z_TEXT (deprecated)
  48. Z_UNKNOWN= 2,
  49. /* The deflate compression method */
  50. Z_DEFLATED= 8;
  51. export function Zlib(mode) {
  52. if (mode < DEFLATE || mode > UNZIP)
  53. throw new TypeError('Bad argument');
  54. this.mode = mode;
  55. this.init_done = false;
  56. this.write_in_progress = false;
  57. this.pending_close = false;
  58. this.windowBits = 0;
  59. this.level = 0;
  60. this.memLevel = 0;
  61. this.strategy = 0;
  62. this.dictionary = null;
  63. }
  64. Zlib.prototype.init = function(windowBits, level, memLevel, strategy, dictionary) {
  65. this.windowBits = windowBits;
  66. this.level = level;
  67. this.memLevel = memLevel;
  68. this.strategy = strategy;
  69. // dictionary not supported.
  70. if (this.mode === GZIP || this.mode === GUNZIP)
  71. this.windowBits += 16;
  72. if (this.mode === UNZIP)
  73. this.windowBits += 32;
  74. if (this.mode === DEFLATERAW || this.mode === INFLATERAW)
  75. this.windowBits = -this.windowBits;
  76. this.strm = new zstream();
  77. var status;
  78. switch (this.mode) {
  79. case DEFLATE:
  80. case GZIP:
  81. case DEFLATERAW:
  82. status = deflateInit2(
  83. this.strm,
  84. this.level,
  85. Z_DEFLATED,
  86. this.windowBits,
  87. this.memLevel,
  88. this.strategy
  89. );
  90. break;
  91. case INFLATE:
  92. case GUNZIP:
  93. case INFLATERAW:
  94. case UNZIP:
  95. status = inflateInit2(
  96. this.strm,
  97. this.windowBits
  98. );
  99. break;
  100. default:
  101. throw new Error('Unknown mode ' + this.mode);
  102. }
  103. if (status !== Z_OK) {
  104. this._error(status);
  105. return;
  106. }
  107. this.write_in_progress = false;
  108. this.init_done = true;
  109. };
  110. Zlib.prototype.params = function() {
  111. throw new Error('deflateParams Not supported');
  112. };
  113. Zlib.prototype._writeCheck = function() {
  114. if (!this.init_done)
  115. throw new Error('write before init');
  116. if (this.mode === NONE)
  117. throw new Error('already finalized');
  118. if (this.write_in_progress)
  119. throw new Error('write already in progress');
  120. if (this.pending_close)
  121. throw new Error('close is pending');
  122. };
  123. Zlib.prototype.write = function(flush, input, in_off, in_len, out, out_off, out_len) {
  124. this._writeCheck();
  125. this.write_in_progress = true;
  126. var self = this;
  127. process.nextTick(function() {
  128. self.write_in_progress = false;
  129. var res = self._write(flush, input, in_off, in_len, out, out_off, out_len);
  130. self.callback(res[0], res[1]);
  131. if (self.pending_close)
  132. self.close();
  133. });
  134. return this;
  135. };
  136. // set method for Node buffers, used by pako
  137. function bufferSet(data, offset) {
  138. for (var i = 0; i < data.length; i++) {
  139. this[offset + i] = data[i];
  140. }
  141. }
  142. Zlib.prototype.writeSync = function(flush, input, in_off, in_len, out, out_off, out_len) {
  143. this._writeCheck();
  144. return this._write(flush, input, in_off, in_len, out, out_off, out_len);
  145. };
  146. Zlib.prototype._write = function(flush, input, in_off, in_len, out, out_off, out_len) {
  147. this.write_in_progress = true;
  148. if (flush !== Z_NO_FLUSH &&
  149. flush !== Z_PARTIAL_FLUSH &&
  150. flush !== Z_SYNC_FLUSH &&
  151. flush !== Z_FULL_FLUSH &&
  152. flush !== Z_FINISH &&
  153. flush !== Z_BLOCK) {
  154. throw new Error('Invalid flush value');
  155. }
  156. if (input == null) {
  157. input = new Buffer(0);
  158. in_len = 0;
  159. in_off = 0;
  160. }
  161. if (out._set)
  162. out.set = out._set;
  163. else
  164. out.set = bufferSet;
  165. var strm = this.strm;
  166. strm.avail_in = in_len;
  167. strm.input = input;
  168. strm.next_in = in_off;
  169. strm.avail_out = out_len;
  170. strm.output = out;
  171. strm.next_out = out_off;
  172. var status;
  173. switch (this.mode) {
  174. case DEFLATE:
  175. case GZIP:
  176. case DEFLATERAW:
  177. status = deflate(strm, flush);
  178. break;
  179. case UNZIP:
  180. case INFLATE:
  181. case GUNZIP:
  182. case INFLATERAW:
  183. status = inflate(strm, flush);
  184. break;
  185. default:
  186. throw new Error('Unknown mode ' + this.mode);
  187. }
  188. if (status !== Z_STREAM_END && status !== Z_OK) {
  189. this._error(status);
  190. }
  191. this.write_in_progress = false;
  192. return [strm.avail_in, strm.avail_out];
  193. };
  194. Zlib.prototype.close = function() {
  195. if (this.write_in_progress) {
  196. this.pending_close = true;
  197. return;
  198. }
  199. this.pending_close = false;
  200. if (this.mode === DEFLATE || this.mode === GZIP || this.mode === DEFLATERAW) {
  201. deflateEnd(this.strm);
  202. } else {
  203. inflateEnd(this.strm);
  204. }
  205. this.mode = NONE;
  206. };
  207. var status
  208. Zlib.prototype.reset = function() {
  209. switch (this.mode) {
  210. case DEFLATE:
  211. case DEFLATERAW:
  212. status = deflateReset(this.strm);
  213. break;
  214. case INFLATE:
  215. case INFLATERAW:
  216. status = inflateReset(this.strm);
  217. break;
  218. }
  219. if (status !== Z_OK) {
  220. this._error(status);
  221. }
  222. };
  223. Zlib.prototype._error = function(status) {
  224. this.onerror(msg[status] + ': ' + this.strm.msg, status);
  225. this.write_in_progress = false;
  226. if (this.pending_close)
  227. this.close();
  228. };