-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbin.js
139 lines (122 loc) · 3.78 KB
/
bin.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
135
136
137
138
139
#!/usr/bin/env node
'use strict'
const pkg = require('./package.json')
const input = process.argv.slice(2)
const parseCurl = require('./parse-curl')
const eosp = require('end-of-stream-promise')
const { Readable } = require('streamx')
var parsed
const helpString =
`${pkg.description}
Usage
$ curld <curl_command>
Options
-v, --version Print version
-h, --help Display help message`
function printHelp () {
console.log(helpString)
process.exit(0)
}
function printVersion () {
console.log(pkg.version)
process.exit(0)
}
function printError (n = 0) {
console.error('I don\'t understand the parameters. Error: ' + n)
process.exit(-1)
}
function toFetch (data) {
parsed = parseCurl(data)
return `
fetch(
'${parsed.url}',
{headers:${JSON.stringify(parsed.header)},
method:'${parsed.method}'}
)
.then(console.log, console.error)`
}
function init (data) {
let res = toFetch(data)
if (parsed.url)
switch (parsed.protocol) {
case 'dat':
(async function () {
const fetch = require('dat-fetch')()
try {
const response = await fetch(parsed.url)
const stream = Readable.from(response.body)
stream.pipe(process.stdout)
await eosp(stream)
} finally {
process.exit()
}
})()
break;
case 'gemini':
(async function () {
const fetch = require('gemini-fetch')()
try {
const response = await fetch(parsed.url)
const stream = Readable.from(response.body)
stream.pipe(process.stdout)
await eosp(stream)
} finally {
process.exit()
}
})()
break;
case 'hyper':
(async function () {
const fetch = require('hypercore-fetch')()
try {
const response = await fetch(parsed.url)
const stream = Readable.from(response.body)
stream.pipe(process.stdout)
await eosp(stream)
} finally {
fetch.close()
}
})()
break;
case 'ipfs':
case 'ipns':
(async function () {
const IPFS = require('ipfs')
const makeIpfsFetch = require('js-ipfs-fetch')
const ipfs = await IPFS.create({ silent: true })
const fetch = await makeIpfsFetch({ ipfs })
try {
const response = await fetch(parsed.url)
const stream = Readable.from(response.body)
stream.pipe(process.stdout)
await eosp(stream)
} finally {
process.exit()
}
})()
break;
default:
console.log('Protocol ' + parsed.protocol + ' not supported');
process.exit(-1);
}
else
printError(1)
}
process.argv.map(arg => {
switch (arg) {
case '-h':
case '--help':
printHelp()
break
case '-v':
case '--version':
printVersion()
}
})
if (process.stdin.isTTY)
if (!process.argv[2])
printHelp()
else
init(input)
else
process.stdin.once('data', data => init(data))