-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathccurl.js
More file actions
134 lines (117 loc) · 3.54 KB
/
ccurl.js
File metadata and controls
134 lines (117 loc) · 3.54 KB
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
import os from 'node:os'
import path from 'node:path'
import { readFileSync, existsSync, mkdirSync } from 'node:fs'
import { spawn, spawnSync, execFileSync } from 'node:child_process'
import * as ccurllib from 'ccurllib'
// read package meta data
const pkg = JSON.parse(readFileSync(path.join(import.meta.dirname, 'package.json'), { encoding: 'utf8' }))
const makeTmpDir = function () {
const tmp = os.tmpdir()
const p = path.join(tmp, 'ccurl')
if (!existsSync(p)) {
mkdirSync(p)
}
return p
}
const cookieJar = path.join(makeTmpDir(), 'jar')
// find path of supplied command
const which = (cmd) => {
try {
return execFileSync('which', [cmd]).toString().trim()
} catch (e) {
return null
}
}
// find the paths of curl and jq
const curlPath = which('curl')
const jqPath = which('jq')
// we can't proceed without curl
if (!curlPath) {
console.error('Error: cannot find curl in the path.')
process.exit(1)
}
// only pass the output to jq if the output is a terminal
const isTerminal = !!process.stdout.isTTY
// add slash path if none provided
if (process.argv.length === 2) {
process.argv.push('/')
}
// remove node and path parameters
process.argv.splice(0, 2)
let relativeURL = process.argv.splice(-1, 1)[0]
if (relativeURL[0] !== '/') {
relativeURL = '/' + relativeURL
}
const params = process.argv
// look for IAM_API_KEY
const IAM_API_KEY = process.env.IAM_API_KEY ? process.env.IAM_API_KEY : null
// assume localhost if COUCH_URL is unknown
let COUCH_URL = null
if (typeof process.env.COUCH_URL === 'undefined') {
COUCH_URL = 'http://127.0.0.1:5984'
console.warn('WARNING: no COUCH_URL environment variable found, assuming', COUCH_URL)
} else {
COUCH_URL = process.env.COUCH_URL
}
if (COUCH_URL[COUCH_URL.length - 1] === '/') {
COUCH_URL = COUCH_URL.substring(0, COUCH_URL.length - 1)
}
// check for presence of pre-existing -H parameter
const checkForContentType = function (params) {
for (const i in params) {
if (params[i] === '-H') {
const next = (parseInt(i) + 1).toString()
if (params[next] && params[next].toLowerCase().match(/^content-type:/)) {
return true
}
}
}
return false
}
// add more command-line parameters
params.push('-b')
params.push(cookieJar)
params.push('-c')
params.push(cookieJar)
params.push('-s')
params.push('-g')
if (!checkForContentType(params)) {
params.push('-H')
params.push('Content-Type: application/json')
}
params.push('-H')
params.push(`User-Agent: ${pkg.name}/${pkg.version}(${process.title}${process.version})`)
params.push(COUCH_URL + relativeURL)
// do curl
export async function ccurl() {
if (IAM_API_KEY) {
let obj
obj = ccurllib.get(IAM_API_KEY)
if (!obj) {
try {
obj = await ccurllib.getBearerToken(IAM_API_KEY)
if (obj) {
ccurllib.set(IAM_API_KEY, obj)
}
} catch (e) {
console.error('IAM Auth failed')
process.exit(1)
}
}
if (!obj) {
throw new Error('Could not perform IAM authentication')
}
params.push('-H')
params.push('Authorization: Bearer ' + obj.access_token)
}
// if jq is installed and the output is a terminal (not a file)
if (jqPath && isTerminal) {
// run curl & jq and pipe the output of one into the input of the other
const curl = spawn(curlPath, params, { stdio: ['inherit', 'pipe', 'inherit'] })
const jq = spawn(jqPath, ['.'], { stdio: ['pipe', 'inherit', 'pipe'] })
curl.stdout.pipe(jq.stdin)
} else {
// just run curl, spooling its stdout/stderr to ours
spawnSync(curlPath, params, { stdio: 'inherit' })
}
}