All files / lib/internal dgram.js

100% Statements 92/92
100% Branches 27/27
100% Functions 4/4
100% Lines 92/92

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 532x 532x 532x 110x 110x 55x 55x 55x 110x 248x 248x 237x 133x 133x 237x 237x 248x 11x 11x 238x 238x 210x 210x 210x 210x 210x 28x 31x 19x 19x 19x 19x 19x 19x 19x 19x 9x 9x 248x 110x 110x 24x 24x 24x 24x 24x 4x 4x 2x 2x 2x 2x 24x 19x 19x 24x 24x 4x 4x 4x 20x 20x 24x 110x 110x 110x 110x 110x 110x 110x  
'use strict';
 
const {
  FunctionPrototypeBind,
  Symbol,
} = primordials;
 
const { codes } = require('internal/errors');
const { UDP } = internalBinding('udp_wrap');
const { guessHandleType } = internalBinding('util');
const {
  isInt32,
  validateFunction,
} = require('internal/validators');
const { UV_EINVAL } = internalBinding('uv');
const {
  ERR_SOCKET_BAD_TYPE,
} = codes;
const kStateSymbol = Symbol('state symbol');
let dns;  // Lazy load for startup performance.
 
 
function lookup4(lookup, address, callback) {
  return lookup(address || '127.0.0.1', 4, callback);
}
 
 
function lookup6(lookup, address, callback) {
  return lookup(address || '::1', 6, callback);
}
 
function newHandle(type, lookup) {
  if (lookup === undefined) {
    if (dns === undefined) {
      dns = require('dns');
    }
 
    lookup = dns.lookup;
  } else {
    validateFunction(lookup, 'lookup');
  }
 
  if (type === 'udp4') {
    const handle = new UDP();
 
    handle.lookup = FunctionPrototypeBind(lookup4, handle, lookup);
    return handle;
  }
 
  if (type === 'udp6') {
    const handle = new UDP();
 
    handle.lookup = FunctionPrototypeBind(lookup6, handle, lookup);
    handle.bind = handle.bind6;
    handle.connect = handle.connect6;
    handle.send = handle.send6;
    return handle;
  }
 
  throw new ERR_SOCKET_BAD_TYPE();
}
 
 
function _createSocketHandle(address, port, addressType, fd, flags) {
  const handle = newHandle(addressType);
  let err;
 
  if (isInt32(fd) && fd > 0) {
    const type = guessHandleType(fd);
    if (type !== 'UDP') {
      err = UV_EINVAL;
    } else {
      err = handle.open(fd);
    }
  } else if (port || address) {
    err = handle.bind(address, port || 0, flags);
  }
 
  if (err) {
    handle.close();
    return err;
  }
 
  return handle;
}
 
 
module.exports = {
  kStateSymbol,
  _createSocketHandle,
  newHandle
};