@zerooneit/expressive-tea 1.2.3 → 1.3.0-beta.2

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 (56) hide show
  1. package/.circleci/config.yml +9 -0
  2. package/README.md +70 -88
  3. package/classes/Boot.d.ts +7 -1
  4. package/classes/Boot.js +36 -133
  5. package/classes/LoadBalancer.d.ts +8 -0
  6. package/classes/LoadBalancer.js +31 -0
  7. package/classes/MetaData.js +1 -1
  8. package/classes/ProxyRoute.d.ts +14 -0
  9. package/classes/ProxyRoute.js +40 -0
  10. package/classes/Settings.d.ts +4 -2
  11. package/classes/Settings.js +29 -10
  12. package/decorators/annotations.d.ts +1 -0
  13. package/decorators/module.js +1 -1
  14. package/decorators/router.d.ts +3 -2
  15. package/decorators/router.js +8 -23
  16. package/decorators/server.d.ts +16 -14
  17. package/decorators/server.js +46 -33
  18. package/engines/http/index.d.ts +17 -0
  19. package/engines/http/index.js +60 -0
  20. package/engines/teacup/index.d.ts +19 -0
  21. package/engines/teacup/index.js +107 -0
  22. package/engines/teapot/index.d.ts +23 -0
  23. package/engines/teapot/index.js +157 -0
  24. package/engines/websocket/index.d.ts +9 -0
  25. package/engines/websocket/index.js +37 -0
  26. package/helpers/boot-helper.d.ts +7 -0
  27. package/helpers/boot-helper.js +78 -0
  28. package/helpers/object-helper.d.ts +1 -0
  29. package/helpers/object-helper.js +9 -1
  30. package/helpers/server.d.ts +6 -4
  31. package/helpers/server.js +54 -15
  32. package/helpers/teapot-helper.d.ts +17 -0
  33. package/helpers/teapot-helper.js +47 -0
  34. package/helpers/websocket-helper.d.ts +5 -0
  35. package/helpers/websocket-helper.js +20 -0
  36. package/inversify.config.d.ts +9 -0
  37. package/inversify.config.js +8 -0
  38. package/libs/classNames.d.ts +1 -0
  39. package/libs/classNames.js +4 -0
  40. package/libs/constants.d.ts +4 -2
  41. package/libs/constants.js +8 -6
  42. package/libs/interfaces.d.ts +27 -15
  43. package/package.json +30 -8
  44. package/services/WebsocketService.d.ts +8 -7
  45. package/services/WebsocketService.js +12 -7
  46. package/.editorconfig +0 -31
  47. package/.travis.yml +0 -26
  48. package/CHANGELOG.md +0 -256
  49. package/CODE_OF_CONDUCT.md +0 -76
  50. package/CONTRIBUTING.md +0 -92
  51. package/SECURITY.md +0 -20
  52. package/codecov.yml +0 -10
  53. package/jsdocs-json.json +0 -35
  54. package/tools/jsdocs/helpers/table-builder.js +0 -57
  55. package/tools/jsdocs/plugins/custom-tags.js +0 -33
  56. package/tools/jsdocs/plugins/scape-at.js +0 -24
@@ -14,9 +14,11 @@ import { ExpressiveTeaServerProps } from '../libs/interfaces';
14
14
  *
15
15
  * @class Settings
16
16
  * @param {ExpressiveTeaServerProps} [options={ port: 3000 }]
17
+ * @param {boolean} [isIsolated=false]
17
18
  * @summary Singleton Class to Store Server Settings
18
19
  */
19
20
  declare class Settings {
21
+ static isolatedContext: Map<any, Settings>;
20
22
  /**
21
23
  * Reset Singleton instance to the default values, all changes will be erased is not recommendable to use it
22
24
  * multiple times since all your options will be lost. Unless you have an option how to recover this is not
@@ -37,7 +39,7 @@ declare class Settings {
37
39
  * @memberof Settings
38
40
  * @summary Get Singleton Instance.
39
41
  */
40
- static getInstance(): Settings;
42
+ static getInstance(ctx?: any): Settings;
41
43
  /**
42
44
  * Singleton Instance only for internal.
43
45
  *
@@ -55,7 +57,7 @@ declare class Settings {
55
57
  * @memberof Settings
56
58
  */
57
59
  private options;
58
- constructor(options?: ExpressiveTeaServerProps);
60
+ constructor(options?: ExpressiveTeaServerProps, isIsolated?: boolean);
59
61
  /**
60
62
  * It will return the latest snapshot options registered at the time that this method is called, as Expressive Tea
61
63
  * is designed as async methods some time options should not be available.
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
+ var Settings_1;
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
+ const tslib_1 = require("tslib");
3
5
  const _ = require("lodash");
6
+ const inversify_1 = require("inversify");
7
+ const object_helper_1 = require("../helpers/object-helper");
8
+ const server_1 = require("../helpers/server");
4
9
  /**
5
10
  * Declare the properties which the server will save into settings, is a semi dynamic object since is allowed to save
6
11
  * any property but is contains only one defined property to keep the port of the server.
@@ -16,15 +21,17 @@ const _ = require("lodash");
16
21
  *
17
22
  * @class Settings
18
23
  * @param {ExpressiveTeaServerProps} [options={ port: 3000 }]
24
+ * @param {boolean} [isIsolated=false]
19
25
  * @summary Singleton Class to Store Server Settings
20
26
  */
21
- class Settings {
22
- constructor(options = { port: 3000, securePort: 4443 }) {
23
- if (Settings.instance) {
24
- return Settings.instance;
27
+ let Settings = Settings_1 = class Settings {
28
+ constructor(options = { port: 3000, securePort: 4443 }, isIsolated = false) {
29
+ if (Settings_1.instance && !isIsolated) {
30
+ return Settings_1.instance;
25
31
  }
26
- this.options = Object.assign({}, { port: 3000, securePort: 4443 }, options);
27
- Settings.instance = this;
32
+ const settingsFile = (0, server_1.fileSettings)();
33
+ this.options = Object.assign({}, { port: 3000, securePort: 4443 }, settingsFile, options);
34
+ Settings_1.instance = this;
28
35
  }
29
36
  /**
30
37
  * Reset Singleton instance to the default values, all changes will be erased is not recommendable to use it
@@ -37,7 +44,7 @@ class Settings {
37
44
  * @memberof Settings
38
45
  */
39
46
  static reset() {
40
- delete Settings.instance;
47
+ delete Settings_1.instance;
41
48
  }
42
49
  /**
43
50
  * Get Current Singleton Instance or Created if not exists. If a new instance is created it will created with default
@@ -48,8 +55,15 @@ class Settings {
48
55
  * @memberof Settings
49
56
  * @summary Get Singleton Instance.
50
57
  */
51
- static getInstance() {
52
- return Settings.instance || new Settings();
58
+ static getInstance(ctx) {
59
+ if (ctx) {
60
+ const context = (0, object_helper_1.nameOfClass)(ctx);
61
+ if (!Settings_1.isolatedContext.has(context)) {
62
+ Settings_1.isolatedContext.set(context, new Settings_1(null, true));
63
+ }
64
+ return Settings_1.isolatedContext.get(context);
65
+ }
66
+ return Settings_1.instance || new Settings_1();
53
67
  }
54
68
  /**
55
69
  * It will return the latest snapshot options registered at the time that this method is called, as Expressive Tea
@@ -96,5 +110,10 @@ class Settings {
96
110
  merge(options = { port: 3000, securePort: 4443 }) {
97
111
  this.options = Object.assign(this.options, options);
98
112
  }
99
- }
113
+ };
114
+ Settings.isolatedContext = new Map();
115
+ Settings = Settings_1 = tslib_1.__decorate([
116
+ (0, inversify_1.injectable)(),
117
+ tslib_1.__metadata("design:paramtypes", [Object, Boolean])
118
+ ], Settings);
100
119
  exports.default = Settings;
@@ -1,3 +1,4 @@
1
+ import { ParameterDecorator } from '../libs/types';
1
2
  /**
2
3
  * Is passing directly to the decorated argument described <a href="http://expressjs.com/en/4x/api.html#req">here</a>.
3
4
  * @decorator {ParameterDecorator} request - Assign express Request instance to parameter.
@@ -30,7 +30,7 @@ const DependencyInjection_1 = require("../services/DependencyInjection");
30
30
  */
31
31
  function Module(options) {
32
32
  return (Module) => {
33
- return class extends Module {
33
+ return class ExpressiveTeaModule extends Module {
34
34
  constructor(...args) {
35
35
  super(...args);
36
36
  this.router = (0, express_1.Router)();
@@ -1,6 +1,6 @@
1
1
  import { Router } from 'express';
2
2
  import { ExpressiveTeaHandlerOptions } from '../libs/interfaces';
3
- import { ExpressMiddlewareHandler, MethodDecorator } from '../libs/types';
3
+ import { ClassDecorator, ExpressMiddlewareHandler, MethodDecorator } from '../libs/types';
4
4
  /**
5
5
  * @module Decorators/Router
6
6
  */
@@ -17,8 +17,9 @@ import { ExpressMiddlewareHandler, MethodDecorator } from '../libs/types';
17
17
  * {REPLACE-AT}Route('/)
18
18
  * class Example {}
19
19
  */
20
- export declare function Route(mountpoint?: string): <T extends new (...args: any[]) => {}>(Route: T) => {
20
+ export declare function Route(mountpoint?: string): <T extends new (...args: any[]) => any>(RouterClass: T) => {
21
21
  new (...args: any[]): {
22
+ [x: string]: any;
22
23
  readonly router: Router;
23
24
  readonly mountpoint: string;
24
25
  __mount(parent: Router): any;
@@ -4,7 +4,6 @@ exports.View = exports.Middleware = exports.Param = exports.Delete = exports.Pat
4
4
  const express_1 = require("express");
5
5
  const lodash_1 = require("lodash");
6
6
  const MetaData_1 = require("../classes/MetaData");
7
- const RequestExceptions_1 = require("../exceptions/RequestExceptions");
8
7
  const decorators_1 = require("../helpers/decorators");
9
8
  const server_1 = require("../helpers/server");
10
9
  const constants_1 = require("../libs/constants");
@@ -25,8 +24,8 @@ const constants_1 = require("../libs/constants");
25
24
  * class Example {}
26
25
  */
27
26
  function Route(mountpoint = '/') {
28
- return (Route) => {
29
- return class ExpressiveTeaRoute extends Route {
27
+ return (RouterClass) => {
28
+ return class ExpressiveTeaRoute extends RouterClass {
30
29
  constructor(...args) {
31
30
  super(...args);
32
31
  const handlers = MetaData_1.default.get(constants_1.ROUTER_HANDLERS_KEY, this) || [];
@@ -46,26 +45,12 @@ function Route(mountpoint = '/') {
46
45
  const self = this;
47
46
  const decoratedArguments = MetaData_1.default.get(constants_1.ARGUMENTS_KEY, options.target, options.propertyKey);
48
47
  const annotations = MetaData_1.default.get(constants_1.ROUTER_ANNOTATIONS_KEY, options.target, options.propertyKey);
49
- return async function exec(request, response, next) {
50
- try {
51
- let isNextUsed = false;
52
- const nextWrapper = () => (error) => {
53
- next(error);
54
- isNextUsed = true;
55
- };
56
- // TODO: Must be Depecrated in prior version.
57
- const result = await options.handler.apply(self, (0, server_1.mapArguments)(decoratedArguments, request, response, nextWrapper()));
58
- if (!response.headersSent && !isNextUsed) {
59
- (0, server_1.autoResponse)(request, response, annotations, result);
60
- }
61
- }
62
- catch (e) {
63
- if (e instanceof RequestExceptions_1.GenericRequestException) {
64
- return next(e);
65
- }
66
- next(new RequestExceptions_1.GenericRequestException(e.message || 'System Error'));
67
- }
68
- };
48
+ return server_1.executeRequest.bind({
49
+ options,
50
+ decoratedArguments,
51
+ annotations,
52
+ self
53
+ });
69
54
  }
70
55
  };
71
56
  };
@@ -1,6 +1,6 @@
1
1
  import { Express } from 'express';
2
2
  import { BOOT_STAGES } from '../libs/constants';
3
- import { ExpressiveTeaServerProps, ExpressiveTeaStaticFileServer } from '../libs/interfaces';
3
+ import { ExpressiveTeaPotSettings, ExpressiveTeaServerProps, ExpressiveTeaStaticFileServer, ExpressiveTeaCupSettings } from '../libs/interfaces';
4
4
  /**
5
5
  * Plug Class Decorator create a simple plugin to execute in one of the public stages defined on BOOT_STAGES, might
6
6
  * be useful to attach a simple Express Server configuration.
@@ -25,7 +25,7 @@ export declare function Plug(stage: BOOT_STAGES, name: string, method: (server?:
25
25
  * @decorator {ClassDecorator} Pour - Use Expressive Tea plugin definition instance.
26
26
  * @summary Attach an Expressive Tea Definition Instance.
27
27
  * @param Plugin - A Plugin Class which extends @expressive-tea/plugin/Plugin Class.
28
- * @param pluginArgs - Plugin Constructor Arguments
28
+ * @param pluginArgs any[] - Arguments passed directly to the Plugin constructor.
29
29
  * @version 1.1.0
30
30
  * @link https://www.npmjs.com/package/@expressive-tea/plugin Expressive Tea Plugin
31
31
  */
@@ -36,7 +36,6 @@ export declare function Pour(Plugin: any, ...pluginArgs: any[]): (target: any) =
36
36
  * @decorator {ClassDecorator} ServerSettings - Declares Server Settings.
37
37
  * @summary Declare Server Properties.
38
38
  * @param {ExpressiveTeaModuleProps} options
39
- * @param {object} [port=3000] Select Port Number where the server should be listening.
40
39
  */
41
40
  export declare function ServerSettings(options?: ExpressiveTeaServerProps): (target: any) => any;
42
41
  /**
@@ -60,9 +59,9 @@ export declare function Static(root: string, virtual?: string | null, options?:
60
59
  * @summary Express Setting Directive
61
60
  * @param {string} name - Express Directive Setting Name
62
61
  * @param {*} settings - Setting Arguments
63
- * @decorator {ClassDecorator} ExpressDirecive - Set a Express App Setting.
62
+ * @decorator {ClassDecorator} ExpressDirective - Set a Express App Setting.
64
63
  */
65
- export declare function ExpressDirecive(name: string, ...settings: any[]): (target: any) => void;
64
+ export declare function ExpressDirective(name: string, ...settings: any[]): (target: any) => void;
66
65
  /**
67
66
  * Setting Property Decorator Automatically assign a settings declared on Settings Service into the decorated property.
68
67
  * All properties will contains the settings value or undefined if current settings is not founded.
@@ -71,15 +70,6 @@ export declare function ExpressDirecive(name: string, ...settings: any[]): (targ
71
70
  * @param {string} settingName The Setting name tha
72
71
  */
73
72
  export declare function Setting(settingName: string): (target: any, propertyName: string) => any;
74
- /**
75
- * Register Module Method Decorator this Method Decorator is used at bootstrap level and should decorate the start
76
- * method with a Module Class.
77
- * @decorator {MethodDecorator} RegisterModule - Register a Expressive Tea module to application.
78
- * @summary <b>ONLY</b> Decorate Start Method, this register the Module Classes created by the user.
79
- * @param {Class} Module
80
- */
81
- export declare function RegisterModule(Module: any): (target: any, property: any) => void;
82
- export declare function Proxies(proxyContainers: any[]): (target: any) => void;
83
73
  /**
84
74
  * Register Modules Method Decorator this Method Decorator is used at bootstrap level and should decorate bootstrap class
85
75
  * and register modules.
@@ -88,3 +78,15 @@ export declare function Proxies(proxyContainers: any[]): (target: any) => void;
88
78
  * @param Modules
89
79
  */
90
80
  export declare function Modules(Modules: any[]): (target: any) => void;
81
+ export declare function Proxies(proxyContainers: any[]): (target: any) => void;
82
+ /**
83
+ * Register Module Method Decorator this Method Decorator is used at bootstrap level and should decorate the start
84
+ * method with a Module Class.
85
+ * @decorator {MethodDecorator} RegisterModule - Register a Expressive Tea module to application.
86
+ * @summary <b>ONLY</b> Decorate Start Method, this register the Module Classes created by the user.
87
+ * @param {Class} Module
88
+ * @deprecated Use the new decorator Modules that allow add modules into registered modules.
89
+ */
90
+ export declare function RegisterModule(Module: any): (target: any, property: any) => void;
91
+ export declare function Teapot(teapotSettings: ExpressiveTeaPotSettings): (target: object) => void;
92
+ export declare function Teacup(teacupSettings: ExpressiveTeaCupSettings): (target: object) => void;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Modules = exports.Proxies = exports.RegisterModule = exports.Setting = exports.ExpressDirecive = exports.Static = exports.ServerSettings = exports.Pour = exports.Plug = void 0;
3
+ exports.Teacup = exports.Teapot = exports.RegisterModule = exports.Proxies = exports.Modules = exports.Setting = exports.ExpressDirective = exports.Static = exports.ServerSettings = exports.Pour = exports.Plug = void 0;
4
4
  const lodash_1 = require("lodash");
5
5
  const MetaData_1 = require("../classes/MetaData");
6
6
  const Settings_1 = require("../classes/Settings");
@@ -97,7 +97,7 @@ exports.Plug = Plug;
97
97
  * @decorator {ClassDecorator} Pour - Use Expressive Tea plugin definition instance.
98
98
  * @summary Attach an Expressive Tea Definition Instance.
99
99
  * @param Plugin - A Plugin Class which extends @expressive-tea/plugin/Plugin Class.
100
- * @param pluginArgs - Plugin Constructor Arguments
100
+ * @param pluginArgs any[] - Arguments passed directly to the Plugin constructor.
101
101
  * @version 1.1.0
102
102
  * @link https://www.npmjs.com/package/@expressive-tea/plugin Expressive Tea Plugin
103
103
  */
@@ -105,7 +105,7 @@ function Pour(Plugin, ...pluginArgs) {
105
105
  return (target) => {
106
106
  const stages = getStages(target);
107
107
  const instance = new Plugin(...pluginArgs);
108
- const plugins = instance.register(Settings_1.default.getInstance().getOptions(), getRegisteredPlugins(target));
108
+ const plugins = instance.register(Settings_1.default.getInstance(target).getOptions(), getRegisteredPlugins(target));
109
109
  constants_1.BOOT_STAGES_LIST.forEach(STAGE => {
110
110
  setStage(STAGE, (stages[STAGE] || []).concat(instance.getRegisteredStage(STAGE)), target);
111
111
  });
@@ -119,12 +119,10 @@ exports.Pour = Pour;
119
119
  * @decorator {ClassDecorator} ServerSettings - Declares Server Settings.
120
120
  * @summary Declare Server Properties.
121
121
  * @param {ExpressiveTeaModuleProps} options
122
- * @param {object} [port=3000] Select Port Number where the server should be listening.
123
122
  */
124
123
  function ServerSettings(options = {}) {
125
124
  return target => {
126
- Settings_1.default.getInstance().merge(options);
127
- // MetaData.set(BOOT_STAGES_KEY, STAGES_INIT, target);
125
+ Settings_1.default.getInstance(target).merge(options);
128
126
  return target;
129
127
  };
130
128
  }
@@ -160,9 +158,9 @@ exports.Static = Static;
160
158
  * @summary Express Setting Directive
161
159
  * @param {string} name - Express Directive Setting Name
162
160
  * @param {*} settings - Setting Arguments
163
- * @decorator {ClassDecorator} ExpressDirecive - Set a Express App Setting.
161
+ * @decorator {ClassDecorator} ExpressDirective - Set a Express App Setting.
164
162
  */
165
- function ExpressDirecive(name, ...settings) {
163
+ function ExpressDirective(name, ...settings) {
166
164
  return target => {
167
165
  if (!constants_1.EXPRESS_DIRECTIVES.includes(name)) {
168
166
  throw new Error(`Directive Name ${name} is not valid express behavior setting`);
@@ -172,7 +170,7 @@ function ExpressDirecive(name, ...settings) {
172
170
  MetaData_1.default.set(constants_1.REGISTERED_DIRECTIVES_KEY, registeredDirectives, target);
173
171
  };
174
172
  }
175
- exports.ExpressDirecive = ExpressDirecive;
173
+ exports.ExpressDirective = ExpressDirective;
176
174
  /**
177
175
  * Setting Property Decorator Automatically assign a settings declared on Settings Service into the decorated property.
178
176
  * All properties will contains the settings value or undefined if current settings is not founded.
@@ -184,29 +182,28 @@ function Setting(settingName) {
184
182
  return (target, propertyName) => {
185
183
  Object.defineProperty(target, propertyName, {
186
184
  configurable: false,
187
- get: () => Settings_1.default.getInstance().get(propertyName)
185
+ get: () => Settings_1.default.getInstance(target).get(propertyName)
188
186
  });
189
187
  };
190
188
  }
191
189
  exports.Setting = Setting;
192
190
  /**
193
- * Register Module Method Decorator this Method Decorator is used at bootstrap level and should decorate the start
194
- * method with a Module Class.
191
+ * Register Modules Method Decorator this Method Decorator is used at bootstrap level and should decorate bootstrap class
192
+ * and register modules.
195
193
  * @decorator {MethodDecorator} RegisterModule - Register a Expressive Tea module to application.
196
- * @summary <b>ONLY</b> Decorate Start Method, this register the Module Classes created by the user.
197
- * @param {Class} Module
194
+ * @summary This register the Module Classes created by the user.
195
+ * @param Modules
198
196
  */
199
- function RegisterModule(Module) {
200
- return (target, property) => {
201
- if (property !== 'start') {
202
- throw new Error('Register Module needs to decorate ONLY start method');
197
+ function Modules(Modules) {
198
+ return target => {
199
+ for (const Module of Modules) {
200
+ const registeredModules = MetaData_1.default.get(constants_1.REGISTERED_MODULE_KEY, target, 'start') || [];
201
+ registeredModules.unshift(Module);
202
+ MetaData_1.default.set(constants_1.REGISTERED_MODULE_KEY, registeredModules, target, 'start');
203
203
  }
204
- const registeredModules = MetaData_1.default.get(constants_1.REGISTERED_MODULE_KEY, target, property) || [];
205
- registeredModules.push(Module);
206
- MetaData_1.default.set(constants_1.REGISTERED_MODULE_KEY, registeredModules, target, property);
207
204
  };
208
205
  }
209
- exports.RegisterModule = RegisterModule;
206
+ exports.Modules = Modules;
210
207
  function Proxies(proxyContainers) {
211
208
  return target => {
212
209
  for (const proxyContainer of proxyContainers) {
@@ -218,19 +215,35 @@ function Proxies(proxyContainers) {
218
215
  }
219
216
  exports.Proxies = Proxies;
220
217
  /**
221
- * Register Modules Method Decorator this Method Decorator is used at bootstrap level and should decorate bootstrap class
222
- * and register modules.
218
+ * Register Module Method Decorator this Method Decorator is used at bootstrap level and should decorate the start
219
+ * method with a Module Class.
223
220
  * @decorator {MethodDecorator} RegisterModule - Register a Expressive Tea module to application.
224
- * @summary This register the Module Classes created by the user.
225
- * @param Modules
221
+ * @summary <b>ONLY</b> Decorate Start Method, this register the Module Classes created by the user.
222
+ * @param {Class} Module
223
+ * @deprecated Use the new decorator Modules that allow add modules into registered modules.
226
224
  */
227
- function Modules(Modules) {
228
- return target => {
229
- for (const Module of Modules) {
230
- const registeredModules = MetaData_1.default.get(constants_1.REGISTERED_MODULE_KEY, target, 'start') || [];
231
- registeredModules.unshift(Module);
232
- MetaData_1.default.set(constants_1.REGISTERED_MODULE_KEY, registeredModules, target, 'start');
225
+ function RegisterModule(Module) {
226
+ return (target, property) => {
227
+ if (property !== 'start') {
228
+ throw new Error('Register Module needs to decorate ONLY start method');
233
229
  }
230
+ const registeredModules = MetaData_1.default.get(constants_1.REGISTERED_MODULE_KEY, target, property) || [];
231
+ registeredModules.push(Module);
232
+ MetaData_1.default.set(constants_1.REGISTERED_MODULE_KEY, registeredModules, target, property);
234
233
  };
235
234
  }
236
- exports.Modules = Modules;
235
+ exports.RegisterModule = RegisterModule;
236
+ function Teapot(teapotSettings) {
237
+ return (target) => {
238
+ MetaData_1.default.set(constants_1.ASSIGN_TEAPOT_KEY, true, target, 'isTeapotActive');
239
+ MetaData_1.default.set(constants_1.ASSIGN_TEAPOT_KEY, teapotSettings, target);
240
+ };
241
+ }
242
+ exports.Teapot = Teapot;
243
+ function Teacup(teacupSettings) {
244
+ return (target) => {
245
+ MetaData_1.default.set(constants_1.ASSIGN_TEACUP_KEY, true, target, 'isTeacupActive');
246
+ MetaData_1.default.set(constants_1.ASSIGN_TEACUP_KEY, teacupSettings, target);
247
+ };
248
+ }
249
+ exports.Teacup = Teacup;
@@ -0,0 +1,17 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import * as http from 'http';
4
+ import * as https from 'https';
5
+ import { BOOT_STAGES } from '../../libs/constants';
6
+ export default class HTTPEngine {
7
+ private readonly settings;
8
+ private readonly context;
9
+ private readonly server;
10
+ private readonly serverSecure?;
11
+ constructor(ctx: any, server: any, serverSecure: any, settings: any);
12
+ private listen;
13
+ start(): Promise<(http.Server | https.Server)[]>;
14
+ init(): Promise<void>;
15
+ resolveStages(stages: BOOT_STAGES[], ...extraArgs: any[]): Promise<void[]>;
16
+ resolveProxyContainers(): Promise<void>;
17
+ }
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const $P = require("bluebird");
5
+ const inversify_1 = require("inversify");
6
+ const boot_helper_1 = require("../../helpers/boot-helper");
7
+ const constants_1 = require("../../libs/constants");
8
+ const object_helper_1 = require("../../helpers/object-helper");
9
+ const MetaData_1 = require("../../classes/MetaData");
10
+ let HTTPEngine = class HTTPEngine {
11
+ constructor(ctx, server, serverSecure, settings) {
12
+ this.context = ctx;
13
+ this.server = server;
14
+ this.serverSecure = serverSecure;
15
+ this.settings = settings;
16
+ }
17
+ listen(server, port) {
18
+ return new $P((resolve, reject) => {
19
+ server.listen(port);
20
+ server.on('error', error => {
21
+ reject(error);
22
+ });
23
+ server.on('listening', () => {
24
+ console.log(`Running HTTP Server on [${port}]`);
25
+ resolve(server);
26
+ });
27
+ });
28
+ }
29
+ async start() {
30
+ return [
31
+ await this.listen(this.server, this.settings.get('port')),
32
+ (this.serverSecure) ? await this.listen(this.serverSecure, this.settings.get('securePort')) : null
33
+ ];
34
+ }
35
+ async init() {
36
+ await (0, boot_helper_1.resolveDirectives)(this.context, this.context.getApplication());
37
+ await (0, boot_helper_1.resolveStatic)(this.context, this.context.getApplication());
38
+ }
39
+ async resolveStages(stages, ...extraArgs) {
40
+ return $P.map(stages, s => {
41
+ return (0, boot_helper_1.resolveStage)(s, this.context, this.context.getApplication(), ...extraArgs);
42
+ });
43
+ }
44
+ async resolveProxyContainers() {
45
+ const ProxyContainers = MetaData_1.default.get(constants_1.ROUTER_PROXIES_KEY, (0, object_helper_1.getClass)(this.context)) || [];
46
+ for (const Container of ProxyContainers) {
47
+ (0, boot_helper_1.resolveProxy)(Container, this.context.getApplication());
48
+ }
49
+ }
50
+ };
51
+ HTTPEngine = tslib_1.__decorate([
52
+ (0, inversify_1.injectable)(),
53
+ tslib_1.__param(0, (0, inversify_1.inject)('context')),
54
+ tslib_1.__param(1, (0, inversify_1.inject)('server')),
55
+ tslib_1.__param(2, (0, inversify_1.inject)('secureServer')),
56
+ tslib_1.__param(2, (0, inversify_1.optional)()),
57
+ tslib_1.__param(3, (0, inversify_1.inject)('settings')),
58
+ tslib_1.__metadata("design:paramtypes", [Object, Object, Object, Object])
59
+ ], HTTPEngine);
60
+ exports.default = HTTPEngine;
@@ -0,0 +1,19 @@
1
+ export default class TeacupEngine {
2
+ private readonly settings;
3
+ private readonly context;
4
+ private readonly server;
5
+ private readonly serverSecure?;
6
+ private teacupSettings;
7
+ private isActive;
8
+ private publicKey;
9
+ private privateKey;
10
+ private publicServerKey;
11
+ private serverSignature;
12
+ private clientSignature;
13
+ private client;
14
+ private header;
15
+ constructor(ctx: any, settings: any, server: any, serverSecure: any);
16
+ private handshaked;
17
+ private accepted;
18
+ start(): Promise<void>;
19
+ }
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const chalk = require("chalk");
5
+ const url = require("url");
6
+ // tslint:disable-next-line:no-duplicate-imports
7
+ const socket_io_client_1 = require("socket.io-client");
8
+ const inversify_1 = require("inversify");
9
+ const MetaData_1 = require("../../classes/MetaData");
10
+ const constants_1 = require("../../libs/constants");
11
+ const teapot_helper_1 = require("../../helpers/teapot-helper");
12
+ const object_helper_1 = require("../../helpers/object-helper");
13
+ let TeacupEngine = class TeacupEngine {
14
+ constructor(ctx, settings, server, serverSecure) {
15
+ this.server = server;
16
+ this.serverSecure = serverSecure;
17
+ this.settings = settings;
18
+ this.context = ctx;
19
+ this.teacupSettings = MetaData_1.default.get(constants_1.ASSIGN_TEACUP_KEY, (0, object_helper_1.getClass)(this.context));
20
+ this.isActive = MetaData_1.default.get(constants_1.ASSIGN_TEACUP_KEY, (0, object_helper_1.getClass)(this.context), 'isTeacupActive');
21
+ if (!this.isActive) {
22
+ return;
23
+ }
24
+ const scheme = url.parse(this.teacupSettings.serverUrl);
25
+ const { publicKey, privateKey } = teapot_helper_1.default.generateKeys(this.teacupSettings.clientKey);
26
+ this.publicKey = publicKey;
27
+ this.privateKey = privateKey;
28
+ this.clientSignature = teapot_helper_1.default.sign(this.teacupSettings.clientKey, this.privateKey, this.teacupSettings.clientKey);
29
+ this.client = (0, socket_io_client_1.io)(`http://${scheme.host}`, {
30
+ path: '/teapot',
31
+ reconnection: true,
32
+ autoConnect: false
33
+ });
34
+ }
35
+ header() {
36
+ console.log(chalk.white.bold('Teacup Engine is initializing...'));
37
+ console.log(chalk `
38
+ {grey ( (}
39
+ {white.bold ) )}
40
+ {magenta.bold ........}
41
+ {magenta.bold | |]}
42
+ {magenta.bold \\ /} {yellow.bold [${this.teacupSettings.address}]}
43
+ {magenta.bold \`----'}
44
+
45
+ {yellow.bold NOTICE:}
46
+ All Communication are encrypted to ensure intruder can not connected, however, please does not share any sensitive data like keys or passwords to avoid security issues.
47
+ `);
48
+ }
49
+ handshaked(data) {
50
+ try {
51
+ console.log(chalk `{cyan.bold [TEACUP]} - [{magenta.bold ${this.client.id}}]: {yellow.bold Started Verification}`);
52
+ const serverPublicKey = Buffer.from(data.key, 'base64').toString('ascii');
53
+ const serverSignature = Buffer.from(data.signature, 'base64');
54
+ if (teapot_helper_1.default.verify(this.teacupSettings.clientKey, serverPublicKey, serverSignature)) {
55
+ console.log(chalk `{cyan.bold [TEACUP]} - [{magenta.bold ${this.client.id}}]: {green.bold Verified}`);
56
+ this.publicServerKey = serverPublicKey;
57
+ this.serverSignature = serverSignature;
58
+ this.client.emit('handshake', {
59
+ key: Buffer.from(this.publicKey).toString('base64'),
60
+ signature: this.clientSignature.toString('base64')
61
+ });
62
+ return;
63
+ }
64
+ throw new Error('Fail to Verify Client on Teapod.');
65
+ }
66
+ catch (e) {
67
+ console.error(chalk `{cyan.bold [TEACUP]} - {red.bold TEAPOD} {magenta.bold ${this.client.id}}: Failed with next message: ${e.message}`);
68
+ this.client.disconnect();
69
+ }
70
+ }
71
+ accepted() {
72
+ var _a;
73
+ console.log(chalk `{cyan.bold [TEACUP]} - [{magenta.bold ${this.client.id}}]: {green.bold Registered} - {blue.bold <${this.teacupSettings.serverUrl}>} <-> {white.bold ${this.teacupSettings.mountTo}}`);
74
+ this.client.emit('register', teapot_helper_1.default.encrypt({
75
+ mountTo: this.teacupSettings.mountTo,
76
+ address: this.teacupSettings.address
77
+ }, this.serverSignature.slice(0, 32)));
78
+ const onClose = () => {
79
+ try {
80
+ this.client.close();
81
+ }
82
+ catch (_) { }
83
+ };
84
+ this.server.on('close', onClose);
85
+ (_a = this.serverSecure) === null || _a === void 0 ? void 0 : _a.on('close', onClose);
86
+ }
87
+ async start() {
88
+ if (!this.isActive) {
89
+ return;
90
+ }
91
+ this.header();
92
+ this.client.on('handshake', this.handshaked.bind(this));
93
+ this.client.on('accepted', this.accepted.bind(this));
94
+ this.client.on('error', console.log);
95
+ this.client.connect();
96
+ }
97
+ };
98
+ TeacupEngine = tslib_1.__decorate([
99
+ (0, inversify_1.injectable)(),
100
+ tslib_1.__param(0, (0, inversify_1.inject)('context')),
101
+ tslib_1.__param(1, (0, inversify_1.inject)('settings')),
102
+ tslib_1.__param(2, (0, inversify_1.inject)('server')),
103
+ tslib_1.__param(3, (0, inversify_1.inject)('secureServer')),
104
+ tslib_1.__param(3, (0, inversify_1.optional)()),
105
+ tslib_1.__metadata("design:paramtypes", [Object, Object, Object, Object])
106
+ ], TeacupEngine);
107
+ exports.default = TeacupEngine;
@@ -0,0 +1,23 @@
1
+ export default class TeapotEngine {
2
+ private readonly settings;
3
+ private readonly context;
4
+ private readonly server;
5
+ private readonly serverSecure?;
6
+ private clients;
7
+ private registeredRoute;
8
+ private teapotSettings;
9
+ private publicKey;
10
+ private privateKey;
11
+ private isActive;
12
+ private serverSignature;
13
+ private socketServer;
14
+ private static header;
15
+ private registerTeacup;
16
+ private acceptedHandshake;
17
+ private registered;
18
+ private removeFromRoutes;
19
+ private findClientInRoutes;
20
+ private disconnected;
21
+ constructor(ctx: any, server: any, serverSecure: any, settings: any);
22
+ start(): Promise<void>;
23
+ }