| 35 | libname = libname.join('.'); |
| 36 | |
| 37 | let response = (err, params) => { |
| 38 | |
| 39 | if (err) { |
| 40 | res.writeHead(400, {'Content-Type': 'text/plain'}); |
| 41 | return res.end(`Error: ${err.message}`); |
| 42 | } |
| 43 | |
| 44 | console.log(`[function: ${libname}] ${JSON.stringify({args: params.args, kwargs: params.kwargs})}`); |
| 45 | |
| 46 | lib[`${libname}`](...params.args, params.kwargs, (err, result, headers) => { |
| 47 | |
| 48 | if (err) { |
| 49 | res.writeHead(400, {'Content-Type': 'text/plain'}); |
| 50 | res.end(`Error: ${err.message}`); |
| 51 | } else { |
| 52 | res.writeHead(200, headers); |
| 53 | if (result instanceof Buffer || typeof result !== 'object') { |
| 54 | res.end(result); |
| 55 | } else { |
| 56 | try { |
| 57 | result = JSON.stringify(result); |
| 58 | } catch (e) { |
| 59 | result = '{}'; |
| 60 | } |
| 61 | res.end(result); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | }); |
| 66 | |
| 67 | }; |
| 68 | |
| 69 | if (offline) { |
| 70 | response(null, {args: [], kwargs: urlParts.query, remoteAddress: '::1'}); |