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

discoveryjs / jora-cli / 16999837534

15 Aug 2025 09:33PM UTC coverage: 86.172% (-3.2%) from 89.399%
16999837534

push

github

lahmatiy
Update jsonxl

- Fixed an edge case for signed numbers in the range ±[MAX_SAFE_INTEGER/2 … MAX_SAFE_INTEGER].
- Removed the limitation on total encoded string length exceeding the maximum string length (~500 MB for V8)

473 of 603 branches covered (78.44%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

9 existing lines in 1 file now uncovered.

898 of 988 relevant lines covered (90.89%)

700.86 hits per line

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

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

39✔
8
const now = typeof performace !== 'undefined' && typeof performance.now === 'function' ? performance.now : Date.now;
39!
9
const stringBytes = typeof Buffer === 'function' && typeof Buffer.byteLength === 'function'
39✔
10
    ? Buffer.byteLength
39✔
11
    : (str) => str.length; // incorrect but fast fallback
39!
12

39✔
13
function* createChunkIterator(data, chunkSize = 64 * 1024) {
3✔
14
    for (let offset = 0; offset < data.length; offset += chunkSize) {
3✔
15
        yield data.subarray(offset, offset + chunkSize);
2✔
16
    }
2✔
17
}
3✔
18

39✔
19
async function writeIntoStream(stream, data, options, setStageProgress = () => {}) {
34✔
20
    const { autoEncoding, encoding } = options;
34✔
21
    let payload;
34✔
22
    let totalSize;
34✔
23

34✔
24
    setStageProgress('output-encoding', { autoEncoding, encoding });
34✔
25

34✔
26
    switch (encoding) {
34✔
27
        case 'jsonxl': {
34✔
28
            const startTime = now();
3✔
29

3✔
30
            setStageProgress('encoding', { encoding });
3✔
31
            const jsonxl = encode(data);
3✔
32
            setStageProgress('encoded', {
3✔
33
                encoding,
3✔
34
                size: jsonxl.byteLength,
3✔
35
                time: now() - startTime
3✔
36
            });
3✔
37

3✔
38
            payload = createChunkIterator(jsonxl, /* 1MB */ 1024 * 1024);
3✔
39
            totalSize = jsonxl.byteLength;
3✔
40

3✔
41
            break;
3✔
42
        }
3✔
43

34✔
44
        case 'json': {
34✔
45
            payload = stringifyChunked(data, null, options.pretty);
31✔
46
            break;
31✔
47
        }
31✔
48

34✔
49
        default:
34!
UNCOV
50
            throw new Error('Unknown output encoding ' + encoding);
×
51
    }
34✔
52

34✔
53
    const streamStartTime = now();
34✔
54
    let writtenSize = 0;
34✔
55

34✔
56
    if (stream) {
34✔
57
        const isStdStream = stream.isTTY;
32✔
58
        const endNewline = encoding !== 'jsonxl';
32✔
59
        const applyColorize = encoding === 'json' && options.color;
32✔
60
        const buffer = [];
32✔
61

32✔
62
        await pipeline(async function* () {
32✔
63
            if (isStdStream) {
32!
UNCOV
64
                setStageProgress('start-stdout');
×
UNCOV
65
            }
×
66

32✔
67
            for await (const chunk of payload) {
32✔
68
                writtenSize += typeof chunk === 'string'
32✔
69
                    ? stringBytes(chunk)
32✔
70
                    : chunk.byteLength;
32✔
71

32✔
72
                if (!isStdStream) {
32✔
73
                    setStageProgress('writing', {
32✔
74
                        done: false,
32✔
75
                        elapsed: now() - streamStartTime,
32✔
76
                        units: 'bytes',
32✔
77
                        completed: writtenSize,
32✔
78
                        total: totalSize
32✔
79
                    });
32✔
80
                }
32✔
81

32✔
82
                if (applyColorize) {
32✔
83
                    buffer.push(chunk);
14✔
84
                } else {
32✔
85
                    yield chunk;
18✔
86
                }
18✔
87
            }
32✔
88

32✔
89
            if (applyColorize) {
32✔
90
                yield colorize(buffer.join(''));
14✔
91
            }
14✔
92

32✔
93
            if (isStdStream && endNewline) {
32!
94
                yield'\n';
×
UNCOV
95
            }
×
96

32✔
97
            if (isStdStream) {
32!
UNCOV
98
                setStageProgress('finish-stdout', { newline: !endNewline });
×
UNCOV
99
            }
×
100
        }, stream, { end: !isStdStream });
32✔
101
    } else {
34✔
102
        // dry run
2✔
103
        switch (encoding) {
2✔
104
            case 'jsonxl':
2✔
105
                writtenSize = totalSize;
1✔
106
                break;
1✔
107

2✔
108
            case 'json':
2✔
109
                writtenSize = stringifyInfo(data, null, options.pretty).bytes;
1✔
110
                break;
1✔
111

2✔
112
            default:
2!
UNCOV
113
                throw new Error('Unknown output encoding ' + encoding);
×
114
        }
2✔
115
    }
2✔
116

34✔
117
    setStageProgress('writing', {
34✔
118
        done: true,
34✔
119
        elapsed: now() - streamStartTime,
34✔
120
        units: 'bytes',
34✔
121
        completed: writtenSize,
34✔
122
        total: totalSize
34✔
123
    });
34✔
124
}
34✔
125

39✔
126
function writeIntoStdout(data, options, setStageProgress) {
33✔
127
    const { dryRun } = options;
33✔
128

33✔
129
    setStageProgress('start-writing', { filepath: '<stdout>', dryRun });
33✔
130

33✔
131
    return writeIntoStream(
33✔
132
        !dryRun ? process.stdout : null,
33✔
133
        data,
33✔
134
        options,
33✔
135
        setStageProgress
33✔
136
    );
33✔
137
}
33✔
138

39✔
139
function writeIntoFile(filepath, data, options, setStageProgress) {
1✔
140
    const { dryRun } = options;
1✔
141

1✔
142
    setStageProgress('start-writing', { filepath, dryRun });
1✔
143

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

1✔
148
    const stream = !dryRun
1✔
149
        ? fs.createWriteStream(filepath, { highWaterMark: 512 * 1024 })
1✔
150
        : null;
1!
151

1✔
152
    return writeIntoStream(
1✔
153
        stream,
1✔
154
        data,
1✔
155
        options,
1✔
156
        setStageProgress
1✔
157
    );
1✔
158
}
1✔
159

39✔
160
export function writeToDestination(data, options, setStageProgress) {
39✔
161
    const { outputPath } = options;
34✔
162

34✔
163
    return outputPath
34✔
164
        ? writeIntoFile(outputPath, data, options, setStageProgress)
34✔
165
        : writeIntoStdout(data, options, setStageProgress);
34✔
166
}
34✔
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