@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,362 @@
|
|
|
1
|
+
import { Word, eq } from "../../shell/Word.js";
|
|
2
|
+
import { parse, getFirst, COMMON_SUPPORTED_ARGS } from "../../parse.js";
|
|
3
|
+
import type { Request, Warnings } from "../../parse.js";
|
|
4
|
+
import { parseQueryString } from "../../Query.js";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
reprStr,
|
|
8
|
+
repr,
|
|
9
|
+
reprAsStringToStringDict,
|
|
10
|
+
reprObj,
|
|
11
|
+
asParseFloatTimes1000,
|
|
12
|
+
asParseInt,
|
|
13
|
+
toURLSearchParams,
|
|
14
|
+
type JSImports,
|
|
15
|
+
addImport,
|
|
16
|
+
bySecondElem,
|
|
17
|
+
} from "./javascript.js";
|
|
18
|
+
|
|
19
|
+
export const supportedArgs = new Set([
|
|
20
|
+
...COMMON_SUPPORTED_ARGS,
|
|
21
|
+
|
|
22
|
+
"max-time",
|
|
23
|
+
"connect-timeout",
|
|
24
|
+
|
|
25
|
+
"location",
|
|
26
|
+
"no-location",
|
|
27
|
+
"location-trusted", // not exactly supported, just better warning message
|
|
28
|
+
"no-location-trusted",
|
|
29
|
+
"max-redirs",
|
|
30
|
+
|
|
31
|
+
"compressed",
|
|
32
|
+
|
|
33
|
+
"insecure",
|
|
34
|
+
|
|
35
|
+
"http2",
|
|
36
|
+
"http2-prior-knowledge",
|
|
37
|
+
|
|
38
|
+
"form",
|
|
39
|
+
"form-string",
|
|
40
|
+
|
|
41
|
+
// TODO:
|
|
42
|
+
// "cookie-jar", // and cookie files
|
|
43
|
+
// TODO: tls using https: and agent:
|
|
44
|
+
// TODO: proxy stuff using agent:
|
|
45
|
+
// TODO: methodRewriting: true to match curl?
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
function getBodyString(
|
|
49
|
+
request: Request,
|
|
50
|
+
imports: JSImports,
|
|
51
|
+
): [string | null, string | null] {
|
|
52
|
+
const contentType = request.headers.getContentType();
|
|
53
|
+
// can have things like ; charset=utf-8 which we want to preserve
|
|
54
|
+
const exactContentType = request.headers.get("content-type");
|
|
55
|
+
|
|
56
|
+
if (request.multipartUploads) {
|
|
57
|
+
if (eq(exactContentType, "multipart/form-data")) {
|
|
58
|
+
// TODO: comment it out instead?
|
|
59
|
+
request.headers.delete("content-type");
|
|
60
|
+
}
|
|
61
|
+
return ["body: form", null];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!request.data) {
|
|
65
|
+
return [null, null];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// TODO: @
|
|
69
|
+
const simpleString = "body: " + repr(request.data, imports);
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
if (contentType === "application/json" && request.data.isString()) {
|
|
73
|
+
const dataStr = request.data.toString();
|
|
74
|
+
const parsed = JSON.parse(dataStr);
|
|
75
|
+
const roundtrips = JSON.stringify(parsed) === dataStr;
|
|
76
|
+
const jsonAsJavaScript = "json: " + reprObj(parsed, 1);
|
|
77
|
+
if (roundtrips && eq(exactContentType, "application/json")) {
|
|
78
|
+
request.headers.delete("content-type");
|
|
79
|
+
}
|
|
80
|
+
return [jsonAsJavaScript, roundtrips ? null : simpleString];
|
|
81
|
+
}
|
|
82
|
+
if (contentType === "application/x-www-form-urlencoded") {
|
|
83
|
+
const [queryList, queryDict] = parseQueryString(request.data);
|
|
84
|
+
if (queryDict && queryDict.every((v) => !Array.isArray(v[1]))) {
|
|
85
|
+
if (eq(exactContentType, "application/x-www-form-urlencoded")) {
|
|
86
|
+
request.headers.delete("content-type");
|
|
87
|
+
}
|
|
88
|
+
return [
|
|
89
|
+
"form: " +
|
|
90
|
+
reprAsStringToStringDict(queryDict as [Word, Word][], 1, imports),
|
|
91
|
+
null,
|
|
92
|
+
];
|
|
93
|
+
}
|
|
94
|
+
if (queryList) {
|
|
95
|
+
return [
|
|
96
|
+
"body: " +
|
|
97
|
+
toURLSearchParams([queryList, null], imports) +
|
|
98
|
+
".toString()",
|
|
99
|
+
null,
|
|
100
|
+
];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
} catch {}
|
|
104
|
+
|
|
105
|
+
return [simpleString, null];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function buildOptionsObject(
|
|
109
|
+
request: Request,
|
|
110
|
+
method: Word,
|
|
111
|
+
methodStr: string,
|
|
112
|
+
methods: string[],
|
|
113
|
+
nonDataMethods: string[],
|
|
114
|
+
warnings: Warnings,
|
|
115
|
+
imports: JSImports,
|
|
116
|
+
): string {
|
|
117
|
+
let code = "{\n";
|
|
118
|
+
|
|
119
|
+
if (!method.isString || !methods.includes(methodStr.toUpperCase())) {
|
|
120
|
+
code += " method: " + repr(method, imports) + ",\n";
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (
|
|
124
|
+
request.urls[0].queryDict &&
|
|
125
|
+
request.urls[0].queryDict.every((v) => !Array.isArray(v[1]))
|
|
126
|
+
) {
|
|
127
|
+
code +=
|
|
128
|
+
" searchParams: " +
|
|
129
|
+
reprAsStringToStringDict(
|
|
130
|
+
request.urls[0].queryDict as [Word, Word][],
|
|
131
|
+
1,
|
|
132
|
+
imports,
|
|
133
|
+
) +
|
|
134
|
+
",\n";
|
|
135
|
+
} else if (request.urls[0].queryList) {
|
|
136
|
+
code +=
|
|
137
|
+
" searchParams: " +
|
|
138
|
+
toURLSearchParams([request.urls[0].queryList, null], imports) +
|
|
139
|
+
",\n";
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const [bodyString, commentedOutBodyString] = getBodyString(request, imports); // can delete headers
|
|
143
|
+
|
|
144
|
+
if (request.headers.length) {
|
|
145
|
+
const headers = request.headers.headers.filter((h) => h[1] !== null) as [
|
|
146
|
+
Word,
|
|
147
|
+
Word,
|
|
148
|
+
][];
|
|
149
|
+
if (headers.length) {
|
|
150
|
+
code +=
|
|
151
|
+
" headers: " + reprAsStringToStringDict(headers, 1, imports) + ",\n";
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (request.urls[0].auth) {
|
|
156
|
+
const [username, password] = request.urls[0].auth;
|
|
157
|
+
code += " username: " + repr(username, imports) + ",\n";
|
|
158
|
+
if (password.toBool()) {
|
|
159
|
+
code += " password: " + repr(password, imports) + ",\n";
|
|
160
|
+
}
|
|
161
|
+
if (request.authType !== "basic") {
|
|
162
|
+
// TODO: warn
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (request.data || request.multipartUploads) {
|
|
167
|
+
if (commentedOutBodyString) {
|
|
168
|
+
code += " // " + commentedOutBodyString + ",\n";
|
|
169
|
+
}
|
|
170
|
+
code += " " + bodyString + ",\n";
|
|
171
|
+
|
|
172
|
+
// TODO: Does this work for HEAD?
|
|
173
|
+
if (nonDataMethods.includes(methodStr.toUpperCase())) {
|
|
174
|
+
code += " allowGetBody: true,\n";
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (request.timeout || request.connectTimeout) {
|
|
179
|
+
code += " timeout: {\n";
|
|
180
|
+
if (request.timeout) {
|
|
181
|
+
code +=
|
|
182
|
+
" request: " +
|
|
183
|
+
asParseFloatTimes1000(request.timeout, imports) +
|
|
184
|
+
",\n";
|
|
185
|
+
}
|
|
186
|
+
if (request.connectTimeout) {
|
|
187
|
+
code +=
|
|
188
|
+
" connect: " +
|
|
189
|
+
asParseFloatTimes1000(request.connectTimeout, imports) +
|
|
190
|
+
",\n";
|
|
191
|
+
}
|
|
192
|
+
if (code.endsWith(",\n")) {
|
|
193
|
+
code = code.slice(0, -2);
|
|
194
|
+
code += "\n";
|
|
195
|
+
}
|
|
196
|
+
code += " },\n";
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// By default, curl doesn't follow redirects but got does.
|
|
200
|
+
let followRedirects = request.followRedirects;
|
|
201
|
+
if (followRedirects === undefined) {
|
|
202
|
+
followRedirects = true;
|
|
203
|
+
}
|
|
204
|
+
let maxRedirects = request.maxRedirects
|
|
205
|
+
? asParseInt(request.maxRedirects, imports)
|
|
206
|
+
: null;
|
|
207
|
+
const hasMaxRedirects =
|
|
208
|
+
followRedirects &&
|
|
209
|
+
maxRedirects &&
|
|
210
|
+
maxRedirects !== "0" &&
|
|
211
|
+
maxRedirects !== "10"; // got default
|
|
212
|
+
if (!followRedirects || maxRedirects === "0") {
|
|
213
|
+
code += " followRedirect: false,\n";
|
|
214
|
+
} else if (maxRedirects) {
|
|
215
|
+
if (maxRedirects === "-1") {
|
|
216
|
+
maxRedirects = "Infinity";
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (followRedirects && request.followRedirectsTrusted) {
|
|
220
|
+
warnings.push([
|
|
221
|
+
"--location-trusted",
|
|
222
|
+
// TODO: is this true?
|
|
223
|
+
"got doesn't have an easy way to disable removing the Authorization: header on redirect",
|
|
224
|
+
]);
|
|
225
|
+
}
|
|
226
|
+
if (hasMaxRedirects) {
|
|
227
|
+
code += " maxRedirects: " + maxRedirects + ",\n";
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (request.compressed === false) {
|
|
231
|
+
code += " decompress: false,\n";
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (request.insecure) {
|
|
235
|
+
code += " https: {\n";
|
|
236
|
+
code += " rejectUnauthorized: false\n";
|
|
237
|
+
code += " },\n";
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (request.http2) {
|
|
241
|
+
code += " http2: true,\n";
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (code.endsWith(",\n")) {
|
|
245
|
+
code = code.slice(0, -2);
|
|
246
|
+
}
|
|
247
|
+
code += "\n}";
|
|
248
|
+
return code;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function _toNodeGot(
|
|
252
|
+
requests: Request[],
|
|
253
|
+
warnings: Warnings = [],
|
|
254
|
+
): string {
|
|
255
|
+
const request = getFirst(requests, warnings);
|
|
256
|
+
|
|
257
|
+
const imports: JSImports = [];
|
|
258
|
+
|
|
259
|
+
let code = "";
|
|
260
|
+
|
|
261
|
+
if (request.multipartUploads) {
|
|
262
|
+
code += "const form = new FormData();\n";
|
|
263
|
+
for (const m of request.multipartUploads) {
|
|
264
|
+
code += "form.append(" + repr(m.name, imports) + ", ";
|
|
265
|
+
if ("contentFile" in m) {
|
|
266
|
+
addImport(imports, "* as fs", "fs");
|
|
267
|
+
if (eq(m.contentFile, "-")) {
|
|
268
|
+
code += "fs.readFileSync(0).toString()";
|
|
269
|
+
} else {
|
|
270
|
+
code += "fs.readFileSync(" + repr(m.contentFile, imports) + ")";
|
|
271
|
+
}
|
|
272
|
+
if ("filename" in m && m.filename) {
|
|
273
|
+
code += ", " + repr(m.filename, imports);
|
|
274
|
+
}
|
|
275
|
+
} else {
|
|
276
|
+
code += repr(m.content, imports);
|
|
277
|
+
}
|
|
278
|
+
code += ");\n";
|
|
279
|
+
}
|
|
280
|
+
code += "\n";
|
|
281
|
+
|
|
282
|
+
// TODO: remove warning once Node 16 is EOL'd on 2023-09-11
|
|
283
|
+
warnings.push(["node-form-data", "Node 18 is required for FormData"]);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const method = request.urls[0].method;
|
|
287
|
+
const methodStr = method.toString();
|
|
288
|
+
if (method.isString() && methodStr !== methodStr.toUpperCase()) {
|
|
289
|
+
warnings.push([
|
|
290
|
+
"lowercase-method",
|
|
291
|
+
"got will uppercase the method: " + JSON.stringify(methodStr),
|
|
292
|
+
]);
|
|
293
|
+
}
|
|
294
|
+
// https://github.com/sindresorhus/got/blob/e24b89669931b36530219b9f49965d07da25a7e6/source/create.ts#L28
|
|
295
|
+
const methods = ["GET", "POST", "PUT", "PATCH", "HEAD", "DELETE"];
|
|
296
|
+
// Got will error if you try sending data with these HTTP methods
|
|
297
|
+
const nonDataMethods = ["GET", "HEAD"];
|
|
298
|
+
code += "const response = await got";
|
|
299
|
+
if (
|
|
300
|
+
method.isString() &&
|
|
301
|
+
methods.includes(methodStr.toUpperCase()) &&
|
|
302
|
+
methodStr.toUpperCase() !== "GET"
|
|
303
|
+
) {
|
|
304
|
+
code += "." + methodStr.toLowerCase();
|
|
305
|
+
}
|
|
306
|
+
code += "(";
|
|
307
|
+
|
|
308
|
+
const url = request.urls[0].queryList
|
|
309
|
+
? request.urls[0].urlWithoutQueryList
|
|
310
|
+
: request.urls[0].url;
|
|
311
|
+
code += repr(url, imports);
|
|
312
|
+
|
|
313
|
+
const needsOptions = !!(
|
|
314
|
+
!method.isString() ||
|
|
315
|
+
!methods.includes(methodStr.toUpperCase()) ||
|
|
316
|
+
request.urls[0].queryList ||
|
|
317
|
+
request.urls[0].queryDict ||
|
|
318
|
+
request.headers.length ||
|
|
319
|
+
request.urls[0].auth ||
|
|
320
|
+
request.multipartUploads ||
|
|
321
|
+
request.data ||
|
|
322
|
+
request.followRedirects ||
|
|
323
|
+
request.maxRedirects ||
|
|
324
|
+
request.compressed ||
|
|
325
|
+
request.insecure ||
|
|
326
|
+
request.http2 ||
|
|
327
|
+
request.timeout ||
|
|
328
|
+
request.connectTimeout
|
|
329
|
+
);
|
|
330
|
+
if (needsOptions) {
|
|
331
|
+
code += ", ";
|
|
332
|
+
code += buildOptionsObject(
|
|
333
|
+
request,
|
|
334
|
+
method,
|
|
335
|
+
methodStr,
|
|
336
|
+
methods,
|
|
337
|
+
nonDataMethods,
|
|
338
|
+
warnings,
|
|
339
|
+
imports,
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
code += ");\n";
|
|
344
|
+
|
|
345
|
+
let importCode = "import got from 'got';\n";
|
|
346
|
+
for (const [varName, imp] of Array.from(imports).sort(bySecondElem)) {
|
|
347
|
+
importCode += "import " + varName + " from " + reprStr(imp) + ";\n";
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return importCode + "\n" + code;
|
|
351
|
+
}
|
|
352
|
+
export function toNodeGotWarn(
|
|
353
|
+
curlCommand: string | string[],
|
|
354
|
+
warnings: Warnings = [],
|
|
355
|
+
): [string, Warnings] {
|
|
356
|
+
const requests = parse(curlCommand, supportedArgs, warnings);
|
|
357
|
+
const nodeGot = _toNodeGot(requests, warnings);
|
|
358
|
+
return [nodeGot, warnings];
|
|
359
|
+
}
|
|
360
|
+
export function toNodeGot(curlCommand: string | string[]): string {
|
|
361
|
+
return toNodeGotWarn(curlCommand)[0];
|
|
362
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { Word, eq, mergeWords, joinWords } from "../../shell/Word.js";
|
|
2
|
+
import { parse, getFirst, COMMON_SUPPORTED_ARGS } from "../../parse.js";
|
|
3
|
+
import type { Request, Warnings } from "../../parse.js";
|
|
4
|
+
import { parseQueryString } from "../../Query.js";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
repr,
|
|
8
|
+
reprObj,
|
|
9
|
+
asParseFloatTimes1000,
|
|
10
|
+
toURLSearchParams,
|
|
11
|
+
type JSImports,
|
|
12
|
+
reprImports,
|
|
13
|
+
} from "./javascript.js";
|
|
14
|
+
|
|
15
|
+
import { dedent, getFormString } from "./jquery.js";
|
|
16
|
+
|
|
17
|
+
export const supportedArgs = new Set([
|
|
18
|
+
...COMMON_SUPPORTED_ARGS,
|
|
19
|
+
"form",
|
|
20
|
+
"form-string",
|
|
21
|
+
"max-time",
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
// TODO: @
|
|
25
|
+
function _getDataString(
|
|
26
|
+
data: Word,
|
|
27
|
+
contentType: string | null | undefined,
|
|
28
|
+
imports: JSImports,
|
|
29
|
+
): [string, string | null] {
|
|
30
|
+
const originalStringRepr = repr(data, imports);
|
|
31
|
+
|
|
32
|
+
if (contentType === "application/json" && data.isString()) {
|
|
33
|
+
const dataStr = data.toString();
|
|
34
|
+
const parsed = JSON.parse(dataStr);
|
|
35
|
+
// Only format arrays and {} as JavaScript objects
|
|
36
|
+
if (typeof parsed !== "object" || parsed === null) {
|
|
37
|
+
return [originalStringRepr, null];
|
|
38
|
+
}
|
|
39
|
+
const roundtrips = JSON.stringify(parsed) === dataStr;
|
|
40
|
+
const jsonAsJavaScript = "JSON.stringify(" + reprObj(parsed, 1) + ")";
|
|
41
|
+
return [jsonAsJavaScript, roundtrips ? null : originalStringRepr];
|
|
42
|
+
}
|
|
43
|
+
if (contentType === "application/x-www-form-urlencoded") {
|
|
44
|
+
const query = parseQueryString(data);
|
|
45
|
+
if (query[0]) {
|
|
46
|
+
// if (
|
|
47
|
+
// eq(exactContentType, "application/x-www-form-urlencoded; charset=utf-8")
|
|
48
|
+
// ) {
|
|
49
|
+
// exactContentType = null;
|
|
50
|
+
// }
|
|
51
|
+
// TODO: check roundtrip, add a comment
|
|
52
|
+
return [toURLSearchParams(query, imports) + ".toString()", null];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return [originalStringRepr, null];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function getDataString(
|
|
59
|
+
data: Word,
|
|
60
|
+
contentType: string | null | undefined,
|
|
61
|
+
imports: JSImports,
|
|
62
|
+
): [string, string | null] {
|
|
63
|
+
let dataString: string | null = null;
|
|
64
|
+
let commentedOutDataString: string | null = null;
|
|
65
|
+
try {
|
|
66
|
+
[dataString, commentedOutDataString] = _getDataString(
|
|
67
|
+
data,
|
|
68
|
+
contentType,
|
|
69
|
+
imports,
|
|
70
|
+
);
|
|
71
|
+
} catch {}
|
|
72
|
+
if (!dataString) {
|
|
73
|
+
dataString = repr(data, imports);
|
|
74
|
+
}
|
|
75
|
+
return [dataString, commentedOutDataString];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function _toNodeHttp(
|
|
79
|
+
requests: Request[],
|
|
80
|
+
warnings: Warnings = [],
|
|
81
|
+
): string {
|
|
82
|
+
const request = getFirst(requests, warnings);
|
|
83
|
+
const imports: JSImports = [];
|
|
84
|
+
|
|
85
|
+
let importCode = "";
|
|
86
|
+
let code = "";
|
|
87
|
+
let options = "";
|
|
88
|
+
|
|
89
|
+
const method = request.urls[0].method;
|
|
90
|
+
if (!eq(method, "GET")) {
|
|
91
|
+
options += " method: " + repr(method, imports) + ",\n";
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (!eq(request.urls[0].method.toUpperCase(), method)) {
|
|
95
|
+
warnings.push([
|
|
96
|
+
"method-case",
|
|
97
|
+
"http uppercases the method, so it will be changed to " +
|
|
98
|
+
JSON.stringify(method.toUpperCase().toString()),
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const url = request.urls[0].url;
|
|
103
|
+
|
|
104
|
+
let dataString, commentedOutDataString;
|
|
105
|
+
let formString;
|
|
106
|
+
const contentType = request.headers.getContentType();
|
|
107
|
+
if (request.multipartUploads) {
|
|
108
|
+
formString = getFormString(request.multipartUploads, imports);
|
|
109
|
+
code += formString;
|
|
110
|
+
// Node 18's native FormData doesn't have .pipe() or .getHeaders()
|
|
111
|
+
importCode += "import FormData from 'form-data';\n";
|
|
112
|
+
} else if (request.data) {
|
|
113
|
+
[dataString, commentedOutDataString] = getDataString(
|
|
114
|
+
request.data,
|
|
115
|
+
contentType,
|
|
116
|
+
imports,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (request.urls[0].auth) {
|
|
121
|
+
const [username, password] = request.urls[0].auth;
|
|
122
|
+
options +=
|
|
123
|
+
" auth: " + repr(joinWords([username, password], ":"), imports) + ",\n";
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// TODO: warn about unsent headers
|
|
127
|
+
if (request.headers.length) {
|
|
128
|
+
options += " headers: {\n";
|
|
129
|
+
for (const [key, value] of request.headers) {
|
|
130
|
+
if (value === null) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
options +=
|
|
134
|
+
" " + repr(key, imports) + ": " + repr(value, imports) + ",\n";
|
|
135
|
+
}
|
|
136
|
+
if (formString) {
|
|
137
|
+
options += " ...form.getHeaders(),\n";
|
|
138
|
+
}
|
|
139
|
+
if (options.endsWith(",\n")) {
|
|
140
|
+
options = options.slice(0, -2);
|
|
141
|
+
options += "\n";
|
|
142
|
+
}
|
|
143
|
+
options += " },\n";
|
|
144
|
+
} else if (formString) {
|
|
145
|
+
options += " headers: form.getHeaders(),\n";
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (request.timeout) {
|
|
149
|
+
options +=
|
|
150
|
+
" timeout: " + asParseFloatTimes1000(request.timeout, imports) + ",\n";
|
|
151
|
+
// TODO: warn about differences from curl
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (options.endsWith(",\n")) {
|
|
155
|
+
options = options.slice(0, -2);
|
|
156
|
+
options += "\n";
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const urlObj = request.urls[0].urlObj;
|
|
160
|
+
let optArg = repr(url, imports);
|
|
161
|
+
if (options) {
|
|
162
|
+
code += "const options = {\n";
|
|
163
|
+
code += " hostname: " + repr(urlObj.host, imports) + ",\n";
|
|
164
|
+
if (urlObj.host.includes(":")) {
|
|
165
|
+
// TODO
|
|
166
|
+
// code += " port: " + repr(request.urls[0].urlObj.port, imports) + ",\n";
|
|
167
|
+
warnings.push([
|
|
168
|
+
"node-http-port",
|
|
169
|
+
"Parsing the port out of the hostname is not supported. If you get an ENOTFOUND error, you'll need to do it manually",
|
|
170
|
+
]);
|
|
171
|
+
}
|
|
172
|
+
const path = mergeWords(urlObj.path, urlObj.query);
|
|
173
|
+
if (path.toBool()) {
|
|
174
|
+
code += " path: " + repr(path, imports) + ",\n";
|
|
175
|
+
}
|
|
176
|
+
// code += " protocol: " + repr(urlObj.scheme, imports) + ",\n";
|
|
177
|
+
code += options;
|
|
178
|
+
code += "};\n";
|
|
179
|
+
code += "\n";
|
|
180
|
+
|
|
181
|
+
optArg = "options";
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const module = urlObj.scheme.toString() === "https" ? "https" : "http";
|
|
185
|
+
const fn =
|
|
186
|
+
eq(method, "GET") && !dataString && !commentedOutDataString && !formString
|
|
187
|
+
? "get"
|
|
188
|
+
: "request";
|
|
189
|
+
code +=
|
|
190
|
+
"const req = " + module + "." + fn + "(" + optArg + ", function (res) {\n";
|
|
191
|
+
code += " const chunks = [];\n";
|
|
192
|
+
code += "\n";
|
|
193
|
+
code += " res.on('data', function (chunk) {\n";
|
|
194
|
+
code += " chunks.push(chunk);\n";
|
|
195
|
+
code += " });\n";
|
|
196
|
+
code += "\n";
|
|
197
|
+
code += " res.on('end', function () {\n";
|
|
198
|
+
code += " const body = Buffer.concat(chunks);\n";
|
|
199
|
+
code += " console.log(body.toString());\n";
|
|
200
|
+
code += " });\n";
|
|
201
|
+
code += "});\n";
|
|
202
|
+
|
|
203
|
+
if (commentedOutDataString) {
|
|
204
|
+
code += "\n// req.write(" + commentedOutDataString + ");";
|
|
205
|
+
}
|
|
206
|
+
if (dataString) {
|
|
207
|
+
code += "\nreq.write(" + dedent(dataString) + ");\n";
|
|
208
|
+
} else if (formString) {
|
|
209
|
+
code += "\nform.pipe(req);\n";
|
|
210
|
+
}
|
|
211
|
+
if (fn !== "get" && !formString) {
|
|
212
|
+
code += "req.end();\n";
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
importCode = "import " + module + " from '" + module + "';\n" + importCode;
|
|
216
|
+
importCode += reprImports(imports);
|
|
217
|
+
importCode += "\n";
|
|
218
|
+
|
|
219
|
+
return importCode + code;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function toNodeHttpWarn(
|
|
223
|
+
curlCommand: string | string[],
|
|
224
|
+
warnings: Warnings = [],
|
|
225
|
+
): [string, Warnings] {
|
|
226
|
+
const requests = parse(curlCommand, supportedArgs, warnings);
|
|
227
|
+
const code = _toNodeHttp(requests, warnings);
|
|
228
|
+
return [code, warnings];
|
|
229
|
+
}
|
|
230
|
+
export function toNodeHttp(curlCommand: string | string[]): string {
|
|
231
|
+
return toNodeHttpWarn(curlCommand)[0];
|
|
232
|
+
}
|