window-post-message-proxy
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -1,2 +0,2 @@ | ||
/*! window-post-message-proxy v0.2.3 | (c) 2016 Microsoft Corporation MIT */ | ||
/*! window-post-message-proxy v0.2.4 | (c) 2016 Microsoft Corporation MIT */ | ||
interface ITrackingProperties { | ||
@@ -36,2 +36,10 @@ id: string; | ||
private static messagePropertyName; | ||
/** | ||
* Utility to create a deferred object. | ||
*/ | ||
private static createDeferred(); | ||
/** | ||
* Utility to generate random sequence of characters used as tracking id for promises. | ||
*/ | ||
private static createRandomString(); | ||
private logMessages; | ||
@@ -80,10 +88,2 @@ private name; | ||
private onMessageReceived(event); | ||
/** | ||
* Utility to create a deferred object. | ||
*/ | ||
private static createDeferred(); | ||
/** | ||
* Utility to generate random sequence of characters used as tracking id for promises. | ||
*/ | ||
private static createRandomString(); | ||
} |
@@ -1,2 +0,2 @@ | ||
/*! window-post-message-proxy v0.2.3 | (c) 2016 Microsoft Corporation MIT */ | ||
/*! window-post-message-proxy v0.2.4 | (c) 2016 Microsoft Corporation MIT */ | ||
export interface ITrackingProperties { | ||
@@ -36,2 +36,10 @@ id: string; | ||
private static messagePropertyName; | ||
/** | ||
* Utility to create a deferred object. | ||
*/ | ||
private static createDeferred(); | ||
/** | ||
* Utility to generate random sequence of characters used as tracking id for promises. | ||
*/ | ||
private static createRandomString(); | ||
private logMessages; | ||
@@ -80,10 +88,2 @@ private name; | ||
private onMessageReceived(event); | ||
/** | ||
* Utility to create a deferred object. | ||
*/ | ||
private static createDeferred(); | ||
/** | ||
* Utility to generate random sequence of characters used as tracking id for promises. | ||
*/ | ||
private static createRandomString(); | ||
} |
@@ -1,2 +0,2 @@ | ||
/*! window-post-message-proxy v0.2.3 | (c) 2016 Microsoft Corporation MIT */ | ||
/*! window-post-message-proxy v0.2.4 | (c) 2016 Microsoft Corporation MIT */ | ||
(function webpackUniversalModuleDefinition(root, factory) { | ||
@@ -101,2 +101,26 @@ if(typeof exports === 'object' && typeof module === 'object') | ||
/** | ||
* Utility to create a deferred object. | ||
*/ | ||
// TODO: Look to use RSVP library instead of doing this manually. | ||
// From what I searched RSVP would work better because it has .finally and .deferred; however, it doesn't have Typings information. | ||
WindowPostMessageProxy.createDeferred = function () { | ||
var deferred = { | ||
resolve: null, | ||
reject: null, | ||
promise: null | ||
}; | ||
var promise = new Promise(function (resolve, reject) { | ||
deferred.resolve = resolve; | ||
deferred.reject = reject; | ||
}); | ||
deferred.promise = promise; | ||
return deferred; | ||
}; | ||
/** | ||
* Utility to generate random sequence of characters used as tracking id for promises. | ||
*/ | ||
WindowPostMessageProxy.createRandomString = function () { | ||
return (Math.random() + 1).toString(36).substring(7); | ||
}; | ||
/** | ||
* Adds handler. | ||
@@ -114,3 +138,3 @@ * If the first handler whose test method returns true will handle the message and provide a response. | ||
var handlerIndex = this.handlers.indexOf(handler); | ||
if (handlerIndex == -1) { | ||
if (handlerIndex === -1) { | ||
throw new Error("You attempted to remove a handler but no matching handler was found."); | ||
@@ -263,26 +287,2 @@ } | ||
}; | ||
/** | ||
* Utility to create a deferred object. | ||
*/ | ||
// TODO: Look to use RSVP library instead of doing this manually. | ||
// From what I searched RSVP would work better because it has .finally and .deferred; however, it doesn't have Typings information. | ||
WindowPostMessageProxy.createDeferred = function () { | ||
var deferred = { | ||
resolve: null, | ||
reject: null, | ||
promise: null | ||
}; | ||
var promise = new Promise(function (resolve, reject) { | ||
deferred.resolve = resolve; | ||
deferred.reject = reject; | ||
}); | ||
deferred.promise = promise; | ||
return deferred; | ||
}; | ||
/** | ||
* Utility to generate random sequence of characters used as tracking id for promises. | ||
*/ | ||
WindowPostMessageProxy.createRandomString = function () { | ||
return (Math.random() + 1).toString(36).substring(7); | ||
}; | ||
WindowPostMessageProxy.messagePropertyName = "windowPostMessageProxy"; | ||
@@ -289,0 +289,0 @@ return WindowPostMessageProxy; |
@@ -1,2 +0,2 @@ | ||
/*! window-post-message-proxy v0.2.3 | (c) 2016 Microsoft Corporation MIT */ | ||
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports["window-post-message-proxy"]=r():e["window-post-message-proxy"]=r()}(this,function(){return function(e){function r(n){if(s[n])return s[n].exports;var t=s[n]={exports:{},id:n,loaded:!1};return e[n].call(t.exports,t,t.exports,r),t.loaded=!0,t.exports}var s={};return r.m=e,r.c=s,r.p="",r(0)}([function(e,r){"use strict";var s=function(){function e(r){var s=this;void 0===r&&(r={processTrackingProperties:{addTrackingProperties:e.defaultAddTrackingProperties,getTrackingProperties:e.defaultGetTrackingProperties},isErrorMessage:e.defaultIsErrorMessage,receiveWindow:window,name:e.createRandomString()}),this.pendingRequestPromises={},this.addTrackingProperties=r.processTrackingProperties&&r.processTrackingProperties.addTrackingProperties||e.defaultAddTrackingProperties,this.getTrackingProperties=r.processTrackingProperties&&r.processTrackingProperties.getTrackingProperties||e.defaultGetTrackingProperties,this.isErrorMessage=r.isErrorMessage||e.defaultIsErrorMessage,this.receiveWindow=r.receiveWindow||window,this.name=r.name||e.createRandomString(),this.logMessages=r.logMessages||!1,this.eventSourceOverrideWindow=r.eventSourceOverrideWindow,this.suppressWarnings=r.suppressWarnings||!1,this.logMessages&&console.log("new WindowPostMessageProxy created with name: "+this.name+" receiving on window: "+this.receiveWindow.document.title),this.handlers=[],this.windowMessageHandler=function(e){return s.onMessageReceived(e)},this.start()}return e.defaultAddTrackingProperties=function(r,s){return r[e.messagePropertyName]=s,r},e.defaultGetTrackingProperties=function(r){return r[e.messagePropertyName]},e.defaultIsErrorMessage=function(e){return!!e.error},e.prototype.addHandler=function(e){this.handlers.push(e)},e.prototype.removeHandler=function(e){var r=this.handlers.indexOf(e);if(r==-1)throw new Error("You attempted to remove a handler but no matching handler was found.");this.handlers.splice(r,1)},e.prototype.start=function(){this.receiveWindow.addEventListener("message",this.windowMessageHandler)},e.prototype.stop=function(){this.receiveWindow.removeEventListener("message",this.windowMessageHandler)},e.prototype.postMessage=function(r,s){var n={id:e.createRandomString()};this.addTrackingProperties(s,n),this.logMessages&&(console.log(this.name+" Posting message:"),console.log(JSON.stringify(s,null," "))),r.postMessage(s,"*");var t=e.createDeferred();return this.pendingRequestPromises[n.id]=t,t.promise},e.prototype.sendResponse=function(e,r,s){this.addTrackingProperties(r,s),this.logMessages&&(console.log(this.name+" Sending response:"),console.log(JSON.stringify(r,null," "))),e.postMessage(r,"*")},e.prototype.onMessageReceived=function(e){var r=this;this.logMessages&&(console.log(this.name+" Received message:"),console.log("type: "+e.type),console.log(JSON.stringify(e.data,null," ")));var s=this.eventSourceOverrideWindow||e.source,n=e.data;if("object"!=typeof n)return void(this.suppressWarnings||console.warn("Proxy("+this.name+"): Received message that was not an object. Discarding message"));var t;try{t=this.getTrackingProperties(n)}catch(o){this.suppressWarnings||console.warn("Proxy("+this.name+"): Error occurred when attempting to get tracking properties from incoming message:",JSON.stringify(n,null," "),"Error: ",o)}var i;if(t&&(i=this.pendingRequestPromises[t.id]),i){var a=!0;try{a=this.isErrorMessage(n)}catch(o){console.warn("Proxy("+this.name+") Error occurred when trying to determine if message is consider an error response. Message: ",JSON.stringify(n,null,""),"Error: ",o)}a?i.reject(n):i.resolve(n),delete this.pendingRequestPromises[t.id]}else{var d=this.handlers.some(function(e){var o=!1;try{o=e.test(n)}catch(i){r.suppressWarnings||console.warn("Proxy("+r.name+"): Error occurred when handler was testing incoming message:",JSON.stringify(n,null," "),"Error: ",i)}if(o){var a=void 0;try{a=Promise.resolve(e.handle(n))}catch(i){r.suppressWarnings||console.warn("Proxy("+r.name+"): Error occurred when handler was processing incoming message:",JSON.stringify(n,null," "),"Error: ",i),a=Promise.resolve()}return a.then(function(e){if(!e){var o="Handler for message: "+JSON.stringify(n,null," ")+" did not return a response message. The default response message will be returned instead.";r.suppressWarnings||console.warn("Proxy("+r.name+"): "+o),e={warning:o}}r.sendResponse(s,e,t)}),!0}});d||this.suppressWarnings||console.warn("Proxy("+this.name+") did not handle message. Handlers: "+this.handlers.length+" Message: "+JSON.stringify(n,null,"")+".")}},e.createDeferred=function(){var e={resolve:null,reject:null,promise:null},r=new Promise(function(r,s){e.resolve=r,e.reject=s});return e.promise=r,e},e.createRandomString=function(){return(Math.random()+1).toString(36).substring(7)},e.messagePropertyName="windowPostMessageProxy",e}();r.WindowPostMessageProxy=s}])}); | ||
/*! window-post-message-proxy v0.2.4 | (c) 2016 Microsoft Corporation MIT */ | ||
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports["window-post-message-proxy"]=r():e["window-post-message-proxy"]=r()}(this,function(){return function(e){function r(n){if(s[n])return s[n].exports;var t=s[n]={exports:{},id:n,loaded:!1};return e[n].call(t.exports,t,t.exports,r),t.loaded=!0,t.exports}var s={};return r.m=e,r.c=s,r.p="",r(0)}([function(e,r){"use strict";var s=function(){function e(r){var s=this;void 0===r&&(r={processTrackingProperties:{addTrackingProperties:e.defaultAddTrackingProperties,getTrackingProperties:e.defaultGetTrackingProperties},isErrorMessage:e.defaultIsErrorMessage,receiveWindow:window,name:e.createRandomString()}),this.pendingRequestPromises={},this.addTrackingProperties=r.processTrackingProperties&&r.processTrackingProperties.addTrackingProperties||e.defaultAddTrackingProperties,this.getTrackingProperties=r.processTrackingProperties&&r.processTrackingProperties.getTrackingProperties||e.defaultGetTrackingProperties,this.isErrorMessage=r.isErrorMessage||e.defaultIsErrorMessage,this.receiveWindow=r.receiveWindow||window,this.name=r.name||e.createRandomString(),this.logMessages=r.logMessages||!1,this.eventSourceOverrideWindow=r.eventSourceOverrideWindow,this.suppressWarnings=r.suppressWarnings||!1,this.logMessages&&console.log("new WindowPostMessageProxy created with name: "+this.name+" receiving on window: "+this.receiveWindow.document.title),this.handlers=[],this.windowMessageHandler=function(e){return s.onMessageReceived(e)},this.start()}return e.defaultAddTrackingProperties=function(r,s){return r[e.messagePropertyName]=s,r},e.defaultGetTrackingProperties=function(r){return r[e.messagePropertyName]},e.defaultIsErrorMessage=function(e){return!!e.error},e.createDeferred=function(){var e={resolve:null,reject:null,promise:null},r=new Promise(function(r,s){e.resolve=r,e.reject=s});return e.promise=r,e},e.createRandomString=function(){return(Math.random()+1).toString(36).substring(7)},e.prototype.addHandler=function(e){this.handlers.push(e)},e.prototype.removeHandler=function(e){var r=this.handlers.indexOf(e);if(r===-1)throw new Error("You attempted to remove a handler but no matching handler was found.");this.handlers.splice(r,1)},e.prototype.start=function(){this.receiveWindow.addEventListener("message",this.windowMessageHandler)},e.prototype.stop=function(){this.receiveWindow.removeEventListener("message",this.windowMessageHandler)},e.prototype.postMessage=function(r,s){var n={id:e.createRandomString()};this.addTrackingProperties(s,n),this.logMessages&&(console.log(this.name+" Posting message:"),console.log(JSON.stringify(s,null," "))),r.postMessage(s,"*");var t=e.createDeferred();return this.pendingRequestPromises[n.id]=t,t.promise},e.prototype.sendResponse=function(e,r,s){this.addTrackingProperties(r,s),this.logMessages&&(console.log(this.name+" Sending response:"),console.log(JSON.stringify(r,null," "))),e.postMessage(r,"*")},e.prototype.onMessageReceived=function(e){var r=this;this.logMessages&&(console.log(this.name+" Received message:"),console.log("type: "+e.type),console.log(JSON.stringify(e.data,null," ")));var s=this.eventSourceOverrideWindow||e.source,n=e.data;if("object"!=typeof n)return void(this.suppressWarnings||console.warn("Proxy("+this.name+"): Received message that was not an object. Discarding message"));var t;try{t=this.getTrackingProperties(n)}catch(o){this.suppressWarnings||console.warn("Proxy("+this.name+"): Error occurred when attempting to get tracking properties from incoming message:",JSON.stringify(n,null," "),"Error: ",o)}var i;if(t&&(i=this.pendingRequestPromises[t.id]),i){var a=!0;try{a=this.isErrorMessage(n)}catch(o){console.warn("Proxy("+this.name+") Error occurred when trying to determine if message is consider an error response. Message: ",JSON.stringify(n,null,""),"Error: ",o)}a?i.reject(n):i.resolve(n),delete this.pendingRequestPromises[t.id]}else{var d=this.handlers.some(function(e){var o=!1;try{o=e.test(n)}catch(i){r.suppressWarnings||console.warn("Proxy("+r.name+"): Error occurred when handler was testing incoming message:",JSON.stringify(n,null," "),"Error: ",i)}if(o){var a=void 0;try{a=Promise.resolve(e.handle(n))}catch(i){r.suppressWarnings||console.warn("Proxy("+r.name+"): Error occurred when handler was processing incoming message:",JSON.stringify(n,null," "),"Error: ",i),a=Promise.resolve()}return a.then(function(e){if(!e){var o="Handler for message: "+JSON.stringify(n,null," ")+" did not return a response message. The default response message will be returned instead.";r.suppressWarnings||console.warn("Proxy("+r.name+"): "+o),e={warning:o}}r.sendResponse(s,e,t)}),!0}});d||this.suppressWarnings||console.warn("Proxy("+this.name+") did not handle message. Handlers: "+this.handlers.length+" Message: "+JSON.stringify(n,null,"")+".")}},e.messagePropertyName="windowPostMessageProxy",e}();r.WindowPostMessageProxy=s}])}); |
{ | ||
"name": "window-post-message-proxy", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "A library used in place of the native window.postMessage which when used on both the sending and receiving windows allow for a nicer asynchronouse promise messaging between the windows", | ||
@@ -10,4 +10,5 @@ "main": "dist/windowPostMessageProxy.js", | ||
"test": "gulp test", | ||
"postinstall": "typings install", | ||
"prepublish": "gulp build" | ||
"prepublish": "typings install && gulp build", | ||
"gulp": "gulp", | ||
"typings": "typings" | ||
}, | ||
@@ -42,2 +43,3 @@ "repository": { | ||
"gulp-replace": "^0.5.4", | ||
"gulp-tslint": "^6.1.1", | ||
"gulp-typedoc": "^2.0.0", | ||
@@ -58,2 +60,3 @@ "gulp-uglify": "^1.5.3", | ||
"ts-loader": "^0.8.2", | ||
"tslint": "^3.15.0", | ||
"typedoc": "^0.4.4", | ||
@@ -60,0 +63,0 @@ "typescript": "^1.8.10", |
@@ -8,8 +8,11 @@ # window-post-message-proxy | ||
A library used in place of the native window.postMessage which when used on both the sending and receiving windows allow for a nicer asynchronouse promise messaging between the windows. | ||
A library used in place of the native window.postMessage which when used on both the sending and receiving windows allow for a nicer asynchronous promise messaging between the windows. | ||
When sending messages using the proxy, it will apply a unique id to the message, create a deferred object referenced by the id, and pass the message on to the target window. | ||
The target window will also have an instance of the windowPostMessage proxy setup which will send back messages and preserve the unique id. | ||
Then the original sending instance receives the response messag with id, it will look to see if there is matching id in cache and if so resolve the deferred object with the response. | ||
Then the original sending instance receives the response message with id, it will look to see if there is matching id in cache and if so resolve the deferred object with the response. | ||
## Documentation | ||
### [https://microsoft.github.io/window-post-message-proxy](https://microsoft.github.io/window-post-message-proxy) | ||
## Installation | ||
@@ -65,3 +68,3 @@ | ||
If you want to customize how the tracking properties are added to and retreived from the message you can provide it at construction time as an object with two funtions. See the interface below: | ||
If you want to customize how the tracking properties are added to and retrieved from the message you can provide it at construction time as an object with two functions. See the interface below: | ||
@@ -74,3 +77,3 @@ ```typescript | ||
``` | ||
`addTrackingProperties` takes a message adds the tracking properties object an returns the message. | ||
`addTrackingProperties` takes a message and adds the tracking properties object and returns the message. | ||
`getTrackingProperties` takes a message and extracts the tracking properties. | ||
@@ -123,3 +126,3 @@ | ||
By default messagse are not logged, but you can override this behavior by passing `logMessages: true` in the options object. | ||
By default messages are not logged, but you can override this behavior by passing `logMessages: true` in the options object. | ||
@@ -129,7 +132,7 @@ ```typescript | ||
``` | ||
This will print out a stringified JSON of each object that is recieved or sent by the specific instance. | ||
This will print out a stringified JSON of each object that is received or sent by the specific instance. | ||
### Supplying custom name | ||
Each windowPostMessageProxy gives itself a randomly generated name so you can see which instance is communicating in the log messages. | ||
Oftem times you may want to pass a custom name which window the windowPostMessageProxy instance is running. | ||
Often times you may want to pass a custom name for which window the windowPostMessageProxy instance is running. | ||
@@ -136,0 +139,0 @@ You can provided a name by passing `name: 'Iframe'` in the options object. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
53114
147
0
29