5htp-core 0.2.9 → 0.3.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 (75) hide show
  1. package/package.json +7 -5
  2. package/src/client/app/component.tsx +2 -2
  3. package/src/client/assets/css/text/titres.less +4 -0
  4. package/src/client/components/Dialog/card.tsx +1 -1
  5. package/src/client/components/Dialog/index.less +3 -3
  6. package/src/client/components/inputv3/date/index.tsx +1 -1
  7. package/src/client/components/inputv3/index.tsx +14 -4
  8. package/src/client/pages/_layout/index.tsx +3 -3
  9. package/src/client/pages/_messages/400.tsx +2 -2
  10. package/src/client/pages/_messages/401.tsx +2 -2
  11. package/src/client/pages/_messages/403.tsx +2 -2
  12. package/src/client/pages/_messages/404.tsx +2 -2
  13. package/src/client/pages/_messages/500.tsx +2 -2
  14. package/src/client/pages/useHeader.tsx +2 -1
  15. package/src/client/services/router/components/router.tsx +1 -1
  16. package/src/client/services/router/index.tsx +5 -1
  17. package/src/client/services/router/response/index.tsx +5 -2
  18. package/src/common/data/objets.ts +0 -25
  19. package/src/common/router/index.ts +8 -12
  20. package/src/common/router/layouts.ts +3 -1
  21. package/src/common/router/register.ts +5 -3
  22. package/src/common/router/response/index.ts +3 -3
  23. package/src/server/app/commands.ts +2 -11
  24. package/src/server/app/{config.ts → container/config.ts} +4 -1
  25. package/src/server/app/container/index.ts +84 -0
  26. package/src/server/app/index.ts +73 -136
  27. package/src/server/app/instance.ts +3 -0
  28. package/src/server/app/service/container.ts +136 -0
  29. package/src/server/app/service/index.ts +219 -0
  30. package/src/server/index.ts +9 -3
  31. package/src/server/services/cache/index.ts +42 -16
  32. package/src/server/services/cache/service.json +6 -0
  33. package/src/server/services/console/index.ts +35 -22
  34. package/src/server/services/console/service.json +6 -0
  35. package/src/server/services/cron/index.ts +11 -11
  36. package/src/server/services/cron/service.json +6 -0
  37. package/src/server/services/database/connection.ts +26 -34
  38. package/src/server/services/database/index.ts +41 -21
  39. package/src/server/services/database/metas.ts +12 -7
  40. package/src/server/services/database/repository.ts +0 -9
  41. package/src/server/services/database/service.json +6 -0
  42. package/src/server/services/database/stats.ts +3 -3
  43. package/src/server/services/disks/driver.ts +9 -7
  44. package/src/server/services/disks/drivers/local/index.ts +199 -0
  45. package/src/server/services/disks/drivers/local/service.json +6 -0
  46. package/src/server/services/disks/drivers/s3/index.ts +282 -0
  47. package/src/server/services/disks/drivers/s3/service.json +6 -0
  48. package/src/server/services/disks/index.ts +47 -16
  49. package/src/server/services/disks/service.json +6 -0
  50. package/src/server/services/email/index.ts +16 -4
  51. package/src/server/services/email/service.json +6 -0
  52. package/src/server/services/fetch/index.ts +52 -8
  53. package/src/server/services/fetch/service.json +6 -0
  54. package/src/server/services/router/http/index.ts +12 -9
  55. package/src/server/services/router/index.ts +95 -68
  56. package/src/server/services/router/request/api.ts +1 -1
  57. package/src/server/services/router/response/index.ts +13 -19
  58. package/src/server/services/router/service.json +6 -0
  59. package/src/server/services/router/service.ts +16 -29
  60. package/src/server/services/schema/{router.ts → router/index.ts} +19 -7
  61. package/src/server/services/schema/router/service.json +6 -0
  62. package/src/server/services/schema/service.json +6 -0
  63. package/src/server/services/security/encrypt/{aes.ts → aes/index.ts} +16 -3
  64. package/src/server/services/security/encrypt/aes/service.json +6 -0
  65. package/src/server/services/socket/index.ts +39 -13
  66. package/src/server/services/socket/service.json +6 -0
  67. package/src/server/services/users/index.ts +16 -4
  68. package/src/server/services/users/old.ts +1 -1
  69. package/src/server/services/users/router/index.ts +21 -14
  70. package/src/server/services/users/router/service.json +6 -0
  71. package/src/server/services/users/service.json +6 -0
  72. package/src/types/aliases.d.ts +5 -0
  73. package/tsconfig.common.json +2 -0
  74. package/src/server/app/service.ts +0 -109
  75. /package/src/server/{patch.ts → app/container/patch.ts} +0 -0
@@ -2,8 +2,11 @@
2
2
  - DEPENDANCES
3
3
  ----------------------------------*/
4
4
 
5
+ // Core
6
+ import type { Application } from '@server/app';
7
+ import Service, { TRegisteredServicesIndex } from '@server/app/service';
8
+
5
9
  // Specific
6
- import type Application from '@server/app';
7
10
  import type { default as Router } from '.';
8
11
  import type ServerRequest from './request';
9
12
  import type RequestService from './request/service';
@@ -11,38 +14,22 @@ import type RequestService from './request/service';
11
14
  /*----------------------------------
12
15
  - SERVICE
13
16
  ----------------------------------*/
14
- export default abstract class RouterService<TRouter extends Router = Router> {
15
-
16
- protected router!: TRouter;
17
- protected app!: Application;
18
-
19
- public constructor(
20
-
17
+ export default abstract class RouterService<
18
+ TConfig extends {} = {}
19
+ > extends Service<TConfig, {}, Application> {
20
+
21
+ public constructor(
22
+ // Parent is always a router in RouterService
23
+ public router: Router,
24
+ config: TConfig,
25
+ services: TRegisteredServicesIndex,
26
+ app: Application
21
27
  ) {
22
28
 
29
+ super(router, config, services, app);
30
+
23
31
  }
24
32
 
25
- /*
26
- We can't pass the router instance in the routerservice constructor
27
- Because we instanciate the routerservice in the router instanciation itself
28
- So if we do:
29
- public router = new Router(this, {
30
- ...,
31
- services: () => ({
32
-
33
- auth: new AuthService(this.router, this.users),
34
-
35
- }),
36
- )
37
- We would have a cicular reference in typings, which will make router typed as any
38
- */
39
- public attach( router: TRouter ) {
40
- this.router = router;
41
- this.app = router.app;
42
- }
43
-
44
- public abstract register(): Promise<void>;
45
-
46
33
  public abstract requestService( request: ServerRequest<TRouter> ): RequestService | null;
47
34
 
48
35
  }
@@ -8,29 +8,41 @@ import {
8
8
  RouterService
9
9
  } from '@server/services/router';
10
10
 
11
- import RequestValidator, { TConfig } from './request';
11
+ import RequestValidator, { TConfig } from '../request';
12
12
 
13
13
  /*----------------------------------
14
14
  - TYPES
15
15
  ----------------------------------*/
16
16
 
17
- type TRouterWithSchema<TAuthService extends AuthenticationRouterService> = Router<RouterConfig<{ auth: TAuthService }>>
17
+ type TRouterWithSchema<TAuthService extends SchemaRouterService> = Router<RouterConfig<{ auth: TAuthService }>>
18
18
 
19
19
  /*----------------------------------
20
20
  - SERVICE
21
21
  ----------------------------------*/
22
- export default class AuthenticationRouterService<
22
+ export default class SchemaRouterService<
23
23
  TUser extends {} = {}
24
24
  > extends RouterService {
25
25
 
26
- public constructor( protected config: TConfig ) {
27
- super();
28
- }
26
+ /*----------------------------------
27
+ - LIFECYCLE
28
+ ----------------------------------*/
29
29
 
30
- public async register() {
30
+ public async start() {
31
31
 
32
32
  }
33
33
 
34
+ public async ready() {
35
+
36
+ }
37
+
38
+ public async shutdown() {
39
+
40
+ }
41
+
42
+ /*----------------------------------
43
+ - ROUTER SERVICE LIFECYCLE
44
+ ----------------------------------*/
45
+
34
46
  public requestService( request: ServerRequest< TRouterWithSchema<this>> ): RequestValidator {
35
47
  return new RequestValidator( request, this.config );
36
48
  }
@@ -0,0 +1,6 @@
1
+ {
2
+ "id": "Core/Schema/Router",
3
+ "name": "RouteSchema",
4
+ "parent": "router",
5
+ "dependences": []
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "id": "Core/Schema",
3
+ "name": "Schema",
4
+ "parent": "app",
5
+ "dependences": []
6
+ }
@@ -6,7 +6,8 @@
6
6
  import crypto, { Encoding } from 'crypto';
7
7
 
8
8
  // Core
9
- import Application, { Service } from '@server/app';
9
+ import type { Application } from '@server/app';
10
+ import Service from '@server/app/service';
10
11
  import { Forbidden } from '@common/errors';
11
12
 
12
13
  /*----------------------------------
@@ -45,14 +46,26 @@ type TDecryptOptions = {
45
46
  ----------------------------------*/
46
47
  export default class AES<TConfig extends Config = Config> extends Service<TConfig, Hooks, Application> {
47
48
 
48
- public async register() {
49
+ /*----------------------------------
50
+ - LIFECYCLE
51
+ ----------------------------------*/
52
+
53
+ public async start() {
49
54
 
50
55
  }
51
56
 
52
- public async start() {
57
+ public async ready() {
53
58
 
54
59
  }
55
60
 
61
+ public async shutdown() {
62
+
63
+ }
64
+
65
+ /*----------------------------------
66
+ - ACTIONS
67
+ ----------------------------------*/
68
+
56
69
  public encrypt( keyName: keyof TConfig["keys"], data: any, options: TEncryptOptions = {
57
70
  encoding: 'base64url'
58
71
  }) {
@@ -0,0 +1,6 @@
1
+ {
2
+ "id": "Core/Encryption/AES",
3
+ "name": "AES",
4
+ "parent": "app",
5
+ "dependences": []
6
+ }
@@ -9,8 +9,10 @@ import { IncomingMessage } from 'http';
9
9
  import cookie from 'cookie';
10
10
 
11
11
  // Core
12
- import Application, { Service } from '@server/app';
12
+ import type { Application } from '@server/app';
13
+ import Service, { AnyService, TRegisteredService } from '@server/app/service';
13
14
  import SocketScope, { WebSocket } from './scope';
15
+ import type Router from '@server/services/router';
14
16
  export type { WebSocket, default as SocketScope } from './scope';
15
17
  import type UsersManagementService from '../users';
16
18
 
@@ -20,8 +22,9 @@ import type UsersManagementService from '../users';
20
22
 
21
23
 
22
24
  export type Config<TUser extends {}> = {
23
- server: ServerOptions["server"],
24
- users: UsersManagementService<TUser>,
25
+ debug?: boolean,
26
+ //server: ServerOptions["server"],
27
+ //users: UsersManagementService<TUser>,
25
28
  port: number,
26
29
  }
27
30
 
@@ -40,33 +43,44 @@ export default class WebSocketCommander<
40
43
  // Services
41
44
  public ws!: WebSocketServer;
42
45
  public users: UsersManagementService<TUser>;
46
+ public router: Router;
43
47
 
44
48
  // Context
45
49
  public scopes: {[path: string]: SocketScope<TUser>} = {}
46
50
 
47
- public constructor( app: Application, config: TConfig ) {
48
- super(app, config);
51
+ public constructor(
52
+ parent: AnyService,
53
+ config: TConfig,
54
+ services: {
55
+ users: TRegisteredService< UsersManagementService<TUser> >,
56
+ router: Router
57
+ },
58
+ app: Application,
59
+ ) {
49
60
 
50
- this.users = config.users;
61
+ super(parent, config, services, app);
62
+
63
+ this.users = this.services.users;
64
+ this.router = this.services.router;
51
65
  }
66
+
67
+ /*----------------------------------
68
+ - LIFECYCLE
69
+ ----------------------------------*/
52
70
 
53
- public async register() {
71
+ public loading: Promise<void> | undefined = undefined;
72
+ protected async start() {
54
73
 
55
74
  this.app.on('cleanup', async () => {
56
75
  this.closeAll();
57
76
  });
58
77
 
59
-
60
78
  this.users.on('disconnect', async (userId: string) => {
61
79
  this.disconnect(userId, 'Logout');
62
80
  });
63
- }
64
-
65
- public loading: Promise<void> | undefined = undefined;
66
- public async start() {
67
81
 
68
82
  console.info(`Loading socket commander`);
69
- this.ws = new WebSocketServer({ server: this.config.server })
83
+ this.ws = new WebSocketServer({ server: this.router.http.http })
70
84
  .on('connection', (socket: WebSocket, req: IncomingMessage) => {
71
85
 
72
86
  // Resolve scope
@@ -107,6 +121,18 @@ export default class WebSocketCommander<
107
121
  console.info(`Socket commander bound to http server.`);
108
122
  }
109
123
 
124
+ public async ready() {
125
+
126
+ }
127
+
128
+ public async shutdown() {
129
+
130
+ }
131
+
132
+ /*----------------------------------
133
+ - ACTIONS
134
+ ----------------------------------*/
135
+
110
136
  public open(path: string) {
111
137
 
112
138
  if (!(path in this.scopes)) {
@@ -0,0 +1,6 @@
1
+ {
2
+ "id": "Core/Socket",
3
+ "name": "Socket",
4
+ "parent": "app",
5
+ "dependences": []
6
+ }
@@ -8,7 +8,7 @@ import type express from 'express';
8
8
  import type http from 'http';
9
9
 
10
10
  // Core
11
- import Application from '@server/app';
11
+ import { Application } from '@server/app';
12
12
  import Service from '@server/app/service';
13
13
  import {
14
14
  default as Router, Request as ServerRequest,
@@ -68,14 +68,26 @@ export default abstract class UsersManagementService<
68
68
  TRequest extends ServerRequest<Router> = ServerRequest<Router>,
69
69
  > extends Service<TConfig, THooks, TApplication> {
70
70
 
71
- public async register() {
72
-
73
- }
71
+ /*----------------------------------
72
+ - LIFECYCLE
73
+ ----------------------------------*/
74
74
 
75
75
  public async start() {
76
+
77
+ }
78
+
79
+ public async ready() {
76
80
 
77
81
  }
78
82
 
83
+ public async shutdown() {
84
+
85
+ }
86
+
87
+ /*----------------------------------
88
+ - ACTIONS
89
+ ----------------------------------*/
90
+
79
91
  public abstract login( ...args: any[] ): Promise<{ user: TUser, token: string }>;
80
92
  public abstract decodeSession( jwt: TJwtSession, req: THttpRequest ): Promise<TUser>;
81
93
 
@@ -13,7 +13,7 @@ type AuthResponse = {
13
13
  export default class {
14
14
 
15
15
 
16
- public async start() {
16
+ protected async start() {
17
17
 
18
18
  // Google auth client
19
19
  if (this.config.google) {
@@ -5,10 +5,8 @@
5
5
  // Npm
6
6
 
7
7
  // Core
8
- import type Application from '@server/app';
9
-
10
8
  import {
11
- default as Router, Request as ServerRequest, TRoute,
9
+ default as Router, Request as ServerRequest, TAnyRoute,
12
10
  RouterService
13
11
  } from '@server/services/router';
14
12
 
@@ -35,35 +33,44 @@ export default class AuthenticationRouterService<
35
33
  TRequest extends ServerRequest<Router> = ServerRequest<Router>,
36
34
  > extends RouterService {
37
35
 
38
- public constructor(
39
- public users: UsersService<TUser, Application>,
40
- public config = users.config
41
- ) {
42
-
43
- super();
36
+ /*----------------------------------
37
+ - LIFECYCLE
38
+ ----------------------------------*/
44
39
 
45
- }
40
+ public users = this.services.users;
46
41
 
47
- public async register() {
42
+ protected async start() {
48
43
 
49
44
  // Decode current user
50
45
  this.router.on('request', async (request: TRequest) => {
51
46
 
52
47
  // TODO: Typings. (context.user ?)
53
- const decoded = await this.users.decode( request.req, true);
48
+ const decoded = await this.services.users.decode( request.req, true);
54
49
 
55
50
  request.user = decoded || null;
56
51
  })
57
52
 
58
53
  // Check route permissions
59
- this.router.on('resolved', async (route: TRoute, request: TRequest) => {
54
+ this.router.on('resolved', async (route: TAnyRoute, request: TRequest) => {
60
55
 
61
56
  if (route.options.auth !== undefined)
62
57
  // TODO: How to pas the router type to router config ? Circular rfeerence ?
63
- this.users.check(request, route.options.auth);
58
+ this.services.users.check(request, route.options.auth);
64
59
  })
65
60
  }
66
61
 
62
+ protected async ready() {
63
+
64
+ }
65
+
66
+ protected async shutdown() {
67
+
68
+ }
69
+
70
+ /*----------------------------------
71
+ - ROUTER SERVICE LIFECYCLE
72
+ ----------------------------------*/
73
+
67
74
  public requestService( request: TRequest ): UsersRequestService<TUser> {
68
75
  return new UsersRequestService( request, this );
69
76
  }
@@ -0,0 +1,6 @@
1
+ {
2
+ "id": "Core/Users/Router",
3
+ "name": "RouteUser",
4
+ "parent": "router",
5
+ "dependences": []
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "id": "Core/Users",
3
+ "name": "Users",
4
+ "parent": "app",
5
+ "dependences": []
6
+ }
@@ -51,4 +51,9 @@ declare module "@/client/context" {
51
51
  const useContext: () => ClientContext;
52
52
 
53
53
  export = useContext;
54
+ }
55
+
56
+ declare module "@app" {
57
+ const ServerApplicationClass: import("../server/app").default;
58
+ export = ServerApplicationClass;
54
59
  }
@@ -18,6 +18,8 @@
18
18
  // Décorateurs
19
19
  "experimentalDecorators": true,
20
20
 
21
+ "inlineSourceMap": true,
22
+
21
23
  // React
22
24
  "jsx": "react-jsx",
23
25
  "jsxImportSource": "preact",
@@ -1,109 +0,0 @@
1
- /*----------------------------------
2
- - DEPENDANCES
3
- ----------------------------------*/
4
-
5
- // Specific
6
- import Application from ".";
7
- import type { Command } from "./commands";
8
-
9
- /*----------------------------------
10
- - TYPES: OPTIONS
11
- ----------------------------------*/
12
-
13
- export type AnyService = Service<{}, {}, Application>
14
-
15
- type THookCallback<THookArgs extends THookOptions> = (...args: THookArgs["args"]) => Promise<void>;
16
-
17
- type THooksList = {
18
- [hookName: string]: THookOptions
19
- }
20
-
21
- type THookOptions = {
22
- args: any[]
23
- }
24
-
25
- export type TPriority = -2 | -1 | 0 | 1 | 2
26
-
27
- type TServiceConfig = {
28
- debug?: boolean
29
- }
30
-
31
- /*----------------------------------
32
- - CONFIG
33
- ----------------------------------*/
34
-
35
- const LogPrefix = '[service]';
36
-
37
- /*----------------------------------
38
- - CLASS
39
- ----------------------------------*/
40
- export default abstract class Service<
41
- TConfig extends TServiceConfig,
42
- THooks extends THooksList,
43
- TApplication extends Application
44
- > {
45
-
46
- public priority: TPriority = 0;
47
- public started?: Promise<void>;
48
- public status: 'stopped' | 'starting' | 'running' | 'paused' = 'stopped';
49
-
50
- public commands?: Command[];
51
-
52
- public constructor(
53
- public app: TApplication,
54
- public config: TConfig,
55
- ) {
56
-
57
- if (!( this instanceof Application ))
58
- // Make the app aware of his services
59
- app.registerService(this);
60
-
61
- }
62
-
63
- public abstract register?(): Promise<void>;
64
-
65
- public abstract start?(): Promise<void>;
66
-
67
- /*----------------------------------
68
- - HOOKS
69
- ----------------------------------*/
70
- public hooks: {[name in keyof THooks]?: THookCallback< THooks[name] >[]} = {}
71
-
72
- public on<THookName extends keyof THooksList>(
73
- name: THookName,
74
- callback: THookCallback<THooksList[THookName]>
75
- ) {
76
-
77
- const callbacks = this.hooks[ name ];
78
- if (callbacks)
79
- callbacks.push( callback );
80
- else
81
- this.hooks[ name ] = [callback]
82
-
83
- return this;
84
- }
85
-
86
- public runHook<THookName extends keyof THooksList>(
87
- name: THookName,
88
- ...args: THooksList[THookName]["args"]
89
- ) {
90
-
91
- const callbacks = this.hooks[name];
92
- if (!callbacks)
93
- return console.info(LogPrefix, `No ${name} hook defined in the current service instance.`);
94
-
95
- this.config.debug && console.info(`[hook] Run all ${name} hook (${callbacks.length}).`);
96
- return Promise.all(
97
- callbacks.map(
98
- cb => cb(...args).catch(e => {
99
- console.error(`[hook] Error while executing hook ${name}:`, e);
100
- if (name !== 'error')
101
- this.runHook('error', e);
102
- })
103
- )
104
- ).then(() => {
105
- this.config.debug && console.info(`[hook] Hooks ${name} executed with success.`);
106
- })
107
- }
108
-
109
- }
File without changes