All files / lib/internal/process report.js

100% Statements 120/120
100% Branches 32/32
100% Functions 19/19
100% Lines 120/120

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 121149x 149x 149x 149x 149x 149x 149x 149x 149x 149x 149x 149x 149x 149x 149x 149x 149x 19x 3x 3x 19x 4x 16x 6x 6x 6x 6x 10x 10x 149x 149x 13x 13x 8x 8x 8x 8x 149x 149x 4x 149x 149x 9x 9x 149x 149x 4x 149x 149x 3x 3x 149x 149x 20x 149x 149x 3x 3x 149x 149x 3x 149x 149x 6x 6x 6x 6x 149x 149x 4x 149x 149x 3x 3x 3x 149x 149x 4x 149x 149x 6x 6x 6x 6x 6x 149x 149x 8653x 149x 149x 27162x 27162x 27162x 27162x 149x 149x 4830x 4830x 7x 7x 7x 7x 7x 4830x 149x 8x 8x 8x 8x 8x 8x 149x 1x 1x 1x 149x 149x 149x 149x 149x  
'use strict';
const {
  ERR_INVALID_ARG_TYPE,
  ERR_SYNTHETIC
} = require('internal/errors').codes;
const {
  validateBoolean,
  validateObject,
  validateSignalName,
  validateString,
} = 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 {
      validateObject(err, 'err');
    }
 
    return nr.writeReport('JavaScript API', 'API', file, err);
  },
  getReport(err) {
    if (err === undefined)
      err = new ERR_SYNTHETIC();
    else
      validateObject(err, '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) {
    validateBoolean(trigger, 'trigger');
 
    nr.setReportOnFatalError(trigger);
  },
  get reportOnSignal() {
    return nr.shouldReportOnSignal();
  },
  set reportOnSignal(trigger) {
    validateBoolean(trigger, 'trigger');
 
    nr.setReportOnSignal(trigger);
    removeSignalHandler();
    addSignalHandler();
  },
  get reportOnUncaughtException() {
    return nr.shouldReportOnUncaughtException();
  },
  set reportOnUncaughtException(trigger) {
    validateBoolean(trigger, '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
};