diff --git a/lib/Page.js b/lib/Page.js index 461f304246603..80d960dc3b043 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -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, @@ -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); } /** diff --git a/lib/Tracing.js b/lib/Tracing.js index b76674fcd3e3b..fa2276e60b254 100644 --- a/lib/Tracing.js +++ b/lib/Tracing.js @@ -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 { /** @@ -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; diff --git a/lib/helper.js b/lib/helper.js index 3c1581338a0cb..e12d63dbeceed 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -14,8 +14,8 @@ * limitations under the License. */ const {TimeoutError} = require('./Errors'); - const debugError = require('debug')(`puppeteer:error`); +const fs = require('fs'); class Helper { /** @@ -221,8 +221,43 @@ class Helper { clearTimeout(timeoutTimer); } } + + /** + * @param {!Puppeteer.CDPSession} client + * @param {string} handle + * @param {?string} path + * @return {!Promise} + */ + 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