@e22m4u/js-trie-router 0.7.2 → 0.7.3

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.
@@ -2071,12 +2071,28 @@ var _TrieRouterOptions = class _TrieRouterOptions {
2071
2071
  */
2072
2072
  _requestBodyBytesLimit = 512 * 1024;
2073
2073
  // 512kb
2074
+ /**
2075
+ * Request body bytes limit.
2076
+ *
2077
+ * @type {number}
2078
+ */
2079
+ get requestBodyBytesLimit() {
2080
+ return this._requestBodyBytesLimit;
2081
+ }
2074
2082
  /**
2075
2083
  * Ignored media types.
2076
2084
  *
2077
2085
  * @type {string[]}
2078
2086
  */
2079
2087
  _ignoredMediaTypes = [];
2088
+ /**
2089
+ * Ignored media types.
2090
+ *
2091
+ * @type {string[]}
2092
+ */
2093
+ get ignoredMediaTypes() {
2094
+ return this._ignoredMediaTypes;
2095
+ }
2080
2096
  /**
2081
2097
  * Constructor.
2082
2098
  *
@@ -2118,74 +2134,9 @@ var _TrieRouterOptions = class _TrieRouterOptions {
2118
2134
  this._ignoredMediaTypes.push(mediaTypeLc);
2119
2135
  }
2120
2136
  });
2137
+ Object.freeze(this._ignoredMediaTypes);
2121
2138
  }
2122
2139
  }
2123
- /**
2124
- * Get request body bytes limit.
2125
- *
2126
- * @param {number} limit
2127
- * @returns {this}
2128
- */
2129
- setRequestBodyBytesLimit(limit) {
2130
- if (typeof limit !== "number" || limit < 0) {
2131
- throw new import_js_format18.InvalidArgumentError(
2132
- 'Parameter "limit" must be a positive Number or 0, but %v was given.',
2133
- limit
2134
- );
2135
- }
2136
- this._requestBodyBytesLimit = limit;
2137
- return this;
2138
- }
2139
- /**
2140
- * Get request body bytes limit.
2141
- *
2142
- * @returns {number}
2143
- */
2144
- getRequestBodyBytesLimit() {
2145
- return this._requestBodyBytesLimit;
2146
- }
2147
- /**
2148
- * Get ignored media types.
2149
- *
2150
- * @param {string} mediaType
2151
- * @returns {this}
2152
- */
2153
- addIgnoredMediaType(mediaType) {
2154
- if (!mediaType || typeof mediaType !== "string") {
2155
- throw new import_js_format18.InvalidArgumentError(
2156
- 'Parameter "mediaType" must be a non-empty String, but %v was given.',
2157
- mediaType
2158
- );
2159
- }
2160
- const mediaTypeLc = mediaType.toLowerCase();
2161
- if (!this._ignoredMediaTypes.includes(mediaTypeLc)) {
2162
- this._ignoredMediaTypes.push(mediaTypeLc);
2163
- }
2164
- return this;
2165
- }
2166
- /**
2167
- * Has ignored media type.
2168
- *
2169
- * @param {string} mediaType
2170
- * @returns {boolean}
2171
- */
2172
- hasIgnoredMediaType(mediaType) {
2173
- if (!mediaType || typeof mediaType !== "string") {
2174
- throw new import_js_format18.InvalidArgumentError(
2175
- 'Parameter "mediaType" must be a non-empty String, but %v was given.',
2176
- mediaType
2177
- );
2178
- }
2179
- return this._ignoredMediaTypes.includes(mediaType.toLowerCase());
2180
- }
2181
- /**
2182
- * Get ignored media types.
2183
- *
2184
- * @returns {string[]}
2185
- */
2186
- getIgnoredMediaTypes() {
2187
- return this._ignoredMediaTypes.slice();
2188
- }
2189
2140
  };
2190
2141
  __name(_TrieRouterOptions, "TrieRouterOptions");
2191
2142
  var TrieRouterOptions = _TrieRouterOptions;
@@ -2307,12 +2258,13 @@ var _RequestBodyParser = class _RequestBodyParser extends DebuggableService {
2307
2258
  );
2308
2259
  }
2309
2260
  const options = this.getService(TrieRouterOptions);
2310
- const isMediaTypeIgnored = options.hasIgnoredMediaType(mediaType);
2261
+ const mediaTypeLc = mediaType.toLowerCase();
2262
+ const isMediaTypeIgnored = options.ignoredMediaTypes.includes(mediaTypeLc);
2311
2263
  if (isMediaTypeIgnored) {
2312
2264
  debug("Media type %v is ignored.", mediaType);
2313
2265
  return;
2314
2266
  }
2315
- const parser = this._parsers[mediaType.toLowerCase()];
2267
+ const parser = this._parsers[mediaTypeLc];
2316
2268
  if (!parser) {
2317
2269
  throw createError(
2318
2270
  import_http_errors2.default.UnsupportedMediaType,
@@ -2320,7 +2272,7 @@ var _RequestBodyParser = class _RequestBodyParser extends DebuggableService {
2320
2272
  mediaType
2321
2273
  );
2322
2274
  }
2323
- const bodyBytesLimit = options.getRequestBodyBytesLimit();
2275
+ const bodyBytesLimit = options.requestBodyBytesLimit;
2324
2276
  debug("Fetching a request body.");
2325
2277
  debug("Body limit is %v bytes.", bodyBytesLimit);
2326
2278
  return fetchRequestBody(request, bodyBytesLimit).then((rawBody) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/js-trie-router",
3
- "version": "0.7.2",
3
+ "version": "0.7.3",
4
4
  "description": "HTTP маршрутизатор для Node.js на основе префиксного дерева",
5
5
  "author": "Mikhail Evstropov <e22m4u@yandex.ru>",
6
6
  "license": "MIT",
@@ -147,14 +147,15 @@ export class RequestBodyParser extends DebuggableService {
147
147
  // если текущий медиа тип исключен
148
148
  // настройками, то парсинг пропускается
149
149
  const options = this.getService(TrieRouterOptions);
150
- const isMediaTypeIgnored = options.hasIgnoredMediaType(mediaType);
150
+ const mediaTypeLc = mediaType.toLowerCase();
151
+ const isMediaTypeIgnored = options.ignoredMediaTypes.includes(mediaTypeLc);
151
152
  if (isMediaTypeIgnored) {
152
153
  debug('Media type %v is ignored.', mediaType);
153
154
  return;
154
155
  }
155
156
  // если парсер для текущего медиа типа
156
157
  // не определен, то выбрасывается ошибка
157
- const parser = this._parsers[mediaType.toLowerCase()];
158
+ const parser = this._parsers[mediaTypeLc];
158
159
  if (!parser) {
159
160
  throw createError(
160
161
  HttpErrors.UnsupportedMediaType,
@@ -164,7 +165,7 @@ export class RequestBodyParser extends DebuggableService {
164
165
  }
165
166
  // определение максимального количества
166
167
  // байт, извлекаемых из тела запроса
167
- const bodyBytesLimit = options.getRequestBodyBytesLimit();
168
+ const bodyBytesLimit = options.requestBodyBytesLimit;
168
169
  debug('Fetching a request body.');
169
170
  debug('Body limit is %v bytes.', bodyBytesLimit);
170
171
  // извлечение тела запроса для последующего
@@ -11,33 +11,19 @@ export type TrieRouterOptionsInput = {
11
11
  */
12
12
  export declare class TrieRouterOptions {
13
13
  /**
14
- * Get request body bytes limit.
15
- *
16
- * @param limit
17
- */
18
- setRequestBodyBytesLimit(limit: number): this;
19
-
20
- /**
21
- * Get request body bytes limit.
14
+ * Request body bytes limit.
22
15
  */
23
- getRequestBodyBytesLimit(): number;
16
+ get requestBodyBytesLimit(): number;
24
17
 
25
18
  /**
26
- * Get ignored media types.
27
- *
28
- * @param mediaType
19
+ * Request body bytes limit.
29
20
  */
30
- addIgnoredMediaType(mediaType: string): this;
21
+ get ignoredMediaTypes(): string[];
31
22
 
32
23
  /**
33
- * Has ignored media type.
24
+ * Constructor.
34
25
  *
35
- * @param mediaType
36
- */
37
- hasIgnoredMediaType(mediaType: string): boolean;
38
-
39
- /**
40
- * Get ignored media types.
26
+ * @param options
41
27
  */
42
- getIgnoredMediaTypes(): string[];
28
+ constructor(options?: TrieRouterOptionsInput);
43
29
  }
@@ -11,6 +11,15 @@ export class TrieRouterOptions {
11
11
  */
12
12
  _requestBodyBytesLimit = 512 * 1024; // 512kb
13
13
 
14
+ /**
15
+ * Request body bytes limit.
16
+ *
17
+ * @type {number}
18
+ */
19
+ get requestBodyBytesLimit() {
20
+ return this._requestBodyBytesLimit;
21
+ }
22
+
14
23
  /**
15
24
  * Ignored media types.
16
25
  *
@@ -18,6 +27,15 @@ export class TrieRouterOptions {
18
27
  */
19
28
  _ignoredMediaTypes = [];
20
29
 
30
+ /**
31
+ * Ignored media types.
32
+ *
33
+ * @type {string[]}
34
+ */
35
+ get ignoredMediaTypes() {
36
+ return this._ignoredMediaTypes;
37
+ }
38
+
21
39
  /**
22
40
  * Constructor.
23
41
  *
@@ -66,77 +84,7 @@ export class TrieRouterOptions {
66
84
  this._ignoredMediaTypes.push(mediaTypeLc);
67
85
  }
68
86
  });
87
+ Object.freeze(this._ignoredMediaTypes);
69
88
  }
70
89
  }
71
-
72
- /**
73
- * Get request body bytes limit.
74
- *
75
- * @param {number} limit
76
- * @returns {this}
77
- */
78
- setRequestBodyBytesLimit(limit) {
79
- if (typeof limit !== 'number' || limit < 0) {
80
- throw new InvalidArgumentError(
81
- 'Parameter "limit" must be a positive Number or 0, but %v was given.',
82
- limit,
83
- );
84
- }
85
- this._requestBodyBytesLimit = limit;
86
- return this;
87
- }
88
-
89
- /**
90
- * Get request body bytes limit.
91
- *
92
- * @returns {number}
93
- */
94
- getRequestBodyBytesLimit() {
95
- return this._requestBodyBytesLimit;
96
- }
97
-
98
- /**
99
- * Get ignored media types.
100
- *
101
- * @param {string} mediaType
102
- * @returns {this}
103
- */
104
- addIgnoredMediaType(mediaType) {
105
- if (!mediaType || typeof mediaType !== 'string') {
106
- throw new InvalidArgumentError(
107
- 'Parameter "mediaType" must be a non-empty String, but %v was given.',
108
- mediaType,
109
- );
110
- }
111
- const mediaTypeLc = mediaType.toLowerCase();
112
- if (!this._ignoredMediaTypes.includes(mediaTypeLc)) {
113
- this._ignoredMediaTypes.push(mediaTypeLc);
114
- }
115
- return this;
116
- }
117
-
118
- /**
119
- * Has ignored media type.
120
- *
121
- * @param {string} mediaType
122
- * @returns {boolean}
123
- */
124
- hasIgnoredMediaType(mediaType) {
125
- if (!mediaType || typeof mediaType !== 'string') {
126
- throw new InvalidArgumentError(
127
- 'Parameter "mediaType" must be a non-empty String, but %v was given.',
128
- mediaType,
129
- );
130
- }
131
- return this._ignoredMediaTypes.includes(mediaType.toLowerCase());
132
- }
133
-
134
- /**
135
- * Get ignored media types.
136
- *
137
- * @returns {string[]}
138
- */
139
- getIgnoredMediaTypes() {
140
- return this._ignoredMediaTypes.slice();
141
- }
142
90
  }
@@ -87,210 +87,31 @@ describe('TrieRouterOptions', function () {
87
87
  it('should set the option "requestBodyBytesLimit" to the current instance', function () {
88
88
  const value = 10;
89
89
  const inst = new TrieRouterOptions({requestBodyBytesLimit: value});
90
- expect(inst.getRequestBodyBytesLimit()).to.be.eq(value);
90
+ expect(inst.requestBodyBytesLimit).to.be.eq(value);
91
91
  });
92
92
 
93
93
  it('should add elements of the option "ignoredMediaTypes" to the current instance', function () {
94
94
  const value = ['text/plain', 'text/html'];
95
95
  const inst = new TrieRouterOptions({ignoredMediaTypes: value});
96
- expect(inst.getIgnoredMediaTypes()).to.include(value[0]);
97
- expect(inst.getIgnoredMediaTypes()).to.include(value[1]);
96
+ expect(inst.ignoredMediaTypes).to.be.eql(['text/plain', 'text/html']);
98
97
  });
99
98
 
100
99
  it('should add elements of the option "ignoredMediaTypes" without duplicates', function () {
101
100
  const value = ['text/plain', 'text/html', 'text/plain'];
102
101
  const inst = new TrieRouterOptions({ignoredMediaTypes: value});
103
- expect(inst.getIgnoredMediaTypes()).to.be.eql([
104
- 'text/plain',
105
- 'text/html',
106
- ]);
102
+ expect(inst.ignoredMediaTypes).to.be.eql(['text/plain', 'text/html']);
107
103
  });
108
104
 
109
- it('should convert ignored media types to lower case', function () {
110
- const inst = new TrieRouterOptions({ignoredMediaTypes: ['TEXT/PLAIN']});
111
- expect(inst.getIgnoredMediaTypes()).to.be.eql(['text/plain']);
112
- });
113
- });
114
-
115
- describe('setRequestBodyBytesLimit', function () {
116
- it('should require the parameter "value" to be a positive number or zero', function () {
117
- const throwable = v => () => {
118
- const S = new TrieRouterOptions();
119
- S.setRequestBodyBytesLimit(v);
120
- };
121
- const error = s =>
122
- format(
123
- 'Parameter "limit" must be a positive Number or 0, but %s was given.',
124
- s,
125
- );
126
- expect(throwable('str')).to.throw(error('"str"'));
127
- expect(throwable('')).to.throw(error('""'));
128
- expect(throwable(-1)).to.throw(error('-1'));
129
- expect(throwable(true)).to.throw(error('true'));
130
- expect(throwable(false)).to.throw(error('false'));
131
- expect(throwable([])).to.throw(error('Array'));
132
- expect(throwable({})).to.throw(error('Object'));
133
- expect(throwable(undefined)).to.throw(error('undefined'));
134
- expect(throwable(null)).to.throw(error('null'));
135
- throwable(10)();
136
- throwable(0)();
137
- });
138
-
139
- it('should set the option value to the current instance', function () {
140
- const S = new TrieRouterOptions();
141
- const customValue = 10;
142
- S.setRequestBodyBytesLimit(customValue);
143
- const res = S.getRequestBodyBytesLimit();
144
- expect(res).to.be.eq(customValue);
145
- });
146
-
147
- it('should return the current instance to allow chaining', function () {
148
- const S = new TrieRouterOptions();
149
- const res = S.setRequestBodyBytesLimit(10);
150
- expect(res).to.be.eq(S);
151
- });
152
- });
153
-
154
- describe('getRequestBodyBytesLimit', function () {
155
- it('should return a default value when the option is not specified', function () {
156
- const defaultValue = 512 * 1024; // 512kb
157
- const inst = new TrieRouterOptions();
158
- expect(inst.getRequestBodyBytesLimit()).to.be.eq(defaultValue);
159
- });
160
-
161
- it('should return a value specified in the constructor', function () {
162
- const customValue = 10;
163
- const inst = new TrieRouterOptions({requestBodyBytesLimit: customValue});
164
- expect(inst.getRequestBodyBytesLimit()).to.be.eq(customValue);
165
- });
166
- });
167
-
168
- describe('addIgnoredMediaType', function () {
169
- it('should require the parameter "mediaType" to be a non-empty String', function () {
170
- const throwable = v => () => {
171
- const S = new TrieRouterOptions();
172
- S.addIgnoredMediaType(v);
173
- };
174
- const error = s =>
175
- format(
176
- 'Parameter "mediaType" must be a non-empty String, ' +
177
- 'but %s was given.',
178
- s,
179
- );
180
- expect(throwable('')).to.throw(error('""'));
181
- expect(throwable(10)).to.throw(error('10'));
182
- expect(throwable(0)).to.throw(error('0'));
183
- expect(throwable(true)).to.throw(error('true'));
184
- expect(throwable(false)).to.throw(error('false'));
185
- expect(throwable([])).to.throw(error('Array'));
186
- expect(throwable({})).to.throw(error('Object'));
187
- expect(throwable(undefined)).to.throw(error('undefined'));
188
- expect(throwable(null)).to.throw(error('null'));
189
- throwable('text/plain')();
190
- });
191
-
192
- it('should add an ignored media type to the current instance', function () {
193
- const S = new TrieRouterOptions();
194
- const mediaType = 'text/plain';
195
- expect(S.hasIgnoredMediaType(mediaType)).to.be.false;
196
- S.addIgnoredMediaType(mediaType);
197
- expect(S.hasIgnoredMediaType(mediaType)).to.be.true;
198
- });
199
-
200
- it('should return the current instance to allow chaining', function () {
201
- const S = new TrieRouterOptions();
202
- const res = S.addIgnoredMediaType('text/plain');
203
- expect(res).to.be.eq(S);
204
- });
205
- });
206
-
207
- describe('hasIgnoredMediaType', function () {
208
- it('should require the parameter "mediaType" to be a non-empty String', function () {
209
- const throwable = v => () => {
210
- const S = new TrieRouterOptions();
211
- S.hasIgnoredMediaType(v);
212
- };
213
- const error = s =>
214
- format(
215
- 'Parameter "mediaType" must be a non-empty String, ' +
216
- 'but %s was given.',
217
- s,
218
- );
219
- expect(throwable('')).to.throw(error('""'));
220
- expect(throwable(10)).to.throw(error('10'));
221
- expect(throwable(0)).to.throw(error('0'));
222
- expect(throwable(true)).to.throw(error('true'));
223
- expect(throwable(false)).to.throw(error('false'));
224
- expect(throwable([])).to.throw(error('Array'));
225
- expect(throwable({})).to.throw(error('Object'));
226
- expect(throwable(undefined)).to.throw(error('undefined'));
227
- expect(throwable(null)).to.throw(error('null'));
228
- throwable('text/plain')();
229
- });
230
-
231
- it('should return true when the given type is registered', function () {
232
- const S = new TrieRouterOptions();
233
- const mediaType = 'text/plain';
234
- expect(S.hasIgnoredMediaType(mediaType)).to.be.false;
235
- S.addIgnoredMediaType(mediaType);
236
- expect(S.hasIgnoredMediaType(mediaType)).to.be.true;
237
- });
238
-
239
- it('should lookup the media type in case-insensitive mode', function () {
240
- const S = new TrieRouterOptions();
241
- S.addIgnoredMediaType('TeXt/PlAiN');
242
- expect(S.hasIgnoredMediaType('tExT/pLaIn')).to.be.true;
243
- });
244
-
245
- it('should not add duplicates even with different cases', function () {
246
- const S = new TrieRouterOptions();
247
- S.addIgnoredMediaType('text/plain');
248
- S.addIgnoredMediaType('TEXT/PLAIN');
249
- S.addIgnoredMediaType('text/plain');
250
- const res = S.getIgnoredMediaTypes();
251
- expect(res).to.have.lengthOf(1);
252
- expect(res[0]).to.be.eq('text/plain');
253
- });
254
- });
255
-
256
- describe('getIgnoredMediaTypes', function () {
257
- it('should return media types specified in the constructor', function () {
258
- const mediaTypes = ['text/plain', 'text/html'];
259
- const S = new TrieRouterOptions({ignoredMediaTypes: mediaTypes});
260
- const res = S.getIgnoredMediaTypes();
261
- expect(res).to.be.eql(mediaTypes);
262
- });
263
-
264
- it('should return media types added by the "addIgnoredMediaType" method', function () {
265
- const mediaTypes = ['text/plain', 'text/html'];
266
- const S = new TrieRouterOptions();
267
- S.addIgnoredMediaType(mediaTypes[0]);
268
- S.addIgnoredMediaType(mediaTypes[1]);
269
- const res = S.getIgnoredMediaTypes();
270
- expect(res).to.be.eql(mediaTypes);
271
- });
272
-
273
- it('should combine media types specified by the constructor and the "addIgnoredMediaType" method', function () {
105
+ it('should freeze the option "ignoredMediaTypes" to prevent mutations', function () {
274
106
  const mediaTypes = ['text/plain', 'text/html'];
275
- const S = new TrieRouterOptions({ignoredMediaTypes: [mediaTypes[0]]});
276
- S.addIgnoredMediaType(mediaTypes[1]);
277
- const res = S.getIgnoredMediaTypes();
278
- expect(res).to.be.eql(mediaTypes);
107
+ const inst = new TrieRouterOptions({ignoredMediaTypes: mediaTypes});
108
+ const res = inst.ignoredMediaTypes;
109
+ expect(Object.isFrozen(res)).to.be.true;
279
110
  });
280
111
 
281
- it('should return a clone of the media type list that prevents mutate the state', function () {
282
- const mediaTypes = ['text/plain', 'text/html'];
283
- const S = new TrieRouterOptions();
284
- S.addIgnoredMediaType(mediaTypes[0]);
285
- S.addIgnoredMediaType(mediaTypes[1]);
286
- const res1 = S.getIgnoredMediaTypes();
287
- const res2 = S.getIgnoredMediaTypes();
288
- expect(res1).to.be.eql(mediaTypes);
289
- expect(res2).to.be.eql(mediaTypes);
290
- expect(res1).to.be.not.eq(res2);
291
- res1[0] = '123';
292
- const res3 = S.getIgnoredMediaTypes();
293
- expect(res3).to.be.eql(mediaTypes);
112
+ it('should convert ignored media types to lower case', function () {
113
+ const inst = new TrieRouterOptions({ignoredMediaTypes: ['TEXT/PLAIN']});
114
+ expect(inst.ignoredMediaTypes).to.be.eql(['text/plain']);
294
115
  });
295
116
  });
296
117
  });
@@ -28,7 +28,7 @@ describe('TrieRouter', function () {
28
28
  const optionsInput = {requestBodyBytesLimit: 10};
29
29
  const router = new TrieRouter(optionsInput);
30
30
  const options = router.getService(TrieRouterOptions);
31
- expect(options.getRequestBodyBytesLimit()).to.be.eq(10);
31
+ expect(options.requestBodyBytesLimit).to.be.eq(10);
32
32
  });
33
33
 
34
34
  it('should use the service container and the router options from parameters', function () {
@@ -37,7 +37,7 @@ describe('TrieRouter', function () {
37
37
  const router = new TrieRouter(container, optionsInput);
38
38
  const options = router.getService(TrieRouterOptions);
39
39
  expect(router.container).to.be.eq(container);
40
- expect(options.getRequestBodyBytesLimit()).to.be.eq(10);
40
+ expect(options.requestBodyBytesLimit).to.be.eq(10);
41
41
  });
42
42
  });
43
43