git ssb

0+

cel / pull-postmsg



Commit 0e406483fa15d28f0f5eda0c569b37021fa66136

Initial commit

Charles Lehner committed on 7/23/2016, 8:40:23 PM

Files changed

README.mdadded
example/.gitignoreadded
example/index.htmladded
example/inner.htmladded
index.jsadded
package.jsonadded
README.mdView
@@ -1,0 +1,38 @@
1+# pull-postmsg
2+
3+[pull-stream][] for bidirectional communication between frames using
4+[window.postMessage][], which respects backpressure.
5+
6+```js
7+var Postmsg = require('pull-postsg')
8+```
9+
10+```
11+var through = Postmsg(win, origin)
12+```
13+
14+- `through`: through-stream for communication between `win` and the current
15+ window
16+- `win`: window object to post messages to. If not specified, the source
17+ window for the first message received on the current window will be used.
18+- `origin`: origin URL or path. defaults to `'*'`. This value is used when
19+ [posting messages to `win`](window.postMessage) and is used to filter
20+ messages received on the current window.
21+
22+[pull-stream]: https://github.com/dominictarr/pull-stream/
23+[window.postMessage]: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
24+
25+## See also
26+
27+- [mmckegg's window-stream](https://github.com/mmckegg/window-stream)
28+- https://steveliles.github.io/cross_domain_inter_frame_communication_in_javascript.html
29+
30+## License
31+
32+Copyright (c) 2016 Charles Lehner
33+
34+Usage of the works is permitted provided that this instrument is
35+retained with the works, so that any entity that uses the works is
36+notified of this instrument.
37+
38+DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
example/.gitignoreView
@@ -1,0 +1,1 @@
1+bundle.js
example/index.htmlView
@@ -1,0 +1,23 @@
1+<!doctype html>
2+<html>
3+ <head>
4+ <meta charset=utf-8>
5+ </head>
6+ <body>
7+ <iframe id="frame" src="inner.html"></iframe>
8+ <pre id="out"></pre>
9+
10+ <script src="bundle.js"></script>
11+ <script>
12+ function print(str) {
13+ document.getElementById('out').appendChild(document.createTextNode(str + '\n'))
14+ }
15+
16+ pull(
17+ pull.values(['from outer', 'a', 'b', 'c']),
18+ Postmsg(),
19+ pull.drain(print, print)
20+ )
21+ </script>
22+ </body>
23+</html>
example/inner.htmlView
@@ -1,0 +1,23 @@
1+<!doctype html>
2+<html>
3+ <head>
4+ <meta charset=utf-8>
5+ </head>
6+ <body>
7+ inner
8+ <pre id="out"></pre>
9+
10+ <script src="bundle.js"></script>
11+ <script>
12+ function print(str) {
13+ document.getElementById('out').appendChild(document.createTextNode(str + '\n'))
14+ }
15+
16+ pull(
17+ pull.values(['from inner', 1, 2, 3]),
18+ Postmsg(window.parent),
19+ pull.drain(print, print)
20+ )
21+ </script>
22+ </body>
23+</html>
index.jsView
@@ -1,0 +1,71 @@
1+function fixJSON(key, value) {
2+ if (value instanceof Error) return {
3+ message: value.message,
4+ stack: value.stack,
5+ name: value.name,
6+ code: value.code,
7+ }
8+ return value
9+}
10+
11+function isOriginCompatible(a, b) {
12+ return !a || a === '*' || a.slice(0, b.length) === b
13+}
14+
15+module.exports = function (win, origin) {
16+ var _cb, _msg
17+
18+ if(origin && origin[0] === '/') {
19+ origin = location.origin + origin
20+ }
21+
22+ function post(msg) {
23+ if (!win) _msg = msg
24+ else win.postMessage(JSON.stringify(msg, fixJSON), origin || '*')
25+ }
26+
27+ function gotWin(_win) {
28+ win = _win
29+ if (_msg) {
30+ var msg = _msg
31+ _msg = null
32+ post(msg)
33+ }
34+ }
35+
36+ return function (read) {
37+ function onMessage(e) {
38+ if(isOriginCompatible(origin, e.origin)) {
39+ if(!win) gotWin(e.source)
40+ 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])
45+ }
46+ }
47+ }
48+
49+ function onRead(end) {
50+ read(end, function (end, data) {
51+ post(['next', end, data])
52+ })
53+ }
54+
55+ function onNext(end, data) {
56+ if (end) window.removeEventListener('message', onMessage, false)
57+ if (!_cb) return console.error('unexpected data', end, data)
58+ var cb = _cb
59+ _cb = null
60+ return cb(end, data)
61+ }
62+
63+ window.addEventListener('message', onMessage, false)
64+
65+ return function (end, cb) {
66+ if(_cb) return cb(new Error('read too soon'))
67+ _cb = cb
68+ post(['read', end])
69+ }
70+ }
71+}
package.jsonView
@@ -1,0 +1,19 @@
1+{
2+ "name": "pull-postmsg",
3+ "version": "0.0.0",
4+ "description": "pull-stream for cross-domain inter-frame communication using postMessage",
5+ "main": "index.js",
6+ "keywords": [
7+ "postMessage",
8+ "pull-stream"
9+ ],
10+ "author": "Charles Lehner (http://celehner.com/)",
11+ "license": "Fair",
12+ "devDependencies": {
13+ "browserify": "^13.0.1",
14+ "pull-stream": "^3.4.3"
15+ },
16+ "scripts": {
17+ "prepublish": "echo \"window.pull = require('pull-stream'); window.Postmsg = require('.')\" | browserify -o example/bundle.js -"
18+ }
19+}

Built with git-ssb-web