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 84 85 86 87 88 | 2x 2x 2x 2x 2x 8x 44x 42x 42x 40x 40x 40x 198x 40x 16x 16x 16x 16x 2x 16x 16x 16x 16x 16x 8x 16x 16x 14x 14x 14x 14x 2x 8x 8x | // Dependencies const request = require('request-promise'); const fs = require('fs'); const debug = require('debug')('node-cba-netbank'); // Initialisation const DEFAULT_OPTION = { jar: true, followAllRedirects: true, resolveWithFullResponse: true, headers: { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36', 'Cache-Control': 'no-cache', }, }; // Utilities const myRequest = request.defaults(DEFAULT_OPTION); const isString = obj => typeof obj === 'string' || obj instanceof String; function writeToFile(filename, content) { fs.writeFile(`log/${filename}`, content, (err) => { Iif (err) { debug(err); } debug(`Wrote to file => 'log/${filename}'.`); }); } function shorten(form) { const MAX_LEN = 200; const shortenForm = {}; Object.keys(form).forEach((key) => { shortenForm[key] = form[key].length > MAX_LEN ? `${form[key].substring(0, MAX_LEN)}... (${form[key].length - MAX_LEN} bytes more)` : form[key]; }); return shortenForm; } function req(params = {}) { const sequence = Date.now(); const { partial } = params; const headers = Object.assign({}, params.headers); if (partial) { headers['X-MicrosoftAjax'] = 'Delta=true'; } const myParams = Object.assign({ method: 'GET' }, params, { headers }); Eif (debug.enabled) { debug(`${myParams.method} ${myParams.url.substring(0, 80)}...`); debug(`headers => ${JSON.stringify(shorten(myParams.headers))}`); if (myParams.method === 'POST') { debug(`form => ${JSON.stringify(shorten(myParams.form))}`); } writeToFile(`${sequence}-1-request.json`, JSON.stringify(shorten(myParams), null, 2)); } return myRequest(myParams).then((response) => { Eif (debug.enabled) { writeToFile( `${sequence}-2-response.json`, JSON.stringify( { request: response.request, status: { code: response.statusCode, message: response.statusMessage, }, headers: response.headers, body: `${response.body.substring(0, 1000)}...`, }, null, 2, ), ); writeToFile(`${sequence}-3-response-body.html`, response.body); } return { url: response.request.href, headers: response.headers, body: response.body }; }); } // Export module.exports = { get: params => (isString(params) ? req({ url: params }) : req(params)), post: params => req(Object.assign({ method: 'POST' }, params)), }; |