<commit_before>/**
 * Message Entity
 */
'use strict';
var Promise = require('bluebird');
var debug = require('debug')('broker:Message');
/**
 * Base class for received messages.
 *
 * @param {[type]} options [description]
 * @param {Object} options.body [description]
 * @param {Channel} options.channel [description]
 * @param {Object} options.headers [description]
 * @param {Object} options.properties [description]
 * @param {String} options.deliveryTag [description]
 * @param {String} options.deliveryInfo [description]
 * @param {String} options.correlationId [description]
 * @param {String} options.replyTo [description]
 * @param {Number} options.expiration [description]
 */
var Message = function (options, rawMessage) {
  this.body = options.body;
  this.channel = options.channel;
  this.headers = options.headers;
  this.properties = options.properties;
  this.deliveryTag = options.deliveryTag;
  this.deliveryInfo = options.deliveryInfo;
  this.deliveryModel = options.deliveryModel;
  this.correlationId = options.correlationId;
  this.replyTo = options.replyTo;
  this.expiration = options.expiration;
  this.rawMessage = rawMessage || {
    fields: {
      deliveryTag: this.deliveryTag
    }
  };
  this._state = 'RECEIVED';
};

Message.fromRawMessage = function (message) {
  var properties = message.properties;
  var headers = message.headers;
  if (!message.fields) {
    debug('fromRawMessage', 'no fields');
  }
  var deliveryTag = message.fields.deliveryTag;
  var deliveryModel = message.fields.deliveryModel;
  var correlationId = message.properties.correlationId;
  var replyTo = message.properties.replyTo;
  return new Message({
    body: message.content.toString(),
    properties: properties,
    headers: headers,
    deliveryTag: deliveryTag,
    deliveryModel: deliveryModel,
    correlationId: correlationId,
    replyTo: replyTo
  }, message);
};

Message.prototype.encode = function () {
  return new Buffer(JSON.stringify(this.body));
};

Message.prototype.getPublishOptions = function () {
  return {
    headers: this.headers,
    correlationId: this.correlationId,
    deliveryModel: this.deliveryModel,
    expiration: this.expiration,
    replyTo: this.replyTo
  };
};

Message.prototype.ACK_STATES = ['ACK', 'REJECTED', 'REQUEUED'];

/**
 * Acknowledge this message as being processed.,
 * This will remove the message from the queue.

 * @throws {MessageStateError} If the message has already been
 * @throws {MessageStateError} If acknowledged/requeued/rejected.
 *
 * @return {[type]} [description]
 */
Message.prototype.ack = function () {
  this._state = 'ACK';
  return this.channel.getChannel()
    .then(function (channel) {
      return channel.ack(this.rawMessage);
    });

};

/**
 * Reject this message.
 *
 * The message will be discarded by the server.
 *
 * @throws {MessageStateError} If the message has already been
 *                             acknowledged/requeued/rejected.
 * @param  {[type]} options [description]
 * @param  {Boolean} options.requeue [description]
 * @return {[type]}         [description]
 */
Message.prototype.reject = function (channel, options) {
  options = options || {};
  var requeue = options.requeue;
  this._state = 'REJECTED';
  return Promise.resolve(channel.reject(this.rawMessage, requeue));
};

/**
 * Reject this message and put it back on the queue.
 *
 * You must not use this method as a means of selecting messages to process.
 *
 * @throws {MessageStateError} If the message has already been
 *                             acknowledged/requeued/rejected.
 * @return {[type]} [description]
 */
Message.prototype.requeue = function (channel) {
  if (this.isAcknowledged()) {
    return Promise.reject(new Error('Message already acknowledged with state'));
  }
  this._state = 'REQUEUED';
  var requeue = true;
  return Promise.resolve(channel.reject(this.rawMessage, requeue));
};

/**
 * Set to true if the message has been acknowledged.
 *
 * @return {Boolean} [description]
 */
Message.prototype.isAcknowledged = function () {
  return this.ACK_STATES.indexOf(this._state) !== -1;
};

/**
 * The decoded message body.
 *
 * @return {[type]} [description]
 */
Message.prototype.getPayload = Promise.method(function () {
  return JSON.parse(this.body);
});

module.exports = Message;
<commit_msg>[message] fix #ack<commit_after>/**
 * Message Entity
 */
'use strict';
var Promise = require('bluebird');
var debug = require('debug')('broker:Message');
/**
 * Base class for received messages.
 *
 * @param {[type]} options [description]
 * @param {Object} options.body [description]
 * @param {Channel} options.channel [description]
 * @param {Object} options.headers [description]
 * @param {Object} options.properties [description]
 * @param {String} options.deliveryTag [description]
 * @param {String} options.deliveryInfo [description]
 * @param {String} options.correlationId [description]
 * @param {String} options.replyTo [description]
 * @param {Number} options.expiration [description]
 */
var Message = function (options, rawMessage) {
  this.body = options.body;
  this.channel = options.channel;
  this.headers = options.headers;
  this.properties = options.properties;
  this.deliveryTag = options.deliveryTag;
  this.deliveryInfo = options.deliveryInfo;
  this.deliveryModel = options.deliveryModel;
  this.correlationId = options.correlationId;
  this.replyTo = options.replyTo;
  this.expiration = options.expiration;
  this.rawMessage = rawMessage || {
    fields: {
      deliveryTag: this.deliveryTag
    }
  };
  this._state = 'RECEIVED';
};

Message.fromRawMessage = function (message) {
  var properties = message.properties;
  var headers = message.headers;
  if (!message.fields) {
    debug('fromRawMessage', 'no fields');
  }
  var deliveryTag = message.fields.deliveryTag;
  var deliveryModel = message.fields.deliveryModel;
  var correlationId = message.properties.correlationId;
  var replyTo = message.properties.replyTo;
  return new Message({
    body: message.content.toString(),
    properties: properties,
    headers: headers,
    deliveryTag: deliveryTag,
    deliveryModel: deliveryModel,
    correlationId: correlationId,
    replyTo: replyTo
  }, message);
};

Message.prototype.encode = function () {
  return new Buffer(JSON.stringify(this.body));
};

Message.prototype.getPublishOptions = function () {
  return {
    headers: this.headers,
    correlationId: this.correlationId,
    deliveryModel: this.deliveryModel,
    expiration: this.expiration,
    replyTo: this.replyTo
  };
};

Message.prototype.ACK_STATES = ['ACK', 'REJECTED', 'REQUEUED'];

/**
 * Acknowledge this message as being processed.,
 * This will remove the message from the queue.

 * @throws {MessageStateError} If the message has already been
 * @throws {MessageStateError} If acknowledged/requeued/rejected.
 *
 * @return {[type]} [description]
 */
Message.prototype.ack = function () {
  this._state = 'ACK';
  var self = this;
  return this.channel.getChannel()
    .then(function (channel) {
      return channel.ack(self.rawMessage);
    });

};

/**
 * Reject this message.
 *
 * The message will be discarded by the server.
 *
 * @throws {MessageStateError} If the message has already been
 *                             acknowledged/requeued/rejected.
 * @param  {[type]} options [description]
 * @param  {Boolean} options.requeue [description]
 * @return {[type]}         [description]
 */
Message.prototype.reject = function (channel, options) {
  options = options || {};
  var requeue = options.requeue;
  this._state = 'REJECTED';
  var self = this;
  return this.channel.getChannel()
    .then(function (channel) {
      return channel.reject(self.rawMessage, requeue);
    });
};

/**
 * Reject this message and put it back on the queue.
 *
 * You must not use this method as a means of selecting messages to process.
 *
 * @throws {MessageStateError} If the message has already been
 *                             acknowledged/requeued/rejected.
 * @return {[type]} [description]
 */
Message.prototype.requeue = function (channel) {
  if (this.isAcknowledged()) {
    return Promise.reject(new Error('Message already acknowledged with state'));
  }
  var self = this;
  this._state = 'REQUEUED';
  var requeue = true;
  return this.channel.getChannel()
    .then(function (channel) {
      return channel.reject(self.rawMessage, requeue);
    });
};

/**
 * Set to true if the message has been acknowledged.
 *
 * @return {Boolean} [description]
 */
Message.prototype.isAcknowledged = function () {
  return this.ACK_STATES.indexOf(this._state) !== -1;
};

/**
 * The decoded message body.
 *
 * @return {[type]} [description]
 */
Message.prototype.getPayload = Promise.method(function () {
  return JSON.parse(this.body);
});

module.exports = Message;
<|endoftext|>


<commit_before>/**
 * A common requirement for a service is send metrics information to the Metric service.
 *
 * @params access_token The current access_token for the request
 * @params realm The Realm name
 * @params service Name of the service the metric is associated with
 * @params metric The actual metric information payload
 * @params type The logical type of the metric payload (e.g. location)
 * @params callback The async callback with the result of the metrics submission
 */
function sendMetric(token, realm, service, metric, type, cb) {

    //In order to have modules respect the logging configuration supplied by the service, we
    //need to get the logger in a 'lazy' fashion.  If we get it during initialization, then
    //the logger instance only has the default configuration.
    var logger = require('./loggers').getLogger();

    //If you want to run your service without actually sending metrics to a Metrics service, you
    //can disable it during development.
    if (!config.metrics.enabled) {
        logger.warn('[config.metrics.enabled = ' + config.metrics.enabled + ']',
            '** Metrics has been disabled for development. It should be re-enabled in a production environment. **');
        cb(null, {msg: 'permission(s) granted', username: 'developmentUser'});
        return;
    }

    logger.debug('sendMetric:' +
        '\n  access_token: ' + token +
        '\n  realm: ' + realm +
        '\n  service: ' + service +
        '\n  metric: ' + metric +
        '\n  type: ' + type
    );

    var errorMessage;

    if (!token) {
        errorMessage = 'missing access_token';
    } else if (!realm) {
        errorMessage = 'missing realm';
    } else if (!service) {
        errorMessage = 'missing service';
    } else if (!metric) {
        errorMessage = 'missing service';
    } else if (!objectType) {
        errorMessage = 'missing service';
    }

    if (errorMessage) {
        logger.error(errorMessage);
        callback(errorMessage);
        return;
    }

    //Prepend realm to type so that data is stored in a realm specific collection (. not allowed so use _ instead)
    var type = realm.split(".").join("_") + "_" + service;
    var content = {
        "access_token": token,
        "type": type,
        "time": new Date(),
        "data": {
            "content": metric,
            "postType": objectType
        }
    };

    // Set our options, such as where to POST to
    var options = {
        uri: config.metrics.url + realm + "/stats",
        body: JSON.stringify(content),
        headers: { 'Content-type': 'application/json' }
    };

    logger.info("posting metrics: " + JSON.stringify(options));

    // Use "request" library to POST
    httpClient.post(options, function (err, response, body) {

        if (err) {
            logger.error(errorMessage);
            callback(err);
            return;
        }

        callback(null, result);
    });
}
exports.sendMetric = sendMetric;<commit_msg>NTFY-19: adjust naming for clarification and to fix typos<commit_after>var config = require('./config');
var httpClient = require('request');

/**
 * A common requirement for a service is send metrics information to the Metric service.
 *
 * @params access_token The current access_token for the request
 * @params realm The Realm name
 * @params service Name of the service the metric is associated with
 * @params metric The actual metric information payload
 * @params type The logical type of the metric payload (e.g. location)
 * @params callback The async callback with the result of the metrics submission
 */
function sendMetric(token, realm, service, metricData, metricType, cb) {

    //In order to have modules respect the logging configuration supplied by the service, we
    //need to get the logger in a 'lazy' fashion.  If we get it during initialization, then
    //the logger instance only has the default configuration.
    var logger = require('./loggers').getLogger();

    //If you want to run your service without actually sending metrics to a Metrics service, you
    //can disable it during development.
    if (!config.metrics.enabled) {
        logger.warn('[config.metrics.enabled = ' + config.metrics.enabled + ']',
            '** Metrics has been disabled for development. It should be re-enabled in a production environment. **');
        cb(null, {msg: 'permission(s) granted', username: 'developmentUser'});
        return;
    }

    logger.debug('sendMetric:' +
        '\n  access_token: ' + token +
        '\n  realm: ' + realm +
        '\n  service: ' + service +
        '\n  data: ' + metricData +
        '\n  type: ' + metricType
    );

    var errorMessage;

    if (!token) {
        errorMessage = 'missing access_token';
    } else if (!realm) {
        errorMessage = 'missing realm';
    } else if (!service) {
        errorMessage = 'missing service';
    } else if (!metricData) {
        errorMessage = 'missing service';
    } else if (!metricType) {
        errorMessage = 'missing service';
    }

    if (errorMessage) {
        logger.error(errorMessage);
        callback(errorMessage);
        return;
    }

    //Prepend realm to type so that data is stored in a realm specific collection (. not allowed so use _ instead)
    var collection = realm.split(".").join("_") + "_" + service;
    var content = {
        "access_token": token,
        "type": collection,
        "time": new Date(),
        "data": {
            "content": metricData,
            "postType": metricType
        }
    };

    // Set our options, such as where to POST to
    var options = {
        uri: config.metrics.url + realm + "/stats",
        body: JSON.stringify(content),
        headers: { 'Content-type': 'application/json' }
    };

    logger.info("posting metrics: " + JSON.stringify(options));

    // Use "request" library to POST
    httpClient.post(options, function (err, response, body) {

        if (err) {
            logger.error(errorMessage);
            callback(err);
            return;
        }

        callback(null, body);
    });
}
exports.sendMetric = sendMetric;<|endoftext|>


<commit_before>// ==UserScript==
// @name         tc.zwergenviertel
// @namespace    com.tangledcode
// @version      0.3
// @description  visual aid for the zwergenviertel forum
// @author       Daniel Malone
// @match        http://www.zwergenviertel.de/*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// @require      https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js
// @resource     bootstrap https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css
// ==/UserScript==

GM_addStyle(GM_getResourceText("bootstrap"));

String.prototype.endsWith = function(suffix) {
  return this.slice(-suffix.length) == suffix;
};

function setChildTransparent(index, element) {
  var $td = $(element);

  $td.css(transparent);
}

function setRowTransparent(index, element) {
  var $td = $(element);
  var $img = $td.find('img');

  if (!$img) return;

  var src = $img.attr('src');

  if (!src) {
    $td.parent().css(transparent);
  } else if (src.endsWith('new.gif')) {
    $img.parent().append('NEW');
    $img.remove();
  } else if (src.endsWith('off.png') || src.endsWith('redirect.png')) {
    $td.parent().css(transparent);
  }
}

var invisable   = { 'display': 'none' };
var transparent = { 'opacity': 0.25 };
var collapse    = { 'border-collapse': 'inherit' };

$(document).load(function() {
  var $header = $('#upper_section');
  $header.find('.user p.avatar').css(invisable);
  $header.find('.news').css(invisable);
  $('table.table_grid').css(collapse);

  $('#boardindex_table td.icon').each(setRowTransparent);
  $('#messageindex td.subject').each(setRowTransparent);

  $('#boardindex_table td.children').each(setChildTransparent);
});
<commit_msg>added grant to zwergenviertel script<commit_after>// ==UserScript==
// @name         tc.zwergenviertel
// @namespace    com.tangledcode
// @version      0.4
// @description  visual aid for the zwergenviertel forum
// @author       Daniel Malone
// @match        http://www.zwergenviertel.de/*
// @grant        GM_addStyle
// @require      https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// @resource     bootstrap https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css
// ==/UserScript==

GM_addStyle(GM_getResourceText("bootstrap"));

String.prototype.endsWith = function(suffix) {
  return this.slice(-suffix.length) == suffix;
};

function setChildTransparent(index, element) {
  var $td = $(element);

  $td.css(transparent);
}

function setRowTransparent(index, element) {
  var $td = $(element);
  var $img = $td.find('img');

  if (!$img) return;

  var src = $img.attr('src');

  if (!src) {
    $td.parent().css(transparent);
  } else if (src.endsWith('new.gif')) {
    $img.parent().append('NEW');
    $img.remove();
  } else if (src.endsWith('off.png') || src.endsWith('redirect.png')) {
    $td.parent().css(transparent);
  }
}

var invisable   = { 'display': 'none' };
var transparent = { 'opacity': 0.25 };
var collapse    = { 'border-collapse': 'inherit' };

$(document).load(function() {
  var $header = $('#upper_section');
  $header.find('.user p.avatar').css(invisable);
  $header.find('.news').css(invisable);
  $('table.table_grid').css(collapse);

  $('#boardindex_table td.icon').each(setRowTransparent);
  $('#messageindex td.subject').each(setRowTransparent);

  $('#boardindex_table td.children').each(setChildTransparent);
});
<|endoftext|>


<commit_before>/**
 * Module dependencies.
 */
var http = require('http');
var https = require('https');
var url  = require('url');
var EventEmitter = require('events').EventEmitter;
var PollerCollection = require('./pollers/pollerCollection');

/**
 * Monitor constructor
 *
 * The monitor pings the checks regularly and saves the response status and time.
 * The monitor doesn't interact with the model classes directly, but instead uses
 * the REST HTTP API. This way, the monitor can run on a separate process, so that the
 * ping measurements don't get distorted by a heavy usage of the GUI.
 *
 * The constructor expects a configuration object as parameter, with these properties:
 *   pollingInterval: Interval between each poll in milliseconds, defaults to 10 seconds
 *   timeout: Request timeout in milliseconds, defaults to 5 seconds
 *
 * @param {Object} Monitor configuration
 * @api   public
 */
function Monitor(config) {
  config.pollingInterval = config.pollingInterval || 10 * 1000;
  config.timeout = config.timeout || 5 * 1000;
  this.config = config;
  this.pollerCollection = new PollerCollection();
  this.selfSigned = config.selfSigned;
  this.ssl = url.parse(config.apiUrl).protocol.indexOf('https') > -1 ? true : false;
  this.connection = this.ssl === true ? https : http;
  this.apiHttpOptions = {};
}

/**
 * Inherit from EventEmitter.
 */
Monitor.prototype.__proto__ = EventEmitter.prototype;

/**
 * Start the monitoring of all checks.
 *
 * The polling actually starts after the pollingInterval set to the constructor.
 *
 * @api   public
 */
Monitor.prototype.start = function() {
  // start polling right away
  this.pollChecksNeedingPoll();
  // schedule future polls
  this.intervalForPoll   = setInterval(this.pollChecksNeedingPoll.bind(this), this.config.pollingInterval);
  console.log('Monitor ' + this.config.name + ' started');
};

/**
 * Stop the monitoring of all checks
 *
 * @api   public
 */
Monitor.prototype.stop = function() {
  clearInterval(this.intervalForPoll);
  console.log('Monitor ' + this.config.name + ' stopped');
};

/**
 * Find checks that need to be polled.
 *
 * A check needs to be polled if it was last polled sine a longer time than its own interval.
 *
 * @param {Function} Callback function to be called with each Check
 * @api   private
 */
Monitor.prototype.pollChecksNeedingPoll = function(callback) {
  var self = this;
  this.findChecksNeedingPoll(function(err, checks) {
    if (err) {
      console.error(err);
      if (callback) callback(err);
      return;
    }
    checks.forEach(function(check) {
      self.pollCheck(check, function(err) {
        if (err) console.error(err);
      });
    });
  });
};

Monitor.prototype.findChecksNeedingPoll = function(callback) {
  var options = url.parse(this.config.apiUrl + '/checks/needingPoll');
  this.applyApiHttpOptions(options);
  var self = this;
  if (self.selfSigned === true) options.rejectUnauthorized = false;
  this.connection.get(options, function(res) {
    if (res.statusCode != 200) {
      return callback(new Error(self.config.apiUrl + '/checks/needingPoll resource responded with error code: ' + res.statusCode));
    }
    var body = '';
    res.on('data', function(chunk) {
      body += chunk;
    });
    res.on('end', function() {
      callback(null, JSON.parse(body));
    });
  }).on('error', function(e) {
    callback(new Error(self.config.apiUrl + '/checks/needingPoll resource not available: ' + e.message));
  });
};

/**
 * Poll a given check, and create a ping according to the result.
 *
 * @param {Object} check is a simple JSON object returned by the API, NOT a Check object
 * @api   private
 */
Monitor.prototype.pollCheck = function(check, callback) {
  if (!check) return;
  var Poller, p;
  var now = Date.now();
  var self = this;
  // change lastTested date right away to avoid polling twice if the target doesn't answer in timely fashion

  this.declarePoll(check, function(err) { });
  var details = {};
  try {
    Poller = this.pollerCollection.getForType(check.type || 'http');
  } catch (unknownPollerError) {
    return self.createPing(unknownPollerError, check, now, 0, details, callback);
  }
  var pollerCallback = function(err, time, res, pollerDetails) {
    if (err) {
      return self.createPing(err, check, now, time, pollerDetails || details, callback);
    }
    try {
      self.emit('pollerPolled', check, res, pollerDetails || details);
      self.createPing(null, check, now, time, pollerDetails || details, callback);
    } catch (error) {
      return self.createPing(error, check, now, time, pollerDetails || details, callback);
    }
  };
  try {
    var timeout = check.timeout || this.config.timeout;
    p = new Poller(check.url, timeout, pollerCallback,check);
    if ('setUserAgent' in p) {
      p.setUserAgent(this.config.userAgent);
    }
    self.emit('pollerCreated', p, check, details);
  } catch (incorrectPollerUrl) {
    return self.createPing(incorrectPollerUrl, check, now, 0, details, callback);
  }
  //p.setDebug(true);
  p.poll();
};

Monitor.prototype.declarePoll = function(check, callback) {
  var options = url.parse(this.config.apiUrl + '/check/' + check._id + '/test');
  options.method = 'PUT';
  this.applyApiHttpOptions(options);
  var self = this;
  if (self.selfSigned === true) options.rejectUnauthorized = false;
  var req = this.connection.request(options, function(res) {
    if (res.statusCode != 200) {
      return callback(new Error(self.config.apiUrl + '/check/:id/test resource responded with error code: ' + res.statusCode));
    }
    res.on('data', function(chunk) {
      // do nothing
    });
    res.on('end', function() {
      if (callback) callback();
    });
  }).on('error', function(e) {
    callback(new Error(self.config.apiUrl + '/check/:id/test resource not available: ' + e.message));
  });
  req.end();
};

Monitor.prototype.createPing = function(error, check, timestamp, time, details, callback) {
  if(!check.owner.apiKeys[0]){
    return;
  }
  var postData = 'checkId=' + check._id +
                '&status=' + (error ? 'false' : 'true') +
                '&timestamp=' + timestamp +
                '&time='+ time +
                '&name=' + this.config.name;
  if (error) {
    postData += '&error=' + error.message;
  }
  if (details) {
    postData += '&details=' + encodeURIComponent(JSON.stringify(details));
  }
  var options = url.parse(this.config.apiUrl + '/pings?&apikey='+check.owner.apiKeys[0].apiKey);
  options.method = 'POST';
  options.headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': Buffer.byteLength(postData)
  };
  this.applyApiHttpOptions(options);
  var self = this;
  if (self.selfSigned === true) options.rejectUnauthorized = false;
  var req = this.connection.request(options, function(res) {
    if (res.statusCode != 200) {
      return callback(new Error(self.config.apiUrl + '/pings resource responded with error code: ' + res.statusCode));
    }
    var body = '';
    res.on('data', function(chunk) {
    body += chunk;
    });
    res.on('end', function() {
      if (callback) callback(null, body);
    });
  }).on('error', function(e) {
    callback(new Error(self.config.apiUrl + '/pings resource not available: ' + e.message));
  });
  req.write(postData);
  req.end();
};

/**
 * Add custom HTTP options to all the API calls
 * Useful to add proxy headers, Basic HTTP auth, etc.
 */
Monitor.prototype.addApiHttpOption = function(key, value) {
  this.apiHttpOptions[key] = value;
};

/**
 * Called before every API HTTP call
 */
Monitor.prototype.applyApiHttpOptions = function(options) {
  for (var key in this.apiHttpOptions) {
    options[key] = this.apiHttpOptions[key];
  }
};

/**
 * Create a monitor to poll all checks at a given interval.
 *
 * Example:
 *
 *    m = monitor.createMonitor({ pollingInterval: 60000});
 *    m.start();
 *    // the polling starts, every 60 seconds
 *    m.stop();
 *
 * @param {Object} Configuration object
 * @api   public
 */
exports.createMonitor = function(config) {
  return new Monitor(config);
};
<commit_msg>enable debugging<commit_after>/**
 * Module dependencies.
 */
var http = require('http');
var https = require('https');
var url  = require('url');
var EventEmitter = require('events').EventEmitter;
var PollerCollection = require('./pollers/pollerCollection');

/**
 * Monitor constructor
 *
 * The monitor pings the checks regularly and saves the response status and time.
 * The monitor doesn't interact with the model classes directly, but instead uses
 * the REST HTTP API. This way, the monitor can run on a separate process, so that the
 * ping measurements don't get distorted by a heavy usage of the GUI.
 *
 * The constructor expects a configuration object as parameter, with these properties:
 *   pollingInterval: Interval between each poll in milliseconds, defaults to 10 seconds
 *   timeout: Request timeout in milliseconds, defaults to 5 seconds
 *
 * @param {Object} Monitor configuration
 * @api   public
 */
function Monitor(config) {
  config.pollingInterval = config.pollingInterval || 10 * 1000;
  config.timeout = config.timeout || 5 * 1000;
  this.config = config;
  this.pollerCollection = new PollerCollection();
  this.selfSigned = config.selfSigned;
  this.ssl = url.parse(config.apiUrl).protocol.indexOf('https') > -1 ? true : false;
  this.connection = this.ssl === true ? https : http;
  this.apiHttpOptions = {};
}

/**
 * Inherit from EventEmitter.
 */
Monitor.prototype.__proto__ = EventEmitter.prototype;

/**
 * Start the monitoring of all checks.
 *
 * The polling actually starts after the pollingInterval set to the constructor.
 *
 * @api   public
 */
Monitor.prototype.start = function() {
  // start polling right away
  this.pollChecksNeedingPoll();
  // schedule future polls
  this.intervalForPoll   = setInterval(this.pollChecksNeedingPoll.bind(this), this.config.pollingInterval);
  console.log('Monitor ' + this.config.name + ' started');
};

/**
 * Stop the monitoring of all checks
 *
 * @api   public
 */
Monitor.prototype.stop = function() {
  clearInterval(this.intervalForPoll);
  console.log('Monitor ' + this.config.name + ' stopped');
};

/**
 * Find checks that need to be polled.
 *
 * A check needs to be polled if it was last polled sine a longer time than its own interval.
 *
 * @param {Function} Callback function to be called with each Check
 * @api   private
 */
Monitor.prototype.pollChecksNeedingPoll = function(callback) {
  var self = this;
  this.findChecksNeedingPoll(function(err, checks) {
    if (err) {
      console.error(err);
      if (callback) callback(err);
      return;
    }
    checks.forEach(function(check) {
      self.pollCheck(check, function(err) {
        if (err) console.error(err);
      });
    });
  });
};

Monitor.prototype.findChecksNeedingPoll = function(callback) {
  var options = url.parse(this.config.apiUrl + '/checks/needingPoll');
  this.applyApiHttpOptions(options);
  var self = this;
  if (self.selfSigned === true) options.rejectUnauthorized = false;
  this.connection.get(options, function(res) {
    if (res.statusCode != 200) {
      return callback(new Error(self.config.apiUrl + '/checks/needingPoll resource responded with error code: ' + res.statusCode));
    }
    var body = '';
    res.on('data', function(chunk) {
      body += chunk;
    });
    res.on('end', function() {
      callback(null, JSON.parse(body));
    });
  }).on('error', function(e) {
    callback(new Error(self.config.apiUrl + '/checks/needingPoll resource not available: ' + e.message));
  });
};

/**
 * Poll a given check, and create a ping according to the result.
 *
 * @param {Object} check is a simple JSON object returned by the API, NOT a Check object
 * @api   private
 */
Monitor.prototype.pollCheck = function(check, callback) {
  if (!check) return;
  var Poller, p;
  var now = Date.now();
  var self = this;
  // change lastTested date right away to avoid polling twice if the target doesn't answer in timely fashion

  this.declarePoll(check, function(err) { });
  var details = {};
  try {
    Poller = this.pollerCollection.getForType(check.type || 'http');
  } catch (unknownPollerError) {
    return self.createPing(unknownPollerError, check, now, 0, details, callback);
  }
  var pollerCallback = function(err, time, res, pollerDetails) {
    if (err) {
      return self.createPing(err, check, now, time, pollerDetails || details, callback);
    }
    try {
      self.emit('pollerPolled', check, res, pollerDetails || details);
      self.createPing(null, check, now, time, pollerDetails || details, callback);
    } catch (error) {
      return self.createPing(error, check, now, time, pollerDetails || details, callback);
    }
  };
  try {
    var timeout = check.timeout || this.config.timeout;
    p = new Poller(check.url, timeout, pollerCallback,check);
    if ('setUserAgent' in p) {
      p.setUserAgent(this.config.userAgent);
    }
    self.emit('pollerCreated', p, check, details);
  } catch (incorrectPollerUrl) {
    return self.createPing(incorrectPollerUrl, check, now, 0, details, callback);
  }

  /**
    for debugging proposals
  **/
  p.setDebug(true);
  p.poll();
};

Monitor.prototype.declarePoll = function(check, callback) {
  var options = url.parse(this.config.apiUrl + '/check/' + check._id + '/test');
  options.method = 'PUT';
  this.applyApiHttpOptions(options);
  var self = this;
  if (self.selfSigned === true) options.rejectUnauthorized = false;
  var req = this.connection.request(options, function(res) {
    if (res.statusCode != 200) {
      return callback(new Error(self.config.apiUrl + '/check/:id/test resource responded with error code: ' + res.statusCode));
    }
    res.on('data', function(chunk) {
      // do nothing
    });
    res.on('end', function() {
      if (callback) callback();
    });
  }).on('error', function(e) {
    callback(new Error(self.config.apiUrl + '/check/:id/test resource not available: ' + e.message));
  });
  req.end();
};

Monitor.prototype.createPing = function(error, check, timestamp, time, details, callback) {
  if(!check.owner.apiKeys[0]){
    return;
  }
  var postData = 'checkId=' + check._id +
                '&status=' + (error ? 'false' : 'true') +
                '&timestamp=' + timestamp +
                '&time='+ time +
                '&name=' + this.config.name;
  if (error) {
    postData += '&error=' + error.message;
  }
  if (details) {
    postData += '&details=' + encodeURIComponent(JSON.stringify(details));
  }
  var options = url.parse(this.config.apiUrl + '/pings?&apikey='+check.owner.apiKeys[0].apiKey);
  options.method = 'POST';
  options.headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': Buffer.byteLength(postData)
  };
  this.applyApiHttpOptions(options);
  var self = this;
  if (self.selfSigned === true) options.rejectUnauthorized = false;
  var req = this.connection.request(options, function(res) {
    if (res.statusCode != 200) {
      return callback(new Error(self.config.apiUrl + '/pings resource responded with error code: ' + res.statusCode));
    }
    var body = '';
    res.on('data', function(chunk) {
    body += chunk;
    });
    res.on('end', function() {
      if (callback) callback(null, body);
    });
  }).on('error', function(e) {
    callback(new Error(self.config.apiUrl + '/pings resource not available: ' + e.message));
  });
  req.write(postData);
  req.end();
};

/**
 * Add custom HTTP options to all the API calls
 * Useful to add proxy headers, Basic HTTP auth, etc.
 */
Monitor.prototype.addApiHttpOption = function(key, value) {
  this.apiHttpOptions[key] = value;
};

/**
 * Called before every API HTTP call
 */
Monitor.prototype.applyApiHttpOptions = function(options) {
  for (var key in this.apiHttpOptions) {
    options[key] = this.apiHttpOptions[key];
  }
};

/**
 * Create a monitor to poll all checks at a given interval.
 *
 * Example:
 *
 *    m = monitor.createMonitor({ pollingInterval: 60000});
 *    m.start();
 *    // the polling starts, every 60 seconds
 *    m.stop();
 *
 * @param {Object} Configuration object
 * @api   public
 */
exports.createMonitor = function(config) {
  return new Monitor(config);
};
<|endoftext|>


<commit_before>(function () {
function Rule(name, symbols, postprocess) {
    this.id = ++Rule.highestId;
    this.name = name;
    this.symbols = symbols;        // a list of literal | regex class | nonterminal
    this.postprocess = postprocess;
    return this;
}
Rule.highestId = 0;

Rule.prototype.toString = function(withCursorAt) {
    function stringifySymbolSequence (e) {
        return (e.literal) ? JSON.stringify(e.literal)
                           : e.toString();
    }
    var symbolSequence = (typeof withCursorAt === "undefined")
                         ? this.symbols.map(stringifySymbolSequence).join(' ')
                         : (   this.symbols.slice(0, withCursorAt).map(stringifySymbolSequence).join(' ')
                             + " ● "
                             + this.symbols.slice(withCursorAt).map(stringifySymbolSequence).join(' ')     );
    return this.name + " → " + symbolSequence;
}

// a State is a rule at a position from a given starting point in the input stream (reference)
function State(rule, dot, reference, wantedBy) {
    this.rule = rule;
    this.dot = dot;
    this.reference = reference;
    this.data = [];
    this.wantedBy = wantedBy;
    this.isComplete = this.dot === rule.symbols.length;
}

State.prototype.toString = function() {
    return "{" + this.rule.toString(this.dot) + "}, from: " + (this.reference || 0);
};

State.prototype.nextState = function(data) {
    var state = new State(this.rule, this.dot + 1, this.reference, this.wantedBy);
    state.data = this.data.slice(0);  // make a cheap copy of currentState's data
    state.data.push(data);            // append the passed data
    return state;
};

State.prototype.consumeTerminal = function(inp) {
    var val = false;
    if (this.rule.symbols[this.dot]) {                  // is there a symbol to test?
       if (this.rule.symbols[this.dot].test) {          // is the symbol a regex?
          if (this.rule.symbols[this.dot].test(inp)) {  // does the regex match
             val = this.nextState(inp);  // nextState on a successful regex match
          }
       } else {   // not a regex, must be a literal
          if (this.rule.symbols[this.dot].literal === inp) {
             val = this.nextState(inp);  // nextState on a successful literal match
          }
       }
    }
    return val;
};

State.prototype.consumeNonTerminal = function(inp) {
    if (this.rule.symbols[this.dot] === inp) {
        return this.nextState(inp);
    }
    return false;
};

State.prototype.finish = function() {
    if (this.rule.postprocess) {
        this.data = this.rule.postprocess(this.data, this.reference, Parser.fail);
    }
};

function Column(grammar, index) {
    this.grammar = grammar;
    this.index = index;
    this.states = [];
    this.wants = {}; // states that want some token
    this.completed = {}; // states that are nullable
}

Column.prototype.process = function(nextColumn) {
    var states = this.states;
    var wants = this.wants;
    var completed = this.completed;

    for (var w = 0; w < states.length; w++) { // nb. we push() during iteration
        var state = states[w];

        if (state.isComplete) {
            state.finish();
            if (state.data !== Parser.fail) {
                // complete
                var wantedBy = state.wantedBy;
                for (var i = 0; i < wantedBy.length; i++) {
                    var left = wantedBy[i];
                    this.complete(left, state);
                }

                // special-case nullables
                if (state.reference === this.index) {
                    // make sure future predictors of this rule get completed.
                    var exp = state.rule.name;
                    (this.completed[exp] = this.completed[exp] || []).push(state);
                }
            }

        } else {
            var exp = state.rule.symbols[state.dot];
            if (typeof exp !== 'string') { continue; }

            if (wants.hasOwnProperty(exp)) {
                wants[exp].push(state);

                if (completed.hasOwnProperty(exp)) {
                    var nulls = completed[exp];
                    for (var i = 0; i < nulls.length; i++) {
                        var right = nulls[i];
                        this.complete(state, right);
                    }
                }
            } else {
                wants[exp] = [state];
                this.predict(exp);
            }
        }
    }
}

Column.prototype.predict = function(exp) {
    var rules = this.grammar.byName[exp] || [];

    for (var i = 0; i < rules.length; i++) {
        var r = rules[i];
        var wantedBy = this.wants[exp];
        var s = new State(r, 0, this.index, wantedBy);
        this.states.push(s);
    }
}

Column.prototype.complete = function(left, right) {
    var copy = left.consumeNonTerminal(right.rule.name);
    if (copy) {
        copy.data[copy.data.length - 1] = right.data;
        this.states.push(copy);
    }
}

function Grammar(rules, start) {
    this.rules = rules.map(function (r) { return (new Rule(r.name, r.symbols, r.postprocess)); });
    this.start = start || this.rules[0].name;
    var byName = this.byName = {};
    this.rules.forEach(function(rule) {
        if (!byName.hasOwnProperty(rule.name)) {
            byName[rule.name] = [];
        }
        byName[rule.name].push(rule);
    });
}

function Parser(rules, start) {
    //var grammar = this.grammar = rules instanceof Grammar ? rules : new Grammar(rules, start);
    var grammar = this.grammar = new Grammar(rules, start);

    // Setup a table
    var column = new Column(grammar, 0);
    var table = this.table = [column];

    // I could be expecting anything.
    column.wants[grammar.start] = [];
    column.predict(grammar.start);
    // TODO what if start rule is nullable?
    column.process();
    this.current = 0;
}

// create a reserved token for indicating a parse fail
Parser.fail = {};

Parser.prototype.feed = function(chunk) {
    for (var chunkPos = 0; chunkPos < chunk.length; chunkPos++) {
        // We add new states to table[current+1]
        var column = this.table[this.current + chunkPos];

        var n = this.current + chunkPos + 1;
        var nextColumn = new Column(this.grammar, n);
        this.table.push(nextColumn);

        // Advance all tokens that expect the symbol
        // So for each state in the previous row,

        for (var w = 0; w < column.states.length; w++) {
            var s = column.states[w];
            var x = s.consumeTerminal(chunk[chunkPos]);      // Try to consume the token
            if (x) {
                // And then add it
                nextColumn.states.push(x);
            }
        }

        // Next, for each of the rules, we either
        // (a) complete it, and try to see if the reference row expected that
        //     rule
        // (b) predict the next nonterminal it expects by adding that
        //     nonterminal's start state
        // To prevent duplication, we also keep track of rules we have already
        // added

        nextColumn.process();

        // If needed, throw an error:
        if (nextColumn.states.length === 0) {
            // No states at all! This is not good.
            var err = new Error(
                "nearley: No possible parsings (@" + (this.current + chunkPos)
                    + ": '" + chunk[chunkPos] + "')."
            );
            err.offset = this.current + chunkPos;
            throw err;
        }
    }

    this.current += chunkPos;
    // Incrementally keep track of results
    this.results = this.finish();

    // Allow chaining, for whatever it's worth
    return this;
};

Parser.prototype.finish = function() {
    // Return the possible parsings
    var considerations = [];
    var start = this.grammar.start;
    var column = this.table[this.table.length - 1]
    column.states.forEach(function (t) {
        if (t.rule.name === start
                && t.dot === t.rule.symbols.length
                && t.reference === 0
                && t.data !== Parser.fail) {
            considerations.push(t);
        }
    });
    return considerations.map(function(c) {return c.data; });
};

var nearley = {
    Parser: Parser,
    Rule: Rule
};

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
   module.exports = nearley;
} else {
   window.nearley = nearley;
}
})();
<commit_msg>GC unused states (!)<commit_after>(function () {
function Rule(name, symbols, postprocess) {
    this.id = ++Rule.highestId;
    this.name = name;
    this.symbols = symbols;        // a list of literal | regex class | nonterminal
    this.postprocess = postprocess;
    return this;
}
Rule.highestId = 0;

Rule.prototype.toString = function(withCursorAt) {
    function stringifySymbolSequence (e) {
        return (e.literal) ? JSON.stringify(e.literal)
                           : e.toString();
    }
    var symbolSequence = (typeof withCursorAt === "undefined")
                         ? this.symbols.map(stringifySymbolSequence).join(' ')
                         : (   this.symbols.slice(0, withCursorAt).map(stringifySymbolSequence).join(' ')
                             + " ● "
                             + this.symbols.slice(withCursorAt).map(stringifySymbolSequence).join(' ')     );
    return this.name + " → " + symbolSequence;
}

// a State is a rule at a position from a given starting point in the input stream (reference)
function State(rule, dot, reference, wantedBy) {
    this.rule = rule;
    this.dot = dot;
    this.reference = reference;
    this.data = [];
    this.wantedBy = wantedBy;
    this.isComplete = this.dot === rule.symbols.length;
}

State.prototype.toString = function() {
    return "{" + this.rule.toString(this.dot) + "}, from: " + (this.reference || 0);
};

State.prototype.nextState = function(data) {
    var state = new State(this.rule, this.dot + 1, this.reference, this.wantedBy);
    state.data = this.data.slice(0);  // make a cheap copy of currentState's data
    state.data.push(data);            // append the passed data
    return state;
};

State.prototype.consumeTerminal = function(inp) {
    var val = false;
    if (this.rule.symbols[this.dot]) {                  // is there a symbol to test?
       if (this.rule.symbols[this.dot].test) {          // is the symbol a regex?
          if (this.rule.symbols[this.dot].test(inp)) {  // does the regex match
             val = this.nextState(inp);  // nextState on a successful regex match
          }
       } else {   // not a regex, must be a literal
          if (this.rule.symbols[this.dot].literal === inp) {
             val = this.nextState(inp);  // nextState on a successful literal match
          }
       }
    }
    return val;
};

State.prototype.consumeNonTerminal = function(inp) {
    if (this.rule.symbols[this.dot] === inp) {
        return this.nextState(inp);
    }
    return false;
};

State.prototype.finish = function() {
    if (this.rule.postprocess) {
        this.data = this.rule.postprocess(this.data, this.reference, Parser.fail);
    }
};

function Column(grammar, index) {
    this.grammar = grammar;
    this.index = index;
    this.states = [];
    this.wants = {}; // states that want some token
    this.completed = {}; // states that are nullable
}

Column.prototype.process = function(nextColumn) {
    var states = this.states;
    var wants = this.wants;
    var completed = this.completed;

    for (var w = 0; w < states.length; w++) { // nb. we push() during iteration
        var state = states[w];

        if (state.isComplete) {
            state.finish();
            if (state.data !== Parser.fail) {
                // complete
                var wantedBy = state.wantedBy;
                for (var i = 0; i < wantedBy.length; i++) {
                    var left = wantedBy[i];
                    this.complete(left, state);
                }

                // special-case nullables
                if (state.reference === this.index) {
                    // make sure future predictors of this rule get completed.
                    var exp = state.rule.name;
                    (this.completed[exp] = this.completed[exp] || []).push(state);
                }
            }

        } else {
            var exp = state.rule.symbols[state.dot];
            if (typeof exp !== 'string') { continue; }

            if (wants.hasOwnProperty(exp)) {
                wants[exp].push(state);

                if (completed.hasOwnProperty(exp)) {
                    var nulls = completed[exp];
                    for (var i = 0; i < nulls.length; i++) {
                        var right = nulls[i];
                        this.complete(state, right);
                    }
                }
            } else {
                wants[exp] = [state];
                this.predict(exp);
            }
        }
    }
}

Column.prototype.predict = function(exp) {
    var rules = this.grammar.byName[exp] || [];

    for (var i = 0; i < rules.length; i++) {
        var r = rules[i];
        var wantedBy = this.wants[exp];
        var s = new State(r, 0, this.index, wantedBy);
        this.states.push(s);
    }
}

Column.prototype.complete = function(left, right) {
    var copy = left.consumeNonTerminal(right.rule.name);
    if (copy) {
        copy.data[copy.data.length - 1] = right.data;
        this.states.push(copy);
    }
}

function Grammar(rules, start) {
    this.rules = rules.map(function (r) { return (new Rule(r.name, r.symbols, r.postprocess)); });
    this.start = start || this.rules[0].name;
    var byName = this.byName = {};
    this.rules.forEach(function(rule) {
        if (!byName.hasOwnProperty(rule.name)) {
            byName[rule.name] = [];
        }
        byName[rule.name].push(rule);
    });
}

function Parser(rules, start) {
    //var grammar = this.grammar = rules instanceof Grammar ? rules : new Grammar(rules, start);
    var grammar = this.grammar = new Grammar(rules, start);

    // Setup a table
    var column = new Column(grammar, 0);
    var table = this.table = [column];

    // I could be expecting anything.
    column.wants[grammar.start] = [];
    column.predict(grammar.start);
    // TODO what if start rule is nullable?
    column.process();
    this.current = 0;
}

// create a reserved token for indicating a parse fail
Parser.fail = {};

Parser.prototype.feed = function(chunk) {
    for (var chunkPos = 0; chunkPos < chunk.length; chunkPos++) {
        // We add new states to table[current+1]
        var column = this.table[this.current + chunkPos];

        // GC unused states
        delete this.table[this.current + chunkPos - 1];

        var n = this.current + chunkPos + 1;
        var nextColumn = new Column(this.grammar, n);
        this.table.push(nextColumn);

        // Advance all tokens that expect the symbol
        // So for each state in the previous row,

        for (var w = 0; w < column.states.length; w++) {
            var s = column.states[w];
            var x = s.consumeTerminal(chunk[chunkPos]);      // Try to consume the token
            if (x) {
                // And then add it
                nextColumn.states.push(x);
            }
        }

        // Next, for each of the rules, we either
        // (a) complete it, and try to see if the reference row expected that
        //     rule
        // (b) predict the next nonterminal it expects by adding that
        //     nonterminal's start state
        // To prevent duplication, we also keep track of rules we have already
        // added

        nextColumn.process();

        // If needed, throw an error:
        if (nextColumn.states.length === 0) {
            // No states at all! This is not good.
            var err = new Error(
                "nearley: No possible parsings (@" + (this.current + chunkPos)
                    + ": '" + chunk[chunkPos] + "')."
            );
            err.offset = this.current + chunkPos;
            throw err;
        }
    }

    this.current += chunkPos;
    // Incrementally keep track of results
    this.results = this.finish();

    // Allow chaining, for whatever it's worth
    return this;
};

Parser.prototype.finish = function() {
    // Return the possible parsings
    var considerations = [];
    var start = this.grammar.start;
    var column = this.table[this.table.length - 1]
    column.states.forEach(function (t) {
        if (t.rule.name === start
                && t.dot === t.rule.symbols.length
                && t.reference === 0
                && t.data !== Parser.fail) {
            considerations.push(t);
        }
    });
    return considerations.map(function(c) {return c.data; });
};

var nearley = {
    Parser: Parser,
    Rule: Rule
};

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
   module.exports = nearley;
} else {
   window.nearley = nearley;
}
})();
<|endoftext|>


<commit_before>'use strict'
var int53 = require('int53')

function getWrite (name) {
  if (Buffer.prototype[name]) return Buffer.prototype[name]
  return function (value, offset) {
    return int53[name](value, this, offset)
  }
}

function getRead (name) {
  if (Buffer.prototype[name]) return Buffer.prototype[name]
  return function (offset) {
    return int53[name](this, offset)
  }
}

exports.create = function (type, length) {
  var write = getWrite('write' + type)
  var read = getRead('read' + type)

  function encode (value, buffer, offset) {
    if (!buffer) buffer = Buffer.allocUnsafe(length)
    if (!offset) offset = 0
    write.call(buffer, value, offset)
    return buffer
  }

  function decode (buffer, offset, end) {
    if (!offset) offset = 0
    if (!end) return read.call(buffer, offset)
    return read.call(buffer.slice(offset, end), 0)
  }

  encode.bytes = decode.bytes = length
  return { encode: encode, decode: decode, encodingLength: function () { return length } }
}
<commit_msg>numbers: add type check<commit_after>'use strict'
var int53 = require('int53')

function getWrite (name) {
  if (Buffer.prototype[name]) return Buffer.prototype[name]
  return function (value, offset) {
    return int53[name](value, this, offset)
  }
}

function getRead (name) {
  if (Buffer.prototype[name]) return Buffer.prototype[name]
  return function (offset) {
    return int53[name](this, offset)
  }
}

exports.create = function (type, length) {
  var write = getWrite('write' + type)
  var read = getRead('read' + type)

  function encode (value, buffer, offset) {
    if (typeof value !== 'number') throw new TypeError('value must be a number')
    if (!buffer) buffer = Buffer.allocUnsafe(length)
    if (!offset) offset = 0
    write.call(buffer, value, offset)
    return buffer
  }

  function decode (buffer, offset, end) {
    if (!offset) offset = 0
    if (!end) return read.call(buffer, offset)
    return read.call(buffer.slice(offset, end), 0)
  }

  encode.bytes = decode.bytes = length
  return { encode: encode, decode: decode, encodingLength: function () { return length } }
}
<|endoftext|>


<commit_before>/**
 * Copyright 2015 Mozilla
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

// Import this first so we can use it to wrap other modules we import.
var promisify = require('promisify-node');

var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var glob = require('glob');
var template = require('gulp-template');
var crypto = require('crypto');
var gulp = require('gulp');
var ghslug = promisify(require('github-slug'));
var prettyBytes = require('pretty-bytes');
var gzipSize = require('gzip-size');

module.exports = function(config) {
  return new Promise(function(resolve, reject) {
    'use strict';

    var rootDir = config.rootDir || './';

    process.stdout.write(
      'Offlining ' + chalk.bold(rootDir) + ' to ' + chalk.bold(path.join(rootDir, 'offline-worker.js')) + '…\n'
    );

    // Ensure root directory ends with slash so we strip leading slash from paths
    // of files to cache, so they're relative paths, and the offline worker
    // will match them regardless of whether the app is deployed to a top-level
    // directory for a custom domain (f.e. https://example.com/) or a subdirectory
    // of a GitHub Pages domain (f.e. https://mykmelez.github.io/example/).
    if (rootDir.lastIndexOf('/') !== rootDir.length -1) {
      rootDir = rootDir + '/';
    }

    var fileGlobs = config.fileGlobs || ['**/*'];
    var importScripts = config.importScripts || [];

    // Remove the existing service worker, if any, so the worker doesn't include
    // itself in the list of files to cache.
    try {
      fs.unlinkSync(path.join(rootDir, 'offline-worker.js'));
    } catch (ex) {
      // Only ignore the error if it's a 'file not found' error.
      if (!ex.code || ex.code !== 'ENOENT') {
        reject(ex);
        return;
      }
    }

    ghslug('./')
    .catch(function() {
      // Use the name from package.json if there's an error while fetching the GitHub slug.
      try {
        return JSON.parse(fs.readFileSync('package.json')).name;
      } catch (ex) {
        return '';
      }
    })
    .then(function(cacheId) {
      checkImports(importScripts, rootDir);

      var absoluteGlobs = fileGlobs.map(function (v) {
        return path.join(rootDir, v);
      });
      var files = flatGlobs(absoluteGlobs);
      var filesAndHashes = getFilesAndHashes(files, rootDir, absoluteGlobs);

      var replacements = {
        cacheId: cacheId,
        cacheVersion: getHashOfHashes(pluckHashes(filesAndHashes)),
        resources: filesAndHashes,
        importScripts: importScripts,
      };

      var stream = gulp.src([__dirname + '/../templates/app/offline-worker.js'])
        .pipe(template(replacements))
        .pipe(gulp.dest(rootDir));

      return new Promise(function (resolve, reject) {
        stream.on('finish', resolve);
        stream.on('error', reject);
      });
    })
    .then(resolve, reject);

    function flatGlobs(fileGlobs) {
      return Object.keys(fileGlobs.reduce(function (matches, fileGlob) {
        fileGlob = fileGlob.replace(path.sep, '/');
        glob.sync(fileGlob, { nodir: true }).forEach(function (filepath) {
          matches[filepath] = filepath;
        });
        return matches;
      }, {}));
    }

    function getFilesAndHashes(files, rootDir, sizeWhiteList) {
      var totalSize = 0;
      var totalGzipSize = 0;
      var filesAndHashes = files.map(function (filepath) {
        var size = countFileSize(filepath, sizeWhiteList);
        totalSize += size;
        var data = fs.readFileSync(filepath);
        totalGzipSize += gzipSize.sync(data);
        var hash = getHash(data);
        process.stdout.write(chalk.green.bold('✓ ') + 'Caching ' + filepath + ' (' + prettyBytes(size) + ')\n');
        return {
          path: filepath.replace(rootDir, function (match, offset) {
            // The root must be the worker's directory
            return offset === 0 ? './' : match;
          }),
          hash: hash,
        };
      });

      process.stdout.write('Total cache size is ' + prettyBytes(totalSize) + ' (' + prettyBytes(totalGzipSize) + ' if served with gzip) for ' + files.length + ' files.\n');

      return filesAndHashes;
    }

    function checkImports(imports, rootDir) {
      imports.forEach(function (filepath) {
        try {
          assertExists(path.join(rootDir, filepath));
        } catch (ex) {
          process.stdout.write(chalk.yellow.bold(
            '⚠ ' + filepath + ' doesn\'t exist or is a directory, but you\'ve added it to importScripts.\n' +
            '  Are you sure you want to import it?\n'
          ));
        }
      });
    }

    function pluckHashes(entries) {
      return entries.map(function (entry) { return entry.hash; });
    }

    function assertExists(filepath) {
      var stat;
      try {
        stat = fs.statSync(filepath);
      } catch (ex) {
        throw ex;
      }

      if (!stat.isFile()) {
        throw new Error(filepath + ' is not a file.');
      }
    }

    function countFileSize(filepath, whiteList) {
      var stat = fs.statSync(filepath);
      if (stat.isFile() && stat.size > 2 * 1024 * 1024 && whiteList.indexOf(filepath) === -1) {
        process.stdout.write(chalk.yellow.bold(
          '⚠ ' + filepath + ' is bigger than 2 MiB. Are you sure you want to cache it?\n' +
          '  To suppress this warning, explicitly include the file in the fileGlobs list.\n'
        ));
      }
      return stat.size;
    }

    function getHashOfHashes(hashArray) {
      return getHash(new Buffer(hashArray.join(''), 'hex'));
    }

    function getHash(data) {
      var sha1 = crypto.createHash('sha1');
      sha1.update(data);
      return sha1.digest('hex');
    }
  });
};
<commit_msg>Don't needlessly prompt the user<commit_after>/**
 * Copyright 2015 Mozilla
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

// Import this first so we can use it to wrap other modules we import.
var promisify = require('promisify-node');

var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var glob = require('glob');
var template = require('gulp-template');
var crypto = require('crypto');
var gulp = require('gulp');
var ghslug = promisify(require('github-slug'));
var prettyBytes = require('pretty-bytes');
var gzipSize = require('gzip-size');

module.exports = function(config) {
  return new Promise(function(resolve, reject) {
    'use strict';

    var rootDir = config.rootDir || './';

    process.stdout.write(
      'Offlining ' + chalk.bold(rootDir) + ' to ' + chalk.bold(path.join(rootDir, 'offline-worker.js')) + '…\n'
    );

    // Ensure root directory ends with slash so we strip leading slash from paths
    // of files to cache, so they're relative paths, and the offline worker
    // will match them regardless of whether the app is deployed to a top-level
    // directory for a custom domain (f.e. https://example.com/) or a subdirectory
    // of a GitHub Pages domain (f.e. https://mykmelez.github.io/example/).
    if (rootDir.lastIndexOf('/') !== rootDir.length -1) {
      rootDir = rootDir + '/';
    }

    var fileGlobs = config.fileGlobs || ['**/*'];
    var importScripts = config.importScripts || [];

    // Remove the existing service worker, if any, so the worker doesn't include
    // itself in the list of files to cache.
    try {
      fs.unlinkSync(path.join(rootDir, 'offline-worker.js'));
    } catch (ex) {
      // Only ignore the error if it's a 'file not found' error.
      if (!ex.code || ex.code !== 'ENOENT') {
        reject(ex);
        return;
      }
    }

    ghslug('./')
    .catch(function() {
      // Use the name from package.json if there's an error while fetching the GitHub slug.
      try {
        return JSON.parse(fs.readFileSync('package.json')).name;
      } catch (ex) {
        return '';
      }
    })
    .then(function(cacheId) {
      checkImports(importScripts, rootDir);

      var absoluteGlobs = fileGlobs.map(function (v) {
        return path.join(rootDir, v);
      });
      var files = flatGlobs(absoluteGlobs);
      var filesAndHashes = getFilesAndHashes(files, rootDir, absoluteGlobs);

      var replacements = {
        cacheId: cacheId,
        cacheVersion: getHashOfHashes(pluckHashes(filesAndHashes)),
        resources: filesAndHashes,
        importScripts: importScripts,
      };

      var stream = gulp.src([__dirname + '/../templates/app/offline-worker.js'])
        .pipe(template(replacements))
        .pipe(gulp.dest(rootDir));

      return new Promise(function (resolve, reject) {
        stream.on('finish', resolve);
        stream.on('error', reject);
      });
    })
    .then(resolve, reject);

    function flatGlobs(fileGlobs) {
      return Object.keys(fileGlobs.reduce(function (matches, fileGlob) {
        fileGlob = fileGlob.replace(path.sep, '/');
        glob.sync(fileGlob, { nodir: true }).forEach(function (filepath) {
          matches[filepath] = filepath;
        });
        return matches;
      }, {}));
    }

    function getFilesAndHashes(files, rootDir, sizeWhiteList) {
      var totalSize = 0;
      var totalGzipSize = 0;
      var filesAndHashes = files.map(function (filepath) {
        var size = countFileSize(filepath, sizeWhiteList);
        totalSize += size;
        var data = fs.readFileSync(filepath);
        totalGzipSize += gzipSize.sync(data);
        var hash = getHash(data);
        process.stdout.write(chalk.green.bold('✓ ') + 'Caching ' + filepath + ' (' + prettyBytes(size) + ')\n');
        return {
          path: filepath.replace(rootDir, function (match, offset) {
            // The root must be the worker's directory
            return offset === 0 ? './' : match;
          }),
          hash: hash,
        };
      });

      process.stdout.write('Total cache size is ' + prettyBytes(totalSize) + ' (' + prettyBytes(totalGzipSize) + ' if served with gzip) for ' + files.length + ' files.\n');

      return filesAndHashes;
    }

    function checkImports(imports, rootDir) {
      imports.forEach(function (filepath) {
        try {
          assertExists(path.join(rootDir, filepath));
        } catch (ex) {
          process.stdout.write(chalk.yellow.bold(
            '⚠ ' + filepath + ' doesn\'t exist or is a directory, but you\'ve added it to importScripts.\n' +
            '  To fix this problem, ensure file/path is a file or remove it from importScripts.\n'
          ));
        }
      });
    }

    function pluckHashes(entries) {
      return entries.map(function (entry) { return entry.hash; });
    }

    function assertExists(filepath) {
      var stat;
      try {
        stat = fs.statSync(filepath);
      } catch (ex) {
        throw ex;
      }

      if (!stat.isFile()) {
        throw new Error(filepath + ' is not a file.');
      }
    }

    function countFileSize(filepath, whiteList) {
      var stat = fs.statSync(filepath);
      if (stat.isFile() && stat.size > 2 * 1024 * 1024 && whiteList.indexOf(filepath) === -1) {
        process.stdout.write(chalk.yellow.bold(
          '⚠ ' + filepath + ' is bigger than 2 MiB. Are you sure you want to cache it?\n' +
          '  To suppress this warning, explicitly include the file in the fileGlobs list.\n'
        ));
      }
      return stat.size;
    }

    function getHashOfHashes(hashArray) {
      return getHash(new Buffer(hashArray.join(''), 'hex'));
    }

    function getHash(data) {
      var sha1 = crypto.createHash('sha1');
      sha1.update(data);
      return sha1.digest('hex');
    }
  });
};
<|endoftext|>


<commit_before>var _os = require('os');

exports.platform = function(){
    return process.platform;
}

exports.cpuCount = function(){
    return _os.cpus().length;
}

exports.sysUptime = function(){
    //seconds
    return _os.uptime();
}

exports.processUptime = function(){
    //seconds
    return process.uptime();
}

// Memory
exports.freemem = function(){
    return _os.freemem() / ( 1024 * 1024 );
}

exports.totalmem = function(){

    return _os.totalmem() / ( 1024 * 1024 );
}

exports.freememPercentage = function(){
    return _os.freemem() / _os.totalmem();
}

// Hard Disk Drive
exports.harddrive = function(callback){

    require('child_process').exec('df -k', function(error, stdout, stderr) {

        var total = 0;
        var used = 0;
        var free = 0;

        var lines = stdout.split("\n");

        var str_disk_info = lines[1].replace( /[\s\n\r]+/g,' ');

        var disk_info = str_disk_info.split(' ');

        total = Math.ceil((disk_info[1] * 1024)/ Math.pow(1024,2));
        used = Math.ceil(disk_info[2] * 1024 / Math.pow(1024,2)) ;
        free = Math.ceil(disk_info[3] * 1024 / Math.pow(1024,2)) ;

        callback(total, free, used);
    });
}

// Return process running current
exports.getProcesses = function(nProcess, callback){

    // if nprocess is undefined then is function
    if(typeof nProcess === 'function'){

        callback =nProcess;
        nProcess = 0
    }

    command = 'ps aux | head -n '+ 11
    if (nProcess > 0)
        command = 'ps aux | head -n '+ (nProcess + 1)

    require('child_process').exec(command, function(error, stdout, stderr) {

        var that = this

        var lines = stdout.split("\n");
        lines.pop()

        var result = ''

        lines.forEach(function(_item,_i){

            if (_i > 0){
                var _str = _item.replace( /[\s\n\r]+/g,' ');

                _str = _str.split(' ')

                result += _str[10]+" "+_str[9]+" "+_str[2]+" "+_str[3]+"\n";
            }

        });
        callback(result);
    });
}

/*
* Returns All the load average usage for 1, 5 or 15 minutes.
*/
exports.allLoadavg = function(){

    var loads = _os.loadavg();

    return loads[0].toFixed(4)+','+loads[1].toFixed(4)+','+loads[2].toFixed(4);
}

/*
* Returns the load average usage for 1, 5 or 15 minutes.
*/
exports.loadavg = function(_time){

    if(_time === undefined || (_time !== 5 && _time !== 15) ) _time = 1;

    var loads = _os.loadavg();
    var v = 0;
    if(_time == 1) v = loads[0];
    if(_time == 5) v = loads[1];
    if(_time == 15) v = loads[2];

    return v;
}

exports.cpuFree = function(callback){
    getCPUUsage(callback, true);
}

exports.cpuUsage = function(callback){
    getCPUUsage(callback, false);
}

function getCPUUsage(callback, free){

    var stats1 = getCPUInfo();
    var startIdle = stats1.idle;
    var startTotal = stats1.total;

    setTimeout(function() {
        var stats2 = getCPUInfo();
        var endIdle = stats2.idle;
        var endTotal = stats2.total;

        var idle 	= endIdle - startIdle;
        var total 	= endTotal - startTotal;
        var perc	= idle / total;

        if(free === true)
            callback( perc );
        else
            callback( (1 - perc) );

    }, 1000 );
}

function getCPUInfo(callback){
    var cpus = _os.cpus();

    var user = 0;
    var nice = 0;
    var sys = 0;
    var idle = 0;
    var irq = 0;
    var total = 0;

    for(var cpu in cpus){

        user += cpus[cpu].times.user;
        nice += cpus[cpu].times.nice;
        sys += cpus[cpu].times.sys;
        irq += cpus[cpu].times.irq;
        idle += cpus[cpu].times.idle;
    }

    var total = user + nice + sys + idle + irq;

    return {
        'idle': idle,
        'total': total
    };
}

<commit_msg>new methods and updates<commit_after>var _os = require('os');

exports.platform = function(){
    return process.platform;
}

exports.cpuCount = function(){
    return _os.cpus().length;
}

exports.sysUptime = function(){
    //seconds
    return _os.uptime();
}

exports.processUptime = function(){
    //seconds
    return process.uptime();
}

// Memory
exports.freemem = function(){
    return _os.freemem() / ( 1024 * 1024 );
}

exports.totalmem = function(){

    return _os.totalmem() / ( 1024 * 1024 );
}

exports.freememPercentage = function(){
    return _os.freemem() / _os.totalmem();
}

exports.freeCommand = function(callback){

    // Only Linux
    require('child_process').exec('free -m', function(error, stdout, stderr) {

       var lines = stdout.split("\n");

       var str_mem_info = lines[1].replace( /[\s\n\r]+/g,' ');

       var mem_info = str_mem_info.split(' ')

       total_mem    = parseFloat(mem_info[1])
       free_mem     = parseFloat(mem_info[3])
       buffers_mem  = parseFloat(mem_info[5])
       cached_mem   = parseFloat(mem_info[6])

       used_mem = total_mem - (free_mem + buffers_mem + cached_mem)

       callback(used_mem -2);
    });
}

// Hard Disk Drive
exports.harddrive = function(callback){

    require('child_process').exec('df -k', function(error, stdout, stderr) {

        var total = 0;
        var used = 0;
        var free = 0;

        var lines = stdout.split("\n");

        var str_disk_info = lines[1].replace( /[\s\n\r]+/g,' ');

        var disk_info = str_disk_info.split(' ');

        total = Math.ceil((disk_info[1] * 1024)/ Math.pow(1024,2));
        used = Math.ceil(disk_info[2] * 1024 / Math.pow(1024,2)) ;
        free = Math.ceil(disk_info[3] * 1024 / Math.pow(1024,2)) ;

        callback(total, free, used);
    });
}

// Return process running current
exports.getProcesses = function(nProcess, callback){

    // if nprocess is undefined then is function
    if(typeof nProcess === 'function'){

        callback =nProcess;
        nProcess = 0
    }

    command = 'ps -eo pcpu,pmem,time,args | sort -k 1 -r | head -n'+10
    //command = 'ps aux | head -n '+ 11
    //command = 'ps aux | head -n '+ (nProcess + 1)
    if (nProcess > 0)
        command = 'ps -eo pcpu,pmem,time,args | sort -k 1 -r | head -n'+(nProcess + 1)

    require('child_process').exec(command, function(error, stdout, stderr) {

        var that = this

        var lines = stdout.split("\n");
        lines.shift()
        lines.pop()

        var result = ''

        lines.forEach(function(_item,_i){

            var _str = _item.replace( /[\s\n\r]+/g,' ');

            _str = _str.split(' ')

            // result += _str[10]+" "+_str[9]+" "+_str[2]+" "+_str[3]+"\n";  // process
            result += _str[1]+" "+_str[2]+" "+_str[3]+" "+_str[4].substring((_str[4].length - 25))+"\n";  // process

        });

        callback(result);
    });
}

/*
* Returns All the load average usage for 1, 5 or 15 minutes.
*/
exports.allLoadavg = function(){

    var loads = _os.loadavg();

    return loads[0].toFixed(4)+','+loads[1].toFixed(4)+','+loads[2].toFixed(4);
}

/*
* Returns the load average usage for 1, 5 or 15 minutes.
*/
exports.loadavg = function(_time){

    if(_time === undefined || (_time !== 5 && _time !== 15) ) _time = 1;

    var loads = _os.loadavg();
    var v = 0;
    if(_time == 1) v = loads[0];
    if(_time == 5) v = loads[1];
    if(_time == 15) v = loads[2];

    return v;
}

exports.cpuFree = function(callback){
    getCPUUsage(callback, true);
}

exports.cpuUsage = function(callback){
    getCPUUsage(callback, false);
}

function getCPUUsage(callback, free){

    var stats1 = getCPUInfo();
    var startIdle = stats1.idle;
    var startTotal = stats1.total;

    setTimeout(function() {
        var stats2 = getCPUInfo();
        var endIdle = stats2.idle;
        var endTotal = stats2.total;

        var idle 	= endIdle - startIdle;
        var total 	= endTotal - startTotal;
        var perc	= idle / total;

        if(free === true)
            callback( perc );
        else
            callback( (1 - perc) );

    }, 1000 );
}

function getCPUInfo(callback){
    var cpus = _os.cpus();

    var user = 0;
    var nice = 0;
    var sys = 0;
    var idle = 0;
    var irq = 0;
    var total = 0;

    for(var cpu in cpus){

        user += cpus[cpu].times.user;
        nice += cpus[cpu].times.nice;
        sys += cpus[cpu].times.sys;
        irq += cpus[cpu].times.irq;
        idle += cpus[cpu].times.idle;
    }

    var total = user + nice + sys + idle + irq;

    return {
        'idle': idle,
        'total': total
    };
}

<|endoftext|>


<commit_before>var fs = require('fs'),
    path = require('path'),
    util = require('util')

var CleanCSS = require('clean-css')

var cssTemplate = 'define("%s", [], function() { seajs.importStyle(%s); });'
var tplTemplate = 'define("%s", [], function() { return %s });'

module.exports = {
    '.js': function(meta) {
        var source = null
        if (fs.existsSync(meta.uri)) {
            source = fs.readFileSync(meta.uri).toString()
        }
        return source
    },
    '.css': function(meta) {
        var source = null
        if (fs.existsSync(meta.uri)) {
            var source = fs.readFileSync(meta.uri).toString()
            source = new CleanCSS().minify(source).styles
            source = util.format(cssTemplate, meta.id, JSON.stringify(source))
        }
        return source
    },
    '.tpl': function(meta) {
        var source = null
        if (fs.existsSync(meta.uri)) {
            source = fs.readFileSync(meta.uri).toString()
            source = util.format(tplTemplate, meta.id, JSON.stringify(source))
        }
        return source
    }
}<commit_msg>review parsers.js => .css<commit_after>var fs = require('fs'),
    path = require('path'),
    util = require('util')

var CleanCSS = require('clean-css')

var cssTemplate = 'define("%s", [], function() { seajs.importStyle(%s); });'
var tplTemplate = 'define("%s", [], function() { return %s });'

module.exports = {
    '.js': function(meta) {
        var source = null
        if (fs.existsSync(meta.uri)) {
            source = fs.readFileSync(meta.uri).toString()
        }
        return source
    },
    '.css': function(meta) {
        var source = null
        if (fs.existsSync(meta.uri)) {
            source = fs.readFileSync(meta.uri).toString()
            source = new CleanCSS().minify(source).styles
            source = util.format(cssTemplate, meta.id, JSON.stringify(source))
        }
        return source
    },
    '.tpl': function(meta) {
        var source = null
        if (fs.existsSync(meta.uri)) {
            source = fs.readFileSync(meta.uri).toString()
            source = util.format(tplTemplate, meta.id, JSON.stringify(source))
        }
        return source
    }
}<|endoftext|>


<commit_before>"use strict";

var parseURL = require("url").parse;
var utils = require("./utils");

function PDFDATA (key) {
    if (!(this instanceof PDFDATA)) {
        return new PDFDATA(key);
    }

    if (!key || typeof key != 'string') {
        throw new Error("PDFDATA.io API key must be provided when creating an API client instance, " +
                        "e.g. `require(\"pdfdata\")(\"API_KEY\")`");
    }

    this._api = {resolveWithFullResponse:true,
                 simple:false};
    this.setApiKey(key);
    this.setEndpoint(PDFDATA.DEFAULT_ENDPOINT);
    this._setup(["documents", "procs", "resources"]);
}

PDFDATA.DEFAULT_ENDPOINT = "https://api.pdfdata.io/v1/";

PDFDATA.prototype = {
    setApiKey: function (key) {
        this._api.auth = {"username": key};
    },
    setEndpoint: function (url) {
        this._api.url = url;
        var url = parseURL(url);
        if (url.hostname != "api.pdfdata.io") {
            this._api.rejectUnauthorized = false;
            this._api.headers = {"Host": "api.pdfdata.io:" + (url.port || 443)};
        } else {
            delete this._api.rejectUnauthorized;
            this._api.headers = {};
        }
    },

    info: function () {
        var options = utils.requestOptions(this, "/", {method: "GET"});
        return utils.request(options).then(utils.return_body);
    },

    // this "proxies" all "resource" objects' functions so that `this` is
    // set to this pdfdata object
    _setup: function (resources) {
        var pdfdata = this;
        for (var i in resources) {
            var name = resources[i];
            var methods = require("./" + name);
            for (var mname in methods) {
                methods[mname] = utils.proxy(pdfdata, methods[mname]);
            }
            this[name] = methods;
        }

    }

}

module.exports = PDFDATA;
<commit_msg>fix default endpoint URL<commit_after>"use strict";

var parseURL = require("url").parse;
var utils = require("./utils");

function PDFDATA (key) {
    if (!(this instanceof PDFDATA)) {
        return new PDFDATA(key);
    }

    if (!key || typeof key != 'string') {
        throw new Error("PDFDATA.io API key must be provided when creating an API client instance, " +
                        "e.g. `require(\"pdfdata\")(\"API_KEY\")`");
    }

    this._api = {resolveWithFullResponse:true,
                 simple:false};
    this.setApiKey(key);
    this.setEndpoint(PDFDATA.DEFAULT_ENDPOINT);
    this._setup(["documents", "procs", "resources"]);
}

PDFDATA.DEFAULT_ENDPOINT = "https://api.pdfdata.io/v1";

PDFDATA.prototype = {
    setApiKey: function (key) {
        this._api.auth = {"username": key};
    },
    setEndpoint: function (url) {
        this._api.url = url;
        var url = parseURL(url);
        if (url.hostname != "api.pdfdata.io") {
            this._api.rejectUnauthorized = false;
            this._api.headers = {"Host": "api.pdfdata.io:" + (url.port || 443)};
        } else {
            delete this._api.rejectUnauthorized;
            this._api.headers = {};
        }
    },

    info: function () {
        var options = utils.requestOptions(this, "/", {method: "GET"});
        return utils.request(options).then(utils.return_body);
    },

    // this "proxies" all "resource" objects' functions so that `this` is
    // set to this pdfdata object
    _setup: function (resources) {
        var pdfdata = this;
        for (var i in resources) {
            var name = resources[i];
            var methods = require("./" + name);
            for (var mname in methods) {
                methods[mname] = utils.proxy(pdfdata, methods[mname]);
            }
            this[name] = methods;
        }

    }

}

module.exports = PDFDATA;
<|endoftext|>


<commit_before>var _ = require('lodash'),
  async = require('async'),
  colors = require('colors'),
  fs = require('fs'),
  req = require('bagofrequest'),
  semver = require('semver'),
  util = require('util');

/**
 * class PkjUtil
 *
 * @param {String} file: path to package.json file
 */
function PkjUtil(file) {
  this.file = file || 'package.json';
}

/**
 * List dependencies.
 *
 * @param {String} type: dependencies type (dependencies or devDependencies), all when type is not provided
 * @param {Function} cb: standard cb(err, result) callback
 */
PkjUtil.prototype.listDependencies = function (type, cb) {
  fs.readFile(this.file, function (err, data) {
    var pkg = JSON.parse(data),
      types = type ? [type] : ['dependencies', 'devDependencies'],
      modules = [];
    types.forEach(function (type) {
      modules = modules.concat(Object.keys(pkg[type]));
    });
    modules = _.uniq(modules);
    cb(err, modules);
  });
};

/**
 * Sort dependencies in alphabetical order.
 *
 * @param {String} type: dependencies type (dependencies or devDependencies), all when type is not provided
 * @param {Function} cb: standard cb(err, result) callback
 */
PkjUtil.prototype.sortDependencies = function (type, cb) {

  function process(pkg, cb) {
    var types = type ? [type] : ['dependencies', 'devDependencies'],
      modules = [];
    types.forEach(function (type) {
      var sorted = [];
      Object.keys(pkg[type]).sort().forEach(function (module) {
        sorted.push(util.format('"%s": "%s"', module, pkg[type][module]));
      });
      pkg[type] = JSON.parse(util.format('{%s}', sorted.join(',')));
    });
    cb(null, pkg);
  }

  this._updatePackage(process, cb);
};

/**
 * Upgrade version based on type.
 *
 * @param {String} type: version type (major, minor, patch), default to patch
 * @param {Function} cb: standard cb(err, result) callback
 */
PkjUtil.prototype.upgradeVersion = function (type, cb) {

  function process(pkg, cb) {
    var version = semver.parse(pkg.version);
    if (type === 'major') {
      version.major += 1;
      version.minor = 0;
      version.patch = 0;
    } else if (type === 'minor') {
      version.minor += 1;
      version.patch = 0;
    } else {
      version.patch += 1;
    }
    pkg.version = version.format();
    cb(null, pkg);
  }

  this._updatePackage(process, cb);
};

/**
 * Upgrade the version of each dependency to the latest published
 * in the registry.
 * Version range will be kept, e.g. ~0.0.1 is upgraded to ~0.0.18 .
 * If version does not contain numeric major.minor.patch, then
 * version is upgraded to latest, e.g. 0.0.x is upgraded to 0.0.18 .
 *
 * @param {Object} opts: optional
 *   - registry: npm registry, default to public npm registry
 * @param {Function} cb: standard cb(err, result) callback
 */
PkjUtil.prototype.upgradeDependencies = function (opts, cb) {
  const LIMIT = 5;

  opts = opts || {};
  opts.registry = opts.registry || 'https://registry.npmjs.org/';
  if (!opts.registry.match(/\/$/)) {
    opts.registry += '/';
  }

  function process(pkg, cb) {
    function iterator(dependency, cb) {
      var requestOpts = {
        handlers: {
          200: function (result, cb) {

            var latestVersion = JSON.parse(result.body).version;

            function update(type) {
              var dependencies = pkg[type],
                currVersionMatch = dependencies[dependency].match(/[0-9]+\.[0-9]+\.[0-9]+/),
                currVersion = currVersionMatch ? currVersionMatch.toString() : null,
                range = currVersionMatch ? dependencies[dependency].replace(/[0-9]+\.[0-9]+\.[0-9]+.*$/, '') : null;

              if (currVersion != latestVersion) {
                dependencies[dependency] = (range || '') + latestVersion;
                console.log('%s - %s - upgraded to %s', dependency.green, type, dependencies[dependency]);
              } else {
                console.log('%s - %s - already latest %s', dependency.grey, type, dependencies[dependency]);
              }
            }

            ['dependencies', 'devDependencies', 'peerDependencies'].forEach(function (type) {
              if (pkg[type] && pkg[type][dependency]) {
                update(type);
              }
            });
            cb();
          }
        }
      };
      req.request('get', opts.registry + dependency + '/latest', requestOpts, cb);
    }

    var dependencies = _.union(_.keys(pkg.dependencies), _.keys(pkg.devDependencies), _.keys(pkg.peerDependencies));
    async.eachLimit(dependencies, LIMIT, iterator, function (err) {
      cb(err, pkg);
    });
  }

  this._updatePackage(process, cb);
};

/**
 * Read package.json file, apply process function, then overwrite the file.
 *
 * @param {Function} process: process function to be applied to parsed package.json
 * @param {Function} cb: standard cb(err, result) callback
 */
PkjUtil.prototype._updatePackage = function (process, cb) {
  var self = this;
  fs.readFile(this.file, function (err, data) {
    if (err) {
      cb(err);
    } else {
      var pkg = JSON.parse(data);
      process(pkg, function (err, result) {
        if (err) {
          cb(err, result);
        } else {
          fs.writeFile(self.file, JSON.stringify(result, null, 2), function (err) {
            cb(err, result);
          });
        }
      });
    }
  });
};

module.exports = PkjUtil;<commit_msg>Modify dependencies processing steps to use the same types array.<commit_after>var _ = require('lodash'),
  async = require('async'),
  colors = require('colors'),
  fs = require('fs'),
  req = require('bagofrequest'),
  semver = require('semver'),
  util = require('util');

/**
 * class PkjUtil
 *
 * @param {String} file: path to package.json file
 */
function PkjUtil(file) {
  this.file = file || 'package.json';
}

/**
 * List dependencies.
 *
 * @param {String} type: dependencies type (dependencies or devDependencies), all when type is not provided
 * @param {Function} cb: standard cb(err, result) callback
 */
PkjUtil.prototype.listDependencies = function (type, cb) {
  fs.readFile(this.file, function (err, data) {
    var pkg = JSON.parse(data),
      types = type ? [type] : ['dependencies', 'devDependencies'],
      modules = [];
    types.forEach(function (type) {
      modules = modules.concat(Object.keys(pkg[type]));
    });
    modules = _.uniq(modules);
    cb(err, modules);
  });
};

/**
 * Sort dependencies in alphabetical order.
 *
 * @param {String} type: dependencies type (dependencies or devDependencies), all when type is not provided
 * @param {Function} cb: standard cb(err, result) callback
 */
PkjUtil.prototype.sortDependencies = function (type, cb) {

  function process(pkg, cb) {
    var types = type ? [type] : ['dependencies', 'devDependencies'],
      modules = [];
    types.forEach(function (type) {
      var sorted = [];
      Object.keys(pkg[type]).sort().forEach(function (module) {
        sorted.push(util.format('"%s": "%s"', module, pkg[type][module]));
      });
      pkg[type] = JSON.parse(util.format('{%s}', sorted.join(',')));
    });
    cb(null, pkg);
  }

  this._updatePackage(process, cb);
};

/**
 * Upgrade version based on type.
 *
 * @param {String} type: version type (major, minor, patch), default to patch
 * @param {Function} cb: standard cb(err, result) callback
 */
PkjUtil.prototype.upgradeVersion = function (type, cb) {

  function process(pkg, cb) {
    var version = semver.parse(pkg.version);
    if (type === 'major') {
      version.major += 1;
      version.minor = 0;
      version.patch = 0;
    } else if (type === 'minor') {
      version.minor += 1;
      version.patch = 0;
    } else {
      version.patch += 1;
    }
    pkg.version = version.format();
    cb(null, pkg);
  }

  this._updatePackage(process, cb);
};

/**
 * Upgrade the version of each dependency to the latest published
 * in the registry.
 * Version range will be kept, e.g. ~0.0.1 is upgraded to ~0.0.18 .
 * If version does not contain numeric major.minor.patch, then
 * version is upgraded to latest, e.g. 0.0.x is upgraded to 0.0.18 .
 *
 * @param {Object} opts: optional
 *   - registry: npm registry, default to public npm registry
 * @param {Function} cb: standard cb(err, result) callback
 */
PkjUtil.prototype.upgradeDependencies = function (opts, cb) {
  const LIMIT = 5;
  const DEPENDENCY_TYPES = ['dependencies', 'devDependencies', 'peerDependencies'];

  opts = opts || {};
  opts.registry = opts.registry || 'https://registry.npmjs.org/';
  if (!opts.registry.match(/\/$/)) {
    opts.registry += '/';
  }

  function process(pkg, cb) {
    function iterator(dependency, cb) {
      var requestOpts = {
        handlers: {
          200: function (result, cb) {

            var latestVersion = JSON.parse(result.body).version;

            function update(type) {
              var dependencies = pkg[type],
                currVersionMatch = dependencies[dependency].match(/[0-9]+\.[0-9]+\.[0-9]+/),
                currVersion = currVersionMatch ? currVersionMatch.toString() : null,
                range = currVersionMatch ? dependencies[dependency].replace(/[0-9]+\.[0-9]+\.[0-9]+.*$/, '') : null;

              if (currVersion != latestVersion) {
                dependencies[dependency] = (range || '') + latestVersion;
                console.log('%s - %s - upgraded to %s', dependency.green, type, dependencies[dependency]);
              } else {
                console.log('%s - %s - already latest %s', dependency.grey, type, dependencies[dependency]);
              }
            }

            DEPENDENCY_TYPES.forEach(function (type) {
              if (pkg[type] && pkg[type][dependency]) {
                update(type);
              }
            });
            cb();
          }
        }
      };
      req.request('get', opts.registry + dependency + '/latest', requestOpts, cb);
    }

    var dependencies = [];
    DEPENDENCY_TYPES.forEach(function (type) {
      dependencies = _.union(dependencies, _.keys(pkg[type]));
    });

    async.eachLimit(dependencies, LIMIT, iterator, function (err) {
      cb(err, pkg);
    });
  }

  this._updatePackage(process, cb);
};

/**
 * Read package.json file, apply process function, then overwrite the file.
 *
 * @param {Function} process: process function to be applied to parsed package.json
 * @param {Function} cb: standard cb(err, result) callback
 */
PkjUtil.prototype._updatePackage = function (process, cb) {
  var self = this;
  fs.readFile(this.file, function (err, data) {
    if (err) {
      cb(err);
    } else {
      var pkg = JSON.parse(data);
      process(pkg, function (err, result) {
        if (err) {
          cb(err, result);
        } else {
          fs.writeFile(self.file, JSON.stringify(result, null, 2), function (err) {
            cb(err, result);
          });
        }
      });
    }
  });
};

module.exports = PkjUtil;<|endoftext|>


<commit_before>/**
 * Adleria bindings for nodejs
 *
 * (c) 2015 Optimal Bits Sweden AB.
 *
 */
/// <reference path="../typings/node/node.d.ts"/>
"use strict";

var Promise = require("bluebird");
var request = require("request-promise");
var _ = require("lodash");
var urlJoin = require("url-join");

var requestAsync = Promise.promisify(request);

module.exports = function (auth, baseUrl, opts) {
  var playersUrl = urlJoin(baseUrl, "players");

  return {
    campaigns: function (auth, playerId) {
      const url = urlJoin(playersUrl, playerId, "campaigns");
      console.log({ url });
      return requestAsync({
        method: "GET",
        url: url,
        auth: auth,
        json: true,
      });
    },
    impressions: function (auth, playerId) {
      var url = urlJoin(playersUrl, playerId, "impressions");
      return {
        add: function (campaigns) {
          if (_.isEmpty(campaigns)) {
            return Promise.resolve([{ statusCode: 400 }]);
          }
          return Promise.all(
            Object.keys(campaigns).map(function (campaignId) {
              var impressions = campaigns[campaignId];

              impressions.campaignId = campaignId;
              console.log("MAPPING:", impressions);
              return requestAsync({
                method: "PUT",
                url: url,
                auth: auth,
                json: true,
                body: impressions,
              });
            })
          ).then(function (results) {
            var invalid = _.find(results, function (result) {
              if (result[0].statusCode !== 200) {
                return result;
              }
            });
            return invalid ? invalid : results[0];
          });
        },
      };
    },
  };
};
<commit_msg>chore(players): remove console.log<commit_after>/**
 * Adleria bindings for nodejs
 *
 * (c) 2015 Optimal Bits Sweden AB.
 *
 */
/// <reference path="../typings/node/node.d.ts"/>
"use strict";

var Promise = require("bluebird");
var request = require("request-promise");
var _ = require("lodash");
var urlJoin = require("url-join");

var requestAsync = Promise.promisify(request);

module.exports = function (auth, baseUrl, opts) {
  var playersUrl = urlJoin(baseUrl, "players");

  return {
    campaigns: function (auth, playerId) {
      const url = urlJoin(playersUrl, playerId, "campaigns");
      return requestAsync({
        method: "GET",
        url: url,
        auth: auth,
        json: true,
      });
    },
    impressions: function (auth, playerId) {
      var url = urlJoin(playersUrl, playerId, "impressions");
      return {
        add: function (campaigns) {
          if (_.isEmpty(campaigns)) {
            return Promise.resolve([{ statusCode: 400 }]);
          }
          return Promise.all(
            Object.keys(campaigns).map(function (campaignId) {
              var impressions = campaigns[campaignId];

              impressions.campaignId = campaignId;
              console.log("MAPPING:", impressions);
              return requestAsync({
                method: "PUT",
                url: url,
                auth: auth,
                json: true,
                body: impressions,
              });
            })
          ).then(function (results) {
            var invalid = _.find(results, function (result) {
              if (result[0].statusCode !== 200) {
                return result;
              }
            });
            return invalid ? invalid : results[0];
          });
        },
      };
    },
  };
};
<|endoftext|>