Skip to content

Commit

Permalink
WIP for issue #11.
Browse files Browse the repository at this point in the history
  • Loading branch information
fgm committed Nov 21, 2016
1 parent d90ea10 commit 5662bef
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 31 deletions.
87 changes: 63 additions & 24 deletions lib/ServerLogger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
Expand All @@ -8,11 +8,11 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _Logger2 = require('./Logger');
var _Logger2 = require("./Logger");

var _Logger3 = _interopRequireDefault(_Logger2);

var _util = require('util');
var _util = require("util");

var util = _interopRequireWildcard(_util);

Expand All @@ -26,13 +26,19 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var serverCatcher = function serverCatcher(err) {
//Error.captureStackTrace(err);
console.log('Caught uE, err:', err);
};

/**
* An extension of the base logger which accepts log input on a HTTP URL.
*
* Its main method is log(level, message, context).
*
* @see ServerLogger.log
*/

var ServerLogger = function (_Logger) {
_inherits(ServerLogger, _Logger);

Expand All @@ -55,13 +61,13 @@ var ServerLogger = function (_Logger) {
var _this = _possibleConstructorReturn(this, (ServerLogger.__proto__ || Object.getPrototypeOf(ServerLogger)).call(this, strategy));

var defaultParameters = {
servePath: '/logger'
servePath: "/logger"
};

// Loop on defaults, not arguments, to avoid injecting any random junk.
for (var key in defaultParameters) {
if (defaultParameters.hasOwnProperty(key)) {
_this[key] = typeof parameters[key] !== 'undefined' ? parameters[key] : defaultParameters[key];
_this[key] = typeof parameters[key] !== "undefined" ? parameters[key] : defaultParameters[key];
}
}

Expand All @@ -70,26 +76,59 @@ var ServerLogger = function (_Logger) {
}

/**
* Handle a log message from the client.
*
* @param {IncomingMessage} req
* The request.
* @param {ServerResponse} res
* The response.
* @param {function} next
* A callback, not used.
*
* @returns {void}
* Arm the report subscriber.
*/


_createClass(ServerLogger, [{
key: 'handleClientLogRequest',
key: "arm",
value: function arm() {
console.log('Armed serverCatcher');
process.on('uncaughtException', serverCatcher);
// this.tk.report.subscribe(this.reportSubscriber.bind(this));
}

/**
* Disarm the subscriber.
*
* In most cases, we do not want to disarm immediately: a stack trace being
* build may take several hundred milliseconds, and we would lose it.
*
* @param delay
*/

}, {
key: "disarm",
value: function disarm() {
var delay = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2000;

console.log('Request to disarm serverCatcher');
setTimeout(function () {
console.log('Disarmed serverCatcher');
process.removeListener('uncaughtException', serverCatcher);
}, delay);
}

/**
* Handle a log message from the client.
*
* @param {IncomingMessage} req
* The request.
* @param {ServerResponse} res
* The response.
* @param {function} next
* A callback, not used.
*
* @returns {void}
*/

}, {
key: "handleClientLogRequest",
value: function handleClientLogRequest(req, res, next) {
var _this2 = this;

var method = req.method.toUpperCase();
if (method !== 'POST') {
if (method !== "POST") {
// RFC2616: 405 means Method not allowed.
res.writeHead(405);
res.end();
Expand All @@ -100,9 +139,9 @@ var ServerLogger = function (_Logger) {
req.setMaxListeners(20);

var body = "";
req.setEncoding('utf-8');
req.setEncoding("utf-8");

req.on('data', function (chunk) {
req.on("data", function (chunk) {
body += chunk;
});

Expand All @@ -119,7 +158,7 @@ var ServerLogger = function (_Logger) {
result = "";
} catch (err) {
res.statusCode = 422;
result = 'Could not parse JSON message: ' + err.message + '.';
result = "Could not parse JSON message: " + err.message + ".";
}
res.end(result);
}, function (e) {
Expand All @@ -140,7 +179,7 @@ var ServerLogger = function (_Logger) {
*/

}, {
key: 'setupConnect',
key: "setupConnect",
value: function setupConnect(webapp, servePath) {
this.webapp = webapp;
if (this.webapp) {
Expand All @@ -152,7 +191,7 @@ var ServerLogger = function (_Logger) {
}
}
}], [{
key: 'stringifyMessage',
key: "stringifyMessage",
value: function stringifyMessage(doc) {
var rawMessage = doc.message;
var message = void 0;
Expand Down Expand Up @@ -185,10 +224,10 @@ var ServerLogger = function (_Logger) {
*/

}, {
key: 'objectifyContext',
key: "objectifyContext",
value: function objectifyContext(rawContext) {
var context = {};
if ((typeof rawContext === 'undefined' ? 'undefined' : _typeof(rawContext)) === "object") {
if ((typeof rawContext === "undefined" ? "undefined" : _typeof(rawContext)) === "object") {
// For some reason, JS null is an object: handle it like a scalar.
if (rawContext === null) {
context = { value: null };
Expand Down
44 changes: 37 additions & 7 deletions src/ServerLogger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import Logger from './Logger';
import * as util from 'util';
import Logger from "./Logger";
import * as util from "util";

const serverCatcher = function (err) {
//Error.captureStackTrace(err);
console.log('Caught uE, err:', err);
};

/**
* An extension of the base logger which accepts log input on a HTTP URL.
Expand All @@ -22,13 +27,13 @@ class ServerLogger extends Logger {
constructor(strategy, webapp = null, parameters = {}) {
super(strategy);
const defaultParameters = {
servePath: '/logger'
servePath: "/logger"
};

// Loop on defaults, not arguments, to avoid injecting any random junk.
for (const key in defaultParameters) {
if (defaultParameters.hasOwnProperty(key)) {
this[key] = (typeof parameters[key] !== 'undefined')
this[key] = (typeof parameters[key] !== "undefined")
? parameters[key]
: defaultParameters[key];
}
Expand All @@ -37,6 +42,31 @@ class ServerLogger extends Logger {
this.setupConnect(webapp, this.servePath);
}

/**
* Arm the report subscriber.
*/
arm() {
console.log('Armed serverCatcher');
process.on('uncaughtException', serverCatcher);
// this.tk.report.subscribe(this.reportSubscriber.bind(this));
}

/**
* Disarm the subscriber.
*
* In most cases, we do not want to disarm immediately: a stack trace being
* build may take several hundred milliseconds, and we would lose it.
*
* @param delay
*/
disarm(delay = 2000) {
console.log('Request to disarm serverCatcher');
setTimeout(() => {
console.log('Disarmed serverCatcher');
process.removeListener('uncaughtException', serverCatcher);
}, delay);
}

/**
* Handle a log message from the client.
*
Expand All @@ -51,7 +81,7 @@ class ServerLogger extends Logger {
*/
handleClientLogRequest(req, res, next) {
const method = req.method.toUpperCase();
if (method !== 'POST') {
if (method !== "POST") {
// RFC2616: 405 means Method not allowed.
res.writeHead(405);
res.end();
Expand All @@ -62,9 +92,9 @@ class ServerLogger extends Logger {
req.setMaxListeners(20);

let body = "";
req.setEncoding('utf-8');
req.setEncoding("utf-8");

req.on('data', chunk => { body += chunk; });
req.on("data", chunk => { body += chunk; });

req.on("end", Meteor.bindEnvironment(() => {
let result;
Expand Down

0 comments on commit 5662bef

Please sign in to comment.