Skip to content

Commit

Permalink
allow transports to be configured and registred for use
Browse files Browse the repository at this point in the history
  • Loading branch information
esatterwhite committed May 24, 2017
1 parent 67ec426 commit e43917e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions conf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const conf = require('keef')

module.exports = conf.defaults({
seeds: ["127.0.0.1:3455", "127.0.0.1:3456"]
, transport: []
, storage: {
backend: "memdown"
, path: null
Expand Down
2 changes: 1 addition & 1 deletion lib/transports/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const STATUS_CODES = require('http').STATUS_CODES
* @param {String} id The id of the timer being executed
* @param {module:skyring/lib/timer} cache A timer cache instance to delete from after execution
**/
module.exports = function makeRequest( method, url, payload, id, cache) {
module.exports = function httpTransport( method, url, payload, id, cache ) {
const isJSON = typeof payload === 'object'
, _method = method.toLowerCase()
, options = {
Expand Down
39 changes: 37 additions & 2 deletions lib/transports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,51 @@
* @requires skyring/lib/transports/http
*/

const debug = require('debug')('skyring:transports:tcp')
const conf = require('../../conf');
const callback = require('./callback');
const ENV = process.env.NODE_ENV;
const ENV = conf.get('NODE_ENV');
const transports = new Set(toArray(conf.get('with-transport')))

/**
* Primary http transport
* @memberof module:skyring/lib/transports
* @property {Object} http The HTTP transport
* @property {Object} http The default HTTP transport
**/
exports.http = require('./http');

debug('to load', transports)
for (const path of transports) {
const transport = require(path)
if (typeof transport !== 'function') {
throw new TypeError('A Transport must export a function')
}

if(transport.length !== 5) {
throw new Error('Transports must accept five parameters')
}

if(typeof transport.name !== 'string' && transport.name.length <= 0) {
throw new TypeError('transports.name is required and must be a string')
}

debug('loading %s transport', transport.name)
Object.defineProperty(exports, transport.name, {
configrable: false
, enumerable: true
, get: () => {
return transport
}
})
}

if(ENV === 'development' || ENV === 'test') {
exports.callback = callback;
}

function toArray(item) {
if (!item) return []
if (Array.isArray(item)) return item
return typeof item === 'string' ? item.split(',') : [item]
}

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "hhttps://github.com/esatterwhite/skyring"
"url": "https://github.com/esatterwhite/skyring"
},
"engines": {
"node": ">=6.0.0"
Expand All @@ -32,7 +32,7 @@
},
"dependencies": {
"body": "^5.1.0",
"debug": "^2.2.0",
"debug": "^2.6.8",
"keef": "^2.0.0",
"level": "^1.6.0",
"memdown": "^1.2.4",
Expand Down

0 comments on commit e43917e

Please sign in to comment.