Skip to content

Commit

Permalink
refactor(transport): pass sessionsBackgroundUrl to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
marekrjpolak committed Sep 27, 2024
1 parent 4ef300c commit 971409a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 46 deletions.
10 changes: 2 additions & 8 deletions packages/connect/src/device/DeviceList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,11 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> implements IDevic

const transportLogger = initLog('@trezor/transport', debug);

// todo: this should be passed from above
const abortController = new AbortController();

this.authPenaltyManager = createAuthPenaltyManager(priority);
this.transportCommonArgs = {
messages,
logger: transportLogger,
signal: abortController.signal,
_sessionsBackgroundUrl,
sessionsBackgroundUrl: _sessionsBackgroundUrl,
};

this.transports = [
Expand Down Expand Up @@ -338,9 +334,7 @@ export class DeviceList extends TypedEmitter<DeviceListEvents> implements IDevic
}

private async selectTransport([transport, ...rest]: Transport[]): Promise<Transport> {
const result = await transport.init({
sessionsBackgroundUrl: this.transportCommonArgs._sessionsBackgroundUrl,
});
const result = await transport.init();
if (result.success) return transport;
else if (rest.length) return this.selectTransport(rest);
else throw new Error(result.error);
Expand Down
6 changes: 1 addition & 5 deletions packages/transport/src/transports/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,7 @@ export abstract class AbstractTransport extends TransportEmitter {
/**
* Tries to initiate transport. Transport might not be available e.g. bridge not running.
*/
abstract init(
params?: AbortableParam & {
sessionsBackgroundUrl?: string;
},
): AsyncResultWithTypedError<
abstract init(params?: AbortableParam): AsyncResultWithTypedError<
undefined,
// webusb only
| typeof ERRORS.SESSION_BACKGROUND_TIMEOUT
Expand Down
33 changes: 18 additions & 15 deletions packages/transport/src/transports/abstractApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,27 @@ export abstract class AbstractApiTransport extends AbstractTransport {
this.api = api;
}

public init(_args: AbstractTransportMethodParams<'init'>) {
return this.scheduleAction(async () => {
// in nodeusb there is no synchronization yet. this is a followup and needs to be decided
// so far, sessionsClient has direct access to sessionBackground
this.sessionsClient.init({
requestFn: args => this.sessionsBackground.handleMessage(args),
registerBackgroundCallbacks: () => {},
});
public init({ signal }: AbstractTransportMethodParams<'init'> = {}) {
return this.scheduleAction(
async () => {
// in nodeusb there is no synchronization yet. this is a followup and needs to be decided
// so far, sessionsClient has direct access to sessionBackground
this.sessionsClient.init({
requestFn: args => this.sessionsBackground.handleMessage(args),
registerBackgroundCallbacks: () => {},
});

this.sessionsBackground.on('descriptors', descriptors => {
this.sessionsClient.emit('descriptors', descriptors);
});
this.sessionsBackground.on('descriptors', descriptors => {
this.sessionsClient.emit('descriptors', descriptors);
});

const handshakeRes = await this.sessionsClient.handshake();
this.stopped = !handshakeRes.success;
const handshakeRes = await this.sessionsClient.handshake();
this.stopped = !handshakeRes.success;

return handshakeRes;
});
return handshakeRes;
},
{ signal },
);
}

public listen() {
Expand Down
43 changes: 25 additions & 18 deletions packages/transport/src/transports/webusb.browser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { AbstractTransportParams } from './abstract';
import { AbstractTransportMethodParams, AbstractTransportParams } from './abstract';
import { AbstractApiTransport } from './abstractApi';
import { UsbApi } from '../api/usb';

import { initBackgroundInBrowser } from '../sessions/background-browser';

type WebUsbTransportParams = AbstractTransportParams & { sessionsBackgroundUrl?: string };

/**
* WebUsbTransport
* - chrome supported
Expand All @@ -12,9 +14,9 @@ import { initBackgroundInBrowser } from '../sessions/background-browser';
export class WebUsbTransport extends AbstractApiTransport {
public name = 'WebUsbTransport' as const;

constructor(params: AbstractTransportParams) {
const { messages, logger } = params;
private readonly sessionsBackgroundUrl;

constructor({ messages, logger, sessionsBackgroundUrl }: WebUsbTransportParams) {
super({
messages,
api: new UsbApi({
Expand All @@ -23,23 +25,28 @@ export class WebUsbTransport extends AbstractApiTransport {
}),
logger,
});
this.sessionsBackgroundUrl = sessionsBackgroundUrl;
}

public init({ sessionsBackgroundUrl }: { sessionsBackgroundUrl: string }) {
return this.scheduleAction(async () => {
const { requestFn, registerBackgroundCallbacks } =
await initBackgroundInBrowser(sessionsBackgroundUrl);
// sessions client initiated with a request fn facilitating communication with a session backend (shared worker in case of webusb)
this.sessionsClient.init({
requestFn,
registerBackgroundCallbacks,
});

const handshakeRes = await this.sessionsClient.handshake();
this.stopped = !handshakeRes.success;

return handshakeRes;
});
public init({ signal }: AbstractTransportMethodParams<'init'> = {}) {
return this.scheduleAction(
async () => {
const { sessionsBackgroundUrl } = this;
const { requestFn, registerBackgroundCallbacks } =
await initBackgroundInBrowser(sessionsBackgroundUrl);
// sessions client initiated with a request fn facilitating communication with a session backend (shared worker in case of webusb)
this.sessionsClient.init({
requestFn,
registerBackgroundCallbacks,
});

const handshakeRes = await this.sessionsClient.handshake();
this.stopped = !handshakeRes.success;

return handshakeRes;
},
{ signal },
);
}

public listen() {
Expand Down

0 comments on commit 971409a

Please sign in to comment.