@lvce-editor/embeds-process 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/package.json +57 -0
  4. package/src/embedsProcessMain.js +3 -0
  5. package/src/parts/ApplyIncomingIpcResponse/ApplyIncomingIpcResponse.js +11 -0
  6. package/src/parts/Assert/Assert.js +94 -0
  7. package/src/parts/AssertionError/AssertionError.js +6 -0
  8. package/src/parts/Callback/Callback.js +1 -0
  9. package/src/parts/Command/Command.js +9 -0
  10. package/src/parts/CommandMap/CommandMap.js +24 -0
  11. package/src/parts/CommandState/CommandState.js +17 -0
  12. package/src/parts/ElectronBrowserViewState/ElectronBrowserViewState.js +15 -0
  13. package/src/parts/ElectronWebContents/ElectronWebContents.js +77 -0
  14. package/src/parts/ElectronWebContentsView/ElectronWebContentsView.js +58 -0
  15. package/src/parts/ElectronWebContentsViewIpcState/ElectronWebContentsViewIpcState.js +19 -0
  16. package/src/parts/HandleElectronMessagePort/HandleElectronMessagePort.js +20 -0
  17. package/src/parts/HandleIncomingIpc/HandleIncomingIpc.js +15 -0
  18. package/src/parts/HandleIncomingIpcMessagePort/HandleIncomingIpcMessagePort.js +8 -0
  19. package/src/parts/HandleIpc/HandleIpc.js +5 -0
  20. package/src/parts/HandleIpcClosed/HandleIpcClosed.js +23 -0
  21. package/src/parts/HandleIpcEmbedsWorker/HandleIpcEmbedsWorker.js +20 -0
  22. package/src/parts/HandleIpcMainProcess/HandleIpcMainProcess.js +20 -0
  23. package/src/parts/HandleIpcModule/HandleIpcModule.js +19 -0
  24. package/src/parts/HandleIpcSharedProcess/HandleIpcSharedProcess.js +20 -0
  25. package/src/parts/HandleMessage/HandleMessage.js +19 -0
  26. package/src/parts/IpcChild/IpcChild.js +15 -0
  27. package/src/parts/IpcChildModule/IpcChildModule.js +25 -0
  28. package/src/parts/IpcChildType/IpcChildType.js +20 -0
  29. package/src/parts/IpcId/IpcId.js +9 -0
  30. package/src/parts/JsonRpc/JsonRpc.js +1 -0
  31. package/src/parts/Listen/Listen.js +10 -0
  32. package/src/parts/Main/Main.js +8 -0
  33. package/src/parts/MainProcessIpc/MainProcessIpc.js +20 -0
  34. package/src/parts/ParentIpc/ParentIpc.js +20 -0
  35. package/src/parts/SharedProcessIpc/SharedProcessIpc.js +20 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Lvce Editor
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 all
13
+ 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 THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # embeds-process
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@lvce-editor/embeds-process",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "src/embedsProcessMain.js",
6
+ "type": "module",
7
+ "directories": {
8
+ "test": "test"
9
+ },
10
+ "keywords": [],
11
+ "author": "",
12
+ "license": "MIT",
13
+ "xo": {
14
+ "rules": {
15
+ "unicorn/filename-case": "off",
16
+ "indent": "off",
17
+ "semi": "off",
18
+ "no-unused-vars": "off",
19
+ "unicorn/numeric-separators-style": "off",
20
+ "no-extra-semi": "off",
21
+ "arrow-body-style": "off",
22
+ "padded-blocks": "off",
23
+ "capitalized-comments": "off",
24
+ "padding-line-between-statements": "off",
25
+ "arrow-parens": "off",
26
+ "no-warning-comments": "off",
27
+ "array-bracket-spacing": "off",
28
+ "comma-spacing": "off",
29
+ "unicorn/no-array-callback-reference": "off",
30
+ "comma-dangle": "off",
31
+ "operator-linebreak": "off",
32
+ "no-case-declarations": "off",
33
+ "no-undef": "off",
34
+ "object-curly-spacing": "off",
35
+ "object-shorthand": "off",
36
+ "complexity": "off",
37
+ "no-labels": "off",
38
+ "no-multi-assign": "off",
39
+ "max-params": "off",
40
+ "no-bitwise": "off",
41
+ "unicorn/prefer-math-trunc": "off",
42
+ "no-await-in-loop": "off",
43
+ "unicorn/prefer-add-event-listener": "off",
44
+ "no-unused-expressions": "off",
45
+ "unicorn/better-regex": "off",
46
+ "unicorn/no-array-push-push": "off",
47
+ "unicorn/prevent-abbreviations": "off"
48
+ },
49
+ "ignores": [
50
+ "distmin"
51
+ ]
52
+ },
53
+ "dependencies": {
54
+ "@lvce-editor/ipc": "^8.2.1",
55
+ "@lvce-editor/json-rpc": "^1.3.0"
56
+ }
57
+ }
@@ -0,0 +1,3 @@
1
+ import * as Main from './parts/Main/Main.js'
2
+
3
+ Main.main()
@@ -0,0 +1,11 @@
1
+ import * as HandleIpc from '../HandleIpc/HandleIpc.js'
2
+
3
+ export const applyIncomingIpcResponse = async (target, response, ipcId) => {
4
+ switch (response.type) {
5
+ case 'handle':
6
+ HandleIpc.handleIpc(target)
7
+ break
8
+ default:
9
+ throw new Error('unexpected response')
10
+ }
11
+ }
@@ -0,0 +1,94 @@
1
+ // TODO treeshake out this whole module in production
2
+
3
+ import { AssertionError } from '../AssertionError/AssertionError.js'
4
+
5
+ const getType = (value) => {
6
+ switch (typeof value) {
7
+ case 'number':
8
+ return 'number'
9
+ case 'function':
10
+ return 'function'
11
+ case 'string':
12
+ return 'string'
13
+ case 'object':
14
+ if (value === null) {
15
+ return 'null'
16
+ }
17
+ if (Array.isArray(value)) {
18
+ return 'array'
19
+ }
20
+ if (value instanceof Uint32Array) {
21
+ return 'uint32array'
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
+ if (isNaN(value)) {
44
+ throw new AssertionError('value is NaN')
45
+ }
46
+ }
47
+
48
+ export const array = (value) => {
49
+ const type = getType(value)
50
+ if (type !== 'array') {
51
+ throw new AssertionError('expected value to be of type array')
52
+ }
53
+ }
54
+
55
+ export const string = (value) => {
56
+ const type = getType(value)
57
+ if (type !== 'string') {
58
+ throw new AssertionError('expected value to be of type string')
59
+ }
60
+ }
61
+
62
+ export const null_ = (value) => {
63
+ const type = getType(value)
64
+ if (type !== 'null') {
65
+ throw new AssertionError('expected value to be of type null')
66
+ }
67
+ }
68
+
69
+ export const boolean = (value) => {
70
+ const type = getType(value)
71
+ if (type !== 'boolean') {
72
+ throw new AssertionError('expected value to be of type boolean')
73
+ }
74
+ }
75
+
76
+ export const fn = (value) => {
77
+ const type = getType(value)
78
+ if (type !== 'function') {
79
+ throw new AssertionError('expected value to be of type function')
80
+ }
81
+ }
82
+
83
+ export const uint32array = (value) => {
84
+ const type = getType(value)
85
+ if (type !== 'uint32array') {
86
+ throw new AssertionError('expected value to be of type uint32array')
87
+ }
88
+ }
89
+
90
+ export const greaterZero = (value) => {
91
+ if (value <= 0) {
92
+ throw new Error('value must be greater than zero')
93
+ }
94
+ }
@@ -0,0 +1,6 @@
1
+ export class AssertionError extends Error {
2
+ constructor(message) {
3
+ super(message)
4
+ this.name = 'AssertionError'
5
+ }
6
+ }
@@ -0,0 +1 @@
1
+ export { resolve } from '@lvce-editor/json-rpc'
@@ -0,0 +1,9 @@
1
+ import * as CommandState from '../CommandState/CommandState.js'
2
+
3
+ export const execute = (command, ...args) => {
4
+ const fn = CommandState.getCommand(command)
5
+ if (!fn) {
6
+ throw new Error(`Command not found ${command}`)
7
+ }
8
+ return fn(...args)
9
+ }
@@ -0,0 +1,24 @@
1
+ import * as HandleElectronMessagePort from '../HandleElectronMessagePort/HandleElectronMessagePort.js'
2
+ import * as ElectronWebContentsView from '../ElectronWebContentsView/ElectronWebContentsView.js'
3
+ import * as ElectronWebContents from '../ElectronWebContents/ElectronWebContents.js'
4
+
5
+ export const commandMap = {
6
+ 'ElectronWebContentsView.backward': ElectronWebContents.backward,
7
+ 'ElectronWebContentsView.createWebContentsView': ElectronWebContentsView.createWebContentsView,
8
+ 'ElectronWebContentsView.disposeWebContentsView': ElectronWebContentsView.disposeWebContentsView,
9
+ 'ElectronWebContentsView.focus': ElectronWebContents.focus,
10
+ 'ElectronWebContentsView.forward': ElectronWebContents.forward,
11
+ 'ElectronWebContentsView.getStats': ElectronWebContentsView.getStats,
12
+ 'ElectronWebContentsView.inspectElement': ElectronWebContents.inspectElement,
13
+ 'ElectronWebContentsView.openDevtools': ElectronWebContents.openDevtools,
14
+ 'ElectronWebContentsView.reload': ElectronWebContents.reload,
15
+ 'ElectronWebContentsView.resizeBrowserView': ElectronWebContentsView.resizeWebContentsView,
16
+ 'ElectronWebContentsView.setIframeSrc': ElectronWebContentsView.setIframeSrc,
17
+ 'ElectronWebContentsView.show': ElectronWebContentsView.show,
18
+ 'ElectronWebContents.handleDidNavigate': ElectronWebContentsView.handleDidNavigate,
19
+ 'ElectronWebContents.handleTitleUpdated': ElectronWebContentsView.handleTitleUpdated,
20
+ 'ElectronWebContents.handleBrowserViewDestroyed': ElectronWebContentsView.handleBrowserViewDestroyed,
21
+ 'ElectronWebContents.handleWillNavigate': ElectronWebContentsView.handleWillNavigate,
22
+ 'ElectronWebContents.handleContextMenu': ElectronWebContentsView.handleContextMenu,
23
+ 'HandleElectronMessagePort.handleElectronMessagePort': HandleElectronMessagePort.handleElectronMessagePort,
24
+ }
@@ -0,0 +1,17 @@
1
+ export const state = {
2
+ commands: Object.create(null),
3
+ }
4
+
5
+ export const registerCommand = (key, fn) => {
6
+ state.commands[key] = fn
7
+ }
8
+
9
+ export const registerCommands = (commandMap) => {
10
+ for (const [key, value] of Object.entries(commandMap)) {
11
+ registerCommand(key, value)
12
+ }
13
+ }
14
+
15
+ export const getCommand = (key) => {
16
+ return state.commands[key]
17
+ }
@@ -0,0 +1,15 @@
1
+ const state = {
2
+ browserViews: Object.create(null),
3
+ }
4
+
5
+ export const add = (id) => {
6
+ state.browserViews[id] = id
7
+ }
8
+
9
+ export const remove = (id) => {
10
+ delete state.browserViews[id]
11
+ }
12
+
13
+ export const getAll = () => {
14
+ return Object.values(state.browserViews)
15
+ }
@@ -0,0 +1,77 @@
1
+ import * as ElectronBrowserViewIpcState from '../ElectronWebContentsViewIpcState/ElectronWebContentsViewIpcState.js'
2
+ import * as ElectronBrowserViewState from '../ElectronBrowserViewState/ElectronBrowserViewState.js'
3
+ import * as ParentIpc from '../ParentIpc/ParentIpc.js'
4
+
5
+ export const dispose = async (id) => {
6
+ await ParentIpc.invoke('ElectronWebContents.dispose', id)
7
+ }
8
+
9
+ export const callFunction = (webContentsId, method, ...params) => {
10
+ return ParentIpc.invoke(`ElectronWebContents.callFunction`, webContentsId, method, ...params)
11
+ }
12
+
13
+ export const focus = (webContentsId) => {
14
+ return callFunction(webContentsId, 'focus')
15
+ }
16
+
17
+ export const openDevtools = (webContentsId) => {
18
+ return callFunction(webContentsId, 'openDevTools')
19
+ }
20
+
21
+ export const reload = (webContentsId) => {
22
+ return callFunction(webContentsId, 'reload')
23
+ }
24
+
25
+ export const forward = (webContentsId) => {
26
+ return callFunction(webContentsId, 'goForward')
27
+ }
28
+
29
+ export const backward = (webContentsId) => {
30
+ return callFunction(webContentsId, 'goBack')
31
+ }
32
+
33
+ export const inspectElement = (webContentsId, x, y) => {
34
+ return callFunction(webContentsId, `inspectElement`, x, y)
35
+ }
36
+
37
+ export const handleWindowOpen = (webContentsId, url) => {
38
+ // TODO
39
+ }
40
+
41
+ const forwardEvent = (method, webContentsId, ...params) => {
42
+ const ipc = ElectronBrowserViewIpcState.get(webContentsId)
43
+ if (!ipc) {
44
+ console.log(`[shared-process] no ipc for webcontents ${webContentsId}`)
45
+ return
46
+ }
47
+ ipc.send({
48
+ jsonrpc: '2.0',
49
+ method: `SimpleBrowser.${method}`,
50
+ params,
51
+ })
52
+ }
53
+
54
+ export const handleWillNavigate = (...args) => {
55
+ return forwardEvent('handleWillNavigate', ...args)
56
+ }
57
+
58
+ export const handleDidNavigate = (...args) => {
59
+ return forwardEvent('handleDidNavigate', ...args)
60
+ }
61
+
62
+ export const handleContextMenu = (...args) => {
63
+ return forwardEvent('handleContextMenu', ...args)
64
+ }
65
+
66
+ export const handleTitleUpdated = (...args) => {
67
+ return forwardEvent('handleTitleUpdated', ...args)
68
+ }
69
+
70
+ export const handleKeyBinding = (...args) => {
71
+ forwardEvent('handleKeyBinding', ...args)
72
+ }
73
+
74
+ export const handleBrowserViewDestroyed = (id) => {
75
+ ElectronBrowserViewState.remove(id)
76
+ ElectronBrowserViewIpcState.remove(id)
77
+ }
@@ -0,0 +1,58 @@
1
+ import * as Assert from '../Assert/Assert.js'
2
+ import * as ElectronWebContents from '../ElectronWebContents/ElectronWebContents.js'
3
+ import * as ElectronWebContentsViewIpcState from '../ElectronWebContentsViewIpcState/ElectronWebContentsViewIpcState.js'
4
+ import * as ParentIpc from '../ParentIpc/ParentIpc.js'
5
+ import * as JsonRpc from '../JsonRpc/JsonRpc.js'
6
+
7
+ export const createWebContentsView = async (ipc, restoreId, fallthroughKeyBindings) => {
8
+ Assert.number(restoreId)
9
+ const webContentsId = await ParentIpc.invoke('ElectronWebContentsView.createWebContentsView', restoreId)
10
+ ElectronWebContentsViewIpcState.add(webContentsId, ipc)
11
+ // TODO get window id from renderer worker
12
+ await ParentIpc.invoke('ElectronWebContentsView.attachEventListeners', webContentsId)
13
+ await ParentIpc.invoke('ElectronWebContentsViewFunctions.setBackgroundColor', webContentsId, 'white')
14
+ return webContentsId
15
+ }
16
+
17
+ export const disposeWebContentsView = async (id) => {
18
+ await ParentIpc.invoke('ElectronWebContentsView.disposeWebContentsView', id)
19
+ await ElectronWebContents.dispose(id)
20
+ }
21
+
22
+ export const resizeWebContentsView = async (id, ...args) => {
23
+ return ParentIpc.invoke('ElectronWebContentsViewFunctions.resizeBrowserView', id, ...args)
24
+ }
25
+
26
+ export const setIframeSrc = async (id, ...args) => {
27
+ return ParentIpc.invoke('ElectronWebContentsViewFunctions.setIframeSrc', id, ...args)
28
+ }
29
+
30
+ export const getStats = async (id, ...args) => {
31
+ return ParentIpc.invoke('ElectronWebContentsViewFunctions.getStats', id, ...args)
32
+ }
33
+
34
+ export const show = async (id, ...args) => {
35
+ return ParentIpc.invoke('ElectronWebContentsViewFunctions.show', id, ...args)
36
+ }
37
+
38
+ const forwardIpcEvent =
39
+ (key) =>
40
+ (id, ...args) => {
41
+ const ipc = ElectronWebContentsViewIpcState.get(id)
42
+ if (!ipc) {
43
+ return
44
+ }
45
+ JsonRpc.send(ipc, key, id, ...args)
46
+ }
47
+
48
+ export const handleDidNavigate = forwardIpcEvent('ElectronWebContentsView.handleDidNavigate')
49
+
50
+ export const handleTitleUpdated = forwardIpcEvent('ElectronWebContentsView.handleTitleUpdated')
51
+
52
+ export const handleWillNavigate = forwardIpcEvent('ElectronWebContentsView.handleWillNavigate')
53
+
54
+ export const handleContextMenu = forwardIpcEvent('ElectronWebContentsView.handleContextMenu')
55
+
56
+ export const handleBrowserViewDestroyed = (id, ...args) => {
57
+ // TODO send to embeds worker?
58
+ }
@@ -0,0 +1,19 @@
1
+ const state = {
2
+ ipcMap: Object.create(null),
3
+ }
4
+
5
+ export const add = (id, ipc) => {
6
+ state.ipcMap[id] = ipc
7
+ }
8
+
9
+ export const get = (id) => {
10
+ return state.ipcMap[id]
11
+ }
12
+
13
+ export const remove = (id) => {
14
+ delete state.ipcMap[id]
15
+ }
16
+
17
+ export const getAll = () => {
18
+ return Object.entries(state.ipcMap)
19
+ }
@@ -0,0 +1,20 @@
1
+ import * as Assert from '../Assert/Assert.js'
2
+ import * as HandleIncomingIpc from '../HandleIncomingIpc/HandleIncomingIpc.js'
3
+
4
+ export const handleElectronMessagePort = async (messagePort, ipcId) => {
5
+ Assert.object(messagePort)
6
+ Assert.number(ipcId)
7
+ return HandleIncomingIpc.handleIncomingIpc(ipcId, messagePort, {})
8
+ // const ipc = await IpcChild.listen({
9
+ // method: IpcChildType.ElectronMessagePort,
10
+ // messagePort,
11
+ // })
12
+ // HandleIpc.handleIpc(ipc)
13
+ // if (ipcId === IpcId.MainProcess) {
14
+ // ParentIpc.state.ipc = ipc
15
+ // } else if (ipcId === IpcId.SharedProcess) {
16
+ // SharedProcessIpc.state.ipc = ipc
17
+ // } else if (ipcId === IpcId.EmbedsWorker) {
18
+ // ipc.addEventListener('close', HandleIpcClosed.handleIpcClosed)
19
+ // }
20
+ }
@@ -0,0 +1,15 @@
1
+ import * as ApplyIncomingIpcResponse from '../ApplyIncomingIpcResponse/ApplyIncomingIpcResponse.js'
2
+ import * as Assert from '../Assert/Assert.js'
3
+ import * as HandleIncomingIpcMessagePort from '../HandleIncomingIpcMessagePort/HandleIncomingIpcMessagePort.js'
4
+ import * as HandleIpcModule from '../HandleIpcModule/HandleIpcModule.js'
5
+
6
+ const getIpcAndResponse = (module, handle, message) => {
7
+ return HandleIncomingIpcMessagePort.handleIncomingIpcMessagePort(module, handle, message)
8
+ }
9
+
10
+ export const handleIncomingIpc = async (ipcId, handle, message) => {
11
+ Assert.number(ipcId)
12
+ const module = HandleIpcModule.getModule(ipcId)
13
+ const { target, response } = await getIpcAndResponse(module, handle, message)
14
+ await ApplyIncomingIpcResponse.applyIncomingIpcResponse(target, response, ipcId)
15
+ }
@@ -0,0 +1,8 @@
1
+ export const handleIncomingIpcMessagePort = async (module, handle, message) => {
2
+ const target = await module.targetMessagePort(handle, message)
3
+ const response = module.upgradeMessagePort(handle, message)
4
+ return {
5
+ target,
6
+ response,
7
+ }
8
+ }
@@ -0,0 +1,5 @@
1
+ import * as HandleMessage from '../HandleMessage/HandleMessage.js'
2
+
3
+ export const handleIpc = (ipc) => {
4
+ ipc.addEventListener('message', HandleMessage.handleMessage)
5
+ }
@@ -0,0 +1,23 @@
1
+ import * as ElectronWebContentsView from '../ElectronWebContentsView/ElectronWebContentsView.js'
2
+ import * as ElectronWebContentsViewIpcState from '../ElectronWebContentsViewIpcState/ElectronWebContentsViewIpcState.js'
3
+
4
+ const getIdsToDispose = (ipc) => {
5
+ const entries = ElectronWebContentsViewIpcState.getAll()
6
+ const toDispose = []
7
+
8
+ for (const [id, value] of entries) {
9
+ if (value === ipc) {
10
+ toDispose.push(parseInt(id))
11
+ }
12
+ }
13
+ return toDispose
14
+ }
15
+
16
+ export const handleIpcClosed = async (event) => {
17
+ const idsToDispose = getIdsToDispose(event.target)
18
+ for (const id of idsToDispose) {
19
+ ElectronWebContentsViewIpcState.remove(id)
20
+ await ElectronWebContentsView.disposeWebContentsView(id)
21
+ }
22
+ // SharedProcessIpc.send('HandleMessagePortForEmbedsProcess.handleEmbedsProcessIpcClosed')
23
+ }
@@ -0,0 +1,20 @@
1
+ import * as Assert from '../Assert/Assert.js'
2
+ import * as HandleIpcClosed from '../HandleIpcClosed/HandleIpcClosed.js'
3
+ import * as IpcChild from '../IpcChild/IpcChild.js'
4
+ import * as IpcChildType from '../IpcChildType/IpcChildType.js'
5
+
6
+ export const targetMessagePort = async (messagePort, message) => {
7
+ Assert.object(messagePort)
8
+ const ipc = await IpcChild.listen({
9
+ method: IpcChildType.ElectronMessagePort,
10
+ messagePort,
11
+ })
12
+ ipc.addEventListener('close', HandleIpcClosed.handleIpcClosed)
13
+ return ipc
14
+ }
15
+
16
+ export const upgradeMessagePort = () => {
17
+ return {
18
+ type: 'handle',
19
+ }
20
+ }
@@ -0,0 +1,20 @@
1
+ import * as Assert from '../Assert/Assert.js'
2
+ import * as IpcChild from '../IpcChild/IpcChild.js'
3
+ import * as IpcChildType from '../IpcChildType/IpcChildType.js'
4
+ import * as ParentIpc from '../ParentIpc/ParentIpc.js'
5
+
6
+ export const targetMessagePort = async (messagePort, message) => {
7
+ Assert.object(messagePort)
8
+ const ipc = await IpcChild.listen({
9
+ method: IpcChildType.ElectronMessagePort,
10
+ messagePort,
11
+ })
12
+ ParentIpc.state.ipc = ipc
13
+ return ipc
14
+ }
15
+
16
+ export const upgradeMessagePort = () => {
17
+ return {
18
+ type: 'handle',
19
+ }
20
+ }
@@ -0,0 +1,19 @@
1
+ import * as Assert from '../Assert/Assert.js'
2
+ import * as HandleIpcEmbedsWorker from '../HandleIpcEmbedsWorker/HandleIpcEmbedsWorker.js'
3
+ import * as HandleIpcMainProcess from '../HandleIpcMainProcess/HandleIpcMainProcess.js'
4
+ import * as HandleIpcSharedProcess from '../HandleIpcSharedProcess/HandleIpcSharedProcess.js'
5
+ import * as IpcId from '../IpcId/IpcId.js'
6
+
7
+ export const getModule = (ipcId) => {
8
+ Assert.number(ipcId)
9
+ switch (ipcId) {
10
+ case IpcId.SharedProcess:
11
+ return HandleIpcSharedProcess
12
+ case IpcId.EmbedsWorker:
13
+ return HandleIpcEmbedsWorker
14
+ case IpcId.MainProcess:
15
+ return HandleIpcMainProcess
16
+ default:
17
+ throw new Error(`unexpected incoming ipc`)
18
+ }
19
+ }
@@ -0,0 +1,20 @@
1
+ import * as Assert from '../Assert/Assert.js'
2
+ import * as IpcChild from '../IpcChild/IpcChild.js'
3
+ import * as IpcChildType from '../IpcChildType/IpcChildType.js'
4
+ import * as SharedProcessIpc from '../SharedProcessIpc/SharedProcessIpc.js'
5
+
6
+ export const targetMessagePort = async (messagePort, message) => {
7
+ Assert.object(messagePort)
8
+ const ipc = await IpcChild.listen({
9
+ method: IpcChildType.ElectronMessagePort,
10
+ messagePort,
11
+ })
12
+ SharedProcessIpc.state.ipc = ipc
13
+ return ipc
14
+ }
15
+
16
+ export const upgradeMessagePort = () => {
17
+ return {
18
+ type: 'handle',
19
+ }
20
+ }
@@ -0,0 +1,19 @@
1
+ import * as Callback from '../Callback/Callback.js'
2
+ import * as Command from '../Command/Command.js'
3
+ import * as JsonRpc from '../JsonRpc/JsonRpc.js'
4
+
5
+ const prepare = (error) => {
6
+ return error
7
+ }
8
+
9
+ const requiresSocket = (method) => {
10
+ return false
11
+ }
12
+
13
+ const logError = (error, prettyError) => {
14
+ console.error(error)
15
+ }
16
+
17
+ export const handleMessage = (event) => {
18
+ return JsonRpc.handleJsonRpcMessage(event.target, event.data, Command.execute, Callback.resolve, prepare, logError, requiresSocket)
19
+ }
@@ -0,0 +1,15 @@
1
+ import * as IpcChildModule from '../IpcChildModule/IpcChildModule.js'
2
+
3
+ export const listen = async ({ method, ...params }) => {
4
+ const module = await IpcChildModule.getModule(method)
5
+ // @ts-ignore
6
+ const rawIpc = await module.listen(params)
7
+ // @ts-ignore
8
+ if (module.signal) {
9
+ // @ts-ignore
10
+ module.signal(rawIpc)
11
+ }
12
+ // @ts-ignore
13
+ const ipc = module.wrap(rawIpc)
14
+ return ipc
15
+ }
@@ -0,0 +1,25 @@
1
+ import {
2
+ IpcChildWithElectronMessagePort,
3
+ IpcChildWithElectronUtilityProcess,
4
+ IpcChildWithNodeForkedProcess,
5
+ IpcChildWithNodeWorker,
6
+ IpcChildWithWebSocket,
7
+ } from '@lvce-editor/ipc'
8
+ import * as IpcChildType from '../IpcChildType/IpcChildType.js'
9
+
10
+ export const getModule = (method) => {
11
+ switch (method) {
12
+ case IpcChildType.NodeForkedProcess:
13
+ return IpcChildWithNodeForkedProcess
14
+ case IpcChildType.NodeWorker:
15
+ return IpcChildWithNodeWorker
16
+ case IpcChildType.ElectronUtilityProcess:
17
+ return IpcChildWithElectronUtilityProcess
18
+ case IpcChildType.ElectronMessagePort:
19
+ return IpcChildWithElectronMessagePort
20
+ case IpcChildType.WebSocket:
21
+ return IpcChildWithWebSocket
22
+ default:
23
+ throw new Error('unexpected ipc type')
24
+ }
25
+ }
@@ -0,0 +1,20 @@
1
+ export const NodeWorker = 1
2
+ export const NodeForkedProcess = 2
3
+ export const ElectronUtilityProcess = 3
4
+ export const ElectronMessagePort = 4
5
+ export const NodeMessagePort = 5
6
+ export const WebSocket = 6
7
+
8
+ export const Auto = () => {
9
+ const { argv } = process
10
+ if (argv.includes('--ipc-type=node-worker')) {
11
+ return NodeWorker
12
+ }
13
+ if (argv.includes('--ipc-type=node-forked-process')) {
14
+ return NodeForkedProcess
15
+ }
16
+ if (argv.includes('--ipc-type=electron-utility-process')) {
17
+ return ElectronUtilityProcess
18
+ }
19
+ throw new Error(`[shared-process] unknown ipc type`)
20
+ }
@@ -0,0 +1,9 @@
1
+ export const EmbedsProcess = 2
2
+ export const EmbedsWorker = 77
3
+ export const ExtensionHostHelperProcess = 3
4
+ export const MainProcess = -5
5
+ export const ProcessExplorer = 11
6
+ export const SearchProcess = 13
7
+ export const SharedProcess = 1
8
+ export const TerminalProcess = 7
9
+ export const Unknown = 0
@@ -0,0 +1 @@
1
+ export * from '@lvce-editor/json-rpc'
@@ -0,0 +1,10 @@
1
+ import * as HandleIpc from '../HandleIpc/HandleIpc.js'
2
+ import * as IpcChild from '../IpcChild/IpcChild.js'
3
+ import * as IpcChildType from '../IpcChildType/IpcChildType.js'
4
+ import * as ParentIpc from '../ParentIpc/ParentIpc.js'
5
+
6
+ export const listen = async () => {
7
+ const ipc = await IpcChild.listen({ method: IpcChildType.Auto() })
8
+ HandleIpc.handleIpc(ipc)
9
+ ParentIpc.state.ipc = ipc
10
+ }
@@ -0,0 +1,8 @@
1
+ import * as CommandMap from '../CommandMap/CommandMap.js'
2
+ import * as CommandState from '../CommandState/CommandState.js'
3
+ import * as Listen from '../Listen/Listen.js'
4
+
5
+ export const main = async () => {
6
+ CommandState.registerCommands(CommandMap.commandMap)
7
+ await Listen.listen()
8
+ }
@@ -0,0 +1,20 @@
1
+ import * as JsonRpc from '../JsonRpc/JsonRpc.js'
2
+
3
+ export const state = {
4
+ /**
5
+ * @type {any}
6
+ */
7
+ ipc: undefined,
8
+ }
9
+
10
+ export const invoke = (method, ...params) => {
11
+ return JsonRpc.invoke(state.ipc, method, ...params)
12
+ }
13
+
14
+ export const send = (method, ...params) => {
15
+ return JsonRpc.send(state.ipc, method, ...params)
16
+ }
17
+
18
+ export const invokeAndTransfer = (method, transfer, ...params) => {
19
+ return JsonRpc.invokeAndTransfer(state.ipc, transfer, method, ...params)
20
+ }
@@ -0,0 +1,20 @@
1
+ import * as JsonRpc from '../JsonRpc/JsonRpc.js'
2
+
3
+ export const state = {
4
+ /**
5
+ * @type {any}
6
+ */
7
+ ipc: undefined,
8
+ }
9
+
10
+ export const invoke = (method, ...params) => {
11
+ return JsonRpc.invoke(state.ipc, method, ...params)
12
+ }
13
+
14
+ export const send = (method, ...params) => {
15
+ return JsonRpc.send(state.ipc, method, ...params)
16
+ }
17
+
18
+ export const invokeAndTransfer = (method, transfer, ...params) => {
19
+ return JsonRpc.invokeAndTransfer(state.ipc, transfer, method, ...params)
20
+ }
@@ -0,0 +1,20 @@
1
+ import * as JsonRpc from '../JsonRpc/JsonRpc.js'
2
+
3
+ export const state = {
4
+ /**
5
+ * @type {any}
6
+ */
7
+ ipc: undefined,
8
+ }
9
+
10
+ export const invoke = (method, ...params) => {
11
+ return JsonRpc.invoke(state.ipc, method, ...params)
12
+ }
13
+
14
+ export const send = (method, ...params) => {
15
+ return JsonRpc.send(state.ipc, method, ...params)
16
+ }
17
+
18
+ export const invokeAndTransfer = (method, transfer, ...params) => {
19
+ return JsonRpc.invokeAndTransfer(state.ipc, transfer, method, ...params)
20
+ }