Skip to content

Commit

Permalink
Revert "feat(transport): add scheduleActionParams to call api"
Browse files Browse the repository at this point in the history
This reverts commit 40a0530.
  • Loading branch information
mroz22 committed Apr 23, 2024
1 parent 91e2f7c commit 4de6cbe
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 85 deletions.
1 change: 0 additions & 1 deletion packages/transport/src/transports/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ export abstract class AbstractTransport extends TypedEmitter<{
name: string;
data: Record<string, unknown>;
protocol?: TransportProtocol;
scheduleActionParams?: ScheduleActionParams;
}): AbortableCall<
MessageFromTrezor,
// bridge
Expand Down
121 changes: 61 additions & 60 deletions packages/transport/src/transports/abstractApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,73 +181,74 @@ export abstract class AbstractApiTransport extends AbstractTransport {
name,
data,
protocol: customProtocol,
scheduleActionParams,
}: AbstractTransportMethodParams<'call'>) {
return this.scheduleAction(async () => {
const getPathBySessionResponse = await this.sessionsClient.getPathBySession({
session,
});
if (!getPathBySessionResponse.success) {
// session not found means that device was disconnected
if (getPathBySessionResponse.error === 'session not found') {
return this.error({ error: ERRORS.DEVICE_DISCONNECTED_DURING_ACTION });
}

return this.error({ error: ERRORS.UNEXPECTED_ERROR });
}
const { path } = getPathBySessionResponse.payload;

try {
const protocol = customProtocol || v1Protocol;
const bytes = buildMessage({
messages: this.messages,
name,
data,
encode: protocol.encode,
return this.scheduleAction(
async () => {
const getPathBySessionResponse = await this.sessionsClient.getPathBySession({
session,
});
const buffers = createChunks(
bytes,
protocol.getChunkHeader(bytes),
this.api.chunkSize,
);

for (let i = 0; i < buffers.length; i++) {
const chunk = buffers[i];
if (!getPathBySessionResponse.success) {
// session not found means that device was disconnected
if (getPathBySessionResponse.error === 'session not found') {
return this.error({ error: ERRORS.DEVICE_DISCONNECTED_DURING_ACTION });
}

await this.api.write(path, chunk).then(result => {
if (!result.success) {
throw new Error(result.error);
}
});
return this.error({ error: ERRORS.UNEXPECTED_ERROR });
}

const message = await receiveAndParse(
this.messages,
() =>
this.api.read(path).then(result => {
if (result.success) {
return result.payload;
const { path } = getPathBySessionResponse.payload;

try {
const protocol = customProtocol || v1Protocol;
const bytes = buildMessage({
messages: this.messages,
name,
data,
encode: protocol.encode,
});
const buffers = createChunks(
bytes,
protocol.getChunkHeader(bytes),
this.api.chunkSize,
);
for (let i = 0; i < buffers.length; i++) {
const chunk = buffers[i];

await this.api.write(path, chunk).then(result => {
if (!result.success) {
throw new Error(result.error);
}
throw new Error(result.error);
}),
protocol,
);
});
}

return this.success(message);
} catch (err) {
// if user revokes usb permissions in browser we need a way how propagate that the device was technically disconnected,
if (err.message === ERRORS.DEVICE_DISCONNECTED_DURING_ACTION) {
this.enumerate();
}
const message = await receiveAndParse(
this.messages,
() =>
this.api.read(path).then(result => {
if (result.success) {
return result.payload;
}
throw new Error(result.error);
}),
protocol,
);

return this.success(message);
} catch (err) {
// if user revokes usb permissions in browser we need a way how propagate that the device was technically disconnected,
if (err.message === ERRORS.DEVICE_DISCONNECTED_DURING_ACTION) {
this.enumerate();
}

return this.unknownError(err, [
ERRORS.DEVICE_DISCONNECTED_DURING_ACTION,
ERRORS.DEVICE_NOT_FOUND,
ERRORS.INTERFACE_UNABLE_TO_OPEN_DEVICE,
ERRORS.INTERFACE_DATA_TRANSFER,
]);
}
}, scheduleActionParams);
return this.unknownError(err, [
ERRORS.DEVICE_DISCONNECTED_DURING_ACTION,
ERRORS.DEVICE_NOT_FOUND,
ERRORS.INTERFACE_UNABLE_TO_OPEN_DEVICE,
ERRORS.INTERFACE_DATA_TRANSFER,
]);
}
},
{ timeout: undefined },
);
}

public send({ data, session, name, protocol }: AbstractTransportMethodParams<'send'>) {
Expand Down
50 changes: 26 additions & 24 deletions packages/transport/src/transports/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,32 +230,34 @@ export class BridgeTransport extends AbstractTransport {
name,
data,
protocol: customProtocol,
scheduleActionParams,
}: AbstractTransportMethodParams<'call'>) {
return this.scheduleAction(async signal => {
const protocol = customProtocol || bridgeProtocol;
const bytes = buildMessage({
messages: this.messages,
name,
data,
encode: protocol.encode,
});
const response = await this.post(`/call`, {
params: session,
body: bytes.toString('hex'),
signal,
});
if (!response.success) {
return response;
}
const message = await receiveAndParse(
this.messages,
() => Promise.resolve(Buffer.from(response.payload, 'hex')),
protocol,
);
return this.scheduleAction(
async signal => {
const protocol = customProtocol || bridgeProtocol;
const bytes = buildMessage({
messages: this.messages,
name,
data,
encode: protocol.encode,
});
const response = await this.post(`/call`, {
params: session,
body: bytes.toString('hex'),
signal,
});
if (!response.success) {
return response;
}
const message = await receiveAndParse(
this.messages,
() => Promise.resolve(Buffer.from(response.payload, 'hex')),
protocol,
);

return this.success(message);
}, scheduleActionParams);
return this.success(message);
},
{ timeout: undefined },
);
}

public send({ session, name, data, protocol }: AbstractTransportMethodParams<'send'>) {
Expand Down

0 comments on commit 4de6cbe

Please sign in to comment.