-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·134 lines (122 loc) · 3.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env node
const fetch = require('ilp-fetch')
const log = require('ilp-logger')('ilp-curl')
const fs = require('fs')
const plugin = require('ilp-plugin')()
const die = (message) => {
console.error(message)
process.exit(1)
}
const argv = require('yargs')
.usage('ilp-curl <url> [options]')
.option('data', {
alias: 'd',
describe: 'body data'
})
.option('data-raw', {
describe: 'body data that does not load file with @'
})
.option('json', {
alias: 'j',
describe: 'send data as json'
})
.option('header', {
alias: 'H',
describe: 'header with data',
array: true,
default: []
})
.option('form', {
alias: 'F',
describe: 'form data',
array: true
})
.option('max-redirs', {
describe: 'max number of redirects',
number: true,
default: 0
})
.option('request', {
describe: 'http method to use',
alias: 'X',
default: 'GET'
})
.option('url', {
describe: 'url to fetch'
})
.option('user', {
alias: 'u',
describe: '<user:password> for basic auth'
})
.option('max-amount', {
alias: 'a',
describe: `maximum amount`,
default: 100000
})
.argv
const splitOnFirst = (string, delim) => {
const splitAt = string.indexOf(delim)
const head = string.substring(0, splitAt)
const tail = string.substring(splitAt + 1)
return [ head, tail ]
}
const url = argv.url || argv._[0]
const rawData = argv['data-raw'] || argv.data
if (argv.form && rawData) die('cannot specify --form (-F) and --data (-d)')
if (argv.data && argv['data-raw']) die('cannot specify --data-raw and -data (-d)')
if (argv.url && argv._[0]) die('cannot specify --url and positional <url>')
if (!url) die('must specify a URL with positional <url> or --url')
const fetchOptions = {
method: argv.request.toUpperCase(),
redirect: 'follow',
follow: argv['max-redirs'],
headers: {
'Content-Type': (argv.json ? 'application/json' : 'application/x-www-form-urlencoded')
}
}
if (rawData) {
// the '@' causes a file to be loaded
if (argv.data && rawData.startsWith('@')) {
log.info('loading file', rawData.substring(1))
const contents = fs.readFileSync(rawData.substring(1))
fetchOptions.headers['Content-Type'] = (argv.json ? 'application/json' : 'application/octet-stream')
fetchOptions.body = (argv.json ? contents.toString('utf8') : contents)
} else {
fetchOptions.body = rawData
}
} else if (argv.form) {
const keyValueBody = {}
for (const field of argv.form) {
const [ key, value ] = splitOnFirst(field, '=')
if (value.startsWith('@')) {
log.info('loading file', rawData.substring(1))
const valueContents = fs
.readFileSync(value.substring(1))
.toString('utf8')
keyValueBody[key] = valueContents
} else {
keyValueBody[key] = value
}
}
fetchOptions.body = JSON.stringify(keyValueBody)
}
for (const header of argv.header) {
const [ name, value ] = splitOnFirst(header, ':')
fetchOptions.headers[name] = value
}
if (argv.user) {
fetchOptions.headers['Authorization'] = 'Basic ' +
Buffer.from(argv.user).toString('base64')
}
async function run () {
log.info('running')
await plugin.connect()
log.info('connected')
fetchOptions.plugin = plugin
fetchOptions.maxPrice = +argv['max-amount']
const result = await fetch(url, fetchOptions)
const text = await result.text()
console.log(text)
process.exit(0)
}
run().catch(e => die((e.res && e.res.text) || e.message))