raiton 6.8.8-beta.24 → 6.10.0-beta.28
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.
- package/CHANGELOG.md +50 -0
- package/README.md +46 -1
- package/benchmarks/README.md +16 -0
- package/benchmarks/latest.json +172 -0
- package/benchmarks/latest.md +66 -0
- package/build/bin/index.mjs +33575 -21723
- package/build/types/bin/bootstrapper.d.ts +2 -0
- package/build/types/bin/cli-output.d.ts +46 -0
- package/build/types/bin/cli-tools.d.ts +13 -0
- package/build/types/bin/cli.d.ts +3 -0
- package/build/types/bin/commands.registry.d.ts +4 -0
- package/build/types/bin/constants.d.ts +3 -0
- package/build/types/bin/file-utils.d.ts +21 -0
- package/build/types/bin/index.d.ts +2 -0
- package/build/types/bin/name-utils.d.ts +4 -0
- package/build/types/bin/project-utils.d.ts +12 -0
- package/build/types/commands/artifact.command.d.ts +7 -0
- package/build/types/commands/build.command.d.ts +8 -0
- package/build/types/commands/client.command.d.ts +7 -0
- package/build/types/commands/contract.command.d.ts +8 -0
- package/build/types/commands/develop.command.d.ts +9 -0
- package/build/types/commands/docs.command.d.ts +8 -0
- package/build/types/commands/doctor.command.d.ts +9 -0
- package/build/types/commands/generate.command.d.ts +7 -0
- package/build/types/commands/inspect.command.d.ts +7 -0
- package/build/types/commands/mcp.command.d.ts +11 -0
- package/build/types/commands/new.command.d.ts +7 -0
- package/build/types/commands/openapi.command.d.ts +7 -0
- package/build/types/commands/routes.command.d.ts +7 -0
- package/build/types/commands/start.command.d.ts +9 -0
- package/build/types/commands/why.command.d.ts +7 -0
- package/build/types/contracts/decorators.d.ts +41 -0
- package/build/types/contracts/errors.d.ts +42 -0
- package/build/types/contracts/index.d.ts +4 -0
- package/build/types/contracts/schema.d.ts +70 -0
- package/build/types/contracts/typebox.d.ts +81 -0
- package/build/types/core/application.d.ts +37 -0
- package/build/types/core/artifacts/artifact.d.ts +3 -0
- package/build/types/core/artifacts/index.d.ts +1 -0
- package/build/types/core/artifacts/runner.d.ts +1 -0
- package/build/types/core/builder.d.ts +42 -0
- package/build/types/core/bytes.util.d.ts +3 -0
- package/build/types/core/command.d.ts +13 -0
- package/build/types/core/commands.d.ts +10 -0
- package/build/types/core/config/config.d.ts +27 -0
- package/build/types/core/config/define.d.ts +8 -0
- package/build/types/core/config/index.d.ts +3 -0
- package/build/types/core/config/schema.d.ts +24 -0
- package/build/types/core/context.d.ts +11 -0
- package/build/types/core/controller/builder.d.ts +26 -0
- package/build/types/core/controller/compiler.d.ts +2 -0
- package/build/types/core/controller/index.d.ts +4 -0
- package/build/types/core/controller/metadata.d.ts +3 -0
- package/build/types/core/controller/tracker.d.ts +9 -0
- package/build/types/core/directories.d.ts +10 -0
- package/build/types/core/guards.d.ts +11 -0
- package/build/types/core/helpers/index.d.ts +1 -0
- package/build/types/core/helpers/raiton.d.ts +1 -0
- package/build/types/core/hooks.d.ts +14 -0
- package/build/types/core/index.d.ts +17 -0
- package/build/types/core/injection/index.d.ts +1 -0
- package/build/types/core/injection/injection.d.ts +37 -0
- package/build/types/core/mcp/builder.d.ts +2 -0
- package/build/types/core/mcp/index.d.ts +3 -0
- package/build/types/core/mcp/metadata.d.ts +9 -0
- package/build/types/core/mcp/tracker.d.ts +9 -0
- package/build/types/core/middleware/compose.d.ts +8 -0
- package/build/types/core/middleware/index.d.ts +2 -0
- package/build/types/core/middleware/pipeline.d.ts +12 -0
- package/build/types/core/plugins/index.d.ts +2 -0
- package/build/types/core/plugins/plugin.d.ts +2 -0
- package/build/types/core/plugins/scope.d.ts +15 -0
- package/build/types/core/process.util.d.ts +1 -0
- package/build/types/core/raiton.d.ts +15 -0
- package/build/types/core/router/handler.d.ts +2 -0
- package/build/types/core/router/handler.utils.d.ts +37 -0
- package/build/types/core/router/index.d.ts +4 -0
- package/build/types/core/router/matcher.d.ts +50 -0
- package/build/types/core/router/route.d.ts +52 -0
- package/build/types/core/router/router.d.ts +23 -0
- package/build/types/core/scheduler/builder.d.ts +2 -0
- package/build/types/core/scheduler/cron.d.ts +32 -0
- package/build/types/core/scheduler/duration.d.ts +7 -0
- package/build/types/core/scheduler/index.d.ts +6 -0
- package/build/types/core/scheduler/manager.d.ts +47 -0
- package/build/types/core/scheduler/metadata.d.ts +8 -0
- package/build/types/core/scheduler/queue/backend.d.ts +42 -0
- package/build/types/core/scheduler/queue/index.d.ts +3 -0
- package/build/types/core/scheduler/queue/rabbitmq.backend.d.ts +37 -0
- package/build/types/core/scheduler/queue/redis.backend.d.ts +38 -0
- package/build/types/core/server.d.ts +9 -0
- package/build/types/core/socket/builder.d.ts +1 -0
- package/build/types/core/socket/index.d.ts +2 -0
- package/build/types/core/socket/metadata.d.ts +14 -0
- package/build/types/core/thread.d.ts +21 -0
- package/build/types/framework/artifacts.d.ts +20 -0
- package/build/types/framework/cache/cache.d.ts +84 -0
- package/build/types/framework/cache/cache.decorator.d.ts +21 -0
- package/build/types/framework/cache/etag.d.ts +62 -0
- package/build/types/framework/cache/index.d.ts +4 -0
- package/build/types/framework/cache/types.d.ts +89 -0
- package/build/types/framework/constants/decorators.constant.d.ts +28 -0
- package/build/types/framework/constants/index.d.ts +2 -0
- package/build/types/framework/constants/microservices.constant.d.ts +3 -0
- package/build/types/framework/controllers.d.ts +2 -0
- package/build/types/framework/data-transfer-object.d.ts +5 -0
- package/build/types/framework/decorators/access-guard.decorator.d.ts +1 -0
- package/build/types/framework/decorators/api-response.decorator.d.ts +66 -0
- package/build/types/framework/decorators/api.decorator.d.ts +160 -0
- package/build/types/framework/decorators/controllable.decorator.d.ts +3 -0
- package/build/types/framework/decorators/csrf-guard.decorator.d.ts +2 -0
- package/build/types/framework/decorators/csrf-token.decorator.d.ts +1 -0
- package/build/types/framework/decorators/guard.decorator.d.ts +2 -0
- package/build/types/framework/decorators/health-check.decorator.d.ts +17 -0
- package/build/types/framework/decorators/index.d.ts +15 -0
- package/build/types/framework/decorators/injection.decorator.d.ts +5 -0
- package/build/types/framework/decorators/mcp.decorator.d.ts +68 -0
- package/build/types/framework/decorators/middleware.decorator.d.ts +3 -0
- package/build/types/framework/decorators/parametrable.decorator.d.ts +9 -0
- package/build/types/framework/decorators/routable.decorator.d.ts +12 -0
- package/build/types/framework/decorators/route-interaction.decorator.d.ts +81 -0
- package/build/types/framework/decorators/scheduler.decorator.d.ts +54 -0
- package/build/types/framework/decorators/socket.decorator.d.ts +5 -0
- package/build/types/framework/encryption.d.ts +25 -0
- package/build/types/framework/enums/csrf.enum.d.ts +4 -0
- package/build/types/framework/enums/encrypted.enum.d.ts +20 -0
- package/build/types/framework/enums/event.message.enum.d.ts +4 -0
- package/build/types/framework/enums/http-method.enum.d.ts +10 -0
- package/build/types/framework/enums/http-parameters.enum.d.ts +11 -0
- package/build/types/framework/enums/http-status.enum.d.ts +64 -0
- package/build/types/framework/enums/index.d.ts +9 -0
- package/build/types/framework/enums/runtime.enum.d.ts +5 -0
- package/build/types/framework/enums/scheduler.enum.d.ts +21 -0
- package/build/types/framework/enums/timestamp.enum.d.ts +6 -0
- package/build/types/framework/env.d.ts +3 -0
- package/build/types/framework/errors/catalog.d.ts +42 -0
- package/build/types/framework/exceptions/http-exception.d.ts +18 -0
- package/build/types/framework/exceptions/index.d.ts +2 -0
- package/build/types/framework/exceptions/throwable.d.ts +76 -0
- package/build/types/framework/health-check.d.ts +42 -0
- package/build/types/framework/index.d.ts +20 -0
- package/build/types/framework/mcp/audit.d.ts +12 -0
- package/build/types/framework/mcp/conformance.d.ts +31 -0
- package/build/types/framework/mcp/handler.d.ts +11 -0
- package/build/types/framework/mcp/index.d.ts +5 -0
- package/build/types/framework/mcp/protocol.d.ts +17 -0
- package/build/types/framework/mcp/rate-limit.d.ts +10 -0
- package/build/types/framework/mcp/stdio.d.ts +40 -0
- package/build/types/framework/mcp/streamable-http.d.ts +17 -0
- package/build/types/framework/observability/index.d.ts +6 -0
- package/build/types/framework/observability/metrics.d.ts +23 -0
- package/build/types/framework/observability/plugin.d.ts +20 -0
- package/build/types/framework/observability/readiness.d.ts +9 -0
- package/build/types/framework/observability/request.d.ts +7 -0
- package/build/types/framework/observability/shutdown.d.ts +21 -0
- package/build/types/framework/observability/trace.d.ts +96 -0
- package/build/types/framework/parameter-bag.d.ts +14 -0
- package/build/types/framework/plugins/body-parser.plugin.d.ts +3 -0
- package/build/types/framework/plugins/dev.plugin.d.ts +12 -0
- package/build/types/framework/plugins/index.d.ts +5 -0
- package/build/types/framework/plugins/mcp.plugin.d.ts +15 -0
- package/build/types/framework/plugins/openapi.plugin.d.ts +58 -0
- package/build/types/framework/plugins/security/body-limit.d.ts +1 -0
- package/build/types/framework/plugins/security/cors.d.ts +10 -0
- package/build/types/framework/plugins/security/csrf.d.ts +2 -0
- package/build/types/framework/plugins/security/headers.d.ts +1 -0
- package/build/types/framework/plugins/security/index.d.ts +13 -0
- package/build/types/framework/plugins/security/method-guard.d.ts +1 -0
- package/build/types/framework/plugins/security/rate-limit.d.ts +5 -0
- package/build/types/framework/repositories.d.ts +5 -0
- package/build/types/framework/responses/error.d.ts +13 -0
- package/build/types/framework/responses/helpers.d.ts +7 -0
- package/build/types/framework/responses/http-throwable.d.ts +14 -0
- package/build/types/framework/responses/http.d.ts +13 -0
- package/build/types/framework/responses/index.d.ts +4 -0
- package/build/types/framework/runtime/bun/server.d.ts +2 -0
- package/build/types/framework/runtime/deno/server.d.ts +2 -0
- package/build/types/framework/runtime/index.d.ts +17 -0
- package/build/types/framework/runtime/node/reply.d.ts +12 -0
- package/build/types/framework/runtime/node/request.d.ts +33 -0
- package/build/types/framework/runtime/node/server.d.ts +2 -0
- package/build/types/framework/runtime/web/server.d.ts +1 -0
- package/build/types/framework/services.d.ts +5 -0
- package/build/types/framework/utilities/alias-path.util.d.ts +1 -0
- package/build/types/framework/utilities/artifact.util.d.ts +7 -0
- package/build/types/framework/utilities/callable.util.d.ts +1 -0
- package/build/types/framework/utilities/cookie.util.d.ts +1 -0
- package/build/types/framework/utilities/csrf.util.d.ts +26 -0
- package/build/types/framework/utilities/https.util.d.ts +49 -0
- package/build/types/framework/utilities/index.d.ts +12 -0
- package/build/types/framework/utilities/ip.util.d.ts +15 -0
- package/build/types/framework/utilities/json.util.d.ts +10 -0
- package/build/types/framework/utilities/openapi.utils.d.ts +7 -0
- package/build/types/framework/utilities/openapi.utils.impl.d.ts +18 -0
- package/build/types/framework/utilities/path.util.d.ts +3 -0
- package/build/types/framework/utilities/route-suggestion.util.d.ts +10 -0
- package/build/types/framework/utilities/url.util.d.ts +1 -0
- package/build/types/framework/utilities/utilities.util.d.ts +3 -0
- package/build/types/framework/utilities/validation.util.d.ts +8 -0
- package/build/types/framework/view-model.d.ts +4 -0
- package/build/types/generators/artifact.templates.d.ts +7 -0
- package/build/types/generators/index.d.ts +2 -0
- package/build/types/generators/project.templates.d.ts +21 -0
- package/build/types/manifest/client.d.ts +12 -0
- package/build/types/manifest/contract.d.ts +25 -0
- package/build/types/manifest/index.d.ts +5 -0
- package/build/types/manifest/introspect.d.ts +18 -0
- package/build/types/manifest/manifest.d.ts +18 -0
- package/build/types/manifest/schema.d.ts +177 -0
- package/build/types/requirements.d.ts +1 -0
- package/build/types/types/access-guards.d.ts +4 -0
- package/build/types/types/application.d.ts +32 -0
- package/build/types/types/artifact.d.ts +8 -0
- package/build/types/types/builder.d.ts +37 -0
- package/build/types/types/config.d.ts +62 -0
- package/build/types/types/controller.d.ts +27 -0
- package/build/types/types/contruct.d.ts +1 -0
- package/build/types/types/core.d.ts +11 -0
- package/build/types/types/csrf.d.ts +33 -0
- package/build/types/types/directory.d.ts +6 -0
- package/build/types/types/encryption.d.ts +14 -0
- package/build/types/types/generic.d.ts +2 -0
- package/build/types/types/guard.d.ts +9 -0
- package/build/types/types/http-responses.d.ts +7 -0
- package/build/types/types/index.d.ts +27 -0
- package/build/types/types/injection.d.ts +11 -0
- package/build/types/types/lifecycle.d.ts +9 -0
- package/build/types/types/mcp.d.ts +148 -0
- package/build/types/types/middleware.d.ts +12 -0
- package/build/types/types/parseable.d.ts +3 -0
- package/build/types/types/plugin.d.ts +6 -0
- package/build/types/types/raiton.d.ts +13 -0
- package/build/types/types/responses.d.ts +20 -0
- package/build/types/types/router.d.ts +13 -0
- package/build/types/types/runtime.d.ts +48 -0
- package/build/types/types/scheduler.d.ts +140 -0
- package/build/types/types/scheme.d.ts +125 -0
- package/build/types/types/server.d.ts +10 -0
- package/build/types/types/socket.d.ts +11 -0
- package/build/types/types/thread.d.ts +25 -0
- package/build/types/types/utilities.d.ts +4 -0
- package/build/types/types/values.d.ts +2 -0
- package/docs/AGENTS.md +55 -0
- package/docs/CLAUDE.md +55 -0
- package/docs/README.md +5 -0
- package/docs/contributors/cli/README.md +17 -3
- package/docs/contributors/cli/commands.md +28 -17
- package/docs/contributors/framework/plugins/openapi.md +12 -3
- package/docs/generated/cli/commands.md +26 -0
- package/docs/generated/config-schema.json +150 -0
- package/docs/generated/manifest-schema.json +236 -0
- package/docs/generated/manifest.json +101 -0
- package/docs/llms-full.txt +1271 -0
- package/docs/llms.txt +46 -0
- package/docs/user-guide/README.md +18 -12
- package/docs/user-guide/cli.md +21 -0
- package/docs/user-guide/config.md +14 -0
- package/docs/user-guide/contracts.md +67 -0
- package/docs/user-guide/en/README.md +83 -0
- package/docs/user-guide/getting-started.md +13 -1
- package/docs/user-guide/mcp.md +45 -2
- package/docs/user-guide/observability.md +74 -0
- package/docs/user-guide/openapi/plugin.md +10 -0
- package/docs/user-guide/recipes/README.md +32 -0
- package/docs/user-guide/recipes/contracts.md +52 -0
- package/docs/user-guide/recipes/doctor.md +16 -0
- package/docs/user-guide/recipes/generate.md +28 -0
- package/docs/user-guide/recipes/manifest.md +17 -0
- package/docs/user-guide/recipes/mcp-stdio.md +35 -0
- package/docs/user-guide/recipes/new-project.md +34 -0
- package/docs/user-guide/recipes/observability.md +17 -0
- package/package.json +40 -7
- package/scripts/bench.ts +586 -0
- package/scripts/postinstall.mjs +15 -0
- package/source/bin/cli-output.ts +139 -0
- package/source/bin/commands.registry.ts +23 -3
- package/source/bin/file-utils.ts +95 -0
- package/source/bin/name-utils.ts +26 -0
- package/source/bin/project-utils.ts +78 -0
- package/source/commands/artifact.command.ts +30 -15
- package/source/commands/client.command.ts +64 -0
- package/source/commands/contract.command.ts +104 -0
- package/source/commands/docs.command.ts +263 -0
- package/source/commands/doctor.command.ts +226 -0
- package/source/commands/generate.command.ts +58 -0
- package/source/commands/inspect.command.ts +108 -0
- package/source/commands/mcp.command.ts +258 -0
- package/source/commands/new.command.ts +135 -0
- package/source/commands/openapi.command.ts +95 -0
- package/source/commands/routes.command.ts +39 -0
- package/source/commands/why.command.ts +61 -0
- package/source/contracts/decorators.ts +151 -0
- package/source/contracts/errors.ts +76 -0
- package/source/contracts/index.ts +4 -0
- package/source/contracts/schema.ts +377 -0
- package/source/contracts/typebox.ts +193 -0
- package/source/core/application.ts +215 -73
- package/source/core/builder.ts +6 -2
- package/source/core/config/config.ts +144 -26
- package/source/core/config/define.ts +35 -8
- package/source/core/config/index.ts +2 -1
- package/source/core/config/schema.ts +263 -0
- package/source/core/controller/builder.ts +38 -13
- package/source/core/controller/compiler.ts +14 -7
- package/source/core/controller/metadata.ts +1 -1
- package/source/core/hooks.ts +13 -1
- package/source/core/injection/injection.ts +47 -9
- package/source/core/mcp/builder.ts +13 -2
- package/source/core/mcp/metadata.ts +2 -1
- package/source/core/middleware/compose.ts +48 -0
- package/source/core/middleware/pipeline.ts +69 -46
- package/source/core/raiton.ts +8 -0
- package/source/core/router/handler.ts +91 -31
- package/source/core/router/handler.utils.ts +107 -51
- package/source/core/router/matcher.ts +195 -24
- package/source/core/router/route.ts +100 -31
- package/source/core/router/router.ts +27 -2
- package/source/core/scheduler/metadata.ts +1 -1
- package/source/core/socket/metadata.ts +1 -1
- package/source/core/thread.ts +37 -2
- package/source/framework/artifacts.ts +92 -19
- package/source/framework/cache/cache.ts +48 -3
- package/source/framework/constants/decorators.constant.ts +5 -0
- package/source/framework/data-transfer-object.ts +4 -3
- package/source/framework/decorators/controllable.decorator.ts +3 -3
- package/source/framework/decorators/csrf-guard.decorator.ts +3 -2
- package/source/framework/decorators/csrf-token.decorator.ts +1 -1
- package/source/framework/decorators/guard.decorator.ts +3 -2
- package/source/framework/decorators/health-check.decorator.ts +1 -1
- package/source/framework/decorators/mcp.decorator.ts +38 -7
- package/source/framework/decorators/middleware.decorator.ts +2 -2
- package/source/framework/decorators/parametrable.decorator.ts +2 -1
- package/source/framework/decorators/routable.decorator.ts +3 -3
- package/source/framework/decorators/route-interaction.decorator.ts +2 -2
- package/source/framework/decorators/scheduler.decorator.ts +3 -3
- package/source/framework/decorators/socket.decorator.ts +3 -3
- package/source/framework/encryption.ts +46 -7
- package/source/framework/errors/catalog.ts +159 -0
- package/source/framework/exceptions/http-exception.ts +1 -1
- package/source/framework/health-check.ts +18 -0
- package/source/framework/index.ts +2 -0
- package/source/framework/mcp/audit.ts +57 -0
- package/source/framework/mcp/conformance.ts +195 -0
- package/source/framework/mcp/handler.ts +131 -9
- package/source/framework/mcp/index.ts +6 -1
- package/source/framework/mcp/protocol.ts +6 -0
- package/source/framework/mcp/rate-limit.ts +33 -0
- package/source/framework/mcp/stdio.ts +150 -0
- package/source/framework/mcp/streamable-http.ts +36 -5
- package/source/framework/observability/index.ts +6 -0
- package/source/framework/observability/metrics.ts +162 -0
- package/source/framework/observability/plugin.ts +151 -0
- package/source/framework/observability/readiness.ts +22 -0
- package/source/framework/observability/request.ts +31 -0
- package/source/framework/observability/shutdown.ts +68 -0
- package/source/framework/observability/trace.ts +241 -0
- package/source/framework/plugins/body-parser.plugin.ts +88 -85
- package/source/framework/plugins/dev.plugin.ts +227 -0
- package/source/framework/plugins/index.ts +2 -1
- package/source/framework/plugins/mcp.plugin.ts +14 -3
- package/source/framework/plugins/openapi.plugin.ts +56 -13
- package/source/framework/responses/helpers.ts +4 -4
- package/source/framework/responses/http-throwable.ts +1 -1
- package/source/framework/runtime/bun/server.ts +2 -1
- package/source/framework/runtime/deno/server.ts +2 -1
- package/source/framework/runtime/index.ts +45 -13
- package/source/framework/runtime/node/request.ts +59 -13
- package/source/framework/utilities/index.ts +3 -1
- package/source/framework/utilities/openapi.utils.impl.ts +705 -0
- package/source/framework/utilities/openapi.utils.ts +6 -636
- package/source/framework/utilities/route-suggestion.util.ts +59 -0
- package/source/framework/utilities/validation.util.ts +20 -0
- package/source/generators/artifact.templates.ts +230 -0
- package/source/generators/index.ts +2 -0
- package/source/generators/project.templates.ts +392 -0
- package/source/manifest/client.ts +299 -0
- package/source/manifest/contract.ts +138 -0
- package/source/manifest/index.ts +5 -0
- package/source/manifest/introspect.ts +52 -0
- package/source/manifest/manifest.ts +341 -0
- package/source/manifest/schema.ts +149 -0
- package/source/types/builder.ts +5 -0
- package/source/types/config.ts +64 -2
- package/source/types/mcp.ts +63 -1
- package/tsconfig.build.json +13 -0
- package/source/commands/grafts.command.ts +0 -27
- package/tests/artifact-resolution.test.ts +0 -60
|
@@ -3,31 +3,51 @@ import {RequestContext} from "../../core/context.ts";
|
|
|
3
3
|
import {Logger} from "@protorians/logger";
|
|
4
4
|
import {tryParseJson} from "../utilities/json.util.ts";
|
|
5
5
|
|
|
6
|
+
const textDecoder = new TextDecoder()
|
|
7
|
+
const installedScopes = new WeakSet<object>()
|
|
8
|
+
|
|
6
9
|
export function bodyParserPlugin(): PluginInterface {
|
|
7
10
|
return {
|
|
8
11
|
name: 'body-parser-plugin',
|
|
9
12
|
setup: (scope) => {
|
|
13
|
+
// Installing the plugin twice would run the parser twice and read
|
|
14
|
+
// the request stream a second time.
|
|
15
|
+
if (installedScopes.has(scope as object)) return
|
|
16
|
+
installedScopes.add(scope as object)
|
|
17
|
+
|
|
10
18
|
scope.use(async ({context, next}: MiddlewareParametersInterface) => {
|
|
11
|
-
|
|
19
|
+
const url = getRequestUrl(context)
|
|
20
|
+
readQueryBody(context, url);
|
|
12
21
|
|
|
13
|
-
const
|
|
22
|
+
const method = context.req.method.toUpperCase();
|
|
14
23
|
|
|
15
|
-
|
|
24
|
+
// GET/HEAD never carry a body: expose the already-parsed query
|
|
25
|
+
// without touching content-type or the body stream.
|
|
26
|
+
if (method === 'GET' || method === 'HEAD') {
|
|
16
27
|
context.req.body = context.req.query;
|
|
28
|
+
await next();
|
|
29
|
+
return;
|
|
17
30
|
}
|
|
18
31
|
|
|
19
32
|
const hasBody = context.req.body !== undefined && context.req.body !== null;
|
|
20
33
|
|
|
21
|
-
if (hasBody && !context.state.bodyParsed
|
|
34
|
+
if (hasBody && !context.state.bodyParsed) {
|
|
22
35
|
try {
|
|
23
36
|
const contentType = context.req.headers.get('content-type') || '';
|
|
24
37
|
const rawBody = await readRawBody(context.req.body);
|
|
25
38
|
const bodyString = decode(rawBody);
|
|
26
39
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
40
|
+
if (contentType.includes('multipart/form-data')) {
|
|
41
|
+
await readMultipartBody(context, rawBody);
|
|
42
|
+
} else if (contentType.includes('application/json')) {
|
|
43
|
+
context.req.body = tryParseJson(bodyString) || {};
|
|
44
|
+
context.state.bodyParsed = true;
|
|
45
|
+
} else if (contentType.includes('application/x-www-form-urlencoded')) {
|
|
46
|
+
readUrlEncodedBody(context, bodyString);
|
|
47
|
+
} else if (contentType.includes('text/')) {
|
|
48
|
+
context.req.body = bodyString as unknown as any;
|
|
49
|
+
context.state.bodyParsed = true;
|
|
50
|
+
}
|
|
31
51
|
|
|
32
52
|
if (!context.state.bodyParsed) {
|
|
33
53
|
context.req.body = rawBody as any;
|
|
@@ -45,105 +65,88 @@ export function bodyParserPlugin(): PluginInterface {
|
|
|
45
65
|
};
|
|
46
66
|
}
|
|
47
67
|
|
|
48
|
-
function
|
|
49
|
-
|
|
50
|
-
ctx.req.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
async function readMultipartBody(ctx: RequestContext, contentType: string, rawBody: any) {
|
|
56
|
-
if (contentType.includes('multipart/form-data')) {
|
|
57
|
-
try {
|
|
58
|
-
const tempRequest = new Request(ctx.req.url, {
|
|
59
|
-
method: ctx.req.method,
|
|
60
|
-
headers: ctx.req.headers,
|
|
61
|
-
body: rawBody
|
|
62
|
-
});
|
|
68
|
+
async function readMultipartBody(ctx: RequestContext, rawBody: Uint8Array | string) {
|
|
69
|
+
try {
|
|
70
|
+
const tempRequest = new Request(ctx.req.url, {
|
|
71
|
+
method: ctx.req.method,
|
|
72
|
+
headers: ctx.req.headers,
|
|
73
|
+
body: rawBody as BodyInit
|
|
74
|
+
});
|
|
63
75
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
76
|
+
const formData = await tempRequest.formData();
|
|
77
|
+
const record: Record<string, any> = {};
|
|
78
|
+
const files: Record<string, any> = {};
|
|
79
|
+
|
|
80
|
+
for (const [key, value] of formData.entries()) {
|
|
81
|
+
const v = value as any;
|
|
82
|
+
if (v && (v instanceof File)) {
|
|
83
|
+
files[key] = {
|
|
84
|
+
name: v.name,
|
|
85
|
+
type: v.type,
|
|
86
|
+
size: v.size,
|
|
87
|
+
lastModified: v.lastModified,
|
|
88
|
+
buffer: Buffer.from(await v.arrayBuffer())
|
|
89
|
+
};
|
|
90
|
+
} else {
|
|
91
|
+
record[key] = value;
|
|
81
92
|
}
|
|
82
|
-
|
|
83
|
-
ctx.req.body = record;
|
|
84
|
-
ctx.req.files = files;
|
|
85
|
-
ctx.req.file = Object.values(files)[0] || null;
|
|
86
|
-
ctx.state.bodyParsed = true;
|
|
87
|
-
} catch (e) {
|
|
88
|
-
Logger.error('Error parsing multipart body:', e);
|
|
89
93
|
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
94
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const record: Record<string, any> = {};
|
|
97
|
-
params.forEach((value, key) => {
|
|
98
|
-
record[key] = value;
|
|
99
|
-
});
|
|
100
|
-
ctx.req.body = record as unknown as any;
|
|
95
|
+
ctx.req.body = record;
|
|
96
|
+
ctx.req.files = files;
|
|
97
|
+
ctx.req.file = Object.values(files)[0] || null;
|
|
101
98
|
ctx.state.bodyParsed = true;
|
|
99
|
+
} catch (e) {
|
|
100
|
+
Logger.error('Error parsing multipart body:', e);
|
|
102
101
|
}
|
|
103
102
|
}
|
|
104
103
|
|
|
105
|
-
function
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
104
|
+
function readUrlEncodedBody(ctx: RequestContext, body: string) {
|
|
105
|
+
const params = new URLSearchParams(body);
|
|
106
|
+
const record: Record<string, any> = {};
|
|
107
|
+
params.forEach((value, key) => {
|
|
108
|
+
record[key] = value;
|
|
109
|
+
});
|
|
110
|
+
ctx.req.body = record as unknown as any;
|
|
111
|
+
ctx.state.bodyParsed = true;
|
|
110
112
|
}
|
|
111
113
|
|
|
112
|
-
function
|
|
114
|
+
function getRequestUrl(ctx: RequestContext): URL {
|
|
115
|
+
const cached = ctx.state?.url;
|
|
116
|
+
if (cached instanceof URL) return cached
|
|
117
|
+
|
|
118
|
+
const url = new URL(ctx.req.url, 'http://localhost')
|
|
119
|
+
ctx.state.url = url
|
|
120
|
+
return url
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function readQueryBody(ctx: RequestContext, url: URL) {
|
|
124
|
+
// Avoid allocating URLSearchParams when the request has no query string.
|
|
125
|
+
if (!url.search) {
|
|
126
|
+
ctx.req.query = {} as unknown as any;
|
|
127
|
+
return
|
|
128
|
+
}
|
|
113
129
|
|
|
114
|
-
const searchParams = new URLSearchParams(ctx.req.url.split('?')[1] || '');
|
|
115
130
|
const record = {} as Record<string, any>;
|
|
116
|
-
searchParams.forEach((value, key) => record[key] = value);
|
|
131
|
+
url.searchParams.forEach((value, key) => record[key] = value);
|
|
117
132
|
ctx.req.query = record as unknown as any;
|
|
118
133
|
}
|
|
119
134
|
|
|
120
|
-
async function readRawBody(body: any): Promise<Uint8Array | string> {
|
|
135
|
+
export async function readRawBody(body: any): Promise<Uint8Array | string> {
|
|
121
136
|
if (!body) return new Uint8Array(0);
|
|
122
137
|
|
|
123
138
|
if (body instanceof Uint8Array || typeof body === 'string') {
|
|
124
139
|
return body;
|
|
125
140
|
}
|
|
126
141
|
|
|
127
|
-
|
|
128
|
-
|
|
142
|
+
// Fetch/Web streams (Bun, Deno, Workers): let the runtime decode the body
|
|
143
|
+
// through the native Response path instead of a manual reader loop.
|
|
144
|
+
if (body instanceof ReadableStream) {
|
|
145
|
+
return new Uint8Array(await new Response(body).arrayBuffer());
|
|
129
146
|
}
|
|
130
147
|
|
|
131
|
-
if (body
|
|
132
|
-
|
|
133
|
-
const chunks: Uint8Array[] = [];
|
|
134
|
-
while (true) {
|
|
135
|
-
const {done, value} = await reader.read();
|
|
136
|
-
if (done) break;
|
|
137
|
-
chunks.push(value);
|
|
138
|
-
}
|
|
139
|
-
const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
|
|
140
|
-
const result = new Uint8Array(totalLength);
|
|
141
|
-
let offset = 0;
|
|
142
|
-
for (const chunk of chunks) {
|
|
143
|
-
result.set(chunk, offset);
|
|
144
|
-
offset += chunk.length;
|
|
145
|
-
}
|
|
146
|
-
return result;
|
|
148
|
+
if (typeof body === 'object' && !('on' in body)) {
|
|
149
|
+
return JSON.stringify(body);
|
|
147
150
|
}
|
|
148
151
|
|
|
149
152
|
if (typeof body === 'object' && body !== null && 'on' in body && typeof body.on === 'function') {
|
|
@@ -173,5 +176,5 @@ async function readRawBody(body: any): Promise<Uint8Array | string> {
|
|
|
173
176
|
|
|
174
177
|
function decode(data: Uint8Array | string): string {
|
|
175
178
|
if (typeof data === 'string') return data;
|
|
176
|
-
return
|
|
179
|
+
return textDecoder.decode(data);
|
|
177
180
|
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import type {PluginInterface} from "../../types/plugin.ts";
|
|
2
|
+
import type {ContextInterface} from "../../types/core.ts";
|
|
3
|
+
import type {PluginScope} from "../../core/plugins/scope.ts";
|
|
4
|
+
import {HealthCheckManager} from "../health-check.ts";
|
|
5
|
+
import {Readiness} from "../observability/readiness.ts";
|
|
6
|
+
import {Raiton} from "../../core/raiton.ts";
|
|
7
|
+
|
|
8
|
+
export interface DevPageOptions {
|
|
9
|
+
path?: string;
|
|
10
|
+
enabled?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface HmrEvent {
|
|
14
|
+
time: string;
|
|
15
|
+
artifact: string;
|
|
16
|
+
file: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const hmrEvents: HmrEvent[] = [];
|
|
20
|
+
|
|
21
|
+
function recordHmrEvent(filename: string, artifactType?: string) {
|
|
22
|
+
hmrEvents.unshift({
|
|
23
|
+
time: new Date().toISOString(),
|
|
24
|
+
artifact: artifactType ?? "unknown",
|
|
25
|
+
file: filename,
|
|
26
|
+
});
|
|
27
|
+
hmrEvents.splice(25);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function escape(value: unknown): string {
|
|
31
|
+
return String(value ?? "")
|
|
32
|
+
.replace(/&/g, "&")
|
|
33
|
+
.replace(/</g, "<")
|
|
34
|
+
.replace(/>/g, ">")
|
|
35
|
+
.replace(/"/g, """)
|
|
36
|
+
.replace(/'/g, "'");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function routesTable(routes: any[]): string {
|
|
40
|
+
return routes.map(route => `<tr>
|
|
41
|
+
<td><code>${escape(route.method)}</code></td>
|
|
42
|
+
<td><code>${escape(route.path)}</code></td>
|
|
43
|
+
<td>${escape(route.controller)}.${escape(route.handler)}</td>
|
|
44
|
+
<td><code>${escape(route.file)}${route.line ? `:${route.line}` : ""}</code></td>
|
|
45
|
+
</tr>`).join("");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function injectionList(injection: Record<string, string[]>): string {
|
|
49
|
+
return Object.entries(injection).map(([name, dependencies]) => `<li><code>${escape(name)}</code>${
|
|
50
|
+
dependencies.length ? ` → ${dependencies.map(dep => `<code>${escape(dep)}</code>`).join(", ")}` : ""
|
|
51
|
+
}</li>`).join("");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function publicPath(path: string): string {
|
|
55
|
+
const prefix = ((Raiton.thread?.application?.config?.prefix ?? "") as string).replace(/\/$/, "");
|
|
56
|
+
return `${prefix}${path.startsWith("/") ? path : `/${path}`}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function renderPage(path: string): Promise<string> {
|
|
60
|
+
const [{generateManifest}, {SchedulerManager}] = await Promise.all([
|
|
61
|
+
import("../../manifest/manifest.ts"),
|
|
62
|
+
import("../../core/scheduler/manager.ts"),
|
|
63
|
+
])
|
|
64
|
+
const manifest = generateManifest({refresh: true});
|
|
65
|
+
const health = HealthCheckManager.getConfig();
|
|
66
|
+
const readiness = Readiness.snapshot();
|
|
67
|
+
const routes = manifest.routes;
|
|
68
|
+
const manifestPath = publicPath(`${path}/manifest`);
|
|
69
|
+
const schedulerJobs = SchedulerManager.current.getJobs();
|
|
70
|
+
|
|
71
|
+
return `<!DOCTYPE html>
|
|
72
|
+
<html lang="en">
|
|
73
|
+
<head>
|
|
74
|
+
<meta charset="utf-8" />
|
|
75
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
76
|
+
<title>Raiton — Development</title>
|
|
77
|
+
<style>
|
|
78
|
+
:root { color-scheme: light dark; font-family: ui-sans-serif, system-ui, sans-serif; }
|
|
79
|
+
body { margin: 0; padding: 2rem; background: #0b1020; color: #e8ecff; }
|
|
80
|
+
h1 { margin: 0 0 .25rem; font-size: 1.6rem; }
|
|
81
|
+
h2 { margin: 2rem 0 .75rem; font-size: 1.05rem; text-transform: uppercase; letter-spacing: .08em; color: #8ea1ff; }
|
|
82
|
+
.muted { color: #94a3b8; }
|
|
83
|
+
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem; }
|
|
84
|
+
.card { background: #121936; border: 1px solid #24305e; border-radius: .8rem; padding: 1rem; }
|
|
85
|
+
table { width: 100%; border-collapse: collapse; font-size: .85rem; }
|
|
86
|
+
th, td { text-align: left; padding: .45rem .5rem; border-bottom: 1px solid #24305e; vertical-align: top; }
|
|
87
|
+
code { background: #0b1020; padding: .1rem .3rem; border-radius: .25rem; }
|
|
88
|
+
a { color: #8ea1ff; }
|
|
89
|
+
button { background: #2b47b8; color: white; border: 0; border-radius: .45rem; padding: .45rem .8rem; cursor: pointer; }
|
|
90
|
+
ul { margin: .25rem 0; padding-left: 1.2rem; }
|
|
91
|
+
.pill { display: inline-block; padding: .1rem .45rem; border-radius: 999px; font-size: .75rem; background: ${readiness.ready ? "#14532d" : "#7f1d1d"}; }
|
|
92
|
+
</style>
|
|
93
|
+
</head>
|
|
94
|
+
<body>
|
|
95
|
+
<h1>Raiton development</h1>
|
|
96
|
+
<p class="muted">${escape(manifest.framework.name)} ${escape(manifest.framework.version)} · runtime ${escape(manifest.runtime)} · manifest ${escape(manifest.schemaVersion)}</p>
|
|
97
|
+
<p>Readiness <span class="pill">${readiness.ready ? "ready" : escape(readiness.reason ?? "not ready")}</span></p>
|
|
98
|
+
|
|
99
|
+
<div class="grid">
|
|
100
|
+
<div class="card">
|
|
101
|
+
<h2 style="margin-top:0">Health</h2>
|
|
102
|
+
<p class="muted">${health.enabled ? `GET ${escape(publicPath(health.path))}` : "disabled"}</p>
|
|
103
|
+
</div>
|
|
104
|
+
<div class="card">
|
|
105
|
+
<h2 style="margin-top:0">API documentation</h2>
|
|
106
|
+
<p><a href="${escape(publicPath("/docs"))}">${escape(publicPath("/docs"))}</a> · <a href="${escape(publicPath("/docs/json"))}">${escape(publicPath("/docs/json"))}</a></p>
|
|
107
|
+
</div>
|
|
108
|
+
<div class="card">
|
|
109
|
+
<h2 style="margin-top:0">MCP</h2>
|
|
110
|
+
<p>${manifest.mcp.length ? manifest.mcp.map(server => `<a href="${escape(publicPath(server.path))}">${escape(server.server)}</a>`).join(" · ") : "no server registered"}</p>
|
|
111
|
+
</div>
|
|
112
|
+
<div class="card">
|
|
113
|
+
<h2 style="margin-top:0">Scheduler</h2>
|
|
114
|
+
<p class="muted">${schedulerJobs.length} job(s) registered</p>
|
|
115
|
+
</div>
|
|
116
|
+
<div class="card">
|
|
117
|
+
<h2 style="margin-top:0">Manifest</h2>
|
|
118
|
+
<p><a href="${escape(manifestPath)}">${escape(manifestPath)}</a></p>
|
|
119
|
+
<p><button id="copy">Copy manifest</button> <span id="copied" class="muted"></span></p>
|
|
120
|
+
</div>
|
|
121
|
+
</div>
|
|
122
|
+
|
|
123
|
+
<h2>Routes (${routes.length})</h2>
|
|
124
|
+
<table><thead><tr><th>Method</th><th>Path</th><th>Handler</th><th>Source</th></tr></thead>
|
|
125
|
+
<tbody>${routesTable(routes) || `<tr><td colspan="4" class="muted">No route</td></tr>`}</tbody></table>
|
|
126
|
+
|
|
127
|
+
<h2>Dependency injection</h2>
|
|
128
|
+
<ul>${injectionList(manifest.injection) || `<li class="muted">No registered dependency</li>`}</ul>
|
|
129
|
+
|
|
130
|
+
<h2>Artifacts</h2>
|
|
131
|
+
<p>${manifest.artifacts.map(artifact => `<code>${escape(artifact)}</code>`).join(" ") || `<span class="muted">none</span>`}</p>
|
|
132
|
+
|
|
133
|
+
<h2>Recent HMR events</h2>
|
|
134
|
+
<ul>${hmrEvents.map(event => `<li>${escape(event.time)} · <code>${escape(event.artifact)}</code> · ${escape(event.file)}</li>`).join("") || `<li class="muted">No reload since start</li>`}</ul>
|
|
135
|
+
|
|
136
|
+
<h2>Environment variables (names only)</h2>
|
|
137
|
+
<p>${manifest.env.map(name => `<code>${escape(name)}</code>`).join(" ") || `<span class="muted">none detected</span>`}</p>
|
|
138
|
+
|
|
139
|
+
<script>
|
|
140
|
+
document.getElementById('copy')?.addEventListener('click', async () => {
|
|
141
|
+
const response = await fetch('${escape(manifestPath)}');
|
|
142
|
+
const text = await response.text();
|
|
143
|
+
await navigator.clipboard.writeText(text);
|
|
144
|
+
document.getElementById('copied').textContent = 'Copied';
|
|
145
|
+
});
|
|
146
|
+
</script>
|
|
147
|
+
</body>
|
|
148
|
+
</html>`;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function routeOrigin(method: string, path: string) {
|
|
152
|
+
const {generateManifest} = await import("../../manifest/manifest.ts")
|
|
153
|
+
const manifest = generateManifest({refresh: true});
|
|
154
|
+
const normalized = path.replace(/\/+$/, "") || "/";
|
|
155
|
+
const route = manifest.routes.find(item =>
|
|
156
|
+
item.method.toUpperCase() === method.toUpperCase()
|
|
157
|
+
&& (item.path.replace(/\/+$/, "") || "/") === normalized
|
|
158
|
+
);
|
|
159
|
+
if (!route) return undefined;
|
|
160
|
+
return {
|
|
161
|
+
route: `${route.method} ${route.path}`,
|
|
162
|
+
file: route.file,
|
|
163
|
+
line: route.line,
|
|
164
|
+
controller: route.controller,
|
|
165
|
+
handler: route.handler,
|
|
166
|
+
decorator: `@${route.method.charAt(0)}${route.method.slice(1).toLowerCase()}("${route.path}")`,
|
|
167
|
+
artifact: route.controller === "Application" ? "application" : "controller",
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Development-only page and manifest endpoint. It is mounted by `Application`
|
|
173
|
+
* only when HMR/development is active, so production never exposes it.
|
|
174
|
+
*/
|
|
175
|
+
export function devPagePlugin(options: DevPageOptions = {}): PluginInterface {
|
|
176
|
+
const route = options.path || "/__dev";
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
name: "dev-page-plugin",
|
|
180
|
+
setup: (scope: PluginScope) => {
|
|
181
|
+
if (options.enabled === false) return;
|
|
182
|
+
|
|
183
|
+
scope.get(route, async ({reply}: ContextInterface) => {
|
|
184
|
+
reply.type("text/html; charset=utf-8");
|
|
185
|
+
return await renderPage(route);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
scope.get(`${route}/manifest`, async ({reply}: ContextInterface) => {
|
|
189
|
+
reply.type("application/json");
|
|
190
|
+
const {generateManifest} = await import("../../manifest/manifest.ts")
|
|
191
|
+
return generateManifest({refresh: true});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
scope.get(`${route}/routes`, async ({reply}: ContextInterface) => {
|
|
195
|
+
reply.type("application/json");
|
|
196
|
+
const {generateManifest} = await import("../../manifest/manifest.ts")
|
|
197
|
+
return generateManifest({refresh: true}).routes;
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
scope.get(`${route}/why`, async ({req, reply}: ContextInterface) => {
|
|
201
|
+
const method = String((req as any).query?.method ?? "GET");
|
|
202
|
+
const path = String((req as any).query?.path ?? req.url.split("?")[0]);
|
|
203
|
+
const origin = await routeOrigin(method, path);
|
|
204
|
+
reply.type("application/json");
|
|
205
|
+
reply.status(origin ? 200 : 404);
|
|
206
|
+
return origin ?? {error: true, code: "NOT_FOUND", message: `Route ${method} ${path} not found`};
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
scope.get(`${route}/openapi`, async ({reply}: ContextInterface) => {
|
|
210
|
+
reply.type("application/json");
|
|
211
|
+
const {generateOpenApiSpec} = await import("../utilities/openapi.utils.ts")
|
|
212
|
+
return generateOpenApiSpec(scope, {title: "API", version: "1.0.0", description: ""});
|
|
213
|
+
});
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
let watchingHmr = false;
|
|
219
|
+
|
|
220
|
+
/** Record HMR reloads for the dev page. */
|
|
221
|
+
export function watchDevPageHmr(): void {
|
|
222
|
+
if (watchingHmr) return;
|
|
223
|
+
watchingHmr = true;
|
|
224
|
+
Raiton.signals.listen("hmr:controller", async ({filename}) => recordHmrEvent(filename, "controller"));
|
|
225
|
+
Raiton.signals.listen("hmr:di", async ({filename}) => recordHmrEvent(filename, "di"));
|
|
226
|
+
Raiton.signals.listen("hmr:mcp", async ({filename}) => recordHmrEvent(filename, "mcp"));
|
|
227
|
+
}
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import type {PluginInterface} from "../../types/index.ts";
|
|
2
2
|
import type {ContextInterface} from "../../types/core.ts";
|
|
3
|
-
import {PluginScope} from "../../core/
|
|
3
|
+
import type {PluginScope} from "../../core/plugins/scope.ts";
|
|
4
4
|
import {findMcpServerForPath} from "../../core/mcp/metadata.ts";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
isMcpJsonRpc,
|
|
7
|
+
mcpContextFromHeaders,
|
|
8
|
+
mcpHandleGet,
|
|
9
|
+
mcpHandlePost,
|
|
10
|
+
SESSION_HEADER
|
|
11
|
+
} from "../mcp/streamable-http.ts";
|
|
6
12
|
|
|
7
13
|
export interface McpPluginOptions {
|
|
8
14
|
/**
|
|
@@ -41,7 +47,12 @@ export function mcpPlugin(options: McpPluginOptions = {}): PluginInterface {
|
|
|
41
47
|
return {error: {code: -32700, message: 'Invalid JSON-RPC request'}}
|
|
42
48
|
}
|
|
43
49
|
|
|
44
|
-
const result = await mcpHandlePost(
|
|
50
|
+
const result = await mcpHandlePost(
|
|
51
|
+
entry,
|
|
52
|
+
context.req.body,
|
|
53
|
+
existingSessionId,
|
|
54
|
+
mcpContextFromHeaders(context.req.headers),
|
|
55
|
+
)
|
|
45
56
|
context.reply.status(result.status)
|
|
46
57
|
if (result.headers[SESSION_HEADER]) {
|
|
47
58
|
context.reply.header(SESSION_HEADER, result.headers[SESSION_HEADER])
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type {PluginInterface} from "../../types/index.ts";
|
|
2
2
|
import type {ContextInterface} from "../../types/core.ts";
|
|
3
|
-
import {PluginScope} from "../../core/
|
|
4
|
-
import {Logger} from "@protorians/logger";
|
|
5
|
-
import {generateOpenApiSpec} from "../utilities/openapi.utils.ts";
|
|
3
|
+
import type {PluginScope} from "../../core/plugins/scope.ts";
|
|
6
4
|
|
|
7
5
|
/**
|
|
8
6
|
* OpenAPI plugin options
|
|
@@ -12,10 +10,10 @@ export interface OpenApiOptions {
|
|
|
12
10
|
* The base route for the documentation UI (e.g., '/docs')
|
|
13
11
|
* @default '/docs'
|
|
14
12
|
*/
|
|
15
|
-
route?: string;
|
|
13
|
+
route?: string | false;
|
|
16
14
|
|
|
17
15
|
/**
|
|
18
|
-
* The HTML string page
|
|
16
|
+
* The HTML string page. A Redoc UI is provided by default.
|
|
19
17
|
*/
|
|
20
18
|
ui?: (config: Omit<OpenApiOptions, 'ui'>, context: ContextInterface) => string;
|
|
21
19
|
|
|
@@ -61,8 +59,47 @@ export interface OpenApiOptions {
|
|
|
61
59
|
};
|
|
62
60
|
}
|
|
63
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Dependency-free default documentation UI (Redoc via CDN). The page only
|
|
64
|
+
* references the JSON endpoint served by the same plugin.
|
|
65
|
+
*/
|
|
66
|
+
function relativeEndpoint(route: string | false | undefined, endpoint: string): string {
|
|
67
|
+
if (!route) return endpoint;
|
|
68
|
+
const base = route.split("/").slice(0, -1).join("/") || "/";
|
|
69
|
+
if (!endpoint.startsWith(base === "/" ? "/" : `${base}/`)) return endpoint;
|
|
70
|
+
const relative = base === "/" ? endpoint.slice(1) : endpoint.slice(base.length + 1);
|
|
71
|
+
return relative || endpoint;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function defaultOpenApiUi(config: Omit<OpenApiOptions, "ui">): string {
|
|
75
|
+
const title = config.title || "API";
|
|
76
|
+
const endpoint = relativeEndpoint(config.route, config.endpoint || "/docs/json");
|
|
77
|
+
return `<!DOCTYPE html>
|
|
78
|
+
<html lang="en">
|
|
79
|
+
<head>
|
|
80
|
+
<meta charset="utf-8" />
|
|
81
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
82
|
+
<title>${escapeHtml(title)} — API documentation</title>
|
|
83
|
+
<style>body{margin:0;padding:0;font-family:system-ui,sans-serif;}</style>
|
|
84
|
+
</head>
|
|
85
|
+
<body>
|
|
86
|
+
<redoc spec-url="${escapeHtml(endpoint)}"></redoc>
|
|
87
|
+
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"></script>
|
|
88
|
+
</body>
|
|
89
|
+
</html>`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function escapeHtml(value: string): string {
|
|
93
|
+
return value
|
|
94
|
+
.replace(/&/g, "&")
|
|
95
|
+
.replace(/</g, "<")
|
|
96
|
+
.replace(/>/g, ">")
|
|
97
|
+
.replace(/"/g, """);
|
|
98
|
+
}
|
|
99
|
+
|
|
64
100
|
export function openApiPlugin(options: OpenApiOptions = {}): PluginInterface {
|
|
65
101
|
const opts = {
|
|
102
|
+
route: '/docs',
|
|
66
103
|
endpoint: '/docs/json',
|
|
67
104
|
title: 'API',
|
|
68
105
|
version: '1.0.0',
|
|
@@ -77,22 +114,28 @@ export function openApiPlugin(options: OpenApiOptions = {}): PluginInterface {
|
|
|
77
114
|
name: 'openapi-plugin',
|
|
78
115
|
setup: (scope: PluginScope) => {
|
|
79
116
|
|
|
80
|
-
if (opts.route
|
|
81
|
-
|
|
117
|
+
if (opts.route) {
|
|
118
|
+
const ui = opts.ui ?? defaultOpenApiUi;
|
|
119
|
+
const handler = async ({reply, req, ...context}: ContextInterface) => {
|
|
82
120
|
const optionsWithoutUi = {...opts, ui: undefined}
|
|
83
|
-
const html =
|
|
121
|
+
const html = ui(optionsWithoutUi, {reply, req, ...context});
|
|
84
122
|
reply.type('text/html');
|
|
85
|
-
|
|
86
|
-
}
|
|
123
|
+
return html || defaultOpenApiUi(optionsWithoutUi);
|
|
124
|
+
};
|
|
125
|
+
(handler as any)._raitonInternal = true;
|
|
126
|
+
scope.get(opts.route, handler);
|
|
87
127
|
}
|
|
88
128
|
|
|
89
129
|
if (opts.endpoint) {
|
|
90
|
-
|
|
130
|
+
const handler = async ({reply}: ContextInterface) => {
|
|
131
|
+
const {generateOpenApiSpec} = await import("../utilities/openapi.utils.ts")
|
|
91
132
|
const spec = await generateOpenApiSpec(scope, opts);
|
|
92
133
|
reply.type('application/json');
|
|
93
134
|
return spec
|
|
94
|
-
}
|
|
135
|
+
};
|
|
136
|
+
(handler as any)._raitonInternal = true;
|
|
137
|
+
scope.get(opts.endpoint, handler);
|
|
95
138
|
}
|
|
96
139
|
}
|
|
97
140
|
};
|
|
98
|
-
}
|
|
141
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {Raiton} from "../../core/
|
|
1
|
+
import type {HttpResponseInterface} from "../../types/responses.ts";
|
|
2
|
+
import type {HttpStatus} from "../enums/http-status.enum.ts";
|
|
3
|
+
import {Raiton} from "../../core/raiton.ts";
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
export function RaitonResponses(
|
|
@@ -28,4 +28,4 @@ export function RaitonResponses(
|
|
|
28
28
|
message,
|
|
29
29
|
data,
|
|
30
30
|
}
|
|
31
|
-
}
|
|
31
|
+
}
|
|
@@ -6,7 +6,8 @@ import {Logger} from "@protorians/logger";
|
|
|
6
6
|
import {getRealIp} from "../../utilities/index.ts";
|
|
7
7
|
import {findSocketForPath} from "../../../core/socket/index.ts";
|
|
8
8
|
import {Injection} from "../../../core/injection/index.ts";
|
|
9
|
-
import {RaitonResponses
|
|
9
|
+
import {RaitonResponses} from "../../responses/helpers.ts";
|
|
10
|
+
import {HttpStatus} from "../../enums/http-status.enum.ts";
|
|
10
11
|
|
|
11
12
|
export const bunRuntime: RuntimeAdapterInterface = {
|
|
12
13
|
createServer(handler, options?: RuntimeServerOptionsInterface) {
|
|
@@ -3,7 +3,8 @@ import {Logger} from "@protorians/logger";
|
|
|
3
3
|
import {getRealIp} from "../../utilities/index.ts";
|
|
4
4
|
import {findSocketForPath} from "../../../core/socket/index.ts";
|
|
5
5
|
import {Injection} from "../../../core/injection/index.ts";
|
|
6
|
-
import {RaitonResponses
|
|
6
|
+
import {RaitonResponses} from "../../responses/helpers.ts";
|
|
7
|
+
import {HttpStatus} from "../../enums/http-status.enum.ts";
|
|
7
8
|
|
|
8
9
|
export const denoRuntime: RuntimeAdapterInterface = {
|
|
9
10
|
createServer(handler, options?: RuntimeServerOptionsInterface) {
|