aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/local-message-stream.js
blob: 76fedd9dfbd0d6071d8aa5f3a2d3be75f01748c6 (plain) (blame)
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
const Duplex = require('readable-stream').Duplex
const inherits = require('util').inherits

module.exports = LocalMessageDuplexStream


inherits(LocalMessageDuplexStream, Duplex)

function LocalMessageDuplexStream(opts){
  Duplex.call(this, {
    objectMode: true,
  })

  // this._origin = opts.origin
  this._name = opts.name
  this._target = opts.target

  // console.log('LocalMessageDuplexStream ('+this._name+') - initialized...')
  window.addEventListener('message', this._onMessage.bind(this), false)
}

// private

LocalMessageDuplexStream.prototype._onMessage = function(event){
  var msg = event.data
  // console.log('LocalMessageDuplexStream ('+this._name+') - heard message...', event)
  // validate message
  if (event.origin !== location.origin) return //console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (event.origin !== location.origin) ')
  if (typeof msg !== 'object') return //console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (typeof msg !== "object") ')
  if (msg.target !== this._name) return //console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (msg.target !== this._name) ', msg.target, this._name)
  if (!msg.data) return //console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (!msg.data) ')
  // console.log('LocalMessageDuplexStream ('+this._name+') - accepted', msg.data)
  // forward message
  try {
    this.push(msg.data)
  } catch(err) {
    this.emit('error', err)
  }
}

// stream plumbing

LocalMessageDuplexStream.prototype._read = noop

LocalMessageDuplexStream.prototype._write = function(data, encoding, cb){
  // console.log('LocalMessageDuplexStream ('+this._name+') - sending message...')
  var message = {
    target: this._target,
    data: data,
  }
  window.postMessage(message, location.origin)
  cb()
}

// util

function noop(){}
td>-0/+1 * dns/djbdns: Fix *-conf referencing stage directoriesbapt2014-03-021-2/+2 * distinfo for other patches was trimmed out by make makesum :-/lx2014-02-181-0/+12 * Revert to test23 version of fefe's patch, as test25 causes repeated segfaults...lx2014-02-183-34/+14 * Readd stage supportbapt2014-02-172-14/+38 * Further remove remnants of stage support.lx2014-02-052-28/+9 * Remove STAGE support, it screws up the run scripts.lx2014-02-051-3/+4 * Add conflictslx2014-01-081-0/+1 * Add stage support, update ipv6 patch, update root server IPs, change MAN hand...lx2014-01-045-32/+60 * multiple: sync LEGALeadler2013-12-121-0/+2 * Add NO_STAGE all over the place in preparation for the staging support (cat: ...bapt2013-09-211-0/+1 * Add option to auto-add users for dnscache, tinydns and dnslog.lx2013-07-241-1/+7 * Dodge "WITH_IPV6" newly becoming the default, since it pulls in an unofficiallx2013-05-232-12/+12 * Finish converting dns to new options frameworkbapt2013-03-26