2021-02-04 19:15:23 +01:00
|
|
|
import log from 'loglevel';
|
2022-03-18 20:07:05 +01:00
|
|
|
import browser from 'webextension-polyfill';
|
2019-12-20 16:32:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a middleware that intercepts `wallet_registerOnboarding` messages
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2020-02-20 23:39:00 +01:00
|
|
|
* @param {{ location: string, registerOnboarding: Function }} opts - The middleware options
|
2019-12-20 16:32:31 +01:00
|
|
|
* @returns {(req: any, res: any, next: Function, end: Function) => void}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function createOnboardingMiddleware({
|
|
|
|
location,
|
|
|
|
registerOnboarding,
|
|
|
|
}) {
|
|
|
|
return async function originMiddleware(req, res, next, end) {
|
2019-12-20 16:32:31 +01:00
|
|
|
try {
|
|
|
|
if (req.method !== 'wallet_registerOnboarding') {
|
2021-02-04 19:15:23 +01:00
|
|
|
next();
|
|
|
|
return;
|
2019-12-20 16:32:31 +01:00
|
|
|
}
|
2022-03-18 20:07:05 +01:00
|
|
|
if (req.tabId && req.tabId !== browser.tabs.TAB_ID_NONE) {
|
2021-02-04 19:15:23 +01:00
|
|
|
await registerOnboarding(location, req.tabId);
|
2019-12-20 16:32:31 +01:00
|
|
|
} else {
|
2020-11-03 00:41:28 +01:00
|
|
|
log.debug(
|
|
|
|
`'wallet_registerOnboarding' message from ${location} ignored due to missing tabId`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-12-20 16:32:31 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
res.result = true;
|
|
|
|
end();
|
2019-12-20 16:32:31 +01:00
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
end(error);
|
2019-12-20 16:32:31 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-12-20 16:32:31 +01:00
|
|
|
}
|