Skip to content

Commit a271145

Browse files
jschfflratersolis
andauthored
feat: add initiator to HTTPRequest (#7614)
Co-Authored-By: atersolis <atersolis@atersolis.net>
1 parent eebf452 commit a271145

5 files changed

Lines changed: 70 additions & 0 deletions

File tree

docs/api.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@
341341
* [httpRequest.finalizeInterceptions()](#httprequestfinalizeinterceptions)
342342
* [httpRequest.frame()](#httprequestframe)
343343
* [httpRequest.headers()](#httprequestheaders)
344+
* [httpRequest.initiator()](#httprequestinitiator)
344345
* [httpRequest.isNavigationRequest()](#httprequestisnavigationrequest)
345346
* [httpRequest.method()](#httprequestmethod)
346347
* [httpRequest.postData()](#httprequestpostdata)
@@ -4792,6 +4793,14 @@ When in Cooperative Mode, awaits pending interception handlers and then decides
47924793

47934794
- returns: <[Object]> An object with HTTP headers associated with the request. All header names are lower-case.
47944795

4796+
#### httpRequest.initiator()
4797+
4798+
- returns: <[Object]> An object describing the initiator of the request
4799+
- `type` <[string]> Type of this initiator. Possible values: `parser`, `script`, `preload`, `SignedExchange` and `other`.
4800+
- `stack` <?[Object]> JavaScript stack trace for the initiator, set for `script` only.
4801+
- `url` <?[string]> Initiator URL, set for `parser`, `script` and `SignedExchange` type.
4802+
- `lineNumber` <?[number]> 0 based initiator line number, set for `parser` and `script`.
4803+
47954804
#### httpRequest.isNavigationRequest()
47964805

47974806
- returns: <[boolean]>

src/common/HTTPRequest.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export class HTTPRequest {
130130
private _currentStrategy: InterceptResolutionStrategy;
131131
private _currentPriority: number | undefined;
132132
private _interceptActions: Array<() => void | PromiseLike<any>>;
133+
private _initiator: Protocol.Network.Initiator;
133134

134135
/**
135136
* @internal
@@ -158,6 +159,7 @@ export class HTTPRequest {
158159
this._currentStrategy = 'none';
159160
this._currentPriority = undefined;
160161
this._interceptActions = [];
162+
this._initiator = event.initiator;
161163

162164
for (const key of Object.keys(event.request.headers))
163165
this._headers[key.toLowerCase()] = event.request.headers[key];
@@ -298,6 +300,13 @@ export class HTTPRequest {
298300
return this._isNavigationRequest;
299301
}
300302

303+
/**
304+
* @returns the initiator of the request.
305+
*/
306+
initiator(): Protocol.Network.Initiator {
307+
return this._initiator;
308+
}
309+
301310
/**
302311
* A `redirectChain` is a chain of requests initiated to fetch a resource.
303312
* @remarks

test/assets/initiator.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<iframe src="./frames/frame.html"></iframe>
2+
<script src="./initiator.js"></script>

test/assets/initiator.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const script = document.createElement('script');
2+
script.src = './injectedfile.js';
3+
document.body.appendChild(script);
4+
5+
const style = document.createElement('link');
6+
style.rel = 'stylesheet';
7+
style.href = './injectedstyle.css';
8+
document.head.appendChild(style);

test/network.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,48 @@ describe('network', function () {
137137
});
138138
});
139139

140+
describeFailsFirefox('Request.initiator', () => {
141+
it('shoud return the initiator', async () => {
142+
const { page, server } = getTestState();
143+
144+
const initiators = new Map();
145+
page.on('request', (request) =>
146+
initiators.set(request.url().split('/').pop(), request.initiator())
147+
);
148+
await page.goto(server.PREFIX + '/initiator.html');
149+
150+
expect(initiators.get('initiator.html').type).toBe('other');
151+
expect(initiators.get('initiator.js').type).toBe('parser');
152+
expect(initiators.get('initiator.js').url).toBe(
153+
server.PREFIX + '/initiator.html'
154+
);
155+
expect(initiators.get('frame.html').type).toBe('parser');
156+
expect(initiators.get('frame.html').url).toBe(
157+
server.PREFIX + '/initiator.html'
158+
);
159+
expect(initiators.get('script.js').type).toBe('parser');
160+
expect(initiators.get('script.js').url).toBe(
161+
server.PREFIX + '/frames/frame.html'
162+
);
163+
expect(initiators.get('style.css').type).toBe('parser');
164+
expect(initiators.get('style.css').url).toBe(
165+
server.PREFIX + '/frames/frame.html'
166+
);
167+
expect(initiators.get('initiator.js').type).toBe('parser');
168+
expect(initiators.get('injectedfile.js').type).toBe('script');
169+
expect(initiators.get('injectedfile.js').stack.callFrames[0].url).toBe(
170+
server.PREFIX + '/initiator.js'
171+
);
172+
expect(initiators.get('injectedstyle.css').type).toBe('script');
173+
expect(initiators.get('injectedstyle.css').stack.callFrames[0].url).toBe(
174+
server.PREFIX + '/initiator.js'
175+
);
176+
expect(initiators.get('initiator.js').url).toBe(
177+
server.PREFIX + '/initiator.html'
178+
);
179+
});
180+
});
181+
140182
describeFailsFirefox('Response.fromCache', function () {
141183
it('should return |false| for non-cached content', async () => {
142184
const { page, server } = getTestState();

0 commit comments

Comments
 (0)