evaengine 1.0.14 → 2.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 (199) hide show
  1. package/README.md +15 -1
  2. package/bin/engine +2 -31
  3. package/dist/bin.d.ts +1 -0
  4. package/dist/bin.js +40 -0
  5. package/dist/bin.js.map +1 -0
  6. package/{src/commands/index.js → dist/commands/index.d.ts} +1 -8
  7. package/dist/commands/index.js +6 -0
  8. package/dist/commands/index.js.map +1 -0
  9. package/dist/commands/interface.d.ts +18 -0
  10. package/dist/commands/interface.js +40 -0
  11. package/dist/commands/interface.js.map +1 -0
  12. package/dist/commands/make_entity.d.ts +47 -0
  13. package/dist/commands/make_entity.js +410 -0
  14. package/dist/commands/make_entity.js.map +1 -0
  15. package/dist/commands/tramp.d.ts +7 -0
  16. package/dist/commands/tramp.js +29 -0
  17. package/dist/commands/tramp.js.map +1 -0
  18. package/dist/config/index.d.ts +92 -0
  19. package/dist/config/index.js +96 -0
  20. package/dist/config/index.js.map +1 -0
  21. package/dist/di.d.ts +73 -0
  22. package/dist/di.js +76 -0
  23. package/dist/di.js.map +1 -0
  24. package/dist/engine.d.ts +85 -0
  25. package/dist/engine.js +404 -0
  26. package/dist/engine.js.map +1 -0
  27. package/dist/entities/index.d.ts +54 -0
  28. package/dist/entities/index.js +228 -0
  29. package/dist/entities/index.js.map +1 -0
  30. package/dist/exceptions/index.d.ts +153 -0
  31. package/dist/exceptions/index.js +439 -0
  32. package/dist/exceptions/index.js.map +1 -0
  33. package/dist/index.d.ts +45 -0
  34. package/dist/index.js +45 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/middlewares/auth.d.ts +5 -0
  37. package/dist/middlewares/auth.js +63 -0
  38. package/dist/middlewares/auth.js.map +1 -0
  39. package/dist/middlewares/auth_kong.d.ts +2 -0
  40. package/dist/middlewares/auth_kong.js +24 -0
  41. package/dist/middlewares/auth_kong.js.map +1 -0
  42. package/dist/middlewares/debug.d.ts +3 -0
  43. package/dist/middlewares/debug.js +50 -0
  44. package/dist/middlewares/debug.js.map +1 -0
  45. package/{src/middlewares/index.js → dist/middlewares/index.d.ts} +1 -11
  46. package/dist/middlewares/index.js +9 -0
  47. package/dist/middlewares/index.js.map +1 -0
  48. package/dist/middlewares/providers.d.ts +25 -0
  49. package/dist/middlewares/providers.js +63 -0
  50. package/dist/middlewares/providers.js.map +1 -0
  51. package/dist/middlewares/session.d.ts +6 -0
  52. package/dist/middlewares/session.js +51 -0
  53. package/dist/middlewares/session.js.map +1 -0
  54. package/dist/middlewares/trace.d.ts +52 -0
  55. package/dist/middlewares/trace.js +225 -0
  56. package/dist/middlewares/trace.js.map +1 -0
  57. package/dist/middlewares/validator.d.ts +10 -0
  58. package/dist/middlewares/validator.js +43 -0
  59. package/dist/middlewares/validator.js.map +1 -0
  60. package/dist/middlewares/view_cache.d.ts +14 -0
  61. package/dist/middlewares/view_cache.js +102 -0
  62. package/dist/middlewares/view_cache.js.map +1 -0
  63. package/dist/services/cache.d.ts +66 -0
  64. package/dist/services/cache.js +207 -0
  65. package/dist/services/cache.js.map +1 -0
  66. package/dist/services/config.d.ts +25 -0
  67. package/dist/services/config.js +88 -0
  68. package/dist/services/config.js.map +1 -0
  69. package/dist/services/env.d.ts +12 -0
  70. package/dist/services/env.js +24 -0
  71. package/dist/services/env.js.map +1 -0
  72. package/dist/services/event_manager.d.ts +29 -0
  73. package/dist/services/event_manager.js +75 -0
  74. package/dist/services/event_manager.js.map +1 -0
  75. package/dist/services/http_client.d.ts +66 -0
  76. package/dist/services/http_client.js +62 -0
  77. package/dist/services/http_client.js.map +1 -0
  78. package/{src/services/index.js → dist/services/index.d.ts} +1 -15
  79. package/dist/services/index.js +14 -0
  80. package/dist/services/index.js.map +1 -0
  81. package/{src/services/interface.js → dist/services/interface.d.ts} +1 -3
  82. package/dist/services/interface.js +6 -0
  83. package/dist/services/interface.js.map +1 -0
  84. package/{src/services/joi.js → dist/services/joi.d.ts} +2 -9
  85. package/dist/services/joi.js +11 -0
  86. package/dist/services/joi.js.map +1 -0
  87. package/dist/services/jwt_token.d.ts +24 -0
  88. package/dist/services/jwt_token.js +77 -0
  89. package/dist/services/jwt_token.js.map +1 -0
  90. package/dist/services/jwt_token_kong.d.ts +25 -0
  91. package/dist/services/jwt_token_kong.js +65 -0
  92. package/dist/services/jwt_token_kong.js.map +1 -0
  93. package/dist/services/logger.d.ts +30 -0
  94. package/dist/services/logger.js +126 -0
  95. package/dist/services/logger.js.map +1 -0
  96. package/dist/services/namespace.d.ts +55 -0
  97. package/dist/services/namespace.js +157 -0
  98. package/dist/services/namespace.js.map +1 -0
  99. package/dist/services/now.d.ts +14 -0
  100. package/dist/services/now.js +43 -0
  101. package/dist/services/now.js.map +1 -0
  102. package/dist/services/providers.d.ts +55 -0
  103. package/dist/services/providers.js +134 -0
  104. package/dist/services/providers.js.map +1 -0
  105. package/dist/services/redis.d.ts +18 -0
  106. package/dist/services/redis.js +53 -0
  107. package/dist/services/redis.js.map +1 -0
  108. package/dist/services/rest_client.d.ts +48 -0
  109. package/dist/services/rest_client.js +96 -0
  110. package/dist/services/rest_client.js.map +1 -0
  111. package/dist/swagger/index.d.ts +223 -0
  112. package/dist/swagger/index.js +564 -0
  113. package/dist/swagger/index.js.map +1 -0
  114. package/dist/types/express.d.ts +14 -0
  115. package/dist/types/vendor.d.ts +102 -0
  116. package/dist/utils/api_scaffold.d.ts +74 -0
  117. package/dist/utils/api_scaffold.js +220 -0
  118. package/dist/utils/api_scaffold.js.map +1 -0
  119. package/dist/utils/case_converter.d.ts +7 -0
  120. package/dist/utils/case_converter.js +29 -0
  121. package/dist/utils/case_converter.js.map +1 -0
  122. package/dist/utils/crc32.d.ts +2 -0
  123. package/dist/utils/crc32.js +43 -0
  124. package/dist/utils/crc32.js.map +1 -0
  125. package/dist/utils/cron.d.ts +27 -0
  126. package/dist/utils/cron.js +186 -0
  127. package/dist/utils/cron.js.map +1 -0
  128. package/dist/utils/datetime.d.ts +4 -0
  129. package/dist/utils/datetime.js +6 -0
  130. package/dist/utils/datetime.js.map +1 -0
  131. package/dist/utils/host.d.ts +18 -0
  132. package/dist/utils/host.js +33 -0
  133. package/dist/utils/host.js.map +1 -0
  134. package/{src/utils/index.js → dist/utils/index.d.ts} +2 -26
  135. package/dist/utils/index.js +12 -0
  136. package/dist/utils/index.js.map +1 -0
  137. package/dist/utils/pagination.d.ts +150 -0
  138. package/dist/utils/pagination.js +242 -0
  139. package/dist/utils/pagination.js.map +1 -0
  140. package/dist/utils/random.d.ts +3 -0
  141. package/dist/utils/random.js +18 -0
  142. package/dist/utils/random.js.map +1 -0
  143. package/dist/utils/request_client.d.ts +33 -0
  144. package/dist/utils/request_client.js +153 -0
  145. package/dist/utils/request_client.js.map +1 -0
  146. package/dist/utils/smart_query.d.ts +144 -0
  147. package/dist/utils/smart_query.js +310 -0
  148. package/dist/utils/smart_query.js.map +1 -0
  149. package/dist/utils/test.d.ts +14 -0
  150. package/dist/utils/test.js +37 -0
  151. package/dist/utils/test.js.map +1 -0
  152. package/dist/utils/wrapper.d.ts +3 -0
  153. package/dist/utils/wrapper.js +5 -0
  154. package/dist/utils/wrapper.js.map +1 -0
  155. package/package.json +24 -10
  156. package/index.js +0 -2
  157. package/src/commands/interface.js +0 -45
  158. package/src/commands/make_entity.js +0 -490
  159. package/src/commands/tramp.js +0 -34
  160. package/src/config/index.js +0 -95
  161. package/src/di.js +0 -96
  162. package/src/engine.js +0 -556
  163. package/src/entities/index.js +0 -272
  164. package/src/exceptions/index.js +0 -501
  165. package/src/index.js +0 -55
  166. package/src/middlewares/auth.js +0 -67
  167. package/src/middlewares/auth_kong.js +0 -28
  168. package/src/middlewares/debug.js +0 -88
  169. package/src/middlewares/providers.js +0 -73
  170. package/src/middlewares/session.js +0 -63
  171. package/src/middlewares/trace.js +0 -254
  172. package/src/middlewares/validator.js +0 -50
  173. package/src/middlewares/view_cache.js +0 -127
  174. package/src/services/cache.js +0 -223
  175. package/src/services/config.js +0 -115
  176. package/src/services/env.js +0 -28
  177. package/src/services/event_manager.js +0 -84
  178. package/src/services/http_client.js +0 -69
  179. package/src/services/jwt_token.js +0 -89
  180. package/src/services/jwt_token_kong.js +0 -75
  181. package/src/services/logger.js +0 -150
  182. package/src/services/namespace.js +0 -176
  183. package/src/services/now.js +0 -49
  184. package/src/services/providers.js +0 -158
  185. package/src/services/redis.js +0 -63
  186. package/src/services/rest_client.js +0 -112
  187. package/src/swagger/index.js +0 -665
  188. package/src/utils/api_scaffold.js +0 -240
  189. package/src/utils/case_converter.js +0 -33
  190. package/src/utils/crc32.js +0 -44
  191. package/src/utils/cron.js +0 -208
  192. package/src/utils/datetime.js +0 -13
  193. package/src/utils/host.js +0 -39
  194. package/src/utils/pagination.js +0 -260
  195. package/src/utils/random.js +0 -23
  196. package/src/utils/request_client.js +0 -179
  197. package/src/utils/smart_query.js +0 -352
  198. package/src/utils/test.js +0 -48
  199. package/src/utils/wrapper.js +0 -8
@@ -1,223 +0,0 @@
1
- import constitute from 'constitute';
2
- import { OperationUnsupportedException } from './../exceptions/index.js';
3
- import Redis from './redis.js';
4
- import DI from './../di.js';
5
- import Config from './config.js';
6
- import ServiceInterface from './interface.js';
7
-
8
- export class Store {
9
- getInstance() {
10
- return this;
11
- }
12
-
13
- has() {
14
- return Promise.resolve(false);
15
- }
16
-
17
- get() {
18
- return Promise.resolve(null);
19
- }
20
-
21
- set() {
22
- return Promise.resolve(this);
23
- }
24
-
25
- del() {
26
- return Promise.resolve(this);
27
- }
28
-
29
- flush() {
30
- return Promise.resolve(true);
31
- }
32
-
33
- tags() {
34
- throw new OperationUnsupportedException('Not support tag feature');
35
- }
36
- }
37
-
38
- export class NullStore extends Store {
39
- namespace() {
40
- return this;
41
- }
42
-
43
- tags() {
44
- return this;
45
- }
46
- }
47
-
48
- export class RedisNamespaceStore extends Store {
49
- constructor(prefix, namespace, redis) {
50
- super();
51
- this.prefix = prefix;
52
- this.namespace = namespace;
53
- this.redis = redis;
54
- }
55
-
56
- getInstance() {
57
- return this.redis;
58
- }
59
-
60
- has(key) {
61
- return this.redis.exists(this.key(key)).then(res => res > 0);
62
- }
63
-
64
- key(key) {
65
- return this.prefix ? [this.prefix, this.namespace, key].join(':') : key;
66
- }
67
-
68
- get(key) {
69
- return this.redis.get(this.key(key)).then(res => JSON.parse(res));
70
- }
71
-
72
- del(key) {
73
- return this.redis.del(this.key(key));
74
- }
75
-
76
- set(key, value, minutes, mutex) {
77
- const args = [
78
- this.key(key),
79
- JSON.stringify(value)
80
- ];
81
- if (minutes) {
82
- args.push('ex', minutes * 60);
83
- }
84
- if (mutex) {
85
- const m = mutex.toUpperCase();
86
- if (m === 'NX' || m === 'XX') {
87
- args.push(m);
88
- }
89
- }
90
- return this.redis.set(...args);
91
- }
92
-
93
- flush() {
94
- return this.redis.keys([this.prefix, this.namespace, '*'].join(':'))
95
- .then((keys) => {
96
- if (keys.length) {
97
- return this.redis.del(keys);
98
- }
99
- return 0;
100
- });
101
- }
102
- }
103
-
104
- export class RedisStore extends Store {
105
- constructor(prefix, redis) {
106
- super();
107
- this.prefix = prefix;
108
- this.namespaceHandler = {};
109
- this.redis = redis || DI.get('redis').getInstance();
110
- }
111
-
112
- getInstance() {
113
- return this.redis;
114
- }
115
-
116
- namespace(namespace) {
117
- if ({}.hasOwnProperty.call(this.namespaceHandler, namespace)) {
118
- return this.namespaceHandler[namespace];
119
- }
120
- this.namespaceHandler[namespace] = new RedisNamespaceStore(this.prefix, namespace, this.redis);
121
- return this.namespaceHandler[namespace];
122
- }
123
-
124
- has(key) {
125
- return this.redis.exists(this.key(key));
126
- }
127
-
128
- key(key) {
129
- return this.prefix ? [this.prefix, key].join(':') : key;
130
- }
131
-
132
- get(key) {
133
- return this.redis.get(this.key(key)).then(res => JSON.parse(res));
134
- }
135
-
136
- del(key) {
137
- return this.redis.del(this.key(key));
138
- }
139
-
140
- set(key, value, minutes, mutex) {
141
- const args = [
142
- this.key(key),
143
- JSON.stringify(value)
144
- ];
145
- if (minutes) {
146
- args.push('ex', minutes * 60);
147
- }
148
- if (mutex) {
149
- const m = mutex.toUpperCase();
150
- if (m === 'NX' || m === 'XX') {
151
- args.push(m);
152
- }
153
- }
154
- return this.redis.set(...args);
155
- }
156
-
157
- flush() {
158
- return this.redis.flushall();
159
- }
160
- }
161
-
162
- class Cache extends ServiceInterface {
163
- constructor(config) {
164
- super();
165
- this.config = config.get('cache');
166
- this.prefix = this.config.prefix || 'eva';
167
- this.driver = this.config.driver;
168
- this.store = null;
169
- }
170
-
171
- setStore(store) {
172
- this.store = store;
173
- return this;
174
- }
175
-
176
- /**
177
- * @returns {Store}
178
- */
179
- getStore() {
180
- if (this.store) {
181
- return this.store;
182
- }
183
- this.store = this.driver === 'redis' ?
184
- new RedisStore(this.prefix) : new NullStore(this.prefix);
185
- return this.store;
186
- }
187
-
188
- setPrefix(prefix) {
189
- this.prefix = prefix;
190
- }
191
-
192
- getPrefix() {
193
- return this.prefix;
194
- }
195
-
196
- namespace(...args) {
197
- return this.getStore().namespace(...args);
198
- }
199
-
200
- has(key) {
201
- return this.getStore().has(key);
202
- }
203
-
204
- get(key) {
205
- return this.getStore().get(key);
206
- }
207
-
208
- set(...args) {
209
- return this.getStore().set(...args);
210
- }
211
-
212
- del(...args) {
213
- return this.getStore().del(...args);
214
- }
215
-
216
- flush() {
217
- return this.getStore().flush();
218
- }
219
- }
220
-
221
- constitute.Dependencies(Config, Redis)(Cache);
222
- export default Cache;
223
-
@@ -1,115 +0,0 @@
1
- import get from 'lodash/get.js';
2
- import isString from 'lodash/isString.js';
3
- import merge from 'lodash/merge.js';
4
- import set from 'lodash/set.js';
5
- import { createRequire } from 'module';
6
- import constitute from 'constitute';
7
- import * as springConfigClient from 'cloud-config-client';
8
- import Env from './env.js';
9
- import EngineConfig from '../config/index.js';
10
- import ServiceInterface from './interface.js';
11
-
12
- const require = createRequire(import.meta.url);
13
-
14
- class Config extends ServiceInterface {
15
- /**
16
- * @param env {Env}
17
- */
18
- constructor(env) {
19
- super();
20
- this.env = env;
21
- this.path = null;
22
- this.mergedFiles = [];
23
- this.config = null;
24
- }
25
-
26
- setPath(path) {
27
- this.path = path;
28
- return this;
29
- }
30
-
31
- /**
32
- * Resolve configurations from Spring Cloud Config Server.
33
- * @param endpoint
34
- * @param name
35
- * @param profiles
36
- * @param label
37
- * @returns {Promise.<void>}
38
- */
39
- async resolveSpringConfig({
40
- endpoint,
41
- name,
42
- profiles,
43
- label = 'main'
44
- }) {
45
- if (!this.config) {
46
- this.config = this.loadConfigFromFiles();
47
- }
48
- const configRemote = await springConfigClient.load({
49
- endpoint,
50
- name,
51
- profiles: isString(profiles) ? profiles.split(',') : [],
52
- label
53
- });
54
- configRemote.forEach((key, value) => {
55
- set(this.config, key, value);
56
- });
57
- }
58
-
59
- get(key) {
60
- if (this.config) {
61
- return key ? Config.search(key, this.config) : this.config;
62
- }
63
- this.config = this.loadConfigFromFiles();
64
- return key ? Config.search(key, this.config) : this.config;
65
- }
66
-
67
- loadConfigFromFiles() {
68
- const env = this.env.get();
69
- const configPath = this.path;
70
- const pathDefault = `${configPath}/config.default.cjs`;
71
- const pathEnv = `${configPath}/config.${env}.cjs`;
72
- const pathLocal = `${configPath}/config.local.${env}.cjs`;
73
-
74
-
75
- const configDefault = require(pathDefault);
76
- this.mergedFiles.push(pathDefault);
77
- const configEnv = require(pathEnv);
78
- this.mergedFiles.push(pathEnv);
79
- let configLocal = {};
80
- try {
81
- configLocal = require(pathLocal);
82
- this.mergedFiles.push(pathLocal);
83
- } catch {
84
- configLocal = {};
85
- }
86
-
87
-
88
- return merge({}, EngineConfig, configDefault, configEnv, configLocal);
89
- }
90
-
91
- getMergedFiles() {
92
- return this.mergedFiles;
93
- }
94
-
95
- reload() {
96
- this.config = null;
97
- this.mergedFiles = [];
98
- }
99
-
100
- /**
101
- * @param {string} keyString
102
- * @param {Object} target
103
- * @returns {*}
104
- */
105
- static search(keyString, target) {
106
- if (typeof keyString !== 'string') {
107
- return target;
108
- }
109
- return get(target, keyString);
110
- }
111
- }
112
-
113
- constitute.Dependencies(Env)(Config);
114
- export default Config;
115
-
@@ -1,28 +0,0 @@
1
- import ServiceInterface from './interface.js';
2
-
3
- let env = null;
4
- export const TYPES = {
5
- PRODUCTION: 'production',
6
- TEST: 'test',
7
- DEVELOPMENT: 'development'
8
- };
9
-
10
- export default class Env extends ServiceInterface {
11
- get() {
12
- env = [TYPES.PRODUCTION, TYPES.TEST, TYPES.DEVELOPMENT].indexOf(process.env.NODE_ENV) > -1
13
- ? process.env.NODE_ENV : TYPES.DEVELOPMENT;
14
- return env;
15
- }
16
-
17
- isTest() {
18
- return this.get() === TYPES.TEST;
19
- }
20
-
21
- isProduction() {
22
- return this.get() === TYPES.PRODUCTION;
23
- }
24
-
25
- isDevelopment() {
26
- return this.get() === TYPES.DEVELOPMENT;
27
- }
28
- }
@@ -1,84 +0,0 @@
1
- import constitute from 'constitute';
2
- import EventEmitter from 'events';
3
- import camelCase from 'lodash/camelCase.js';
4
- import { RuntimeException } from '../exceptions/index.js';
5
- import Logger from './logger.js';
6
- import ServiceInterface from './interface.js';
7
-
8
- /**
9
- * A strict version of EventEmitter
10
- * 1. listener must be a standard class
11
- * 2. unregistered event not allow to emit
12
- * 3. all events register and trigger will be recorded
13
- */
14
- class EventManager extends ServiceInterface {
15
- constructor(logger) {
16
- super();
17
- this.logger = logger;
18
- this.emitter = new EventEmitter();
19
- this.events = new Set();
20
- }
21
-
22
- getProto() {
23
- return EventEmitter;
24
- }
25
-
26
- getAllowEvents() {
27
- return this.events;
28
- }
29
-
30
- getEmitter() {
31
- //TODO: 屏蔽写入
32
- return this.emitter;
33
- }
34
-
35
- /**
36
- * Allow to register a class such like
37
- * class Foo { get prefix(){ return 'foo'} get actions() { return ['bar']} }
38
- * @param ListenerClass
39
- * @returns {EventManager}
40
- */
41
- addListener(ListenerClass) {
42
- if (typeof ListenerClass !== 'function') {
43
- throw new RuntimeException('Not a standard listener input');
44
- }
45
- const listener = new ListenerClass();
46
- const { prefix, actions } = listener;
47
- if (!prefix || !actions) {
48
- throw new RuntimeException('Not a standard listener input');
49
- }
50
-
51
- const events = {};
52
- const eventsCount = this.events.size;
53
- for (const action of actions) {
54
- events[[prefix, action, 'before'].join(':')] = camelCase(['before', action].join('_'));
55
- events[[prefix, action, 'after'].join(':')] = camelCase(['after', action].join('_'));
56
- }
57
-
58
- //TODO: 用Set保存注册的事件
59
- for (const [eventName, callback] of Object.entries(events)) {
60
- this.events.add(eventName);
61
- if (Reflect.has(listener, callback) === false) {
62
- continue;
63
- }
64
- this.emitter.addListener(eventName, listener[callback]);
65
- }
66
-
67
- if (eventsCount + Object.keys(events).length !== this.events.size) {
68
- throw new RuntimeException('Repeated event name has been registered, please check');
69
- }
70
-
71
- // this.logger.debug('Registered events', this.emitter.eventNames());
72
- return this;
73
- }
74
-
75
- emit(eventName, callback) {
76
- if (!this.events.has(eventName)) {
77
- throw new RuntimeException(`Event ${eventName} not registered yet`);
78
- }
79
- return this.emitter.emit(eventName, callback);
80
- }
81
- }
82
-
83
- constitute.Dependencies(Logger)(EventManager);
84
- export default EventManager;
@@ -1,69 +0,0 @@
1
- import constitute from 'constitute';
2
- import Config from './config.js';
3
- import Logger from './logger.js';
4
- import { createRequestClient } from '../utils/request_client.js';
5
- import { HttpRequestLogicException, HttpRequestIOException } from '../exceptions/index.js';
6
- import ServiceInterface from './interface.js';
7
-
8
- class HttpClient extends ServiceInterface {
9
- /**
10
- * @param config {Config}
11
- * @param logger {Logger}
12
- */
13
- constructor(config, logger) {
14
- super();
15
- this.config = config.get();
16
- this.client = createRequestClient(logger);
17
- }
18
-
19
- getProto() {
20
- return this.client;
21
- }
22
-
23
- getInstance() {
24
- return this.client;
25
- }
26
-
27
- async request(params) {
28
- try {
29
- return await this.client(params);
30
- } catch (e) {
31
- const { statusCode } = e;
32
- if (statusCode && statusCode >= 400 && statusCode < 500) {
33
- throw new HttpRequestLogicException(e);
34
- }
35
- throw new HttpRequestIOException(e);
36
- }
37
- }
38
-
39
- dumpRequest(req, asString = false) {
40
- const getBody = (r) => {
41
- if (r._json) {
42
- return r.body;
43
- }
44
- return r.form(r.formData).body;
45
- };
46
- const dump = {
47
- method: req.method,
48
- protocol: req.uri && req.uri.protocol === 'https:' ? 'https' : 'http',
49
- url: req.uri ? req.uri.href : null,
50
- headers: req.headers,
51
- body: req.req && (req._json || req.formData)
52
- ? getBody(req) : null
53
- };
54
- return asString === true ? JSON.stringify(dump) : dump;
55
- }
56
-
57
- dumpResponse(res, asString = false) {
58
- const dump = {
59
- statusCode: res.statusCode,
60
- statusMessage: res.statusMessage,
61
- headers: res.headers,
62
- body: res.body
63
- };
64
- return asString === true ? JSON.stringify(dump) : dump;
65
- }
66
- }
67
-
68
- constitute.Dependencies(Config, Logger)(HttpClient);
69
- export default HttpClient;
@@ -1,89 +0,0 @@
1
- import constitute from 'constitute';
2
- import jwt from 'jwt-simple';
3
- import Config from './config.js';
4
- import Redis from './redis.js';
5
- import ServiceInterface from './interface.js';
6
-
7
- class JsonWebToken extends ServiceInterface {
8
- /**
9
- * @param config {Config}
10
- * @param redis {Redis}
11
- */
12
- constructor(config, redis) {
13
- super();
14
- this.redis = redis.getInstance();
15
- this.config = config.get('token');
16
- }
17
-
18
- getProto() {
19
- return jwt;
20
- }
21
-
22
- getRedis() {
23
- return this.redis;
24
- }
25
-
26
- getPrefix() {
27
- return this.config.prefix;
28
- }
29
-
30
- async save(uid, item) {
31
- const toSaveItem = Object.assign({ uid }, item);
32
- const tokenString = this.encode(toSaveItem);
33
- const key = [this.getPrefix(), uid, tokenString.split('.').pop()].join(':');
34
- //TODO 过期时间
35
- await this.getRedis().set(key, JSON.stringify(toSaveItem));
36
- return tokenString;
37
- }
38
-
39
- async find(tokenString) {
40
- if (!tokenString) {
41
- return { uid: null, expiredAt: 0 };
42
- }
43
- const parsedToken = this.decode(tokenString);
44
- if (!parsedToken || !{}.hasOwnProperty.call(parsedToken, 'uid')) {
45
- return { uid: null, expiredAt: 0 };
46
- }
47
- const key = [this.getPrefix(), parsedToken.uid, tokenString.split('.').pop()].join(':');
48
- const storedToken = await this.redis.get(key);
49
- if (!storedToken) {
50
- return {
51
- uid: parsedToken.uid,
52
- expiredAt: 0
53
- };
54
- }
55
- return JSON.parse(storedToken);
56
- }
57
-
58
- clear(tokenString) {
59
- if (!tokenString) {
60
- return true;
61
- }
62
- const key = this.getRedisKey(tokenString);
63
- if (!key) {
64
- return true;
65
- }
66
- return this.redis.del(key);
67
- }
68
-
69
- getRedisKey(tokenString, groupOnly = false) {
70
- const parsedToken = this.decode(tokenString);
71
- if (!parsedToken || !{}.hasOwnProperty.call(parsedToken, 'uid')) {
72
- return '';
73
- }
74
- return groupOnly === true ?
75
- [this.getPrefix(), parsedToken.uid].join(':') :
76
- [this.getPrefix(), parsedToken.uid, tokenString.split('.').pop()].join(':');
77
- }
78
-
79
- encode(item, secret = this.config.secret) {
80
- return jwt.encode(item, secret);
81
- }
82
-
83
- decode(str, secret = this.config.secret) {
84
- return jwt.decode(str, secret);
85
- }
86
- }
87
-
88
- constitute.Dependencies(Config, Redis)(JsonWebToken);
89
- export default JsonWebToken;
@@ -1,75 +0,0 @@
1
- import constitute from 'constitute';
2
- import jwt from 'jwt-simple';
3
- import get from 'lodash/get.js';
4
-
5
- import { RuntimeException } from '../exceptions/index.js';
6
- import Config from './config.js';
7
- import RestClient from '../services/rest_client.js';
8
- import ServiceInterface from './interface.js';
9
-
10
- class KongJsonWebToken extends ServiceInterface {
11
- /**
12
- * @param config {Config}
13
- * @param restClient {RestClient}
14
- */
15
- constructor(config, restClient) {
16
- super();
17
- this.restClient = restClient;
18
- this.config = config.get('token');
19
- if (!get(this.config, 'kong.endpoint')) {
20
- throw new RuntimeException('config item `token.kong.endpoint` can not be null');
21
- }
22
- }
23
-
24
- getProto() {
25
- return jwt;
26
- }
27
-
28
- async save(uid, item) {
29
- const toSaveItem = Object.assign({ uid }, item);
30
- const tokenString = this.encode(toSaveItem);
31
- await this.restClient.request({
32
- url: `${this.config.kong.endpoint}/rbac/credentials`,
33
- method: 'post',
34
- body: {
35
- custom_id: uid.toString(),
36
- username: item.username || null,
37
- key: tokenString,
38
- expired_at: item.expiredAt ? item.expiredAt * 1000 : null
39
- }
40
- });
41
- return tokenString;
42
- }
43
-
44
- async find(tokenString) {
45
- if (!tokenString) {
46
- return { uid: null, expiredAt: 0 };
47
- }
48
- const parsedToken = this.decode(tokenString);
49
- if (!parsedToken || !{}.hasOwnProperty.call(parsedToken, 'uid')) {
50
- return { uid: null, expiredAt: 0 };
51
- }
52
- return parsedToken;
53
- }
54
-
55
- clear(tokenString) {
56
- if (!tokenString) {
57
- return true;
58
- }
59
- return this.restClient.request({
60
- url: `${this.config.kong.endpoint}/rbac/credentials/${tokenString}`,
61
- method: 'delete'
62
- });
63
- }
64
-
65
- encode(item, secret = this.config.secret) {
66
- return jwt.encode(item, secret);
67
- }
68
-
69
- decode(str, secret = this.config.secret) {
70
- return jwt.decode(str, secret);
71
- }
72
- }
73
-
74
- constitute.Dependencies(Config, RestClient)(KongJsonWebToken);
75
- export default KongJsonWebToken;