Skip to content

Commit

Permalink
feature/relp (#8)
Browse files Browse the repository at this point in the history
* fix npmrc

* stop calling unref and export it as a function

* v2.3.3

* fix test for node8

* (wip) format RELP output messages, include open and close commands

* emit RSP events

* minor refactor and add command to rsp attributes

* resend test

* add RELP instructions to sender readme

* v2.4.0

* fix resend method return value

* chore(package): update version and js-helper dependencie

* update CHANGELOG and lockfile

* tentative patch bump and changelog message for chore/update-version

* fix write after end test, now it runs both in 12.x and 14.x

* strict condition

* emit after everything else

* Update lib/sender.js

Co-authored-by: tufosa <[email protected]>

* rewrite if-else with ternaries

* use constant

* ternary, ternary, more ternary!

* Update lib/sender.js

Co-authored-by: tufosa <[email protected]>

* Update docs/sender.md

Co-authored-by: tufosa <[email protected]>

* one-liners

* Update docs/sender.md

Co-authored-by: tufosa <[email protected]>

* Update test/sender.js

Co-authored-by: tufosa <[email protected]>

* Update test/sender.js

Co-authored-by: tufosa <[email protected]>

* Update test/sender.js

Co-authored-by: tufosa <[email protected]>

* Update test/sender.js

Co-authored-by: tufosa <[email protected]>

* Update test/sender.js

Co-authored-by: tufosa <[email protected]>

* strict equality

* be consistent at send/resend return value when using RELP

* Update test/sender.js

Co-authored-by: tufosa <[email protected]>

* update linter forcing semicolons, huge fix

---------

Co-authored-by: Alberto Bastos <[email protected]>
Co-authored-by: José Ramón <[email protected]>
Co-authored-by: Diego Lafuente <[email protected]>
Co-authored-by: tufosa <[email protected]>
  • Loading branch information
5 people authored Oct 18, 2024
1 parent 0c16a08 commit 931d6ed
Show file tree
Hide file tree
Showing 17 changed files with 665 additions and 429 deletions.
37 changes: 24 additions & 13 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
{
"env": {
"node": true,
"mocha": true,
"es6": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaVersion": 2018,
"ecmaFeatures": {
"globalReturn": true,
"modules": true
}
},
"env": {
"es6": true,
"node": true,
"mocha": true
},
"rules": {
"quotes": ["error", "single"],
"no-extra-semi": "error",
"no-console": "off",
"no-trailing-spaces": "error",
"indent": ["error", 2],
"max-len": ["error", {"code": 80}],
"no-unused-vars": ["error", {"args": "none"}]
"semi": ["error", "always"],
"no-inner-declarations": 0,
"no-unused-vars": ["error", {
"args": "after-used",
"argsIgnorePattern": "^_"
}],
"no-console": 0,
"indent": ["error", 2, {
"SwitchCase": 1
}],
"max-len": ["error", {
"code": 80,
"tabWidth": 2
}],
"linebreak-style": ["error", "unix"],
"no-constant-condition": ["error", {
"checkLoops": false
}]
}
}
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
strict-ssl=true
strict-ssl=false
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

Change log.

## Version 3.0.0

* Added support for RELP protocol.

## Version 2.3.5

* Bump `js-helper` dependency to `2.0.0`

## Version 2.3.4

* Upgraded the `js-helper` version to the latest
Expand Down
20 changes: 10 additions & 10 deletions bin/cli.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
'use strict'
'use strict';

const commandLineArgs = require('command-line-args');
const getUsage = require('command-line-usage');
Expand Down Expand Up @@ -94,7 +94,7 @@ const SECTIONS = [{
optionList: OPTION_LIST
}];

runClient()
runClient();


function runClient() {
Expand All @@ -104,20 +104,20 @@ function runClient() {
if (options.help) {
return console.log(getUsage(SECTIONS));
}
let credentials = null
let credentials = null;
// with no credentials options use ~/.devo.json
if (options.credentials) {
credentials = require(options.credentials)
console.log('cred %j', credentials)
credentials = require(options.credentials);
console.log('cred %j', credentials);
} else if ((options.key && options.secret) || options.token) {
credentials = {
url: options.url,
apiKey: options.key,
apiSecret: options.secret,
apiToken: options.token,
}
};
}
const client = clientLib.create(credentials)
const client = clientLib.create(credentials);
const queryOptions = {
dateFrom: options.from,
dateTo: options.to,
Expand All @@ -126,11 +126,11 @@ function runClient() {
format: options.format,
skip: options.skip,
limit: options.limit,
}
};
client.query(queryOptions, (error, result) => {
if (error ) return console.error('Could not run query: %s', error);
console.log(result)
})
console.log(result);
});
}

/**
Expand Down
24 changes: 12 additions & 12 deletions bin/sender.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
'use strict'
'use strict';

const fs = require('fs');
const commandLineArgs = require('command-line-args');
Expand Down Expand Up @@ -71,7 +71,7 @@ const SECTIONS = [{
hide: 'insecure',
}];

runSender()
runSender();


function runSender() {
Expand All @@ -82,19 +82,19 @@ function runSender() {
return console.log(getUsage(SECTIONS));
}
if (!options.host || !options.port) {
console.error('Host and port required')
console.log(getUsage(SECTIONS))
console.error('Host and port required');
console.log(getUsage(SECTIONS));
}
const params = {...options}
if (options.cert) params.cert = fs.readFileSync(options.cert)
if (options.key) params.key = fs.readFileSync(options.key)
if (options.ca) params.ca = fs.readFileSync(options.ca)
const sender = senderLib.create(params)
let input = process.stdin
const params = {...options};
if (options.cert) params.cert = fs.readFileSync(options.cert);
if (options.key) params.key = fs.readFileSync(options.key);
if (options.ca) params.ca = fs.readFileSync(options.ca);
const sender = senderLib.create(params);
let input = process.stdin;
if (options.file) {
input = fs.createReadStream(options.file)
input = fs.createReadStream(options.file);
}
input.pipe(sender)
input.pipe(sender);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions docs/sender.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ sender.send('something happened')
sender.send({message: 'something happened', priority: 'high'})
```

Sender supports the [unref](https://nodejs.org/api/net.html#net_socket_unref)
method. See the official Nodejs documentation linked above for more details.
The sender creates a TCP socket underneath, and an application using the SDK
will wait until these sockets are freed before ending. This means that an
application using the SDK will not end until all sockets are properly closed.
Expand All @@ -42,6 +44,24 @@ See detailed info on
[sending events](#sending-events)
and [command line uploads](#command-line-uploads).

## RELP Protocol

The sender supports RELP protocol emitting events for each RSP command received
and enables the resend and close commands:

```js
const sender = devo.sender({host, port, relp: true})
const txno = sender.send('a RELP message') // txno === 2
sender.resend('a RELP message', txno) // reuse txno
sender.sendClose() // sends close command
sender.on('rsp', (rsp) => {
rsp; // { txno: 1, command: 'open', body: '200 OK' }
// { txno: 2, command: 'syslog', body: '200 OK' }
// { txno: 2, command: 'syslog', body: '200 OK' }
// { txno: 3, command: 'close', body: '200 OK' }
})
```

## Sender Credentials

You need to be a customer to send events to Devo.
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
module.exports = {
client: require('./lib/client.js').create,
sender: require('./lib/sender.js').create,
}
};

34 changes: 17 additions & 17 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const config = require('./config.js');
const httpRequest = require('./httpRequest.js');
const queryStream = require('./queryStream.js');

const STREAMING_FORMAT = 'json/simple/compact'
const STREAMING_FORMAT = 'json/simple/compact';


/**
Expand All @@ -28,11 +28,11 @@ class Client {
* @param {function(*)} callback Function to invoke with (error, result).
*/
query(options, callback) {
const opc = this._config.parseQuery(options)
const opc = this._config.parseQuery(options);
if (!helper.config.validate(opc)) {
return callback('Invalid options')
return callback('Invalid options');
}
httpRequest.create().post(opc, callback)
httpRequest.create().post(opc, callback);
}

/**
Expand All @@ -44,17 +44,17 @@ class Client {
stream(options) {
const opc = this._config.parseQuery(options, STREAMING_FORMAT);
if (!helper.config.validate(opc)) {
throw new Error('Invalid options')
throw new Error('Invalid options');
}
const stream = queryStream.create(this._config, options);
httpRequest.create().getResponse(opc, (error, response) => {
if (error) {
stream.emit('error', 'Invalid response: ' + error)
return
stream.emit('error', 'Invalid response: ' + error);
return;
}
response.pipe(stream)
})
return stream
response.pipe(stream);
});
return stream;
}

/**
Expand All @@ -63,7 +63,7 @@ class Client {
* @param {function(*)=} callback Function to invoke with (error, info).
*/
getTasks(callback) {
const opc = this._config.parseGet(helper.taskPaths.getTasks())
const opc = this._config.parseGet(helper.taskPaths.getTasks());
httpRequest.create().get(opc, callback);
}

Expand All @@ -74,7 +74,7 @@ class Client {
* @param {function(*)=} callback Function to invoke with (error, info).
*/
getTasksByType(type, callback) {
const opc = this._config.parseGet(helper.taskPaths.getTasksByType(type))
const opc = this._config.parseGet(helper.taskPaths.getTasksByType(type));
httpRequest.create().get(opc, callback);
}

Expand All @@ -85,7 +85,7 @@ class Client {
* @param {function(*)=} callback Function to invoke with (error, info).
*/
getTaskInfo(taskId, callback) {
const opc = this._config.parseGet(helper.taskPaths.getTaskInfo(taskId))
const opc = this._config.parseGet(helper.taskPaths.getTaskInfo(taskId));
httpRequest.create().get(opc, callback);
}

Expand All @@ -96,7 +96,7 @@ class Client {
* @param {function(*)=} callback Function to invoke with (error, info).
*/
startTask(taskId, callback) {
const opc = this._config.parseGet(helper.taskPaths.startTask(taskId))
const opc = this._config.parseGet(helper.taskPaths.startTask(taskId));
httpRequest.create().get(opc, callback);
}

Expand All @@ -107,7 +107,7 @@ class Client {
* @param {function(*)=} callback Function to invoke with (error, info).
*/
stopTask(taskId, callback) {
const opc = this._config.parseGet(helper.taskPaths.stopTask(taskId))
const opc = this._config.parseGet(helper.taskPaths.stopTask(taskId));
httpRequest.create().get(opc, callback);
}

Expand All @@ -118,10 +118,10 @@ class Client {
* @param {function(*)=} callback Function to invoke with (error, info).
*/
deleteTask(taskId, callback) {
const opc = this._config.parseGet(helper.taskPaths.deleteTask(taskId))
const opc = this._config.parseGet(helper.taskPaths.deleteTask(taskId));
httpRequest.create().get(opc, callback);
}
}

exports.create = credentials => new Client(credentials)
exports.create = credentials => new Client(credentials);

34 changes: 17 additions & 17 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

const helper = require('@devo/js-helper');

const CONFIG_FILE = '.devo.json'
const CONFIG_FILE = '.devo.json';


module.exports = {
read,
}
};

/**
* Use the provided credentials.
Expand All @@ -16,32 +16,32 @@ module.exports = {
* @return {Object} parsed configuration.
*/
function read(credentials) {
credentials = credentials || readDefault()
return helper.config.create(credentials)
credentials = credentials || readDefault();
return helper.config.create(credentials);
}

function readDefault() {
const env = {}
if (process.env.DEVO_KEY) env.apiKey = process.env.DEVO_KEY
if (process.env.DEVO_SECRET) env.apiSecret = process.env.DEVO_SECRET
if (process.env.DEVO_TOKEN) env.token = process.env.DEVO_TOKEN
if (process.env.DEVO_URL) env.url = process.env.DEVO_URL
const home = require('os').homedir()
const filename = home + '/' + CONFIG_FILE
const env = {};
if (process.env.DEVO_KEY) env.apiKey = process.env.DEVO_KEY;
if (process.env.DEVO_SECRET) env.apiSecret = process.env.DEVO_SECRET;
if (process.env.DEVO_TOKEN) env.token = process.env.DEVO_TOKEN;
if (process.env.DEVO_URL) env.url = process.env.DEVO_URL;
const home = require('os').homedir();
const filename = home + '/' + CONFIG_FILE;
try {
const read = require(filename)
const read = require(filename);
return {
...read,
...env,
}
};
} catch (exception) {
if (env.url && (env.apiKey || env.token)) {
return env
return env;
}
console.error('Could not read credentials from process env or from %s',
filename)
console.error('Please refer to README.md for details')
process.exit(1)
filename);
console.error('Please refer to README.md for details');
process.exit(1);
}
}

Loading

0 comments on commit 931d6ed

Please sign in to comment.