@wabot-dev/framework 0.2.0-beta.1 → 0.2.0-beta.11

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 (35) hide show
  1. package/dist/src/addon/auth/api-key/@apiKeyHandshakeGuard.js +16 -0
  2. package/dist/src/addon/auth/api-key/ApiKey.js +3 -0
  3. package/dist/src/addon/auth/api-key/ApiKeyGuardMiddleware.js +1 -4
  4. package/dist/src/addon/auth/api-key/{ApiKeyConnectionGuardMiddleware.js → ApiKeyHandshakeGuardMiddleware.js} +5 -8
  5. package/dist/src/addon/auth/api-key/ApiKeyRepository.js +8 -5
  6. package/dist/src/addon/auth/api-key/PgApiKeyRepository.js +10 -2
  7. package/dist/src/addon/auth/api-key/RemoteApiKeyRepository.js +13 -25
  8. package/dist/src/addon/auth/jwt/@jwtHandshakeGuard.js +16 -0
  9. package/dist/src/addon/auth/jwt/Jwt.js +7 -9
  10. package/dist/src/addon/auth/jwt/{JwtConnectionGuardMiddleware.js → JwtHandshakeGuardMiddleware.js} +4 -4
  11. package/dist/src/addon/auth/jwt/JwtRefreshToken.js +40 -35
  12. package/dist/src/addon/auth/jwt/JwtRefreshTokenRepository.js +6 -0
  13. package/dist/src/addon/auth/jwt/JwtSigner.js +1 -1
  14. package/dist/src/addon/auth/jwt/PgJwtRefreshTokenRepository.js +27 -0
  15. package/dist/src/addon/chat-controller/cmd/CmdChannel.js +12 -7
  16. package/dist/src/addon/chat-controller/socket/@socket.js +1 -1
  17. package/dist/src/addon/chat-controller/socket/SocketChannel.js +74 -33
  18. package/dist/src/addon/chat-controller/socket/SocketChannelConfig.js +6 -4
  19. package/dist/src/core/auth/Auth.js +6 -0
  20. package/dist/src/core/mapper/Mapper.js +22 -6
  21. package/dist/src/core/validation/core/validateModel.js +37 -4
  22. package/dist/src/core/validation/{validate.js → validateAndTransform.js} +2 -2
  23. package/dist/src/feature/chat-controller/runChatControllers.js +6 -7
  24. package/dist/src/feature/rest-controller/runRestControllers.js +2 -2
  25. package/dist/src/feature/socket-controller/metadata/@handshakeMiddlewares.js +16 -0
  26. package/dist/src/feature/socket-controller/metadata/{@socketEvent.js → @onSocketEvent.js} +2 -2
  27. package/dist/src/feature/socket-controller/metadata/SocketControllerMetadataStore.js +12 -34
  28. package/dist/src/feature/socket-controller/runSocketControllers.js +97 -78
  29. package/dist/src/index.d.ts +112 -110
  30. package/dist/src/index.js +8 -9
  31. package/package.json +4 -5
  32. package/dist/src/addon/auth/api-key/@apiKeyConnectionGuard.js +0 -16
  33. package/dist/src/addon/auth/jwt/@jwtConnectionGuard.js +0 -16
  34. package/dist/src/feature/socket-controller/metadata/@connectionMiddleware.js +0 -16
  35. package/dist/src/feature/socket-controller/metadata/@socketConnection.js +0 -18
@@ -0,0 +1,16 @@
1
+ import { handshakeMiddlewares } from '../../../feature/socket-controller/metadata/@handshakeMiddlewares.js';
2
+ import '../../../feature/socket-controller/metadata/SocketControllerMetadataStore.js';
3
+ import '../../../core/injection/index.js';
4
+ import 'debug';
5
+ import '../../../core/validation/metadata/ValidationMetadataStore.js';
6
+ import 'socket.io';
7
+ import '../../../feature/socket/SocketServerProvider.js';
8
+ import { ApiKeyHandshakeGuardMiddleware } from './ApiKeyHandshakeGuardMiddleware.js';
9
+
10
+ function apiKeyHandshakeGuard() {
11
+ return function (target) {
12
+ handshakeMiddlewares([ApiKeyHandshakeGuardMiddleware])(target);
13
+ };
14
+ }
15
+
16
+ export { apiKeyHandshakeGuard };
@@ -13,6 +13,9 @@ class ApiKey extends Entity {
13
13
  get metadata() {
14
14
  return this.data.metadata ?? {};
15
15
  }
16
+ get name() {
17
+ return this.data.name;
18
+ }
16
19
  setAuthInfo(authInfo) {
17
20
  this.data.authInfo = authInfo;
18
21
  }
@@ -21,10 +21,7 @@ let ApiKeyGuardMiddleware = class ApiKeyGuardMiddleware {
21
21
  throw new CustomError({ httpCode: 401, message: 'Authorization should be an Api-Key' });
22
22
  }
23
23
  try {
24
- const authInfo = await this.apiKeyRepository.findAuthInfoBySecret(keySecret);
25
- if (!authInfo) {
26
- throw new CustomError({ httpCode: 401, message: 'Invalid key' });
27
- }
24
+ const authInfo = await this.apiKeyRepository.findAndValidate(keySecret);
28
25
  this.auth.assign(authInfo);
29
26
  }
30
27
  catch (err) {
@@ -4,7 +4,7 @@ import { CustomError } from '../../../core/error/CustomError.js';
4
4
  import { injectable } from '../../../core/injection/index.js';
5
5
  import { ApiKeyRepository } from './ApiKeyRepository.js';
6
6
 
7
- let ApiKeyConnectionGuardMiddleware = class ApiKeyConnectionGuardMiddleware {
7
+ let ApiKeyHandshakeGuardMiddleware = class ApiKeyHandshakeGuardMiddleware {
8
8
  apiKeyRepository;
9
9
  auth;
10
10
  constructor(apiKeyRepository, auth) {
@@ -33,10 +33,7 @@ let ApiKeyConnectionGuardMiddleware = class ApiKeyConnectionGuardMiddleware {
33
33
  throw new CustomError({ httpCode: 401, message: 'Token not available' });
34
34
  }
35
35
  try {
36
- const authInfo = await this.apiKeyRepository.findAuthInfoBySecret(keySecret);
37
- if (!authInfo) {
38
- throw new CustomError({ httpCode: 401, message: 'Invalid token' });
39
- }
36
+ const authInfo = await this.apiKeyRepository.findAndValidate(keySecret);
40
37
  this.auth.assign(authInfo);
41
38
  }
42
39
  catch (err) {
@@ -48,10 +45,10 @@ let ApiKeyConnectionGuardMiddleware = class ApiKeyConnectionGuardMiddleware {
48
45
  }
49
46
  }
50
47
  };
51
- ApiKeyConnectionGuardMiddleware = __decorate([
48
+ ApiKeyHandshakeGuardMiddleware = __decorate([
52
49
  injectable(),
53
50
  __metadata("design:paramtypes", [ApiKeyRepository,
54
51
  Auth])
55
- ], ApiKeyConnectionGuardMiddleware);
52
+ ], ApiKeyHandshakeGuardMiddleware);
56
53
 
57
- export { ApiKeyConnectionGuardMiddleware };
54
+ export { ApiKeyHandshakeGuardMiddleware };
@@ -1,20 +1,23 @@
1
1
  class ApiKeyRepository {
2
- findAuthInfoBySecret(secret) {
2
+ find(id) {
3
+ throw new Error('Method not implemented.');
4
+ }
5
+ findOrThrow(id) {
3
6
  throw new Error('Method not implemented.');
4
7
  }
5
8
  findByMetadata(metadata) {
6
9
  throw new Error('Method not implemented.');
7
10
  }
8
- find(id) {
11
+ create(item) {
9
12
  throw new Error('Method not implemented.');
10
13
  }
11
- findOrThrow(id) {
14
+ generate(req) {
12
15
  throw new Error('Method not implemented.');
13
16
  }
14
- create(item) {
17
+ findBySecret(secret) {
15
18
  throw new Error('Method not implemented.');
16
19
  }
17
- generate(req) {
20
+ findAndValidate(secret) {
18
21
  throw new Error('Method not implemented.');
19
22
  }
20
23
  }
@@ -3,6 +3,7 @@ import { PgCrudRepository } from '../../../feature/pg/PgCrudRepository.js';
3
3
  import { Pool } from 'pg';
4
4
  import { singleton } from 'tsyringe';
5
5
  import { ApiKey } from './ApiKey.js';
6
+ import { CustomError } from '../../../core/error/CustomError.js';
6
7
 
7
8
  let PgApiKeyRepository = class PgApiKeyRepository extends PgCrudRepository {
8
9
  constructor(pool) {
@@ -12,7 +13,14 @@ let PgApiKeyRepository = class PgApiKeyRepository extends PgCrudRepository {
12
13
  constructor: ApiKey,
13
14
  });
14
15
  }
15
- async findAuthInfoBySecret(secret) {
16
+ async findAndValidate(secret) {
17
+ const apiKey = await this.findBySecret(secret);
18
+ if (!apiKey) {
19
+ throw new CustomError({ message: 'Invalid Api Key', httpCode: 401 });
20
+ }
21
+ return apiKey.authInfo;
22
+ }
23
+ async findBySecret(secret) {
16
24
  const secretHash = ApiKey.hashSecret(secret);
17
25
  const query = `
18
26
  SELECT ${this.columns}
@@ -21,7 +29,7 @@ let PgApiKeyRepository = class PgApiKeyRepository extends PgCrudRepository {
21
29
  LIMIT 1
22
30
  `;
23
31
  const items = await this.query(query, [JSON.stringify({ secretHash })]);
24
- return items[0]?.authInfo ?? null;
32
+ return items[0] ?? null;
25
33
  }
26
34
  async findByMetadata(metadata) {
27
35
  const query = `
@@ -3,42 +3,21 @@ import { CustomError } from '../../../core/error/CustomError.js';
3
3
  class RemoteApiKeyRepository {
4
4
  fetcher;
5
5
  cacheSeconds;
6
- cacheById = new Map();
7
6
  cacheBySecret = new Map();
8
7
  constructor(fetcher, cacheSeconds) {
9
8
  this.fetcher = fetcher;
10
9
  this.cacheSeconds = cacheSeconds;
11
10
  }
12
- async find(id) {
13
- const now = Date.now();
14
- const cached = this.cacheById.get(id);
15
- if (cached && cached.expiresAt > now) {
16
- return cached.value;
17
- }
18
- const result = await this.fetcher.fetchById(id);
19
- this.cacheById.set(id, {
20
- value: result,
21
- expiresAt: now + this.cacheSeconds * 1000,
22
- });
23
- return result;
24
- }
25
- async findOrThrow(id) {
26
- const result = await this.find(id);
27
- if (!result) {
28
- throw new Error(`API key with ID '${id}' not found.`);
29
- }
30
- return result;
31
- }
32
- async findAuthInfoBySecret(secret) {
11
+ async findAndValidate(secret) {
33
12
  const now = Date.now();
34
13
  const cached = this.cacheBySecret.get(secret);
35
14
  if (cached && cached.expiresAt > now) {
36
15
  if (!cached.value) {
37
16
  throw new CustomError({ message: 'Invalid Api Key', httpCode: 401 });
38
17
  }
39
- return cached.value.authInfo;
18
+ return cached.value;
40
19
  }
41
- const result = await this.fetcher.fetchBySecret(secret);
20
+ const result = await this.fetcher.fetchAuthInfoBySecret(secret);
42
21
  this.cacheBySecret.set(secret, {
43
22
  value: result,
44
23
  expiresAt: now + this.cacheSeconds * 1000,
@@ -46,7 +25,16 @@ class RemoteApiKeyRepository {
46
25
  if (!result) {
47
26
  throw new CustomError({ message: 'Invalid Api Key', httpCode: 401 });
48
27
  }
49
- return result.authInfo;
28
+ return result;
29
+ }
30
+ find(id) {
31
+ throw new Error('Method not implemented.');
32
+ }
33
+ findOrThrow(id) {
34
+ throw new Error('Method not implemented.');
35
+ }
36
+ findBySecret(secret) {
37
+ throw new Error('Method not implemented.');
50
38
  }
51
39
  findByMetadata(metadata) {
52
40
  throw new Error('Method not implemented.');
@@ -0,0 +1,16 @@
1
+ import { handshakeMiddlewares } from '../../../feature/socket-controller/metadata/@handshakeMiddlewares.js';
2
+ import '../../../feature/socket-controller/metadata/SocketControllerMetadataStore.js';
3
+ import '../../../core/injection/index.js';
4
+ import 'debug';
5
+ import '../../../core/validation/metadata/ValidationMetadataStore.js';
6
+ import 'socket.io';
7
+ import '../../../feature/socket/SocketServerProvider.js';
8
+ import { JwtHandshakeGuardMiddleware } from './JwtHandshakeGuardMiddleware.js';
9
+
10
+ function jwtHandshakeGuard() {
11
+ return function (target) {
12
+ handshakeMiddlewares([JwtHandshakeGuardMiddleware])(target);
13
+ };
14
+ }
15
+
16
+ export { jwtHandshakeGuard };
@@ -17,17 +17,18 @@ let Jwt = class Jwt {
17
17
  this.jwtRefreshTokenRepository = jwtRefreshTokenRepository;
18
18
  this.config = config;
19
19
  }
20
- async createToken() {
20
+ async createToken(metadata) {
21
21
  const authInfo = this.auth.require();
22
22
  const refreshToken = new JwtRefreshToken({
23
+ metadata,
23
24
  authInfo,
24
- expirationTime: new Date().getTime() + this.config.refreshExpirationSeconds * 1000,
25
+ expirationTime: Date.now() + this.config.refreshExpirationSeconds * 1000,
25
26
  });
26
- const refreshPassword = refreshToken.generatePassword();
27
+ const refreshSecret = refreshToken.generateSecret();
27
28
  await this.jwtRefreshTokenRepository.create(refreshToken);
28
29
  const access = await this.jwtSigner.signAccessToken(refreshToken);
29
30
  const refresh = {
30
- token: JwtRefreshToken.deflate({ id: refreshToken.id, pass: refreshPassword }),
31
+ token: refreshSecret,
31
32
  expiration: new Date(refreshToken.expirationTime),
32
33
  };
33
34
  return {
@@ -35,11 +36,8 @@ let Jwt = class Jwt {
35
36
  refresh,
36
37
  };
37
38
  }
38
- async refreshToken(refreshSecret) {
39
- const { id, pass } = JwtRefreshToken.inflate(refreshSecret);
40
- const refreshToken = await this.jwtRefreshTokenRepository.findOrThrow(id);
41
- refreshToken.validatePassword(pass);
42
- return this.jwtSigner.signAccessToken(refreshToken);
39
+ async findRefreshTokenAuthInfo(secret) {
40
+ return await this.jwtRefreshTokenRepository.findAndValidate(secret);
43
41
  }
44
42
  };
45
43
  Jwt = __decorate([
@@ -5,7 +5,7 @@ import { Auth } from '../../../core/auth/Auth.js';
5
5
  import { CustomError } from '../../../core/error/CustomError.js';
6
6
  import { JwtConfig } from './JwtConfig.js';
7
7
 
8
- let JwtConnectionGuardMiddleware = class JwtConnectionGuardMiddleware {
8
+ let JwtHandshakeGuardMiddleware = class JwtHandshakeGuardMiddleware {
9
9
  config;
10
10
  auth;
11
11
  constructor(config, auth) {
@@ -48,10 +48,10 @@ let JwtConnectionGuardMiddleware = class JwtConnectionGuardMiddleware {
48
48
  }
49
49
  }
50
50
  };
51
- JwtConnectionGuardMiddleware = __decorate([
51
+ JwtHandshakeGuardMiddleware = __decorate([
52
52
  injectable(),
53
53
  __metadata("design:paramtypes", [JwtConfig,
54
54
  Auth])
55
- ], JwtConnectionGuardMiddleware);
55
+ ], JwtHandshakeGuardMiddleware);
56
56
 
57
- export { JwtConnectionGuardMiddleware };
57
+ export { JwtHandshakeGuardMiddleware };
@@ -1,55 +1,60 @@
1
1
  import { Entity } from '../../../core/entity/Entity.js';
2
2
  import { CustomError } from '../../../core/error/CustomError.js';
3
- import { Password } from '../../../core/password/Password.js';
3
+ import crypto from 'node:crypto';
4
4
 
5
5
  class JwtRefreshToken extends Entity {
6
+ static PREFIX = 'rt_';
7
+ static hashSecret(secret) {
8
+ return crypto.createHash('sha256').update(secret).digest('hex');
9
+ }
6
10
  get authInfo() {
7
11
  return this.data.authInfo;
8
12
  }
13
+ get metadata() {
14
+ return this.data.metadata ?? {};
15
+ }
9
16
  get expirationTime() {
10
17
  return new Date(this.data.expirationTime);
11
18
  }
12
- generatePassword() {
13
- if (this.data.passwordHash) {
14
- throw new Error('This api key, already has a secret');
19
+ isExpired() {
20
+ return Date.now() > this.data.expirationTime;
21
+ }
22
+ revoke() {
23
+ this.data.revokedAt = Date.now();
24
+ }
25
+ isRevoked() {
26
+ return this.data.revokedAt != null;
27
+ }
28
+ generateSecret() {
29
+ if (this.data.secretHash) {
30
+ throw new Error('This Token key already has a secret');
15
31
  }
16
- const password = Password.generate(64);
17
- this.data.passwordHash = Password.hash({ password: password });
18
- return password;
32
+ const secret = `${JwtRefreshToken.PREFIX}${crypto.randomBytes(32).toString('hex')}`;
33
+ this.data.secretHash = JwtRefreshToken.hashSecret(secret);
34
+ return secret;
19
35
  }
20
- isValidPassword(password) {
21
- if (new Date().getTime() > this.data.expirationTime) {
36
+ isValidSecret(secret) {
37
+ if (!secret.startsWith(JwtRefreshToken.PREFIX))
22
38
  return false;
23
- }
24
- if (!this.data.passwordHash)
39
+ if (!this.data.secretHash)
25
40
  return false;
26
- return Password.isValid({ password: password, hash: this.data.passwordHash });
41
+ const hashed = JwtRefreshToken.hashSecret(secret);
42
+ const stored = this.data.secretHash;
43
+ const hashedBuf = Buffer.from(hashed, 'hex');
44
+ const storedBuf = Buffer.from(stored, 'hex');
45
+ return hashedBuf.length === storedBuf.length && crypto.timingSafeEqual(hashedBuf, storedBuf);
27
46
  }
28
- validatePassword(password) {
29
- if (!this.isValidPassword(password)) {
30
- throw new CustomError({ message: 'Invalid Api key', httpCode: 401 });
31
- }
32
- }
33
- static inflate(secret) {
34
- try {
35
- const json = Buffer.from(secret, 'base64').toString('utf-8');
36
- const data = JSON.parse(json);
37
- if (!data.id || !data.pass) {
38
- throw new Error('invalid secret structure');
39
- }
40
- return data;
41
- }
42
- catch (err) {
43
- throw new Error('fail to inflate secret: ' + err.message);
44
- }
47
+ isValidToken(secret) {
48
+ if (this.isExpired())
49
+ return false;
50
+ if (this.isRevoked())
51
+ return false;
52
+ return this.isValidSecret(secret);
45
53
  }
46
- static deflate(data) {
47
- const { id, pass } = data;
48
- if (!id || !pass) {
49
- throw new Error('id and pass required');
54
+ validateToken(secret) {
55
+ if (!this.isValidToken(secret)) {
56
+ throw new CustomError({ message: 'Invalid Token', httpCode: 401 });
50
57
  }
51
- const json = JSON.stringify({ id, pass });
52
- return Buffer.from(json, 'utf-8').toString('base64');
53
58
  }
54
59
  }
55
60
 
@@ -1,4 +1,10 @@
1
1
  class JwtRefreshTokenRepository {
2
+ findByMetadata(metadata) {
3
+ throw new Error('Method not implemented.');
4
+ }
5
+ findAndValidate(secret) {
6
+ throw new Error('Method not implemented.');
7
+ }
2
8
  find(id) {
3
9
  throw new Error('Method not implemented.');
4
10
  }
@@ -23,7 +23,7 @@ let JwtSigner = class JwtSigner {
23
23
  const token = jwt.sign(_authInfo, this.config.secretOrPrivateKey, {
24
24
  expiresIn: this.config.accessExpirationSeconds,
25
25
  });
26
- const expiration = new Date().getTime() + this.config.accessExpirationSeconds * 1000;
26
+ const expiration = new Date(new Date().getTime() + this.config.accessExpirationSeconds * 1000);
27
27
  return this.mapper.map({ token, expiration }, JwtTokenDto);
28
28
  }
29
29
  };
@@ -3,6 +3,7 @@ import { singleton } from '../../../core/injection/index.js';
3
3
  import { Pool } from 'pg';
4
4
  import { PgCrudRepository } from '../../../feature/pg/PgCrudRepository.js';
5
5
  import { JwtRefreshToken } from './JwtRefreshToken.js';
6
+ import { CustomError } from '../../../core/error/CustomError.js';
6
7
 
7
8
  let PgJwtRefreshTokenRepository = class PgJwtRefreshTokenRepository extends PgCrudRepository {
8
9
  constructor(pool) {
@@ -12,6 +13,32 @@ let PgJwtRefreshTokenRepository = class PgJwtRefreshTokenRepository extends PgCr
12
13
  constructor: JwtRefreshToken,
13
14
  });
14
15
  }
16
+ async findAndValidate(secret) {
17
+ const apiKey = await this.findBySecret(secret);
18
+ if (!apiKey) {
19
+ throw new CustomError({ message: 'Invalid Token', httpCode: 401 });
20
+ }
21
+ return apiKey.authInfo;
22
+ }
23
+ async findBySecret(secret) {
24
+ const secretHash = JwtRefreshToken.hashSecret(secret);
25
+ const query = `
26
+ SELECT ${this.columns}
27
+ FROM ${this.table}
28
+ WHERE data @> $1::jsonb
29
+ LIMIT 1
30
+ `;
31
+ const items = await this.query(query, [JSON.stringify({ secretHash })]);
32
+ return items[0] ?? null;
33
+ }
34
+ async findByMetadata(metadata) {
35
+ const query = `
36
+ SELECT ${this.columns}
37
+ FROM ${this.table}
38
+ WHERE data @> $1::jsonb
39
+ `;
40
+ return await this.query(query, [JSON.stringify({ metadata })]);
41
+ }
15
42
  };
16
43
  PgJwtRefreshTokenRepository = __decorate([
17
44
  singleton(),
@@ -1,14 +1,16 @@
1
- import { __decorate } from 'tslib';
1
+ import { __decorate, __metadata } from 'tslib';
2
2
  import { injectable } from '../../../core/injection/index.js';
3
3
  import * as readline from 'readline';
4
4
  import * as fs from 'fs';
5
5
  import * as path from 'path';
6
6
  import { Random } from '../../../core/random/Random.js';
7
+ import { Auth } from '../../../core/auth/Auth.js';
7
8
 
8
9
  var CmdChannel_1;
9
10
  const chatIdPath = '.cmd-channel/id.json';
10
11
  const authInfoPath = '.cmd-channel/auth-info.json';
11
12
  let CmdChannel = CmdChannel_1 = class CmdChannel {
13
+ auth;
12
14
  authInfo = undefined;
13
15
  chatId = undefined;
14
16
  rl = readline.createInterface({
@@ -16,6 +18,9 @@ let CmdChannel = CmdChannel_1 = class CmdChannel {
16
18
  output: process.stdout,
17
19
  });
18
20
  callBack = null;
21
+ constructor(auth) {
22
+ this.auth = auth;
23
+ }
19
24
  listen(callback) {
20
25
  this.callBack = callback;
21
26
  }
@@ -55,18 +60,18 @@ let CmdChannel = CmdChannel_1 = class CmdChannel {
55
60
  reply: (message) => {
56
61
  console.log(`\n[${message.senderName}]: ${message.text}\n`);
57
62
  this.rl.prompt();
63
+ if (this.auth.isAssigned()) {
64
+ writeJsonToFile(authInfoPath, this.auth.require());
65
+ }
58
66
  },
59
- authInfo: this.authInfo || undefined,
60
- setAuthInfo: (authInfo) => {
61
- this.authInfo = authInfo || null;
62
- writeJsonToFile(authInfoPath, this.authInfo);
63
- },
67
+ injectInstances: [[Auth, this.auth]],
64
68
  });
65
69
  });
66
70
  }
67
71
  };
68
72
  CmdChannel = CmdChannel_1 = __decorate([
69
- injectable()
73
+ injectable(),
74
+ __metadata("design:paramtypes", [Auth])
70
75
  ], CmdChannel);
71
76
  function writeJsonToFile(filename, data) {
72
77
  const filePath = path.resolve(process.cwd(), filename);
@@ -18,7 +18,7 @@ function socket(config) {
18
18
  channelConstructor: SocketChannel,
19
19
  functionName: propertyKey.toString(),
20
20
  controllerConstructor: target.constructor,
21
- channelConfig: new SocketChannelConfig(config.channel),
21
+ channelConfig: new SocketChannelConfig(config.namespace, config.handshakeMidlewares),
22
22
  });
23
23
  };
24
24
  }
@@ -1,65 +1,106 @@
1
1
  import { __decorate, __metadata } from 'tslib';
2
2
  import { injectable } from '../../../core/injection/index.js';
3
- import { SocketServerProvider } from '../../../feature/socket/SocketServerProvider.js';
3
+ import { handshakeMiddlewares } from '../../../feature/socket-controller/metadata/@handshakeMiddlewares.js';
4
+ import { socketController } from '../../../feature/socket-controller/metadata/@socketController.js';
5
+ import { onSocketEvent } from '../../../feature/socket-controller/metadata/@onSocketEvent.js';
6
+ import '../../../feature/socket-controller/metadata/SocketControllerMetadataStore.js';
7
+ import { runSocketControllers } from '../../../feature/socket-controller/runSocketControllers.js';
8
+ import { Socket } from 'socket.io';
4
9
  import { SocketChannelConfig } from './SocketChannelConfig.js';
10
+ import '../../../core/validation/metadata/ValidationMetadataStore.js';
11
+ import { isNotEmpty } from '../../../core/validation/validators/is-not-empty/@isNotEmpty.js';
12
+ import { isString } from '../../../core/validation/validators/is-string/@isString.js';
13
+ import { Auth } from '../../../core/auth/Auth.js';
5
14
 
6
15
  var SocketChannel_1;
16
+ class SocketChannelReceivedMessage {
17
+ chatId;
18
+ senderName;
19
+ text;
20
+ }
21
+ __decorate([
22
+ isString(),
23
+ isNotEmpty(),
24
+ __metadata("design:type", String)
25
+ ], SocketChannelReceivedMessage.prototype, "chatId", void 0);
26
+ __decorate([
27
+ isString(),
28
+ isNotEmpty(),
29
+ __metadata("design:type", String)
30
+ ], SocketChannelReceivedMessage.prototype, "senderName", void 0);
31
+ __decorate([
32
+ isString(),
33
+ isNotEmpty(),
34
+ __metadata("design:type", String)
35
+ ], SocketChannelReceivedMessage.prototype, "text", void 0);
7
36
  let SocketChannel = SocketChannel_1 = class SocketChannel {
8
37
  config;
9
- socketServerProvider;
10
38
  callBack = null;
11
- server;
12
- constructor(config, socketServerProvider) {
39
+ controller = null;
40
+ constructor(config) {
13
41
  this.config = config;
14
- this.socketServerProvider = socketServerProvider;
15
- this.server = this.socketServerProvider.getSocketServer();
42
+ this.configController();
16
43
  }
17
- listen(callback) {
18
- this.callBack = callback;
19
- }
20
- connect() {
21
- this.server.on('connection', (socket) => {
22
- socket.on(this.config.channel, async (message) => {
44
+ configController() {
45
+ const channel = this;
46
+ let SocketChannelController = class SocketChannelController {
47
+ auth;
48
+ constructor(auth) {
49
+ this.auth = auth;
50
+ }
51
+ onMessage(message, socket) {
52
+ if (!channel.callBack)
53
+ return;
23
54
  const trimmedInput = message.text.trim();
24
55
  if (!trimmedInput) {
25
56
  return;
26
57
  }
27
- if (!message.chatId || !message.userId || !message.senderName) {
28
- socket.emit(this.config.channel, {
29
- error: 'Invalid message format. chatId, userId, and senderName are required.',
30
- });
31
- return;
32
- }
33
58
  const chatConnection = {
34
59
  id: message.chatId,
35
60
  chatType: 'PRIVATE',
36
61
  channelName: SocketChannel_1.name,
37
62
  };
38
- if (!this.callBack)
39
- return;
40
- this.callBack({
63
+ channel.callBack({
41
64
  chatConnection,
42
65
  message: {
43
- text: trimmedInput,
66
+ text: message.text,
44
67
  senderName: message.senderName,
45
68
  },
46
69
  reply: (message) => {
47
- socket.emit(this.config.channel, message);
48
- },
49
- authInfo: socket.data.authInfo,
50
- setAuthInfo: (authInfo) => {
51
- socket.data.authInfo = authInfo;
70
+ socket.emit('message', message);
52
71
  },
72
+ injectInstances: [
73
+ [Socket, socket],
74
+ [Auth, this.auth],
75
+ ],
53
76
  });
54
- });
55
- });
56
- this.socketServerProvider.listen();
77
+ }
78
+ };
79
+ __decorate([
80
+ onSocketEvent('message'),
81
+ __metadata("design:type", Function),
82
+ __metadata("design:paramtypes", [SocketChannelReceivedMessage, Socket]),
83
+ __metadata("design:returntype", void 0)
84
+ ], SocketChannelController.prototype, "onMessage", null);
85
+ SocketChannelController = __decorate([
86
+ socketController(channel.config.namespace),
87
+ handshakeMiddlewares(channel.config.handshakeMidlewares ?? []),
88
+ __metadata("design:paramtypes", [Auth])
89
+ ], SocketChannelController);
90
+ this.controller = SocketChannelController;
91
+ }
92
+ listen(callback) {
93
+ this.callBack = callback;
94
+ }
95
+ connect() {
96
+ if (!this.controller)
97
+ return;
98
+ runSocketControllers([this.controller]);
57
99
  }
58
100
  };
59
101
  SocketChannel = SocketChannel_1 = __decorate([
60
102
  injectable(),
61
- __metadata("design:paramtypes", [SocketChannelConfig,
62
- SocketServerProvider])
103
+ __metadata("design:paramtypes", [SocketChannelConfig])
63
104
  ], SocketChannel);
64
105
 
65
- export { SocketChannel };
106
+ export { SocketChannel, SocketChannelReceivedMessage };
@@ -2,14 +2,16 @@ import { __decorate, __metadata } from 'tslib';
2
2
  import { injectable } from '../../../core/injection/index.js';
3
3
 
4
4
  let SocketChannelConfig = class SocketChannelConfig {
5
- channel;
6
- constructor(channel) {
7
- this.channel = channel;
5
+ namespace;
6
+ handshakeMidlewares;
7
+ constructor(namespace, handshakeMidlewares) {
8
+ this.namespace = namespace;
9
+ this.handshakeMidlewares = handshakeMidlewares;
8
10
  }
9
11
  };
10
12
  SocketChannelConfig = __decorate([
11
13
  injectable(),
12
- __metadata("design:paramtypes", [String])
14
+ __metadata("design:paramtypes", [String, Array])
13
15
  ], SocketChannelConfig);
14
16
 
15
17
  export { SocketChannelConfig };