Commit 28f6e4d3 authored by Robert Knight's avatar Robert Knight

Add missing types in cross-origin-rpc.js

parent 66a89458
......@@ -6,14 +6,17 @@ import { normalizeGroupIds } from './helpers/groups';
* @typedef {import('../types/rpc').FocusUserInfo} FocusUserInfo
*/
// Array to keep track of pre-start requests
/**
* List of not-yet-processed messages received during application startup.
*
* @type {MessageEvent[]}
*/
let preStartQueue = [];
/**
* Return the mapped methods that can be called remotely via this server.
*
* @param {object} store - The global store
* @return {object}
* @param {import('./store').SidebarStore} store - The global store
*/
const registeredMethods = store => {
return {
......@@ -31,10 +34,19 @@ const registeredMethods = store => {
};
};
/**
* @typedef JSONRPCMessage
* @prop {string} jsonrpc
* @prop {string} id
* @prop {string} method
* @prop {unknown[]} params
*/
/**
* Return true if `data` "looks like" a JSON-RPC message.
*
* @param {any} data
* @return {data is JSONRPCMessage}
*/
function isJsonRpcMessage(data) {
// eslint-disable-next-line eqeqeq
......@@ -62,9 +74,14 @@ function isJsonRpcMessage(data) {
* notifications) and sending back a successful "ok" response.
*
* All methods called upon must be mapped in the `registeredMethods` function.
*
* @param {import('./store').SidebarStore} store
* @param {import('../types/config').SidebarSettings} settings
* @param {Window} $window
* @inject
*/
// @inject
export function startServer(store, settings, $window) {
/** @type {Record<string, (...args: any[]) => void>} */
const methods = registeredMethods(store);
// Process the pre-start incoming RPC requests
......@@ -78,6 +95,7 @@ export function startServer(store, settings, $window) {
// Start listening to new RPC requests
$window.addEventListener('message', receiveMessage);
/** @param {MessageEvent} event */
function receiveMessage(event) {
let allowedOrigins = settings.rpcAllowedOrigins || [];
......@@ -96,10 +114,15 @@ export function startServer(store, settings, $window) {
// data param.
let jsonRpcRequest = event.data;
event.source.postMessage(jsonRpcResponse(jsonRpcRequest), event.origin);
const source = /** @type {Window} */ (event.source);
source.postMessage(jsonRpcResponse(jsonRpcRequest), event.origin);
}
/** Return a JSON-RPC response to the given JSON-RPC request object. */
/**
* Return a JSON-RPC response to the given JSON-RPC request object.
*
* @param {JSONRPCMessage} request
*/
function jsonRpcResponse(request) {
const method = methods[request.method];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment