Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/helpers/code-builder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

var util = require('util')
const util = require('util')

/**
* Helper object to format and aggragate lines of code.
Expand All @@ -11,7 +11,7 @@ var util = require('util')
* @param {string} indentation Desired indentation character for aggregated lines of code
* @param {string} join Desired character to join each line of code
*/
var CodeBuilder = function (indentation, join) {
const CodeBuilder = function (indentation, join) {
this.code = []
this.indentation = indentation
this.lineJoin = join || '\n'
Expand All @@ -37,8 +37,8 @@ var CodeBuilder = function (indentation, join) {
* // returns: 'console.log("\t\thello world")'
*/
CodeBuilder.prototype.buildLine = function (indentationLevel, line) {
var lineIndentation = ''
var slice = 2
let lineIndentation = ''
let slice = 2
if (Object.prototype.toString.call(indentationLevel) === '[object String]') {
slice = 1
line = indentationLevel
Expand All @@ -52,7 +52,7 @@ CodeBuilder.prototype.buildLine = function (indentationLevel, line) {
indentationLevel--
}

var format = Array.prototype.slice.call(arguments, slice, arguments.length)
let format = Array.prototype.slice.call(arguments, slice, arguments.length)
format.unshift(lineIndentation + line)

return util.format.apply(this, format)
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/reducer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict'

module.exports = function (obj, pair) {
if (obj[pair.name] === undefined) {
module.exports = (obj, pair) => {
if (obj[pair.name]) {
obj[pair.name] = pair.value
return obj
}

// convert to array
var arr = [
const arr = [
obj[pair.name],
pair.value
]
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/shell.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict'

var util = require('util')
const util = require('util')

module.exports = {
/**
* Use 'strong quoting' using single quotes so that we only need
* to deal with nested single quote characters.
* http://wiki.bash-hackers.org/syntax/quoting#strong_quoting
*/
quote: function (value) {
var safe = /^[a-z0-9-_/.@%^=:]+$/i
quote(value) {
let safe = /^[a-z0-9-_/.@%^=:]+$/i

// Unless `value` is a simple shell-safe string, quote it.
if (!safe.test(value)) {
Expand All @@ -19,7 +19,7 @@ module.exports = {
return value
},

escape: function (value) {
escape(value) {
return value.replace(/\r/g, '\\r').replace(/\n/g, '\\n')
}
}
63 changes: 29 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
'use strict'

var debug = require('debug')('httpsnippet')
var es = require('event-stream')
var MultiPartForm = require('form-data')
var qs = require('querystring')
var reducer = require('./helpers/reducer')
var targets = require('./targets')
var url = require('url')
var util = require('util')
var validate = require('har-validator/lib/async')
const debug = require('debug')('httpsnippet')
const es = require('event-stream')
const MultiPartForm = require('form-data')
const qs = require('querystring')
const reducer = require('./helpers/reducer')
const targets = require('./targets')
const url = require('url')
const util = require('util')
const validate = require('har-validator/lib/async')

// constructor
var HTTPSnippet = function (data) {
var entries
var self = this
var input = util._extend({}, data)
const HTTPSnippet = function (data) {
let entries
let self = this
let input = util._extend({}, data)

// prep the main container
self.requests = []
Expand All @@ -28,7 +28,7 @@ var HTTPSnippet = function (data) {
}]
}

entries.forEach(function (entry) {
entries.forEach(entry => {
// add optional properties to make validation successful
entry.request.httpVersion = entry.request.httpVersion || 'HTTP/1.1'
entry.request.queryString = entry.request.queryString || []
Expand All @@ -41,7 +41,7 @@ var HTTPSnippet = function (data) {
entry.request.headersSize = 0
entry.request.postData.size = 0

validate.request(entry.request, function (err, valid) {
validate.request(entry.request, (err, valid) => {
if (!valid) {
throw err
}
Expand Down Expand Up @@ -85,7 +85,7 @@ HTTPSnippet.prototype.prepare = function (request) {
}

// construct Cookie header
var cookies = request.cookies.map(function (cookie) {
let cookies = request.cookies.map(function (cookie) {
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value)
})

Expand All @@ -103,7 +103,7 @@ HTTPSnippet.prototype.prepare = function (request) {
request.postData.mimeType = 'multipart/form-data'

if (request.postData.params) {
var form = new MultiPartForm()
let form = new MultiPartForm()

// easter egg
form._boundary = '---011000010111000001101001'
Expand Down Expand Up @@ -186,17 +186,15 @@ HTTPSnippet.prototype.prepare = function (request) {
return request
}

HTTPSnippet.prototype.convert = function (target, client, opts) {
HTTPSnippet.prototype.convert = (target, client, opts) => {
if (!opts && client) {
opts = client
}

var func = this._matchTarget(target, client)
let func = this._matchTarget(target, client)

if (func) {
var results = this.requests.map(function (request) {
return func(request, opts)
})
let results = this.requests.map(request => func(request, opts)

return results.length === 1 ? results[0] : results
}
Expand Down Expand Up @@ -224,25 +222,22 @@ module.exports = HTTPSnippet

module.exports.availableTargets = function () {
return Object.keys(targets).map(function (key) {
var target = util._extend({}, targets[key].info)
var clients = Object.keys(targets[key])
let target = util._extend({}, targets[key].info)
let clients = Object.keys(targets[key])

.filter(prop => !~['info', 'index'].indexOf(prop)

.filter(function (prop) {
return !~['info', 'index'].indexOf(prop)
})

.map(function (client) {
return targets[key][client].info
})
.map(client => targets[key][client].info

if (clients.length) {
target.clients = clients
}

return target
})
});
}

module.exports.extname = function (target) {
return targets[target] ? targets[target].info.extname : ''
}
module.exports.extname = target => targets[target] ? targets[target].info.extname : ''