(ctx: KoaContext, HandlerClass, checker, savedContext: CordisContext)
| 532 | } |
| 533 | |
| 534 | private async handleHttp(ctx: KoaContext, HandlerClass, checker, savedContext: CordisContext) { |
| 535 | const { args } = ctx.HydroContext; |
| 536 | Object.assign(args, ctx.params); |
| 537 | await using sub = await forkContextWithScope(savedContext); |
| 538 | const h = new HandlerClass(ctx, sub.ctx); |
| 539 | ctx.handler = h; |
| 540 | const method = ctx.method.toLowerCase(); |
| 541 | const name = ((Object.hasOwn(HandlerClass, kHandler) && typeof HandlerClass[kHandler] === 'string') |
| 542 | ? HandlerClass[kHandler] : HandlerClass.name).replace(/Handler$/, ''); |
| 543 | this.activeHandlers.set(h, { start: Date.now(), name }); |
| 544 | try { |
| 545 | const operation = (method === 'post' && ctx.request.body?.operation) |
| 546 | // eslint-disable-next-line regexp/no-unused-capturing-group |
| 547 | ? `_${ctx.request.body.operation}`.replace(/_([a-z])/g, (s) => s[1].toUpperCase()) |
| 548 | : ''; |
| 549 | |
| 550 | // FIXME: should pass type check |
| 551 | await (this.ctx.parallel as any)('handler/create', h, 'http'); |
| 552 | await (this.ctx.parallel as any)('handler/create/http', h); |
| 553 | |
| 554 | if (checker) checker.call(h); |
| 555 | if (typeof h.all !== 'function') { |
| 556 | if (method === 'post') { |
| 557 | if (operation) { |
| 558 | if (typeof h[`post${operation}`] !== 'function') { |
| 559 | throw new InvalidOperationError(operation); |
| 560 | } |
| 561 | } else if (typeof h.post !== 'function') { |
| 562 | throw new MethodNotAllowedError(method); |
| 563 | } |
| 564 | } else if (typeof h[method] !== 'function') { |
| 565 | throw new MethodNotAllowedError(method); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | const steps = [ |
| 570 | 'log/__init', 'init', 'handler/init', |
| 571 | `handler/before-prepare/${name}#${method}`, `handler/before-prepare/${name}`, 'handler/before-prepare', |
| 572 | 'log/__prepare', '__prepare', '_prepare', 'prepare', 'log/__prepareDone', |
| 573 | `handler/before/${name}#${method}`, `handler/before/${name}`, 'handler/before', |
| 574 | 'log/__method', 'all', method, 'log/__methodDone', |
| 575 | ...operation ? [ |
| 576 | `handler/before-operation/${name}`, 'handler/before-operation', |
| 577 | `post${operation}`, 'log/__operationDone', |
| 578 | ] : [], 'after', |
| 579 | `handler/after/${name}#${method}`, `handler/after/${name}`, 'handler/after', |
| 580 | 'cleanup', |
| 581 | `handler/finish/${name}#${method}`, `handler/finish/${name}`, 'handler/finish', |
| 582 | 'log/__finish', |
| 583 | ]; |
| 584 | |
| 585 | let current = 0; |
| 586 | while (current < steps.length) { |
| 587 | const step = steps[current]; |
| 588 | let control; |
| 589 | if (step.startsWith('log/')) h.args[step.slice(4)] = Date.now(); |
| 590 | // @ts-ignore |
| 591 | else if (step.startsWith('handler/')) control = await this.ctx.serial(step, h); // eslint-disable-line no-await-in-loop |
no test coverage detected