Comparing version 3.0.0-alpha.5 to 3.0.0-alpha.6
@@ -368,4 +368,6 @@ // Derived from https://github.com/umdjs/umd/blob/master/templates/amdWebGlobal.js | ||
var promise = new Penpal.Promise(function (resolve, reject) { | ||
var timeoutId; | ||
if (timeout !== undefined) { | ||
setTimeout(function () { | ||
timeoutId = setTimeout(function () { | ||
reject(new Error('Connection to child timed out after ' + timeout + 'ms')); | ||
@@ -409,5 +411,4 @@ destroy(); | ||
methodNames = event.data.methodNames; | ||
connectCallSender(callSender, info, methodNames, destructionPromise); | ||
clearTimeout(timeoutId); | ||
resolve(callSender); | ||
@@ -469,4 +470,6 @@ } | ||
var promise = new Penpal.Promise(function (resolve, reject) { | ||
var timeoutId; | ||
if (timeout !== undefined) { | ||
setTimeout(function () { | ||
timeoutId = setTimeout(function () { | ||
reject(new Error('Connection to parent timed out after ' + timeout + 'ms')); | ||
@@ -494,2 +497,3 @@ destroy(); | ||
connectCallSender(callSender, info, event.data.methodNames, destructionPromise); | ||
clearTimeout(timeoutId); | ||
resolve(callSender); | ||
@@ -496,0 +500,0 @@ } |
@@ -1,1 +0,1 @@ | ||
!function(e,n){var r={};!function(e){"use strict";function n(e){if(Array.isArray(e)){for(var n=0,r=Array(e.length);n<e.length;n++)r[n]=e[n];return r}return Array.from(e)}Object.defineProperty(e,"__esModule",{value:!0});var r={"http:":"80","https:":"443"},t=/^(https?:)?\/\/([^\/:]+)(:(\d+))?/,o={Promise:function(){try{return window?window.Promise:null}catch(e){return null}}(),debug:!1},a=function(){var e=0;return function(){return++e}}(),i=function(){for(var e=arguments.length,n=Array(e),r=0;r<e;r++)n[r]=arguments[r];if(o.debug){var t;(t=console).log.apply(t,["[Penpal]"].concat(n))}},d=function(e){var n=document.location,o=t.exec(e),a=void 0,i=void 0,d=void 0;return o?(a=o[1]?o[1]:n.protocol,i=o[2],d=o[4]):(a=n.protocol,i=n.hostname,d=n.port),a+"//"+i+(d&&d!==r[a]?":"+d:"")},c=function(e){var n=[];return e(function(){n.forEach(function(e){e()})}),{then:function(e){n.push(e)}}},u=function(e){return{name:e.name,message:e.message,stack:e.stack}},s=function(e){var n=new Error;return Object.keys(e).forEach(function(r){return n[r]=e[r]}),n},l=function(e,n,r,t){var d=n.localName,c=n.local,u=n.remote,l=n.remoteOrigin,f=!1;i(d+": Connecting call sender");var m=function(e){return function(){for(var n=arguments.length,r=Array(n),t=0;t<n;t++)r[t]=arguments[t];return i(d+": Sending "+e+"() call"),new o.Promise(function(n,t){if(f)return void t(new Error("Unable to send "+e+"() call due to destroyed connection"));var o=a(),m=function r(a){if(a.source===u&&a.origin===l&&"reply"===a.data.penpal&&a.data.id===o){i(d+": Received "+e+"() reply"),c.removeEventListener("message",r);var f=a.data.returnValue;a.data.returnValueIsError&&(f=s(f)),("fulfilled"===a.data.resolution?n:t)(f)}};c.addEventListener("message",m),u.postMessage({penpal:"call",id:o,methodName:e,args:r},l)})}};t.then(function(){f=!0}),r.reduce(function(e,n){return e[n]=m(n),e},e)},f=function(e,r,t){var a=e.localName,d=e.local,c=e.remote,s=e.remoteOrigin,l=!1;i(a+": Connecting call receiver");var f=function(e){if(e.source===c&&e.origin===s&&"call"===e.data.penpal){var t=e.data,d=t.methodName,f=t.args,m=t.id;if(i(a+": Received "+d+"() call"),d in r){var p=function(e){return function(n){if(l)throw new Error("Unable to send "+d+"() reply due to destroyed connection");i(a+": Sending "+d+"() reply");var r={penpal:"reply",id:m,resolution:e,returnValue:n};"rejected"===e&&n instanceof Error&&(r.returnValue=u(n),r.returnValueIsError=!0);try{c.postMessage(r,s)}catch(e){throw"DataCloneError"===e.name&&c.postMessage({penpal:"reply",id:m,resolution:"rejected",returnValue:u(e),returnValueIsError:!0},s),e}}};new o.Promise(function(e){return e(r[d].apply(r,n(f)))}).then(p("fulfilled"),p("rejected"))}}};d.addEventListener("message",f),t.then(function(){l=!0,d.removeEventListener("message",f)})};o.connectToChild=function(e){var n=e.url,r=e.appendTo,t=e.methods,a=void 0===t?{}:t,u=e.timeout,s=void 0,m=new c(function(e){return s=e}),p=window,v=document.createElement("iframe");(r||document.body).appendChild(v),m.then(function(){v.parentNode&&v.parentNode.removeChild(v)});var h=v.contentWindow||v.contentDocument.parentWindow,g=d(n);return{promise:new o.Promise(function(e,r){void 0!==u&&setTimeout(function(){r(new Error("Connection to child timed out after "+u+"ms")),s()},u);var t={},o=void 0,d=function(n){if(n.source===h&&n.origin===g&&"handshake"===n.data.penpal){i("Parent: Received handshake"),i("Parent: Sending handshake reply"),n.source.postMessage({penpal:"handshake-reply",methodNames:Object.keys(a)},n.origin);var r={localName:"Parent",local:p,remote:h,remoteOrigin:n.origin};f(r,a,m),o&&o.forEach(function(e){delete t[e]}),o=n.data.methodNames,l(t,r,o,m),e(t)}};p.addEventListener("message",d),m.then(function(){p.removeEventListener("message",d),r("Parent: Connection destroyed")}),i("Parent: Loading iframe"),v.src=n}),iframe:v,destroy:s}},o.connectToParent=function(e){var n=e.parentOrigin,r=void 0===n?"*":n,t=e.methods,a=void 0===t?{}:t,d=e.timeout;if(window===window.top)throw new Error("connectToParent() must be called within an iframe");var u=void 0,s=new c(function(e){return u=e}),m=window,p=m.parent;return{promise:new o.Promise(function(e,n){void 0!==d&&setTimeout(function(){n(new Error("Connection to parent timed out after "+d+"ms")),u()},d);var t=function n(t){if(("*"===r||r===t.origin)&&t.source===p&&"handshake-reply"===t.data.penpal){i("Child: Received handshake reply"),m.removeEventListener("message",n);var o={localName:"Child",local:m,remote:p,remoteOrigin:t.origin},d={};f(o,a,s),l(d,o,t.data.methodNames,s),e(d)}};m.addEventListener("message",t),s.then(function(){m.removeEventListener("message",t),n(new Error("Child: Connection destroyed"))}),i("Child: Sending handshake"),p.postMessage({penpal:"handshake",methodNames:Object.keys(a)},r)}),destroy:u}},e.default=o}(r),"function"==typeof define&&define.amd?define("Penpal",r.default):e.Penpal=r.default}(this); | ||
!function(e,n){var r={};!function(e){"use strict";function n(e){if(Array.isArray(e)){for(var n=0,r=Array(e.length);n<e.length;n++)r[n]=e[n];return r}return Array.from(e)}Object.defineProperty(e,"__esModule",{value:!0});var r={"http:":"80","https:":"443"},t=/^(https?:)?\/\/([^\/:]+)(:(\d+))?/,o={Promise:function(){try{return window?window.Promise:null}catch(e){return null}}(),debug:!1},a=function(){var e=0;return function(){return++e}}(),i=function(){for(var e=arguments.length,n=Array(e),r=0;r<e;r++)n[r]=arguments[r];if(o.debug){var t;(t=console).log.apply(t,["[Penpal]"].concat(n))}},c=function(e){var n=document.location,o=t.exec(e),a=void 0,i=void 0,c=void 0;return o?(a=o[1]?o[1]:n.protocol,i=o[2],c=o[4]):(a=n.protocol,i=n.hostname,c=n.port),a+"//"+i+(c&&c!==r[a]?":"+c:"")},d=function(e){var n=[];return e(function(){n.forEach(function(e){e()})}),{then:function(e){n.push(e)}}},u=function(e){return{name:e.name,message:e.message,stack:e.stack}},s=function(e){var n=new Error;return Object.keys(e).forEach(function(r){return n[r]=e[r]}),n},l=function(e,n,r,t){var c=n.localName,d=n.local,u=n.remote,l=n.remoteOrigin,f=!1;i(c+": Connecting call sender");var m=function(e){return function(){for(var n=arguments.length,r=Array(n),t=0;t<n;t++)r[t]=arguments[t];return i(c+": Sending "+e+"() call"),new o.Promise(function(n,t){if(f)return void t(new Error("Unable to send "+e+"() call due to destroyed connection"));var o=a(),m=function r(a){if(a.source===u&&a.origin===l&&"reply"===a.data.penpal&&a.data.id===o){i(c+": Received "+e+"() reply"),d.removeEventListener("message",r);var f=a.data.returnValue;a.data.returnValueIsError&&(f=s(f)),("fulfilled"===a.data.resolution?n:t)(f)}};d.addEventListener("message",m),u.postMessage({penpal:"call",id:o,methodName:e,args:r},l)})}};t.then(function(){f=!0}),r.reduce(function(e,n){return e[n]=m(n),e},e)},f=function(e,r,t){var a=e.localName,c=e.local,d=e.remote,s=e.remoteOrigin,l=!1;i(a+": Connecting call receiver");var f=function(e){if(e.source===d&&e.origin===s&&"call"===e.data.penpal){var t=e.data,c=t.methodName,f=t.args,m=t.id;if(i(a+": Received "+c+"() call"),c in r){var p=function(e){return function(n){if(l)throw new Error("Unable to send "+c+"() reply due to destroyed connection");i(a+": Sending "+c+"() reply");var r={penpal:"reply",id:m,resolution:e,returnValue:n};"rejected"===e&&n instanceof Error&&(r.returnValue=u(n),r.returnValueIsError=!0);try{d.postMessage(r,s)}catch(e){throw"DataCloneError"===e.name&&d.postMessage({penpal:"reply",id:m,resolution:"rejected",returnValue:u(e),returnValueIsError:!0},s),e}}};new o.Promise(function(e){return e(r[c].apply(r,n(f)))}).then(p("fulfilled"),p("rejected"))}}};c.addEventListener("message",f),t.then(function(){l=!0,c.removeEventListener("message",f)})};o.connectToChild=function(e){var n=e.url,r=e.appendTo,t=e.methods,a=void 0===t?{}:t,u=e.timeout,s=void 0,m=new d(function(e){return s=e}),p=window,v=document.createElement("iframe");(r||document.body).appendChild(v),m.then(function(){v.parentNode&&v.parentNode.removeChild(v)});var h=v.contentWindow||v.contentDocument.parentWindow,g=c(n);return{promise:new o.Promise(function(e,r){var t;void 0!==u&&(t=setTimeout(function(){r(new Error("Connection to child timed out after "+u+"ms")),s()},u));var o={},c=void 0,d=function(n){if(n.source===h&&n.origin===g&&"handshake"===n.data.penpal){i("Parent: Received handshake"),i("Parent: Sending handshake reply"),n.source.postMessage({penpal:"handshake-reply",methodNames:Object.keys(a)},n.origin);var r={localName:"Parent",local:p,remote:h,remoteOrigin:n.origin};f(r,a,m),c&&c.forEach(function(e){delete o[e]}),c=n.data.methodNames,l(o,r,c,m),clearTimeout(t),e(o)}};p.addEventListener("message",d),m.then(function(){p.removeEventListener("message",d),r("Parent: Connection destroyed")}),i("Parent: Loading iframe"),v.src=n}),iframe:v,destroy:s}},o.connectToParent=function(e){var n=e.parentOrigin,r=void 0===n?"*":n,t=e.methods,a=void 0===t?{}:t,c=e.timeout;if(window===window.top)throw new Error("connectToParent() must be called within an iframe");var u=void 0,s=new d(function(e){return u=e}),m=window,p=m.parent;return{promise:new o.Promise(function(e,n){var t;void 0!==c&&(t=setTimeout(function(){n(new Error("Connection to parent timed out after "+c+"ms")),u()},c));var o=function n(o){if(("*"===r||r===o.origin)&&o.source===p&&"handshake-reply"===o.data.penpal){i("Child: Received handshake reply"),m.removeEventListener("message",n);var c={localName:"Child",local:m,remote:p,remoteOrigin:o.origin},d={};f(c,a,s),l(d,c,o.data.methodNames,s),clearTimeout(t),e(d)}};m.addEventListener("message",o),s.then(function(){m.removeEventListener("message",o),n(new Error("Child: Connection destroyed"))}),i("Child: Sending handshake"),p.postMessage({penpal:"handshake",methodNames:Object.keys(a)},r)}),destroy:u}},e.default=o}(r),"function"==typeof define&&define.amd?define("Penpal",r.default):e.Penpal=r.default}(this); |
@@ -354,4 +354,6 @@ 'use strict'; | ||
var promise = new Penpal.Promise(function (resolve, reject) { | ||
var timeoutId; | ||
if (timeout !== undefined) { | ||
setTimeout(function () { | ||
timeoutId = setTimeout(function () { | ||
reject(new Error('Connection to child timed out after ' + timeout + 'ms')); | ||
@@ -395,5 +397,4 @@ destroy(); | ||
methodNames = event.data.methodNames; | ||
connectCallSender(callSender, info, methodNames, destructionPromise); | ||
clearTimeout(timeoutId); | ||
resolve(callSender); | ||
@@ -455,4 +456,6 @@ } | ||
var promise = new Penpal.Promise(function (resolve, reject) { | ||
var timeoutId; | ||
if (timeout !== undefined) { | ||
setTimeout(function () { | ||
timeoutId = setTimeout(function () { | ||
reject(new Error('Connection to parent timed out after ' + timeout + 'ms')); | ||
@@ -480,2 +483,3 @@ destroy(); | ||
connectCallSender(callSender, info, event.data.methodNames, destructionPromise); | ||
clearTimeout(timeoutId); | ||
resolve(callSender); | ||
@@ -482,0 +486,0 @@ } |
{ | ||
"name": "penpal", | ||
"version": "3.0.0-alpha.5", | ||
"version": "3.0.0-alpha.6", | ||
"description": "A promise-based library for communicating with iframes via postMessage.", | ||
@@ -5,0 +5,0 @@ "author": "Aaron Hardy <aaron@aaronhardy.com>", |
48380
893