Skip to content

Commit

Permalink
Merge pull request #9 from ashpool/editorconfig
Browse files Browse the repository at this point in the history
Editorconfig
  • Loading branch information
ashpool committed Apr 15, 2016
2 parents cbd2dc1 + cbd4fa0 commit 5348395
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 195 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# http://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 80
trim_trailing_whitespace = true

[*.md]
max_line_length = 0
trim_trailing_whitespace = false

[COMMIT_EDITMSG]
max_line_length = 0
12 changes: 9 additions & 3 deletions .jscsrc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
"<",
"<="
],
"disallowSpaceBeforeBinaryOperators": [","],
"disallowSpaceBeforeBinaryOperators": [
","
],
"disallowSpaceAfterPrefixUnaryOperators": [
"++",
"--",
Expand Down Expand Up @@ -97,10 +99,14 @@
"<",
"<="
],
"disallowKeywords": ["with"],
"disallowKeywords": [
"with"
],
"disallowMultipleLineBreaks": true,
"validateLineBreaks": "LF",
"disallowMixedSpacesAndTabs": true,
"disallowTrailingComma": true,
"disallowKeywordsOnNewLine": ["else"]
"disallowKeywordsOnNewLine": [
"else"
]
}
36 changes: 18 additions & 18 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
var gulp = require('gulp'),
jshint = require('gulp-jshint'),
mocha = require('gulp-mocha'),
cover = require('gulp-coverage'),
jscs = require('gulp-jscs');
jshint = require('gulp-jshint'),
mocha = require('gulp-mocha'),
cover = require('gulp-coverage'),
jscs = require('gulp-jscs');

gulp.task('default', ['jscs', 'lint', 'test'], function () {
});

gulp.task('lint', function () {
return gulp.src(['./lib/*.js', './test/*.js'])
.pipe(jshint())
.pipe(jshint.reporter('default', {verbose: true}));
return gulp.src(['./lib/*.js', './test/*.js'])
.pipe(jshint())
.pipe(jshint.reporter('default', {verbose: true}));
});

gulp.task('test', ['jscs', 'lint'], function () {
return gulp.src('./test', {read: false})
.pipe(cover.instrument({
pattern: ['*lib/*.js'],
debugDirectory: 'debug'
}))
.pipe(mocha({reporter: 'nyan'}))
.pipe(cover.gather())
.pipe(cover.format())
.pipe(gulp.dest('reports'));
return gulp.src('./test', {read: false})
.pipe(cover.instrument({
pattern: ['*lib/*.js'],
debugDirectory: 'debug'
}))
.pipe(mocha({reporter: 'nyan'}))
.pipe(cover.gather())
.pipe(cover.format())
.pipe(gulp.dest('reports'));
});

gulp.task('jscs', function () {
return gulp.src(['./lib/*.js', './test/*.js'])
.pipe(jscs());
return gulp.src(['./lib/*.js', './test/*.js'])
.pipe(jscs());
});
128 changes: 64 additions & 64 deletions lib/carbon.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
var net = require('net'),
url = require('url');
url = require('url');

function CarbonClient (properties) {
if (typeof(properties) === 'string') {
properties = {url: properties};
}
properties = properties || {};
this._url = properties.url;
this._hostedGraphiteKey = '';
if (properties.hostedGraphiteKey) {
this._hostedGraphiteKey = properties.hostedGraphiteKey + '.';
}
this._socket = properties.socket || null;
function CarbonClient(properties) {
if (typeof(properties) === 'string') {
properties = {url: properties};
}
properties = properties || {};
this._url = properties.url;
this._hostedGraphiteKey = '';
if (properties.hostedGraphiteKey) {
this._hostedGraphiteKey = properties.hostedGraphiteKey + '.';
}
this._socket = properties.socket || null;
}

CarbonClient.prototype.write = function (metrics, timestamp) {
var self = this;
return new Promise(function (resolve, reject) {
self._connect().then(function (socket) {
var lines = '';
for (var path in metrics) {
if (metrics.hasOwnProperty(path)) {
var value = metrics[path];
lines += [path, value, timestamp].join(' ') + '\n';
}
}
socket.write(self._hostedGraphiteKey + lines, 'utf-8', function (err) {
if (err) {
reject(err);
} else {
resolve(lines);
}
});
}, function (error) {
reject(error);
});
var self = this;
return new Promise(function (resolve, reject) {
self._connect().then(function (socket) {
var lines = '';
for (var path in metrics) {
if (metrics.hasOwnProperty(path)) {
var value = metrics[path];
lines += [path, value, timestamp].join(' ') + '\n';
}
}
socket.write(self._hostedGraphiteKey + lines, 'utf-8', function (err) {
if (err) {
reject(err);
} else {
resolve(lines);
}
});
}, function (error) {
reject(error);
});
});
};

CarbonClient.prototype._connect = function () {
var self = this;
return new Promise(function (resolve, reject) {
if (self._socket) {
return resolve(self._socket);
}
var dsn = url.parse(self._url),
port = parseInt(dsn.port, 10) || 2003,
host = dsn.hostname,
timeout = 1000,
socket = new net.Socket();
socket.setTimeout(timeout, function () {
socket.destroy();
reject(new Error('Socket timeout'));
});
socket.on('error', function (err) {
socket.destroy();
reject(err);
});
self._socket = socket.connect(port, host, 1000, function (err) {
if (err) {
reject(err);
} else {
resolve(self._socket);
}
});
var self = this;
return new Promise(function (resolve, reject) {
if (self._socket) {
return resolve(self._socket);
}
var dsn = url.parse(self._url),
port = parseInt(dsn.port, 10) || 2003,
host = dsn.hostname,
timeout = 1000,
socket = new net.Socket();
socket.setTimeout(timeout, function () {
socket.destroy();
reject(new Error('Socket timeout'));
});
socket.on('error', function (err) {
socket.destroy();
reject(err);
});
self._socket = socket.connect(port, host, 1000, function (err) {
if (err) {
reject(err);
} else {
resolve(self._socket);
}
});
});
};

CarbonClient.prototype.end = function () {
var self = this;
return new Promise(function (resolve) {
if (self._socket) {
self._socket.end();
}
resolve();
});
var self = this;
return new Promise(function (resolve) {
if (self._socket) {
self._socket.end();
}
resolve();
});
};

module.exports = CarbonClient;
20 changes: 10 additions & 10 deletions lib/graphite.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var Client = require('./carbon'),
metric = require('./metric');
metric = require('./metric');

function GraphiteClient (properties) {
this._carbon = properties.carbon;
function GraphiteClient(properties) {
this._carbon = properties.carbon;
}

GraphiteClient.createClient = function (config) {
return new this({
carbon: new Client(config)
});
return new this({
carbon: new Client(config)
});
};

/**
Expand All @@ -19,16 +19,16 @@ GraphiteClient.createClient = function (config) {
* @returns {Promise} a promise
*/
GraphiteClient.prototype.write = function (metrics, timestamp) {
timestamp = timestamp || Date.now();
timestamp = Math.floor(timestamp / 1000);
return this._carbon.write(metric.flatten(metrics), timestamp);
timestamp = timestamp || Date.now();
timestamp = Math.floor(timestamp / 1000);
return this._carbon.write(metric.flatten(metrics), timestamp);
};

/**
* Ends the connection
*/
GraphiteClient.prototype.end = function () {
return this._carbon.end();
return this._carbon.end();
};

module.exports = GraphiteClient;
26 changes: 13 additions & 13 deletions lib/metric.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module.exports = {
flatten: function (obj, flat, prefix) {
flat = flat || {};
prefix = prefix || '';
flatten: function (obj, flat, prefix) {
flat = flat || {};
prefix = prefix || '';

for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var value = obj[key];
if (typeof value === 'object') {
this.flatten(value, flat, prefix + key + '.');
} else {
flat[prefix + key] = value;
}
}
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var value = obj[key];
if (typeof value === 'object') {
this.flatten(value, flat, prefix + key + '.');
} else {
flat[prefix + key] = value;
}
return flat;
}
}
return flat;
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphite-promise",
"version": "0.1.2",
"version": "0.1.3",
"description": "A node.js module to interface with Graphite promise style",
"keywords": [
"graphite"
Expand Down
Loading

0 comments on commit 5348395

Please sign in to comment.