Skip to content

Commit

Permalink
feat(page): move page.pdf to protocol streams (#4587)
Browse files Browse the repository at this point in the history
This lets transferring massive PDF files over the protocol.

Fix #4563
  • Loading branch information
aslushnikov committed Jun 15, 2019
1 parent 6c2007f commit 955e7cb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
6 changes: 2 additions & 4 deletions lib/Page.js
Expand Up @@ -924,6 +924,7 @@ class Page extends EventEmitter {
const marginRight = convertPrintParameterToInches(margin.right) || 0;

const result = await this._client.send('Page.printToPDF', {
transferMode: 'ReturnAsStream',
landscape,
displayHeaderFooter,
headerTemplate,
Expand All @@ -939,10 +940,7 @@ class Page extends EventEmitter {
pageRanges,
preferCSSPageSize
});
const buffer = Buffer.from(result.data, 'base64');
if (path !== null)
await writeFileAsync(path, buffer);
return buffer;
return await helper.readProtocolStream(this._client, result.stream, path);
}

/**
Expand Down
35 changes: 1 addition & 34 deletions lib/Tracing.js
Expand Up @@ -14,11 +14,6 @@
* limitations under the License.
*/
const {helper, assert} = require('./helper');
const fs = require('fs');

const openAsync = helper.promisify(fs.open);
const writeAsync = helper.promisify(fs.write);
const closeAsync = helper.promisify(fs.close);

class Tracing {
/**
Expand Down Expand Up @@ -66,40 +61,12 @@ class Tracing {
let fulfill;
const contentPromise = new Promise(x => fulfill = x);
this._client.once('Tracing.tracingComplete', event => {
this._readStream(event.stream, this._path).then(fulfill);
helper.readProtocolStream(this._client, event.stream, this._path).then(fulfill);
});
await this._client.send('Tracing.end');
this._recording = false;
return contentPromise;
}

/**
* @param {string} handle
* @param {?string} path
*/
async _readStream(handle, path) {
let eof = false;
let file;
if (path)
file = await openAsync(path, 'w');
const bufs = [];
while (!eof) {
const response = await this._client.send('IO.read', {handle});
eof = response.eof;
bufs.push(Buffer.from(response.data));
if (path)
await writeAsync(file, response.data);
}
if (path)
await closeAsync(file);
await this._client.send('IO.close', {handle});
let resultBuffer = null;
try {
resultBuffer = Buffer.concat(bufs);
} finally {
return resultBuffer;
}
}
}

module.exports = Tracing;
37 changes: 36 additions & 1 deletion lib/helper.js
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/
const {TimeoutError} = require('./Errors');

const debugError = require('debug')(`puppeteer:error`);
const fs = require('fs');

class Helper {
/**
Expand Down Expand Up @@ -221,8 +221,43 @@ class Helper {
clearTimeout(timeoutTimer);
}
}

/**
* @param {!Puppeteer.CDPSession} client
* @param {string} handle
* @param {?string} path
* @return {!Promise<!Buffer>}
*/
static async readProtocolStream(client, handle, path) {
let eof = false;
let file;
if (path)
file = await openAsync(path, 'w');
const bufs = [];
while (!eof) {
const response = await client.send('IO.read', {handle});
eof = response.eof;
const buf = Buffer.from(response.data, response.base64Encoded ? 'base64' : undefined);
bufs.push(buf);
if (path)
await writeAsync(file, buf);
}
if (path)
await closeAsync(file);
await client.send('IO.close', {handle});
let resultBuffer = null;
try {
resultBuffer = Buffer.concat(bufs);
} finally {
return resultBuffer;
}
}
}

const openAsync = Helper.promisify(fs.open);
const writeAsync = Helper.promisify(fs.write);
const closeAsync = Helper.promisify(fs.close);

/**
* @param {*} value
* @param {string=} message
Expand Down

0 comments on commit 955e7cb

Please sign in to comment.