@decohere/http 0.0.1-dev.116
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/README.md +246 -0
- package/commonjs/src/app/app.js +166 -0
- package/commonjs/src/app/index.js +19 -0
- package/commonjs/src/app/module.js +17 -0
- package/commonjs/src/auth/ability.js +54 -0
- package/commonjs/src/auth/auth.config.js +23 -0
- package/commonjs/src/auth/auth.extensions.js +106 -0
- package/commonjs/src/auth/auth.types.js +65 -0
- package/commonjs/src/auth/casl-authorization.service.js +36 -0
- package/commonjs/src/auth/index.js +23 -0
- package/commonjs/src/auth/principal-provider.js +19 -0
- package/commonjs/src/auth/safe-outbound-url.js +102 -0
- package/commonjs/src/auth/token-service.js +151 -0
- package/commonjs/src/builder/builder.js +95 -0
- package/commonjs/src/builder/index.js +18 -0
- package/commonjs/src/const.js +87 -0
- package/commonjs/src/health/health.controller.js +75 -0
- package/commonjs/src/health/health.extensions.js +50 -0
- package/commonjs/src/health/index.js +21 -0
- package/commonjs/src/health/readiness.service.js +67 -0
- package/commonjs/src/health/readiness.types.js +7 -0
- package/commonjs/src/hosting/hosted-service-host.js +88 -0
- package/commonjs/src/hosting/hosted-service.js +11 -0
- package/commonjs/src/hosting/index.js +19 -0
- package/commonjs/src/http/http.compression.js +27 -0
- package/commonjs/src/http/http.extensions.js +269 -0
- package/commonjs/src/http/http.options.js +26 -0
- package/commonjs/src/http/http.options.store.js +20 -0
- package/commonjs/src/http/http.security.js +27 -0
- package/commonjs/src/http/http.static.js +248 -0
- package/commonjs/src/http/index.js +22 -0
- package/commonjs/src/index.js +27 -0
- package/commonjs/src/openapi/index.js +45 -0
- package/commonjs/src/openapi/openapi.generator.js +103 -0
- package/commonjs/src/openapi/openapi.types.js +4 -0
- package/commonjs/src/openapi/request-schema.js +187 -0
- package/commonjs/src/openapi/request-validator.js +85 -0
- package/commonjs/src/providers/fastify.provider.js +391 -0
- package/commonjs/src/providers/index.js +18 -0
- package/commonjs/src/routing/anvil.types.js +99 -0
- package/commonjs/src/routing/decorators/argument.decorator.js +49 -0
- package/commonjs/src/routing/decorators/auth.decorator.js +70 -0
- package/commonjs/src/routing/decorators/controller.decorator.js +16 -0
- package/commonjs/src/routing/decorators/field.decorator.js +25 -0
- package/commonjs/src/routing/decorators/index.js +24 -0
- package/commonjs/src/routing/decorators/request.decorator.js +30 -0
- package/commonjs/src/routing/decorators/response.decorator.js +34 -0
- package/commonjs/src/routing/decorators/route.decorator.js +67 -0
- package/commonjs/src/routing/filters/exception-filter-host.js +145 -0
- package/commonjs/src/routing/filters/exception-filter.js +28 -0
- package/commonjs/src/routing/index.js +25 -0
- package/commonjs/src/routing/middleware/http-access-log.middleware.js +47 -0
- package/commonjs/src/routing/middleware/middleware.js +20 -0
- package/commonjs/src/routing/middleware/request-id.js +30 -0
- package/commonjs/src/routing/path.util.js +51 -0
- package/commonjs/src/routing/pipes/index.js +18 -0
- package/commonjs/src/routing/pipes/pipes.js +145 -0
- package/commonjs/src/routing/services/index.js +18 -0
- package/commonjs/src/routing/services/route-argument-resolver.js +77 -0
- package/commonjs/src/routing/services/route-registry.js +194 -0
- package/commonjs/src/routing/services/router.js +158 -0
- package/commonjs/src/sse/index.js +26 -0
- package/commonjs/src/sse/sse.decorator.js +42 -0
- package/commonjs/src/sse/sse.encoder.js +73 -0
- package/commonjs/src/sse/sse.types.js +36 -0
- package/commonjs/src/sse/sse.wire.js +40 -0
- package/commonjs/src/testing/boot-module.js +38 -0
- package/commonjs/src/testing/index.js +20 -0
- package/commonjs/src/testing/sse-reader.js +98 -0
- package/es6/src/app/app.js +148 -0
- package/es6/src/app/index.js +2 -0
- package/es6/src/app/module.js +7 -0
- package/es6/src/auth/ability.js +45 -0
- package/es6/src/auth/auth.config.js +19 -0
- package/es6/src/auth/auth.extensions.js +83 -0
- package/es6/src/auth/auth.types.js +35 -0
- package/es6/src/auth/casl-authorization.service.js +40 -0
- package/es6/src/auth/index.js +6 -0
- package/es6/src/auth/principal-provider.js +14 -0
- package/es6/src/auth/safe-outbound-url.js +99 -0
- package/es6/src/auth/token-service.js +141 -0
- package/es6/src/builder/builder.js +85 -0
- package/es6/src/builder/index.js +1 -0
- package/es6/src/const.js +33 -0
- package/es6/src/health/health.controller.js +65 -0
- package/es6/src/health/health.extensions.js +40 -0
- package/es6/src/health/index.js +4 -0
- package/es6/src/health/readiness.service.js +57 -0
- package/es6/src/health/readiness.types.js +4 -0
- package/es6/src/hosting/hosted-service-host.js +81 -0
- package/es6/src/hosting/hosted-service.js +8 -0
- package/es6/src/hosting/index.js +2 -0
- package/es6/src/http/http.compression.js +9 -0
- package/es6/src/http/http.extensions.js +259 -0
- package/es6/src/http/http.options.js +16 -0
- package/es6/src/http/http.options.store.js +13 -0
- package/es6/src/http/http.security.js +9 -0
- package/es6/src/http/http.static.js +224 -0
- package/es6/src/http/index.js +5 -0
- package/es6/src/index.js +10 -0
- package/es6/src/openapi/index.js +3 -0
- package/es6/src/openapi/openapi.generator.js +82 -0
- package/es6/src/openapi/openapi.types.js +1 -0
- package/es6/src/openapi/request-schema.js +160 -0
- package/es6/src/openapi/request-validator.js +71 -0
- package/es6/src/providers/fastify.provider.js +376 -0
- package/es6/src/providers/index.js +1 -0
- package/es6/src/routing/anvil.types.js +57 -0
- package/es6/src/routing/decorators/argument.decorator.js +28 -0
- package/es6/src/routing/decorators/auth.decorator.js +58 -0
- package/es6/src/routing/decorators/controller.decorator.js +6 -0
- package/es6/src/routing/decorators/field.decorator.js +15 -0
- package/es6/src/routing/decorators/index.js +7 -0
- package/es6/src/routing/decorators/request.decorator.js +24 -0
- package/es6/src/routing/decorators/response.decorator.js +16 -0
- package/es6/src/routing/decorators/route.decorator.js +37 -0
- package/es6/src/routing/filters/exception-filter-host.js +127 -0
- package/es6/src/routing/filters/exception-filter.js +10 -0
- package/es6/src/routing/index.js +8 -0
- package/es6/src/routing/middleware/http-access-log.middleware.js +37 -0
- package/es6/src/routing/middleware/middleware.js +10 -0
- package/es6/src/routing/middleware/request-id.js +14 -0
- package/es6/src/routing/path.util.js +33 -0
- package/es6/src/routing/pipes/index.js +1 -0
- package/es6/src/routing/pipes/pipes.js +118 -0
- package/es6/src/routing/services/index.js +1 -0
- package/es6/src/routing/services/route-argument-resolver.js +67 -0
- package/es6/src/routing/services/route-registry.js +184 -0
- package/es6/src/routing/services/router.js +148 -0
- package/es6/src/sse/index.js +2 -0
- package/es6/src/sse/sse.decorator.js +31 -0
- package/es6/src/sse/sse.encoder.js +67 -0
- package/es6/src/sse/sse.types.js +22 -0
- package/es6/src/sse/sse.wire.js +30 -0
- package/es6/src/testing/boot-module.js +35 -0
- package/es6/src/testing/index.js +2 -0
- package/es6/src/testing/sse-reader.js +91 -0
- package/package.json +64 -0
- package/types/src/app/app.d.ts +41 -0
- package/types/src/app/index.d.ts +2 -0
- package/types/src/app/module.d.ts +15 -0
- package/types/src/auth/ability.d.ts +19 -0
- package/types/src/auth/auth.config.d.ts +28 -0
- package/types/src/auth/auth.extensions.d.ts +53 -0
- package/types/src/auth/auth.types.d.ts +73 -0
- package/types/src/auth/casl-authorization.service.d.ts +28 -0
- package/types/src/auth/index.d.ts +6 -0
- package/types/src/auth/principal-provider.d.ts +20 -0
- package/types/src/auth/safe-outbound-url.d.ts +9 -0
- package/types/src/auth/token-service.d.ts +31 -0
- package/types/src/builder/builder.d.ts +36 -0
- package/types/src/builder/index.d.ts +1 -0
- package/types/src/const.d.ts +32 -0
- package/types/src/health/health.controller.d.ts +16 -0
- package/types/src/health/health.extensions.d.ts +31 -0
- package/types/src/health/index.d.ts +4 -0
- package/types/src/health/readiness.service.d.ts +9 -0
- package/types/src/health/readiness.types.d.ts +14 -0
- package/types/src/hosting/hosted-service-host.d.ts +37 -0
- package/types/src/hosting/hosted-service.d.ts +31 -0
- package/types/src/hosting/index.d.ts +2 -0
- package/types/src/http/http.compression.d.ts +32 -0
- package/types/src/http/http.extensions.d.ts +79 -0
- package/types/src/http/http.options.d.ts +39 -0
- package/types/src/http/http.options.store.d.ts +7 -0
- package/types/src/http/http.security.d.ts +20 -0
- package/types/src/http/http.static.d.ts +160 -0
- package/types/src/http/index.d.ts +5 -0
- package/types/src/index.d.ts +10 -0
- package/types/src/openapi/index.d.ts +6 -0
- package/types/src/openapi/openapi.generator.d.ts +6 -0
- package/types/src/openapi/openapi.types.d.ts +58 -0
- package/types/src/openapi/request-schema.d.ts +21 -0
- package/types/src/openapi/request-validator.d.ts +9 -0
- package/types/src/providers/fastify.provider.d.ts +56 -0
- package/types/src/providers/index.d.ts +1 -0
- package/types/src/routing/anvil.types.d.ts +119 -0
- package/types/src/routing/decorators/argument.decorator.d.ts +11 -0
- package/types/src/routing/decorators/auth.decorator.d.ts +30 -0
- package/types/src/routing/decorators/controller.decorator.d.ts +3 -0
- package/types/src/routing/decorators/field.decorator.d.ts +2 -0
- package/types/src/routing/decorators/index.d.ts +7 -0
- package/types/src/routing/decorators/request.decorator.d.ts +8 -0
- package/types/src/routing/decorators/response.decorator.d.ts +4 -0
- package/types/src/routing/decorators/route.decorator.d.ts +7 -0
- package/types/src/routing/filters/exception-filter-host.d.ts +19 -0
- package/types/src/routing/filters/exception-filter.d.ts +16 -0
- package/types/src/routing/index.d.ts +8 -0
- package/types/src/routing/middleware/http-access-log.middleware.d.ts +8 -0
- package/types/src/routing/middleware/middleware.d.ts +10 -0
- package/types/src/routing/middleware/request-id.d.ts +6 -0
- package/types/src/routing/path.util.d.ts +9 -0
- package/types/src/routing/pipes/index.d.ts +1 -0
- package/types/src/routing/pipes/pipes.d.ts +21 -0
- package/types/src/routing/services/index.d.ts +1 -0
- package/types/src/routing/services/route-argument-resolver.d.ts +10 -0
- package/types/src/routing/services/route-registry.d.ts +20 -0
- package/types/src/routing/services/router.d.ts +22 -0
- package/types/src/sse/index.d.ts +3 -0
- package/types/src/sse/sse.decorator.d.ts +17 -0
- package/types/src/sse/sse.encoder.d.ts +16 -0
- package/types/src/sse/sse.types.d.ts +37 -0
- package/types/src/sse/sse.wire.d.ts +9 -0
- package/types/src/testing/boot-module.d.ts +34 -0
- package/types/src/testing/index.d.ts +4 -0
- package/types/src/testing/sse-reader.d.ts +29 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
get ValidationError () {
|
|
13
|
+
return _anviltypes.ValidationError;
|
|
14
|
+
},
|
|
15
|
+
get validateRequest () {
|
|
16
|
+
return validateRequest;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const _ajv = require("ajv");
|
|
20
|
+
const _anviltypes = require("../routing/anvil.types.js");
|
|
21
|
+
const _requestschema = require("./request-schema.js");
|
|
22
|
+
function validateRequest(route, request) {
|
|
23
|
+
const { params, body } = getValidators(route);
|
|
24
|
+
const issues = [];
|
|
25
|
+
if (params != null) {
|
|
26
|
+
collectIssues(params, request.params ?? {}, "/params", issues);
|
|
27
|
+
}
|
|
28
|
+
if (body != null) {
|
|
29
|
+
collectIssues(body, request.body, "/body", issues);
|
|
30
|
+
}
|
|
31
|
+
if (issues.length > 0) {
|
|
32
|
+
throw new _anviltypes.ValidationError("Validation failed", issues);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const ajv = new _ajv.Ajv({
|
|
36
|
+
allErrors: true,
|
|
37
|
+
coerceTypes: false,
|
|
38
|
+
removeAdditional: false,
|
|
39
|
+
useDefaults: false,
|
|
40
|
+
strict: false
|
|
41
|
+
});
|
|
42
|
+
/**
|
|
43
|
+
* Compiled once per route and reused. Compiling per request was slow, and AJV keeps every
|
|
44
|
+
* schema it compiles, so memory grew with traffic (#498).
|
|
45
|
+
*/ const validatorsByRoute = new WeakMap();
|
|
46
|
+
function getValidators(route) {
|
|
47
|
+
let validators = validatorsByRoute.get(route);
|
|
48
|
+
if (validators == null) {
|
|
49
|
+
const schemas = (0, _requestschema.buildRouteRequestSchemas)(route);
|
|
50
|
+
const paramsSchema = (0, _requestschema.pathParamsToSchema)(schemas.pathParams);
|
|
51
|
+
validators = {
|
|
52
|
+
params: paramsSchema != null ? ajv.compile(paramsSchema) : undefined,
|
|
53
|
+
body: schemas.body != null ? ajv.compile(schemas.body) : undefined
|
|
54
|
+
};
|
|
55
|
+
validatorsByRoute.set(route, validators);
|
|
56
|
+
}
|
|
57
|
+
return validators;
|
|
58
|
+
}
|
|
59
|
+
function collectIssues(validate, data, rootPath, issues) {
|
|
60
|
+
if (validate(data)) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
for (const err of validate.errors ?? []){
|
|
64
|
+
issues.push(toIssue(err, rootPath));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function toIssue(err, rootPath) {
|
|
68
|
+
const instance = err.instancePath ?? "";
|
|
69
|
+
const path = instance.length > 0 ? `${rootPath}${instance}` : rootPath;
|
|
70
|
+
const message = err.message ?? "is invalid";
|
|
71
|
+
return {
|
|
72
|
+
path,
|
|
73
|
+
message: formatAjvMessage(err, message),
|
|
74
|
+
keyword: err.keyword
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function formatAjvMessage(err, fallback) {
|
|
78
|
+
if (err.keyword === "required" && typeof err.params?.missingProperty === "string") {
|
|
79
|
+
return `must have required property '${err.params.missingProperty}'`;
|
|
80
|
+
}
|
|
81
|
+
if (err.keyword === "additionalProperties" && typeof err.params?.additionalProperty === "string") {
|
|
82
|
+
return `must NOT have additional property '${err.params.additionalProperty}'`;
|
|
83
|
+
}
|
|
84
|
+
return fallback;
|
|
85
|
+
}
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "FastifyProvider", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return FastifyProvider;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _nodecrypto = require("node:crypto");
|
|
12
|
+
const _promises = require("node:fs/promises");
|
|
13
|
+
const _nodepath = /*#__PURE__*/ _interop_require_default(require("node:path"));
|
|
14
|
+
const _nodestream = require("node:stream");
|
|
15
|
+
const _decorators = require("@decohere/crucible/decorators");
|
|
16
|
+
const _scribe = require("@decohere/scribe");
|
|
17
|
+
const _compress = /*#__PURE__*/ _interop_require_default(require("@fastify/compress"));
|
|
18
|
+
const _cors = /*#__PURE__*/ _interop_require_default(require("@fastify/cors"));
|
|
19
|
+
const _helmet = /*#__PURE__*/ _interop_require_default(require("@fastify/helmet"));
|
|
20
|
+
const _ratelimit = /*#__PURE__*/ _interop_require_default(require("@fastify/rate-limit"));
|
|
21
|
+
const _static = /*#__PURE__*/ _interop_require_default(require("@fastify/static"));
|
|
22
|
+
const _fastify = /*#__PURE__*/ _interop_require_default(require("fastify"));
|
|
23
|
+
const _const = require("../const.js");
|
|
24
|
+
const _httpsecurity = require("../http/http.security.js");
|
|
25
|
+
const _httpcompression = require("../http/http.compression.js");
|
|
26
|
+
const _httpstatic = require("../http/http.static.js");
|
|
27
|
+
const _openapigenerator = require("../openapi/openapi.generator.js");
|
|
28
|
+
const _anviltypes = require("../routing/anvil.types.js");
|
|
29
|
+
const _exceptionfilterhost = require("../routing/filters/exception-filter-host.js");
|
|
30
|
+
const _requestid = require("../routing/middleware/request-id.js");
|
|
31
|
+
const _pathutil = require("../routing/path.util.js");
|
|
32
|
+
const _sseencoder = require("../sse/sse.encoder.js");
|
|
33
|
+
const _ssetypes = require("../sse/sse.types.js");
|
|
34
|
+
function _interop_require_default(obj) {
|
|
35
|
+
return obj && obj.__esModule ? obj : {
|
|
36
|
+
default: obj
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
40
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
41
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
42
|
+
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
43
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
44
|
+
}
|
|
45
|
+
function _ts_metadata(k, v) {
|
|
46
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
47
|
+
}
|
|
48
|
+
function _ts_param(paramIndex, decorator) {
|
|
49
|
+
return function(target, key) {
|
|
50
|
+
decorator(target, key, paramIndex);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
class FastifyProvider {
|
|
54
|
+
provider;
|
|
55
|
+
router;
|
|
56
|
+
routeRegistry;
|
|
57
|
+
httpOptions;
|
|
58
|
+
exceptionFilterHost;
|
|
59
|
+
closing;
|
|
60
|
+
logger = _scribe.scribe.logger(FastifyProvider.name);
|
|
61
|
+
/** One per live `@Sse()` request, so stopping the app can end the streams. */ openStreams = new Set();
|
|
62
|
+
constructor(routeRegistry, router, httpOptions, exceptionFilterHost){
|
|
63
|
+
this.routeRegistry = routeRegistry;
|
|
64
|
+
this.router = router;
|
|
65
|
+
this.httpOptions = httpOptions;
|
|
66
|
+
this.exceptionFilterHost = exceptionFilterHost;
|
|
67
|
+
this.provider = (0, _fastify.default)({
|
|
68
|
+
logger: false,
|
|
69
|
+
requestIdHeader: _requestid.REQUEST_ID_HEADER,
|
|
70
|
+
genReqId: ()=>(0, _nodecrypto.randomUUID)(),
|
|
71
|
+
bodyLimit: httpOptions?.bodyLimit ?? _httpsecurity.DEFAULT_HTTP_BODY_LIMIT
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
async start(options) {
|
|
75
|
+
const statics = await this.resolveStatics();
|
|
76
|
+
await this.registerHelmet(this.httpOptions?.helmet);
|
|
77
|
+
await this.registerRateLimit(this.httpOptions?.rateLimit, statics);
|
|
78
|
+
await this.registerCors(this.httpOptions?.cors);
|
|
79
|
+
await this.registerCompression(this.httpOptions?.compression);
|
|
80
|
+
this.registerOpenApi(this.httpOptions);
|
|
81
|
+
this.registerAllRoutes(this.routeRegistry);
|
|
82
|
+
for (const mount of statics){
|
|
83
|
+
await this.registerStatic(mount, statics);
|
|
84
|
+
}
|
|
85
|
+
this.closeConnectionsWhileStopping();
|
|
86
|
+
await this.provider.listen(options);
|
|
87
|
+
return {
|
|
88
|
+
addresses: this.addresses
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
async stop() {
|
|
92
|
+
// App stops HTTP before disposing the container, which calls this again.
|
|
93
|
+
// A stream never finishes on its own, and close() waits for open responses, so end them first.
|
|
94
|
+
for (const stream of this.openStreams){
|
|
95
|
+
stream.abort(new Error("The app is stopping"));
|
|
96
|
+
}
|
|
97
|
+
this.closing ??= this.provider.close();
|
|
98
|
+
await this.closing;
|
|
99
|
+
return {
|
|
100
|
+
message: "closed"
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
async [Symbol.asyncDispose]() {
|
|
104
|
+
await this.stop();
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Once stopping, tell clients to drop the connection after their response. Otherwise
|
|
108
|
+
* a request that was in flight leaves a kept-alive socket that holds `close()` open
|
|
109
|
+
* until Node's keep-alive timeout (5s).
|
|
110
|
+
*/ closeConnectionsWhileStopping() {
|
|
111
|
+
this.provider.addHook("onSend", async (_request, reply, payload)=>{
|
|
112
|
+
if (this.closing) reply.header("connection", "close");
|
|
113
|
+
return payload;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
get addresses() {
|
|
117
|
+
return this.provider.addresses().filter((a)=>a.family === "IPv4").map((a)=>`http://${a.address}:${a.port}`);
|
|
118
|
+
}
|
|
119
|
+
async registerHelmet(helmetOptions) {
|
|
120
|
+
if (helmetOptions === false) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
await this.provider.register(_helmet.default, helmetOptions ?? {});
|
|
124
|
+
}
|
|
125
|
+
async registerRateLimit(rateLimitOptions, statics) {
|
|
126
|
+
if (rateLimitOptions === false) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
// Static files are outside the rate limit (ADR 0007). Matched on the route the request
|
|
130
|
+
// resolved to, not on its URL, so only requests the static plugin serves are exempt.
|
|
131
|
+
const staticRoutes = new Set(statics.map((mount)=>`${mount.prefix}*`));
|
|
132
|
+
const userAllowList = rateLimitOptions?.allowList;
|
|
133
|
+
await this.provider.register(_ratelimit.default, {
|
|
134
|
+
..._httpsecurity.DEFAULT_HTTP_RATE_LIMIT,
|
|
135
|
+
...rateLimitOptions,
|
|
136
|
+
allowList: (request, key)=>{
|
|
137
|
+
if (staticRoutes.has(request.routeOptions.url ?? "")) return true;
|
|
138
|
+
if (typeof userAllowList === "function") return userAllowList(request, key);
|
|
139
|
+
return Array.isArray(userAllowList) && userAllowList.includes(key);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
async resolveStatics() {
|
|
144
|
+
return _httpstatic.staticFiles.resolve(this.httpOptions?.statics ?? [], {
|
|
145
|
+
nodeEnv: process.env.NODE_ENV,
|
|
146
|
+
directoryExists: async (directory)=>(await (0, _promises.stat)(directory).catch(()=>undefined))?.isDirectory() === true,
|
|
147
|
+
warn: (message)=>this.logger.warn(message)
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* One encapsulated context per mount: its not-found handler (the SPA fallback) applies to
|
|
152
|
+
* paths under the prefix and nowhere else, so an API 404 is never answered with the UI.
|
|
153
|
+
*/ async registerStatic(mount, mounts) {
|
|
154
|
+
const withoutSlash = mount.prefix.slice(0, -1);
|
|
155
|
+
const isRoot = mount.prefix === "/";
|
|
156
|
+
const excluded = _httpstatic.staticFiles.excluder(mount.exclude);
|
|
157
|
+
const belongsToApi = this.apiBoundary();
|
|
158
|
+
// Requests that already fell back once: a missing index must be a 404, not a loop.
|
|
159
|
+
const fellBack = new WeakSet();
|
|
160
|
+
await this.provider.register(async (scope)=>{
|
|
161
|
+
await scope.register(_static.default, {
|
|
162
|
+
root: mount.root,
|
|
163
|
+
prefix: "/",
|
|
164
|
+
index: mount.index,
|
|
165
|
+
dotfiles: "ignore",
|
|
166
|
+
list: false,
|
|
167
|
+
redirect: false,
|
|
168
|
+
etag: true,
|
|
169
|
+
lastModified: true,
|
|
170
|
+
// The header is set in setHeaders, per file.
|
|
171
|
+
cacheControl: false,
|
|
172
|
+
setHeaders: (reply, filePath)=>{
|
|
173
|
+
reply.header("cache-control", _httpstatic.staticFiles.cacheControlFor(mount, _nodepath.default.relative(mount.root, filePath)));
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
// Excluded paths are not served at all: not as a file, not as the fallback.
|
|
177
|
+
scope.addHook("onRequest", async (request, reply)=>{
|
|
178
|
+
if (excluded(_httpstatic.staticFiles.normalizePathname(request.url))) {
|
|
179
|
+
return reply.callNotFound();
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
scope.setNotFoundHandler((request, reply)=>{
|
|
183
|
+
const [pathname = "", query] = request.url.split("?");
|
|
184
|
+
const readOnly = request.method === "GET" || request.method === "HEAD";
|
|
185
|
+
// `/ui` goes to `/ui/`. A request for it reaches the root mount's handler instead
|
|
186
|
+
// when there is one, so that handler redirects for the other mounts too.
|
|
187
|
+
const redirectTo = mounts.find((other)=>other.prefix !== "/" && other.prefix.slice(0, -1) === pathname && (isRoot || other === mount));
|
|
188
|
+
if (readOnly && redirectTo != null) {
|
|
189
|
+
return reply.redirect(query == null ? redirectTo.prefix : `${redirectTo.prefix}?${query}`, 301);
|
|
190
|
+
}
|
|
191
|
+
const normalized = _httpstatic.staticFiles.normalizePathname(request.url);
|
|
192
|
+
if (mount.spaFallback && !fellBack.has(request) && !excluded(normalized) && _httpstatic.staticFiles.shouldFallBack({
|
|
193
|
+
method: request.method,
|
|
194
|
+
pathname: normalized,
|
|
195
|
+
accept: request.headers.accept
|
|
196
|
+
}, belongsToApi)) {
|
|
197
|
+
fellBack.add(request);
|
|
198
|
+
return reply.sendFile(mount.index);
|
|
199
|
+
}
|
|
200
|
+
return reply.code(404).send({
|
|
201
|
+
message: `Route ${request.method}:${pathname} not found`,
|
|
202
|
+
error: "Not Found",
|
|
203
|
+
statusCode: 404
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
}, {
|
|
207
|
+
prefix: withoutSlash
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/** What the static fallback leaves to the API: the global prefix, else the registered routes. ADR 0007. */ apiBoundary() {
|
|
211
|
+
return _httpstatic.staticFiles.apiBoundary({
|
|
212
|
+
globalPrefix: (0, _pathutil.normalizePrefix)(this.httpOptions?.globalPrefix),
|
|
213
|
+
openApiPath: this.httpOptions?.openapi == null ? undefined : (0, _openapigenerator.resolveOpenApiPath)(this.httpOptions.openapi),
|
|
214
|
+
routePaths: [
|
|
215
|
+
...this.routeRegistry.routes
|
|
216
|
+
].map((route)=>route.path)
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* `@fastify/compress` with best-practice defaults; the plugin owns the policy. Registered
|
|
221
|
+
* before any route so every response goes through it, errors and static files included.
|
|
222
|
+
* `providerOptions` is merged last, so a user can override any plugin option.
|
|
223
|
+
*/ async registerCompression(compressionOptions) {
|
|
224
|
+
if (compressionOptions === false) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
await this.provider.register(_compress.default, {
|
|
228
|
+
global: true,
|
|
229
|
+
threshold: compressionOptions?.threshold ?? _httpcompression.DEFAULT_COMPRESSION_THRESHOLD,
|
|
230
|
+
encodings: [
|
|
231
|
+
...compressionOptions?.encodings ?? _httpcompression.DEFAULT_COMPRESSION_ENCODINGS
|
|
232
|
+
],
|
|
233
|
+
...compressionOptions?.types != null && {
|
|
234
|
+
customTypes: compressionOptions.types
|
|
235
|
+
},
|
|
236
|
+
...compressionOptions?.providerOptions
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
async registerCors(corsOptions) {
|
|
240
|
+
if (corsOptions == null) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
await this.provider.register(_cors.default, {
|
|
244
|
+
origin: corsOptions.origin,
|
|
245
|
+
methods: corsOptions.methods,
|
|
246
|
+
allowedHeaders: corsOptions.allowedHeaders,
|
|
247
|
+
exposedHeaders: corsOptions.exposedHeaders,
|
|
248
|
+
credentials: corsOptions.credentials,
|
|
249
|
+
maxAge: corsOptions.maxAge
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
registerOpenApi(httpOptions) {
|
|
253
|
+
const openapi = httpOptions?.openapi;
|
|
254
|
+
if (openapi == null) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const path = (0, _openapigenerator.resolveOpenApiPath)(openapi);
|
|
258
|
+
const document = (0, _openapigenerator.generateOpenApiDocument)(this.routeRegistry, openapi);
|
|
259
|
+
this.provider.get(path, async (_request, reply)=>{
|
|
260
|
+
reply.header("content-type", "application/json; charset=utf-8");
|
|
261
|
+
return document;
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
registerAllRoutes(registry) {
|
|
265
|
+
for (const route of registry.routes){
|
|
266
|
+
const { key, method, path } = route;
|
|
267
|
+
this.provider.route({
|
|
268
|
+
method,
|
|
269
|
+
url: path,
|
|
270
|
+
handler: (request, reply)=>this.routeRequest(route, key, request, reply)
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
async routeRequest(route, key, request, reply) {
|
|
275
|
+
const httpRequest = this.fastifyToRequest(request);
|
|
276
|
+
const disconnect = this.abortOnDisconnect(reply, httpRequest, route.sse != null);
|
|
277
|
+
try {
|
|
278
|
+
const response = await this.router.route(key, httpRequest);
|
|
279
|
+
if (route.httpCode != null) {
|
|
280
|
+
reply.status(route.httpCode);
|
|
281
|
+
}
|
|
282
|
+
if (route.headers != null) {
|
|
283
|
+
for (const [name, value] of Object.entries(route.headers)){
|
|
284
|
+
reply.header(name, value);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
this.applyResponseHeaders(reply, httpRequest);
|
|
288
|
+
if (response instanceof _ssetypes.SseResponse) {
|
|
289
|
+
return this.sendEventStream(route, reply, response, disconnect);
|
|
290
|
+
}
|
|
291
|
+
return response;
|
|
292
|
+
} catch (error) {
|
|
293
|
+
const result = this.exceptionFilterHost ? await this.exceptionFilterHost.handle(error, {
|
|
294
|
+
request: httpRequest
|
|
295
|
+
}) : new _exceptionfilterhost.DefaultHttpExceptionFilter().catch(error, {
|
|
296
|
+
request: httpRequest
|
|
297
|
+
});
|
|
298
|
+
if (result.headers) {
|
|
299
|
+
for (const [name, value] of Object.entries(result.headers)){
|
|
300
|
+
reply.header(name, value);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
this.applyResponseHeaders(reply, httpRequest);
|
|
304
|
+
// Returned so Fastify waits for the async onSend hooks instead of sending an empty reply of its own.
|
|
305
|
+
return reply.status(result.status).send(result.body);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Gives the handler a signal that aborts when the client goes away before the response
|
|
310
|
+
* finished. Streams are also tracked so that stopping the app can abort them.
|
|
311
|
+
*/ abortOnDisconnect(reply, httpRequest, isStream) {
|
|
312
|
+
const controller = new AbortController();
|
|
313
|
+
httpRequest.signal = controller.signal;
|
|
314
|
+
if (isStream) {
|
|
315
|
+
if (this.closing) controller.abort(new Error("The app is stopping"));
|
|
316
|
+
this.openStreams.add(controller);
|
|
317
|
+
}
|
|
318
|
+
reply.raw.once("close", ()=>{
|
|
319
|
+
this.openStreams.delete(controller);
|
|
320
|
+
if (!reply.raw.writableFinished) {
|
|
321
|
+
controller.abort(new Error("The client disconnected"));
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
return controller;
|
|
325
|
+
}
|
|
326
|
+
sendEventStream(route, reply, response, disconnect) {
|
|
327
|
+
let started = false;
|
|
328
|
+
const release = ()=>{
|
|
329
|
+
response.release().catch((error)=>{
|
|
330
|
+
this.logger.error("Releasing the request scope of an SSE stream failed", error);
|
|
331
|
+
});
|
|
332
|
+
};
|
|
333
|
+
const frames = (0, _sseencoder.encodeSse)(response.events, {
|
|
334
|
+
signal: disconnect.signal,
|
|
335
|
+
heartbeatMs: route.sse?.heartbeatMs ?? _ssetypes.DEFAULT_SSE_HEARTBEAT_MS,
|
|
336
|
+
onProducerStopped: (stopped)=>void stopped.then(release)
|
|
337
|
+
});
|
|
338
|
+
const guarded = async function*() {
|
|
339
|
+
started = true;
|
|
340
|
+
yield* frames;
|
|
341
|
+
}();
|
|
342
|
+
// Never pulled if the client left first, in which case nothing else would release the scope.
|
|
343
|
+
reply.raw.once("close", ()=>{
|
|
344
|
+
if (!started) release();
|
|
345
|
+
});
|
|
346
|
+
return reply.status(route.httpCode ?? 200).header("content-type", "text/event-stream; charset=utf-8").header("cache-control", "no-cache, no-transform").header("x-accel-buffering", "no")// A stream owns its connection; closing it afterwards lets app stop finish at once instead of waiting out keep-alive.
|
|
347
|
+
.header("connection", "close").send(_nodestream.Readable.from(guarded));
|
|
348
|
+
}
|
|
349
|
+
applyResponseHeaders(reply, httpRequest) {
|
|
350
|
+
reply.header(_requestid.REQUEST_ID_HEADER, httpRequest.requestId);
|
|
351
|
+
for (const [name, value] of Object.entries(httpRequest.responseHeaders)){
|
|
352
|
+
reply.header(name, value);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
fastifyToRequest(fastifyRequest) {
|
|
356
|
+
const method = fastifyRequest.method;
|
|
357
|
+
const url = fastifyRequest.url;
|
|
358
|
+
const params = fastifyRequest.params;
|
|
359
|
+
const query = fastifyRequest.query;
|
|
360
|
+
const body = fastifyRequest.body ?? {};
|
|
361
|
+
const headers = {};
|
|
362
|
+
for (const [key, value] of Object.entries(fastifyRequest.headers)){
|
|
363
|
+
if (typeof value === "string") {
|
|
364
|
+
headers[key.toLowerCase()] = value;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
const requestId = (0, _requestid.resolveRequestId)(headers, String(fastifyRequest.id ?? ""));
|
|
368
|
+
return new _anviltypes.HttpRequest({
|
|
369
|
+
requestId,
|
|
370
|
+
url,
|
|
371
|
+
method,
|
|
372
|
+
params,
|
|
373
|
+
query,
|
|
374
|
+
body,
|
|
375
|
+
headers
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
FastifyProvider = _ts_decorate([
|
|
380
|
+
_ts_param(0, (0, _decorators.inject)(_const.RouteRegistryToken)),
|
|
381
|
+
_ts_param(1, (0, _decorators.inject)(_const.RouterToken)),
|
|
382
|
+
_ts_param(2, (0, _decorators.inject)(_const.HttpOptionsToken)),
|
|
383
|
+
_ts_param(3, (0, _decorators.inject)(_const.ExceptionFilterHostToken)),
|
|
384
|
+
_ts_metadata("design:type", Function),
|
|
385
|
+
_ts_metadata("design:paramtypes", [
|
|
386
|
+
typeof RouteRegistry === "undefined" ? Object : RouteRegistry,
|
|
387
|
+
typeof Router === "undefined" ? Object : Router,
|
|
388
|
+
typeof HttpOptions === "undefined" ? Object : HttpOptions,
|
|
389
|
+
typeof ExceptionFilterHost === "undefined" ? Object : ExceptionFilterHost
|
|
390
|
+
])
|
|
391
|
+
], FastifyProvider);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
_export_star(require("./fastify.provider.js"), exports);
|
|
6
|
+
function _export_star(from, to) {
|
|
7
|
+
Object.keys(from).forEach(function(k) {
|
|
8
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
9
|
+
Object.defineProperty(to, k, {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function() {
|
|
12
|
+
return from[k];
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
return from;
|
|
18
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
get ForbiddenError () {
|
|
13
|
+
return ForbiddenError;
|
|
14
|
+
},
|
|
15
|
+
get HttpError () {
|
|
16
|
+
return HttpError;
|
|
17
|
+
},
|
|
18
|
+
get HttpRequest () {
|
|
19
|
+
return HttpRequest;
|
|
20
|
+
},
|
|
21
|
+
get InjectedParts () {
|
|
22
|
+
return InjectedParts;
|
|
23
|
+
},
|
|
24
|
+
get RequestParts () {
|
|
25
|
+
return RequestParts;
|
|
26
|
+
},
|
|
27
|
+
get RouteConfigurationError () {
|
|
28
|
+
return RouteConfigurationError;
|
|
29
|
+
},
|
|
30
|
+
get RoutingError () {
|
|
31
|
+
return RoutingError;
|
|
32
|
+
},
|
|
33
|
+
get TransformError () {
|
|
34
|
+
return TransformError;
|
|
35
|
+
},
|
|
36
|
+
get UnauthorizedError () {
|
|
37
|
+
return UnauthorizedError;
|
|
38
|
+
},
|
|
39
|
+
get ValidationError () {
|
|
40
|
+
return ValidationError;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
const RequestParts = {
|
|
44
|
+
query: "query",
|
|
45
|
+
params: "params",
|
|
46
|
+
body: "body",
|
|
47
|
+
principal: "principal",
|
|
48
|
+
signal: "signal",
|
|
49
|
+
lastEventId: "lastEventId"
|
|
50
|
+
};
|
|
51
|
+
const InjectedParts = [
|
|
52
|
+
"principal",
|
|
53
|
+
"signal",
|
|
54
|
+
"lastEventId"
|
|
55
|
+
];
|
|
56
|
+
class HttpRequest {
|
|
57
|
+
constructor(data){
|
|
58
|
+
Object.assign(this, data);
|
|
59
|
+
}
|
|
60
|
+
requestId = "";
|
|
61
|
+
url;
|
|
62
|
+
method;
|
|
63
|
+
params = {};
|
|
64
|
+
query = {};
|
|
65
|
+
body;
|
|
66
|
+
headers = {};
|
|
67
|
+
/** Aborts when the client disconnects or the app stops. Never aborts if the provider cannot tell. */ signal = new AbortController().signal;
|
|
68
|
+
/** Outbound headers set by middleware; applied by the HTTP provider. */ responseHeaders = {};
|
|
69
|
+
}
|
|
70
|
+
class RouteConfigurationError extends Error {
|
|
71
|
+
}
|
|
72
|
+
class RoutingError extends Error {
|
|
73
|
+
}
|
|
74
|
+
class TransformError extends Error {
|
|
75
|
+
}
|
|
76
|
+
class HttpError extends Error {
|
|
77
|
+
status;
|
|
78
|
+
constructor(message, status){
|
|
79
|
+
super(message);
|
|
80
|
+
this.status = status;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
class UnauthorizedError extends HttpError {
|
|
84
|
+
constructor(message = "Unauthorized"){
|
|
85
|
+
super(message, 401);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
class ForbiddenError extends HttpError {
|
|
89
|
+
constructor(message = "Forbidden"){
|
|
90
|
+
super(message, 403);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
class ValidationError extends HttpError {
|
|
94
|
+
errors;
|
|
95
|
+
constructor(message = "Bad Request", errors = []){
|
|
96
|
+
super(message, 400);
|
|
97
|
+
this.errors = errors;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
get Argument () {
|
|
13
|
+
return Argument;
|
|
14
|
+
},
|
|
15
|
+
get Param () {
|
|
16
|
+
return Param;
|
|
17
|
+
},
|
|
18
|
+
get Query () {
|
|
19
|
+
return Query;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const _reflection = require("@decohere/core/reflection");
|
|
23
|
+
const _const = require("../../const.js");
|
|
24
|
+
const _anviltypes = require("../anvil.types.js");
|
|
25
|
+
function Query(key) {
|
|
26
|
+
return Argument({
|
|
27
|
+
mappingKey: key,
|
|
28
|
+
argumentLocation: _anviltypes.RequestParts.query
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function Param(key) {
|
|
32
|
+
return Argument({
|
|
33
|
+
mappingKey: key,
|
|
34
|
+
argumentLocation: _anviltypes.RequestParts.params
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function Argument(options) {
|
|
38
|
+
return (target, functionName, argumentIndex)=>{
|
|
39
|
+
const { mappingKey, argumentLocation, pipes } = options;
|
|
40
|
+
const func = target[functionName];
|
|
41
|
+
const meta = {
|
|
42
|
+
argumentIndex,
|
|
43
|
+
mappingKey,
|
|
44
|
+
argumentLocation,
|
|
45
|
+
pipes
|
|
46
|
+
};
|
|
47
|
+
(0, _reflection.defineArgumentMeta)(_const.MetadataKeys.parameter, meta, func, argumentIndex);
|
|
48
|
+
};
|
|
49
|
+
}
|