@lvce-editor/extension-host-helper-process 0.11.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.
- package/LICENSE +21 -0
- package/index.js +10 -0
- package/package.json +22 -0
- package/src/extensionHostHelperProcessMain.js +10 -0
- package/src/parts/Assert/Assert.js +64 -0
- package/src/parts/Command/Command.js +82 -0
- package/src/parts/CreateProcessIpc/CreateProcessIpc.js +16 -0
- package/src/parts/CreateWebSocketIpc/CreateWebSocketIpc.js +23 -0
- package/src/parts/Exec/Exec.ipc.js +7 -0
- package/src/parts/Exec/Exec.js +13 -0
- package/src/parts/GetWebSocket/GetWebSocket.js +25 -0
- package/src/parts/IpcChild/IpcChild.js +21 -0
- package/src/parts/IpcChild/IpcChildWithElectronMessagePort.js +11 -0
- package/src/parts/IpcChild/IpcChildWithMessagePort.js +4 -0
- package/src/parts/IpcChild/IpcChildWithParent.js +5 -0
- package/src/parts/IpcChild/IpcChildWithWebSocket.js +10 -0
- package/src/parts/IpcChildType/IpcChildType.js +21 -0
- package/src/parts/Json/Json.js +7 -0
- package/src/parts/JsonRpc/JsonRpc.js +3 -0
- package/src/parts/Module/Module.js +10 -0
- package/src/parts/ModuleId/ModuleId.js +1 -0
- package/src/parts/ModuleMap/ModuleMap.js +10 -0
- package/src/parts/Rpc/Rpc.js +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-present, Le Vivilet
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { dirname, join } from 'node:path'
|
|
2
|
+
import { fileURLToPath } from 'node:url'
|
|
3
|
+
|
|
4
|
+
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
5
|
+
|
|
6
|
+
export const extensionHostHelperProcessPath = join(
|
|
7
|
+
__dirname,
|
|
8
|
+
'src',
|
|
9
|
+
'extensionHostHelperProcessMain.js'
|
|
10
|
+
)
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lvce-editor/extension-host-helper-process",
|
|
3
|
+
"version": "0.11.2",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [],
|
|
8
|
+
"author": "",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/lvce-editor/lvce-editor.git",
|
|
13
|
+
"directory": "packages/extension-host-helper-process"
|
|
14
|
+
},
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=16"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"execa": "^6.1.0",
|
|
20
|
+
"ws": "^8.11.0"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as IpcChild from './parts/IpcChild/IpcChild.js'
|
|
2
|
+
import * as IpcChildType from './parts/IpcChildType/IpcChildType.js'
|
|
3
|
+
import * as Rpc from './parts/Rpc/Rpc.js'
|
|
4
|
+
|
|
5
|
+
const main = async () => {
|
|
6
|
+
const ipc = await IpcChild.listen({ method: IpcChildType.Auto() })
|
|
7
|
+
Rpc.listen(ipc)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
main()
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
class AssertionError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message)
|
|
4
|
+
this.name = 'AssertionError'
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const getType = (value) => {
|
|
9
|
+
switch (typeof value) {
|
|
10
|
+
case 'number':
|
|
11
|
+
return 'number'
|
|
12
|
+
case 'function':
|
|
13
|
+
return 'function'
|
|
14
|
+
case 'string':
|
|
15
|
+
return 'string'
|
|
16
|
+
case 'object':
|
|
17
|
+
if (value === null) {
|
|
18
|
+
return 'null'
|
|
19
|
+
}
|
|
20
|
+
if (Array.isArray(value)) {
|
|
21
|
+
return 'array'
|
|
22
|
+
}
|
|
23
|
+
return 'object'
|
|
24
|
+
case 'boolean':
|
|
25
|
+
return 'boolean'
|
|
26
|
+
default:
|
|
27
|
+
return 'unknown'
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const object = (value) => {
|
|
32
|
+
const type = getType(value)
|
|
33
|
+
if (type !== 'object') {
|
|
34
|
+
throw new AssertionError('expected value to be of type object')
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const number = (value) => {
|
|
39
|
+
const type = getType(value)
|
|
40
|
+
if (type !== 'number') {
|
|
41
|
+
throw new AssertionError('expected value to be of type number')
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const array = (value) => {
|
|
46
|
+
const type = getType(value)
|
|
47
|
+
if (type !== 'array') {
|
|
48
|
+
throw new AssertionError('expected value to be of type array')
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const string = (value) => {
|
|
53
|
+
const type = getType(value)
|
|
54
|
+
if (type !== 'string') {
|
|
55
|
+
throw new AssertionError('expected value to be of type string')
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const boolean = (value) => {
|
|
60
|
+
const type = getType(value)
|
|
61
|
+
if (type !== 'boolean') {
|
|
62
|
+
throw new AssertionError('expected value to be of type boolean')
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import * as Module from '../Module/Module.js'
|
|
2
|
+
import * as ModuleMap from '../ModuleMap/ModuleMap.js'
|
|
3
|
+
|
|
4
|
+
const commands = Object.create(null)
|
|
5
|
+
const pendingModules = Object.create(null)
|
|
6
|
+
|
|
7
|
+
const initializeModule = (module) => {
|
|
8
|
+
if (module.Commands) {
|
|
9
|
+
for (const [key, value] of Object.entries(module.Commands)) {
|
|
10
|
+
if (module.name) {
|
|
11
|
+
const actualKey = `${module.name}.${key}`
|
|
12
|
+
register(actualKey, value)
|
|
13
|
+
} else {
|
|
14
|
+
register(key, value)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
throw new Error(`module ${module.name} is missing commands`)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const getOrLoadModule = (moduleId) => {
|
|
23
|
+
if (!pendingModules[moduleId]) {
|
|
24
|
+
const importPromise = Module.load(moduleId)
|
|
25
|
+
pendingModules[moduleId] = importPromise
|
|
26
|
+
.then(initializeModule)
|
|
27
|
+
.catch((error) => {
|
|
28
|
+
console.error(error)
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
return pendingModules[moduleId]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const loadCommand = (command) => getOrLoadModule(ModuleMap.getModuleId(command))
|
|
35
|
+
|
|
36
|
+
export const register = (commandId, listener) => {
|
|
37
|
+
commands[commandId] = listener
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const invoke = async (command, ...args) => {
|
|
41
|
+
if (!(command in commands)) {
|
|
42
|
+
await loadCommand(command)
|
|
43
|
+
if (!(command in commands)) {
|
|
44
|
+
console.warn(
|
|
45
|
+
`[extension host helper process] Unknown command "${command}"`
|
|
46
|
+
)
|
|
47
|
+
throw new Error(`Command ${command} not found`)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (typeof commands[command] !== 'function') {
|
|
51
|
+
throw new TypeError(`Command ${command} is not a function`)
|
|
52
|
+
}
|
|
53
|
+
return commands[command](...args)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const execute = (command, ...args) => {
|
|
57
|
+
if (command in commands) {
|
|
58
|
+
return commands[command](...args)
|
|
59
|
+
} else {
|
|
60
|
+
return (
|
|
61
|
+
loadCommand(command)
|
|
62
|
+
// TODO can skip then block in prod (only to prevent endless loop in dev)
|
|
63
|
+
.then(() => {
|
|
64
|
+
if (!(command in commands)) {
|
|
65
|
+
console.warn(`Unknown command "${command}"`)
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
execute(command, ...args)
|
|
70
|
+
} catch (error) {
|
|
71
|
+
console.error(
|
|
72
|
+
'[extension host helper process] command failed to execute'
|
|
73
|
+
)
|
|
74
|
+
console.error(error)
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
.catch((error) => {
|
|
78
|
+
console.error(error)
|
|
79
|
+
})
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const createProcessIpc = (process) => {
|
|
2
|
+
return {
|
|
3
|
+
send(message) {
|
|
4
|
+
process.send(message)
|
|
5
|
+
},
|
|
6
|
+
on(event, listener) {
|
|
7
|
+
switch (event) {
|
|
8
|
+
case 'message':
|
|
9
|
+
process.on('message', listener)
|
|
10
|
+
break
|
|
11
|
+
default:
|
|
12
|
+
throw new Error('unknown event listener type')
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as Json from '../Json/Json.js'
|
|
2
|
+
|
|
3
|
+
export const createWebSocketIpc = (webSocket) => {
|
|
4
|
+
return {
|
|
5
|
+
send(message) {
|
|
6
|
+
const stringifiedMessage = Json.stringify(message)
|
|
7
|
+
webSocket.send(stringifiedMessage)
|
|
8
|
+
},
|
|
9
|
+
on(event, listener) {
|
|
10
|
+
switch (event) {
|
|
11
|
+
case 'message':
|
|
12
|
+
const wrappedListener = (message) => {
|
|
13
|
+
const parsed = Json.parse(message.toString())
|
|
14
|
+
listener(parsed)
|
|
15
|
+
}
|
|
16
|
+
webSocket.on('message', wrappedListener)
|
|
17
|
+
break
|
|
18
|
+
default:
|
|
19
|
+
throw new Error('unknown event listener type')
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { execa } from 'execa'
|
|
2
|
+
import * as Assert from '../Assert/Assert.js'
|
|
3
|
+
|
|
4
|
+
export const exec = async (file, args, options) => {
|
|
5
|
+
Assert.string(file)
|
|
6
|
+
Assert.array(args)
|
|
7
|
+
const { stdout, stderr, exitCode } = await execa(file, args, options)
|
|
8
|
+
return {
|
|
9
|
+
stdout,
|
|
10
|
+
stderr,
|
|
11
|
+
exitCode,
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as _ws from 'ws'
|
|
2
|
+
|
|
3
|
+
export const getWebSocket = async (message, handle) => {
|
|
4
|
+
// workaround for jest or node bug
|
|
5
|
+
const WebSocketServer = _ws.WebSocketServer
|
|
6
|
+
? _ws.WebSocketServer
|
|
7
|
+
: // @ts-ignore
|
|
8
|
+
_ws.default.WebSocketServer
|
|
9
|
+
|
|
10
|
+
const webSocketServer = new WebSocketServer({
|
|
11
|
+
noServer: true,
|
|
12
|
+
})
|
|
13
|
+
const webSocket = await new Promise((resolve, reject) => {
|
|
14
|
+
const upgradeCallback = (webSocket) => {
|
|
15
|
+
resolve(webSocket)
|
|
16
|
+
}
|
|
17
|
+
webSocketServer.handleUpgrade(
|
|
18
|
+
message,
|
|
19
|
+
handle,
|
|
20
|
+
Buffer.alloc(0),
|
|
21
|
+
upgradeCallback
|
|
22
|
+
)
|
|
23
|
+
})
|
|
24
|
+
return webSocket
|
|
25
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as IpcChildType from '../IpcChildType/IpcChildType.js'
|
|
2
|
+
|
|
3
|
+
const getModule = (method) => {
|
|
4
|
+
switch (method) {
|
|
5
|
+
case IpcChildType.MessagePort:
|
|
6
|
+
return import('./IpcChildWithMessagePort.js')
|
|
7
|
+
case IpcChildType.WebSocket:
|
|
8
|
+
return import('./IpcChildWithWebSocket.js')
|
|
9
|
+
case IpcChildType.Parent:
|
|
10
|
+
return import('./IpcChildWithParent.js')
|
|
11
|
+
case IpcChildType.ElectronMessagePort:
|
|
12
|
+
return import('./IpcChildWithElectronMessagePort.js')
|
|
13
|
+
default:
|
|
14
|
+
throw new Error('unexpected ipc type')
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const listen = async ({ method }) => {
|
|
19
|
+
const module = await getModule(method)
|
|
20
|
+
return module.listen()
|
|
21
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { once } from 'events'
|
|
2
|
+
import * as CreateWebSocketIpc from '../CreateWebSocketIpc/CreateWebSocketIpc.js'
|
|
3
|
+
import * as GetWebSocket from '../GetWebSocket/GetWebSocket.js'
|
|
4
|
+
|
|
5
|
+
export const listen = async () => {
|
|
6
|
+
const [message, handle] = await once(process, 'message')
|
|
7
|
+
const webSocket = await GetWebSocket.getWebSocket(message, handle)
|
|
8
|
+
const ipc = CreateWebSocketIpc.createWebSocketIpc(webSocket)
|
|
9
|
+
return ipc
|
|
10
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const WebSocket = 1
|
|
2
|
+
export const MessagePort = 2
|
|
3
|
+
export const Parent = 3
|
|
4
|
+
export const ElectronMessagePort = 4
|
|
5
|
+
|
|
6
|
+
export const Auto = () => {
|
|
7
|
+
const { argv } = process
|
|
8
|
+
if (argv.includes('--ipc-type=websocket')) {
|
|
9
|
+
return WebSocket
|
|
10
|
+
}
|
|
11
|
+
if (argv.includes('--ipc-type=message-port')) {
|
|
12
|
+
return MessagePort
|
|
13
|
+
}
|
|
14
|
+
if (argv.includes('--ipc-type=parent')) {
|
|
15
|
+
return Parent
|
|
16
|
+
}
|
|
17
|
+
if (argv.includes('--ipc-type=electron-message-port')) {
|
|
18
|
+
return ElectronMessagePort
|
|
19
|
+
}
|
|
20
|
+
throw new Error('[extension-host-helper-process] unknown ipc type')
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const Exec = 1
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as Command from '../Command/Command.js'
|
|
2
|
+
import * as JsonRpc from '../JsonRpc/JsonRpc.js'
|
|
3
|
+
|
|
4
|
+
const getResponse = async (message) => {
|
|
5
|
+
try {
|
|
6
|
+
const result = await Command.invoke(message.method, ...message.params)
|
|
7
|
+
return {
|
|
8
|
+
jsonrpc: JsonRpc.Version,
|
|
9
|
+
id: message.id,
|
|
10
|
+
result,
|
|
11
|
+
}
|
|
12
|
+
} catch (error) {
|
|
13
|
+
if (
|
|
14
|
+
error &&
|
|
15
|
+
error instanceof Error &&
|
|
16
|
+
error.message &&
|
|
17
|
+
error.message.startsWith('method not found')
|
|
18
|
+
) {
|
|
19
|
+
return {
|
|
20
|
+
jsonrpc: JsonRpc.Version,
|
|
21
|
+
id: message.id,
|
|
22
|
+
error: {
|
|
23
|
+
code: JsonRpc.ErrorMethodNotFound,
|
|
24
|
+
message: error.message,
|
|
25
|
+
data: error.stack,
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
jsonrpc: JsonRpc.Version,
|
|
31
|
+
id: message.id,
|
|
32
|
+
error,
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export const listen = (ipc) => {
|
|
37
|
+
const handleMessage = async (message) => {
|
|
38
|
+
if ('method' in message) {
|
|
39
|
+
const response = await getResponse(message)
|
|
40
|
+
console.log({ response })
|
|
41
|
+
ipc.send(response)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
ipc.on('message', handleMessage)
|
|
45
|
+
if (process.send) {
|
|
46
|
+
process.send('ready')
|
|
47
|
+
}
|
|
48
|
+
}
|