Skip to content

Commit

Permalink
updating module docs to be inline with current release
Browse files Browse the repository at this point in the history
  • Loading branch information
esatterwhite committed Jun 5, 2017
1 parent 3f270e0 commit 234be91
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 21 deletions.
6 changes: 6 additions & 0 deletions bin/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = new seeli.Command({
`${seeli.bold('Usage:')} skyring run -p 3000 -s localhost:5522 -s localhost:5523 --channel:port=5522`
, `${seeli.bold('Usage:')} skyring run -d -p 3001 -s localhost:5522 -s localhost:5523 --channel:port=5523`
, `${seeli.bold('Usage:')} skyring run --no-daemon -p 3000 -s localhost:5522 -s localhost:5523 --channel:port=6213`
, `${seeli.bold('Usage:')} skyring run --no-daemon -p 3000 -t @skyring/tcp-transport -t @skyring/zmq-transport`
]
, flags: {
seeds: {
Expand All @@ -44,6 +45,11 @@ module.exports = new seeli.Command({
, required: true
, description: 'Nodes in the ring to use as seed nodes'
}
, transport: {
type: [String, Array]
, shorthand: 't'
, description: 'Custom transports to load into the server at start up time'
}

, 'channel:host': {
type: String
Expand Down
2 changes: 1 addition & 1 deletion lib/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports.parse = function parse(json) {
try {
return { error: null, value: JSON.parse(json) };
} catch( e ) {
return {error: e, value: null};
return { error: e, value: null };
}
};

2 changes: 1 addition & 1 deletion lib/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const http = require('http')
* @param {String} [options.node.app=timers] name of the active ring to join
* @param {Object} [options.nats]
* @param {String[]} [options.nats.servers] An array of nats `host:port` addresses to connect to
* @param {String[]|Function[]} [optsion.transports] an array of custom transport functions, or requireable paths that resolve to functions. All transport function must be named functions
* @param {String[]|Function[]} [options.transports] an array of custom transport functions, or requireable paths that resolve to functions. All transport function must be named functions
* @example
// Use only configuration values
var server = new Server().load().listen(5000)
Expand Down
12 changes: 10 additions & 2 deletions lib/server/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* @requires stream
* @requires events
* @requires url
* @requires util
* @requires http
*/

Expand All @@ -19,7 +18,11 @@ const stream = require('stream')
;



/**
* A mock IncomingMessage Object for proxying requests via tchannel
* @class module:skyring/lib/server/mock.Request
* @extends EventEmitter
*/
exports.Request = class Request extends stream.Readable {
constructor ( options ){
super();
Expand Down Expand Up @@ -55,6 +58,11 @@ exports.Request = class Request extends stream.Readable {
}
};

/**
* A mock ServerResponse Object for proxying requests via tchannel
* @class module:skyring/lib/server/mock.Response
* @extends EventEmitter
*/
exports.Response = class Response extends EventEmitter {

constructor(callback){
Expand Down
17 changes: 12 additions & 5 deletions lib/server/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @module skyring/lib/server/node
* @author Eric Satterwhite
* @since 1.0.0
* @require path
* @requires events
* @requires dns
* @requires ringpop
Expand Down Expand Up @@ -84,7 +85,7 @@ if (err) throw err
, hostPort:`${host}:${this._port}`
, channel: this._tchannel.makeSubChannel({
serviceName: this._name
, trace:false
, trace: false
})
});
this._ring.setupChannel();
Expand Down Expand Up @@ -136,9 +137,9 @@ if (err) throw err
* Determines if this instance is responsible for a specific key.
* proxies the request if it is not
* @method module:skyring/lib/server/node#handleOrProxy
* @param {String} Key The key to use to do a node lookup in the ring
* @param {http.IncomingMessage} req an http request object
* @param {http.ServerResponse} res an http response object
* @param {String} key The key to use to do a node lookup in the ring
* @param {http.IncomingMessage|module:skyring/lib/server/mock.Request} req an http request object
* @param {http.ServerResponse|module:skyring/lib/server/mock.Response} res an http response object
* @example const handle = node.handleOrProxy('foobar', req, res)
if (!handle) return;
// deal with request
Expand Down Expand Up @@ -171,7 +172,7 @@ if (!handle) return;
/**
* Removes itself from the ring and closes and connections
* @method module:skyring/lib/server/node#close
* @param {Function} A callback function to call when the ring is closed
* @param {Function} callback A callback function to call when the ring is closed
**/
close( cb ) {
debug('node close');
Expand All @@ -190,6 +191,12 @@ if (!handle) return;

Object.defineProperty(Node.prototype, 'name', {
get: function() {
/**
* @readonly
* @name name
* @memberof module:skyring/lib/server/node
* @property {String} name The name of the node
**/
return this._app;
}
});
Expand Down
16 changes: 14 additions & 2 deletions lib/server/request.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
/*jshint laxcomma: true, smarttabs: true, node:true, esnext:true*/
'use strict';
/**
* Simple wrapper around the http request object to avoid deopts
* @module skyring/lib/server/request
* @author Eric Satterwhite
* @since 1.0.0
* @requires url
* @requires querystring
*/

const {Url} = require('url')
, cache = new WeakMap()
, qs = require('querystring')
, cache = new WeakMap()
, pathexp = /^(\/\/?(?!\/)[^\?#\s]*)(\?[^#\s]*)?$/
;

/**
* @constructor
* @alias skyring/lib/server/request
* @param {IncommingMessage} req An {@link https://nodejs.org/api/http.html#http_class_http_incomingmessage|IncomingMessage}
* from the node http module
*/
function Request( req ) {

const parsed = parseurl(req);
Expand All @@ -28,7 +36,11 @@ function Request( req ) {
}
this.headers = req.headers;
}

/**
* Returns the value of a header, if it exists
* @param {String} header The name of the header to lookup
* @returns {String} The request header, if set
*/
Request.prototype.get = function get( key ) {
const _key = key.toLowerCae();
const headers = this.req.headers || {};
Expand Down
57 changes: 57 additions & 0 deletions lib/server/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@
*/
const debug = require('debug')('skyring:error');

/**
* @constructor
* @alias module:skyring/lib/server/response
* @param {ServerResponse} res A {@link https://nodejs.org/api/http.html#http_class_http_serverresponse|ServerResponse}
* from the node http module
*/
function Response( res ) {
this.res = res;
this.body = null;
}

/**
* Responsible for returning a response in the case of an error
* If the error has a message, it will be sent with the `x-skyring-reason` http header
* if the error has a `statusCode` property, that will be used, otherwise a 500 will be returned
* @method module:skyring/lib/server/response#error
* @param {Error|number} err The error to handle
* @param {String} [msg] In the case `err` is a number, this will be used as the message
*/
Response.prototype.error = function error( err, msg ) {
if(typeof err === 'number') {
this.res.setHeader('x-skyring-reason', msg || 'Internal Server Error');
return this.status(err).json({
message: msg
});
Expand All @@ -31,16 +46,38 @@ Response.prototype.error = function error( err, msg ) {
return this.end();
};

/**
* Returns the value of a response header
* @method module:skyring/lib/server/response#get
* @param {String} header The name of the header to get
* @returns {String} The header value, if it is set
*/
Response.prototype.get = function get( key ) {
return this.res.getHeader(key);
};

/**
* Helper for responding with an Object. Will serialize the object, and set the
* Content-Type header to `application/json`
* @chainable
* @method module:skyring/lib/server/response#json
* @param {Object} body The object to set as the response body
* @returns {module:skyring/lib/server/response}
*/
Response.prototype.json = function json( body ) {
this.res.setHeader('Content-Type', 'application/json');
this.res.end(JSON.stringify(body));
return this;
};

/**
* Sets a response header
* @chainable
* @method module:skyring/lib/server/response#set
* @param {String} header The header to set
* @param {String} The header value to set
* @returns {module:skyring/lib/server/response}
*/
Response.prototype.set = function set( key, val ) {
const value = Array.isArray(val)
? val.map(String)
Expand All @@ -50,16 +87,36 @@ Response.prototype.set = function set( key, val ) {
return this;
};

/**
* Sets the status code on the response object
* @chainable
* @method module:skyring/lib/server/response#status
* @param {Number} code The http Status code to set
* @returns {module:skyring/lib/server/response}
*/
Response.prototype.status = function status( code ) {
this.res.statusCode = code;
return this;
};

/**
* Writes a chunk to the response stream
* @chainable
* @method module:skyring/lib/server/response#send
* @param {String} [chunk] The chunk to write
* @returns {module:skyring/lib/server/response}
*/
Response.prototype.send = function send( str ) {
this.res.write( str );
return this;
};

/**
* Ends the response
* @method module:skyring/lib/server/response#end
* @param {String} [chunk] An optional chunk to write be for closing the stream
* @returns {module:skyring/lib/server/response}
*/
Response.prototype.end = function end( str ) {
this.res.end(str);
return this;
Expand Down
1 change: 0 additions & 1 deletion lib/server/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
const pathToRegExp = require('path-to-regexp');

/**
*
* @constructor
* @alias module:skyring/lib/server/route
* @param {String} path the url pattern to match
Expand Down
3 changes: 2 additions & 1 deletion lib/server/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const Route = require('./route')
* @constructor
* @alias module:skyring/lib/server/router
* @param {module:skyring/lib/server/node} node The node linked to the application hashring to pass with each request
* @param {module:skyring/lib/timers} timers A timer instance associated with the application hashring to pass with each request
* @param {module:skyring/lib/timer} timer A timer instance associated with the application hashring to pass with each request
* @example var x = new Router(node, timers)
router.handle(req, res)
*/
Expand Down Expand Up @@ -89,6 +89,7 @@ Router.prototype.options = function options( path, fn ) {
* @param {String} path The url path to route on
* @param {String} m handlerethod The http method to associate to the route
* @param {Function} The handler function to call when the route is matched
* @returns {module:skyring/lib/server/route}
**/
Router.prototype.route = function route( path, method, fn ) {
const _method = method.toUpperCase();
Expand Down
71 changes: 63 additions & 8 deletions lib/transports/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,79 @@
/*jshint laxcomma: true, smarttabs: true, node:true, esnext:true*/
'use strict';
/**
* Available tranports
* Loads and maintains all transports
* @module skyring/lib/transports
* @author Eric Satterwhite
* @tutorial transports
* @since 1.0.0
* @requires debug
* @requires skyring/lib/transports/http
* @requires skyring/conf
*/

const debug = require('debug')('skyring:transports')
, conf = require('../../conf')
, callback = require('./callback')
, http = require('./http')
, kLoad = Symbol('kLoad')
const debug = require('debug')('skyring:transports')
, conf = require('../../conf')
, callback = require('./callback')
, http = require('./http')
, kLoad = Symbol('kLoad')
, kShutdown = Symbol.for('kShutdown')
, ENV = conf.get('NODE_ENV')
, defaults = toArray(conf.get('transport'))
, ENV = conf.get('NODE_ENV')
, defaults = toArray(conf.get('transport'))
;

/**
*
* @typedef {function} TransportHandler
* @param {String} method
* @param {String} uri
* @param {String} Payload
* @param {String} id
* @param {LevelUp} storage A levelup instance container all curring timer data
**/

/**
 * @alias module:skyring/lib/transports
* @constructor
 * @param {TransportHandler|TransportHandler[]|String|String[]} transports Custome transports to registe
* @example const path = require('path')
const Skyring = require('skyring')
function fizzbuz(method, uri, payload, id, timer_store) {
// send payload to uri...
timer_store.remove(id)
}
fuzzbuz.shutdown(cb) {
// drain connections...
// free up event loop
cb()
}
const server = new Skyring({
transports: [
'my-transport-module'
, fizzbuz
, path.resolve(__dirname, '../transports/fake-transport')
]
})
* @example const Transports = require('skyring/lib/transports')
function fizzbuz(method, uri, payload, id, timer_store) {
// send payload to uri...
timer_store.remove(id)
}
fuzzbuz.shutdown(cb) {
// drain connections...
// free up event loop
cb()
}
const t = new Transports([
'my-transport-module'
, fizzbuz
, path.resolve(__dirname, '../transports/fake-transport')
])
 **/
module.exports = class Transports {
constructor(transports) {
/**
Expand Down

0 comments on commit 234be91

Please sign in to comment.