• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

palcarazm / batchjs / 17580371047

09 Sep 2025 10:57AM UTC coverage: 98.387% (-1.4%) from 99.825%
17580371047

push

github

web-flow
Merge pull request #58 from palcarazm/fix/1.2.2

fix: prevent write callback call before flushing

89 of 90 branches covered (98.89%)

Branch coverage included in aggregate %.

31 of 39 new or added lines in 8 files covered. (79.49%)

460 of 468 relevant lines covered (98.29%)

75.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/src/streams/classes/SingleStream.ts
1
import { TransformCallback } from "stream";
2
import { SingleStreamError } from "../errors/_index";
120✔
3
import { InternalBufferDuplex, ObjectDuplexOptions } from "../interfaces/_index";
120✔
4

5
/**
6
 * @class
7
 * Class that allows you to verify that a stream contains only one chunk.
8
 * @extends InternalBufferDuplex
9
 * @template T
10
 * @example
11
 * ```typescript
12
 * const stream:SingleStream<string> = new SingleStream({
13
 *     objectMode: true,
14
 * });
15
 * 
16
 * stream.write("data1");
17
 * stream.write("data2"); // This should launch error
18
 * stream.end();
19
 * 
20
 * stream.on("data", (chunk: string) => {
21
 *     console.log(``Pushed chunk: ${chunk}```);
22
 * });
23
 * stream.once("error", (err: SingleStreamError) => {
24
 *     console.log(``Error: ${err.message}```);
25
 * });
26
 * ```
27
 * ```shell
28
 * >> Pushed chunk: data1
29
 * >> Error: Expected only one chunk in the stream
30
 * ```
31
 */
32
export class SingleStream<T> extends InternalBufferDuplex<T,T> {
120✔
33
    private isFirstChunk = true;
12✔
34

35
    /**
36
     * @constructor
37
     * @param {ObjectDuplexOptions} options - The options for the SingleStream.
38
     */
39
    constructor(options: ObjectDuplexOptions) {
40
        super(options);
12✔
41
    }
42

43
    /**
44
     * A method to write data to the stream, push the chunk to the buffer if its the first chunk, otherwise discard it, and execute the callback.
45
     *
46
     * @param {T} chunk - The data chunk to write to the stream.
47
     * @param {BufferEncoding} encoding - The encoding of the data.
48
     * @param {TransformCallback} callback - The callback function to be executed after writing the data.
49
     * @return {void} This function does not return anything.
50
     */
51
    _write(chunk: T, encoding: BufferEncoding, callback: TransformCallback): void {
52
        if (this.isFirstChunk) {
18✔
53
            this.isFirstChunk = false;
12✔
54
            this.buffer.push(chunk);
12✔
55
            this._flush()
12✔
56
                .then(()=>callback())
12✔
NEW
57
                .catch((e)=>callback(e));
×
58
        } else {
59
            const error = new SingleStreamError();
6✔
60
            this.emit("error", error);
6✔
61
            callback();
6✔
62
        }
63
    }
64
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc