@lvce-editor/pty-host 1.0.1 → 1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/pty-host",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
5
  "main": "src/ptyHostMain.js",
6
6
  "bin": "bin/ptyHost.js",
@@ -1,46 +1,68 @@
1
- import { spawn } from 'node-pty'
2
- import { VError } from '../VError/VError.js'
3
- import * as Assert from '../Assert/Assert.js'
1
+ import { spawn } from "node-pty";
2
+ import { VError } from "../VError/VError.js";
3
+ import * as Assert from "../Assert/Assert.js";
4
+
5
+ class DataEvent extends Event {
6
+ constructor(data) {
7
+ super("data");
8
+ this.data = data;
9
+ }
10
+ }
11
+
12
+ class ExitEvent extends Event {
13
+ constructor(data) {
14
+ super("exit");
15
+ this.data = data;
16
+ }
17
+ }
18
+
19
+ class Pty extends EventTarget {
20
+ /**
21
+ *
22
+ * @param {import('node-pty').IPty} pty
23
+ */
24
+ constructor(pty) {
25
+ super();
26
+ this.pty = pty;
27
+
28
+ const handleData = (data) => {
29
+ this.dispatchEvent(new DataEvent(data));
30
+ };
31
+ const handleExit = (data) => {
32
+ this.dispatchEvent(new ExitEvent(data));
33
+ };
34
+ this.pty.onData(handleData);
35
+ this.pty.onExit(handleExit);
36
+ }
37
+
38
+ resize(columns, rows) {
39
+ this.pty.resize(columns, rows);
40
+ }
41
+
42
+ dispose() {
43
+ this.pty.kill();
44
+ }
45
+
46
+ write(data) {
47
+ this.pty.write(data);
48
+ }
49
+ }
4
50
 
5
51
  // @ts-ignore
6
52
  export const create = ({ env = {}, cwd, command, args } = {}) => {
7
53
  try {
8
- Assert.string(cwd)
9
- Assert.string(command)
10
- Assert.array(args)
54
+ Assert.string(cwd);
55
+ Assert.string(command);
56
+ Assert.array(args);
11
57
  const pty = spawn(command, args, {
12
58
  encoding: null,
13
59
  cwd,
14
60
  // cols: 10,
15
61
  // rows: 10,
16
- })
17
- return pty
18
- } catch (error) {
19
- throw new VError(error, `Failed to create terminal`)
20
- }
21
- }
22
-
23
- export const onData = (pty, fn) => {
24
- Assert.object(pty)
25
- pty.onData(fn)
26
- }
27
-
28
- export const write = (pty, data) => {
29
- try {
30
- Assert.object(pty)
31
- pty.write(data)
62
+ });
63
+ const wrapped = new Pty(pty);
64
+ return wrapped;
32
65
  } catch (error) {
33
- throw new VError(error, `Failed to write data to terminal`)
66
+ throw new VError(error, `Failed to create terminal`);
34
67
  }
35
- }
36
-
37
- export const resize = (pty, columns, rows) => {
38
- Assert.object(pty)
39
- Assert.number(columns)
40
- Assert.number(rows)
41
- pty.resize(columns, rows)
42
- }
43
-
44
- export const dispose = (pty) => {
45
- pty.kill()
46
- }
68
+ };
@@ -1,55 +1,56 @@
1
- import * as Assert from '../Assert/Assert.js'
2
- import * as Debug from '../Debug/Debug.js'
3
- import * as Pty from '../Pty/Pty.js'
4
- import * as PtyState from '../PtyState/PtyState.js'
1
+ import * as Assert from "../Assert/Assert.js";
2
+ import * as Debug from "../Debug/Debug.js";
3
+ import * as Pty from "../Pty/Pty.js";
4
+ import * as PtyState from "../PtyState/PtyState.js";
5
5
 
6
6
  // TODO maybe merge pty and pty controller
7
7
  export const create = (ipc, id, cwd, command, args) => {
8
- Assert.number(id)
9
- Assert.string(cwd)
10
- Assert.string(command)
11
- Assert.array(args)
12
- Assert.object(ipc)
13
- Debug.debug(`create ${id} ${cwd}`)
14
- const pty = Pty.create({ cwd, command, args })
15
- const handleData = (data) => {
8
+ Assert.number(id);
9
+ Assert.string(cwd);
10
+ Assert.string(command);
11
+ Assert.array(args);
12
+ Assert.object(ipc);
13
+ Debug.debug(`create ${id} ${cwd}`);
14
+ const pty = Pty.create({ cwd, command, args });
15
+ const handleData = (event) => {
16
16
  ipc.send({
17
- jsonrpc: '2.0',
18
- method: 'Viewlet.send',
19
- params: [id, 'handleData', data],
20
- })
21
- }
22
- Pty.onData(pty, handleData)
23
- PtyState.set(id, pty)
24
- }
17
+ jsonrpc: "2.0",
18
+ method: "Viewlet.send",
19
+ params: [id, "handleData", event.data],
20
+ });
21
+ };
22
+
23
+ pty.addEventListener("data", handleData);
24
+ PtyState.set(id, pty);
25
+ };
25
26
 
26
27
  export const write = (id, data) => {
27
- const pty = PtyState.get(id)
28
+ const pty = PtyState.get(id);
28
29
  if (!pty) {
29
- throw new Error(`pty ${id} not found`)
30
+ throw new Error(`pty ${id} not found`);
30
31
  }
31
- Pty.write(pty, data)
32
- }
32
+ pty.write(data);
33
+ };
33
34
 
34
35
  export const resize = (id, columns, rows) => {
35
- const pty = PtyState.get(id)
36
+ const pty = PtyState.get(id);
36
37
  if (!pty) {
37
- throw new Error(`pty ${id} not found`)
38
+ throw new Error(`pty ${id} not found`);
38
39
  }
39
- Pty.resize(pty, columns, rows)
40
- }
40
+ pty.resize(columns, rows);
41
+ };
41
42
 
42
43
  export const dispose = (id) => {
43
- const pty = PtyState.get(id)
44
+ const pty = PtyState.get(id);
44
45
  if (!pty) {
45
- throw new Error(`pty ${id} not found`)
46
+ throw new Error(`pty ${id} not found`);
46
47
  }
47
- Pty.dispose(pty)
48
- PtyState.remove(id)
49
- }
48
+ pty.dispose();
49
+ PtyState.remove(id);
50
+ };
50
51
 
51
52
  export const disposeAll = () => {
52
53
  for (const id in PtyState.getAll()) {
53
- dispose(id)
54
+ dispose(id);
54
55
  }
55
- }
56
+ };