Skip to content

Commit

Permalink
Merge pull request #18 from Kikobeats/tmp
Browse files Browse the repository at this point in the history
Use lightweight temporal file dependency
  • Loading branch information
Kikobeats committed Sep 7, 2018
2 parents 4dfa097 + e296c75 commit e33c78c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 56 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ const browserless = require('browserless')()

browserless
.screenshot('http://example.com', { device: 'iPhone 6' })
.then(tmpStream => {
console.log(`your screenshot at ${tmpStream.path}`)
tmpStream.cleanupSync()
.then(tmpFile => {
console.log(`your screenshot at ${tmpFile.path}`)
tmpFile.cleanupSync()
})
```

Expand Down Expand Up @@ -187,19 +187,19 @@ const browserless = require('browserless')

;(async () => {
const url = 'https://example.com'
const tmpStream = await browserless.pdf(url, {
const tmpFile = await browserless.pdf(url, {
tmpOpts: {
path: './',
name: `${url.hostname}.${Date.now()}`
}
})

console.log(`PDF generated at '${tmpStream.path}'`)
tmpStream.cleanupSync() // It removes the file!
console.log(`PDF generated at '${tmpFile.path}'`)
tmpFile.cleanupSync() // It removes the file!
})()
```

It returns an [tmpStream](https://github.com/Kikobeats/create-temp-file2#create-temp-file2), with `path` where the temporal file live and `cleanup`/`cleanupSync` methods for clean the temporal file.
It returns an [tmpFile](https://github.com/Kikobeats/create-temp-file2#create-temp-file2), with `path` where the temporal file live and `cleanup`/`cleanupSync` methods for clean the temporal file.

#### options

Expand All @@ -209,7 +209,7 @@ Additionally, you can setup:

##### tmpOptions

See [createTempFile#options](https://github.com/Kikobeats/create-temp-file2#createtempfileoptions).
See [tempy#api](https://github.com/sindresorhus/tempy#api)..

##### media

Expand All @@ -236,19 +236,19 @@ const browserless = require('browserless')

;(async () => {
const url = 'https://example.com'
const tmpStream = await browserless.screenshot(url, {
const tmpFile = await browserless.screenshot(url, {
tmpOpts: {
path: './',
name: `${url.hostname}.${Date.now()}`
}
})

console.log(`Screenshot taken at '${tmpStream.path}'`)
tmpStream.cleanupSync() // It removes the file!
console.log(`Screenshot taken at '${tmpFile.path}'`)
tmpFile.cleanupSync() // It removes the file!
})()
```

It returns an [tmpStream](https://github.com/Kikobeats/create-temp-file2#create-temp-file2), with `path` where the temporal file live and `cleanup`/`cleanupSync` methods for clean the temporal file.
It returns a temporary file path with `cleanup`/`cleanupSync` methods for easily clean it.

#### options

Expand All @@ -258,7 +258,7 @@ Additionally, you can setup:

##### tmpOptions

See [createTempFile#options](https://github.com/Kikobeats/create-temp-file2#createtempfileoptions).
See [tempy#api](https://github.com/sindresorhus/tempy#api).

The `options` provided are passed to [page.pdf](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions).

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
"text"
],
"dependencies": {
"create-temp-file2": "~2.0.0",
"debug": "~3.1.0",
"extract-domain": "~2.0.3",
"generic-pool": "~3.4.2",
"is-tracking-domain": "~1.1.2"
"is-tracking-domain": "~1.1.2",
"tempy": "~0.2.1"
},
"devDependencies": {
"asciichart": "latest",
Expand Down
59 changes: 33 additions & 26 deletions src/browserless.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
'use strict'

const createTempFile = require('create-temp-file2')
const extractDomain = require('extract-domain')
const puppeteer = require('puppeteer')
const debug = require('debug')('browserless')
const puppeteer = require('puppeteer')
const tempy = require('tempy')

const { getDevice } = require('./devices')
const isTracker = require('./is-tracker')

const { promisify } = require('util')
const fs = require('fs')

const unlink = promisify(fs.unlink)
const unlinkSync = promisify(fs.unlinkSync)

const WAIT_UNTIL = ['networkidle2', 'load', 'domcontentloaded']

const EVALUATE_TEXT = page => page.evaluate(() => document.body.innerText)
Expand All @@ -18,6 +24,15 @@ const isEmpty = val => val == null || !(Object.keys(val) || val).length

const isExternalUrl = (domainOne, domainTwo) => domainOne !== domainTwo

const createTempFile = opts => {
const tmp = tempy.file(opts)
return {
path: tmp,
cleanup: unlink.bind(unlink, tmp),
cleanupSync: unlinkSync.bind(unlinkSync, tmp)
}
}

// The puppeteer launch causes many events to be emitted.
process.setMaxListeners(0)

Expand Down Expand Up @@ -87,7 +102,7 @@ module.exports = launchOpts => {

if (userAgent) await page.setUserAgent(userAgent)
if (viewport) await page.setViewport(viewport)
const response = await page.goto(url, Object.assign({ waitUntil }, args))
const response = await page.goto(url, { waitUntil, ...args })
if (waitFor) await page.waitFor(waitFor)
debug(reqCount)
return response
Expand Down Expand Up @@ -134,9 +149,7 @@ module.exports = launchOpts => {
...args
} = opts

const tempFile = createTempFile(Object.assign({ ext: `.${type}` }, tmpOpts))
const { path } = tempFile

const tempFile = createTempFile({ extension: type, ...tmpOpts })
const { userAgent: deviceUserAgent, viewport: deviceViewport } = getDevice(
deviceName
)
Expand All @@ -145,18 +158,18 @@ module.exports = launchOpts => {

await goto(page, {
userAgent: isEmpty(userAgent) ? deviceUserAgent : userAgent,
viewport: Object.assign({}, deviceViewport, viewport),
viewport: { ...deviceViewport, ...viewport },
url,
abortTypes,
waitFor,
waitUntil,
args
})

await page.screenshot(Object.assign({ path, type }, args))
await page.screenshot({ path: tempFile.path, type, ...args })

await page.close()
return Promise.resolve(tempFile)
return tempFile
}

const pdf = async (url, opts = {}) => {
Expand All @@ -182,8 +195,7 @@ module.exports = launchOpts => {
...args
} = opts

const tempFile = createTempFile(Object.assign({ ext: `.pdf` }, tmpOpts))
const { path } = tempFile
const tempFile = createTempFile({ extension: 'pdf', ...tmpOpts })

const { userAgent: deviceUserAgent, viewport: deviceViewport } = getDevice(
deviceName
Expand All @@ -195,7 +207,7 @@ module.exports = launchOpts => {

await goto(page, {
userAgent: isEmpty(userAgent) ? deviceUserAgent : userAgent,
viewport: Object.assign({}, deviceViewport, viewport),
viewport: { ...deviceViewport, ...viewport },
url,
abortTrackers,
abortTypes,
Expand All @@ -204,21 +216,16 @@ module.exports = launchOpts => {
args
})

await page.pdf(
Object.assign(
{
margin,
path,
format,
printBackground,
scale
},
args
)
)

await page.pdf({
path: tempFile.path,
margin,
format,
printBackground,
scale,
...args
})
await page.close()
return Promise.resolve(tempFile)
return tempFile
}

return {
Expand Down
27 changes: 12 additions & 15 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,41 @@ const looksSame = promisify(require('looks-same'))
describe('format', () => {
it('png', async () => {
const browserless = createBrowserless()
const tmpStream = await browserless.screenshot('http://example.com')
const isEqual = await looksSame('test/example.png', tmpStream.path)
tmpStream.cleanupSync()
should(path.extname(tmpStream.path)).be.equal('.png')
const tmp = await browserless.screenshot('http://example.com')
const isEqual = await looksSame('test/example.png', tmp.path)
tmp.cleanupSync()
should(path.extname(tmp.path)).be.equal('.png')
return isEqual
})

it('jpeg', async () => {
const browserless = createBrowserless()
const tmpStream = await browserless.screenshot('http://example.com', {
const tmp = await browserless.screenshot('http://example.com', {
type: 'jpeg'
})
tmpStream.cleanupSync()
should(path.extname(tmpStream.path)).be.equal('.jpeg')
tmp.cleanupSync()
should(path.extname(tmp.path)).be.equal('.jpeg')
})
})

describe('devices', () => {
it('iPhone 6', async () => {
const browserless = createBrowserless()
const tmpStream = await browserless.screenshot('http://example.com', {
const tmp = await browserless.screenshot('http://example.com', {
device: 'iPhone 6'
})

const isEqual = await looksSame(
'test/example-iphone.png',
tmpStream.path
)
tmpStream.cleanupSync()
const isEqual = await looksSame('test/example-iphone.png', tmp.path)
tmp.cleanupSync()
return isEqual
})
})

describe('.pdf', () => {
it('get full PDF from an url', async () => {
const browserless = createBrowserless()
const tmpStream = await browserless.pdf('http://example.com')
should(path.extname(tmpStream.path)).be.equal('.pdf')
const tmp = await browserless.pdf('http://example.com')
should(path.extname(tmp.path)).be.equal('.pdf')
})
})
})
Expand Down

0 comments on commit e33c78c

Please sign in to comment.