11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst EventEmitter = require('events');
41cb0ef41Sopenharmony_ciconst { kEmptyObject } = require('internal/util');
51cb0ef41Sopenharmony_ciconst { Duplex } = require('stream');
61cb0ef41Sopenharmony_ciconst _tls_wrap = require('_tls_wrap');
71cb0ef41Sopenharmony_ciconst _tls_common = require('_tls_common');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst {
101cb0ef41Sopenharmony_ci  Symbol,
111cb0ef41Sopenharmony_ci  ReflectConstruct,
121cb0ef41Sopenharmony_ci} = primordials;
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst kCallback = Symbol('Callback');
151cb0ef41Sopenharmony_ciconst kOtherSide = Symbol('Other');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciclass DuplexSocket extends Duplex {
181cb0ef41Sopenharmony_ci  constructor() {
191cb0ef41Sopenharmony_ci    super();
201cb0ef41Sopenharmony_ci    this[kCallback] = null;
211cb0ef41Sopenharmony_ci    this[kOtherSide] = null;
221cb0ef41Sopenharmony_ci  }
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  _read() {
251cb0ef41Sopenharmony_ci    const callback = this[kCallback];
261cb0ef41Sopenharmony_ci    if (callback) {
271cb0ef41Sopenharmony_ci      this[kCallback] = null;
281cb0ef41Sopenharmony_ci      callback();
291cb0ef41Sopenharmony_ci    }
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  _write(chunk, encoding, callback) {
331cb0ef41Sopenharmony_ci    if (chunk.length === 0) {
341cb0ef41Sopenharmony_ci      process.nextTick(callback);
351cb0ef41Sopenharmony_ci    } else {
361cb0ef41Sopenharmony_ci      this[kOtherSide].push(chunk);
371cb0ef41Sopenharmony_ci      this[kOtherSide][kCallback] = callback;
381cb0ef41Sopenharmony_ci    }
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  _final(callback) {
421cb0ef41Sopenharmony_ci    this[kOtherSide].on('end', callback);
431cb0ef41Sopenharmony_ci    this[kOtherSide].push(null);
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciclass DuplexPair {
481cb0ef41Sopenharmony_ci  constructor() {
491cb0ef41Sopenharmony_ci    this.socket1 = new DuplexSocket();
501cb0ef41Sopenharmony_ci    this.socket2 = new DuplexSocket();
511cb0ef41Sopenharmony_ci    this.socket1[kOtherSide] = this.socket2;
521cb0ef41Sopenharmony_ci    this.socket2[kOtherSide] = this.socket1;
531cb0ef41Sopenharmony_ci  }
541cb0ef41Sopenharmony_ci}
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ciclass SecurePair extends EventEmitter {
571cb0ef41Sopenharmony_ci  constructor(secureContext = _tls_common.createSecureContext(),
581cb0ef41Sopenharmony_ci              isServer = false,
591cb0ef41Sopenharmony_ci              requestCert = !isServer,
601cb0ef41Sopenharmony_ci              rejectUnauthorized = false,
611cb0ef41Sopenharmony_ci              options = kEmptyObject) {
621cb0ef41Sopenharmony_ci    super();
631cb0ef41Sopenharmony_ci    const { socket1, socket2 } = new DuplexPair();
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci    this.server = options.server;
661cb0ef41Sopenharmony_ci    this.credentials = secureContext;
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci    this.encrypted = socket1;
691cb0ef41Sopenharmony_ci    this.cleartext = new _tls_wrap.TLSSocket(socket2, {
701cb0ef41Sopenharmony_ci      secureContext,
711cb0ef41Sopenharmony_ci      isServer,
721cb0ef41Sopenharmony_ci      requestCert,
731cb0ef41Sopenharmony_ci      rejectUnauthorized,
741cb0ef41Sopenharmony_ci      ...options,
751cb0ef41Sopenharmony_ci    });
761cb0ef41Sopenharmony_ci    this.cleartext.once('secure', () => this.emit('secure'));
771cb0ef41Sopenharmony_ci  }
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  destroy() {
801cb0ef41Sopenharmony_ci    this.cleartext.destroy();
811cb0ef41Sopenharmony_ci    this.encrypted.destroy();
821cb0ef41Sopenharmony_ci  }
831cb0ef41Sopenharmony_ci}
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ciexports.createSecurePair = function createSecurePair(...args) {
861cb0ef41Sopenharmony_ci  return ReflectConstruct(SecurePair, args);
871cb0ef41Sopenharmony_ci};
88