2026-09-15 13:00:32 +02:00
|
|
|
import { Transform } from 'node:stream';
|
|
|
|
|
export default class LastNewline extends Transform {
|
2020-02-29 23:01:03 +01:00
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
this.lastByte = false;
|
|
|
|
|
}
|
2026-09-15 13:00:32 +02:00
|
|
|
/** @internal */
|
2020-02-29 23:01:03 +01:00
|
|
|
_transform(chunk, encoding, done) {
|
|
|
|
|
if (chunk.length) {
|
|
|
|
|
this.lastByte = chunk[chunk.length - 1];
|
|
|
|
|
}
|
|
|
|
|
this.push(chunk);
|
|
|
|
|
done();
|
|
|
|
|
}
|
2026-09-15 13:00:32 +02:00
|
|
|
/** @internal */
|
2020-02-29 23:01:03 +01:00
|
|
|
_flush(done) {
|
|
|
|
|
if (this.lastByte === 0x0a) {
|
|
|
|
|
return done();
|
|
|
|
|
}
|
|
|
|
|
if (this.lastByte === 0x0d) {
|
|
|
|
|
this.push(Buffer.from('\n'));
|
|
|
|
|
return done();
|
|
|
|
|
}
|
|
|
|
|
this.push(Buffer.from('\r\n'));
|
|
|
|
|
return done();
|
|
|
|
|
}
|
|
|
|
|
}
|