All files / tao-utils/src Transceiver.js

100% Statements 45/45
100% Branches 38/38
100% Functions 18/18
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 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336          2x   2x     7x     2x     20x         20x                                                                                                                                                                           30x       30x 30x     30x         4x           26x     26x       17x       16x       26x       14x   26x 26x 26x                           30x 16x                                                               30x 20x 20x 20x   20x 20x 2x 2x     20x               27x         8x   19x 19x 7x   12x                               30x 2x                         5x                                 6x                         11x                       2x                             2x                               2x            
import { AppCtx, Network, INTERCEPT, ERROR } from '@tao.js/core';
 
/** @typedef {import('./wire').NetworkSurface} NetworkSurface */
 
// for backwards compatibility
const MAX_SAFE_INTEGER = Math.pow(2, 53) - 1;
 
let transceiverId = 0;
function newTransceiverId() {
  // Stryker disable next-line ArithmeticOperator,UpdateOperator: counter monotonicity/modulo wraparound at MAX_SAFE_INTEGER is untestable without exhausting the counter
  return (transceiverId = ++transceiverId % MAX_SAFE_INTEGER);
}
 
let signalId = 0;
// Stryker disable all: counter monotonicity/modulo wraparound at MAX_SAFE_INTEGER is untestable without exhausting the counter, and the returned id is not observed by tests
function newSignalId() {
  return (signalId = ++signalId % MAX_SAFE_INTEGER);
}
// Stryker restore all
 
function transceiverControl(transceiverId, resolve, reject) {
  return { transceiverId, signal: { id: newSignalId(), resolve, reject } };
}
 
/**
 * A trigram matcher in short (`t`/`a`/`o`) or long (`term`/`action`/`orient`)
 * keys; omitted parts are wildcards.
 *
 * @typedef {Object} Trigram
 * @property {string} [t] - term (short key)
 * @property {string} [term] - term (long key)
 * @property {string} [a] - action (short key)
 * @property {string} [action] - action (long key)
 * @property {string} [o] - orient (short key)
 * @property {string} [orient] - orient (long key)
 */
 
/**
 * A TAO signal handler: called with the concrete trigram handled and the
 * signal's datagram(s); returning an `AppCtx` chains it (see `@tao.js/core`).
 *
 * @callback SignalHandler
 * @param {Object} tao - the concrete `{ t, a, o }` trigram handled
 * @param {*} data - the signal's datagram(s)
 * @returns {(AppCtx|*)}
 */
 
/**
 * Like a Transponder, a Transceiver converts Signals on a Network to Promises.
 * Unlike a Transponder, a Transceiver allows the handlers attached to it to control
 * the behavior of the Promise.
 *
 * If a Handler returns an AppCtx, then it will be chained like a standard handler.
 *
 * If a Handler returns something other than an AppCtx it will behave as the following:
 * - returning truthy value from an InterceptHandler will REJECT the Promise
 * - returning any value not `null` or `undefined` from an AsyncHandler or InlineHandler
 *   it will be used to RESOLVE the Promise
 * - throwing an Error in any Handler will REJECT the Promise
 *
 * A signal to a Promise can only happen once, so the first thing that happens
 * will conclude the Promise despite other handlers that may be continue to be
 * called depending on how you set up your Handlers
 *
 * Fully envelope-native (see ENVELOPE-SPEC.md §12): entries go through
 * `enter` with the transceiver's cascade tag; chained AppCons of matching
 * cascades are mirrored onto the private signals network by an `onForward`
 * decoration (signals dispatch starts before main dispatch of the same
 * hop); signal-handler returns settle the Promise through the signals
 * network's `onReturn` settlement hook; and chains from signal handlers
 * continue the cascade envelope through the main network's hop engine.
 *
 * @export
 * @class Transceiver
 */
export default class Transceiver {
  /**
   * Creates an instance of Transceiver.
   *
   * Surface resolution follows the utils convention
   * (`typeof network.enter === 'function' ? network : network._network`):
   * pass anything exposing the envelope gate directly (a `Network`, or a
   * `Channel` — entries then keep channel affinity), or a Kernel-shaped
   * wrapper exposing `_network`. The resolved surface must support `enter`
   * and `decorate`.
   *
   * Two decorations are registered: an `onForward` mirror on the underlying
   * shared network (`surface._network || surface` — chained hops are
   * observed there even when entries go through a Channel surface),
   * self-filtered on `envelope.cascade.transceiverId`, mirroring matching
   * chained AppCons onto the private signals network; and an `onReturn`
   * settlement decoration on the signals network mapping signal-handler
   * returns onto the Promise (see {@linkcode setAppCtx}).
   *
   * @param {NetworkSurface} network - the surface to wrap with a `Transceiver`
   * @param {(string|function(number): (string|number))} [id] - pass either a desired Transceiver ID value as a `string` or a `function` that will be used to generate a Transceiver ID
   *        the `function` will be called with a new Transceiver ID integer value to help ensure uniqueness
   * @param {number} [timeoutMs=0] - a timeout to be used when awaiting `Promises`
   *        - `0` - no timeout will be used
   *        - `> 0` - timeout will be used to `reject` the `Promise` if it is not signaled in time
   * @param {PromiseConstructor} [promise=Promise] - a `Promise` constructor to be used when creating promises
   *        by a signalling method.
   * @throws {Error} when the resolved surface lacks envelope support
   *         (`enter` + `decorate`) - upgrade `@tao.js/core`
   * @memberof Transceiver
   */
  constructor(network, id, timeoutMs = 0, promise = Promise) {
    this._transceiverId =
      typeof id === 'function'
        ? id(newTransceiverId())
        : id || newTransceiverId();
    this._signals = new Network();
    this._surface = /** @type {Network} */ (
      typeof network.enter === 'function' ? network : network._network
    );
    if (
      !this._surface ||
      typeof this._surface.enter !== 'function' ||
      typeof this._surface.decorate !== 'function'
    ) {
      throw new Error(
        'Transceiver requires a @tao.js/core version with envelope support - upgrade @tao.js/core',
      );
    }
    // mirror from the shared network (a Channel surface delegates entries to
    // it); the cascade tag filters this transceiver's cascades either way
    this._network = /** @type {Network} */ (
      /** @type {NetworkSurface} */ (this._surface)._network || this._surface
    );
    this._undecorateMirror = this._network.decorate({
      // Stryker disable next-line StringLiteral: decoration name is a diagnostic label with no observable behavior
      name: `transceiver:${this._transceiverId}`,
      onForward: (nextAc, envelope, meta) => {
        if (envelope.cascade.transceiverId === this._transceiverId) {
          // same-hop dispatch (envelope verbatim): signals dispatch starts
          // before core dispatches the hop on the main network; chains from
          // signal handlers continue the cascade
          this._signals.mirror(nextAc, envelope, meta.forward);
        }
      },
    });
    this._undecorateSettle = this._signals.decorate({
      // Stryker disable next-line StringLiteral: decoration name is a diagnostic label with no observable behavior
      name: `transceiver-settle:${this._transceiverId}`,
      onReturn: (phase, value, ac, envelope) =>
        this._settle(phase, value, envelope.cascade),
    });
    this._timeoutMs = timeoutMs;
    this._promise = promise;
    this._cloneWithId = typeof id === 'function' ? id : undefined;
  }
 
  /**
   * Builds an AppCtx from a trigram + datagram (long-form keys win) and
   * signals it via {@linkcode setAppCtx}.
   *
   * @param {Trigram} trigram
   * @param {*} data - datagram(s) for the signal
   * @param {Object} [opts] - as {@linkcode setAppCtx}
   * @param {(Object|null)} [opts.chain] - prior chain state to continue
   * @returns {Promise<*>} as {@linkcode setAppCtx}
   * @memberof Transceiver
   */
  setCtx = ({ t, term, a, action, o, orient }, data, opts) => {
    return this.setAppCtx(
      new AppCtx(term || t, action || a, orient || o, data),
      opts,
    );
  };
 
  /**
   * Enters the AppCtx on the wrapped surface with this Transceiver's
   * `{ transceiverId, signal }` cascade tag — the cascade (control) object
   * is one shared reference for the whole cascade, so descendants of the
   * entry are mirrored onto the signals network wherever the chain goes.
   *
   * Settlement semantics: signal handlers observe the mirrored descendants
   * (never the entry hop itself) and their non-AppCtx returns settle the
   * Promise through the signals network's `onReturn` hook — phase mapping:
   * INTERCEPT (truthy return) and ERROR (thrown error / rejected async)
   * reject; ASYNC and INLINE (non-null return) resolve. First settlement
   * wins: it stamps `signalled` on the shared cascade and later returns are
   * ignored. AppCtx returns chain like any handler's (continuing the
   * cascade through the main network's hop engine) instead of settling.
   * With no settling handler the Promise stays pending unless a `timeoutMs`
   * was configured.
   *
   * @param {AppCtx} ac
   * @param {Object} [opts]
   * @param {(Object|null)} [opts.chain] - prior chain state to continue (e.g. a
   *        remote trace received over a transport — ENVELOPE-SPEC.md §9)
   * @returns {Promise<*>} settled by the attached signal handlers; rejects
   *          with the string `reached timeout of: <ms>ms` when a
   *          `timeoutMs` was configured and no settlement arrived in time
   * @memberof Transceiver
   */
  setAppCtx = (ac, { chain = null } = {}) => {
    const transceiverId = this._transceiverId;
    const timeoutMs = this._timeoutMs;
    const promise = this._promise;
 
    return new promise((resolve, reject) => {
      if (timeoutMs) {
        setTimeout(() => {
          reject(`reached timeout of: ${timeoutMs}ms`);
        }, timeoutMs);
      }
      this._surface.enter(ac, {
        cascade: transceiverControl(transceiverId, resolve, reject),
        chain,
      });
    });
  };
 
  _settle(phase, value, control) {
    if (
      control.transceiverId !== this._transceiverId ||
      !control.signal ||
      control.signalled
    ) {
      return;
    }
    control.signalled = true;
    if (phase === INTERCEPT || phase === ERROR) {
      control.signal.reject(value);
    } else {
      control.signal.resolve(value);
    }
  }
 
  /**
   * Attaches a signal handler — an InlineHandler on the private signals
   * network, alias of {@linkcode addInlineHandler} — for matching AppCons
   * mirrored to this Transceiver. Its return value settles the Promise as
   * described on {@linkcode setAppCtx}; remove with
   * {@linkcode removeInlineHandler}.
   *
   * @param {Trigram} trigram
   * @param {SignalHandler} handler
   * @returns {void}
   * @memberof Transceiver
   */
  addSignalHandler = ({ t, term, a, action, o, orient }, handler) => {
    this._signals.addInlineHandler({ t, term, a, action, o, orient }, handler);
  };
 
  /**
   * Attaches an InterceptHandler to the private signals network — a truthy
   * non-AppCtx return rejects the Promise; a thrown error rejects it.
   *
   * @param {Trigram} trigram
   * @param {SignalHandler} handler
   * @returns {void}
   * @memberof Transceiver
   */
  addInterceptHandler({ t, term, a, action, o, orient }, handler) {
    this._signals.addInterceptHandler(
      { t, term, a, action, o, orient },
      handler,
    );
  }
 
  /**
   * Attaches an AsyncHandler to the private signals network — a non-null
   * non-AppCtx return resolves the Promise; a thrown error or rejection
   * rejects it.
   *
   * @param {Trigram} trigram
   * @param {SignalHandler} handler
   * @returns {void}
   * @memberof Transceiver
   */
  addAsyncHandler({ t, term, a, action, o, orient }, handler) {
    this._signals.addAsyncHandler({ t, term, a, action, o, orient }, handler);
  }
 
  /**
   * Attaches an InlineHandler to the private signals network — a non-null
   * non-AppCtx return resolves the Promise; a thrown error rejects it.
   *
   * @param {Trigram} trigram
   * @param {SignalHandler} handler
   * @returns {void}
   * @memberof Transceiver
   */
  addInlineHandler({ t, term, a, action, o, orient }, handler) {
    this._signals.addInlineHandler({ t, term, a, action, o, orient }, handler);
  }
 
  /**
   * Removes an InterceptHandler previously attached for the same trigram.
   *
   * @param {Trigram} trigram
   * @param {SignalHandler} handler
   * @returns {void}
   * @memberof Transceiver
   */
  removeInterceptHandler({ t, term, a, action, o, orient }, handler) {
    this._signals.removeInterceptHandler(
      { t, term, a, action, o, orient },
      handler,
    );
  }
 
  /**
   * Removes an AsyncHandler previously attached for the same trigram.
   *
   * @param {Trigram} trigram
   * @param {SignalHandler} handler
   * @returns {void}
   * @memberof Transceiver
   */
  removeAsyncHandler({ t, term, a, action, o, orient }, handler) {
    this._signals.removeAsyncHandler(
      { t, term, a, action, o, orient },
      handler,
    );
  }
 
  /**
   * Removes an InlineHandler previously attached for the same trigram
   * (including handlers attached via {@linkcode addSignalHandler}).
   *
   * @param {Trigram} trigram
   * @param {SignalHandler} handler
   * @returns {void}
   * @memberof Transceiver
   */
  removeInlineHandler({ t, term, a, action, o, orient }, handler) {
    this._signals.removeInlineHandler(
      { t, term, a, action, o, orient },
      handler,
    );
  }
}