1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/xpcshell/node-spdy/lib/spdy/utils.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,115 @@ 1.4 +var spdy = require('../spdy'), 1.5 + utils = exports; 1.6 + 1.7 +var zlib = require('zlib'), 1.8 + Buffer = require('buffer').Buffer; 1.9 + 1.10 +// 1.11 +// ### function createDeflate () 1.12 +// Creates deflate stream with SPDY dictionary 1.13 +// 1.14 +utils.createDeflate = function createDeflate(version) { 1.15 + var deflate = zlib.createDeflate({ 1.16 + dictionary: spdy.protocol[version].dictionary, 1.17 + windowBits: 11 1.18 + }); 1.19 + 1.20 + // Define lock information early 1.21 + deflate.locked = false; 1.22 + deflate.lockBuffer = []; 1.23 + 1.24 + return deflate; 1.25 +}; 1.26 + 1.27 +// 1.28 +// ### function createInflate () 1.29 +// Creates inflate stream with SPDY dictionary 1.30 +// 1.31 +utils.createInflate = function createInflate(version) { 1.32 + var inflate = zlib.createInflate({ 1.33 + dictionary: spdy.protocol[version].dictionary, 1.34 + windowBits: 15 1.35 + }); 1.36 + 1.37 + // Define lock information early 1.38 + inflate.locked = false; 1.39 + inflate.lockBuffer = []; 1.40 + 1.41 + return inflate; 1.42 +}; 1.43 + 1.44 +// 1.45 +// ### function resetZlibStream (stream) 1.46 +// #### @stream {zlib.Stream} stream 1.47 +// Resets zlib stream and associated locks 1.48 +// 1.49 +utils.resetZlibStream = function resetZlibStream(stream, callback) { 1.50 + if (stream.locked) { 1.51 + stream.lockBuffer.push(function() { 1.52 + resetZlibStream(stream, callback); 1.53 + }); 1.54 + return; 1.55 + } 1.56 + 1.57 + stream.reset(); 1.58 + stream.lockBuffer = []; 1.59 + 1.60 + callback(null); 1.61 +}; 1.62 + 1.63 +var delta = 0; 1.64 +// 1.65 +// ### function zstream (stream, buffer, callback) 1.66 +// #### @stream {Deflate|Inflate} One of streams above 1.67 +// #### @buffer {Buffer} Input data (to compress or to decompress) 1.68 +// #### @callback {Function} Continuation to callback 1.69 +// Compress/decompress data and pass it to callback 1.70 +// 1.71 +utils.zstream = function zstream(stream, buffer, callback) { 1.72 + var flush = stream._flush, 1.73 + chunks = [], 1.74 + total = 0; 1.75 + 1.76 + if (stream.locked) { 1.77 + stream.lockBuffer.push(function() { 1.78 + zstream(stream, buffer, callback); 1.79 + }); 1.80 + return; 1.81 + } 1.82 + stream.locked = true; 1.83 + 1.84 + function collect(chunk) { 1.85 + chunks.push(chunk); 1.86 + total += chunk.length; 1.87 + } 1.88 + stream.on('data', collect); 1.89 + stream.write(buffer); 1.90 + 1.91 + stream.once('error', function(err) { 1.92 + stream.removeAllListeners('data'); 1.93 + callback(err); 1.94 + }); 1.95 + 1.96 + stream.flush(function() { 1.97 + stream.removeAllListeners('data'); 1.98 + stream.removeAllListeners('error'); 1.99 + stream._flush = flush; 1.100 + 1.101 + callback(null, chunks, total); 1.102 + 1.103 + stream.locked = false; 1.104 + var deferred = stream.lockBuffer.shift(); 1.105 + if (deferred) deferred(); 1.106 + }); 1.107 +}; 1.108 + 1.109 +// 1.110 +// ### function zwrap (stream) 1.111 +// #### @stream {zlib.Stream} stream to wrap 1.112 +// Wraps stream within function to allow simple deflate/inflate 1.113 +// 1.114 +utils.zwrap = function zwrap(stream) { 1.115 + return function(data, callback) { 1.116 + utils.zstream(stream, data, callback); 1.117 + }; 1.118 +};