@lakutata/core 1.0.25 → 1.0.29
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.
- package/README.md +4 -0
- package/build/Application.d.ts +3 -0
- package/build/Application.js +16 -0
- package/build/Application.js.map +1 -1
- package/build/ApplicationManagement.js +5 -0
- package/build/ApplicationManagement.js.map +1 -1
- package/build/base/process/Process.d.ts +1 -1
- package/build/base/process/Process.js +2 -2
- package/build/base/process/Process.js.map +1 -1
- package/build/base/process/ProcessManager.d.ts +1 -1
- package/build/base/process/ProcessManager.js +2 -2
- package/build/base/process/ProcessManager.js.map +1 -1
- package/build/base/thread/Thread.d.ts +1 -1
- package/build/base/thread/Thread.js +2 -2
- package/build/base/thread/Thread.js.map +1 -1
- package/build/base/thread/ThreadManager.d.ts +1 -1
- package/build/base/thread/ThreadManager.js +2 -2
- package/build/base/thread/ThreadManager.js.map +1 -1
- package/build/interfaces/IApplicationConfig.d.ts +1 -0
- package/build/lib/multipleRunner/constants/Channel.d.ts +5 -0
- package/build/lib/multipleRunner/constants/Channel.js +9 -0
- package/build/lib/multipleRunner/constants/Channel.js.map +1 -0
- package/build/lib/multipleRunner/constants/MetadataKey.d.ts +2 -0
- package/build/lib/multipleRunner/constants/MetadataKey.js +6 -0
- package/build/lib/multipleRunner/constants/MetadataKey.js.map +1 -0
- package/build/lib/multipleRunner/containers/ContainerVolume.d.ts +4 -0
- package/build/lib/multipleRunner/containers/ContainerVolume.js +85 -0
- package/build/lib/multipleRunner/containers/ContainerVolume.js.map +1 -0
- package/build/lib/multipleRunner/definitions/interfaces/ClassConstructor.d.ts +3 -0
- package/build/lib/multipleRunner/definitions/interfaces/ClassConstructor.js +3 -0
- package/build/lib/multipleRunner/definitions/interfaces/ClassConstructor.js.map +1 -0
- package/build/lib/multipleRunner/definitions/interfaces/MultipleRunnerBaseClassConstructor.d.ts +4 -0
- package/build/lib/multipleRunner/definitions/interfaces/MultipleRunnerBaseClassConstructor.js +3 -0
- package/build/lib/multipleRunner/definitions/interfaces/MultipleRunnerBaseClassConstructor.js.map +1 -0
- package/build/lib/multipleRunner/definitions/interfaces/ProcessClassConstructor.d.ts +7 -0
- package/build/lib/multipleRunner/definitions/interfaces/ProcessClassConstructor.js +3 -0
- package/build/lib/multipleRunner/definitions/interfaces/ProcessClassConstructor.js.map +1 -0
- package/build/lib/multipleRunner/definitions/interfaces/ProxyClass.d.ts +3 -0
- package/build/lib/multipleRunner/definitions/interfaces/ProxyClass.js +3 -0
- package/build/lib/multipleRunner/definitions/interfaces/ProxyClass.js.map +1 -0
- package/build/lib/multipleRunner/definitions/interfaces/ThreadClassConstructor.d.ts +7 -0
- package/build/lib/multipleRunner/definitions/interfaces/ThreadClassConstructor.js +3 -0
- package/build/lib/multipleRunner/definitions/interfaces/ThreadClassConstructor.js.map +1 -0
- package/build/lib/multipleRunner/lib/Process.d.ts +5 -0
- package/build/lib/multipleRunner/lib/Process.js +44 -0
- package/build/lib/multipleRunner/lib/Process.js.map +1 -0
- package/build/lib/multipleRunner/lib/Thread.d.ts +5 -0
- package/build/lib/multipleRunner/lib/Thread.js +52 -0
- package/build/lib/multipleRunner/lib/Thread.js.map +1 -0
- package/build/lib/multipleRunner/lib/bridges/BaseBridge.d.ts +32 -0
- package/build/lib/multipleRunner/lib/bridges/BaseBridge.js +104 -0
- package/build/lib/multipleRunner/lib/bridges/BaseBridge.js.map +1 -0
- package/build/lib/multipleRunner/lib/bridges/ProcessBridge.d.ts +5 -0
- package/build/lib/multipleRunner/lib/bridges/ProcessBridge.js +20 -0
- package/build/lib/multipleRunner/lib/bridges/ProcessBridge.js.map +1 -0
- package/build/lib/multipleRunner/lib/bridges/ThreadBridge.d.ts +5 -0
- package/build/lib/multipleRunner/lib/bridges/ThreadBridge.js +17 -0
- package/build/lib/multipleRunner/lib/bridges/ThreadBridge.js.map +1 -0
- package/build/lib/multipleRunner/lib/cores/GenerateExecArgv.d.ts +2 -0
- package/build/lib/multipleRunner/lib/cores/GenerateExecArgv.js +35 -0
- package/build/lib/multipleRunner/lib/cores/GenerateExecArgv.js.map +1 -0
- package/build/lib/multipleRunner/lib/cores/IsElectron.d.ts +1 -0
- package/build/lib/multipleRunner/lib/cores/IsElectron.js +9 -0
- package/build/lib/multipleRunner/lib/cores/IsElectron.js.map +1 -0
- package/build/lib/multipleRunner/lib/cores/ProcessClass.d.ts +5 -0
- package/build/lib/multipleRunner/lib/cores/ProcessClass.js +12 -0
- package/build/lib/multipleRunner/lib/cores/ProcessClass.js.map +1 -0
- package/build/lib/multipleRunner/lib/cores/ThreadClass.d.ts +5 -0
- package/build/lib/multipleRunner/lib/cores/ThreadClass.js +12 -0
- package/build/lib/multipleRunner/lib/cores/ThreadClass.js.map +1 -0
- package/build/lib/multipleRunner/lib/cores/UniParentBridgeHandler.d.ts +2 -0
- package/build/lib/multipleRunner/lib/cores/UniParentBridgeHandler.js +104 -0
- package/build/lib/multipleRunner/lib/cores/UniParentBridgeHandler.js.map +1 -0
- package/build/lib/multipleRunner/lib/prototypes/MultipleRunnerBaseClass.d.ts +17 -0
- package/build/lib/multipleRunner/lib/prototypes/MultipleRunnerBaseClass.js +53 -0
- package/build/lib/multipleRunner/lib/prototypes/MultipleRunnerBaseClass.js.map +1 -0
- package/build/plugins/Formatter.d.ts +1 -0
- package/build/plugins/Formatter.js +8 -5
- package/build/plugins/Formatter.js.map +1 -1
- package/build/plugins/Logger.js +1 -1
- package/build/plugins/Logger.js.map +1 -1
- package/package.json +12 -11
- package/src/Application.ts +29 -0
- package/src/ApplicationManagement.ts +3 -0
- package/src/base/process/Process.ts +1 -1
- package/src/base/process/ProcessManager.ts +2 -2
- package/src/base/thread/Thread.ts +1 -1
- package/src/base/thread/ThreadManager.ts +2 -2
- package/src/interfaces/IApplicationConfig.ts +1 -0
- package/src/lib/multipleRunner/constants/Channel.ts +5 -0
- package/src/lib/multipleRunner/constants/MetadataKey.ts +2 -0
- package/src/lib/multipleRunner/containers/ContainerVolume.ts +83 -0
- package/src/lib/multipleRunner/definitions/interfaces/ClassConstructor.ts +3 -0
- package/src/lib/multipleRunner/definitions/interfaces/MultipleRunnerBaseClassConstructor.ts +5 -0
- package/src/lib/multipleRunner/definitions/interfaces/ProcessClassConstructor.ts +10 -0
- package/src/lib/multipleRunner/definitions/interfaces/ProxyClass.ts +3 -0
- package/src/lib/multipleRunner/definitions/interfaces/ThreadClassConstructor.ts +10 -0
- package/src/lib/multipleRunner/lib/Process.ts +44 -0
- package/src/lib/multipleRunner/lib/Thread.ts +52 -0
- package/src/lib/multipleRunner/lib/bridges/BaseBridge.ts +115 -0
- package/src/lib/multipleRunner/lib/bridges/ProcessBridge.ts +18 -0
- package/src/lib/multipleRunner/lib/bridges/ThreadBridge.ts +15 -0
- package/src/lib/multipleRunner/lib/cores/GenerateExecArgv.ts +28 -0
- package/src/lib/multipleRunner/lib/cores/IsElectron.ts +3 -0
- package/src/lib/multipleRunner/lib/cores/ProcessClass.ts +8 -0
- package/src/lib/multipleRunner/lib/cores/ThreadClass.ts +8 -0
- package/src/lib/multipleRunner/lib/cores/UniParentBridgeHandler.ts +97 -0
- package/src/lib/multipleRunner/lib/prototypes/MultipleRunnerBaseClass.ts +66 -0
- package/src/plugins/Formatter.ts +10 -5
- package/src/plugins/Logger.ts +1 -1
- package/src/tests/Main.spec.ts +6 -2
- package/src/tests/threads/TestThread.ts +3 -1
- package/.idea/dbnavigator.xml +0 -558
package/src/Application.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {Logger} from './plugins/Logger'
|
|
|
13
13
|
import {format} from 'winston'
|
|
14
14
|
import {TProcessesConfig} from './types/TProcessesConfig'
|
|
15
15
|
import {TThreadsConfig} from './types/TThreadsConfig'
|
|
16
|
+
import momentTimezone from 'moment-timezone'
|
|
16
17
|
|
|
17
18
|
export class Application extends Module {
|
|
18
19
|
/**
|
|
@@ -125,6 +126,18 @@ export class Application extends Module {
|
|
|
125
126
|
*/
|
|
126
127
|
protected readonly workflows: TWorkflowsConfig = []
|
|
127
128
|
|
|
129
|
+
/**
|
|
130
|
+
* 程序时区
|
|
131
|
+
* @type {string}
|
|
132
|
+
* @protected
|
|
133
|
+
*/
|
|
134
|
+
protected tz: string = (() => {
|
|
135
|
+
if (!process.env.TZ) {
|
|
136
|
+
process.env.TZ = momentTimezone.tz.guess()
|
|
137
|
+
}
|
|
138
|
+
return process.env.TZ
|
|
139
|
+
})()
|
|
140
|
+
|
|
128
141
|
/**
|
|
129
142
|
* 初始化
|
|
130
143
|
* @returns {Promise<void>}
|
|
@@ -137,6 +150,22 @@ export class Application extends Module {
|
|
|
137
150
|
})
|
|
138
151
|
}
|
|
139
152
|
|
|
153
|
+
/**
|
|
154
|
+
* 获取程序时区
|
|
155
|
+
* @returns {string}
|
|
156
|
+
*/
|
|
157
|
+
public getTimezone() {
|
|
158
|
+
return this.tz
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 设置程序时区
|
|
163
|
+
* @param {string} tz
|
|
164
|
+
*/
|
|
165
|
+
public setTimezone(tz: string) {
|
|
166
|
+
this.tz = tz
|
|
167
|
+
}
|
|
168
|
+
|
|
140
169
|
/**
|
|
141
170
|
* 获取应用程序ID
|
|
142
171
|
* @returns {string}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import momentTimezone from 'moment-timezone'
|
|
2
|
+
|
|
1
3
|
require('events').captureRejections = true
|
|
2
4
|
import {IApplicationConfig} from './interfaces/IApplicationConfig'
|
|
3
5
|
import {Application} from './Application'
|
|
@@ -19,6 +21,7 @@ export async function createApp(config: IApplicationConfig): Promise<Application
|
|
|
19
21
|
process.title = config.id
|
|
20
22
|
process.env.appId = config.id
|
|
21
23
|
process.env.appName = config.name
|
|
24
|
+
process.env.TZ = config.timezone ? config.timezone : momentTimezone.tz.guess()
|
|
22
25
|
await coreContainer.initialize()
|
|
23
26
|
return coreContainer.getModule(Application)
|
|
24
27
|
} catch (e) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {Process} from './Process'
|
|
2
|
-
import {ProxyClass} from 'ergate'
|
|
3
2
|
import {IProcessConstructor} from '../../interfaces/IProcessConstructor'
|
|
4
|
-
import {Process as processCreate} from 'ergate'
|
|
5
3
|
import {ProcessException} from '../../exceptions/ProcessException'
|
|
4
|
+
import {ProxyClass} from '../../lib/multipleRunner/definitions/interfaces/ProxyClass'
|
|
5
|
+
import {Process as processCreate} from '../../lib/multipleRunner/lib/Process'
|
|
6
6
|
|
|
7
7
|
export class ProcessManager<TProcess extends Process = Process> {
|
|
8
8
|
protected readonly processMap: Map<IProcessConstructor<TProcess>, TProcess & ProxyClass> = new Map()
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {Thread} from './Thread'
|
|
2
2
|
import {IThreadConstructor} from '../../interfaces/IThreadConstructor'
|
|
3
|
-
import {Thread as threadCreate} from 'ergate'
|
|
4
3
|
import {ThreadException} from '../../exceptions/ThreadException'
|
|
5
|
-
import {ProxyClass} from '
|
|
4
|
+
import {ProxyClass} from '../../lib/multipleRunner/definitions/interfaces/ProxyClass'
|
|
5
|
+
import {Thread as threadCreate} from '../../lib/multipleRunner/lib/Thread'
|
|
6
6
|
|
|
7
7
|
export class ThreadManager<TThread extends Thread = Thread> {
|
|
8
8
|
protected readonly threadMap: Map<IThreadConstructor<TThread>, TThread & ProxyClass> = new Map()
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export const READY: string = '_____%$ready$%_____'
|
|
2
|
+
export const SETUP_ERROR: string = '_____%$setup_error$%_____'
|
|
3
|
+
export const INVOKE: string = '__%$invoke$%__'
|
|
4
|
+
export const SET_PROPERTY: string = '_____%$set_property$%_____'
|
|
5
|
+
export const PROPERTY_CHANGE: string = '_____%$property_change$%_____'
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import {Volume} from 'memfs'
|
|
2
|
+
|
|
3
|
+
export const BASE_RUNTIME_CONTAINER_FILE: string = '/multipleRunner/container/base/BaseRuntimeContainer.js'
|
|
4
|
+
export const PROCESS_RUNTIME_CONTAINER_FILE: string = '/multipleRunner/container/ProcessRuntimeContainer.js'
|
|
5
|
+
export const THREAD_RUNTIME_CONTAINER_FILE: string = '/multipleRunner/container/ThreadRuntimeContainer.js'
|
|
6
|
+
|
|
7
|
+
export const ContainerVolume = Volume.fromJSON({
|
|
8
|
+
[BASE_RUNTIME_CONTAINER_FILE]: `
|
|
9
|
+
module.exports.BaseRuntimeContainer = function (bridge) {
|
|
10
|
+
bridge.on('create', (name, path) => {
|
|
11
|
+
const module = require(path)[name]
|
|
12
|
+
const moduleInstance = new module(bridge)
|
|
13
|
+
moduleInstance.setup().then(() => {
|
|
14
|
+
const properties = {}
|
|
15
|
+
const moduleInstancePropertiesObject = {}
|
|
16
|
+
for (const propertyName of Object.keys(moduleInstance)) {
|
|
17
|
+
properties[propertyName] = typeof moduleInstance[propertyName]
|
|
18
|
+
if (propertyName !== '__bridge__')
|
|
19
|
+
moduleInstancePropertiesObject[propertyName] = moduleInstance[propertyName]
|
|
20
|
+
}
|
|
21
|
+
const moduleInstanceProxy = new Proxy(moduleInstance, {
|
|
22
|
+
set(target, p, value) {
|
|
23
|
+
moduleInstance[p] = value
|
|
24
|
+
bridge.propertyChange(p.toString(), value)
|
|
25
|
+
return true
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
bridge.onSet((property, value) => {
|
|
29
|
+
moduleInstanceProxy[property] = value
|
|
30
|
+
})
|
|
31
|
+
bridge.ready(properties, JSON.parse(JSON.stringify(moduleInstancePropertiesObject)))
|
|
32
|
+
bridge.onInvoke(async (method, args) => {
|
|
33
|
+
const _args = []
|
|
34
|
+
const callbackSignature = /^__________%\\$CALLBACK\\[\\d+]\\$%__________$/
|
|
35
|
+
for (const arg of args) {
|
|
36
|
+
if (typeof arg === 'string' && callbackSignature.test(arg)) {
|
|
37
|
+
_args.push((...args) => {
|
|
38
|
+
bridge.onCallback(arg, args)
|
|
39
|
+
})
|
|
40
|
+
} else {
|
|
41
|
+
_args.push(arg)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return await moduleInstanceProxy[method].apply(moduleInstanceProxy, _args)
|
|
45
|
+
})
|
|
46
|
+
}).catch(setupError => {
|
|
47
|
+
bridge.setupError(setupError)
|
|
48
|
+
process.exit(1)
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
`,
|
|
53
|
+
[PROCESS_RUNTIME_CONTAINER_FILE]: `
|
|
54
|
+
const reversedArgvs = process.argv.reverse()
|
|
55
|
+
require(reversedArgvs[0])
|
|
56
|
+
const {ProcessBridge} = require(reversedArgvs[0])
|
|
57
|
+
const {BaseRuntimeContainer} = require(reversedArgvs[1])
|
|
58
|
+
BaseRuntimeContainer(new ProcessBridge(process))
|
|
59
|
+
`,
|
|
60
|
+
[THREAD_RUNTIME_CONTAINER_FILE]: `
|
|
61
|
+
const reversedArgvs = process.argv.reverse()
|
|
62
|
+
if(reversedArgvs[3]!=='undefined'){
|
|
63
|
+
try{require(reversedArgvs[3])}catch(e){}
|
|
64
|
+
}
|
|
65
|
+
const {parentPort} = require('worker_threads')
|
|
66
|
+
let _ThreadBridge
|
|
67
|
+
try{
|
|
68
|
+
const {ThreadBridge} = require(reversedArgvs[0])
|
|
69
|
+
_ThreadBridge = ThreadBridge
|
|
70
|
+
}catch(e){
|
|
71
|
+
const tsNodeRegExp=new RegExp("ts-node")
|
|
72
|
+
for(const reversedArgv of reversedArgvs){
|
|
73
|
+
if(tsNodeRegExp.test(reversedArgv)){
|
|
74
|
+
require(reversedArgv)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const {ThreadBridge} = require(reversedArgvs[0])
|
|
78
|
+
_ThreadBridge = ThreadBridge
|
|
79
|
+
}
|
|
80
|
+
const {BaseRuntimeContainer} = require(reversedArgvs[1])
|
|
81
|
+
BaseRuntimeContainer(new _ThreadBridge(parentPort))
|
|
82
|
+
`
|
|
83
|
+
})
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import {ProcessClass} from '../../lib/cores/ProcessClass'
|
|
2
|
+
import {ClassConstructor} from './ClassConstructor'
|
|
3
|
+
|
|
4
|
+
export interface ProcessClassConstructor<T extends ProcessClass> extends ClassConstructor<T> {
|
|
5
|
+
new(...args: any[]): T
|
|
6
|
+
|
|
7
|
+
CLASS_PATH: string
|
|
8
|
+
|
|
9
|
+
CLASS_NAME: string
|
|
10
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import {ThreadClass} from '../../lib/cores/ThreadClass'
|
|
2
|
+
import {ClassConstructor} from './ClassConstructor'
|
|
3
|
+
|
|
4
|
+
export interface ThreadClassConstructor<T extends ThreadClass> extends ClassConstructor<T> {
|
|
5
|
+
new(...args: any[]): T
|
|
6
|
+
|
|
7
|
+
CLASS_PATH: string
|
|
8
|
+
|
|
9
|
+
CLASS_NAME: string
|
|
10
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import 'reflect-metadata'
|
|
2
|
+
import {ProcessClassConstructor} from '../definitions/interfaces/ProcessClassConstructor'
|
|
3
|
+
import {ChildProcess, fork} from 'child_process'
|
|
4
|
+
import path from 'path'
|
|
5
|
+
import {GenerateExecArgv} from './cores/GenerateExecArgv'
|
|
6
|
+
import {ProcessBridge} from './bridges/ProcessBridge'
|
|
7
|
+
import {ProcessClass} from './cores/ProcessClass'
|
|
8
|
+
import {UniParentBridgeHandler} from './cores/UniParentBridgeHandler'
|
|
9
|
+
import {ProxyClass} from '../definitions/interfaces/ProxyClass'
|
|
10
|
+
import {
|
|
11
|
+
BASE_RUNTIME_CONTAINER_FILE,
|
|
12
|
+
ContainerVolume,
|
|
13
|
+
PROCESS_RUNTIME_CONTAINER_FILE
|
|
14
|
+
} from '../containers/ContainerVolume'
|
|
15
|
+
import fs from 'fs'
|
|
16
|
+
import os from 'os'
|
|
17
|
+
|
|
18
|
+
export async function Process<T extends ProcessClass>(processClassConstructor: ProcessClassConstructor<T>): Promise<T & ProxyClass> {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
ProcessProxy(processClassConstructor, (setupError, instance) => {
|
|
21
|
+
if (setupError) {
|
|
22
|
+
reject(setupError)
|
|
23
|
+
} else {
|
|
24
|
+
resolve(<T & ProxyClass>instance)
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function ProcessProxy<T extends ProcessClass>(processClassConstructor: ProcessClassConstructor<T>, callback: (setupError: Error, instance: T) => void) {
|
|
31
|
+
const className: string = processClassConstructor.CLASS_NAME
|
|
32
|
+
const classPath: string = processClassConstructor.CLASS_PATH
|
|
33
|
+
const tmpProcessRuntimeContainerFilename: string = path.resolve(os.tmpdir(), `./PRC_TMP_${Date.now()}.tmp`)
|
|
34
|
+
const tmpBaseRuntimeContainerFilename: string = path.resolve(os.tmpdir(), `./PBRC_TMP_${Date.now()}.tmp`)
|
|
35
|
+
fs.writeFileSync(tmpBaseRuntimeContainerFilename, ContainerVolume.readFileSync(BASE_RUNTIME_CONTAINER_FILE).toString())
|
|
36
|
+
fs.writeFileSync(tmpProcessRuntimeContainerFilename, ContainerVolume.readFileSync(PROCESS_RUNTIME_CONTAINER_FILE).toString())
|
|
37
|
+
const childProcess: ChildProcess = fork(tmpProcessRuntimeContainerFilename, [
|
|
38
|
+
tmpBaseRuntimeContainerFilename,
|
|
39
|
+
require.resolve(path.resolve(__dirname, './bridges/ProcessBridge'))
|
|
40
|
+
], {
|
|
41
|
+
execArgv: GenerateExecArgv(classPath)
|
|
42
|
+
})
|
|
43
|
+
UniParentBridgeHandler(new ProcessBridge(childProcess), className, classPath, processClassConstructor, callback)
|
|
44
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import 'reflect-metadata'
|
|
2
|
+
import {ThreadClassConstructor} from '../definitions/interfaces/ThreadClassConstructor'
|
|
3
|
+
import {ThreadClass} from './cores/ThreadClass'
|
|
4
|
+
import {Worker} from 'worker_threads'
|
|
5
|
+
import path from 'path'
|
|
6
|
+
import {ThreadBridge} from './bridges/ThreadBridge'
|
|
7
|
+
import {GenerateExecArgv} from './cores/GenerateExecArgv'
|
|
8
|
+
import {UniParentBridgeHandler} from './cores/UniParentBridgeHandler'
|
|
9
|
+
import {ProxyClass} from '../definitions/interfaces/ProxyClass'
|
|
10
|
+
import os from 'os'
|
|
11
|
+
import fs from 'fs'
|
|
12
|
+
import {
|
|
13
|
+
BASE_RUNTIME_CONTAINER_FILE,
|
|
14
|
+
ContainerVolume,
|
|
15
|
+
THREAD_RUNTIME_CONTAINER_FILE
|
|
16
|
+
} from '../containers/ContainerVolume'
|
|
17
|
+
import {Process} from './Process'
|
|
18
|
+
import {isElectron} from './cores/IsElectron'
|
|
19
|
+
|
|
20
|
+
export async function Thread<T extends ThreadClass>(threadClassConstructor: ThreadClassConstructor<T>): Promise<T & ProxyClass> {
|
|
21
|
+
if (isElectron()) {
|
|
22
|
+
return await Process(threadClassConstructor as any) as any
|
|
23
|
+
} else {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
ThreadProxy(threadClassConstructor, (setupError, instance) => {
|
|
26
|
+
if (setupError) {
|
|
27
|
+
reject(setupError)
|
|
28
|
+
} else {
|
|
29
|
+
resolve(<T & ProxyClass>instance)
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function ThreadProxy<T extends ThreadClass>(threadClassConstructor: ThreadClassConstructor<T>, callback: (setupError: Error, instance: T) => void) {
|
|
37
|
+
const className: string = threadClassConstructor.CLASS_NAME
|
|
38
|
+
const classPath: string = threadClassConstructor.CLASS_PATH
|
|
39
|
+
const tmpBaseRuntimeContainerFilename: string = path.resolve(os.tmpdir(), `./TBRC_TMP_${Date.now()}.tmp`)
|
|
40
|
+
fs.writeFileSync(tmpBaseRuntimeContainerFilename, ContainerVolume.readFileSync(BASE_RUNTIME_CONTAINER_FILE).toString())
|
|
41
|
+
const exeArgv = GenerateExecArgv(classPath).reverse()
|
|
42
|
+
const worker: Worker = new Worker(ContainerVolume.readFileSync(THREAD_RUNTIME_CONTAINER_FILE).toString(), {
|
|
43
|
+
eval: true,
|
|
44
|
+
argv: [
|
|
45
|
+
exeArgv.length > 0 ? exeArgv[0] : 'undefined',
|
|
46
|
+
tmpBaseRuntimeContainerFilename,
|
|
47
|
+
require.resolve(path.resolve(__dirname, './bridges/ThreadBridge'))
|
|
48
|
+
]
|
|
49
|
+
})
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
UniParentBridgeHandler(new ThreadBridge(worker), className, classPath, threadClassConstructor, callback)
|
|
52
|
+
}
|
|
@@ -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,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
|
+
}
|