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

discoveryjs / jora-cli / 17013952060

16 Aug 2025 11:14PM UTC coverage: 86.015% (+2.1%) from 83.867%
17013952060

push

github

lahmatiy
Update Node.js versions

533 of 671 branches covered (79.43%)

Branch coverage included in aggregate %.

1060 of 1181 relevant lines covered (89.75%)

738.69 hits per line

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

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

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

49✔
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

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

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

44✔
32
    switch (encoding) {
44✔
33
        case 'jsonxl': {
44✔
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

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

44✔
55
        default:
44!
56
            throw new Error('Unknown output encoding ' + encoding);
×
57
    }
44✔
58

44✔
59
    const streamStartTime = now();
44✔
60
    let writtenSize = 0;
44✔
61

44✔
62
    if (stream) {
44✔
63
        const isStdStream = stream.isTTY;
42✔
64
        const endNewline = encoding !== 'jsonxl';
42✔
65
        const applyColorize = encoding === 'json' && options.color;
42✔
66
        const buffer = [];
42✔
67
        const pipelineDest = compressionTransform
42✔
68
            ? [compressionTransform(), stream]
42✔
69
            : [stream];
42✔
70

42✔
71
        await pipeline(async function* () {
42✔
72
            if (isStdStream) {
42!
73
                setStageProgress('start-stdout');
×
74
            }
×
75

42✔
76
            for await (const chunk of payload) {
42✔
77
                writtenSize += typeof chunk === 'string'
42✔
78
                    ? stringBytes(chunk)
42✔
79
                    : chunk.byteLength;
42✔
80

42✔
81
                if (!isStdStream) {
42✔
82
                    setStageProgress('writing', {
42✔
83
                        done: false,
42✔
84
                        elapsed: now() - streamStartTime,
42✔
85
                        units: 'bytes',
42✔
86
                        completed: writtenSize,
42✔
87
                        total: totalSize
42✔
88
                    });
42✔
89
                }
42✔
90

42✔
91
                if (applyColorize) {
42✔
92
                    buffer.push(chunk);
14✔
93
                } else {
42✔
94
                    yield chunk;
28✔
95
                }
28✔
96
            }
42✔
97

42✔
98
            if (applyColorize) {
42✔
99
                yield colorize(buffer.join(''));
14✔
100
            }
14✔
101
        }, ...pipelineDest, { end: !isStdStream });
42✔
102

42✔
103
        if (isStdStream) {
42!
104
            if (endNewline) {
×
105
                stream.write('\n');
×
106
            }
×
107

×
108
            setStageProgress('finish-stdout', { newline: !endNewline });
×
109
        }
×
110
    } else {
44✔
111
        // dry run
2✔
112
        switch (encoding) {
2✔
113
            case 'jsonxl':
2✔
114
                writtenSize = totalSize;
1✔
115
                break;
1✔
116

2✔
117
            case 'json':
2✔
118
                writtenSize = stringifyInfo(data, null, options.pretty).bytes;
1✔
119
                break;
1✔
120

2✔
121
            default:
2!
122
                throw new Error('Unknown output encoding ' + encoding);
×
123
        }
2✔
124
    }
2✔
125

44✔
126
    setStageProgress('writing', {
44✔
127
        done: true,
44✔
128
        elapsed: now() - streamStartTime,
44✔
129
        units: 'bytes',
44✔
130
        completed: writtenSize,
44✔
131
        total: totalSize
44✔
132
    });
44✔
133
}
44✔
134

49✔
135
function writeIntoStdout(data, options, setStageProgress) {
43✔
136
    const { dryRun } = options;
43✔
137

43✔
138
    setStageProgress('start-writing', { filepath: '<stdout>', dryRun });
43✔
139

43✔
140
    return writeIntoStream(
43✔
141
        !dryRun ? process.stdout : null,
43✔
142
        data,
43✔
143
        options,
43✔
144
        setStageProgress
43✔
145
    );
43✔
146
}
43✔
147

49✔
148
function writeIntoFile(filepath, data, options, setStageProgress) {
1✔
149
    const { dryRun } = options;
1✔
150

1✔
151
    setStageProgress('start-writing', { filepath, dryRun });
1✔
152

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

1✔
157
    const stream = !dryRun
1✔
158
        ? fs.createWriteStream(filepath, { highWaterMark: 512 * 1024 })
1✔
159
        : null;
1!
160

1✔
161
    return writeIntoStream(
1✔
162
        stream,
1✔
163
        data,
1✔
164
        options,
1✔
165
        setStageProgress
1✔
166
    );
1✔
167
}
1✔
168

49✔
169
export function writeToDestination(data, options, setStageProgress) {
49✔
170
    const { outputPath } = options;
44✔
171

44✔
172
    return outputPath
44✔
173
        ? writeIntoFile(outputPath, data, options, setStageProgress)
44✔
174
        : writeIntoStdout(data, options, setStageProgress);
44✔
175
}
44✔
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

© 2025 Coveralls, Inc