var PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE=131094;var EXTENDED_STARTUPINFO_PRESENT=524288;var HEAP_ZERO_MEMORY=8;var duplex=require("stream").Duplex;function vt(){this._ObjectID="win-virtual-terminal";Object.defineProperty(this,"supported",{value:(function(){var f=require("_GenericMarshal");var g=f.CreateNativeProxy("kernel32.dll");try{g.CreateMethod("CreatePseudoConsole")}catch(d){return(false)}return(true)})()});this.Create=function a(k,o,i){if(!this.supported){throw ("This build of Windows does not have support for PseudoConsoles")}if(!o){o=80}if(!i){i=25}var h=require("_GenericMarshal");var j=h.CreateNativeProxy("kernel32.dll");j.CreateMethod("CreatePipe");j.CreateMethod("CreateProcessW");j.CreateMethod("CreatePseudoConsole");j.CreateMethod("GetProcessHeap");j.CreateMethod("HeapAlloc");j.CreateMethod("InitializeProcThreadAttributeList");j.CreateMethod("UpdateProcThreadAttribute");j.CreateMethod("WriteFile");j.CreateMethod("ReadFile");j.CreateMethod("TerminateProcess");var m={_h:h.CreatePointer(),_consoleInput:h.CreatePointer(),_consoleOutput:h.CreatePointer(),_input:h.CreatePointer(),_output:h.CreatePointer(),k32:j};var f=h.CreateVariable(8);var e;var l=h.CreateVariable(h.PointerSize==4?16:24);if(j.CreatePipe(m._consoleInput,m._input,0,0).Val==0){console.log("PIPE/FAIL")}if(j.CreatePipe(m._output,m._consoleOutput,0,0).Val==0){console.log("PIPE/FAIL")}if(j.CreatePseudoConsole((i<<16)|o,m._consoleInput.Deref(),m._consoleOutput.Deref(),0,m._h).Val!=0){throw ("Error calling CreatePseudoConsole()")}j.InitializeProcThreadAttributeList(0,1,0,f);e=h.CreateVariable(f.toBuffer().readUInt32LE());var n=h.CreateVariable(h.PointerSize==8?112:72);n.toBuffer().writeUInt32LE(h.PointerSize==8?112:72,0);e.pointerBuffer().copy(n.Deref(h.PointerSize==8?104:68,h.PointerSize).toBuffer());if(j.InitializeProcThreadAttributeList(e,1,0,f).Val!=0){if(j.UpdateProcThreadAttribute(e,0,PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,m._h.Deref(),h.PointerSize,0,0).Val!=0){if(j.CreateProcessW(0,h.CreateVariable(k,{wide:true}),0,0,1,EXTENDED_STARTUPINFO_PRESENT,0,0,n,l).Val!=0){m._startupinfoex=n;m._process=l.Deref(0);m._pid=l.Deref(h.PointerSize==4?8:16,4).toBuffer().readUInt32LE();var g=new duplex({write:function(p,q){var r=require("_GenericMarshal").CreateVariable(4);this.terminal.k32.WriteFile(this.terminal._input.Deref(),require("_GenericMarshal").CreateVariable(p),p.length,r,0);q();return(true)},"final":function(p){if(this.terminal._process){this.terminal.k32.TerminateProcess(this.terminal._process,0);this.terminal._process=null;this.terminal.k32.ReadFile.async.abort()}p()}});m._waiter=require("DescriptorEvents").addDescriptor(l.Deref(0));m._waiter.ds=g;m._waiter.on("signaled",function(){this.ds.push(null)});g.terminal=m;g._rpbuf=h.CreateVariable(4096);g._rpbufRead=h.CreateVariable(4);g._read=function d(){this._rp=this.terminal.k32.ReadFile.async(this.terminal._output.Deref(),this._rpbuf,this._rpbuf._size,this._rpbufRead,0);this._rp.then(function(){var p=this.parent._rpbufRead.toBuffer().readUInt32LE();this.parent.push(this.parent._rpbuf.toBuffer().slice(0,p));this.parent._read()});this._rp.parent=this};g._read();return(g)}else{console.log("FAILED!")}}}throw ("Internal Error")};this.PowerShellCapable=function(){if(require("os").arch()=="x64"){return(require("fs").existsSync(process.env.windir+"\\SysWow64\\WindowsPowerShell\\v1.0\\powershell.exe"))}else{return(require("fs").existsSync(process.env.windir+"\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"))}};this.Start=function b(e,d){return(this.Create(process.env.windir+"\\System32\\cmd.exe",e,d))};this.StartPowerShell=function c(e,d){if(require("os").arch()=="x64"){return(this.Create(process.env.windir+"\\SysWow64\\WindowsPowerShell\\v1.0\\powershell.exe",e,d))}else{return(this.Create(process.env.windir+"\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",e,d))}}}if(process.platform=="win32"){module.exports=new vt()};