Skip to content

Commit 25dbacb

Browse files
authored
[js][bidi] add Network module events (#12197)
* [js][bidi] add Network module events * [js][bidi] insert await keyword
1 parent ebb232f commit 25dbacb

File tree

9 files changed

+761
-0
lines changed

9 files changed

+761
-0
lines changed

common/src/web/bidi/emptyPage.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!DOCTYPE html>
2+
<html></html>

common/src/web/bidi/emptyText.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
empty

common/src/web/bidi/redirected.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!DOCTYPE html>
2+
<html>redirected</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!DOCTYPE html>
2+
<head>
3+
<meta http-equiv="refresh" content="0;redirected.html" />
4+
</head>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
const { BeforeRequestSent, ResponseStarted } = require('./networkTypes')
19+
20+
class NetworkInspector {
21+
constructor(driver, browsingContextIds) {
22+
this._driver = driver
23+
this._browsingContextIds = browsingContextIds
24+
}
25+
26+
async init() {
27+
this.bidi = await this._driver.getBidi()
28+
}
29+
30+
async beforeRequestSent(callback) {
31+
await this.subscribeAndHandleEvent('network.beforeRequestSent', callback)
32+
}
33+
34+
async responseStarted(callback) {
35+
await this.subscribeAndHandleEvent('network.responseStarted', callback)
36+
}
37+
38+
async responseCompleted(callback) {
39+
await this.subscribeAndHandleEvent('network.responseCompleted', callback)
40+
}
41+
42+
async subscribeAndHandleEvent(eventType, callback) {
43+
if (this._browsingContextIds != null) {
44+
await this.bidi.subscribe(eventType, this._browsingContextIds)
45+
} else {
46+
await this.bidi.subscribe(eventType)
47+
}
48+
await this._on(callback)
49+
}
50+
51+
async _on(callback) {
52+
this.ws = await this.bidi.socket
53+
this.ws.on('message', (event) => {
54+
const { params } = JSON.parse(Buffer.from(event.toString()))
55+
if (params) {
56+
let response = null
57+
if ('initiator' in params) {
58+
response = new BeforeRequestSent(
59+
params.context,
60+
params.navigation,
61+
params.redirectCount,
62+
params.request,
63+
params.timestamp,
64+
params.initiator
65+
)
66+
} else if ('response' in params) {
67+
response = new ResponseStarted(
68+
params.context,
69+
params.navigation,
70+
params.redirectCount,
71+
params.request,
72+
params.timestamp,
73+
params.response
74+
)
75+
}
76+
callback(response)
77+
}
78+
})
79+
}
80+
}
81+
82+
async function getNetworkInspectorInstance(driver, browsingContextIds = null) {
83+
let instance = new NetworkInspector(driver, browsingContextIds)
84+
await instance.init()
85+
return instance
86+
}
87+
88+
module.exports = getNetworkInspectorInstance

0 commit comments

Comments
 (0)