@ikonintegration/ikapi 4.0.1 → 5.0.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 (236) hide show
  1. package/.eslintignore +3 -0
  2. package/.eslintrc.cjs +81 -0
  3. package/.github/workflows/npmpublish.yml +8 -19
  4. package/.github/workflows/prs.yml +12 -0
  5. package/README.md +89 -99
  6. package/dist/index.d.ts +16 -0
  7. package/dist/index.js +27 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/package-lock.json +11881 -0
  10. package/dist/package.json +81 -0
  11. package/dist/src/API/Request.d.ts +125 -0
  12. package/dist/src/API/Request.js +185 -0
  13. package/dist/src/API/Request.js.map +1 -0
  14. package/dist/src/API/Response.d.ts +188 -0
  15. package/dist/src/API/Response.js +270 -0
  16. package/dist/src/API/Response.js.map +1 -0
  17. package/dist/src/BaseEvent/DynamoTransaction.d.ts +70 -0
  18. package/dist/src/BaseEvent/DynamoTransaction.js +104 -0
  19. package/dist/src/BaseEvent/DynamoTransaction.js.map +1 -0
  20. package/dist/src/BaseEvent/EventProcessor.d.ts +58 -0
  21. package/dist/src/BaseEvent/EventProcessor.js +101 -0
  22. package/dist/src/BaseEvent/EventProcessor.js.map +1 -0
  23. package/dist/src/BaseEvent/Process.d.ts +50 -0
  24. package/dist/src/BaseEvent/Process.js +64 -0
  25. package/dist/src/BaseEvent/Process.js.map +1 -0
  26. package/dist/src/BaseEvent/StepTransaction.d.ts +23 -0
  27. package/dist/src/BaseEvent/StepTransaction.js +27 -0
  28. package/dist/src/BaseEvent/StepTransaction.js.map +1 -0
  29. package/dist/src/BaseEvent/Transaction.d.ts +149 -0
  30. package/dist/src/BaseEvent/Transaction.js +224 -0
  31. package/dist/src/BaseEvent/Transaction.js.map +1 -0
  32. package/dist/src/Cache/Redis.d.ts +29 -0
  33. package/dist/src/Cache/Redis.js +80 -0
  34. package/dist/src/Cache/Redis.js.map +1 -0
  35. package/dist/src/Cache/types.d.ts +31 -0
  36. package/dist/src/Cache/types.js +2 -0
  37. package/dist/src/Cache/types.js.map +1 -0
  38. package/dist/src/Config/Configuration.d.ts +123 -0
  39. package/dist/src/Config/Configuration.js +109 -0
  40. package/dist/src/Config/Configuration.js.map +1 -0
  41. package/dist/src/Config/EnvironmentVar.d.ts +74 -0
  42. package/dist/src/Config/EnvironmentVar.js +138 -0
  43. package/dist/src/Config/EnvironmentVar.js.map +1 -0
  44. package/dist/src/Crypto/Crypto.d.ts +45 -0
  45. package/dist/src/Crypto/Crypto.js +72 -0
  46. package/dist/src/Crypto/Crypto.js.map +1 -0
  47. package/dist/src/Database/Database.d.ts +21 -0
  48. package/dist/src/Database/Database.js +15 -0
  49. package/dist/src/Database/Database.js.map +1 -0
  50. package/dist/src/Database/DatabaseManager.d.ts +47 -0
  51. package/dist/src/Database/DatabaseManager.js +60 -0
  52. package/dist/src/Database/DatabaseManager.js.map +1 -0
  53. package/dist/src/Database/DatabaseTransaction.d.ts +101 -0
  54. package/dist/src/Database/DatabaseTransaction.js +126 -0
  55. package/dist/src/Database/DatabaseTransaction.js.map +1 -0
  56. package/dist/src/Database/index.d.ts +10 -0
  57. package/dist/src/Database/index.js +15 -0
  58. package/dist/src/Database/index.js.map +1 -0
  59. package/dist/src/Database/integrations/dynamo/DynamoDatabase.d.ts +35 -0
  60. package/dist/src/Database/integrations/dynamo/DynamoDatabase.js +59 -0
  61. package/dist/src/Database/integrations/dynamo/DynamoDatabase.js.map +1 -0
  62. package/dist/src/Database/integrations/kysely/KyselyDatabase.d.ts +66 -0
  63. package/dist/src/Database/integrations/kysely/KyselyDatabase.js +86 -0
  64. package/dist/src/Database/integrations/kysely/KyselyDatabase.js.map +1 -0
  65. package/dist/src/Database/integrations/kysely/KyselyTransaction.d.ts +70 -0
  66. package/dist/src/Database/integrations/kysely/KyselyTransaction.js +118 -0
  67. package/dist/src/Database/integrations/kysely/KyselyTransaction.js.map +1 -0
  68. package/dist/src/Database/integrations/pgsql/PostgresDatabase.d.ts +36 -0
  69. package/dist/src/Database/integrations/pgsql/PostgresDatabase.js +54 -0
  70. package/dist/src/Database/integrations/pgsql/PostgresDatabase.js.map +1 -0
  71. package/dist/src/Database/integrations/pgsql/PostgresTransaction.d.ts +63 -0
  72. package/dist/src/Database/integrations/pgsql/PostgresTransaction.js +61 -0
  73. package/dist/src/Database/integrations/pgsql/PostgresTransaction.js.map +1 -0
  74. package/dist/src/Database/types.d.ts +76 -0
  75. package/dist/src/Database/types.js +2 -0
  76. package/dist/src/Database/types.js.map +1 -0
  77. package/dist/src/Globals.d.ts +93 -0
  78. package/dist/src/Globals.js +99 -0
  79. package/dist/src/Globals.js.map +1 -0
  80. package/dist/src/Logger/Logger.d.ts +161 -0
  81. package/dist/src/Logger/Logger.js +299 -0
  82. package/dist/src/Logger/Logger.js.map +1 -0
  83. package/dist/src/Mailer/Mailer.d.ts +78 -0
  84. package/dist/src/Mailer/Mailer.js +182 -0
  85. package/dist/src/Mailer/Mailer.js.map +1 -0
  86. package/dist/src/Publisher/Publisher.d.ts +39 -0
  87. package/dist/src/Publisher/Publisher.js +77 -0
  88. package/dist/src/Publisher/Publisher.js.map +1 -0
  89. package/dist/src/Server/RouteResolver.d.ts +33 -0
  90. package/dist/src/Server/RouteResolver.js +100 -0
  91. package/dist/src/Server/RouteResolver.js.map +1 -0
  92. package/dist/src/Server/Router.d.ts +157 -0
  93. package/dist/src/Server/Router.js +32 -0
  94. package/dist/src/Server/Router.js.map +1 -0
  95. package/dist/src/Server/lib/ContainerServer.d.ts +42 -0
  96. package/dist/src/Server/lib/ContainerServer.js +66 -0
  97. package/dist/src/Server/lib/ContainerServer.js.map +1 -0
  98. package/dist/src/Server/lib/Server.d.ts +45 -0
  99. package/dist/src/Server/lib/Server.js +93 -0
  100. package/dist/src/Server/lib/Server.js.map +1 -0
  101. package/dist/src/Server/lib/container/GenericHandler.d.ts +9 -0
  102. package/dist/src/Server/lib/container/GenericHandler.js +82 -0
  103. package/dist/src/Server/lib/container/GenericHandler.js.map +1 -0
  104. package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +52 -0
  105. package/dist/src/Server/lib/container/GenericHandlerEvent.js +132 -0
  106. package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -0
  107. package/dist/src/Server/lib/container/HealthHandler.d.ts +9 -0
  108. package/dist/src/Server/lib/container/HealthHandler.js +19 -0
  109. package/dist/src/Server/lib/container/HealthHandler.js.map +1 -0
  110. package/dist/src/Server/lib/container/Proxy.d.ts +67 -0
  111. package/dist/src/Server/lib/container/Proxy.js +143 -0
  112. package/dist/src/Server/lib/container/Proxy.js.map +1 -0
  113. package/dist/src/Server/lib/container/Utils.d.ts +14 -0
  114. package/dist/src/Server/lib/container/Utils.js +37 -0
  115. package/dist/src/Server/lib/container/Utils.js.map +1 -0
  116. package/dist/src/Util/AsyncSingleton.d.ts +31 -0
  117. package/dist/src/Util/AsyncSingleton.js +83 -0
  118. package/dist/src/Util/AsyncSingleton.js.map +1 -0
  119. package/dist/src/Util/Utils.d.ts +61 -0
  120. package/dist/src/Util/Utils.js +147 -0
  121. package/dist/src/Util/Utils.js.map +1 -0
  122. package/dist/src/Validation/Validator.d.ts +17 -0
  123. package/dist/src/Validation/Validator.js +39 -0
  124. package/dist/src/Validation/Validator.js.map +1 -0
  125. package/dist/tsconfig.tsbuildinfo +1 -0
  126. package/index.ts +41 -0
  127. package/jest.config.ts +37 -0
  128. package/jest.smoke.config.ts +34 -0
  129. package/package.json +66 -22
  130. package/src/API/Request.ts +214 -0
  131. package/src/API/Response.ts +370 -0
  132. package/src/BaseEvent/DynamoTransaction.ts +175 -0
  133. package/src/BaseEvent/EventProcessor.ts +140 -0
  134. package/src/BaseEvent/Process.ts +78 -0
  135. package/src/BaseEvent/StepTransaction.ts +35 -0
  136. package/src/BaseEvent/Transaction.ts +323 -0
  137. package/src/Cache/Redis.ts +89 -0
  138. package/src/Cache/types.ts +33 -0
  139. package/src/Config/Configuration.ts +199 -0
  140. package/src/Config/EnvironmentVar.ts +142 -0
  141. package/src/Crypto/Crypto.ts +89 -0
  142. package/src/Database/Database.ts +22 -0
  143. package/src/Database/DatabaseManager.ts +67 -0
  144. package/src/Database/DatabaseTransaction.ts +170 -0
  145. package/src/Database/index.ts +27 -0
  146. package/src/Database/integrations/dynamo/DynamoDatabase.ts +58 -0
  147. package/src/Database/integrations/kysely/KyselyDatabase.ts +99 -0
  148. package/src/Database/integrations/kysely/KyselyTransaction.ts +172 -0
  149. package/src/Database/integrations/pgsql/PostgresDatabase.ts +56 -0
  150. package/src/Database/integrations/pgsql/PostgresTransaction.ts +87 -0
  151. package/src/Database/types.ts +85 -0
  152. package/src/Globals.ts +103 -0
  153. package/src/Logger/Logger.ts +363 -0
  154. package/src/Mailer/Mailer.ts +217 -0
  155. package/src/Publisher/Publisher.ts +96 -0
  156. package/src/Server/RouteResolver.ts +124 -0
  157. package/src/Server/Router.ts +200 -0
  158. package/src/Server/lib/ContainerServer.ts +65 -0
  159. package/src/Server/lib/Server.ts +109 -0
  160. package/src/Server/lib/container/GenericHandler.ts +76 -0
  161. package/src/Server/lib/container/GenericHandlerEvent.ts +154 -0
  162. package/src/Server/lib/container/HealthHandler.ts +11 -0
  163. package/src/Server/lib/container/Proxy.ts +172 -0
  164. package/src/Server/lib/container/Utils.ts +33 -0
  165. package/src/Util/AsyncSingleton.ts +86 -0
  166. package/src/Util/Utils.ts +131 -0
  167. package/src/Validation/Validator.ts +45 -0
  168. package/tests/API/Request.test.ts +273 -0
  169. package/tests/API/Response.test.ts +367 -0
  170. package/tests/BaseEvent/DynamoTransaction.test.ts +272 -0
  171. package/tests/BaseEvent/EventProcessor.test.ts +263 -0
  172. package/tests/BaseEvent/Process.test.ts +47 -0
  173. package/tests/BaseEvent/StepTransaction.test.ts +44 -0
  174. package/tests/BaseEvent/Transaction.test.ts +402 -0
  175. package/tests/Cache/Redis-client.test.ts +90 -0
  176. package/tests/Cache/Redis-cluster.test.ts +100 -0
  177. package/tests/Config/Config.test.ts +205 -0
  178. package/tests/Config/EnvironmentVar.test.ts +251 -0
  179. package/tests/Crypto/Crypto.test.ts +88 -0
  180. package/tests/Database/DatabaseManager.test.ts +79 -0
  181. package/tests/Database/integrations/dynamo/DynamoDatabase.test.ts +44 -0
  182. package/tests/Database/integrations/kysely/KyselyDatabase.test.ts +113 -0
  183. package/tests/Database/integrations/kysely/KyselyTransaction.test.ts +119 -0
  184. package/tests/Database/integrations/pg/PostgresDatabase.test.ts +76 -0
  185. package/tests/Database/integrations/pg/PostgresTransaction.test.ts +118 -0
  186. package/tests/Logger/Logger.test.ts +215 -0
  187. package/tests/Mailer/Mailer.test.ts +59 -0
  188. package/tests/Publisher/Publisher.test.ts +60 -0
  189. package/tests/Server/RouteResolver.test.ts +116 -0
  190. package/tests/Server/Router.test.ts +39 -0
  191. package/tests/Server/lib/ContainerServer.test.ts +531 -0
  192. package/tests/Server/lib/Server.test.ts +12 -0
  193. package/tests/Server/lib/container/GenericHandler.test.ts +131 -0
  194. package/tests/Server/lib/container/GenericHandlerEvent.test.ts +103 -0
  195. package/tests/Server/lib/container/HealthHandler.test.ts +30 -0
  196. package/tests/Server/lib/container/Proxy.test.ts +268 -0
  197. package/tests/Server/lib/container/Utils.test.ts +47 -0
  198. package/tests/Test.utils.ts +78 -0
  199. package/tests/Utils/Utils.test.ts +229 -0
  200. package/tests/Validation/Validator.test.ts +82 -0
  201. package/tsconfig.json +26 -0
  202. package/tsconfig.smoke.json +26 -0
  203. package/index.js +0 -88
  204. package/src/API/IKRequest.js +0 -52
  205. package/src/API/IKResponse.js +0 -119
  206. package/src/API/IKUtils.js +0 -51
  207. package/src/BaseEvent/IKProcess.js +0 -77
  208. package/src/BaseEvent/IKTransaction.js +0 -139
  209. package/src/Cache/Prototype/IKCache.js +0 -17
  210. package/src/Cache/Redis/IKRedis.js +0 -148
  211. package/src/Database/DDB/IKDB.js +0 -56
  212. package/src/Database/DDB/IKDBBaseExpression.js +0 -130
  213. package/src/Database/DDB/IKDBBaseQuery.js +0 -151
  214. package/src/Database/DDB/IKDBQueryBatchGet.js +0 -37
  215. package/src/Database/DDB/IKDBQueryBatchWrite.js +0 -64
  216. package/src/Database/DDB/IKDBQueryDelete.js +0 -34
  217. package/src/Database/DDB/IKDBQueryGet.js +0 -48
  218. package/src/Database/DDB/IKDBQueryPut.js +0 -87
  219. package/src/Database/DDB/IKDBQueryScan.js +0 -45
  220. package/src/Database/DDB/IKDBQueryTransactionalWrite.js +0 -69
  221. package/src/Database/DDB/IKDBQueryUpdate.js +0 -221
  222. package/src/Database/DDB/_IKDBQueryTransactionalRead.js +0 -46
  223. package/src/Database/PSQL/IKDB.js +0 -41
  224. package/src/Database/PSQL/IKDBBaseQuery.js +0 -26
  225. package/src/Database/Prototype/IKDB.js +0 -21
  226. package/src/Database/Prototype/IKDBBaseQuery.js +0 -14
  227. package/src/IKDynamoStream.js +0 -42
  228. package/src/IKEventProcessor.js +0 -42
  229. package/src/IKGlobals.js +0 -24
  230. package/src/IKRouter.js +0 -47
  231. package/src/IKStepTransaction.js +0 -14
  232. package/src/Logger/IKLogger.js +0 -136
  233. package/src/Mailer/IKMailer.js +0 -69
  234. package/src/Publisher/IKPublisher.js +0 -44
  235. package/src/Tracker/IKExecutionTracker.js +0 -79
  236. package/src/Validation/IKValidation.js +0 -76
@@ -0,0 +1,113 @@
1
+ import { jest } from '@jest/globals'
2
+ import { PostgresDialect } from 'kysely'
3
+ import * as pg from 'pg'
4
+
5
+ import type { DbConfig } from '../../../../src/Database/index.js'
6
+ import { KyselyDatabase } from '../../../../src/Database/integrations/kysely/KyselyDatabase.js'
7
+
8
+ const config: DbConfig<'kysely'> = {
9
+ type: 'kysely',
10
+ username: 'username',
11
+ password: 'password',
12
+ host: 'host',
13
+ port: 1234,
14
+ database: 'database',
15
+ maxConnections: 1,
16
+ autoCommit: true,
17
+ }
18
+
19
+ const config2: DbConfig<'kysely'> = {
20
+ ...config,
21
+ readReplica: config,
22
+ }
23
+
24
+ const expectedImplConfig = {
25
+ host: config.host,
26
+ port: config.port,
27
+ user: config.username,
28
+ password: config.password,
29
+ database: config.database,
30
+ max: config.maxConnections,
31
+ }
32
+
33
+ describe('KyselyDatabase', () => {
34
+ let mockTrans
35
+ let mockClient
36
+ let mockDriver
37
+ let mockDialect
38
+ let mockPgTrans
39
+ let mockPgClient
40
+ beforeEach(() => {
41
+ mockTrans = {
42
+ execute: jest.fn(async (cb: any) => {
43
+ cb(mockTrans)
44
+ }),
45
+ } as any
46
+ mockClient = jest.fn(
47
+ () =>
48
+ ({
49
+ transaction: () => mockTrans,
50
+ }) as any as PostgresDialect
51
+ )
52
+ KyselyDatabase['kyselyProvider'] = mockClient as any
53
+
54
+ mockDriver = {
55
+ execute: jest.fn(async () => {}),
56
+ } as any
57
+ mockDialect = jest.fn(
58
+ () =>
59
+ ({
60
+ createDriver: () => mockDriver,
61
+ }) as any as PostgresDialect
62
+ )
63
+ KyselyDatabase['kyselyPgProvider'] = mockDialect as any
64
+
65
+ mockPgTrans = {
66
+ query: jest.fn(() => {}),
67
+ } as any
68
+ mockPgClient = jest.fn(
69
+ () =>
70
+ ({
71
+ connect: async () => mockPgTrans,
72
+ }) as pg.PoolClient
73
+ )
74
+ KyselyDatabase['pgProvider'] = mockPgClient as any
75
+ })
76
+
77
+ test('KyselyDatabase', async () => {
78
+ const underTest = new KyselyDatabase(config)
79
+ expect(mockPgClient).toHaveBeenNthCalledWith(1, expectedImplConfig)
80
+ expect(mockDialect).toHaveBeenCalledTimes(1)
81
+ expect(mockClient).toHaveBeenCalledTimes(1)
82
+ expect(mockClient.mock.calls[0][0].dialect).toEqual(underTest['pgClient'])
83
+ expect(mockClient.mock.calls[0][0].plugins).toBeInstanceOf(Array)
84
+ const trans = await underTest.transaction()
85
+
86
+ expect(mockTrans.execute).toHaveBeenCalledTimes(1)
87
+ expect(trans.reader).toBeUndefined()
88
+ expect(trans.writer).toBeInstanceOf(Object)
89
+ expect(trans.writer.transaction).toBeInstanceOf(Function)
90
+ expect(trans).toBeInstanceOf(Function)
91
+ expect(trans['transaction']).toBe(mockTrans)
92
+ })
93
+
94
+ test('KyselyDatabase - read replica', async () => {
95
+ const underTest = new KyselyDatabase(config2)
96
+ expect(mockPgClient).toHaveBeenNthCalledWith(2, expectedImplConfig)
97
+ expect(mockDialect).toHaveBeenCalledTimes(2)
98
+ expect(mockClient).toHaveBeenCalledTimes(2)
99
+ expect(mockClient.mock.calls[0][0].dialect).toEqual(underTest['pgClient'])
100
+ expect(mockClient.mock.calls[0][0].plugins).toBeInstanceOf(Array)
101
+ expect(mockClient.mock.calls[1][0].dialect).toEqual(underTest['pgReadClient'])
102
+ expect(mockClient.mock.calls[1][0].plugins).toBeInstanceOf(Array)
103
+
104
+ const trans = await underTest.transaction()
105
+
106
+ expect(mockTrans.execute).toHaveBeenCalledTimes(1)
107
+ expect(trans.writer).toBeInstanceOf(Object)
108
+ expect(trans.writer.transaction).toBeInstanceOf(Function)
109
+ expect(trans.reader).toBeInstanceOf(Object)
110
+ expect(trans).toBeInstanceOf(Function)
111
+ expect(trans['transaction']).toBe(mockTrans)
112
+ })
113
+ })
@@ -0,0 +1,119 @@
1
+ import { jest } from '@jest/globals'
2
+ import { Kysely, Transaction } from 'kysely'
3
+
4
+ import type { DbConfig } from '../../../../src/Database/index.js'
5
+ import { KyselyDatabase } from '../../../../src/Database/integrations/kysely/KyselyDatabase.js'
6
+ import { KyselyTransactionImpl } from '../../../../src/Database/integrations/kysely/KyselyTransaction.js'
7
+
8
+ const testResources = async (config: Partial<DbConfig<'kysely'>>) => {
9
+ type DB = {
10
+ view: {
11
+ id: number
12
+ }
13
+ secondView: {
14
+ title: string
15
+ }
16
+ }
17
+
18
+ const database = {
19
+ config: {
20
+ autoCommit: true,
21
+ ...config,
22
+ } as DbConfig<'kysely'>,
23
+ } as any as KyselyDatabase<DB>
24
+
25
+ const transaction = jest.fn().mockImplementation(() => {
26
+ return {
27
+ execute: jest.fn(async (cb: any) => {
28
+ try {
29
+ await cb(transaction)
30
+ } catch (e) {
31
+ /* empty */
32
+ }
33
+ }),
34
+ executeQuery: jest.fn(),
35
+ }
36
+ })() as any as Transaction<DB>
37
+
38
+ const writer = jest.fn().mockImplementation(() => {
39
+ return {
40
+ transaction: jest.fn(() => transaction),
41
+ }
42
+ })() as any as Kysely<DB>
43
+
44
+ return {
45
+ transaction,
46
+ writer,
47
+ underTest: await KyselyTransactionImpl.newTransaction(writer, database),
48
+ }
49
+ }
50
+
51
+ describe('KyselyTransaction', () => {
52
+ test('Transaction starts opened', async () => {
53
+ const { transaction, underTest } = await testResources({})
54
+ expect(underTest.isOpen()).toBe(true)
55
+ expect(transaction['execute']).toHaveBeenCalledTimes(1)
56
+
57
+ await expect(underTest.begin).rejects.toThrowError(
58
+ 'Cannot begin, transaction is already opened!'
59
+ )
60
+ })
61
+
62
+ test('Can only commit once', async () => {
63
+ const { underTest } = await testResources({})
64
+ await underTest.commit()
65
+ expect(underTest.isOpen()).toBe(false)
66
+
67
+ await expect(underTest.commit).rejects.toThrowError(
68
+ 'Cannot commit, transaction is already closed!'
69
+ )
70
+ })
71
+
72
+ test('Can only rollback once', async () => {
73
+ const { underTest } = await testResources({})
74
+ await underTest.rollback()
75
+ expect(underTest.isOpen()).toBe(false)
76
+ await expect(underTest.rollback).rejects.toThrowError(
77
+ 'Cannot rollback, transaction is already closed!'
78
+ )
79
+ })
80
+
81
+ test("Can't open transaction twice", async () => {
82
+ const { underTest } = await testResources({})
83
+ expect(underTest.isOpen()).toBe(true)
84
+
85
+ await expect(underTest.begin).rejects.toThrowError(
86
+ 'Cannot begin, transaction is already opened!'
87
+ )
88
+ })
89
+
90
+ test('Can commit, begin and commit again ', async () => {
91
+ const { underTest } = await testResources({})
92
+ await underTest.commit()
93
+ expect(underTest.isOpen()).toBe(false)
94
+ await underTest.begin()
95
+ expect(underTest.isOpen()).toBe(true)
96
+ await underTest.commit()
97
+ expect(underTest.isOpen()).toBe(false)
98
+ })
99
+
100
+ test('Can commit, begin and rollback ', async () => {
101
+ const { underTest } = await testResources({})
102
+ await underTest.commit()
103
+ expect(underTest.isOpen()).toBe(false)
104
+ await underTest.begin()
105
+ expect(underTest.isOpen()).toBe(true)
106
+ await underTest.rollback()
107
+ expect(underTest.isOpen()).toBe(false)
108
+ })
109
+
110
+ test('Can rollback, begin and commit ', async () => {
111
+ const { underTest } = await testResources({})
112
+ await underTest.rollback()
113
+ expect(underTest.isOpen()).toBe(false)
114
+ await underTest.begin()
115
+ expect(underTest.isOpen()).toBe(true)
116
+ await underTest.commit()
117
+ expect(underTest.isOpen()).toBe(false)
118
+ })
119
+ })
@@ -0,0 +1,76 @@
1
+ import { jest } from '@jest/globals'
2
+ import * as pg from 'pg'
3
+
4
+ import type { DbConfig } from '../../../../src/Database/index.js'
5
+ import { PostgresDatabase } from '../../../../src/Database/integrations/pgsql/PostgresDatabase.js'
6
+
7
+ const config: DbConfig<'pg'> = {
8
+ type: 'pg',
9
+ username: 'username',
10
+ password: 'password',
11
+ host: 'host',
12
+ port: 1234,
13
+ database: 'database',
14
+ maxConnections: 1,
15
+ autoCommit: true,
16
+ }
17
+
18
+ const config2: DbConfig<'pg'> = {
19
+ ...config,
20
+ readReplica: config,
21
+ }
22
+
23
+ const expectedImplConfig = {
24
+ host: config.host,
25
+ port: config.port,
26
+ user: config.username,
27
+ password: config.password,
28
+ database: config.database,
29
+ max: config.maxConnections,
30
+ }
31
+
32
+ describe('PostgresDatabase', () => {
33
+ let mockTrans
34
+ let mockClient
35
+ beforeEach(() => {
36
+ mockTrans = {
37
+ query: jest.fn(() => {}),
38
+ } as any
39
+ mockClient = jest.fn(
40
+ () =>
41
+ ({
42
+ connect: async () => mockTrans,
43
+ }) as pg.PoolClient
44
+ )
45
+ PostgresDatabase['pgProvider'] = mockClient as any
46
+ })
47
+
48
+ test('PostgresDatabase', async () => {
49
+ const underTest = new PostgresDatabase(config)
50
+ expect(mockClient).toHaveBeenNthCalledWith(1, expectedImplConfig)
51
+
52
+ const trans = await underTest.transaction()
53
+
54
+ expect(mockTrans.query).toBeCalledWith('BEGIN')
55
+ expect(trans.reader).toBeUndefined()
56
+ expect(trans.writer).toBeInstanceOf(Object)
57
+ expect(trans.writer.connect).toBeInstanceOf(Function)
58
+ expect(trans).toBeInstanceOf(Function)
59
+ expect(trans['transaction']).toBe(mockTrans)
60
+ })
61
+
62
+ test('PostgresDatabase - read replica', async () => {
63
+ const underTest = new PostgresDatabase(config2)
64
+ expect(mockClient).toHaveBeenNthCalledWith(2, expectedImplConfig)
65
+
66
+ const trans = await underTest.transaction()
67
+
68
+ expect(mockTrans.query).toBeCalledWith('BEGIN')
69
+ expect(trans.reader).toBeInstanceOf(Object)
70
+ expect(trans.reader.connect).toBeInstanceOf(Function)
71
+ expect(trans.writer).toBeInstanceOf(Object)
72
+ expect(trans.writer.connect).toBeInstanceOf(Function)
73
+ expect(trans).toBeInstanceOf(Function)
74
+ expect(trans['transaction']).toBe(mockTrans)
75
+ })
76
+ })
@@ -0,0 +1,118 @@
1
+ import { jest } from '@jest/globals'
2
+ import * as pg from 'pg'
3
+
4
+ import type { DbConfig } from '../../../../src/Database/index.js'
5
+ import { PostgresDatabase } from '../../../../src/Database/integrations/pgsql/PostgresDatabase.js'
6
+ import { PostgresTransactionImpl } from '../../../../src/Database/integrations/pgsql/PostgresTransaction.js'
7
+
8
+ const testResources = async (config: Partial<DbConfig<'pg'>>) => {
9
+ const database = {
10
+ config: {
11
+ autoCommit: true,
12
+ ...config,
13
+ } as DbConfig<'pg'>,
14
+ } as any as PostgresDatabase
15
+
16
+ const transaction = jest.fn().mockImplementation(() => {
17
+ return {
18
+ commit: jest.fn(),
19
+ rollback: jest.fn(),
20
+ query: jest.fn(),
21
+ }
22
+ })() as any as pg.PoolClient
23
+
24
+ const writer = jest.fn().mockImplementation(() => {
25
+ return {
26
+ connect: jest.fn(() => transaction),
27
+ }
28
+ })() as any as pg.Pool
29
+
30
+ return {
31
+ transaction,
32
+ writer,
33
+ underTest: await PostgresTransactionImpl.newTransaction(writer, database),
34
+ }
35
+ }
36
+
37
+ describe('PostgresTransaction', () => {
38
+ test('Transaction starts opened', async () => {
39
+ const { transaction, underTest } = await testResources({})
40
+ expect(underTest.isOpen()).toBe(true)
41
+ expect(transaction.query).toHaveBeenNthCalledWith(1, 'BEGIN')
42
+
43
+ await expect(underTest.begin).rejects.toThrowError(
44
+ 'Cannot begin, transaction is already opened!'
45
+ )
46
+ })
47
+
48
+ test('Can only commit once', async () => {
49
+ const { transaction, underTest } = await testResources({})
50
+ await underTest.commit()
51
+ expect(underTest.isOpen()).toBe(false)
52
+ expect(transaction.query).toBeCalledWith('COMMIT')
53
+
54
+ await expect(underTest.commit).rejects.toThrowError(
55
+ 'Cannot commit, transaction is already closed!'
56
+ )
57
+ })
58
+
59
+ test('Can only rollback once', async () => {
60
+ const { transaction, underTest } = await testResources({})
61
+ await underTest.rollback()
62
+ expect(underTest.isOpen()).toBe(false)
63
+ expect(transaction.query).toBeCalledWith('ROLLBACK')
64
+
65
+ await expect(underTest.rollback).rejects.toThrowError(
66
+ 'Cannot rollback, transaction is already closed!'
67
+ )
68
+ })
69
+
70
+ test("Can't open transaction twice", async () => {
71
+ const { transaction, underTest } = await testResources({})
72
+ expect(underTest.isOpen()).toBe(true)
73
+ expect(transaction.query).toHaveBeenNthCalledWith(1, 'BEGIN')
74
+
75
+ await expect(underTest.begin).rejects.toThrowError(
76
+ 'Cannot begin, transaction is already opened!'
77
+ )
78
+ })
79
+
80
+ test('Can commit, begin and commit again ', async () => {
81
+ const { transaction, underTest } = await testResources({})
82
+ await underTest.commit()
83
+ expect(underTest.isOpen()).toBe(false)
84
+ expect(transaction.query).toBeCalledWith('COMMIT')
85
+ await underTest.begin()
86
+ expect(underTest.isOpen()).toBe(true)
87
+ expect(transaction.query).toHaveBeenNthCalledWith(3, 'BEGIN')
88
+ await underTest.commit()
89
+ expect(underTest.isOpen()).toBe(false)
90
+ expect(transaction.query).toHaveBeenNthCalledWith(4, 'COMMIT')
91
+ })
92
+
93
+ test('Can commit, begin and rollback ', async () => {
94
+ const { transaction, underTest } = await testResources({})
95
+ await underTest.commit()
96
+ expect(underTest.isOpen()).toBe(false)
97
+ expect(transaction.query).toBeCalledWith('COMMIT')
98
+ await underTest.begin()
99
+ expect(underTest.isOpen()).toBe(true)
100
+ expect(transaction.query).toHaveBeenNthCalledWith(3, 'BEGIN')
101
+ await underTest.rollback()
102
+ expect(underTest.isOpen()).toBe(false)
103
+ expect(transaction.query).toHaveBeenNthCalledWith(4, 'ROLLBACK')
104
+ })
105
+
106
+ test('Can rollback, begin and commit ', async () => {
107
+ const { transaction, underTest } = await testResources({})
108
+ await underTest.rollback()
109
+ expect(underTest.isOpen()).toBe(false)
110
+ expect(transaction.query).toBeCalledWith('ROLLBACK')
111
+ await underTest.begin()
112
+ expect(underTest.isOpen()).toBe(true)
113
+ expect(transaction.query).toHaveBeenNthCalledWith(3, 'BEGIN')
114
+ await underTest.commit()
115
+ expect(underTest.isOpen()).toBe(false)
116
+ expect(transaction.query).toHaveBeenNthCalledWith(4, 'COMMIT')
117
+ })
118
+ })
@@ -0,0 +1,215 @@
1
+ import { jest } from '@jest/globals'
2
+ import { expect as c_expect } from 'chai'
3
+
4
+ // get console ref and mock before logger import
5
+ const consoleProxy = console
6
+ const mock = jest.spyOn(consoleProxy, 'log')
7
+ // import logger after first ref
8
+ import Logger from '../../src/Logger/Logger.js'
9
+ // get console reference after logger import
10
+ const transactionID = '123-456'
11
+
12
+ function setContainerFlag(isContainer: boolean) {
13
+ if (isContainer) {
14
+ process.env['HYBRIDLESS_RUNTIME'] = 'true'
15
+ } else {
16
+ process.env['HYBRIDLESS_RUNTIME'] = undefined
17
+ }
18
+ }
19
+
20
+ function fixLogTypePrefix(logType: string) {
21
+ if (logType == 'exception') return 'error'
22
+ if (logType == 'warning') return 'warn'
23
+ if (logType == 'log') return 'info'
24
+ return logType
25
+ }
26
+
27
+ function randomDeepObject(count, endKey) {
28
+ if (count <= 1) return { [endKey]: 'value' }
29
+ const object = {}
30
+ for (let i = 0; i < count; i++) {
31
+ const key = (+new Date() * Math.random()).toString(36).substring(0, 6)
32
+ object[key] = randomDeepObject(Math.floor(Math.random() * (count / 2)), endKey)
33
+ }
34
+ return object
35
+ }
36
+
37
+ function testLogs(isContainer: boolean, provider?: Logger) {
38
+ const type = isContainer ? 'container' : 'serverless'
39
+ const loggerType = !provider ? 'Console' : 'Logger'
40
+ const localProvider = provider || console
41
+
42
+ test(`${type} - ${loggerType} Log - Suppress sensitive info`, async () => {
43
+ setContainerFlag(isContainer)
44
+ localProvider.log('my password is 123')
45
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
46
+ 1,
47
+ expect.stringContaining((isContainer ? `${transactionID} ` : '') + '[INFO] [Logger.test.ts:')
48
+ )
49
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
50
+ 1,
51
+ expect.stringContaining('] my password is 123')
52
+ )
53
+ })
54
+
55
+ test(`${type} - ${loggerType} Log - Suppress sensitive info (object)`, async () => {
56
+ setContainerFlag(isContainer)
57
+ const object = { password: '123' }
58
+ localProvider.log('TEST', object)
59
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
60
+ 1,
61
+ expect.stringContaining((isContainer ? `${transactionID} ` : '') + '[INFO] [Logger.test.ts:')
62
+ )
63
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
64
+ 1,
65
+ expect.stringContaining('] TEST {\n "password": "**SUPPRESSED_SENSITIVE_DATA** (3 len)"\n}')
66
+ )
67
+ // test if object is not mutate
68
+ c_expect(object.password).to.be.equals('123')
69
+ })
70
+
71
+ test(`${type} - ${loggerType} Log - Suppress sensitive info (long-object)`, async () => {
72
+ setContainerFlag(isContainer)
73
+ const object = randomDeepObject(99, 'password')
74
+ localProvider.log('TEST', object)
75
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
76
+ 1,
77
+ expect.stringContaining((isContainer ? `${transactionID} ` : '') + '[INFO] [Logger.test.ts:')
78
+ )
79
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(1, expect.stringContaining('] TEST'))
80
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
81
+ 1,
82
+ expect.stringContaining('"password": "**SUPPRESSED_SENSITIVE_DATA** (5 len)"')
83
+ )
84
+ })
85
+
86
+ test(`${type} - ${loggerType} Log - Suppress sensitive info (object with sensitive string)`, async () => {
87
+ setContainerFlag(isContainer)
88
+ localProvider.log({ object: JSON.stringify({ password: '123' }) })
89
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
90
+ 1,
91
+ expect.stringContaining((isContainer ? `${transactionID} ` : '') + '[INFO] [Logger.test.ts:')
92
+ )
93
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
94
+ 1,
95
+ expect.stringContaining(
96
+ '] {\n "object": {\n "password": "**SUPPRESSED_SENSITIVE_DATA** (3 len)"\n }\n}'
97
+ )
98
+ )
99
+ })
100
+
101
+ test(`${type} - ${loggerType} Log - Suppress sensitive info (object with sensitive number)`, async () => {
102
+ setContainerFlag(isContainer)
103
+ localProvider.log({ object: JSON.stringify({ password: 123 }) })
104
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
105
+ 1,
106
+ expect.stringContaining((isContainer ? `${transactionID} ` : '') + '[INFO] [Logger.test.ts:')
107
+ )
108
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
109
+ 1,
110
+ expect.stringContaining(
111
+ '] {\n "object": {\n "password": "**SUPPRESSED_SENSITIVE_DATA** (3 len)"\n }\n}'
112
+ )
113
+ )
114
+ })
115
+
116
+ test(`${type} - ${loggerType} Log - Suppress sensitive info (null key)`, async () => {
117
+ setContainerFlag(isContainer)
118
+ localProvider.log({ object: JSON.stringify({ password: null }) })
119
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
120
+ 1,
121
+ expect.stringContaining((isContainer ? `${transactionID} ` : '') + '[INFO] [Logger.test.ts:')
122
+ )
123
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
124
+ 1,
125
+ expect.stringContaining('] {\n "object": {\n "password": null\n }\n}')
126
+ )
127
+ })
128
+
129
+ test(`${type} - ${loggerType} Log - Suppress sensitive info (null)`, async () => {
130
+ setContainerFlag(isContainer)
131
+ localProvider.log(null)
132
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
133
+ 1,
134
+ expect.stringContaining((isContainer ? `${transactionID} ` : '') + '[INFO] [Logger.test.ts:')
135
+ )
136
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(1, expect.stringContaining('] '))
137
+ })
138
+
139
+ test(`${type} - ${loggerType} Log - Suppress sensitive info (array)`, async () => {
140
+ setContainerFlag(isContainer)
141
+ localProvider.log('TEST2', [{ password: '1234' }])
142
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
143
+ 1,
144
+ expect.stringContaining((isContainer ? `${transactionID} ` : '') + '[INFO] [Logger.test.ts:')
145
+ )
146
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
147
+ 1,
148
+ expect.stringContaining(
149
+ '] TEST2 [\n {\n "password": "**SUPPRESSED_SENSITIVE_DATA** (4 len)"\n }\n]'
150
+ )
151
+ )
152
+ })
153
+
154
+ test(`${type} - ${loggerType} Log - Circular reference (Error)`, async () => {
155
+ setContainerFlag(isContainer)
156
+ class SelfRefError extends Error {
157
+ public self: SelfRefError
158
+ constructor() {
159
+ super()
160
+ this.self = this
161
+ }
162
+ }
163
+ localProvider.log(new SelfRefError())
164
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
165
+ 1,
166
+ expect.stringContaining((isContainer ? `${transactionID} ` : '') + '[INFO] [Logger.test.ts:')
167
+ )
168
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
169
+ 1,
170
+ expect.stringContaining('tests/Logger/Logger.test.ts:')
171
+ )
172
+ })
173
+
174
+ for (const logType of ['log', 'debug', 'info', 'warn', 'warning', 'error', 'exception']) {
175
+ test(`${type} - ${loggerType} ${logType}`, async () => {
176
+ setContainerFlag(isContainer)
177
+ localProvider[logType](logType.toUpperCase())
178
+ // alias
179
+ const logTypePrefix = fixLogTypePrefix(logType)
180
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
181
+ 1,
182
+ expect.stringContaining(
183
+ (isContainer ? `${transactionID} ` : '') +
184
+ `[${logTypePrefix.toUpperCase()}] [Logger.test.ts:`
185
+ )
186
+ )
187
+ expect(consoleProxy.log).toHaveBeenNthCalledWith(
188
+ 1,
189
+ expect.stringContaining(`] ${logType.toUpperCase()}`)
190
+ )
191
+ })
192
+ }
193
+ }
194
+
195
+ describe('Logger', () => {
196
+ beforeEach(() => {
197
+ mock.mockClear()
198
+ })
199
+ const provider = new Logger(
200
+ {
201
+ logLevel: 'DEBUG',
202
+ sensitiveFilteringKeywords: true,
203
+ },
204
+ transactionID
205
+ )
206
+ provider.notGlobalLogger()
207
+ // Test serverless logs
208
+ testLogs(false, provider)
209
+ testLogs(false)
210
+ // Test container logs
211
+ testLogs(true, provider)
212
+ testLogs(true)
213
+ })
214
+
215
+ export {}
@@ -0,0 +1,59 @@
1
+ import { SESClient, SendRawEmailCommand } from '@aws-sdk/client-ses'
2
+ import { mockClient } from 'aws-sdk-client-mock'
3
+ import { expect } from 'chai'
4
+
5
+ import Mailer from '../../src/Mailer/Mailer.js'
6
+
7
+ const SESMock = mockClient(SESClient)
8
+
9
+ describe('SendRaw', () => {
10
+ // reset mock
11
+ beforeEach(() => {
12
+ SESMock.reset()
13
+ })
14
+
15
+ const provider = new Mailer('dev+tests@gmail.com', 'ca-central-1')
16
+
17
+ test('Send raw with success', async () => {
18
+ const messageId = 'id-123'
19
+ SESMock.on(SendRawEmailCommand).resolves({
20
+ MessageId: messageId,
21
+ })
22
+ const res = await provider.sendRawEmail('test@test.com', 'My message', 'My subject')
23
+ expect(res).is.not.null
24
+ console.log(res)
25
+ })
26
+
27
+ test('Send raw with success', async () => {
28
+ const messageId = 'id-123'
29
+ const from = 'test@test.com'
30
+ const to = 'test@test2.com'
31
+ const msg = 'My message'
32
+ const subject = 'My subject'
33
+ const cc = 'test@test3.com'
34
+ const replyTo = 'test@test4.com'
35
+ SESMock.on(SendRawEmailCommand).resolves({
36
+ MessageId: messageId,
37
+ })
38
+ const res = await provider.sendRawEmail(to, msg, subject, cc, from, replyTo)
39
+ expect(res).is.not.null
40
+ console.log(res)
41
+ })
42
+
43
+ test('Send raw with failure', async () => {
44
+ SESMock.on(SendRawEmailCommand).rejects(new Error('failed!'))
45
+ let res: any = null,
46
+ err: any = null
47
+ try {
48
+ res = await provider.sendRawEmail('test@test.com', 'My message', 'My subject')
49
+ } catch (e) {
50
+ err = e
51
+ }
52
+ expect(res).is.null
53
+ expect(err?.message).to.be.equals('failed!')
54
+ expect(res).is.null
55
+ console.log(res)
56
+ })
57
+ })
58
+
59
+ export {}