-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
parse.js
63 lines (55 loc) · 1.8 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const isUrl = require('is-url')
const cst = require('./constants')
const isPositiveNumeric = require('../../util/is-positive-numeric')
const isNonEmptyString = require('../../util/is-non-empty-string')
/**
* @param {object} body : JSON-parsed request body
* - url
* - pdfOptions
* @param {object} req: HTTP request
* @param {object} opts : component options
* @param {function} sendToRenderer
* - errorCode
* - result
*/
function parse (body, req, opts, sendToRenderer) {
const result = {}
const errorOut = (code, msg) => {
result.msg = msg
sendToRenderer(code, result)
}
if (isUrl(body.url)) {
result.url = body.url
} else {
return errorOut(400, 'invalid url')
}
result.pdfOptions = body.pdf_options || {}
if (!isNonEmptyString(body.selector) && !isPositiveNumeric(body.timeout)) {
return errorOut(400, 'either selector or timeout must be specified')
}
result.selector = body.selector
result.timeOut = body.timeout
result.tries = Number(result.timeOut * 1000 / cst.minInterval)
if (cst.sizeMapping[result.pdfOptions.pageSize]) {
result.browserSize = cst.sizeMapping[result.pdfOptions.pageSize]
} else if (body.pageSize && isPositiveNumeric(body.pageSize.width) &&
isPositiveNumeric(body.pageSize.height)) {
result.browserSize = {
width: Math.ceil(body.pageSize.width * cst.pixelsInMicron),
height: Math.ceil(body.pageSize.height * cst.pixelsInMicron)
}
result.pdfOptions.pageSize = {
width: Math.ceil(body.pageSize.width),
height: Math.ceil(body.pageSize.height)
}
} else {
return errorOut(
400,
'pageSize must either be A3, A4, A5, Legal, Letter, ' +
'Tabloid or an Object containing height and width ' +
'in microns.'
)
}
sendToRenderer(null, result)
}
module.exports = parse