@typetron/framework 0.5.2 → 0.5.3

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 (195) hide show
  1. package/package.json +3 -6
  2. package/.eslintrc-old.js +0 -208
  3. package/Cache/Cache.ts +0 -13
  4. package/Cache/Stores/DatabaseStore.ts +0 -120
  5. package/Cache/Stores/FileStore.ts +0 -69
  6. package/Cache/Stores/MemoryStore.ts +0 -71
  7. package/Cache/index.ts +0 -5
  8. package/Cache/types.ts +0 -4
  9. package/Container/ClassResolver.ts +0 -106
  10. package/Container/Container.ts +0 -144
  11. package/Container/Contracts.ts +0 -3
  12. package/Container/Decorators.ts +0 -20
  13. package/Container/Metadata.ts +0 -16
  14. package/Container/Resolver.ts +0 -27
  15. package/Container/index.ts +0 -5
  16. package/Database/Clauses/Where.ts +0 -0
  17. package/Database/Connection.ts +0 -52
  18. package/Database/Decorators.ts +0 -142
  19. package/Database/Drivers/DatabaseDriver.ts +0 -37
  20. package/Database/Drivers/MySQL/Alter/index.ts +0 -55
  21. package/Database/Drivers/MySQL/ColumnDefinition.ts +0 -16
  22. package/Database/Drivers/MySQL/Create.ts +0 -47
  23. package/Database/Drivers/MySQL/MysqlDriver.ts +0 -123
  24. package/Database/Drivers/MySQL/Schema.ts +0 -82
  25. package/Database/Drivers/MySQL/index.ts +0 -13
  26. package/Database/Drivers/SQL/Schema.ts +0 -73
  27. package/Database/Drivers/SQL/Statements/Alter.ts +0 -22
  28. package/Database/Drivers/SQL/Statements/BaseStatement.ts +0 -55
  29. package/Database/Drivers/SQL/Statements/Delete.ts +0 -20
  30. package/Database/Drivers/SQL/Statements/Insert.ts +0 -24
  31. package/Database/Drivers/SQL/Statements/Select.ts +0 -55
  32. package/Database/Drivers/SQL/Statements/Update.ts +0 -33
  33. package/Database/Drivers/SQL/index.ts +0 -8
  34. package/Database/Drivers/SQLite/Create.ts +0 -45
  35. package/Database/Drivers/SQLite/Schema.ts +0 -54
  36. package/Database/Drivers/SQLite/SqliteDriver.ts +0 -144
  37. package/Database/Drivers/SQLite/index.ts +0 -15
  38. package/Database/Drivers/SchemaContract.ts +0 -11
  39. package/Database/Drivers/Statement.ts +0 -16
  40. package/Database/Drivers/index.ts +0 -4
  41. package/Database/Entity.ts +0 -377
  42. package/Database/EntityNotFoundError.ts +0 -5
  43. package/Database/EntityProxyHandler.ts +0 -43
  44. package/Database/EntityQuery.ts +0 -197
  45. package/Database/Expression.ts +0 -3
  46. package/Database/Fields.ts +0 -816
  47. package/Database/Helpers.ts +0 -12
  48. package/Database/List.ts +0 -71
  49. package/Database/Migrations/Migration.ts +0 -10
  50. package/Database/Migrations/MigrationHistory.ts +0 -20
  51. package/Database/Migrations/Migrator.ts +0 -122
  52. package/Database/Migrations/index.ts +0 -3
  53. package/Database/ORM/BaseRelationship.ts +0 -10
  54. package/Database/Query.ts +0 -374
  55. package/Database/Seeders/Seeder.ts +0 -3
  56. package/Database/Seeders/SeederManager.ts +0 -27
  57. package/Database/Seeders/index.ts +0 -2
  58. package/Database/StringExpression.ts +0 -9
  59. package/Database/Types.ts +0 -165
  60. package/Database/index.ts +0 -23
  61. package/Encryption/index.ts +0 -51
  62. package/Forms/Decorators.ts +0 -29
  63. package/Forms/Form.ts +0 -70
  64. package/Forms/FormFields.ts +0 -36
  65. package/Forms/index.ts +0 -3
  66. package/Framework/App.ts +0 -10
  67. package/Framework/Application.ts +0 -118
  68. package/Framework/Auth/Auth.ts +0 -122
  69. package/Framework/Auth/Authenticatable.ts +0 -9
  70. package/Framework/Auth/User.ts +0 -27
  71. package/Framework/Auth/index.ts +0 -29
  72. package/Framework/Config/AppConfig.ts +0 -25
  73. package/Framework/Config/AuthConfig.ts +0 -9
  74. package/Framework/Config/BaseConfig.ts +0 -9
  75. package/Framework/Config/CacheConfig.ts +0 -38
  76. package/Framework/Config/DatabaseConfig.ts +0 -21
  77. package/Framework/Config/MailConfig.ts +0 -30
  78. package/Framework/Config/index.ts +0 -6
  79. package/Framework/Middleware/AuthMiddleware.ts +0 -31
  80. package/Framework/Middleware/CorsMiddleware.ts +0 -32
  81. package/Framework/Middleware/StaticAssetsMiddleware.ts +0 -76
  82. package/Framework/Middleware/index.ts +0 -2
  83. package/Framework/Provider.ts +0 -9
  84. package/Framework/Providers/CacheProvider.ts +0 -28
  85. package/Framework/Providers/DatabaseProvider.ts +0 -36
  86. package/Framework/Providers/MailProvider.ts +0 -31
  87. package/Framework/Providers/WebsocketsProvider.ts +0 -120
  88. package/Framework/Resolvers/AuthResolver.ts +0 -23
  89. package/Framework/Resolvers/EntityResolver.ts +0 -41
  90. package/Framework/Resolvers/FormResolver.ts +0 -37
  91. package/Framework/RootDir.ts +0 -1
  92. package/Framework/index.ts +0 -8
  93. package/Frontend/Angular/index.ts +0 -24
  94. package/Frontend/Websockets/index.ts +0 -86
  95. package/Mail/MailAddress.ts +0 -4
  96. package/Mail/MailTransport.ts +0 -6
  97. package/Mail/Mailable.ts +0 -16
  98. package/Mail/Mailer.ts +0 -27
  99. package/Mail/PendingMail.ts +0 -68
  100. package/Mail/SentMessage.ts +0 -20
  101. package/Mail/Transports/MemoryTransport.ts +0 -13
  102. package/Mail/Transports/SendGridTransport.ts +0 -95
  103. package/Mail/index.ts +0 -3
  104. package/Models/Model.ts +0 -41
  105. package/Models/index.ts +0 -54
  106. package/Router/BaseController.ts +0 -7
  107. package/Router/Decorators.ts +0 -224
  108. package/Router/Guard.ts +0 -10
  109. package/Router/Http/Contracts.ts +0 -4
  110. package/Router/Http/ErrorHandler.ts +0 -45
  111. package/Router/Http/ErrorHandlerInterface.ts +0 -6
  112. package/Router/Http/Errors/HttpError.ts +0 -15
  113. package/Router/Http/Handler.ts +0 -183
  114. package/Router/Http/HttpRoute.ts +0 -110
  115. package/Router/Http/Middleware.ts +0 -7
  116. package/Router/Http/Request.ts +0 -225
  117. package/Router/Http/Response.ts +0 -35
  118. package/Router/Http/index.ts +0 -85
  119. package/Router/Metadata.ts +0 -34
  120. package/Router/Middleware.ts +0 -16
  121. package/Router/Request.ts +0 -65
  122. package/Router/Route.ts +0 -16
  123. package/Router/RouteNotFoundError.ts +0 -5
  124. package/Router/Router.ts +0 -35
  125. package/Router/Servers/index.ts +0 -6
  126. package/Router/Servers/node.ts +0 -49
  127. package/Router/Servers/uNetworking.ts +0 -151
  128. package/Router/Websockets/Handler.ts +0 -82
  129. package/Router/Websockets/Middleware.ts +0 -7
  130. package/Router/Websockets/Request.ts +0 -3
  131. package/Router/Websockets/WebsocketRoute.ts +0 -61
  132. package/Router/Websockets/index.ts +0 -57
  133. package/Router/Websockets/types.ts +0 -23
  134. package/Router/index.ts +0 -6
  135. package/Storage/File.ts +0 -30
  136. package/Storage/Image.ts +0 -19
  137. package/Storage/InvalidImage.ts +0 -3
  138. package/Storage/Storage.ts +0 -98
  139. package/Storage/index.ts +0 -4
  140. package/Storage/utils.ts +0 -18
  141. package/Support/Array.ts +0 -184
  142. package/Support/Math.ts +0 -12
  143. package/Support/Metadata.ts +0 -31
  144. package/Support/String.ts +0 -38
  145. package/Support/index.ts +0 -19
  146. package/Support/utils.ts +0 -7
  147. package/Testing/TestCase.ts +0 -193
  148. package/Validation/Rule.ts +0 -9
  149. package/Validation/RuleInterface.ts +0 -10
  150. package/Validation/Rules/Email.ts +0 -20
  151. package/Validation/Rules/FileExtension.ts +0 -21
  152. package/Validation/Rules/MaxLength.ts +0 -17
  153. package/Validation/Rules/MinLength.ts +0 -18
  154. package/Validation/Rules/Optional.ts +0 -18
  155. package/Validation/Rules/Required.ts +0 -29
  156. package/Validation/Validator.ts +0 -7
  157. package/Validation/index.ts +0 -12
  158. package/benchmark-index.js +0 -20
  159. package/benchmark.ts +0 -47
  160. package/tests/Cache/DatabaseStoreTest.ts +0 -110
  161. package/tests/Cache/FileStoreTest.ts +0 -93
  162. package/tests/Cache/MemoryStoreTest.ts +0 -92
  163. package/tests/Container/ContainerTest.ts +0 -336
  164. package/tests/Database/Drivers/MySQL/AlterTest.ts +0 -100
  165. package/tests/Database/Drivers/MySQL/CreateTest.ts +0 -39
  166. package/tests/Database/Drivers/MysqlDriverTest.ts +0 -140
  167. package/tests/Database/Drivers/SqliteDriverTest.ts +0 -126
  168. package/tests/Database/Entities/Article.ts +0 -21
  169. package/tests/Database/Entities/Profile.ts +0 -21
  170. package/tests/Database/Entities/Role.ts +0 -15
  171. package/tests/Database/Entities/User.ts +0 -35
  172. package/tests/Database/EntityRelationshipsTest.ts +0 -475
  173. package/tests/Database/EntityTest.ts +0 -202
  174. package/tests/Database/Migrations/RunnerTest.ts +0 -96
  175. package/tests/Database/Migrations/migrations/1.createUserTable.ts +0 -12
  176. package/tests/Database/Migrations/migrations/2.createArticlesTable.ts +0 -12
  177. package/tests/Database/QueryTest.ts +0 -573
  178. package/tests/Database/SchemaTest.ts +0 -32
  179. package/tests/Database/Seeders/RunnerTest.ts +0 -30
  180. package/tests/Database/Seeders/seeders/RandomSeeder.ts +0 -10
  181. package/tests/Database/utils.ts +0 -16
  182. package/tests/Forms/FormsTest.ts +0 -59
  183. package/tests/Framework/Middleware/StaticAssetsMiddlewareTest.ts +0 -129
  184. package/tests/Framework/Resolvers/EntityResolverTest.ts +0 -48
  185. package/tests/Framework/Router/RouterTest.ts +0 -33
  186. package/tests/Mail/MailerTest.ts +0 -76
  187. package/tests/Model/ModelTest.ts +0 -183
  188. package/tests/Router/HttpHandlerTest.ts +0 -72
  189. package/tests/Router/RequestTest.ts +0 -77
  190. package/tests/Router/WebSocketsHandlerTest.ts +0 -51
  191. package/tests/Storage/StorageTest.ts +0 -97
  192. package/tests/Support/ArrayTest.ts +0 -167
  193. package/tests/Support/StringTest.ts +0 -18
  194. package/tests/Validation/RulesTest.ts +0 -54
  195. package/types.d.ts +0 -8
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@typetron/framework",
3
3
  "description": "Modern Node.js framework for modern apps written in Typescript",
4
4
  "license": "MIT",
5
- "version": "0.5.2",
5
+ "version": "0.5.3",
6
6
  "keywords": [
7
7
  "typetron",
8
8
  "framework",
@@ -15,7 +15,7 @@
15
15
  "name": "Ionel Lupu"
16
16
  },
17
17
  "scripts": {
18
- "build": "tsc && cp package.json build/",
18
+ "build": "tsc",
19
19
  "dev": "tsc --watch",
20
20
  "test": "mocha",
21
21
  "test:watch": "mocha --watch",
@@ -63,9 +63,6 @@
63
63
  "url": "https://github.com/typetron/framework.git"
64
64
  },
65
65
  "files": [
66
- "**/*.ts",
67
- "**/*.js",
68
- "**/*.ts.map",
69
- "**/*.js.map"
66
+ "build/"
70
67
  ]
71
68
  }
package/.eslintrc-old.js DELETED
@@ -1,208 +0,0 @@
1
- /*
2
- 👋 Hi! This file was autogenerated by tslint-to-eslint-config.
3
- https://github.com/typescript-eslint/tslint-to-eslint-config
4
-
5
- It represents the closest reasonable ESLint configuration to this
6
- project's original TSLint configuration.
7
-
8
- We recommend eventually switching this configuration to extend from
9
- the recommended rulesets in typescript-eslint.
10
- https://github.com/typescript-eslint/tslint-to-eslint-config/blob/master/docs/FAQs.md
11
-
12
- Happy linting! 💖
13
- */
14
- module.exports = {
15
- 'env' : {
16
- 'es6' : true,
17
- 'node' : true
18
- },
19
- 'parser' : '@typescript-eslint/parser',
20
- 'parserOptions' : {
21
- 'project' : 'tsconfig.json',
22
- 'sourceType' : 'module'
23
- },
24
- 'plugins' : [
25
- 'eslint-plugin-import',
26
- 'eslint-plugin-no-null',
27
- 'eslint-plugin-jsdoc',
28
- '@angular-eslint/eslint-plugin',
29
- '@typescript-eslint',
30
- '@typescript-eslint/tslint'
31
- ],
32
- 'root' : true,
33
- 'rules' : {
34
- '@angular-eslint/component-class-suffix' : 'error',
35
- '@angular-eslint/directive-class-suffix' : 'error',
36
- '@angular-eslint/no-input-rename' : 'error',
37
- '@angular-eslint/no-output-on-prefix' : 'error',
38
- '@angular-eslint/no-output-rename' : 'error',
39
- '@angular-eslint/use-pipe-transform-interface' : 'error',
40
- '@typescript-eslint/ban-types' : 'error',
41
- '@typescript-eslint/dot-notation' : 'off',
42
- '@typescript-eslint/explicit-member-accessibility' : [
43
- 'off',
44
- {
45
- 'accessibility' : 'explicit'
46
- }
47
- ],
48
- '@typescript-eslint/indent' : 'error',
49
- '@typescript-eslint/member-delimiter-style' : [
50
- 'off',
51
- {
52
- 'multiline' : {
53
- 'delimiter' : 'semi',
54
- 'requireLast' : true
55
- },
56
- 'singleline' : {
57
- 'delimiter' : 'semi',
58
- 'requireLast' : false
59
- }
60
- }
61
- ],
62
- '@typescript-eslint/naming-convention' : [
63
- 'error',
64
- {
65
- 'selector' : 'variable',
66
- 'format' : [
67
- 'camelCase',
68
- 'UPPER_CASE'
69
- ],
70
- 'leadingUnderscore' : 'forbid',
71
- 'trailingUnderscore' : 'forbid'
72
- }
73
- ],
74
- '@typescript-eslint/no-empty-function' : 'off',
75
- '@typescript-eslint/no-empty-interface' : 'error',
76
- '@typescript-eslint/no-explicit-any' : 'error',
77
- '@typescript-eslint/no-inferrable-types' : 'off',
78
- '@typescript-eslint/no-misused-new' : 'error',
79
- '@typescript-eslint/no-non-null-assertion' : 'error',
80
- '@typescript-eslint/no-shadow' : [
81
- 'error',
82
- {
83
- 'hoist' : 'all'
84
- }
85
- ],
86
- '@typescript-eslint/no-unused-expressions' : 'error',
87
- '@typescript-eslint/no-use-before-define' : 'error',
88
- '@typescript-eslint/prefer-function-type' : 'error',
89
- '@typescript-eslint/quotes' : [
90
- 'error',
91
- 'single'
92
- ],
93
- '@typescript-eslint/semi' : [
94
- 'off',
95
- 'always'
96
- ],
97
- '@typescript-eslint/type-annotation-spacing' : 'error',
98
- '@typescript-eslint/unified-signatures' : 'error',
99
- 'arrow-body-style' : 'error',
100
- 'brace-style' : [
101
- 'error',
102
- '1tbs'
103
- ],
104
- 'constructor-super' : 'error',
105
- 'curly' : 'error',
106
- 'dot-notation' : 'off',
107
- 'eol-last' : 'off',
108
- 'eqeqeq' : [
109
- 'error',
110
- 'smart'
111
- ],
112
- 'guard-for-in' : 'off',
113
- 'id-denylist' : 'off',
114
- 'id-match' : 'off',
115
- 'import/no-default-export' : 'error',
116
- 'import/no-deprecated' : 'warn',
117
- 'indent' : 'off',
118
- 'jsdoc/no-types' : 'error',
119
- 'max-len' : [
120
- 'error',
121
- {
122
- 'code' : 140
123
- }
124
- ],
125
- 'no-bitwise' : 'error',
126
- 'no-caller' : 'error',
127
- 'no-console' : [
128
- 'error',
129
- {
130
- 'allow' : [
131
- 'log',
132
- 'warn',
133
- 'dir',
134
- 'timeLog',
135
- 'assert',
136
- 'clear',
137
- 'count',
138
- 'countReset',
139
- 'group',
140
- 'groupEnd',
141
- 'table',
142
- 'dirxml',
143
- 'error',
144
- 'groupCollapsed',
145
- 'Console',
146
- 'profile',
147
- 'profileEnd',
148
- 'timeStamp',
149
- 'context'
150
- ]
151
- }
152
- ],
153
- 'no-debugger' : 'error',
154
- 'no-empty' : 'off',
155
- 'no-empty-function' : 'off',
156
- 'no-eval' : 'error',
157
- 'no-fallthrough' : 'error',
158
- 'no-new-wrappers' : 'error',
159
- 'no-null/no-null' : 'error',
160
- 'no-restricted-imports' : [
161
- 'error',
162
- 'rxjs/Rx'
163
- ],
164
- 'no-shadow' : 'off',
165
- 'no-throw-literal' : 'error',
166
- 'no-trailing-spaces' : 'off',
167
- 'no-undef-init' : 'error',
168
- 'no-underscore-dangle' : 'off',
169
- 'no-unused-expressions' : 'off',
170
- 'no-unused-labels' : 'error',
171
- 'no-use-before-define' : 'off',
172
- 'no-var' : 'error',
173
- 'prefer-const' : 'error',
174
- 'quotes' : 'off',
175
- 'radix' : 'error',
176
- 'semi' : 'off',
177
- 'spaced-comment' : [
178
- 'error',
179
- 'always',
180
- {
181
- 'markers' : [
182
- '/'
183
- ]
184
- }
185
- ],
186
- '@typescript-eslint/tslint/config' : [
187
- 'error',
188
- {
189
- 'rules' : {
190
- 'import-spacing' : true,
191
- 'no-unsafe-any' : true,
192
- 'use-host-property-decorator' : true,
193
- 'use-input-property-decorator' : true,
194
- 'use-life-cycle-interface' : true,
195
- 'use-output-property-decorator' : true,
196
- 'whitespace' : [
197
- true,
198
- 'check-branch',
199
- 'check-decl',
200
- 'check-operator',
201
- 'check-separator',
202
- 'check-type'
203
- ]
204
- }
205
- }
206
- ]
207
- }
208
- }
package/Cache/Cache.ts DELETED
@@ -1,13 +0,0 @@
1
- export abstract class Cache {
2
- abstract has(name: string): Promise<boolean>
3
-
4
- abstract get<T>(name: string, defaultValue?: T | (() => T | Promise<T>)): Promise<T | undefined>
5
-
6
- abstract remember<T>(name: string, defaultValue?: T | (() => T | Promise<T>), duration?: number): Promise<T | undefined>
7
-
8
- abstract set(name: string, data: any, duration?: number): Promise<void>
9
-
10
- abstract delete(name: string): Promise<void>
11
-
12
- abstract flush(): Promise<void>
13
- }
@@ -1,120 +0,0 @@
1
- import { Cache } from '@Typetron/Cache'
2
- import { Query } from '@Typetron/Database'
3
- import { JSONSafeParse } from '@Typetron/Support/utils'
4
- import { CacheItem } from '@Typetron/Cache/types'
5
- import { createHash } from 'crypto'
6
- import { Create } from '@Typetron/Database/Drivers/SQLite/Create'
7
-
8
- export class DatabaseStore extends Cache {
9
- constructor(public table: string) {
10
- super()
11
- }
12
-
13
- private get cacheTable() {
14
- return new Query<CacheItem<unknown>>().table(this.table)
15
- }
16
-
17
- private getNameHash(name: string) {
18
- return createHash('sha1').update(name).digest('hex')
19
- }
20
-
21
- async has(name: string): Promise<boolean> {
22
- const row = await this.cacheTable.where('name', this.getNameHash(name)).first()
23
- return !!row
24
- }
25
-
26
- async get<T>(name: string, defaultValue?: T | (() => T | Promise<T>)): Promise<T | undefined> {
27
- if (!await Query.connection.driver.tableExists(this.table)) {
28
- return undefined
29
- }
30
-
31
- const row = await this.cacheTable.where('name', this.getNameHash(name)).first()
32
-
33
- if (!row) {
34
- return this.getDefaultValue(defaultValue)
35
- }
36
-
37
- const currentTime = Date.now()
38
-
39
- if (row.date && row.date < currentTime) {
40
- await this.delete(name)
41
- return this.getDefaultValue(defaultValue)
42
- }
43
-
44
- return JSONSafeParse<T>(String(row.value))
45
- }
46
-
47
- async remember<T>(name: string, defaultValue?: T | (() => T | Promise<T>), durationInSeconds?: number): Promise<T | undefined> {
48
- const value = await this.get<T>(name)
49
-
50
- if (!value) {
51
- const newValue = await this.getDefaultValue(defaultValue)
52
- await this.set(name, newValue, durationInSeconds)
53
- return newValue
54
- }
55
-
56
- return value
57
- }
58
-
59
- async set(name: string, value: any, durationInSeconds?: number): Promise<void> {
60
- await this.makeSureTheTableExists()
61
- const expirationTime = durationInSeconds ? Date.now() + durationInSeconds * 1000 : undefined
62
- const serializedValue = JSON.stringify(value)
63
-
64
- const existing = await this.cacheTable.where('name', this.getNameHash(name)).first()
65
-
66
- if (existing) {
67
- await this.cacheTable.where('name', this.getNameHash(name)).update({
68
- value: serializedValue,
69
- date: expirationTime
70
- })
71
- } else {
72
- await this.cacheTable.insert({
73
- name: this.getNameHash(name),
74
- value: serializedValue,
75
- date: expirationTime
76
- })
77
- }
78
- }
79
-
80
- async delete(name: string): Promise<void> {
81
- await this.cacheTable.where('name', this.getNameHash(name)).delete()
82
- }
83
-
84
- async flush(): Promise<void> {
85
- await this.cacheTable.delete()
86
- }
87
-
88
- private async getDefaultValue<T>(defaultValue?: (() => (Promise<T> | T)) | T) {
89
- const valueFunction = typeof defaultValue === 'function'
90
- ? defaultValue as () => Promise<T> | T
91
- : () => defaultValue
92
-
93
- return valueFunction()
94
- }
95
-
96
- private async makeSureTheTableExists() {
97
- if (!await Query.connection.driver.tableExists(this.table)) {
98
- const createStatement = new Create(this.table, [
99
- {
100
- name: 'name',
101
- type: 'string',
102
- primaryKey: true,
103
- nullable: true,
104
- },
105
- {
106
- name: 'value',
107
- type: 'string',
108
- nullable: true,
109
- },
110
- {
111
- name: 'date',
112
- type: 'integer',
113
- nullable: true,
114
- },
115
- ])
116
-
117
- await Query.connection.driver.run(createStatement.toSQL())
118
- }
119
- }
120
- }
@@ -1,69 +0,0 @@
1
- import { Cache } from '@Typetron/Cache'
2
- import { Storage } from '@Typetron/Storage'
3
- import { createHash } from 'node:crypto'
4
- import path from 'node:path'
5
- import { JSONSafeParse } from '@Typetron/Support/utils'
6
- import { CacheItem } from '@Typetron/Cache/types'
7
-
8
- export class FileStore extends Cache {
9
- constructor(public storage: Storage, public directory: string) {
10
- super()
11
- }
12
-
13
- async has(name: string): Promise<boolean> {
14
- return await this.storage.exists(this.path(name))
15
- }
16
-
17
- async get<T>(name: string, defaultValue?: T | (() => T | Promise<T>)): Promise<T | undefined> {
18
- const item: CacheItem<T> = await this.storage.read(this.path(name))
19
- .then(async (data) => JSONSafeParse<CacheItem<T>>(data.toString()) ?? {value: await this.getDefaultValue<T>(defaultValue)})
20
- .catch(async () => ({value: await this.getDefaultValue<T>(defaultValue)}))
21
-
22
- if (item['date'] && item['date'] < new Date().getTime()) {
23
- await this.delete(name)
24
- return this.getDefaultValue(defaultValue)
25
- }
26
-
27
- return item['value']
28
- }
29
-
30
- async remember<T>(name: string, defaultValue?: T | (() => T | Promise<T>), durationInSeconds?: number): Promise<T | undefined> {
31
- const data = await this.get<T>(name)
32
-
33
- if (data !== undefined) {
34
- return data
35
- }
36
-
37
- const value = await this.getDefaultValue(defaultValue)
38
- await this.set(name, value, durationInSeconds)
39
- return value
40
- }
41
-
42
- async set(name: string, value: any, durationInSeconds?: number): Promise<void> {
43
- await this.storage.put(this.path(name), JSON.stringify({
44
- value,
45
- date: durationInSeconds ? Date.now() + durationInSeconds * 1000 : undefined
46
- } as CacheItem<unknown>))
47
- }
48
-
49
- async delete(name: string): Promise<void> {
50
- await this.storage.delete(this.path(name))
51
- }
52
-
53
- async flush(): Promise<void> {
54
- await this.storage.deleteDirectory(this.directory)
55
- }
56
-
57
- private path(key: string): string {
58
- const hashKey = createHash('sha1').update(key).digest('hex')
59
- return path.join(this.directory, '/', hashKey)
60
- }
61
-
62
- private async getDefaultValue<T>(defaultValue?: (() => (Promise<T> | T)) | T) {
63
- const valueFunction = typeof defaultValue === 'function'
64
- ? defaultValue as () => Promise<T> | T
65
- : () => defaultValue
66
-
67
- return valueFunction()
68
- }
69
- }
@@ -1,71 +0,0 @@
1
- import { Cache } from '@Typetron/Cache'
2
- import { CacheItem } from '@Typetron/Cache/types'
3
-
4
- export class MemoryStore extends Cache {
5
- public cache: Map<string, CacheItem<unknown>> = new Map()
6
-
7
- async has(name: string): Promise<boolean> {
8
- const cacheItem = this.cache.get(name)
9
-
10
- if (!cacheItem) {
11
- return false
12
- }
13
-
14
- const currentTime = Date.now()
15
-
16
- if (cacheItem.date && cacheItem.date < currentTime) {
17
- this.cache.delete(name)
18
- return false
19
- }
20
-
21
- return true
22
- }
23
-
24
- async get<T>(name: string, defaultValue?: T | (() => T | Promise<T>)): Promise<T | undefined> {
25
- const cacheItem = this.cache.get(name)
26
-
27
- if (!await this.has(name)) {
28
- return this.getDefaultValue(defaultValue)
29
- }
30
-
31
- return cacheItem!.value as T
32
- }
33
-
34
- // return (cacheItem as CacheItem<T>).value as T
35
- async remember<T>(name: string, defaultValue?: T | (() => T | Promise<T>), durationInSeconds?: number): Promise<T | undefined> {
36
- const cacheItem = this.cache.get(name)
37
-
38
- if (await this.has(name)) {
39
- return cacheItem!.value as T
40
- }
41
-
42
- const value = await this.getDefaultValue(defaultValue)
43
- await this.set(name, value, durationInSeconds)
44
- return value
45
- }
46
-
47
- async set(name: string, value: any, durationInSeconds?: number): Promise<void> {
48
- const expirationTime = durationInSeconds ? Date.now() + durationInSeconds * 1000 : Infinity
49
-
50
- this.cache.set(name, {
51
- value: value,
52
- date: expirationTime
53
- })
54
- }
55
-
56
- async delete(name: string): Promise<void> {
57
- this.cache.delete(name)
58
- }
59
-
60
- async flush(): Promise<void> {
61
- this.cache.clear()
62
- }
63
-
64
- private async getDefaultValue<T>(defaultValue?: (() => (Promise<T> | T)) | T) {
65
- const valueFunction = typeof defaultValue === 'function'
66
- ? defaultValue as () => Promise<T> | T
67
- : () => defaultValue
68
-
69
- return valueFunction()
70
- }
71
- }
package/Cache/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from './types'
2
- export * from './Cache'
3
- export * from './Stores/MemoryStore'
4
- export * from './Stores/FileStore'
5
- export * from './Stores/DatabaseStore'
package/Cache/types.ts DELETED
@@ -1,4 +0,0 @@
1
- export interface CacheItem<T>{
2
- value?: T
3
- date?: number
4
- }
@@ -1,106 +0,0 @@
1
- import { Constructor } from '../Support'
2
- import { BaseResolver } from './Resolver'
3
- import { InjectableMetadata, Scope } from './Metadata'
4
- import { Container } from './Container'
5
- import { ParametersTypesMetadata } from '@Typetron/Support/Metadata'
6
-
7
- export class ClassResolver extends BaseResolver {
8
- canResolve<T>(abstract: Constructor<T>): boolean {
9
- return abstract.prototype && abstract.prototype.constructor.name
10
- }
11
-
12
- resolve<T>(abstract: Constructor<T>, parametersValues: object[]) {
13
-
14
- // TODO improve this since it looks very hard to write next time
15
- const parametersTypes: Constructor[] = ParametersTypesMetadata.get(abstract).parameters
16
- ?? (ParametersTypesMetadata.get(abstract).parameters = Reflect.getMetadata('design:paramtypes', abstract) ?? [])
17
-
18
- const parameters = parametersTypes.map((parameterType, index) => {
19
- if (parametersValues[index]) {
20
- return parametersValues[index]
21
- }
22
- if (parameterType.name === 'Object' && parametersValues[index]) {
23
- parameterType = parametersValues[index] as Constructor // || this.getParameterValue(abstract, index);
24
- }
25
- return this.container.get(parameterType)
26
- })
27
-
28
- const instance = new abstract(...parameters)
29
-
30
- const metadata = InjectableMetadata.get(abstract)
31
- let asyncDependencies = 0
32
- let resolve: (value: T | PromiseLike<T>) => void
33
- let reject: (reason?: unknown) => void
34
- for (const dependency in metadata.dependencies) {
35
- const dependencyAbstract = metadata.dependencies[dependency]
36
- if (!dependencyAbstract) {
37
- continue
38
- }
39
- const value = this.container.get(dependencyAbstract) as T[keyof T]
40
- if (value instanceof Promise) {
41
- asyncDependencies++
42
- value
43
- .then((resolvedValue) => {
44
- instance[dependency as keyof T] = resolvedValue
45
- asyncDependencies--
46
- if (!asyncDependencies) {
47
- resolve(instance)
48
- }
49
- })
50
- .catch((error) => {
51
- reject(error)
52
- })
53
- } else {
54
- instance[dependency as keyof T] = value
55
- }
56
- }
57
- if (asyncDependencies) {
58
- return new Promise<T>((resolveFunction, rejectFunction) => {
59
- resolve = resolveFunction
60
- reject = rejectFunction
61
- })
62
- }
63
-
64
- return instance
65
- }
66
-
67
- // TODO figure out how to use this better
68
- // private getParameterValue<T>(abstract: Constructor<T>, index: number) {
69
- // const injectMetadata = Reflect.getMetadata('container:inject', abstract);
70
- // if (!injectMetadata) {
71
- // throw new Error(`No binding found for parameter ${index} in '${abstract.name}'`);
72
- // }
73
- // const value = injectMetadata.find((metadata: any) => metadata.index === index);
74
- // return value.abstract;
75
- // }
76
-
77
- reload<T>(
78
- abstract: Constructor<T>,
79
- concrete: T,
80
- container: Container = this.container
81
- ): T {
82
- const metadata = InjectableMetadata.get(abstract)
83
- for (const dependencyName in metadata.dependencies) {
84
- if (!metadata.dependencies[dependencyName]) {
85
- continue
86
- }
87
- const dependency = metadata.dependencies[dependencyName]
88
- if (!dependency) {
89
- continue
90
- }
91
- if (typeof dependency === 'symbol') {
92
- concrete[dependencyName as keyof T] = container.get(
93
- dependency
94
- ) as T[keyof T]
95
- continue
96
- }
97
- const dependencyMetadata = InjectableMetadata.get(dependency)
98
- if (dependencyMetadata.scope === Scope.REQUEST) {
99
- concrete[dependencyName as keyof T] = container.get(
100
- dependency
101
- ) as T[keyof T]
102
- }
103
- }
104
- return concrete
105
- }
106
- }