34dc915c3fdd5fd6aa48422e6b4d6098bf65c95339d7562978db59ddd9f145b389ba516b349a6ce751bb230fdd6e2ac26d2943bbfd2e60137446e4aa66f40a 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict'
  2. var zlibpool = exports
  3. var zlib = require('zlib')
  4. var transport = require('../../../spdy-transport')
  5. // TODO(indutny): think about it, why has it always been Z_SYNC_FLUSH here.
  6. // It should be possible to manually flush stuff after the write instead
  7. function createDeflate (version, compression) {
  8. var deflate = zlib.createDeflate({
  9. dictionary: transport.protocol.spdy.dictionary[version],
  10. flush: zlib.Z_SYNC_FLUSH,
  11. windowBits: 11,
  12. level: compression ? zlib.Z_DEFAULT_COMPRESSION : zlib.Z_NO_COMPRESSION
  13. })
  14. // For node.js v0.8
  15. deflate._flush = zlib.Z_SYNC_FLUSH
  16. return deflate
  17. }
  18. function createInflate (version) {
  19. var inflate = zlib.createInflate({
  20. dictionary: transport.protocol.spdy.dictionary[version],
  21. flush: zlib.Z_SYNC_FLUSH
  22. })
  23. // For node.js v0.8
  24. inflate._flush = zlib.Z_SYNC_FLUSH
  25. return inflate
  26. }
  27. function Pool (compression) {
  28. this.compression = compression
  29. this.pool = {
  30. 2: [],
  31. 3: [],
  32. 3.1: []
  33. }
  34. }
  35. zlibpool.create = function create (compression) {
  36. return new Pool(compression)
  37. }
  38. Pool.prototype.get = function get (version) {
  39. if (this.pool[version].length > 0) {
  40. return this.pool[version].pop()
  41. } else {
  42. var id = version
  43. return {
  44. version: version,
  45. compress: createDeflate(id, this.compression),
  46. decompress: createInflate(id)
  47. }
  48. }
  49. }
  50. Pool.prototype.put = function put (pair) {
  51. this.pool[pair.version].push(pair)
  52. }