All files / lib/internal abort_controller.js

100% Statements 148/148
96.15% Branches 25/26
100% Functions 11/11
100% Lines 148/148

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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 14923x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 5x 5x 5x 3x 3x 5x 5x 5x 5x 5x 23x 276x 276x 276x 276x 23x 23x 23x 1x 1x 1x 23x 23x 276x 276x 276x 23x 23x 3x 3x 3x 3x 23x 23x 28x 28x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 174x 174x 174x 174x 174x 174x 23x 109x 109x 106x 106x 106x 106x 106x 109x 23x 23x 23x 23x 23x 23x 283x 283x 283x 283x 23x 23x 23x 146x 146x 23x 23x 167x 167x 167x 23x 23x 116x 116x 116x 23x 23x 2x 2x 2x 2x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x  
'use strict';
 
// Modeled very closely on the AbortController implementation
// in https://github.com/mysticatea/abort-controller (MIT license)
 
const {
  ObjectAssign,
  ObjectDefineProperties,
  ObjectSetPrototypeOf,
  ObjectDefineProperty,
  Symbol,
  SymbolToStringTag,
  TypeError,
} = primordials;
 
const {
  defineEventHandler,
  EventTarget,
  Event,
  kTrustEvent
} = require('internal/event_target');
const {
  customInspectSymbol,
} = require('internal/util');
const { inspect } = require('internal/util/inspect');
const {
  codes: {
    ERR_INVALID_THIS,
  }
} = require('internal/errors');
 
const kAborted = Symbol('kAborted');
 
function customInspect(self, obj, depth, options) {
  if (depth < 0)
    return self;
 
  const opts = ObjectAssign({}, options, {
    depth: options.depth === null ? null : options.depth - 1
  });
 
  return `${self.constructor.name} ${inspect(obj, opts)}`;
}
 
function validateAbortSignal(obj) {
  if (obj?.[kAborted] === undefined)
    throw new ERR_INVALID_THIS('AbortSignal');
}
 
class AbortSignal extends EventTarget {
  constructor() {
    // eslint-disable-next-line no-restricted-syntax
    throw new TypeError('Illegal constructor');
  }
 
  get aborted() {
    validateAbortSignal(this);
    return !!this[kAborted];
  }
 
  [customInspectSymbol](depth, options) {
    return customInspect(this, {
      aborted: this.aborted
    }, depth, options);
  }
 
  static abort() {
    return createAbortSignal(true);
  }
}
 
ObjectDefineProperties(AbortSignal.prototype, {
  aborted: { enumerable: true }
});
 
ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, {
  writable: false,
  enumerable: false,
  configurable: true,
  value: 'AbortSignal',
});
 
defineEventHandler(AbortSignal.prototype, 'abort');
 
function createAbortSignal(aborted = false) {
  const signal = new EventTarget();
  ObjectSetPrototypeOf(signal, AbortSignal.prototype);
  signal[kAborted] = aborted;
  return signal;
}
 
function abortSignal(signal) {
  if (signal[kAborted]) return;
  signal[kAborted] = true;
  const event = new Event('abort', {
    [kTrustEvent]: true
  });
  signal.dispatchEvent(event);
}
 
// TODO(joyeecheung): V8 snapshot does not support instance member
// initializers for now:
// https://bugs.chromium.org/p/v8/issues/detail?id=10704
const kSignal = Symbol('signal');
 
function validateAbortController(obj) {
  if (obj?.[kSignal] === undefined)
    throw new ERR_INVALID_THIS('AbortController');
}
 
class AbortController {
  constructor() {
    this[kSignal] = createAbortSignal();
  }
 
  get signal() {
    validateAbortController(this);
    return this[kSignal];
  }
 
  abort() {
    validateAbortController(this);
    abortSignal(this[kSignal]);
  }
 
  [customInspectSymbol](depth, options) {
    return customInspect(this, {
      signal: this.signal
    }, depth, options);
  }
}
 
ObjectDefineProperties(AbortController.prototype, {
  signal: { enumerable: true },
  abort: { enumerable: true }
});
 
ObjectDefineProperty(AbortController.prototype, SymbolToStringTag, {
  writable: false,
  enumerable: false,
  configurable: true,
  value: 'AbortController',
});
 
module.exports = {
  AbortController,
  AbortSignal,
};