@lvce-editor/pty-host 0.0.2
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/LICENSE +21 -0
- package/README.md +9 -0
- package/bin/ptyHost.js +3 -0
- package/index.js +6 -0
- package/package.json +64 -0
- package/src/parts/Assert/Assert.js +57 -0
- package/src/parts/Debug/Debug.js +3 -0
- package/src/parts/Platform/Platform.js +1 -0
- package/src/parts/Pty/Pty.js +48 -0
- package/src/parts/PtyController/PtyController.js +40 -0
- package/src/ptyHostMain.js +26 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-present, Le Vivilet
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# pty-host
|
|
2
|
+
|
|
3
|
+
Like in VSCode, the `pty-host` is NodeJS process that is used for spawning terminal processes (pty's).
|
|
4
|
+
|
|
5
|
+
It is a separate process, because the application should still
|
|
6
|
+
|
|
7
|
+
Even though pty's could be spawned from the shared-process, the application
|
|
8
|
+
will be more error tolerant with a separate pty-host process.
|
|
9
|
+
For example, when node-pty cannot be installed, or when there is a native exception, the application will still work (without terminals).
|
package/bin/ptyHost.js
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lvce-editor/pty-host",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "node --unhandled-rejections=warn --experimental-vm-modules ../../node_modules/jest/bin/jest.js --detectOpenHandles --forceExit",
|
|
9
|
+
"test:watch": "node --unhandled-rejections=warn --experimental-vm-modules ../../node_modules/jest/bin/jest.js --watch"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"debug": "^4.3.4",
|
|
16
|
+
"verror": "^1.10.1"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/verror": "^1.10.5"
|
|
20
|
+
},
|
|
21
|
+
"optionalDependencies": {
|
|
22
|
+
"node-pty": "^0.10.1"
|
|
23
|
+
},
|
|
24
|
+
"xo": {
|
|
25
|
+
"rules": {
|
|
26
|
+
"unicorn/filename-case": "off",
|
|
27
|
+
"indent": "off",
|
|
28
|
+
"semi": "off",
|
|
29
|
+
"no-unused-vars": "off",
|
|
30
|
+
"unicorn/numeric-separators-style": "off",
|
|
31
|
+
"no-extra-semi": "off",
|
|
32
|
+
"arrow-body-style": "off",
|
|
33
|
+
"padded-blocks": "off",
|
|
34
|
+
"capitalized-comments": "off",
|
|
35
|
+
"padding-line-between-statements": "off",
|
|
36
|
+
"arrow-parens": "off",
|
|
37
|
+
"no-warning-comments": "off",
|
|
38
|
+
"array-bracket-spacing": "off",
|
|
39
|
+
"comma-spacing": "off",
|
|
40
|
+
"unicorn/no-array-callback-reference": "off",
|
|
41
|
+
"comma-dangle": "off",
|
|
42
|
+
"operator-linebreak": "off",
|
|
43
|
+
"no-case-declarations": "off",
|
|
44
|
+
"no-undef": "off",
|
|
45
|
+
"object-curly-spacing": "off",
|
|
46
|
+
"object-shorthand": "off",
|
|
47
|
+
"complexity": "off",
|
|
48
|
+
"no-labels": "off",
|
|
49
|
+
"no-multi-assign": "off",
|
|
50
|
+
"max-params": "off",
|
|
51
|
+
"no-bitwise": "off",
|
|
52
|
+
"unicorn/prefer-math-trunc": "off",
|
|
53
|
+
"no-await-in-loop": "off",
|
|
54
|
+
"unicorn/prefer-add-event-listener": "off",
|
|
55
|
+
"no-unused-expressions": "off",
|
|
56
|
+
"node/prefer-global/process": "off",
|
|
57
|
+
"unicorn/no-for-loop": "off",
|
|
58
|
+
"unicorn/prevent-abbreviations": "off"
|
|
59
|
+
},
|
|
60
|
+
"ignores": [
|
|
61
|
+
"distmin"
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const getType = (value) => {
|
|
2
|
+
switch (typeof value) {
|
|
3
|
+
case 'number':
|
|
4
|
+
return 'number'
|
|
5
|
+
case 'function':
|
|
6
|
+
return 'function'
|
|
7
|
+
case 'string':
|
|
8
|
+
return 'string'
|
|
9
|
+
case 'object':
|
|
10
|
+
if (value === null) {
|
|
11
|
+
return 'null'
|
|
12
|
+
}
|
|
13
|
+
if (Array.isArray(value)) {
|
|
14
|
+
return 'array'
|
|
15
|
+
}
|
|
16
|
+
return 'object'
|
|
17
|
+
case 'boolean':
|
|
18
|
+
return 'boolean'
|
|
19
|
+
default:
|
|
20
|
+
return 'unknown'
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const object = (value) => {
|
|
25
|
+
const type = getType(value)
|
|
26
|
+
if (type !== 'object') {
|
|
27
|
+
throw new Error('expected value to be of type object')
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const number = (value) => {
|
|
32
|
+
const type = getType(value)
|
|
33
|
+
if (type !== 'number') {
|
|
34
|
+
throw new Error('expected value to be of type number')
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const array = (value) => {
|
|
39
|
+
const type = getType(value)
|
|
40
|
+
if (type !== 'array') {
|
|
41
|
+
throw new Error('expected value to be of type array')
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const string = (value) => {
|
|
46
|
+
const type = getType(value)
|
|
47
|
+
if (type !== 'string') {
|
|
48
|
+
throw new Error('expected value to be of type string')
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const boolean = (value) => {
|
|
53
|
+
const type = getType(value)
|
|
54
|
+
if (type !== 'boolean') {
|
|
55
|
+
throw new Error('expected value to be of type boolean')
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const isWindows = process.platform === 'win32'
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { spawn } from 'node-pty'
|
|
2
|
+
import * as Platform from '../Platform/Platform.js'
|
|
3
|
+
import * as Assert from '../Assert/Assert.js'
|
|
4
|
+
|
|
5
|
+
const getSpawnOptions = () => {
|
|
6
|
+
if (Platform.isWindows) {
|
|
7
|
+
return {
|
|
8
|
+
command: 'powershell.exe',
|
|
9
|
+
args: [],
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
command: 'bash',
|
|
14
|
+
args: ['-i'],
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const create = ({ env = {}, cwd } = {}) => {
|
|
19
|
+
const spawnOptions = getSpawnOptions()
|
|
20
|
+
const pty = spawn(spawnOptions.command, spawnOptions.args, {
|
|
21
|
+
encoding: null,
|
|
22
|
+
cwd,
|
|
23
|
+
// cols: 10,
|
|
24
|
+
// rows: 10,
|
|
25
|
+
})
|
|
26
|
+
return pty
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const onData = (pty, fn) => {
|
|
30
|
+
Assert.object(pty)
|
|
31
|
+
pty.onData(fn)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const write = (pty, data) => {
|
|
35
|
+
Assert.object(pty)
|
|
36
|
+
pty.write(data)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const resize = (pty, columns, rows) => {
|
|
40
|
+
Assert.object(pty)
|
|
41
|
+
Assert.number(columns)
|
|
42
|
+
Assert.number(rows)
|
|
43
|
+
pty.resize(columns, rows)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const dispose = (pty) => {
|
|
47
|
+
pty.kill()
|
|
48
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as Pty from '../Pty/Pty.js'
|
|
2
|
+
import * as Debug from '../Debug/Debug.js'
|
|
3
|
+
|
|
4
|
+
export const state = {
|
|
5
|
+
ptyMap: Object.create(null),
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// TODO maybe merge pty and pty controller
|
|
9
|
+
export const create = (id, cwd) => {
|
|
10
|
+
Debug.debug(`create ${id} ${cwd}`)
|
|
11
|
+
const pty = Pty.create({ cwd })
|
|
12
|
+
const handleData = (data) => {
|
|
13
|
+
if (process.send) {
|
|
14
|
+
process.send({
|
|
15
|
+
jsonrpc: '2.0',
|
|
16
|
+
method: 'Terminal.handleData',
|
|
17
|
+
params: [id, data],
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
Pty.onData(pty, handleData)
|
|
22
|
+
state.ptyMap[id] = pty
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const write = (id, data) => {
|
|
26
|
+
const pty = state.ptyMap[id]
|
|
27
|
+
Pty.write(pty, data)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const dispose = (id) => {
|
|
31
|
+
const pty = state.ptyMap[id]
|
|
32
|
+
Pty.dispose(pty)
|
|
33
|
+
delete state.ptyMap[id]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const disposeAll = () => {
|
|
37
|
+
for (const id in state.ptyMap) {
|
|
38
|
+
dispose(id)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as PtyController from './parts/PtyController/PtyController.js'
|
|
2
|
+
import * as Debug from './parts/Debug/Debug.js'
|
|
3
|
+
|
|
4
|
+
const handleMessage = (message) => {
|
|
5
|
+
switch (message.method) {
|
|
6
|
+
case 'Terminal.create':
|
|
7
|
+
Debug.debug('create terminal')
|
|
8
|
+
PtyController.create(...message.params)
|
|
9
|
+
break
|
|
10
|
+
case 'Terminal.write':
|
|
11
|
+
PtyController.write(...message.params)
|
|
12
|
+
break
|
|
13
|
+
default:
|
|
14
|
+
console.warn('unknown message', message)
|
|
15
|
+
break
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const main = async () => {
|
|
20
|
+
process.on('message', handleMessage)
|
|
21
|
+
if (process.send) {
|
|
22
|
+
process.send('ready')
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
main()
|