cocalc/src / smc-project / node_modules / broadway / node_modules / winston / lib / winston / transports / http.js
50665 viewsvar util = require('util'),1winston = require('../../winston'),2request = require('request'),3Stream = require('stream').Stream;45//6// ### function Http (options)7// #### @options {Object} Options for this instance.8// Constructor function for the Http transport object responsible9// for persisting log messages and metadata to a terminal or TTY.10//11var Http = exports.Http = function (options) {12options = options || {};1314this.name = 'http';15this.ssl = !!options.ssl;16this.host = options.host || 'localhost';17this.port = options.port;18this.auth = options.auth;19this.path = options.path || '';2021if (!this.port) {22this.port = this.ssl ? 443 : 80;23}24};2526util.inherits(Http, winston.Transport);2728//29// Expose the name of this Transport on the prototype30//31Http.prototype.name = 'http';3233//34// ### function _request (options, callback)35// #### @callback {function} Continuation to respond to when complete.36// Make a request to a winstond server or any http server which can37// handle json-rpc.38//39Http.prototype._request = function (options, callback) {40var options = options || {},41auth = options.auth || this.auth,42path = options.path || this.path || '';4344delete options.auth;45delete options.path;4647options = { json: options };48options.method = 'POST';49options.url = 'http'50+ (this.ssl ? 's' : '')51+ '://'52+ (auth ? auth.username + ':' : '')53+ (auth ? auth.password + '@' : '')54+ this.host55+ ':'56+ this.port57+ '/'58+ path;5960return request(options, callback);61};6263//64// ### function log (level, msg, [meta], callback)65// #### @level {string} Level at which to log the message.66// #### @msg {string} Message to log67// #### @meta {Object} **Optional** Additional metadata to attach68// #### @callback {function} Continuation to respond to when complete.69// Core logging method exposed to Winston. Metadata is optional.70//71Http.prototype.log = function (level, msg, meta, callback) {72var self = this;7374if (typeof meta === 'function') {75callback = meta;76meta = {};77}7879var options = {80method: 'collect',81params: {82level: level,83message: msg,84meta: meta85}86};8788// hack89if (meta.auth) {90options.auth = meta.auth;91delete meta.auth;92}9394// hack95if (meta.path) {96options.path = meta.path;97delete meta.path;98}99100this._request(options, function (err, res, body) {101if (res && res.statusCode !== 200) {102err = new Error('HTTP Status Code: ' + res.statusCode);103}104105if (err) return callback(err);106107// TODO: emit 'logged' correctly,108// keep track of pending logs.109self.emit('logged');110111if (callback) callback(null, true);112});113};114115//116// ### function query (options, callback)117// #### @options {Object} Loggly-like query options for this instance.118// #### @callback {function} Continuation to respond to when complete.119// Query the transport. Options object is optional.120//121Http.prototype.query = function (options, callback) {122if (typeof options === 'function') {123callback = options;124options = {};125}126127var self = this,128options = this.normalizeQuery(options);129130options = {131method: 'query',132params: options133};134135this._request(options, function (err, res, body) {136if (res && res.statusCode !== 200) {137err = new Error('HTTP Status Code: ' + res.statusCode);138}139140if (err) return callback(err);141142if (typeof body === 'string') {143try {144body = JSON.parse(body);145} catch (e) {146return callback(e);147}148}149150callback(null, body);151});152};153154//155// ### function stream (options)156// #### @options {Object} Stream options for this instance.157// Returns a log stream for this transport. Options object is optional.158//159Http.prototype.stream = function (options) {160var self = this,161options = options || {},162stream = new Stream,163req,164buff;165166stream.destroy = function () {167req.destroy();168};169170options = {171method: 'stream',172params: options173};174175req = this._request(options);176buff = '';177178req.on('data', function (data) {179var data = (buff + data).split(/\n+/),180l = data.length - 1,181i = 0;182183for (; i < l; i++) {184try {185stream.emit('log', JSON.parse(data[i]));186} catch (e) {187stream.emit('error', e);188}189}190191buff = data[l];192});193194req.on('error', function (err) {195stream.emit('error', err);196});197198return stream;199};200201202