@lakutata/core 1.0.26 → 1.0.30

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 (120) hide show
  1. package/README.md +4 -0
  2. package/build/Application.d.ts +3 -0
  3. package/build/Application.js +16 -0
  4. package/build/Application.js.map +1 -1
  5. package/build/ApplicationManagement.js +5 -0
  6. package/build/ApplicationManagement.js.map +1 -1
  7. package/build/base/Container.js +16 -0
  8. package/build/base/Container.js.map +1 -1
  9. package/build/base/abstracts/Module.js.map +1 -1
  10. package/build/base/process/Process.d.ts +1 -1
  11. package/build/base/process/Process.js +2 -2
  12. package/build/base/process/Process.js.map +1 -1
  13. package/build/base/process/ProcessManager.d.ts +1 -1
  14. package/build/base/process/ProcessManager.js +2 -2
  15. package/build/base/process/ProcessManager.js.map +1 -1
  16. package/build/base/thread/Thread.d.ts +1 -1
  17. package/build/base/thread/Thread.js +2 -2
  18. package/build/base/thread/Thread.js.map +1 -1
  19. package/build/base/thread/ThreadManager.d.ts +1 -1
  20. package/build/base/thread/ThreadManager.js +2 -2
  21. package/build/base/thread/ThreadManager.js.map +1 -1
  22. package/build/interfaces/IApplicationConfig.d.ts +1 -0
  23. package/build/lib/multipleRunner/constants/Channel.d.ts +5 -0
  24. package/build/lib/multipleRunner/constants/Channel.js +9 -0
  25. package/build/lib/multipleRunner/constants/Channel.js.map +1 -0
  26. package/build/lib/multipleRunner/constants/MetadataKey.d.ts +2 -0
  27. package/build/lib/multipleRunner/constants/MetadataKey.js +6 -0
  28. package/build/lib/multipleRunner/constants/MetadataKey.js.map +1 -0
  29. package/build/lib/multipleRunner/containers/ContainerVolume.d.ts +4 -0
  30. package/build/lib/multipleRunner/containers/ContainerVolume.js +85 -0
  31. package/build/lib/multipleRunner/containers/ContainerVolume.js.map +1 -0
  32. package/build/lib/multipleRunner/definitions/interfaces/ClassConstructor.d.ts +3 -0
  33. package/build/lib/multipleRunner/definitions/interfaces/ClassConstructor.js +3 -0
  34. package/build/lib/multipleRunner/definitions/interfaces/ClassConstructor.js.map +1 -0
  35. package/build/lib/multipleRunner/definitions/interfaces/MultipleRunnerBaseClassConstructor.d.ts +4 -0
  36. package/build/lib/multipleRunner/definitions/interfaces/MultipleRunnerBaseClassConstructor.js +3 -0
  37. package/build/lib/multipleRunner/definitions/interfaces/MultipleRunnerBaseClassConstructor.js.map +1 -0
  38. package/build/lib/multipleRunner/definitions/interfaces/ProcessClassConstructor.d.ts +7 -0
  39. package/build/lib/multipleRunner/definitions/interfaces/ProcessClassConstructor.js +3 -0
  40. package/build/lib/multipleRunner/definitions/interfaces/ProcessClassConstructor.js.map +1 -0
  41. package/build/lib/multipleRunner/definitions/interfaces/ProxyClass.d.ts +3 -0
  42. package/build/lib/multipleRunner/definitions/interfaces/ProxyClass.js +3 -0
  43. package/build/lib/multipleRunner/definitions/interfaces/ProxyClass.js.map +1 -0
  44. package/build/lib/multipleRunner/definitions/interfaces/ThreadClassConstructor.d.ts +7 -0
  45. package/build/lib/multipleRunner/definitions/interfaces/ThreadClassConstructor.js +3 -0
  46. package/build/lib/multipleRunner/definitions/interfaces/ThreadClassConstructor.js.map +1 -0
  47. package/build/lib/multipleRunner/lib/Process.d.ts +5 -0
  48. package/build/lib/multipleRunner/lib/Process.js +44 -0
  49. package/build/lib/multipleRunner/lib/Process.js.map +1 -0
  50. package/build/lib/multipleRunner/lib/Thread.d.ts +5 -0
  51. package/build/lib/multipleRunner/lib/Thread.js +52 -0
  52. package/build/lib/multipleRunner/lib/Thread.js.map +1 -0
  53. package/build/lib/multipleRunner/lib/bridges/BaseBridge.d.ts +32 -0
  54. package/build/lib/multipleRunner/lib/bridges/BaseBridge.js +104 -0
  55. package/build/lib/multipleRunner/lib/bridges/BaseBridge.js.map +1 -0
  56. package/build/lib/multipleRunner/lib/bridges/ProcessBridge.d.ts +5 -0
  57. package/build/lib/multipleRunner/lib/bridges/ProcessBridge.js +20 -0
  58. package/build/lib/multipleRunner/lib/bridges/ProcessBridge.js.map +1 -0
  59. package/build/lib/multipleRunner/lib/bridges/ThreadBridge.d.ts +5 -0
  60. package/build/lib/multipleRunner/lib/bridges/ThreadBridge.js +17 -0
  61. package/build/lib/multipleRunner/lib/bridges/ThreadBridge.js.map +1 -0
  62. package/build/lib/multipleRunner/lib/cores/GenerateExecArgv.d.ts +2 -0
  63. package/build/lib/multipleRunner/lib/cores/GenerateExecArgv.js +35 -0
  64. package/build/lib/multipleRunner/lib/cores/GenerateExecArgv.js.map +1 -0
  65. package/build/lib/multipleRunner/lib/cores/IsElectron.d.ts +1 -0
  66. package/build/lib/multipleRunner/lib/cores/IsElectron.js +9 -0
  67. package/build/lib/multipleRunner/lib/cores/IsElectron.js.map +1 -0
  68. package/build/lib/multipleRunner/lib/cores/ProcessClass.d.ts +5 -0
  69. package/build/lib/multipleRunner/lib/cores/ProcessClass.js +12 -0
  70. package/build/lib/multipleRunner/lib/cores/ProcessClass.js.map +1 -0
  71. package/build/lib/multipleRunner/lib/cores/ThreadClass.d.ts +5 -0
  72. package/build/lib/multipleRunner/lib/cores/ThreadClass.js +12 -0
  73. package/build/lib/multipleRunner/lib/cores/ThreadClass.js.map +1 -0
  74. package/build/lib/multipleRunner/lib/cores/UniParentBridgeHandler.d.ts +2 -0
  75. package/build/lib/multipleRunner/lib/cores/UniParentBridgeHandler.js +104 -0
  76. package/build/lib/multipleRunner/lib/cores/UniParentBridgeHandler.js.map +1 -0
  77. package/build/lib/multipleRunner/lib/prototypes/MultipleRunnerBaseClass.d.ts +17 -0
  78. package/build/lib/multipleRunner/lib/prototypes/MultipleRunnerBaseClass.js +53 -0
  79. package/build/lib/multipleRunner/lib/prototypes/MultipleRunnerBaseClass.js.map +1 -0
  80. package/build/plugins/Formatter.d.ts +1 -0
  81. package/build/plugins/Formatter.js +8 -5
  82. package/build/plugins/Formatter.js.map +1 -1
  83. package/build/plugins/Logger.js +6 -2
  84. package/build/plugins/Logger.js.map +1 -1
  85. package/build/types/TPluginsConfig.d.ts +1 -0
  86. package/package.json +11 -11
  87. package/src/Application.ts +29 -0
  88. package/src/ApplicationManagement.ts +3 -0
  89. package/src/base/Container.ts +14 -1
  90. package/src/base/abstracts/Module.ts +2 -1
  91. package/src/base/process/Process.ts +1 -1
  92. package/src/base/process/ProcessManager.ts +2 -2
  93. package/src/base/thread/Thread.ts +1 -1
  94. package/src/base/thread/ThreadManager.ts +2 -2
  95. package/src/interfaces/IApplicationConfig.ts +1 -0
  96. package/src/lib/multipleRunner/constants/Channel.ts +5 -0
  97. package/src/lib/multipleRunner/constants/MetadataKey.ts +2 -0
  98. package/src/lib/multipleRunner/containers/ContainerVolume.ts +83 -0
  99. package/src/lib/multipleRunner/definitions/interfaces/ClassConstructor.ts +3 -0
  100. package/src/lib/multipleRunner/definitions/interfaces/MultipleRunnerBaseClassConstructor.ts +5 -0
  101. package/src/lib/multipleRunner/definitions/interfaces/ProcessClassConstructor.ts +10 -0
  102. package/src/lib/multipleRunner/definitions/interfaces/ProxyClass.ts +3 -0
  103. package/src/lib/multipleRunner/definitions/interfaces/ThreadClassConstructor.ts +10 -0
  104. package/src/lib/multipleRunner/lib/Process.ts +44 -0
  105. package/src/lib/multipleRunner/lib/Thread.ts +52 -0
  106. package/src/lib/multipleRunner/lib/bridges/BaseBridge.ts +115 -0
  107. package/src/lib/multipleRunner/lib/bridges/ProcessBridge.ts +18 -0
  108. package/src/lib/multipleRunner/lib/bridges/ThreadBridge.ts +15 -0
  109. package/src/lib/multipleRunner/lib/cores/GenerateExecArgv.ts +28 -0
  110. package/src/lib/multipleRunner/lib/cores/IsElectron.ts +3 -0
  111. package/src/lib/multipleRunner/lib/cores/ProcessClass.ts +8 -0
  112. package/src/lib/multipleRunner/lib/cores/ThreadClass.ts +8 -0
  113. package/src/lib/multipleRunner/lib/cores/UniParentBridgeHandler.ts +97 -0
  114. package/src/lib/multipleRunner/lib/prototypes/MultipleRunnerBaseClass.ts +66 -0
  115. package/src/plugins/Formatter.ts +10 -5
  116. package/src/plugins/Logger.ts +3 -2
  117. package/src/tests/Main.spec.ts +10 -6
  118. package/src/tests/threads/TestThread.ts +3 -1
  119. package/src/types/TPluginsConfig.ts +1 -1
  120. package/.idea/dbnavigator.xml +0 -558
@@ -0,0 +1,115 @@
1
+ import {EventEmitter} from 'events'
2
+ import random from 'random'
3
+ import {INVOKE, PROPERTY_CHANGE, READY, SET_PROPERTY, SETUP_ERROR} from '../../constants/Channel'
4
+
5
+ export class BaseBridge {
6
+ protected readonly eventEmitter: EventEmitter = new EventEmitter()
7
+ protected readonly bridge: any
8
+
9
+ constructor(bridgeObject: any) {
10
+ this.bridge = bridgeObject
11
+ this.bridge.on('message', data => {
12
+ this.eventEmitter.emit.apply(this.eventEmitter, [data.channel].concat(data.args) as any)
13
+ })
14
+ }
15
+
16
+ public off(channel: string) {
17
+ this.eventEmitter.removeAllListeners(channel)
18
+ return this
19
+ }
20
+
21
+ public on(channel: string, listener: (...args: any[]) => void) {
22
+ this.eventEmitter.on.apply(this.eventEmitter, [channel, listener])
23
+ return this
24
+ }
25
+
26
+ public once(channel: string, listener: (...args: any[]) => void) {
27
+ this.eventEmitter.once.apply(this.eventEmitter, [channel, listener])
28
+ return this
29
+ }
30
+
31
+ public send(channel: string, ...args: any[]) {
32
+ throw new Error('Method did not implemented')
33
+ }
34
+
35
+ public ready(properties: { [key: string]: any }, propertyValues: { [key: string]: any }) {
36
+ this.send(READY, properties, propertyValues)
37
+ return this
38
+ }
39
+
40
+ public onReady(callback: (properties: { [key: string]: any }, propertyValues: { [key: string]: any }) => void) {
41
+ this.once(READY, callback)
42
+ return this
43
+ }
44
+
45
+ public error(error: Error) {
46
+ this.send('error', error.message)
47
+ }
48
+
49
+ public setupError(error: Error) {
50
+ this.send(SETUP_ERROR, error.message)
51
+ }
52
+
53
+ public async invoke(method: string, args: Array<any>) {
54
+ return new Promise((resolve, reject) => {
55
+ const invokeId: string = `ts_${Date.now()}_rnd_${random.int(100, 999999)}`
56
+ const responseChannelName: string = `__%$invoke_res_${invokeId}$%__`
57
+ this.once(responseChannelName, (errMsg: string, result: any) => {
58
+ if (errMsg) {
59
+ reject(new Error(errMsg))
60
+ } else {
61
+ resolve(result)
62
+ }
63
+ })
64
+ this.send(INVOKE, method, args, responseChannelName)
65
+ })
66
+ }
67
+
68
+ public onInvoke(handler: (method: string, args: Array<any>) => Promise<any>): void {
69
+ this.on(INVOKE, (method: string, args: Array<any>, responseChannelName: string) => {
70
+ handler(method, args).then(result => {
71
+ this.send(responseChannelName, null, result)
72
+ }).catch(invokeError => {
73
+ this.send(responseChannelName, invokeError.message, undefined)
74
+ })
75
+ })
76
+ }
77
+
78
+ public set(property: string, value: any) {
79
+ this.send(SET_PROPERTY, property, value)
80
+ return this
81
+ }
82
+
83
+ public onSet(handler: (property: string, value: any) => void) {
84
+ this.on(SET_PROPERTY, handler)
85
+ return this
86
+ }
87
+
88
+ public propertyChange(property: string, value: any) {
89
+ this.send(PROPERTY_CHANGE, property, value)
90
+ this.send('property-changed', property, value)
91
+ return this
92
+ }
93
+
94
+ public onPropertyChanged(handler: (property: string, value: any) => void) {
95
+ this.on(PROPERTY_CHANGE, handler)
96
+ return this
97
+ }
98
+
99
+ public callbackHandler(thisArg: any, handler: (...args: any[]) => any): string {
100
+ const callbackEvtName: string = `__________%$CALLBACK[${Date.now()}]$%__________`
101
+ this.once(callbackEvtName, (...args: any[]) => {
102
+ handler.apply(thisArg, args)
103
+ })
104
+ return callbackEvtName
105
+ }
106
+
107
+ public onCallback(callbackEvtName: string, args: any[]) {
108
+ this.send.apply(this, [callbackEvtName].concat(args) as any)
109
+ return this
110
+ }
111
+
112
+ public async terminate(): Promise<void> {
113
+ throw new Error('Method did not implemented')
114
+ }
115
+ }
@@ -0,0 +1,18 @@
1
+ import {BaseBridge} from './BaseBridge'
2
+
3
+ export class ProcessBridge extends BaseBridge {
4
+
5
+ public send(channel: string, ...args: any[]) {
6
+ this.bridge.send({
7
+ channel: channel,
8
+ args: args
9
+ })
10
+ }
11
+
12
+ public async terminate(): Promise<void> {
13
+ return new Promise(resolve => {
14
+ this.bridge.once('close', resolve)
15
+ this.bridge.kill()
16
+ })
17
+ }
18
+ }
@@ -0,0 +1,15 @@
1
+ import {BaseBridge} from './BaseBridge'
2
+
3
+ export class ThreadBridge extends BaseBridge {
4
+
5
+ public send(channel: string, ...args: any[]) {
6
+ this.bridge.postMessage({
7
+ channel: channel,
8
+ args: args
9
+ })
10
+ }
11
+
12
+ public async terminate(): Promise<void> {
13
+ await this.bridge.terminate()
14
+ }
15
+ }
@@ -0,0 +1,28 @@
1
+ import 'reflect-metadata'
2
+ import path from 'path'
3
+
4
+ export function GenerateExecArgv(modulePath: string): Array<string> {
5
+ const moduleExtName: string = path.extname(modulePath).toUpperCase()
6
+ switch (moduleExtName) {
7
+ case '.TS': {
8
+ require('ts-node')
9
+ const registerRegEx = /ts-node\/register/
10
+ const CMs = require('module')._cache
11
+ for (const cmPath of Object.keys(CMs)) {
12
+ if (registerRegEx.test(cmPath)) {
13
+ return [
14
+ '-r',
15
+ cmPath
16
+ ]
17
+ }
18
+ }
19
+ return []
20
+ }
21
+ case '.JS': {
22
+ return []
23
+ }
24
+ default: {
25
+ throw new Error('Invalid module type')
26
+ }
27
+ }
28
+ }
@@ -0,0 +1,3 @@
1
+ export function isElectron() {
2
+ return !!(process?.versions?.electron)
3
+ }
@@ -0,0 +1,8 @@
1
+ import 'reflect-metadata'
2
+ import {MultipleRunnerBaseClass} from '../prototypes/MultipleRunnerBaseClass'
3
+
4
+ export class ProcessClass extends MultipleRunnerBaseClass {
5
+ protected async setup(): Promise<void> {
6
+ return super.setup()
7
+ }
8
+ }
@@ -0,0 +1,8 @@
1
+ import 'reflect-metadata'
2
+ import {MultipleRunnerBaseClass} from '../prototypes/MultipleRunnerBaseClass'
3
+
4
+ export class ThreadClass extends MultipleRunnerBaseClass {
5
+ protected async setup(): Promise<void> {
6
+ return super.setup()
7
+ }
8
+ }
@@ -0,0 +1,97 @@
1
+ import {BaseBridge} from '../bridges/BaseBridge'
2
+ import {SETUP_ERROR} from '../../constants/Channel'
3
+
4
+ export function UniParentBridgeHandler<T>(bridge: BaseBridge, className: string, classPath: string, classConstructor: any, callback: (setupError: Error, proxyInstance: T) => void): void {
5
+ const reservedMethodNames: Array<string> = ['off', 'on', 'once', 'emit', 'terminate']
6
+ bridge.on(SETUP_ERROR, (setupErrorMsg => {
7
+ callback(new Error(setupErrorMsg), null as any)
8
+ })).onReady((properties: { [key: string]: any }, propertyValues: { [key: string]: any }) => {
9
+ bridge.onPropertyChanged((property, value) => {
10
+ propertyValues[property] = value
11
+ })
12
+ const proxyHandler: any = new Proxy({}, {
13
+ get(target: T, p: PropertyKey): any {
14
+ if (reservedMethodNames.indexOf(p.toString()) > -1) {
15
+ switch (p.toString()) {
16
+ case 'off': {
17
+ return new Proxy((...args: any[]) => {
18
+ }, {
19
+ apply(applyTarget: (...args: any[]) => void, thisArg: any, argArray?: any): any {
20
+ bridge.off.apply(bridge, argArray)
21
+ }
22
+ })
23
+ }
24
+ case 'on': {
25
+ return new Proxy((...args: any[]) => {
26
+ }, {
27
+ apply(applyTarget: (...args: any[]) => void, thisArg: any, argArray?: any): any {
28
+ bridge.on.apply(bridge, argArray)
29
+ }
30
+ })
31
+ }
32
+ case 'once': {
33
+ return new Proxy((...args: any[]) => {
34
+ }, {
35
+ apply(applyTarget: (...args: any[]) => void, thisArg: any, argArray?: any): any {
36
+ bridge.once.apply(bridge, argArray)
37
+ }
38
+ })
39
+ }
40
+ case 'emit': {
41
+ return new Proxy((...args: any[]) => {
42
+ }, {
43
+ apply(applyTarget: (...args: any[]) => void, thisArg: any, argArray?: any): any {
44
+ bridge.send.apply(bridge, argArray)
45
+ }
46
+ })
47
+ }
48
+ case 'terminate': {
49
+ return new Proxy((...args: any[]) => {
50
+ }, {
51
+ apply(applyTarget: (...args: any[]) => void, thisArg: any, argArray?: any): any {
52
+ return new Promise((resolve, reject) => {
53
+ bridge.terminate().then(resolve).catch(reject)
54
+ })
55
+ }
56
+ })
57
+ }
58
+ default:
59
+ return undefined
60
+ }
61
+ } else {
62
+ const method = classConstructor.prototype[p]
63
+ const isPropertyMethod: boolean = properties[p.toString()] === 'function'
64
+ if (method || isPropertyMethod) {
65
+ return new Proxy(isPropertyMethod ? (...args: any[]) => {
66
+ } : method, {
67
+ apply(target, thisArg: any, argArray?: any): any {
68
+ const _argArray: any[] = []
69
+ for (const _arg of argArray) {
70
+ if (typeof _arg !== 'function') {
71
+ _argArray.push(_arg)
72
+ } else {
73
+ _argArray.push(bridge.callbackHandler(thisArg, _arg))
74
+ }
75
+ }
76
+ return new Promise((resolve, reject) => {
77
+ bridge.invoke(p.toString(), _argArray).then(resolve).catch(reject)
78
+ })
79
+ }
80
+ })
81
+ } else if (properties[p.toString()]) {
82
+ return propertyValues[p.toString()]
83
+ } else {
84
+ return undefined
85
+ }
86
+ }
87
+ },
88
+ set(target: {}, p: PropertyKey, value: any, receiver: any): boolean {
89
+ bridge.set(p.toString(), value)
90
+ return true
91
+ }
92
+ })
93
+ bridge.off(SETUP_ERROR)
94
+ callback(null as any, proxyHandler)
95
+ })
96
+ bridge.send('create', className, classPath)
97
+ }
@@ -0,0 +1,66 @@
1
+ import 'reflect-metadata'
2
+ import {BaseBridge} from '../bridges/BaseBridge'
3
+ import {MULTIPLE_RUNNER_CLASS_NAME, MULTIPLE_RUNNER__CLASS_PATH} from '../../constants/MetadataKey'
4
+
5
+ export class MultipleRunnerBaseClass {
6
+ private readonly __bridge__: BaseBridge
7
+
8
+ public static get CLASS_PATH(): string {
9
+ if (!Reflect.getMetadata(MULTIPLE_RUNNER__CLASS_PATH, this)) {
10
+ const cachedModules = require('module')._cache
11
+ for (const modulePath of Object.keys(cachedModules)) {
12
+ const cachedModule = cachedModules[modulePath]
13
+ if (typeof cachedModule === 'object') {
14
+ const exports = cachedModule.exports
15
+ if (exports && typeof exports === 'object') {
16
+ for (const exportName of Object.keys(exports)) {
17
+ if (typeof exports[exportName] === 'function') {
18
+ if (exports[exportName] === this) {
19
+ Reflect.defineMetadata(MULTIPLE_RUNNER__CLASS_PATH, modulePath, this)
20
+ }
21
+ }
22
+ }
23
+ }
24
+ }
25
+ }
26
+ }
27
+ return Reflect.getMetadata(MULTIPLE_RUNNER__CLASS_PATH, this)
28
+ }
29
+
30
+ public static get CLASS_NAME(): string {
31
+ if (!Reflect.getMetadata(MULTIPLE_RUNNER_CLASS_NAME, this)) {
32
+ Reflect.defineMetadata(MULTIPLE_RUNNER_CLASS_NAME, this.name, this)
33
+ }
34
+ return Reflect.getMetadata(MULTIPLE_RUNNER_CLASS_NAME, this)
35
+ }
36
+
37
+ constructor(bridge: BaseBridge) {
38
+ this.__bridge__ = bridge
39
+ }
40
+
41
+ protected async setup(): Promise<void> {
42
+ //Empty setup method
43
+ }
44
+
45
+ public off(event: string): void {
46
+ this.__bridge__.off(event)
47
+ }
48
+
49
+ public on(event: 'property-changed', handler: (property: string, value: any) => void): void
50
+ public on(event: 'error', handler: (error: Error) => void): void
51
+ public on(event: string, handler: (...args: any[]) => void): void
52
+ public on(event: string, handler: (...args: any[]) => void): void {
53
+ this.__bridge__.on(event, handler)
54
+ }
55
+
56
+ public once(event: 'property-changed', handler: (property: string, value: any) => void): void
57
+ public once(event: 'error', handler: (error: Error) => void): void
58
+ public once(event: string, handler: (...args: any[]) => void): void
59
+ public once(event: string, handler: (...args: any[]) => void): void {
60
+ this.__bridge__.once(event, handler)
61
+ }
62
+
63
+ public emit(event: string, ...args: any[]): void {
64
+ this.__bridge__.send.apply(this.__bridge__, [event].concat(args) as any)
65
+ }
66
+ }
@@ -1,5 +1,5 @@
1
1
  import {Plugin} from '../base/Plugin'
2
- import moment from 'moment'
2
+ import momentTimezone from 'moment-timezone'
3
3
  import {Configurable} from '../decorators/DependencyInjection'
4
4
 
5
5
  declare module '../Core' {
@@ -9,6 +9,11 @@ declare module '../Core' {
9
9
  }
10
10
 
11
11
  export class Formatter extends Plugin {
12
+
13
+ protected get timezone(): string {
14
+ return <string>process.env.TZ
15
+ }
16
+
12
17
  /**
13
18
  * 默认时间格式
14
19
  * @type {string}
@@ -23,7 +28,7 @@ export class Formatter extends Plugin {
23
28
  * @returns {number}
24
29
  */
25
30
  public asTimestamp(inp: string | number | Date = new Date()): number {
26
- return moment(inp).unix()
31
+ return momentTimezone.tz(inp, this.timezone).unix()
27
32
  }
28
33
 
29
34
  /**
@@ -32,7 +37,7 @@ export class Formatter extends Plugin {
32
37
  * @returns {number}
33
38
  */
34
39
  public asMillisecondTimestamp(inp: string | number | Date = new Date()): number {
35
- return moment(inp).valueOf()
40
+ return momentTimezone.tz(inp, this.timezone).valueOf()
36
41
  }
37
42
 
38
43
  /**
@@ -41,7 +46,7 @@ export class Formatter extends Plugin {
41
46
  * @returns {Date}
42
47
  */
43
48
  public asDate(inp: string | number | Date): Date {
44
- return moment(inp).toDate()
49
+ return momentTimezone.tz(inp, this.timezone).toDate()
45
50
  }
46
51
 
47
52
  /**
@@ -51,7 +56,7 @@ export class Formatter extends Plugin {
51
56
  * @returns {string}
52
57
  */
53
58
  public asDateText(inp: Date | string | number = new Date(), format?: string): string {
54
- return moment(inp).format(format ? format : this.dateFormat)
59
+ return momentTimezone.tz(inp, this.timezone).format(format ? format : this.dateFormat)
55
60
  }
56
61
 
57
62
  /**
@@ -4,6 +4,7 @@ import {Configurable} from '../decorators/DependencyInjection'
4
4
  import * as Transport from 'winston-transport'
5
5
  import {format as utilFormat} from 'util'
6
6
  import {ConsoleTransportOptions} from 'winston/lib/winston/transports'
7
+ import momentTimezone from 'moment-timezone'
7
8
 
8
9
  declare module '../Core' {
9
10
  interface Application {
@@ -27,10 +28,10 @@ export class Logger extends Plugin {
27
28
  protected readonly console: ConsoleTransportOptions = {
28
29
  silent: false,
29
30
  format: format.combine(
30
- format.timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
31
+ format.timestamp(),
31
32
  format.colorize(),
32
33
  format.printf(packet => {
33
- return `${packet.timestamp} ${packet.level}: ${packet.message}`
34
+ return `${momentTimezone.tz(packet.timestamp, <string>process.env.TZ).format('YYYY-MM-DD HH:mm:ss')} ${packet.level}: ${packet.message}`
34
35
  })
35
36
  )
36
37
  }
@@ -29,6 +29,7 @@ import {TestException} from './exceptions/TestException'
29
29
  const config: IApplicationConfig = {
30
30
  id: 'test.app',
31
31
  name: 'test app',
32
+ timezone: 'US/Alpine',
32
33
  processes: [TestProcess],
33
34
  threads: [TestThread],
34
35
  plugins: [
@@ -38,12 +39,12 @@ import {TestException} from './exceptions/TestException'
38
39
  },
39
40
  {
40
41
  class: Formatter,
41
- dateFormat: 'YYYY'
42
+ dateFormat: 'YYYY-MM-DD HH:mm:ss'
42
43
  },
43
- {
44
- class: Logger,
45
- level: 'debug'
46
- }
44
+ // {
45
+ // class: Logger,
46
+ // level: 'debug'
47
+ // }
47
48
  ],
48
49
  components: {
49
50
  tc1: {
@@ -147,7 +148,10 @@ import {TestException} from './exceptions/TestException'
147
148
  app.Logger.debug('this is debug')
148
149
  // new TestThread()
149
150
  const tt = app.Threads.get(TestThread)
150
- await tt.test()
151
+ const threadResult = await tt.test('a', 'b', 'c', (timestamp: number) => {
152
+ console.log('fn', timestamp)
153
+ })
154
+ console.log('==========> threadResult:', threadResult)
151
155
  await app.Threads.destroy(TestThread)
152
156
  const tp = app.Processes.get(TestProcess)
153
157
  await tp.procTest()
@@ -5,7 +5,9 @@ export class TestThread extends Thread {
5
5
  console.log('this is test thread')
6
6
  }
7
7
 
8
- public async test() {
8
+ public async test(a, b, c, callback) {
9
9
  console.log('this is thread test function')
10
+ callback(Date.now())
11
+ return a + b + c + 'sfsdfasdfasdfasdfasdfasdf'
10
12
  }
11
13
  }
@@ -2,4 +2,4 @@ import {IConstructor} from '../interfaces/IConstructor'
2
2
  import {Plugin} from '../base/Plugin'
3
3
 
4
4
  export type TPluginsConfig<TPlugin extends Plugin = Plugin> =
5
- ({ class: IConstructor<TPlugin>, [propertyKey: string]: any } | IConstructor<TPlugin>)[]
5
+ ({ class: IConstructor<TPlugin>, scope?: 'Transient' | 'Singleton'; [propertyKey: string]: any } | IConstructor<TPlugin>)[]