@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.
Files changed (72) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +145 -0
  3. package/dist/index.d.mts +425 -0
  4. package/dist/index.d.ts +425 -0
  5. package/dist/index.js +1744 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/index.mjs +1657 -0
  8. package/dist/index.mjs.map +1 -0
  9. package/package.json +45 -0
  10. package/src/__tests__/controller.spec.mts +59 -0
  11. package/src/attribute.factory.mts +155 -0
  12. package/src/decorators/controller.decorator.mts +36 -0
  13. package/src/decorators/endpoint.decorator.mts +81 -0
  14. package/src/decorators/index.mts +4 -0
  15. package/src/decorators/module.decorator.mts +47 -0
  16. package/src/decorators/use-guards.decorator.mts +43 -0
  17. package/src/exceptions/bad-request.exception.mts +7 -0
  18. package/src/exceptions/conflict.exception.mts +7 -0
  19. package/src/exceptions/forbidden.exception.mts +7 -0
  20. package/src/exceptions/http.exception.mts +7 -0
  21. package/src/exceptions/index.mts +7 -0
  22. package/src/exceptions/internal-server-error.exception.mts +7 -0
  23. package/src/exceptions/not-found.exception.mts +10 -0
  24. package/src/exceptions/unauthorized.exception.mts +7 -0
  25. package/src/index.mts +10 -0
  26. package/src/interfaces/can-activate.mts +5 -0
  27. package/src/interfaces/index.mts +2 -0
  28. package/src/interfaces/navios-module.mts +3 -0
  29. package/src/metadata/controller.metadata.mts +71 -0
  30. package/src/metadata/endpoint.metadata.mts +69 -0
  31. package/src/metadata/index.mts +4 -0
  32. package/src/metadata/injectable.metadata.mts +11 -0
  33. package/src/metadata/module.metadata.mts +62 -0
  34. package/src/navios.application.mts +113 -0
  35. package/src/navios.factory.mts +17 -0
  36. package/src/service-locator/__tests__/injectable.spec.mts +170 -0
  37. package/src/service-locator/decorators/get-injectable-token.mts +23 -0
  38. package/src/service-locator/decorators/index.mts +2 -0
  39. package/src/service-locator/decorators/injectable.decorator.mts +103 -0
  40. package/src/service-locator/enums/index.mts +1 -0
  41. package/src/service-locator/enums/injectable-scope.enum.mts +10 -0
  42. package/src/service-locator/errors/errors.enum.mts +7 -0
  43. package/src/service-locator/errors/factory-not-found.mts +8 -0
  44. package/src/service-locator/errors/index.mts +6 -0
  45. package/src/service-locator/errors/instance-destroying.mts +8 -0
  46. package/src/service-locator/errors/instance-expired.mts +8 -0
  47. package/src/service-locator/errors/instance-not-found.mts +8 -0
  48. package/src/service-locator/errors/unknown-error.mts +15 -0
  49. package/src/service-locator/event-emitter.mts +107 -0
  50. package/src/service-locator/index.mts +14 -0
  51. package/src/service-locator/inject.mts +37 -0
  52. package/src/service-locator/injection-token.mts +36 -0
  53. package/src/service-locator/injector.mts +18 -0
  54. package/src/service-locator/interfaces/factory.interface.mts +3 -0
  55. package/src/service-locator/override.mts +22 -0
  56. package/src/service-locator/proxy-service-locator.mts +80 -0
  57. package/src/service-locator/service-locator-abstract-factory-context.mts +23 -0
  58. package/src/service-locator/service-locator-event-bus.mts +96 -0
  59. package/src/service-locator/service-locator-instance-holder.mts +63 -0
  60. package/src/service-locator/service-locator-manager.mts +89 -0
  61. package/src/service-locator/service-locator.mts +445 -0
  62. package/src/service-locator/sync-injector.mts +55 -0
  63. package/src/services/controller-adapter.service.mts +124 -0
  64. package/src/services/execution-context.mts +53 -0
  65. package/src/services/guard-runner.service.mts +93 -0
  66. package/src/services/index.mts +4 -0
  67. package/src/services/module-loader.service.mts +68 -0
  68. package/src/tokens/application.token.mts +9 -0
  69. package/src/tokens/execution-context.token.mts +8 -0
  70. package/src/tokens/index.mts +4 -0
  71. package/src/tokens/reply.token.mts +7 -0
  72. 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,4 @@
1
+ export * from './controller-adapter.service.mjs'
2
+ export * from './execution-context.mjs'
3
+ export * from './guard-runner.service.mjs'
4
+ export * from './module-loader.service.mjs'
@@ -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,4 @@
1
+ export * from './application.token.mjs'
2
+ export * from './execution-context.token.mjs'
3
+ export * from './reply.token.mjs'
4
+ export * from './request.token.mjs'
@@ -0,0 +1,7 @@
1
+ import type { FastifyReply } from 'fastify'
2
+
3
+ import { InjectionToken } from '../service-locator/index.mjs'
4
+
5
+ const ReplyInjectionToken = 'ReplyInjectionToken'
6
+
7
+ export const Reply = InjectionToken.create<FastifyReply>(ReplyInjectionToken)
@@ -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
+ )