Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
khanghoang committed Jul 15, 2019
1 parent be3c73c commit 9b51ded
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
20 changes: 20 additions & 0 deletions front_end/ndb/InspectorFrontendHostOverrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,25 @@
}
};

while (true) {
const message = await target.runtimeAgent().invoke_evaluate({
expression: 'process._getNetworkMessages()',
awaitPromise: true
});

if (!message.result) return;
const arrMessages = JSON.parse(message.result.value);

for (const mes of arrMessages) {
const { type, payload } = mes;

if (type) {
SDK._mainConnection._onMessage(JSON.stringify({
method: type,
params: payload
}));
}
}
}

})();
24 changes: 5 additions & 19 deletions lib/preload/ndb/httpMonkeyPatching.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const getId = () => id++;
let messages = [];
let messageAdded = null;

// this function is our instrumentation, to report anything to frontend - call it instead of process.send
function reportMessage(message) {
messages.push(message);
if (messageAdded) {
Expand All @@ -52,7 +51,6 @@ function reportMessage(message) {
}
}

// this function should be called from frontend in the loop using `target.runtimeAgent().invoke_evaluate`
process._getNetworkMessages = async function() {
if (!messages.length)
await new Promise(resolve => messageAdded = resolve);
Expand All @@ -61,28 +59,18 @@ process._getNetworkMessages = async function() {

process._sendMessage = async function(rawMessage) {
return new Promise(resolve => {
setTimeout(() => {
console.log({ rawMessage });
const message = rawMessage;
console.log({ message });
// send message to frontend directly
// (eg: getResponseBody)
console.log({ cacheRequests });
console.log({ foo: cacheRequests[message.params.requestId] });

if (!cacheRequests[message.params.requestId]) {
resolve(JSON.stringify({}));
return;
}

const message = rawMessage;
if (!cacheRequests[message.params.requestId]) {
resolve(JSON.stringify({}));
} else {
if (message.method === 'Network.getResponseBody') {
const { base64Encoded, data } = cacheRequests[message.params.requestId];

console.log({ cacheRequests });
console.log({ data });
resolve(JSON.stringify([message.id, { base64Encoded, body: data }]));
}
}, 0);
}
});
};

Expand Down Expand Up @@ -127,8 +115,6 @@ const callbackWrapper = (callback, req) => res => {
base64Encoded: true
};

console.log({ bar: cacheRequests[res.req.__requestId] });

const payload = {
id: res.req.__requestId,
requestId: res.req.__requestId,
Expand Down

0 comments on commit 9b51ded

Please sign in to comment.