x-shell.js 0.1.0 → 0.1.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 x-shell.js contributors
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 all
13
+ 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 THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,304 +1,304 @@
1
- # x-shell.js
2
-
3
- > WebSocket-based terminal for Node.js - the truth is in your shell
4
-
5
- A plug-and-play terminal solution for web applications. Includes a server component (node-pty), client library, and ready-to-use Lit web component.
6
-
7
- ## Features
8
-
9
- - **Server**: WebSocket server with node-pty for real shell sessions
10
- - **Client**: Lightweight WebSocket client with auto-reconnection
11
- - **UI**: `<x-shell-terminal>` Lit web component with xterm.js
12
- - **Themes**: Built-in dark/light/auto theme support
13
- - **Security**: Configurable shell and path allowlists
14
- - **Framework Agnostic**: Works with React, Vue, Angular, Svelte, or vanilla JS
15
-
16
- ## Installation
17
-
18
- ```bash
19
- npm install x-shell.js node-pty
20
- ```
21
-
22
- Note: `node-pty` requires native compilation. See [node-pty docs](https://github.com/microsoft/node-pty) for platform-specific requirements.
23
-
24
- ## Quick Start
25
-
26
- ### Server Setup
27
-
28
- ```javascript
29
- import express from 'express';
30
- import { createServer } from 'http';
31
- import { TerminalServer } from 'x-shell.js/server';
32
-
33
- const app = express();
34
- const server = createServer(app);
35
-
36
- // Create and attach terminal server
37
- const terminalServer = new TerminalServer({
38
- allowedShells: ['/bin/bash', '/bin/zsh'],
39
- allowedPaths: ['/home/user'],
40
- defaultCwd: '/home/user',
41
- verbose: true,
42
- });
43
-
44
- terminalServer.attach(server);
45
-
46
- server.listen(3000, () => {
47
- console.log('Server running on http://localhost:3000');
48
- });
49
- ```
50
-
51
- ### Client Usage (Web Component)
52
-
53
- ```html
54
- <!-- Load xterm.js CSS -->
55
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@5.3.0/css/xterm.css">
56
-
57
- <!-- Load x-shell.js UI bundle -->
58
- <script type="module" src="https://unpkg.com/x-shell.js/dist/ui/browser-bundle.js"></script>
59
-
60
- <!-- Use the component -->
61
- <x-shell-terminal
62
- url="ws://localhost:3000/terminal"
63
- theme="dark"
64
- auto-connect
65
- auto-spawn
66
- ></x-shell-terminal>
67
- ```
68
-
69
- ### Client Usage (JavaScript)
70
-
71
- ```javascript
72
- import { TerminalClient } from 'x-shell.js/client';
73
-
74
- const client = new TerminalClient({
75
- url: 'ws://localhost:3000/terminal'
76
- });
77
-
78
- await client.connect();
79
-
80
- client.onData((data) => {
81
- console.log('Output:', data);
82
- });
83
-
84
- client.onExit((code) => {
85
- console.log('Exited with code:', code);
86
- });
87
-
88
- await client.spawn({
89
- shell: '/bin/bash',
90
- cwd: '/home/user'
91
- });
92
-
93
- client.write('ls -la\n');
94
- client.resize(120, 40);
95
- ```
96
-
97
- ## API Reference
98
-
99
- ### Server
100
-
101
- #### `TerminalServer`
102
-
103
- ```typescript
104
- import { TerminalServer } from 'x-shell.js/server';
105
-
106
- const server = new TerminalServer({
107
- // Allowed shells (empty = all allowed)
108
- allowedShells: ['/bin/bash', '/bin/zsh', 'cmd.exe'],
109
-
110
- // Allowed working directories (empty = all allowed)
111
- allowedPaths: ['/home/user', '/var/www'],
112
-
113
- // Default shell if not specified
114
- defaultShell: '/bin/bash',
115
-
116
- // Default working directory
117
- defaultCwd: '/home/user',
118
-
119
- // Max sessions per client (default: 5)
120
- maxSessionsPerClient: 5,
121
-
122
- // Idle timeout in ms (default: 30 minutes, 0 = disabled)
123
- idleTimeout: 30 * 60 * 1000,
124
-
125
- // WebSocket path (default: '/terminal')
126
- path: '/terminal',
127
-
128
- // Enable verbose logging
129
- verbose: false,
130
- });
131
-
132
- // Attach to HTTP server
133
- server.attach(httpServer);
134
-
135
- // Or start standalone
136
- server.listen(3001);
137
-
138
- // Get active sessions
139
- const sessions = server.getSessions();
140
-
141
- // Close server
142
- server.close();
143
- ```
144
-
145
- ### Client
146
-
147
- #### `TerminalClient`
148
-
149
- ```typescript
150
- import { TerminalClient } from 'x-shell.js/client';
151
-
152
- const client = new TerminalClient({
153
- url: 'ws://localhost:3000/terminal',
154
- reconnect: true, // Auto-reconnect (default: true)
155
- maxReconnectAttempts: 10, // Max attempts (default: 10)
156
- reconnectDelay: 1000, // Initial delay ms (default: 1000)
157
- });
158
-
159
- // Connect to server
160
- await client.connect();
161
-
162
- // Spawn terminal session
163
- const sessionInfo = await client.spawn({
164
- shell: '/bin/bash',
165
- cwd: '/home/user',
166
- env: { TERM: 'xterm-256color' },
167
- cols: 80,
168
- rows: 24,
169
- });
170
-
171
- // Write to terminal
172
- client.write('echo "Hello World"\n');
173
-
174
- // Resize terminal
175
- client.resize(120, 40);
176
-
177
- // Kill session
178
- client.kill();
179
-
180
- // Disconnect
181
- client.disconnect();
182
-
183
- // Event handlers
184
- client.onConnect(() => console.log('Connected'));
185
- client.onDisconnect(() => console.log('Disconnected'));
186
- client.onData((data) => console.log('Data:', data));
187
- client.onExit((code) => console.log('Exit:', code));
188
- client.onError((err) => console.log('Error:', err));
189
- client.onSpawned((info) => console.log('Spawned:', info));
190
-
191
- // State getters
192
- client.isConnected(); // boolean
193
- client.hasActiveSession(); // boolean
194
- client.getSessionId(); // string | null
195
- client.getSessionInfo(); // SessionInfo | null
196
- ```
197
-
198
- ### UI Component
199
-
200
- #### `<x-shell-terminal>`
201
-
202
- ```html
203
- <x-shell-terminal
204
- url="ws://localhost:3000/terminal"
205
- shell="/bin/bash"
206
- cwd="/home/user"
207
- theme="dark"
208
- font-size="14"
209
- font-family="Menlo, Monaco, monospace"
210
- cols="80"
211
- rows="24"
212
- auto-connect
213
- auto-spawn
214
- no-header
215
- ></x-shell-terminal>
216
- ```
217
-
218
- **Attributes:**
219
-
220
- | Attribute | Type | Default | Description |
221
- |-----------|------|---------|-------------|
222
- | `url` | string | `''` | WebSocket URL |
223
- | `shell` | string | `''` | Shell to use |
224
- | `cwd` | string | `''` | Working directory |
225
- | `theme` | `'dark'` \| `'light'` \| `'auto'` | `'dark'` | Color theme |
226
- | `font-size` | number | `14` | Terminal font size |
227
- | `font-family` | string | `'Menlo, Monaco, ...'` | Terminal font |
228
- | `cols` | number | `80` | Initial columns |
229
- | `rows` | number | `24` | Initial rows |
230
- | `auto-connect` | boolean | `false` | Connect on mount |
231
- | `auto-spawn` | boolean | `false` | Spawn on connect |
232
- | `no-header` | boolean | `false` | Hide header bar |
233
-
234
- **Methods:**
235
-
236
- ```javascript
237
- const terminal = document.querySelector('x-shell-terminal');
238
-
239
- await terminal.connect(); // Connect to server
240
- terminal.disconnect(); // Disconnect
241
- await terminal.spawn(); // Spawn session
242
- terminal.kill(); // Kill session
243
- terminal.clear(); // Clear display
244
- terminal.write('text'); // Write to display
245
- terminal.writeln('line'); // Write line to display
246
- terminal.focus(); // Focus terminal
247
- ```
248
-
249
- **Events:**
250
-
251
- ```javascript
252
- terminal.addEventListener('connect', () => {});
253
- terminal.addEventListener('disconnect', () => {});
254
- terminal.addEventListener('spawned', (e) => console.log(e.detail.session));
255
- terminal.addEventListener('exit', (e) => console.log(e.detail.exitCode));
256
- terminal.addEventListener('error', (e) => console.log(e.detail.error));
257
- ```
258
-
259
- ## Theming
260
-
261
- The component uses CSS custom properties for theming:
262
-
263
- ```css
264
- x-shell-terminal {
265
- --xs-bg: #1e1e1e;
266
- --xs-bg-header: #2d2d2d;
267
- --xs-text: #cccccc;
268
- --xs-text-muted: #808080;
269
- --xs-border: #3e3e3e;
270
- --xs-terminal-bg: #1e1e1e;
271
- --xs-terminal-fg: #cccccc;
272
- --xs-terminal-cursor: #ffffff;
273
- --xs-terminal-selection: #264f78;
274
- --xs-btn-bg: #3c3c3c;
275
- --xs-btn-text: #cccccc;
276
- --xs-btn-hover: #4a4a4a;
277
- --xs-status-connected: #22c55e;
278
- --xs-status-disconnected: #ef4444;
279
- }
280
- ```
281
-
282
- ## Security
283
-
284
- **Always configure security for production:**
285
-
286
- ```javascript
287
- const server = new TerminalServer({
288
- // Restrict allowed shells
289
- allowedShells: ['/bin/bash'],
290
-
291
- // Restrict working directories
292
- allowedPaths: ['/home/app', '/var/www'],
293
-
294
- // Limit sessions per client
295
- maxSessionsPerClient: 2,
296
-
297
- // Set idle timeout
298
- idleTimeout: 10 * 60 * 1000, // 10 minutes
299
- });
300
- ```
301
-
302
- ## License
303
-
304
- MIT
1
+ # x-shell.js
2
+
3
+ > WebSocket-based terminal for Node.js - the truth is in your shell
4
+
5
+ A plug-and-play terminal solution for web applications. Includes a server component (node-pty), client library, and ready-to-use Lit web component.
6
+
7
+ ## Features
8
+
9
+ - **Server**: WebSocket server with node-pty for real shell sessions
10
+ - **Client**: Lightweight WebSocket client with auto-reconnection
11
+ - **UI**: `<x-shell-terminal>` Lit web component with xterm.js
12
+ - **Themes**: Built-in dark/light/auto theme support
13
+ - **Security**: Configurable shell and path allowlists
14
+ - **Framework Agnostic**: Works with React, Vue, Angular, Svelte, or vanilla JS
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install x-shell.js node-pty
20
+ ```
21
+
22
+ Note: `node-pty` requires native compilation. See [node-pty docs](https://github.com/microsoft/node-pty) for platform-specific requirements.
23
+
24
+ ## Quick Start
25
+
26
+ ### Server Setup
27
+
28
+ ```javascript
29
+ import express from 'express';
30
+ import { createServer } from 'http';
31
+ import { TerminalServer } from 'x-shell.js/server';
32
+
33
+ const app = express();
34
+ const server = createServer(app);
35
+
36
+ // Create and attach terminal server
37
+ const terminalServer = new TerminalServer({
38
+ allowedShells: ['/bin/bash', '/bin/zsh'],
39
+ allowedPaths: ['/home/user'],
40
+ defaultCwd: '/home/user',
41
+ verbose: true,
42
+ });
43
+
44
+ terminalServer.attach(server);
45
+
46
+ server.listen(3000, () => {
47
+ console.log('Server running on http://localhost:3000');
48
+ });
49
+ ```
50
+
51
+ ### Client Usage (Web Component)
52
+
53
+ ```html
54
+ <!-- Load xterm.js CSS -->
55
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@5.3.0/css/xterm.css">
56
+
57
+ <!-- Load x-shell.js UI bundle -->
58
+ <script type="module" src="https://unpkg.com/x-shell.js/dist/ui/browser-bundle.js"></script>
59
+
60
+ <!-- Use the component -->
61
+ <x-shell-terminal
62
+ url="ws://localhost:3000/terminal"
63
+ theme="dark"
64
+ auto-connect
65
+ auto-spawn
66
+ ></x-shell-terminal>
67
+ ```
68
+
69
+ ### Client Usage (JavaScript)
70
+
71
+ ```javascript
72
+ import { TerminalClient } from 'x-shell.js/client';
73
+
74
+ const client = new TerminalClient({
75
+ url: 'ws://localhost:3000/terminal'
76
+ });
77
+
78
+ await client.connect();
79
+
80
+ client.onData((data) => {
81
+ console.log('Output:', data);
82
+ });
83
+
84
+ client.onExit((code) => {
85
+ console.log('Exited with code:', code);
86
+ });
87
+
88
+ await client.spawn({
89
+ shell: '/bin/bash',
90
+ cwd: '/home/user'
91
+ });
92
+
93
+ client.write('ls -la\n');
94
+ client.resize(120, 40);
95
+ ```
96
+
97
+ ## API Reference
98
+
99
+ ### Server
100
+
101
+ #### `TerminalServer`
102
+
103
+ ```typescript
104
+ import { TerminalServer } from 'x-shell.js/server';
105
+
106
+ const server = new TerminalServer({
107
+ // Allowed shells (empty = all allowed)
108
+ allowedShells: ['/bin/bash', '/bin/zsh', 'cmd.exe'],
109
+
110
+ // Allowed working directories (empty = all allowed)
111
+ allowedPaths: ['/home/user', '/var/www'],
112
+
113
+ // Default shell if not specified
114
+ defaultShell: '/bin/bash',
115
+
116
+ // Default working directory
117
+ defaultCwd: '/home/user',
118
+
119
+ // Max sessions per client (default: 5)
120
+ maxSessionsPerClient: 5,
121
+
122
+ // Idle timeout in ms (default: 30 minutes, 0 = disabled)
123
+ idleTimeout: 30 * 60 * 1000,
124
+
125
+ // WebSocket path (default: '/terminal')
126
+ path: '/terminal',
127
+
128
+ // Enable verbose logging
129
+ verbose: false,
130
+ });
131
+
132
+ // Attach to HTTP server
133
+ server.attach(httpServer);
134
+
135
+ // Or start standalone
136
+ server.listen(3001);
137
+
138
+ // Get active sessions
139
+ const sessions = server.getSessions();
140
+
141
+ // Close server
142
+ server.close();
143
+ ```
144
+
145
+ ### Client
146
+
147
+ #### `TerminalClient`
148
+
149
+ ```typescript
150
+ import { TerminalClient } from 'x-shell.js/client';
151
+
152
+ const client = new TerminalClient({
153
+ url: 'ws://localhost:3000/terminal',
154
+ reconnect: true, // Auto-reconnect (default: true)
155
+ maxReconnectAttempts: 10, // Max attempts (default: 10)
156
+ reconnectDelay: 1000, // Initial delay ms (default: 1000)
157
+ });
158
+
159
+ // Connect to server
160
+ await client.connect();
161
+
162
+ // Spawn terminal session
163
+ const sessionInfo = await client.spawn({
164
+ shell: '/bin/bash',
165
+ cwd: '/home/user',
166
+ env: { TERM: 'xterm-256color' },
167
+ cols: 80,
168
+ rows: 24,
169
+ });
170
+
171
+ // Write to terminal
172
+ client.write('echo "Hello World"\n');
173
+
174
+ // Resize terminal
175
+ client.resize(120, 40);
176
+
177
+ // Kill session
178
+ client.kill();
179
+
180
+ // Disconnect
181
+ client.disconnect();
182
+
183
+ // Event handlers
184
+ client.onConnect(() => console.log('Connected'));
185
+ client.onDisconnect(() => console.log('Disconnected'));
186
+ client.onData((data) => console.log('Data:', data));
187
+ client.onExit((code) => console.log('Exit:', code));
188
+ client.onError((err) => console.log('Error:', err));
189
+ client.onSpawned((info) => console.log('Spawned:', info));
190
+
191
+ // State getters
192
+ client.isConnected(); // boolean
193
+ client.hasActiveSession(); // boolean
194
+ client.getSessionId(); // string | null
195
+ client.getSessionInfo(); // SessionInfo | null
196
+ ```
197
+
198
+ ### UI Component
199
+
200
+ #### `<x-shell-terminal>`
201
+
202
+ ```html
203
+ <x-shell-terminal
204
+ url="ws://localhost:3000/terminal"
205
+ shell="/bin/bash"
206
+ cwd="/home/user"
207
+ theme="dark"
208
+ font-size="14"
209
+ font-family="Menlo, Monaco, monospace"
210
+ cols="80"
211
+ rows="24"
212
+ auto-connect
213
+ auto-spawn
214
+ no-header
215
+ ></x-shell-terminal>
216
+ ```
217
+
218
+ **Attributes:**
219
+
220
+ | Attribute | Type | Default | Description |
221
+ |-----------|------|---------|-------------|
222
+ | `url` | string | `''` | WebSocket URL |
223
+ | `shell` | string | `''` | Shell to use |
224
+ | `cwd` | string | `''` | Working directory |
225
+ | `theme` | `'dark'` \| `'light'` \| `'auto'` | `'dark'` | Color theme |
226
+ | `font-size` | number | `14` | Terminal font size |
227
+ | `font-family` | string | `'Menlo, Monaco, ...'` | Terminal font |
228
+ | `cols` | number | `80` | Initial columns |
229
+ | `rows` | number | `24` | Initial rows |
230
+ | `auto-connect` | boolean | `false` | Connect on mount |
231
+ | `auto-spawn` | boolean | `false` | Spawn on connect |
232
+ | `no-header` | boolean | `false` | Hide header bar |
233
+
234
+ **Methods:**
235
+
236
+ ```javascript
237
+ const terminal = document.querySelector('x-shell-terminal');
238
+
239
+ await terminal.connect(); // Connect to server
240
+ terminal.disconnect(); // Disconnect
241
+ await terminal.spawn(); // Spawn session
242
+ terminal.kill(); // Kill session
243
+ terminal.clear(); // Clear display
244
+ terminal.write('text'); // Write to display
245
+ terminal.writeln('line'); // Write line to display
246
+ terminal.focus(); // Focus terminal
247
+ ```
248
+
249
+ **Events:**
250
+
251
+ ```javascript
252
+ terminal.addEventListener('connect', () => {});
253
+ terminal.addEventListener('disconnect', () => {});
254
+ terminal.addEventListener('spawned', (e) => console.log(e.detail.session));
255
+ terminal.addEventListener('exit', (e) => console.log(e.detail.exitCode));
256
+ terminal.addEventListener('error', (e) => console.log(e.detail.error));
257
+ ```
258
+
259
+ ## Theming
260
+
261
+ The component uses CSS custom properties for theming:
262
+
263
+ ```css
264
+ x-shell-terminal {
265
+ --xs-bg: #1e1e1e;
266
+ --xs-bg-header: #2d2d2d;
267
+ --xs-text: #cccccc;
268
+ --xs-text-muted: #808080;
269
+ --xs-border: #3e3e3e;
270
+ --xs-terminal-bg: #1e1e1e;
271
+ --xs-terminal-fg: #cccccc;
272
+ --xs-terminal-cursor: #ffffff;
273
+ --xs-terminal-selection: #264f78;
274
+ --xs-btn-bg: #3c3c3c;
275
+ --xs-btn-text: #cccccc;
276
+ --xs-btn-hover: #4a4a4a;
277
+ --xs-status-connected: #22c55e;
278
+ --xs-status-disconnected: #ef4444;
279
+ }
280
+ ```
281
+
282
+ ## Security
283
+
284
+ **Always configure security for production:**
285
+
286
+ ```javascript
287
+ const server = new TerminalServer({
288
+ // Restrict allowed shells
289
+ allowedShells: ['/bin/bash'],
290
+
291
+ // Restrict working directories
292
+ allowedPaths: ['/home/app', '/var/www'],
293
+
294
+ // Limit sessions per client
295
+ maxSessionsPerClient: 2,
296
+
297
+ // Set idle timeout
298
+ idleTimeout: 10 * 60 * 1000, // 10 minutes
299
+ });
300
+ ```
301
+
302
+ ## License
303
+
304
+ MIT