MCPcopy
hub / github.com/Unitech/pm2 / open

Method open

modules/pm2-io-agent/src/Utility.js:336–379  ·  view source on GitHub ↗

* Send an HTTP request and return data or error if status > 200 * @param {Object} opts * @param {String} opts.url * @param {String} opts.method * @param {Object} [opts.data] * @param {Object} [opts.headers] * @param {Function} cb invoked with

(opts, cb)

Source from the content-addressed store, hash-verified

334 * @param {Function} cb invoked with <err, body>
335 */
336 open (opts, cb) {
337 const http = this.getModule(opts.url)
338 const parsedUrl = new URL(opts.url)
339 let data = null
340 const options = {
341 hostname: parsedUrl.hostname,
342 path: parsedUrl.pathname + parsedUrl.search,
343 port: parsedUrl.port,
344 method: opts.method,
345 headers: opts.headers,
346 agent: cst.PROXY ? new ProxyAgent(cst.PROXY) : undefined
347 }
348
349 if (opts.data) {
350 data = JSON.stringify(opts.data)
351 options.headers = Object.assign({
352 'Content-Type': 'application/json',
353 'Content-Length': data.length
354 }, opts.headers)
355 }
356
357 const req = http.request(options, (res) => {
358 let body = ''
359
360 res.setEncoding('utf8')
361 res.on('data', (chunk) => {
362 body += chunk.toString()
363 })
364 res.on('end', () => {
365 try {
366 let jsonData = JSON.parse(body)
367 return cb(null, jsonData)
368 } catch (err) {
369 return cb(err)
370 }
371 })
372 })
373 req.on('error', cb)
374
375 if (data) {
376 req.write(data)
377 }
378 req.end()
379 }
380}
381
382module.exports = {

Callers 1

_pingRootMethod · 0.45

Calls 8

getModuleMethod · 0.95
cbFunction · 0.85
toStringMethod · 0.80
setEncodingMethod · 0.65
onMethod · 0.65
writeMethod · 0.65
endMethod · 0.65
parseMethod · 0.45

Tested by

no test coverage detected