express-ext 0.1.16 → 0.1.17

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.
@@ -45,7 +45,7 @@ var LoadController = (function () {
45
45
  var id = view_1.buildAndCheckId(req, res, this.keys);
46
46
  if (id) {
47
47
  this.view(id)
48
- .then(function (obj) { return http_1.respondModel(obj, res); })
48
+ .then(function (obj) { return http_1.respondModel(http_1.minimize(obj), res); })
49
49
  .catch(function (err) { return http_1.handleError(err, res, _this.log); });
50
50
  }
51
51
  };
package/lib/http.js CHANGED
@@ -262,3 +262,41 @@ function getDate(req, name, d) {
262
262
  return date;
263
263
  }
264
264
  exports.getDate = getDate;
265
+ var o = 'object';
266
+ function minimize(obj) {
267
+ if (!obj || typeof obj !== o) {
268
+ return obj;
269
+ }
270
+ var keys = Object.keys(obj);
271
+ for (var _i = 0, keys_2 = keys; _i < keys_2.length; _i++) {
272
+ var key = keys_2[_i];
273
+ var v = obj[key];
274
+ if (v == null) {
275
+ delete obj[key];
276
+ }
277
+ else if (Array.isArray(v) && v.length > 0) {
278
+ var v1 = v[0];
279
+ if (typeof v1 === o && !(v1 instanceof Date)) {
280
+ for (var _a = 0, v_1 = v; _a < v_1.length; _a++) {
281
+ var item = v_1[_a];
282
+ minimize(item);
283
+ }
284
+ }
285
+ }
286
+ }
287
+ return obj;
288
+ }
289
+ exports.minimize = minimize;
290
+ function minimizeArray(arrs) {
291
+ if (!arrs) {
292
+ return arrs;
293
+ }
294
+ if (arrs.length > 0) {
295
+ for (var _i = 0, arrs_1 = arrs; _i < arrs_1.length; _i++) {
296
+ var obj = arrs_1[_i];
297
+ minimize(obj);
298
+ }
299
+ }
300
+ return arrs;
301
+ }
302
+ exports.minimizeArray = minimizeArray;
package/lib/log.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var stream_1 = require("stream");
4
+ var resources_1 = require("./resources");
4
5
  function createConfig(c) {
5
6
  if (!c) {
6
7
  return { skips: [], duration: 'duration', request: '', response: '', status: '', size: '' };
@@ -78,7 +79,7 @@ var MiddlewareLogger = (function () {
78
79
  op[x_1] = JSON.stringify(req.body);
79
80
  }
80
81
  if (_this.conf.response.length > 0) {
81
- var rsBody = Buffer.concat(chunks_1).toString();
82
+ var rsBody = Buffer.concat(chunks_1).toString(resources_1.resources.encoding);
82
83
  op[_this.conf.response] = rsBody;
83
84
  }
84
85
  if (_this.conf.status.length > 0) {
@@ -210,3 +211,37 @@ function isValidSkips(s) {
210
211
  return true;
211
212
  }
212
213
  exports.isValidSkips = isValidSkips;
214
+ function mask(s, start, end, replace) {
215
+ if (start < 0) {
216
+ start = 0;
217
+ }
218
+ if (end < 0) {
219
+ end = 0;
220
+ }
221
+ var t = start + end;
222
+ if (t >= s.length) {
223
+ return replace.repeat(s.length);
224
+ }
225
+ return s.substr(0, start) + replace.repeat(s.length - t) + s.substr(s.length - end);
226
+ }
227
+ exports.mask = mask;
228
+ function margin(s, start, end, replace) {
229
+ if (start >= end) {
230
+ return '';
231
+ }
232
+ if (start < 0) {
233
+ start = 0;
234
+ }
235
+ if (end < 0) {
236
+ end = 0;
237
+ }
238
+ if (start >= s.length) {
239
+ return replace.repeat(s.length);
240
+ }
241
+ if (end >= s.length) {
242
+ return replace.repeat(start) + s.substr(start);
243
+ }
244
+ return replace.repeat(start) + s.substr(start, end - start) + replace.repeat(s.length - end);
245
+ }
246
+ exports.margin = margin;
247
+ exports.maskMargin = margin;
package/lib/resources.js CHANGED
@@ -4,6 +4,7 @@ var fs = require("fs");
4
4
  var resources = (function () {
5
5
  function resources() {
6
6
  }
7
+ resources.encoding = 'utf-8';
7
8
  return resources;
8
9
  }());
9
10
  exports.resources = resources;
package/lib/search.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ var http_1 = require("./http");
3
4
  function jsonResult(res, result, quick, fields, config) {
4
5
  if (quick && fields && fields.length > 0) {
5
6
  res.status(200).json(toCsv(fields, result)).end();
@@ -15,7 +16,7 @@ function buildResult(r, conf) {
15
16
  }
16
17
  var x = {};
17
18
  var li = (conf.list ? conf.list : 'list');
18
- x[li] = r.list;
19
+ x[li] = http_1.minimizeArray(r.list);
19
20
  var to = (conf.total ? conf.total : 'total');
20
21
  x[to] = r.total;
21
22
  if (r.nextPageToken && r.nextPageToken.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.1.16",
3
+ "version": "0.1.17",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -1,5 +1,5 @@
1
1
  import {Request, Response} from 'express';
2
- import {attrs, handleError, Log, respondModel} from './http';
2
+ import {attrs, handleError, Log, minimize, respondModel} from './http';
3
3
  import {Attribute, Attributes} from './metadata';
4
4
  import {buildAndCheckId, buildKeys} from './view';
5
5
 
@@ -48,7 +48,7 @@ export class LoadController<T, ID> {
48
48
  const id = buildAndCheckId<ID>(req, res, this.keys);
49
49
  if (id) {
50
50
  this.view(id)
51
- .then(obj => respondModel(obj, res))
51
+ .then(obj => respondModel(minimize(obj), res))
52
52
  .catch(err => handleError(err, res, this.log));
53
53
  }
54
54
  }
package/src/http.ts CHANGED
@@ -239,3 +239,35 @@ export function getDate(req: Request, name: string, d?: Date): Date|undefined {
239
239
  }
240
240
  return date;
241
241
  }
242
+ const o = 'object';
243
+ export function minimize(obj: any): any {
244
+ if (!obj || typeof obj !== o) {
245
+ return obj;
246
+ }
247
+ const keys = Object.keys(obj);
248
+ for (const key of keys) {
249
+ const v = obj[key];
250
+ if (v == null) {
251
+ delete obj[key];
252
+ } else if (Array.isArray(v) && v.length > 0) {
253
+ const v1 = v[0];
254
+ if (typeof v1 === o && !(v1 instanceof Date)) {
255
+ for (const item of v) {
256
+ minimize(item);
257
+ }
258
+ }
259
+ }
260
+ }
261
+ return obj;
262
+ }
263
+ export function minimizeArray<T>(arrs: T[]): T[] {
264
+ if (!arrs) {
265
+ return arrs;
266
+ }
267
+ if (arrs.length > 0) {
268
+ for (const obj of arrs) {
269
+ minimize(obj);
270
+ }
271
+ }
272
+ return arrs;
273
+ }
package/src/log.ts CHANGED
@@ -2,6 +2,7 @@ import { NextFunction } from 'express';
2
2
  import { ParamsDictionary, Request, Response } from 'express-serve-static-core';
3
3
  import { ParsedQs } from 'qs';
4
4
  import { PassThrough } from 'stream';
5
+ import { resources } from './resources';
5
6
 
6
7
  export interface LogConf {
7
8
  log?: boolean;
@@ -99,7 +100,7 @@ export class MiddlewareLogger {
99
100
  op[x] = JSON.stringify(req.body);
100
101
  }
101
102
  if (this.conf.response.length > 0) {
102
- const rsBody = Buffer.concat(chunks).toString();
103
+ const rsBody = Buffer.concat(chunks).toString(resources.encoding);
103
104
  op[this.conf.response] = rsBody;
104
105
  }
105
106
  if (this.conf.status.length > 0) {
@@ -153,6 +154,7 @@ const getDurationInMilliseconds = (start: [number, number] | undefined) => {
153
154
  return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
154
155
  };
155
156
 
157
+ // tslint:disable-next-line:max-classes-per-file
156
158
  export class MiddlewareController {
157
159
  constructor(public logger: Middleware) {
158
160
  this.config = this.config.bind(this);
@@ -215,3 +217,35 @@ export function isValidSkips(s: string[]): boolean {
215
217
  }
216
218
  return true;
217
219
  }
220
+ export function mask(s: string, start: number, end: number, replace: string): string {
221
+ if (start < 0) {
222
+ start = 0;
223
+ }
224
+ if (end < 0) {
225
+ end = 0;
226
+ }
227
+ const t = start + end;
228
+ if (t >= s.length) {
229
+ return replace.repeat(s.length);
230
+ }
231
+ return s.substr(0, start) + replace.repeat(s.length - t) + s.substr(s.length - end);
232
+ }
233
+ export function margin(s: string, start: number, end: number, replace: string): string {
234
+ if (start >= end) {
235
+ return '';
236
+ }
237
+ if (start < 0) {
238
+ start = 0;
239
+ }
240
+ if (end < 0) {
241
+ end = 0;
242
+ }
243
+ if (start >= s.length) {
244
+ return replace.repeat(s.length);
245
+ }
246
+ if (end >= s.length) {
247
+ return replace.repeat(start) + s.substr(start);
248
+ }
249
+ return replace.repeat(start) + s.substr(start, end - start) + replace.repeat(s.length - end);
250
+ }
251
+ export const maskMargin = margin;
package/src/resources.ts CHANGED
@@ -6,12 +6,14 @@ import { Attributes, ErrorMessage } from './metadata';
6
6
  export class resources {
7
7
  static createValidator?: <T>(attributes: Attributes, allowUndefined?: boolean, max?: number) => Validator<T>;
8
8
  static check: (obj: any, attributes: Attributes, allowUndefined?: boolean, patch?: boolean) => ErrorMessage[];
9
+ static encoding?: BufferEncoding = 'utf-8';
9
10
  }
10
11
 
11
12
  export interface Validator<T> {
12
13
  validate(obj: T, patch?: boolean): Promise<ErrorMessage[]>;
13
14
  }
14
15
 
16
+ // tslint:disable-next-line:max-classes-per-file
15
17
  export class TypeChecker {
16
18
  constructor(public attributes: Attributes, public allowUndefined?: boolean) {
17
19
  this.check = this.check.bind(this);
package/src/search.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import {Request, Response} from 'express';
2
+ import {minimizeArray} from './http';
2
3
  import {Attribute, Attributes} from './metadata';
3
4
 
4
5
  export interface Filter {
@@ -41,7 +42,7 @@ export function buildResult<T>(r: SearchResult<T>, conf?: SearchConfig): any {
41
42
  }
42
43
  const x: any = {};
43
44
  const li = (conf.list ? conf.list : 'list');
44
- x[li] = r.list;
45
+ x[li] = minimizeArray(r.list);
45
46
  const to = (conf.total ? conf.total : 'total');
46
47
  x[to] = r.total;
47
48
  if (r.nextPageToken && r.nextPageToken.length > 0) {