All files / lib/internal/process report.js

100% Statements 122/122
100% Branches 42/42
100% Functions 19/19
100% Lines 122/122

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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 19x 3x 3x 19x 4x 16x 6x 6x 5x 5x 10x 10x 140x 140x 13x 13x 8x 8x 8x 8x 140x 140x 4x 140x 140x 7x 7x 140x 140x 4x 140x 140x 3x 3x 140x 140x 17x 140x 140x 3x 3x 140x 140x 3x 140x 140x 6x 6x 6x 6x 140x 140x 4x 140x 140x 3x 3x 2x 2x 140x 140x 4x 140x 140x 6x 6x 5x 5x 5x 5x 140x 140x 2765x 140x 140x 11963x 11963x 11962x 11962x 11963x 140x 140x 4594x 4594x 7x 7x 7x 7x 7x 4594x 140x 8x 8x 8x 8x 8x 8x 140x 1x 1x 1x 140x 140x 140x 140x 140x  
'use strict';
const {
  ERR_INVALID_ARG_TYPE,
  ERR_SYNTHETIC
} = require('internal/errors').codes;
const {
  validateSignalName,
  validateString,
  validateBoolean,
} = require('internal/validators');
const nr = internalBinding('report');
const {
  JSONParse,
} = primordials;
const report = {
  writeReport(file, err) {
    if (typeof file === 'object' && file !== null) {
      err = file;
      file = undefined;
    } else if (file !== undefined && typeof file !== 'string') {
      throw new ERR_INVALID_ARG_TYPE('file', 'String', file);
    } else if (err === undefined) {
      err = new ERR_SYNTHETIC();
    } else if (err === null || typeof err !== 'object') {
      throw new ERR_INVALID_ARG_TYPE('err', 'Object', err);
    }
 
    return nr.writeReport('JavaScript API', 'API', file, err);
  },
  getReport(err) {
    if (err === undefined)
      err = new ERR_SYNTHETIC();
    else if (err === null || typeof err !== 'object')
      throw new ERR_INVALID_ARG_TYPE('err', 'Object', err);
 
    return JSONParse(nr.getReport(err));
  },
  get directory() {
    return nr.getDirectory();
  },
  set directory(dir) {
    validateString(dir, 'directory');
    nr.setDirectory(dir);
  },
  get filename() {
    return nr.getFilename();
  },
  set filename(name) {
    validateString(name, 'filename');
    nr.setFilename(name);
  },
  get compact() {
    return nr.getCompact();
  },
  set compact(b) {
    validateBoolean(b, 'compact');
    nr.setCompact(b);
  },
  get signal() {
    return nr.getSignal();
  },
  set signal(sig) {
    validateSignalName(sig, 'signal');
    removeSignalHandler();
    addSignalHandler(sig);
    nr.setSignal(sig);
  },
  get reportOnFatalError() {
    return nr.shouldReportOnFatalError();
  },
  set reportOnFatalError(trigger) {
    if (typeof trigger !== 'boolean')
      throw new ERR_INVALID_ARG_TYPE('trigger', 'boolean', trigger);
 
    nr.setReportOnFatalError(trigger);
  },
  get reportOnSignal() {
    return nr.shouldReportOnSignal();
  },
  set reportOnSignal(trigger) {
    if (typeof trigger !== 'boolean')
      throw new ERR_INVALID_ARG_TYPE('trigger', 'boolean', trigger);
 
    nr.setReportOnSignal(trigger);
    removeSignalHandler();
    addSignalHandler();
  },
  get reportOnUncaughtException() {
    return nr.shouldReportOnUncaughtException();
  },
  set reportOnUncaughtException(trigger) {
    if (typeof trigger !== 'boolean')
      throw new ERR_INVALID_ARG_TYPE('trigger', 'boolean', trigger);
 
    nr.setReportOnUncaughtException(trigger);
  }
};
 
function addSignalHandler(sig) {
  if (nr.shouldReportOnSignal()) {
    if (typeof sig !== 'string')
      sig = nr.getSignal();
 
    process.on(sig, signalHandler);
  }
}
 
function removeSignalHandler() {
  const sig = nr.getSignal();
 
  if (sig)
    process.removeListener(sig, signalHandler);
}
 
function signalHandler(sig) {
  nr.writeReport(sig, 'Signal', null, '');
}
 
module.exports = {
  addSignalHandler,
  report
};