All files / lib/internal/cluster worker.js

100% Statements 57/57
100% Branches 14/14
100% Functions 5/5
100% Lines 57/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 5883x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 517x 517x 517x 516x 516x 516x 517x 517x 516x 516x 516x 517x 517x 517x 517x 514x 514x 2051x 514x 514x 1960x 514x 514x 517x 83x 83x 83x 83x 83x 15x 83x 83x 83x 3957x 83x 83x 83x 261x 83x 83x 83x 10072x 83x  
'use strict';
 
const {
  ObjectSetPrototypeOf,
  ReflectApply,
} = primordials;
 
const EventEmitter = require('events');
 
const { kEmptyObject } = require('internal/util');
 
module.exports = Worker;
 
// Common Worker implementation shared between the cluster primary and workers.
function Worker(options) {
  if (!(this instanceof Worker))
    return new Worker(options);
 
  ReflectApply(EventEmitter, this, []);
 
  if (options === null || typeof options !== 'object')
    options = kEmptyObject;
 
  this.exitedAfterDisconnect = undefined;
 
  this.state = options.state || 'none';
  this.id = options.id | 0;
 
  if (options.process) {
    this.process = options.process;
    this.process.on('error', (code, signal) =>
      this.emit('error', code, signal)
    );
    this.process.on('message', (message, handle) =>
      this.emit('message', message, handle)
    );
  }
}
 
ObjectSetPrototypeOf(Worker.prototype, EventEmitter.prototype);
ObjectSetPrototypeOf(Worker, EventEmitter);
 
Worker.prototype.kill = function() {
  ReflectApply(this.destroy, this, arguments);
};
 
Worker.prototype.send = function() {
  return ReflectApply(this.process.send, this.process, arguments);
};
 
Worker.prototype.isDead = function() {
  return this.process.exitCode != null || this.process.signalCode != null;
};
 
Worker.prototype.isConnected = function() {
  return this.process.connected;
};