@lvce-editor/shared-process 0.15.17 → 0.15.19

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 (67) hide show
  1. package/config/builtinCommands.json +4 -0
  2. package/extensions/builtin.language-basics-dart/src/tokenizeDart.js +2 -1
  3. package/extensions/builtin.language-basics-html/src/tokenizeHtml.js +77 -3
  4. package/extensions/builtin.language-basics-javascript/src/tokenizeJavaScript.js +3 -0
  5. package/package.json +9 -9
  6. package/src/parts/AttachDebugger/AttachDebugger.ipc.js +7 -0
  7. package/src/parts/AttachDebugger/AttachDebugger.js +6 -0
  8. package/src/parts/Callback/Callback.js +17 -26
  9. package/src/parts/Character/Character.js +11 -0
  10. package/src/parts/CleanStack/CleanStack.js +4 -0
  11. package/src/parts/CommandNotFoundError/CommandNotFoundError.js +2 -1
  12. package/src/parts/Configuration/Configuration.js +4 -7
  13. package/src/parts/CreateCpuProfile/CreateCpuProfile.js +59 -0
  14. package/src/parts/DestroySocket/DestroySocket.js +5 -0
  15. package/src/parts/DevtoolsProtocolRpc/DevtoolsProtocolRpc.js +32 -0
  16. package/src/parts/ElectronInitialize/ElectronInitialize.js +9 -38
  17. package/src/parts/ErrorHandling/ErrorHandling.js +14 -15
  18. package/src/parts/ExportStatic/ExportStatic.js +4 -14
  19. package/src/parts/GetActualPath/GetActualPath.js +8 -0
  20. package/src/parts/GetErrorResponse/GetErrorResponse.js +5 -3
  21. package/src/parts/GetFirstWebSocketEvent/GetFirstWebSocketEvent.js +41 -0
  22. package/src/parts/GetNewLineIndex/GetNewLineIndex.js +3 -1
  23. package/src/parts/GetResponse/GetResponse.js +3 -3
  24. package/src/parts/GetTerminalSpawnOptions/GetTerminalSpawnOptions.ipc.js +7 -0
  25. package/src/parts/GetTerminalSpawnOptions/GetTerminalSpawnOptions.js +14 -0
  26. package/src/parts/HandleElectronMessagePort/HandleElectronMessagePort.ipc.js +7 -0
  27. package/src/parts/HandleElectronMessagePort/HandleElectronMessagePort.js +14 -0
  28. package/src/parts/HandleIpc/HandleIpc.js +4 -4
  29. package/src/parts/HandleNodeMessagePort/HandleNodeMessagePort.ipc.js +7 -0
  30. package/src/parts/HandleNodeMessagePort/HandleNodeMessagePort.js +12 -0
  31. package/src/parts/HandleWebSocket/HandleWebSocket.js +15 -14
  32. package/src/parts/HandleWebSocketForSharedProcess/HandleWebSocketForSharedProcess.js +10 -3
  33. package/src/parts/Id/Id.js +7 -0
  34. package/src/parts/IpcChild/IpcChild.js +3 -2
  35. package/src/parts/IpcChildModule/IpcChildModule.js +6 -0
  36. package/src/parts/IpcChildType/IpcChildType.js +3 -0
  37. package/src/parts/IpcChildWithElectronMessagePort/IpcChildWithElectronMessagePort.js +41 -0
  38. package/src/parts/IpcChildWithElectronUtilityProcess/IpcChildWithElectronUtilityProcess.js +25 -1
  39. package/src/parts/IpcChildWithNodeForkedProcess/IpcChildWithNodeForkedProcess.js +21 -1
  40. package/src/parts/IpcChildWithNodeMessagePort/IpcChildWithNodeMessagePort.js +41 -0
  41. package/src/parts/IpcChildWithNodeWorker/IpcChildWithNodeWorker.js +17 -1
  42. package/src/parts/IpcChildWithWebSocket/IpcChildWithWebSocket.js +51 -0
  43. package/src/parts/IpcError/IpcError.js +6 -0
  44. package/src/parts/IsMessagePort/IsMessagePort.js +3 -0
  45. package/src/parts/IsMessagePortMain/IsMessagePortMain.js +3 -0
  46. package/src/parts/IsSocket/IsSocket.js +5 -0
  47. package/src/parts/IsWebSocketOpen/IsWebSocketOpen.js +5 -0
  48. package/src/parts/JoinLines/JoinLines.js +3 -1
  49. package/src/parts/Json/Json.js +2 -1
  50. package/src/parts/JsonError/JsonError.js +3 -1
  51. package/src/parts/JsonRpc/JsonRpc.js +18 -22
  52. package/src/parts/Module/Module.js +10 -0
  53. package/src/parts/ModuleId/ModuleId.js +5 -0
  54. package/src/parts/ModuleMap/ModuleMap.js +14 -0
  55. package/src/parts/PrettyError/PrettyError.js +43 -53
  56. package/src/parts/Process/Process.ipc.js +1 -0
  57. package/src/parts/Process/Process.js +8 -0
  58. package/src/parts/ProcessDisplayName/ProcessDisplayName.js +1 -0
  59. package/src/parts/RequiresSocket/RequiresSocket.js +0 -1
  60. package/src/parts/Signal/Signal.js +2 -0
  61. package/src/parts/SplitLines/SplitLines.js +3 -1
  62. package/src/parts/Terminal/Terminal.js +31 -18
  63. package/src/parts/TerminalState/TerminalState.js +15 -0
  64. package/src/parts/WebSocketServer/WebSocketServer.js +7 -2
  65. package/src/sharedProcessMain.cjs +1 -0
  66. package/src/sharedProcessMain.js +2 -1
  67. package/src/parts/Socket/Socket.js +0 -77
@@ -12,7 +12,11 @@ const shouldLogError = (error) => {
12
12
  return true
13
13
  }
14
14
 
15
- export const getErrorResponse = (message, error) => {
15
+ export const getErrorResponse = (message, error, ipc) => {
16
+ const prettyError = PrettyError.prepare(error)
17
+ if (shouldLogError(error) && (!ipc || ipc.shouldLogError !== false)) {
18
+ PrintPrettyError.printPrettyError(prettyError, `[shared-process] `)
19
+ }
16
20
  if (error && error instanceof CommandNotFoundError) {
17
21
  return {
18
22
  jsonrpc: JsonRpcVersion.Two,
@@ -37,8 +41,6 @@ export const getErrorResponse = (message, error) => {
37
41
  },
38
42
  }
39
43
  }
40
- const prettyError = PrettyError.prepare(error)
41
- PrintPrettyError.printPrettyError(prettyError, `[shared-process] `)
42
44
  return {
43
45
  jsonrpc: JsonRpcVersion.Two,
44
46
  id: message.id,
@@ -0,0 +1,41 @@
1
+ import * as FirstWebSocketEventType from '../FirstWebSocketEventType/FirstWebSocketEventType.js'
2
+ import { WebSocket } from 'ws'
3
+
4
+ export const getFirstWebSocketEvent = async (webSocket) => {
5
+ switch (webSocket.readyState) {
6
+ case WebSocket.OPEN:
7
+ return {
8
+ type: FirstWebSocketEventType.Open,
9
+ event: undefined,
10
+ }
11
+ case WebSocket.CLOSED:
12
+ return {
13
+ type: FirstWebSocketEventType.Close,
14
+ event: undefined,
15
+ }
16
+ default:
17
+ break
18
+ }
19
+ const { type, event } = await new Promise((resolve) => {
20
+ const cleanup = (value) => {
21
+ webSocket.off('open', handleOpen)
22
+ webSocket.off('close', handleClose)
23
+ resolve(value)
24
+ }
25
+ const handleOpen = (event) => {
26
+ cleanup({
27
+ type: FirstWebSocketEventType.Open,
28
+ event,
29
+ })
30
+ }
31
+ const handleClose = (event) => {
32
+ cleanup({
33
+ type: FirstWebSocketEventType.Close,
34
+ event,
35
+ })
36
+ }
37
+ webSocket.on('open', handleOpen)
38
+ webSocket.on('close', handleClose)
39
+ })
40
+ return { type, event }
41
+ }
@@ -1,3 +1,5 @@
1
+ import * as Character from '../Character/Character.js'
2
+
1
3
  /**
2
4
  *
3
5
  * @param {string} string
@@ -5,5 +7,5 @@
5
7
  * @returns
6
8
  */
7
9
  export const getNewLineIndex = (string, startIndex = undefined) => {
8
- return string.indexOf('\n', startIndex)
10
+ return string.indexOf(Character.NewLine, startIndex)
9
11
  }
@@ -3,13 +3,13 @@ import * as GetErrorResponse from '../GetErrorResponse/GetErrorResponse.js'
3
3
  import * as GetSuccessResponse from '../GetSuccessResponse/GetSuccessResponse.js'
4
4
  import * as RequiresSocket from '../RequiresSocket/RequiresSocket.js'
5
5
 
6
- export const getResponse = async (message, handle) => {
6
+ export const getResponse = async (message, ipc) => {
7
7
  try {
8
8
  const result = RequiresSocket.requiresSocket(message.method)
9
- ? await Command.execute(message.method, handle, ...message.params)
9
+ ? await Command.execute(message.method, ipc, ...message.params)
10
10
  : await Command.execute(message.method, ...message.params)
11
11
  return GetSuccessResponse.getSuccessResponse(message, result)
12
12
  } catch (error) {
13
- return GetErrorResponse.getErrorResponse(message, error)
13
+ return GetErrorResponse.getErrorResponse(message, error, ipc)
14
14
  }
15
15
  }
@@ -0,0 +1,7 @@
1
+ import * as GetTerminalSpawnOptions from './GetTerminalSpawnOptions.js'
2
+
3
+ export const name = 'GetTerminalSpawnOptions'
4
+
5
+ export const Commands = {
6
+ getTerminalSpawnOptions: GetTerminalSpawnOptions.getTerminalSpawnOptions,
7
+ }
@@ -0,0 +1,14 @@
1
+ import * as Platform from '../Platform/Platform.js'
2
+
3
+ export const getTerminalSpawnOptions = () => {
4
+ if (Platform.isWindows) {
5
+ return {
6
+ command: 'powershell.exe',
7
+ args: [],
8
+ }
9
+ }
10
+ return {
11
+ command: 'bash',
12
+ args: ['-i'],
13
+ }
14
+ }
@@ -0,0 +1,7 @@
1
+ import * as HandleElectronMessagePort from './HandleElectronMessagePort.js'
2
+
3
+ export const name = 'HandleElectronMessagePort'
4
+
5
+ export const Commands = {
6
+ handleElectronMessagePort: HandleElectronMessagePort.handleElectronMessagePort,
7
+ }
@@ -0,0 +1,14 @@
1
+ import * as Assert from '../Assert/Assert.js'
2
+ import * as HandleIpc from '../HandleIpc/HandleIpc.js'
3
+ import * as IpcChild from '../IpcChild/IpcChild.js'
4
+ import * as IpcChildType from '../IpcChildType/IpcChildType.js'
5
+
6
+ export const handleElectronMessagePort = async (messagePort) => {
7
+ Assert.object(messagePort)
8
+ const ipc = await IpcChild.listen({
9
+ method: IpcChildType.ElectronMessagePort,
10
+ messagePort,
11
+ })
12
+ HandleIpc.handleIpc(ipc)
13
+ messagePort.start()
14
+ }
@@ -7,9 +7,9 @@ const handleJsonRpcResult = (message) => {
7
7
  }
8
8
 
9
9
  export const handleIpc = (ipc) => {
10
- const handleJsonRpcMessage = async (message, handle) => {
10
+ const handleJsonRpcMessage = async (message) => {
11
11
  if (message.method) {
12
- const response = await GetResponse.getResponse(message, handle)
12
+ const response = await GetResponse.getResponse(message, ipc)
13
13
  ipc.send(response)
14
14
  } else {
15
15
  // TODO handle error
@@ -17,12 +17,12 @@ export const handleIpc = (ipc) => {
17
17
  }
18
18
  }
19
19
 
20
- const handleMessageFromParentProcess = async (message, handle) => {
20
+ const handleMessageFromParentProcess = async (message) => {
21
21
  if (message.result) {
22
22
  return handleJsonRpcResult(message)
23
23
  }
24
24
  if (message.method) {
25
- return handleJsonRpcMessage(message, handle)
25
+ return handleJsonRpcMessage(message)
26
26
  }
27
27
  console.warn('unknown message', message)
28
28
  }
@@ -0,0 +1,7 @@
1
+ import * as HandleNodeMessagePort from './HandleNodeMessagePort.js'
2
+
3
+ export const name = 'HandleNodeMessagePort'
4
+
5
+ export const Commands = {
6
+ handleNodeMessagePort: HandleNodeMessagePort.handleNodeMessagePort,
7
+ }
@@ -0,0 +1,12 @@
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
+
5
+ export const handleNodeMessagePort = async (messagePort) => {
6
+ const ipc = await IpcChild.listen({
7
+ method: IpcChildType.NodeMessagePort,
8
+ messagePort,
9
+ })
10
+ HandleIpc.handleIpc(ipc)
11
+ messagePort.start()
12
+ }
@@ -1,25 +1,26 @@
1
1
  import * as Assert from '../Assert/Assert.js'
2
+ import * as DestroyWebSocket from '../DestroySocket/DestroySocket.js'
2
3
  import * as HandleWebSocketModule from '../HandleWebSocketModule/HandleWebSocketModule.js'
3
4
  import { VError } from '../VError/VError.js'
4
5
 
5
- export const handleWebSocket = async (handle, message) => {
6
- Assert.object(message)
7
- Assert.object(handle)
8
- const headers = message.headers
9
- if (!headers) {
10
- throw new VError('missing websocket headers')
11
- }
12
- const protocol = headers['sec-websocket-protocol']
13
- if (!protocol) {
14
- throw new VError('missing sec websocket protocol header')
15
- }
6
+ export const handleWebSocket = async (message, handle) => {
16
7
  try {
8
+ Assert.object(message)
9
+ Assert.object(handle)
10
+ const headers = message.headers
11
+ if (!headers) {
12
+ throw new VError('missing websocket headers')
13
+ }
14
+ const protocol = headers['sec-websocket-protocol']
15
+ if (!protocol) {
16
+ throw new VError('missing sec websocket protocol header')
17
+ }
18
+ handle.pause()
17
19
  const module = await HandleWebSocketModule.load(protocol)
18
20
  await module.handleWebSocket(message, handle, protocol)
21
+ handle.resume()
19
22
  } catch (error) {
20
- try {
21
- handle.destroy()
22
- } catch {}
23
+ DestroyWebSocket.destroySocket(handle)
23
24
  throw new VError(error, `Failed to connect to websocket`)
24
25
  }
25
26
  }
@@ -1,16 +1,23 @@
1
1
  import * as ErrorCodes from '../ErrorCodes/ErrorCodes.js'
2
- import * as Socket from '../Socket/Socket.js'
2
+ import * as HandleIpc from '../HandleIpc/HandleIpc.js'
3
+ import * as IpcChild from '../IpcChild/IpcChild.js'
4
+ import * as IpcChildType from '../IpcChildType/IpcChildType.js'
5
+ import * as Logger from '../Logger/Logger.js'
3
6
  import * as WebSocketServer from '../WebSocketServer/WebSocketServer.js'
4
7
 
5
8
  const handleSocketError = (error) => {
6
9
  if (error && error.code === ErrorCodes.ECONNRESET) {
7
10
  return
8
11
  }
9
- console.info('[info shared process: handle error]', error)
12
+ Logger.info('[info shared process: handle error]', error)
10
13
  }
11
14
 
12
15
  export const handleWebSocket = async (message, handle) => {
13
16
  handle.on('error', handleSocketError)
14
17
  const webSocket = await WebSocketServer.handleUpgrade(message, handle)
15
- Socket.set(webSocket)
18
+ const ipc = await IpcChild.listen({
19
+ method: IpcChildType.WebSocket,
20
+ webSocket,
21
+ })
22
+ HandleIpc.handleIpc(ipc)
16
23
  }
@@ -0,0 +1,7 @@
1
+ export const state = {
2
+ id: 0,
3
+ }
4
+
5
+ export const create = () => {
6
+ return ++state.id
7
+ }
@@ -1,8 +1,9 @@
1
1
  import * as IpcChildModule from '../IpcChildModule/IpcChildModule.js'
2
2
 
3
- export const listen = async ({ method }) => {
3
+ export const listen = async ({ method, ...params }) => {
4
4
  const module = await IpcChildModule.getModule(method)
5
- const rawIpc = await module.listen()
5
+ // @ts-ignore
6
+ const rawIpc = await module.listen(params)
6
7
  const ipc = module.wrap(rawIpc)
7
8
  return ipc
8
9
  }
@@ -8,6 +8,12 @@ export const getModule = (method) => {
8
8
  return import('../IpcChildWithNodeWorker/IpcChildWithNodeWorker.js')
9
9
  case IpcChildType.ElectronUtilityProcess:
10
10
  return import('../IpcChildWithElectronUtilityProcess/IpcChildWithElectronUtilityProcess.js')
11
+ case IpcChildType.ElectronMessagePort:
12
+ return import('../IpcChildWithElectronMessagePort/IpcChildWithElectronMessagePort.js')
13
+ case IpcChildType.NodeMessagePort:
14
+ return import('../IpcChildWithNodeMessagePort/IpcChildWithNodeMessagePort.js')
15
+ case IpcChildType.WebSocket:
16
+ return import('../IpcChildWithWebSocket/IpcChildWithWebSocket.js')
11
17
  default:
12
18
  throw new Error('unexpected ipc type')
13
19
  }
@@ -1,6 +1,9 @@
1
1
  export const NodeWorker = 1
2
2
  export const NodeForkedProcess = 2
3
3
  export const ElectronUtilityProcess = 3
4
+ export const ElectronMessagePort = 4
5
+ export const NodeMessagePort = 5
6
+ export const WebSocket = 6
4
7
 
5
8
  export const Auto = () => {
6
9
  const { argv } = process
@@ -0,0 +1,41 @@
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
+ return event.data
13
+ }
14
+
15
+ export const wrap = (messagePort) => {
16
+ return {
17
+ messagePort,
18
+ on(event, listener) {
19
+ if (event === 'message') {
20
+ const wrappedListener = (event) => {
21
+ const actualData = getActualData(event)
22
+ listener(actualData)
23
+ }
24
+ this.messagePort.on(event, wrappedListener)
25
+ } else if (event === 'close') {
26
+ this.messagePort.on('close', listener)
27
+ } else {
28
+ throw new Error('unsupported event type')
29
+ }
30
+ },
31
+ off(event, listener) {
32
+ this.messagePort.off(event, listener)
33
+ },
34
+ send(message) {
35
+ this.messagePort.postMessage(message)
36
+ },
37
+ dispose() {
38
+ this.messagePort.close()
39
+ },
40
+ }
41
+ }
@@ -1,3 +1,5 @@
1
+ import * as IsElectron from '../IsElectron/IsElectron.js'
2
+
1
3
  export const listen = () => {
2
4
  // @ts-ignore
3
5
  const parentPort = process.parentPort
@@ -8,11 +10,33 @@ export const listen = () => {
8
10
  return parentPort
9
11
  }
10
12
 
13
+ const getActualData = (event) => {
14
+ const { data, ports } = event
15
+ if (ports.length === 0) {
16
+ return data
17
+ }
18
+ return {
19
+ ...data,
20
+ params: [...data.params, ...ports],
21
+ }
22
+ }
23
+
11
24
  export const wrap = (parentPort) => {
12
25
  return {
26
+ shouldLogError: !IsElectron.isElectron(),
13
27
  parentPort,
14
28
  on(event, listener) {
15
- this.parentPort.on(event, listener)
29
+ if (event === 'message') {
30
+ const wrappedListener = (event) => {
31
+ const actualData = getActualData(event)
32
+ listener(actualData)
33
+ }
34
+ this.parentPort.on(event, wrappedListener)
35
+ } else if (event === 'close') {
36
+ this.parentPort.on('close', listener)
37
+ } else {
38
+ throw new Error('unsupported event type')
39
+ }
16
40
  },
17
41
  off(event, listener) {
18
42
  this.parentPort.off(event, listener)
@@ -6,11 +6,31 @@ export const listen = async () => {
6
6
  return process
7
7
  }
8
8
 
9
+ const getActualData = (message, handle) => {
10
+ if (handle) {
11
+ return {
12
+ ...message,
13
+ params: [...message.params, handle],
14
+ }
15
+ }
16
+ return message
17
+ }
18
+
9
19
  export const wrap = (process) => {
10
20
  return {
11
21
  process,
12
22
  on(event, listener) {
13
- this.process.on(event, listener)
23
+ if (event === 'message') {
24
+ const wrappedListener = (event, handle) => {
25
+ const actualData = getActualData(event, handle)
26
+ listener(actualData)
27
+ }
28
+ this.process.on(event, wrappedListener)
29
+ } else if (event === 'close') {
30
+ this.process.on('close', listener)
31
+ } else {
32
+ throw new Error('unsupported event type')
33
+ }
14
34
  },
15
35
  off(event, listener) {
16
36
  this.process.off(event, listener)
@@ -0,0 +1,41 @@
1
+ import { IpcError } from '../IpcError/IpcError.js'
2
+ import * as IsMessagePort from '../IsMessagePort/IsMessagePort.js'
3
+
4
+ export const listen = ({ messagePort }) => {
5
+ if (!IsMessagePort.isMessagePort(messagePort)) {
6
+ throw new IpcError('port must be of type MessagePort')
7
+ }
8
+ return messagePort
9
+ }
10
+
11
+ const getActualData = (message) => {
12
+ return message
13
+ }
14
+
15
+ export const wrap = (messagePort) => {
16
+ return {
17
+ messagePort,
18
+ on(event, listener) {
19
+ if (event === 'message') {
20
+ const wrappedListener = (event) => {
21
+ const actualData = getActualData(event)
22
+ listener(actualData)
23
+ }
24
+ this.messagePort.on(event, wrappedListener)
25
+ } else if (event === 'close') {
26
+ this.messagePort.on('close', listener)
27
+ } else {
28
+ throw new Error(`unsupported event type ${event}`)
29
+ }
30
+ },
31
+ off(event, listener) {
32
+ this.messagePort.off(event, listener)
33
+ },
34
+ send(message) {
35
+ this.messagePort.postMessage(message)
36
+ },
37
+ dispose() {
38
+ this.messagePort.close()
39
+ },
40
+ }
41
+ }
@@ -1,4 +1,5 @@
1
1
  import { parentPort } from 'node:worker_threads'
2
+ import * as IsElectron from '../IsElectron/IsElectron.js'
2
3
 
3
4
  export const listen = async () => {
4
5
  if (!parentPort) {
@@ -8,11 +9,26 @@ export const listen = async () => {
8
9
  return parentPort
9
10
  }
10
11
 
12
+ const getActualData = (message) => {
13
+ return message
14
+ }
15
+
11
16
  export const wrap = (parentPort) => {
12
17
  return {
18
+ shouldLogError: !IsElectron.isElectron(),
13
19
  parentPort,
14
20
  on(event, listener) {
15
- this.parentPort.on(event, listener)
21
+ if (event === 'message') {
22
+ const wrappedListener = (event) => {
23
+ const actualData = getActualData(event)
24
+ listener(actualData)
25
+ }
26
+ this.parentPort.on(event, wrappedListener)
27
+ } else if (event === 'close') {
28
+ this.parentPort.on('close', listener)
29
+ } else {
30
+ throw new Error('unsupported event type')
31
+ }
16
32
  },
17
33
  off(event, listener) {
18
34
  this.parentPort.off(event, listener)
@@ -0,0 +1,51 @@
1
+ import * as GetFirstWebSocketEvent from '../GetFirstWebSocketEvent/GetFirstWebSocketEvent.js'
2
+ import * as IsWebSocketOpen from '../IsWebSocketOpen/IsWebSocketOpen.js'
3
+
4
+ export const listen = async ({ webSocket }) => {
5
+ if (!IsWebSocketOpen.isWebSocketOpen(webSocket)) {
6
+ const { type, event } = await GetFirstWebSocketEvent.getFirstWebSocketEvent(webSocket)
7
+ console.log({ type, event })
8
+ }
9
+ return webSocket
10
+ }
11
+
12
+ const getActualData = (message) => {
13
+ const data = JSON.parse(message.toString())
14
+ return data
15
+ }
16
+
17
+ export const wrap = (webSocket) => {
18
+ return {
19
+ webSocket,
20
+ /**
21
+ * @type {any}
22
+ */
23
+ wrappedListener: undefined,
24
+ on(event, listener) {
25
+ switch (event) {
26
+ case 'message':
27
+ const wrappedListener = (message) => {
28
+ const data = getActualData(message)
29
+ listener(data)
30
+ }
31
+ webSocket.on('message', wrappedListener)
32
+ break
33
+ case 'close':
34
+ webSocket.on('close', listener)
35
+ break
36
+ default:
37
+ throw new Error('unknown event listener type')
38
+ }
39
+ },
40
+ off(event, listener) {
41
+ this.webSocket.off(event, listener)
42
+ },
43
+ send(message) {
44
+ const stringifiedMessage = JSON.stringify(message)
45
+ this.webSocket.send(stringifiedMessage)
46
+ },
47
+ dispose() {
48
+ this.webSocket.close()
49
+ },
50
+ }
51
+ }
@@ -0,0 +1,6 @@
1
+ export class IpcError extends Error {
2
+ constructor(message) {
3
+ super(message)
4
+ this.name = 'IpcError'
5
+ }
6
+ }
@@ -0,0 +1,3 @@
1
+ export const isMessagePort = (value) => {
2
+ return value && value instanceof MessagePort
3
+ }
@@ -0,0 +1,3 @@
1
+ export const isMessagePortMain = (value) => {
2
+ return value && value.constructor && value.constructor.name === 'MessagePortMain'
3
+ }
@@ -0,0 +1,5 @@
1
+ import { Socket } from 'node:net'
2
+
3
+ export const isSocket = (value) => {
4
+ return value instanceof Socket
5
+ }
@@ -0,0 +1,5 @@
1
+ import { WebSocket } from 'ws'
2
+
3
+ export const isWebSocketOpen = (webSocket) => {
4
+ return webSocket.readyState === WebSocket.OPEN
5
+ }
@@ -1,3 +1,5 @@
1
+ import * as Character from '../Character/Character.js'
2
+
1
3
  export const joinLines = (lines) => {
2
- return lines.join('\n')
4
+ return lines.join(Character.NewLine)
3
5
  }
@@ -1,6 +1,7 @@
1
1
  // parsing error handling based on https://github.com/sindresorhus/parse-json/blob/main/index.js
2
2
 
3
3
  import { JsonParsingError } from '../JsonParsingError/JsonParsingError.js'
4
+ import * as Character from '../Character/Character.js'
4
5
 
5
6
  export const parse = async (string, filePath) => {
6
7
  try {
@@ -13,5 +14,5 @@ export const parse = async (string, filePath) => {
13
14
  }
14
15
 
15
16
  export const stringify = (value) => {
16
- return JSON.stringify(value, null, 2) + '\n'
17
+ return JSON.stringify(value, null, 2) + Character.NewLine
17
18
  }
@@ -9,8 +9,10 @@ const emptyError = {
9
9
  codeFrame: '',
10
10
  }
11
11
 
12
+ const RE_POSITION = /in JSON at position (\d+)/
13
+
12
14
  export const getErrorPropsFromError = (error, string, filePath) => {
13
- const indexMatch = error.message.match(/in JSON at position (\d+)/)
15
+ const indexMatch = error.message.match(RE_POSITION)
14
16
  if (indexMatch && indexMatch.length > 0) {
15
17
  const lines = new LinesAndColumns(string)
16
18
  const index = Number(indexMatch[1])