@lvce-editor/pty-host 0.20.10 → 0.20.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/pty-host",
3
- "version": "0.20.10",
3
+ "version": "0.20.12",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -16,6 +16,7 @@
16
16
  "node": ">=16"
17
17
  },
18
18
  "dependencies": {
19
+ "@lvce-editor/json-rpc": "^0.3.0",
19
20
  "@lvce-editor/pretty-error": "^1.2.2",
20
21
  "@lvce-editor/verror": "^1.1.2",
21
22
  "debug": "^4.3.4",
@@ -1,42 +1 @@
1
- import * as Assert from '../Assert/Assert.js'
2
- import * as Id from '../Id/Id.js'
3
-
4
- export const state = {
5
- callbacks: Object.create(null),
6
- }
7
-
8
- export const registerPromise = () => {
9
- const id = Id.create()
10
- const promise = new Promise((resolve, reject) => {
11
- state.callbacks[id] = {
12
- resolve,
13
- reject,
14
- }
15
- })
16
- return { id, promise }
17
- }
18
-
19
- export const unregister = (id) => {
20
- delete state.callbacks[id]
21
- }
22
-
23
- export const resolve = (id, args) => {
24
- Assert.number(id)
25
- if (!(id in state.callbacks)) {
26
- console.log(args)
27
- console.warn(`callback ${id} may already be disposed`)
28
- return
29
- }
30
- state.callbacks[id].resolve(args)
31
- delete state.callbacks[id]
32
- }
33
-
34
- export const reject = (id, error) => {
35
- Assert.number(id)
36
- if (!(id in state.callbacks)) {
37
- console.warn(`callback ${id} may already be disposed`)
38
- return
39
- }
40
- state.callbacks[id].reject(error)
41
- delete state.callbacks[id]
42
- }
1
+ export { resolve } from '@lvce-editor/json-rpc'
@@ -1,12 +1,22 @@
1
1
  import * as Assert from '../Assert/Assert.js'
2
2
  import * as Callback from '../Callback/Callback.js'
3
3
  import * as Command from '../Command/Command.js'
4
- import * as HandleJsonRpcMessage from '../HandleJsonRpcMessage/HandleJsonRpcMessage.js'
4
+ import * as JsonRpc from '../JsonRpc/JsonRpc.js'
5
+ import * as PrettyError from '../PrettyError/PrettyError.js'
6
+ import * as PrintPrettyError from '../PrintPrettyError/PrintPrettyError.js'
7
+ import * as RequiresSocket from '../RequiresSocket/RequiresSocket.js'
8
+
9
+ const preparePrettyError = PrettyError.prepare
10
+
11
+ const logError = (error, prettyError) => {
12
+ PrintPrettyError.printPrettyError(prettyError, `[terminal-process] `)
13
+ }
5
14
 
6
15
  export const handleIpc = (ipc) => {
7
16
  Assert.object(ipc)
8
17
  const handleMessage = (message) => {
9
- return HandleJsonRpcMessage.handleJsonRpcMessage(ipc, message, Command.execute, Callback.resolve)
18
+ // @ts-ignore
19
+ return JsonRpc.handleJsonRpcMessage(ipc, message, Command.execute, Callback.resolve, preparePrettyError, logError, RequiresSocket.requiresSocket)
10
20
  }
11
21
  ipc.on('message', handleMessage)
12
22
  }
@@ -0,0 +1 @@
1
+ export * from '@lvce-editor/json-rpc'
@@ -0,0 +1,19 @@
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
+ }
@@ -7,7 +7,7 @@ export const state = {
7
7
  }
8
8
 
9
9
  // TODO maybe merge pty and pty controller
10
- export const create = (id, cwd, command, args, ipc) => {
10
+ export const create = (ipc, id, cwd, command, args) => {
11
11
  Assert.number(id)
12
12
  Assert.string(cwd)
13
13
  Assert.string(command)
@@ -1,56 +0,0 @@
1
- import { CommandNotFoundError } from '../CommandNotFoundError/CommandNotFoundError.js'
2
- import * as ErrorCodes from '../ErrorCodes/ErrorCodes.js'
3
- import * as JsonRpcErrorCode from '../JsonRpcErrorCode/JsonRpcErrorCode.js'
4
- import * as JsonRpcVersion from '../JsonRpcVersion/JsonRpcVersion.js'
5
- import * as PrettyError from '../PrettyError/PrettyError.js'
6
- import * as PrintPrettyError from '../PrintPrettyError/PrintPrettyError.js'
7
-
8
- const shouldLogError = (error) => {
9
- if (error && error.code === ErrorCodes.ENOENT) {
10
- return false
11
- }
12
- return true
13
- }
14
-
15
- export const getErrorResponse = (message, error) => {
16
- if (error && error instanceof CommandNotFoundError) {
17
- return {
18
- jsonrpc: JsonRpcVersion.Two,
19
- id: message.id,
20
- error: {
21
- code: JsonRpcErrorCode.MethodNotFound,
22
- message: error.message,
23
- data: error.stack,
24
- },
25
- }
26
- }
27
- if (!shouldLogError(error)) {
28
- return {
29
- jsonrpc: JsonRpcVersion.Two,
30
- id: message.id,
31
- error: {
32
- code: JsonRpcErrorCode.Custom,
33
- message: `${error}`,
34
- data: {
35
- code: error.code,
36
- },
37
- },
38
- }
39
- }
40
- const prettyError = PrettyError.prepare(error)
41
- PrintPrettyError.printPrettyError(prettyError, `[terminal-process] `)
42
- return {
43
- jsonrpc: JsonRpcVersion.Two,
44
- id: message.id,
45
- error: {
46
- code: JsonRpcErrorCode.Custom,
47
- message: prettyError.message,
48
- data: {
49
- stack: prettyError.stack,
50
- codeFrame: prettyError.codeFrame,
51
- type: prettyError.type,
52
- code: prettyError.code,
53
- },
54
- },
55
- }
56
- }
@@ -1,14 +0,0 @@
1
- import * as GetErrorResponse from '../GetErrorResponse/GetErrorResponse.js'
2
- import * as GetSuccessResponse from '../GetSuccessResponse/GetSuccessResponse.js'
3
- import * as RequiresSocket from '../RequiresSocket/RequiresSocket.js'
4
-
5
- export const getResponse = async (message, execute, handle) => {
6
- try {
7
- const result = RequiresSocket.requiresSocket(message.method)
8
- ? await execute(message.method, ...message.params, handle)
9
- : await execute(message.method, ...message.params)
10
- return GetSuccessResponse.getSuccessResponse(message, result)
11
- } catch (error) {
12
- return GetErrorResponse.getErrorResponse(message, error)
13
- }
14
- }
@@ -1,12 +0,0 @@
1
- import * as JsonRpcVersion from '../JsonRpcVersion/JsonRpcVersion.js'
2
-
3
- export const getSuccessResponse = (message, result) => {
4
- if (!message.id) {
5
- return undefined
6
- }
7
- return {
8
- jsonrpc: JsonRpcVersion.Two,
9
- id: message.id,
10
- result: result ?? null,
11
- }
12
- }
@@ -1,12 +0,0 @@
1
- import * as GetResponse from '../GetResponse/GetResponse.js'
2
-
3
- export const handleJsonRpcMessage = async (ipc, message, execute, resolve) => {
4
- if ('result' in message || 'error' in message) {
5
- resolve(message.id, message)
6
- return
7
- }
8
- const response = await GetResponse.getResponse(message, execute, ipc)
9
- if ('id' in message) {
10
- ipc.send(response)
11
- }
12
- }
@@ -1,2 +0,0 @@
1
- export const MethodNotFound = -32601
2
- export const Custom = -32001
@@ -1,9 +0,0 @@
1
- import * as JsonRpcVersion from '../JsonRpcVersion/JsonRpcVersion.js'
2
-
3
- export const create = (method, params) => {
4
- return {
5
- jsonrpc: JsonRpcVersion.Two,
6
- method,
7
- params,
8
- }
9
- }
@@ -1,16 +0,0 @@
1
- import * as Callback from '../Callback/Callback.js'
2
- import * as JsonRpcVersion from '../JsonRpcVersion/JsonRpcVersion.js'
3
-
4
- export const create = (method, params) => {
5
- const { id, promise } = Callback.registerPromise()
6
- const message = {
7
- jsonrpc: JsonRpcVersion.Two,
8
- method,
9
- params,
10
- id,
11
- }
12
- return {
13
- message,
14
- promise,
15
- }
16
- }
@@ -1 +0,0 @@
1
- export const Two = '2.0'