@navios/core 0.1.0
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/LICENSE +7 -0
- package/README.md +145 -0
- package/dist/index.d.mts +425 -0
- package/dist/index.d.ts +425 -0
- package/dist/index.js +1744 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1657 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +45 -0
- package/src/__tests__/controller.spec.mts +59 -0
- package/src/attribute.factory.mts +155 -0
- package/src/decorators/controller.decorator.mts +36 -0
- package/src/decorators/endpoint.decorator.mts +81 -0
- package/src/decorators/index.mts +4 -0
- package/src/decorators/module.decorator.mts +47 -0
- package/src/decorators/use-guards.decorator.mts +43 -0
- package/src/exceptions/bad-request.exception.mts +7 -0
- package/src/exceptions/conflict.exception.mts +7 -0
- package/src/exceptions/forbidden.exception.mts +7 -0
- package/src/exceptions/http.exception.mts +7 -0
- package/src/exceptions/index.mts +7 -0
- package/src/exceptions/internal-server-error.exception.mts +7 -0
- package/src/exceptions/not-found.exception.mts +10 -0
- package/src/exceptions/unauthorized.exception.mts +7 -0
- package/src/index.mts +10 -0
- package/src/interfaces/can-activate.mts +5 -0
- package/src/interfaces/index.mts +2 -0
- package/src/interfaces/navios-module.mts +3 -0
- package/src/metadata/controller.metadata.mts +71 -0
- package/src/metadata/endpoint.metadata.mts +69 -0
- package/src/metadata/index.mts +4 -0
- package/src/metadata/injectable.metadata.mts +11 -0
- package/src/metadata/module.metadata.mts +62 -0
- package/src/navios.application.mts +113 -0
- package/src/navios.factory.mts +17 -0
- package/src/service-locator/__tests__/injectable.spec.mts +170 -0
- package/src/service-locator/decorators/get-injectable-token.mts +23 -0
- package/src/service-locator/decorators/index.mts +2 -0
- package/src/service-locator/decorators/injectable.decorator.mts +103 -0
- package/src/service-locator/enums/index.mts +1 -0
- package/src/service-locator/enums/injectable-scope.enum.mts +10 -0
- package/src/service-locator/errors/errors.enum.mts +7 -0
- package/src/service-locator/errors/factory-not-found.mts +8 -0
- package/src/service-locator/errors/index.mts +6 -0
- package/src/service-locator/errors/instance-destroying.mts +8 -0
- package/src/service-locator/errors/instance-expired.mts +8 -0
- package/src/service-locator/errors/instance-not-found.mts +8 -0
- package/src/service-locator/errors/unknown-error.mts +15 -0
- package/src/service-locator/event-emitter.mts +107 -0
- package/src/service-locator/index.mts +14 -0
- package/src/service-locator/inject.mts +37 -0
- package/src/service-locator/injection-token.mts +36 -0
- package/src/service-locator/injector.mts +18 -0
- package/src/service-locator/interfaces/factory.interface.mts +3 -0
- package/src/service-locator/override.mts +22 -0
- package/src/service-locator/proxy-service-locator.mts +80 -0
- package/src/service-locator/service-locator-abstract-factory-context.mts +23 -0
- package/src/service-locator/service-locator-event-bus.mts +96 -0
- package/src/service-locator/service-locator-instance-holder.mts +63 -0
- package/src/service-locator/service-locator-manager.mts +89 -0
- package/src/service-locator/service-locator.mts +445 -0
- package/src/service-locator/sync-injector.mts +55 -0
- package/src/services/controller-adapter.service.mts +124 -0
- package/src/services/execution-context.mts +53 -0
- package/src/services/guard-runner.service.mts +93 -0
- package/src/services/index.mts +4 -0
- package/src/services/module-loader.service.mts +68 -0
- package/src/tokens/application.token.mts +9 -0
- package/src/tokens/execution-context.token.mts +8 -0
- package/src/tokens/index.mts +4 -0
- package/src/tokens/reply.token.mts +7 -0
- package/src/tokens/request.token.mts +9 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { CanActivate } from '../interfaces/index.mjs'
|
|
2
|
+
import type { ClassTypeWithInstance } from '../service-locator/index.mjs'
|
|
3
|
+
import type { ExecutionContext } from './execution-context.mjs'
|
|
4
|
+
|
|
5
|
+
import { HttpException } from '../exceptions/index.mjs'
|
|
6
|
+
import {
|
|
7
|
+
inject,
|
|
8
|
+
Injectable,
|
|
9
|
+
InjectionToken,
|
|
10
|
+
} from '../service-locator/index.mjs'
|
|
11
|
+
|
|
12
|
+
@Injectable()
|
|
13
|
+
export class GuardRunnerService {
|
|
14
|
+
async runGuards(
|
|
15
|
+
allGuards: Set<
|
|
16
|
+
| ClassTypeWithInstance<CanActivate>
|
|
17
|
+
| InjectionToken<CanActivate, undefined>
|
|
18
|
+
>,
|
|
19
|
+
executionContext: ExecutionContext,
|
|
20
|
+
) {
|
|
21
|
+
let canActivate = true
|
|
22
|
+
for (const guard of Array.from(allGuards).reverse()) {
|
|
23
|
+
const guardInstance = await inject(
|
|
24
|
+
guard as InjectionToken<CanActivate, undefined>,
|
|
25
|
+
)
|
|
26
|
+
if (!guardInstance.canActivate) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`[Navios] Guard ${guard.name as string} does not implement canActivate()`,
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
canActivate = await guardInstance.canActivate(executionContext)
|
|
33
|
+
if (!canActivate) {
|
|
34
|
+
break
|
|
35
|
+
}
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (error instanceof HttpException) {
|
|
38
|
+
executionContext
|
|
39
|
+
.getReply()
|
|
40
|
+
.status(error.statusCode)
|
|
41
|
+
.send(error.response)
|
|
42
|
+
return false
|
|
43
|
+
} else {
|
|
44
|
+
executionContext
|
|
45
|
+
.getReply()
|
|
46
|
+
.status(500)
|
|
47
|
+
.send({
|
|
48
|
+
message: 'Internal server error',
|
|
49
|
+
error: (error as Error).message,
|
|
50
|
+
})
|
|
51
|
+
return false
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!canActivate) {
|
|
56
|
+
executionContext.getReply().status(403).send({
|
|
57
|
+
message: 'Forbidden',
|
|
58
|
+
})
|
|
59
|
+
return false
|
|
60
|
+
}
|
|
61
|
+
return canActivate
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
makeContext(
|
|
65
|
+
executionContext: ExecutionContext,
|
|
66
|
+
): Set<
|
|
67
|
+
ClassTypeWithInstance<CanActivate> | InjectionToken<CanActivate, undefined>
|
|
68
|
+
> {
|
|
69
|
+
const guards = new Set<
|
|
70
|
+
| ClassTypeWithInstance<CanActivate>
|
|
71
|
+
| InjectionToken<CanActivate, undefined>
|
|
72
|
+
>()
|
|
73
|
+
const endpointGuards = executionContext.getHandler().guards
|
|
74
|
+
const controllerGuards = executionContext.getController().guards
|
|
75
|
+
const moduleGuards = executionContext.getModule().guards
|
|
76
|
+
if (endpointGuards.size > 0) {
|
|
77
|
+
for (const guard of endpointGuards) {
|
|
78
|
+
guards.add(guard)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (controllerGuards.size > 0) {
|
|
82
|
+
for (const guard of controllerGuards) {
|
|
83
|
+
guards.add(guard)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (moduleGuards.size > 0) {
|
|
87
|
+
for (const guard of moduleGuards) {
|
|
88
|
+
guards.add(guard)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return guards
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { NaviosModule } from '../interfaces/index.mjs'
|
|
2
|
+
import type { ModuleMetadata } from '../metadata/index.mjs'
|
|
3
|
+
import type { ClassTypeWithInstance } from '../service-locator/index.mjs'
|
|
4
|
+
|
|
5
|
+
import { extractModuleMetadata } from '../metadata/index.mjs'
|
|
6
|
+
import { inject, Injectable } from '../service-locator/index.mjs'
|
|
7
|
+
|
|
8
|
+
@Injectable()
|
|
9
|
+
export class ModuleLoaderService {
|
|
10
|
+
private modulesMetadata: Map<string, ModuleMetadata> = new Map()
|
|
11
|
+
private loadedModules: Map<string, any> = new Map()
|
|
12
|
+
private initialized = false
|
|
13
|
+
|
|
14
|
+
async loadModules(appModule: ClassTypeWithInstance<NaviosModule>) {
|
|
15
|
+
if (this.initialized) {
|
|
16
|
+
return
|
|
17
|
+
}
|
|
18
|
+
await this.traverseModules(appModule)
|
|
19
|
+
this.initialized = true
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private async traverseModules(
|
|
23
|
+
module: ClassTypeWithInstance<NaviosModule>,
|
|
24
|
+
parentMetadata?: ModuleMetadata,
|
|
25
|
+
) {
|
|
26
|
+
const metadata = extractModuleMetadata(module)
|
|
27
|
+
if (parentMetadata) {
|
|
28
|
+
this.mergeMetadata(metadata, parentMetadata)
|
|
29
|
+
}
|
|
30
|
+
const moduleName = module.name
|
|
31
|
+
if (this.modulesMetadata.has(moduleName)) {
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
this.modulesMetadata.set(moduleName, metadata)
|
|
35
|
+
const imports = metadata.imports ?? new Set()
|
|
36
|
+
const loadingPromises = Array.from(imports).map(async (importedModule) =>
|
|
37
|
+
this.traverseModules(importedModule, metadata),
|
|
38
|
+
)
|
|
39
|
+
await Promise.all(loadingPromises)
|
|
40
|
+
const instance = await inject(module)
|
|
41
|
+
if (instance.onModuleInit) {
|
|
42
|
+
await instance.onModuleInit()
|
|
43
|
+
}
|
|
44
|
+
this.loadedModules.set(moduleName, instance)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private mergeMetadata(
|
|
48
|
+
metadata: ModuleMetadata,
|
|
49
|
+
parentMetadata: ModuleMetadata,
|
|
50
|
+
): void {
|
|
51
|
+
if (parentMetadata.guards) {
|
|
52
|
+
for (const guard of parentMetadata.guards) {
|
|
53
|
+
metadata.guards.add(guard)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (parentMetadata.customAttributes) {
|
|
57
|
+
for (const [key, value] of parentMetadata.customAttributes) {
|
|
58
|
+
if (metadata.customAttributes.has(key)) {
|
|
59
|
+
continue
|
|
60
|
+
}
|
|
61
|
+
metadata.customAttributes.set(key, value)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
getAllModules(): Map<string, ModuleMetadata> {
|
|
66
|
+
return this.modulesMetadata
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FastifyInstance } from 'fastify'
|
|
2
|
+
|
|
3
|
+
import { InjectionToken } from '../service-locator/index.mjs'
|
|
4
|
+
|
|
5
|
+
const ApplicationInjectionToken = 'ApplicationInjectionToken'
|
|
6
|
+
|
|
7
|
+
export const Application = InjectionToken.create<FastifyInstance>(
|
|
8
|
+
ApplicationInjectionToken,
|
|
9
|
+
)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { InjectionToken } from '../service-locator/index.mjs'
|
|
2
|
+
import { ExecutionContext } from '../services/index.mjs'
|
|
3
|
+
|
|
4
|
+
export const ExecutionContextInjectionToken = 'ExecutionContextInjectionToken'
|
|
5
|
+
|
|
6
|
+
export const ExecutionContextToken = InjectionToken.create<ExecutionContext>(
|
|
7
|
+
ExecutionContextInjectionToken,
|
|
8
|
+
)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FastifyRequest } from 'fastify'
|
|
2
|
+
|
|
3
|
+
import { InjectionToken } from '../service-locator/index.mjs'
|
|
4
|
+
|
|
5
|
+
const RequestInjectionToken = 'RequestInjectionToken'
|
|
6
|
+
|
|
7
|
+
export const Request = InjectionToken.create<FastifyRequest>(
|
|
8
|
+
RequestInjectionToken,
|
|
9
|
+
)
|