@@ -2,24 +2,34 @@ import * as crypto from "crypto";
22import * as net from "net" ;
33import * as childProcess from "child_process" ;
44import * as fs from "fs" ;
5+ import * as os from "os" ;
6+ import * as path from "path" ;
57
68export interface DebugPipe {
79 open : ( onData : ( data : unknown ) => void , onError : ( err : unknown ) => void ) => void ;
810 close : ( ) => void ;
911 write : ( data : string ) => void ;
12+ openPull : ( onError : ( err : unknown ) => void ) => void ;
13+ requestPull : ( ) => void ;
1014 getOutputPipePath : ( ) => string ;
1115 getInputPipePath : ( ) => string ;
16+ getPullPipePath : ( ) => string ;
1217}
1318
1419export function createNamedPipe ( ) : DebugPipe {
1520 const pipeId = crypto . randomBytes ( 16 ) . toString ( "hex" ) ;
1621 const outputPipePath = `\\\\.\\pipe\\lldbg_out_${ pipeId } ` ;
1722 const inputPipePath = `\\\\.\\pipe\\lldbg_in_${ pipeId } ` ;
23+ const pullPipePath = `\\\\.\\pipe\\lldbg_pull_${ pipeId } ` ;
1824 let outputPipe : net . Server | null = null ;
1925 let inputPipe : net . Server | null = null ;
26+ let pullPipe : net . Server | null = null ;
2027 let inputStream : net . Socket | null ;
28+ let pullStream : net . Socket | null ;
29+ let onErrorCallback : ( ( err : unknown ) => void ) | null = null ;
2130 return {
2231 open : ( onData , onError ) => {
32+ onErrorCallback = onError ;
2333 outputPipe = net . createServer (
2434 stream => {
2535 stream . on ( "data" , onData ) ;
@@ -35,31 +45,56 @@ export function createNamedPipe(): DebugPipe {
3545 ) ;
3646 inputPipe . listen ( inputPipePath ) ;
3747 } ,
48+ openPull : ( onError : ( err : unknown ) => void ) => {
49+ if ( ! onErrorCallback ) {
50+ onErrorCallback = onError ;
51+ }
52+
53+ pullPipe = net . createServer (
54+ stream => {
55+ stream . on ( "error" , err => {
56+ onError ( `error on pull pipe: ${ err } ` ) ;
57+ } ) ;
58+ pullStream = stream ;
59+ }
60+ ) ;
61+ pullPipe . listen ( pullPipePath ) ;
62+ } ,
3863
3964 close : ( ) => {
4065 outputPipe ?. close ( ) ;
4166 outputPipe = null ;
4267 inputPipe ?. close ( ) ;
4368 inputPipe = null ;
4469 inputStream = null ;
70+ pullPipe = null ;
71+ pullStream = null ;
4572 } ,
4673
4774 write : data => {
4875 inputStream ?. write ( data ) ;
4976 } ,
77+ requestPull : ( ) => {
78+ pullStream ?. write ( "pull|\n" ) ;
79+ } ,
5080
5181 getOutputPipePath : ( ) => outputPipePath ,
5282 getInputPipePath : ( ) => inputPipePath ,
83+ getPullPipePath : ( ) => pullPipePath ,
5384 } ;
5485}
5586
5687export function createFifoPipe ( ) : DebugPipe {
5788 const pipeId = crypto . randomBytes ( 16 ) . toString ( "hex" ) ;
5889 const outputPipePath = `/tmp/lldbg_out_${ pipeId } ` ;
5990 const inputPipePath = `/tmp/lldbg_in_${ pipeId } ` ;
60- let outputFd : number | null ;
61- let inputFd : number | null ;
91+ let pullPipePath = "" ;
92+ let debuggerTmpDir = "" ;
93+ let outputFd : number | null = null ;
94+ let inputFd : number | null = null ;
95+ let pullFd : number | null = null ;
6296 let inputStream : fs . WriteStream | null = null ;
97+ let pullStream : fs . WriteStream | null = null ;
6398 let onErrorCallback : ( ( err : unknown ) => void ) | null = null ;
6499 return {
65100 open : ( onData , onError ) => {
@@ -113,7 +148,27 @@ export function createFifoPipe(): DebugPipe {
113148 ) ;
114149 }
115150 ) ;
151+ } ,
116152
153+ openPull : ( onError : ( err : unknown ) => void ) => {
154+ if ( ! onErrorCallback ) {
155+ onErrorCallback = onError ;
156+ }
157+
158+ const appPrefix = "lldebugger" ;
159+ try {
160+ debuggerTmpDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , appPrefix ) ) ;
161+ pullPipePath = path . join ( debuggerTmpDir , "pull.txt" ) ;
162+
163+ const fd = fs . openSync (
164+ pullPipePath ,
165+ fs . constants . O_WRONLY | fs . constants . O_CREAT
166+ ) ;
167+ pullFd = fd ;
168+ pullStream = fs . createWriteStream ( null as unknown as fs . PathLike , { fd} ) ;
169+ } catch ( e : unknown ) {
170+ onErrorCallback ( e ) ;
171+ }
117172 } ,
118173
119174 close : ( ) => {
@@ -141,13 +196,31 @@ export function createFifoPipe(): DebugPipe {
141196 }
142197 ) ;
143198 }
199+ inputStream = null ;
200+ try {
201+ if ( pullFd !== null ) {
202+ fs . close ( pullFd ) ;
203+ fs . rmSync ( pullPipePath ) ;
204+ pullFd = null ;
205+ }
206+ pullStream = null ;
207+ if ( debuggerTmpDir . length > 0 ) {
208+ fs . rmdirSync ( debuggerTmpDir ) ;
209+ }
210+ } catch ( e : unknown ) {
211+ onErrorCallback ?.( e ) ;
212+ }
144213 } ,
145214
146215 write : data => {
147216 inputStream ?. write ( data ) ;
148217 } ,
218+ requestPull : ( ) => {
219+ pullStream ?. write ( "pull|\n" ) ;
220+ } ,
149221
150222 getOutputPipePath : ( ) => outputPipePath ,
151223 getInputPipePath : ( ) => inputPipePath ,
224+ getPullPipePath : ( ) => pullPipePath ,
152225 } ;
153226}
0 commit comments