Sha256: b7f36628522e67c052fa76f80492f7328759a40ceb88eacedf0208316b3cdec9

Contents?: true

Size: 1.82 KB

Versions: 40

Compression:

Stored size: 1.82 KB

Contents

const http = require('http')
const https = require('https')

function doRequest (options) {
  const { ssl, encoding, requestOptions } = options
  return new Promise((resolve, reject) => {
    let responseText = ''
    const responseBinary = []
    const httpRequest = ssl ? https.request : http.request
    const req = httpRequest(requestOptions, function (response) {
      if (encoding !== 'binary') {
        response.setEncoding(encoding)
      }
      response.on('data', function (chunk) {
        if (encoding === 'binary') {
          responseBinary.push(chunk)
        } else {
          responseText += chunk
        }
      })
      response.on('end', function () {
        const result = {
          error: null,
          data: { statusCode: response.statusCode, headers: response.headers }
        }
        if (encoding === 'binary') {
          result.data['binary'] = Buffer.concat(responseBinary)
        } else {
          result.data['text'] = responseText
        }
        resolve(result)
      })
      response.on('error', function (error) {
        reject(error)
      })
    }).on('error', function (error) {
      reject(error)
    })
    req.end()
  })
}

(async () => {
  try {
    const args = process.argv.slice(2)
    const options = {}
    for (let j = 0; j < args.length; j++) {
      const arg = args[j]
      if (arg.startsWith('--ssl=')) {
        options.ssl = arg.slice('--ssl='.length) === 'true'
      } else if (arg.startsWith('--encoding=')) {
        options.encoding = arg.slice('--encoding='.length)
      } else if (arg.startsWith('--request-options=')) {
        options.requestOptions = JSON.parse(arg.slice('--request-options='.length))
      }
    }
    const result = await doRequest(options)
    console.log(JSON.stringify(result))
  } catch (e) {
    console.log(JSON.stringify({ 'error': e }))
  }
})()

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.8.2 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.8.1 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.8.0 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.8.0.beta1 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.7.4 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.8.0.alpha1 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.7.3 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.7.2 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.7.1 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.7.0 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.7.0.rc1 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.6.1 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.6.0 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.6.0.rc1 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.6.0.alpha1 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.5.1 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.5.0 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.5.0.rc1 stdlib/nodejs/node_modules/unxhr/lib/request.js
opal-1.4.1 stdlib/nodejs/node_modules/unxhr/lib/request.js