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

discoveryjs / jora-cli / 25293405750

03 May 2026 11:04PM UTC coverage: 86.354% (+0.3%) from 86.015%
25293405750

push

github

lahmatiy
2.2.0

541 of 676 branches covered (80.03%)

Branch coverage included in aggregate %.

1079 of 1200 relevant lines covered (89.92%)

799.6 hits per line

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

90.54
/src/write.js
1
import fs from 'node:fs';
54✔
2
import { pipeline } from 'node:stream/promises';
54✔
3
import { createGzip, createDeflate } from 'node:zlib';
54✔
4
import { encode } from './tmp/jsonxl-snapshot9.js';
54✔
5
import { colorize } from './colorize.js';
54✔
6
import { stringifyChunked, stringifyInfo } from '@discoveryjs/json-ext';
54✔
7
import * as clap from 'clap';
54✔
8

54✔
9
const now = typeof performance !== 'undefined' && typeof performance.now === 'function' ? performance.now.bind(performance) : Date.now;
54!
10
const stringBytes = typeof Buffer === 'function' && typeof Buffer.byteLength === 'function'
54✔
11
    ? Buffer.byteLength
54✔
12
    : (str) => str.length; // incorrect but fast fallback
54!
13
const compressionTransforms = {
54✔
14
    gzip: createGzip,
54✔
15
    deflate: createDeflate
54✔
16
};
54✔
17

54✔
18
function* createChunkIterator(data, chunkSize = 64 * 1024) {
5✔
19
    for (let offset = 0; offset < data.length; offset += chunkSize) {
5✔
20
        yield Buffer.from(data.subarray(offset, offset + chunkSize));
4✔
21
    }
4✔
22
}
5✔
23

54✔
24
async function writeIntoStream(stream, data, options, setStageProgress = () => {}) {
49✔
25
    const { autoEncoding, encoding, compression } = options;
49✔
26
    const compressionTransform = compressionTransforms[compression];
49✔
27
    let payload;
49✔
28
    let totalSize;
49✔
29

49✔
30
    setStageProgress('output-encoding', { autoEncoding, encoding, compression });
49✔
31

49✔
32
    switch (encoding) {
49✔
33
        case 'jsonxl': {
49✔
34
            const startTime = now();
5✔
35

5✔
36
            setStageProgress('encoding', { encoding });
5✔
37
            const jsonxl = encode(data);
5✔
38
            setStageProgress('encoded', {
5✔
39
                encoding,
5✔
40
                size: jsonxl.byteLength,
5✔
41
                time: now() - startTime
5✔
42
            });
5✔
43

5✔
44
            payload = createChunkIterator(jsonxl, /* 1MB */ 1024 * 1024);
5✔
45
            totalSize = jsonxl.byteLength;
5✔
46

5✔
47
            break;
5✔
48
        }
5✔
49

49✔
50
        case 'json': {
49✔
51
            payload = stringifyChunked(data, null, options.pretty);
41✔
52
            break;
41✔
53
        }
41✔
54

49✔
55
        case 'jsonl': {
49✔
56
            payload = stringifyChunked(data, {
3✔
57
                space: options.pretty,
3✔
58
                mode: 'jsonl'
3✔
59
            });
3✔
60
            break;
3✔
61
        }
3✔
62

49✔
63
        default:
49!
64
            throw new Error('Unknown output encoding ' + encoding);
×
65
    }
49✔
66

49✔
67
    const streamStartTime = now();
49✔
68
    let writtenSize = 0;
49✔
69

49✔
70
    if (stream) {
49✔
71
        const isStdStream = stream.isTTY;
46✔
72
        const endNewline = encoding !== 'jsonxl';
46✔
73
        const applyColorize = (encoding === 'json' || encoding === 'jsonl') && options.color;
46✔
74
        const buffer = [];
46✔
75
        const pipelineDest = compressionTransform
46✔
76
            ? [compressionTransform(), stream]
46✔
77
            : [stream];
46✔
78

46✔
79
        await pipeline(async function* () {
46✔
80
            if (isStdStream) {
46!
81
                setStageProgress('start-stdout');
×
82
            }
×
83

46✔
84
            for await (const chunk of payload) {
46✔
85
                writtenSize += typeof chunk === 'string'
46✔
86
                    ? stringBytes(chunk)
46✔
87
                    : chunk.byteLength;
46✔
88

46✔
89
                if (!isStdStream) {
46✔
90
                    setStageProgress('writing', {
46✔
91
                        done: false,
46✔
92
                        elapsed: now() - streamStartTime,
46✔
93
                        units: 'bytes',
46✔
94
                        completed: writtenSize,
46✔
95
                        total: totalSize
46✔
96
                    });
46✔
97
                }
46✔
98

46✔
99
                if (applyColorize) {
46✔
100
                    buffer.push(chunk);
14✔
101
                } else {
46✔
102
                    yield chunk;
32✔
103
                }
32✔
104
            }
46✔
105

46✔
106
            if (applyColorize) {
46✔
107
                yield colorize(buffer.join(''));
14✔
108
            }
14✔
109
        }, ...pipelineDest, { end: !isStdStream });
46✔
110

46✔
111
        if (isStdStream) {
46!
112
            if (endNewline) {
×
113
                stream.write('\n');
×
114
            }
×
115

×
116
            setStageProgress('finish-stdout', { newline: !endNewline });
×
117
        }
×
118
    } else {
49✔
119
        // dry run
3✔
120
        switch (encoding) {
3✔
121
            case 'jsonxl':
3✔
122
                writtenSize = totalSize;
1✔
123
                break;
1✔
124

3✔
125
            case 'json':
3✔
126
            case 'jsonl':
3✔
127
                writtenSize = stringifyInfo(data, { space: options.pretty, mode: encoding }).bytes;
2✔
128
                break;
2✔
129

3✔
130
            default:
3!
131
                throw new Error('Unknown output encoding ' + encoding);
×
132
        }
3✔
133
    }
3✔
134

49✔
135
    setStageProgress('writing', {
49✔
136
        done: true,
49✔
137
        elapsed: now() - streamStartTime,
49✔
138
        units: 'bytes',
49✔
139
        completed: writtenSize,
49✔
140
        total: totalSize
49✔
141
    });
49✔
142
}
49✔
143

54✔
144
function writeIntoStdout(data, options, setStageProgress) {
48✔
145
    const { dryRun } = options;
48✔
146

48✔
147
    setStageProgress('start-writing', { filepath: '<stdout>', dryRun });
48✔
148

48✔
149
    return writeIntoStream(
48✔
150
        !dryRun ? process.stdout : null,
48✔
151
        data,
48✔
152
        options,
48✔
153
        setStageProgress
48✔
154
    );
48✔
155
}
48✔
156

54✔
157
function writeIntoFile(filepath, data, options, setStageProgress) {
1✔
158
    const { dryRun } = options;
1✔
159

1✔
160
    setStageProgress('start-writing', { filepath, dryRun });
1✔
161

1✔
162
    if (!dryRun && fs.existsSync(filepath) && !options.forceRewrite) {
1!
163
        throw new clap.Error('Output file already exists. Use the --force option or the -f flag to overwrite');
×
164
    }
×
165

1✔
166
    const stream = !dryRun
1✔
167
        ? fs.createWriteStream(filepath, { highWaterMark: 512 * 1024 })
1✔
168
        : null;
1!
169

1✔
170
    return writeIntoStream(
1✔
171
        stream,
1✔
172
        data,
1✔
173
        options,
1✔
174
        setStageProgress
1✔
175
    );
1✔
176
}
1✔
177

54✔
178
export function writeToDestination(data, options, setStageProgress) {
54✔
179
    const { outputPath } = options;
49✔
180

49✔
181
    return outputPath
49✔
182
        ? writeIntoFile(outputPath, data, options, setStageProgress)
49✔
183
        : writeIntoStdout(data, options, setStageProgress);
49✔
184
}
49✔
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