import * as os from 'os' ; import * as fs from 'fs' ; import { Edge , Vertex } from 'lsif-protocol' ; import { Store } from './store' ; const __stdout = process . stdout ; const __eol = os . EOL ; export interface Writer { write ( ... data ) : void ; writeEOL ( ) : void ; writeln ( ... data ) : void ; } export class StdoutWriter implements Writer { public constructor ( ) { } write ( ... data ) { for ( let chunk of data ) { __stdout . write ( chunk ) ; } } writeEOL ( ) { __stdout . write ( __eol ) ; } writeln ( ... data ) { for ( let chunk of data ) { __stdout . write ( chunk ) ; } __stdout . write ( __eol ) ; } } export class FileWriter implements Writer { public constructor ( private fd ) { } write ( ... data ) { if ( data . length === 0 ) { return ; } for ( let chunk of data ) { this . writeBuffer ( Buffer . from ( chunk , 'utf8' ) ) ; } } writeEOL ( ) { this . writeBuffer ( Buffer . from ( __eol , 'utf8' ) ) ; } writeln ( ... data ) { if ( data . length === 0 ) { this . writeEOL ( ) ; return ; } for ( let chunk of data ) { this . writeBuffer ( Buffer . from ( chunk , 'utf8' ) ) ; } this . writeEOL ( ) ; } private writeBuffer ( buffer ) { let offset = 0 ; while ( offset < buffer . length ) { offset += fs . writeSync ( this . fd , buffer , offset ) ; } } } export class CompressStore extends Store { private writer : Writer ; constructor ( input , fileName ? ) { super ( input ) ; this . writer = fileName !== undefined ? new FileWriter ( fs . openSync ( fileName , 'w' ) ) : new StdoutWriter ( ) ; } public insert ( element ) { this . writer . writeln ( this . compress ( element ) ) ; } }