| 316 | } |
| 317 | |
| 318 | function postPayload(endpointUrl, payload) { |
| 319 | return new Promise((resolve, reject) => { |
| 320 | const transport = endpointUrl.protocol === 'https:' ? https : http; |
| 321 | const data = Buffer.from(JSON.stringify(payload), 'utf8'); |
| 322 | const request = transport.request( |
| 323 | endpointUrl, |
| 324 | { |
| 325 | method: 'POST', |
| 326 | headers: { |
| 327 | 'Content-Type': 'application/json', |
| 328 | 'Content-Length': data.length, |
| 329 | }, |
| 330 | }, |
| 331 | (response) => { |
| 332 | const chunks = []; |
| 333 | response.on('data', (chunk) => chunks.push(chunk)); |
| 334 | response.on('end', () => { |
| 335 | resolve({ |
| 336 | statusCode: response.statusCode, |
| 337 | body: Buffer.concat(chunks).toString('utf8'), |
| 338 | }); |
| 339 | }); |
| 340 | } |
| 341 | ); |
| 342 | request.setTimeout(REQUEST_TIMEOUT_MS, () => { |
| 343 | request.destroy(new Error(`request timed out after ${REQUEST_TIMEOUT_MS}ms`)); |
| 344 | }); |
| 345 | request.on('error', reject); |
| 346 | request.end(data); |
| 347 | }); |
| 348 | } |
| 349 | |
| 350 | function summarize(metrics, malformed) { |
| 351 | const parts = []; |