profoundjs-swagger-stats 2.0.3 → 2.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.
package/lib/swsHapi.js CHANGED
@@ -9,6 +9,7 @@ const debug = require('debug')('sws:hapi');
9
9
  const url = require('url');
10
10
  const qs = require('qs');
11
11
  const send = require('send');
12
+ const mime = require('mime-types');
12
13
 
13
14
  /* HAPI Plugin */
14
15
  class SwsHapi {
@@ -117,7 +118,7 @@ class SwsHapi {
117
118
  dotfiles: 'deny'
118
119
  // TODO Caching
119
120
  };
120
- request.raw.res.setHeader('Content-Type', send.mime.lookup(path.basename(fileName)));
121
+ request.raw.res.setHeader('Content-Type', mime.lookup(path.basename(fileName)) || 'application/octet-stream');
121
122
  send(request.raw.req, fileName, options).pipe(request.raw.res);
122
123
  return h.abandon;
123
124
  },
@@ -15,6 +15,7 @@ const swsProcessor = require('./swsProcessor');
15
15
  const swsEgress = require('./swsegress');
16
16
  const swsAuth = require('./swsAuth');
17
17
  const send = require('send');
18
+ const mime = require('mime-types');
18
19
  const qs = require('qs');
19
20
  const swsHapi = require('./swsHapi');
20
21
 
@@ -101,7 +102,7 @@ function processGetUX(req,res){
101
102
  dotfiles: 'deny'
102
103
  // TODO Caching
103
104
  };
104
- res.setHeader('Content-Type', send.mime.lookup(path.basename(fileName)));
105
+ res.setHeader('Content-Type', mime.lookup(path.basename(fileName)) || 'application/octet-stream');
105
106
  send(req, fileName, options).pipe(res);
106
107
  }
107
108
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "profoundjs-swagger-stats",
3
- "version": "2.0.3",
3
+ "version": "2.1.0",
4
4
  "description": "API Telemetry and APM. Trace API calls and Monitor API performance, health and usage statistics in Node.js Microservices, based on express routes and Swagger (Open API) specification",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -9,10 +9,9 @@
9
9
  "start": "node examples/testapp/testapp.js",
10
10
  "hapitestapp": "cross-env SWS_AUTHTEST_MAXAGE=2 nyc --reporter=lcov --reporter=html --reporter=json --reporter=text --report-dir=coverage/hapitestapp node examples/hapijstest/hapijstest.js",
11
11
  "fastifytestapp": "cross-env SWS_AUTHTEST_MAXAGE=2 nyc --reporter=lcov --reporter=html --reporter=json --reporter=text --report-dir=coverage/fastifytestapp node examples/fastify/fasifytest.js",
12
- "restifytestapp": "cross-env SWS_AUTHTEST_MAXAGE=2 nyc --reporter=lcov --reporter=html --reporter=json --reporter=text --report-dir=coverage/restifytestapp node examples/restify/restifytest.js",
13
12
  "testappstop": "mocha --delay --exit test/stoptestapp.js",
14
13
  "delay1s": "mocha --delay --exit test/delay.js",
15
- "test": "npm run testExpress && npm run testHapi && npm run testFastify && npm run testRestify && npm run coverage-report",
14
+ "test": "npm run testExpress && npm run testHapi && npm run testFastify && npm run coverage-report",
16
15
  "testExpress": "npm run cov000 && npm run cov010 && npm run cov100 && npm run cov200 && npm run cov300 && npm run cov400 && npm run cov500",
17
16
  "cov000": "nyc --reporter=lcov --reporter=html --reporter=json --reporter=text --report-dir=coverage/000 mocha --delay --exit test/000_baseline.js",
18
17
  "cov010": "nyc --reporter=lcov --reporter=html --reporter=json --reporter=text --report-dir=coverage/010 mocha --delay --exit test/010_swsapistats.js",
@@ -37,13 +36,6 @@
37
36
  "cov300f": "nyc --reporter=lcov --reporter=html --reporter=json --reporter=text --report-dir=coverage/300f mocha --delay --exit test/300_timeline.js",
38
37
  "cov400f": "nyc --reporter=lcov --reporter=html --reporter=json --reporter=text --report-dir=coverage/400f mocha --delay --exit test/400_auth.js",
39
38
  "cov500f": "nyc --reporter=lcov --reporter=html --reporter=json --reporter=text --report-dir=coverage/500f mocha --delay --exit test/500_elastic.js",
40
- "testRestify": "concurrently -k --success first \"npm run restifytestapp\" \"npm run covRestify\"",
41
- "covRestify": "npm run delay1s && npm run cov000r && npm run cov100r && npm run cov200r && npm run cov300r && npm run cov400r && npm run testappstop",
42
- "cov000r": "nyc --reporter=lcov --reporter=html --reporter=json --reporter=text --report-dir=coverage/000f mocha --delay --exit test/000_baseline.js",
43
- "cov100r": "nyc --reporter=lcov --reporter=html --reporter=json --reporter=text --report-dir=coverage/100f mocha --delay --exit test/100_method.js",
44
- "cov200r": "nyc --reporter=lcov --reporter=html --reporter=json --reporter=text --report-dir=coverage/200f mocha --delay --exit test/200_apicore.js",
45
- "cov300r": "nyc --reporter=lcov --reporter=html --reporter=json --reporter=text --report-dir=coverage/300f mocha --delay --exit test/300_timeline.js",
46
- "cov400r": "nyc --reporter=lcov --reporter=html --reporter=json --reporter=text --report-dir=coverage/400f mocha --delay --exit test/400_auth.js",
47
39
  "coverage-report": "node_modules/.bin/istanbul report --root ./coverage --dir ./coverage-report lcov",
48
40
  "specstest": "mocha test/specstest/swaggerspecstest.js"
49
41
  },
@@ -94,15 +86,16 @@
94
86
  },
95
87
  "license": "MIT",
96
88
  "dependencies": {
89
+ "axios": "^1.4.0",
97
90
  "basic-auth": "^2.0.1",
98
- "cookies": "^0.8.0",
91
+ "cookies": "^0.9.1",
99
92
  "debug": "^4.3.4",
93
+ "mime-types": "^3.0.2",
100
94
  "moment": "^2.29.4",
101
95
  "path-to-regexp": "^6.2.1",
102
96
  "qs": "^6.11.2",
103
- "send": "^0.18.0",
104
- "uuid": "^9.0.0",
105
- "axios": "^1.4.0"
97
+ "send": "^1.2.0",
98
+ "uuid": "^9.0.0"
106
99
  },
107
100
  "peerDependencies": {
108
101
  "prom-client": ">= 10 <= 14"
@@ -124,15 +117,14 @@
124
117
  "mocha": "^10.2.0",
125
118
  "ncp": "^2.0.0",
126
119
  "nyc": "^15.1.0",
120
+ "profoundjs-swagger-stats-ux": "^1.0.0",
127
121
  "prom-client": "^14.2.0",
128
122
  "q": "^1.5.1",
129
- "restify": "^11.1.0",
123
+ "request": "^2.88.2",
130
124
  "serve-favicon": "^2.5.0",
131
125
  "serve-static": "^1.15.0",
132
126
  "should": "^13.2.3",
133
127
  "supertest": "^6.3.3",
134
- "swagger-parser": "^10.0.3",
135
- "profoundjs-swagger-stats-ux": "^1.0.0",
136
- "request": "^2.88.2"
128
+ "swagger-parser": "^10.0.3"
137
129
  }
138
130
  }
@@ -1,174 +0,0 @@
1
- const path = require('path');
2
- const restify = require('restify');
3
- const debug = require('debug')('sws:restify');
4
-
5
- const swStats = require('../../lib'); // require('swagger-stats');
6
-
7
- const specLocation = path.join(__dirname, '..', 'spectest','petstore3.json');
8
- debug('Loading Swagger Spec from ' + specLocation );
9
- const swaggerSpec = require( specLocation );
10
-
11
- const server = restify.createServer();
12
-
13
- server.use(restify.plugins.queryParser());
14
-
15
- // Use swagger-stats middleware
16
- server.pre(swStats.getMiddleware({
17
- name: 'restifytest',
18
- version: '0.99.7',
19
- hostname: "hostname",
20
- ip: "127.0.0.1",
21
- timelineBucketDuration:1000,
22
- swaggerSpec:swaggerSpec,
23
- swaggerOnly: false,
24
- uriPath: '/swagger-stats',
25
- durationBuckets: [10, 25, 50, 100, 200],
26
- requestSizeBuckets: [10, 25, 50, 100, 200],
27
- responseSizeBuckets: [10, 25, 50, 100, 200],
28
- apdexThreshold: 100,
29
- elasticsearch: 'http://127.0.0.1:9200',
30
- elasticsearchIndexPrefix: 'swaggerstats-',
31
- onResponseFinish: function(req,res,rrr){
32
- debug('onResponseFinish: %s', JSON.stringify(rrr));
33
- },
34
- authentication: true,
35
- sessionMaxAge: process.env.SWS_AUTHTEST_MAXAGE || 900,
36
- onAuthenticate: function(req,username,password){
37
- // simple check for username and password
38
- if(username==='swagger-stats') {
39
- return ((username === 'swagger-stats') && (password === 'swagger-stats'));
40
- } else if(username==='swagger-promise'){
41
- return new Promise(function(resolve) {
42
- setTimeout(function(){
43
- resolve((username === 'swagger-promise') && (password === 'swagger-promise'));
44
- }, 1000);
45
- });
46
- }
47
- return false;
48
- }
49
- }));
50
-
51
- // Mock API
52
- server.pre(mockApiImplementation);
53
-
54
- // Tester API
55
- server.get('/v2/paramstest/:code/and/:value', testerImpl);
56
-
57
- // Stats
58
- server.get('/stats', function(req,res,next){
59
- res.status(200);
60
- res.send(swStats.getCoreStats());
61
- next();
62
- });
63
-
64
-
65
-
66
- server.listen(3040, function() {
67
- console.log('%s listening at %s', server.name, server.url);
68
- });
69
-
70
- // Mock implementation of any API request
71
- // Supports the following parameters in x-sws-res header:
72
- // x-sws-res={ code:<response code>,
73
- // message:<message to provide in response>,
74
- // delay:<delay to respond>,
75
- // payloadsize:<size of payload JSON to generate>
76
- // }
77
- function mockApiImplementation(req,res,next){
78
-
79
- var code = 500;
80
- var message = "MOCK API RESPONSE";
81
- var delay = 0;
82
- var payloadsize = 0;
83
-
84
- if(req.url.startsWith('/v2/paramstest') || req.url.startsWith('/stats')) {
85
- next();
86
- return;
87
- }
88
-
89
- if(req.url.startsWith('/v2/success')) {
90
- res.sendRaw(200, 'OK');
91
- next();
92
- return;
93
- }
94
- if(req.url.startsWith('/v2/redirect')) {
95
- res.redirect('/v2/success',next);
96
- return;
97
- }
98
- if(req.url.startsWith('/v2/client_error')) {
99
- res.sendRaw(404, 'Not found');
100
- next();
101
- return;
102
- }
103
- if(req.url.startsWith('/v2/server_error')) {
104
- res.sendRaw(500, 'Server Error');
105
- next();
106
- return;
107
- }
108
-
109
- // get header
110
- var hdrSwsRes = req.header('x-sws-res');
111
-
112
- if(typeof hdrSwsRes !== 'undefined'){
113
- var swsRes = JSON.parse(hdrSwsRes);
114
- if( 'code' in swsRes ) code = swsRes.code;
115
- if( 'message' in swsRes ) message = swsRes.message;
116
- if( 'delay' in swsRes ) delay = swsRes.delay;
117
- if( 'payloadsize' in swsRes ) payloadsize = swsRes.payloadsize;
118
- }
119
-
120
- if( delay > 0 ){
121
- setTimeout(function(){
122
- mockApiSendResponse(res,code,message,payloadsize);
123
- next();
124
- },delay);
125
- }else{
126
- mockApiSendResponse(res,code,message,payloadsize);
127
- next();
128
- }
129
- }
130
-
131
- function mockApiSendResponse(res,code,message,payloadsize){
132
- if(payloadsize<=0){
133
- res.status(parseInt(code));
134
- res.send(message);
135
- }else{
136
- // generate dummy payload of approximate size
137
- var dummyPayload = [];
138
- var adjSize = payloadsize-4;
139
- if(adjSize<=0) adjSize = 1;
140
- var str = '';
141
- for(var i=0;i<adjSize;i++) str += 'a';
142
- dummyPayload.push(str);
143
- res.contentType = 'json';
144
- res.status(parseInt(code));
145
- res.send(dummyPayload);
146
- }
147
- }
148
-
149
-
150
- function testerImpl(req, res, next) {
151
- var code = 500;
152
- var message = "ERROR: Wrong parameters";
153
- if(('params' in req) && 'code' in req.params ){
154
- code = parseInt(req.params.code);
155
- message = "Request Method:" + req.method.toUpperCase() +', params.code: ' + req.params.code;
156
- }
157
-
158
- let delay = 0;
159
- if(('query' in req) && ('delay' in req.query)){
160
- delay = parseInt(req.query.delay);
161
- }
162
-
163
- if( delay > 0 ){
164
- setTimeout(function(){
165
- res.status(code);
166
- res.send({code: code, message: message});
167
- next();
168
- },delay);
169
- }else{
170
- res.status(code);
171
- res.send({code: code, message: message});
172
- next();
173
- }
174
- }