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

Feat/network #282

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
28 changes: 17 additions & 11 deletions front_end/ndb.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
{
"modules" : [
{ "name": "ndb_sdk", "type": "autostart" },
{ "name": "ndb", "type": "autostart" },
{ "name": "layer_viewer" },
{ "name": "timeline_model" },
{ "name": "timeline" },
{ "name": "product_registry" },
{ "name": "mobile_throttling" },
{ "name": "ndb_ui" },
{ "name": "xterm" }
],
"modules": [
{ "name": "ndb_sdk", "type": "autostart" },
{ "name": "ndb", "type": "autostart" },
{ "name": "layer_viewer" },
{ "name": "timeline_model" },
{ "name": "timeline" },
{ "name": "product_registry" },
{ "name": "mobile_throttling" },
{ "name": "ndb_ui" },
{ "name": "xterm" },
{ "name": "emulation", "type": "autostart" },
{ "name": "inspector_main", "type": "autostart" },
{ "name": "mobile_throttling", "type": "autostart" },
{ "name": "cookie_table" },
{ "name": "har_importer" },
{ "name": "network" }
],
"extends": "shell",
"has_html": true
}
40 changes: 40 additions & 0 deletions front_end/ndb/Connection.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
Ndb.ConnectionInterceptor = class {
constructor() {
this._onMessage = null;
}

/**
* @param {string} message
* @return {boolean}
*/
sendRawMessage(message) {
throw new Error('Not implemented');
}

setOnMessage(onMessage) {
this._onMessage = onMessage;
}

dispatchMessage(message) {
if (this._onMessage)
this._onMessage(message);
}

disconnect() {
}
};

Ndb.Connection = class {
constructor(channel) {
this._onMessage = null;
this._onDisconnect = null;
this._channel = channel;
this._interceptors = [];
}

static async create(channel) {
Expand All @@ -16,6 +43,8 @@ Ndb.Connection = class {
*/
setOnMessage(onMessage) {
this._onMessage = onMessage;
for (const interceptor of this._interceptors)
interceptor.setOnMessage(this._onMessage);
}

/**
Expand All @@ -29,14 +58,25 @@ Ndb.Connection = class {
* @param {string} message
*/
sendRawMessage(message) {
for (const interceptor of this._interceptors) {
if (interceptor.sendRawMessage(message))
return;
}
this._channel.send(message);
}

addInterceptor(interceptor) {
this._interceptors.push(interceptor);
interceptor.setOnMessage(this._onMessage);
}

/**
* @return {!Promise}
*/
disconnect() {
this._channel.close();
for (const interceptor of this._interceptors)
interceptor.disconnect();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion front_end/ndb/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ Ndb.FileSystem = class extends Persistence.PlatformFileSystem {
*/
getMetadata(path) {
// This method should never be called as long as we are matching using file urls.
throw new Error('not implemented');
// throw new Error('not implemented');
return Promise.resolve('');
}

/**
Expand Down
51 changes: 50 additions & 1 deletion front_end/ndb/InspectorFrontendHostOverrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

(function(){
(async function(){
InspectorFrontendHost.getPreferences = async function(callback) {
[Ndb.backend] = await carlo.loadParams();
const prefs = {
Expand All @@ -30,4 +30,53 @@
callback({statusCode: 404});
}
};

// InspectorFrontendHost.sendMessageToBackend = rawMessage => {
// const parsedMes = JSON.parse(rawMessage);
// if (parsedMes.method !== 'Network.getResponseBody')
// return;
//
// const mes = await target.runtimeAgent().invoke_evaluate({
// expression: `process._sendMessage(${JSON.stringify(JSON.parse(rawMessage))})`,
// awaitPromise: true
// });
//
// if (!mes.result) return;
// try {
// const [id, result] = mes.result.value;
// if (result) {
// InspectorFrontendHost.events.dispatchEventToListeners(
// InspectorFrontendHostAPI.Events.DispatchMessage,
// {
// id,
// result
// }
// );
// }
// } catch (err) {
// console.log(err);
// }
// };
//
// 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
// }));
// }
// }
// }

})();
10 changes: 10 additions & 0 deletions front_end/ndb/NdbMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,17 @@ Ndb.NodeProcessManager = class extends Common.Object {
const target = this._targetManager.createTarget(
info.id, userFriendlyName(info), SDK.Target.Type.Node,
this._targetManager.targetById(info.ppid) || this._targetManager.mainTarget(), undefined, false, connection);

target.runtimeAgent().invoke_evaluate({
expression: await Ndb.backend.httpMonkeyPatchingSource(),
includeCommandLineAPI: true
});

const networkInterceptor = new Ndb.NetworkInterceptor();
connection.addInterceptor(networkInterceptor);
networkInterceptor.setTarget(target);
target[NdbSdk.connectionSymbol] = connection;

await this.addFileSystem(info.cwd, info.scriptName);
if (info.scriptName) {
const scriptURL = Common.ParsedURL.platformPathToURL(info.scriptName);
Expand Down
99 changes: 99 additions & 0 deletions front_end/ndb/NetworkInterceptor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
Ndb.NetworkInterceptor = class extends Ndb.ConnectionInterceptor {
constructor() {
super();
this._buffer = [];
this._cacheRequests = [];
}

setTarget(target) {
this._target = target;
for (const message of this._buffer.splice(0))
this._sendRawMessage(message);
this._listen();
}

sendRawMessage(message) {
const parsed = JSON.parse(message);
if (parsed.method.startsWith('Network.')) {
this._sendRawMessage(message);
return true;
}
return false;
}

setOnMessage(onMessage) {
this._onMessage = onMessage;
}

dispatchMessage(message) {
if (this._onMessage) this._onMessage(message);
}

disconnect() {
this._target = null;
}

_sendRawMessage(rawMessage) {}

async _listen() {
InspectorFrontendHost.sendMessageToBackend = rawMessage => {
const message = JSON.parse(rawMessage);

const request = this._cacheRequests.filter(res => {
if (
res.type === 'Network.getResponseBody' &&
res.payload.requestId === message.params.requestId
)
return res;
})[0];

if (request) {
InspectorFrontendHost.events.dispatchEventToListeners(
InspectorFrontendHostAPI.Events.DispatchMessage,
{
id: message.id,
result: {
base64Encoded: true,
body: request.payload.data
}
}
);
}
};

while (this._target) {
const rawResponse = await this._target
.runtimeAgent()
.invoke_evaluate({
expression: `process._fetchNetworkMessages()`,
awaitPromise: true,
returnByValue: true
});

if (!rawResponse || !rawResponse.result) return;

const {
result: { value: messages }
} = rawResponse;

if (!messages) return;

// messages is array-like
const messagesArr = Array.from(JSON.parse(messages));

for (const message of messagesArr) {
const { type, payload } = message;
this._cacheRequests.push(message);

// this is on the way back, this way doesn't work
if (type !== 'Network.getResponseBody') {
// but this does
SDK._mainConnection._onMessage(JSON.stringify({
method: type,
params: payload
}));
}
}
}
}
};
1 change: 1 addition & 0 deletions front_end/ndb/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"scripts": [
"InspectorFrontendHostOverrides.js",
"Connection.js",
"NetworkInterceptor.js",
"FileSystem.js",
"NdbMain.js"
]
Expand Down
6 changes: 6 additions & 0 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class Backend {
opn(url);
}

async httpMonkeyPatchingSource() {
const pathToHttpPatch = path.resolve(__dirname, '..', './lib/preload/ndb/httpMonkeyPatching.js');
const content = await fsReadFile(pathToHttpPatch, 'utf8');
return content;
}

pkg() {
// TODO(ak239spb): implement it as decorations over package.json file.
try {
Expand Down
Loading