caesar
Version:
An easy-to-use advanced cryptography library.
127 lines (103 loc) • 3.58 kB
JavaScript
// Generated by CoffeeScript 1.7.1
(function() {
var stream,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
stream = require('stream');
exports.EncodeByLine = (function(_super) {
__extends(EncodeByLine, _super);
function EncodeByLine(opts) {
if (!this instanceof exports.EncodeByLine) {
return new exports.EncodeByLine(opts);
}
stream.Transform.call(this, opts);
}
EncodeByLine.prototype._transform = function(chunk, encoding, done) {
this.push(chunk + "\n");
return done();
};
return EncodeByLine;
})(stream.Transform);
exports.DecodeByLine = (function(_super) {
__extends(DecodeByLine, _super);
function DecodeByLine(opts) {
if (!this instanceof exports.DecodeByLine) {
return new exports.DecodeByLine(opts);
}
stream.Transform.call(this, opts);
this._lastLine = "";
}
DecodeByLine.prototype._transform = function(chunk, encoding, done) {
var data, line, lines, _i, _len;
data = this._lastLine + chunk.toString();
lines = data.split('\n');
this._lastLine = lines.splice(lines.length - 1, 1)[0];
for (_i = 0, _len = lines.length; _i < _len; _i++) {
line = lines[_i];
this.push(line);
}
return done();
};
DecodeByLine.prototype._flush = function(done) {
if (this._lastLine.length !== 0) {
this.push(this._lastLine);
}
this._lastLine = "";
return done();
};
return DecodeByLine;
})(stream.Transform);
exports.EncodeByLength = (function(_super) {
__extends(EncodeByLength, _super);
function EncodeByLength(opts) {
if (!this instanceof exports.EncodeByLength) {
return new exports.EncodeByLength(opts);
}
stream.Transform.call(this, opts);
}
EncodeByLength.prototype._transform = function(chunk, encoding, done) {
var buff;
buff = new Buffer(chunk.length + 2);
buff.writeUInt16BE(chunk.length, 0);
if (chunk instanceof Buffer) {
chunk.copy(buff, 2);
} else {
buff.write(chunk, 2, chunk.length, encoding);
}
this.push(buff);
return done();
};
return EncodeByLength;
})(stream.Transform);
exports.DecodeByLength = (function(_super) {
__extends(DecodeByLength, _super);
function DecodeByLength(opts) {
if (!this instanceof exports.DecodeByLength) {
return new exports.DecodeByLength(opts);
}
stream.Transform.call(this, opts);
this._buffer = new Buffer('');
}
DecodeByLength.prototype._transform = function(chunk, encoding, done) {
var going, len, temp;
if (!Buffer.isBuffer(chunk)) {
chunk = new Buffer(chunk, enc);
}
this._buffer = Buffer.concat([this._buffer, chunk]);
going = true;
while (going && this._buffer.length > 2) {
len = this._buffer.readUInt16BE(0);
if (this._buffer.length >= (len + 2)) {
temp = new Buffer(len);
this._buffer.copy(temp, 0, 2, len + 2);
this._buffer = this._buffer.slice(len + 2);
this.push(temp);
} else {
going = false;
}
}
return done();
};
return DecodeByLength;
})(stream.Transform);
}).call(this);