@e22m4u/js-trie-router 0.5.8 → 0.5.9

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.
@@ -1167,7 +1167,7 @@ var _Route = class _Route extends import_js_debug.Debuggable {
1167
1167
  this._hookRegistry.addHook(RouterHookType.POST_HANDLER, hook);
1168
1168
  });
1169
1169
  }
1170
- this.ctorDebug("Route %s %v created.", this.method, this.path);
1170
+ this.ctorDebug("Created a route %s %v.", this.method, this.path);
1171
1171
  }
1172
1172
  /**
1173
1173
  * Handle request.
@@ -1178,7 +1178,7 @@ var _Route = class _Route extends import_js_debug.Debuggable {
1178
1178
  handle(context) {
1179
1179
  const debug = this.getDebuggerFor(this.handle);
1180
1180
  const requestPath = getRequestPathname(context.request);
1181
- debug("Invoking route handler for %s %v.", this.method, requestPath);
1181
+ debug("Invoking a route handler %s %v.", this.method, requestPath);
1182
1182
  return this.handler(context);
1183
1183
  }
1184
1184
  };
@@ -1310,9 +1310,14 @@ var _BodyParser = class _BodyParser extends DebuggableService {
1310
1310
  */
1311
1311
  parse(request) {
1312
1312
  const debug = this.getDebuggerFor(this.parse);
1313
+ debug(
1314
+ "Parsing a request body %s %v.",
1315
+ request.method.toUpperCase(),
1316
+ getRequestPathname(request)
1317
+ );
1313
1318
  if (!METHODS_WITH_BODY.includes(request.method.toUpperCase())) {
1314
1319
  debug(
1315
- "Body parsing skipped for %s method.",
1320
+ "Skipping body parsing for the %s method.",
1316
1321
  request.method.toUpperCase()
1317
1322
  );
1318
1323
  return;
@@ -1322,7 +1327,7 @@ var _BodyParser = class _BodyParser extends DebuggableService {
1322
1327
  "$1"
1323
1328
  );
1324
1329
  if (!contentType) {
1325
- debug("Body parsing skipped because no content type provided.");
1330
+ debug("Skipping body parsing because no content type is provided.");
1326
1331
  return;
1327
1332
  }
1328
1333
  const { mediaType } = parseContentType(contentType);
@@ -1335,7 +1340,7 @@ var _BodyParser = class _BodyParser extends DebuggableService {
1335
1340
  const parser = this._parsers[mediaType];
1336
1341
  if (!parser) {
1337
1342
  if (UNPARSABLE_MEDIA_TYPES.includes(mediaType)) {
1338
- debug("Body parsing skipped for media type %v.", mediaType);
1343
+ debug("Skipping body parsing for the media type %v.", mediaType);
1339
1344
  return;
1340
1345
  }
1341
1346
  throw createError(
@@ -1345,14 +1350,14 @@ var _BodyParser = class _BodyParser extends DebuggableService {
1345
1350
  );
1346
1351
  }
1347
1352
  const bodyBytesLimit = this.getService(RouterOptions).requestBodyBytesLimit;
1348
- debug("Fetching request body.");
1349
- debug("Body limit %v bytes.", bodyBytesLimit);
1353
+ debug("Fetching a request body.");
1354
+ debug("Body limit is %v bytes.", bodyBytesLimit);
1350
1355
  return fetchRequestBody(request, bodyBytesLimit).then((rawBody) => {
1351
1356
  if (rawBody != null) {
1352
1357
  debug("Read %v bytes.", Buffer.byteLength(rawBody, "utf8"));
1353
1358
  return parser(rawBody);
1354
1359
  }
1355
- debug("No request body content.");
1360
+ debug("Request body has no content.");
1356
1361
  return rawBody;
1357
1362
  });
1358
1363
  }
@@ -1387,7 +1392,7 @@ var _QueryParser = class _QueryParser extends DebuggableService {
1387
1392
  const queryKeys = Object.keys(query);
1388
1393
  if (queryKeys.length) {
1389
1394
  queryKeys.forEach((key) => {
1390
- debug("Found query parameter %v with value %v.", key, query[key]);
1395
+ debug("Found a query parameter %v with a value %v.", key, query[key]);
1391
1396
  });
1392
1397
  } else {
1393
1398
  debug(
@@ -1417,7 +1422,7 @@ var _CookiesParser = class _CookiesParser extends DebuggableService {
1417
1422
  const cookiesKeys = Object.keys(cookies);
1418
1423
  if (cookiesKeys.length) {
1419
1424
  cookiesKeys.forEach((key) => {
1420
- debug("Found cookie %v with value %v.", key, cookies[key]);
1425
+ debug("Found a cookie %v with a value %v.", key, cookies[key]);
1421
1426
  });
1422
1427
  } else {
1423
1428
  debug(
@@ -1507,7 +1512,7 @@ var _RouteRegistry = class _RouteRegistry extends DebuggableService {
1507
1512
  const route = new Route(routeDef);
1508
1513
  const triePath = `${route.method}/${route.path}`;
1509
1514
  this._trie.add(triePath, route);
1510
- debug("Route %s %v registered.", route.method.toUpperCase(), route.path);
1515
+ debug("Registered a route %s %v.", route.method.toUpperCase(), route.path);
1511
1516
  return route;
1512
1517
  }
1513
1518
  /**
@@ -1520,7 +1525,7 @@ var _RouteRegistry = class _RouteRegistry extends DebuggableService {
1520
1525
  const debug = this.getDebuggerFor(this.matchRouteByRequest);
1521
1526
  const requestPath = getRequestPathname(request);
1522
1527
  debug(
1523
- "Matching routes for %s %v.",
1528
+ "Matching routes for the request %s %v.",
1524
1529
  request.method.toUpperCase(),
1525
1530
  requestPath
1526
1531
  );
@@ -1529,12 +1534,12 @@ var _RouteRegistry = class _RouteRegistry extends DebuggableService {
1529
1534
  const resolved = this._trie.match(triePath);
1530
1535
  if (resolved) {
1531
1536
  const route = resolved.value;
1532
- debug("Matched route %s %v.", route.method.toUpperCase(), route.path);
1537
+ debug("Matched route is %s %v.", route.method.toUpperCase(), route.path);
1533
1538
  const paramNames = Object.keys(resolved.params);
1534
1539
  if (paramNames.length) {
1535
1540
  paramNames.forEach((name) => {
1536
1541
  debug(
1537
- "Found path parameter %v with value %v.",
1542
+ "Found a path parameter %v with a value %v.",
1538
1543
  name,
1539
1544
  resolved.params[name]
1540
1545
  );
@@ -1545,7 +1550,7 @@ var _RouteRegistry = class _RouteRegistry extends DebuggableService {
1545
1550
  return { route, params: resolved.params };
1546
1551
  }
1547
1552
  debug(
1548
- "No matched route for %s %v.",
1553
+ "No route found for the request %s %v.",
1549
1554
  request.method.toUpperCase(),
1550
1555
  requestPath
1551
1556
  );
@@ -1752,19 +1757,19 @@ var _DataSender = class _DataSender extends DebuggableService {
1752
1757
  send(response, data) {
1753
1758
  const debug = this.getDebuggerFor(this.send);
1754
1759
  if (data === response || response.headersSent) {
1755
- debug("Response skipped because headers already sent.");
1760
+ debug("Skipping response because headers have already been sent.");
1756
1761
  return;
1757
1762
  }
1758
1763
  if (data == null) {
1759
1764
  response.statusCode = 204;
1760
1765
  response.end();
1761
- debug("Empty response sent.");
1766
+ debug("Empty response has been sent.");
1762
1767
  return;
1763
1768
  }
1764
1769
  if (isReadableStream(data)) {
1765
1770
  response.setHeader("Content-Type", "application/octet-stream");
1766
1771
  data.pipe(response);
1767
- debug("Stream response sent.");
1772
+ debug("Sending response with a Stream.");
1768
1773
  return;
1769
1774
  }
1770
1775
  let debugMsg;
@@ -1774,16 +1779,19 @@ var _DataSender = class _DataSender extends DebuggableService {
1774
1779
  case "number":
1775
1780
  if (Buffer.isBuffer(data)) {
1776
1781
  response.setHeader("content-type", "application/octet-stream");
1777
- debugMsg = "Buffer sent as binary data.";
1782
+ debugMsg = "Buffer has been sent as binary data.";
1778
1783
  } else {
1779
1784
  response.setHeader("content-type", "application/json");
1780
- debugMsg = (0, import_js_format18.format)("%v sent as JSON.", toPascalCase(typeof data));
1785
+ debugMsg = (0, import_js_format18.format)(
1786
+ "%v has been sent as JSON.",
1787
+ toPascalCase(typeof data)
1788
+ );
1781
1789
  data = JSON.stringify(data);
1782
1790
  }
1783
1791
  break;
1784
1792
  default:
1785
1793
  response.setHeader("content-type", "text/plain");
1786
- debugMsg = "Response data sent as plain text.";
1794
+ debugMsg = "Response data has been sent as plain text.";
1787
1795
  data = String(data);
1788
1796
  break;
1789
1797
  }
@@ -1851,7 +1859,7 @@ var _ErrorSender = class _ErrorSender extends DebuggableService {
1851
1859
  response.setHeader("content-type", "application/json; charset=utf-8");
1852
1860
  response.end(JSON.stringify(body, null, 2), "utf-8");
1853
1861
  debug(
1854
- "%s error sent for %s %v request.",
1862
+ "%s error has been sent for the request %s %v.",
1855
1863
  statusCode,
1856
1864
  request.method,
1857
1865
  getRequestPathname(request)
@@ -1870,7 +1878,7 @@ var _ErrorSender = class _ErrorSender extends DebuggableService {
1870
1878
  response.setHeader("content-type", "text/plain; charset=utf-8");
1871
1879
  response.end("404 Not Found", "utf-8");
1872
1880
  debug(
1873
- "404 error sent for %s %v.",
1881
+ "404 error has been sent for the request %s %v.",
1874
1882
  request.method,
1875
1883
  getRequestPathname(request)
1876
1884
  );
@@ -2068,8 +2076,8 @@ var _RouterBranch = class _RouterBranch extends DebuggableService {
2068
2076
  validateRouterBranchDefinition(branchDef);
2069
2077
  this._definition = cloneDeep(branchDef);
2070
2078
  }
2071
- this.ctorDebug("Branch %v created.", normalizePath(branchDef.path, true));
2072
- this.ctorDebug("Branch path set to %v.", this._definition.path);
2079
+ this.ctorDebug("Created a branch %v.", normalizePath(branchDef.path, true));
2080
+ this.ctorDebug("Branch path is %v.", this._definition.path);
2073
2081
  }
2074
2082
  /**
2075
2083
  * Define route.
@@ -2186,10 +2194,14 @@ var _TrieRouter = class _TrieRouter extends DebuggableService {
2186
2194
  async _handleRequest(request, response) {
2187
2195
  const debug = this.getDebuggerFor(this._handleRequest);
2188
2196
  const requestPath = getRequestPathname(request);
2189
- debug("Handling incoming request %s %v.", request.method, requestPath);
2197
+ debug("Handling an incoming request %s %v.", request.method, requestPath);
2190
2198
  const resolved = this.getService(RouteRegistry).matchRouteByRequest(request);
2191
2199
  if (!resolved) {
2192
- debug("No route found for %s %v.", request.method, requestPath);
2200
+ debug(
2201
+ "No route found for the request %s %v.",
2202
+ request.method,
2203
+ requestPath
2204
+ );
2193
2205
  this.getService(ErrorSender).send404(request, response);
2194
2206
  } else {
2195
2207
  const { route, params } = resolved;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/js-trie-router",
3
- "version": "0.5.8",
3
+ "version": "0.5.9",
4
4
  "description": "HTTP маршрутизатор для Node.js на основе префиксного дерева",
5
5
  "author": "Mikhail Evstropov <e22m4u@yandex.ru>",
6
6
  "license": "MIT",
@@ -47,8 +47,8 @@
47
47
  "statuses": "~2.0.2"
48
48
  },
49
49
  "devDependencies": {
50
- "@commitlint/cli": "~20.2.0",
51
- "@commitlint/config-conventional": "~20.2.0",
50
+ "@commitlint/cli": "~20.3.0",
51
+ "@commitlint/config-conventional": "~20.3.0",
52
52
  "@eslint/js": "~9.39.2",
53
53
  "@types/chai": "~5.2.3",
54
54
  "@types/chai-as-promised": "~8.0.2",
@@ -120,8 +120,8 @@ export class RouterBranch extends DebuggableService {
120
120
  validateRouterBranchDefinition(branchDef);
121
121
  this._definition = cloneDeep(branchDef);
122
122
  }
123
- this.ctorDebug('Branch %v created.', normalizePath(branchDef.path, true));
124
- this.ctorDebug('Branch path set to %v.', this._definition.path);
123
+ this.ctorDebug('Created a branch %v.', normalizePath(branchDef.path, true));
124
+ this.ctorDebug('Branch path is %v.', this._definition.path);
125
125
  }
126
126
 
127
127
  /**
@@ -7,6 +7,7 @@ import {
7
7
  createError,
8
8
  parseContentType,
9
9
  fetchRequestBody,
10
+ getRequestPathname,
10
11
  } from '../utils/index.js';
11
12
 
12
13
  /**
@@ -113,9 +114,14 @@ export class BodyParser extends DebuggableService {
113
114
  */
114
115
  parse(request) {
115
116
  const debug = this.getDebuggerFor(this.parse);
117
+ debug(
118
+ 'Parsing a request body %s %v.',
119
+ request.method.toUpperCase(),
120
+ getRequestPathname(request),
121
+ );
116
122
  if (!METHODS_WITH_BODY.includes(request.method.toUpperCase())) {
117
123
  debug(
118
- 'Body parsing skipped for %s method.',
124
+ 'Skipping body parsing for the %s method.',
119
125
  request.method.toUpperCase(),
120
126
  );
121
127
  return;
@@ -125,7 +131,7 @@ export class BodyParser extends DebuggableService {
125
131
  '$1',
126
132
  );
127
133
  if (!contentType) {
128
- debug('Body parsing skipped because no content type provided.');
134
+ debug('Skipping body parsing because no content type is provided.');
129
135
  return;
130
136
  }
131
137
  const {mediaType} = parseContentType(contentType);
@@ -138,7 +144,7 @@ export class BodyParser extends DebuggableService {
138
144
  const parser = this._parsers[mediaType];
139
145
  if (!parser) {
140
146
  if (UNPARSABLE_MEDIA_TYPES.includes(mediaType)) {
141
- debug('Body parsing skipped for media type %v.', mediaType);
147
+ debug('Skipping body parsing for the media type %v.', mediaType);
142
148
  return;
143
149
  }
144
150
  throw createError(
@@ -148,14 +154,14 @@ export class BodyParser extends DebuggableService {
148
154
  );
149
155
  }
150
156
  const bodyBytesLimit = this.getService(RouterOptions).requestBodyBytesLimit;
151
- debug('Fetching request body.');
152
- debug('Body limit %v bytes.', bodyBytesLimit);
157
+ debug('Fetching a request body.');
158
+ debug('Body limit is %v bytes.', bodyBytesLimit);
153
159
  return fetchRequestBody(request, bodyBytesLimit).then(rawBody => {
154
160
  if (rawBody != null) {
155
161
  debug('Read %v bytes.', Buffer.byteLength(rawBody, 'utf8'));
156
162
  return parser(rawBody);
157
163
  }
158
- debug('No request body content.');
164
+ debug('Request body has no content.');
159
165
  return rawBody;
160
166
  });
161
167
  }
@@ -18,7 +18,7 @@ export class CookiesParser extends DebuggableService {
18
18
  const cookiesKeys = Object.keys(cookies);
19
19
  if (cookiesKeys.length) {
20
20
  cookiesKeys.forEach(key => {
21
- debug('Found cookie %v with value %v.', key, cookies[key]);
21
+ debug('Found a cookie %v with a value %v.', key, cookies[key]);
22
22
  });
23
23
  } else {
24
24
  debug(
@@ -19,7 +19,7 @@ export class QueryParser extends DebuggableService {
19
19
  const queryKeys = Object.keys(query);
20
20
  if (queryKeys.length) {
21
21
  queryKeys.forEach(key => {
22
- debug('Found query parameter %v with value %v.', key, query[key]);
22
+ debug('Found a query parameter %v with a value %v.', key, query[key]);
23
23
  });
24
24
  } else {
25
25
  debug(
@@ -147,7 +147,7 @@ export class Route extends Debuggable {
147
147
  this._hookRegistry.addHook(RouterHookType.POST_HANDLER, hook);
148
148
  });
149
149
  }
150
- this.ctorDebug('Route %s %v created.', this.method, this.path);
150
+ this.ctorDebug('Created a route %s %v.', this.method, this.path);
151
151
  }
152
152
 
153
153
  /**
@@ -159,7 +159,7 @@ export class Route extends Debuggable {
159
159
  handle(context) {
160
160
  const debug = this.getDebuggerFor(this.handle);
161
161
  const requestPath = getRequestPathname(context.request);
162
- debug('Invoking route handler for %s %v.', this.method, requestPath);
162
+ debug('Invoking a route handler %s %v.', this.method, requestPath);
163
163
  return this.handler(context);
164
164
  }
165
165
  }
@@ -43,7 +43,7 @@ export class RouteRegistry extends DebuggableService {
43
43
  const route = new Route(routeDef);
44
44
  const triePath = `${route.method}/${route.path}`;
45
45
  this._trie.add(triePath, route);
46
- debug('Route %s %v registered.', route.method.toUpperCase(), route.path);
46
+ debug('Registered a route %s %v.', route.method.toUpperCase(), route.path);
47
47
  return route;
48
48
  }
49
49
 
@@ -57,7 +57,7 @@ export class RouteRegistry extends DebuggableService {
57
57
  const debug = this.getDebuggerFor(this.matchRouteByRequest);
58
58
  const requestPath = getRequestPathname(request);
59
59
  debug(
60
- 'Matching routes for %s %v.',
60
+ 'Matching routes for the request %s %v.',
61
61
  request.method.toUpperCase(),
62
62
  requestPath,
63
63
  );
@@ -68,12 +68,12 @@ export class RouteRegistry extends DebuggableService {
68
68
  const resolved = this._trie.match(triePath);
69
69
  if (resolved) {
70
70
  const route = resolved.value;
71
- debug('Matched route %s %v.', route.method.toUpperCase(), route.path);
71
+ debug('Matched route is %s %v.', route.method.toUpperCase(), route.path);
72
72
  const paramNames = Object.keys(resolved.params);
73
73
  if (paramNames.length) {
74
74
  paramNames.forEach(name => {
75
75
  debug(
76
- 'Found path parameter %v with value %v.',
76
+ 'Found a path parameter %v with a value %v.',
77
77
  name,
78
78
  resolved.params[name],
79
79
  );
@@ -84,7 +84,7 @@ export class RouteRegistry extends DebuggableService {
84
84
  return {route, params: resolved.params};
85
85
  }
86
86
  debug(
87
- 'No matched route for %s %v.',
87
+ 'No route found for the request %s %v.',
88
88
  request.method.toUpperCase(),
89
89
  requestPath,
90
90
  );
@@ -20,7 +20,7 @@ export class DataSender extends DebuggableService {
20
20
  // заголовки, то считаем, что контроллер
21
21
  // уже отправил ответ самостоятельно
22
22
  if (data === response || response.headersSent) {
23
- debug('Response skipped because headers already sent.');
23
+ debug('Skipping response because headers have already been sent.');
24
24
  return;
25
25
  }
26
26
  // если ответ контроллера пуст, то отправляем
@@ -28,7 +28,7 @@ export class DataSender extends DebuggableService {
28
28
  if (data == null) {
29
29
  response.statusCode = 204;
30
30
  response.end();
31
- debug('Empty response sent.');
31
+ debug('Empty response has been sent.');
32
32
  return;
33
33
  }
34
34
  // если ответ контроллера является стримом,
@@ -36,7 +36,7 @@ export class DataSender extends DebuggableService {
36
36
  if (isReadableStream(data)) {
37
37
  response.setHeader('Content-Type', 'application/octet-stream');
38
38
  data.pipe(response);
39
- debug('Stream response sent.');
39
+ debug('Sending response with a Stream.');
40
40
  return;
41
41
  }
42
42
  // подготовка данных перед отправкой, и установка
@@ -50,16 +50,19 @@ export class DataSender extends DebuggableService {
50
50
  // тип Buffer отправляется
51
51
  // как бинарные данные
52
52
  response.setHeader('content-type', 'application/octet-stream');
53
- debugMsg = 'Buffer sent as binary data.';
53
+ debugMsg = 'Buffer has been sent as binary data.';
54
54
  } else {
55
55
  response.setHeader('content-type', 'application/json');
56
- debugMsg = format('%v sent as JSON.', toPascalCase(typeof data));
56
+ debugMsg = format(
57
+ '%v has been sent as JSON.',
58
+ toPascalCase(typeof data),
59
+ );
57
60
  data = JSON.stringify(data);
58
61
  }
59
62
  break;
60
63
  default:
61
64
  response.setHeader('content-type', 'text/plain');
62
- debugMsg = 'Response data sent as plain text.';
65
+ debugMsg = 'Response data has been sent as plain text.';
63
66
  data = String(data);
64
67
  break;
65
68
  }
@@ -66,7 +66,7 @@ export class ErrorSender extends DebuggableService {
66
66
  response.setHeader('content-type', 'application/json; charset=utf-8');
67
67
  response.end(JSON.stringify(body, null, 2), 'utf-8');
68
68
  debug(
69
- '%s error sent for %s %v request.',
69
+ '%s error has been sent for the request %s %v.',
70
70
  statusCode,
71
71
  request.method,
72
72
  getRequestPathname(request),
@@ -86,7 +86,7 @@ export class ErrorSender extends DebuggableService {
86
86
  response.setHeader('content-type', 'text/plain; charset=utf-8');
87
87
  response.end('404 Not Found', 'utf-8');
88
88
  debug(
89
- '404 error sent for %s %v.',
89
+ '404 error has been sent for the request %s %v.',
90
90
  request.method,
91
91
  getRequestPathname(request),
92
92
  );
@@ -99,11 +99,15 @@ export class TrieRouter extends DebuggableService {
99
99
  async _handleRequest(request, response) {
100
100
  const debug = this.getDebuggerFor(this._handleRequest);
101
101
  const requestPath = getRequestPathname(request);
102
- debug('Handling incoming request %s %v.', request.method, requestPath);
102
+ debug('Handling an incoming request %s %v.', request.method, requestPath);
103
103
  const resolved =
104
104
  this.getService(RouteRegistry).matchRouteByRequest(request);
105
105
  if (!resolved) {
106
- debug('No route found for %s %v.', request.method, requestPath);
106
+ debug(
107
+ 'No route found for the request %s %v.',
108
+ request.method,
109
+ requestPath,
110
+ );
107
111
  this.getService(ErrorSender).send404(request, response);
108
112
  } else {
109
113
  const {route, params} = resolved;