Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

window-post-message-proxy

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

window-post-message-proxy - npm Package Compare versions

Comparing version 0.2.6 to 0.2.7

dist/src/windowPostMessageProxy.d.ts

300

dist/windowPostMessageProxy.js

@@ -1,299 +0,3 @@

/*! window-post-message-proxy v0.2.6 | (c) 2016 Microsoft Corporation MIT */
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["window-post-message-proxy"] = factory();
else
root["window-post-message-proxy"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {
"use strict";
var WindowPostMessageProxy = (function () {
function WindowPostMessageProxy(options) {
var _this = this;
if (options === void 0) { options = {
processTrackingProperties: {
addTrackingProperties: WindowPostMessageProxy.defaultAddTrackingProperties,
getTrackingProperties: WindowPostMessageProxy.defaultGetTrackingProperties
},
isErrorMessage: WindowPostMessageProxy.defaultIsErrorMessage,
receiveWindow: window,
name: WindowPostMessageProxy.createRandomString()
}; }
this.pendingRequestPromises = {};
// save options with defaults
this.addTrackingProperties = (options.processTrackingProperties && options.processTrackingProperties.addTrackingProperties) || WindowPostMessageProxy.defaultAddTrackingProperties;
this.getTrackingProperties = (options.processTrackingProperties && options.processTrackingProperties.getTrackingProperties) || WindowPostMessageProxy.defaultGetTrackingProperties;
this.isErrorMessage = options.isErrorMessage || WindowPostMessageProxy.defaultIsErrorMessage;
this.receiveWindow = options.receiveWindow || window;
this.name = options.name || WindowPostMessageProxy.createRandomString();
this.logMessages = options.logMessages || false;
this.eventSourceOverrideWindow = options.eventSourceOverrideWindow;
this.suppressWarnings = options.suppressWarnings || false;
if (this.logMessages) {
console.log("new WindowPostMessageProxy created with name: " + this.name + " receiving on window: " + this.receiveWindow.document.title);
}
// Initialize
this.handlers = [];
this.windowMessageHandler = function (event) { return _this.onMessageReceived(event); };
this.start();
}
// Static
WindowPostMessageProxy.defaultAddTrackingProperties = function (message, trackingProperties) {
message[WindowPostMessageProxy.messagePropertyName] = trackingProperties;
return message;
};
WindowPostMessageProxy.defaultGetTrackingProperties = function (message) {
return message[WindowPostMessageProxy.messagePropertyName];
};
WindowPostMessageProxy.defaultIsErrorMessage = function (message) {
return !!message.error;
};
/**
* 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 () {
// window.msCrypto for IE
var cryptoObj = window.crypto || window.msCrypto;
var randomValueArray = new Uint32Array(1);
cryptoObj.getRandomValues(randomValueArray);
return randomValueArray[0].toString(36).substring(1);
};
/**
* Adds handler.
* If the first handler whose test method returns true will handle the message and provide a response.
*/
WindowPostMessageProxy.prototype.addHandler = function (handler) {
this.handlers.push(handler);
};
/**
* Removes handler.
* The reference must match the original object that was provided when adding the handler.
*/
WindowPostMessageProxy.prototype.removeHandler = function (handler) {
var handlerIndex = this.handlers.indexOf(handler);
if (handlerIndex === -1) {
throw new Error("You attempted to remove a handler but no matching handler was found.");
}
this.handlers.splice(handlerIndex, 1);
};
/**
* Start listening to message events.
*/
WindowPostMessageProxy.prototype.start = function () {
this.receiveWindow.addEventListener('message', this.windowMessageHandler);
};
/**
* Stops listening to message events.
*/
WindowPostMessageProxy.prototype.stop = function () {
this.receiveWindow.removeEventListener('message', this.windowMessageHandler);
};
/**
* Post message to target window with tracking properties added and save deferred object referenced by tracking id.
*/
WindowPostMessageProxy.prototype.postMessage = function (targetWindow, message) {
// Add tracking properties to indicate message came from this proxy
var trackingProperties = { id: WindowPostMessageProxy.createRandomString() };
this.addTrackingProperties(message, trackingProperties);
if (this.logMessages) {
console.log(this.name + " Posting message:");
console.log(JSON.stringify(message, null, ' '));
}
targetWindow.postMessage(message, "*");
var deferred = WindowPostMessageProxy.createDeferred();
this.pendingRequestPromises[trackingProperties.id] = deferred;
return deferred.promise;
};
/**
* Send response message to target window.
* Response messages re-use tracking properties from a previous request message.
*/
WindowPostMessageProxy.prototype.sendResponse = function (targetWindow, message, trackingProperties) {
this.addTrackingProperties(message, trackingProperties);
if (this.logMessages) {
console.log(this.name + " Sending response:");
console.log(JSON.stringify(message, null, ' '));
}
targetWindow.postMessage(message, "*");
};
/**
* Message handler.
*/
WindowPostMessageProxy.prototype.onMessageReceived = function (event) {
var _this = this;
if (this.logMessages) {
console.log(this.name + " Received message:");
console.log("type: " + event.type);
console.log(JSON.stringify(event.data, null, ' '));
}
var sendingWindow = this.eventSourceOverrideWindow || event.source;
var message = event.data;
if (typeof message !== "object") {
if (!this.suppressWarnings) {
console.warn("Proxy(" + this.name + "): Received message that was not an object. Discarding message");
}
return;
}
var trackingProperties;
try {
trackingProperties = this.getTrackingProperties(message);
}
catch (e) {
if (!this.suppressWarnings) {
console.warn("Proxy(" + this.name + "): Error occurred when attempting to get tracking properties from incoming message:", JSON.stringify(message, null, ' '), "Error: ", e);
}
}
var deferred;
if (trackingProperties) {
deferred = this.pendingRequestPromises[trackingProperties.id];
}
// If message does not have a known ID, treat it as a request
// Otherwise, treat message as response
if (!deferred) {
var handled = this.handlers.some(function (handler) {
var canMessageBeHandled = false;
try {
canMessageBeHandled = handler.test(message);
}
catch (e) {
if (!_this.suppressWarnings) {
console.warn("Proxy(" + _this.name + "): Error occurred when handler was testing incoming message:", JSON.stringify(message, null, ' '), "Error: ", e);
}
}
if (canMessageBeHandled) {
var responseMessagePromise = void 0;
try {
responseMessagePromise = Promise.resolve(handler.handle(message));
}
catch (e) {
if (!_this.suppressWarnings) {
console.warn("Proxy(" + _this.name + "): Error occurred when handler was processing incoming message:", JSON.stringify(message, null, ' '), "Error: ", e);
}
responseMessagePromise = Promise.resolve();
}
responseMessagePromise
.then(function (responseMessage) {
if (!responseMessage) {
var warningMessage = "Handler for message: " + JSON.stringify(message, null, ' ') + " did not return a response message. The default response message will be returned instead.";
if (!_this.suppressWarnings) {
console.warn("Proxy(" + _this.name + "): " + warningMessage);
}
responseMessage = {
warning: warningMessage
};
}
_this.sendResponse(sendingWindow, responseMessage, trackingProperties);
});
return true;
}
});
/**
* TODO: Consider returning an error message if nothing handled the message.
* In the case of the Report receiving messages all of them should be handled,
* however, in the case of the SDK receiving messages it's likely it won't register handlers
* for all events. Perhaps make this an option at construction time.
*/
if (!handled && !this.suppressWarnings) {
console.warn("Proxy(" + this.name + ") did not handle message. Handlers: " + this.handlers.length + " Message: " + JSON.stringify(message, null, '') + ".");
}
}
else {
/**
* If error message reject promise,
* Otherwise, resolve promise
*/
var isErrorMessage = true;
try {
isErrorMessage = this.isErrorMessage(message);
}
catch (e) {
console.warn("Proxy(" + this.name + ") Error occurred when trying to determine if message is consider an error response. Message: ", JSON.stringify(message, null, ''), 'Error: ', e);
}
if (isErrorMessage) {
deferred.reject(message);
}
else {
deferred.resolve(message);
}
// TODO: Move to .finally clause up where promise is created for better maitenance like original proxy code.
delete this.pendingRequestPromises[trackingProperties.id];
}
};
WindowPostMessageProxy.messagePropertyName = "windowPostMessageProxy";
return WindowPostMessageProxy;
}());
exports.WindowPostMessageProxy = WindowPostMessageProxy;
/***/ })
/******/ ])
});
;
/*! For license information please see windowPostMessageProxy.js.LICENSE.txt */
!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()}(self,(()=>(()=>{"use strict";var e={};return(()=>{var r=e;Object.defineProperty(r,"__esModule",{value:!0}),r.WindowPostMessageProxy=void 0;var s=function(){function e(r){void 0===r&&(r={processTrackingProperties:{addTrackingProperties:e.defaultAddTrackingProperties,getTrackingProperties:e.defaultGetTrackingProperties},isErrorMessage:e.defaultIsErrorMessage,receiveWindow:window,name:e.createRandomString()});var s=this;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: ".concat(this.name," receiving on window: ").concat(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(){var e=window.crypto||window.msCrypto,r=new Uint32Array(1);return e.getRandomValues(r),r[0].toString(36).substring(1)},e.prototype.addHandler=function(e){this.handlers.push(e)},e.prototype.removeHandler=function(e){var r=this.handlers.indexOf(e);if(-1===r)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("".concat(this.name," Posting message:")),console.log(JSON.stringify(s,null," "))),r.postMessage(s,"*");var o=e.createDeferred();return this.pendingRequestPromises[n.id]=o,o.promise},e.prototype.sendResponse=function(e,r,s){this.addTrackingProperties(r,s),this.logMessages&&(console.log("".concat(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("".concat(this.name," Received message:")),console.log("type: ".concat(e.type)),console.log(JSON.stringify(e.data,null," ")));var s=this.eventSourceOverrideWindow||e.source;if(s){var n=e.data;if("object"==typeof n){var o,t;try{o=this.getTrackingProperties(n)}catch(e){this.suppressWarnings||console.warn("Proxy(".concat(this.name,"): Error occurred when attempting to get tracking properties from incoming message:"),JSON.stringify(n,null," "),"Error: ",e)}if(o&&(t=this.pendingRequestPromises[o.id]),t){var i=!0;try{i=this.isErrorMessage(n)}catch(e){console.warn("Proxy(".concat(this.name,") Error occurred when trying to determine if message is consider an error response. Message: "),JSON.stringify(n,null,""),"Error: ",e)}i?t.reject(n):t.resolve(n),delete this.pendingRequestPromises[o.id]}else this.handlers.some((function(e){var t=!1;try{t=e.test(n)}catch(e){r.suppressWarnings||console.warn("Proxy(".concat(r.name,"): Error occurred when handler was testing incoming message:"),JSON.stringify(n,null," "),"Error: ",e)}if(t){var i=void 0;try{i=Promise.resolve(e.handle(n))}catch(e){r.suppressWarnings||console.warn("Proxy(".concat(r.name,"): Error occurred when handler was processing incoming message:"),JSON.stringify(n,null," "),"Error: ",e),i=Promise.resolve()}return i.then((function(e){if(!e){var t="Handler for message: ".concat(JSON.stringify(n,null," ")," did not return a response message. The default response message will be returned instead.");r.suppressWarnings||console.warn("Proxy(".concat(r.name,"): ").concat(t)),e={warning:t}}r.sendResponse(s,e,o)})),!0}}))||this.suppressWarnings||console.warn("Proxy(".concat(this.name,") did not handle message. Handlers: ").concat(this.handlers.length," Message: ").concat(JSON.stringify(n,null,""),"."))}else this.suppressWarnings||console.warn("Proxy(".concat(this.name,"): Received message that was not an object. Discarding message"))}},e.messagePropertyName="windowPostMessageProxy",e}();r.WindowPostMessageProxy=s})(),e})()));
//# sourceMappingURL=windowPostMessageProxy.js.map

@@ -1,2 +0,2 @@

/*! window-post-message-proxy v0.2.6 | (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(){var e=window.crypto||window.msCrypto,r=new Uint32Array(1);return e.getRandomValues(r),r[0].toString(36).substring(1)},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}])});
/*! For license information please see windowPostMessageProxy.js.LICENSE.txt */
!function(e,s){"object"==typeof exports&&"object"==typeof module?module.exports=s():"function"==typeof define&&define.amd?define([],s):"object"==typeof exports?exports["window-post-message-proxy"]=s():e["window-post-message-proxy"]=s()}(self,()=>(()=>{"use strict";var e,s,r={};return e=r,Object.defineProperty(e,"__esModule",{value:!0}),e.WindowPostMessageProxy=void 0,n.defaultAddTrackingProperties=function(e,s){return e[n.messagePropertyName]=s,e},n.defaultGetTrackingProperties=function(e){return e[n.messagePropertyName]},n.defaultIsErrorMessage=function(e){return!!e.error},n.createDeferred=function(){var r={resolve:null,reject:null,promise:null},e=new Promise(function(e,s){r.resolve=e,r.reject=s});return r.promise=e,r},n.createRandomString=function(){var e=window.crypto||window.msCrypto,s=new Uint32Array(1);return e.getRandomValues(s),s[0].toString(36).substring(1)},n.prototype.addHandler=function(e){this.handlers.push(e)},n.prototype.removeHandler=function(e){e=this.handlers.indexOf(e);if(-1===e)throw new Error("You attempted to remove a handler but no matching handler was found.");this.handlers.splice(e,1)},n.prototype.start=function(){this.receiveWindow.addEventListener("message",this.windowMessageHandler)},n.prototype.stop=function(){this.receiveWindow.removeEventListener("message",this.windowMessageHandler)},n.prototype.postMessage=function(e,s){var r={id:n.createRandomString()},e=(this.addTrackingProperties(s,r),this.logMessages&&(console.log("".concat(this.name," Posting message:")),console.log(JSON.stringify(s,null," "))),e.postMessage(s,"*"),n.createDeferred());return(this.pendingRequestPromises[r.id]=e).promise},n.prototype.sendResponse=function(e,s,r){this.addTrackingProperties(s,r),this.logMessages&&(console.log("".concat(this.name," Sending response:")),console.log(JSON.stringify(s,null," "))),e.postMessage(s,"*")},n.prototype.onMessageReceived=function(e){var r=this,n=(this.logMessages&&(console.log("".concat(this.name," Received message:")),console.log("type: ".concat(e.type)),console.log(JSON.stringify(e.data,null," "))),this.eventSourceOverrideWindow||e.source);if(n){var o,s,t=e.data;if("object"==typeof t){try{o=this.getTrackingProperties(t)}catch(e){this.suppressWarnings||console.warn("Proxy(".concat(this.name,"): Error occurred when attempting to get tracking properties from incoming message:"),JSON.stringify(t,null," "),"Error: ",e)}if(s=o?this.pendingRequestPromises[o.id]:s){var i=!0;try{i=this.isErrorMessage(t)}catch(e){console.warn("Proxy(".concat(this.name,") Error occurred when trying to determine if message is consider an error response. Message: "),JSON.stringify(t,null,""),"Error: ",e)}i?s.reject(t):s.resolve(t),delete this.pendingRequestPromises[o.id]}else this.handlers.some(function(e){var s=!1;try{s=e.test(t)}catch(e){r.suppressWarnings||console.warn("Proxy(".concat(r.name,"): Error occurred when handler was testing incoming message:"),JSON.stringify(t,null," "),"Error: ",e)}if(s){s=void 0;try{s=Promise.resolve(e.handle(t))}catch(e){r.suppressWarnings||console.warn("Proxy(".concat(r.name,"): Error occurred when handler was processing incoming message:"),JSON.stringify(t,null," "),"Error: ",e),s=Promise.resolve()}return s.then(function(e){var s;e||(s="Handler for message: ".concat(JSON.stringify(t,null," ")," did not return a response message. The default response message will be returned instead."),r.suppressWarnings||console.warn("Proxy(".concat(r.name,"): ").concat(s)),e={warning:s}),r.sendResponse(n,e,o)}),!0}})||this.suppressWarnings||console.warn("Proxy(".concat(this.name,") did not handle message. Handlers: ").concat(this.handlers.length," Message: ").concat(JSON.stringify(t,null,""),"."))}else this.suppressWarnings||console.warn("Proxy(".concat(this.name,"): Received message that was not an object. Discarding message"))}},n.messagePropertyName="windowPostMessageProxy",s=n,e.WindowPostMessageProxy=s,r;function n(e){void 0===e&&(e={processTrackingProperties:{addTrackingProperties:n.defaultAddTrackingProperties,getTrackingProperties:n.defaultGetTrackingProperties},isErrorMessage:n.defaultIsErrorMessage,receiveWindow:window,name:n.createRandomString()});var s=this;this.pendingRequestPromises={},this.addTrackingProperties=e.processTrackingProperties&&e.processTrackingProperties.addTrackingProperties||n.defaultAddTrackingProperties,this.getTrackingProperties=e.processTrackingProperties&&e.processTrackingProperties.getTrackingProperties||n.defaultGetTrackingProperties,this.isErrorMessage=e.isErrorMessage||n.defaultIsErrorMessage,this.receiveWindow=e.receiveWindow||window,this.name=e.name||n.createRandomString(),this.logMessages=e.logMessages||!1,this.eventSourceOverrideWindow=e.eventSourceOverrideWindow,this.suppressWarnings=e.suppressWarnings||!1,this.logMessages&&console.log("new WindowPostMessageProxy created with name: ".concat(this.name," receiving on window: ").concat(this.receiveWindow.document.title)),this.handlers=[],this.windowMessageHandler=function(e){return s.onMessageReceived(e)},this.start()}})());
{
"name": "window-post-message-proxy",
"version": "0.2.6",
"version": "0.2.7",
"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",
"main": "dist/windowPostMessageProxy.js",
"typings": "dist/windowPostMessageProxy.d.ts",
"types": "dist/windowPostMessageProxy.d.ts",
"scripts": {
"build": "gulp build",
"test": "gulp test",
"prepublish": "typings install && gulp build",
"gulp": "gulp",
"typings": "typings"
"gulp": "gulp"
},

@@ -35,32 +33,47 @@ "repository": {

"devDependencies": {
"@types/es6-promise": "^3.3.0",
"@types/jasmine": "^3.5.10",
"@types/jquery": "^3.3.34",
"@types/karma-jasmine": "^3.1.0",
"@types/node": "^14.14.7",
"@types/npm": "^2.0.31",
"@typescript-eslint/eslint-plugin": "^4.7.0",
"@typescript-eslint/parser": "^4.7.0",
"del": "^2.2.1",
"gulp": "^3.9.1",
"eslint": "^7.13.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsdoc": "^30.7.7",
"eslint-plugin-prefer-arrow": "^1.2.2",
"gulp": "^4.0.2",
"gulp-eslint": "^6.0.0",
"gulp-gh-pages": "^0.5.4",
"gulp-header": "^1.8.7",
"gulp-help": "^1.6.1",
"gulp-help-four": "^0.2.3",
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.5.4",
"gulp-tslint": "^6.1.1",
"gulp-typedoc": "^2.0.0",
"gulp-uglify": "^1.5.3",
"gulp-uglify": "^3.0.2",
"gulp4-run-sequence": "^1.0.1",
"jasmine-core": "^2.4.1",
"jquery": "^3.3.1",
"karma": "^0.13.22",
"jquery": "^3.6.0",
"karma": "^6.3.17",
"karma-chrome-launcher": "^1.0.1",
"karma-coverage": "^0.5.5",
"karma-jasmine": "^0.3.8",
"karma-phantomjs-launcher": "^1.0.0",
"karma-coverage": "^2.2.0",
"karma-jasmine": "^1.0.2",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "0.0.26",
"moment": "^2.14.1",
"phantomjs-prebuilt": "^2.1.7",
"run-sequence": "^1.1.5",
"ts-loader": "^0.8.2",
"tslint": "^3.15.0",
"typedoc": "^0.4.4",
"typescript": "^1.8.10",
"typings": "^1.3.2",
"webpack-stream": "^3.2.0",
"yargs": "^4.6.0"
"ts-loader": "^9.2.8",
"typedoc": "^0.22.15",
"typescript": "^4.4.4",
"uglify-save-license": "^0.4.1",
"webpack": "^5.71.0",
"webpack-stream": "^7.0.0",
"yargs": "^17.4.1"
},
"overrides": {
"glob-parent": "^6.0.2",
"lodash.template": "^4.5.0",
"micromatch": "^4.0.5"
},
"publishConfig": {

@@ -67,0 +80,0 @@ "tag": "beta"

# window-post-message-proxy
[![Travis branch](https://img.shields.io/travis/Microsoft/window-post-message-proxy.svg)](https://travis-ci.org/Microsoft/window-post-message-proxy)
[![npm](https://img.shields.io/npm/v/window-post-message-proxy.svg)](https://www.npmjs.com/package/window-post-message-proxy)

@@ -8,7 +7,7 @@ [![Total Downloads](https://img.shields.io/npm/dt/window-post-message-proxy.svg)](https://www.npmjs.com/package/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 asynchronous 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 allows for 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 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.
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.
The original sending instance then receives the response message with the ID and looks to see if there is a matching id in its cache. If so, it resolves the deferred object with the response.

@@ -36,5 +35,5 @@ ## Documentation

windowPostMessageProxy.postMessage(iframe.conentWindow, message)
windowPostMessageProxy.postMessage(iframe.contentWindow, message)
.then(response => {
});

@@ -47,3 +46,3 @@ ```

By default the windowPostMessage proxy will store the tracking properties as object on the message by known property: `windowPostMesssageProxy`.
By default, the windowPostMessage proxy will store the tracking properties as an object on the message named `windowPostMessageProxy`.

@@ -57,3 +56,3 @@ This means if you call:

windowPostMessageProxy.postMessage(iframe.conentWindow, message);
windowPostMessageProxy.postMessage(iframe.contentWindow, message);
```

@@ -71,3 +70,3 @@ The message is actually modified before it's sent to become:

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:
If you want to customize how the tracking properties are added to and retrieved from the message, you can pass settings to the constructor in the form of an object with two functions:

@@ -92,3 +91,3 @@ ```typescript

};
return message;

@@ -107,5 +106,5 @@ },

By default response messages are considered error message if they contain an error property.
By default, response messages are considered error messages if they contain an error property.
You can override this behavior by passing an `isErrorMessage` function at construction time. See interface:
You can override this behavior by passing an `isErrorMessage` function at construction time:

@@ -130,3 +129,3 @@ ```typescript

By default messages 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.

@@ -140,3 +139,3 @@ ```typescript

Each windowPostMessageProxy gives itself a randomly generated name so you can see which instance is communicating in the log messages.
Often times you may want to pass a custom name for which window the windowPostMessageProxy instance is running.
Often times you may want to pass a custom name for the window on which the windowPostMessageProxy instance is running.

@@ -150,7 +149,7 @@ You can provided a name by passing `name: 'Iframe'` in the options object.

### Supress Warning Message about unhandled messages
By default the window post message proxy will warn you if it received a message that was not handled since this is usually an indication of error; however,
if you are register multiple window message handlers the message may handled but it's just not able to be known by the windowPostMessageProxy and this warning no longer applies.
By default, the window post message proxy will warn you if it receives a message that was not handled, since this is usually an indication of error. However,
if you register multiple window message handlers, the message may in fact be handled despite being unknown to the windowPostMessageProxy. In cases like this, this warning no longer applies, so you can disable it by setting `suppressWarnings: true`:
```typescript
const windowPostMessageProxy = new WindowPostMessageProxy({ suppressMessageNotHandledWarning: true });
const windowPostMessageProxy = new WindowPostMessageProxy({ suppressWarnings: true });
```

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc