Skip to content

Commit

Permalink
Fix open() failure case to not hang the proxy forever
Browse files Browse the repository at this point in the history
  • Loading branch information
gre committed Oct 26, 2018
1 parent c744805 commit 95f3e31
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
41 changes: 33 additions & 8 deletions packages/hw-http-proxy-devserver/src/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,55 @@ app.post("/", bodyParser.json(), async (req, res) => {
});

let wsIndex = 0;
let transportP = null;
wss.on("connection", ws => {
const index = ++wsIndex;
try {
let transportP;
let transport;
let destroyed = false;

ws.on("close", () => {
destroyed = true;
if (transportP) {
transportP.then(transport => transport.close());
transportP.then(
transport => {
transportP = null;
transport.close();
},
() => {}
);
console.log(`WS(${index}): close`);
}
});

ws.on("message", async apduHex => {
if (apduHex === "open") {
console.log(`WS(${index}): opening...`);
transportP = TransportNodeHid.create(5000);
if (destroyed) return;
transport = await transportP;
transport.on("disconnect", () => ws.close());
console.log(`WS(${index}): opened!`);
ws.send(JSON.stringify({ type: "opened" }));
try {
if (transportP) {
const p = transportP;
await transportP.then(
t => {
if (p !== transportP) return;
transportP = null;
console.warn(
"/!\\ /!\\ /!\\ warning: you didn't transport.close() the previous transport. force closing it! /!\\ /!\\ /!\\"
);
return t.close();
},
() => {}
);
}
transportP = TransportNodeHid.create(2000);
if (destroyed) return;
transport = await transportP;
transport.on("disconnect", () => ws.close());
console.log(`WS(${index}): opened!`);
ws.send(JSON.stringify({ type: "opened" }));
} catch (e) {
console.log(`WS(${index}): open failed! ${e}`);
ws.close();
}
return;
}
if (!transport) {
Expand Down
1 change: 1 addition & 0 deletions packages/hw-transport-http/src/WebSocketTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default class WebSocketTransport extends Transport<string> {
};
socket.onclose = () => {
exchangeMethods.onDisconnect();
reject(new TransportError("OpenFailed", "OpenFailed"));
};
socket.onmessage = e => {
if (typeof e.data !== "string") return;
Expand Down

0 comments on commit 95f3e31

Please sign in to comment.