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

Inist-CNRS / ezs / 17267738980

27 Aug 2025 01:12PM UTC coverage: 95.388% (-0.1%) from 95.519%
17267738980

Pull #462

github

web-flow
Merge 22c6668dc into 56ca7b899
Pull Request #462: feat: 🎸 add [detach]

2220 of 2404 branches covered (92.35%)

Branch coverage included in aggregate %.

34 of 38 new or added lines in 6 files covered. (89.47%)

4 existing lines in 2 files now uncovered.

4584 of 4729 relevant lines covered (96.93%)

80855.68 hits per line

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

93.33
/packages/core/src/statements/detach.js
1
import debug from 'debug';
2
import path from 'path';
3
import { Worker } from 'worker_threads';
4
import filedirname from 'filedirname';
5
import JSONezs from '../json.js';
6

7
const [, dirname] = filedirname();
1,074✔
8
/**
9
 * Delegate processing to an external pipeline.
10
 *
11
 * > **Note**: works like {@link spawn}, but each chunk share the same external pipeline.
12
 *
13
 * @name detach
14
 * @param {String} [file] the external pipeline is described in a file
15
 * @param {String} [script] the external pipeline is described in a string of characters
16
 * @param {String} [commands] the external pipeline is described in a object
17
 * @param {String} [command] the external pipeline is described in a URL-like command
18
 * @param {String} [logger] A dedicaded pipeline described in a file to trap or log errors
19
 * @param {String} [encoder=pack] The statement to encode each chunk to a string
20
 * @param {String} [decoder=unpack] The statement to decode each chunk as a string
21
 * @returns {Object}
22
 */
23
export default function detach(data, feed) {
24
    const { ezs } = this;
300,990✔
25
    if (this.isFirst()) {
300,990✔
26
        this.input = ezs.createStream(ezs.objectMode());
114✔
27
        ;
28
        const workerFile = path.resolve(dirname, './detach-worker.js');
114✔
29
        const workerData = {
114✔
30
            file: this.getParam('file'),
31
            script: this.getParam('script'),
32
            commandString: JSONezs.stringify(this.getParam('command')),
33
            commandsString: JSONezs.stringify(this.getParam('commands')),
34
            prepend: this.getParam('prepend'),
35
            append: this.getParam('append'),
36
            encoder: this.getParam('encoder', 'pack'),
37
            decoder: this.getParam('decoder', 'unpack'),
38
            environment: this.getEnv(),
39
            loggerParam: this.getParam('logger'),
40
            settings: ezs.settings,
41
            plugins: ezs.useFiles(),
42
        };
43
        this.worker = new Worker(workerFile, {
114✔
44
            workerData,
45
            stdin: true,
46
            stdout: true,
47
        });
48
        this.worker.on('exit', (code) => {
114✔
49
            if (code !== 0) feed.stop(new Error(`Worker stopped with exit code ${code}`));
114✔
50
        });
51
        this.whenReady =  new Promise((resolve, reject) => {
114✔
52
            this.worker.once('online', resolve);
114✔
53
            this.worker.once('error', (err) => {
114✔
NEW
54
                debug('ezs:error')('Worker crash', this.ezs.serializeError(err));
×
55
                // exit event will stop the feed;
NEW
56
                reject(err);
×
57
            });
58
        });
59
        this.input
114✔
60
            .pipe(ezs.createCommand(workerData.encoder))
61
            .pipe(this.worker.stdin);
62
        const output = this.worker.stdout
114✔
63
            .pipe(ezs.createCommand(workerData.decoder));
64
        this.whenFinish = feed.flow(output, { autoclose: true, emptyclose: false });
114✔
65
    }
66
    if (this.isLast()) {
300,990✔
67
        debug('ezs:debug')(`${this.getIndex()} chunks have been detached`);
114✔
68
        this.whenFinish.finally(() => {
114✔
69
            feed.close();
108✔
70
            return this.worker.terminate();
108✔
71
        });
72
        return this.input.end();
114✔
73
    }
74
    this.whenReady.then(() => ezs.writeTo(this.input, data, () => feed.end()));
300,876✔
75
}
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