Skip to content

Commit 6f803c4

Browse files
authored
Expose the main HTTP request response. (#299)
1 parent 93bd4d7 commit 6f803c4

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

src/CreateOptions.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,19 @@ export interface CreateOptions {
120120
*/
121121
_mainRequestId?: string;
122122

123+
/**
124+
* A private variable to store the main page navigation response.
125+
*
126+
* @type {Protocol.Network.Response}
127+
* @memberof CreateOptions
128+
*/
129+
_mainRequestResponse?: Protocol.Network.Response;
130+
123131
/**
124132
* A private flag to signify that generation failed or timed out.
125133
*
126134
* @type {Error}
127135
* @memberof CreateOptions
128136
*/
129-
_exitCondition?: Error;
137+
_exitCondition?: Error;
130138
}

src/CreateResult.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
import Protocol from 'devtools-protocol';
34
import * as fs from 'fs';
45
import { Readable, Stream } from 'stream';
56

@@ -39,14 +40,21 @@ export class CreateResult {
3940
*/
4041
private data: string;
4142

43+
/**
44+
* The main page network response, if any.
45+
*/
46+
readonly response?: Protocol.Network.Response;
47+
4248
/**
4349
* Creates an instance of CreateResult.
4450
* @param {string} data base64 PDF data
51+
* @param {Protocol.Network.Response} response the main page network response, if any.
4552
*
4653
* @memberof CreateResult
4754
*/
48-
public constructor(data: string) {
55+
public constructor(data: string, response?: Protocol.Network.Response) {
4956
this.data = data;
57+
this.response = response;
5058
}
5159

5260
/**

src/index.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ export { CompletionTrigger, CreateOptions, CreateResult };
2626
* @returns {Promise<CreateResult>} the generated PDF data.
2727
*/
2828
export async function create(html: string, options?: CreateOptions): Promise<CreateResult> {
29-
const myOptions = Object.assign({}, options);
30-
// make sure these aren't set externally
31-
delete myOptions._exitCondition;
32-
delete myOptions._mainRequestId;
33-
let chrome: LaunchedChrome;
29+
const myOptions = normalizeCreateOptions(options);
3430

31+
let chrome: LaunchedChrome;
3532
if (!myOptions.host && !myOptions.port) {
3633
chrome = await launchChrome(myOptions);
3734
}
@@ -98,7 +95,7 @@ async function generate(html: string, options: CreateOptions, tab: any): Promise
9895
// https://chromedevtools.github.io/debugger-protocol-viewer/tot/Page/#method-printToPDF
9996
const pdf = await Page.printToPDF(options.printOptions);
10097
await throwIfExitCondition(options);
101-
return new CreateResult(pdf.data);
98+
return new CreateResult(pdf.data, options._mainRequestResponse);
10299
} finally {
103100
client.close();
104101
}
@@ -143,6 +140,11 @@ async function beforeNavigate(options: CreateOptions, client: any): Promise<void
143140
options._exitCondition = new Error('HtmlPdf.create() page navigate failed.');
144141
}
145142
});
143+
Network.responseReceived((e: Protocol.Network.ResponseReceivedEvent) => {
144+
if (e.requestId === options._mainRequestId) {
145+
options._mainRequestResponse = e.response;
146+
}
147+
});
146148
if (options.extraHTTPHeaders) {
147149
Network.setExtraHTTPHeaders({headers: options.extraHTTPHeaders});
148150
}
@@ -188,6 +190,17 @@ async function throwIfExitCondition(options: CreateOptions): Promise<void> {
188190
}
189191
}
190192

193+
function normalizeCreateOptions(options: CreateOptions): CreateOptions {
194+
const myOptions = Object.assign({}, options); // clone
195+
196+
// make sure these aren't set externally
197+
delete myOptions._exitCondition;
198+
delete myOptions._mainRequestId;
199+
delete myOptions._mainRequestResponse;
200+
201+
return myOptions;
202+
}
203+
191204
/**
192205
* Launches Chrome with the specified options.
193206
*

test/index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('HtmlPdf', () => {
8080
};
8181

8282
try {
83-
await HtmlPdf.create('https://westy92.github.io/html-pdf-chrome/test/cookie.html', options);
83+
await HtmlPdf.create('<p>hello!</p>', options);
8484
expect.fail();
8585
} catch (err) {
8686
expect(err.message).to.equal('HtmlPdf.create() connection lost.');
@@ -145,6 +145,19 @@ describe('HtmlPdf', () => {
145145
expect(result).to.be.an.instanceOf(HtmlPdf.CreateResult);
146146
});
147147

148+
it('should generate without a response object if not using a path', async () => {
149+
const result = await HtmlPdf.create('<p>hello!</p>', { port });
150+
expect(result).to.be.an.instanceOf(HtmlPdf.CreateResult);
151+
expect(result.response).to.be.undefined;
152+
});
153+
154+
it('should generate with a response object if using a path', async () => {
155+
const url = 'https://www.google.com/';
156+
const result = await HtmlPdf.create(url, { port });
157+
expect(result.response.url).to.equal(url);
158+
expect(result.response.status).to.equal(200);
159+
});
160+
148161
it('should generate a PDF with cookies', async () => {
149162
const options: HtmlPdf.CreateOptions = {
150163
port,
@@ -253,7 +266,7 @@ describe('HtmlPdf', () => {
253266
const html = `
254267
<html>
255268
<head>
256-
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
269+
<script src="https://code.jquery.com/jquery-3.6.0.slim.min.js"></script>
257270
</head>
258271
<body>
259272
<div id="test">Failed!</div>

0 commit comments

Comments
 (0)