<commit_before>/* global Blob */

module.exports = Peer

var debug = require('debug')('simple-peer')
var extend = require('xtend/mutable')
var hat = require('hat')
var inherits = require('inherits')
var isTypedArray = require('is-typedarray')
var once = require('once')
var stream = require('stream')
var toBuffer = require('typedarray-to-buffer')

inherits(Peer, stream.Duplex)

/**
 * WebRTC peer connection. Same API as node core `net.Socket`, plus a few extra methods.
 * Duplex stream.
 * @param {Object} opts
 */
function Peer (opts) {
  var self = this
  if (!(self instanceof Peer)) return new Peer(opts)
  if (!opts) opts = {}

  extend(self, {
    initiator: false,
    stream: false,
    config: Peer.config,
    constraints: Peer.constraints,
    channelName: (opts && opts.initiator) ? hat(160) : null,
    trickle: true,
    allowHalfOpen: false, // duplex stream option
    highWaterMark: 1024 * 1024 // duplex stream option
  }, opts)

  stream.Duplex.call(self, opts)

  var wrtc = opts.wrtc || getBrowserRTC()
  if (!wrtc && typeof window === 'undefined') {
    throw new Error('Missing WebRTC support - You must supply ' +
      '`opts.wrtc` in this environment')
  } else if (!wrtc) {
    throw new Error('Missing WebRTC support - is this a supported browser?')
  }
  self._wrtc = wrtc

  self._debug('new peer (initiator: %s)', self.initiator)

  self.destroyed = false
  self.connected = false

  self.remoteAddress = undefined
  self.remoteFamily = undefined
  self.remotePort = undefined
  self.localAddress = undefined
  self.localPort = undefined

  self._pcReady = false
  self._channelReady = false
  self._iceComplete = false // ice candidate trickle done (got null candidate)
  self._channel = null

  self._chunk = null
  self._cb = null
  self._interval

  self._pc = new (self._wrtc.RTCPeerConnection)(self.config, self.constraints)
  self._pc.oniceconnectionstatechange = self._onIceConnectionStateChange.bind(self)
  self._pc.onsignalingstatechange = self._onSignalingStateChange.bind(self)
  self._pc.onicecandidate = self._onIceCandidate.bind(self)

  if (self.stream) self._pc.addStream(self.stream)
  self._pc.onaddstream = self._onAddStream.bind(self)

  if (self.initiator) {
    self._setupData({ channel: self._pc.createDataChannel(self.channelName) })
    self._pc.onnegotiationneeded = once(self._createOffer.bind(self))
    // Only Chrome triggers "negotiationneeded"; this is a workaround for other
    // implementations
    if (typeof window === 'undefined' || !window.webkitRTCPeerConnection) {
      self._pc.onnegotiationneeded()
    }
  } else {<commit_msg>Allow data channel options to be passed into PeerConnection.createDataChannel<commit_after>/* global Blob */

module.exports = Peer

var debug = require('debug')('simple-peer')
var extend = require('xtend/mutable')
var hat = require('hat')
var inherits = require('inherits')
var isTypedArray = require('is-typedarray')
var once = require('once')
var stream = require('stream')
var toBuffer = require('typedarray-to-buffer')

inherits(Peer, stream.Duplex)

/**
 * WebRTC peer connection. Same API as node core `net.Socket`, plus a few extra methods.
 * Duplex stream.
 * @param {Object} opts
 */
function Peer (opts) {
  var self = this
  if (!(self instanceof Peer)) return new Peer(opts)
  if (!opts) opts = {}

  extend(self, {
    initiator: false,
    stream: false,
    config: Peer.config,
    constraints: Peer.constraints,
    channelName: (opts && opts.initiator) ? hat(160) : null,
    dataChannelOpts: {},
    trickle: true,
    allowHalfOpen: false, // duplex stream option
    highWaterMark: 1024 * 1024 // duplex stream option
  }, opts)

  stream.Duplex.call(self, opts)

  var wrtc = opts.wrtc || getBrowserRTC()
  if (!wrtc && typeof window === 'undefined') {
    throw new Error('Missing WebRTC support - You must supply ' +
      '`opts.wrtc` in this environment')
  } else if (!wrtc) {
    throw new Error('Missing WebRTC support - is this a supported browser?')
  }
  self._wrtc = wrtc

  self._debug('new peer (initiator: %s)', self.initiator)

  self.destroyed = false
  self.connected = false

  self.remoteAddress = undefined
  self.remoteFamily = undefined
  self.remotePort = undefined
  self.localAddress = undefined
  self.localPort = undefined

  self._pcReady = false
  self._channelReady = false
  self._iceComplete = false // ice candidate trickle done (got null candidate)
  self._channel = null

  self._chunk = null
  self._cb = null
  self._interval

  self._pc = new (self._wrtc.RTCPeerConnection)(self.config, self.constraints)
  self._pc.oniceconnectionstatechange = self._onIceConnectionStateChange.bind(self)
  self._pc.onsignalingstatechange = self._onSignalingStateChange.bind(self)
  self._pc.onicecandidate = self._onIceCandidate.bind(self)

  if (self.stream) self._pc.addStream(self.stream)
  self._pc.onaddstream = self._onAddStream.bind(self)

  if (self.initiator) {
    self._setupData({ channel: self._pc.createDataChannel(self.channelName, self.dataChannelOpts) })
    self._pc.onnegotiationneeded = once(self._createOffer.bind(self))
    // Only Chrome triggers "negotiationneeded"; this is a workaround for other
    // implementations
    if (typeof window === 'undefined' || !window.webkitRTCPeerConnection) {
      self._pc.onnegotiationneeded()
    }
  } else {
{'fraction_non_alphanumeric': 0.12136559744888388, 'fraction_numerical': 0.004126805477396361, 'mean_word_length': 3.937037037037037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1900012', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1626, 'n_words': 432}