Skip to content

Commit b1a3b23

Browse files
authored
api(request): make request.response a promise (#1377)
1 parent 24d4fb1 commit b1a3b23

File tree

8 files changed

+21
-29
lines changed

8 files changed

+21
-29
lines changed

docs/api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3290,7 +3290,7 @@ Contains the request's resource type as it was perceived by the rendering engine
32903290
ResourceType will be one of the following: `document`, `stylesheet`, `image`, `media`, `font`, `script`, `texttrack`, `xhr`, `fetch`, `eventsource`, `websocket`, `manifest`, `other`.
32913291

32923292
#### request.response()
3293-
- returns: <?[Response]> A matching [Response] object, or `null` if the response has not been received yet.
3293+
- returns: <[Promise]<?[Response]> A matching [Response] object, or `null` if the response was not received due to error.
32943294

32953295
#### request.url()
32963296
- returns: <[string]> URL of the request.

src/chromium/crNetworkManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export class CRNetworkManager {
215215

216216
// Under certain conditions we never get the Network.responseReceived
217217
// event from protocol. @see https://crbug.com/883475
218-
const response = request.request.response();
218+
const response = request.request._existingResponse();
219219
if (response)
220220
response._requestFinished();
221221
this._requestIdToRequest.delete(request._requestId);
@@ -230,7 +230,7 @@ export class CRNetworkManager {
230230
// @see https://crbug.com/750469
231231
if (!request)
232232
return;
233-
const response = request.request.response();
233+
const response = request.request._existingResponse();
234234
if (response)
235235
response._requestFinished();
236236
this._requestIdToRequest.delete(request._requestId);

src/firefox/ffNetworkManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class FFNetworkManager {
9090
const request = this._requests.get(event.requestId);
9191
if (!request)
9292
return;
93-
const response = request.request.response()!;
93+
const response = request.request._existingResponse()!;
9494
// Keep redirected requests in the map for future reference in redirectChain.
9595
const isRedirected = response.status() >= 300 && response.status() <= 399;
9696
if (isRedirected) {
@@ -107,7 +107,7 @@ export class FFNetworkManager {
107107
if (!request)
108108
return;
109109
this._requests.delete(request._id);
110-
const response = request.request.response();
110+
const response = request.request._existingResponse();
111111
if (response)
112112
response._requestFinished();
113113
request.request._setFailureText(event.errorCode);

src/frames.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ export class Frame {
387387

388388
disposer.dispose();
389389

390-
return request ? request._finalRequest._waitForResponse() : null;
390+
return request ? request._finalRequest.response() : null;
391391

392392
function throwIfError(error: Error|void): asserts error is void {
393393
if (!error)
@@ -430,7 +430,7 @@ export class Frame {
430430
if (error)
431431
throw error;
432432

433-
return request ? request._finalRequest._waitForResponse() : null;
433+
return request ? request._finalRequest.response() : null;
434434
}
435435

436436
async _waitForLoadState(options: types.NavigateOptions = {}): Promise<void> {

src/network.ts

+7-15
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,8 @@ export class Request {
108108
private _postData: string | null;
109109
private _headers: Headers;
110110
private _frame: frames.Frame;
111-
private _waitForResponsePromise: Promise<Response>;
112-
private _waitForResponsePromiseCallback: (value: Response) => void = () => {};
113-
private _waitForFinishedPromise: Promise<Response | null>;
114-
private _waitForFinishedPromiseCallback: (value: Response | null) => void = () => {};
111+
private _waitForResponsePromise: Promise<Response | null>;
112+
private _waitForResponsePromiseCallback: (value: Response | null) => void = () => {};
115113
private _interceptionHandled = false;
116114

117115
constructor(delegate: RequestDelegate | null, frame: frames.Frame, redirectChain: Request[], documentId: string | undefined,
@@ -130,13 +128,12 @@ export class Request {
130128
this._postData = postData;
131129
this._headers = headers;
132130
this._waitForResponsePromise = new Promise(f => this._waitForResponsePromiseCallback = f);
133-
this._waitForFinishedPromise = new Promise(f => this._waitForFinishedPromiseCallback = f);
134131
this._isFavicon = url.endsWith('/favicon.ico');
135132
}
136133

137134
_setFailureText(failureText: string) {
138135
this._failureText = failureText;
139-
this._waitForFinishedPromiseCallback(null);
136+
this._waitForResponsePromiseCallback(null);
140137
}
141138

142139
url(): string {
@@ -159,22 +156,17 @@ export class Request {
159156
return this._headers;
160157
}
161158

162-
response(): Response | null {
163-
return this._response;
164-
}
165-
166-
async _waitForFinished(): Promise<Response | null> {
167-
return this._waitForFinishedPromise;
159+
response(): Promise<Response | null> {
160+
return this._waitForResponsePromise;
168161
}
169162

170-
async _waitForResponse(): Promise<Response> {
171-
return await this._waitForResponsePromise;
163+
_existingResponse(): Response | null {
164+
return this._response;
172165
}
173166

174167
_setResponse(response: Response) {
175168
this._response = response;
176169
this._waitForResponsePromiseCallback(response);
177-
response._finishedPromise.then(() => this._waitForFinishedPromiseCallback(response));
178170
}
179171

180172
frame(): frames.Frame {

src/webkit/wkPage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ export class WKPage implements PageDelegate {
789789

790790
// Under certain conditions we never get the Network.responseReceived
791791
// event from protocol. @see https://crbug.com/883475
792-
const response = request.request.response();
792+
const response = request.request._existingResponse();
793793
if (response)
794794
response._requestFinished();
795795
this._requestIdToRequest.delete(request._requestId);
@@ -802,7 +802,7 @@ export class WKPage implements PageDelegate {
802802
// @see https://crbug.com/750469
803803
if (!request)
804804
return;
805-
const response = request.request.response();
805+
const response = request.request._existingResponse();
806806
if (response)
807807
response._requestFinished();
808808
this._requestIdToRequest.delete(request._requestId);

test/interception.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
314314
const response = await page.goto(`data:text/html,<link rel="stylesheet" href="${server.PREFIX}/fonts?helvetica|arial"/>`);
315315
expect(response).toBe(null);
316316
expect(requests.length).toBe(1);
317-
expect(requests[0].response().status()).toBe(404);
317+
expect((await requests[0].response()).status()).toBe(404);
318318
});
319319
it('should not throw "Invalid Interception Id" if the request was cancelled', async({page, server}) => {
320320
await page.setContent('<iframe></iframe>');

test/network.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ module.exports.describe = function({testRunner, expect, MAC, WIN, FFOX, CHROMIUM
132132
const response = await page.goto(server.PREFIX + '/foo.html');
133133
const redirectChain = response.request().redirectChain();
134134
expect(redirectChain.length).toBe(1);
135-
const redirected = redirectChain[0].response();
135+
const redirected = await redirectChain[0].response();
136136
expect(redirected.status()).toBe(302);
137137
let error = null;
138138
await redirected.text().catch(e => error = e);
@@ -216,7 +216,7 @@ module.exports.describe = function({testRunner, expect, MAC, WIN, FFOX, CHROMIUM
216216
expect(requests[0].url()).toBe(server.EMPTY_PAGE);
217217
expect(requests[0].resourceType()).toBe('document');
218218
expect(requests[0].method()).toBe('GET');
219-
expect(requests[0].response()).toBeTruthy();
219+
expect(await requests[0].response()).toBeTruthy();
220220
expect(requests[0].frame() === page.mainFrame()).toBe(true);
221221
expect(requests[0].frame().url()).toBe(server.EMPTY_PAGE);
222222
});
@@ -241,7 +241,7 @@ module.exports.describe = function({testRunner, expect, MAC, WIN, FFOX, CHROMIUM
241241
await page.goto(server.PREFIX + '/one-style.html');
242242
expect(failedRequests.length).toBe(1);
243243
expect(failedRequests[0].url()).toContain('one-style.css');
244-
expect(failedRequests[0].response()).toBe(null);
244+
expect(await failedRequests[0].response()).toBe(null);
245245
expect(failedRequests[0].resourceType()).toBe('stylesheet');
246246
if (CHROMIUM) {
247247
expect(failedRequests[0].failure().errorText).toBe('net::ERR_INVALID_HTTP_RESPONSE');
@@ -264,7 +264,7 @@ module.exports.describe = function({testRunner, expect, MAC, WIN, FFOX, CHROMIUM
264264
]);
265265
const request = response.request();
266266
expect(request.url()).toBe(server.EMPTY_PAGE);
267-
expect(request.response()).toBeTruthy();
267+
expect(await request.response()).toBeTruthy();
268268
expect(request.frame() === page.mainFrame()).toBe(true);
269269
expect(request.frame().url()).toBe(server.EMPTY_PAGE);
270270
});

0 commit comments

Comments
 (0)