Unverified Commit cd6c4c31 authored by Kyle Keating's avatar Kyle Keating Committed by GitHub

requestGroups RPC call should not timeout (#2159)

Change the client RPC request to optionally bypass the timeout. There could be a case where it's waiting for user input to proceed with authorization and could take any number of seconds.
parent ec84d3be
...@@ -160,8 +160,9 @@ async function fetchGroupsAsync(config, rpcCall) { ...@@ -160,8 +160,9 @@ async function fetchGroupsAsync(config, rpcCall) {
if (Array.isArray(config.services)) { if (Array.isArray(config.services)) {
config.services.forEach((service, index) => { config.services.forEach((service, index) => {
if (service.groups === '$rpc:requestGroups') { if (service.groups === '$rpc:requestGroups') {
// The `groups` need to be fetched from a secondary RPC call // The `groups` need to be fetched from a secondary RPC call and
service.groups = rpcCall('requestGroups', [index], 5000).catch(() => { // there should be no timeout as it may be waiting for user input.
service.groups = rpcCall('requestGroups', [index], null).catch(() => {
throw new Error('Unable to fetch groups'); throw new Error('Unable to fetch groups');
}); });
} }
......
...@@ -21,7 +21,8 @@ function createTimeout(delay, message) { ...@@ -21,7 +21,8 @@ function createTimeout(delay, message) {
* @param {string} origin - Origin filter for `window.postMessage` call * @param {string} origin - Origin filter for `window.postMessage` call
* @param {string} method - Name of the JSON-RPC method * @param {string} method - Name of the JSON-RPC method
* @param {any[]} params - Parameters of the JSON-RPC method * @param {any[]} params - Parameters of the JSON-RPC method
* @param [number] timeout - Maximum time to wait in ms * @param {number|null} [number=2000] timeout - Maximum time to wait in
* ms or null or 0 to ignore timeout.
* @param [Window] window_ - Test seam. * @param [Window] window_ - Test seam.
* @param [id] id - Test seam. * @param [id] id - Test seam.
* @return {Promise<any>} - A Promise for the response to the call * @return {Promise<any>} - A Promise for the response to the call
...@@ -79,14 +80,16 @@ export function call( ...@@ -79,14 +80,16 @@ export function call(
window_.addEventListener('message', listener); window_.addEventListener('message', listener);
}); });
const timeoutExpired = createTimeout( const responseOrTimeout = [response];
timeout, if (timeout) {
`Request to ${origin} timed out` responseOrTimeout.push(
); createTimeout(timeout, `Request to ${origin} timed out`)
);
}
// Cleanup and return. // Cleanup and return.
// FIXME: If we added a `Promise.finally` polyfill we could simplify this. // FIXME: If we added a `Promise.finally` polyfill we could simplify this.
return Promise.race([response, timeoutExpired]) return Promise.race(responseOrTimeout)
.then(result => { .then(result => {
window_.removeEventListener('message', listener); window_.removeEventListener('message', listener);
return result; return result;
......
...@@ -257,7 +257,7 @@ describe('sidebar.util.fetch-config', () => { ...@@ -257,7 +257,7 @@ describe('sidebar.util.fetch-config', () => {
'https://embedder.com', 'https://embedder.com',
'requestGroups', 'requestGroups',
[0], // passes service index to requestGroups [0], // passes service index to requestGroups
5000 null // no timeout
) )
); );
}); });
......
...@@ -18,8 +18,7 @@ describe('sidebar.util.postmessage-json-rpc', () => { ...@@ -18,8 +18,7 @@ describe('sidebar.util.postmessage-json-rpc', () => {
let frame; let frame;
let fakeWindow; let fakeWindow;
function doCall() { function doCall(timeout = 1) {
const timeout = 1;
return call( return call(
frame, frame,
origin, origin,
...@@ -146,5 +145,21 @@ describe('sidebar.util.postmessage-json-rpc', () => { ...@@ -146,5 +145,21 @@ describe('sidebar.util.postmessage-json-rpc', () => {
'Request to https://embedder.com timed out' 'Request to https://embedder.com timed out'
); );
}); });
it('if timeout is null, then it does not timeout', async () => {
const clock = sinon.useFakeTimers();
const result = doCall(null);
clock.tick(100000); // wait a long time
fakeWindow.emitter.emit('message', {
origin,
data: {
jsonrpc: '2.0',
id: messageId,
result: {},
},
});
await result;
clock.restore();
});
}); });
}); });
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