| 70 | } |
| 71 | |
| 72 | export class StreamMessageWriter extends AbstractMessageWriter implements MessageWriter { |
| 73 | |
| 74 | private writable: NodeJS.WritableStream; |
| 75 | private encoding: string; |
| 76 | private errorCount: number; |
| 77 | |
| 78 | public constructor(writable: NodeJS.WritableStream, encoding: string = 'utf8') { |
| 79 | super(); |
| 80 | this.writable = writable; |
| 81 | this.encoding = encoding; |
| 82 | this.errorCount = 0; |
| 83 | this.writable.on('error', (error: any) => this.fireError(error)); |
| 84 | this.writable.on('close', () => this.fireClose()); |
| 85 | } |
| 86 | |
| 87 | public write(msg: Message): void { |
| 88 | let json = JSON.stringify(msg); |
| 89 | let contentLength = Buffer.byteLength(json, this.encoding); |
| 90 | |
| 91 | let headers: string[] = [ |
| 92 | ContentLength, contentLength.toString(), CRLF, |
| 93 | CRLF |
| 94 | ]; |
| 95 | try { |
| 96 | // Header must be written in ASCII encoding |
| 97 | this.writable.write(headers.join(''), 'ascii'); |
| 98 | // Now write the content. This can be written in any encoding |
| 99 | this.writable.write(json, this.encoding); |
| 100 | this.errorCount = 0; |
| 101 | } catch (error) { |
| 102 | this.errorCount++; |
| 103 | this.fireError(error, msg, this.errorCount); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | export class IPCMessageWriter extends AbstractMessageWriter implements MessageWriter { |
| 109 |
nothing calls this directly
no outgoing calls
no test coverage detected