import fs = require ( "fs" ) ; interface ServerCancellationToken { isCancellationRequested ( ) : boolean ; setRequest ( requestId ) : void ; resetRequest ( requestId ) : void ; } function pipeExists ( name ) { return fs . existsSync ( name ) ; } function createCancellationToken ( args ) { let cancellationPipeName ; for ( let i = 0 ; i < args . length - 1 ; i ++ ) { if ( args [ i ] === "--cancellationPipeName" ) { cancellationPipeName = args [ i + 1 ] ; break ; } } if ( ! cancellationPipeName ) { return { isCancellationRequested : ( ) => false , setRequest : ( _requestId ) : void => void 0 , resetRequest : ( _requestId ) : void => void 0 } ; } if ( cancellationPipeName . charAt ( cancellationPipeName . length - 1 ) === "*" ) { const namePrefix = cancellationPipeName . slice ( 0 , - 1 ) ; if ( namePrefix . length === 0 || namePrefix . indexOf ( "*" ) >= 0 ) { throw new Error ( "Invalid name for template cancellation pipe: it should have length greater than 2 characters and contain only one '*'." ) ; } let perRequestPipeName ; let currentRequestId ; return { isCancellationRequested : ( ) => perRequestPipeName !== undefined && pipeExists ( perRequestPipeName ) , setRequest ( requestId ) { currentRequestId = requestId ; perRequestPipeName = namePrefix + requestId ; } , resetRequest ( requestId ) { if ( currentRequestId !== requestId ) { throw new Error ( `Mismatched request id, expected ${ currentRequestId }, actual ${ requestId }` ) ; } perRequestPipeName = undefined ; } } ; } else { return { isCancellationRequested : ( ) => pipeExists ( cancellationPipeName ! ) , setRequest : ( _requestId ) : void => void 0 , resetRequest : ( _requestId ) : void => void 0 } ; } } export = createCancellationToken ;