-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
83 lines (71 loc) · 2.36 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const { resolve } = require('path')
const NODE_MAJOR_VERSION = process.versions.node.split('.')[0]
let pool = null
if (NODE_MAJOR_VERSION < 12 || process.env.CAMARO_FORCE_SINGLE_THREAD === 'true') {
console.warn('[camaro] worker_threads is not available, expect performance drop. Try using Node version >= 12.')
const workerFn = require('./worker')
pool = {
run: async (args) => workerFn(args)
}
} else {
const WorkerPool = require('piscina')
pool = new WorkerPool({ filename: resolve(__dirname, 'worker.js') })
}
function isNonEmptyString(str) {
return typeof str === 'string' && str.length > 0
}
function isEmptyObject(obj) {
return Object.entries(obj).length === 0 && obj.constructor === Object
}
/**
* convert xml to json base on the template object
* @param {string} xml xml string
* @param {object} template template object
* @returns {object} xml converted to json object based on the template
*/
function transform(xml, template) {
if (!isNonEmptyString(xml)) {
throw new TypeError('1st argument (xml) must be a non-empty string')
}
if (!template || typeof template !== 'object' || isEmptyObject(template)) {
throw new TypeError('2nd argument (template) must be an object')
}
return pool.run({
fn: 'transform',
args: [xml, JSON.stringify(template)],
})
}
/**
* convert xml to json
* @param {string} xml xml string
* @returns {object} json object converted from the input xml
*/
function toJson(xml) {
throw new Error('Not yet implemented')
// if (!isNonEmptyString(xml)) {
// throw new TypeError('expecting xml input to be non-empty string')
// }
// return pool.run({ fn: 'toJson', args: [xml] })
}
/**
* pretty print xml string
* @param {string} xml xml string
* @param {object} opts pretty print options
* @param {number} [opts.indentSize=2] indent size, default=2
* @returns {string} xml pretty print string
*/
function prettyPrint(xml, opts = { indentSize: 2 }) {
if (!isNonEmptyString(xml)) {
throw new TypeError('expecting xml input to be non-empty string')
}
return pool.run({ fn: 'prettyPrint', args: [xml, opts] })
}
/**
* destroy the worker pool
*/
function destroy() {
if (pool && typeof pool.destroy === 'function') {
return pool.destroy();
}
}
module.exports = { transform, toJson, prettyPrint, destroy }