-
-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client, server, core: Add StreamService interface and TerminalService…
… device (#1171) * wip * clean up shell on disconnect * fix null reference * remove debug logs * use async queue in buffered buffer, add max buffer size before connection teardown * Revert "use async queue in buffered buffer, add max buffer size before connection teardown" This reverts commit 1b3c283. * reimplement per feedback * feedback
- Loading branch information
Showing
11 changed files
with
148 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { ScryptedDeviceBase, StreamService } from "@scrypted/sdk"; | ||
import type { IPty, spawn as ptySpawn } from 'node-pty-prebuilt-multiarch'; | ||
import { createAsyncQueue } from '@scrypted/common/src/async-queue' | ||
|
||
export const TerminalServiceNativeId = 'terminalservice'; | ||
|
||
export class TerminalService extends ScryptedDeviceBase implements StreamService { | ||
async connectStream(input: AsyncGenerator<any, void>): Promise<AsyncGenerator<any, void>> { | ||
const spawn = require('node-pty-prebuilt-multiarch').spawn as typeof ptySpawn; | ||
const cp: IPty = spawn(process.env.SHELL as string, [], {}); | ||
const queue = createAsyncQueue<Buffer>(); | ||
cp.onExit(() => queue.end()); | ||
|
||
let bufferedLength = 0; | ||
const MAX_BUFFERED_LENGTH = 64000; | ||
cp.onData(async data => { | ||
const buffer = Buffer.from(data); | ||
bufferedLength += buffer.length; | ||
const promise = queue.enqueue(buffer).then(() => bufferedLength -= buffer.length); | ||
if (bufferedLength >= MAX_BUFFERED_LENGTH) { | ||
cp.pause(); | ||
await promise; | ||
if (bufferedLength < MAX_BUFFERED_LENGTH) | ||
cp.resume(); | ||
} | ||
}); | ||
|
||
async function* generator() { | ||
try { | ||
while (true) { | ||
const buffers = queue.clear(); | ||
if (buffers.length) { | ||
yield Buffer.concat(buffers); | ||
continue; | ||
} | ||
|
||
yield await queue.dequeue(); | ||
} | ||
} | ||
finally { | ||
cp.kill(); | ||
} | ||
} | ||
|
||
(async () => { | ||
try { | ||
for await (const message of input) { | ||
if (!message) { | ||
cp.kill(); | ||
return; | ||
} | ||
if (Buffer.isBuffer(message)) { | ||
cp.write(message.toString()); | ||
continue; | ||
} | ||
try { | ||
const parsed = JSON.parse(message.toString()); | ||
if (parsed.dim) { | ||
cp.resize(parsed.dim.cols, parsed.dim.rows); | ||
} | ||
} catch { | ||
cp.write(message.toString()); | ||
} | ||
} | ||
} | ||
catch (e) { | ||
this.console.log(e); | ||
} | ||
finally { | ||
cp.kill(); | ||
} | ||
})(); | ||
|
||
return generator(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters