@lvce-editor/ipc 2.1.0 → 3.1.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 (36) hide show
  1. package/README.md +1 -1
  2. package/dist/index.d.ts +18 -0
  3. package/dist/index.js +913 -7
  4. package/package.json +4 -3
  5. package/dist/parts/Assert/Assert.js +0 -1
  6. package/dist/parts/CamelCase/CamelCase.js +0 -11
  7. package/dist/parts/Character/Character.js +0 -12
  8. package/dist/parts/ChildProcessError/ChildProcessError.js +0 -21
  9. package/dist/parts/ErrorCodes/ErrorCodes.js +0 -22
  10. package/dist/parts/FirstNodeWorkerEventType/FirstNodeWorkerEventType.js +0 -3
  11. package/dist/parts/FirstWebSocketEventType/FirstWebSocketEventType.js +0 -2
  12. package/dist/parts/FormatUtilityProcessName/FormatUtilityProcessName.js +0 -5
  13. package/dist/parts/GetFirstEvent/GetFirstEvent.js +0 -23
  14. package/dist/parts/GetFirstNodeChildProcessEvent/GetFirstNodeChildProcessEvent.js +0 -41
  15. package/dist/parts/GetFirstNodeWorkerEvent/GetFirstNodeWorkerEvent.js +0 -9
  16. package/dist/parts/GetFirstUtilityProcessEvent/GetFirstUtilityProcessEvent.js +0 -42
  17. package/dist/parts/GetFirstWebSocketEvent/GetFirstWebSocketEvent.js +0 -25
  18. package/dist/parts/GetHelpfulChildProcessError/GetHelpfulChildProcessError.js +0 -119
  19. package/dist/parts/GetUtilityProcessPortData/GetUtilityProcessPortData.js +0 -10
  20. package/dist/parts/IpcChildWithElectronMessagePort/IpcChildWithElectronMessagePort.js +0 -51
  21. package/dist/parts/IpcChildWithElectronUtilityProcess/IpcChildWithElectronUtilityProcess.js +0 -45
  22. package/dist/parts/IpcChildWithNodeForkedProcess/IpcChildWithNodeForkedProcess.js +0 -46
  23. package/dist/parts/IpcChildWithWebSocket/IpcChildWithWebSocket.js +0 -59
  24. package/dist/parts/IpcError/IpcError.js +0 -20
  25. package/dist/parts/IpcParentWithElectronUtilityProcess/IpcParentWithElectronUtilityProcess.js +0 -43
  26. package/dist/parts/IpcParentWithNodeForkedProcess/IpcParentWithNodeForkedProcess.js +0 -54
  27. package/dist/parts/IpcParentWithNodeWorker/IpcParentWithNodeWorker.js +0 -49
  28. package/dist/parts/IsMessagePortMain/IsMessagePortMain.js +0 -3
  29. package/dist/parts/IsWebSocketOpen/IsWebSocketOpen.js +0 -5
  30. package/dist/parts/JoinLines/JoinLines.js +0 -5
  31. package/dist/parts/Promises/Promises.js +0 -19
  32. package/dist/parts/SplitLines/SplitLines.js +0 -5
  33. package/dist/parts/VError/VError.js +0 -1
  34. package/dist/parts/WebSocketReadyState/WebSocketReadyState.js +0 -5
  35. package/dist/parts/WebSocketSerialization/WebSocketSerialization.js +0 -7
  36. package/dist/parts/WebSocketServer/WebSocketServer.js +0 -1
@@ -1,54 +0,0 @@
1
- import { fork } from 'node:child_process'
2
- import * as Assert from '../Assert/Assert.js'
3
- import { ChildProcessError } from '../ChildProcessError/ChildProcessError.js'
4
- import * as FirstNodeWorkerEventType from '../FirstNodeWorkerEventType/FirstNodeWorkerEventType.js'
5
- import * as GetFirstNodeChildProcessEvent from '../GetFirstNodeChildProcessEvent/GetFirstNodeChildProcessEvent.js'
6
- import { VError } from '../VError/VError.js'
7
-
8
- export const create = async ({ path, argv = [], env, execArgv = [], stdio = 'inherit', name = 'child process' }) => {
9
- try {
10
- Assert.string(path)
11
- const actualArgv = ['--ipc-type=node-forked-process', ...argv]
12
- const childProcess = fork(path, actualArgv, {
13
- env,
14
- execArgv,
15
- stdio: 'pipe',
16
- })
17
- const { type, event, stdout, stderr } = await GetFirstNodeChildProcessEvent.getFirstNodeChildProcessEvent(childProcess)
18
- if (type === FirstNodeWorkerEventType.Exit) {
19
- throw new ChildProcessError(stderr)
20
- }
21
- if (type === FirstNodeWorkerEventType.Error) {
22
- throw new Error(`child process had an error ${event}`)
23
- }
24
- if (stdio === 'inherit' && childProcess.stdout && childProcess.stderr) {
25
- childProcess.stdout.pipe(process.stdout)
26
- childProcess.stderr.pipe(process.stderr)
27
- }
28
- return childProcess
29
- } catch (error) {
30
- throw new VError(error, `Failed to launch ${name}`)
31
- }
32
- }
33
-
34
- export const wrap = (childProcess) => {
35
- return {
36
- childProcess,
37
- on(event, listener) {
38
- this.childProcess.on(event, listener)
39
- },
40
- off(event, listener) {
41
- this.childProcess.off(event, listener)
42
- },
43
- send(message) {
44
- this.childProcess.send(message)
45
- },
46
- sendAndTransfer(message, handle) {
47
- this.childProcess.send(message, handle)
48
- },
49
- dispose() {
50
- this.childProcess.kill()
51
- },
52
- pid: childProcess.pid,
53
- }
54
- }
@@ -1,49 +0,0 @@
1
- import { Worker } from 'node:worker_threads'
2
- import * as Assert from '../Assert/Assert.js'
3
- import * as FirstNodeWorkerEventType from '../FirstNodeWorkerEventType/FirstNodeWorkerEventType.js'
4
- import * as GetFirstNodeWorkerEvent from '../GetFirstNodeWorkerEvent/GetFirstNodeWorkerEvent.js'
5
- import { IpcError } from '../IpcError/IpcError.js'
6
-
7
- export const create = async ({ path, argv = [], env = process.env, execArgv = [] }) => {
8
- Assert.string(path)
9
- const actualArgv = ['--ipc-type=node-worker', ...argv]
10
- const actualEnv = {
11
- ...env,
12
- ELECTRON_RUN_AS_NODE: '1',
13
- }
14
- const worker = new Worker(path, {
15
- argv: actualArgv,
16
- env: actualEnv,
17
- execArgv,
18
- })
19
- const { type, event } = await GetFirstNodeWorkerEvent.getFirstNodeWorkerEvent(worker)
20
- if (type === FirstNodeWorkerEventType.Exit) {
21
- throw new IpcError(`Worker exited before ipc connection was established`)
22
- }
23
- if (type === FirstNodeWorkerEventType.Error) {
24
- throw new IpcError(`Worker threw an error before ipc connection was established: ${event}`)
25
- }
26
- if (event !== 'ready') {
27
- throw new IpcError('unexpected first message from worker')
28
- }
29
- return worker
30
- }
31
-
32
- export const wrap = (worker) => {
33
- return {
34
- worker,
35
- on(event, listener) {
36
- this.worker.on(event, listener)
37
- },
38
- send(message) {
39
- this.worker.postMessage(message)
40
- },
41
- sendAndTransfer(message, transfer) {
42
- Assert.array(transfer)
43
- this.worker.postMessage(message, transfer)
44
- },
45
- dispose() {
46
- this.worker.terminate()
47
- },
48
- }
49
- }
@@ -1,3 +0,0 @@
1
- export const isMessagePortMain = (value) => {
2
- return value && value.constructor && value.constructor.name === 'MessagePortMain'
3
- }
@@ -1,5 +0,0 @@
1
- import { WebSocket } from 'ws'
2
-
3
- export const isWebSocketOpen = (webSocket) => {
4
- return webSocket.readyState === WebSocket.OPEN
5
- }
@@ -1,5 +0,0 @@
1
- import * as Character from '../Character/Character.js'
2
-
3
- export const joinLines = (lines) => {
4
- return lines.join(Character.NewLine)
5
- }
@@ -1,19 +0,0 @@
1
- export const withResolvers = () => {
2
- /**
3
- * @type {any}
4
- */
5
- let _resolve
6
- /**
7
- * @type {any}
8
- */
9
- let _reject
10
- const promise = new Promise((resolve, reject) => {
11
- _resolve = resolve
12
- _reject = reject
13
- })
14
- return {
15
- resolve: _resolve,
16
- reject: _reject,
17
- promise,
18
- }
19
- }
@@ -1,5 +0,0 @@
1
- import * as Character from '../Character/Character.js'
2
-
3
- export const splitLines = (lines) => {
4
- return lines.split(Character.NewLine)
5
- }
@@ -1 +0,0 @@
1
- export * from '@lvce-editor/verror'
@@ -1,5 +0,0 @@
1
- import { WebSocket } from 'ws'
2
-
3
- export const Open = WebSocket.OPEN
4
-
5
- export const Closed = WebSocket.CLOSED
@@ -1,7 +0,0 @@
1
- export const serialize = (message) => {
2
- return JSON.stringify(message)
3
- }
4
-
5
- export const deserialize = (message) => {
6
- return JSON.parse(message.toString())
7
- }
@@ -1 +0,0 @@
1
- export * from '@lvce-editor/web-socket-server'