{"version":3,"file":"index.es.js","sources":["../api/eventEmitter2.ts","../api/index.ts"],"sourcesContent":["/**\r\n * Copyright (c) 2019, Microsoft Corporation (MIT License).\r\n */\r\n\r\nimport { IDisposable } from \".\";\r\n\r\ninterface IListener {\r\n (e: T): void;\r\n}\r\n\r\nexport interface IEvent {\r\n (listener: (e: T) => any): IDisposable;\r\n}\r\n\r\nexport class EventEmitter2 {\r\n private _listeners: IListener[] = [];\r\n private _event?: IEvent;\r\n\r\n public get event(): IEvent {\r\n if (!this._event) {\r\n this._event = (listener: (e: T) => any) => {\r\n this._listeners.push(listener);\r\n const disposable = {\r\n dispose: () => {\r\n for (let i = 0; i < this._listeners.length; i++) {\r\n if (this._listeners[i] === listener) {\r\n this._listeners.splice(i, 1);\r\n return;\r\n }\r\n }\r\n }\r\n };\r\n return disposable;\r\n };\r\n }\r\n return this._event;\r\n }\r\n\r\n public fire(data: T): void {\r\n const queue: IListener[] = [];\r\n for (let i = 0; i < this._listeners.length; i++) {\r\n queue.push(this._listeners[i]);\r\n }\r\n for (let i = 0; i < queue.length; i++) {\r\n queue[i].call(undefined, data);\r\n }\r\n }\r\n}","/**\r\n * Copyright (c) 2017, Daniel Imms (MIT License).\r\n * Copyright (c) 2018, Microsoft Corporation (MIT License).\r\n * Copyright (c) 2023, Tnze (MIT License).\r\n */\r\nimport { invoke } from '@tauri-apps/api'\r\nimport { EventEmitter2 } from \"./eventEmitter2\";\r\n\r\n/**\r\n * Forks a process as a pseudoterminal.\r\n * @param file The file to launch.\r\n * @param args The file's arguments as argv (string[]) or in a pre-escaped CommandLine format\r\n * (string). Note that the CommandLine option is only available on Windows and is expected to be\r\n * escaped properly.\r\n * @param options The options of the terminal.\r\n * @see CommandLineToArgvW https://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx\r\n * @see Parsing C++ Comamnd-Line Arguments https://msdn.microsoft.com/en-us/library/17w5ykft.aspx\r\n * @see GetCommandLine https://msdn.microsoft.com/en-us/library/windows/desktop/ms683156.aspx\r\n */\r\nexport function spawn(file: string, args: string[] | string, options: IPtyForkOptions | IWindowsPtyForkOptions): IPty {\r\n return new TauriPty(file, args, options)\r\n}\r\n\r\nexport interface IBasePtyForkOptions {\r\n\r\n /**\r\n * Name of the terminal to be set in environment ($TERM variable).\r\n */\r\n name?: string;\r\n\r\n /**\r\n * Number of intial cols of the pty.\r\n */\r\n cols?: number;\r\n\r\n /**\r\n * Number of initial rows of the pty.\r\n */\r\n rows?: number;\r\n\r\n /**\r\n * Working directory to be set for the child program.\r\n */\r\n cwd?: string;\r\n\r\n /**\r\n * Environment to be set for the child program.\r\n */\r\n env?: { [key: string]: string | undefined };\r\n\r\n /**\r\n * String encoding of the underlying pty.\r\n * If set, incoming data will be decoded to strings and outgoing strings to bytes applying this encoding.\r\n * If unset, incoming data will be delivered as raw bytes (Buffer type).\r\n * By default 'utf8' is assumed, to unset it explicitly set it to `null`.\r\n */\r\n encoding?: string | null;\r\n\r\n /**\r\n * (EXPERIMENTAL)\r\n * Whether to enable flow control handling (false by default). If enabled a message of `flowControlPause`\r\n * will pause the socket and thus blocking the child program execution due to buffer back pressure.\r\n * A message of `flowControlResume` will resume the socket into flow mode.\r\n * For performance reasons only a single message as a whole will match (no message part matching).\r\n * If flow control is enabled the `flowControlPause` and `flowControlResume` messages are not forwarded to\r\n * the underlying pseudoterminal.\r\n */\r\n handleFlowControl?: boolean;\r\n\r\n /**\r\n * (EXPERIMENTAL)\r\n * The string that should pause the pty when `handleFlowControl` is true. Default is XOFF ('\\x13').\r\n */\r\n flowControlPause?: string;\r\n\r\n /**\r\n * (EXPERIMENTAL)\r\n * The string that should resume the pty when `handleFlowControl` is true. Default is XON ('\\x11').\r\n */\r\n flowControlResume?: string;\r\n}\r\n\r\nexport interface IPtyForkOptions extends IBasePtyForkOptions {\r\n /**\r\n * Security warning: use this option with great caution,\r\n * as opened file descriptors with higher privileges might leak to the child program.\r\n */\r\n uid?: number;\r\n gid?: number;\r\n}\r\n\r\nexport interface IWindowsPtyForkOptions extends IBasePtyForkOptions {\r\n /**\r\n * Whether to use the ConPTY system on Windows. When this is not set, ConPTY will be used when\r\n * the Windows build number is >= 18309 (instead of winpty). Note that ConPTY is available from\r\n * build 17134 but is too unstable to enable by default.\r\n *\r\n * This setting does nothing on non-Windows.\r\n */\r\n useConpty?: boolean;\r\n\r\n /**\r\n * Whether to use PSEUDOCONSOLE_INHERIT_CURSOR in conpty.\r\n * @see https://docs.microsoft.com/en-us/windows/console/createpseudoconsole\r\n */\r\n conptyInheritCursor?: boolean;\r\n}\r\n\r\n/**\r\n * An interface representing a pseudoterminal, on Windows this is emulated via the winpty library.\r\n */\r\nexport interface IPty {\r\n /**\r\n * The process ID of the outer process.\r\n */\r\n readonly pid: number;\r\n\r\n /**\r\n * The column size in characters.\r\n */\r\n readonly cols: number;\r\n\r\n /**\r\n * The row size in characters.\r\n */\r\n readonly rows: number;\r\n\r\n /**\r\n * The title of the active process.\r\n */\r\n readonly process: string;\r\n\r\n /**\r\n * (EXPERIMENTAL)\r\n * Whether to handle flow control. Useful to disable/re-enable flow control during runtime.\r\n * Use this for binary data that is likely to contain the `flowControlPause` string by accident.\r\n */\r\n handleFlowControl: boolean;\r\n\r\n /**\r\n * Adds an event listener for when a data event fires. This happens when data is returned from\r\n * the pty.\r\n * @returns an `IDisposable` to stop listening.\r\n */\r\n readonly onData: IEvent;\r\n\r\n /**\r\n * Adds an event listener for when an exit event fires. This happens when the pty exits.\r\n * @returns an `IDisposable` to stop listening.\r\n */\r\n readonly onExit: IEvent<{ exitCode: number, signal?: number }>;\r\n\r\n /**\r\n * Resizes the dimensions of the pty.\r\n * @param columns The number of columns to use.\r\n * @param rows The number of rows to use.\r\n */\r\n resize(columns: number, rows: number): void;\r\n\r\n /**\r\n * Clears the pty's internal representation of its buffer. This is a no-op\r\n * unless on Windows/ConPTY. This is useful if the buffer is cleared on the\r\n * frontend in order to synchronize state with the backend to avoid ConPTY\r\n * possibly reprinting the screen.\r\n */\r\n clear(): void;\r\n\r\n /**\r\n * Writes data to the pty.\r\n * @param data The data to write.\r\n */\r\n write(data: string): void;\r\n\r\n /**\r\n * Kills the pty.\r\n * @param signal The signal to use, defaults to SIGHUP. This parameter is not supported on\r\n * Windows.\r\n * @throws Will throw when signal is used on Windows.\r\n */\r\n kill(signal?: string): void;\r\n\r\n /**\r\n * Pauses the pty for customizable flow control.\r\n */\r\n pause(): void;\r\n\r\n /**\r\n * Resumes the pty for customizable flow control.\r\n */\r\n resume(): void;\r\n}\r\n\r\n/**\r\n * An object that can be disposed via a dispose function.\r\n */\r\nexport interface IDisposable {\r\n dispose(): void;\r\n}\r\n\r\n/**\r\n * An event that can be listened to.\r\n * @returns an `IDisposable` to stop listening.\r\n */\r\nexport interface IEvent {\r\n (listener: (e: T) => any): IDisposable;\r\n}\r\n\r\nexport type ArgvOrCommandLine = string[] | string;\r\n\r\nclass TauriPty implements IPty, IDisposable {\r\n pid: number;\r\n cols: number;\r\n rows: number;\r\n process: string;\r\n handleFlowControl: boolean;\r\n\r\n private _exitted: boolean;\r\n private _init: Promise;\r\n\r\n private _onData = new EventEmitter2();\r\n private _onExit = new EventEmitter2<{ exitCode: number; signal?: number | undefined; }>();\r\n\r\n constructor(file: string, args?: ArgvOrCommandLine, opt?: IWindowsPtyForkOptions) {\r\n args = typeof args === 'string' ? [args] : args ?? []; // Convert args to string[] anyways.\r\n const invokeArgs = {\r\n file, args,\r\n termName: opt?.name ?? 'Terminal',\r\n cols: opt?.cols ?? null,\r\n rows: opt?.rows ?? null,\r\n cwd: opt?.cwd ?? null,\r\n env: opt?.env ?? {},\r\n encoding: opt?.encoding ?? null,\r\n handleFlowControl: opt?.handleFlowControl ?? null,\r\n flowControlPause: opt?.flowControlPause ?? null,\r\n flowControlResume: opt?.flowControlResume ?? null,\r\n };\r\n this._exitted = false;\r\n this._init = invoke('plugin:pty|spawn', invokeArgs).then(pid => {\r\n this.pid = pid;\r\n this.readData();\r\n this.wait();\r\n });\r\n }\r\n dispose(): void {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n\r\n public get onData(): IEvent { return this._onData.event; }\r\n public get onExit(): IEvent<{ exitCode: number; signal?: number | undefined; }> { return this._onExit.event; }\r\n\r\n resize(columns: number, rows: number): void {\r\n this.cols = columns;\r\n this.rows = rows;\r\n this._init.then(() =>\r\n invoke('plugin:pty|resize', { pid: this.pid, cols: columns, rows }).catch(e => {\r\n console.error('Resize error: ', e);\r\n })\r\n );\r\n }\r\n clear(): void {\r\n console.warn(\"clear is un implemented!\")\r\n }\r\n write(data: string): void {\r\n this._init.then(() =>\r\n invoke('plugin:pty|write', { pid: this.pid, data }).catch(e => {\r\n console.error('Writing error: ', e);\r\n })\r\n );\r\n }\r\n kill(signal?: string | undefined): void {\r\n this._init.then(() =>\r\n invoke('plugin:pty|kill', { pid: this.pid })\r\n );\r\n }\r\n pause(): void {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n resume(): void {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n\r\n private async readData() {\r\n await this._init;\r\n try {\r\n for (; ;) {\r\n const data = await invoke('plugin:pty|read', { pid: this.pid });\r\n this._onData.fire(data);\r\n }\r\n } catch (e: any) {\r\n if (typeof e === 'string' && e.includes('EOF')) {\r\n return;\r\n }\r\n console.error('Reading error: ', e);\r\n }\r\n }\r\n\r\n private async wait() {\r\n if (this._exitted) {\r\n return;\r\n }\r\n try {\r\n const exitCode = await invoke('plugin:pty|exitstatus', { pid: this.pid })\r\n this._exitted = true;\r\n this._onExit.fire({ exitCode });\r\n } catch (e: any) {\r\n console.error(e);\r\n }\r\n }\r\n}\r\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAca,aAAa,CAAA;AAA1B,IAAA,WAAA,GAAA;QACU,IAAU,CAAA,UAAA,GAAmB,EAAE,CAAC;KAgCzC;AA7BC,IAAA,IAAW,KAAK,GAAA;AACd,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;AAChB,YAAA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