init
This commit is contained in:
69
node_modules/matrix-widget-api/lib/transport/ITransport.d.ts
generated
vendored
Normal file
69
node_modules/matrix-widget-api/lib/transport/ITransport.d.ts
generated
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
import { EventEmitter } from "events";
|
||||
import { IWidgetApiAcknowledgeResponseData, IWidgetApiRequest, IWidgetApiRequestData, IWidgetApiResponse, IWidgetApiResponseData, WidgetApiAction } from "..";
|
||||
/**
|
||||
* A transport for widget requests/responses. All actions
|
||||
* get raised through a "message" CustomEvent with detail
|
||||
* of the IWidgetApiRequest.
|
||||
*/
|
||||
export interface ITransport extends EventEmitter {
|
||||
/**
|
||||
* True if the transport is ready to start sending, false otherwise.
|
||||
*/
|
||||
readonly ready: boolean;
|
||||
/**
|
||||
* The widget ID, if known. If not known, null.
|
||||
*/
|
||||
readonly widgetId: string | null;
|
||||
/**
|
||||
* If true, the transport will refuse requests from origins other than the
|
||||
* widget's current origin. This is intended to be used only by widgets which
|
||||
* need excess security.
|
||||
*/
|
||||
strictOriginCheck: boolean;
|
||||
/**
|
||||
* The origin the transport should be replying/sending to. If not known, leave
|
||||
* null.
|
||||
*/
|
||||
targetOrigin: string | null;
|
||||
/**
|
||||
* The number of seconds an outbound request is allowed to take before it
|
||||
* times out.
|
||||
*/
|
||||
timeoutSeconds: number;
|
||||
/**
|
||||
* Starts the transport for listening
|
||||
*/
|
||||
start(): void;
|
||||
/**
|
||||
* Stops the transport. It cannot be re-started.
|
||||
*/
|
||||
stop(): void;
|
||||
/**
|
||||
* Sends a request to the remote end.
|
||||
* @param action The action to send.
|
||||
* @param data The request data.
|
||||
* @returns A promise which resolves to the remote end's response.
|
||||
* @throws {Error} if the request failed with a generic error.
|
||||
* @throws {WidgetApiResponseError} if the request failed with error details
|
||||
* that can be communicated to the Widget API.
|
||||
*/
|
||||
send<T extends IWidgetApiRequestData, R extends IWidgetApiResponseData = IWidgetApiAcknowledgeResponseData>(action: WidgetApiAction, data: T): Promise<R>;
|
||||
/**
|
||||
* Sends a request to the remote end. This is similar to the send() function
|
||||
* however this version returns the full response rather than just the response
|
||||
* data.
|
||||
* @param {WidgetApiAction} action The action to send.
|
||||
* @param {IWidgetApiRequestData} data The request data.
|
||||
* @returns {Promise<IWidgetApiResponseData>} A promise which resolves to the remote end's response
|
||||
* @throws {Error} if the request failed with a generic error.
|
||||
* @throws {WidgetApiResponseError} if the request failed with error details
|
||||
* that can be communicated to the Widget API.
|
||||
*/
|
||||
sendComplete<T extends IWidgetApiRequestData, R extends IWidgetApiResponse>(action: WidgetApiAction, data: T): Promise<R>;
|
||||
/**
|
||||
* Replies to a request.
|
||||
* @param {IWidgetApiRequest} request The request to reply to.
|
||||
* @param {IWidgetApiResponseData} responseData The response data to reply with.
|
||||
*/
|
||||
reply<T extends IWidgetApiResponseData>(request: IWidgetApiRequest, responseData: T): void;
|
||||
}
|
||||
6
node_modules/matrix-widget-api/lib/transport/ITransport.js
generated
vendored
Normal file
6
node_modules/matrix-widget-api/lib/transport/ITransport.js
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
//# sourceMappingURL=ITransport.js.map
|
||||
1
node_modules/matrix-widget-api/lib/transport/ITransport.js.map
generated
vendored
Normal file
1
node_modules/matrix-widget-api/lib/transport/ITransport.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ITransport.js","names":[],"sources":["../../src/transport/ITransport.ts"],"sourcesContent":["/*\n * Copyright 2020 - 2024 The Matrix.org Foundation C.I.C.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { EventEmitter } from \"events\";\n\nimport {\n IWidgetApiAcknowledgeResponseData,\n IWidgetApiRequest,\n IWidgetApiRequestData,\n IWidgetApiResponse,\n IWidgetApiResponseData,\n WidgetApiAction,\n} from \"..\";\n\n/**\n * A transport for widget requests/responses. All actions\n * get raised through a \"message\" CustomEvent with detail\n * of the IWidgetApiRequest.\n */\nexport interface ITransport extends EventEmitter {\n /**\n * True if the transport is ready to start sending, false otherwise.\n */\n readonly ready: boolean;\n\n /**\n * The widget ID, if known. If not known, null.\n */\n readonly widgetId: string | null;\n\n /**\n * If true, the transport will refuse requests from origins other than the\n * widget's current origin. This is intended to be used only by widgets which\n * need excess security.\n */\n strictOriginCheck: boolean;\n\n /**\n * The origin the transport should be replying/sending to. If not known, leave\n * null.\n */\n targetOrigin: string | null;\n\n /**\n * The number of seconds an outbound request is allowed to take before it\n * times out.\n */\n timeoutSeconds: number;\n\n /**\n * Starts the transport for listening\n */\n start(): void;\n\n /**\n * Stops the transport. It cannot be re-started.\n */\n stop(): void;\n\n /**\n * Sends a request to the remote end.\n * @param action The action to send.\n * @param data The request data.\n * @returns A promise which resolves to the remote end's response.\n * @throws {Error} if the request failed with a generic error.\n * @throws {WidgetApiResponseError} if the request failed with error details\n * that can be communicated to the Widget API.\n */\n send<T extends IWidgetApiRequestData, R extends IWidgetApiResponseData = IWidgetApiAcknowledgeResponseData>(\n action: WidgetApiAction,\n data: T,\n ): Promise<R>;\n\n /**\n * Sends a request to the remote end. This is similar to the send() function\n * however this version returns the full response rather than just the response\n * data.\n * @param {WidgetApiAction} action The action to send.\n * @param {IWidgetApiRequestData} data The request data.\n * @returns {Promise<IWidgetApiResponseData>} A promise which resolves to the remote end's response\n * @throws {Error} if the request failed with a generic error.\n * @throws {WidgetApiResponseError} if the request failed with error details\n * that can be communicated to the Widget API.\n */\n sendComplete<T extends IWidgetApiRequestData, R extends IWidgetApiResponse>(\n action: WidgetApiAction,\n data: T,\n ): Promise<R>;\n\n /**\n * Replies to a request.\n * @param {IWidgetApiRequest} request The request to reply to.\n * @param {IWidgetApiResponseData} responseData The response data to reply with.\n */\n reply<T extends IWidgetApiResponseData>(request: IWidgetApiRequest, responseData: T): void;\n}\n"],"mappings":"","ignoreList":[]}
|
||||
31
node_modules/matrix-widget-api/lib/transport/PostmessageTransport.d.ts
generated
vendored
Normal file
31
node_modules/matrix-widget-api/lib/transport/PostmessageTransport.d.ts
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
import { EventEmitter } from "events";
|
||||
import { ITransport } from "./ITransport";
|
||||
import { IWidgetApiRequest, IWidgetApiRequestData, IWidgetApiResponse, IWidgetApiResponseData, WidgetApiAction, WidgetApiDirection } from "..";
|
||||
/**
|
||||
* Transport for the Widget API over postMessage.
|
||||
*/
|
||||
export declare class PostmessageTransport extends EventEmitter implements ITransport {
|
||||
private readonly sendDirection;
|
||||
private readonly transportWindow;
|
||||
private readonly inboundWindow;
|
||||
strictOriginCheck: boolean;
|
||||
targetOrigin: string;
|
||||
timeoutSeconds: number;
|
||||
private _ready;
|
||||
private _widgetId;
|
||||
private readonly outboundRequests;
|
||||
private readonly stopController;
|
||||
get ready(): boolean;
|
||||
get widgetId(): string | null;
|
||||
constructor(sendDirection: WidgetApiDirection, initialWidgetId: string | null, transportWindow: Window | typeof globalThis, inboundWindow: Window | typeof globalThis);
|
||||
private get nextRequestId();
|
||||
private sendInternal;
|
||||
reply<T extends IWidgetApiResponseData>(request: IWidgetApiRequest, responseData: T): void;
|
||||
send<T extends IWidgetApiRequestData, R extends IWidgetApiResponseData>(action: WidgetApiAction, data: T): Promise<R>;
|
||||
sendComplete<T extends IWidgetApiRequestData, R extends IWidgetApiResponse>(action: WidgetApiAction, data: T): Promise<R>;
|
||||
start(): void;
|
||||
stop(): void;
|
||||
private readonly handleMessage;
|
||||
private handleRequest;
|
||||
private handleResponse;
|
||||
}
|
||||
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
|
||||
1
node_modules/matrix-widget-api/lib/transport/PostmessageTransport.js.map
generated
vendored
Normal file
1
node_modules/matrix-widget-api/lib/transport/PostmessageTransport.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user