express-ext 0.1.13 → 0.1.14

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.
@@ -1,9 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.map = {
4
+ TRACE: -2,
5
+ DEBUG: -1,
6
+ INFO: 0,
7
+ WARN: 1,
8
+ ERROR: 2,
9
+ PANIC: 3,
10
+ FATAL: 4
11
+ };
3
12
  var LogController = (function () {
4
13
  function LogController(logger, mp) {
5
14
  this.logger = logger;
6
- this.map = mp;
15
+ this.map = (mp ? mp : exports.map);
7
16
  this.config = this.config.bind(this);
8
17
  }
9
18
  LogController.prototype.config = function (req, res) {
@@ -11,14 +20,14 @@ var LogController = (function () {
11
20
  if (!obj || obj === '') {
12
21
  return res.status(400).end('The request body cannot be empty');
13
22
  }
14
- if (!this.logger || !this.map) {
23
+ if (!this.logger) {
15
24
  return res.status(503).end('Logger is not available');
16
25
  }
17
- if (!this.map) {
18
- return res.status(503).end('Map is not available');
19
- }
20
26
  var changed = false;
21
- if (obj.level && typeof obj.level === 'string' && obj.level.length > 0) {
27
+ if (typeof obj.level === 'string' && obj.level.length > 0) {
28
+ if (!this.map) {
29
+ return res.status(503).end('Map is not available');
30
+ }
22
31
  var lv = this.map[obj.level.toUpperCase()];
23
32
  if (lv !== undefined) {
24
33
  this.logger.level = lv;
@@ -26,19 +35,35 @@ var LogController = (function () {
26
35
  }
27
36
  }
28
37
  if (obj.map) {
29
- if (obj.map.level && typeof obj.map.level === 'string' && obj.map.level.length > 0) {
38
+ if (typeof obj.map.level === 'string' && obj.map.level.length > 0) {
30
39
  this.logger.map.level = obj.map.level;
31
40
  changed = true;
32
41
  }
33
- if (obj.map.time && typeof obj.map.time === 'string' && obj.map.time.length > 0) {
42
+ if (typeof obj.map.time === 'string' && obj.map.time.length > 0) {
34
43
  this.logger.map.time = obj.map.time;
35
44
  changed = true;
36
45
  }
37
- if (obj.map.msg && typeof obj.map.msg === 'string' && obj.map.msg.length > 0) {
46
+ if (typeof obj.map.msg === 'string' && obj.map.msg.length > 0) {
38
47
  this.logger.map.msg = obj.map.msg;
39
48
  changed = true;
40
49
  }
41
50
  }
51
+ if (obj.constants !== undefined && typeof obj.constants === 'object') {
52
+ this.logger.constants = obj.constants;
53
+ changed = true;
54
+ }
55
+ if (obj.name) {
56
+ if (typeof obj.name.trace === 'string'
57
+ && typeof obj.name.debug === 'string'
58
+ && typeof obj.name.info === 'string'
59
+ && typeof obj.name.warn === 'string'
60
+ && typeof obj.name.error === 'string'
61
+ && typeof obj.name.panic === 'string'
62
+ && typeof obj.name.fatal === 'string') {
63
+ this.logger.name = obj.name;
64
+ changed = true;
65
+ }
66
+ }
42
67
  if (changed) {
43
68
  return res.status(200).end('true');
44
69
  }
package/lib/log.js CHANGED
@@ -37,28 +37,28 @@ function removeUrlParams(url) {
37
37
  return startParams !== -1 ? url.substring(0, startParams) : url;
38
38
  }
39
39
  exports.removeUrlParams = removeUrlParams;
40
- var Logger = (function () {
41
- function Logger(write, conf, build) {
40
+ var MiddlewareLogger = (function () {
41
+ function MiddlewareLogger(write, conf, build) {
42
42
  this.write = write;
43
43
  this.build = build;
44
44
  this.log = this.log.bind(this);
45
- this.c = createConfig(conf);
45
+ this.conf = createConfig(conf);
46
46
  }
47
- Logger.prototype.log = function (req, res, next) {
47
+ MiddlewareLogger.prototype.log = function (req, res, next) {
48
48
  var _this = this;
49
- if (this.c.log && !skip(this.c.skips, req.originalUrl)) {
49
+ if (this.conf.log && !skip(this.conf.skips, req.originalUrl)) {
50
50
  var start_1 = process.hrtime();
51
51
  var m = req.method;
52
- var x = this.c.request;
52
+ var x_1 = this.conf.request;
53
53
  var r_1 = false;
54
54
  if (m !== 'GET' && m !== 'DELETE') {
55
55
  r_1 = true;
56
56
  }
57
57
  var msg_1 = m + " " + req.originalUrl;
58
- if (this.c.separate && r_1) {
59
- if (this.c.request.length > 0) {
58
+ if (this.conf.separate && r_1) {
59
+ if (this.conf.request.length > 0) {
60
60
  var op = {};
61
- op[x] = JSON.stringify(req.body);
61
+ op[x_1] = JSON.stringify(req.body);
62
62
  if (this.build) {
63
63
  var op2 = this.build(req, op);
64
64
  this.write(msg_1, op2);
@@ -73,28 +73,28 @@ var Logger = (function () {
73
73
  res.on('finish', function () {
74
74
  var duration = getDurationInMilliseconds(start_1);
75
75
  var op = {};
76
- op[_this.c.duration] = duration;
77
- if (r_1 && !_this.c.separate && _this.c.request.length > 0) {
78
- op[_this.c.request] = JSON.stringify(req.body);
76
+ if (r_1 && !_this.conf.separate && _this.conf.request.length > 0) {
77
+ op[x_1] = JSON.stringify(req.body);
79
78
  }
80
- if (_this.c.response.length > 0) {
79
+ if (_this.conf.response.length > 0) {
81
80
  var rsBody = Buffer.concat(chunks_1).toString();
82
- op[_this.c.response] = rsBody;
81
+ op[_this.conf.response] = rsBody;
83
82
  }
84
- if (_this.c.status.length > 0) {
85
- op[_this.c.status] = res.statusCode;
83
+ if (_this.conf.status.length > 0) {
84
+ op[_this.conf.status] = res.statusCode;
86
85
  }
87
- if (_this.c.size.length > 0) {
86
+ if (_this.conf.size.length > 0) {
88
87
  if ('_contentLength' in res) {
89
- op[_this.c.size] = res['_contentLength'];
88
+ op[_this.conf.size] = res['_contentLength'];
90
89
  }
91
90
  else if (res.hasHeader('content-length')) {
92
91
  var l = res.getHeader('content-length');
93
92
  if (typeof l === 'number' || typeof l === 'string') {
94
- op[_this.c.size] = l;
93
+ op[_this.conf.size] = l;
95
94
  }
96
95
  }
97
96
  }
97
+ op[_this.conf.duration] = duration;
98
98
  if (_this.build) {
99
99
  var op2 = _this.build(req, op);
100
100
  _this.write(msg_1, op2);
@@ -109,9 +109,9 @@ var Logger = (function () {
109
109
  next();
110
110
  }
111
111
  };
112
- return Logger;
112
+ return MiddlewareLogger;
113
113
  }());
114
- exports.Logger = Logger;
114
+ exports.MiddlewareLogger = MiddlewareLogger;
115
115
  var mapResponseBody = function (res, chunks) {
116
116
  var defaultWrite = res.write.bind(res);
117
117
  var defaultEnd = res.end.bind(res);
@@ -141,3 +141,71 @@ var getDurationInMilliseconds = function (start) {
141
141
  var diff = process.hrtime(start);
142
142
  return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
143
143
  };
144
+ var MiddlewareController = (function () {
145
+ function MiddlewareController(logger) {
146
+ this.logger = logger;
147
+ this.config = this.config.bind(this);
148
+ }
149
+ MiddlewareController.prototype.config = function (req, res) {
150
+ var obj = req.body;
151
+ if (!obj || obj === '') {
152
+ return res.status(400).end('The request body cannot be empty');
153
+ }
154
+ if (!this.logger) {
155
+ return res.status(503).end('Logger is not available');
156
+ }
157
+ var changed = false;
158
+ if (obj.log !== undefined) {
159
+ this.logger.conf.log = obj.log;
160
+ changed = true;
161
+ }
162
+ if (obj.separate !== undefined) {
163
+ this.logger.conf.separate = obj.separate;
164
+ changed = true;
165
+ }
166
+ if (Array.isArray(obj.skips)) {
167
+ if (isValidSkips(obj.skips)) {
168
+ this.logger.conf.skips = obj.skips;
169
+ changed = true;
170
+ }
171
+ }
172
+ if (typeof obj.duration === 'string' && obj.duration.length > 0) {
173
+ this.logger.conf.duration = obj.duration;
174
+ changed = true;
175
+ }
176
+ if (typeof obj.request === 'string') {
177
+ this.logger.conf.request = obj.request;
178
+ changed = true;
179
+ }
180
+ if (typeof obj.response === 'string') {
181
+ this.logger.conf.response = obj.response;
182
+ changed = true;
183
+ }
184
+ if (typeof obj.status === 'string') {
185
+ this.logger.conf.status = obj.status;
186
+ changed = true;
187
+ }
188
+ if (typeof obj.size === 'string') {
189
+ this.logger.conf.size = obj.size;
190
+ changed = true;
191
+ }
192
+ if (changed) {
193
+ return res.status(200).end('true');
194
+ }
195
+ else {
196
+ return res.status(204).end('false');
197
+ }
198
+ };
199
+ return MiddlewareController;
200
+ }());
201
+ exports.MiddlewareController = MiddlewareController;
202
+ function isValidSkips(s) {
203
+ for (var _i = 0, s_1 = s; _i < s_1.length; _i++) {
204
+ var x = s_1[_i];
205
+ if (!(typeof x === 'string')) {
206
+ return false;
207
+ }
208
+ }
209
+ return true;
210
+ }
211
+ exports.isValidSkips = isValidSkips;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -1,4 +1,5 @@
1
- import {Request, Response} from 'express';
1
+ import { Request, Response } from 'express';
2
+ import { SimpleMap } from './log';
2
3
 
3
4
  export interface NumberMap {
4
5
  [key: string]: number;
@@ -6,6 +7,8 @@ export interface NumberMap {
6
7
  export interface LogConf {
7
8
  level?: string;
8
9
  map?: LogMapConfig;
10
+ constants?: SimpleMap;
11
+ name?: Name;
9
12
  }
10
13
  export interface LogMapConfig {
11
14
  time?: string;
@@ -17,15 +20,49 @@ export interface LogMap {
17
20
  level: string;
18
21
  msg: string;
19
22
  }
20
- export interface LoggerConf {
23
+ export interface Name {
24
+ trace: string;
25
+ debug: string;
26
+ info: string;
27
+ warn: string;
28
+ error: string;
29
+ panic: string;
30
+ fatal: string;
31
+ }
32
+ export interface Logger {
33
+ name: Name;
21
34
  level: number;
22
35
  map: LogMap;
36
+ constants?: SimpleMap;
37
+ trace(msg: string, m?: SimpleMap, ctx?: any): void;
38
+ debug(msg: string, m?: SimpleMap, ctx?: any): void;
39
+ info(msg: string, m?: SimpleMap, ctx?: any): void;
40
+ warn(msg: string, m?: SimpleMap, ctx?: any): void;
41
+ error(msg: string, m?: SimpleMap, ctx?: any): void;
42
+ panic(msg: string, m?: SimpleMap, ctx?: any): void;
43
+ fatal(msg: string, m?: SimpleMap, ctx?: any): void;
44
+ isLevelEnabled(level: number): boolean;
45
+ isTraceEnabled(): boolean;
46
+ isDebugEnabled(): boolean;
47
+ isInfoEnabled(): boolean;
48
+ isWarnEnabled(): boolean;
49
+ isErrorEnabled(): boolean;
50
+ isPanicEnabled(): boolean;
51
+ isFatalEnabled(): boolean;
23
52
  }
24
-
53
+ export const map: NumberMap = {
54
+ TRACE: -2,
55
+ DEBUG: -1,
56
+ INFO: 0,
57
+ WARN: 1,
58
+ ERROR: 2,
59
+ PANIC: 3,
60
+ FATAL: 4
61
+ };
25
62
  export class LogController {
26
- map?: NumberMap;
27
- constructor(public logger: LoggerConf, mp?: NumberMap) {
28
- this.map = mp;
63
+ map: NumberMap;
64
+ constructor(public logger: Logger, mp?: NumberMap) {
65
+ this.map = (mp ? mp : map);
29
66
  this.config = this.config.bind(this);
30
67
  }
31
68
  config(req: Request, res: Response) {
@@ -33,14 +70,14 @@ export class LogController {
33
70
  if (!obj || obj === '') {
34
71
  return res.status(400).end('The request body cannot be empty');
35
72
  }
36
- if (!this.logger || !this.map) {
73
+ if (!this.logger) {
37
74
  return res.status(503).end('Logger is not available');
38
75
  }
39
- if (!this.map) {
40
- return res.status(503).end('Map is not available');
41
- }
42
76
  let changed = false;
43
- if (obj.level && typeof obj.level === 'string' && obj.level.length > 0) {
77
+ if (typeof obj.level === 'string' && obj.level.length > 0) {
78
+ if (!this.map) {
79
+ return res.status(503).end('Map is not available');
80
+ }
44
81
  const lv = this.map[obj.level.toUpperCase()];
45
82
  if (lv !== undefined) {
46
83
  this.logger.level = lv;
@@ -48,19 +85,35 @@ export class LogController {
48
85
  }
49
86
  }
50
87
  if (obj.map) {
51
- if (obj.map.level && typeof obj.map.level === 'string' && obj.map.level.length > 0) {
88
+ if (typeof obj.map.level === 'string' && obj.map.level.length > 0) {
52
89
  this.logger.map.level = obj.map.level;
53
90
  changed = true;
54
91
  }
55
- if (obj.map.time && typeof obj.map.time === 'string' && obj.map.time.length > 0) {
92
+ if (typeof obj.map.time === 'string' && obj.map.time.length > 0) {
56
93
  this.logger.map.time = obj.map.time;
57
94
  changed = true;
58
95
  }
59
- if (obj.map.msg && typeof obj.map.msg === 'string' && obj.map.msg.length > 0) {
96
+ if (typeof obj.map.msg === 'string' && obj.map.msg.length > 0) {
60
97
  this.logger.map.msg = obj.map.msg;
61
98
  changed = true;
62
99
  }
63
100
  }
101
+ if (obj.constants !== undefined && typeof obj.constants === 'object') {
102
+ this.logger.constants = obj.constants;
103
+ changed = true;
104
+ }
105
+ if (obj.name) {
106
+ if (typeof obj.name.trace === 'string'
107
+ && typeof obj.name.debug === 'string'
108
+ && typeof obj.name.info === 'string'
109
+ && typeof obj.name.warn === 'string'
110
+ && typeof obj.name.error === 'string'
111
+ && typeof obj.name.panic === 'string'
112
+ && typeof obj.name.fatal === 'string') {
113
+ this.logger.name = obj.name;
114
+ changed = true;
115
+ }
116
+ }
64
117
  if (changed) {
65
118
  return res.status(200).end('true');
66
119
  } else {
package/src/log.ts CHANGED
@@ -23,7 +23,7 @@ export interface MiddleLog {
23
23
  status: string;
24
24
  size: string;
25
25
  }
26
- interface SimpleMap {
26
+ export interface SimpleMap {
27
27
  [key: string]: string|number|boolean|Date;
28
28
  }
29
29
  export function createConfig(c?: LogConfig): MiddleLog {
@@ -58,24 +58,27 @@ export function removeUrlParams(url: string): string {
58
58
  const startParams = url.indexOf('?');
59
59
  return startParams !== -1 ? url.substring(0, startParams) : url;
60
60
  }
61
- export class Logger {
61
+ export interface Middleware {
62
+ conf: MiddleLog;
63
+ }
64
+ export class MiddlewareLogger {
62
65
  constructor(public write: (msg: string, m?: SimpleMap) => void, conf?: LogConfig, public build?: (req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, m: SimpleMap) => SimpleMap) {
63
66
  this.log = this.log.bind(this);
64
- this.c = createConfig(conf);
67
+ this.conf = createConfig(conf);
65
68
  }
66
- c: MiddleLog;
69
+ conf: MiddleLog;
67
70
  log(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>, number>, next: NextFunction) {
68
- if (this.c.log && !skip(this.c.skips, req.originalUrl)) {
71
+ if (this.conf.log && !skip(this.conf.skips, req.originalUrl)) {
69
72
  const start = process.hrtime();
70
73
  const m = req.method;
71
- const x = this.c.request;
74
+ const x = this.conf.request;
72
75
  let r = false;
73
76
  if (m !== 'GET' && m !== 'DELETE') {
74
77
  r = true;
75
78
  }
76
79
  const msg = `${m} ${req.originalUrl}`;
77
- if (this.c.separate && r) {
78
- if (this.c.request.length > 0) {
80
+ if (this.conf.separate && r) {
81
+ if (this.conf.request.length > 0) {
79
82
  const op: SimpleMap = {};
80
83
  op[x] = JSON.stringify(req.body);
81
84
  if (this.build) {
@@ -91,27 +94,27 @@ export class Logger {
91
94
  res.on('finish', () => {
92
95
  const duration = getDurationInMilliseconds(start);
93
96
  const op: SimpleMap = {};
94
- op[this.c.duration] = duration;
95
- if (r && !this.c.separate && this.c.request.length > 0) {
96
- op[this.c.request] = JSON.stringify(req.body);
97
+ if (r && !this.conf.separate && this.conf.request.length > 0) {
98
+ op[x] = JSON.stringify(req.body);
97
99
  }
98
- if (this.c.response.length > 0) {
100
+ if (this.conf.response.length > 0) {
99
101
  const rsBody = Buffer.concat(chunks).toString();
100
- op[this.c.response] = rsBody;
102
+ op[this.conf.response] = rsBody;
101
103
  }
102
- if (this.c.status.length > 0) {
103
- op[this.c.status] = res.statusCode;
104
+ if (this.conf.status.length > 0) {
105
+ op[this.conf.status] = res.statusCode;
104
106
  }
105
- if (this.c.size.length > 0) {
107
+ if (this.conf.size.length > 0) {
106
108
  if ('_contentLength' in res) {
107
- op[this.c.size] = (res as any)['_contentLength'];
109
+ op[this.conf.size] = (res as any)['_contentLength'];
108
110
  } else if (res.hasHeader('content-length')) {
109
111
  const l = res.getHeader('content-length');
110
112
  if (typeof l === 'number' || typeof l === 'string') {
111
- op[this.c.size] = l;
113
+ op[this.conf.size] = l;
112
114
  }
113
115
  }
114
116
  }
117
+ op[this.conf.duration] = duration;
115
118
  if (this.build) {
116
119
  const op2 = this.build(req, op);
117
120
  this.write(msg, op2);
@@ -148,3 +151,66 @@ const getDurationInMilliseconds = (start: [number, number] | undefined) => {
148
151
  const diff = process.hrtime(start);
149
152
  return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
150
153
  };
154
+
155
+ export class MiddlewareController {
156
+ constructor(public logger: Middleware) {
157
+ this.config = this.config.bind(this);
158
+ }
159
+ config(req: Request, res: Response) {
160
+ const obj: MiddleLog = req.body;
161
+ if (!obj || (obj as any) === '') {
162
+ return res.status(400).end('The request body cannot be empty');
163
+ }
164
+ if (!this.logger) {
165
+ return res.status(503).end('Logger is not available');
166
+ }
167
+ let changed = false;
168
+ if (obj.log !== undefined) {
169
+ this.logger.conf.log = obj.log;
170
+ changed = true;
171
+ }
172
+ if (obj.separate !== undefined) {
173
+ this.logger.conf.separate = obj.separate;
174
+ changed = true;
175
+ }
176
+ if (Array.isArray(obj.skips)) {
177
+ if (isValidSkips(obj.skips)) {
178
+ this.logger.conf.skips = obj.skips;
179
+ changed = true;
180
+ }
181
+ }
182
+ if (typeof obj.duration === 'string' && obj.duration.length > 0) {
183
+ this.logger.conf.duration = obj.duration;
184
+ changed = true;
185
+ }
186
+ if (typeof obj.request === 'string') {
187
+ this.logger.conf.request = obj.request;
188
+ changed = true;
189
+ }
190
+ if (typeof obj.response === 'string') {
191
+ this.logger.conf.response = obj.response;
192
+ changed = true;
193
+ }
194
+ if (typeof obj.status === 'string') {
195
+ this.logger.conf.status = obj.status;
196
+ changed = true;
197
+ }
198
+ if (typeof obj.size === 'string') {
199
+ this.logger.conf.size = obj.size;
200
+ changed = true;
201
+ }
202
+ if (changed) {
203
+ return res.status(200).end('true');
204
+ } else {
205
+ return res.status(204).end('false');
206
+ }
207
+ }
208
+ }
209
+ export function isValidSkips(s: string[]): boolean {
210
+ for (const x of s) {
211
+ if (!(typeof x === 'string')) {
212
+ return false;
213
+ }
214
+ }
215
+ return true;
216
+ }