Commit e57ae82e4ad29ce90e5085f85c8f351e54f5231e
Don't encode data as JSON
Charles Lehner committed on 7/23/2016, 8:41:41 PMParent: 0e406483fa15d28f0f5eda0c569b37021fa66136
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -11,18 +11,26 @@ | ||
11 | 11 | function isOriginCompatible(a, b) { |
12 | 12 | return !a || a === '*' || a.slice(0, b.length) === b |
13 | 13 | } |
14 | 14 | |
15 | +var cmds = { | |
16 | + read: 'r', | |
17 | + readEnd: 'R', | |
18 | + next: 'n', | |
19 | + nextEnd: 'N', | |
20 | +} | |
21 | + | |
15 | 22 | module.exports = function (win, origin) { |
16 | 23 | var _cb, _msg |
17 | 24 | |
18 | 25 | if(origin && origin[0] === '/') { |
19 | 26 | origin = location.origin + origin |
20 | 27 | } |
21 | 28 | |
22 | - function post(msg) { | |
29 | + function post(cmd, data) { | |
30 | + var msg = cmd + data | |
23 | 31 | if (!win) _msg = msg |
24 | - else win.postMessage(JSON.stringify(msg, fixJSON), origin || '*') | |
32 | + else win.postMessage(msg, origin || '*') | |
25 | 33 | } |
26 | 34 | |
27 | 35 | function gotWin(_win) { |
28 | 36 | win = _win |
@@ -37,19 +45,21 @@ | ||
37 | 45 | function onMessage(e) { |
38 | 46 | if(isOriginCompatible(origin, e.origin)) { |
39 | 47 | if(!win) gotWin(e.source) |
40 | 48 | 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))) | |
45 | 54 | } |
46 | 55 | } |
47 | 56 | } |
48 | 57 | |
49 | 58 | function onRead(end) { |
50 | 59 | 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) | |
52 | 62 | }) |
53 | 63 | } |
54 | 64 | |
55 | 65 | function onNext(end, data) { |
@@ -64,8 +74,9 @@ | ||
64 | 74 | |
65 | 75 | return function (end, cb) { |
66 | 76 | if(_cb) return cb(new Error('read too soon')) |
67 | 77 | _cb = cb |
68 | - post(['read', end]) | |
78 | + if (end) post(cmds.readEnd, JSON.stringify(end, fixJSON)) | |
79 | + else post(cmds.read) | |
69 | 80 | } |
70 | 81 | } |
71 | 82 | } |
Built with git-ssb-web