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'; ...@@ -6,14 +6,17 @@ import { normalizeGroupIds } from './helpers/groups';
* @typedef {import('../types/rpc').FocusUserInfo} FocusUserInfo * @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 = []; let preStartQueue = [];
/** /**
* Return the mapped methods that can be called remotely via this server. * Return the mapped methods that can be called remotely via this server.
* *
* @param {object} store - The global store * @param {import('./store').SidebarStore} store - The global store
* @return {object}
*/ */
const registeredMethods = store => { const registeredMethods = store => {
return { return {
...@@ -31,10 +34,19 @@ const registeredMethods = store => { ...@@ -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. * Return true if `data` "looks like" a JSON-RPC message.
* *
* @param {any} data * @param {any} data
* @return {data is JSONRPCMessage}
*/ */
function isJsonRpcMessage(data) { function isJsonRpcMessage(data) {
// eslint-disable-next-line eqeqeq // eslint-disable-next-line eqeqeq
...@@ -62,9 +74,14 @@ function isJsonRpcMessage(data) { ...@@ -62,9 +74,14 @@ function isJsonRpcMessage(data) {
* notifications) and sending back a successful "ok" response. * notifications) and sending back a successful "ok" response.
* *
* All methods called upon must be mapped in the `registeredMethods` function. * 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) { export function startServer(store, settings, $window) {
/** @type {Record<string, (...args: any[]) => void>} */
const methods = registeredMethods(store); const methods = registeredMethods(store);
// Process the pre-start incoming RPC requests // Process the pre-start incoming RPC requests
...@@ -78,6 +95,7 @@ export function startServer(store, settings, $window) { ...@@ -78,6 +95,7 @@ export function startServer(store, settings, $window) {
// Start listening to new RPC requests // Start listening to new RPC requests
$window.addEventListener('message', receiveMessage); $window.addEventListener('message', receiveMessage);
/** @param {MessageEvent} event */
function receiveMessage(event) { function receiveMessage(event) {
let allowedOrigins = settings.rpcAllowedOrigins || []; let allowedOrigins = settings.rpcAllowedOrigins || [];
...@@ -96,10 +114,15 @@ export function startServer(store, settings, $window) { ...@@ -96,10 +114,15 @@ export function startServer(store, settings, $window) {
// data param. // data param.
let jsonRpcRequest = event.data; 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) { function jsonRpcResponse(request) {
const method = methods[request.method]; 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