Skip to content

Commit

Permalink
Expose the main HTTP request response.
Browse files Browse the repository at this point in the history
  • Loading branch information
westy92 committed May 10, 2021
1 parent 93bd4d7 commit c24d2ef
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
10 changes: 9 additions & 1 deletion src/CreateOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,19 @@ export interface CreateOptions {
*/
_mainRequestId?: string;

/**
* A private variable to store the main page navigation response.
*
* @type {Protocol.Network.Response}
* @memberof CreateOptions
*/
_mainRequestResponse?: Protocol.Network.Response;

/**
* A private flag to signify that generation failed or timed out.
*
* @type {Error}
* @memberof CreateOptions
*/
_exitCondition?: Error;
_exitCondition?: Error;
}
10 changes: 9 additions & 1 deletion src/CreateResult.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

import Protocol from 'devtools-protocol';
import * as fs from 'fs';
import { Readable, Stream } from 'stream';

Expand Down Expand Up @@ -39,14 +40,21 @@ export class CreateResult {
*/
private data: string;

/**
* The main page network response, if any.
*/
readonly response?: Protocol.Network.Response;

/**
* Creates an instance of CreateResult.
* @param {string} data base64 PDF data
* @param {Protocol.Network.Response} response the main page network response, if any.
*
* @memberof CreateResult
*/
public constructor(data: string) {
public constructor(data: string, response?: Protocol.Network.Response) {
this.data = data;
this.response = response;
}

/**
Expand Down
25 changes: 19 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ export { CompletionTrigger, CreateOptions, CreateResult };
* @returns {Promise<CreateResult>} the generated PDF data.
*/
export async function create(html: string, options?: CreateOptions): Promise<CreateResult> {
const myOptions = Object.assign({}, options);
// make sure these aren't set externally
delete myOptions._exitCondition;
delete myOptions._mainRequestId;
let chrome: LaunchedChrome;
const myOptions = normalizeCreateOptions(options);

let chrome: LaunchedChrome;
if (!myOptions.host && !myOptions.port) {
chrome = await launchChrome(myOptions);
}
Expand Down Expand Up @@ -98,7 +95,7 @@ async function generate(html: string, options: CreateOptions, tab: any): Promise
// https://chromedevtools.github.io/debugger-protocol-viewer/tot/Page/#method-printToPDF
const pdf = await Page.printToPDF(options.printOptions);
await throwIfExitCondition(options);
return new CreateResult(pdf.data);
return new CreateResult(pdf.data, options._mainRequestResponse);
} finally {
client.close();
}
Expand Down Expand Up @@ -143,6 +140,11 @@ async function beforeNavigate(options: CreateOptions, client: any): Promise<void
options._exitCondition = new Error('HtmlPdf.create() page navigate failed.');
}
});
Network.responseReceived((e: Protocol.Network.ResponseReceivedEvent) => {
if (e.requestId === options._mainRequestId) {
options._mainRequestResponse = e.response;
}
});
if (options.extraHTTPHeaders) {
Network.setExtraHTTPHeaders({headers: options.extraHTTPHeaders});
}
Expand Down Expand Up @@ -188,6 +190,17 @@ async function throwIfExitCondition(options: CreateOptions): Promise<void> {
}
}

function normalizeCreateOptions(options: CreateOptions): CreateOptions {
const myOptions = Object.assign({}, options); // clone

// make sure these aren't set externally
delete myOptions._exitCondition;
delete myOptions._mainRequestId;
delete myOptions._mainRequestResponse;

return myOptions;
}

/**
* Launches Chrome with the specified options.
*
Expand Down
17 changes: 15 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('HtmlPdf', () => {
};

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

it('should generate without a response object if not using a path', async () => {
const result = await HtmlPdf.create('<p>hello!</p>', { port });
expect(result).to.be.an.instanceOf(HtmlPdf.CreateResult);
expect(result.response).to.be.undefined;
});

it('should generate with a response object if using a path', async () => {
const url = 'https://www.google.com/';
const result = await HtmlPdf.create(url, { port });
expect(result.response.url).to.equal(url);
expect(result.response.status).to.equal(200);
});

it('should generate a PDF with cookies', async () => {
const options: HtmlPdf.CreateOptions = {
port,
Expand Down Expand Up @@ -253,7 +266,7 @@ describe('HtmlPdf', () => {
const html = `
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.slim.min.js"></script>
</head>
<body>
<div id="test">Failed!</div>
Expand Down

0 comments on commit c24d2ef

Please sign in to comment.