|
| 1 | +import { nextTick } from 'node:process'; |
| 2 | +import type { Writable } from 'node:stream'; |
| 3 | +import type { |
| 4 | + LogMessage, |
| 5 | + LogTransporter, |
| 6 | +} from '../../../common/logger/types.js'; |
| 7 | +import type { LogFormatter } from '../types.js'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Transports logs via a {@link Writable} stream. |
| 11 | + */ |
| 12 | +export class WritableLogTransporterImpl implements LogTransporter { |
| 13 | + // To allow for asynchronous writing of logs without the use of promises, |
| 14 | + // we use a recursive-like queue draining algorithm. This provides an |
| 15 | + // ergonomic API while still allowing for asynchronous writing. |
| 16 | + // This also allows us to buffer when we need to wait for the writer to drain. |
| 17 | + private asyncWriteQueue = new Array<LogMessage>(); |
| 18 | + private asyncWriteInProgress = false; |
| 19 | + |
| 20 | + private writer: Writable; |
| 21 | + private formatter?: LogFormatter; |
| 22 | + |
| 23 | + constructor(options: { |
| 24 | + /** |
| 25 | + * Where to write the log messages. |
| 26 | + */ |
| 27 | + writable: Writable; |
| 28 | + /** |
| 29 | + * Optional to format the log messages before writing them. |
| 30 | + */ |
| 31 | + formatter?: LogFormatter; |
| 32 | + }) { |
| 33 | + this.formatter = options?.formatter; |
| 34 | + this.writer = options.writable; |
| 35 | + |
| 36 | + this.writer.on('error', (error) => { |
| 37 | + this.callback(error); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + public transport(message: LogMessage): void { |
| 42 | + // We queue messages to write then process them in the background |
| 43 | + // to allow for asynchronous writing of logs without the use of promises |
| 44 | + // so that the use of our logger doesn't require 'await' everywhere. |
| 45 | + // And to buffer when we need to wait for the writer to drain. |
| 46 | + this.asyncWriteQueue.push(message); |
| 47 | + |
| 48 | + // Ensure the log is written asynchronously, but soonish. |
| 49 | + nextTick(() => { |
| 50 | + this.writeNextMessageAsync(); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + protected writeNextMessageAsync(): void { |
| 55 | + if (this.asyncWriteInProgress || this.asyncWriteQueue.length === 0) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + const message = this.asyncWriteQueue.shift(); |
| 60 | + if (!message) { |
| 61 | + this.callback(); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + this.asyncWriteInProgress = true; |
| 66 | + |
| 67 | + try { |
| 68 | + const formattedMessage = this.formatter?.format(message) ?? message; |
| 69 | + |
| 70 | + const doDrain = !this.writer.write(formattedMessage); |
| 71 | + |
| 72 | + if (doDrain) { |
| 73 | + this.writer.once('drain', () => { |
| 74 | + this.callback(); |
| 75 | + }); |
| 76 | + } else { |
| 77 | + this.callback(); |
| 78 | + } |
| 79 | + } catch (error) { |
| 80 | + this.callback(error); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + protected callback(error?: Error | null): void { |
| 85 | + if (error) { |
| 86 | + console.error('[LOGGER:WRITE:ERROR]', error); |
| 87 | + } |
| 88 | + |
| 89 | + this.asyncWriteInProgress = false; |
| 90 | + |
| 91 | + // Now check for more messages to write. |
| 92 | + // Using `nextTick` avoids stack overflow when the queue is large. |
| 93 | + nextTick(() => { |
| 94 | + this.writeNextMessageAsync(); |
| 95 | + }); |
| 96 | + } |
| 97 | +} |
0 commit comments