tr-apisrv 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +71 -1
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,12 +1,24 @@
1
1
  'use strict';
2
2
 
3
3
  const http = require('http');
4
+ const https = require('https');
4
5
  const qs = require('querystring')
5
6
 
6
7
  var ApiSrv = function(opts) {
8
+ var proto = http;
9
+ var srvOpts = {};
7
10
  if (! (opts && (typeof(opts) === 'object'))) {
8
11
  throw new Error('Bad opts for ApiSrv constructor');
9
12
  }
13
+ if (opts.key && opts.cert) {
14
+ srvOpts.key = opts.key;
15
+ srvOpts.cert = opts.cert;
16
+ proto = https;
17
+ } else if (opts.key) {
18
+ throw new Error('Key defined without cert');
19
+ } else if (opts.cert) {
20
+ throw new Error('Cert defined without key');
21
+ }
10
22
  this.debug = opts.debug ? true : false;
11
23
  this.prettyPrintJsonResponses = opts.prettyPrintJsonResponses ? true : false;
12
24
  if (Number.isSafeInteger(opts.port) && (opts.port > 0) && (opts.port < 65536)) {
@@ -36,6 +48,15 @@ var ApiSrv = function(opts) {
36
48
  }
37
49
  this.authCallback = function(r) { return true; }.bind(this);
38
50
  }
51
+ if ((opts.upgradeCallback !== undefined) && (opts.upgradeCallback !== null)) {
52
+ if (typeof(opts.upgradeCallback) === 'function') {
53
+ this.upgradeCallback = opts.upgradeCallback;
54
+ } else {
55
+ throw new Error('Bad upgradeCallback for ApiSrv constructor');
56
+ }
57
+ } else {
58
+ this.upgradeCallback = undefined
59
+ }
39
60
  if ((opts.bodyReadTimeoutMs !== undefined) && (opts.bodyReadTimeoutMs !== null)) {
40
61
  if (Number.isSafeInteger(opts.bodyReadTimeoutMs) && (opts.bodyReadTimeoutMs > 0)) {
41
62
  this.bodyReadTimeoutMs = opts.bodyReadTimeoutMs;
@@ -45,6 +66,52 @@ var ApiSrv = function(opts) {
45
66
  } else {
46
67
  this.bodyReadTimeoutMs = 2 * 1000;
47
68
  }
69
+ srvOpts.requestTimeout = this.bodyReadTimeoutMs;
70
+ var upgradeCb = function(req, s, head) {
71
+ var m, r = {};
72
+ r.method = req.method;
73
+ if (m = req.url.match(/^([^\?]*)\?(.*)$/)) {
74
+ r.url = m[1];
75
+ if (! (r.params = qs.parse(m[2].toString('utf8')))) {
76
+ s.end();
77
+ return;
78
+ }
79
+ } else {
80
+ r.url = req.url;
81
+ r.params = {};
82
+ }
83
+ r.headers = req.headers;
84
+ r.req = req;
85
+ return (Promise.resolve(this.authCallback(r))
86
+ .then(function(ret) {
87
+ if (ret) {
88
+ r.head = head;
89
+ r.s = s;
90
+ return opts.upgradeCallback(r);
91
+ } else {
92
+ s.destroy();
93
+ s = undefined;
94
+ }
95
+ }.bind(this))
96
+ .then(function(ret) {
97
+ if (ret !== false) {
98
+ if (this.debug) {
99
+ console.log('Upgrade successfully processed (resource :' + r.url + ').');
100
+ }
101
+ }
102
+ }.bind(this))
103
+ .catch(function(e) {
104
+ if (s) {
105
+ try {
106
+ s.destroy();
107
+ s = undefined;
108
+ } catch(ignored) {
109
+ s = undefined;
110
+ }
111
+ return false;
112
+ }
113
+ }.bind(this)));
114
+ }.bind(this);
48
115
  var requestCb = function(req, res) {
49
116
  var completed = false, body = Buffer.alloc(0), r = {}, timeout;
50
117
  var contentType, contentTypeArgs;
@@ -220,7 +287,10 @@ var ApiSrv = function(opts) {
220
287
  }
221
288
  res.end();
222
289
  }.bind(this);
223
- this.server = http.createServer(requestCb);
290
+ this.server = proto.createServer(srvOpts, requestCb);
291
+ if (this.upgradeCallback) {
292
+ this.server.on('upgrade', upgradeCb);
293
+ }
224
294
  this.server.on('error', function(e) {
225
295
  console.log('Unable to start HTTP server');
226
296
  process.exit(1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tr-apisrv",
3
- "version": "0.0.1",
3
+ "version": "0.1.0",
4
4
  "description": "A simple asynchronous API server class",
5
5
  "main": "index.js",
6
6
  "scripts": {