5htp-core 0.1.2 → 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 (127) 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/assets/css/medias.less +14 -0
  9. package/src/client/components/Card/index.tsx +2 -2
  10. package/src/client/components/Dialog/Manager.tsx +39 -12
  11. package/src/client/components/Form/index.tsx +1 -1
  12. package/src/client/components/button.tsx +2 -2
  13. package/src/client/components/containers/Popover/index.tsx +1 -1
  14. package/src/client/components/data/spintext/index.tsx +1 -1
  15. package/src/client/components/dropdown/index.tsx +1 -1
  16. package/src/client/components/index.ts +8 -0
  17. package/src/client/components/input/BaseV2/index.tsx +1 -1
  18. package/src/client/components/input/UploadImage/index.tsx +1 -1
  19. package/src/client/hooks/index.ts +5 -0
  20. package/src/client/hooks/useState/index.tsx +2 -2
  21. package/src/client/hooks.ts +22 -0
  22. package/src/client/index.ts +5 -0
  23. package/src/client/pages/_layout/landing/index.tsx +0 -2
  24. package/src/client/pages/_messages/400.tsx +2 -2
  25. package/src/client/pages/_messages/401.tsx +2 -2
  26. package/src/client/pages/_messages/403.tsx +2 -2
  27. package/src/client/pages/_messages/404.tsx +2 -2
  28. package/src/client/pages/_messages/500.tsx +2 -2
  29. package/src/client/pages/bug.tsx +1 -1
  30. package/src/client/pages/useHeader.tsx +1 -1
  31. package/src/client/{context/captcha.ts → services/captcha/index.ts} +0 -0
  32. package/src/client/services/metrics/index.ts +37 -0
  33. package/src/client/{router → services/router/components}/Link.tsx +1 -1
  34. package/src/client/services/router/components/Page.tsx +59 -0
  35. package/src/client/{router/component.tsx → services/router/components/router.tsx} +43 -74
  36. package/src/client/services/router/index.tsx +448 -0
  37. package/src/client/services/router/request/api.ts +229 -0
  38. package/src/client/{router → services/router}/request/history.ts +0 -0
  39. package/src/client/services/router/request/index.ts +52 -0
  40. package/src/client/services/router/response/index.tsx +107 -0
  41. package/src/client/services/router/response/page.ts +95 -0
  42. package/src/client/{context/socket.ts → services/socket/index.ts} +2 -2
  43. package/src/client/utils/dom.ts +1 -1
  44. package/src/common/app/index.ts +9 -0
  45. package/src/common/data/chaines/index.ts +9 -6
  46. package/src/common/data/input/validate.ts +3 -166
  47. package/src/common/data/objets.ts +25 -0
  48. package/src/common/data/tableaux.ts +8 -0
  49. package/src/common/errors/index.ts +3 -1
  50. package/src/common/router/index.ts +67 -88
  51. package/src/common/router/layouts.ts +50 -0
  52. package/src/common/router/register.ts +62 -0
  53. package/src/common/router/request/api.ts +72 -0
  54. package/src/common/router/request/index.ts +31 -0
  55. package/src/common/router/{response.ts → response/index.ts} +9 -13
  56. package/src/common/router/response/page.ts +40 -56
  57. package/src/common/validation/index.ts +3 -0
  58. package/src/common/validation/schema.ts +184 -0
  59. package/src/common/validation/validator.ts +88 -0
  60. package/src/common/validation/validators.ts +313 -0
  61. package/src/server/app/config.ts +9 -27
  62. package/src/server/app/index.ts +81 -124
  63. package/src/server/app/service.ts +98 -0
  64. package/src/server/app.tsconfig.json +0 -8
  65. package/src/server/error/index.ts +13 -0
  66. package/src/server/index.ts +5 -0
  67. package/src/server/patch.ts +0 -6
  68. package/src/server/{data/Cache.ts → services/cache/index.ts} +79 -47
  69. package/src/server/services/console/bugReporter.ts +26 -16
  70. package/src/server/services/console/index.ts +59 -51
  71. package/src/server/services/cron/index.ts +12 -26
  72. package/src/server/services/database/bucket.ts +40 -0
  73. package/src/server/services/database/connection.ts +206 -75
  74. package/src/server/services/database/datatypes.ts +63 -40
  75. package/src/server/services/database/index.ts +295 -272
  76. package/src/server/services/database/metas.ts +246 -135
  77. package/src/server/services/database/stats.ts +151 -126
  78. package/src/server/services/email/index.ts +28 -52
  79. package/src/server/services/{router/request/services → metrics}/detect.ts +8 -10
  80. package/src/server/services/{router/request/services/tracking.ts → metrics/index.ts} +68 -45
  81. package/src/server/services/{http → router/http}/index.ts +28 -70
  82. package/src/server/services/{http → router/http}/multipart.ts +0 -0
  83. package/src/server/services/{http → router/http}/session.ts.old +0 -0
  84. package/src/server/services/router/index.ts +273 -203
  85. package/src/server/services/router/request/api.ts +73 -0
  86. package/src/server/services/router/request/index.ts +16 -97
  87. package/src/server/services/router/request/service.ts +21 -0
  88. package/src/server/services/router/response/index.ts +125 -64
  89. package/src/server/services/router/response/{filter → mask}/Filter.ts +0 -0
  90. package/src/server/services/router/response/{filter → mask}/index.ts +0 -2
  91. package/src/server/services/router/response/{filter → mask}/selecteurs.ts +0 -0
  92. package/src/server/services/router/response/page/document.tsx +194 -0
  93. package/src/server/services/router/response/page/index.tsx +157 -0
  94. package/src/server/{libs/pages → services/router/response/page}/schemaGenerator.ts +0 -0
  95. package/src/server/services/router/service.ts +48 -0
  96. package/src/server/services/schema/index.ts +47 -0
  97. package/src/server/services/schema/request.ts +55 -0
  98. package/src/server/services/schema/router.ts +33 -0
  99. package/src/server/services/socket/index.ts +38 -43
  100. package/src/server/services/socket/scope.ts +6 -4
  101. package/src/server/services/users/index.ts +203 -0
  102. package/src/server/services/{auth/base.ts → users/old.ts} +28 -112
  103. package/src/server/services/users/router/index.ts +72 -0
  104. package/src/server/services/users/router/request.ts +49 -0
  105. package/src/types/aliases.d.ts +43 -2
  106. package/templates/composant.tsx +1 -1
  107. package/templates/modal.tsx +1 -1
  108. package/templates/page.tsx +1 -1
  109. package/tsconfig.common.json +0 -4
  110. package/src/client/context/api.ts +0 -92
  111. package/src/client/context/index.ts +0 -246
  112. package/src/client/index.tsx +0 -129
  113. package/src/client/router/index.ts +0 -286
  114. package/src/client/router/request/index.ts +0 -106
  115. package/src/client/router/response/index.ts +0 -38
  116. package/src/client/router/route.ts +0 -75
  117. package/src/common/data/input/validators/basic.ts +0 -299
  118. package/src/common/data/input/validators/build.ts +0 -63
  119. package/src/common/router/request.ts +0 -83
  120. package/src/server/data/ApiClient.ts +0 -119
  121. package/src/server/data/input.ts +0 -41
  122. package/src/server/libs/pages/document.static.tsx +0 -41
  123. package/src/server/libs/pages/document.tsx +0 -203
  124. package/src/server/libs/pages/render.tsx +0 -90
  125. package/src/server/routes/auth.ts +0 -151
  126. package/src/server/services/redis/index.ts +0 -71
  127. 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
@@ -11,9 +11,6 @@ import path from 'path';
11
11
  import cors from 'cors';
12
12
  //var serveStatic = require('serve-static')
13
13
 
14
- // Npm: Autres
15
- import fs from 'fs-extra';
16
-
17
14
  // Middlewares (npm)
18
15
  import morgan from 'morgan';
19
16
  import hpp from 'hpp'; // Protection contre la pollution des reuqtees http
@@ -25,10 +22,8 @@ import cookieParser from 'cookie-parser';
25
22
  import * as csp from 'express-csp-header';
26
23
 
27
24
  // Core
28
- import app from '@server/app';
29
- // Services
30
- import '../router'; // force loading the service
31
- import { Router } from '../router';
25
+ import Application, { Service } from '@server/app';
26
+ import type Router from '..';
32
27
 
33
28
  // Middlewaees (core)
34
29
  import { MiddlewareFormData } from './multipart';
@@ -37,83 +32,58 @@ import { MiddlewareFormData } from './multipart';
37
32
  - CONFIG
38
33
  ----------------------------------*/
39
34
 
40
- export type HttpServiceConfig = {
41
- port: number
35
+ export type Config = {
36
+
37
+ // Access
38
+ domain: string,
39
+ port: number,
42
40
  ssl: boolean,
43
41
 
42
+ // Limitations / Load restriction
44
43
  upload: {
45
44
  maxSize: string // Expression package bytes
46
45
  },
47
-
48
- security: {
49
- recaptcha: {
50
- prv: string,
51
- pub: string
52
- },
53
- iphub: string
54
- }
55
46
  }
56
47
 
57
- declare global {
58
- namespace Core {
59
- namespace Config {
60
- interface Services {
61
- http: HttpServiceConfig
62
- }
63
- }
64
- }
48
+ export type Hooks = {
49
+
65
50
  }
66
51
 
67
52
  /*----------------------------------
68
53
  - FUNCTION
69
54
  ----------------------------------*/
70
- export default class HttpServer {
55
+ export default class HttpServer extends Service<Config, Hooks, Application> {
71
56
 
72
57
  public http: http.Server | https.Server;
73
58
  public express: express.Express;
74
59
 
75
- public router: Router;
60
+ public publicUrl: string;
76
61
 
77
- public config: HttpServiceConfig;
78
- public url: string;
62
+ public constructor( config: Config, public router: Router ) {
79
63
 
80
- public constructor() {
64
+ super( router.app, config );
81
65
 
82
66
  // Init
83
- this.config = app.config.http;
84
- this.url = (this.config.ssl ? 'https' : 'http') + '://' + app.env.domain;
85
- this.express = express();
67
+ this.publicUrl = this.app.env.name === 'local'
68
+ ? 'http://localhost:' + this.config.port
69
+ : ((this.config.ssl ? 'https' : 'http') + '://' + this.config.domain);
86
70
 
87
71
  // Configure HTTP server
88
- if (!this.config.ssl) {
89
-
90
- this.http = http.createServer(this.express);
91
-
92
- } /*else if ('ssh' in app.env) {
93
-
94
- const ssh = app.env.ssh;
95
-
96
- console.log("Création du serveur https pour le socket:", '/home/' + ssh.login + '/ssl.*');
97
- this.http = https.createServer({
98
- key: fs.readFileSync('/home/' + ssh.login + '/ssl.key'),
99
- cert: fs.readFileSync('/home/' + ssh.login + '/ssl.cert'),
100
- ca: fs.readFileSync('/home/' + ssh.login + '/ssl.ca'),
101
- requestCert: true,
102
- rejectUnauthorized: false
103
- }, this.express);
104
-
105
- }*/ else
106
- throw new Error(`SSL was enabled, but no ssh config was specified in app.env (required to load ssl certificate files)`);
72
+ this.express = express();
73
+ this.http = http.createServer(this.express);
107
74
 
108
75
  // Start HTTP Server
109
- this.router = app.services.router;
110
- app.on('cleanup', () => this.cleanup());
76
+ this.app.on('cleanup', () => this.cleanup());
111
77
  }
112
78
 
113
79
  /*----------------------------------
114
80
  - HOOKS
115
81
  ----------------------------------*/
116
- public async load() {
82
+ public async register() {
83
+
84
+ }
85
+
86
+ public async start() {
117
87
 
118
88
  const routes = this.express
119
89
 
@@ -137,7 +107,7 @@ export default class HttpServer {
137
107
  routes.use('/public', cors());
138
108
  routes.use(
139
109
  '/public',
140
- expressStaticGzip( app.path.root + '/bin/public', {
110
+ expressStaticGzip( this.app.path.root + '/bin/public', {
141
111
  enableBrotli: true,
142
112
  serveStatic: {
143
113
  setHeaders: function setCustomCacheControl(res, path) {
@@ -220,7 +190,7 @@ export default class HttpServer {
220
190
  // https://www.google.com/search?q=http+cors+from+android%7Cwindows%7Cdesktop%7Cmodile+app
221
191
  //routes.use('/auth', cors());
222
192
 
223
- routes.use(csp.expressCspHeader({
193
+ routes.use( csp.expressCspHeader({
224
194
  directives: {
225
195
  'script-src': [csp.INLINE, csp.SELF,
226
196
  // Whitelist external js scripts
@@ -248,7 +218,7 @@ export default class HttpServer {
248
218
  // Impossible donc de créer un serveur http ici, on le fera dans start.js
249
219
  console.info("Lancement du serveur web");
250
220
  this.http.listen(this.config.port, () => {
251
- console.info(`Serveur web démarré sur https://${app.env.domain}:${this.config.port}/`);
221
+ console.info(`Serveur web démarré sur ${this.publicUrl}`);
252
222
  });
253
223
 
254
224
  }
@@ -256,16 +226,4 @@ export default class HttpServer {
256
226
  public async cleanup() {
257
227
  this.http.close();
258
228
  }
259
- }
260
-
261
- /*----------------------------------
262
- - REGISTER SERVICE
263
- ----------------------------------*/
264
- app.register('http', HttpServer);
265
- declare global {
266
- namespace Core {
267
- interface Services {
268
- http: HttpServer;
269
- }
270
- }
271
229
  }