git ssb

0+

cel / pull-postmsg



Commit e57ae82e4ad29ce90e5085f85c8f351e54f5231e

Don't encode data as JSON

Charles Lehner committed on 7/23/2016, 8:41:41 PM
Parent: 0e406483fa15d28f0f5eda0c569b37021fa66136

Files changed

index.jschanged
index.jsView
@@ -11,18 +11,26 @@
1111 function isOriginCompatible(a, b) {
1212 return !a || a === '*' || a.slice(0, b.length) === b
1313 }
1414
15+var cmds = {
16+ read: 'r',
17+ readEnd: 'R',
18+ next: 'n',
19+ nextEnd: 'N',
20+}
21+
1522 module.exports = function (win, origin) {
1623 var _cb, _msg
1724
1825 if(origin && origin[0] === '/') {
1926 origin = location.origin + origin
2027 }
2128
22- function post(msg) {
29+ function post(cmd, data) {
30+ var msg = cmd + data
2331 if (!win) _msg = msg
24- else win.postMessage(JSON.stringify(msg, fixJSON), origin || '*')
32+ else win.postMessage(msg, origin || '*')
2533 }
2634
2735 function gotWin(_win) {
2836 win = _win
@@ -37,19 +45,21 @@
3745 function onMessage(e) {
3846 if(isOriginCompatible(origin, e.origin)) {
3947 if(!win) gotWin(e.source)
4048 if(!origin) origin = e.origin
41- var msg = JSON.parse(e.data)
42- switch (msg[0]) {
43- case 'read': return onRead(msg[1])
44- case 'next': return onNext(msg[1], msg[2])
49+ switch (e.data[0]) {
50+ case cmds.read: return onRead(null)
51+ case cmds.readEnd: return onRead(JSON.parse(e.data.substr(1)))
52+ case cmds.next: return onNext(null, e.data.substr(1))
53+ case cmds.nextEnd: return onNext(JSON.parse(e.data.substr(1)))
4554 }
4655 }
4756 }
4857
4958 function onRead(end) {
5059 read(end, function (end, data) {
51- post(['next', end, data])
60+ if (end) post(cmds.nextEnd, JSON.stringify(end, fixJSON))
61+ else post(cmds.next, data)
5262 })
5363 }
5464
5565 function onNext(end, data) {
@@ -64,8 +74,9 @@
6474
6575 return function (end, cb) {
6676 if(_cb) return cb(new Error('read too soon'))
6777 _cb = cb
68- post(['read', end])
78+ if (end) post(cmds.readEnd, JSON.stringify(end, fixJSON))
79+ else post(cmds.read)
6980 }
7081 }
7182 }

Built with git-ssb-web