Repository navigation
feat(cli): stream terminal input over a single StreamInput RPC #2005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| '@e2b/cli': patch | ||
| --- | ||
|
|
||
| `e2b sandbox connect`/`create` send terminal input over a single persistent stream instead of one request per 10ms batch |
This file contains hidden or 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 hidden or 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,186 @@ | ||
| import type { Sandbox } from 'e2b' | ||
| import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' | ||
|
|
||
| import { spawnConnectedTerminal } from '../src/terminal' | ||
|
|
||
| function createDeferred() { | ||
| let resolve!: () => void | ||
| const promise = new Promise<void>((done) => { | ||
| resolve = done | ||
| }) | ||
| return { promise, resolve } | ||
| } | ||
|
|
||
| function createInputHandle( | ||
| sendImplementation: (data: Buffer) => Promise<void> = async () => {} | ||
| ) { | ||
| return { | ||
| send: vi.fn<(data: Buffer) => Promise<void>>(async (data: Buffer) => | ||
| sendImplementation(data) | ||
| ), | ||
| close: vi.fn(async () => {}), | ||
| } | ||
| } | ||
|
|
||
| function sentChunks(handle: ReturnType<typeof createInputHandle>) { | ||
| const calls = handle.send.mock.calls as [Buffer][] | ||
| return calls.map(([data]) => data.toString()) | ||
| } | ||
|
|
||
| function createSandbox(handles: ReturnType<typeof createInputHandle>[]) { | ||
| const wait = createDeferred() | ||
| let nextHandle = 0 | ||
| const streamInput = vi.fn( | ||
| () => handles[nextHandle++] ?? handles[handles.length - 1] | ||
| ) | ||
| const sandbox = { | ||
| pty: { | ||
| create: vi.fn().mockResolvedValue({ | ||
| pid: 1, | ||
| wait: () => wait.promise, | ||
| }), | ||
| resize: vi.fn().mockResolvedValue(undefined), | ||
| streamInput, | ||
| }, | ||
| } as unknown as Sandbox | ||
|
|
||
| return { sandbox, streamInput, wait } | ||
| } | ||
|
|
||
| const flush = () => new Promise<void>((resolve) => setImmediate(resolve)) | ||
| const originalRawModeDescriptor = Object.getOwnPropertyDescriptor( | ||
| process.stdin, | ||
| 'setRawMode' | ||
| ) | ||
| let setRawModeMock = vi.fn() | ||
|
|
||
| beforeEach(() => { | ||
| setRawModeMock = vi.fn() | ||
| Object.defineProperty(process.stdin, 'setRawMode', { | ||
| configurable: true, | ||
| writable: true, | ||
| value: setRawModeMock, | ||
| }) | ||
| vi.spyOn(process.stdout, 'write').mockImplementation((() => true) as never) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| if (originalRawModeDescriptor) { | ||
| Object.defineProperty( | ||
| process.stdin, | ||
| 'setRawMode', | ||
| originalRawModeDescriptor | ||
| ) | ||
| } else { | ||
| Reflect.deleteProperty(process.stdin, 'setRawMode') | ||
| } | ||
| }) | ||
|
|
||
| describe('spawnConnectedTerminal input stream', () => { | ||
| test('sends chunks in order over one persistent stream', async () => { | ||
| const handle = createInputHandle() | ||
| const { sandbox, streamInput, wait } = createSandbox([handle]) | ||
| const terminal = spawnConnectedTerminal(sandbox) | ||
|
|
||
| try { | ||
| await flush() | ||
| process.stdin.emit('data', Buffer.from('first')) | ||
| process.stdin.emit('data', Buffer.from('second')) | ||
| await flush() | ||
|
|
||
| expect(streamInput).toHaveBeenCalledTimes(1) | ||
| expect(streamInput).toHaveBeenCalledWith(1, { timeoutMs: 0 }) | ||
| expect(sentChunks(handle)).toEqual(['first', 'second']) | ||
| } finally { | ||
| wait.resolve() | ||
| await terminal | ||
| } | ||
| }) | ||
|
|
||
| test('reopens once per failed handle and resends chunks in order', async () => { | ||
| const first = createInputHandle(async () => { | ||
| throw new Error('first stream failed') | ||
| }) | ||
| const replacement = createInputHandle() | ||
| const { sandbox, streamInput, wait } = createSandbox([first, replacement]) | ||
| const terminal = spawnConnectedTerminal(sandbox) | ||
|
|
||
| try { | ||
| await flush() | ||
| process.stdin.emit('data', Buffer.from('first')) | ||
| process.stdin.emit('data', Buffer.from('second')) | ||
| await flush() | ||
|
|
||
| expect(streamInput).toHaveBeenCalledTimes(2) | ||
| expect(streamInput).toHaveBeenNthCalledWith(1, 1, { timeoutMs: 0 }) | ||
| expect(streamInput).toHaveBeenNthCalledWith(2, 1, { timeoutMs: 0 }) | ||
| expect(sentChunks(replacement)).toEqual(['first', 'second']) | ||
|
|
||
| process.stdin.emit('data', Buffer.from('later')) | ||
| await flush() | ||
|
|
||
| expect(sentChunks(replacement)).toEqual(['first', 'second', 'later']) | ||
| } finally { | ||
| wait.resolve() | ||
| await terminal | ||
| } | ||
| }) | ||
|
|
||
| test('reports repeated resend failures only once', async () => { | ||
| const first = createInputHandle(async () => { | ||
| throw new Error('first stream failed') | ||
| }) | ||
| const replacement = createInputHandle(async () => { | ||
| throw new Error('replacement stream failed') | ||
| }) | ||
| const finalHandle = createInputHandle() | ||
| const { sandbox, streamInput, wait } = createSandbox([ | ||
| first, | ||
| replacement, | ||
| finalHandle, | ||
| ]) | ||
| const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}) | ||
| const terminal = spawnConnectedTerminal(sandbox) | ||
|
|
||
| try { | ||
| await flush() | ||
| process.stdin.emit('data', Buffer.from('first')) | ||
| process.stdin.emit('data', Buffer.from('second')) | ||
| await flush() | ||
|
|
||
| expect(streamInput).toHaveBeenCalledTimes(3) | ||
| expect(sentChunks(replacement)).toEqual(['first', 'second']) | ||
| expect(consoleError).toHaveBeenCalledTimes(1) | ||
| expect(consoleError).toHaveBeenCalledWith( | ||
| 'Error sending input:', | ||
| expect.any(Error) | ||
| ) | ||
| } finally { | ||
| wait.resolve() | ||
| await terminal | ||
| } | ||
| }) | ||
|
|
||
| test('cleans up listeners, closes the input stream, and disables raw mode', async () => { | ||
| const handle = createInputHandle() | ||
| const { sandbox, wait } = createSandbox([handle]) | ||
| const stdinListeners = process.stdin.listenerCount('data') | ||
| const resizeListeners = process.stdout.listenerCount('resize') | ||
| const terminal = spawnConnectedTerminal(sandbox) | ||
|
|
||
| try { | ||
| await flush() | ||
| expect(process.stdin.listenerCount('data')).toBe(stdinListeners + 1) | ||
| expect(process.stdout.listenerCount('resize')).toBe(resizeListeners + 1) | ||
| } finally { | ||
| wait.resolve() | ||
| await terminal | ||
| } | ||
|
|
||
| expect(process.stdin.listenerCount('data')).toBe(stdinListeners) | ||
| expect(process.stdout.listenerCount('resize')).toBe(resizeListeners) | ||
| expect(handle.close).toHaveBeenCalledOnce() | ||
| expect(setRawModeMock).toHaveBeenCalledWith(false) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.