@miqro/core 1.5.14 → 1.5.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.
Files changed (62) hide show
  1. package/dist/api-router.d.ts +6 -5
  2. package/dist/api-router.js +9 -3
  3. package/dist/api-router.js.map +1 -1
  4. package/dist/app-utils.d.ts +13 -0
  5. package/dist/app-utils.js +82 -0
  6. package/dist/app-utils.js.map +1 -0
  7. package/dist/app.js +6 -88
  8. package/dist/app.js.map +1 -1
  9. package/dist/common/api-router-utils.d.ts +1 -1
  10. package/dist/common/api-router-utils.js +7 -3
  11. package/dist/common/api-router-utils.js.map +1 -1
  12. package/dist/common/config/config.js +12 -0
  13. package/dist/common/config/config.js.map +1 -1
  14. package/dist/common/config/loader.js +2 -2
  15. package/dist/common/config/loader.js.map +1 -1
  16. package/dist/common/context.js +6 -3
  17. package/dist/common/context.js.map +1 -1
  18. package/dist/common/router-handler.d.ts +20 -0
  19. package/dist/common/router-handler.js +136 -0
  20. package/dist/common/router-handler.js.map +1 -0
  21. package/dist/common/router-utils.d.ts +7 -5
  22. package/dist/common/router-utils.js +37 -47
  23. package/dist/common/router-utils.js.map +1 -1
  24. package/dist/common/session/service.js +4 -1
  25. package/dist/common/session/service.js.map +1 -1
  26. package/dist/common/tokenize-match.d.ts +13 -20
  27. package/dist/common/tokenize-match.js +27 -59
  28. package/dist/common/tokenize-match.js.map +1 -1
  29. package/dist/middleware/group.js +2 -0
  30. package/dist/middleware/group.js.map +1 -1
  31. package/dist/middleware/index.d.ts +1 -0
  32. package/dist/middleware/index.js +1 -0
  33. package/dist/middleware/index.js.map +1 -1
  34. package/dist/middleware/result.d.ts +3 -0
  35. package/dist/middleware/result.js +51 -0
  36. package/dist/middleware/result.js.map +1 -0
  37. package/dist/router.d.ts +3 -30
  38. package/dist/router.js +5 -167
  39. package/dist/router.js.map +1 -1
  40. package/package.json +10 -8
  41. package/src/api-router.ts +16 -7
  42. package/src/api-router.unit.test.js +273 -0
  43. package/src/app-utils.ts +82 -0
  44. package/src/app-utils.unit.test.js +100 -0
  45. package/src/app.ts +4 -89
  46. package/src/app.unit.test.js +334 -0
  47. package/src/common/api-router-utils.ts +6 -3
  48. package/src/common/config/config.ts +12 -0
  49. package/src/common/config/loader.ts +2 -2
  50. package/src/common/context.ts +7 -3
  51. package/src/common/context.unit.test.js +286 -0
  52. package/src/common/router-handler.ts +159 -0
  53. package/src/common/router-utils.ts +46 -44
  54. package/src/common/session/service.ts +4 -1
  55. package/src/common/tokenize-match.ts +32 -54
  56. package/src/middleware/group.ts +2 -0
  57. package/src/middleware/index.ts +1 -0
  58. package/src/middleware/result.ts +43 -0
  59. package/src/proxy.unit.test.js +157 -0
  60. package/src/router.ts +7 -196
  61. package/src/router.unit.test.js +397 -0
  62. package/src/setup-test.js +5 -0
@@ -0,0 +1,397 @@
1
+ const {fake, requireMock} = require("@miqro/test");
2
+ const {resolve} = require("path");
3
+ const {strictEqual} = require("assert");
4
+ const {format} = require("util");
5
+ const {distPath, basePath} = require("./setup-test.js");
6
+
7
+ const testFilePath = resolve(distPath, "router.js");
8
+
9
+ describe("router unit tests", () => {
10
+
11
+ describe("this.run", () => {
12
+ [
13
+ {prePathTokens: undefined},
14
+ {prePathTokens: ["prePathTokens1"]}
15
+ ].forEach(({prePathTokens}) => {
16
+ [
17
+ {
18
+ pathTokens: undefined
19
+ },
20
+ {
21
+ pathTokens: "pathTokens"
22
+ }
23
+ ].forEach(({pathTokens}) => {
24
+ let router, splitPath;
25
+ const splitPathRet = "splitPathRet";
26
+
27
+ function createFakeHandler({isMatch, invokeEnds, throws}) {
28
+ return {
29
+ invoke: fake((ctx) => {
30
+ if (throws) {
31
+ throw new Error("fakeHandlerError");
32
+ }
33
+ if (invokeEnds) {
34
+ ctx.res.headersSent = true;
35
+ }
36
+ return invokeEnds ? false : true;
37
+ }),
38
+ isMatch: fake(() => {
39
+ return {
40
+ match: isMatch
41
+ };
42
+ })
43
+ };
44
+ }
45
+
46
+ function createFakeContext() {
47
+ return {
48
+ method: "method",
49
+ res: {
50
+ headersSent: false
51
+ }
52
+ };
53
+ }
54
+
55
+
56
+ splitPath = fake(() => {
57
+ return splitPathRet;
58
+ });
59
+ before(() => {
60
+ splitPath.reset();
61
+ const {Router} = requireMock(testFilePath, {
62
+ "http": {},
63
+ "uuid": {},
64
+ "./common/logger": {},
65
+ "./common/context": {},
66
+ "./common/router-utils": {},
67
+ "./common/router-helpers": {},
68
+ "./common/router-handler": {},
69
+ "./common/tokenize-match": {
70
+ splitPath
71
+ }
72
+ }, distPath);
73
+ router = new Router();
74
+ });
75
+ describe("some handlers match with " + prePathTokens + " " + pathTokens, () => {
76
+ it("one handler ends response", async () => {
77
+ router.handlers = [
78
+ createFakeHandler({invokeEnds: false, isMatch: true}),
79
+ createFakeHandler({invokeEnds: false, isMatch: true}),
80
+ createFakeHandler({invokeEnds: false, isMatch: false}),
81
+ createFakeHandler({invokeEnds: true, isMatch: true}),
82
+ createFakeHandler({invokeEnds: true, isMatch: true})
83
+ ];
84
+ const context = createFakeContext();
85
+ const ret = await router.run(context, pathTokens, prePathTokens);
86
+ strictEqual(router.handlers.filter(r => r.isMatch.callCount === 1).length, router.handlers.length - 1);
87
+ strictEqual(splitPath.callCount, pathTokens ? 0 : 1);
88
+ strictEqual(router.handlers[0].isMatch.callArgs[0][0], context.method);
89
+ strictEqual(router.handlers[0].isMatch.callArgs[0][1], pathTokens ? pathTokens : splitPathRet);
90
+ strictEqual(router.handlers[0].isMatch.callArgs[0][2].length, prePathTokens ? prePathTokens.length : 0);
91
+ if (prePathTokens) {
92
+ strictEqual(router.handlers[0].isMatch.callArgs[0][2], prePathTokens);
93
+ }
94
+ strictEqual(router.handlers[0].invoke.callCount, 1);
95
+ strictEqual(router.handlers[0].invoke.callArgs[0][0], context);
96
+ strictEqual(router.handlers[0].invoke.callArgs[0][1], pathTokens ? pathTokens : splitPathRet);
97
+ strictEqual(router.handlers[0].invoke.callArgs[0][2].length, prePathTokens ? prePathTokens.length : 0);
98
+ if (prePathTokens) {
99
+ strictEqual(router.handlers[0].invoke.callArgs[0][2], prePathTokens);
100
+ }
101
+ strictEqual(router.handlers[1].invoke.callCount, 1);
102
+ strictEqual(router.handlers[2].invoke.callCount, 0);
103
+ strictEqual(router.handlers[3].invoke.callCount, 1);
104
+ strictEqual(router.handlers[4].invoke.callCount, 0);
105
+ });
106
+
107
+ it("no handler ends response", async () => {
108
+ router.handlers = [
109
+ createFakeHandler({invokeEnds: false, isMatch: true}),
110
+ createFakeHandler({invokeEnds: false, isMatch: true}),
111
+ createFakeHandler({invokeEnds: false, isMatch: false}),
112
+ createFakeHandler({invokeEnds: false, isMatch: true}),
113
+ createFakeHandler({invokeEnds: false, isMatch: true})
114
+ ];
115
+ const context = createFakeContext();
116
+ const ret = await router.run(context, pathTokens, prePathTokens);
117
+ strictEqual(router.handlers.filter(r => r.isMatch.callCount === 1).length, router.handlers.length);
118
+ strictEqual(splitPath.callCount, pathTokens ? 0 : 1);
119
+ strictEqual(router.handlers[0].invoke.callCount, 1);
120
+ strictEqual(router.handlers[1].invoke.callCount, 1);
121
+ strictEqual(router.handlers[2].invoke.callCount, 0);
122
+ strictEqual(router.handlers[3].invoke.callCount, 1);
123
+ strictEqual(router.handlers[4].invoke.callCount, 1);
124
+ });
125
+
126
+ it("one handler throws second errorHandler catches", async () => {
127
+ router.handlers = [
128
+ createFakeHandler({invokeEnds: false, isMatch: true}),
129
+ createFakeHandler({invokeEnds: false, isMatch: true, throws: true}),
130
+ createFakeHandler({invokeEnds: false, isMatch: false}),
131
+ createFakeHandler({invokeEnds: true, isMatch: true}),
132
+ createFakeHandler({invokeEnds: false, isMatch: true})
133
+ ];
134
+ const errorHandlerMisses = fake(() => {
135
+
136
+ });
137
+ const errorHandler = fake((e, ctx) => {
138
+ ctx.res.headersSent = true;
139
+ });
140
+ const context = createFakeContext();
141
+ router.catch([errorHandlerMisses, errorHandler]);
142
+ const ret = await router.run(context, pathTokens, prePathTokens);
143
+ strictEqual(router.handlers.filter(r => r.isMatch.callCount === 1).length, 2);
144
+ strictEqual(splitPath.callCount, pathTokens ? 0 : 1);
145
+ strictEqual(router.handlers[0].invoke.callCount, 1);
146
+ strictEqual(router.handlers[1].invoke.callCount, 1);
147
+ strictEqual(router.handlers[2].invoke.callCount, 0);
148
+ strictEqual(router.handlers[3].invoke.callCount, 0);
149
+ strictEqual(router.handlers[4].invoke.callCount, 0);
150
+ strictEqual(errorHandlerMisses.callCount, 1);
151
+ strictEqual(errorHandler.callCount, 1);
152
+ });
153
+
154
+ it("one handler throws first errorHandler catches", async () => {
155
+ router.handlers = [
156
+ createFakeHandler({invokeEnds: false, isMatch: true}),
157
+ createFakeHandler({invokeEnds: false, isMatch: true, throws: true}),
158
+ createFakeHandler({invokeEnds: false, isMatch: false}),
159
+ createFakeHandler({invokeEnds: true, isMatch: true}),
160
+ createFakeHandler({invokeEnds: false, isMatch: true})
161
+ ];
162
+ const errorHandlerMisses = fake(() => {
163
+
164
+ });
165
+ const errorHandler = fake((e, ctx) => {
166
+ ctx.res.headersSent = true;
167
+ });
168
+ const context = createFakeContext();
169
+ router.catch([errorHandler, errorHandlerMisses]);
170
+ const ret = await router.run(context, pathTokens, prePathTokens);
171
+ strictEqual(router.handlers.filter(r => r.isMatch.callCount === 1).length, 2);
172
+ strictEqual(splitPath.callCount, pathTokens ? 0 : 1);
173
+ strictEqual(router.handlers[0].invoke.callCount, 1);
174
+ strictEqual(router.handlers[1].invoke.callCount, 1);
175
+ strictEqual(router.handlers[2].invoke.callCount, 0);
176
+ strictEqual(router.handlers[3].invoke.callCount, 0);
177
+ strictEqual(router.handlers[4].invoke.callCount, 0);
178
+ strictEqual(errorHandlerMisses.callCount, 0);
179
+ strictEqual(errorHandler.callCount, 1);
180
+ });
181
+
182
+ it("one handler throws errorHandler doesn't catch", async () => {
183
+ router.handlers = [
184
+ createFakeHandler({invokeEnds: false, isMatch: true}),
185
+ createFakeHandler({invokeEnds: false, isMatch: true, throws: true}),
186
+ createFakeHandler({invokeEnds: false, isMatch: false}),
187
+ createFakeHandler({invokeEnds: true, isMatch: true}),
188
+ createFakeHandler({invokeEnds: false, isMatch: true})
189
+ ];
190
+ const context = createFakeContext();
191
+ try {
192
+ const ret = await router.run(context, pathTokens, prePathTokens);
193
+ strictEqual(false, true);
194
+ } catch (e) {
195
+ strictEqual(e.message, "fakeHandlerError")
196
+ }
197
+ strictEqual(router.handlers.filter(r => r.isMatch.callCount === 1).length, 2);
198
+ strictEqual(splitPath.callCount, pathTokens ? 0 : 1);
199
+ strictEqual(router.handlers[0].invoke.callCount, 1);
200
+ strictEqual(router.handlers[1].invoke.callCount, 1);
201
+ strictEqual(router.handlers[2].invoke.callCount, 0);
202
+ strictEqual(router.handlers[3].invoke.callCount, 0);
203
+ strictEqual(router.handlers[4].invoke.callCount, 0);
204
+ });
205
+ });
206
+ });
207
+ });
208
+ });
209
+
210
+ [
211
+ {method: "options"},
212
+ {method: "put"},
213
+ {method: "patch"},
214
+ {method: "delete"},
215
+ {method: "post"},
216
+ {method: "get"}
217
+ ].forEach(({method}) => {
218
+ let router;
219
+ before(() => {
220
+ const {Router} = requireMock(testFilePath, {
221
+ "http": {},
222
+ "uuid": {},
223
+ "./common/logger": {},
224
+ "./common/context": {},
225
+ "./common/router-utils": {},
226
+ "./common/router-helpers": {},
227
+ "./common/router-handler": {},
228
+ "./common/tokenize-match": {}
229
+ }, distPath);
230
+ router = new Router();
231
+ });
232
+ it(method + " just alias for use", async () => {
233
+ router.use = fake(() => {
234
+ });
235
+ const path = "path";
236
+ const handler = "handler";
237
+ const options = "options";
238
+ router[method](path, handler, options);
239
+ strictEqual(router.use.callCount, 1);
240
+ strictEqual(router.use.callArgs[0][0], handler);
241
+ strictEqual(router.use.callArgs[0][1], path);
242
+ strictEqual(router.use.callArgs[0][2], method);
243
+ strictEqual(router.use.callArgs[0][3], options);
244
+ });
245
+ });
246
+
247
+ describe("this.catch", () => {
248
+
249
+ let router;
250
+ before(() => {
251
+ const {Router} = requireMock(testFilePath, {
252
+ "http": {},
253
+ "uuid": {},
254
+ "./common/logger": {},
255
+ "./common/context": {},
256
+ "./common/router-utils": {},
257
+ "./common/router-helpers": {},
258
+ "./common/router-handler": {},
259
+ "./common/tokenize-match": {}
260
+ }, distPath);
261
+ router = new Router();
262
+ });
263
+ [
264
+ {
265
+ errorHandler: [
266
+ () => {
267
+ },
268
+ () => {
269
+ }
270
+ ]
271
+ },
272
+ {
273
+ errorHandler: () => {
274
+ }
275
+ },
276
+ {
277
+ errorHandler: undefined,
278
+ throws: true,
279
+ },
280
+ {
281
+ errorHandler: [
282
+ () => {
283
+ },
284
+ undefined
285
+ ],
286
+ throws: true
287
+ }
288
+ ].forEach(({errorHandler, throws}) => {
289
+ it(format("errorHandler %o throws %o", errorHandler, throws), () => {
290
+ router.errorHandlers = {
291
+ push: fake(() => {
292
+
293
+ })
294
+ };
295
+ try {
296
+ const ret = router.catch(errorHandler);
297
+ strictEqual(undefined, throws);
298
+ strictEqual(ret, router);
299
+ } catch (e) {
300
+ strictEqual(true, throws);
301
+ } finally {
302
+ const count = (!errorHandler ? [] : (errorHandler instanceof Array ? errorHandler : [errorHandler])).filter(m => typeof m === "function").length;
303
+ strictEqual(router.errorHandlers.push.callCount, count);
304
+ }
305
+ });
306
+ });
307
+ });
308
+
309
+ describe("this.use", () => {
310
+ let RouterHandler, router, routerHandlerCtr, normalizePath, splitPath;
311
+ const normalizePathRet = "normalizePathRet";
312
+
313
+ before(() => {
314
+ routerHandlerCtr = fake(() => {
315
+
316
+ });
317
+
318
+ normalizePath = fake(() => {
319
+ return normalizePathRet;
320
+ });
321
+
322
+ splitPath = fake(() => {
323
+
324
+ });
325
+
326
+ RouterHandler = class {
327
+ constructor() {
328
+ routerHandlerCtr(...arguments);
329
+ }
330
+ }
331
+
332
+ const {Router} = requireMock(testFilePath, {
333
+ "http": {},
334
+ "uuid": {},
335
+ "./common/logger": {},
336
+ "./common/context": {},
337
+ "./common/router-utils": {},
338
+ "./common/router-helpers": {},
339
+ "./common/router-handler": {
340
+ RouterHandler
341
+ },
342
+ "./common/tokenize-match": {
343
+ normalizePath,
344
+ splitPath
345
+ }
346
+ }, distPath);
347
+ router = new Router();
348
+ });
349
+
350
+ [
351
+ {handler: "handler"},
352
+ {handler: undefined},
353
+ {handler: ["handler1", "handler2"]}
354
+ ].forEach(({handler}) => {
355
+ [
356
+ {options: "options"},
357
+ {options: undefined}
358
+ ].forEach(({options}) => {
359
+ [
360
+ {
361
+ path: "path",
362
+ method: "method"
363
+ },
364
+ {
365
+ path: "path"
366
+ },
367
+ {}
368
+ ].forEach(
369
+ ({
370
+ path,
371
+ method
372
+ }) => {
373
+ it(`use with handler ${handler} path ${path} method ${method} options ${options}`, async () => {
374
+ router.handlers = {
375
+ push: fake(() => {
376
+
377
+ })
378
+ }
379
+ const ret = router.use(handler, path, method, options);
380
+ strictEqual(ret, router);
381
+ strictEqual(normalizePath.callCount, path !== undefined ? 1 : 0);
382
+ if (path) {
383
+ strictEqual(normalizePath.callArgs[0][0], path);
384
+ }
385
+ strictEqual(routerHandlerCtr.callCount, 1);
386
+ strictEqual(routerHandlerCtr.callArgs[0][0], handler);
387
+ strictEqual(routerHandlerCtr.callArgs[0][1], path ? normalizePathRet : path);
388
+ strictEqual(routerHandlerCtr.callArgs[0][2], method ? method.toUpperCase() : method);
389
+ strictEqual(routerHandlerCtr.callArgs[0][3], options);
390
+ strictEqual(router.handlers.push.callCount, 1);
391
+ strictEqual(router.handlers.push.callArgs[0][0] instanceof RouterHandler, true);
392
+ });
393
+ });
394
+ });
395
+ });
396
+ });
397
+ });
@@ -0,0 +1,5 @@
1
+ const {resolve} = require("path");
2
+
3
+ module.exports.basePath = resolve(__dirname, "..");
4
+
5
+ module.exports.distPath = resolve(module.exports.basePath, "dist");