@lvce-editor/ipc 2.1.0 → 3.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/README.md +1 -1
  2. package/dist/index.js +831 -7
  3. package/package.json +2 -2
  4. package/dist/parts/Assert/Assert.js +0 -1
  5. package/dist/parts/CamelCase/CamelCase.js +0 -11
  6. package/dist/parts/Character/Character.js +0 -12
  7. package/dist/parts/ChildProcessError/ChildProcessError.js +0 -21
  8. package/dist/parts/ErrorCodes/ErrorCodes.js +0 -22
  9. package/dist/parts/FirstNodeWorkerEventType/FirstNodeWorkerEventType.js +0 -3
  10. package/dist/parts/FirstWebSocketEventType/FirstWebSocketEventType.js +0 -2
  11. package/dist/parts/FormatUtilityProcessName/FormatUtilityProcessName.js +0 -5
  12. package/dist/parts/GetFirstEvent/GetFirstEvent.js +0 -23
  13. package/dist/parts/GetFirstNodeChildProcessEvent/GetFirstNodeChildProcessEvent.js +0 -41
  14. package/dist/parts/GetFirstNodeWorkerEvent/GetFirstNodeWorkerEvent.js +0 -9
  15. package/dist/parts/GetFirstUtilityProcessEvent/GetFirstUtilityProcessEvent.js +0 -42
  16. package/dist/parts/GetFirstWebSocketEvent/GetFirstWebSocketEvent.js +0 -25
  17. package/dist/parts/GetHelpfulChildProcessError/GetHelpfulChildProcessError.js +0 -119
  18. package/dist/parts/GetUtilityProcessPortData/GetUtilityProcessPortData.js +0 -10
  19. package/dist/parts/IpcChildWithElectronMessagePort/IpcChildWithElectronMessagePort.js +0 -51
  20. package/dist/parts/IpcChildWithElectronUtilityProcess/IpcChildWithElectronUtilityProcess.js +0 -45
  21. package/dist/parts/IpcChildWithNodeForkedProcess/IpcChildWithNodeForkedProcess.js +0 -46
  22. package/dist/parts/IpcChildWithWebSocket/IpcChildWithWebSocket.js +0 -59
  23. package/dist/parts/IpcError/IpcError.js +0 -20
  24. package/dist/parts/IpcParentWithElectronUtilityProcess/IpcParentWithElectronUtilityProcess.js +0 -43
  25. package/dist/parts/IpcParentWithNodeForkedProcess/IpcParentWithNodeForkedProcess.js +0 -54
  26. package/dist/parts/IpcParentWithNodeWorker/IpcParentWithNodeWorker.js +0 -49
  27. package/dist/parts/IsMessagePortMain/IsMessagePortMain.js +0 -3
  28. package/dist/parts/IsWebSocketOpen/IsWebSocketOpen.js +0 -5
  29. package/dist/parts/JoinLines/JoinLines.js +0 -5
  30. package/dist/parts/Promises/Promises.js +0 -19
  31. package/dist/parts/SplitLines/SplitLines.js +0 -5
  32. package/dist/parts/VError/VError.js +0 -1
  33. package/dist/parts/WebSocketReadyState/WebSocketReadyState.js +0 -5
  34. package/dist/parts/WebSocketSerialization/WebSocketSerialization.js +0 -7
  35. package/dist/parts/WebSocketServer/WebSocketServer.js +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/ipc",
3
- "version": "2.1.0",
3
+ "version": "3.0.0",
4
4
  "description": "Inter Process Communication for Lvce Editor",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@lvce-editor/assert": "^1.2.0",
18
- "@lvce-editor/verror": "^1.1.2",
18
+ "@lvce-editor/verror": "^1.2.0",
19
19
  "@lvce-editor/web-socket-server": "^1.1.0"
20
20
  },
21
21
  "engines": {
@@ -1 +0,0 @@
1
- export * from '@lvce-editor/assert'
@@ -1,11 +0,0 @@
1
- import * as Character from '../Character/Character.js'
2
-
3
- const firstLetterLowerCase = (string) => {
4
- return string[0].toLowerCase() + string.slice(1)
5
- }
6
-
7
- export const camelCase = (string) => {
8
- const parts = string.split(Character.Space)
9
- const lowerParts = parts.map(firstLetterLowerCase)
10
- return lowerParts.join(Character.Dash)
11
- }
@@ -1,12 +0,0 @@
1
- export const Backslash = '\\'
2
- export const Dash = '-'
3
- export const Dot = '.'
4
- export const EmptyString = ''
5
- export const NewLine = '\n'
6
- export const OpenAngleBracket = '<'
7
- export const Slash = '/'
8
- export const Space = ' '
9
- export const Tab = '\t'
10
- export const Underline = '_'
11
- export const T = 't'
12
- export const SemiColon = ';'
@@ -1,21 +0,0 @@
1
- import * as GetHelpfulChildProcessError from '../GetHelpfulChildProcessError/GetHelpfulChildProcessError.js'
2
- import * as JoinLines from '../JoinLines/JoinLines.js'
3
- import * as SplitLines from '../SplitLines/SplitLines.js'
4
-
5
- export class ChildProcessError extends Error {
6
- constructor(stderr) {
7
- const { message, code, stack } = GetHelpfulChildProcessError.getHelpfulChildProcessError('', stderr)
8
- super(message || 'child process error')
9
- this.name = 'ChildProcessError'
10
- if (code) {
11
- this.code = code
12
- }
13
- if (stack) {
14
- const lines = SplitLines.splitLines(this.stack)
15
- const [firstLine, ...stackLines] = lines
16
- const newStackLines = [firstLine, ...stack, ...stackLines]
17
- const newStack = JoinLines.joinLines(newStackLines)
18
- this.stack = newStack
19
- }
20
- }
21
- }
@@ -1,22 +0,0 @@
1
- export const E_COLOR_THEME_NOT_FOUND = 'E_COLOR_THEME_NOT_FOUND'
2
- export const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND'
3
- export const E_ICON_THEME_NOT_FOUND = 'E_ICON_THEME_NOT_FOUND'
4
- export const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE'
5
- export const E_MANIFEST_NOT_FOUND = 'E_MANIFEST_NOT_FOUND'
6
- export const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON'
7
- export const E_JSON_PARSE = 'E_JSON_PARSE'
8
- export const EACCES = 'EACCES'
9
- export const ECONNRESET = 'ECONNRESET'
10
- export const EEXIST = 'EEXIST'
11
- export const EISDIR = 'EISDIR'
12
- export const ELOOP = 'ELOOP'
13
- export const ENOENT = 'ENOENT'
14
- export const ENOTDIR = 'ENOTDIR'
15
- export const EPERM = 'EPERM'
16
- export const EPIPE = 'EPIPE'
17
- export const ERR_IPC_CHANNEL_CLOSED = 'ERR_IPC_CHANNEL_CLOSED'
18
- export const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND'
19
- export const EXDEV = 'EXDEV'
20
- export const ERR_DLOPEN_FAILED = 'ERR_DLOPEN_FAILED'
21
- export const ESRCH = 'ESRCH'
22
- export const E_RIP_GREP_NOT_FOUND = 'E_RIP_GREP_NOT_FOUND'
@@ -1,3 +0,0 @@
1
- export const Exit = 1
2
- export const Error = 2
3
- export const Message = 3
@@ -1,2 +0,0 @@
1
- export const Open = 1
2
- export const Close = 2
@@ -1,5 +0,0 @@
1
- import * as CamelCase from '../CamelCase/CamelCase.js'
2
-
3
- export const formatUtilityProcessName = (name) => {
4
- return CamelCase.camelCase(name)
5
- }
@@ -1,23 +0,0 @@
1
- import * as Promises from '../Promises/Promises.js'
2
-
3
- export const getFirstEvent = (eventEmitter, eventMap) => {
4
- const { resolve, promise } = Promises.withResolvers()
5
- const listenerMap = Object.create(null)
6
- const cleanup = (value) => {
7
- for (const event of Object.keys(eventMap)) {
8
- eventEmitter.off(event, listenerMap[event])
9
- }
10
- resolve(value)
11
- }
12
- for (const [event, type] of Object.entries(eventMap)) {
13
- const listener = (event) => {
14
- cleanup({
15
- type,
16
- event,
17
- })
18
- }
19
- eventEmitter.on(event, listener)
20
- listenerMap[event] = listener
21
- }
22
- return promise
23
- }
@@ -1,41 +0,0 @@
1
- import * as FirstNodeWorkerEventType from '../FirstNodeWorkerEventType/FirstNodeWorkerEventType.js'
2
-
3
- export const getFirstNodeChildProcessEvent = async (childProcess) => {
4
- const { type, event, stdout, stderr } = await new Promise((resolve, reject) => {
5
- let stderr = ''
6
- let stdout = ''
7
- const cleanup = (value) => {
8
- if (childProcess.stdout && childProcess.stderr) {
9
- childProcess.stderr.off('data', handleStdErrData)
10
- childProcess.stdout.off('data', handleStdoutData)
11
- }
12
- childProcess.off('message', handleMessage)
13
- childProcess.off('exit', handleExit)
14
- childProcess.off('error', handleError)
15
- resolve(value)
16
- }
17
- const handleStdErrData = (data) => {
18
- stderr += data
19
- }
20
- const handleStdoutData = (data) => {
21
- stdout += data
22
- }
23
- const handleMessage = (event) => {
24
- cleanup({ type: FirstNodeWorkerEventType.Message, event, stdout, stderr })
25
- }
26
- const handleExit = (event) => {
27
- cleanup({ type: FirstNodeWorkerEventType.Exit, event, stdout, stderr })
28
- }
29
- const handleError = (event) => {
30
- cleanup({ type: FirstNodeWorkerEventType.Error, event, stdout, stderr })
31
- }
32
- if (childProcess.stdout && childProcess.stderr) {
33
- childProcess.stderr.on('data', handleStdErrData)
34
- childProcess.stdout.on('data', handleStdoutData)
35
- }
36
- childProcess.on('message', handleMessage)
37
- childProcess.on('exit', handleExit)
38
- childProcess.on('error', handleError)
39
- })
40
- return { type, event, stdout, stderr }
41
- }
@@ -1,9 +0,0 @@
1
- import * as FirstNodeWorkerEventType from '../FirstNodeWorkerEventType/FirstNodeWorkerEventType.js'
2
- import * as GetFirstEvent from '../GetFirstEvent/GetFirstEvent.js'
3
-
4
- export const getFirstNodeWorkerEvent = (worker) => {
5
- return GetFirstEvent.getFirstEvent(worker, {
6
- exit: FirstNodeWorkerEventType.Exit,
7
- error: FirstNodeWorkerEventType.Error,
8
- })
9
- }
@@ -1,42 +0,0 @@
1
- import * as FirstNodeWorkerEventType from '../FirstNodeWorkerEventType/FirstNodeWorkerEventType.js'
2
- import * as Promises from '../Promises/Promises.js'
3
-
4
- /**
5
- *
6
- * @param {import('electron').UtilityProcess} utilityProcess
7
- * @returns
8
- */
9
- export const getFirstUtilityProcessEvent = async (utilityProcess) => {
10
- const { resolve, promise } = Promises.withResolvers()
11
- let stdout = ''
12
- let stderr = ''
13
- const cleanup = (value) => {
14
- // @ts-ignore
15
- utilityProcess.stderr.off('data', handleStdErrData)
16
- // @ts-ignore
17
- utilityProcess.stdout.off('data', handleStdoutData)
18
- utilityProcess.off('message', handleMessage)
19
- utilityProcess.off('exit', handleExit)
20
- resolve(value)
21
- }
22
- const handleStdErrData = (data) => {
23
- stderr += data
24
- }
25
- const handleStdoutData = (data) => {
26
- stdout += data
27
- }
28
- const handleMessage = (event) => {
29
- cleanup({ type: FirstNodeWorkerEventType.Message, event, stdout, stderr })
30
- }
31
- const handleExit = (event) => {
32
- cleanup({ type: FirstNodeWorkerEventType.Exit, event, stdout, stderr })
33
- }
34
- // @ts-ignore
35
- utilityProcess.stderr.on('data', handleStdErrData)
36
- // @ts-ignore
37
- utilityProcess.stdout.on('data', handleStdoutData)
38
- utilityProcess.on('message', handleMessage)
39
- utilityProcess.on('exit', handleExit)
40
- const { type, event } = await promise
41
- return { type, event, stdout, stderr }
42
- }
@@ -1,25 +0,0 @@
1
- import * as FirstWebSocketEventType from '../FirstWebSocketEventType/FirstWebSocketEventType.js'
2
- import * as GetFirstEvent from '../GetFirstEvent/GetFirstEvent.js'
3
- import * as WebSocketReadyState from '../WebSocketReadyState/WebSocketReadyState.js'
4
-
5
- export const getFirstWebSocketEvent = async (webSocket) => {
6
- switch (webSocket.readyState) {
7
- case WebSocketReadyState.Open:
8
- return {
9
- type: FirstWebSocketEventType.Open,
10
- event: undefined,
11
- }
12
- case WebSocketReadyState.Closed:
13
- return {
14
- type: FirstWebSocketEventType.Close,
15
- event: undefined,
16
- }
17
- default:
18
- break
19
- }
20
- const { type, event } = await GetFirstEvent.getFirstEvent(webSocket, {
21
- open: FirstWebSocketEventType.Open,
22
- close: FirstWebSocketEventType.Close,
23
- })
24
- return { type, event }
25
- }
@@ -1,119 +0,0 @@
1
- import * as ErrorCodes from '../ErrorCodes/ErrorCodes.js'
2
- import * as SplitLines from '../SplitLines/SplitLines.js'
3
- import * as JoinLines from '../JoinLines/JoinLines.js'
4
-
5
- const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/
6
- const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/
7
-
8
- const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/
9
- const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/
10
-
11
- const RE_AT = /^\s+at/
12
- const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/
13
-
14
- const isUnhelpfulNativeModuleError = (stderr) => {
15
- return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr)
16
- }
17
-
18
- const isMessageCodeBlockStartIndex = (line) => {
19
- return RE_MESSAGE_CODE_BLOCK_START.test(line)
20
- }
21
-
22
- const isMessageCodeBlockEndIndex = (line) => {
23
- return RE_MESSAGE_CODE_BLOCK_END.test(line)
24
- }
25
-
26
- const getMessageCodeBlock = (stderr) => {
27
- const lines = SplitLines.splitLines(stderr)
28
- const startIndex = lines.findIndex(isMessageCodeBlockStartIndex)
29
- const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex)
30
- const relevantLines = lines.slice(startIndex, endIndex)
31
- const relevantMessage = relevantLines.join(' ').slice('Error: '.length)
32
- return relevantMessage
33
- }
34
-
35
- const getNativeModuleErrorMessage = (stderr) => {
36
- const message = getMessageCodeBlock(stderr)
37
- return {
38
- message: `Incompatible native node module: ${message}`,
39
- code: ErrorCodes.E_INCOMPATIBLE_NATIVE_MODULE,
40
- }
41
- }
42
-
43
- const isModulesSyntaxError = (stderr) => {
44
- if (!stderr) {
45
- return false
46
- }
47
- return stderr.includes('SyntaxError: Cannot use import statement outside a module')
48
- }
49
-
50
- const getModuleSyntaxError = (stderr) => {
51
- return {
52
- message: `ES Modules are not supported in electron`,
53
- code: ErrorCodes.E_MODULES_NOT_SUPPORTED_IN_ELECTRON,
54
- }
55
- }
56
-
57
- const isModuleNotFoundError = (stderr) => {
58
- if (!stderr) {
59
- return false
60
- }
61
- return stderr.includes('ERR_MODULE_NOT_FOUND')
62
- }
63
-
64
- const isModuleNotFoundMessage = (line) => {
65
- return line.includes('ERR_MODULE_NOT_FOUND')
66
- }
67
-
68
- const getModuleNotFoundError = (stderr) => {
69
- const lines = SplitLines.splitLines(stderr)
70
- const messageIndex = lines.findIndex(isModuleNotFoundMessage)
71
- const message = lines[messageIndex]
72
- return {
73
- message,
74
- code: ErrorCodes.ERR_MODULE_NOT_FOUND,
75
- }
76
- }
77
-
78
- const isNormalStackLine = (line) => {
79
- return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line)
80
- }
81
-
82
- const getDetails = (lines) => {
83
- const index = lines.findIndex(isNormalStackLine)
84
- if (index === -1) {
85
- return {
86
- actualMessage: JoinLines.joinLines(lines),
87
- rest: [],
88
- }
89
- }
90
- let lastIndex = index - 1
91
- while (++lastIndex < lines.length) {
92
- if (!isNormalStackLine(lines[lastIndex])) {
93
- break
94
- }
95
- }
96
- return {
97
- actualMessage: lines[index - 1],
98
- rest: lines.slice(index, lastIndex),
99
- }
100
- }
101
-
102
- export const getHelpfulChildProcessError = (stdout, stderr) => {
103
- if (isUnhelpfulNativeModuleError(stderr)) {
104
- return getNativeModuleErrorMessage(stderr)
105
- }
106
- if (isModulesSyntaxError(stderr)) {
107
- return getModuleSyntaxError(stderr)
108
- }
109
- if (isModuleNotFoundError(stderr)) {
110
- return getModuleNotFoundError(stderr)
111
- }
112
- const lines = SplitLines.splitLines(stderr)
113
- const { actualMessage, rest } = getDetails(lines)
114
- return {
115
- message: `${actualMessage}`,
116
- code: '',
117
- stack: rest,
118
- }
119
- }
@@ -1,10 +0,0 @@
1
- export const getUtilityProcessPortData = (event) => {
2
- const { data, ports } = event
3
- if (ports.length === 0) {
4
- return data
5
- }
6
- return {
7
- ...data,
8
- params: [...ports, ...data.params],
9
- }
10
- }
@@ -1,51 +0,0 @@
1
- import { IpcError } from '../IpcError/IpcError.js'
2
- import * as IsMessagePortMain from '../IsMessagePortMain/IsMessagePortMain.js'
3
-
4
- export const listen = ({ messagePort }) => {
5
- if (!IsMessagePortMain.isMessagePortMain(messagePort)) {
6
- throw new IpcError('port must be of type MessagePortMain')
7
- }
8
- return messagePort
9
- }
10
-
11
- const getActualData = (event) => {
12
- const { data, ports } = event
13
- if (ports.length === 0) {
14
- return data
15
- }
16
- return {
17
- ...data,
18
- params: [...ports, ...data.params],
19
- }
20
- }
21
-
22
- export const wrap = (messagePort) => {
23
- return {
24
- messagePort,
25
- on(event, listener) {
26
- if (event === 'message') {
27
- const wrappedListener = (event) => {
28
- const actualData = getActualData(event)
29
- listener(actualData)
30
- }
31
- this.messagePort.on(event, wrappedListener)
32
- } else if (event === 'close') {
33
- this.messagePort.on('close', listener)
34
- } else {
35
- throw new Error('unsupported event type')
36
- }
37
- },
38
- off(event, listener) {
39
- this.messagePort.off(event, listener)
40
- },
41
- send(message) {
42
- this.messagePort.postMessage(message)
43
- },
44
- dispose() {
45
- this.messagePort.close()
46
- },
47
- start() {
48
- this.messagePort.start()
49
- },
50
- }
51
- }
@@ -1,45 +0,0 @@
1
- import * as GetUtilityProcessPortData from '../GetUtilityProcessPortData/GetUtilityProcessPortData.js'
2
-
3
- export const listen = () => {
4
- // @ts-ignore
5
- const { parentPort } = process
6
- if (!parentPort) {
7
- throw new Error('parent port must be defined')
8
- }
9
- return parentPort
10
- }
11
-
12
- export const signal = (parentPort) => {
13
- parentPort.postMessage('ready')
14
- }
15
-
16
- export const wrap = (parentPort) => {
17
- return {
18
- parentPort,
19
- on(event, listener) {
20
- if (event === 'message') {
21
- const wrappedListener = (event) => {
22
- const actualData = GetUtilityProcessPortData.getUtilityProcessPortData(event)
23
- listener(actualData)
24
- }
25
- this.parentPort.on(event, wrappedListener)
26
- } else if (event === 'close') {
27
- this.parentPort.on('close', listener)
28
- } else {
29
- throw new Error('unsupported event type')
30
- }
31
- },
32
- off(event, listener) {
33
- this.parentPort.off(event, listener)
34
- },
35
- send(message) {
36
- this.parentPort.postMessage(message)
37
- },
38
- sendAndTransfer(message, transfer) {
39
- this.parentPort.postMessage(message, transfer)
40
- },
41
- dispose() {
42
- this.parentPort.close()
43
- },
44
- }
45
- }
@@ -1,46 +0,0 @@
1
- export const listen = async () => {
2
- if (!process.send) {
3
- throw new Error('process.send must be defined')
4
- }
5
- return process
6
- }
7
-
8
- export const signal = (process) => {
9
- process.send('ready')
10
- }
11
-
12
- const getActualData = (message, handle) => {
13
- if (handle) {
14
- return {
15
- ...message,
16
- params: [...message.params, handle],
17
- }
18
- }
19
- return message
20
- }
21
-
22
- export const wrap = (process) => {
23
- return {
24
- process,
25
- on(event, listener) {
26
- if (event === 'message') {
27
- const wrappedListener = (event, handle) => {
28
- const actualData = getActualData(event, handle)
29
- listener(actualData)
30
- }
31
- this.process.on(event, wrappedListener)
32
- } else if (event === 'close') {
33
- this.process.on('close', listener)
34
- } else {
35
- throw new Error('unsupported event type')
36
- }
37
- },
38
- off(event, listener) {
39
- this.process.off(event, listener)
40
- },
41
- send(message) {
42
- this.process.send(message)
43
- },
44
- dispose() {},
45
- }
46
- }
@@ -1,59 +0,0 @@
1
- import * as GetFirstWebSocketEvent from '../GetFirstWebSocketEvent/GetFirstWebSocketEvent.js'
2
- import { IpcError } from '../IpcError/IpcError.js'
3
- import * as IsWebSocketOpen from '../IsWebSocketOpen/IsWebSocketOpen.js'
4
- import * as WebSocketSerialization from '../WebSocketSerialization/WebSocketSerialization.js'
5
- import * as WebSocketServer from '../WebSocketServer/WebSocketServer.js'
6
-
7
- export const listen = async ({ request, handle }) => {
8
- if (!request) {
9
- throw new IpcError('request must be defined')
10
- }
11
- if (!handle) {
12
- throw new IpcError('handle must be defined')
13
- }
14
- const webSocket = await WebSocketServer.handleUpgrade(request, handle)
15
- webSocket.pause()
16
- if (!IsWebSocketOpen.isWebSocketOpen(webSocket)) {
17
- const { type, event } = await GetFirstWebSocketEvent.getFirstWebSocketEvent(webSocket)
18
- }
19
- return webSocket
20
- }
21
-
22
- export const wrap = (webSocket) => {
23
- return {
24
- webSocket,
25
- /**
26
- * @type {any}
27
- */
28
- wrappedListener: undefined,
29
- on(event, listener) {
30
- switch (event) {
31
- case 'message':
32
- const wrappedListener = (message) => {
33
- const data = WebSocketSerialization.deserialize(message)
34
- listener(data)
35
- }
36
- webSocket.on('message', wrappedListener)
37
- break
38
- case 'close':
39
- webSocket.on('close', listener)
40
- break
41
- default:
42
- throw new Error('unknown event listener type')
43
- }
44
- },
45
- off(event, listener) {
46
- this.webSocket.off(event, listener)
47
- },
48
- send(message) {
49
- const stringifiedMessage = WebSocketSerialization.serialize(message)
50
- this.webSocket.send(stringifiedMessage)
51
- },
52
- dispose() {
53
- this.webSocket.close()
54
- },
55
- start() {
56
- this.webSocket.resume()
57
- },
58
- }
59
- }
@@ -1,20 +0,0 @@
1
- import * as GetHelpfulChildProcessError from '../GetHelpfulChildProcessError/GetHelpfulChildProcessError.js'
2
- import { VError } from '../VError/VError.js'
3
-
4
- export class IpcError extends VError {
5
- constructor(message, stdout = '', stderr = '') {
6
- if (stdout || stderr) {
7
- const { message, code, stack } = GetHelpfulChildProcessError.getHelpfulChildProcessError(stdout, stderr)
8
- const cause = new Error(message)
9
- // @ts-ignore
10
- cause.code = code
11
- cause.stack = stack
12
- super(cause, message)
13
- } else {
14
- super(message)
15
- }
16
- this.name = 'IpcError'
17
- this.stdout = stdout
18
- this.stderr = stderr
19
- }
20
- }
@@ -1,43 +0,0 @@
1
- import { utilityProcess } from 'electron'
2
- import * as Assert from '../Assert/Assert.js'
3
- import * as FirstNodeWorkerEventType from '../FirstNodeWorkerEventType/FirstNodeWorkerEventType.js'
4
- import * as GetFirstUtilityProcessEvent from '../GetFirstUtilityProcessEvent/GetFirstUtilityProcessEvent.js'
5
- import { IpcError } from '../IpcError/IpcError.js'
6
-
7
- export const create = async ({ path, argv = [], execArgv = [], name }) => {
8
- Assert.string(path)
9
- const actualArgv = ['--ipc-type=electron-utility-process', ...argv]
10
- const childProcess = utilityProcess.fork(path, actualArgv, {
11
- execArgv,
12
- stdio: 'pipe',
13
- serviceName: name,
14
- })
15
- // @ts-ignore
16
- childProcess.stdout.pipe(process.stdout)
17
- const { type, event, stdout, stderr } = await GetFirstUtilityProcessEvent.getFirstUtilityProcessEvent(childProcess)
18
- if (type === FirstNodeWorkerEventType.Exit) {
19
- throw new IpcError(`Utility process exited before ipc connection was established`, stdout, stderr)
20
- }
21
- // @ts-ignore
22
- childProcess.stderr.pipe(process.stderr)
23
- return childProcess
24
- }
25
-
26
- export const wrap = (process) => {
27
- return {
28
- process,
29
- on(event, listener) {
30
- this.process.on(event, listener)
31
- },
32
- send(message) {
33
- this.process.postMessage(message)
34
- },
35
- sendAndTransfer(message, transfer) {
36
- Assert.array(transfer)
37
- this.process.postMessage(message, transfer)
38
- },
39
- dispose() {
40
- this.process.kill()
41
- },
42
- }
43
- }