@ohos-ports/curlconverter 4.12.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +153 -0
- package/dist/src/Headers.d.ts +23 -0
- package/dist/src/Headers.js +287 -0
- package/dist/src/Headers.js.map +1 -0
- package/dist/src/Query.d.ts +8 -0
- package/dist/src/Query.js +136 -0
- package/dist/src/Query.js.map +1 -0
- package/dist/src/Request.d.ts +200 -0
- package/dist/src/Request.js +1191 -0
- package/dist/src/Request.js.map +1 -0
- package/dist/src/Warnings.d.ts +15 -0
- package/dist/src/Warnings.js +111 -0
- package/dist/src/Warnings.js.map +1 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +333 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/curl/auth.d.ts +12 -0
- package/dist/src/curl/auth.js +40 -0
- package/dist/src/curl/auth.js.map +1 -0
- package/dist/src/curl/form.d.ts +28 -0
- package/dist/src/curl/form.js +218 -0
- package/dist/src/curl/form.js.map +1 -0
- package/dist/src/curl/opts.d.ts +2052 -0
- package/dist/src/curl/opts.js +1000 -0
- package/dist/src/curl/opts.js.map +1 -0
- package/dist/src/curl/url.d.ts +14 -0
- package/dist/src/curl/url.js +218 -0
- package/dist/src/curl/url.js.map +1 -0
- package/dist/src/generators/ansible.d.ts +5 -0
- package/dist/src/generators/ansible.js +282 -0
- package/dist/src/generators/ansible.js.map +1 -0
- package/dist/src/generators/c.d.ts +12 -0
- package/dist/src/generators/c.js +1230 -0
- package/dist/src/generators/c.js.map +1 -0
- package/dist/src/generators/cfml.d.ts +5 -0
- package/dist/src/generators/cfml.js +134 -0
- package/dist/src/generators/cfml.js.map +1 -0
- package/dist/src/generators/clojure.d.ts +7 -0
- package/dist/src/generators/clojure.js +436 -0
- package/dist/src/generators/clojure.js.map +1 -0
- package/dist/src/generators/csharp.d.ts +8 -0
- package/dist/src/generators/csharp.js +356 -0
- package/dist/src/generators/csharp.js.map +1 -0
- package/dist/src/generators/dart.d.ts +5 -0
- package/dist/src/generators/dart.js +273 -0
- package/dist/src/generators/dart.js.map +1 -0
- package/dist/src/generators/elixir.d.ts +8 -0
- package/dist/src/generators/elixir.js +296 -0
- package/dist/src/generators/elixir.js.map +1 -0
- package/dist/src/generators/go.d.ts +5 -0
- package/dist/src/generators/go.js +228 -0
- package/dist/src/generators/go.js.map +1 -0
- package/dist/src/generators/har.d.ts +7 -0
- package/dist/src/generators/har.js +115 -0
- package/dist/src/generators/har.js.map +1 -0
- package/dist/src/generators/http.d.ts +7 -0
- package/dist/src/generators/http.js +165 -0
- package/dist/src/generators/http.js.map +1 -0
- package/dist/src/generators/httpie.d.ts +5 -0
- package/dist/src/generators/httpie.js +446 -0
- package/dist/src/generators/httpie.js.map +1 -0
- package/dist/src/generators/java/httpurlconnection.d.ts +5 -0
- package/dist/src/generators/java/httpurlconnection.js +123 -0
- package/dist/src/generators/java/httpurlconnection.js.map +1 -0
- package/dist/src/generators/java/java.d.ts +8 -0
- package/dist/src/generators/java/java.js +247 -0
- package/dist/src/generators/java/java.js.map +1 -0
- package/dist/src/generators/java/jsoup.d.ts +5 -0
- package/dist/src/generators/java/jsoup.js +152 -0
- package/dist/src/generators/java/jsoup.js.map +1 -0
- package/dist/src/generators/java/okhttp.d.ts +5 -0
- package/dist/src/generators/java/okhttp.js +232 -0
- package/dist/src/generators/java/okhttp.js.map +1 -0
- package/dist/src/generators/javascript/axios.d.ts +5 -0
- package/dist/src/generators/javascript/axios.js +332 -0
- package/dist/src/generators/javascript/axios.js.map +1 -0
- package/dist/src/generators/javascript/got.d.ts +5 -0
- package/dist/src/generators/javascript/got.js +277 -0
- package/dist/src/generators/javascript/got.js.map +1 -0
- package/dist/src/generators/javascript/http.d.ts +8 -0
- package/dist/src/generators/javascript/http.js +183 -0
- package/dist/src/generators/javascript/http.js.map +1 -0
- package/dist/src/generators/javascript/javascript.d.ts +37 -0
- package/dist/src/generators/javascript/javascript.js +676 -0
- package/dist/src/generators/javascript/javascript.js.map +1 -0
- package/dist/src/generators/javascript/jquery.d.ts +15 -0
- package/dist/src/generators/javascript/jquery.js +297 -0
- package/dist/src/generators/javascript/jquery.js.map +1 -0
- package/dist/src/generators/javascript/ky.d.ts +7 -0
- package/dist/src/generators/javascript/ky.js +245 -0
- package/dist/src/generators/javascript/ky.js.map +1 -0
- package/dist/src/generators/javascript/request.d.ts +5 -0
- package/dist/src/generators/javascript/request.js +113 -0
- package/dist/src/generators/javascript/request.js.map +1 -0
- package/dist/src/generators/javascript/superagent.d.ts +10 -0
- package/dist/src/generators/javascript/superagent.js +301 -0
- package/dist/src/generators/javascript/superagent.js.map +1 -0
- package/dist/src/generators/javascript/xhr.d.ts +8 -0
- package/dist/src/generators/javascript/xhr.js +147 -0
- package/dist/src/generators/javascript/xhr.js.map +1 -0
- package/dist/src/generators/json.d.ts +45 -0
- package/dist/src/generators/json.js +197 -0
- package/dist/src/generators/json.js.map +1 -0
- package/dist/src/generators/julia.d.ts +8 -0
- package/dist/src/generators/julia.js +365 -0
- package/dist/src/generators/julia.js.map +1 -0
- package/dist/src/generators/kotlin.d.ts +8 -0
- package/dist/src/generators/kotlin.js +325 -0
- package/dist/src/generators/kotlin.js.map +1 -0
- package/dist/src/generators/lua.d.ts +5 -0
- package/dist/src/generators/lua.js +192 -0
- package/dist/src/generators/lua.js.map +1 -0
- package/dist/src/generators/matlab/common.d.ts +16 -0
- package/dist/src/generators/matlab/common.js +238 -0
- package/dist/src/generators/matlab/common.js.map +1 -0
- package/dist/src/generators/matlab/httpinterface.d.ts +2 -0
- package/dist/src/generators/matlab/httpinterface.js +211 -0
- package/dist/src/generators/matlab/httpinterface.js.map +1 -0
- package/dist/src/generators/matlab/matlab.d.ts +5 -0
- package/dist/src/generators/matlab/matlab.js +29 -0
- package/dist/src/generators/matlab/matlab.js.map +1 -0
- package/dist/src/generators/matlab/webservices.d.ts +2 -0
- package/dist/src/generators/matlab/webservices.js +191 -0
- package/dist/src/generators/matlab/webservices.js.map +1 -0
- package/dist/src/generators/objectivec.d.ts +8 -0
- package/dist/src/generators/objectivec.js +269 -0
- package/dist/src/generators/objectivec.js.map +1 -0
- package/dist/src/generators/ocaml.d.ts +8 -0
- package/dist/src/generators/ocaml.js +150 -0
- package/dist/src/generators/ocaml.js.map +1 -0
- package/dist/src/generators/perl.d.ts +9 -0
- package/dist/src/generators/perl.js +209 -0
- package/dist/src/generators/perl.js.map +1 -0
- package/dist/src/generators/php/guzzle.d.ts +5 -0
- package/dist/src/generators/php/guzzle.js +328 -0
- package/dist/src/generators/php/guzzle.js.map +1 -0
- package/dist/src/generators/php/php.d.ts +8 -0
- package/dist/src/generators/php/php.js +217 -0
- package/dist/src/generators/php/php.js.map +1 -0
- package/dist/src/generators/php/requests.d.ts +5 -0
- package/dist/src/generators/php/requests.js +89 -0
- package/dist/src/generators/php/requests.js.map +1 -0
- package/dist/src/generators/powershell.d.ts +11 -0
- package/dist/src/generators/powershell.js +377 -0
- package/dist/src/generators/powershell.js.map +1 -0
- package/dist/src/generators/python/http.d.ts +5 -0
- package/dist/src/generators/python/http.js +128 -0
- package/dist/src/generators/python/http.js.map +1 -0
- package/dist/src/generators/python/python.d.ts +22 -0
- package/dist/src/generators/python/python.js +1693 -0
- package/dist/src/generators/python/python.js.map +1 -0
- package/dist/src/generators/r/httr.d.ts +10 -0
- package/dist/src/generators/r/httr.js +298 -0
- package/dist/src/generators/r/httr.js.map +1 -0
- package/dist/src/generators/r/httr2.d.ts +5 -0
- package/dist/src/generators/r/httr2.js +214 -0
- package/dist/src/generators/r/httr2.js.map +1 -0
- package/dist/src/generators/ruby/httparty.d.ts +6 -0
- package/dist/src/generators/ruby/httparty.js +308 -0
- package/dist/src/generators/ruby/httparty.js.map +1 -0
- package/dist/src/generators/ruby/ruby.d.ts +14 -0
- package/dist/src/generators/ruby/ruby.js +515 -0
- package/dist/src/generators/ruby/ruby.js.map +1 -0
- package/dist/src/generators/rust.d.ts +8 -0
- package/dist/src/generators/rust.js +177 -0
- package/dist/src/generators/rust.js.map +1 -0
- package/dist/src/generators/swift.d.ts +8 -0
- package/dist/src/generators/swift.js +219 -0
- package/dist/src/generators/swift.js.map +1 -0
- package/dist/src/generators/wget.d.ts +8 -0
- package/dist/src/generators/wget.js +472 -0
- package/dist/src/generators/wget.js.map +1 -0
- package/dist/src/index.d.ts +53 -0
- package/dist/src/index.js +54 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/parse.d.ts +13 -0
- package/dist/src/parse.js +43 -0
- package/dist/src/parse.js.map +1 -0
- package/dist/src/shell/Parser.d.ts +4 -0
- package/dist/src/shell/Parser.js +6 -0
- package/dist/src/shell/Parser.js.map +1 -0
- package/dist/src/shell/Word.d.ts +48 -0
- package/dist/src/shell/Word.js +486 -0
- package/dist/src/shell/Word.js.map +1 -0
- package/dist/src/shell/tokenizer.d.ts +3 -0
- package/dist/src/shell/tokenizer.js +610 -0
- package/dist/src/shell/tokenizer.js.map +1 -0
- package/dist/src/shell/webParser.d.ts +4 -0
- package/dist/src/shell/webParser.js +15 -0
- package/dist/src/shell/webParser.js.map +1 -0
- package/dist/src/utils.d.ts +6 -0
- package/dist/src/utils.js +21 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/tools/compare-requests.d.ts +2 -0
- package/dist/tools/compare-requests.js +448 -0
- package/dist/tools/compare-requests.js.map +1 -0
- package/dist/tools/gen-test.d.ts +2 -0
- package/dist/tools/gen-test.js +114 -0
- package/dist/tools/gen-test.js.map +1 -0
- package/dist/tree-sitter-bash.wasm +0 -0
- package/index.d.ts +3 -0
- package/package.json +97 -0
- package/src/Headers.ts +334 -0
- package/src/Query.ts +147 -0
- package/src/Request.ts +1649 -0
- package/src/Warnings.ts +154 -0
- package/src/cli.ts +561 -0
- package/src/curl/auth.ts +45 -0
- package/src/curl/form.ts +297 -0
- package/src/curl/opts.ts +1417 -0
- package/src/curl/url.ts +241 -0
- package/src/generators/ansible.ts +364 -0
- package/src/generators/c.ts +1371 -0
- package/src/generators/cfml.ts +155 -0
- package/src/generators/clojure.ts +521 -0
- package/src/generators/csharp.ts +383 -0
- package/src/generators/dart.ts +293 -0
- package/src/generators/elixir.ts +349 -0
- package/src/generators/go.ts +269 -0
- package/src/generators/har.ts +147 -0
- package/src/generators/http.ts +194 -0
- package/src/generators/httpie.ts +506 -0
- package/src/generators/java/httpurlconnection.ts +151 -0
- package/src/generators/java/java.ts +280 -0
- package/src/generators/java/jsoup.ts +172 -0
- package/src/generators/java/okhttp.ts +257 -0
- package/src/generators/javascript/axios.ts +395 -0
- package/src/generators/javascript/got.ts +362 -0
- package/src/generators/javascript/http.ts +232 -0
- package/src/generators/javascript/javascript.ts +825 -0
- package/src/generators/javascript/jquery.ts +360 -0
- package/src/generators/javascript/ky.ts +315 -0
- package/src/generators/javascript/request.ts +152 -0
- package/src/generators/javascript/superagent.ts +373 -0
- package/src/generators/javascript/xhr.ts +192 -0
- package/src/generators/json.ts +293 -0
- package/src/generators/julia.ts +398 -0
- package/src/generators/kotlin.ts +355 -0
- package/src/generators/lua.ts +209 -0
- package/src/generators/matlab/common.ts +281 -0
- package/src/generators/matlab/httpinterface.ts +284 -0
- package/src/generators/matlab/matlab.ts +39 -0
- package/src/generators/matlab/webservices.ts +263 -0
- package/src/generators/objectivec.ts +297 -0
- package/src/generators/ocaml.ts +168 -0
- package/src/generators/perl.ts +237 -0
- package/src/generators/php/guzzle.ts +368 -0
- package/src/generators/php/php.ts +239 -0
- package/src/generators/php/requests.ts +103 -0
- package/src/generators/powershell.ts +430 -0
- package/src/generators/python/http.ts +167 -0
- package/src/generators/python/python.ts +1934 -0
- package/src/generators/r/httr.ts +328 -0
- package/src/generators/r/httr2.ts +268 -0
- package/src/generators/ruby/httparty.ts +351 -0
- package/src/generators/ruby/ruby.ts +564 -0
- package/src/generators/rust.ts +243 -0
- package/src/generators/swift.ts +235 -0
- package/src/generators/wget.ts +533 -0
- package/src/index.ts +89 -0
- package/src/parse.ts +72 -0
- package/src/shell/Parser.ts +8 -0
- package/src/shell/Word.ts +531 -0
- package/src/shell/tokenizer.ts +736 -0
- package/src/shell/webParser.ts +18 -0
- package/src/utils.ts +25 -0
- package/tools/compare-requests.ts +571 -0
- package/tools/extract_curl_args.py +511 -0
- package/tools/gen-test.ts +129 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { Word } from "../shell/Word.js";
|
|
2
|
+
import { parse, getFirst, COMMON_SUPPORTED_ARGS } from "../parse.js";
|
|
3
|
+
import type { Request, Warnings } from "../parse.js";
|
|
4
|
+
|
|
5
|
+
import { esc as jsesc } from "./javascript/javascript.js";
|
|
6
|
+
|
|
7
|
+
export const supportedArgs = new Set([
|
|
8
|
+
...COMMON_SUPPORTED_ARGS,
|
|
9
|
+
"form",
|
|
10
|
+
"form-string",
|
|
11
|
+
"max-time",
|
|
12
|
+
"proxy",
|
|
13
|
+
"proxy-user",
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
function repr(w: Word | string): string {
|
|
17
|
+
if (typeof w !== "string" && !w.isString()) {
|
|
18
|
+
// TODO: warn
|
|
19
|
+
}
|
|
20
|
+
let s = w.toString();
|
|
21
|
+
let quote: "'" | '"' = '"';
|
|
22
|
+
if (s.includes('"') && !s.includes("'")) {
|
|
23
|
+
quote = "'";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// TODO: CFML doesn't support backslash escapes such as \n
|
|
27
|
+
s = jsesc(s, quote).replace(/#/g, "##");
|
|
28
|
+
if (quote === '"') {
|
|
29
|
+
return '"' + s.replace(/"/g, '""') + '"';
|
|
30
|
+
}
|
|
31
|
+
return "'" + s.replace(/'/g, "''") + "'";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function _toCFML(requests: Request[], warnings: Warnings = []): string {
|
|
35
|
+
const request = getFirst(requests, warnings);
|
|
36
|
+
|
|
37
|
+
let cfmlCode = "";
|
|
38
|
+
|
|
39
|
+
cfmlCode += "httpService = new http();\n";
|
|
40
|
+
cfmlCode += "httpService.setUrl(" + repr(request.urls[0].url) + ");\n";
|
|
41
|
+
cfmlCode += "httpService.setMethod(" + repr(request.urls[0].method) + ");\n";
|
|
42
|
+
|
|
43
|
+
if (request.cookies) {
|
|
44
|
+
for (const [headerName, headerValue] of request.cookies) {
|
|
45
|
+
cfmlCode +=
|
|
46
|
+
'httpService.addParam(type="cookie", name=' +
|
|
47
|
+
repr(headerName) +
|
|
48
|
+
", value=" +
|
|
49
|
+
repr(headerValue) +
|
|
50
|
+
");\n";
|
|
51
|
+
}
|
|
52
|
+
request.headers.delete("Cookie");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (request.headers.length) {
|
|
56
|
+
for (const [headerName, headerValue] of request.headers) {
|
|
57
|
+
cfmlCode +=
|
|
58
|
+
'httpService.addParam(type="header", name=' +
|
|
59
|
+
repr(headerName) +
|
|
60
|
+
", value=" +
|
|
61
|
+
repr(headerValue || new Word()) +
|
|
62
|
+
");\n";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (request.timeout) {
|
|
67
|
+
cfmlCode +=
|
|
68
|
+
"httpService.setTimeout(" +
|
|
69
|
+
(parseInt(request.timeout.toString(), 10) || 0) +
|
|
70
|
+
");\n";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (request.urls[0].auth) {
|
|
74
|
+
const [authUser, authPassword] = request.urls[0].auth;
|
|
75
|
+
cfmlCode += "httpService.setUsername(" + repr(authUser) + ");\n";
|
|
76
|
+
cfmlCode += "httpService.setPassword(" + repr(authPassword || "") + ");\n";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (request.proxy) {
|
|
80
|
+
const p = request.proxy.toString();
|
|
81
|
+
let proxy = p;
|
|
82
|
+
let proxyPort = "1080";
|
|
83
|
+
const proxyPart = p.match(/:([0-9]+)/);
|
|
84
|
+
if (proxyPart) {
|
|
85
|
+
proxy = p.slice(0, proxyPart.index);
|
|
86
|
+
proxyPort = proxyPart[1];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
cfmlCode += "httpService.setProxyServer(" + repr(proxy) + ");\n";
|
|
90
|
+
cfmlCode += "httpService.setProxyPort(" + proxyPort.trim() + ");\n";
|
|
91
|
+
|
|
92
|
+
if (request.proxyAuth) {
|
|
93
|
+
const [proxyUser, proxyPassword] = request.proxyAuth.split(":", 2);
|
|
94
|
+
cfmlCode += "httpService.setProxyUser(" + repr(proxyUser) + ");\n";
|
|
95
|
+
cfmlCode +=
|
|
96
|
+
"httpService.setProxyPassword(" + repr(proxyPassword || "") + ");\n";
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (request.data || request.multipartUploads) {
|
|
101
|
+
if (request.multipartUploads) {
|
|
102
|
+
for (const m of request.multipartUploads) {
|
|
103
|
+
if ("contentFile" in m) {
|
|
104
|
+
cfmlCode +=
|
|
105
|
+
'httpService.addParam(type="file", name=' +
|
|
106
|
+
repr(m.name) +
|
|
107
|
+
', file="#expandPath(' +
|
|
108
|
+
repr(m.contentFile) +
|
|
109
|
+
')#");\n';
|
|
110
|
+
} else {
|
|
111
|
+
cfmlCode +=
|
|
112
|
+
'httpService.addParam(type="formfield", name=' +
|
|
113
|
+
repr(m.name) +
|
|
114
|
+
", value=" +
|
|
115
|
+
repr(m.content) +
|
|
116
|
+
");\n";
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
} else if (
|
|
120
|
+
!request.isDataRaw &&
|
|
121
|
+
request.data &&
|
|
122
|
+
request.data.charAt(0) === "@"
|
|
123
|
+
) {
|
|
124
|
+
cfmlCode +=
|
|
125
|
+
'httpService.addParam(type="body", value="#' +
|
|
126
|
+
(request.isDataBinary ? "fileReadBinary" : "fileRead") +
|
|
127
|
+
"(expandPath(" +
|
|
128
|
+
repr(request.data.toString().substring(1)) +
|
|
129
|
+
'))#");\n';
|
|
130
|
+
} else {
|
|
131
|
+
cfmlCode +=
|
|
132
|
+
'httpService.addParam(type="body", value=' +
|
|
133
|
+
repr(request.data!) +
|
|
134
|
+
");\n";
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
cfmlCode += "\nresult = httpService.send().getPrefix();\n";
|
|
139
|
+
cfmlCode += "writeDump(result);\n";
|
|
140
|
+
|
|
141
|
+
return cfmlCode;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function toCFMLWarn(
|
|
145
|
+
curlCommand: string | string[],
|
|
146
|
+
warnings: Warnings = [],
|
|
147
|
+
): [string, Warnings] {
|
|
148
|
+
const requests = parse(curlCommand, supportedArgs, warnings);
|
|
149
|
+
const cfml = _toCFML(requests, warnings);
|
|
150
|
+
return [cfml, warnings];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function toCFML(curlCommand: string | string[]): string {
|
|
154
|
+
return toCFMLWarn(curlCommand)[0];
|
|
155
|
+
}
|
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
import { CCError } from "../utils.js";
|
|
2
|
+
import { Word, eq } from "../shell/Word.js";
|
|
3
|
+
import { parse, getFirst, COMMON_SUPPORTED_ARGS } from "../parse.js";
|
|
4
|
+
import type { Request, Warnings } from "../parse.js";
|
|
5
|
+
import type { QueryList, QueryDict } from "../Query.js";
|
|
6
|
+
import { Headers } from "../Headers.js";
|
|
7
|
+
import type { DataParam } from "../Request.js";
|
|
8
|
+
import type { FormParam } from "../curl/form.js";
|
|
9
|
+
import { parseQueryString } from "../Query.js";
|
|
10
|
+
|
|
11
|
+
// "Clojure strings are Java Strings."
|
|
12
|
+
// https://clojure.org/reference/data_structures#Strings
|
|
13
|
+
import { reprStr } from "./java/java.js";
|
|
14
|
+
|
|
15
|
+
export const supportedArgs = new Set([
|
|
16
|
+
...COMMON_SUPPORTED_ARGS,
|
|
17
|
+
"form",
|
|
18
|
+
"form-string",
|
|
19
|
+
|
|
20
|
+
// "http0.9",
|
|
21
|
+
// "http1.0",
|
|
22
|
+
// "http1.1",
|
|
23
|
+
// "no-http0.9",
|
|
24
|
+
|
|
25
|
+
"insecure",
|
|
26
|
+
"no-insecure",
|
|
27
|
+
"compressed",
|
|
28
|
+
"no-compressed",
|
|
29
|
+
|
|
30
|
+
"max-time",
|
|
31
|
+
"connect-timeout",
|
|
32
|
+
|
|
33
|
+
"max-redirs",
|
|
34
|
+
"location",
|
|
35
|
+
"no-location",
|
|
36
|
+
"location-trusted",
|
|
37
|
+
"no-location-trusted",
|
|
38
|
+
|
|
39
|
+
// "anyauth",
|
|
40
|
+
// "no-anyauth",
|
|
41
|
+
"digest",
|
|
42
|
+
"no-digest",
|
|
43
|
+
// "aws-sigv4",
|
|
44
|
+
// "negotiate",
|
|
45
|
+
// "no-negotiate",
|
|
46
|
+
// "delegation", // GSS/kerberos
|
|
47
|
+
// "service-name", // GSS/kerberos
|
|
48
|
+
"ntlm",
|
|
49
|
+
"no-ntlm",
|
|
50
|
+
"ntlm-wb",
|
|
51
|
+
"no-ntlm-wb",
|
|
52
|
+
|
|
53
|
+
// "upload-file",
|
|
54
|
+
// "output",
|
|
55
|
+
// "proxy",
|
|
56
|
+
// "proxy-user",
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
export function repr(w: Word, importLines: Set<string>): string {
|
|
60
|
+
const args: string[] = [];
|
|
61
|
+
for (const t of w.tokens) {
|
|
62
|
+
if (typeof t === "string") {
|
|
63
|
+
args.push(reprStr(t));
|
|
64
|
+
} else if (t.type === "variable") {
|
|
65
|
+
// (str) to return empty string if var missing
|
|
66
|
+
// TODO: is there a better way?
|
|
67
|
+
args.push("(str (System/getenv " + reprStr(t.value) + "))");
|
|
68
|
+
// TODO: Seems to be unnecessary
|
|
69
|
+
// importLines.add("(import 'java.lang.System)");
|
|
70
|
+
} else {
|
|
71
|
+
importLines.add("(use '[clojure.java.shell :only [sh]])");
|
|
72
|
+
args.push('(sh "bash" "-c" ' + reprStr(t.value) + ")");
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (args.length > 1) {
|
|
76
|
+
// TODO: format on multiple lines if it's long enough?
|
|
77
|
+
return "(str " + args.join(" ") + ")";
|
|
78
|
+
}
|
|
79
|
+
return args[0];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// https://clojure.org/reference/reader#_symbols
|
|
83
|
+
// https://clojure.org/reference/reader#_literals
|
|
84
|
+
function safeAsKeyword(s: string): boolean {
|
|
85
|
+
return /^[a-zA-Z_][a-zA-Z0-9*+!\-_'?<>=]*$/.test(s);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function reprQueryDict(query: QueryDict, importLines: Set<string>): string {
|
|
89
|
+
return (
|
|
90
|
+
"{" +
|
|
91
|
+
query
|
|
92
|
+
.map((q) => {
|
|
93
|
+
const key = q[0].toString();
|
|
94
|
+
if (!q[0].isString() || !safeAsKeyword(key)) {
|
|
95
|
+
throw new CCError(
|
|
96
|
+
"can't use query key as Clojure keyword: " + JSON.stringify(key),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
return (
|
|
100
|
+
":" +
|
|
101
|
+
key +
|
|
102
|
+
" " +
|
|
103
|
+
(Array.isArray(q[1])
|
|
104
|
+
? "[" + q[1].map((qq) => repr(qq, importLines)).join(" ") + "]"
|
|
105
|
+
: repr(q[1], importLines))
|
|
106
|
+
);
|
|
107
|
+
})
|
|
108
|
+
.join("\n ") +
|
|
109
|
+
"}"
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function reprQueryList(query: QueryList, importLines: Set<string>): string {
|
|
114
|
+
// TODO: this is only necessary if there are repeated keys
|
|
115
|
+
return (
|
|
116
|
+
"[" +
|
|
117
|
+
query
|
|
118
|
+
.map(
|
|
119
|
+
(q) =>
|
|
120
|
+
"[" + repr(q[0], importLines) + " " + repr(q[1], importLines) + "]",
|
|
121
|
+
)
|
|
122
|
+
.join("\n ") +
|
|
123
|
+
"]"
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function rerpQuery(
|
|
128
|
+
queryList: QueryList | null | undefined,
|
|
129
|
+
queryDict: QueryDict | null | undefined,
|
|
130
|
+
importLines: Set<string>,
|
|
131
|
+
): string | null {
|
|
132
|
+
if (queryDict) {
|
|
133
|
+
try {
|
|
134
|
+
return reprQueryDict(queryDict, importLines);
|
|
135
|
+
} catch {}
|
|
136
|
+
}
|
|
137
|
+
if (queryList) {
|
|
138
|
+
try {
|
|
139
|
+
return reprQueryList(queryList, importLines);
|
|
140
|
+
} catch {}
|
|
141
|
+
}
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function reprHeaders(headers: Headers, importLines: Set<string>): string {
|
|
146
|
+
const lines = headers.headers
|
|
147
|
+
// Can't be null
|
|
148
|
+
.map(
|
|
149
|
+
// TODO: convert to keywords and lowercase known headers
|
|
150
|
+
// TODO: :content-type is a top-level key and changes how the body is interpreted
|
|
151
|
+
(h) => repr(h[0], importLines) + " " + repr(h[1] as Word, importLines),
|
|
152
|
+
);
|
|
153
|
+
return "{" + lines.join(",\n ") + "}";
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function indent(s: string, indent: number): string {
|
|
157
|
+
const withSpaces = "\n" + " ".repeat(indent);
|
|
158
|
+
return s.replace(/\n/g, withSpaces);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function reprJson(
|
|
162
|
+
obj: Word | Word[] | string | number | boolean | object | null,
|
|
163
|
+
importLines?: Set<string>,
|
|
164
|
+
): string {
|
|
165
|
+
if (importLines && obj instanceof Word) {
|
|
166
|
+
return repr(obj, importLines);
|
|
167
|
+
}
|
|
168
|
+
switch (typeof obj) {
|
|
169
|
+
case "string":
|
|
170
|
+
return reprStr(obj);
|
|
171
|
+
case "number":
|
|
172
|
+
return obj.toString(); // TODO
|
|
173
|
+
case "boolean":
|
|
174
|
+
return obj ? "true" : "false";
|
|
175
|
+
case "object":
|
|
176
|
+
if (obj === null) {
|
|
177
|
+
return "nil";
|
|
178
|
+
}
|
|
179
|
+
if (Array.isArray(obj)) {
|
|
180
|
+
const objReprs = obj.map((o) => reprJson(o));
|
|
181
|
+
const totalLength = objReprs.reduce((a, b) => a + b.length, 0);
|
|
182
|
+
if (totalLength < 100) {
|
|
183
|
+
return "[" + objReprs.join(" ") + "]";
|
|
184
|
+
}
|
|
185
|
+
return "[" + indent(objReprs.join("\n"), 1) + "]";
|
|
186
|
+
} else {
|
|
187
|
+
const objReprs = Object.entries(obj).map(([k, v]) => {
|
|
188
|
+
if (!safeAsKeyword(k)) {
|
|
189
|
+
throw new CCError(
|
|
190
|
+
"can't use JSON key as Clojure keyword: " + JSON.stringify(k),
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
// TODO: indent logic is wrong?
|
|
194
|
+
return ":" + k + " " + indent(reprJson(v), 1 + k.length + 1);
|
|
195
|
+
});
|
|
196
|
+
const totalLength = objReprs.reduce((a, b) => a + b.length, 0);
|
|
197
|
+
if (totalLength < 100 && objReprs.every((o) => !o.includes("\n"))) {
|
|
198
|
+
return "{" + objReprs.join(" ") + "}";
|
|
199
|
+
}
|
|
200
|
+
return "{" + indent(objReprs.join("\n"), 1) + "}";
|
|
201
|
+
}
|
|
202
|
+
default:
|
|
203
|
+
throw new CCError("unexpect type in JSON: " + typeof obj);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function addDataString(
|
|
208
|
+
params: Params,
|
|
209
|
+
request: Request,
|
|
210
|
+
data: Word,
|
|
211
|
+
importLines: Set<string>,
|
|
212
|
+
) {
|
|
213
|
+
const contentType = request.headers.getContentType();
|
|
214
|
+
const exactContentType = request.headers.get("content-type");
|
|
215
|
+
if (contentType === "application/json") {
|
|
216
|
+
const dataStr = data.toString();
|
|
217
|
+
const parsed = JSON.parse(dataStr);
|
|
218
|
+
// Only convert arrays and {} as Clojure objects
|
|
219
|
+
if (typeof parsed !== "object" || parsed === null) {
|
|
220
|
+
params["body"] = repr(data, importLines);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const roundtrips = JSON.stringify(parsed) === dataStr;
|
|
224
|
+
const jsonAsClojure = reprJson(parsed);
|
|
225
|
+
const originalAsStr = repr(data, importLines);
|
|
226
|
+
params["form-params"] = jsonAsClojure;
|
|
227
|
+
importLines.add("(require '[cheshire.core :as json])");
|
|
228
|
+
if (!roundtrips) {
|
|
229
|
+
params["commented-body"] = originalAsStr; // commented-body is a special case
|
|
230
|
+
}
|
|
231
|
+
if (eq(exactContentType, "application/json")) {
|
|
232
|
+
request.headers.delete("content-type");
|
|
233
|
+
}
|
|
234
|
+
// Put this line after the commented body so that the option map's closing }) isn't commented out
|
|
235
|
+
params["content-type"] = ":json";
|
|
236
|
+
if (eq(request.headers.get("accept"), "application/json")) {
|
|
237
|
+
request.headers.delete("accept");
|
|
238
|
+
params["accept"] = ":json";
|
|
239
|
+
}
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (contentType === "application/x-www-form-urlencoded") {
|
|
244
|
+
const [queryList, queryDict] = parseQueryString(data);
|
|
245
|
+
const formParams = rerpQuery(queryList, queryDict, importLines);
|
|
246
|
+
if (formParams !== null) {
|
|
247
|
+
if (eq(exactContentType, "application/x-www-form-urlencoded")) {
|
|
248
|
+
request.headers.delete("content-type");
|
|
249
|
+
}
|
|
250
|
+
// TODO: check roundtrip, add a comment
|
|
251
|
+
params["form-params"] = formParams;
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
params["body"] = repr(data, importLines);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function addData(
|
|
260
|
+
params: Params,
|
|
261
|
+
request: Request,
|
|
262
|
+
data: DataParam[],
|
|
263
|
+
importLines: Set<string>,
|
|
264
|
+
) {
|
|
265
|
+
if (data.length === 1 && data[0] instanceof Word && data[0].isString()) {
|
|
266
|
+
try {
|
|
267
|
+
return addDataString(params, request, data[0], importLines);
|
|
268
|
+
} catch {}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const parts = [];
|
|
272
|
+
for (const d of data) {
|
|
273
|
+
if (d instanceof Word) {
|
|
274
|
+
parts.push(repr(d, importLines));
|
|
275
|
+
} else {
|
|
276
|
+
const { filetype, name, filename } = d;
|
|
277
|
+
if (filetype === "urlencode" && name) {
|
|
278
|
+
// TODO: add this to the previous Word
|
|
279
|
+
parts.push(repr(name, importLines));
|
|
280
|
+
}
|
|
281
|
+
if (eq(filename, "-")) {
|
|
282
|
+
// TODO: does this work?
|
|
283
|
+
parts.push("(slurp *in*)");
|
|
284
|
+
} else {
|
|
285
|
+
parts.push(
|
|
286
|
+
"(clojure.java.io/file " + repr(filename, importLines) + ")",
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (parts.length === 1) {
|
|
292
|
+
params["body"] = parts[0];
|
|
293
|
+
} else {
|
|
294
|
+
// TODO: this probably doesn't work with files
|
|
295
|
+
// TODO: this needlessly nests str if there are variables/subcommands
|
|
296
|
+
params["body"] = "(str " + parts.join(" ") + ")";
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function reprMultipart(
|
|
301
|
+
form: FormParam[],
|
|
302
|
+
importLines: Set<string>,
|
|
303
|
+
warnings: Warnings,
|
|
304
|
+
): string {
|
|
305
|
+
const parts = [];
|
|
306
|
+
for (const f of form) {
|
|
307
|
+
let part = "{:name " + repr(f.name, importLines);
|
|
308
|
+
if ("content" in f) {
|
|
309
|
+
part += " :content " + repr(f.content, importLines);
|
|
310
|
+
} else {
|
|
311
|
+
part +=
|
|
312
|
+
" :content (clojure.java.io/file " +
|
|
313
|
+
repr(f.contentFile, importLines) +
|
|
314
|
+
")";
|
|
315
|
+
if (f.filename && !eq(f.filename, f.contentFile)) {
|
|
316
|
+
warnings.push([
|
|
317
|
+
"cant-fake-filename",
|
|
318
|
+
"there's no way to send the filename " +
|
|
319
|
+
JSON.stringify(f.filename.toString()) +
|
|
320
|
+
" instead of " +
|
|
321
|
+
JSON.stringify(f.contentFile.toString()),
|
|
322
|
+
]);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
parts.push(part + "}");
|
|
326
|
+
}
|
|
327
|
+
return "[" + parts.join("\n ") + "]";
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
type Params = { [key: string]: string };
|
|
331
|
+
export function _toClojure(
|
|
332
|
+
requests: Request[],
|
|
333
|
+
warnings: Warnings = [],
|
|
334
|
+
): string {
|
|
335
|
+
const request = getFirst(requests, warnings, { dataReadsFile: true });
|
|
336
|
+
|
|
337
|
+
// TODO: merge require statements
|
|
338
|
+
const importLines = new Set<string>([
|
|
339
|
+
"(require '[clj-http.client :as client])",
|
|
340
|
+
]);
|
|
341
|
+
|
|
342
|
+
const methods = new Set([
|
|
343
|
+
"GET",
|
|
344
|
+
"HEAD",
|
|
345
|
+
"POST",
|
|
346
|
+
"PUT",
|
|
347
|
+
"DELETE",
|
|
348
|
+
"OPTIONS",
|
|
349
|
+
"COPY",
|
|
350
|
+
"MOVE",
|
|
351
|
+
"PATCH",
|
|
352
|
+
]);
|
|
353
|
+
const dataMethods = new Set(["POST", "PUT", "PATCH", "DELETE"]);
|
|
354
|
+
|
|
355
|
+
const method = request.urls[0].method;
|
|
356
|
+
const methodStr = method.toString();
|
|
357
|
+
const params: Params = {};
|
|
358
|
+
let fn;
|
|
359
|
+
if (method.isString() && methods.has(methodStr)) {
|
|
360
|
+
fn = "client/" + methodStr.toLowerCase();
|
|
361
|
+
} else {
|
|
362
|
+
// TODO: do all the other params still work?
|
|
363
|
+
fn = "client/request";
|
|
364
|
+
params["url"] = ""; // placeholder so that it comes first
|
|
365
|
+
params["method"] = repr(method, importLines);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
let url = request.urls[0].url; // TODO: .urlWithOriginalQuery?
|
|
369
|
+
try {
|
|
370
|
+
const queryParams = rerpQuery(
|
|
371
|
+
request.urls[0].queryList,
|
|
372
|
+
request.urls[0].queryDict,
|
|
373
|
+
importLines,
|
|
374
|
+
);
|
|
375
|
+
|
|
376
|
+
if (queryParams) {
|
|
377
|
+
params["query-params"] = queryParams;
|
|
378
|
+
url = request.urls[0].urlWithoutQueryList;
|
|
379
|
+
}
|
|
380
|
+
} catch {}
|
|
381
|
+
|
|
382
|
+
// addData() can delete headers, but to put them before the body we add a placeholder here
|
|
383
|
+
params["headers"] = "";
|
|
384
|
+
|
|
385
|
+
if (request.urls[0].auth) {
|
|
386
|
+
const [user, pass] = request.urls[0].auth;
|
|
387
|
+
const authParam = {
|
|
388
|
+
basic: "basic-auth",
|
|
389
|
+
digest: "digest-auth",
|
|
390
|
+
// TODO: should be ["user" "pass" "host" "domain"]
|
|
391
|
+
// TODO: warn
|
|
392
|
+
ntlm: "ntlm-auth",
|
|
393
|
+
"ntlm-wb": "ntlm-auth",
|
|
394
|
+
|
|
395
|
+
// TODO: error
|
|
396
|
+
negotiate: "basic-auth",
|
|
397
|
+
bearer: "basic-auth",
|
|
398
|
+
"aws-sigv4": "basic-auth",
|
|
399
|
+
none: "basic-auth",
|
|
400
|
+
}[request.authType];
|
|
401
|
+
|
|
402
|
+
params[authParam] =
|
|
403
|
+
"[" + repr(user, importLines) + " " + repr(pass, importLines) + "]";
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (request.multipartUploads) {
|
|
407
|
+
params["multipart"] = reprMultipart(
|
|
408
|
+
request.multipartUploads,
|
|
409
|
+
importLines,
|
|
410
|
+
warnings,
|
|
411
|
+
);
|
|
412
|
+
// TODO: above warning probably also applies here
|
|
413
|
+
} else if (request.dataArray && request.data) {
|
|
414
|
+
// Can delete headers
|
|
415
|
+
addData(params, request, request.dataArray, importLines);
|
|
416
|
+
if (!dataMethods.has(methodStr)) {
|
|
417
|
+
warnings.push([
|
|
418
|
+
"data-not-posted",
|
|
419
|
+
"clj-http doesn't send data with " + methodStr + " requests",
|
|
420
|
+
]);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (request.headers.length) {
|
|
425
|
+
// TODO: clojure http supports duplicate headers, they don't need to be merged
|
|
426
|
+
params["headers"] = reprHeaders(request.headers, importLines);
|
|
427
|
+
} else {
|
|
428
|
+
delete params["headers"];
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if (request.compressed === false) {
|
|
432
|
+
params[":decompress-body"] = "false";
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
if (request.insecure) {
|
|
436
|
+
params["insecure?"] = "true";
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if (request.followRedirects === false) {
|
|
440
|
+
params["redirect-strategy"] = ":none";
|
|
441
|
+
} else if (request.followRedirects) {
|
|
442
|
+
if (request.followRedirectsTrusted) {
|
|
443
|
+
// TODO: is this right? Docs link 404's
|
|
444
|
+
params["redirect-strategy"] = ":lax";
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
if (request.maxRedirects) {
|
|
448
|
+
params["max-redirects"] = request.maxRedirects.toString();
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const times1000 = (n: Word, importLines: Set<string>) => {
|
|
452
|
+
if (n.isString()) {
|
|
453
|
+
// TODO: this throws away text after the number
|
|
454
|
+
const asFloat = parseFloat(n.toString());
|
|
455
|
+
if (!isNaN(asFloat)) {
|
|
456
|
+
return (asFloat * 1000).toString();
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return "(* (Float/parseFloat " + repr(n, importLines) + ") 1000)";
|
|
460
|
+
};
|
|
461
|
+
if (request.timeout) {
|
|
462
|
+
const timeout = times1000(request.timeout, importLines);
|
|
463
|
+
params["socket-timeout"] = timeout;
|
|
464
|
+
params["connection-timeout"] = timeout;
|
|
465
|
+
}
|
|
466
|
+
if (request.connectTimeout) {
|
|
467
|
+
params["connection-timeout"] = times1000(
|
|
468
|
+
request.connectTimeout,
|
|
469
|
+
importLines,
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
let code = "(" + fn;
|
|
474
|
+
|
|
475
|
+
if (fn === "client/request") {
|
|
476
|
+
params["url"] = repr(url, importLines);
|
|
477
|
+
} else {
|
|
478
|
+
code += " " + repr(url, importLines);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
const paramLines = [];
|
|
482
|
+
let param, value;
|
|
483
|
+
for ([param, value] of Object.entries(params)) {
|
|
484
|
+
let commentOut = false;
|
|
485
|
+
if (param === "commented-body") {
|
|
486
|
+
commentOut = true;
|
|
487
|
+
param = "body";
|
|
488
|
+
}
|
|
489
|
+
const key = ":" + param + " ";
|
|
490
|
+
let paramStr = key + indent(value, key.length);
|
|
491
|
+
if (commentOut) {
|
|
492
|
+
paramStr = paramStr.replace(/^/gm, ";; ");
|
|
493
|
+
}
|
|
494
|
+
paramLines.push(paramStr);
|
|
495
|
+
}
|
|
496
|
+
if (paramLines.length) {
|
|
497
|
+
let paramStart = code.length + 1;
|
|
498
|
+
if (code.length > 70) {
|
|
499
|
+
paramStart = 1 + fn.length + 1;
|
|
500
|
+
code += "\n" + " ".repeat(paramStart);
|
|
501
|
+
} else {
|
|
502
|
+
code += " ";
|
|
503
|
+
}
|
|
504
|
+
// +1 for the outer "{"
|
|
505
|
+
code += indent("{" + paramLines.join("\n") + "}", paramStart + 1);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
return [...importLines].sort().join("\n") + "\n\n" + code + ")\n";
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
export function toClojureWarn(
|
|
512
|
+
curlCommand: string | string[],
|
|
513
|
+
warnings: Warnings = [],
|
|
514
|
+
): [string, Warnings] {
|
|
515
|
+
const requests = parse(curlCommand, supportedArgs, warnings);
|
|
516
|
+
const clojure = _toClojure(requests, warnings);
|
|
517
|
+
return [clojure, warnings];
|
|
518
|
+
}
|
|
519
|
+
export function toClojure(curlCommand: string | string[]): string {
|
|
520
|
+
return toClojureWarn(curlCommand)[0];
|
|
521
|
+
}
|