@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.
Files changed (270) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +153 -0
  3. package/dist/src/Headers.d.ts +23 -0
  4. package/dist/src/Headers.js +287 -0
  5. package/dist/src/Headers.js.map +1 -0
  6. package/dist/src/Query.d.ts +8 -0
  7. package/dist/src/Query.js +136 -0
  8. package/dist/src/Query.js.map +1 -0
  9. package/dist/src/Request.d.ts +200 -0
  10. package/dist/src/Request.js +1191 -0
  11. package/dist/src/Request.js.map +1 -0
  12. package/dist/src/Warnings.d.ts +15 -0
  13. package/dist/src/Warnings.js +111 -0
  14. package/dist/src/Warnings.js.map +1 -0
  15. package/dist/src/cli.d.ts +2 -0
  16. package/dist/src/cli.js +333 -0
  17. package/dist/src/cli.js.map +1 -0
  18. package/dist/src/curl/auth.d.ts +12 -0
  19. package/dist/src/curl/auth.js +40 -0
  20. package/dist/src/curl/auth.js.map +1 -0
  21. package/dist/src/curl/form.d.ts +28 -0
  22. package/dist/src/curl/form.js +218 -0
  23. package/dist/src/curl/form.js.map +1 -0
  24. package/dist/src/curl/opts.d.ts +2052 -0
  25. package/dist/src/curl/opts.js +1000 -0
  26. package/dist/src/curl/opts.js.map +1 -0
  27. package/dist/src/curl/url.d.ts +14 -0
  28. package/dist/src/curl/url.js +218 -0
  29. package/dist/src/curl/url.js.map +1 -0
  30. package/dist/src/generators/ansible.d.ts +5 -0
  31. package/dist/src/generators/ansible.js +282 -0
  32. package/dist/src/generators/ansible.js.map +1 -0
  33. package/dist/src/generators/c.d.ts +12 -0
  34. package/dist/src/generators/c.js +1230 -0
  35. package/dist/src/generators/c.js.map +1 -0
  36. package/dist/src/generators/cfml.d.ts +5 -0
  37. package/dist/src/generators/cfml.js +134 -0
  38. package/dist/src/generators/cfml.js.map +1 -0
  39. package/dist/src/generators/clojure.d.ts +7 -0
  40. package/dist/src/generators/clojure.js +436 -0
  41. package/dist/src/generators/clojure.js.map +1 -0
  42. package/dist/src/generators/csharp.d.ts +8 -0
  43. package/dist/src/generators/csharp.js +356 -0
  44. package/dist/src/generators/csharp.js.map +1 -0
  45. package/dist/src/generators/dart.d.ts +5 -0
  46. package/dist/src/generators/dart.js +273 -0
  47. package/dist/src/generators/dart.js.map +1 -0
  48. package/dist/src/generators/elixir.d.ts +8 -0
  49. package/dist/src/generators/elixir.js +296 -0
  50. package/dist/src/generators/elixir.js.map +1 -0
  51. package/dist/src/generators/go.d.ts +5 -0
  52. package/dist/src/generators/go.js +228 -0
  53. package/dist/src/generators/go.js.map +1 -0
  54. package/dist/src/generators/har.d.ts +7 -0
  55. package/dist/src/generators/har.js +115 -0
  56. package/dist/src/generators/har.js.map +1 -0
  57. package/dist/src/generators/http.d.ts +7 -0
  58. package/dist/src/generators/http.js +165 -0
  59. package/dist/src/generators/http.js.map +1 -0
  60. package/dist/src/generators/httpie.d.ts +5 -0
  61. package/dist/src/generators/httpie.js +446 -0
  62. package/dist/src/generators/httpie.js.map +1 -0
  63. package/dist/src/generators/java/httpurlconnection.d.ts +5 -0
  64. package/dist/src/generators/java/httpurlconnection.js +123 -0
  65. package/dist/src/generators/java/httpurlconnection.js.map +1 -0
  66. package/dist/src/generators/java/java.d.ts +8 -0
  67. package/dist/src/generators/java/java.js +247 -0
  68. package/dist/src/generators/java/java.js.map +1 -0
  69. package/dist/src/generators/java/jsoup.d.ts +5 -0
  70. package/dist/src/generators/java/jsoup.js +152 -0
  71. package/dist/src/generators/java/jsoup.js.map +1 -0
  72. package/dist/src/generators/java/okhttp.d.ts +5 -0
  73. package/dist/src/generators/java/okhttp.js +232 -0
  74. package/dist/src/generators/java/okhttp.js.map +1 -0
  75. package/dist/src/generators/javascript/axios.d.ts +5 -0
  76. package/dist/src/generators/javascript/axios.js +332 -0
  77. package/dist/src/generators/javascript/axios.js.map +1 -0
  78. package/dist/src/generators/javascript/got.d.ts +5 -0
  79. package/dist/src/generators/javascript/got.js +277 -0
  80. package/dist/src/generators/javascript/got.js.map +1 -0
  81. package/dist/src/generators/javascript/http.d.ts +8 -0
  82. package/dist/src/generators/javascript/http.js +183 -0
  83. package/dist/src/generators/javascript/http.js.map +1 -0
  84. package/dist/src/generators/javascript/javascript.d.ts +37 -0
  85. package/dist/src/generators/javascript/javascript.js +676 -0
  86. package/dist/src/generators/javascript/javascript.js.map +1 -0
  87. package/dist/src/generators/javascript/jquery.d.ts +15 -0
  88. package/dist/src/generators/javascript/jquery.js +297 -0
  89. package/dist/src/generators/javascript/jquery.js.map +1 -0
  90. package/dist/src/generators/javascript/ky.d.ts +7 -0
  91. package/dist/src/generators/javascript/ky.js +245 -0
  92. package/dist/src/generators/javascript/ky.js.map +1 -0
  93. package/dist/src/generators/javascript/request.d.ts +5 -0
  94. package/dist/src/generators/javascript/request.js +113 -0
  95. package/dist/src/generators/javascript/request.js.map +1 -0
  96. package/dist/src/generators/javascript/superagent.d.ts +10 -0
  97. package/dist/src/generators/javascript/superagent.js +301 -0
  98. package/dist/src/generators/javascript/superagent.js.map +1 -0
  99. package/dist/src/generators/javascript/xhr.d.ts +8 -0
  100. package/dist/src/generators/javascript/xhr.js +147 -0
  101. package/dist/src/generators/javascript/xhr.js.map +1 -0
  102. package/dist/src/generators/json.d.ts +45 -0
  103. package/dist/src/generators/json.js +197 -0
  104. package/dist/src/generators/json.js.map +1 -0
  105. package/dist/src/generators/julia.d.ts +8 -0
  106. package/dist/src/generators/julia.js +365 -0
  107. package/dist/src/generators/julia.js.map +1 -0
  108. package/dist/src/generators/kotlin.d.ts +8 -0
  109. package/dist/src/generators/kotlin.js +325 -0
  110. package/dist/src/generators/kotlin.js.map +1 -0
  111. package/dist/src/generators/lua.d.ts +5 -0
  112. package/dist/src/generators/lua.js +192 -0
  113. package/dist/src/generators/lua.js.map +1 -0
  114. package/dist/src/generators/matlab/common.d.ts +16 -0
  115. package/dist/src/generators/matlab/common.js +238 -0
  116. package/dist/src/generators/matlab/common.js.map +1 -0
  117. package/dist/src/generators/matlab/httpinterface.d.ts +2 -0
  118. package/dist/src/generators/matlab/httpinterface.js +211 -0
  119. package/dist/src/generators/matlab/httpinterface.js.map +1 -0
  120. package/dist/src/generators/matlab/matlab.d.ts +5 -0
  121. package/dist/src/generators/matlab/matlab.js +29 -0
  122. package/dist/src/generators/matlab/matlab.js.map +1 -0
  123. package/dist/src/generators/matlab/webservices.d.ts +2 -0
  124. package/dist/src/generators/matlab/webservices.js +191 -0
  125. package/dist/src/generators/matlab/webservices.js.map +1 -0
  126. package/dist/src/generators/objectivec.d.ts +8 -0
  127. package/dist/src/generators/objectivec.js +269 -0
  128. package/dist/src/generators/objectivec.js.map +1 -0
  129. package/dist/src/generators/ocaml.d.ts +8 -0
  130. package/dist/src/generators/ocaml.js +150 -0
  131. package/dist/src/generators/ocaml.js.map +1 -0
  132. package/dist/src/generators/perl.d.ts +9 -0
  133. package/dist/src/generators/perl.js +209 -0
  134. package/dist/src/generators/perl.js.map +1 -0
  135. package/dist/src/generators/php/guzzle.d.ts +5 -0
  136. package/dist/src/generators/php/guzzle.js +328 -0
  137. package/dist/src/generators/php/guzzle.js.map +1 -0
  138. package/dist/src/generators/php/php.d.ts +8 -0
  139. package/dist/src/generators/php/php.js +217 -0
  140. package/dist/src/generators/php/php.js.map +1 -0
  141. package/dist/src/generators/php/requests.d.ts +5 -0
  142. package/dist/src/generators/php/requests.js +89 -0
  143. package/dist/src/generators/php/requests.js.map +1 -0
  144. package/dist/src/generators/powershell.d.ts +11 -0
  145. package/dist/src/generators/powershell.js +377 -0
  146. package/dist/src/generators/powershell.js.map +1 -0
  147. package/dist/src/generators/python/http.d.ts +5 -0
  148. package/dist/src/generators/python/http.js +128 -0
  149. package/dist/src/generators/python/http.js.map +1 -0
  150. package/dist/src/generators/python/python.d.ts +22 -0
  151. package/dist/src/generators/python/python.js +1693 -0
  152. package/dist/src/generators/python/python.js.map +1 -0
  153. package/dist/src/generators/r/httr.d.ts +10 -0
  154. package/dist/src/generators/r/httr.js +298 -0
  155. package/dist/src/generators/r/httr.js.map +1 -0
  156. package/dist/src/generators/r/httr2.d.ts +5 -0
  157. package/dist/src/generators/r/httr2.js +214 -0
  158. package/dist/src/generators/r/httr2.js.map +1 -0
  159. package/dist/src/generators/ruby/httparty.d.ts +6 -0
  160. package/dist/src/generators/ruby/httparty.js +308 -0
  161. package/dist/src/generators/ruby/httparty.js.map +1 -0
  162. package/dist/src/generators/ruby/ruby.d.ts +14 -0
  163. package/dist/src/generators/ruby/ruby.js +515 -0
  164. package/dist/src/generators/ruby/ruby.js.map +1 -0
  165. package/dist/src/generators/rust.d.ts +8 -0
  166. package/dist/src/generators/rust.js +177 -0
  167. package/dist/src/generators/rust.js.map +1 -0
  168. package/dist/src/generators/swift.d.ts +8 -0
  169. package/dist/src/generators/swift.js +219 -0
  170. package/dist/src/generators/swift.js.map +1 -0
  171. package/dist/src/generators/wget.d.ts +8 -0
  172. package/dist/src/generators/wget.js +472 -0
  173. package/dist/src/generators/wget.js.map +1 -0
  174. package/dist/src/index.d.ts +53 -0
  175. package/dist/src/index.js +54 -0
  176. package/dist/src/index.js.map +1 -0
  177. package/dist/src/parse.d.ts +13 -0
  178. package/dist/src/parse.js +43 -0
  179. package/dist/src/parse.js.map +1 -0
  180. package/dist/src/shell/Parser.d.ts +4 -0
  181. package/dist/src/shell/Parser.js +6 -0
  182. package/dist/src/shell/Parser.js.map +1 -0
  183. package/dist/src/shell/Word.d.ts +48 -0
  184. package/dist/src/shell/Word.js +486 -0
  185. package/dist/src/shell/Word.js.map +1 -0
  186. package/dist/src/shell/tokenizer.d.ts +3 -0
  187. package/dist/src/shell/tokenizer.js +610 -0
  188. package/dist/src/shell/tokenizer.js.map +1 -0
  189. package/dist/src/shell/webParser.d.ts +4 -0
  190. package/dist/src/shell/webParser.js +15 -0
  191. package/dist/src/shell/webParser.js.map +1 -0
  192. package/dist/src/utils.d.ts +6 -0
  193. package/dist/src/utils.js +21 -0
  194. package/dist/src/utils.js.map +1 -0
  195. package/dist/tools/compare-requests.d.ts +2 -0
  196. package/dist/tools/compare-requests.js +448 -0
  197. package/dist/tools/compare-requests.js.map +1 -0
  198. package/dist/tools/gen-test.d.ts +2 -0
  199. package/dist/tools/gen-test.js +114 -0
  200. package/dist/tools/gen-test.js.map +1 -0
  201. package/dist/tree-sitter-bash.wasm +0 -0
  202. package/index.d.ts +3 -0
  203. package/package.json +97 -0
  204. package/src/Headers.ts +334 -0
  205. package/src/Query.ts +147 -0
  206. package/src/Request.ts +1649 -0
  207. package/src/Warnings.ts +154 -0
  208. package/src/cli.ts +561 -0
  209. package/src/curl/auth.ts +45 -0
  210. package/src/curl/form.ts +297 -0
  211. package/src/curl/opts.ts +1417 -0
  212. package/src/curl/url.ts +241 -0
  213. package/src/generators/ansible.ts +364 -0
  214. package/src/generators/c.ts +1371 -0
  215. package/src/generators/cfml.ts +155 -0
  216. package/src/generators/clojure.ts +521 -0
  217. package/src/generators/csharp.ts +383 -0
  218. package/src/generators/dart.ts +293 -0
  219. package/src/generators/elixir.ts +349 -0
  220. package/src/generators/go.ts +269 -0
  221. package/src/generators/har.ts +147 -0
  222. package/src/generators/http.ts +194 -0
  223. package/src/generators/httpie.ts +506 -0
  224. package/src/generators/java/httpurlconnection.ts +151 -0
  225. package/src/generators/java/java.ts +280 -0
  226. package/src/generators/java/jsoup.ts +172 -0
  227. package/src/generators/java/okhttp.ts +257 -0
  228. package/src/generators/javascript/axios.ts +395 -0
  229. package/src/generators/javascript/got.ts +362 -0
  230. package/src/generators/javascript/http.ts +232 -0
  231. package/src/generators/javascript/javascript.ts +825 -0
  232. package/src/generators/javascript/jquery.ts +360 -0
  233. package/src/generators/javascript/ky.ts +315 -0
  234. package/src/generators/javascript/request.ts +152 -0
  235. package/src/generators/javascript/superagent.ts +373 -0
  236. package/src/generators/javascript/xhr.ts +192 -0
  237. package/src/generators/json.ts +293 -0
  238. package/src/generators/julia.ts +398 -0
  239. package/src/generators/kotlin.ts +355 -0
  240. package/src/generators/lua.ts +209 -0
  241. package/src/generators/matlab/common.ts +281 -0
  242. package/src/generators/matlab/httpinterface.ts +284 -0
  243. package/src/generators/matlab/matlab.ts +39 -0
  244. package/src/generators/matlab/webservices.ts +263 -0
  245. package/src/generators/objectivec.ts +297 -0
  246. package/src/generators/ocaml.ts +168 -0
  247. package/src/generators/perl.ts +237 -0
  248. package/src/generators/php/guzzle.ts +368 -0
  249. package/src/generators/php/php.ts +239 -0
  250. package/src/generators/php/requests.ts +103 -0
  251. package/src/generators/powershell.ts +430 -0
  252. package/src/generators/python/http.ts +167 -0
  253. package/src/generators/python/python.ts +1934 -0
  254. package/src/generators/r/httr.ts +328 -0
  255. package/src/generators/r/httr2.ts +268 -0
  256. package/src/generators/ruby/httparty.ts +351 -0
  257. package/src/generators/ruby/ruby.ts +564 -0
  258. package/src/generators/rust.ts +243 -0
  259. package/src/generators/swift.ts +235 -0
  260. package/src/generators/wget.ts +533 -0
  261. package/src/index.ts +89 -0
  262. package/src/parse.ts +72 -0
  263. package/src/shell/Parser.ts +8 -0
  264. package/src/shell/Word.ts +531 -0
  265. package/src/shell/tokenizer.ts +736 -0
  266. package/src/shell/webParser.ts +18 -0
  267. package/src/utils.ts +25 -0
  268. package/tools/compare-requests.ts +571 -0
  269. package/tools/extract_curl_args.py +511 -0
  270. package/tools/gen-test.ts +129 -0
@@ -0,0 +1,533 @@
1
+ import { warnIfPartsIgnored } from "../Warnings.js";
2
+ import { Word, eq, mergeWords } from "../shell/Word.js";
3
+ import { parse, COMMON_SUPPORTED_ARGS } from "../parse.js";
4
+ import type { Request, Warnings } from "../parse.js";
5
+
6
+ export const supportedArgs = new Set([
7
+ ...COMMON_SUPPORTED_ARGS,
8
+ "cookie-jar",
9
+
10
+ "ipv4",
11
+ "no-ipv4",
12
+ "ipv6",
13
+ "no-ipv6",
14
+
15
+ "form", // not supported, more specific warning
16
+ "form-string",
17
+
18
+ "ciphers",
19
+ "insecure",
20
+ "cert",
21
+ "cert-type",
22
+ "key",
23
+ "key-type",
24
+ "cacert",
25
+ "capath",
26
+ "crlfile",
27
+ "pinnedpubkey",
28
+ "random-file",
29
+ "egd-file",
30
+ "hsts",
31
+
32
+ "noproxy", // not fully supported
33
+ "proxy", // not supported
34
+ "proxy-user",
35
+
36
+ // TODO: supported by all?
37
+ "globoff",
38
+ "no-globoff",
39
+
40
+ "netrc",
41
+ "netrc-optional",
42
+
43
+ "timeout",
44
+ "connect-timeout",
45
+ "limit-rate",
46
+
47
+ "compressed",
48
+ "no-compressed",
49
+
50
+ // Wget picks the auth and some it doesn't support but there's a more
51
+ // specific error message for those.
52
+ "anyauth",
53
+ "no-anyauth",
54
+ "digest",
55
+ "no-digest",
56
+ "aws-sigv4",
57
+ "negotiate",
58
+ "no-negotiate",
59
+ "delegation", // GSS/kerberos
60
+ // "service-name", // GSS/kerberos, not supported
61
+ "ntlm",
62
+ "no-ntlm",
63
+ "ntlm-wb",
64
+ "no-ntlm-wb",
65
+ "no-digest",
66
+
67
+ "output",
68
+ "upload-file",
69
+
70
+ "continue-at",
71
+ "clobber",
72
+ "remote-time",
73
+
74
+ "keep-alive",
75
+
76
+ "silent",
77
+ "verbose",
78
+
79
+ "next",
80
+ ]);
81
+
82
+ // TODO: check this
83
+ // https://www.gnu.org/software/bash/manual/html_node/Quoting.html
84
+ const regexDoubleEscape = /\$|`|"|\\|!/gu;
85
+ const unprintableChars = /\p{C}|[^ \P{Z}]/gu; // TODO: there's probably more
86
+ const regexAnsiCEscape = /\p{C}|[^ \P{Z}]|\\|'/gu;
87
+ // https://unix.stackexchange.com/questions/270977/
88
+ const shellChars = /[\x02-\x09\x0b-\x1a\\#?`(){}[\]^*<=>~|; "!$&'\x82-\xff]/;
89
+ export function reprStr(s: string, mustQuote = false): string {
90
+ const containsUnprintableChars = unprintableChars.test(s);
91
+ if (containsUnprintableChars) {
92
+ return (
93
+ "$'" +
94
+ s.replace(regexAnsiCEscape, (c: string) => {
95
+ switch (c) {
96
+ case "\x07":
97
+ return "\\a";
98
+ case "\b":
99
+ return "\\b";
100
+ case "\x1B":
101
+ return "\\e";
102
+ case "\f":
103
+ return "\\f";
104
+ case "\n":
105
+ return "\\n";
106
+ case "\r":
107
+ return "\\r";
108
+ case "\t":
109
+ return "\\t";
110
+ case "\v":
111
+ return "\\v";
112
+ case "\\":
113
+ return "\\\\";
114
+ case "'":
115
+ return "\\'";
116
+ // TODO: unnecessary?
117
+ // case '"':
118
+ // return '\\"';
119
+ // case "?":
120
+ // return "\\?";
121
+ }
122
+
123
+ const hex = (c.codePointAt(0) as number).toString(16);
124
+ if (hex.length <= 2) {
125
+ return "\\x" + hex.padStart(2, "0");
126
+ }
127
+ if (hex.length <= 4) {
128
+ return "\\u" + hex.padStart(4, "0");
129
+ }
130
+ return "\\U" + hex.padStart(8, "0");
131
+ }) +
132
+ "'"
133
+ );
134
+ }
135
+ if (
136
+ (s.includes('"') ||
137
+ s.includes("$") ||
138
+ s.includes("`") ||
139
+ s.includes("!") ||
140
+ mustQuote) &&
141
+ !s.includes("'")
142
+ ) {
143
+ return "'" + s + "'";
144
+ }
145
+ if (shellChars.test(s) || mustQuote) {
146
+ return (
147
+ '"' +
148
+ s.replace(regexDoubleEscape, (c: string) => {
149
+ switch (c[0]) {
150
+ case "$":
151
+ return "\\$";
152
+ case "`":
153
+ return "\\`";
154
+ case '"':
155
+ return '\\"';
156
+ case "\\":
157
+ return "\\\\";
158
+ case "!":
159
+ // ! might have an effect or it might not, depending on if
160
+ // the command is in a file or pasted into the shell and other things.
161
+ // If it's backslash escaped, the backslash is still not removed, a
162
+ // safe way to escape it is to close the double quote, put the ! in
163
+ // single quotes and then open the double quote again.
164
+ return "\"'!'\"";
165
+ }
166
+ // Should never happen
167
+ return c;
168
+ }) +
169
+ '"'
170
+ );
171
+ }
172
+ return s;
173
+ }
174
+
175
+ export function repr(w: Word): string {
176
+ // TODO: put variables in the quotes
177
+ const args: string[] = [];
178
+ for (const [i, t] of w.tokens.entries()) {
179
+ if (typeof t === "string") {
180
+ // A string after a variable cannot be un-quoted
181
+ // After a subcommand it's usually okay but that's more complex.
182
+ args.push(reprStr(t, !!i));
183
+ } else if (t.type === "variable") {
184
+ args.push(t.text);
185
+ } else {
186
+ // TODO: this will probably lead to syntax errors
187
+ args.push(t.text);
188
+ }
189
+ }
190
+ return args.join("");
191
+ }
192
+
193
+ function requestToWget(request: Request, warnings: Warnings): string {
194
+ warnIfPartsIgnored(request, warnings, {
195
+ dataReadsFile: true,
196
+ cookieFiles: true,
197
+ multipleUrls: true,
198
+ });
199
+ if (
200
+ request.dataReadsFile &&
201
+ request.dataArray &&
202
+ request.dataArray.length &&
203
+ (request.dataArray.length > 1 ||
204
+ (!(request.dataArray[0] instanceof Word) && request.dataArray[0].name))
205
+ ) {
206
+ warnings.push([
207
+ "unsafe-data",
208
+ "the generated data content is wrong, " +
209
+ // TODO: might not come from "@"
210
+ JSON.stringify("@" + request.dataReadsFile) +
211
+ " means read the file " +
212
+ JSON.stringify(request.dataReadsFile),
213
+ ]);
214
+ }
215
+
216
+ const args: string[] = [];
217
+
218
+ if (
219
+ request.urls[0].uploadFile ||
220
+ (request.dataArray &&
221
+ request.dataArray.length === 1 &&
222
+ !(request.dataArray[0] instanceof Word) &&
223
+ !request.dataArray[0].name) ||
224
+ request.data
225
+ ) {
226
+ if (!eq(request.urls[0].method, "POST")) {
227
+ args.push("--method=" + repr(request.urls[0].method));
228
+ }
229
+ } else if (!eq(request.urls[0].method, "GET")) {
230
+ args.push("--method=" + repr(request.urls[0].method));
231
+ }
232
+ if (request.urls.length > 1) {
233
+ const uniqueMethods = new Set<string>(
234
+ request.urls.map((u) => u.method.toString()),
235
+ );
236
+
237
+ // TODO: add tons of checks/warnings that wget doesn't let you set things per-URL
238
+ if (uniqueMethods.size > 1) {
239
+ warnings.push([
240
+ "mixed-methods",
241
+ "the input curl command uses multiple HTTP methods, which Wget doesn't support: " +
242
+ request.urls
243
+ .map((u) => JSON.stringify(u.method.toString()))
244
+ .join(", "),
245
+ ]);
246
+ }
247
+ }
248
+
249
+ if (request.cookieFiles && request.cookieFiles.length) {
250
+ for (const cookieFile of request.cookieFiles) {
251
+ args.push("--load-cookies=" + repr(cookieFile));
252
+ }
253
+
254
+ if (request.cookieFiles.length > 1) {
255
+ const lastCookieFile =
256
+ request.cookieFiles[request.cookieFiles.length - 1];
257
+ warnings.push([
258
+ "multiple-cookie-files",
259
+ "Wget only supports reading cookies from one file, only the last one will be sent: " +
260
+ lastCookieFile.toString(),
261
+ ]);
262
+ }
263
+ }
264
+ if (request.cookieJar) {
265
+ args.push("--save-cookies=" + repr(request.cookieJar));
266
+ // TODO: --keep-session-cookies
267
+ }
268
+
269
+ if (request.headers.length) {
270
+ for (const [headerName, headerValue] of request.headers) {
271
+ args.push(
272
+ "--header=" + repr(mergeWords(headerName, ": ", headerValue ?? "")),
273
+ );
274
+ // TODO: there's also --referer, --user-agent and --content-disposition
275
+ }
276
+ }
277
+
278
+ if (request.compressed !== undefined) {
279
+ if (request.compressed) {
280
+ args.push("--compression=auto");
281
+ } else {
282
+ args.push("--compression=none");
283
+ }
284
+ }
285
+
286
+ if (request.maxRedirects && request.maxRedirects.toString() !== "20") {
287
+ // TODO: escape/parse?
288
+ args.push("--max-redirect=" + repr(request.maxRedirects));
289
+ }
290
+
291
+ if (request.ipv4) {
292
+ args.push("--inet4-only");
293
+ }
294
+ if (request.ipv6) {
295
+ args.push("--inet6-only");
296
+ }
297
+
298
+ if (request.urls[0].auth) {
299
+ const [user, password] = request.urls[0].auth;
300
+ args.push("--user=" + repr(user));
301
+ if (password.toBool()) {
302
+ args.push("--password=" + repr(password));
303
+ }
304
+ if (request.authType === "basic") {
305
+ args.push("--auth-no-challenge");
306
+ }
307
+
308
+ if (
309
+ !["none", "basic", "digest", "ntlm", "ntlm-wb", "negotiate"].includes(
310
+ request.authType,
311
+ )
312
+ ) {
313
+ warnings.push([
314
+ "wget-unsupported-auth",
315
+ "Wget doesn't support " + request.authType + " authentication",
316
+ ]);
317
+ }
318
+ }
319
+
320
+ if (request.urls[0].uploadFile) {
321
+ if (
322
+ eq(request.urls[0].uploadFile, "-") ||
323
+ eq(request.urls[0].uploadFile, ".")
324
+ ) {
325
+ warnings.push([
326
+ "wget-stdin",
327
+ "Wget does not support uploading from stdin",
328
+ ]);
329
+ }
330
+ if (eq(request.urls[0].method, "POST")) {
331
+ args.push("--post-file=" + repr(request.urls[0].uploadFile));
332
+ } else {
333
+ args.push("--body-file=" + repr(request.urls[0].uploadFile));
334
+ }
335
+ } else if (request.multipartUploads) {
336
+ warnings.push([
337
+ "multipart",
338
+ "Wget does not support sending multipart/form-data",
339
+ ]);
340
+ } else if (
341
+ request.dataArray &&
342
+ request.dataArray.length === 1 &&
343
+ !(request.dataArray[0] instanceof Word) &&
344
+ !request.dataArray[0].name
345
+ ) {
346
+ if (eq(request.urls[0].method, "POST")) {
347
+ args.push("--post-file=" + repr(request.dataArray[0].filename));
348
+ } else {
349
+ args.push("--body-file=" + repr(request.dataArray[0].filename));
350
+ }
351
+ } else if (request.data) {
352
+ if (eq(request.urls[0].method, "POST")) {
353
+ args.push("--post-data=" + repr(request.data));
354
+ } else {
355
+ args.push("--body-data=" + repr(request.data));
356
+ }
357
+ }
358
+
359
+ // https://www.gnu.org/software/wget/manual/html_node/HTTPS-_0028SSL_002fTLS_0029-Options.html
360
+ if (request.ciphers) {
361
+ args.push("--ciphers=" + repr(request.ciphers));
362
+ }
363
+ if (request.insecure) {
364
+ args.push("--no-check-certificate");
365
+ }
366
+ if (request.cert) {
367
+ const [cert, password] = request.cert;
368
+ args.push("--certificate=" + repr(cert));
369
+ if (password) {
370
+ warnings.push([
371
+ "wget-cert-password",
372
+ // TODO: is this true?
373
+ "Wget does not support certificate passwords",
374
+ ]);
375
+ }
376
+ }
377
+ if (request.certType) {
378
+ args.push("--certificate-type=" + repr(request.certType));
379
+ }
380
+ if (request.key) {
381
+ args.push("--private-key=" + repr(request.key));
382
+ }
383
+ if (request.keyType) {
384
+ args.push("--private-key-type=" + repr(request.keyType));
385
+ }
386
+
387
+ if (request.cacert) {
388
+ args.push("--ca-certificate=" + repr(request.cacert));
389
+ }
390
+ if (request.capath) {
391
+ args.push("--ca-directory=" + repr(request.capath));
392
+ }
393
+ if (request.crlfile) {
394
+ args.push("--crl-file=" + repr(request.crlfile));
395
+ }
396
+ if (request.pinnedpubkey) {
397
+ args.push("--pinnedpubkey=" + repr(request.pinnedpubkey));
398
+ }
399
+ if (request.randomFile) {
400
+ args.push("--random-file=" + repr(request.randomFile));
401
+ }
402
+ if (request.egdFile) {
403
+ args.push("--egd-file=" + repr(request.egdFile));
404
+ }
405
+ if (request.hsts) {
406
+ for (const hsts of request.hsts) {
407
+ // TODO: warn multiple won't work
408
+ args.push("--hsts-file=" + repr(hsts));
409
+ }
410
+ }
411
+ // TODO: --secure-protocol
412
+
413
+ if (request.netrc === "ignored") {
414
+ args.push("--no-netrc");
415
+ }
416
+
417
+ if (request.noproxy) {
418
+ if (eq(request.noproxy, "*")) {
419
+ args.push("--no-proxy");
420
+ } else {
421
+ warnings.push([
422
+ "wget-noproxy",
423
+ "Wget does not support noproxy for specific hosts",
424
+ ]);
425
+ }
426
+ }
427
+ if (request.proxy) {
428
+ warnings.push([
429
+ "wget-proxy",
430
+ "Wget requires specifying proxies in environment variables: " +
431
+ JSON.stringify(request.proxy.toString()),
432
+ ]);
433
+ }
434
+ if (request.proxyAuth) {
435
+ const [proxyUser, proxyPassword] = request.proxyAuth.split(":", 2);
436
+ args.push("--proxy-user=" + repr(proxyUser));
437
+ if (proxyPassword && proxyPassword.toBool()) {
438
+ args.push("--proxy-password=" + repr(proxyPassword));
439
+ }
440
+ }
441
+
442
+ if (request.timeout) {
443
+ // TODO: warn that this is not for the whole request
444
+ args.push("--timeout=" + repr(request.timeout));
445
+ }
446
+ if (request.connectTimeout) {
447
+ args.push("--connect-timeout=" + repr(request.connectTimeout));
448
+ }
449
+
450
+ // TODO: curl's --speed-limit
451
+ if (request.limitRate) {
452
+ // TODO: they probably have different syntaxes
453
+ args.push("--limit-rate=" + repr(request.limitRate));
454
+ }
455
+
456
+ // TODO: this doesn't match up. Warn if multiple, etc.
457
+ if (request.urls[0].output) {
458
+ args.push("--output-document=" + repr(request.urls[0].output));
459
+ } else {
460
+ args.push("--output-document -");
461
+ }
462
+ if (request.clobber === false) {
463
+ args.push("--no-clobber");
464
+ }
465
+ // TODO: --create-dirs
466
+
467
+ if (request.remoteTime === false) {
468
+ args.push("--no-use-server-timestamps");
469
+ }
470
+
471
+ if (request.continueAt) {
472
+ if (eq(request.continueAt, "-")) {
473
+ args.push("--continue");
474
+ } else {
475
+ args.push("--start-pos=" + repr(request.continueAt));
476
+ }
477
+ }
478
+
479
+ if (request.keepAlive === false) {
480
+ args.push("--no-http-keep-alive");
481
+ }
482
+
483
+ // This is used only for FTP by Wget, we might as well add it even
484
+ // though we don't support FTP.
485
+ if (request.globoff) {
486
+ // TODO: this isn't great, it has different meanings from curl to Wget
487
+ // TODO: don't remove \[ and \] from original URL?
488
+ args.push("--no-glob");
489
+ }
490
+
491
+ // TODO:
492
+ // curl:
493
+ // --silent, --verbose, --no-verbose (the default)
494
+ // --progress-meter (the default), --no-progress-meter, --progress-bar
495
+ // --trace-ascii, --trace
496
+ // wget:
497
+ // --quiet, --verbose (the default), --no-verbose
498
+ // --progress=bar (the default), --progress=dot
499
+ // --debug
500
+ if (request.silent) {
501
+ args.push("--quiet");
502
+ } else if (request.verbose === false) {
503
+ args.push("--no-verbose");
504
+ }
505
+
506
+ for (const url of request.urls) {
507
+ // TODO: wget probably has a different syntax
508
+ args.push(repr(url.url));
509
+ }
510
+
511
+ const multiline =
512
+ args.length > 3 || args.reduce((a, b) => a + b.length, 0) > 80 - 5;
513
+ const joiner = multiline ? " \\\n " : " ";
514
+ return "wget " + args.join(joiner) + "\n";
515
+ }
516
+
517
+ // TODO: --verbose ?
518
+ export function _toWget(requests: Request[], warnings: Warnings = []): string {
519
+ return requests.map((r) => requestToWget(r, warnings)).join("\n\n");
520
+ }
521
+
522
+ export function toWgetWarn(
523
+ curlCommand: string | string[],
524
+ warnings: Warnings = [],
525
+ ): [string, Warnings] {
526
+ const requests = parse(curlCommand, supportedArgs, warnings);
527
+ const wget = _toWget(requests, warnings);
528
+ return [wget, warnings];
529
+ }
530
+
531
+ export function toWget(curlCommand: string | string[]): string {
532
+ return toWgetWarn(curlCommand)[0];
533
+ }
package/src/index.ts ADDED
@@ -0,0 +1,89 @@
1
+ export { toAnsible, toAnsibleWarn } from "./generators/ansible.js";
2
+ export { toC, toCWarn } from "./generators/c.js";
3
+ export { toCFML, toCFMLWarn } from "./generators/cfml.js";
4
+ export { toClojure, toClojureWarn } from "./generators/clojure.js";
5
+ export { toCSharp, toCSharpWarn } from "./generators/csharp.js";
6
+ export { toDart, toDartWarn } from "./generators/dart.js";
7
+ export { toElixir, toElixirWarn } from "./generators/elixir.js";
8
+ export { toGo, toGoWarn } from "./generators/go.js";
9
+ export { toHarString, toHarStringWarn } from "./generators/har.js";
10
+ export { toHTTP, toHTTPWarn } from "./generators/http.js";
11
+ export { toHttpie, toHttpieWarn } from "./generators/httpie.js";
12
+ export { toJava, toJavaWarn } from "./generators/java/java.js";
13
+ export {
14
+ toJavaHttpUrlConnection,
15
+ toJavaHttpUrlConnectionWarn,
16
+ } from "./generators/java/httpurlconnection.js";
17
+ export { toJavaJsoup, toJavaJsoupWarn } from "./generators/java/jsoup.js";
18
+ export { toJavaOkHttp, toJavaOkHttpWarn } from "./generators/java/okhttp.js";
19
+ export {
20
+ toJavaScript,
21
+ toJavaScriptWarn,
22
+ } from "./generators/javascript/javascript.js";
23
+ export {
24
+ toJavaScriptJquery,
25
+ toJavaScriptJqueryWarn,
26
+ } from "./generators/javascript/jquery.js";
27
+ export {
28
+ toJavaScriptXHR,
29
+ toJavaScriptXHRWarn,
30
+ } from "./generators/javascript/xhr.js";
31
+ export {
32
+ toJsonObject,
33
+ toJsonObjectWarn,
34
+ toJsonString,
35
+ toJsonStringWarn,
36
+ type JSONOutput,
37
+ } from "./generators/json.js";
38
+ export { toJulia, toJuliaWarn } from "./generators/julia.js";
39
+ export { toKotlin, toKotlinWarn } from "./generators/kotlin.js";
40
+ export { toLua, toLuaWarn } from "./generators/lua.js";
41
+ export { toMATLAB, toMATLABWarn } from "./generators/matlab/matlab.js";
42
+ export { toNode, toNodeWarn } from "./generators/javascript/javascript.js";
43
+ export { toNodeAxios, toNodeAxiosWarn } from "./generators/javascript/axios.js";
44
+ export { toNodeGot, toNodeGotWarn } from "./generators/javascript/got.js";
45
+ export { toNodeHttp, toNodeHttpWarn } from "./generators/javascript/http.js";
46
+ export { toNodeKy, toNodeKyWarn } from "./generators/javascript/ky.js";
47
+ export {
48
+ toNodeSuperAgent,
49
+ toNodeSuperAgentWarn,
50
+ } from "./generators/javascript/superagent.js";
51
+ export {
52
+ toNodeRequest,
53
+ toNodeRequestWarn,
54
+ } from "./generators/javascript/request.js";
55
+ export { toObjectiveC, toObjectiveCWarn } from "./generators/objectivec.js";
56
+ export { toOCaml, toOCamlWarn } from "./generators/ocaml.js";
57
+ export { toPerl, toPerlWarn } from "./generators/perl.js";
58
+ export { toPhp, toPhpWarn } from "./generators/php/php.js";
59
+ export { toPhpGuzzle, toPhpGuzzleWarn } from "./generators/php/guzzle.js";
60
+ export { toPhpRequests, toPhpRequestsWarn } from "./generators/php/requests.js";
61
+ export {
62
+ toPowershellRestMethod,
63
+ toPowershellRestMethodWarn,
64
+ } from "./generators/powershell.js";
65
+ export {
66
+ toPowershellWebRequest,
67
+ toPowershellWebRequestWarn,
68
+ } from "./generators/powershell.js";
69
+ export { toPython, toPythonWarn } from "./generators/python/python.js";
70
+ export { toPythonHttp, toPythonHttpWarn } from "./generators/python/http.js";
71
+ export { toR, toRWarn } from "./generators/r/httr.js";
72
+ export { toRHttr2, toRHttr2Warn } from "./generators/r/httr2.js";
73
+ export { toRuby, toRubyWarn } from "./generators/ruby/ruby.js";
74
+ export {
75
+ toRubyHttparty,
76
+ toRubyHttpartyWarn,
77
+ } from "./generators/ruby/httparty.js";
78
+ export { toRust, toRustWarn } from "./generators/rust.js";
79
+ export { toSwift, toSwiftWarn } from "./generators/swift.js";
80
+ export { toWget, toWgetWarn } from "./generators/wget.js";
81
+
82
+ // backwards compatibility aliases
83
+ export { toJavaScript as toBrowser } from "./generators/javascript/javascript.js";
84
+ export { toNode as toNodeFetch } from "./generators/javascript/javascript.js";
85
+ export { toJavaScriptWarn as toBrowserWarn } from "./generators/javascript/javascript.js";
86
+ export { toNodeWarn as toNodeFetchWarn } from "./generators/javascript/javascript.js";
87
+
88
+ export { CCError } from "./utils.js";
89
+ export type { Warnings } from "./Warnings.js";
package/src/parse.ts ADDED
@@ -0,0 +1,72 @@
1
+ import { CCError } from "./utils.js";
2
+ import { Word } from "./shell/Word.js";
3
+ import { tokenize } from "./shell/tokenizer.js";
4
+ import * as curl from "./curl/opts.js";
5
+ import {
6
+ curlLongOpts,
7
+ curlLongOptsShortened,
8
+ curlShortOpts,
9
+ } from "./curl/opts.js";
10
+ import { buildRequests } from "./Request.js";
11
+ import type { Request, RequestUrl } from "./Request.js";
12
+ import type { Warnings } from "./Warnings.js";
13
+
14
+ export { COMMON_SUPPORTED_ARGS } from "./curl/opts.js";
15
+ export { getFirst } from "./Request.js";
16
+
17
+ export type { Request, RequestUrl, Warnings };
18
+
19
+ export function clip(s: string, maxLength = 30): string {
20
+ if (s.length > maxLength) {
21
+ return s.slice(0, maxLength - 3) + "...";
22
+ }
23
+ return s;
24
+ }
25
+
26
+ function findCommands(
27
+ curlCommand: string | string[],
28
+ warnings: Warnings,
29
+ ): [Word[], Word?, Word?][] {
30
+ if (typeof curlCommand === "string") {
31
+ return tokenize(curlCommand, warnings);
32
+ }
33
+
34
+ if (curlCommand.length === 0) {
35
+ throw new CCError("no arguments provided");
36
+ }
37
+ if (curlCommand[0].trim() !== "curl") {
38
+ throw new CCError(
39
+ 'command should begin with "curl" but instead begins with ' +
40
+ JSON.stringify(clip(curlCommand[0])),
41
+ );
42
+ }
43
+ return [[curlCommand.map((arg) => new Word(arg)), undefined, undefined]];
44
+ }
45
+
46
+ /**
47
+ * Accepts a string of Bash code or a tokenized argv array.
48
+ * Returns an array of parsed curl objects.
49
+ * @param command a string of Bash code containing at least one curl command or an
50
+ * array of shell argument tokens (meant for passing process.argv).
51
+ */
52
+ export function parse(
53
+ command: string | string[],
54
+ supportedArgs?: Set<string>,
55
+ warnings: Warnings = [],
56
+ ): Request[] {
57
+ let requests: Request[] = [];
58
+ const curlCommands = findCommands(command, warnings);
59
+ for (const [argv, stdin, stdinFile] of curlCommands) {
60
+ const [globalConfig] = curl.parseArgs(
61
+ argv,
62
+ curlLongOpts,
63
+ curlLongOptsShortened,
64
+ curlShortOpts,
65
+ supportedArgs,
66
+ warnings,
67
+ );
68
+
69
+ requests = requests.concat(buildRequests(globalConfig, stdin, stdinFile));
70
+ }
71
+ return requests;
72
+ }
@@ -0,0 +1,8 @@
1
+ import Parser from "@ohos-ports/tree-sitter";
2
+ import Bash from "@ohos-ports/tree-sitter-bash";
3
+
4
+ const parser = new Parser();
5
+ parser.setLanguage(Bash);
6
+
7
+ export default parser;
8
+ export type { Parser };