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

post-me

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

post-me - npm Package Compare versions

Comparing version 0.4.1 to 0.4.2

dist/index.es5.mjs

1378

dist/index.js

@@ -0,877 +1,879 @@

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.PostMe = {}));
}(this, (function (exports) { 'use strict';
if (typeof define === "function" && define.amd) {
define("PostMe", ["exports"], factory);
} else if (typeof exports !== "undefined") {
factory(exports);
} else {
var mod = {
exports: {}
};
factory(mod.exports);
global.PostMe = mod.exports;
}
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports) {
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(_exports, "__esModule", {
value: true
});
_exports.ChildHandshake = ChildHandshake;
_exports.DebugMessenger = DebugMessenger;
_exports.ParentHandshake = ParentHandshake;
_exports.debug = _exports.WorkerMessenger = _exports.WindowMessenger = _exports.PortMessenger = _exports.BareMessenger = void 0;
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var MARKER = '@post-me';
var MARKER = '@post-me';
function createUniqueIdFn() {
var __id = 0;
return function () {
var id = __id;
__id += 1;
return id;
};
}
function createUniqueIdFn() {
var __id = 0;
return function () {
var id = __id;
__id += 1;
return id;
};
}
var Emitter = /*#__PURE__*/function () {
function Emitter() {
_classCallCheck(this, Emitter);
var Emitter = /*#__PURE__*/function () {
function Emitter() {
_classCallCheck(this, Emitter);
this._listeners = {};
}
this._listeners = {};
}
_createClass(Emitter, [{
key: "addEventListener",
value: function addEventListener(eventName, listener) {
var listeners = this._listeners[eventName];
_createClass(Emitter, [{
key: "addEventListener",
value: function addEventListener(eventName, listener) {
var listeners = this._listeners[eventName];
if (!listeners) {
listeners = new Set();
this._listeners[eventName] = listeners;
}
listeners.add(listener);
if (!listeners) {
listeners = new Set();
this._listeners[eventName] = listeners;
}
}, {
key: "removeEventListener",
value: function removeEventListener(eventName, listener) {
var listeners = this._listeners[eventName];
if (!listeners) {
return;
}
listeners.add(listener);
}
}, {
key: "removeEventListener",
value: function removeEventListener(eventName, listener) {
var listeners = this._listeners[eventName];
listeners["delete"](listener);
if (!listeners) {
return;
}
}, {
key: "once",
value: function once(eventName) {
var _this = this;
return new Promise(function (resolve) {
var listener = function listener(data) {
_this.removeEventListener(eventName, listener);
listeners["delete"](listener);
}
}, {
key: "once",
value: function once(eventName) {
var _this = this;
resolve(data);
};
return new Promise(function (resolve) {
var listener = function listener(data) {
_this.removeEventListener(eventName, listener);
_this.addEventListener(eventName, listener);
});
}
}, {
key: "emit",
value: function emit(eventName, data) {
var listeners = this._listeners[eventName];
resolve(data);
};
if (!listeners) {
return;
}
_this.addEventListener(eventName, listener);
});
}
}, {
key: "emit",
value: function emit(eventName, data) {
var listeners = this._listeners[eventName];
listeners.forEach(function (listener) {
listener(data);
});
if (!listeners) {
return;
}
}, {
key: "removeAllListeners",
value: function removeAllListeners() {
Object.values(this._listeners).forEach(function (listeners) {
if (listeners) {
listeners.clear();
}
});
}
}]);
return Emitter;
}();
listeners.forEach(function (listener) {
listener(data);
});
}
}, {
key: "removeAllListeners",
value: function removeAllListeners() {
Object.values(this._listeners).forEach(function (listeners) {
if (listeners) {
listeners.clear();
}
});
}
}]);
var MessageType;
return Emitter;
}();
(function (MessageType) {
MessageType["HandshakeRequest"] = "handshake-request";
MessageType["HandshakeResponse"] = "handshake-response";
MessageType["Call"] = "call";
MessageType["Response"] = "response";
MessageType["Error"] = "error";
MessageType["Event"] = "event";
MessageType["Callback"] = "callback";
})(MessageType || (MessageType = {})); // Message Creators
var MessageType;
(function (MessageType) {
MessageType["HandshakeRequest"] = "handshake-request";
MessageType["HandshakeResponse"] = "handshake-response";
MessageType["Call"] = "call";
MessageType["Response"] = "response";
MessageType["Error"] = "error";
MessageType["Event"] = "event";
MessageType["Callback"] = "callback";
})(MessageType || (MessageType = {})); // Message Creators
function createHandshakeRequestMessage(sessionId) {
return {
type: MARKER,
action: MessageType.HandshakeRequest,
sessionId: sessionId
};
}
function createHandshakeResponseMessage(sessionId) {
return {
type: MARKER,
action: MessageType.HandshakeResponse,
sessionId: sessionId
};
}
function createHandshakeRequestMessage(sessionId) {
return {
type: MARKER,
action: MessageType.HandshakeRequest,
sessionId: sessionId
};
}
function createCallMessage(sessionId, requestId, methodName, args) {
return {
type: MARKER,
action: MessageType.Call,
sessionId: sessionId,
requestId: requestId,
methodName: methodName,
args: args
};
}
function createHandshakeResponseMessage(sessionId) {
return {
type: MARKER,
action: MessageType.HandshakeResponse,
sessionId: sessionId
};
}
function createResponsMessage(sessionId, requestId, result, error) {
var message = {
type: MARKER,
action: MessageType.Response,
sessionId: sessionId,
requestId: requestId
};
function createCallMessage(sessionId, requestId, methodName, args) {
return {
type: MARKER,
action: MessageType.Call,
sessionId: sessionId,
requestId: requestId,
methodName: methodName,
args: args
};
}
if (result !== undefined) {
message.result = result;
}
function createResponsMessage(sessionId, requestId, result, error) {
var message = {
type: MARKER,
action: MessageType.Response,
sessionId: sessionId,
requestId: requestId
};
if (error !== undefined) {
message.error = error;
}
return message;
if (result !== undefined) {
message.result = result;
}
function createCallbackMessage(sessionId, requestId, callbackId, args) {
return {
type: MARKER,
action: MessageType.Callback,
sessionId: sessionId,
requestId: requestId,
callbackId: callbackId,
args: args
};
if (error !== undefined) {
message.error = error;
}
function createEventMessage(sessionId, eventName, payload) {
return {
type: MARKER,
action: MessageType.Event,
sessionId: sessionId,
eventName: eventName,
payload: payload
};
} // Type Guards
return message;
}
function createCallbackMessage(sessionId, requestId, callbackId, args) {
return {
type: MARKER,
action: MessageType.Callback,
sessionId: sessionId,
requestId: requestId,
callbackId: callbackId,
args: args
};
}
function isMessage(m) {
return m.type === MARKER;
}
function createEventMessage(sessionId, eventName, payload) {
return {
type: MARKER,
action: MessageType.Event,
sessionId: sessionId,
eventName: eventName,
payload: payload
};
} // Type Guards
function isHandshakeRequestMessage(m) {
return isMessage(m) && m.action === MessageType.HandshakeRequest;
}
function isHandshakeResponseMessage(m) {
return isMessage(m) && m.action === MessageType.HandshakeResponse;
}
function isMessage(m) {
return m.type === MARKER;
}
function isCallMessage(m) {
return isMessage(m) && m.action === MessageType.Call;
}
function isHandshakeRequestMessage(m) {
return isMessage(m) && m.action === MessageType.HandshakeRequest;
}
function isResponseMessage(m) {
return isMessage(m) && m.action === MessageType.Response;
}
function isHandshakeResponseMessage(m) {
return isMessage(m) && m.action === MessageType.HandshakeResponse;
}
function isCallbackMessage(m) {
return isMessage(m) && m.action === MessageType.Callback;
}
function isCallMessage(m) {
return isMessage(m) && m.action === MessageType.Call;
}
function isEventMessage(m) {
return isMessage(m) && m.action === MessageType.Event;
}
function isResponseMessage(m) {
return isMessage(m) && m.action === MessageType.Response;
}
function makeCallbackEvent(requestId) {
return "callback_".concat(requestId);
}
function isCallbackMessage(m) {
return isMessage(m) && m.action === MessageType.Callback;
}
function makeResponseEvent(requestId) {
return "response_".concat(requestId);
}
function isEventMessage(m) {
return isMessage(m) && m.action === MessageType.Event;
}
var Dispatcher = /*#__PURE__*/function (_Emitter) {
_inherits(Dispatcher, _Emitter);
function makeCallbackEvent(requestId) {
return "callback_".concat(requestId);
}
var _super = _createSuper(Dispatcher);
function makeResponseEvent(requestId) {
return "response_".concat(requestId);
}
function Dispatcher(messenger, sessionId) {
var _this2;
var Dispatcher = /*#__PURE__*/function (_Emitter) {
_inherits(Dispatcher, _Emitter);
_classCallCheck(this, Dispatcher);
var _super = _createSuper(Dispatcher);
_this2 = _super.call(this);
_this2.uniqueId = createUniqueIdFn();
_this2.messenger = messenger;
_this2.sessionId = sessionId;
_this2.removeMessengerListener = _this2.messenger.addMessageListener(_this2.messengerListener.bind(_assertThisInitialized(_this2)));
return _this2;
}
function Dispatcher(messenger, sessionId) {
var _this2;
_createClass(Dispatcher, [{
key: "messengerListener",
value: function messengerListener(event) {
var data = event.data;
_classCallCheck(this, Dispatcher);
if (!isMessage(data)) {
return;
}
_this2 = _super.call(this);
_this2.uniqueId = createUniqueIdFn();
_this2.messenger = messenger;
_this2.sessionId = sessionId;
_this2.removeMessengerListener = _this2.messenger.addMessageListener(_this2.messengerListener.bind(_assertThisInitialized(_this2)));
return _this2;
}
if (this.sessionId !== data.sessionId) {
return;
}
_createClass(Dispatcher, [{
key: "messengerListener",
value: function messengerListener(event) {
var data = event.data;
if (isCallMessage(data)) {
this.emit(MessageType.Call, data);
} else if (isResponseMessage(data)) {
this.emit(makeResponseEvent(data.requestId), data);
} else if (isEventMessage(data)) {
this.emit(MessageType.Event, data);
} else if (isCallbackMessage(data)) {
this.emit(makeCallbackEvent(data.requestId), data);
}
if (!isMessage(data)) {
return;
}
}, {
key: "callOnRemote",
value: function callOnRemote(methodName, args, transfer) {
var requestId = this.uniqueId();
var callbackEvent = makeCallbackEvent(requestId);
var responseEvent = makeResponseEvent(requestId);
var message = createCallMessage(this.sessionId, requestId, methodName, args);
this.messenger.postMessage(message, transfer);
return {
callbackEvent: callbackEvent,
responseEvent: responseEvent
};
if (this.sessionId !== data.sessionId) {
return;
}
}, {
key: "respondToRemote",
value: function respondToRemote(requestId, value, error, transfer) {
var message = createResponsMessage(this.sessionId, requestId, value, error);
this.messenger.postMessage(message, transfer);
if (isCallMessage(data)) {
this.emit(MessageType.Call, data);
} else if (isResponseMessage(data)) {
this.emit(makeResponseEvent(data.requestId), data);
} else if (isEventMessage(data)) {
this.emit(MessageType.Event, data);
} else if (isCallbackMessage(data)) {
this.emit(makeCallbackEvent(data.requestId), data);
}
}, {
key: "callbackToRemote",
value: function callbackToRemote(requestId, callbackId, args) {
var message = createCallbackMessage(this.sessionId, requestId, callbackId, args);
this.messenger.postMessage(message);
}
}, {
key: "emitToRemote",
value: function emitToRemote(eventName, payload, transfer) {
var message = createEventMessage(this.sessionId, eventName, payload);
this.messenger.postMessage(message, transfer);
}
}, {
key: "close",
value: function close() {
this.removeMessengerListener();
this.removeAllListeners();
}
}]);
}
}, {
key: "callOnRemote",
value: function callOnRemote(methodName, args, transfer) {
var requestId = this.uniqueId();
var callbackEvent = makeCallbackEvent(requestId);
var responseEvent = makeResponseEvent(requestId);
var message = createCallMessage(this.sessionId, requestId, methodName, args);
this.messenger.postMessage(message, transfer);
return {
callbackEvent: callbackEvent,
responseEvent: responseEvent
};
}
}, {
key: "respondToRemote",
value: function respondToRemote(requestId, value, error, transfer) {
var message = createResponsMessage(this.sessionId, requestId, value, error);
this.messenger.postMessage(message, transfer);
}
}, {
key: "callbackToRemote",
value: function callbackToRemote(requestId, callbackId, args) {
var message = createCallbackMessage(this.sessionId, requestId, callbackId, args);
this.messenger.postMessage(message);
}
}, {
key: "emitToRemote",
value: function emitToRemote(eventName, payload, transfer) {
var message = createEventMessage(this.sessionId, eventName, payload);
this.messenger.postMessage(message, transfer);
}
}, {
key: "close",
value: function close() {
this.removeMessengerListener();
this.removeAllListeners();
}
}]);
return Dispatcher;
}(Emitter);
return Dispatcher;
}(Emitter);
var ParentHandshakeDispatcher = /*#__PURE__*/function (_Emitter2) {
_inherits(ParentHandshakeDispatcher, _Emitter2);
var ParentHandshakeDispatcher = /*#__PURE__*/function (_Emitter2) {
_inherits(ParentHandshakeDispatcher, _Emitter2);
var _super2 = _createSuper(ParentHandshakeDispatcher);
var _super2 = _createSuper(ParentHandshakeDispatcher);
function ParentHandshakeDispatcher(messenger, sessionId) {
var _this3;
function ParentHandshakeDispatcher(messenger, sessionId) {
var _this3;
_classCallCheck(this, ParentHandshakeDispatcher);
_classCallCheck(this, ParentHandshakeDispatcher);
_this3 = _super2.call(this);
_this3.messenger = messenger;
_this3.sessionId = sessionId;
_this3.removeMessengerListener = _this3.messenger.addMessageListener(_this3.messengerListener.bind(_assertThisInitialized(_this3)));
return _this3;
}
_this3 = _super2.call(this);
_this3.messenger = messenger;
_this3.sessionId = sessionId;
_this3.removeMessengerListener = _this3.messenger.addMessageListener(_this3.messengerListener.bind(_assertThisInitialized(_this3)));
return _this3;
}
_createClass(ParentHandshakeDispatcher, [{
key: "messengerListener",
value: function messengerListener(event) {
var data = event.data;
_createClass(ParentHandshakeDispatcher, [{
key: "messengerListener",
value: function messengerListener(event) {
var data = event.data;
if (!isMessage(data)) {
return;
}
if (!isMessage(data)) {
return;
}
if (this.sessionId !== data.sessionId) {
return;
}
if (this.sessionId !== data.sessionId) {
return;
}
if (isHandshakeResponseMessage(data)) {
this.emit(data.sessionId, data);
}
if (isHandshakeResponseMessage(data)) {
this.emit(data.sessionId, data);
}
}, {
key: "initiateHandshake",
value: function initiateHandshake() {
var message = createHandshakeRequestMessage(this.sessionId);
this.messenger.postMessage(message);
return this.sessionId;
}
}, {
key: "close",
value: function close() {
this.removeMessengerListener();
this.removeAllListeners();
}
}]);
}
}, {
key: "initiateHandshake",
value: function initiateHandshake() {
var message = createHandshakeRequestMessage(this.sessionId);
this.messenger.postMessage(message);
return this.sessionId;
}
}, {
key: "close",
value: function close() {
this.removeMessengerListener();
this.removeAllListeners();
}
}]);
return ParentHandshakeDispatcher;
}(Emitter);
return ParentHandshakeDispatcher;
}(Emitter);
var ChildHandshakeDispatcher = /*#__PURE__*/function (_Emitter3) {
_inherits(ChildHandshakeDispatcher, _Emitter3);
var ChildHandshakeDispatcher = /*#__PURE__*/function (_Emitter3) {
_inherits(ChildHandshakeDispatcher, _Emitter3);
var _super3 = _createSuper(ChildHandshakeDispatcher);
var _super3 = _createSuper(ChildHandshakeDispatcher);
function ChildHandshakeDispatcher(messenger) {
var _this4;
function ChildHandshakeDispatcher(messenger) {
var _this4;
_classCallCheck(this, ChildHandshakeDispatcher);
_classCallCheck(this, ChildHandshakeDispatcher);
_this4 = _super3.call(this);
_this4.messenger = messenger;
_this4.removeMessengerListener = _this4.messenger.addMessageListener(_this4.messengerListener.bind(_assertThisInitialized(_this4)));
return _this4;
}
_this4 = _super3.call(this);
_this4.messenger = messenger;
_this4.removeMessengerListener = _this4.messenger.addMessageListener(_this4.messengerListener.bind(_assertThisInitialized(_this4)));
return _this4;
}
_createClass(ChildHandshakeDispatcher, [{
key: "messengerListener",
value: function messengerListener(event) {
var data = event.data;
_createClass(ChildHandshakeDispatcher, [{
key: "messengerListener",
value: function messengerListener(event) {
var data = event.data;
if (isHandshakeRequestMessage(data)) {
this.emit(MessageType.HandshakeRequest, data);
}
if (isHandshakeRequestMessage(data)) {
this.emit(MessageType.HandshakeRequest, data);
}
}, {
key: "acceptHandshake",
value: function acceptHandshake(sessionId) {
var message = createHandshakeResponseMessage(sessionId);
this.messenger.postMessage(message);
}
}, {
key: "close",
value: function close() {
this.removeMessengerListener();
this.removeAllListeners();
}
}]);
}
}, {
key: "acceptHandshake",
value: function acceptHandshake(sessionId) {
var message = createHandshakeResponseMessage(sessionId);
this.messenger.postMessage(message);
}
}, {
key: "close",
value: function close() {
this.removeMessengerListener();
this.removeAllListeners();
}
}]);
return ChildHandshakeDispatcher;
}(Emitter);
return ChildHandshakeDispatcher;
}(Emitter);
var ProxyType;
var ProxyType;
(function (ProxyType) {
ProxyType["Callback"] = "callback";
})(ProxyType || (ProxyType = {}));
(function (ProxyType) {
ProxyType["Callback"] = "callback";
})(ProxyType || (ProxyType = {}));
function createCallbackProxy(callbackId) {
return {
type: MARKER,
proxy: ProxyType.Callback,
callbackId: callbackId
};
}
function createCallbackProxy(callbackId) {
return {
type: MARKER,
proxy: ProxyType.Callback,
callbackId: callbackId
};
}
function isCallbackProxy(p) {
return p.type === MARKER && p.proxy === ProxyType.Callback;
}
function isCallbackProxy(p) {
return p.type === MARKER && p.proxy === ProxyType.Callback;
}
var ConcreteRemoteHandle = /*#__PURE__*/function (_Emitter4) {
_inherits(ConcreteRemoteHandle, _Emitter4);
var ConcreteRemoteHandle = /*#__PURE__*/function (_Emitter4) {
_inherits(ConcreteRemoteHandle, _Emitter4);
var _super4 = _createSuper(ConcreteRemoteHandle);
var _super4 = _createSuper(ConcreteRemoteHandle);
function ConcreteRemoteHandle(dispatcher) {
var _this5;
function ConcreteRemoteHandle(dispatcher) {
var _this5;
_classCallCheck(this, ConcreteRemoteHandle);
_classCallCheck(this, ConcreteRemoteHandle);
_this5 = _super4.call(this);
_this5._dispatcher = dispatcher;
_this5._callTransfer = {};
_this5 = _super4.call(this);
_this5._dispatcher = dispatcher;
_this5._callTransfer = {};
_this5._dispatcher.addEventListener(MessageType.Event, _this5._handleEvent.bind(_assertThisInitialized(_this5)));
_this5._dispatcher.addEventListener(MessageType.Event, _this5._handleEvent.bind(_assertThisInitialized(_this5)));
return _this5;
return _this5;
}
_createClass(ConcreteRemoteHandle, [{
key: "close",
value: function close() {
this.removeAllListeners();
}
}, {
key: "setCallTransfer",
value: function setCallTransfer(methodName, transfer) {
this._callTransfer[methodName] = transfer;
}
}, {
key: "call",
value: function call(methodName) {
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
_createClass(ConcreteRemoteHandle, [{
key: "close",
value: function close() {
this.removeAllListeners();
}
}, {
key: "setCallTransfer",
value: function setCallTransfer(methodName, transfer) {
this._callTransfer[methodName] = transfer;
}
}, {
key: "call",
value: function call(methodName) {
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
return this.customCall(methodName, args);
}
}, {
key: "customCall",
value: function customCall(methodName, args) {
var _this6 = this;
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return new Promise(function (resolve, reject) {
var sanitizedArgs = [];
var callbacks = [];
var callbackId = 0;
args.forEach(function (arg) {
if (typeof arg === 'function') {
callbacks.push(arg);
sanitizedArgs.push(createCallbackProxy(callbackId));
callbackId += 1;
} else {
sanitizedArgs.push(arg);
}
});
var hasCallbacks = callbacks.length > 0;
var callbackListener = undefined;
if (hasCallbacks) {
callbackListener = function callbackListener(data) {
var callbackId = data.callbackId,
args = data.args;
callbacks[callbackId].apply(callbacks, _toConsumableArray(args));
};
}
return this.customCall(methodName, args);
}
}, {
key: "customCall",
value: function customCall(methodName, args) {
var _this6 = this;
var transfer = options.transfer;
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return new Promise(function (resolve, reject) {
var sanitizedArgs = [];
var callbacks = [];
var callbackId = 0;
args.forEach(function (arg) {
if (typeof arg === 'function') {
callbacks.push(arg);
sanitizedArgs.push(createCallbackProxy(callbackId));
callbackId += 1;
} else {
sanitizedArgs.push(arg);
}
});
var hasCallbacks = callbacks.length > 0;
var callbackListener = undefined;
if (transfer === undefined && _this6._callTransfer[methodName]) {
var _this6$_callTransfer;
if (hasCallbacks) {
callbackListener = function callbackListener(data) {
var callbackId = data.callbackId,
args = data.args;
callbacks[callbackId].apply(callbacks, _toConsumableArray(args));
};
}
transfer = (_this6$_callTransfer = _this6._callTransfer)[methodName].apply(_this6$_callTransfer, sanitizedArgs);
}
var transfer = options.transfer;
var _this6$_dispatcher$ca = _this6._dispatcher.callOnRemote(methodName, sanitizedArgs, transfer),
callbackEvent = _this6$_dispatcher$ca.callbackEvent,
responseEvent = _this6$_dispatcher$ca.responseEvent;
if (transfer === undefined && _this6._callTransfer[methodName]) {
var _this6$_callTransfer;
if (hasCallbacks) {
_this6._dispatcher.addEventListener(callbackEvent, callbackListener);
}
transfer = (_this6$_callTransfer = _this6._callTransfer)[methodName].apply(_this6$_callTransfer, sanitizedArgs);
_this6._dispatcher.once(responseEvent).then(function (response) {
if (callbackListener) {
_this6._dispatcher.removeEventListener(callbackEvent, callbackListener);
}
var _this6$_dispatcher$ca = _this6._dispatcher.callOnRemote(methodName, sanitizedArgs, transfer),
callbackEvent = _this6$_dispatcher$ca.callbackEvent,
responseEvent = _this6$_dispatcher$ca.responseEvent;
var result = response.result,
error = response.error;
if (hasCallbacks) {
_this6._dispatcher.addEventListener(callbackEvent, callbackListener);
if (error !== undefined) {
reject(error);
} else {
resolve(result);
}
});
});
}
}, {
key: "_handleEvent",
value: function _handleEvent(data) {
var eventName = data.eventName,
payload = data.payload;
this.emit(eventName, payload);
}
}]);
_this6._dispatcher.once(responseEvent).then(function (response) {
if (callbackListener) {
_this6._dispatcher.removeEventListener(callbackEvent, callbackListener);
}
return ConcreteRemoteHandle;
}(Emitter);
var result = response.result,
error = response.error;
var ConcreteLocalHandle = /*#__PURE__*/function () {
function ConcreteLocalHandle(dispatcher, localMethods) {
_classCallCheck(this, ConcreteLocalHandle);
if (error !== undefined) {
reject(error);
} else {
resolve(result);
}
});
});
}
}, {
key: "_handleEvent",
value: function _handleEvent(data) {
var eventName = data.eventName,
payload = data.payload;
this.emit(eventName, payload);
}
}]);
this._dispatcher = dispatcher;
this._methods = localMethods;
this._returnTransfer = {};
this._emitTransfer = {};
return ConcreteRemoteHandle;
}(Emitter);
this._dispatcher.addEventListener(MessageType.Call, this._handleCall.bind(this));
}
var ConcreteLocalHandle = /*#__PURE__*/function () {
function ConcreteLocalHandle(dispatcher, localMethods) {
_classCallCheck(this, ConcreteLocalHandle);
_createClass(ConcreteLocalHandle, [{
key: "emit",
value: function emit(eventName, payload) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var transfer = options.transfer;
this._dispatcher = dispatcher;
this._methods = localMethods;
this._returnTransfer = {};
this._emitTransfer = {};
if (transfer === undefined && this._emitTransfer[eventName]) {
transfer = this._emitTransfer[eventName](payload);
}
this._dispatcher.addEventListener(MessageType.Call, this._handleCall.bind(this));
this._dispatcher.emitToRemote(eventName, payload, transfer);
}
}, {
key: "setReturnTransfer",
value: function setReturnTransfer(methodName, transfer) {
this._returnTransfer[methodName] = transfer;
}
}, {
key: "setEmitTransfer",
value: function setEmitTransfer(eventName, transfer) {
this._emitTransfer[eventName] = transfer;
}
}, {
key: "_handleCall",
value: function _handleCall(data) {
var _this7 = this;
_createClass(ConcreteLocalHandle, [{
key: "emit",
value: function emit(eventName, payload) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var transfer = options.transfer;
var requestId = data.requestId,
methodName = data.methodName,
args = data.args;
var callMethod = new Promise(function (resolve, reject) {
var method = _this7._methods[methodName];
if (transfer === undefined && this._emitTransfer[eventName]) {
transfer = this._emitTransfer[eventName](payload);
if (typeof method !== 'function') {
reject(new Error("The method \"".concat(methodName, "\" has not been implemented.")));
return;
}
this._dispatcher.emitToRemote(eventName, payload, transfer);
}
}, {
key: "setReturnTransfer",
value: function setReturnTransfer(methodName, transfer) {
this._returnTransfer[methodName] = transfer;
}
}, {
key: "setEmitTransfer",
value: function setEmitTransfer(eventName, transfer) {
this._emitTransfer[eventName] = transfer;
}
}, {
key: "_handleCall",
value: function _handleCall(data) {
var _this7 = this;
var desanitizedArgs = args.map(function (arg) {
if (isCallbackProxy(arg)) {
var callbackId = arg.callbackId;
return function () {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
var requestId = data.requestId,
methodName = data.methodName,
args = data.args;
var callMethod = new Promise(function (resolve, reject) {
var method = _this7._methods[methodName];
if (typeof method !== 'function') {
reject(new Error("The method \"".concat(methodName, "\" has not been implemented.")));
return;
_this7._dispatcher.callbackToRemote(requestId, callbackId, args);
};
} else {
return arg;
}
});
Promise.resolve(method.apply(void 0, _toConsumableArray(desanitizedArgs))).then(resolve)["catch"](reject);
});
callMethod.then(function (result) {
var transfer;
var desanitizedArgs = args.map(function (arg) {
if (isCallbackProxy(arg)) {
var callbackId = arg.callbackId;
return function () {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
if (_this7._returnTransfer[methodName]) {
transfer = _this7._returnTransfer[methodName](result);
}
_this7._dispatcher.callbackToRemote(requestId, callbackId, args);
};
} else {
return arg;
}
});
Promise.resolve(method.apply(void 0, _toConsumableArray(desanitizedArgs))).then(resolve)["catch"](reject);
});
callMethod.then(function (result) {
var transfer;
_this7._dispatcher.respondToRemote(requestId, result, undefined, transfer);
})["catch"](function (error) {
_this7._dispatcher.respondToRemote(requestId, undefined, error);
});
}
}]);
if (_this7._returnTransfer[methodName]) {
transfer = _this7._returnTransfer[methodName](result);
}
return ConcreteLocalHandle;
}();
_this7._dispatcher.respondToRemote(requestId, result, undefined, transfer);
})["catch"](function (error) {
_this7._dispatcher.respondToRemote(requestId, undefined, error);
});
}
}]);
var ConcreteConnection = /*#__PURE__*/function () {
function ConcreteConnection(dispatcher, localMethods) {
_classCallCheck(this, ConcreteConnection);
return ConcreteLocalHandle;
}();
this._dispatcher = dispatcher;
this._localHandle = new ConcreteLocalHandle(dispatcher, localMethods);
this._remoteHandle = new ConcreteRemoteHandle(dispatcher);
}
var ConcreteConnection = /*#__PURE__*/function () {
function ConcreteConnection(dispatcher, localMethods) {
_classCallCheck(this, ConcreteConnection);
_createClass(ConcreteConnection, [{
key: "close",
value: function close() {
this._dispatcher.close();
this._dispatcher = dispatcher;
this._localHandle = new ConcreteLocalHandle(dispatcher, localMethods);
this._remoteHandle = new ConcreteRemoteHandle(dispatcher);
this.remoteHandle().close();
}
}, {
key: "localHandle",
value: function localHandle() {
return this._localHandle;
}
}, {
key: "remoteHandle",
value: function remoteHandle() {
return this._remoteHandle;
}
}]);
_createClass(ConcreteConnection, [{
key: "close",
value: function close() {
this._dispatcher.close();
return ConcreteConnection;
}();
this.remoteHandle().close();
}
}, {
key: "localHandle",
value: function localHandle() {
return this._localHandle;
}
}, {
key: "remoteHandle",
value: function remoteHandle() {
return this._remoteHandle;
}
}]);
var uniqueSessionId = createUniqueIdFn();
return ConcreteConnection;
}();
var runUntil = function runUntil(worker, condition, unfulfilled, maxAttempts, attemptInterval) {
var attempt = 0;
var uniqueSessionId = createUniqueIdFn();
var fn = function fn() {
if (!condition() && (attempt < maxAttempts || maxAttempts < 1)) {
worker();
attempt += 1;
setTimeout(fn, attemptInterval);
} else if (!condition() && attempt >= maxAttempts && maxAttempts >= 1) {
unfulfilled();
}
};
var runUntil = function runUntil(worker, condition, unfulfilled, maxAttempts, attemptInterval) {
var attempt = 0;
fn();
};
var fn = function fn() {
if (!condition() && (attempt < maxAttempts || maxAttempts < 1)) {
worker();
attempt += 1;
setTimeout(fn, attemptInterval);
} else if (!condition() && attempt >= maxAttempts && maxAttempts >= 1) {
unfulfilled();
}
};
function ParentHandshake(messenger) {
var localMethods = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var maxAttempts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 5;
var attemptsInterval = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 100;
var thisSessionId = uniqueSessionId();
var connected = false;
return new Promise(function (resolve, reject) {
var handshakeDispatcher = new ParentHandshakeDispatcher(messenger, thisSessionId);
handshakeDispatcher.once(thisSessionId).then(function (response) {
connected = true;
handshakeDispatcher.close();
var sessionId = response.sessionId;
var dispatcher = new Dispatcher(messenger, sessionId);
var connection = new ConcreteConnection(dispatcher, localMethods);
resolve(connection);
});
runUntil(function () {
return handshakeDispatcher.initiateHandshake();
}, function () {
return connected;
}, function () {
return reject(new Error("Handshake failed, reached maximum number of attempts"));
}, maxAttempts, attemptsInterval);
});
}
fn();
};
function ChildHandshake(messenger) {
var localMethods = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new Promise(function (resolve, reject) {
var handshakeDispatcher = new ChildHandshakeDispatcher(messenger);
handshakeDispatcher.once(MessageType.HandshakeRequest).then(function (response) {
var sessionId = response.sessionId;
handshakeDispatcher.acceptHandshake(sessionId);
handshakeDispatcher.close();
var dispatcher = new Dispatcher(messenger, sessionId);
var connection = new ConcreteConnection(dispatcher, localMethods);
resolve(connection);
});
});
}
function ParentHandshake(messenger) {
var localMethods = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var maxAttempts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 5;
var attemptsInterval = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 100;
var thisSessionId = uniqueSessionId();
var connected = false;
return new Promise(function (resolve, reject) {
var handshakeDispatcher = new ParentHandshakeDispatcher(messenger, thisSessionId);
handshakeDispatcher.once(thisSessionId).then(function (response) {
connected = true;
handshakeDispatcher.close();
var sessionId = response.sessionId;
var dispatcher = new Dispatcher(messenger, sessionId);
var connection = new ConcreteConnection(dispatcher, localMethods);
resolve(connection);
});
runUntil(function () {
return handshakeDispatcher.initiateHandshake();
}, function () {
return connected;
}, function () {
return reject(new Error("Handshake failed, reached maximum number of attempts"));
}, maxAttempts, attemptsInterval);
});
var acceptableMessageEvent = function acceptableMessageEvent(event, remoteWindow, acceptedOrigin) {
var source = event.source,
origin = event.origin;
if (source !== remoteWindow) {
return false;
}
function ChildHandshake(messenger) {
var localMethods = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new Promise(function (resolve, reject) {
var handshakeDispatcher = new ChildHandshakeDispatcher(messenger);
handshakeDispatcher.once(MessageType.HandshakeRequest).then(function (response) {
var sessionId = response.sessionId;
handshakeDispatcher.acceptHandshake(sessionId);
handshakeDispatcher.close();
var dispatcher = new Dispatcher(messenger, sessionId);
var connection = new ConcreteConnection(dispatcher, localMethods);
resolve(connection);
});
});
if (origin !== acceptedOrigin && acceptedOrigin !== '*') {
return false;
}
var acceptableMessageEvent = function acceptableMessageEvent(event, remoteWindow, acceptedOrigin) {
var source = event.source,
origin = event.origin;
return true;
};
if (source !== remoteWindow) {
return false;
}
var WindowMessenger = function WindowMessenger(_ref) {
var localWindow = _ref.localWindow,
remoteWindow = _ref.remoteWindow,
remoteOrigin = _ref.remoteOrigin;
if (origin !== acceptedOrigin && acceptedOrigin !== '*') {
return false;
}
_classCallCheck(this, WindowMessenger);
return true;
localWindow = localWindow || window;
this.postMessage = function (message, transfer) {
remoteWindow.postMessage(message, remoteOrigin, transfer);
};
var WindowMessenger = function WindowMessenger(_ref) {
var localWindow = _ref.localWindow,
remoteWindow = _ref.remoteWindow,
remoteOrigin = _ref.remoteOrigin;
this.addMessageListener = function (listener) {
var outerListener = function outerListener(event) {
if (acceptableMessageEvent(event, remoteWindow, remoteOrigin)) {
listener(event);
}
};
_classCallCheck(this, WindowMessenger);
localWindow.addEventListener('message', outerListener);
localWindow = localWindow || window;
this.postMessage = function (message, transfer) {
remoteWindow.postMessage(message, remoteOrigin, transfer);
var removeListener = function removeListener() {
localWindow.removeEventListener('message', outerListener);
};
this.addMessageListener = function (listener) {
var outerListener = function outerListener(event) {
if (acceptableMessageEvent(event, remoteWindow, remoteOrigin)) {
listener(event);
}
};
return removeListener;
};
};
localWindow.addEventListener('message', outerListener);
_exports.WindowMessenger = WindowMessenger;
var removeListener = function removeListener() {
localWindow.removeEventListener('message', outerListener);
};
var BareMessenger = function BareMessenger(postable) {
_classCallCheck(this, BareMessenger);
return removeListener;
};
this.postMessage = function (message) {
var transfer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
postable.postMessage(message, transfer);
};
var BareMessenger = /*#__PURE__*/function () {
function BareMessenger(postable) {
_classCallCheck(this, BareMessenger);
this.addMessageListener = function (listener) {
var outerListener = function outerListener(event) {
listener(event);
};
this._postable = postable;
}
postable.addEventListener('message', outerListener);
_createClass(BareMessenger, [{
key: "postMessage",
value: function postMessage(message) {
var transfer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var removeListener = function removeListener() {
postable.removeEventListener('message', outerListener);
};
this._postable.postMessage(message, transfer);
}
}, {
key: "addMessageListener",
value: function addMessageListener(listener) {
var _this8 = this;
return removeListener;
};
};
var outerListener = function outerListener(event) {
listener(event);
};
_exports.BareMessenger = BareMessenger;
this._postable.addEventListener('message', outerListener);
var WorkerMessenger = /*#__PURE__*/function (_BareMessenger) {
_inherits(WorkerMessenger, _BareMessenger);
var removeListener = function removeListener() {
_this8._postable.removeEventListener('message', outerListener);
};
var _super5 = _createSuper(WorkerMessenger);
return removeListener;
}
}]);
function WorkerMessenger(_ref2) {
var worker = _ref2.worker;
return BareMessenger;
}();
_classCallCheck(this, WorkerMessenger);
var WorkerMessenger = /*#__PURE__*/function (_BareMessenger) {
_inherits(WorkerMessenger, _BareMessenger);
return _super5.call(this, worker);
}
var _super5 = _createSuper(WorkerMessenger);
return WorkerMessenger;
}(BareMessenger);
function WorkerMessenger(_ref2) {
var worker = _ref2.worker;
_exports.WorkerMessenger = WorkerMessenger;
_classCallCheck(this, WorkerMessenger);
var PortMessenger = /*#__PURE__*/function (_BareMessenger2) {
_inherits(PortMessenger, _BareMessenger2);
return _super5.call(this, worker);
}
var _super6 = _createSuper(PortMessenger);
return WorkerMessenger;
}(BareMessenger);
function PortMessenger(_ref3) {
var port = _ref3.port;
var PortMessenger = /*#__PURE__*/function (_BareMessenger2) {
_inherits(PortMessenger, _BareMessenger2);
_classCallCheck(this, PortMessenger);
var _super6 = _createSuper(PortMessenger);
return _super6.call(this, port);
}
function PortMessenger(_ref3) {
var port = _ref3.port;
return PortMessenger;
}(BareMessenger);
_classCallCheck(this, PortMessenger);
_exports.PortMessenger = PortMessenger;
return _super6.call(this, port);
var debug = function debug(namespace, log) {
log = log || console.debug || console.log || function () {};
return function () {
for (var _len3 = arguments.length, data = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
data[_key3] = arguments[_key3];
}
return PortMessenger;
}(BareMessenger);
log.apply(void 0, [namespace].concat(data));
};
};
var debug = function debug(namespace, log) {
log = log || console.debug || console.log || function () {};
_exports.debug = debug;
return function () {
for (var _len3 = arguments.length, data = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
data[_key3] = arguments[_key3];
}
function DebugMessenger(messenger, log) {
log = log || debug('post-me');
log.apply(void 0, [namespace].concat(data));
};
var debugListener = function debugListener(event) {
var data = event.data;
log('⬅️ received message', data);
};
function DebugMessenger(messenger, log) {
log = log || debug('post-me');
var debugListener = function debugListener(event) {
var data = event.data;
log('⬅️ received message', data);
};
messenger.addMessageListener(debugListener);
return {
postMessage: function postMessage(message) {
log('➡️ sending message', message);
messenger.postMessage(message);
},
addMessageListener: function addMessageListener(listener) {
return messenger.addMessageListener(listener);
}
};
}
exports.BareMessenger = BareMessenger;
exports.ChildHandshake = ChildHandshake;
exports.DebugMessenger = DebugMessenger;
exports.ParentHandshake = ParentHandshake;
exports.PortMessenger = PortMessenger;
exports.WindowMessenger = WindowMessenger;
exports.WorkerMessenger = WorkerMessenger;
exports.debug = debug;
Object.defineProperty(exports, '__esModule', { value: true });
})));
messenger.addMessageListener(debugListener);
return {
postMessage: function postMessage(message) {
log('➡️ sending message', message);
messenger.postMessage(message);
},
addMessageListener: function addMessageListener(listener) {
return messenger.addMessageListener(listener);
}
};
}
});

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

(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?factory(exports):typeof define==="function"&&define.amd?define(["exports"],factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,factory(global.PostMe={}))})(this,function(exports){"use strict";function _typeof(obj){"@babel/helpers - typeof";if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj}}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj}}return _typeof(obj)}function _toConsumableArray(arr){return _arrayWithoutHoles(arr)||_iterableToArray(arr)||_unsupportedIterableToArray(arr)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(o,minLen){if(!o)return;if(typeof o==="string")return _arrayLikeToArray(o,minLen);var n=Object.prototype.toString.call(o).slice(8,-1);if(n==="Object"&&o.constructor)n=o.constructor.name;if(n==="Map"||n==="Set")return Array.from(o);if(n==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return _arrayLikeToArray(o,minLen)}function _iterableToArray(iter){if(typeof Symbol!=="undefined"&&Symbol.iterator in Object(iter))return Array.from(iter)}function _arrayWithoutHoles(arr){if(Array.isArray(arr))return _arrayLikeToArray(arr)}function _arrayLikeToArray(arr,len){if(len==null||len>arr.length)len=arr.length;for(var i=0,arr2=new Array(len);i<len;i++){arr2[i]=arr[i]}return arr2}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function")}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:true,configurable:true}});if(superClass)_setPrototypeOf(subClass,superClass)}function _setPrototypeOf(o,p){_setPrototypeOf=Object.setPrototypeOf||function _setPrototypeOf(o,p){o.__proto__=p;return o};return _setPrototypeOf(o,p)}function _createSuper(Derived){var hasNativeReflectConstruct=_isNativeReflectConstruct();return function _createSuperInternal(){var Super=_getPrototypeOf(Derived),result;if(hasNativeReflectConstruct){var NewTarget=_getPrototypeOf(this).constructor;result=Reflect.construct(Super,arguments,NewTarget)}else{result=Super.apply(this,arguments)}return _possibleConstructorReturn(this,result)}}function _possibleConstructorReturn(self,call){if(call&&(_typeof(call)==="object"||typeof call==="function")){return call}return _assertThisInitialized(self)}function _assertThisInitialized(self){if(self===void 0){throw new ReferenceError("this hasn't been initialised - super() hasn't been called")}return self}function _isNativeReflectConstruct(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{Date.prototype.toString.call(Reflect.construct(Date,[],function(){}));return true}catch(e){return false}}function _getPrototypeOf(o){_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function _getPrototypeOf(o){return o.__proto__||Object.getPrototypeOf(o)};return _getPrototypeOf(o)}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor}var MARKER="@post-me";function createUniqueIdFn(){var __id=0;return function(){var id=__id;__id+=1;return id}}var Emitter=function(){function Emitter(){_classCallCheck(this,Emitter);this._listeners={}}_createClass(Emitter,[{key:"addEventListener",value:function addEventListener(eventName,listener){var listeners=this._listeners[eventName];if(!listeners){listeners=new Set;this._listeners[eventName]=listeners}listeners.add(listener)}},{key:"removeEventListener",value:function removeEventListener(eventName,listener){var listeners=this._listeners[eventName];if(!listeners){return}listeners["delete"](listener)}},{key:"once",value:function once(eventName){var _this=this;return new Promise(function(resolve){var listener=function listener(data){_this.removeEventListener(eventName,listener);resolve(data)};_this.addEventListener(eventName,listener)})}},{key:"emit",value:function emit(eventName,data){var listeners=this._listeners[eventName];if(!listeners){return}listeners.forEach(function(listener){listener(data)})}},{key:"removeAllListeners",value:function removeAllListeners(){Object.values(this._listeners).forEach(function(listeners){if(listeners){listeners.clear()}})}}]);return Emitter}();var MessageType;(function(MessageType){MessageType["HandshakeRequest"]="handshake-request";MessageType["HandshakeResponse"]="handshake-response";MessageType["Call"]="call";MessageType["Response"]="response";MessageType["Error"]="error";MessageType["Event"]="event";MessageType["Callback"]="callback"})(MessageType||(MessageType={}));function createHandshakeRequestMessage(sessionId){return{type:MARKER,action:MessageType.HandshakeRequest,sessionId:sessionId}}function createHandshakeResponseMessage(sessionId){return{type:MARKER,action:MessageType.HandshakeResponse,sessionId:sessionId}}function createCallMessage(sessionId,requestId,methodName,args){return{type:MARKER,action:MessageType.Call,sessionId:sessionId,requestId:requestId,methodName:methodName,args:args}}function createResponsMessage(sessionId,requestId,result,error){var message={type:MARKER,action:MessageType.Response,sessionId:sessionId,requestId:requestId};if(result!==undefined){message.result=result}if(error!==undefined){message.error=error}return message}function createCallbackMessage(sessionId,requestId,callbackId,args){return{type:MARKER,action:MessageType.Callback,sessionId:sessionId,requestId:requestId,callbackId:callbackId,args:args}}function createEventMessage(sessionId,eventName,payload){return{type:MARKER,action:MessageType.Event,sessionId:sessionId,eventName:eventName,payload:payload}}function isMessage(m){return m.type===MARKER}function isHandshakeRequestMessage(m){return isMessage(m)&&m.action===MessageType.HandshakeRequest}function isHandshakeResponseMessage(m){return isMessage(m)&&m.action===MessageType.HandshakeResponse}function isCallMessage(m){return isMessage(m)&&m.action===MessageType.Call}function isResponseMessage(m){return isMessage(m)&&m.action===MessageType.Response}function isCallbackMessage(m){return isMessage(m)&&m.action===MessageType.Callback}function isEventMessage(m){return isMessage(m)&&m.action===MessageType.Event}function makeCallbackEvent(requestId){return"callback_".concat(requestId)}function makeResponseEvent(requestId){return"response_".concat(requestId)}var Dispatcher=function(_Emitter){_inherits(Dispatcher,_Emitter);var _super=_createSuper(Dispatcher);function Dispatcher(messenger,sessionId){var _this2;_classCallCheck(this,Dispatcher);_this2=_super.call(this);_this2.uniqueId=createUniqueIdFn();_this2.messenger=messenger;_this2.sessionId=sessionId;_this2.removeMessengerListener=_this2.messenger.addMessageListener(_this2.messengerListener.bind(_assertThisInitialized(_this2)));return _this2}_createClass(Dispatcher,[{key:"messengerListener",value:function messengerListener(event){var data=event.data;if(!isMessage(data)){return}if(this.sessionId!==data.sessionId){return}if(isCallMessage(data)){this.emit(MessageType.Call,data)}else if(isResponseMessage(data)){this.emit(makeResponseEvent(data.requestId),data)}else if(isEventMessage(data)){this.emit(MessageType.Event,data)}else if(isCallbackMessage(data)){this.emit(makeCallbackEvent(data.requestId),data)}}},{key:"callOnRemote",value:function callOnRemote(methodName,args,transfer){var requestId=this.uniqueId();var callbackEvent=makeCallbackEvent(requestId);var responseEvent=makeResponseEvent(requestId);var message=createCallMessage(this.sessionId,requestId,methodName,args);this.messenger.postMessage(message,transfer);return{callbackEvent:callbackEvent,responseEvent:responseEvent}}},{key:"respondToRemote",value:function respondToRemote(requestId,value,error,transfer){var message=createResponsMessage(this.sessionId,requestId,value,error);this.messenger.postMessage(message,transfer)}},{key:"callbackToRemote",value:function callbackToRemote(requestId,callbackId,args){var message=createCallbackMessage(this.sessionId,requestId,callbackId,args);this.messenger.postMessage(message)}},{key:"emitToRemote",value:function emitToRemote(eventName,payload,transfer){var message=createEventMessage(this.sessionId,eventName,payload);this.messenger.postMessage(message,transfer)}},{key:"close",value:function close(){this.removeMessengerListener();this.removeAllListeners()}}]);return Dispatcher}(Emitter);var ParentHandshakeDispatcher=function(_Emitter2){_inherits(ParentHandshakeDispatcher,_Emitter2);var _super2=_createSuper(ParentHandshakeDispatcher);function ParentHandshakeDispatcher(messenger,sessionId){var _this3;_classCallCheck(this,ParentHandshakeDispatcher);_this3=_super2.call(this);_this3.messenger=messenger;_this3.sessionId=sessionId;_this3.removeMessengerListener=_this3.messenger.addMessageListener(_this3.messengerListener.bind(_assertThisInitialized(_this3)));return _this3}_createClass(ParentHandshakeDispatcher,[{key:"messengerListener",value:function messengerListener(event){var data=event.data;if(!isMessage(data)){return}if(this.sessionId!==data.sessionId){return}if(isHandshakeResponseMessage(data)){this.emit(data.sessionId,data)}}},{key:"initiateHandshake",value:function initiateHandshake(){var message=createHandshakeRequestMessage(this.sessionId);this.messenger.postMessage(message);return this.sessionId}},{key:"close",value:function close(){this.removeMessengerListener();this.removeAllListeners()}}]);return ParentHandshakeDispatcher}(Emitter);var ChildHandshakeDispatcher=function(_Emitter3){_inherits(ChildHandshakeDispatcher,_Emitter3);var _super3=_createSuper(ChildHandshakeDispatcher);function ChildHandshakeDispatcher(messenger){var _this4;_classCallCheck(this,ChildHandshakeDispatcher);_this4=_super3.call(this);_this4.messenger=messenger;_this4.removeMessengerListener=_this4.messenger.addMessageListener(_this4.messengerListener.bind(_assertThisInitialized(_this4)));return _this4}_createClass(ChildHandshakeDispatcher,[{key:"messengerListener",value:function messengerListener(event){var data=event.data;if(isHandshakeRequestMessage(data)){this.emit(MessageType.HandshakeRequest,data)}}},{key:"acceptHandshake",value:function acceptHandshake(sessionId){var message=createHandshakeResponseMessage(sessionId);this.messenger.postMessage(message)}},{key:"close",value:function close(){this.removeMessengerListener();this.removeAllListeners()}}]);return ChildHandshakeDispatcher}(Emitter);var ProxyType;(function(ProxyType){ProxyType["Callback"]="callback"})(ProxyType||(ProxyType={}));function createCallbackProxy(callbackId){return{type:MARKER,proxy:ProxyType.Callback,callbackId:callbackId}}function isCallbackProxy(p){return p.type===MARKER&&p.proxy===ProxyType.Callback}var ConcreteRemoteHandle=function(_Emitter4){_inherits(ConcreteRemoteHandle,_Emitter4);var _super4=_createSuper(ConcreteRemoteHandle);function ConcreteRemoteHandle(dispatcher){var _this5;_classCallCheck(this,ConcreteRemoteHandle);_this5=_super4.call(this);_this5._dispatcher=dispatcher;_this5._callTransfer={};_this5._dispatcher.addEventListener(MessageType.Event,_this5._handleEvent.bind(_assertThisInitialized(_this5)));return _this5}_createClass(ConcreteRemoteHandle,[{key:"close",value:function close(){this.removeAllListeners()}},{key:"setCallTransfer",value:function setCallTransfer(methodName,transfer){this._callTransfer[methodName]=transfer}},{key:"call",value:function call(methodName){for(var _len=arguments.length,args=new Array(_len>1?_len-1:0),_key=1;_key<_len;_key++){args[_key-1]=arguments[_key]}return this.customCall(methodName,args)}},{key:"customCall",value:function customCall(methodName,args){var _this6=this;var options=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};return new Promise(function(resolve,reject){var sanitizedArgs=[];var callbacks=[];var callbackId=0;args.forEach(function(arg){if(typeof arg==="function"){callbacks.push(arg);sanitizedArgs.push(createCallbackProxy(callbackId));callbackId+=1}else{sanitizedArgs.push(arg)}});var hasCallbacks=callbacks.length>0;var callbackListener=undefined;if(hasCallbacks){callbackListener=function callbackListener(data){var callbackId=data.callbackId,args=data.args;callbacks[callbackId].apply(callbacks,_toConsumableArray(args))}}var transfer=options.transfer;if(transfer===undefined&&_this6._callTransfer[methodName]){var _this6$_callTransfer;transfer=(_this6$_callTransfer=_this6._callTransfer)[methodName].apply(_this6$_callTransfer,sanitizedArgs)}var _this6$_dispatcher$ca=_this6._dispatcher.callOnRemote(methodName,sanitizedArgs,transfer),callbackEvent=_this6$_dispatcher$ca.callbackEvent,responseEvent=_this6$_dispatcher$ca.responseEvent;if(hasCallbacks){_this6._dispatcher.addEventListener(callbackEvent,callbackListener)}_this6._dispatcher.once(responseEvent).then(function(response){if(callbackListener){_this6._dispatcher.removeEventListener(callbackEvent,callbackListener)}var result=response.result,error=response.error;if(error!==undefined){reject(error)}else{resolve(result)}})})}},{key:"_handleEvent",value:function _handleEvent(data){var eventName=data.eventName,payload=data.payload;this.emit(eventName,payload)}}]);return ConcreteRemoteHandle}(Emitter);var ConcreteLocalHandle=function(){function ConcreteLocalHandle(dispatcher,localMethods){_classCallCheck(this,ConcreteLocalHandle);this._dispatcher=dispatcher;this._methods=localMethods;this._returnTransfer={};this._emitTransfer={};this._dispatcher.addEventListener(MessageType.Call,this._handleCall.bind(this))}_createClass(ConcreteLocalHandle,[{key:"emit",value:function emit(eventName,payload){var options=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};var transfer=options.transfer;if(transfer===undefined&&this._emitTransfer[eventName]){transfer=this._emitTransfer[eventName](payload)}this._dispatcher.emitToRemote(eventName,payload,transfer)}},{key:"setReturnTransfer",value:function setReturnTransfer(methodName,transfer){this._returnTransfer[methodName]=transfer}},{key:"setEmitTransfer",value:function setEmitTransfer(eventName,transfer){this._emitTransfer[eventName]=transfer}},{key:"_handleCall",value:function _handleCall(data){var _this7=this;var requestId=data.requestId,methodName=data.methodName,args=data.args;var callMethod=new Promise(function(resolve,reject){var method=_this7._methods[methodName];if(typeof method!=="function"){reject(new Error('The method "'.concat(methodName,'" has not been implemented.')));return}var desanitizedArgs=args.map(function(arg){if(isCallbackProxy(arg)){var callbackId=arg.callbackId;return function(){for(var _len2=arguments.length,args=new Array(_len2),_key2=0;_key2<_len2;_key2++){args[_key2]=arguments[_key2]}_this7._dispatcher.callbackToRemote(requestId,callbackId,args)}}else{return arg}});Promise.resolve(method.apply(void 0,_toConsumableArray(desanitizedArgs))).then(resolve)["catch"](reject)});callMethod.then(function(result){var transfer;if(_this7._returnTransfer[methodName]){transfer=_this7._returnTransfer[methodName](result)}_this7._dispatcher.respondToRemote(requestId,result,undefined,transfer)})["catch"](function(error){_this7._dispatcher.respondToRemote(requestId,undefined,error)})}}]);return ConcreteLocalHandle}();var ConcreteConnection=function(){function ConcreteConnection(dispatcher,localMethods){_classCallCheck(this,ConcreteConnection);this._dispatcher=dispatcher;this._localHandle=new ConcreteLocalHandle(dispatcher,localMethods);this._remoteHandle=new ConcreteRemoteHandle(dispatcher)}_createClass(ConcreteConnection,[{key:"close",value:function close(){this._dispatcher.close();this.remoteHandle().close()}},{key:"localHandle",value:function localHandle(){return this._localHandle}},{key:"remoteHandle",value:function remoteHandle(){return this._remoteHandle}}]);return ConcreteConnection}();var uniqueSessionId=createUniqueIdFn();var runUntil=function runUntil(worker,condition,unfulfilled,maxAttempts,attemptInterval){var attempt=0;var fn=function fn(){if(!condition()&&(attempt<maxAttempts||maxAttempts<1)){worker();attempt+=1;setTimeout(fn,attemptInterval)}else if(!condition()&&attempt>=maxAttempts&&maxAttempts>=1){unfulfilled()}};fn()};function ParentHandshake(messenger){var localMethods=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var maxAttempts=arguments.length>2&&arguments[2]!==undefined?arguments[2]:5;var attemptsInterval=arguments.length>3&&arguments[3]!==undefined?arguments[3]:100;var thisSessionId=uniqueSessionId();var connected=false;return new Promise(function(resolve,reject){var handshakeDispatcher=new ParentHandshakeDispatcher(messenger,thisSessionId);handshakeDispatcher.once(thisSessionId).then(function(response){connected=true;handshakeDispatcher.close();var sessionId=response.sessionId;var dispatcher=new Dispatcher(messenger,sessionId);var connection=new ConcreteConnection(dispatcher,localMethods);resolve(connection)});runUntil(function(){return handshakeDispatcher.initiateHandshake()},function(){return connected},function(){return reject(new Error("Handshake failed, reached maximum number of attempts"))},maxAttempts,attemptsInterval)})}function ChildHandshake(messenger){var localMethods=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return new Promise(function(resolve,reject){var handshakeDispatcher=new ChildHandshakeDispatcher(messenger);handshakeDispatcher.once(MessageType.HandshakeRequest).then(function(response){var sessionId=response.sessionId;handshakeDispatcher.acceptHandshake(sessionId);handshakeDispatcher.close();var dispatcher=new Dispatcher(messenger,sessionId);var connection=new ConcreteConnection(dispatcher,localMethods);resolve(connection)})})}var acceptableMessageEvent=function acceptableMessageEvent(event,remoteWindow,acceptedOrigin){var source=event.source,origin=event.origin;if(source!==remoteWindow){return false}if(origin!==acceptedOrigin&&acceptedOrigin!=="*"){return false}return true};var WindowMessenger=function WindowMessenger(_ref){var localWindow=_ref.localWindow,remoteWindow=_ref.remoteWindow,remoteOrigin=_ref.remoteOrigin;_classCallCheck(this,WindowMessenger);localWindow=localWindow||window;this.postMessage=function(message,transfer){remoteWindow.postMessage(message,remoteOrigin,transfer)};this.addMessageListener=function(listener){var outerListener=function outerListener(event){if(acceptableMessageEvent(event,remoteWindow,remoteOrigin)){listener(event)}};localWindow.addEventListener("message",outerListener);var removeListener=function removeListener(){localWindow.removeEventListener("message",outerListener)};return removeListener}};var BareMessenger=function(){function BareMessenger(postable){_classCallCheck(this,BareMessenger);this._postable=postable}_createClass(BareMessenger,[{key:"postMessage",value:function postMessage(message){var transfer=arguments.length>1&&arguments[1]!==undefined?arguments[1]:[];this._postable.postMessage(message,transfer)}},{key:"addMessageListener",value:function addMessageListener(listener){var _this8=this;var outerListener=function outerListener(event){listener(event)};this._postable.addEventListener("message",outerListener);var removeListener=function removeListener(){_this8._postable.removeEventListener("message",outerListener)};return removeListener}}]);return BareMessenger}();var WorkerMessenger=function(_BareMessenger){_inherits(WorkerMessenger,_BareMessenger);var _super5=_createSuper(WorkerMessenger);function WorkerMessenger(_ref2){var worker=_ref2.worker;_classCallCheck(this,WorkerMessenger);return _super5.call(this,worker)}return WorkerMessenger}(BareMessenger);var PortMessenger=function(_BareMessenger2){_inherits(PortMessenger,_BareMessenger2);var _super6=_createSuper(PortMessenger);function PortMessenger(_ref3){var port=_ref3.port;_classCallCheck(this,PortMessenger);return _super6.call(this,port)}return PortMessenger}(BareMessenger);var debug=function debug(namespace,log){log=log||console.debug||console.log||function(){};return function(){for(var _len3=arguments.length,data=new Array(_len3),_key3=0;_key3<_len3;_key3++){data[_key3]=arguments[_key3]}log.apply(void 0,[namespace].concat(data))}};function DebugMessenger(messenger,log){log=log||debug("post-me");var debugListener=function debugListener(event){var data=event.data;log("⬅️ received message",data)};messenger.addMessageListener(debugListener);return{postMessage:function postMessage(message){log("➡️ sending message",message);messenger.postMessage(message)},addMessageListener:function addMessageListener(listener){return messenger.addMessageListener(listener)}}}exports.BareMessenger=BareMessenger;exports.ChildHandshake=ChildHandshake;exports.DebugMessenger=DebugMessenger;exports.ParentHandshake=ParentHandshake;exports.PortMessenger=PortMessenger;exports.WindowMessenger=WindowMessenger;exports.WorkerMessenger=WorkerMessenger;exports.debug=debug;Object.defineProperty(exports,"__esModule",{value:true})});
function _typeof(obj){"@babel/helpers - typeof";if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj}}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj}}return _typeof(obj)}(function(global,factory){if(typeof define==="function"&&define.amd){define("PostMe",["exports"],factory)}else if(typeof exports!=="undefined"){factory(exports)}else{var mod={exports:{}};factory(mod.exports);global.PostMe=mod.exports}})(typeof globalThis!=="undefined"?globalThis:typeof self!=="undefined"?self:this,function(_exports){"use strict";Object.defineProperty(_exports,"__esModule",{value:true});_exports.ChildHandshake=ChildHandshake;_exports.DebugMessenger=DebugMessenger;_exports.ParentHandshake=ParentHandshake;_exports.debug=_exports.WorkerMessenger=_exports.WindowMessenger=_exports.PortMessenger=_exports.BareMessenger=void 0;function _toConsumableArray(arr){return _arrayWithoutHoles(arr)||_iterableToArray(arr)||_unsupportedIterableToArray(arr)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(o,minLen){if(!o)return;if(typeof o==="string")return _arrayLikeToArray(o,minLen);var n=Object.prototype.toString.call(o).slice(8,-1);if(n==="Object"&&o.constructor)n=o.constructor.name;if(n==="Map"||n==="Set")return Array.from(o);if(n==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return _arrayLikeToArray(o,minLen)}function _iterableToArray(iter){if(typeof Symbol!=="undefined"&&Symbol.iterator in Object(iter))return Array.from(iter)}function _arrayWithoutHoles(arr){if(Array.isArray(arr))return _arrayLikeToArray(arr)}function _arrayLikeToArray(arr,len){if(len==null||len>arr.length)len=arr.length;for(var i=0,arr2=new Array(len);i<len;i++){arr2[i]=arr[i]}return arr2}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function")}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:true,configurable:true}});if(superClass)_setPrototypeOf(subClass,superClass)}function _setPrototypeOf(o,p){_setPrototypeOf=Object.setPrototypeOf||function _setPrototypeOf(o,p){o.__proto__=p;return o};return _setPrototypeOf(o,p)}function _createSuper(Derived){var hasNativeReflectConstruct=_isNativeReflectConstruct();return function _createSuperInternal(){var Super=_getPrototypeOf(Derived),result;if(hasNativeReflectConstruct){var NewTarget=_getPrototypeOf(this).constructor;result=Reflect.construct(Super,arguments,NewTarget)}else{result=Super.apply(this,arguments)}return _possibleConstructorReturn(this,result)}}function _possibleConstructorReturn(self,call){if(call&&(_typeof(call)==="object"||typeof call==="function")){return call}return _assertThisInitialized(self)}function _assertThisInitialized(self){if(self===void 0){throw new ReferenceError("this hasn't been initialised - super() hasn't been called")}return self}function _isNativeReflectConstruct(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{Date.prototype.toString.call(Reflect.construct(Date,[],function(){}));return true}catch(e){return false}}function _getPrototypeOf(o){_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function _getPrototypeOf(o){return o.__proto__||Object.getPrototypeOf(o)};return _getPrototypeOf(o)}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor}var MARKER="@post-me";function createUniqueIdFn(){var __id=0;return function(){var id=__id;__id+=1;return id}}var Emitter=function(){function Emitter(){_classCallCheck(this,Emitter);this._listeners={}}_createClass(Emitter,[{key:"addEventListener",value:function addEventListener(eventName,listener){var listeners=this._listeners[eventName];if(!listeners){listeners=new Set;this._listeners[eventName]=listeners}listeners.add(listener)}},{key:"removeEventListener",value:function removeEventListener(eventName,listener){var listeners=this._listeners[eventName];if(!listeners){return}listeners["delete"](listener)}},{key:"once",value:function once(eventName){var _this=this;return new Promise(function(resolve){var listener=function listener(data){_this.removeEventListener(eventName,listener);resolve(data)};_this.addEventListener(eventName,listener)})}},{key:"emit",value:function emit(eventName,data){var listeners=this._listeners[eventName];if(!listeners){return}listeners.forEach(function(listener){listener(data)})}},{key:"removeAllListeners",value:function removeAllListeners(){Object.values(this._listeners).forEach(function(listeners){if(listeners){listeners.clear()}})}}]);return Emitter}();var MessageType;(function(MessageType){MessageType["HandshakeRequest"]="handshake-request";MessageType["HandshakeResponse"]="handshake-response";MessageType["Call"]="call";MessageType["Response"]="response";MessageType["Error"]="error";MessageType["Event"]="event";MessageType["Callback"]="callback"})(MessageType||(MessageType={}));function createHandshakeRequestMessage(sessionId){return{type:MARKER,action:MessageType.HandshakeRequest,sessionId:sessionId}}function createHandshakeResponseMessage(sessionId){return{type:MARKER,action:MessageType.HandshakeResponse,sessionId:sessionId}}function createCallMessage(sessionId,requestId,methodName,args){return{type:MARKER,action:MessageType.Call,sessionId:sessionId,requestId:requestId,methodName:methodName,args:args}}function createResponsMessage(sessionId,requestId,result,error){var message={type:MARKER,action:MessageType.Response,sessionId:sessionId,requestId:requestId};if(result!==undefined){message.result=result}if(error!==undefined){message.error=error}return message}function createCallbackMessage(sessionId,requestId,callbackId,args){return{type:MARKER,action:MessageType.Callback,sessionId:sessionId,requestId:requestId,callbackId:callbackId,args:args}}function createEventMessage(sessionId,eventName,payload){return{type:MARKER,action:MessageType.Event,sessionId:sessionId,eventName:eventName,payload:payload}}function isMessage(m){return m.type===MARKER}function isHandshakeRequestMessage(m){return isMessage(m)&&m.action===MessageType.HandshakeRequest}function isHandshakeResponseMessage(m){return isMessage(m)&&m.action===MessageType.HandshakeResponse}function isCallMessage(m){return isMessage(m)&&m.action===MessageType.Call}function isResponseMessage(m){return isMessage(m)&&m.action===MessageType.Response}function isCallbackMessage(m){return isMessage(m)&&m.action===MessageType.Callback}function isEventMessage(m){return isMessage(m)&&m.action===MessageType.Event}function makeCallbackEvent(requestId){return"callback_".concat(requestId)}function makeResponseEvent(requestId){return"response_".concat(requestId)}var Dispatcher=function(_Emitter){_inherits(Dispatcher,_Emitter);var _super=_createSuper(Dispatcher);function Dispatcher(messenger,sessionId){var _this2;_classCallCheck(this,Dispatcher);_this2=_super.call(this);_this2.uniqueId=createUniqueIdFn();_this2.messenger=messenger;_this2.sessionId=sessionId;_this2.removeMessengerListener=_this2.messenger.addMessageListener(_this2.messengerListener.bind(_assertThisInitialized(_this2)));return _this2}_createClass(Dispatcher,[{key:"messengerListener",value:function messengerListener(event){var data=event.data;if(!isMessage(data)){return}if(this.sessionId!==data.sessionId){return}if(isCallMessage(data)){this.emit(MessageType.Call,data)}else if(isResponseMessage(data)){this.emit(makeResponseEvent(data.requestId),data)}else if(isEventMessage(data)){this.emit(MessageType.Event,data)}else if(isCallbackMessage(data)){this.emit(makeCallbackEvent(data.requestId),data)}}},{key:"callOnRemote",value:function callOnRemote(methodName,args,transfer){var requestId=this.uniqueId();var callbackEvent=makeCallbackEvent(requestId);var responseEvent=makeResponseEvent(requestId);var message=createCallMessage(this.sessionId,requestId,methodName,args);this.messenger.postMessage(message,transfer);return{callbackEvent:callbackEvent,responseEvent:responseEvent}}},{key:"respondToRemote",value:function respondToRemote(requestId,value,error,transfer){var message=createResponsMessage(this.sessionId,requestId,value,error);this.messenger.postMessage(message,transfer)}},{key:"callbackToRemote",value:function callbackToRemote(requestId,callbackId,args){var message=createCallbackMessage(this.sessionId,requestId,callbackId,args);this.messenger.postMessage(message)}},{key:"emitToRemote",value:function emitToRemote(eventName,payload,transfer){var message=createEventMessage(this.sessionId,eventName,payload);this.messenger.postMessage(message,transfer)}},{key:"close",value:function close(){this.removeMessengerListener();this.removeAllListeners()}}]);return Dispatcher}(Emitter);var ParentHandshakeDispatcher=function(_Emitter2){_inherits(ParentHandshakeDispatcher,_Emitter2);var _super2=_createSuper(ParentHandshakeDispatcher);function ParentHandshakeDispatcher(messenger,sessionId){var _this3;_classCallCheck(this,ParentHandshakeDispatcher);_this3=_super2.call(this);_this3.messenger=messenger;_this3.sessionId=sessionId;_this3.removeMessengerListener=_this3.messenger.addMessageListener(_this3.messengerListener.bind(_assertThisInitialized(_this3)));return _this3}_createClass(ParentHandshakeDispatcher,[{key:"messengerListener",value:function messengerListener(event){var data=event.data;if(!isMessage(data)){return}if(this.sessionId!==data.sessionId){return}if(isHandshakeResponseMessage(data)){this.emit(data.sessionId,data)}}},{key:"initiateHandshake",value:function initiateHandshake(){var message=createHandshakeRequestMessage(this.sessionId);this.messenger.postMessage(message);return this.sessionId}},{key:"close",value:function close(){this.removeMessengerListener();this.removeAllListeners()}}]);return ParentHandshakeDispatcher}(Emitter);var ChildHandshakeDispatcher=function(_Emitter3){_inherits(ChildHandshakeDispatcher,_Emitter3);var _super3=_createSuper(ChildHandshakeDispatcher);function ChildHandshakeDispatcher(messenger){var _this4;_classCallCheck(this,ChildHandshakeDispatcher);_this4=_super3.call(this);_this4.messenger=messenger;_this4.removeMessengerListener=_this4.messenger.addMessageListener(_this4.messengerListener.bind(_assertThisInitialized(_this4)));return _this4}_createClass(ChildHandshakeDispatcher,[{key:"messengerListener",value:function messengerListener(event){var data=event.data;if(isHandshakeRequestMessage(data)){this.emit(MessageType.HandshakeRequest,data)}}},{key:"acceptHandshake",value:function acceptHandshake(sessionId){var message=createHandshakeResponseMessage(sessionId);this.messenger.postMessage(message)}},{key:"close",value:function close(){this.removeMessengerListener();this.removeAllListeners()}}]);return ChildHandshakeDispatcher}(Emitter);var ProxyType;(function(ProxyType){ProxyType["Callback"]="callback"})(ProxyType||(ProxyType={}));function createCallbackProxy(callbackId){return{type:MARKER,proxy:ProxyType.Callback,callbackId:callbackId}}function isCallbackProxy(p){return p.type===MARKER&&p.proxy===ProxyType.Callback}var ConcreteRemoteHandle=function(_Emitter4){_inherits(ConcreteRemoteHandle,_Emitter4);var _super4=_createSuper(ConcreteRemoteHandle);function ConcreteRemoteHandle(dispatcher){var _this5;_classCallCheck(this,ConcreteRemoteHandle);_this5=_super4.call(this);_this5._dispatcher=dispatcher;_this5._callTransfer={};_this5._dispatcher.addEventListener(MessageType.Event,_this5._handleEvent.bind(_assertThisInitialized(_this5)));return _this5}_createClass(ConcreteRemoteHandle,[{key:"close",value:function close(){this.removeAllListeners()}},{key:"setCallTransfer",value:function setCallTransfer(methodName,transfer){this._callTransfer[methodName]=transfer}},{key:"call",value:function call(methodName){for(var _len=arguments.length,args=new Array(_len>1?_len-1:0),_key=1;_key<_len;_key++){args[_key-1]=arguments[_key]}return this.customCall(methodName,args)}},{key:"customCall",value:function customCall(methodName,args){var _this6=this;var options=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};return new Promise(function(resolve,reject){var sanitizedArgs=[];var callbacks=[];var callbackId=0;args.forEach(function(arg){if(typeof arg==="function"){callbacks.push(arg);sanitizedArgs.push(createCallbackProxy(callbackId));callbackId+=1}else{sanitizedArgs.push(arg)}});var hasCallbacks=callbacks.length>0;var callbackListener=undefined;if(hasCallbacks){callbackListener=function callbackListener(data){var callbackId=data.callbackId,args=data.args;callbacks[callbackId].apply(callbacks,_toConsumableArray(args))}}var transfer=options.transfer;if(transfer===undefined&&_this6._callTransfer[methodName]){var _this6$_callTransfer;transfer=(_this6$_callTransfer=_this6._callTransfer)[methodName].apply(_this6$_callTransfer,sanitizedArgs)}var _this6$_dispatcher$ca=_this6._dispatcher.callOnRemote(methodName,sanitizedArgs,transfer),callbackEvent=_this6$_dispatcher$ca.callbackEvent,responseEvent=_this6$_dispatcher$ca.responseEvent;if(hasCallbacks){_this6._dispatcher.addEventListener(callbackEvent,callbackListener)}_this6._dispatcher.once(responseEvent).then(function(response){if(callbackListener){_this6._dispatcher.removeEventListener(callbackEvent,callbackListener)}var result=response.result,error=response.error;if(error!==undefined){reject(error)}else{resolve(result)}})})}},{key:"_handleEvent",value:function _handleEvent(data){var eventName=data.eventName,payload=data.payload;this.emit(eventName,payload)}}]);return ConcreteRemoteHandle}(Emitter);var ConcreteLocalHandle=function(){function ConcreteLocalHandle(dispatcher,localMethods){_classCallCheck(this,ConcreteLocalHandle);this._dispatcher=dispatcher;this._methods=localMethods;this._returnTransfer={};this._emitTransfer={};this._dispatcher.addEventListener(MessageType.Call,this._handleCall.bind(this))}_createClass(ConcreteLocalHandle,[{key:"emit",value:function emit(eventName,payload){var options=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};var transfer=options.transfer;if(transfer===undefined&&this._emitTransfer[eventName]){transfer=this._emitTransfer[eventName](payload)}this._dispatcher.emitToRemote(eventName,payload,transfer)}},{key:"setReturnTransfer",value:function setReturnTransfer(methodName,transfer){this._returnTransfer[methodName]=transfer}},{key:"setEmitTransfer",value:function setEmitTransfer(eventName,transfer){this._emitTransfer[eventName]=transfer}},{key:"_handleCall",value:function _handleCall(data){var _this7=this;var requestId=data.requestId,methodName=data.methodName,args=data.args;var callMethod=new Promise(function(resolve,reject){var method=_this7._methods[methodName];if(typeof method!=="function"){reject(new Error('The method "'.concat(methodName,'" has not been implemented.')));return}var desanitizedArgs=args.map(function(arg){if(isCallbackProxy(arg)){var callbackId=arg.callbackId;return function(){for(var _len2=arguments.length,args=new Array(_len2),_key2=0;_key2<_len2;_key2++){args[_key2]=arguments[_key2]}_this7._dispatcher.callbackToRemote(requestId,callbackId,args)}}else{return arg}});Promise.resolve(method.apply(void 0,_toConsumableArray(desanitizedArgs))).then(resolve)["catch"](reject)});callMethod.then(function(result){var transfer;if(_this7._returnTransfer[methodName]){transfer=_this7._returnTransfer[methodName](result)}_this7._dispatcher.respondToRemote(requestId,result,undefined,transfer)})["catch"](function(error){_this7._dispatcher.respondToRemote(requestId,undefined,error)})}}]);return ConcreteLocalHandle}();var ConcreteConnection=function(){function ConcreteConnection(dispatcher,localMethods){_classCallCheck(this,ConcreteConnection);this._dispatcher=dispatcher;this._localHandle=new ConcreteLocalHandle(dispatcher,localMethods);this._remoteHandle=new ConcreteRemoteHandle(dispatcher)}_createClass(ConcreteConnection,[{key:"close",value:function close(){this._dispatcher.close();this.remoteHandle().close()}},{key:"localHandle",value:function localHandle(){return this._localHandle}},{key:"remoteHandle",value:function remoteHandle(){return this._remoteHandle}}]);return ConcreteConnection}();var uniqueSessionId=createUniqueIdFn();var runUntil=function runUntil(worker,condition,unfulfilled,maxAttempts,attemptInterval){var attempt=0;var fn=function fn(){if(!condition()&&(attempt<maxAttempts||maxAttempts<1)){worker();attempt+=1;setTimeout(fn,attemptInterval)}else if(!condition()&&attempt>=maxAttempts&&maxAttempts>=1){unfulfilled()}};fn()};function ParentHandshake(messenger){var localMethods=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var maxAttempts=arguments.length>2&&arguments[2]!==undefined?arguments[2]:5;var attemptsInterval=arguments.length>3&&arguments[3]!==undefined?arguments[3]:100;var thisSessionId=uniqueSessionId();var connected=false;return new Promise(function(resolve,reject){var handshakeDispatcher=new ParentHandshakeDispatcher(messenger,thisSessionId);handshakeDispatcher.once(thisSessionId).then(function(response){connected=true;handshakeDispatcher.close();var sessionId=response.sessionId;var dispatcher=new Dispatcher(messenger,sessionId);var connection=new ConcreteConnection(dispatcher,localMethods);resolve(connection)});runUntil(function(){return handshakeDispatcher.initiateHandshake()},function(){return connected},function(){return reject(new Error("Handshake failed, reached maximum number of attempts"))},maxAttempts,attemptsInterval)})}function ChildHandshake(messenger){var localMethods=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return new Promise(function(resolve,reject){var handshakeDispatcher=new ChildHandshakeDispatcher(messenger);handshakeDispatcher.once(MessageType.HandshakeRequest).then(function(response){var sessionId=response.sessionId;handshakeDispatcher.acceptHandshake(sessionId);handshakeDispatcher.close();var dispatcher=new Dispatcher(messenger,sessionId);var connection=new ConcreteConnection(dispatcher,localMethods);resolve(connection)})})}var acceptableMessageEvent=function acceptableMessageEvent(event,remoteWindow,acceptedOrigin){var source=event.source,origin=event.origin;if(source!==remoteWindow){return false}if(origin!==acceptedOrigin&&acceptedOrigin!=="*"){return false}return true};var WindowMessenger=function WindowMessenger(_ref){var localWindow=_ref.localWindow,remoteWindow=_ref.remoteWindow,remoteOrigin=_ref.remoteOrigin;_classCallCheck(this,WindowMessenger);localWindow=localWindow||window;this.postMessage=function(message,transfer){remoteWindow.postMessage(message,remoteOrigin,transfer)};this.addMessageListener=function(listener){var outerListener=function outerListener(event){if(acceptableMessageEvent(event,remoteWindow,remoteOrigin)){listener(event)}};localWindow.addEventListener("message",outerListener);var removeListener=function removeListener(){localWindow.removeEventListener("message",outerListener)};return removeListener}};_exports.WindowMessenger=WindowMessenger;var BareMessenger=function BareMessenger(postable){_classCallCheck(this,BareMessenger);this.postMessage=function(message){var transfer=arguments.length>1&&arguments[1]!==undefined?arguments[1]:[];postable.postMessage(message,transfer)};this.addMessageListener=function(listener){var outerListener=function outerListener(event){listener(event)};postable.addEventListener("message",outerListener);var removeListener=function removeListener(){postable.removeEventListener("message",outerListener)};return removeListener}};_exports.BareMessenger=BareMessenger;var WorkerMessenger=function(_BareMessenger){_inherits(WorkerMessenger,_BareMessenger);var _super5=_createSuper(WorkerMessenger);function WorkerMessenger(_ref2){var worker=_ref2.worker;_classCallCheck(this,WorkerMessenger);return _super5.call(this,worker)}return WorkerMessenger}(BareMessenger);_exports.WorkerMessenger=WorkerMessenger;var PortMessenger=function(_BareMessenger2){_inherits(PortMessenger,_BareMessenger2);var _super6=_createSuper(PortMessenger);function PortMessenger(_ref3){var port=_ref3.port;_classCallCheck(this,PortMessenger);return _super6.call(this,port)}return PortMessenger}(BareMessenger);_exports.PortMessenger=PortMessenger;var debug=function debug(namespace,log){log=log||console.debug||console.log||function(){};return function(){for(var _len3=arguments.length,data=new Array(_len3),_key3=0;_key3<_len3;_key3++){data[_key3]=arguments[_key3]}log.apply(void 0,[namespace].concat(data))}};_exports.debug=debug;function DebugMessenger(messenger,log){log=log||debug("post-me");var debugListener=function debugListener(event){var data=event.data;log("⬅️ received message",data)};messenger.addMessageListener(debugListener);return{postMessage:function postMessage(message){log("➡️ sending message",message);messenger.postMessage(message)},addMessageListener:function addMessageListener(listener){return messenger.addMessageListener(listener)}}}});

@@ -22,6 +22,5 @@ declare type MessageListener = (event: MessageEvent) => void;

export declare class BareMessenger implements Messenger {
_postable: Postable;
postMessage: (message: any, transfer?: Transferable[]) => void;
addMessageListener: (listener: MessageListener) => ListenerRemover;
constructor(postable: Postable);
postMessage(message: any, transfer?: Transferable[]): void;
addMessageListener(listener: MessageListener): ListenerRemover;
}

@@ -28,0 +27,0 @@ export declare class WorkerMessenger extends BareMessenger implements Messenger {

{
"name": "post-me",
"version": "0.4.1",
"version": "0.4.2",
"description": "Use web Workers and other Windows through a simple Promise API",

@@ -17,6 +17,9 @@ "author": {

},
"main": "dist/index.js",
"browser": "dist/index.min.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"main": "./dist/index.js",
"exports": {
"import": "./dist/index.esnext.mjs",
"require": "./dist/index.js"
},
"module": "./dist/index.es5.mjs",
"types": "./dist/index.d.ts",
"files": [

@@ -27,4 +30,8 @@ "dist"

"scripts": {
"build": "rollup -c && uglifyjs dist/index.js -o dist/index.min.js && tsc --emitDeclarationOnly",
"build:demo": "npm run build && node scripts/build-demo.js www",
"build": "npm run build:src && npm run build:min && npm run build:types",
"build:src": "rollup -c",
"build:types": "tsc --emitDeclarationOnly",
"build:min": "uglifyjs dist/index.js -o dist/index.min.js",
"build:demo": "npm run build:src && node scripts/build-demo.js www",
"prepublishOnly": "npm run build",
"demo": "npm run build:demo && npx serve www",

@@ -84,2 +91,2 @@ "deploy:demo": "npm run build:demo && gh-pages -d www",

]
}
}
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