15 Commits

Author SHA1 Message Date
James Coglan dca0a77b0d Bump version to 0.1.3. 2015-04-10 20:46:44 +01:00
James Coglan 6e314bde56 Stash the scripts we built for #4 in the examples directory. 2015-04-10 20:46:21 +01:00
James Coglan 47b3b3614b It might be dangerous to permanently set _flushFlag, so we need to reimplement more of flush() to get the patch in place. 2015-04-09 18:25:22 +01:00
James Coglan 5da6a768b0 Monkey-patch deflate on Node 0.10 to produce smaller output; see https://github.com/faye/permessage-deflate-node/issues/4 2015-04-09 18:07:24 +01:00
James Coglan f03aef1954 Pass Z_SYNC_FLUSH to deflate.flush() where supported, to ensure optimal output. 2015-04-09 08:37:45 +01:00
James Coglan adebc51630 I forgot to replace SYNC_FLUSH with FULL_FLUSH in the last commit. 2015-04-09 00:06:47 +01:00
James Coglan 958de927aa Prevent a race condition causing malformed compression of frames; see https://github.com/faye/permessage-deflate-node/issues/4. 2015-04-08 23:59:51 +01:00
James Coglan 037afc350c Bump copyright year. 2015-02-19 09:30:58 +00:00
James Coglan 10b5d6be57 Test on Node 0.12 and io.js. 2015-02-19 09:25:00 +00:00
James Coglan f48eaac6d3 Bump version to 0.1.2. 2014-12-18 02:06:19 +00:00
James Coglan a71a06180a Throw an error if configure() is called with unrecognised options. 2014-12-17 22:06:13 +00:00
James Coglan e7619d9091 Bump version to 0.1.1. 2014-12-15 21:25:48 +00:00
James Coglan cc2978bdfb Fix the flush() mock so it works if not given a callback. 2014-12-15 19:20:56 +00:00
James Coglan 983c0d30c3 Fix the race conditions that happen when multiple messages attempt to use the same deflate/inflate stream at the same time, and fix the mechanism for flushing deflate streams so as not to hose the compression ratio on Node v0.10. 2014-12-15 18:59:30 +00:00
James Coglan 72955f0b1e Fix some typos in the readme. 2014-12-14 12:54:13 +00:00
10 changed files with 6014 additions and 55 deletions
+1
View File
@@ -2,5 +2,6 @@
.gitignore
.npmignore
.travis.yml
examples
node_modules
spec
+2 -1
View File
@@ -4,7 +4,8 @@ node_js:
- "0.6"
- "0.8"
- "0.10"
- "0.11"
- "0.12"
- "iojs"
before_install:
- '[ "${TRAVIS_NODE_VERSION}" = "0.6" ] && npm conf set strict-ssl false || true'
+19
View File
@@ -0,0 +1,19 @@
### 0.1.3 / 2015-04-10
* Fix a race condition causing some fragments of deflate output to be dropped
* Make sure to emit minimal output on all Node versions
### 0.1.2 / 2014-12-18
* Don't allow configure() to be called with unrecognized options
### 0.1.1 / 2014-12-15
* Fix race condition when using context takeover, where adjacent messages have
data listeners bound at the same time and end up duplicating output
* Use `DeflateRaw.flush()` correctly on v0.10 so that optimal compression is
achieved
### 0.1.0 / 2014-12-13
* Initial release
+11 -12
View File
@@ -41,12 +41,12 @@ exts.add(deflate);
The set of available options can be split into two sets: those that control the
session's compressor for outgoing messages and do not need to be communicated to
the peer, and those that are negoatiated as part of the protocol. The settings
the peer, and those that are negotiated as part of the protocol. The settings
only affecting the compressor are described fully in the [zlib
documentation](http://nodejs.org/api/zlib.html#zlib_options):
* `level`: sets the compression level, can be an integer from `0` to `9`, or one
of the contants `zlib.Z_NO_COMPRESSION`, `zlib.Z_BEST_SPEED`,
of the constants `zlib.Z_NO_COMPRESSION`, `zlib.Z_BEST_SPEED`,
`zlib.Z_BEST_COMPRESSION`, or `zlib.Z_DEFAULT_COMPRESSION`
* `memLevel`: sets how much memory the compressor allocates, can be an integer
from `1` to `9`, or one of the constants `zlib.Z_MIN_MEMLEVEL`,
@@ -72,22 +72,21 @@ can be used to set the local session's behaviour and control that of the peer:
(The MIT License)
Copyright (c) 2014 James Coglan
Copyright (c) 2014-2015 James Coglan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the 'Software'), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+5809
View File
File diff suppressed because it is too large Load Diff
+52
View File
@@ -0,0 +1,52 @@
var fs = require('fs'),
deflate = require('..');
var records = fs.readFileSync(__dirname + '/bad.out.log', 'utf8')
.replace(/\s*$/g, '')
.split(/\n/)
.map(JSON.parse);
var client = deflate.createClientSession(),
offer = client.generateOffer(),
server = deflate.createServerSession([offer]),
response = server.generateResponse(),
compressed = [],
size = [0, 0];
client.activate(response);
function compress(index) {
var record = records[index];
if (!record) {
console.log(size, size[0] / size[1]);
return decompress(0);
}
var message = {data: new Buffer(record[3], 'base64')};
size[0] += message.data.length;
server.processOutgoingMessage(message, function(error, message) {
compressed[index] = message;
size[1] += message.data.length;
compress(index + 1);
});
}
function decompress(index) {
var record = records[index];
if (!record) return;
var payload = record[3],
message = compressed[index];
client.processIncomingMessage(message, function(error, message) {
var output = message.data.toString('base64');
if (output !== payload) {
console.error('Failed on', record, message);
process.exit(1);
}
decompress(index + 1);
});
}
compress(0);
+7
View File
@@ -29,6 +29,13 @@ var common = {
return _default;
},
validateOptions: function(options, validKeys) {
for (var key in options) {
if (validKeys.indexOf(key) < 0)
throw new Error('Unrecognized option: ' + key);
}
},
validParams: function(params) {
var keys = Object.keys(params), i = keys.length;
while (i--) {
+13 -1
View File
@@ -1,10 +1,22 @@
'use strict';
var ClientSession = require('./client_session'),
ServerSession = require('./server_session');
ServerSession = require('./server_session'),
common = require('./common');
var VALID_OPTIONS = [
'level',
'memLevel',
'strategy',
'noContextTakeover',
'maxWindowBits',
'requestNoContextTakeover',
'requestMaxWindowBits'
];
var PermessageDeflate = {
configure: function(options) {
common.validateOptions(options, VALID_OPTIONS);
var opts = this._options || {};
for (var key in opts) options[key] = opts[key];
return Object.create(this, {_options: {value: options}});
+99 -40
View File
@@ -12,34 +12,105 @@ var Session = function(options) {
this._acceptMaxWindowBits = common.fetch(options, 'maxWindowBits', undefined);
this._requestNoContextTakeover = common.fetch(options, 'requestNoContextTakeover', false);
this._requestMaxWindowBits = common.fetch(options, 'requestMaxWindowBits', undefined);
this._queueIn = [];
this._queueOut = [];
};
Session.prototype.processIncomingMessage = function(message, callback) {
if (!message.rsv1) return callback(null, message);
if (this._lockIn) return this._queueIn.push([message, callback]);
var inflate = this._getInflate(),
chunks = [message.data, new Buffer([0x00, 0x00, 0xff, 0xff])],
chunks = [],
length = 0,
self = this;
this._processMessage(inflate, chunks, function(error, data) {
if (this._inflate) this._lockIn = true;
var return_ = function(error, message) {
return_ = function() {};
inflate.removeListener('data', onData);
inflate.removeListener('error', onError);
if (!self._inflate && inflate.close) inflate.close();
if (error) return callback(error, null);
message.data = data;
callback(null, message);
self._lockIn = false;
var next = self._queueIn.shift();
if (next) self.processIncomingMessage.apply(self, next);
callback(error, message);
};
var onData = function(data) {
chunks.push(data);
length += data.length;
};
var onError = function(error) {
return_(error, null);
};
inflate.on('data', onData);
inflate.on('error', onError);
inflate.write(message.data);
inflate.write(new Buffer([0x00, 0x00, 0xff, 0xff]));
inflate.flush(function() {
message.data = common.concat(chunks, length);
return_(null, message);
});
};
Session.prototype.processOutgoingMessage = function(message, callback) {
if (this._lockOut) return this._queueOut.push([message, callback]);
var deflate = this._getDeflate(),
chunks = [],
length = 0,
self = this;
this._processMessage(deflate, [message.data], function(error, data) {
if (this._deflate) this._lockOut = true;
var return_ = function(error, message) {
return_ = function() {};
deflate.removeListener('data', onData);
deflate.removeListener('error', onError);
if (!self._deflate && deflate.close) deflate.close();
if (error) return callback(error, null);
message.rsv1 = true;
self._lockOut = false;
var next = self._queueOut.shift();
if (next) self.processOutgoingMessage.apply(self, next);
callback(error, message);
};
var onData = function(data) {
chunks.push(data);
length += data.length;
};
var onError = function(error) {
return_(error, null);
};
deflate.on('data', onData);
deflate.on('error', onError);
deflate.write(message.data);
var onFlush = function() {
var data = common.concat(chunks, length);
message.data = data.slice(0, data.length - 4);
callback(null, message);
});
message.rsv1 = true;
return_(null, message);
};
if (deflate.params !== undefined)
deflate.flush(zlib.Z_SYNC_FLUSH, onFlush);
else
deflate.flush(onFlush);
};
Session.prototype.close = function() {
@@ -66,38 +137,26 @@ Session.prototype._getDeflate = function() {
memLevel: this._memLevel,
strategy: this._strategy
});
var flush = deflate.flush;
// This monkey-patch is needed to make Node 0.10 produce optimal output.
// Without this it uses Z_FULL_FLUSH and effectively drops all its context
// state on every flush.
if (deflate._flushFlag !== undefined && deflate.params === undefined)
deflate.flush = function(callback) {
var ws = this._writableState;
if (ws.ended || ws.ending || ws.needDrain) {
flush.call(this, callback);
} else {
this._flushFlag = zlib.Z_SYNC_FLUSH;
this.write(new Buffer(0), '', callback);
}
};
if (this._ownContextTakeover) this._deflate = deflate;
return deflate;
};
Session.prototype._processMessage = function(codec, data, callback) {
var chunks = [],
length = 0;
var return_ = function(error, result) {
return_ = function() {};
codec.removeListener('data', onData);
codec.removeListener('error', onError);
codec = null;
callback(error, result);
};
var onData = function(chunk) {
chunks.push(chunk);
length += chunk.length;
};
var onError = function(error) {
return_(error, null);
};
codec.on('data', onData);
codec.on('error', onError);
data.forEach(function(c) { codec.write(c) });
codec.flush(function() {
return_(null, common.concat(chunks, length));
});
};
module.exports = Session;
+1 -1
View File
@@ -5,7 +5,7 @@
, "keywords" : ["websocket", "compression", "deflate"]
, "license" : "MIT"
, "version" : "0.1.0"
, "version" : "0.1.3"
, "engines" : {"node": ">=0.6.0"}
, "main" : "./lib/permessage_deflate"
, "devDependencies" : {"jstest": ""}