Skip to content

Commit

Permalink
change monkeypatch to handle errors and be more consistent in regard …
Browse files Browse the repository at this point in the history
…to race conditions
  • Loading branch information
aewering committed Feb 19, 2024
1 parent a86fb02 commit 13c8ed2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 134 deletions.
117 changes: 0 additions & 117 deletions src/DevTools/monkeypatch.js

This file was deleted.

67 changes: 50 additions & 17 deletions src/Generator/DevToolsWorker.elm
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,13 @@ const workerDecoder = (workerRequest) =>
worker.ports.inc.send(workerRequest);
});
let response;
let responses = new Map();
const client = {
client_: {
rpcCall: (method, req, metadata, info, callback) => {
// first argument is error
callback(null, response);
callback(null, responses.get(method));
responses.delete(method);
},
},
};
Expand All @@ -239,37 +240,69 @@ const bufferToArr = (buffer) => {
return arr;
};
const grpcStati = {
1: "Cancelled",
2: "Unknown",
3: "InvalidArgument",
4: "DeadlineExceeded",
5: "NotFound",
6: "AlreadyExists",
7: "PermissionDenied",
8: "ResourceExhausted",
9: "FailedPrecondition",
10: "Aborted",
11: "OutOfRange",
12: "Unimplemented",
13: "Internal",
14: "Unavailable",
15: "DataLoss",
16: "Unauthenticated",
};
function applyMonkeypatch(decoder) {
const ORIGINAL = XMLHttpRequest;
window.XMLHttpRequest = function () {
let serviceAndMethod;
let reqBody;
const original = new ORIGINAL();
original.addEventListener("loadend", async () => {
if (
original.getResponseHeader("content-type") ===
original.getResponseHeader("content-type") !==
"application/grpc-web+proto"
) {
const reqAsJson = await decoder({
url: serviceAndMethod,
isRequest: true,
bytes: bufferToArr(reqBody.buffer),
});
const resAsJson = await decoder({
return;
}
const reqAsJson = await decoder({
url: serviceAndMethod,
isRequest: true,
bytes: bufferToArr(reqBody.buffer),
});
const responseStatus = original.getResponseHeader("grpc-status");
let resAsJson;
if (responseStatus && responseStatus !== "0") {
// error!
const errorMessage = original.getResponseHeader("grpc-message");
resAsJson = {
error: grpcStati[responseStatus],
message: decodeURIComponent(errorMessage),
};
} else {
resAsJson = await decoder({
url: serviceAndMethod,
isRequest: false,
bytes: bufferToArr(original.response),
});
response = wrapToObject(resAsJson);
client.client_.rpcCall(
serviceAndMethod,
wrapToObject(reqAsJson),
null,
null,
() => {}
);
}
responses.set(serviceAndMethod, wrapToObject(resAsJson));
client.client_.rpcCall(
serviceAndMethod,
wrapToObject(reqAsJson),
null,
null,
() => {}
);
});
const originalOpen = original.open.bind(original);
Expand Down

0 comments on commit 13c8ed2

Please sign in to comment.