tr-apisrv 0.0.2 → 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 +14 -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)) {
@@ -54,6 +66,7 @@ var ApiSrv = function(opts) {
54
66
  } else {
55
67
  this.bodyReadTimeoutMs = 2 * 1000;
56
68
  }
69
+ srvOpts.requestTimeout = this.bodyReadTimeoutMs;
57
70
  var upgradeCb = function(req, s, head) {
58
71
  var m, r = {};
59
72
  r.method = req.method;
@@ -274,7 +287,7 @@ var ApiSrv = function(opts) {
274
287
  }
275
288
  res.end();
276
289
  }.bind(this);
277
- this.server = http.createServer(requestCb);
290
+ this.server = proto.createServer(srvOpts, requestCb);
278
291
  if (this.upgradeCallback) {
279
292
  this.server.on('upgrade', upgradeCb);
280
293
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tr-apisrv",
3
- "version": "0.0.2",
3
+ "version": "0.1.0",
4
4
  "description": "A simple asynchronous API server class",
5
5
  "main": "index.js",
6
6
  "scripts": {