123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- var net = require('net'),
- crypto = require('crypto'),
- format = require('util').format,
- fs = require('fs');
- var nl = '\r\n';
- function GNTP(type, opts) {
- opts = opts || {};
- this.type = type;
- this.host = opts.host || 'localhost';
- this.port = opts.port || 23053;
- this.request = 'GNTP/1.0 ' + type + ' NONE' + nl;
- this.resources = [];
- this.attempts = 0;
- this.maxAttempts = 5;
- }
- GNTP.prototype.parseResp = function(resp) {
- var parsed = {}, head, body;
- resp = resp.slice(0, resp.indexOf(nl + nl)).split(nl);
- head = resp[0];
- body = resp.slice(1);
- parsed.state = head.match(/-(OK|ERROR|CALLBACK)/)[0].slice(1);
- body.forEach(function(ln) {
- ln = ln.split(': ');
- parsed[ln[0]] = ln[1];
- });
- return parsed;
- };
- GNTP.prototype.retry = function() {
- var self = this,
- args = arguments;
- setTimeout(function() {
- self.send.apply(self, args);
- }, 750);
- };
- GNTP.prototype.addResource = function(file) {
- var id = crypto.createHash('md5').update(file).digest('hex'),
- header = 'Identifier: ' + id + nl + 'Length: ' + file.length + nl + nl;
- this.resources.push({ header: header, file: file });
- return 'x-growl-resource://' + id;
- };
- GNTP.prototype.add = function(name, val) {
- if (val === undefined)
- return;
-
- if (/-Icon/.test(name) && !/^https?:\/\//.test(val) ) {
- if (/\.(png|gif|jpe?g)$/.test(val))
- val = this.addResource(fs.readFileSync(val));
- else if (val instanceof Buffer)
- val = this.addResource(val);
- }
- this.request += name + ': ' + val + nl;
- };
- GNTP.prototype.newline = function() {
- this.request += nl;
- };
- GNTP.prototype.send = function(callback) {
- var self = this,
- socket = net.connect(this.port, this.host),
- resp = '';
- callback = callback || function() {};
- this.attempts += 1;
- socket.on('connect', function() {
- socket.write(self.request);
- self.resources.forEach(function(res) {
- socket.write(res.header);
- socket.write(res.file);
- socket.write(nl + nl);
- });
- });
- socket.on('data', function(data) {
- resp += data.toString();
-
- if (resp.slice(resp.length - 4) !== (nl + nl)) return;
- resp = self.parseResp(resp);
-
- if (resp.state === 'ERROR' || resp.state === 'CALLBACK')
- socket.end();
- else
- resp = '';
- });
- socket.on('end', function() {
-
- if (['200', '401', '402'].indexOf(resp['Error-Code']) >= 0) {
- if (self.attempts <= self.maxAttempts) {
- self.retry(callback);
- } else {
- var msg = 'GNTP request to "%s:%d" failed with error code %s (%s)';
- callback(new Error(format(msg, self.host, self.port, resp['Error-Code'], resp['Error-Description'])));
- }
- } else {
- callback(undefined, resp);
- }
- });
- socket.on('error', function() {
- callback(new Error(format('Error while sending GNTP request to "%s:%d"', self.host, self.port)));
- socket.destroy();
- });
- };
- module.exports = GNTP;
|