-
Notifications
You must be signed in to change notification settings - Fork 45
feat: lazy load node-pty only when usePty: true #511
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
Open
caarlos0
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
caarlos0:caarlos0/lazy-nodepty
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7be16c3
feat: lazy load node-pty only when usePty: true
caarlos0 39c2dea
docs: update
caarlos0 a9f73b2
fix(sdk): address review feedback on lazy node-pty loading
caarlos0 5b2b8b1
fix(sdk): keep node-pty types self-contained and default usePty to false
caarlos0 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,43 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { createRequire } from 'node:module'; | ||
| import type { NodePty } from './pty-types.js'; | ||
|
|
||
| const require = createRequire(import.meta.url); | ||
|
|
||
| let cached: NodePty | undefined; | ||
|
|
||
| /** | ||
| * Lazily loads the `node-pty` native addon. | ||
| * | ||
| * `node-pty` loads its native binary during module evaluation, so a top-level | ||
| * `import` would force every consumer of the SDK to ship and load the addon β | ||
| * even those that only ever spawn in pipe mode (`usePty: false`). Deferring the | ||
| * require keeps the `usePty: false` path from ever touching `node-pty`. | ||
| * | ||
| * The return type is the vendored {@link NodePty} shape rather than node-pty's | ||
| * own module type so the generated `.d.ts` stays self-contained and consumers | ||
| * without the optional peer dependency can still type-check. | ||
| * | ||
| * Uses `createRequire` because `node-pty` is CommonJS and the call sites are | ||
| * synchronous. | ||
| */ | ||
| export function loadPty(): NodePty { | ||
| if (cached) { | ||
| return cached; | ||
| } | ||
| try { | ||
| cached = require('node-pty') as NodePty; | ||
| } catch (err) { | ||
| const e = err as NodeJS.ErrnoException; | ||
| if (e?.code === 'MODULE_NOT_FOUND' && /'node-pty'/.test(e.message)) { | ||
| throw new Error( | ||
| "PTY mode requires the optional peer dependency 'node-pty', which is not " + | ||
| 'installed. Install it (e.g. `npm install node-pty`) or spawn with `usePty: false`.', | ||
| ); | ||
| } | ||
| throw err; | ||
| } | ||
| return cached; | ||
| } |
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,109 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| /** | ||
| * Minimal, vendored subset of the `node-pty` public type surface. | ||
| * | ||
| * `node-pty` is an *optional* peer dependency (see {@link ./lazyPty}). If the | ||
| * SDK referenced `node-pty`'s own types in its public API, every consumer would | ||
| * need `node-pty` installed just to type-check β even pipe-only consumers that | ||
| * never spawn a PTY (they would otherwise hit `TS2307: Cannot find module | ||
| * 'node-pty'`). Re-declaring the slice we expose keeps the generated `.d.ts` | ||
| * self-contained. | ||
| * | ||
| * These declarations are structurally compatible with the real `node-pty` | ||
| * types, so values produced by the actual module satisfy them at runtime. | ||
| */ | ||
|
|
||
| /** An object that can be disposed via a dispose function. */ | ||
| export interface IDisposable { | ||
| dispose(): void; | ||
| } | ||
|
|
||
| /** | ||
| * An event that can be listened to. | ||
| * @returns an `IDisposable` to stop listening. | ||
| */ | ||
| export interface IEvent<T> { | ||
| (listener: (e: T) => unknown): IDisposable; | ||
| } | ||
|
|
||
| /** Options shared by all platforms when forking a pseudoterminal. */ | ||
| export interface IBasePtyForkOptions { | ||
| /** Name of the terminal to be set in environment ($TERM variable). */ | ||
| name?: string; | ||
| /** Number of initial cols of the pty. */ | ||
| cols?: number; | ||
| /** Number of initial rows of the pty. */ | ||
| rows?: number; | ||
| /** Working directory to be set for the child program. */ | ||
| cwd?: string; | ||
| /** Environment to be set for the child program. */ | ||
| env?: { [key: string]: string | undefined }; | ||
| /** String encoding of the underlying pty. */ | ||
| encoding?: string | null; | ||
| /** (EXPERIMENTAL) Whether to enable flow control handling. */ | ||
| handleFlowControl?: boolean; | ||
| /** (EXPERIMENTAL) String that pauses the pty when `handleFlowControl` is true. */ | ||
| flowControlPause?: string; | ||
| /** (EXPERIMENTAL) String that resumes the pty when `handleFlowControl` is true. */ | ||
| flowControlResume?: string; | ||
| } | ||
|
|
||
| /** POSIX-specific fork options. */ | ||
| export interface IPtyForkOptions extends IBasePtyForkOptions { | ||
| uid?: number; | ||
| gid?: number; | ||
| } | ||
|
|
||
| /** Windows-specific fork options. */ | ||
| export interface IWindowsPtyForkOptions extends IBasePtyForkOptions { | ||
| /** @deprecated Ignored by node-pty; retained for compatibility. */ | ||
| useConpty?: boolean; | ||
| /** (EXPERIMENTAL) Use the conpty.dll shipped with node-pty. */ | ||
| useConptyDll?: boolean; | ||
| /** Whether to use PSEUDOCONSOLE_INHERIT_CURSOR in conpty. */ | ||
| conptyInheritCursor?: boolean; | ||
| } | ||
|
|
||
| /** An interface representing a pseudoterminal. */ | ||
| export interface IPty { | ||
| /** The process ID of the outer process. */ | ||
| readonly pid: number; | ||
| /** The column size in characters. */ | ||
| readonly cols: number; | ||
| /** The row size in characters. */ | ||
| readonly rows: number; | ||
| /** The title of the active process. */ | ||
| readonly process: string; | ||
| /** (EXPERIMENTAL) Whether to handle flow control at runtime. */ | ||
| handleFlowControl: boolean; | ||
| /** Fires when data is returned from the pty. */ | ||
| readonly onData: IEvent<string>; | ||
| /** Fires when the pty exits. */ | ||
| readonly onExit: IEvent<{ exitCode: number; signal?: number }>; | ||
| /** Resizes the dimensions of the pty. */ | ||
| resize(columns: number, rows: number, pixelSize?: { width: number; height: number }): void; | ||
| /** Clears the pty's internal representation of its buffer (ConPTY only). */ | ||
| clear(): void; | ||
| /** Writes data to the pty. */ | ||
| write(data: string | Buffer): void; | ||
| /** Kills the pty. */ | ||
| kill(signal?: string): void; | ||
| /** Pauses the pty for customizable flow control. */ | ||
| pause(): void; | ||
| /** Resumes the pty for customizable flow control. */ | ||
| resume(): void; | ||
| } | ||
|
|
||
| /** | ||
| * The slice of the `node-pty` module shape that the SDK calls into. Returned by | ||
| * {@link ./lazyPty.loadPty}. | ||
| */ | ||
| export interface NodePty { | ||
| spawn( | ||
| file: string, | ||
| args: string[] | string, | ||
| options: IPtyForkOptions | IWindowsPtyForkOptions, | ||
| ): IPty; | ||
| } |
Oops, something went wrong.
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.