makeRequest - utility function to generate a request * @param {object} params - params for making request * @param {string} params.hostname - request hostname * @param {number} [params.port] - request port * @param {string} params.method - request method * @param {object} [params.queryObj] - qu
(params, callback)
| 73 | * @return {undefined} - and call callback |
| 74 | */ |
| 75 | function makeRequest(params, callback) { |
| 76 | const { hostname, port, method, queryObj, headers, path, |
| 77 | authCredentials, requestBody, jsonResponse, |
| 78 | urlForSignature } = params; |
| 79 | const options = { |
| 80 | hostname, |
| 81 | port, |
| 82 | method, |
| 83 | headers, |
| 84 | path: path || '/', |
| 85 | rejectUnauthorized: false, |
| 86 | }; |
| 87 | const qs = querystring.stringify(queryObj); |
| 88 | |
| 89 | if (params.GCP && authCredentials) { |
| 90 | const bucketMatch = options.path.match(/^\/([^\/]+)/); |
| 91 | const bucketName = bucketMatch ? bucketMatch[1] : undefined; |
| 92 | const gcpPath = queryObj ? `${options.path}?${qs}` : options.path; |
| 93 | const requestForSigning = { |
| 94 | method, |
| 95 | headers: options.headers || {}, |
| 96 | url: gcpPath, |
| 97 | path: options.path, |
| 98 | endpoint: { host: hostname }, |
| 99 | bucketName, |
| 100 | query: queryObj || {}, |
| 101 | }; |
| 102 | signGcpRequest(requestForSigning, authCredentials, new Date()); |
| 103 | Object.assign(options.headers, { |
| 104 | Authorization: requestForSigning.headers.Authorization, |
| 105 | Date: requestForSigning.headers['x-goog-date'], |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | const req = transport.request(options, res => { |
| 110 | const body = []; |
| 111 | res.on('data', chunk => { |
| 112 | body.push(chunk); |
| 113 | }); |
| 114 | res.on('error', err => { |
| 115 | process.stdout.write('err receiving response'); |
| 116 | return callback(err); |
| 117 | }); |
| 118 | res.on('end', () => { |
| 119 | const total = body.join(''); |
| 120 | const data = { |
| 121 | headers: res.headers, |
| 122 | statusCode: res.statusCode, |
| 123 | body: total, |
| 124 | }; |
| 125 | const err = _parseError(total, res.statusCode, jsonResponse); |
| 126 | if (err) { |
| 127 | err.statusCode = res.statusCode; |
| 128 | } |
| 129 | return callback(err, data); |
| 130 | }); |
| 131 | }); |
| 132 | req.on('error', err => { |
no test coverage detected