5htp-core 0.1.2-3 → 0.2.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 (126) hide show
  1. package/changelog.md +5 -0
  2. package/doc/TODO.md +71 -0
  3. package/package.json +5 -4
  4. package/src/client/{App.tsx → app/component.tsx} +15 -8
  5. package/src/client/app/index.ts +128 -0
  6. package/src/client/app/service.ts +34 -0
  7. package/src/client/app.tsconfig.json +0 -4
  8. package/src/client/components/Card/index.tsx +1 -1
  9. package/src/client/components/Dialog/Manager.tsx +39 -12
  10. package/src/client/components/Form/index.tsx +1 -1
  11. package/src/client/components/button.tsx +2 -2
  12. package/src/client/components/containers/Popover/index.tsx +1 -1
  13. package/src/client/components/data/spintext/index.tsx +1 -1
  14. package/src/client/components/dropdown/index.tsx +1 -1
  15. package/src/client/components/index.ts +8 -0
  16. package/src/client/components/input/BaseV2/index.tsx +1 -1
  17. package/src/client/components/input/UploadImage/index.tsx +1 -1
  18. package/src/client/hooks/index.ts +5 -0
  19. package/src/client/hooks/useState/index.tsx +2 -2
  20. package/src/client/hooks.ts +22 -0
  21. package/src/client/index.ts +5 -0
  22. package/src/client/pages/_layout/landing/index.tsx +0 -2
  23. package/src/client/pages/_messages/400.tsx +2 -2
  24. package/src/client/pages/_messages/401.tsx +2 -2
  25. package/src/client/pages/_messages/403.tsx +2 -2
  26. package/src/client/pages/_messages/404.tsx +2 -2
  27. package/src/client/pages/_messages/500.tsx +2 -2
  28. package/src/client/pages/bug.tsx +1 -1
  29. package/src/client/pages/useHeader.tsx +1 -1
  30. package/src/client/{context/captcha.ts → services/captcha/index.ts} +0 -0
  31. package/src/client/services/metrics/index.ts +37 -0
  32. package/src/client/{router → services/router/components}/Link.tsx +1 -1
  33. package/src/client/services/router/components/Page.tsx +59 -0
  34. package/src/client/{router/component.tsx → services/router/components/router.tsx} +43 -74
  35. package/src/client/services/router/index.tsx +448 -0
  36. package/src/client/services/router/request/api.ts +229 -0
  37. package/src/client/{router → services/router}/request/history.ts +0 -0
  38. package/src/client/services/router/request/index.ts +52 -0
  39. package/src/client/services/router/response/index.tsx +107 -0
  40. package/src/client/services/router/response/page.ts +95 -0
  41. package/src/client/{context/socket.ts → services/socket/index.ts} +2 -2
  42. package/src/client/utils/dom.ts +1 -1
  43. package/src/common/app/index.ts +9 -0
  44. package/src/common/data/chaines/index.ts +9 -6
  45. package/src/common/data/input/validate.ts +3 -166
  46. package/src/common/data/objets.ts +25 -0
  47. package/src/common/data/tableaux.ts +8 -0
  48. package/src/common/errors/index.ts +3 -1
  49. package/src/common/router/index.ts +67 -88
  50. package/src/common/router/layouts.ts +50 -0
  51. package/src/common/router/register.ts +62 -0
  52. package/src/common/router/request/api.ts +72 -0
  53. package/src/common/router/request/index.ts +31 -0
  54. package/src/common/router/{response.ts → response/index.ts} +9 -13
  55. package/src/common/router/response/page.ts +40 -56
  56. package/src/common/validation/index.ts +3 -0
  57. package/src/common/validation/schema.ts +184 -0
  58. package/src/common/validation/validator.ts +88 -0
  59. package/src/common/validation/validators.ts +313 -0
  60. package/src/server/app/config.ts +8 -10
  61. package/src/server/app/index.ts +81 -124
  62. package/src/server/app/service.ts +98 -0
  63. package/src/server/app.tsconfig.json +0 -8
  64. package/src/server/error/index.ts +13 -0
  65. package/src/server/index.ts +5 -0
  66. package/src/server/patch.ts +0 -6
  67. package/src/server/{data/Cache.ts → services/cache/index.ts} +79 -47
  68. package/src/server/services/console/bugReporter.ts +26 -16
  69. package/src/server/services/console/index.ts +50 -51
  70. package/src/server/services/cron/index.ts +12 -26
  71. package/src/server/services/database/bucket.ts +40 -0
  72. package/src/server/services/database/connection.ts +206 -75
  73. package/src/server/services/database/datatypes.ts +63 -40
  74. package/src/server/services/database/index.ts +295 -272
  75. package/src/server/services/database/metas.ts +246 -135
  76. package/src/server/services/database/stats.ts +151 -126
  77. package/src/server/services/email/index.ts +24 -54
  78. package/src/server/services/{router/request/services → metrics}/detect.ts +6 -10
  79. package/src/server/services/{router/request/services/tracking.ts → metrics/index.ts} +68 -45
  80. package/src/server/services/{http → router/http}/index.ts +19 -47
  81. package/src/server/services/{http → router/http}/multipart.ts +0 -0
  82. package/src/server/services/{http → router/http}/session.ts.old +0 -0
  83. package/src/server/services/router/index.ts +273 -203
  84. package/src/server/services/router/request/api.ts +73 -0
  85. package/src/server/services/router/request/index.ts +16 -95
  86. package/src/server/services/router/request/service.ts +21 -0
  87. package/src/server/services/router/response/index.ts +125 -64
  88. package/src/server/services/router/response/{filter → mask}/Filter.ts +0 -0
  89. package/src/server/services/router/response/{filter → mask}/index.ts +0 -2
  90. package/src/server/services/router/response/{filter → mask}/selecteurs.ts +0 -0
  91. package/src/server/services/router/response/page/document.tsx +194 -0
  92. package/src/server/services/router/response/page/index.tsx +157 -0
  93. package/src/server/{libs/pages → services/router/response/page}/schemaGenerator.ts +0 -0
  94. package/src/server/services/router/service.ts +48 -0
  95. package/src/server/services/schema/index.ts +47 -0
  96. package/src/server/services/schema/request.ts +55 -0
  97. package/src/server/services/schema/router.ts +33 -0
  98. package/src/server/services/socket/index.ts +38 -43
  99. package/src/server/services/socket/scope.ts +6 -4
  100. package/src/server/services/users/index.ts +203 -0
  101. package/src/server/services/{auth/base.ts → users/old.ts} +20 -111
  102. package/src/server/services/users/router/index.ts +72 -0
  103. package/src/server/services/users/router/request.ts +49 -0
  104. package/src/types/aliases.d.ts +43 -2
  105. package/templates/composant.tsx +1 -1
  106. package/templates/modal.tsx +1 -1
  107. package/templates/page.tsx +1 -1
  108. package/tsconfig.common.json +0 -4
  109. package/src/client/context/api.ts +0 -92
  110. package/src/client/context/index.ts +0 -246
  111. package/src/client/index.tsx +0 -129
  112. package/src/client/router/index.ts +0 -286
  113. package/src/client/router/request/index.ts +0 -106
  114. package/src/client/router/response/index.ts +0 -38
  115. package/src/client/router/route.ts +0 -75
  116. package/src/common/data/input/validators/basic.ts +0 -299
  117. package/src/common/data/input/validators/build.ts +0 -63
  118. package/src/common/router/request.ts +0 -83
  119. package/src/server/data/ApiClient.ts +0 -119
  120. package/src/server/data/input.ts +0 -41
  121. package/src/server/libs/pages/document.static.tsx +0 -41
  122. package/src/server/libs/pages/document.tsx +0 -203
  123. package/src/server/libs/pages/render.tsx +0 -90
  124. package/src/server/routes/auth.ts +0 -151
  125. package/src/server/services/redis/index.ts +0 -71
  126. package/src/server/services/router/request/services/auth.ts +0 -177
@@ -8,19 +8,29 @@ import dayjs from 'dayjs';
8
8
  import got from 'got';
9
9
 
10
10
  // Core
11
+ import Application, { Service } from '@server/app';
12
+ import type Router from '@server/services/router'
13
+ import type ServerRequest from '@server/services/router/request'
14
+ import type SQL from '@server/services/database';
11
15
  import { arrayToObj } from '@common/data/tableaux';
12
16
  import { Forbidden } from '@common/errors';
13
- import requete from '@server/data/ApiClient';
14
- import app, { $ } from '@server/app';
15
-
16
- // Models
17
- import { IP, User } from '@models';
18
17
 
19
18
  /*----------------------------------
20
19
  - TYPES
21
20
  ----------------------------------*/
22
21
 
23
- import type Request from '@server/services/router/request'
22
+ type IP = {
23
+ // Identity
24
+ ip: string,
25
+ address: string,
26
+ isp: string,
27
+ country: string,
28
+ iphub: number,
29
+ // Status
30
+ banned: Date,
31
+ banReason?: string,
32
+ updated?: Date
33
+ }
24
34
 
25
35
  export type TrackingInfos = {
26
36
  user: User | null,
@@ -32,10 +42,10 @@ export type TrackingInfos = {
32
42
  type TAssoChaines = { [id: string]: string }
33
43
 
34
44
  /*----------------------------------
35
- - TYPES
45
+ - SERVICE
36
46
  ----------------------------------*/
37
47
 
38
- export type TrackingServiceConfig = {
48
+ export type Config = {
39
49
  ga: {
40
50
  pub: string,
41
51
  prv: string,
@@ -43,32 +53,32 @@ export type TrackingServiceConfig = {
43
53
  }
44
54
  }
45
55
 
46
- declare global {
47
- namespace Core {
48
- namespace Config {
49
- interface Services {
50
- tracking: TrackingServiceConfig
51
- }
52
- }
53
- }
56
+ export type Hooks = {
57
+
54
58
  }
55
59
 
56
- /*----------------------------------
57
- - SERVICES
58
- ----------------------------------*/
59
- export default class TrackerService {
60
+ export default class TrackerService extends Service<Config, Hooks, Application> {
60
61
 
61
- /*----------------------------------
62
- - GLOBAL
63
- ----------------------------------*/
64
- public static countries: TAssoChaines;
65
- public static langues: TAssoChaines;
66
- public static locales: locale.Locales;
62
+ public countries!: TAssoChaines;
63
+ public langues!: TAssoChaines;
64
+ public locales!: locale.Locales;
65
+
66
+ public async register() {
67
+
68
+ }
67
69
 
68
- public static async LoadCache() {
70
+ public async start() {
71
+
72
+ await this.indexData();
73
+
74
+
75
+
76
+ }
77
+
78
+ private async indexData() {
69
79
 
70
80
  // On n'oublie pas le tri alphabétique pour les listings
71
- const [countries, langues] = await $.sql`
81
+ /*const [countries, langues] = await this.app.sql`
72
82
  SELECT id, name FROM Countries ORDER BY name ASC;
73
83
  SELECT id, name FROM Locales ORDER BY name ASC;
74
84
  `().then(([listePays, listeLangues]) => [
@@ -76,28 +86,41 @@ export default class TrackerService {
76
86
  arrayToObj(listeLangues, { index: 'id', val: 'name' }),
77
87
  ]);
78
88
 
79
- TrackerService.countries = countries as TAssoChaines;
80
- TrackerService.langues = langues as TAssoChaines;
81
- TrackerService.locales = new locale.Locales(Object.keys(this.langues));
89
+ this.countries = countries as TAssoChaines;
90
+ this.langues = langues as TAssoChaines;
91
+ this.locales = new locale.Locales(Object.keys(this.langues));*/
92
+ }
82
93
 
94
+ public async request( request: ServerRequest ) {
95
+ return new TrackingRequestService(request, this);
83
96
  }
97
+ }
98
+
99
+ /*----------------------------------
100
+ - REQUEST SERVICE
101
+ ----------------------------------*/
102
+ export class TrackingRequestService {
84
103
 
85
- /*----------------------------------
86
- - INSTANCE
87
- ----------------------------------*/
104
+ // Services
105
+ protected sql: SQL;
88
106
 
89
107
  // Caches
90
108
  private langue?: string;
91
109
  private ip?: IP;
92
110
 
93
- public constructor( private request: Request ) {
111
+ public constructor(
112
+ private request: ServerRequest,
113
+ private tracker: TrackerService,
114
+ ) {
115
+
116
+ this.sql = tracker.sql;
94
117
 
95
118
  }
96
119
 
97
120
  public event( event: 'pageview' ) {
98
121
 
99
122
  // Ne compte pas les events lorsque mode dev ou admin
100
- if (app.env.profile == 'dev' ||
123
+ if (this.app.env.profile == 'dev' ||
101
124
  (this.request.user && this.request.user.roles.includes('ADMIN'))
102
125
  )
103
126
  return;
@@ -146,9 +169,9 @@ export default class TrackerService {
146
169
  private getLangue() {
147
170
 
148
171
  const locales = new locale.Locales( this.request.headers["accept-language"] )
149
- const langue = locales.best( TrackerService.locales ).language.toUpperCase();
172
+ const langue = locales.best( this.tracker.locales ).language.toUpperCase();
150
173
 
151
- return (langue in TrackerService.langues) ? langue : 'EN';
174
+ return (langue in this.tracker.langues) ? langue : 'EN';
152
175
 
153
176
  }
154
177
 
@@ -160,7 +183,7 @@ export default class TrackerService {
160
183
  console.log('Checking IP ...', address);
161
184
  const now: Date = new Date;
162
185
 
163
- let ip = await $.sql`SELECT * FROM logs.IP WHERE address = ${address}`.first();
186
+ let ip = await this.sql`SELECT * FROM logs.IP WHERE address = ${address}`.first();
164
187
  if (!ip) {
165
188
 
166
189
  console.log(`New IP`);
@@ -168,13 +191,13 @@ export default class TrackerService {
168
191
  ip = {
169
192
  address,
170
193
  meet: now,
171
- activity: now,
194
+ activity: now,
172
195
  user_name: this.request.user?.name,
173
196
  }
174
197
 
175
198
  await this.retrieveScore(ip);
176
199
 
177
- $.sql.insert("logs.IP", ip);
200
+ this.sql.insert("logs.IP", ip);
178
201
 
179
202
  // Nouvelle IP
180
203
  } else {
@@ -194,7 +217,7 @@ export default class TrackerService {
194
217
 
195
218
  ip.activity = now;
196
219
 
197
- $.sql.update("logs.IP", ip, { address });
220
+ this.sql.update("logs.IP", ip, { address });
198
221
 
199
222
  }
200
223
 
@@ -209,11 +232,11 @@ export default class TrackerService {
209
232
  const [iphubOk] = await Promise.all([
210
233
 
211
234
  // IPhub = le plus fiable en premier
212
- requete.get('http://v2.api.iphub.info/ip/' + ip.address, {
235
+ got.get('http://v2.api.iphub.info/ip/' + ip.address, {
213
236
  headers: {
214
237
  'X-Key': app.config.http.security.iphub
215
238
  }
216
- }).then((res) => res.data).then((iphub: any) => {
239
+ }).json().then((iphub: any) => {
217
240
 
218
241
  ip.iphub = iphub.block;
219
242
  ip.country = iphub.countryCode;
@@ -234,7 +257,7 @@ export default class TrackerService {
234
257
 
235
258
  // Impossible d'avoir le country = douteux
236
259
  // NOTE: Pour Iphub, ZZ = inconnu
237
- if (!(ip.country in TrackerService.countries)) {
260
+ if (!(ip.country in this.tracker.countries)) {
238
261
  ip.banned = new Date;
239
262
  ip.banReason = "Invalid location: " + ip.country;
240
263
  // Considéré comme suspect par iphub etou getipintel
@@ -22,10 +22,8 @@ import cookieParser from 'cookie-parser';
22
22
  import * as csp from 'express-csp-header';
23
23
 
24
24
  // Core
25
- import app from '@server/app';
26
- // Services
27
- import '../router'; // force loading the service
28
- import { Router } from '../router';
25
+ import Application, { Service } from '@server/app';
26
+ import type Router from '..';
29
27
 
30
28
  // Middlewaees (core)
31
29
  import { MiddlewareFormData } from './multipart';
@@ -34,56 +32,39 @@ import { MiddlewareFormData } from './multipart';
34
32
  - CONFIG
35
33
  ----------------------------------*/
36
34
 
37
- export type HttpServiceConfig = {
35
+ export type Config = {
38
36
 
39
37
  // Access
40
38
  domain: string,
41
39
  port: number,
42
- ssl: number,
40
+ ssl: boolean,
43
41
 
44
42
  // Limitations / Load restriction
45
43
  upload: {
46
44
  maxSize: string // Expression package bytes
47
45
  },
48
-
49
- // Protections against bots
50
- security: {
51
- recaptcha: {
52
- prv: string,
53
- pub: string
54
- },
55
- iphub: string
56
- }
57
46
  }
58
47
 
59
- declare global {
60
- namespace Core {
61
- namespace Config {
62
- interface Services {
63
- http: HttpServiceConfig
64
- }
65
- }
66
- }
48
+ export type Hooks = {
49
+
67
50
  }
68
51
 
69
52
  /*----------------------------------
70
53
  - FUNCTION
71
54
  ----------------------------------*/
72
- export default class HttpServer {
55
+ export default class HttpServer extends Service<Config, Hooks, Application> {
73
56
 
74
57
  public http: http.Server | https.Server;
75
58
  public express: express.Express;
76
59
 
77
- public router: Router;
78
-
79
- public config: HttpServiceConfig;
80
60
  public publicUrl: string;
81
61
 
82
- public constructor() {
62
+ public constructor( config: Config, public router: Router ) {
63
+
64
+ super( router.app, config );
83
65
 
84
66
  // Init
85
- this.config = app.config.http;
86
- this.publicUrl = app.env.name === 'local'
67
+ this.publicUrl = this.app.env.name === 'local'
87
68
  ? 'http://localhost:' + this.config.port
88
69
  : ((this.config.ssl ? 'https' : 'http') + '://' + this.config.domain);
89
70
 
@@ -92,14 +73,17 @@ export default class HttpServer {
92
73
  this.http = http.createServer(this.express);
93
74
 
94
75
  // Start HTTP Server
95
- this.router = app.services.router;
96
- app.on('cleanup', () => this.cleanup());
76
+ this.app.on('cleanup', () => this.cleanup());
97
77
  }
98
78
 
99
79
  /*----------------------------------
100
80
  - HOOKS
101
81
  ----------------------------------*/
102
- public async load() {
82
+ public async register() {
83
+
84
+ }
85
+
86
+ public async start() {
103
87
 
104
88
  const routes = this.express
105
89
 
@@ -123,7 +107,7 @@ export default class HttpServer {
123
107
  routes.use('/public', cors());
124
108
  routes.use(
125
109
  '/public',
126
- expressStaticGzip( app.path.root + '/bin/public', {
110
+ expressStaticGzip( this.app.path.root + '/bin/public', {
127
111
  enableBrotli: true,
128
112
  serveStatic: {
129
113
  setHeaders: function setCustomCacheControl(res, path) {
@@ -206,7 +190,7 @@ export default class HttpServer {
206
190
  // https://www.google.com/search?q=http+cors+from+android%7Cwindows%7Cdesktop%7Cmodile+app
207
191
  //routes.use('/auth', cors());
208
192
 
209
- routes.use(csp.expressCspHeader({
193
+ routes.use( csp.expressCspHeader({
210
194
  directives: {
211
195
  'script-src': [csp.INLINE, csp.SELF,
212
196
  // Whitelist external js scripts
@@ -242,16 +226,4 @@ export default class HttpServer {
242
226
  public async cleanup() {
243
227
  this.http.close();
244
228
  }
245
- }
246
-
247
- /*----------------------------------
248
- - REGISTER SERVICE
249
- ----------------------------------*/
250
- app.register('http', HttpServer);
251
- declare global {
252
- namespace Core {
253
- interface Services {
254
- http: HttpServer;
255
- }
256
- }
257
229
  }