init
This commit is contained in:
220
node_modules/matrix-widget-api/lib/transport/PostmessageTransport.js
generated
vendored
Normal file
220
node_modules/matrix-widget-api/lib/transport/PostmessageTransport.js
generated
vendored
Normal file
@@ -0,0 +1,220 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.PostmessageTransport = void 0;
|
||||
var _events = require("events");
|
||||
var _ = require("..");
|
||||
var _excluded = ["message"];
|
||||
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
||||
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
||||
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
||||
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
||||
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
||||
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
||||
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
||||
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
||||
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
|
||||
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
|
||||
function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = _getPrototypeOf(t); if (r) { var s = _getPrototypeOf(this).constructor; e = Reflect.construct(o, arguments, s); } else e = o.apply(this, arguments); return _possibleConstructorReturn(this, e); }; }
|
||||
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
|
||||
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
|
||||
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
||||
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
|
||||
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
||||
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
||||
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*
|
||||
* Copyright 2020 - 2024 The Matrix.org Foundation C.I.C.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/**
|
||||
* Transport for the Widget API over postMessage.
|
||||
*/
|
||||
var PostmessageTransport = /*#__PURE__*/function (_EventEmitter) {
|
||||
_inherits(PostmessageTransport, _EventEmitter);
|
||||
var _super = _createSuper(PostmessageTransport);
|
||||
function PostmessageTransport(sendDirection, initialWidgetId, transportWindow, inboundWindow) {
|
||||
var _this;
|
||||
_classCallCheck(this, PostmessageTransport);
|
||||
_this = _super.call(this);
|
||||
_this.sendDirection = sendDirection;
|
||||
_this.transportWindow = transportWindow;
|
||||
_this.inboundWindow = inboundWindow;
|
||||
_defineProperty(_assertThisInitialized(_this), "strictOriginCheck", false);
|
||||
_defineProperty(_assertThisInitialized(_this), "targetOrigin", "*");
|
||||
_defineProperty(_assertThisInitialized(_this), "timeoutSeconds", 10);
|
||||
_defineProperty(_assertThisInitialized(_this), "_ready", false);
|
||||
_defineProperty(_assertThisInitialized(_this), "_widgetId", void 0);
|
||||
_defineProperty(_assertThisInitialized(_this), "outboundRequests", new Map());
|
||||
_defineProperty(_assertThisInitialized(_this), "stopController", new AbortController());
|
||||
_defineProperty(_assertThisInitialized(_this), "handleMessage", function (ev) {
|
||||
if (_this.stopController.signal.aborted) return;
|
||||
if (!ev.data) return; // invalid event
|
||||
|
||||
if (_this.strictOriginCheck && ev.origin !== globalThis.origin) return; // bad origin
|
||||
|
||||
// treat the message as a response first, then downgrade to a request
|
||||
var response = ev.data;
|
||||
if (!response.action || !response.requestId || !response.widgetId) return; // invalid request/response
|
||||
|
||||
if (response.response) {
|
||||
if (response.api !== _this.sendDirection) return; // wrong direction
|
||||
_this.handleResponse(response);
|
||||
} else {
|
||||
var request = response;
|
||||
if (request.api !== (0, _.invertedDirection)(_this.sendDirection)) return; // wrong direction
|
||||
_this.handleRequest(request);
|
||||
}
|
||||
});
|
||||
_this._widgetId = initialWidgetId;
|
||||
return _this;
|
||||
}
|
||||
_createClass(PostmessageTransport, [{
|
||||
key: "ready",
|
||||
get: function get() {
|
||||
return this._ready;
|
||||
}
|
||||
}, {
|
||||
key: "widgetId",
|
||||
get: function get() {
|
||||
return this._widgetId || null;
|
||||
}
|
||||
}, {
|
||||
key: "nextRequestId",
|
||||
get: function get() {
|
||||
var idBase = "widgetapi-".concat(Date.now());
|
||||
var index = 0;
|
||||
var id = idBase;
|
||||
while (this.outboundRequests.has(id)) {
|
||||
id = "".concat(idBase, "-").concat(index++);
|
||||
}
|
||||
|
||||
// reserve the ID
|
||||
this.outboundRequests.set(id, null);
|
||||
return id;
|
||||
}
|
||||
}, {
|
||||
key: "sendInternal",
|
||||
value: function sendInternal(message) {
|
||||
console.log("[PostmessageTransport] Sending object to ".concat(this.targetOrigin, ": "), message);
|
||||
this.transportWindow.postMessage(message, this.targetOrigin);
|
||||
}
|
||||
}, {
|
||||
key: "reply",
|
||||
value: function reply(request, responseData) {
|
||||
return this.sendInternal(_objectSpread(_objectSpread({}, request), {}, {
|
||||
response: responseData
|
||||
}));
|
||||
}
|
||||
}, {
|
||||
key: "send",
|
||||
value: function send(action, data) {
|
||||
return this.sendComplete(action, data).then(function (r) {
|
||||
return r.response;
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: "sendComplete",
|
||||
value: function sendComplete(action, data) {
|
||||
var _this2 = this;
|
||||
if (!this.ready || !this.widgetId) {
|
||||
return Promise.reject(new Error("Not ready or unknown widget ID"));
|
||||
}
|
||||
var request = {
|
||||
api: this.sendDirection,
|
||||
widgetId: this.widgetId,
|
||||
requestId: this.nextRequestId,
|
||||
action: action,
|
||||
data: data
|
||||
};
|
||||
if (action === _.WidgetApiToWidgetAction.UpdateVisibility) {
|
||||
request["visible"] = data["visible"];
|
||||
}
|
||||
return new Promise(function (prResolve, prReject) {
|
||||
var resolve = function resolve(response) {
|
||||
cleanUp();
|
||||
prResolve(response);
|
||||
};
|
||||
var reject = function reject(err) {
|
||||
cleanUp();
|
||||
prReject(err);
|
||||
};
|
||||
var timerId = setTimeout(function () {
|
||||
return reject(new Error("Request timed out"));
|
||||
}, (_this2.timeoutSeconds || 1) * 1000);
|
||||
var onStop = function onStop() {
|
||||
return reject(new Error("Transport stopped"));
|
||||
};
|
||||
_this2.stopController.signal.addEventListener("abort", onStop);
|
||||
var cleanUp = function cleanUp() {
|
||||
_this2.outboundRequests["delete"](request.requestId);
|
||||
clearTimeout(timerId);
|
||||
_this2.stopController.signal.removeEventListener("abort", onStop);
|
||||
};
|
||||
_this2.outboundRequests.set(request.requestId, {
|
||||
request: request,
|
||||
resolve: resolve,
|
||||
reject: reject
|
||||
});
|
||||
_this2.sendInternal(request);
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: "start",
|
||||
value: function start() {
|
||||
this.inboundWindow.addEventListener("message", this.handleMessage);
|
||||
this._ready = true;
|
||||
}
|
||||
}, {
|
||||
key: "stop",
|
||||
value: function stop() {
|
||||
this._ready = false;
|
||||
this.stopController.abort();
|
||||
this.inboundWindow.removeEventListener("message", this.handleMessage);
|
||||
}
|
||||
}, {
|
||||
key: "handleRequest",
|
||||
value: function handleRequest(request) {
|
||||
if (this.widgetId) {
|
||||
if (this.widgetId !== request.widgetId) return; // wrong widget
|
||||
} else {
|
||||
this._widgetId = request.widgetId;
|
||||
}
|
||||
this.emit("message", new CustomEvent("message", {
|
||||
detail: request
|
||||
}));
|
||||
}
|
||||
}, {
|
||||
key: "handleResponse",
|
||||
value: function handleResponse(response) {
|
||||
if (response.widgetId !== this.widgetId) return; // wrong widget
|
||||
|
||||
var req = this.outboundRequests.get(response.requestId);
|
||||
if (!req) return; // response to an unknown request
|
||||
|
||||
if ((0, _.isErrorResponse)(response.response)) {
|
||||
var _response$response$er = response.response.error,
|
||||
message = _response$response$er.message,
|
||||
data = _objectWithoutProperties(_response$response$er, _excluded);
|
||||
req.reject(new _.WidgetApiResponseError(message, data));
|
||||
} else {
|
||||
req.resolve(response);
|
||||
}
|
||||
}
|
||||
}]);
|
||||
return PostmessageTransport;
|
||||
}(_events.EventEmitter);
|
||||
exports.PostmessageTransport = PostmessageTransport;
|
||||
//# sourceMappingURL=PostmessageTransport.js.map
|
||||
Reference in New Issue
Block a user