Initial commit
This commit is contained in:
32
src/common/bufferSegment.js
Normal file
32
src/common/bufferSegment.js
Normal file
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Create a buffer segment
|
||||
* @param {Buffer} buf
|
||||
* @param {number} [offset]
|
||||
* @param {number} [count]
|
||||
* @constructor
|
||||
*/
|
||||
function BufferSegment(buf, offset, count) {
|
||||
if (!Buffer.isBuffer(buf)) throw new TypeError('buf must be a buffer');
|
||||
|
||||
this.buffer = buf;
|
||||
this.offset = offset || 0;
|
||||
this.count = count || buf.length;
|
||||
}
|
||||
|
||||
BufferSegment.prototype.toString = function() {
|
||||
return this.buffer.toString('utf8', this.offset, this.offset + this.count);
|
||||
};
|
||||
|
||||
BufferSegment.prototype.toBuffer = function() {
|
||||
if (this.offset === 0 && this.count === this.buffer.length)
|
||||
return this.buffer;
|
||||
return this.buffer.slice(this.offset, this.offset + this.count);
|
||||
};
|
||||
|
||||
BufferSegment.prototype.copyTo = function(dst, offset) {
|
||||
this.buffer.copy(dst, offset, this.offset, this.offset + this.count);
|
||||
};
|
||||
|
||||
module.exports = function(buf, offset, count) {
|
||||
return new BufferSegment(buf, offset, count);
|
||||
};
|
41
src/common/hash.js
Normal file
41
src/common/hash.js
Normal file
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @constructor
|
||||
* @property {number} length
|
||||
*/
|
||||
function Hash() {
|
||||
this._ = {};
|
||||
this._length = 0;
|
||||
}
|
||||
Object.defineProperty(Hash.prototype, 'length', {
|
||||
get: function() {
|
||||
return this._length;
|
||||
}
|
||||
});
|
||||
|
||||
Hash.prototype.add = function(key,value) {
|
||||
this._[key] = value;
|
||||
this._length++;
|
||||
};
|
||||
|
||||
Hash.prototype.clear = function() {
|
||||
this._ = {};
|
||||
this._length = 0;
|
||||
};
|
||||
|
||||
Hash.prototype.forEach = function(cb) {
|
||||
for(var k in this._) {
|
||||
cb(k, this._[k]);
|
||||
}
|
||||
};
|
||||
|
||||
Hash.prototype.get = function(key) {
|
||||
return this._[key];
|
||||
};
|
||||
|
||||
Hash.prototype.remove = function(key) {
|
||||
delete this._[key];
|
||||
this._length--;
|
||||
};
|
||||
|
||||
|
||||
module.exports = Hash;
|
7
src/common/log/noopLogger.js
Normal file
7
src/common/log/noopLogger.js
Normal file
@ -0,0 +1,7 @@
|
||||
function NoopLogger() {
|
||||
}
|
||||
NoopLogger.prototype.error = function() {};
|
||||
NoopLogger.prototype.debug = function() {};
|
||||
NoopLogger.prototype.info = function() {};
|
||||
|
||||
module.exports = NoopLogger;
|
5
src/common/systemEventTypes.js
Normal file
5
src/common/systemEventTypes.js
Normal file
@ -0,0 +1,5 @@
|
||||
const SystemEventTypes = {
|
||||
StreamMetadata: '$metadata'
|
||||
};
|
||||
|
||||
module.exports = SystemEventTypes;
|
6
src/common/systemStreams.js
Normal file
6
src/common/systemStreams.js
Normal file
@ -0,0 +1,6 @@
|
||||
module.exports.metastreamOf = function(stream) {
|
||||
return '$$' + stream;
|
||||
};
|
||||
module.exports.isMetastream = function(stream) {
|
||||
return stream.indexOf('$$') === 0;
|
||||
};
|
6
src/common/utils/ensure.js
Normal file
6
src/common/utils/ensure.js
Normal file
@ -0,0 +1,6 @@
|
||||
module.exports.notNullOrEmpty = function(value, name) {
|
||||
if (value === null)
|
||||
throw new Error(name + " is null.");
|
||||
if (value === '')
|
||||
throw new Error(name + " is empty.");
|
||||
};
|
Reference in New Issue
Block a user