All files / lib/internal/cluster shared_handle.js

100% Statements 45/45
100% Branches 16/16
100% Functions 3/3
100% Lines 45/45

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 4674x 74x 74x 74x 74x 74x 74x 74x 25x 25x 25x 25x 25x 25x 25x 25x 25x 7x 7x 25x 25x 25x 23x 23x 25x 74x 74x 41x 41x 41x 74x 74x 74x 57x 57x 38x 38x 38x 38x 57x 22x 22x 22x 22x 74x  
'use strict';
const { SafeMap } = primordials;
const assert = require('internal/assert');
const dgram = require('internal/dgram');
const net = require('net');
 
module.exports = SharedHandle;
 
function SharedHandle(key, address, { port, addressType, fd, flags }) {
  this.key = key;
  this.workers = new SafeMap();
  this.handle = null;
  this.errno = 0;
 
  let rval;
  if (addressType === 'udp4' || addressType === 'udp6')
    rval = dgram._createSocketHandle(address, port, addressType, fd, flags);
  else
    rval = net._createServerHandle(address, port, addressType, fd, flags);
 
  if (typeof rval === 'number')
    this.errno = rval;
  else
    this.handle = rval;
}
 
SharedHandle.prototype.add = function(worker, send) {
  assert(!this.workers.has(worker.id));
  this.workers.set(worker.id, worker);
  send(this.errno, null, this.handle);
};
 
SharedHandle.prototype.remove = function(worker) {
  if (!this.workers.has(worker.id))
    return false;
 
  this.workers.delete(worker.id);
 
  if (this.workers.size !== 0)
    return false;
 
  this.handle.close();
  this.handle = null;
  return true;
};