-
-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(transport): background sessions improved
- Loading branch information
1 parent
6566b02
commit 7644107
Showing
10 changed files
with
133 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,69 @@ | ||
import type { Descriptor } from '../types'; | ||
import { SessionsBackground } from './background'; | ||
|
||
import { RegisterBackgroundCallbacks } from './types'; | ||
|
||
const defaultSessionsBackgroundUrl = | ||
window.location.origin + | ||
`${process.env.ASSET_PREFIX || ''}/workers/sessions-background-sharedworker.js` | ||
// just in case so that whoever defines ASSET_PREFIX does not need to worry about trailing slashes | ||
.replace(/\/+/g, '/'); | ||
import { HandleMessageParams, HandleMessageResponse, SessionsBackgroundInterface } from './types'; | ||
|
||
/** | ||
* calling initBackgroundInBrowser initiates sessions-background for browser based environments and returns: | ||
* - `requestFn` which is used to send messages to sessions background | ||
* - `registerBackgroundCallbacks` which is used to accept information about descriptors change from | ||
* creating BrowserSessionsBackground initiates sessions-background for browser based environments and provides: | ||
* - `handleMessage` which is used to send messages to sessions background | ||
* - `on` which is used to accept information about descriptors change from | ||
* another tab and notify local transport | ||
* if possible sessions background utilizes native Sharedworker. If for whatever reason | ||
* Sharedworker is not available, it fallbacks to local module behavior | ||
* Sharedworker is not available, the constructor throws an error. | ||
*/ | ||
export const initBackgroundInBrowser = async ( | ||
sessionsBackgroundUrl = defaultSessionsBackgroundUrl, | ||
): Promise<{ | ||
background: SessionsBackground | SharedWorker; | ||
requestFn: SessionsBackground['handleMessage']; | ||
registerBackgroundCallbacks: RegisterBackgroundCallbacks; | ||
}> => { | ||
try { | ||
// fetch to validate - failed fetch via SharedWorker constructor does not throw. It even hangs resulting in all kinds of weird behaviors | ||
await fetch(sessionsBackgroundUrl, { method: 'HEAD' }).then(response => { | ||
if (!response.ok) { | ||
throw new Error( | ||
`Failed to fetch sessions-background SharedWorker from url: ${sessionsBackgroundUrl}`, | ||
); | ||
} | ||
}); | ||
const background = new SharedWorker(sessionsBackgroundUrl, { | ||
export class BrowserSessionsBackground implements SessionsBackgroundInterface { | ||
private readonly background; | ||
|
||
constructor(sessionsBackgroundUrl: string) { | ||
this.background = new SharedWorker(sessionsBackgroundUrl, { | ||
name: '@trezor/connect-web transport sessions worker', | ||
}); | ||
} | ||
|
||
const requestFn: SessionsBackground['handleMessage'] = ( | ||
params: Parameters<SessionsBackground['handleMessage']>[0], | ||
) => | ||
new Promise(resolve => { | ||
const onmessage = (message: MessageEvent<any>) => { | ||
if (params.id === message.data.id) { | ||
resolve(message.data); | ||
background.port.removeEventListener('message', onmessage); | ||
} | ||
}; | ||
handleMessage<M extends HandleMessageParams>(params: M): Promise<HandleMessageResponse<M>> { | ||
const { background } = this; | ||
|
||
background.port.addEventListener('message', onmessage); | ||
return new Promise(resolve => { | ||
const onmessage = (message: MessageEvent<any>) => { | ||
if (params.id === message.data.id) { | ||
resolve(message.data); | ||
background.port.removeEventListener('message', onmessage); | ||
} | ||
}; | ||
|
||
background.port.onmessageerror = message => { | ||
// not sure under what circumstances this error occurs. let's observe it during testing | ||
console.error('background-browser onmessageerror,', message); | ||
background.port.addEventListener('message', onmessage); | ||
|
||
background.port.removeEventListener('message', onmessage); | ||
}; | ||
background.port.postMessage(params); | ||
}); | ||
background.port.onmessageerror = message => { | ||
// not sure under what circumstances this error occurs. let's observe it during testing | ||
console.error('background-browser onmessageerror,', message); | ||
|
||
const registerBackgroundCallbacks: RegisterBackgroundCallbacks = onDescriptorsCallback => { | ||
background.port.addEventListener( | ||
'message', | ||
( | ||
e: MessageEvent< | ||
// either standard response from sessions background (we ignore this one) | ||
| Awaited<ReturnType<SessionsBackground['handleMessage']>> | ||
// or artificially broadcasted message to all clients (see background-sharedworker) | ||
| { type: 'descriptors'; payload: Descriptor[] } | ||
>, | ||
) => { | ||
if ('type' in e?.data) { | ||
if (e.data.type === 'descriptors') { | ||
onDescriptorsCallback(e.data.payload); | ||
} | ||
} | ||
}, | ||
); | ||
}; | ||
background.port.removeEventListener('message', onmessage); | ||
}; | ||
background.port.postMessage(params); | ||
}); | ||
} | ||
|
||
return { background, requestFn, registerBackgroundCallbacks }; | ||
} catch (err) { | ||
console.warn( | ||
'Unable to load background-sharedworker. Falling back to use local module. Say bye bye to tabs synchronization. Error details: ', | ||
err.message, | ||
on(_event: 'descriptors', listener: (descriptors: Descriptor[]) => void): void { | ||
this.background.port.addEventListener( | ||
'message', | ||
( | ||
e: MessageEvent< | ||
// either standard response from sessions background (we ignore this one) | ||
| Awaited<ReturnType<SessionsBackground['handleMessage']>> | ||
// or artificially broadcasted message to all clients (see background-sharedworker) | ||
| { type: 'descriptors'; payload: Descriptor[] } | ||
>, | ||
) => { | ||
if ('type' in e?.data) { | ||
if (e.data.type === 'descriptors') { | ||
listener(e.data.payload); | ||
} | ||
} | ||
}, | ||
); | ||
} | ||
|
||
const background = new SessionsBackground(); | ||
const registerBackgroundCallbacks: RegisterBackgroundCallbacks = onDescriptorsCallback => { | ||
background.on('descriptors', descriptors => { | ||
onDescriptorsCallback(descriptors); | ||
}); | ||
}; | ||
|
||
return { | ||
background, | ||
requestFn: background.handleMessage.bind(background), | ||
registerBackgroundCallbacks, | ||
}; | ||
dispose() { | ||
/* is it needed? */ | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.