Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | var spdy = require('../spdy'), |
michael@0 | 2 | utils = exports; |
michael@0 | 3 | |
michael@0 | 4 | var zlib = require('zlib'), |
michael@0 | 5 | Buffer = require('buffer').Buffer; |
michael@0 | 6 | |
michael@0 | 7 | // |
michael@0 | 8 | // ### function createDeflate () |
michael@0 | 9 | // Creates deflate stream with SPDY dictionary |
michael@0 | 10 | // |
michael@0 | 11 | utils.createDeflate = function createDeflate(version) { |
michael@0 | 12 | var deflate = zlib.createDeflate({ |
michael@0 | 13 | dictionary: spdy.protocol[version].dictionary, |
michael@0 | 14 | windowBits: 11 |
michael@0 | 15 | }); |
michael@0 | 16 | |
michael@0 | 17 | // Define lock information early |
michael@0 | 18 | deflate.locked = false; |
michael@0 | 19 | deflate.lockBuffer = []; |
michael@0 | 20 | |
michael@0 | 21 | return deflate; |
michael@0 | 22 | }; |
michael@0 | 23 | |
michael@0 | 24 | // |
michael@0 | 25 | // ### function createInflate () |
michael@0 | 26 | // Creates inflate stream with SPDY dictionary |
michael@0 | 27 | // |
michael@0 | 28 | utils.createInflate = function createInflate(version) { |
michael@0 | 29 | var inflate = zlib.createInflate({ |
michael@0 | 30 | dictionary: spdy.protocol[version].dictionary, |
michael@0 | 31 | windowBits: 15 |
michael@0 | 32 | }); |
michael@0 | 33 | |
michael@0 | 34 | // Define lock information early |
michael@0 | 35 | inflate.locked = false; |
michael@0 | 36 | inflate.lockBuffer = []; |
michael@0 | 37 | |
michael@0 | 38 | return inflate; |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | // |
michael@0 | 42 | // ### function resetZlibStream (stream) |
michael@0 | 43 | // #### @stream {zlib.Stream} stream |
michael@0 | 44 | // Resets zlib stream and associated locks |
michael@0 | 45 | // |
michael@0 | 46 | utils.resetZlibStream = function resetZlibStream(stream, callback) { |
michael@0 | 47 | if (stream.locked) { |
michael@0 | 48 | stream.lockBuffer.push(function() { |
michael@0 | 49 | resetZlibStream(stream, callback); |
michael@0 | 50 | }); |
michael@0 | 51 | return; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | stream.reset(); |
michael@0 | 55 | stream.lockBuffer = []; |
michael@0 | 56 | |
michael@0 | 57 | callback(null); |
michael@0 | 58 | }; |
michael@0 | 59 | |
michael@0 | 60 | var delta = 0; |
michael@0 | 61 | // |
michael@0 | 62 | // ### function zstream (stream, buffer, callback) |
michael@0 | 63 | // #### @stream {Deflate|Inflate} One of streams above |
michael@0 | 64 | // #### @buffer {Buffer} Input data (to compress or to decompress) |
michael@0 | 65 | // #### @callback {Function} Continuation to callback |
michael@0 | 66 | // Compress/decompress data and pass it to callback |
michael@0 | 67 | // |
michael@0 | 68 | utils.zstream = function zstream(stream, buffer, callback) { |
michael@0 | 69 | var flush = stream._flush, |
michael@0 | 70 | chunks = [], |
michael@0 | 71 | total = 0; |
michael@0 | 72 | |
michael@0 | 73 | if (stream.locked) { |
michael@0 | 74 | stream.lockBuffer.push(function() { |
michael@0 | 75 | zstream(stream, buffer, callback); |
michael@0 | 76 | }); |
michael@0 | 77 | return; |
michael@0 | 78 | } |
michael@0 | 79 | stream.locked = true; |
michael@0 | 80 | |
michael@0 | 81 | function collect(chunk) { |
michael@0 | 82 | chunks.push(chunk); |
michael@0 | 83 | total += chunk.length; |
michael@0 | 84 | } |
michael@0 | 85 | stream.on('data', collect); |
michael@0 | 86 | stream.write(buffer); |
michael@0 | 87 | |
michael@0 | 88 | stream.once('error', function(err) { |
michael@0 | 89 | stream.removeAllListeners('data'); |
michael@0 | 90 | callback(err); |
michael@0 | 91 | }); |
michael@0 | 92 | |
michael@0 | 93 | stream.flush(function() { |
michael@0 | 94 | stream.removeAllListeners('data'); |
michael@0 | 95 | stream.removeAllListeners('error'); |
michael@0 | 96 | stream._flush = flush; |
michael@0 | 97 | |
michael@0 | 98 | callback(null, chunks, total); |
michael@0 | 99 | |
michael@0 | 100 | stream.locked = false; |
michael@0 | 101 | var deferred = stream.lockBuffer.shift(); |
michael@0 | 102 | if (deferred) deferred(); |
michael@0 | 103 | }); |
michael@0 | 104 | }; |
michael@0 | 105 | |
michael@0 | 106 | // |
michael@0 | 107 | // ### function zwrap (stream) |
michael@0 | 108 | // #### @stream {zlib.Stream} stream to wrap |
michael@0 | 109 | // Wraps stream within function to allow simple deflate/inflate |
michael@0 | 110 | // |
michael@0 | 111 | utils.zwrap = function zwrap(stream) { |
michael@0 | 112 | return function(data, callback) { |
michael@0 | 113 | utils.zstream(stream, data, callback); |
michael@0 | 114 | }; |
michael@0 | 115 | }; |