@poe-platform/safe-bash 0.1.756 → 0.1.757
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/dist/image-ast/index.browser.js +4 -4
- package/dist/image-ast/index.js +4 -4
- package/dist/safe-bash/chunks/{chunk-LESPJ3OG.js → chunk-WNWIBBJZ.js} +15 -2
- package/dist/safe-bash/command-chunks/{chunk-S4J6MSLY.js → chunk-444OTXM7.js} +3 -3
- package/dist/safe-bash/command-chunks/{chunk-6AIOGLZO.js → chunk-CE6Z2ULX.js} +2 -2
- package/dist/safe-bash/command-chunks/{chunk-5VSGEE6J.js → chunk-F4IQCXZG.js} +5 -5
- package/dist/safe-bash/command-chunks/{chunk-F5MORDSU.js → chunk-FDNCPZQS.js} +32 -1
- package/dist/safe-bash/command-chunks/{chunk-SG3ZDZCX.js → chunk-FLMLM7Y4.js} +2 -2
- package/dist/safe-bash/command-chunks/{chunk-CT3FB722.js → chunk-J7UMKBRK.js} +1 -1
- package/dist/safe-bash/command-chunks/{chunk-QQOAK7JQ.js → chunk-KKJCKPTA.js} +1 -1
- package/dist/safe-bash/command-chunks/{chunk-7KS6PX4U.js → chunk-KSIGF4AG.js} +3 -3
- package/dist/safe-bash/command-chunks/{chunk-4HQ7WYSI.js → chunk-MED2BOOW.js} +30 -5
- package/dist/safe-bash/command-chunks/{chunk-JRNLDPLI.js → chunk-OZSWW23M.js} +1 -1
- package/dist/safe-bash/command-chunks/{chunk-DKQ5REXF.js → chunk-QDPRSPHQ.js} +1 -1
- package/dist/safe-bash/command-chunks/{chunk-WOPU2VWF.js → chunk-RID3B5RE.js} +1 -1
- package/dist/safe-bash/command-chunks/{chunk-DVZCHNB5.js → chunk-S4MLIYSN.js} +7 -7
- package/dist/safe-bash/command-chunks/{chunk-REYTQLLB.js → chunk-TT2DMDBY.js} +1 -1
- package/dist/safe-bash/command-chunks/{chunk-UGX6MXDD.js → chunk-UIIT4ULR.js} +3 -3
- package/dist/safe-bash/command-chunks/{chunk-A6NFLWZI.js → chunk-WKPOGTZY.js} +22 -6
- package/dist/safe-bash/command-chunks/{chunk-3DOHQQJR.js → chunk-Y64PA6IA.js} +8 -8
- package/dist/safe-bash/command-chunks/{chunk-6Z7OS3TZ.js → chunk-ZP5KDHB4.js} +7 -7
- package/dist/safe-bash/commands/pandoc/index.d.ts +1 -1
- package/dist/safe-bash/commands/pandoc/index.js +5 -3
- package/dist/safe-bash/commands/python/index.browser.js +1 -1
- package/dist/safe-bash/commands/python/index.js +6 -0
- package/dist/safe-bash/commands/python/jspi.js +11 -2
- package/dist/safe-bash/core.browser.js +6 -4
- package/dist/safe-bash-command-csvkit/index.js +8 -8
- package/dist/safe-bash-command-soffice/index.js +16 -16
- package/dist/safe-bash-command-ssconvert/core.js +8 -8
- package/dist/safe-bash-command-ssconvert/formats/csv.js +9 -9
- package/dist/safe-bash-command-ssconvert/formats/ods.js +8 -8
- package/dist/safe-bash-command-ssconvert/formats/spreadsheetml.js +1 -1
- package/dist/safe-bash-command-ssconvert/formats/xls.js +9 -8
- package/dist/safe-bash-command-ssconvert/formats/xlsx.js +8 -8
- package/dist/safe-bash-command-ssconvert/index.js +107 -27
- package/dist/safe-bash-contracts/filesystem-descriptor.js +1 -1
- package/dist/safe-bash-contracts/filesystem-output.js +1 -1
- package/dist/safe-bash-mcp/index.js +4 -4
- package/dist/spreadsheet-engine/formulas/ast.d.ts +13 -0
- package/dist/spreadsheet-engine/formulas/conventions.js +1 -1
- package/dist/spreadsheet-engine/formulas/dependencies.js +2 -2
- package/dist/spreadsheet-engine/formulas/evaluator.js +2 -2
- package/dist/spreadsheet-engine/formulas/named-expressions.d.ts +2 -2
- package/dist/spreadsheet-engine/formulas/named-expressions.js +31 -5
- package/dist/spreadsheet-engine/formulas/parser.js +34 -0
- package/dist/spreadsheet-engine/formulas/rewriting.js +19 -1
- package/dist/spreadsheet-engine/formulas/serialization.js +6 -0
- package/dist/spreadsheet-format-xls/biff-write-formulas.js +2 -0
- package/dist/xlsx-ast/formula-semantics.js +2 -0
- package/package.json +2 -2
- package/dist/safe-bash/chunks/browser-ECQX6GMK.js +0 -6766
- package/dist/safe-bash/chunks/bz2-4OP3OWW3.js +0 -8
- package/dist/safe-bash/chunks/chunk-22XPAAOI.js +0 -173
- package/dist/safe-bash/chunks/chunk-44WUNUPV.js +0 -649
- package/dist/safe-bash/chunks/chunk-4E3SGG37.js +0 -876
- package/dist/safe-bash/chunks/chunk-4QTSSQKP.js +0 -898
- package/dist/safe-bash/chunks/chunk-5UVGPVXP.js +0 -410
- package/dist/safe-bash/chunks/chunk-64OE6Q6X.js +0 -3487
- package/dist/safe-bash/chunks/chunk-6IIFUPN2.js +0 -1526
- package/dist/safe-bash/chunks/chunk-BENDLTQD.js +0 -7065
- package/dist/safe-bash/chunks/chunk-BMO5LWEX.js +0 -534
- package/dist/safe-bash/chunks/chunk-BPMCPTNP.js +0 -1920
- package/dist/safe-bash/chunks/chunk-BRJTAWP5.js +0 -2955
- package/dist/safe-bash/chunks/chunk-C3GEIN7Q.js +0 -20054
- package/dist/safe-bash/chunks/chunk-CBVANNKK.js +0 -174814
- package/dist/safe-bash/chunks/chunk-CEWCRI6H.js +0 -2813
- package/dist/safe-bash/chunks/chunk-CGL53DQQ.js +0 -37
- package/dist/safe-bash/chunks/chunk-CTYJJD4O.js +0 -869
- package/dist/safe-bash/chunks/chunk-CWSS4MQA.js +0 -7
- package/dist/safe-bash/chunks/chunk-DLIGMTCP.js +0 -3266
- package/dist/safe-bash/chunks/chunk-EMAJ7WUW.js +0 -1031
- package/dist/safe-bash/chunks/chunk-FDKIZ2IJ.js +0 -1950
- package/dist/safe-bash/chunks/chunk-FZWJ2A7V.js +0 -3093
- package/dist/safe-bash/chunks/chunk-G2VCIF2J.js +0 -22
- package/dist/safe-bash/chunks/chunk-GVKBUQDX.js +0 -2649
- package/dist/safe-bash/chunks/chunk-IO2OGW3E.js +0 -28051
- package/dist/safe-bash/chunks/chunk-ISCZG7WA.js +0 -2648
- package/dist/safe-bash/chunks/chunk-JCN3FXLP.js +0 -38983
- package/dist/safe-bash/chunks/chunk-KJHDD4A2.js +0 -759
- package/dist/safe-bash/chunks/chunk-LB6HVRXS.js +0 -857
- package/dist/safe-bash/chunks/chunk-LLWAJOZH.js +0 -15
- package/dist/safe-bash/chunks/chunk-LMUKYZIJ.js +0 -708
- package/dist/safe-bash/chunks/chunk-M5SCNKIK.js +0 -2012
- package/dist/safe-bash/chunks/chunk-MJPFHANT.js +0 -3350
- package/dist/safe-bash/chunks/chunk-MZQW2DOL.js +0 -82
- package/dist/safe-bash/chunks/chunk-NB2V7EOY.js +0 -1348
- package/dist/safe-bash/chunks/chunk-NJVGOIYN.js +0 -17
- package/dist/safe-bash/chunks/chunk-OLNZHDIQ.js +0 -5141
- package/dist/safe-bash/chunks/chunk-OSUXMC43.js +0 -2317
- package/dist/safe-bash/chunks/chunk-OUUVROTM.js +0 -38
- package/dist/safe-bash/chunks/chunk-PA6GYKBQ.js +0 -151
- package/dist/safe-bash/chunks/chunk-PEFHZ226.js +0 -6957
- package/dist/safe-bash/chunks/chunk-QBB5P5KW.js +0 -4839
- package/dist/safe-bash/chunks/chunk-RSAWYAEH.js +0 -973
- package/dist/safe-bash/chunks/chunk-UWLYRWT4.js +0 -739
- package/dist/safe-bash/chunks/chunk-V463NBQH.js +0 -13
- package/dist/safe-bash/chunks/chunk-WPUALYUS.js +0 -843
- package/dist/safe-bash/chunks/chunk-XHKFSRGU.js +0 -5747
- package/dist/safe-bash/chunks/chunk-XHRCLZ3N.js +0 -13391
- package/dist/safe-bash/chunks/chunk-XPF5XHLY.js +0 -15
- package/dist/safe-bash/chunks/chunk-ZBFOWARG.js +0 -305
- package/dist/safe-bash/chunks/chunk-ZG7GWQ5P.js +0 -1859
- package/dist/safe-bash/chunks/model-store-EIY3ZQLZ.js +0 -10
- package/dist/safe-bash/chunks/pdf-renderer-CMF3CSVU.js +0 -22
- package/dist/safe-bash/chunks/pptx-ZL6L7KP2-O425BFIU.js +0 -30658
- package/dist/safe-bash/chunks/src-HHEPGR7U.js +0 -18969
- package/dist/safe-bash/chunks/src-MXIKQCIC.js +0 -187
- package/dist/safe-bash/chunks/usage-6TGVA2OM.js +0 -35
- package/dist/safe-bash/chunks/xz-EGQGQUEI.js +0 -8
- package/dist/safe-bash/chunks/xz-K2E2WLM6.js +0 -9
- package/dist/safe-bash/chunks/zstd-6JBLP5EF.js +0 -8
- package/dist/safe-bash/git_rust-C3QYOAZL.wasm +0 -0
|
@@ -1,2649 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
shellValueByteLength,
|
|
3
|
-
value_exports
|
|
4
|
-
} from "./chunk-CGL53DQQ.js";
|
|
5
|
-
import {
|
|
6
|
-
escaping_exports
|
|
7
|
-
} from "./chunk-NJVGOIYN.js";
|
|
8
|
-
import {
|
|
9
|
-
contracts_exports
|
|
10
|
-
} from "./chunk-MZQW2DOL.js";
|
|
11
|
-
import {
|
|
12
|
-
yield_exports
|
|
13
|
-
} from "./chunk-XPF5XHLY.js";
|
|
14
|
-
import {
|
|
15
|
-
normalizePath,
|
|
16
|
-
posixPath
|
|
17
|
-
} from "./chunk-OUUVROTM.js";
|
|
18
|
-
import {
|
|
19
|
-
io_exports
|
|
20
|
-
} from "./chunk-G2VCIF2J.js";
|
|
21
|
-
import {
|
|
22
|
-
builtInDirectContextExecutors,
|
|
23
|
-
path_exports,
|
|
24
|
-
syncCommandEvaluators
|
|
25
|
-
} from "./chunk-ISCZG7WA.js";
|
|
26
|
-
import {
|
|
27
|
-
TransformStream,
|
|
28
|
-
__reExport,
|
|
29
|
-
import_buffer,
|
|
30
|
-
init_platform
|
|
31
|
-
} from "./chunk-ZG7GWQ5P.js";
|
|
32
|
-
|
|
33
|
-
// packages/safe-bash/src/commands/network/public.ts
|
|
34
|
-
init_platform();
|
|
35
|
-
|
|
36
|
-
// packages/safe-bash/src/commands/network/wget.ts
|
|
37
|
-
init_platform();
|
|
38
|
-
|
|
39
|
-
// packages/safe-bash/src/commands/network/shared.ts
|
|
40
|
-
init_platform();
|
|
41
|
-
|
|
42
|
-
// packages/safe-bash/src/commands/network/deadline.ts
|
|
43
|
-
init_platform();
|
|
44
|
-
function scheduleNetworkDeadline(milliseconds, expire) {
|
|
45
|
-
const deadline = performance.now() + milliseconds;
|
|
46
|
-
let timer;
|
|
47
|
-
let closed = false;
|
|
48
|
-
const arm = () => {
|
|
49
|
-
if (closed || !Number.isFinite(deadline)) return;
|
|
50
|
-
const remaining = deadline - performance.now();
|
|
51
|
-
timer = setTimeout(remaining > 2147483647 ? arm : expire, Math.max(0, Math.min(remaining, 2147483647)));
|
|
52
|
-
};
|
|
53
|
-
arm();
|
|
54
|
-
return () => {
|
|
55
|
-
closed = true;
|
|
56
|
-
clearTimeout(timer);
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// packages/safe-bash/src/commands/network/types.ts
|
|
61
|
-
init_platform();
|
|
62
|
-
var defaultNetworkLimits = Object.freeze({
|
|
63
|
-
maxUploadBytes: Infinity,
|
|
64
|
-
maxDownloadBytes: Infinity,
|
|
65
|
-
maxBufferBytes: Infinity,
|
|
66
|
-
maxHeaderBytes: Infinity,
|
|
67
|
-
maxRedirects: Infinity,
|
|
68
|
-
maxRetries: Infinity,
|
|
69
|
-
maxUrls: Infinity,
|
|
70
|
-
maxTimeMs: Infinity,
|
|
71
|
-
maxTotalTimeMs: Infinity
|
|
72
|
-
});
|
|
73
|
-
var cloudflareWorkerNetworkLimits = Object.freeze({
|
|
74
|
-
maxUploadBytes: 4 * 1024 * 1024,
|
|
75
|
-
maxDownloadBytes: 4 * 1024 * 1024,
|
|
76
|
-
maxBufferBytes: 1024 * 1024,
|
|
77
|
-
maxHeaderBytes: 32 * 1024,
|
|
78
|
-
maxRedirects: 2,
|
|
79
|
-
maxRetries: 1,
|
|
80
|
-
maxUrls: 8,
|
|
81
|
-
maxTimeMs: 1e4,
|
|
82
|
-
maxTotalTimeMs: 1e4
|
|
83
|
-
});
|
|
84
|
-
var CurlError = class extends Error {
|
|
85
|
-
constructor(exitCode, message) {
|
|
86
|
-
super(message);
|
|
87
|
-
this.exitCode = exitCode;
|
|
88
|
-
this.name = "CurlError";
|
|
89
|
-
}
|
|
90
|
-
exitCode;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
// packages/safe-bash/src/commands/network/shared.ts
|
|
94
|
-
var encode = (text) => new TextEncoder().encode(text);
|
|
95
|
-
function limitsFor(overrides = {}) {
|
|
96
|
-
const result = { ...defaultNetworkLimits, ...overrides };
|
|
97
|
-
for (const [name, value] of Object.entries(overrides)) {
|
|
98
|
-
const minimum = name === "maxRedirects" || name === "maxRetries" ? 0 : 1;
|
|
99
|
-
if (value !== Infinity && (!Number.isSafeInteger(value) || value < minimum)) {
|
|
100
|
-
throw new RangeError(`Invalid network limit: ${name}`);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return Object.freeze(result);
|
|
104
|
-
}
|
|
105
|
-
async function withSignal(operation, signal) {
|
|
106
|
-
signal.throwIfAborted();
|
|
107
|
-
return new Promise((resolve, reject) => {
|
|
108
|
-
const onAbort = () => {
|
|
109
|
-
cleanup();
|
|
110
|
-
reject(signal.reason);
|
|
111
|
-
};
|
|
112
|
-
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
113
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
114
|
-
Promise.resolve().then(operation).then((value) => {
|
|
115
|
-
cleanup();
|
|
116
|
-
resolve(value);
|
|
117
|
-
}, (error) => {
|
|
118
|
-
cleanup();
|
|
119
|
-
reject(error);
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
async function delay(milliseconds, signal) {
|
|
124
|
-
let cancel;
|
|
125
|
-
try {
|
|
126
|
-
await withSignal(() => new Promise((resolve) => {
|
|
127
|
-
cancel = scheduleNetworkDeadline(milliseconds, resolve);
|
|
128
|
-
}), signal);
|
|
129
|
-
} finally {
|
|
130
|
-
cancel?.();
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
function header(headers, name) {
|
|
134
|
-
return headers.find(([key]) => key.toLowerCase() === name.toLowerCase())?.[1];
|
|
135
|
-
}
|
|
136
|
-
function networkError(error) {
|
|
137
|
-
if (error instanceof CurlError) return error;
|
|
138
|
-
const code = error && typeof error === "object" && "code" in error ? String(error.code) : "";
|
|
139
|
-
if (["ENOTFOUND", "EAI_AGAIN"].includes(code)) return new CurlError(6, "Could not resolve host");
|
|
140
|
-
if (["ECONNREFUSED", "ENETUNREACH", "EHOSTUNREACH"].includes(code)) return new CurlError(7, "Failed to connect");
|
|
141
|
-
if (/CERT|SELF_SIGNED|UNABLE_TO_VERIFY/.test(code)) return new CurlError(60, "TLS certificate verification failed");
|
|
142
|
-
if (/TLS|SSL/.test(code)) return new CurlError(35, "TLS handshake failed");
|
|
143
|
-
return new CurlError(56, "Network transfer failed");
|
|
144
|
-
}
|
|
145
|
-
async function diagnostic(context, error) {
|
|
146
|
-
await (0, escaping_exports.writeDiagnostic)(context.stderr, `${context.command}: (${error.exitCode}) ${error.message}
|
|
147
|
-
`, context.signal);
|
|
148
|
-
}
|
|
149
|
-
async function collectNetworkBytes(source, options) {
|
|
150
|
-
if (Number.isFinite(options.maxBytes)) return (0, contracts_exports.collectBytes)(source, options);
|
|
151
|
-
const chunks = [];
|
|
152
|
-
let size = 0;
|
|
153
|
-
for await (const chunk of (0, contracts_exports.readBytes)(source, options.signal)) {
|
|
154
|
-
chunks.push(new Uint8Array(chunk));
|
|
155
|
-
size += chunk.length;
|
|
156
|
-
}
|
|
157
|
-
const bytes = new Uint8Array(size);
|
|
158
|
-
let offset = 0;
|
|
159
|
-
for (const chunk of chunks) {
|
|
160
|
-
bytes.set(chunk, offset);
|
|
161
|
-
offset += chunk.length;
|
|
162
|
-
}
|
|
163
|
-
return bytes;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// packages/safe-bash/src/commands/network/args.ts
|
|
167
|
-
init_platform();
|
|
168
|
-
|
|
169
|
-
// packages/safe-bash/src/commands/network/platform-portable.ts
|
|
170
|
-
init_platform();
|
|
171
|
-
var requiresFiniteUrlLimits = true;
|
|
172
|
-
function createDefaultHttpTransport() {
|
|
173
|
-
throw new TypeError("Portable network commands require an explicit HTTP transport; inject createFetchTransport() or a host transport");
|
|
174
|
-
}
|
|
175
|
-
function randomBytes(length) {
|
|
176
|
-
const bytes = new Uint8Array(length);
|
|
177
|
-
globalThis.crypto.getRandomValues(bytes);
|
|
178
|
-
return bytes;
|
|
179
|
-
}
|
|
180
|
-
function validateHeaderName(name) {
|
|
181
|
-
if (!name.length) throw new TypeError("Invalid HTTP header name");
|
|
182
|
-
for (const character of name) {
|
|
183
|
-
const code = character.charCodeAt(0);
|
|
184
|
-
if (!(code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122 || "!#$%&'*+-.^_`|~".includes(character))) {
|
|
185
|
-
throw new TypeError("Invalid HTTP header name");
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
function validateHeaderValue(_name, value) {
|
|
190
|
-
for (const character of value) {
|
|
191
|
-
const code = character.charCodeAt(0);
|
|
192
|
-
if (code !== 9 && (code < 32 || code === 127 || code > 255)) throw new TypeError("Invalid HTTP header value");
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// packages/safe-bash/src/commands/network/args.ts
|
|
197
|
-
var values = {
|
|
198
|
-
C: "continue-at",
|
|
199
|
-
X: "request",
|
|
200
|
-
d: "data",
|
|
201
|
-
H: "header",
|
|
202
|
-
u: "user",
|
|
203
|
-
A: "user-agent",
|
|
204
|
-
e: "referer",
|
|
205
|
-
o: "output",
|
|
206
|
-
D: "dump-header",
|
|
207
|
-
w: "write-out",
|
|
208
|
-
T: "upload-file",
|
|
209
|
-
m: "max-time",
|
|
210
|
-
F: "form",
|
|
211
|
-
r: "range"
|
|
212
|
-
};
|
|
213
|
-
var flags = {
|
|
214
|
-
L: "location",
|
|
215
|
-
I: "head",
|
|
216
|
-
i: "include",
|
|
217
|
-
f: "fail",
|
|
218
|
-
s: "silent",
|
|
219
|
-
S: "show-error",
|
|
220
|
-
G: "get",
|
|
221
|
-
O: "remote-name",
|
|
222
|
-
v: "verbose",
|
|
223
|
-
q: "disable",
|
|
224
|
-
N: "no-buffer",
|
|
225
|
-
g: "globoff",
|
|
226
|
-
h: "help",
|
|
227
|
-
V: "version"
|
|
228
|
-
};
|
|
229
|
-
var longValues = /* @__PURE__ */ new Set([
|
|
230
|
-
...Object.values(values),
|
|
231
|
-
"data-ascii",
|
|
232
|
-
"data-raw",
|
|
233
|
-
"data-binary",
|
|
234
|
-
"data-urlencode",
|
|
235
|
-
"url-query",
|
|
236
|
-
"json",
|
|
237
|
-
"form-string",
|
|
238
|
-
"url",
|
|
239
|
-
"oauth2-bearer",
|
|
240
|
-
"max-redirs",
|
|
241
|
-
"max-filesize",
|
|
242
|
-
"retry",
|
|
243
|
-
"retry-delay",
|
|
244
|
-
"connect-timeout",
|
|
245
|
-
"output-dir",
|
|
246
|
-
"cacert",
|
|
247
|
-
"etag-save",
|
|
248
|
-
"etag-compare"
|
|
249
|
-
]);
|
|
250
|
-
var booleans = {
|
|
251
|
-
location: "location",
|
|
252
|
-
head: "head",
|
|
253
|
-
include: "include",
|
|
254
|
-
"show-headers": "include",
|
|
255
|
-
get: "get",
|
|
256
|
-
"remote-name": "remoteName",
|
|
257
|
-
fail: "fail",
|
|
258
|
-
"fail-with-body": "failWithBody",
|
|
259
|
-
silent: "silent",
|
|
260
|
-
"show-error": "showError",
|
|
261
|
-
verbose: "verbose",
|
|
262
|
-
globoff: "globoff",
|
|
263
|
-
compressed: "compressed",
|
|
264
|
-
raw: "raw",
|
|
265
|
-
"ignore-content-length": "ignoreContentLength"
|
|
266
|
-
};
|
|
267
|
-
function number(value, integral = false) {
|
|
268
|
-
if (!(integral ? /^\d+$/ : /^\d+(?:\.\d+)?$/).test(value)) throw new CurlError(2, "Invalid numeric option");
|
|
269
|
-
const parsed = Number(value);
|
|
270
|
-
if (!Number.isFinite(parsed) || parsed < 0 || parsed > Number.MAX_SAFE_INTEGER) throw new CurlError(2, "Invalid numeric option");
|
|
271
|
-
return parsed;
|
|
272
|
-
}
|
|
273
|
-
function timeoutSeconds(value) {
|
|
274
|
-
const invalid = () => {
|
|
275
|
-
throw new CurlError(2, "Invalid numeric option");
|
|
276
|
-
};
|
|
277
|
-
let index = 0;
|
|
278
|
-
while (index < value.length && " \n\r\v\f".includes(value[index])) index++;
|
|
279
|
-
const negative = value[index] === "-";
|
|
280
|
-
if (negative || value[index] === "+") index++;
|
|
281
|
-
const hexadecimal = value.slice(index, index + 2).toLowerCase() === "0x";
|
|
282
|
-
if (hexadecimal) index += 2;
|
|
283
|
-
const digits = hexadecimal ? "0123456789abcdef" : "0123456789";
|
|
284
|
-
const radix = hexadecimal ? 16 : 10;
|
|
285
|
-
let mantissa = 0;
|
|
286
|
-
let digitCount = 0;
|
|
287
|
-
let fractionDigits = 0;
|
|
288
|
-
let point = false;
|
|
289
|
-
for (; index < value.length; index++) {
|
|
290
|
-
const character = value[index].toLowerCase();
|
|
291
|
-
const digit = digits.indexOf(character);
|
|
292
|
-
if (digit >= 0) {
|
|
293
|
-
mantissa = mantissa * radix + digit;
|
|
294
|
-
digitCount++;
|
|
295
|
-
if (point) fractionDigits++;
|
|
296
|
-
} else if (character === "." && !point) point = true;
|
|
297
|
-
else break;
|
|
298
|
-
}
|
|
299
|
-
if (!digitCount) invalid();
|
|
300
|
-
let exponent = 0;
|
|
301
|
-
if (value[index]?.toLowerCase() === (hexadecimal ? "p" : "e")) {
|
|
302
|
-
index++;
|
|
303
|
-
const exponentNegative = value[index] === "-";
|
|
304
|
-
if (exponentNegative || value[index] === "+") index++;
|
|
305
|
-
const start = index;
|
|
306
|
-
while (index < value.length && "0123456789".includes(value[index])) index++;
|
|
307
|
-
if (index === start) invalid();
|
|
308
|
-
exponent = Number(value.slice(start, index)) * (exponentNegative ? -1 : 1);
|
|
309
|
-
}
|
|
310
|
-
if (index !== value.length) invalid();
|
|
311
|
-
const parsed = hexadecimal ? (negative ? -1 : 1) * mantissa * 2 ** (exponent - fractionDigits * 4) : Number(value);
|
|
312
|
-
if (!Number.isFinite(parsed) || parsed < 0 || parsed > Number.MAX_SAFE_INTEGER) invalid();
|
|
313
|
-
return parsed;
|
|
314
|
-
}
|
|
315
|
-
function validateRequestHeader(name, value) {
|
|
316
|
-
try {
|
|
317
|
-
validateHeaderName(name);
|
|
318
|
-
validateHeaderValue(name, value);
|
|
319
|
-
} catch {
|
|
320
|
-
throw new CurlError(2, "Invalid HTTP header");
|
|
321
|
-
}
|
|
322
|
-
if (["host", "content-length", "transfer-encoding", "connection", "proxy-authorization", "upgrade", "expect"].includes(name.toLowerCase())) {
|
|
323
|
-
throw new CurlError(2, "Transport-controlled HTTP header is not supported");
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
function addHeader(result, raw) {
|
|
327
|
-
const colon = raw.indexOf(":");
|
|
328
|
-
const semicolon = colon < 0 && raw.endsWith(";");
|
|
329
|
-
if (colon < 1 && !semicolon) throw new CurlError(2, "Invalid HTTP header");
|
|
330
|
-
const name = semicolon ? raw.slice(0, -1) : raw.slice(0, colon);
|
|
331
|
-
const value = semicolon ? "" : raw.slice(colon + 1).trim();
|
|
332
|
-
validateRequestHeader(name, value);
|
|
333
|
-
result.headers.push([name, value === "" && !semicolon ? null : value]);
|
|
334
|
-
}
|
|
335
|
-
function parseArguments(args, limits) {
|
|
336
|
-
const result = {
|
|
337
|
-
urls: [],
|
|
338
|
-
data: [],
|
|
339
|
-
headers: [],
|
|
340
|
-
include: false,
|
|
341
|
-
head: false,
|
|
342
|
-
get: false,
|
|
343
|
-
location: false,
|
|
344
|
-
remoteName: false,
|
|
345
|
-
fail: false,
|
|
346
|
-
failWithBody: false,
|
|
347
|
-
silent: false,
|
|
348
|
-
showError: false,
|
|
349
|
-
verbose: false,
|
|
350
|
-
globoff: false,
|
|
351
|
-
help: false,
|
|
352
|
-
version: false,
|
|
353
|
-
retries: 0,
|
|
354
|
-
retryDelayMs: 0,
|
|
355
|
-
maxTimeMs: limits.maxTimeMs,
|
|
356
|
-
maxRedirects: limits.maxRedirects,
|
|
357
|
-
maxFileSize: limits.maxDownloadBytes
|
|
358
|
-
};
|
|
359
|
-
const apply = (option, value) => {
|
|
360
|
-
const positive = option.startsWith("no-") ? option.slice(3) : option;
|
|
361
|
-
const property = Object.hasOwn(booleans, positive) ? booleans[positive] : void 0;
|
|
362
|
-
if (property) {
|
|
363
|
-
Object.assign(result, { [property]: !option.startsWith("no-") });
|
|
364
|
-
if (property === "remoteName") {
|
|
365
|
-
(result.outputs ??= []).push({ remoteName: result.remoteName });
|
|
366
|
-
if (result.remoteName) delete result.output;
|
|
367
|
-
}
|
|
368
|
-
return;
|
|
369
|
-
}
|
|
370
|
-
switch (option) {
|
|
371
|
-
case "continue-at": {
|
|
372
|
-
if (value === "-") result.continueAt = "auto";
|
|
373
|
-
else {
|
|
374
|
-
if (!value || [...value].some((character) => !"0123456789".includes(character)) || !Number.isSafeInteger(Number(value))) {
|
|
375
|
-
throw new CurlError(2, "Invalid resume offset");
|
|
376
|
-
}
|
|
377
|
-
result.continueAt = Number(value);
|
|
378
|
-
}
|
|
379
|
-
break;
|
|
380
|
-
}
|
|
381
|
-
case "etag-save":
|
|
382
|
-
result.etagSave = value;
|
|
383
|
-
break;
|
|
384
|
-
case "etag-compare":
|
|
385
|
-
result.etagCompare = value;
|
|
386
|
-
break;
|
|
387
|
-
case "cacert":
|
|
388
|
-
result.caFile = value;
|
|
389
|
-
break;
|
|
390
|
-
case "http1.0":
|
|
391
|
-
result.httpVersion = "1.0";
|
|
392
|
-
break;
|
|
393
|
-
case "http1.1":
|
|
394
|
-
result.httpVersion = "1.1";
|
|
395
|
-
break;
|
|
396
|
-
case "url-query":
|
|
397
|
-
(result.query ??= []).push({ kind: value.startsWith("+") ? "raw" : "urlencode", value: value.startsWith("+") ? value.slice(1) : value });
|
|
398
|
-
break;
|
|
399
|
-
case "request":
|
|
400
|
-
if (!value || !/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(value) || ["CONNECT", "TRACE"].includes(value.toUpperCase())) {
|
|
401
|
-
throw new CurlError(2, "Invalid or unsupported HTTP method");
|
|
402
|
-
}
|
|
403
|
-
result.method = value;
|
|
404
|
-
break;
|
|
405
|
-
case "header":
|
|
406
|
-
addHeader(result, value);
|
|
407
|
-
break;
|
|
408
|
-
case "range":
|
|
409
|
-
try {
|
|
410
|
-
validateHeaderValue("Range", value);
|
|
411
|
-
} catch {
|
|
412
|
-
throw new CurlError(2, "Invalid byte range");
|
|
413
|
-
}
|
|
414
|
-
result.range = value;
|
|
415
|
-
break;
|
|
416
|
-
case "user-agent":
|
|
417
|
-
addHeader(result, `User-Agent: ${value}`);
|
|
418
|
-
break;
|
|
419
|
-
case "referer": {
|
|
420
|
-
validateRequestHeader("Referer", value);
|
|
421
|
-
const auto = value.indexOf(";auto");
|
|
422
|
-
result.autoReferer = auto >= 0;
|
|
423
|
-
result.referer = auto >= 0 ? value.slice(0, auto) : value;
|
|
424
|
-
break;
|
|
425
|
-
}
|
|
426
|
-
case "user":
|
|
427
|
-
if (!value.includes(":")) throw new CurlError(2, "Basic authentication requires user:password; prompting is unsupported");
|
|
428
|
-
result.user = value;
|
|
429
|
-
break;
|
|
430
|
-
case "oauth2-bearer":
|
|
431
|
-
result.bearer = value;
|
|
432
|
-
break;
|
|
433
|
-
case "data":
|
|
434
|
-
case "data-ascii":
|
|
435
|
-
result.data.push({ kind: "data", value });
|
|
436
|
-
break;
|
|
437
|
-
case "data-raw":
|
|
438
|
-
result.data.push({ kind: "raw", value });
|
|
439
|
-
break;
|
|
440
|
-
case "data-binary":
|
|
441
|
-
result.data.push({ kind: "binary", value });
|
|
442
|
-
break;
|
|
443
|
-
case "data-urlencode":
|
|
444
|
-
result.data.push({ kind: "urlencode", value });
|
|
445
|
-
break;
|
|
446
|
-
case "json":
|
|
447
|
-
case "form":
|
|
448
|
-
case "form-string":
|
|
449
|
-
result.data.push({ kind: option, value });
|
|
450
|
-
break;
|
|
451
|
-
case "output":
|
|
452
|
-
result.output = value;
|
|
453
|
-
result.remoteName = false;
|
|
454
|
-
(result.outputs ??= []).push({ output: value, remoteName: false });
|
|
455
|
-
break;
|
|
456
|
-
case "output-dir":
|
|
457
|
-
result.outputDirectory = value;
|
|
458
|
-
break;
|
|
459
|
-
case "dump-header":
|
|
460
|
-
result.dumpHeader = value;
|
|
461
|
-
break;
|
|
462
|
-
case "write-out":
|
|
463
|
-
result.writeOut = value;
|
|
464
|
-
break;
|
|
465
|
-
case "upload-file":
|
|
466
|
-
result.upload = value;
|
|
467
|
-
break;
|
|
468
|
-
case "url":
|
|
469
|
-
result.urls.push(value);
|
|
470
|
-
break;
|
|
471
|
-
case "max-time": {
|
|
472
|
-
const milliseconds = timeoutSeconds(value) * 1e3;
|
|
473
|
-
result.maxTimeMs = Math.min(milliseconds === 0 ? Infinity : milliseconds, limits.maxTimeMs);
|
|
474
|
-
break;
|
|
475
|
-
}
|
|
476
|
-
case "connect-timeout": {
|
|
477
|
-
const milliseconds = timeoutSeconds(value) * 1e3;
|
|
478
|
-
if (milliseconds === 0) delete result.connectTimeoutMs;
|
|
479
|
-
else result.connectTimeoutMs = Math.min(milliseconds, limits.maxTimeMs);
|
|
480
|
-
break;
|
|
481
|
-
}
|
|
482
|
-
case "max-filesize":
|
|
483
|
-
result.maxFileSize = Math.min(number(value, true), limits.maxDownloadBytes);
|
|
484
|
-
break;
|
|
485
|
-
case "max-redirs":
|
|
486
|
-
result.maxRedirects = Math.min(number(value, true), limits.maxRedirects);
|
|
487
|
-
break;
|
|
488
|
-
case "retry":
|
|
489
|
-
result.retries = Math.min(number(value, true), limits.maxRetries);
|
|
490
|
-
break;
|
|
491
|
-
case "retry-delay":
|
|
492
|
-
result.retryDelayMs = Math.min(number(value) * 1e3, limits.maxTimeMs);
|
|
493
|
-
break;
|
|
494
|
-
case "help":
|
|
495
|
-
result.help = true;
|
|
496
|
-
break;
|
|
497
|
-
case "version":
|
|
498
|
-
result.version = true;
|
|
499
|
-
break;
|
|
500
|
-
case "disable":
|
|
501
|
-
case "no-buffer":
|
|
502
|
-
case "no-progress-meter":
|
|
503
|
-
case "basic":
|
|
504
|
-
break;
|
|
505
|
-
default:
|
|
506
|
-
throw new CurlError(2, "Unsupported curl option");
|
|
507
|
-
}
|
|
508
|
-
};
|
|
509
|
-
let ended = false;
|
|
510
|
-
for (let index = 0; index < args.length; index++) {
|
|
511
|
-
const argument = args[index];
|
|
512
|
-
if (ended || !argument.startsWith("-") || argument === "-") {
|
|
513
|
-
result.urls.push(argument);
|
|
514
|
-
continue;
|
|
515
|
-
}
|
|
516
|
-
if (argument === "--") {
|
|
517
|
-
ended = true;
|
|
518
|
-
continue;
|
|
519
|
-
}
|
|
520
|
-
if (argument.startsWith("--")) {
|
|
521
|
-
const equals = argument.indexOf("=");
|
|
522
|
-
const name = argument.slice(2, equals < 0 ? void 0 : equals);
|
|
523
|
-
let value = equals < 0 ? void 0 : argument.slice(equals + 1);
|
|
524
|
-
if (longValues.has(name)) {
|
|
525
|
-
value ??= args[++index];
|
|
526
|
-
if (value === void 0) throw new CurlError(2, "Option requires an argument");
|
|
527
|
-
} else if (value !== void 0) throw new CurlError(2, "Unexpected option argument");
|
|
528
|
-
apply(name, value);
|
|
529
|
-
} else {
|
|
530
|
-
for (let offset = 1; offset < argument.length; offset++) {
|
|
531
|
-
const name = argument[offset];
|
|
532
|
-
if (values[name]) {
|
|
533
|
-
const value = argument.slice(offset + 1) || args[++index];
|
|
534
|
-
if (value === void 0) throw new CurlError(2, "Option requires an argument");
|
|
535
|
-
apply(values[name], value);
|
|
536
|
-
break;
|
|
537
|
-
}
|
|
538
|
-
if (!flags[name]) throw new CurlError(2, "Unsupported curl option");
|
|
539
|
-
apply(flags[name]);
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
if (!result.help && !result.version && !result.urls.length) throw new CurlError(2, "No URL specified");
|
|
544
|
-
if (result.urls.length > limits.maxUrls) throw new CurlError(2, "URL count exceeds host limit");
|
|
545
|
-
if (result.fail && result.failWithBody) throw new CurlError(2, "--fail and --fail-with-body are mutually exclusive");
|
|
546
|
-
const form = result.data.some((part) => part.kind === "form" || part.kind === "form-string");
|
|
547
|
-
if (result.upload !== void 0 && result.data.length || form && result.data.some((part) => !part.kind.startsWith("form")) || result.get && (form || result.upload !== void 0) || result.head && (result.upload !== void 0 || result.data.length > 0) && !result.get) {
|
|
548
|
-
throw new CurlError(2, "Incompatible request body options");
|
|
549
|
-
}
|
|
550
|
-
if (result.data.some((part) => part.kind === "json") && result.data.some((part) => part.kind !== "json")) {
|
|
551
|
-
throw new CurlError(2, "Mixing JSON and other data modes is unsupported");
|
|
552
|
-
}
|
|
553
|
-
return result;
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
// packages/safe-bash/src/commands/network/curl.ts
|
|
557
|
-
init_platform();
|
|
558
|
-
|
|
559
|
-
// packages/safe-bash/src/commands/network/aggregate.ts
|
|
560
|
-
init_platform();
|
|
561
|
-
function createDeadlineOutput(context, destination, remaining) {
|
|
562
|
-
const lifetime = new AbortController();
|
|
563
|
-
const signal = AbortSignal.any([context.signal, lifetime.signal]);
|
|
564
|
-
const operation = (0, contracts_exports.createOutputOperation)({ ...context, signal }, destination);
|
|
565
|
-
if (operation.signal.aborted) return operation;
|
|
566
|
-
const cancel = scheduleNetworkDeadline(remaining, () => lifetime.abort(new CurlError(28, "Operation timed out")));
|
|
567
|
-
operation.registerCleanup(cancel);
|
|
568
|
-
return operation;
|
|
569
|
-
}
|
|
570
|
-
async function deadlineDiagnostic(context, error, remaining) {
|
|
571
|
-
const operation = createDeadlineOutput(context, context.stderr, remaining);
|
|
572
|
-
try {
|
|
573
|
-
await diagnostic({ ...context, stderr: operation.output, signal: operation.signal }, error);
|
|
574
|
-
} finally {
|
|
575
|
-
await operation.close();
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
// packages/safe-bash/src/commands/network/url.ts
|
|
580
|
-
init_platform();
|
|
581
|
-
function curlRequestTarget(text) {
|
|
582
|
-
const scheme = text.indexOf("://");
|
|
583
|
-
if (scheme < 0) throw new CurlError(3, "Malformed URL; explicit HTTP(S) scheme required");
|
|
584
|
-
let start = scheme + 3;
|
|
585
|
-
while (start < text.length && !"/?#".includes(text[start])) {
|
|
586
|
-
if (text[start] === "\\") throw new CurlError(3, "Malformed URL authority");
|
|
587
|
-
start++;
|
|
588
|
-
}
|
|
589
|
-
const fragment = text.indexOf("#", start);
|
|
590
|
-
const target = text.slice(start, fragment < 0 ? void 0 : fragment);
|
|
591
|
-
const queryIndex = target.indexOf("?");
|
|
592
|
-
const path = queryIndex < 0 ? target : target.slice(0, queryIndex);
|
|
593
|
-
const query = queryIndex < 0 ? "" : target.slice(queryIndex);
|
|
594
|
-
const segments = [];
|
|
595
|
-
for (const segment of (path || "/").split("/")) {
|
|
596
|
-
if (segment === "..") {
|
|
597
|
-
if (segments.length > 1) segments.pop();
|
|
598
|
-
} else if (segment !== ".") segments.push(segment);
|
|
599
|
-
}
|
|
600
|
-
if (path.endsWith("/.") || path.endsWith("/..")) segments.push("");
|
|
601
|
-
return (segments.join("/") || "/") + query;
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
// packages/safe-bash/src/commands/network/glob.ts
|
|
605
|
-
init_platform();
|
|
606
|
-
function decimal(text) {
|
|
607
|
-
if (!text || ![...text].every((char) => char >= "0" && char <= "9")) throw new CurlError(3, "Malformed URL range");
|
|
608
|
-
const value = Number(text);
|
|
609
|
-
if (!Number.isSafeInteger(value)) throw new CurlError(3, "Malformed URL range");
|
|
610
|
-
return value;
|
|
611
|
-
}
|
|
612
|
-
function range(text, maximum, admit) {
|
|
613
|
-
const parts = text.split(":");
|
|
614
|
-
if (parts.length > 2) throw new CurlError(3, "Malformed URL range");
|
|
615
|
-
const ends = parts[0].split("-");
|
|
616
|
-
if (ends.length !== 2) throw new CurlError(3, "Malformed URL range");
|
|
617
|
-
const [left, right] = ends;
|
|
618
|
-
const character = left.length === 1 && right.length === 1 && (left >= "a" && right <= "z" && right >= "a" || left >= "A" && right <= "Z" && right >= "A");
|
|
619
|
-
const start = character ? left.charCodeAt(0) : decimal(left);
|
|
620
|
-
const end = character ? right.charCodeAt(0) : decimal(right);
|
|
621
|
-
const step = parts[1] === void 0 ? 1 : decimal(parts[1]);
|
|
622
|
-
if (start > end || step === 0 || step > end - start + 1) throw new CurlError(3, "Malformed URL range");
|
|
623
|
-
const count = Math.floor((end - start) / step) + 1;
|
|
624
|
-
if (count > maximum) throw new CurlError(2, "URL count exceeds host limit");
|
|
625
|
-
const width = !character && left.startsWith("0") ? left.length : 0;
|
|
626
|
-
admit(count, count * Math.max(width, character ? 1 : String(end).length));
|
|
627
|
-
return Array.from({ length: count }, (_, index) => character ? String.fromCharCode(start + index * step) : String(start + index * step).padStart(width, "0"));
|
|
628
|
-
}
|
|
629
|
-
function expandUrls(urls, globoff, limits) {
|
|
630
|
-
const result = [];
|
|
631
|
-
let bytes = 0;
|
|
632
|
-
let work = 0;
|
|
633
|
-
const charge = (size) => {
|
|
634
|
-
work += size;
|
|
635
|
-
if (work > limits.maxBufferBytes) throw new CurlError(2, "URL expansion work exceeds host limit");
|
|
636
|
-
};
|
|
637
|
-
for (const input of urls) {
|
|
638
|
-
let expanded = [{ url: "", captures: [] }];
|
|
639
|
-
const append = (suffix) => {
|
|
640
|
-
const retained = expanded.reduce((total, item) => total + import_buffer.Buffer.byteLength(item.url) + import_buffer.Buffer.byteLength(suffix) + item.captures.reduce((size, value) => size + import_buffer.Buffer.byteLength(value), 0), bytes);
|
|
641
|
-
if (retained > limits.maxBufferBytes) throw new CurlError(2, "Expanded URLs exceed host buffer limit");
|
|
642
|
-
charge(retained - bytes);
|
|
643
|
-
expanded = expanded.map((item) => ({ ...item, url: item.url + suffix }));
|
|
644
|
-
};
|
|
645
|
-
for (let index = 0; index < input.length; ) {
|
|
646
|
-
const char = input[index];
|
|
647
|
-
if (globoff) {
|
|
648
|
-
append(input.slice(index));
|
|
649
|
-
break;
|
|
650
|
-
}
|
|
651
|
-
if (char === "\\" && "{}[]".includes(input[index + 1] ?? " ")) {
|
|
652
|
-
append(input[index + 1]);
|
|
653
|
-
index += 2;
|
|
654
|
-
continue;
|
|
655
|
-
}
|
|
656
|
-
if (char !== "{" && char !== "[") {
|
|
657
|
-
if (char === "}" || char === "]") throw new CurlError(3, "Unmatched URL glob delimiter");
|
|
658
|
-
let end = index + 1;
|
|
659
|
-
while (end < input.length && !"{}[]\\".includes(input[end])) end++;
|
|
660
|
-
append(input.slice(index, end));
|
|
661
|
-
index = end;
|
|
662
|
-
continue;
|
|
663
|
-
}
|
|
664
|
-
const close = input.indexOf(char === "{" ? "}" : "]", index + 1);
|
|
665
|
-
if (close < 0) throw new CurlError(3, "Unmatched URL glob delimiter");
|
|
666
|
-
const content = input.slice(index + 1, close);
|
|
667
|
-
if (char === "{" && content === "") throw new CurlError(3, "Malformed URL glob");
|
|
668
|
-
if (char === "[" && content.includes(":") && input.slice(0, index).endsWith("//")) {
|
|
669
|
-
append(input.slice(index, close + 1));
|
|
670
|
-
index = close + 1;
|
|
671
|
-
continue;
|
|
672
|
-
}
|
|
673
|
-
if ([...content].some((value) => "{}[]".includes(value))) throw new CurlError(3, "Nested URL globs are unsupported");
|
|
674
|
-
const maximum = Math.floor((limits.maxUrls - result.length) / expanded.length);
|
|
675
|
-
const admit = (count, valueBytes) => {
|
|
676
|
-
const prefixBytes = expanded.reduce((total, item) => total + import_buffer.Buffer.byteLength(item.url) + item.captures.reduce((size, capture) => size + import_buffer.Buffer.byteLength(capture), 0), 0);
|
|
677
|
-
const retained = count * prefixBytes + expanded.length * valueBytes * 2;
|
|
678
|
-
if (retained + bytes > limits.maxBufferBytes) throw new CurlError(2, "Expanded URLs exceed host buffer limit");
|
|
679
|
-
charge(retained);
|
|
680
|
-
};
|
|
681
|
-
const values2 = char === "{" ? content.split(",") : range(content, maximum, admit);
|
|
682
|
-
if (values2.length > maximum) throw new CurlError(2, "URL count exceeds host limit");
|
|
683
|
-
if (char === "{") admit(values2.length, values2.reduce((total, value) => total + import_buffer.Buffer.byteLength(value), 0));
|
|
684
|
-
const next = [];
|
|
685
|
-
for (const item of expanded) for (const value of values2) {
|
|
686
|
-
next.push({ url: item.url + value, captures: [...item.captures, value] });
|
|
687
|
-
}
|
|
688
|
-
expanded = next;
|
|
689
|
-
index = close + 1;
|
|
690
|
-
}
|
|
691
|
-
for (const item of expanded) {
|
|
692
|
-
bytes += import_buffer.Buffer.byteLength(item.url) + item.captures.reduce((total, value) => total + import_buffer.Buffer.byteLength(value), 0);
|
|
693
|
-
if (bytes > limits.maxBufferBytes) throw new CurlError(2, "Expanded URLs exceed host buffer limit");
|
|
694
|
-
if (result.length >= limits.maxUrls) throw new CurlError(2, "URL count exceeds host limit");
|
|
695
|
-
result.push(item);
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
return result;
|
|
699
|
-
}
|
|
700
|
-
function globFilename(template, captures) {
|
|
701
|
-
let output = "";
|
|
702
|
-
for (let index = 0; index < template.length; ) {
|
|
703
|
-
if (template[index] !== "#") {
|
|
704
|
-
output += template[index++];
|
|
705
|
-
continue;
|
|
706
|
-
}
|
|
707
|
-
let end = index + 1;
|
|
708
|
-
while (end < template.length && template[end] >= "0" && template[end] <= "9") end++;
|
|
709
|
-
const number2 = Number(template.slice(index + 1, end));
|
|
710
|
-
const capture = captures[number2 - 1];
|
|
711
|
-
output += capture === void 0 ? template.slice(index, end) : capture;
|
|
712
|
-
index = end;
|
|
713
|
-
}
|
|
714
|
-
return output;
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
// packages/safe-bash/src/commands/network/input.ts
|
|
718
|
-
init_platform();
|
|
719
|
-
function whitespace(character) {
|
|
720
|
-
return " \r\n\v\f".includes(character);
|
|
721
|
-
}
|
|
722
|
-
function configArguments(text) {
|
|
723
|
-
const args = [];
|
|
724
|
-
for (const line of text.split("\n")) {
|
|
725
|
-
let offset = 0;
|
|
726
|
-
while (offset < line.length && whitespace(line[offset])) offset++;
|
|
727
|
-
if (offset === line.length || line[offset] === "#") continue;
|
|
728
|
-
const start = offset;
|
|
729
|
-
const dashed = line[offset] === "-";
|
|
730
|
-
while (offset < line.length && !whitespace(line[offset]) && (dashed || !"=:".includes(line[offset]))) offset++;
|
|
731
|
-
const option = line.slice(start, offset);
|
|
732
|
-
args.push(dashed ? option : `--${option}`);
|
|
733
|
-
while (offset < line.length && whitespace(line[offset])) offset++;
|
|
734
|
-
if (!dashed && "=:".includes(line[offset] ?? "\0")) offset++;
|
|
735
|
-
while (offset < line.length && whitespace(line[offset])) offset++;
|
|
736
|
-
if (offset === line.length) continue;
|
|
737
|
-
let value = "";
|
|
738
|
-
if (line[offset] === '"') {
|
|
739
|
-
offset++;
|
|
740
|
-
let closed = false;
|
|
741
|
-
while (offset < line.length) {
|
|
742
|
-
let character = line[offset++];
|
|
743
|
-
if (character === '"') {
|
|
744
|
-
closed = true;
|
|
745
|
-
break;
|
|
746
|
-
}
|
|
747
|
-
if (character === "\\" && offset < line.length) {
|
|
748
|
-
character = line[offset++];
|
|
749
|
-
character = { n: "\n", r: "\r", t: " ", v: "\v" }[character] ?? character;
|
|
750
|
-
}
|
|
751
|
-
value += character;
|
|
752
|
-
}
|
|
753
|
-
if (!closed) throw new CurlError(2, "Unterminated config string");
|
|
754
|
-
if (line.slice(offset).trim()) throw new CurlError(2, "Unexpected config string suffix");
|
|
755
|
-
} else value = line.slice(offset).trimEnd();
|
|
756
|
-
args.push(value);
|
|
757
|
-
}
|
|
758
|
-
return args;
|
|
759
|
-
}
|
|
760
|
-
function variableName(name) {
|
|
761
|
-
return name.length > 0 && [...name].every((character) => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_".includes(character));
|
|
762
|
-
}
|
|
763
|
-
async function parseCurlInput(context, limits) {
|
|
764
|
-
const variables = /* @__PURE__ */ new Map();
|
|
765
|
-
const output = [];
|
|
766
|
-
let inputBytes = 0;
|
|
767
|
-
let outputBytes = 0;
|
|
768
|
-
let optionCount = 0;
|
|
769
|
-
const admit = (value) => {
|
|
770
|
-
inputBytes += import_buffer.Buffer.byteLength(value);
|
|
771
|
-
if (inputBytes > limits.maxBufferBytes) throw new CurlError(2, "Curl input exceeds host buffer limit");
|
|
772
|
-
return value;
|
|
773
|
-
};
|
|
774
|
-
const append = (value) => {
|
|
775
|
-
outputBytes += import_buffer.Buffer.byteLength(value);
|
|
776
|
-
if (outputBytes > limits.maxBufferBytes) throw new CurlError(2, "Expanded arguments exceed host buffer limit");
|
|
777
|
-
output.push(value);
|
|
778
|
-
};
|
|
779
|
-
const read = async (file) => {
|
|
780
|
-
try {
|
|
781
|
-
const maxBytes = limits.maxBufferBytes - inputBytes;
|
|
782
|
-
const bytes = file === "-" ? await collectNetworkBytes(context.stdin, { signal: context.signal, maxBytes }) : await withSignal(() => context.fs.readFile((0, path_exports.pathOf)(context, file), { signal: context.signal, ...Number.isFinite(maxBytes) ? { maxBytes } : {} }), context.signal);
|
|
783
|
-
context.signal.throwIfAborted();
|
|
784
|
-
inputBytes += bytes.length;
|
|
785
|
-
if (inputBytes > limits.maxBufferBytes) throw new CurlError(2, "Curl input exceeds host buffer limit");
|
|
786
|
-
return import_buffer.Buffer.from(bytes);
|
|
787
|
-
} catch (error) {
|
|
788
|
-
context.signal.throwIfAborted();
|
|
789
|
-
if (error instanceof CurlError) throw error;
|
|
790
|
-
throw new CurlError(26, "Failed to read virtual curl input");
|
|
791
|
-
}
|
|
792
|
-
};
|
|
793
|
-
const expand = (value) => {
|
|
794
|
-
let result = "";
|
|
795
|
-
let size = 0;
|
|
796
|
-
let offset = 0;
|
|
797
|
-
const appendPiece = (piece) => {
|
|
798
|
-
size += import_buffer.Buffer.byteLength(piece);
|
|
799
|
-
if (size > limits.maxBufferBytes) throw new CurlError(2, "Variable expansion exceeds host buffer limit");
|
|
800
|
-
result += piece;
|
|
801
|
-
};
|
|
802
|
-
while (offset < value.length) {
|
|
803
|
-
const start = value.indexOf("{{", offset);
|
|
804
|
-
if (start < 0) {
|
|
805
|
-
appendPiece(value.slice(offset));
|
|
806
|
-
break;
|
|
807
|
-
}
|
|
808
|
-
appendPiece(value.slice(offset, start));
|
|
809
|
-
const end = value.indexOf("}}", start + 2);
|
|
810
|
-
if (end < 0) {
|
|
811
|
-
appendPiece(value.slice(start));
|
|
812
|
-
break;
|
|
813
|
-
}
|
|
814
|
-
const [name, ...functions] = value.slice(start + 2, end).split(":");
|
|
815
|
-
if (!variableName(name)) throw new CurlError(2, "Invalid expansion variable");
|
|
816
|
-
let bytes = variables.get(name) ?? import_buffer.Buffer.alloc(0);
|
|
817
|
-
for (const functionName of functions) {
|
|
818
|
-
const requireSpace = (length) => {
|
|
819
|
-
if (length > limits.maxBufferBytes) throw new CurlError(2, "Variable transformation exceeds host buffer limit");
|
|
820
|
-
};
|
|
821
|
-
switch (functionName) {
|
|
822
|
-
case "trim": {
|
|
823
|
-
let start2 = 0, end2 = bytes.length;
|
|
824
|
-
while (start2 < end2 && whitespace(String.fromCharCode(bytes[start2]))) start2++;
|
|
825
|
-
while (end2 > start2 && whitespace(String.fromCharCode(bytes[end2 - 1]))) end2--;
|
|
826
|
-
bytes = bytes.subarray(start2, end2);
|
|
827
|
-
break;
|
|
828
|
-
}
|
|
829
|
-
case "json": {
|
|
830
|
-
requireSpace(bytes.length * 6);
|
|
831
|
-
bytes = import_buffer.Buffer.from(JSON.stringify(new TextDecoder("utf-8", { fatal: true }).decode(bytes)).slice(1, -1));
|
|
832
|
-
break;
|
|
833
|
-
}
|
|
834
|
-
case "url": {
|
|
835
|
-
requireSpace(bytes.length * 3);
|
|
836
|
-
let encoded = "";
|
|
837
|
-
for (const byte of bytes) {
|
|
838
|
-
const character = String.fromCharCode(byte);
|
|
839
|
-
encoded += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~".includes(character) ? character : `%${byte.toString(16).toUpperCase().padStart(2, "0")}`;
|
|
840
|
-
}
|
|
841
|
-
bytes = import_buffer.Buffer.from(encoded);
|
|
842
|
-
break;
|
|
843
|
-
}
|
|
844
|
-
case "b64":
|
|
845
|
-
requireSpace(Math.ceil(bytes.length / 3) * 4);
|
|
846
|
-
bytes = import_buffer.Buffer.from(bytes.toString("base64"));
|
|
847
|
-
break;
|
|
848
|
-
default:
|
|
849
|
-
throw new CurlError(2, "Unsupported variable expansion function");
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
let replacement;
|
|
853
|
-
try {
|
|
854
|
-
replacement = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
855
|
-
} catch {
|
|
856
|
-
throw new CurlError(2, "Binary variable requires url or b64 expansion");
|
|
857
|
-
}
|
|
858
|
-
if (replacement.includes("\0")) throw new CurlError(2, "Variable expansion contains NUL");
|
|
859
|
-
appendPiece(replacement);
|
|
860
|
-
offset = end + 2;
|
|
861
|
-
}
|
|
862
|
-
return result;
|
|
863
|
-
};
|
|
864
|
-
const define = async (value) => {
|
|
865
|
-
const fromEnv = value.startsWith("%");
|
|
866
|
-
const spec = fromEnv ? value.slice(1) : value;
|
|
867
|
-
let delimiter = spec.indexOf("=");
|
|
868
|
-
const at = spec.indexOf("@");
|
|
869
|
-
if (delimiter < 0 || at >= 0 && at < delimiter) delimiter = at;
|
|
870
|
-
const name = delimiter < 0 ? spec : spec.slice(0, delimiter);
|
|
871
|
-
if (!variableName(name)) throw new CurlError(2, "Invalid variable name");
|
|
872
|
-
let content = fromEnv && context.env[name] !== void 0 ? import_buffer.Buffer.from(admit(context.env[name])) : void 0;
|
|
873
|
-
if (content === void 0) {
|
|
874
|
-
if (delimiter < 0) {
|
|
875
|
-
if (fromEnv) throw new CurlError(2, "Required environment variable is unset");
|
|
876
|
-
content = import_buffer.Buffer.alloc(0);
|
|
877
|
-
} else content = spec[delimiter] === "@" ? await read(spec.slice(delimiter + 1)) : import_buffer.Buffer.from(admit(spec.slice(delimiter + 1)));
|
|
878
|
-
}
|
|
879
|
-
variables.set(name, content);
|
|
880
|
-
};
|
|
881
|
-
let ended = false;
|
|
882
|
-
const visit = async (args, depth) => {
|
|
883
|
-
if (depth > 16) throw new CurlError(2, "Config nesting exceeds host limit");
|
|
884
|
-
for (let index = 0; index < args.length; index++) {
|
|
885
|
-
context.signal.throwIfAborted();
|
|
886
|
-
if (++optionCount > limits.maxBufferBytes) throw new CurlError(2, "Curl option count exceeds host limit");
|
|
887
|
-
const argument = args[index];
|
|
888
|
-
if (ended || argument === "-" || !argument.startsWith("-")) {
|
|
889
|
-
append(argument);
|
|
890
|
-
continue;
|
|
891
|
-
}
|
|
892
|
-
if (argument === "--") {
|
|
893
|
-
ended = true;
|
|
894
|
-
append(argument);
|
|
895
|
-
continue;
|
|
896
|
-
}
|
|
897
|
-
const apply = async (name, attached) => {
|
|
898
|
-
const expanded = name.startsWith("expand-");
|
|
899
|
-
const base = expanded ? name.slice(7) : name;
|
|
900
|
-
const takesValue = longValues.has(base) || base === "config" || base === "variable";
|
|
901
|
-
if (expanded && !longValues.has(base)) throw new CurlError(2, "Unsupported expand option");
|
|
902
|
-
if (!takesValue) {
|
|
903
|
-
if (attached !== void 0) throw new CurlError(2, "Unexpected option argument");
|
|
904
|
-
append(`--${name}`);
|
|
905
|
-
return;
|
|
906
|
-
}
|
|
907
|
-
const value = attached ?? args[++index];
|
|
908
|
-
if (value === void 0) throw new CurlError(2, "Option requires an argument");
|
|
909
|
-
const resolved = expanded ? expand(value) : value;
|
|
910
|
-
if (base === "config") await visit(configArguments((await read(resolved)).toString("utf8")), depth + 1);
|
|
911
|
-
else if (base === "variable") await define(resolved);
|
|
912
|
-
else if (base === "header" && resolved.startsWith("@")) {
|
|
913
|
-
const text = (await read(resolved.slice(1))).toString("utf8");
|
|
914
|
-
for (const raw of text.split("\n")) {
|
|
915
|
-
context.signal.throwIfAborted();
|
|
916
|
-
const line = raw.endsWith("\r") ? raw.slice(0, -1) : raw;
|
|
917
|
-
if (!line || line.startsWith("#")) continue;
|
|
918
|
-
append("--header");
|
|
919
|
-
append(line);
|
|
920
|
-
}
|
|
921
|
-
} else {
|
|
922
|
-
append(`--${base}`);
|
|
923
|
-
append(resolved);
|
|
924
|
-
}
|
|
925
|
-
};
|
|
926
|
-
if (argument.startsWith("--")) {
|
|
927
|
-
const equals = argument.indexOf("=");
|
|
928
|
-
await apply(argument.slice(2, equals < 0 ? void 0 : equals), equals < 0 ? void 0 : argument.slice(equals + 1));
|
|
929
|
-
} else {
|
|
930
|
-
for (let offset = 1; offset < argument.length; offset++) {
|
|
931
|
-
const short = argument[offset];
|
|
932
|
-
const name = short === "K" ? "config" : values[short];
|
|
933
|
-
if (name) {
|
|
934
|
-
await apply(name, argument.slice(offset + 1) || void 0);
|
|
935
|
-
break;
|
|
936
|
-
}
|
|
937
|
-
if (!flags[short]) throw new CurlError(2, "Unsupported curl option");
|
|
938
|
-
await apply(flags[short]);
|
|
939
|
-
}
|
|
940
|
-
}
|
|
941
|
-
}
|
|
942
|
-
};
|
|
943
|
-
await visit(context.args, 0);
|
|
944
|
-
return parseArguments(output, limits);
|
|
945
|
-
}
|
|
946
|
-
|
|
947
|
-
// packages/safe-bash/src/commands/network/body.ts
|
|
948
|
-
init_platform();
|
|
949
|
-
|
|
950
|
-
// packages/safe-bash/src/commands/network/form-headers.ts
|
|
951
|
-
init_platform();
|
|
952
|
-
function validatePartHeader(header2) {
|
|
953
|
-
const colon = header2.indexOf(":");
|
|
954
|
-
if (colon <= 0) throw new CurlError(2, "Invalid multipart part header");
|
|
955
|
-
for (const character of header2.slice(0, colon)) {
|
|
956
|
-
const code = character.charCodeAt(0);
|
|
957
|
-
if (!(code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122 || "!#$%&'*+-.^_`|~".includes(character)))
|
|
958
|
-
throw new CurlError(2, "Invalid multipart part header");
|
|
959
|
-
}
|
|
960
|
-
for (const character of header2) {
|
|
961
|
-
const code = character.charCodeAt(0);
|
|
962
|
-
if (code < 32 && code !== 9 || code === 127)
|
|
963
|
-
throw new CurlError(2, "Invalid multipart part header");
|
|
964
|
-
}
|
|
965
|
-
return header2;
|
|
966
|
-
}
|
|
967
|
-
function filePartHeaders(bytes) {
|
|
968
|
-
const headers = [];
|
|
969
|
-
for (let line of import_buffer.Buffer.from(bytes).toString("utf8").split("\n")) {
|
|
970
|
-
if (line.endsWith("\r")) line = line.slice(0, -1);
|
|
971
|
-
if (!line || line.startsWith("#")) continue;
|
|
972
|
-
if ((line.startsWith(" ") || line.startsWith(" ")) && headers.length) {
|
|
973
|
-
headers[headers.length - 1] += " " + line.trim();
|
|
974
|
-
validatePartHeader(headers[headers.length - 1]);
|
|
975
|
-
} else headers.push(validatePartHeader(line));
|
|
976
|
-
}
|
|
977
|
-
return headers;
|
|
978
|
-
}
|
|
979
|
-
|
|
980
|
-
// packages/safe-bash/src/commands/network/form-content-type.ts
|
|
981
|
-
init_platform();
|
|
982
|
-
function token(character) {
|
|
983
|
-
if (character === void 0) return false;
|
|
984
|
-
const code = character.charCodeAt(0);
|
|
985
|
-
return code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122 || "!#$%&'*+-.^_`|~".includes(character);
|
|
986
|
-
}
|
|
987
|
-
function formContentType(input, start, fileList = false) {
|
|
988
|
-
let end = start;
|
|
989
|
-
const invalid = () => {
|
|
990
|
-
throw new CurlError(2, "Invalid multipart content type");
|
|
991
|
-
};
|
|
992
|
-
const readToken = () => {
|
|
993
|
-
const begin = end;
|
|
994
|
-
while (token(input[end])) end++;
|
|
995
|
-
if (end === begin) invalid();
|
|
996
|
-
};
|
|
997
|
-
const whitespace2 = () => {
|
|
998
|
-
while (input[end] === " " || input[end] === " ") end++;
|
|
999
|
-
};
|
|
1000
|
-
readToken();
|
|
1001
|
-
if (input[end++] !== "/") invalid();
|
|
1002
|
-
readToken();
|
|
1003
|
-
whitespace2();
|
|
1004
|
-
while (end < input.length) {
|
|
1005
|
-
if (fileList && input[end] === ",") break;
|
|
1006
|
-
if (input[end] !== ";") invalid();
|
|
1007
|
-
const separator = end++;
|
|
1008
|
-
whitespace2();
|
|
1009
|
-
if (["type=", "filename=", "encoder=", "headers="].some((attribute) => input.startsWith(attribute, end))) {
|
|
1010
|
-
return { value: input.slice(start, separator), end: separator };
|
|
1011
|
-
}
|
|
1012
|
-
readToken();
|
|
1013
|
-
whitespace2();
|
|
1014
|
-
if (input[end++] !== "=") invalid();
|
|
1015
|
-
whitespace2();
|
|
1016
|
-
if (input[end] === '"') {
|
|
1017
|
-
end++;
|
|
1018
|
-
let closed = false;
|
|
1019
|
-
while (end < input.length) {
|
|
1020
|
-
const character = input[end++];
|
|
1021
|
-
if (character === '"') {
|
|
1022
|
-
closed = true;
|
|
1023
|
-
break;
|
|
1024
|
-
}
|
|
1025
|
-
const literal = character === "\\" ? input[end++] : character;
|
|
1026
|
-
if (literal === void 0 || literal.charCodeAt(0) < 32 || literal.charCodeAt(0) === 127) invalid();
|
|
1027
|
-
}
|
|
1028
|
-
if (!closed) invalid();
|
|
1029
|
-
} else readToken();
|
|
1030
|
-
whitespace2();
|
|
1031
|
-
}
|
|
1032
|
-
return { value: input.slice(start, end), end };
|
|
1033
|
-
}
|
|
1034
|
-
|
|
1035
|
-
// packages/safe-bash/src/commands/network/body.ts
|
|
1036
|
-
function multipartBoundary() {
|
|
1037
|
-
let boundary = "virtual-bash-";
|
|
1038
|
-
for (const byte of randomBytes(18)) boundary += byte.toString(16).padStart(2, "0");
|
|
1039
|
-
return boundary;
|
|
1040
|
-
}
|
|
1041
|
-
function percent(bytes) {
|
|
1042
|
-
let output = "";
|
|
1043
|
-
for (const byte of bytes) output += byte >= 65 && byte <= 90 || byte >= 97 && byte <= 122 || byte >= 48 && byte <= 57 || [45, 46, 95, 126].includes(byte) ? String.fromCharCode(byte) : byte === 32 ? "+" : `%${byte.toString(16).toUpperCase().padStart(2, "0")}`;
|
|
1044
|
-
return encode(output);
|
|
1045
|
-
}
|
|
1046
|
-
function dataPart(argument) {
|
|
1047
|
-
const value = argument.value;
|
|
1048
|
-
if (argument.kind === "urlencode") {
|
|
1049
|
-
const equals = value.indexOf("=");
|
|
1050
|
-
if (equals >= 0) return [{ bytes: encode(equals ? `${value.slice(0, equals)}=` : "") }, { bytes: encode(value.slice(equals + 1)), urlencode: true }];
|
|
1051
|
-
const at = value.indexOf("@");
|
|
1052
|
-
if (at >= 0) return [{ prefix: encode(at ? `${value.slice(0, at)}=` : ""), file: value.slice(at + 1), urlencode: true }];
|
|
1053
|
-
return [{ bytes: encode(value), urlencode: true }];
|
|
1054
|
-
}
|
|
1055
|
-
if (argument.kind !== "raw" && value.startsWith("@")) return [{ file: value.slice(1), strip: argument.kind === "data" }];
|
|
1056
|
-
return [{ bytes: encode(value) }];
|
|
1057
|
-
}
|
|
1058
|
-
function quoted(value) {
|
|
1059
|
-
if (/[\r\n\0]/.test(value)) throw new CurlError(2, "Invalid multipart field metadata");
|
|
1060
|
-
return value.replace(/["\\]/g, "\\$&");
|
|
1061
|
-
}
|
|
1062
|
-
function formWord(input, start, file) {
|
|
1063
|
-
const delimiter = (character) => character === ";" || file && character === ",";
|
|
1064
|
-
if (input[start] === '"') {
|
|
1065
|
-
let value = "";
|
|
1066
|
-
for (let index = start + 1; index < input.length; index++) {
|
|
1067
|
-
const character = input[index];
|
|
1068
|
-
if (character === '"') {
|
|
1069
|
-
let end2 = index + 1;
|
|
1070
|
-
while (end2 < input.length && !delimiter(input[end2])) end2++;
|
|
1071
|
-
return { value, end: end2 };
|
|
1072
|
-
}
|
|
1073
|
-
if (character === "\\" && (input[index + 1] === "\\" || input[index + 1] === '"')) {
|
|
1074
|
-
value += input[++index];
|
|
1075
|
-
} else value += character;
|
|
1076
|
-
}
|
|
1077
|
-
}
|
|
1078
|
-
let end = start;
|
|
1079
|
-
while (end < input.length && !delimiter(input[end])) end++;
|
|
1080
|
-
return { value: input.slice(start, end), end };
|
|
1081
|
-
}
|
|
1082
|
-
var multipartContentTypes = /* @__PURE__ */ new Map([
|
|
1083
|
-
["gif", "image/gif"],
|
|
1084
|
-
["jpg", "image/jpeg"],
|
|
1085
|
-
["jpeg", "image/jpeg"],
|
|
1086
|
-
["png", "image/png"],
|
|
1087
|
-
["svg", "image/svg+xml"],
|
|
1088
|
-
["txt", "text/plain"],
|
|
1089
|
-
["html", "text/html"],
|
|
1090
|
-
["htm", "text/html"],
|
|
1091
|
-
["pdf", "application/pdf"],
|
|
1092
|
-
["xml", "application/xml"]
|
|
1093
|
-
]);
|
|
1094
|
-
function filenameContentType(filename) {
|
|
1095
|
-
const basename = posixPath.basename(filename);
|
|
1096
|
-
const dot = basename.lastIndexOf(".");
|
|
1097
|
-
return dot < 0 ? void 0 : multipartContentTypes.get(basename.slice(dot + 1).toLowerCase());
|
|
1098
|
-
}
|
|
1099
|
-
function multipart(argument, boundary) {
|
|
1100
|
-
const equals = argument.value.indexOf("=");
|
|
1101
|
-
if (equals < 0) throw new CurlError(2, "Multipart form requires name=value");
|
|
1102
|
-
const name = quoted(argument.value.slice(0, equals));
|
|
1103
|
-
const input = argument.value.slice(equals + 1);
|
|
1104
|
-
const upload = argument.kind === "form" && input.startsWith("@");
|
|
1105
|
-
const isFile = argument.kind === "form" && (upload || input.startsWith("<"));
|
|
1106
|
-
const entries = [];
|
|
1107
|
-
let start = isFile ? 1 : 0;
|
|
1108
|
-
do {
|
|
1109
|
-
const word = argument.kind === "form" ? formWord(input, start, isFile) : { value: input, end: input.length };
|
|
1110
|
-
const entry = { value: word.value };
|
|
1111
|
-
if (isFile) {
|
|
1112
|
-
if (!word.value) throw new CurlError(2, "Empty multipart file operand");
|
|
1113
|
-
entry.file = word.value;
|
|
1114
|
-
if (upload) entry.filename = posixPath.basename(word.value);
|
|
1115
|
-
}
|
|
1116
|
-
let end = word.end;
|
|
1117
|
-
while (end < input.length && input[end] !== ",") {
|
|
1118
|
-
const attributeStart = end + 1;
|
|
1119
|
-
if (input.startsWith("type=", attributeStart)) {
|
|
1120
|
-
const attribute = formContentType(input, attributeStart + 5, upload);
|
|
1121
|
-
entry.type = attribute.value;
|
|
1122
|
-
end = attribute.end;
|
|
1123
|
-
} else if (input.startsWith("filename=", attributeStart)) {
|
|
1124
|
-
const attribute = formWord(input, attributeStart + 9, upload);
|
|
1125
|
-
entry.filename = attribute.value;
|
|
1126
|
-
end = attribute.end;
|
|
1127
|
-
} else if (input.startsWith("headers=", attributeStart)) {
|
|
1128
|
-
const file = input[attributeStart + 8] === "@";
|
|
1129
|
-
const attribute = formWord(input, attributeStart + 8 + (file ? 1 : 0), upload);
|
|
1130
|
-
if (!attribute.value) throw new CurlError(2, "Empty multipart header operand");
|
|
1131
|
-
(entry.headers ??= []).push({ value: file ? attribute.value : validatePartHeader(attribute.value), file });
|
|
1132
|
-
end = attribute.end;
|
|
1133
|
-
} else if (input.startsWith("encoder=", attributeStart)) {
|
|
1134
|
-
const attribute = formWord(input, attributeStart + 8, upload);
|
|
1135
|
-
entry.encoder = attribute.value.toLowerCase();
|
|
1136
|
-
end = attribute.end;
|
|
1137
|
-
if (!["binary", "8bit", "7bit", "base64", "quoted-printable"].includes(entry.encoder))
|
|
1138
|
-
throw new CurlError(2, "Unsupported multipart transfer encoder");
|
|
1139
|
-
} else throw new CurlError(2, "Unsupported multipart form attribute");
|
|
1140
|
-
}
|
|
1141
|
-
entries.push(entry);
|
|
1142
|
-
if (end === input.length) break;
|
|
1143
|
-
if (!upload) throw new CurlError(2, "Unsupported multipart file list");
|
|
1144
|
-
start = end + 1;
|
|
1145
|
-
} while (start <= input.length);
|
|
1146
|
-
const mixed = entries.length > 1;
|
|
1147
|
-
const childBoundary = mixed ? multipartBoundary() : boundary;
|
|
1148
|
-
const disposition = `form-data${name ? `; name="${name}"` : ""}`;
|
|
1149
|
-
const parts = [];
|
|
1150
|
-
if (mixed) parts.push({ bytes: encode(`--${boundary}\r
|
|
1151
|
-
Content-Disposition: ${disposition}\r
|
|
1152
|
-
Content-Type: multipart/mixed; boundary=${childBoundary}\r
|
|
1153
|
-
\r
|
|
1154
|
-
`) });
|
|
1155
|
-
for (const entry of entries) {
|
|
1156
|
-
let preamble = `--${childBoundary}\r
|
|
1157
|
-
Content-Disposition: ${mixed ? "attachment" : disposition}`;
|
|
1158
|
-
if (entry.filename !== void 0) preamble += `; filename="${quoted(entry.filename)}"`;
|
|
1159
|
-
preamble += "\r\n";
|
|
1160
|
-
if (entry.type || entry.filename !== void 0) {
|
|
1161
|
-
const inferred = entry.filename === void 0 ? void 0 : filenameContentType(entry.filename) ?? (entry.file === void 0 ? void 0 : filenameContentType(entry.file));
|
|
1162
|
-
preamble += `Content-Type: ${entry.type ?? inferred ?? "application/octet-stream"}\r
|
|
1163
|
-
`;
|
|
1164
|
-
}
|
|
1165
|
-
if (entry.encoder) preamble += `Content-Transfer-Encoding: ${entry.encoder}\r
|
|
1166
|
-
`;
|
|
1167
|
-
const encoding = entry.encoder === void 0 ? {} : { encoder: entry.encoder };
|
|
1168
|
-
parts.push({ bytes: encode(preamble), headers: entry.headers ?? [] }, entry.file !== void 0 ? { file: entry.file, ...encoding } : { bytes: encode(entry.value), ...encoding }, { bytes: encode("\r\n") });
|
|
1169
|
-
}
|
|
1170
|
-
if (mixed) parts.push({ bytes: encode(`--${childBoundary}--\r
|
|
1171
|
-
\r
|
|
1172
|
-
`) });
|
|
1173
|
-
return parts;
|
|
1174
|
-
}
|
|
1175
|
-
async function* transfer(source, encoder, signal) {
|
|
1176
|
-
if (!encoder || encoder === "binary" || encoder === "8bit") {
|
|
1177
|
-
yield* source;
|
|
1178
|
-
return;
|
|
1179
|
-
}
|
|
1180
|
-
let pending = [];
|
|
1181
|
-
let column = 0;
|
|
1182
|
-
let output = "";
|
|
1183
|
-
const quotedByte = (byte, next, after) => {
|
|
1184
|
-
if (byte === 13 && next === 10) {
|
|
1185
|
-
output += "\r\n";
|
|
1186
|
-
column = 0;
|
|
1187
|
-
return 2;
|
|
1188
|
-
}
|
|
1189
|
-
const trailing = next === void 0 || next === 13 && after === 10;
|
|
1190
|
-
const literal = byte >= 33 && byte <= 126 && byte !== 61 || (byte === 32 || byte === 9) && !trailing;
|
|
1191
|
-
const token2 = literal ? String.fromCharCode(byte) : `=${byte.toString(16).toUpperCase().padStart(2, "0")}`;
|
|
1192
|
-
if (column + token2.length > 75) {
|
|
1193
|
-
output += "=\r\n";
|
|
1194
|
-
column = 0;
|
|
1195
|
-
}
|
|
1196
|
-
output += token2;
|
|
1197
|
-
column += token2.length;
|
|
1198
|
-
return 1;
|
|
1199
|
-
};
|
|
1200
|
-
for await (const raw of source) {
|
|
1201
|
-
for (let offset = 0; offset < raw.length; offset += 16 * 1024) {
|
|
1202
|
-
signal.throwIfAborted();
|
|
1203
|
-
const chunk = raw.subarray(offset, offset + 16 * 1024);
|
|
1204
|
-
if (encoder === "7bit") {
|
|
1205
|
-
if (chunk.some((byte) => byte > 127)) throw new CurlError(26, "Non-ASCII byte in 7bit multipart data");
|
|
1206
|
-
yield chunk;
|
|
1207
|
-
continue;
|
|
1208
|
-
}
|
|
1209
|
-
pending.push(...chunk);
|
|
1210
|
-
let consumed = 0;
|
|
1211
|
-
if (encoder === "base64") {
|
|
1212
|
-
while (pending.length - consumed >= 57) {
|
|
1213
|
-
if (column) output += "\r\n";
|
|
1214
|
-
output += import_buffer.Buffer.from(pending.slice(consumed, consumed + 57)).toString("base64");
|
|
1215
|
-
consumed += 57;
|
|
1216
|
-
column = 76;
|
|
1217
|
-
}
|
|
1218
|
-
} else {
|
|
1219
|
-
while (pending.length - consumed >= 3)
|
|
1220
|
-
consumed += quotedByte(pending[consumed], pending[consumed + 1], pending[consumed + 2]);
|
|
1221
|
-
}
|
|
1222
|
-
pending = pending.slice(consumed);
|
|
1223
|
-
if (output) {
|
|
1224
|
-
yield encode(output);
|
|
1225
|
-
output = "";
|
|
1226
|
-
}
|
|
1227
|
-
await (0, yield_exports.yieldTurn)(signal);
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
signal.throwIfAborted();
|
|
1231
|
-
if (encoder === "base64" && pending.length) {
|
|
1232
|
-
if (column) output += "\r\n";
|
|
1233
|
-
output += import_buffer.Buffer.from(pending).toString("base64");
|
|
1234
|
-
} else if (encoder === "quoted-printable") {
|
|
1235
|
-
for (let i = 0; i < pending.length; )
|
|
1236
|
-
i += quotedByte(pending[i], pending[i + 1], pending[i + 2]);
|
|
1237
|
-
}
|
|
1238
|
-
if (output) yield encode(output);
|
|
1239
|
-
}
|
|
1240
|
-
function createBody(context, args, limits) {
|
|
1241
|
-
if (!args.data.length && args.upload === void 0) return void 0;
|
|
1242
|
-
const parts = [];
|
|
1243
|
-
let contentType;
|
|
1244
|
-
const form = args.data.some((part) => part.kind.startsWith("form"));
|
|
1245
|
-
if (form) {
|
|
1246
|
-
const boundary = multipartBoundary();
|
|
1247
|
-
contentType = `multipart/form-data; boundary=${boundary}`;
|
|
1248
|
-
for (const argument of args.data) parts.push(...multipart(argument, boundary));
|
|
1249
|
-
parts.push({ bytes: encode(`--${boundary}--\r
|
|
1250
|
-
`) });
|
|
1251
|
-
} else if (args.upload !== void 0) parts.push({ file: args.upload });
|
|
1252
|
-
else {
|
|
1253
|
-
const json = args.data[0]?.kind === "json";
|
|
1254
|
-
contentType = json ? "application/json" : "application/x-www-form-urlencoded";
|
|
1255
|
-
args.data.forEach((argument, index) => {
|
|
1256
|
-
if (index && !json) parts.push({ bytes: encode("&"), separator: true });
|
|
1257
|
-
parts.push(...dataPart(argument));
|
|
1258
|
-
});
|
|
1259
|
-
}
|
|
1260
|
-
const hasStdin = parts.some((part) => part.file === "-");
|
|
1261
|
-
let opened = false;
|
|
1262
|
-
let replayComplete = false;
|
|
1263
|
-
let replayable = true;
|
|
1264
|
-
let cachedBytes = 0;
|
|
1265
|
-
let cache = [];
|
|
1266
|
-
let stdinUsed = false;
|
|
1267
|
-
const source = async function* (part, signal) {
|
|
1268
|
-
if (part.bytes !== void 0) {
|
|
1269
|
-
if (!part.headers) {
|
|
1270
|
-
yield part.bytes;
|
|
1271
|
-
return;
|
|
1272
|
-
}
|
|
1273
|
-
const headers = [];
|
|
1274
|
-
let headerBytes = 0;
|
|
1275
|
-
for (const header2 of part.headers) {
|
|
1276
|
-
signal.throwIfAborted();
|
|
1277
|
-
if (header2.file) {
|
|
1278
|
-
const bytes = await collectNetworkBytes(source({ file: header2.value }, signal), {
|
|
1279
|
-
signal,
|
|
1280
|
-
maxBytes: Math.min(limits.maxBufferBytes, limits.maxUploadBytes) - headerBytes
|
|
1281
|
-
});
|
|
1282
|
-
headerBytes += bytes.length;
|
|
1283
|
-
headers.push(...filePartHeaders(bytes));
|
|
1284
|
-
} else {
|
|
1285
|
-
headerBytes += encode(header2.value).length;
|
|
1286
|
-
if (headerBytes > Math.min(limits.maxBufferBytes, limits.maxUploadBytes))
|
|
1287
|
-
throw new CurlError(63, "Multipart headers exceed host byte limit");
|
|
1288
|
-
headers.push(header2.value);
|
|
1289
|
-
}
|
|
1290
|
-
}
|
|
1291
|
-
const overridden = new Set(headers.map((header2) => header2.slice(0, header2.indexOf(":")).toLowerCase()));
|
|
1292
|
-
const defaults = import_buffer.Buffer.from(part.bytes).toString("utf8").split("\r\n");
|
|
1293
|
-
const preamble = defaults.filter((line, index) => index === 0 || line && !overridden.has(line.slice(0, line.indexOf(":")).toLowerCase()));
|
|
1294
|
-
yield encode([...preamble, ...headers, "", ""].join("\r\n"));
|
|
1295
|
-
return;
|
|
1296
|
-
}
|
|
1297
|
-
if (part.file === "-") {
|
|
1298
|
-
if (!stdinUsed) {
|
|
1299
|
-
stdinUsed = true;
|
|
1300
|
-
yield* (0, contracts_exports.readBytes)(context.stdin, signal);
|
|
1301
|
-
}
|
|
1302
|
-
return;
|
|
1303
|
-
}
|
|
1304
|
-
try {
|
|
1305
|
-
const path = (0, path_exports.pathOf)(context, part.file);
|
|
1306
|
-
const capabilities = await context.fs.capabilitiesFor?.(path, { signal }) ?? context.fs.capabilities;
|
|
1307
|
-
signal.throwIfAborted();
|
|
1308
|
-
if (context.fs.readStream && capabilities.streamingRead !== false) yield* (0, contracts_exports.readBytes)(context.fs.readStream(path, { signal }), signal);
|
|
1309
|
-
else yield await context.fs.readFile(path, { signal, ...Number.isFinite(Math.min(limits.maxBufferBytes, limits.maxUploadBytes)) ? { maxBytes: Math.min(limits.maxBufferBytes, limits.maxUploadBytes) } : {} });
|
|
1310
|
-
} catch (error) {
|
|
1311
|
-
signal.throwIfAborted();
|
|
1312
|
-
if (error instanceof CurlError) throw error;
|
|
1313
|
-
throw new CurlError(26, "Failed to read virtual upload file");
|
|
1314
|
-
}
|
|
1315
|
-
};
|
|
1316
|
-
return {
|
|
1317
|
-
...contentType === void 0 ? {} : { contentType },
|
|
1318
|
-
open(signal) {
|
|
1319
|
-
return (async function* () {
|
|
1320
|
-
if (hasStdin && opened) {
|
|
1321
|
-
if (!replayComplete || !replayable) throw new CurlError(65, "Cannot replay stdin upload within the host buffer limit");
|
|
1322
|
-
for (const chunk of cache) {
|
|
1323
|
-
signal.throwIfAborted();
|
|
1324
|
-
yield chunk.slice();
|
|
1325
|
-
}
|
|
1326
|
-
return;
|
|
1327
|
-
}
|
|
1328
|
-
opened = true;
|
|
1329
|
-
let count = 0;
|
|
1330
|
-
let chunks = 0;
|
|
1331
|
-
for (const part of parts) {
|
|
1332
|
-
if (part.separator && count === 0) continue;
|
|
1333
|
-
let prefix = part.prefix;
|
|
1334
|
-
for await (const raw of transfer(source(part, signal), part.encoder, signal)) {
|
|
1335
|
-
if (++chunks % 256 === 0) await (0, yield_exports.yieldTurn)(signal);
|
|
1336
|
-
for (let offset = 0; offset < raw.length; offset += 16 * 1024) {
|
|
1337
|
-
signal.throwIfAborted();
|
|
1338
|
-
let chunk = raw.subarray(offset, offset + 16 * 1024);
|
|
1339
|
-
if (part.strip) chunk = chunk.filter((byte) => byte !== 0 && byte !== 10 && byte !== 13);
|
|
1340
|
-
if (part.urlencode) chunk = percent(chunk);
|
|
1341
|
-
count += chunk.length + (prefix?.length ?? 0);
|
|
1342
|
-
if (count > limits.maxUploadBytes) throw new CurlError(63, "Upload exceeds host byte limit");
|
|
1343
|
-
if (prefix?.length) {
|
|
1344
|
-
const prefixed = new Uint8Array(prefix.length + chunk.length);
|
|
1345
|
-
prefixed.set(prefix);
|
|
1346
|
-
prefixed.set(chunk, prefix.length);
|
|
1347
|
-
chunk = prefixed;
|
|
1348
|
-
prefix = void 0;
|
|
1349
|
-
}
|
|
1350
|
-
if (hasStdin && replayable) {
|
|
1351
|
-
cachedBytes += chunk.length;
|
|
1352
|
-
if (cachedBytes > limits.maxBufferBytes) {
|
|
1353
|
-
replayable = false;
|
|
1354
|
-
cache = [];
|
|
1355
|
-
} else cache.push(new Uint8Array(chunk));
|
|
1356
|
-
}
|
|
1357
|
-
if (chunk.length) yield chunk;
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
|
-
}
|
|
1361
|
-
replayComplete = true;
|
|
1362
|
-
})();
|
|
1363
|
-
}
|
|
1364
|
-
};
|
|
1365
|
-
}
|
|
1366
|
-
async function queryData(body, signal, limits) {
|
|
1367
|
-
try {
|
|
1368
|
-
return import_buffer.Buffer.from(await collectNetworkBytes(body.open(signal), { signal, maxBytes: limits.maxBufferBytes })).toString("utf8");
|
|
1369
|
-
} catch (error) {
|
|
1370
|
-
signal.throwIfAborted();
|
|
1371
|
-
if (error instanceof CurlError) throw error;
|
|
1372
|
-
throw new CurlError(63, "Query data exceeds host buffer limit");
|
|
1373
|
-
}
|
|
1374
|
-
}
|
|
1375
|
-
|
|
1376
|
-
// packages/safe-bash/src/commands/network/decode.ts
|
|
1377
|
-
init_platform();
|
|
1378
|
-
var maxEncodingLayers = 4;
|
|
1379
|
-
async function* decodeContent(source, encoding, signal, maxBytes) {
|
|
1380
|
-
let layers = 1;
|
|
1381
|
-
for (const character of encoding) {
|
|
1382
|
-
if (character === "," && ++layers > maxEncodingLayers) throw new CurlError(61, "Too many content encoding layers");
|
|
1383
|
-
}
|
|
1384
|
-
const formats = encoding.split(",").map((value) => value.trim().toLowerCase()).filter((value) => value !== "identity");
|
|
1385
|
-
if (formats.some((value) => value !== "gzip" && value !== "deflate")) throw new CurlError(61, "Unsupported content encoding");
|
|
1386
|
-
if (!formats.length) {
|
|
1387
|
-
yield* source;
|
|
1388
|
-
return;
|
|
1389
|
-
}
|
|
1390
|
-
const iterator = (0, contracts_exports.readBytes)(source, signal)[Symbol.asyncIterator]();
|
|
1391
|
-
let inputError;
|
|
1392
|
-
let inputFailed = false;
|
|
1393
|
-
let stream = new ReadableStream({
|
|
1394
|
-
async pull(controller) {
|
|
1395
|
-
try {
|
|
1396
|
-
const next = await iterator.next();
|
|
1397
|
-
if (next.done) controller.close();
|
|
1398
|
-
else controller.enqueue(new Uint8Array(next.value));
|
|
1399
|
-
} catch (error) {
|
|
1400
|
-
inputFailed = true;
|
|
1401
|
-
inputError = error;
|
|
1402
|
-
controller.error(error);
|
|
1403
|
-
}
|
|
1404
|
-
},
|
|
1405
|
-
async cancel() {
|
|
1406
|
-
await iterator.return?.(void 0);
|
|
1407
|
-
}
|
|
1408
|
-
}, { highWaterMark: 0 });
|
|
1409
|
-
let budgetError;
|
|
1410
|
-
for (const format of formats.reverse()) {
|
|
1411
|
-
let decodedBytes = 0;
|
|
1412
|
-
stream = stream.pipeThrough(new DecompressionStream(format)).pipeThrough(new TransformStream({
|
|
1413
|
-
transform(chunk, controller) {
|
|
1414
|
-
signal.throwIfAborted();
|
|
1415
|
-
decodedBytes += chunk.byteLength;
|
|
1416
|
-
if (decodedBytes > maxBytes) {
|
|
1417
|
-
budgetError = new CurlError(63, "Decoded response exceeds download byte limit");
|
|
1418
|
-
throw budgetError;
|
|
1419
|
-
}
|
|
1420
|
-
controller.enqueue(chunk);
|
|
1421
|
-
}
|
|
1422
|
-
}));
|
|
1423
|
-
}
|
|
1424
|
-
const reader = stream.getReader();
|
|
1425
|
-
try {
|
|
1426
|
-
while (true) {
|
|
1427
|
-
const next = await withSignal(() => reader.read(), signal);
|
|
1428
|
-
if (next.done) return;
|
|
1429
|
-
yield next.value;
|
|
1430
|
-
}
|
|
1431
|
-
} catch {
|
|
1432
|
-
signal.throwIfAborted();
|
|
1433
|
-
if (inputFailed) throw inputError;
|
|
1434
|
-
if (budgetError) throw budgetError;
|
|
1435
|
-
throw new CurlError(61, "Invalid compressed response body");
|
|
1436
|
-
} finally {
|
|
1437
|
-
await reader.cancel().catch(() => void 0);
|
|
1438
|
-
await iterator.return(void 0).catch(() => void 0);
|
|
1439
|
-
reader.releaseLock();
|
|
1440
|
-
}
|
|
1441
|
-
}
|
|
1442
|
-
|
|
1443
|
-
// packages/safe-bash/src/commands/network/output.ts
|
|
1444
|
-
init_platform();
|
|
1445
|
-
|
|
1446
|
-
// packages/safe-bash/src/contracts/filesystem-output.ts
|
|
1447
|
-
var filesystem_output_exports = {};
|
|
1448
|
-
init_platform();
|
|
1449
|
-
__reExport(filesystem_output_exports, filesystem_output_star);
|
|
1450
|
-
import * as filesystem_output_star from "../../safe-bash-contracts/filesystem-output.js";
|
|
1451
|
-
|
|
1452
|
-
// packages/safe-bash/src/commands/network/output.ts
|
|
1453
|
-
function responseHeaders(response, maxBytes) {
|
|
1454
|
-
if (!Number.isInteger(response.status) || response.status < 100 || response.status > 599 || /[\r\n]/.test(response.statusText)) {
|
|
1455
|
-
throw new CurlError(56, "Invalid HTTP response status");
|
|
1456
|
-
}
|
|
1457
|
-
const version = response.httpVersion ?? "1.1";
|
|
1458
|
-
if (!/^\d(?:\.\d)?$/.test(version)) throw new CurlError(56, "Invalid HTTP response version");
|
|
1459
|
-
let text = `HTTP/${version} ${response.status} ${response.statusText}\r
|
|
1460
|
-
`;
|
|
1461
|
-
for (const [name, value] of response.headers) {
|
|
1462
|
-
if (!/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(name) || /[\r\n\0]/.test(value)) throw new CurlError(56, "Invalid HTTP response header");
|
|
1463
|
-
text += `${name}: ${value}\r
|
|
1464
|
-
`;
|
|
1465
|
-
if (import_buffer.Buffer.byteLength(text, "latin1") > maxBytes) throw new CurlError(63, "Response headers exceed host byte limit");
|
|
1466
|
-
}
|
|
1467
|
-
return new Uint8Array(import_buffer.Buffer.from(`${text}\r
|
|
1468
|
-
`, "latin1"));
|
|
1469
|
-
}
|
|
1470
|
-
async function writeOutput(context, path, source, signal, append = false, preserveEmpty = false) {
|
|
1471
|
-
if (path === void 0 || path === "-") {
|
|
1472
|
-
try {
|
|
1473
|
-
for await (const chunk of (0, contracts_exports.readBytes)(source, signal)) {
|
|
1474
|
-
try {
|
|
1475
|
-
await (0, contracts_exports.writeBytes)(context.stdout, chunk, signal);
|
|
1476
|
-
} catch {
|
|
1477
|
-
signal.throwIfAborted();
|
|
1478
|
-
throw new CurlError(23, "Failed writing output");
|
|
1479
|
-
}
|
|
1480
|
-
}
|
|
1481
|
-
} catch (error) {
|
|
1482
|
-
await context.stdout[io_exports.outputFailure]?.(error);
|
|
1483
|
-
throw error;
|
|
1484
|
-
}
|
|
1485
|
-
return;
|
|
1486
|
-
}
|
|
1487
|
-
try {
|
|
1488
|
-
let target = preserveEmpty ? void 0 : await (0, filesystem_output_exports.openFileOutput)({ ...context, signal, outputBudget: "independent" }, (0, path_exports.pathOf)(context, path), append ? "a" : "w");
|
|
1489
|
-
try {
|
|
1490
|
-
for await (const chunk of (0, contracts_exports.readBytes)(source, target?.signal ?? signal)) {
|
|
1491
|
-
if (preserveEmpty && chunk.length === 0) continue;
|
|
1492
|
-
target ??= await (0, filesystem_output_exports.openFileOutput)({ ...context, signal, outputBudget: "independent" }, (0, path_exports.pathOf)(context, path), append ? "a" : "w");
|
|
1493
|
-
await target.sink.write(chunk);
|
|
1494
|
-
}
|
|
1495
|
-
await target?.finish();
|
|
1496
|
-
} catch (error) {
|
|
1497
|
-
await target?.abort(error);
|
|
1498
|
-
throw error;
|
|
1499
|
-
}
|
|
1500
|
-
} catch (error) {
|
|
1501
|
-
signal.throwIfAborted();
|
|
1502
|
-
if (error instanceof CurlError) throw error;
|
|
1503
|
-
throw new CurlError(23, error instanceof contracts_exports.FsError ? `Failed writing virtual output file: ${error.message}` : "Failed writing virtual output file");
|
|
1504
|
-
}
|
|
1505
|
-
}
|
|
1506
|
-
async function dumpHeaders(context, path, bytes, append, signal) {
|
|
1507
|
-
try {
|
|
1508
|
-
if (path === "-") await (0, contracts_exports.writeBytes)(context.stdout, bytes, signal);
|
|
1509
|
-
else {
|
|
1510
|
-
const target = await (0, filesystem_output_exports.openFileOutput)({ ...context, signal, outputBudget: "independent" }, (0, path_exports.pathOf)(context, path), append ? "a" : "w");
|
|
1511
|
-
try {
|
|
1512
|
-
await target.sink.write(bytes);
|
|
1513
|
-
await target.finish();
|
|
1514
|
-
} catch (error) {
|
|
1515
|
-
await target.abort(error);
|
|
1516
|
-
throw error;
|
|
1517
|
-
}
|
|
1518
|
-
}
|
|
1519
|
-
} catch {
|
|
1520
|
-
signal.throwIfAborted();
|
|
1521
|
-
throw new CurlError(23, "Failed writing response headers");
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
function writeOutFormat(format, values2, maxBytes) {
|
|
1525
|
-
let result = "";
|
|
1526
|
-
let bytes = 0;
|
|
1527
|
-
const sizes = /* @__PURE__ */ new Map();
|
|
1528
|
-
const append = (text) => {
|
|
1529
|
-
let size = sizes.get(text);
|
|
1530
|
-
if (size === void 0) {
|
|
1531
|
-
size = import_buffer.Buffer.byteLength(text);
|
|
1532
|
-
sizes.set(text, size);
|
|
1533
|
-
}
|
|
1534
|
-
if (size > maxBytes - bytes) throw new CurlError(63, "Write-out exceeds host buffer limit");
|
|
1535
|
-
bytes += size;
|
|
1536
|
-
result += text;
|
|
1537
|
-
};
|
|
1538
|
-
for (let index = 0; index < format.length; index++) {
|
|
1539
|
-
const character = format[index];
|
|
1540
|
-
if (character === "\\") {
|
|
1541
|
-
const next = format[++index];
|
|
1542
|
-
append(next === "n" ? "\n" : next === "r" ? "\r" : next === "t" ? " " : next === void 0 ? "\\" : `\\${next}`);
|
|
1543
|
-
} else if (character === "%" && format[index + 1] === "%") {
|
|
1544
|
-
append("%");
|
|
1545
|
-
index++;
|
|
1546
|
-
} else if (character === "%" && format[index + 1] === "{") {
|
|
1547
|
-
const end = format.indexOf("}", index + 2);
|
|
1548
|
-
if (end < 0) throw new CurlError(2, "Invalid write-out format");
|
|
1549
|
-
const name = format.slice(index + 2, end);
|
|
1550
|
-
if (!Object.hasOwn(values2, name)) throw new CurlError(2, "Unsupported write-out variable");
|
|
1551
|
-
append(values2[name]);
|
|
1552
|
-
index = end;
|
|
1553
|
-
} else {
|
|
1554
|
-
const start = index;
|
|
1555
|
-
while (index + 1 < format.length && format[index + 1] !== "%" && format[index + 1] !== "\\") index++;
|
|
1556
|
-
append(format.slice(start, index + 1));
|
|
1557
|
-
}
|
|
1558
|
-
}
|
|
1559
|
-
return encode(result);
|
|
1560
|
-
}
|
|
1561
|
-
|
|
1562
|
-
// packages/safe-bash/src/commands/network/curl.ts
|
|
1563
|
-
var retryStatuses = /* @__PURE__ */ new Set([408, 429, 500, 502, 503, 504]);
|
|
1564
|
-
var redirectStatuses = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
|
|
1565
|
-
var writeOutDefaults = Object.freeze({
|
|
1566
|
-
http_code: "000",
|
|
1567
|
-
response_code: "000",
|
|
1568
|
-
url_effective: "",
|
|
1569
|
-
redirect_url: "",
|
|
1570
|
-
content_type: "",
|
|
1571
|
-
size_download: "0",
|
|
1572
|
-
size_upload: "0",
|
|
1573
|
-
num_redirects: "0",
|
|
1574
|
-
num_retries: "0",
|
|
1575
|
-
time_total: "0.000000",
|
|
1576
|
-
exitcode: "0",
|
|
1577
|
-
errormsg: "",
|
|
1578
|
-
filename_effective: "",
|
|
1579
|
-
method: "GET",
|
|
1580
|
-
http_version: ""
|
|
1581
|
-
});
|
|
1582
|
-
function parseUrl(text, redirect = false) {
|
|
1583
|
-
if (/[\s\x00-\x1f\x7f]/.test(text)) throw new CurlError(3, "Malformed URL");
|
|
1584
|
-
let url;
|
|
1585
|
-
try {
|
|
1586
|
-
url = new URL(text);
|
|
1587
|
-
} catch {
|
|
1588
|
-
throw new CurlError(3, "Malformed URL; explicit HTTP(S) scheme required");
|
|
1589
|
-
}
|
|
1590
|
-
if (!["http:", "https:"].includes(url.protocol)) throw new CurlError(1, "Only HTTP and HTTPS are supported");
|
|
1591
|
-
if (!url.hostname) throw new CurlError(3, "Malformed URL");
|
|
1592
|
-
let user;
|
|
1593
|
-
if (url.username || url.password) {
|
|
1594
|
-
if (redirect) throw new CurlError(3, "Redirect URLs may not supply credentials");
|
|
1595
|
-
try {
|
|
1596
|
-
user = `${decodeURIComponent(url.username)}:${decodeURIComponent(url.password)}`;
|
|
1597
|
-
} catch {
|
|
1598
|
-
throw new CurlError(3, "Malformed URL credentials");
|
|
1599
|
-
}
|
|
1600
|
-
url.username = "";
|
|
1601
|
-
url.password = "";
|
|
1602
|
-
}
|
|
1603
|
-
url.hash = "";
|
|
1604
|
-
return { url, ...user === void 0 ? {} : { user } };
|
|
1605
|
-
}
|
|
1606
|
-
function requestHeaders(args, contentType, user, scoped, maxBytes, previous) {
|
|
1607
|
-
const json = args.data[0]?.kind === "json";
|
|
1608
|
-
if (json) contentType = "application/json";
|
|
1609
|
-
const defaults = [["Accept", json ? "application/json" : "*/*"], ["User-Agent", args.agent ?? "virtual-bash-curl/0.0"]];
|
|
1610
|
-
if (contentType !== void 0) defaults.push(["Content-Type", contentType]);
|
|
1611
|
-
if (args.compressed) defaults.push(["Accept-Encoding", "gzip, deflate"]);
|
|
1612
|
-
const referer = args.autoReferer && previous !== void 0 ? previous : args.referer;
|
|
1613
|
-
if (referer) {
|
|
1614
|
-
validateRequestHeader("Referer", referer);
|
|
1615
|
-
defaults.push(["Referer", referer]);
|
|
1616
|
-
}
|
|
1617
|
-
if (scoped && args.etag !== void 0) defaults.push(["If-None-Match", args.etag]);
|
|
1618
|
-
if (args.range !== void 0) defaults.push(["Range", `bytes=${args.range}`]);
|
|
1619
|
-
if (scoped && user !== void 0) defaults.push(["Authorization", `Basic ${import_buffer.Buffer.from(user).toString("base64")}`]);
|
|
1620
|
-
if (scoped && args.bearer !== void 0) {
|
|
1621
|
-
if (/[\r\n\0]/.test(args.bearer)) throw new CurlError(2, "Invalid bearer token");
|
|
1622
|
-
defaults.push(["Authorization", `Bearer ${args.bearer}`]);
|
|
1623
|
-
}
|
|
1624
|
-
const custom = scoped ? args.headers : [];
|
|
1625
|
-
const names = new Set(custom.map(([name]) => name.toLowerCase()));
|
|
1626
|
-
const result = [
|
|
1627
|
-
...defaults.filter(([name]) => !names.has(name.toLowerCase())),
|
|
1628
|
-
...custom.filter((entry) => entry[1] !== null)
|
|
1629
|
-
];
|
|
1630
|
-
if (result.reduce((size, [name, value]) => size + import_buffer.Buffer.byteLength(name + value) + 4, 0) > maxBytes) {
|
|
1631
|
-
throw new CurlError(63, "Request headers exceed host byte limit");
|
|
1632
|
-
}
|
|
1633
|
-
return result;
|
|
1634
|
-
}
|
|
1635
|
-
async function stop(response, signal) {
|
|
1636
|
-
if (!response) return;
|
|
1637
|
-
const cleanup = Promise.resolve().then(() => response.dispose());
|
|
1638
|
-
void cleanup.catch(() => {
|
|
1639
|
-
});
|
|
1640
|
-
if (!signal.aborted) await withSignal(() => cleanup, signal).catch(() => {
|
|
1641
|
-
});
|
|
1642
|
-
}
|
|
1643
|
-
function pipeClosed(error) {
|
|
1644
|
-
return error instanceof Error && "code" in error && error.code === "EPIPE";
|
|
1645
|
-
}
|
|
1646
|
-
function remoteFilename(url) {
|
|
1647
|
-
if (url.pathname === "/") return "curl_response";
|
|
1648
|
-
const name = posixPath.basename(url.pathname);
|
|
1649
|
-
if (!name || name === "/" || name === "." || name === "..") throw new CurlError(23, "URL has no safe remote filename");
|
|
1650
|
-
return name;
|
|
1651
|
-
}
|
|
1652
|
-
async function existingSize(context, output, signal) {
|
|
1653
|
-
try {
|
|
1654
|
-
return (await context.fs.stat((0, path_exports.pathOf)(context, output), { signal })).size;
|
|
1655
|
-
} catch (error) {
|
|
1656
|
-
signal.throwIfAborted();
|
|
1657
|
-
if (error instanceof contracts_exports.FsError && error.code === "ENOENT") return void 0;
|
|
1658
|
-
throw new CurlError(23, "Failed inspecting virtual output file");
|
|
1659
|
-
}
|
|
1660
|
-
}
|
|
1661
|
-
function createCurlCommand(options) {
|
|
1662
|
-
return createTransferCommand(options, {
|
|
1663
|
-
name: "curl",
|
|
1664
|
-
help: "Usage: curl [HTTP(S) URL] [-X METHOD] [-H HEADER] [-d DATA] [-L] [-o VFSFILE]\nExplicit host authorization is required. See network/README.md for supported flags and limits.\n",
|
|
1665
|
-
version: "virtual-bash curl 0.0 (HTTP HTTPS; Node streaming transport)\n",
|
|
1666
|
-
parse: parseCurlInput,
|
|
1667
|
-
status: (code) => code
|
|
1668
|
-
});
|
|
1669
|
-
}
|
|
1670
|
-
function createTransferCommand(options, profile) {
|
|
1671
|
-
if (typeof options?.authorize !== "function") throw new TypeError(`${profile.name} requires an explicit network authorizer`);
|
|
1672
|
-
const limits = limitsFor(options.limits);
|
|
1673
|
-
if (requiresFiniteUrlLimits && (!Number.isFinite(limits.maxUrls) || !Number.isFinite(limits.maxBufferBytes))) {
|
|
1674
|
-
throw new TypeError("Portable network commands require finite maxUrls and maxBufferBytes limits");
|
|
1675
|
-
}
|
|
1676
|
-
const transport = options.transport ?? createDefaultHttpTransport({ maxHeaderBytes: limits.maxHeaderBytes });
|
|
1677
|
-
if (typeof transport !== "function") throw new TypeError("Invalid HTTP transport");
|
|
1678
|
-
const authorize = options.authorize;
|
|
1679
|
-
const executions = /* @__PURE__ */ new WeakMap();
|
|
1680
|
-
const def = {
|
|
1681
|
-
name: profile.name,
|
|
1682
|
-
async execute(context) {
|
|
1683
|
-
context.signal.throwIfAborted();
|
|
1684
|
-
const scope = context.executionScope ?? {};
|
|
1685
|
-
let started = executions.get(scope) ?? performance.now();
|
|
1686
|
-
const parsing = new AbortController();
|
|
1687
|
-
const parsingSignal = AbortSignal.any([context.signal, parsing.signal]);
|
|
1688
|
-
(0, yield_exports.inheritYieldCheckpoint)(context.signal, parsingSignal);
|
|
1689
|
-
const cancelParsingDeadline = scheduleNetworkDeadline(
|
|
1690
|
-
limits.maxTotalTimeMs - (performance.now() - started),
|
|
1691
|
-
() => parsing.abort(new CurlError(28, "Operation timed out"))
|
|
1692
|
-
);
|
|
1693
|
-
let args;
|
|
1694
|
-
let expanded;
|
|
1695
|
-
try {
|
|
1696
|
-
if (context.args.reduce((size, value) => size + import_buffer.Buffer.byteLength(value), 0) > limits.maxBufferBytes) throw new CurlError(2, "Arguments exceed host buffer limit");
|
|
1697
|
-
args = await withSignal(() => profile.parse({ ...context, signal: parsingSignal }, limits), parsingSignal);
|
|
1698
|
-
parsingSignal.throwIfAborted();
|
|
1699
|
-
if (performance.now() - started >= limits.maxTotalTimeMs) throw new CurlError(28, "Operation timed out");
|
|
1700
|
-
if (args.help || args.version) {
|
|
1701
|
-
await (0, contracts_exports.writeBytes)(context.stdout, encode(args.version ? profile.version : profile.help), context.signal);
|
|
1702
|
-
return { exitCode: 0 };
|
|
1703
|
-
}
|
|
1704
|
-
if (args.connectTimeoutMs !== void 0 && transport.supportsConnectTimeout !== true) {
|
|
1705
|
-
throw new CurlError(2, "Transport cannot enforce connection timeout");
|
|
1706
|
-
}
|
|
1707
|
-
if (args.caFile !== void 0 && transport.supportsRequestCa !== true) {
|
|
1708
|
-
throw new CurlError(2, "Transport cannot enforce request CA trust");
|
|
1709
|
-
}
|
|
1710
|
-
if (args.httpVersion !== void 0 && !transport.supportedHttpVersions?.includes(args.httpVersion)) {
|
|
1711
|
-
throw new CurlError(2, "Transport cannot enforce requested HTTP version");
|
|
1712
|
-
}
|
|
1713
|
-
if (args.ignoreContentLength && transport.supportsIgnoreContentLength !== true) {
|
|
1714
|
-
throw new CurlError(2, "Transport cannot enforce ignored Content-Length");
|
|
1715
|
-
}
|
|
1716
|
-
expanded = expandUrls(args.urls, args.globoff, limits);
|
|
1717
|
-
for (const item of expanded) parseUrl(item.url);
|
|
1718
|
-
if (!args.download) {
|
|
1719
|
-
expanded = args.urls.flatMap((url, index) => expandUrls([url], args.globoff, limits).map((item) => ({ ...item, destination: args.outputs?.[index] ?? { remoteName: false } })));
|
|
1720
|
-
}
|
|
1721
|
-
} catch (error) {
|
|
1722
|
-
context.signal.throwIfAborted();
|
|
1723
|
-
const failure = error instanceof CurlError ? error : new CurlError(2, `Invalid ${profile.name} arguments`);
|
|
1724
|
-
const reported = new CurlError(profile.status(failure.exitCode), failure.message);
|
|
1725
|
-
if (failure.exitCode === 28) {
|
|
1726
|
-
try {
|
|
1727
|
-
await deadlineDiagnostic(context, reported, 0);
|
|
1728
|
-
} catch (error2) {
|
|
1729
|
-
context.signal.throwIfAborted();
|
|
1730
|
-
if (!(error2 instanceof CurlError)) throw error2;
|
|
1731
|
-
}
|
|
1732
|
-
} else await diagnostic(context, reported);
|
|
1733
|
-
return { exitCode: profile.status(failure.exitCode) };
|
|
1734
|
-
} finally {
|
|
1735
|
-
cancelParsingDeadline();
|
|
1736
|
-
}
|
|
1737
|
-
started = Math.min(started, executions.get(scope) ?? started);
|
|
1738
|
-
executions.set(scope, started);
|
|
1739
|
-
let exitCode = 0;
|
|
1740
|
-
const headerState = { dumped: false };
|
|
1741
|
-
for (const item of expanded) {
|
|
1742
|
-
context.signal.throwIfAborted();
|
|
1743
|
-
if (performance.now() - started >= limits.maxTotalTimeMs) {
|
|
1744
|
-
const failure = new CurlError(28, "Operation timed out");
|
|
1745
|
-
if (!args.silent || args.showError) {
|
|
1746
|
-
try {
|
|
1747
|
-
await deadlineDiagnostic(context, failure, 0);
|
|
1748
|
-
} catch (error) {
|
|
1749
|
-
context.signal.throwIfAborted();
|
|
1750
|
-
if (!(error instanceof CurlError)) throw error;
|
|
1751
|
-
}
|
|
1752
|
-
}
|
|
1753
|
-
return { exitCode: profile.status(failure.exitCode) };
|
|
1754
|
-
}
|
|
1755
|
-
const transferArgs = { ...args };
|
|
1756
|
-
if (item.destination) {
|
|
1757
|
-
delete transferArgs.output;
|
|
1758
|
-
transferArgs.remoteName = item.destination.remoteName;
|
|
1759
|
-
if (item.destination.output !== void 0) transferArgs.output = item.destination.output;
|
|
1760
|
-
}
|
|
1761
|
-
if (transferArgs.output !== void 0) transferArgs.output = globFilename(transferArgs.output, item.captures);
|
|
1762
|
-
const code = await transfer2(context, transferArgs, item.url, limits, transport, authorize, started, profile.status, headerState);
|
|
1763
|
-
exitCode = args.download ? code || exitCode : code;
|
|
1764
|
-
}
|
|
1765
|
-
return { exitCode };
|
|
1766
|
-
}
|
|
1767
|
-
};
|
|
1768
|
-
builtInDirectContextExecutors.add(def.execute);
|
|
1769
|
-
return def;
|
|
1770
|
-
}
|
|
1771
|
-
async function transfer2(context, args, input, limits, transport, authorize, started, status, headerState) {
|
|
1772
|
-
const start = performance.now();
|
|
1773
|
-
const remaining = () => Math.min(args.maxTimeMs - (performance.now() - start), limits.maxTotalTimeMs - (performance.now() - started));
|
|
1774
|
-
const hasFileOutput = args.etagSave !== void 0 && args.etagSave !== "-" || args.remoteName || args.output !== void 0 && args.output !== "-" || args.dumpHeader !== void 0 && args.dumpHeader !== "-";
|
|
1775
|
-
const operation = createDeadlineOutput(context, hasFileOutput ? { write: (chunk) => context.stdout.write(chunk) } : context.stdout, remaining());
|
|
1776
|
-
const signal = operation.signal;
|
|
1777
|
-
const borrowed = context.stdout.ownedOutput ? { [Symbol.asyncIterator]() {
|
|
1778
|
-
const iterator = context.stdin[Symbol.asyncIterator]();
|
|
1779
|
-
return { next: () => iterator.next() };
|
|
1780
|
-
} } : context.stdin;
|
|
1781
|
-
let response;
|
|
1782
|
-
let failure;
|
|
1783
|
-
const values2 = { ...writeOutDefaults };
|
|
1784
|
-
let format = args.writeOut;
|
|
1785
|
-
let formatReady = false;
|
|
1786
|
-
let dumped = false;
|
|
1787
|
-
let downloaded = 0;
|
|
1788
|
-
let uploaded = 0;
|
|
1789
|
-
let included = [];
|
|
1790
|
-
let closedOutput = false;
|
|
1791
|
-
const publish = async (bytes) => {
|
|
1792
|
-
const writing = createDeadlineOutput(context, context.stdout, remaining());
|
|
1793
|
-
try {
|
|
1794
|
-
await (0, contracts_exports.writeBytes)(writing.output, bytes, writing.signal);
|
|
1795
|
-
} catch (error) {
|
|
1796
|
-
context.signal.throwIfAborted();
|
|
1797
|
-
if (!pipeClosed(error)) throw error;
|
|
1798
|
-
closedOutput = true;
|
|
1799
|
-
} finally {
|
|
1800
|
-
await writing.close();
|
|
1801
|
-
}
|
|
1802
|
-
};
|
|
1803
|
-
try {
|
|
1804
|
-
if (args.etagCompare !== void 0) {
|
|
1805
|
-
let bytes;
|
|
1806
|
-
try {
|
|
1807
|
-
bytes = await withSignal(() => context.fs.readFile((0, path_exports.pathOf)(context, args.etagCompare), { signal, ...Number.isFinite(limits.maxBufferBytes) ? { maxBytes: limits.maxBufferBytes } : {} }), signal);
|
|
1808
|
-
} catch (error) {
|
|
1809
|
-
signal.throwIfAborted();
|
|
1810
|
-
if (!(error instanceof contracts_exports.FsError && error.code === "ENOENT")) throw new CurlError(26, "Failed reading virtual ETag file");
|
|
1811
|
-
bytes = new Uint8Array();
|
|
1812
|
-
}
|
|
1813
|
-
if (bytes.length > limits.maxBufferBytes) throw new CurlError(63, "ETag file exceeds host buffer limit");
|
|
1814
|
-
const text = import_buffer.Buffer.from(bytes).toString("latin1");
|
|
1815
|
-
args.etag = text.split("\r").join("").split("\n").join("") || '""';
|
|
1816
|
-
validateRequestHeader("If-None-Match", args.etag);
|
|
1817
|
-
}
|
|
1818
|
-
let ca;
|
|
1819
|
-
if (args.caFile !== void 0) {
|
|
1820
|
-
try {
|
|
1821
|
-
const bytes = await withSignal(() => context.fs.readFile((0, path_exports.pathOf)(context, args.caFile), { signal, ...Number.isFinite(limits.maxBufferBytes) ? { maxBytes: limits.maxBufferBytes } : {} }), signal);
|
|
1822
|
-
signal.throwIfAborted();
|
|
1823
|
-
if (bytes.length > limits.maxBufferBytes) throw new CurlError(77, "CA certificate exceeds host buffer limit");
|
|
1824
|
-
ca = new Uint8Array(bytes);
|
|
1825
|
-
} catch {
|
|
1826
|
-
signal.throwIfAborted();
|
|
1827
|
-
throw new CurlError(77, "Failed reading virtual CA certificate file");
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
if (format?.startsWith("@")) {
|
|
1831
|
-
try {
|
|
1832
|
-
const bytes = format === "@-" ? await collectNetworkBytes(borrowed, { signal, maxBytes: limits.maxBufferBytes }) : await withSignal(() => context.fs.readFile((0, path_exports.pathOf)(context, format.slice(1)), { signal, ...Number.isFinite(limits.maxBufferBytes) ? { maxBytes: limits.maxBufferBytes } : {} }), signal);
|
|
1833
|
-
format = import_buffer.Buffer.from(bytes).toString("utf8");
|
|
1834
|
-
} catch {
|
|
1835
|
-
signal.throwIfAborted();
|
|
1836
|
-
throw new CurlError(26, "Failed reading write-out format");
|
|
1837
|
-
}
|
|
1838
|
-
}
|
|
1839
|
-
if (format !== void 0) {
|
|
1840
|
-
writeOutFormat(format, values2, limits.maxBufferBytes);
|
|
1841
|
-
formatReady = true;
|
|
1842
|
-
}
|
|
1843
|
-
const parsed = parseUrl(input);
|
|
1844
|
-
const initial = parsed.url;
|
|
1845
|
-
const rawTarget = curlRequestTarget(input);
|
|
1846
|
-
let initialTarget = rawTarget;
|
|
1847
|
-
const initialSearch = initial.search;
|
|
1848
|
-
if (args.query?.length) {
|
|
1849
|
-
const queryArgs = { ...args, data: args.query };
|
|
1850
|
-
delete queryArgs.upload;
|
|
1851
|
-
const queryBody = createBody({ ...context, stdin: borrowed }, queryArgs, limits);
|
|
1852
|
-
const encoded = await queryData(queryBody, signal, limits);
|
|
1853
|
-
let query = "";
|
|
1854
|
-
for (let index = 0; index < encoded.length; index++) {
|
|
1855
|
-
const character = encoded[index];
|
|
1856
|
-
if (character.charCodeAt(0) < 33 || character.charCodeAt(0) > 126) throw new CurlError(3, "Query data must be URL encoded");
|
|
1857
|
-
if (character === "%" && index + 2 < encoded.length && "0123456789abcdefABCDEF".includes(encoded[index + 1]) && "0123456789abcdefABCDEF".includes(encoded[index + 2])) {
|
|
1858
|
-
query += encoded.slice(index, index + 3).toLowerCase();
|
|
1859
|
-
index += 2;
|
|
1860
|
-
} else query += character;
|
|
1861
|
-
}
|
|
1862
|
-
initial.search += `${initial.search ? "&" : "?"}${query}`;
|
|
1863
|
-
}
|
|
1864
|
-
let body = createBody({ ...context, stdin: borrowed }, args, limits);
|
|
1865
|
-
if (args.get && body) {
|
|
1866
|
-
const query = await queryData(body, signal, limits);
|
|
1867
|
-
if (/[^\x21-\x7e]/.test(query)) throw new CurlError(3, "GET data must be URL encoded");
|
|
1868
|
-
initial.search += `${initial.search ? "&" : "?"}${query.replace(/%[0-9a-f]{2}/gi, (escape) => escape.toLowerCase())}`;
|
|
1869
|
-
body = void 0;
|
|
1870
|
-
}
|
|
1871
|
-
if (initial.search !== initialSearch) initialTarget += initial.search.slice(initialSearch.length);
|
|
1872
|
-
let output = args.remoteName ? args.directoryIndex && initial.pathname.endsWith("/") ? args.directoryIndex : remoteFilename(initial) : args.output;
|
|
1873
|
-
if (args.outputDirectory !== void 0 && output !== void 0 && output !== "-" && (!args.download || args.remoteName)) {
|
|
1874
|
-
output = `${args.outputDirectory}/${output}`;
|
|
1875
|
-
}
|
|
1876
|
-
values2.filename_effective = output && output !== "-" ? output : "";
|
|
1877
|
-
let resumeOffset = 0;
|
|
1878
|
-
if (args.continueAt !== void 0) {
|
|
1879
|
-
resumeOffset = args.continueAt === "auto" ? output && output !== "-" ? await existingSize(context, output, signal) ?? 0 : 0 : args.continueAt;
|
|
1880
|
-
}
|
|
1881
|
-
if (args.download && !args.download.spider && output && output !== "-") {
|
|
1882
|
-
if (args.download.noClobber && !args.download.contentDisposition && await existingSize(context, output, signal) !== void 0) return 0;
|
|
1883
|
-
if (args.download.resume) resumeOffset = await existingSize(context, output, signal) ?? 0;
|
|
1884
|
-
}
|
|
1885
|
-
if (output && output !== "-" && args.dumpHeader && args.dumpHeader !== "-" && normalizePath((0, path_exports.pathOf)(context, output)) === normalizePath((0, path_exports.pathOf)(context, args.dumpHeader))) {
|
|
1886
|
-
throw new CurlError(23, "Body and header output files must differ");
|
|
1887
|
-
}
|
|
1888
|
-
const initialRequestMethod = args.head || args.download?.spider ? "HEAD" : args.get ? "GET" : args.upload !== void 0 ? "PUT" : body ? "POST" : "GET";
|
|
1889
|
-
const initialMethod = args.method ?? initialRequestMethod;
|
|
1890
|
-
requestHeaders(args, body?.contentType, args.user ?? parsed.user, true, limits.maxHeaderBytes);
|
|
1891
|
-
attempts: for (let attempt = 0; attempt <= args.retries; attempt++) {
|
|
1892
|
-
if (attempt && args.download?.resume && output && output !== "-") resumeOffset = await existingSize(context, output, signal) ?? 0;
|
|
1893
|
-
values2.num_retries = String(attempt);
|
|
1894
|
-
downloaded = 0;
|
|
1895
|
-
uploaded = 0;
|
|
1896
|
-
failure = void 0;
|
|
1897
|
-
let current = new URL(initial);
|
|
1898
|
-
let currentUrl = current.origin + initialTarget;
|
|
1899
|
-
let method = initialMethod;
|
|
1900
|
-
let requestMethod = initialRequestMethod;
|
|
1901
|
-
let currentBody = body;
|
|
1902
|
-
let credentialsInScope = true;
|
|
1903
|
-
let previous;
|
|
1904
|
-
let redirects = 0;
|
|
1905
|
-
included = [];
|
|
1906
|
-
let headerBytes = 0;
|
|
1907
|
-
while (true) {
|
|
1908
|
-
signal.throwIfAborted();
|
|
1909
|
-
if (remaining() <= 0) throw new CurlError(28, "Operation timed out");
|
|
1910
|
-
values2.url_effective = currentUrl;
|
|
1911
|
-
values2.method = method;
|
|
1912
|
-
let denyPrivateNetworks = false;
|
|
1913
|
-
let allowed;
|
|
1914
|
-
try {
|
|
1915
|
-
allowed = await withSignal(() => authorize({
|
|
1916
|
-
url: currentUrl,
|
|
1917
|
-
method,
|
|
1918
|
-
attempt,
|
|
1919
|
-
signal,
|
|
1920
|
-
requirePrivateNetworkDeny() {
|
|
1921
|
-
denyPrivateNetworks = true;
|
|
1922
|
-
},
|
|
1923
|
-
...previous === void 0 ? {} : { redirectFrom: previous }
|
|
1924
|
-
}), signal);
|
|
1925
|
-
} catch {
|
|
1926
|
-
signal.throwIfAborted();
|
|
1927
|
-
throw new CurlError(7, "Network authorization failed");
|
|
1928
|
-
}
|
|
1929
|
-
if (remaining() <= 0) throw new CurlError(28, "Operation timed out");
|
|
1930
|
-
if (allowed !== true) throw new CurlError(7, "Network access denied by host policy");
|
|
1931
|
-
const policy = denyPrivateNetworks ? { denyPrivateNetworks: true } : {};
|
|
1932
|
-
if (policy.denyPrivateNetworks && transport.supportsPrivateNetworkDeny !== true) {
|
|
1933
|
-
throw new CurlError(7, "Transport cannot enforce private network policy");
|
|
1934
|
-
}
|
|
1935
|
-
const headers = requestHeaders(resumeOffset ? { ...args, range: `${resumeOffset}-` } : args, currentBody?.contentType, args.user ?? parsed.user, credentialsInScope, limits.maxHeaderBytes, previous);
|
|
1936
|
-
if (args.verbose) await (0, contracts_exports.writeBytes)(context.stderr, encode(`> ${method} ${current.origin}
|
|
1937
|
-
${headers.map(([name]) => `> ${name}: [redacted]
|
|
1938
|
-
`).join("")}`), signal);
|
|
1939
|
-
const upload = currentBody && (async function* () {
|
|
1940
|
-
for await (const chunk of currentBody.open(signal)) {
|
|
1941
|
-
uploaded += chunk.length;
|
|
1942
|
-
yield chunk;
|
|
1943
|
-
}
|
|
1944
|
-
})();
|
|
1945
|
-
try {
|
|
1946
|
-
response = await operation.acquire(async () => {
|
|
1947
|
-
const acquired = await transport({
|
|
1948
|
-
url: currentUrl,
|
|
1949
|
-
method,
|
|
1950
|
-
headers,
|
|
1951
|
-
signal,
|
|
1952
|
-
responseBodyMode: args.download && method === "HEAD" || args.head || args.download?.spider ? "omit" : args.fail ? "omit-on-http-error" : "read",
|
|
1953
|
-
...args.httpVersion === void 0 ? {} : { httpVersion: args.httpVersion },
|
|
1954
|
-
...args.ignoreContentLength ? { ignoreContentLength: true } : {},
|
|
1955
|
-
registerCleanup: operation.registerCleanup,
|
|
1956
|
-
...policy,
|
|
1957
|
-
...upload ? { body: upload } : {},
|
|
1958
|
-
...ca === void 0 ? {} : { ca },
|
|
1959
|
-
...args.connectTimeoutMs === void 0 ? {} : { connectTimeoutMs: args.connectTimeoutMs }
|
|
1960
|
-
});
|
|
1961
|
-
let cleanup;
|
|
1962
|
-
return { ...acquired, dispose() {
|
|
1963
|
-
cleanup ??= Promise.resolve().then(() => acquired.dispose());
|
|
1964
|
-
return cleanup;
|
|
1965
|
-
} };
|
|
1966
|
-
}, (result) => result.dispose());
|
|
1967
|
-
} catch (error) {
|
|
1968
|
-
signal.throwIfAborted();
|
|
1969
|
-
const code = error && typeof error === "object" && "code" in error ? error.code : void 0;
|
|
1970
|
-
if (args.retryTransport && (code === "ECONNRESET" || code === "ETIMEDOUT" || code === "EPIPE") && attempt < args.retries) {
|
|
1971
|
-
await delay(args.retryDelayMs || Math.min(1e3 * 2 ** attempt, 6e5), signal);
|
|
1972
|
-
continue attempts;
|
|
1973
|
-
}
|
|
1974
|
-
throw error;
|
|
1975
|
-
}
|
|
1976
|
-
const block = responseHeaders(response, limits.maxHeaderBytes);
|
|
1977
|
-
headerBytes += block.length;
|
|
1978
|
-
if (headerBytes > limits.maxHeaderBytes) throw new CurlError(63, "Combined response headers exceed host byte limit");
|
|
1979
|
-
values2.http_code = values2.response_code = String(response.status).padStart(3, "0");
|
|
1980
|
-
values2.content_type = header(response.headers, "content-type") ?? "";
|
|
1981
|
-
values2.http_version = response.httpVersion ?? "1.1";
|
|
1982
|
-
if (args.verbose) await (0, contracts_exports.writeBytes)(context.stderr, encode(`< HTTP ${response.status}
|
|
1983
|
-
`), signal);
|
|
1984
|
-
if (args.dumpHeader !== void 0) {
|
|
1985
|
-
if (args.dumpHeader === "-") await publish(block);
|
|
1986
|
-
else {
|
|
1987
|
-
await dumpHeaders(context, args.dumpHeader, block, dumped || headerState.dumped, signal);
|
|
1988
|
-
headerState.dumped = true;
|
|
1989
|
-
}
|
|
1990
|
-
dumped = true;
|
|
1991
|
-
}
|
|
1992
|
-
if (args.etagSave !== void 0) {
|
|
1993
|
-
const tag = header(response.headers, "etag");
|
|
1994
|
-
const bytes = tag === void 0 ? new Uint8Array() : new Uint8Array(import_buffer.Buffer.from(`${tag}
|
|
1995
|
-
`, "latin1"));
|
|
1996
|
-
if (args.etagSave === "-") await publish(bytes);
|
|
1997
|
-
else await writeOutput(context, args.etagSave, (0, contracts_exports.toByteSource)(bytes), signal);
|
|
1998
|
-
}
|
|
1999
|
-
if (args.head || args.include) included.push(block);
|
|
2000
|
-
const location = header(response.headers, "location");
|
|
2001
|
-
if (redirectStatuses.has(response.status) && location !== void 0) {
|
|
2002
|
-
let target;
|
|
2003
|
-
let targetUrl;
|
|
2004
|
-
try {
|
|
2005
|
-
const base = currentUrl.split("?")[0];
|
|
2006
|
-
const colon = location.indexOf(":");
|
|
2007
|
-
const boundary = [...location].findIndex((character) => "/?#".includes(character));
|
|
2008
|
-
const resolved = colon >= 0 && (boundary < 0 || colon < boundary) ? location : location.startsWith("//") ? current.protocol + location : location.startsWith("/") ? current.origin + location : location.startsWith("?") ? base + location : location.startsWith("#") || location === "" ? currentUrl + location : base.slice(0, base.lastIndexOf("/") + 1) + location;
|
|
2009
|
-
target = parseUrl(resolved, true).url;
|
|
2010
|
-
targetUrl = target.origin + curlRequestTarget(resolved);
|
|
2011
|
-
} catch (error) {
|
|
2012
|
-
if (error instanceof CurlError) throw error;
|
|
2013
|
-
throw new CurlError(3, "Malformed redirect URL");
|
|
2014
|
-
}
|
|
2015
|
-
values2.redirect_url = targetUrl;
|
|
2016
|
-
if (args.location) {
|
|
2017
|
-
if (redirects++ >= args.maxRedirects) throw new CurlError(47, "Maximum redirects exceeded");
|
|
2018
|
-
if (current.protocol === "https:" && target.protocol === "http:") throw new CurlError(1, "HTTPS-to-HTTP redirects are disabled");
|
|
2019
|
-
if (target.origin !== current.origin) credentialsInScope = false;
|
|
2020
|
-
if (response.status === 303 && method !== "HEAD" || [301, 302].includes(response.status) && requestMethod === "POST") {
|
|
2021
|
-
currentBody = void 0;
|
|
2022
|
-
requestMethod = "GET";
|
|
2023
|
-
if (args.method === void 0) method = "GET";
|
|
2024
|
-
}
|
|
2025
|
-
previous = currentUrl;
|
|
2026
|
-
current = target;
|
|
2027
|
-
currentUrl = targetUrl;
|
|
2028
|
-
values2.num_redirects = String(redirects);
|
|
2029
|
-
await stop(response, signal);
|
|
2030
|
-
response = void 0;
|
|
2031
|
-
continue;
|
|
2032
|
-
}
|
|
2033
|
-
}
|
|
2034
|
-
break;
|
|
2035
|
-
}
|
|
2036
|
-
if (!response) throw new CurlError(56, "No HTTP response");
|
|
2037
|
-
if (args.download?.contentDisposition && args.remoteName) {
|
|
2038
|
-
const disposition = header(response.headers, "content-disposition");
|
|
2039
|
-
const parameter = disposition?.split(";").map((part) => part.trim()).find((part) => part.slice(0, part.indexOf("=")).toLowerCase() === "filename");
|
|
2040
|
-
if (parameter) {
|
|
2041
|
-
let filename = parameter.slice(parameter.indexOf("=") + 1).trim();
|
|
2042
|
-
if (filename.startsWith('"') && filename.endsWith('"')) filename = filename.slice(1, -1);
|
|
2043
|
-
filename = posixPath.basename(filename.split("\\").join("/"));
|
|
2044
|
-
if (filename && filename !== "." && filename !== ".." && !filename.includes("\0")) output = args.outputDirectory === void 0 ? filename : `${args.outputDirectory}/${filename}`;
|
|
2045
|
-
}
|
|
2046
|
-
}
|
|
2047
|
-
if (args.download?.noClobber && output && output !== "-" && await existingSize(context, output, signal) !== void 0) return 0;
|
|
2048
|
-
let append = false;
|
|
2049
|
-
if (resumeOffset && response.status === 206) {
|
|
2050
|
-
const range2 = header(response.headers, "content-range") ?? "";
|
|
2051
|
-
if (!range2.startsWith(`bytes ${resumeOffset}-`)) throw new CurlError(args.continueAt === void 0 ? 56 : 33, "Invalid resume Content-Range");
|
|
2052
|
-
append = true;
|
|
2053
|
-
}
|
|
2054
|
-
if (resumeOffset && response.status === 416) {
|
|
2055
|
-
if (header(response.headers, "content-range") === `bytes */${resumeOffset}`) return 0;
|
|
2056
|
-
}
|
|
2057
|
-
if (resumeOffset && args.continueAt !== void 0 && !append) throw new CurlError(33, "Server does not support resuming this transfer");
|
|
2058
|
-
if (response.status >= 400 && (args.fail || args.failWithBody)) failure = new CurlError(22, `HTTP response status ${response.status}`);
|
|
2059
|
-
const suppressBody = response.status === 304 || args.fail && failure !== void 0;
|
|
2060
|
-
const readBody = !(args.download && method === "HEAD") && !args.head && !suppressBody;
|
|
2061
|
-
let published = 0;
|
|
2062
|
-
if (!args.download?.spider && (!suppressBody || included.length)) {
|
|
2063
|
-
if (args.download && output && output !== "-") await context.fs.mkdir(posixPath.dirname((0, path_exports.pathOf)(context, output)), { recursive: true, signal });
|
|
2064
|
-
const length = args.ignoreContentLength ? void 0 : header(response.headers, "content-length");
|
|
2065
|
-
if (readBody && length && /^\d+$/.test(length) && Number(length) > args.maxFileSize) throw new CurlError(63, "Response exceeds download byte limit");
|
|
2066
|
-
const final = response;
|
|
2067
|
-
const encoding = header(final.headers, "content-encoding");
|
|
2068
|
-
const writing = output === void 0 || output === "-" ? (0, contracts_exports.createOutputOperation)({ ...context, signal }, context.stdout) : void 0;
|
|
2069
|
-
const bodySignal = writing?.signal ?? signal;
|
|
2070
|
-
const source = (async function* () {
|
|
2071
|
-
for (const bytes of included) {
|
|
2072
|
-
published += bytes.length;
|
|
2073
|
-
yield bytes;
|
|
2074
|
-
}
|
|
2075
|
-
if (readBody) {
|
|
2076
|
-
if (args.raw && final.contentDecoded && encoding) throw new CurlError(61, "Transport cannot preserve encoded response bytes");
|
|
2077
|
-
if (args.raw && header(final.headers, "transfer-encoding")) throw new CurlError(61, "Raw transfer encoding is unsupported by this transport");
|
|
2078
|
-
let chunks = 0;
|
|
2079
|
-
const encoded = (async function* () {
|
|
2080
|
-
for await (const chunk of (0, contracts_exports.readBytes)(final.body, bodySignal)) {
|
|
2081
|
-
downloaded += chunk.length;
|
|
2082
|
-
if (downloaded > args.maxFileSize) throw new CurlError(63, "Response exceeds download byte limit");
|
|
2083
|
-
yield chunk;
|
|
2084
|
-
}
|
|
2085
|
-
if (!final.contentDecoded && length && /^\d+$/.test(length) && downloaded !== Number(length)) throw new CurlError(18, "Partial HTTP response body");
|
|
2086
|
-
})();
|
|
2087
|
-
const decoded = args.compressed && !args.raw && !final.contentDecoded && encoding ? decodeContent(encoded, encoding, bodySignal, args.maxFileSize) : encoded;
|
|
2088
|
-
let outputBytes = 0;
|
|
2089
|
-
try {
|
|
2090
|
-
for await (const chunk of decoded) {
|
|
2091
|
-
if (++chunks % 256 === 0) {
|
|
2092
|
-
await (0, yield_exports.yieldTurn)(context.signal);
|
|
2093
|
-
bodySignal.throwIfAborted();
|
|
2094
|
-
}
|
|
2095
|
-
outputBytes += chunk.length;
|
|
2096
|
-
if (outputBytes > args.maxFileSize) throw new CurlError(63, "Decoded response exceeds download byte limit");
|
|
2097
|
-
published += chunk.length;
|
|
2098
|
-
yield chunk;
|
|
2099
|
-
}
|
|
2100
|
-
} catch (error) {
|
|
2101
|
-
bodySignal.throwIfAborted();
|
|
2102
|
-
if (!(error instanceof CurlError) && !final.contentDecoded && length && /^\d+$/.test(length) && downloaded < Number(length)) throw new CurlError(18, "Partial HTTP response body");
|
|
2103
|
-
throw networkError(error);
|
|
2104
|
-
}
|
|
2105
|
-
}
|
|
2106
|
-
})();
|
|
2107
|
-
try {
|
|
2108
|
-
await writeOutput(writing ? { ...context, stdout: writing.output } : context, output, source, bodySignal, append, args.failWithBody && failure !== void 0);
|
|
2109
|
-
} catch (error) {
|
|
2110
|
-
context.signal.throwIfAborted();
|
|
2111
|
-
if (!pipeClosed(error)) throw error;
|
|
2112
|
-
closedOutput = true;
|
|
2113
|
-
} finally {
|
|
2114
|
-
await writing?.close();
|
|
2115
|
-
}
|
|
2116
|
-
}
|
|
2117
|
-
if (retryStatuses.has(response.status) && attempt < args.retries) {
|
|
2118
|
-
if (failure && (!args.silent || args.showError)) await deadlineDiagnostic(context, new CurlError(status(failure.exitCode), failure.message), remaining());
|
|
2119
|
-
const after = header(response.headers, "retry-after");
|
|
2120
|
-
let wait = args.retryDelayMs || Math.min(1e3 * 2 ** attempt, 6e5);
|
|
2121
|
-
if (after !== void 0) {
|
|
2122
|
-
const parsed2 = /^\d+$/.test(after) ? Number(after) * 1e3 : Date.parse(after) - Date.now();
|
|
2123
|
-
if (Number.isFinite(parsed2)) wait = Math.max(wait, parsed2);
|
|
2124
|
-
}
|
|
2125
|
-
await stop(response, signal);
|
|
2126
|
-
response = void 0;
|
|
2127
|
-
if (published && output !== void 0 && output !== "-" && !args.download?.resume) await writeOutput(context, output, (0, contracts_exports.toByteSource)(""), signal);
|
|
2128
|
-
await delay(Math.min(wait, limits.maxTimeMs), signal);
|
|
2129
|
-
continue;
|
|
2130
|
-
}
|
|
2131
|
-
break;
|
|
2132
|
-
}
|
|
2133
|
-
} catch (error) {
|
|
2134
|
-
if (context.signal.aborted) throw context.signal.reason;
|
|
2135
|
-
if (pipeClosed(error)) closedOutput = true;
|
|
2136
|
-
else if (!hasFileOutput && context.stdout.ownedOutput?.consumerClosed.aborted) failure = new CurlError(23, "Failed writing output");
|
|
2137
|
-
else failure = signal.aborted && signal.reason instanceof CurlError ? signal.reason : networkError(error);
|
|
2138
|
-
} finally {
|
|
2139
|
-
try {
|
|
2140
|
-
await operation.close();
|
|
2141
|
-
} finally {
|
|
2142
|
-
context.signal.throwIfAborted();
|
|
2143
|
-
}
|
|
2144
|
-
}
|
|
2145
|
-
values2.size_download = String(downloaded);
|
|
2146
|
-
values2.size_upload = String(uploaded);
|
|
2147
|
-
values2.time_total = ((performance.now() - start) / 1e3).toFixed(6);
|
|
2148
|
-
values2.exitcode = String(failure?.exitCode ?? 0);
|
|
2149
|
-
values2.errormsg = failure?.message ?? "";
|
|
2150
|
-
if (format !== void 0 && formatReady) {
|
|
2151
|
-
try {
|
|
2152
|
-
await publish(writeOutFormat(format, values2, limits.maxBufferBytes));
|
|
2153
|
-
} catch (error) {
|
|
2154
|
-
context.signal.throwIfAborted();
|
|
2155
|
-
failure = error instanceof CurlError ? error : new CurlError(23, "Failed writing write-out result");
|
|
2156
|
-
}
|
|
2157
|
-
}
|
|
2158
|
-
if (failure && (!args.silent || args.showError)) {
|
|
2159
|
-
try {
|
|
2160
|
-
await deadlineDiagnostic(context, new CurlError(status(failure.exitCode), failure.message), remaining());
|
|
2161
|
-
} catch (error) {
|
|
2162
|
-
context.signal.throwIfAborted();
|
|
2163
|
-
if (!(error instanceof CurlError)) throw error;
|
|
2164
|
-
failure = error;
|
|
2165
|
-
}
|
|
2166
|
-
}
|
|
2167
|
-
return failure ? status(failure.exitCode) : closedOutput ? 141 : 0;
|
|
2168
|
-
}
|
|
2169
|
-
|
|
2170
|
-
// packages/safe-bash/src/commands/network/wget.ts
|
|
2171
|
-
async function parseWget(context, limits) {
|
|
2172
|
-
const carrier = (0, contracts_exports.getCommandArguments)(context);
|
|
2173
|
-
let argumentBytes = 0;
|
|
2174
|
-
for (const value of carrier.values) {
|
|
2175
|
-
if (typeof value === "string" && value.length > limits.maxBufferBytes) throw new CurlError(2, "Arguments exceed host buffer limit");
|
|
2176
|
-
argumentBytes += shellValueByteLength(value);
|
|
2177
|
-
if (argumentBytes > limits.maxBufferBytes) throw new CurlError(2, "Arguments exceed host buffer limit");
|
|
2178
|
-
if (typeof value === "string") {
|
|
2179
|
-
for (let index = 0; index < value.length; index++) {
|
|
2180
|
-
if (index % 1024 === 0) await (0, yield_exports.yieldTurn)(context.signal);
|
|
2181
|
-
const code = value.charCodeAt(index);
|
|
2182
|
-
if (code >= 55296 && code <= 56319) {
|
|
2183
|
-
const next = value.charCodeAt(++index);
|
|
2184
|
-
if (!(next >= 56320 && next <= 57343)) throw new CurlError(2, "Arguments must be valid UTF-8");
|
|
2185
|
-
} else if (code >= 56320 && code <= 57343) throw new CurlError(2, "Arguments must be valid UTF-8");
|
|
2186
|
-
}
|
|
2187
|
-
continue;
|
|
2188
|
-
}
|
|
2189
|
-
const bytes = (0, value_exports.shellValueBytes)(value);
|
|
2190
|
-
const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
2191
|
-
for (let offset = 0; offset < bytes.length; offset += 1024) {
|
|
2192
|
-
await (0, yield_exports.yieldTurn)(context.signal);
|
|
2193
|
-
try {
|
|
2194
|
-
decoder.decode(bytes.subarray(offset, offset + 1024), { stream: offset + 1024 < bytes.length });
|
|
2195
|
-
} catch (error) {
|
|
2196
|
-
context.signal.throwIfAborted();
|
|
2197
|
-
if (!(error instanceof TypeError) || "code" in error && error.code !== "ERR_ENCODING_INVALID_ENCODED_DATA") throw error;
|
|
2198
|
-
throw new CurlError(2, "Arguments must be valid UTF-8");
|
|
2199
|
-
}
|
|
2200
|
-
}
|
|
2201
|
-
}
|
|
2202
|
-
const result = {
|
|
2203
|
-
urls: [],
|
|
2204
|
-
data: [],
|
|
2205
|
-
headers: [],
|
|
2206
|
-
include: false,
|
|
2207
|
-
head: false,
|
|
2208
|
-
get: false,
|
|
2209
|
-
location: true,
|
|
2210
|
-
remoteName: true,
|
|
2211
|
-
fail: true,
|
|
2212
|
-
failWithBody: false,
|
|
2213
|
-
silent: false,
|
|
2214
|
-
showError: false,
|
|
2215
|
-
verbose: false,
|
|
2216
|
-
globoff: true,
|
|
2217
|
-
help: false,
|
|
2218
|
-
version: false,
|
|
2219
|
-
retries: Math.min(19, limits.maxRetries),
|
|
2220
|
-
retryDelayMs: 0,
|
|
2221
|
-
maxTimeMs: limits.maxTimeMs,
|
|
2222
|
-
maxRedirects: limits.maxRedirects,
|
|
2223
|
-
maxFileSize: limits.maxDownloadBytes,
|
|
2224
|
-
agent: "virtual-bash-wget/0.0",
|
|
2225
|
-
retryTransport: true,
|
|
2226
|
-
directoryIndex: "index.html",
|
|
2227
|
-
download: { spider: false, resume: false, noClobber: false, contentDisposition: false }
|
|
2228
|
-
};
|
|
2229
|
-
const number2 = (value, integral) => {
|
|
2230
|
-
let start = 0;
|
|
2231
|
-
let end = value.length;
|
|
2232
|
-
if (!integral) {
|
|
2233
|
-
while (start < end && " \n\r\v\f".includes(value[start])) start++;
|
|
2234
|
-
while (end > start && " \n\r\v\f".includes(value[end - 1])) end--;
|
|
2235
|
-
}
|
|
2236
|
-
let multiplier = 1;
|
|
2237
|
-
if (!integral) {
|
|
2238
|
-
const units = { s: 1, m: 60, h: 3600, d: 86400 };
|
|
2239
|
-
const unit = units[value[end - 1]];
|
|
2240
|
-
if (unit !== void 0) {
|
|
2241
|
-
multiplier = unit;
|
|
2242
|
-
end--;
|
|
2243
|
-
}
|
|
2244
|
-
}
|
|
2245
|
-
let position = start;
|
|
2246
|
-
if (!integral && (value[position] === "+" || value[position] === "-")) position++;
|
|
2247
|
-
let digits = 0;
|
|
2248
|
-
let dot = false;
|
|
2249
|
-
for (; position < end; position++) {
|
|
2250
|
-
const character = value[position];
|
|
2251
|
-
if (character >= "0" && character <= "9") digits++;
|
|
2252
|
-
else if (!integral && character === "." && !dot) dot = true;
|
|
2253
|
-
else throw new CurlError(2, "Invalid numeric option");
|
|
2254
|
-
}
|
|
2255
|
-
if (!digits) throw new CurlError(2, "Invalid numeric option");
|
|
2256
|
-
const parsed = Number(value.slice(start, end)) * multiplier;
|
|
2257
|
-
if (!Number.isFinite(parsed) || parsed < 0 || parsed > Number.MAX_SAFE_INTEGER) throw new CurlError(2, "Invalid numeric option");
|
|
2258
|
-
return parsed;
|
|
2259
|
-
};
|
|
2260
|
-
const bodies = {};
|
|
2261
|
-
const headers = /* @__PURE__ */ new Map();
|
|
2262
|
-
let agent;
|
|
2263
|
-
let referer;
|
|
2264
|
-
let ended = false;
|
|
2265
|
-
let inputFile;
|
|
2266
|
-
for (let index = 0; index < context.args.length; index++) {
|
|
2267
|
-
if (index % 128 === 0) await (0, yield_exports.yieldTurn)(context.signal);
|
|
2268
|
-
const argument = context.args[index];
|
|
2269
|
-
if (ended || !argument.startsWith("-") || argument === "-") {
|
|
2270
|
-
result.urls.push(argument);
|
|
2271
|
-
continue;
|
|
2272
|
-
}
|
|
2273
|
-
if (argument === "--") {
|
|
2274
|
-
ended = true;
|
|
2275
|
-
continue;
|
|
2276
|
-
}
|
|
2277
|
-
if (!argument.startsWith("--")) {
|
|
2278
|
-
for (let position = 1; position < argument.length; position++) {
|
|
2279
|
-
if (position % 1024 === 0) await (0, yield_exports.yieldTurn)(context.signal);
|
|
2280
|
-
const flag2 = argument[position];
|
|
2281
|
-
if (flag2 === "q") result.silent = true;
|
|
2282
|
-
else if (flag2 === "c") result.download.resume = true;
|
|
2283
|
-
else if (flag2 === "n" && argument[position + 1] === "c") {
|
|
2284
|
-
result.download.noClobber = true;
|
|
2285
|
-
position++;
|
|
2286
|
-
} else if (flag2 === "n" && argument[position + 1] === "v") position++;
|
|
2287
|
-
else if (flag2 === "h") result.help = true;
|
|
2288
|
-
else if (flag2 === "r") throw new CurlError(2, "Recursive mirroring is unsupported");
|
|
2289
|
-
else if (["O", "P", "i", "T", "t"].includes(flag2)) {
|
|
2290
|
-
const output = position + 1 < argument.length ? argument.slice(position + 1) : context.args[++index];
|
|
2291
|
-
if (!output) throw new CurlError(2, `-${flag2} requires a nonempty argument`);
|
|
2292
|
-
if (flag2 === "O") {
|
|
2293
|
-
result.output = output;
|
|
2294
|
-
result.remoteName = false;
|
|
2295
|
-
} else if (flag2 === "P") result.outputDirectory = output;
|
|
2296
|
-
else if (flag2 === "i") inputFile = output;
|
|
2297
|
-
else if (flag2 === "T") {
|
|
2298
|
-
const seconds = number2(output, false);
|
|
2299
|
-
result.maxTimeMs = Math.min(seconds === 0 ? Infinity : seconds * 1e3, limits.maxTimeMs);
|
|
2300
|
-
} else {
|
|
2301
|
-
const tries = number2(output, true);
|
|
2302
|
-
result.retries = tries === 0 ? limits.maxRetries : Math.min(tries - 1, limits.maxRetries);
|
|
2303
|
-
}
|
|
2304
|
-
break;
|
|
2305
|
-
} else throw new CurlError(2, `Unsupported option: -${flag2}`);
|
|
2306
|
-
}
|
|
2307
|
-
continue;
|
|
2308
|
-
}
|
|
2309
|
-
if (argument === "--quiet") {
|
|
2310
|
-
result.silent = true;
|
|
2311
|
-
continue;
|
|
2312
|
-
}
|
|
2313
|
-
if (argument === "--no-verbose") continue;
|
|
2314
|
-
if (argument === "--help") {
|
|
2315
|
-
result.help = true;
|
|
2316
|
-
continue;
|
|
2317
|
-
}
|
|
2318
|
-
if (argument === "--version") {
|
|
2319
|
-
result.version = true;
|
|
2320
|
-
continue;
|
|
2321
|
-
}
|
|
2322
|
-
if (argument === "--spider") {
|
|
2323
|
-
result.download.spider = true;
|
|
2324
|
-
continue;
|
|
2325
|
-
}
|
|
2326
|
-
if (argument === "--continue") {
|
|
2327
|
-
result.download.resume = true;
|
|
2328
|
-
continue;
|
|
2329
|
-
}
|
|
2330
|
-
if (argument === "--no-clobber") {
|
|
2331
|
-
result.download.noClobber = true;
|
|
2332
|
-
continue;
|
|
2333
|
-
}
|
|
2334
|
-
if (argument === "--content-disposition") {
|
|
2335
|
-
result.download.contentDisposition = true;
|
|
2336
|
-
continue;
|
|
2337
|
-
}
|
|
2338
|
-
if (argument === "--recursive") throw new CurlError(2, "Recursive mirroring is unsupported");
|
|
2339
|
-
const equal = argument.indexOf("=");
|
|
2340
|
-
const flag = equal < 0 ? argument : argument.slice(0, equal);
|
|
2341
|
-
let operand;
|
|
2342
|
-
if (["--directory-prefix", "--input-file", "--output-document", "--timeout", "--tries", "--method", "--post-data", "--post-file", "--body-data", "--body-file", "--header", "--user-agent", "--referer"].includes(flag)) {
|
|
2343
|
-
operand = equal < 0 ? context.args[++index] : argument.slice(equal + 1);
|
|
2344
|
-
} else throw new CurlError(2, `Unsupported option: ${argument}`);
|
|
2345
|
-
if (operand === void 0) throw new CurlError(2, `${flag} requires an argument`);
|
|
2346
|
-
if (flag === "--output-document") {
|
|
2347
|
-
if (!operand) throw new CurlError(2, "Output filename must not be empty");
|
|
2348
|
-
result.output = operand;
|
|
2349
|
-
result.remoteName = false;
|
|
2350
|
-
} else if (flag === "--directory-prefix") result.outputDirectory = operand;
|
|
2351
|
-
else if (flag === "--input-file") inputFile = operand;
|
|
2352
|
-
else if (flag === "--timeout") {
|
|
2353
|
-
const seconds = number2(operand, false);
|
|
2354
|
-
result.maxTimeMs = Math.min(seconds === 0 ? Infinity : seconds * 1e3, limits.maxTimeMs);
|
|
2355
|
-
} else if (flag === "--tries") {
|
|
2356
|
-
const tries = number2(operand, true);
|
|
2357
|
-
result.retries = tries === 0 ? limits.maxRetries : Math.min(tries - 1, limits.maxRetries);
|
|
2358
|
-
} else if (flag === "--header") {
|
|
2359
|
-
if (operand === "") {
|
|
2360
|
-
headers.clear();
|
|
2361
|
-
continue;
|
|
2362
|
-
}
|
|
2363
|
-
const colon = operand.indexOf(":");
|
|
2364
|
-
if (colon < 1) throw new CurlError(2, "Invalid HTTP header");
|
|
2365
|
-
const name = operand.slice(0, colon);
|
|
2366
|
-
const value = operand.slice(colon + 1).trim();
|
|
2367
|
-
validateRequestHeader(name, value);
|
|
2368
|
-
headers.set(name.toLowerCase(), [name, value]);
|
|
2369
|
-
} else if (flag === "--user-agent") {
|
|
2370
|
-
validateRequestHeader("User-Agent", operand);
|
|
2371
|
-
agent = operand;
|
|
2372
|
-
} else if (flag === "--referer") {
|
|
2373
|
-
validateRequestHeader("Referer", operand);
|
|
2374
|
-
referer = operand;
|
|
2375
|
-
} else if (flag === "--method") {
|
|
2376
|
-
try {
|
|
2377
|
-
validateHeaderName(operand);
|
|
2378
|
-
} catch {
|
|
2379
|
-
throw new CurlError(2, "Invalid or unsupported HTTP method");
|
|
2380
|
-
}
|
|
2381
|
-
result.method = operand.toUpperCase();
|
|
2382
|
-
if (["CONNECT", "TRACE"].includes(result.method)) throw new CurlError(2, "Invalid or unsupported HTTP method");
|
|
2383
|
-
} else {
|
|
2384
|
-
const bodyFlag = flag;
|
|
2385
|
-
if (bodyFlag.endsWith("-file") && !operand) throw new CurlError(2, "Upload filename must not be empty");
|
|
2386
|
-
bodies[bodyFlag] = operand;
|
|
2387
|
-
}
|
|
2388
|
-
}
|
|
2389
|
-
if (agent !== void 0 && !headers.has("user-agent")) result.headers.push(["User-Agent", agent === "" ? null : agent]);
|
|
2390
|
-
if (referer !== void 0 && !headers.has("referer")) result.headers.push(["Referer", referer]);
|
|
2391
|
-
result.headers.push(...headers.values());
|
|
2392
|
-
const post = bodies["--post-data"] !== void 0 || bodies["--post-file"] !== void 0;
|
|
2393
|
-
const customBody = bodies["--body-data"] !== void 0 || bodies["--body-file"] !== void 0;
|
|
2394
|
-
if (post && result.method !== void 0 || customBody && result.method === void 0 || bodies["--post-data"] !== void 0 && bodies["--post-file"] !== void 0 || bodies["--body-data"] !== void 0 && bodies["--body-file"] !== void 0) {
|
|
2395
|
-
throw new CurlError(2, "Incompatible request body options: use --post-data or --post-file, or --method with --body-data or --body-file");
|
|
2396
|
-
}
|
|
2397
|
-
const data = bodies["--post-data"] ?? bodies["--body-data"];
|
|
2398
|
-
const file = bodies["--post-file"] ?? bodies["--body-file"];
|
|
2399
|
-
if (data !== void 0) result.data.push({ kind: "raw", value: data });
|
|
2400
|
-
else if (file !== void 0) result.data.push({ kind: "binary", value: `@${file === "-" ? "./-" : file}` });
|
|
2401
|
-
if (!result.help && !result.version) {
|
|
2402
|
-
if (inputFile !== void 0) {
|
|
2403
|
-
let bytes;
|
|
2404
|
-
try {
|
|
2405
|
-
bytes = inputFile === "-" ? await collectNetworkBytes(context.stdin, { signal: context.signal, maxBytes: limits.maxBufferBytes }) : await context.fs.readFile((0, path_exports.pathOf)(context, inputFile), { signal: context.signal, ...Number.isFinite(limits.maxBufferBytes) ? { maxBytes: limits.maxBufferBytes } : {} });
|
|
2406
|
-
} catch {
|
|
2407
|
-
context.signal.throwIfAborted();
|
|
2408
|
-
throw new CurlError(26, "Failed reading virtual URL input file");
|
|
2409
|
-
}
|
|
2410
|
-
let text;
|
|
2411
|
-
try {
|
|
2412
|
-
text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
2413
|
-
} catch {
|
|
2414
|
-
throw new CurlError(2, "URL input must be valid UTF-8");
|
|
2415
|
-
}
|
|
2416
|
-
let start = 0;
|
|
2417
|
-
let lines = 0;
|
|
2418
|
-
for (let index = 0; index <= text.length; index++) {
|
|
2419
|
-
if (index % 1024 === 0) await (0, yield_exports.yieldTurn)(context.signal);
|
|
2420
|
-
if (index !== text.length && text[index] !== "\n") continue;
|
|
2421
|
-
if (index === text.length && start === index) break;
|
|
2422
|
-
if (++lines > 4096) throw new CurlError(2, "URL input exceeds 4096-line limit");
|
|
2423
|
-
const url = text.slice(start, index).trim();
|
|
2424
|
-
start = index + 1;
|
|
2425
|
-
if (url) result.urls.push(url);
|
|
2426
|
-
if (result.urls.length > limits.maxUrls) throw new CurlError(2, "URL count exceeds host limit");
|
|
2427
|
-
}
|
|
2428
|
-
}
|
|
2429
|
-
if (!result.urls.length) throw new CurlError(2, "An HTTP(S) URL is required");
|
|
2430
|
-
if (result.urls.length > limits.maxUrls) throw new CurlError(2, "URL count exceeds host limit");
|
|
2431
|
-
if (result.download.resume && result.download.noClobber) throw new CurlError(2, "Continue and no-clobber are incompatible");
|
|
2432
|
-
if (result.output === "-" && (result.download.resume || result.download.noClobber)) throw new CurlError(2, "Download controls require a file output");
|
|
2433
|
-
}
|
|
2434
|
-
return result;
|
|
2435
|
-
}
|
|
2436
|
-
function createWgetCommand(options) {
|
|
2437
|
-
return createTransferCommand(options, {
|
|
2438
|
-
name: "wget",
|
|
2439
|
-
parse: parseWget,
|
|
2440
|
-
help: "Usage: wget [-O FILE|-] [-q|-nv] [-T SECONDS] [-t COUNT] [URL ...]\nDownloads: --spider | -c/--continue | -nc/--no-clobber | -P/--directory-prefix DIR | --content-disposition\nInput: -i/--input-file FILE (VFS URL list; '-' reads stdin)\nRequest headers: --header 'NAME: VALUE' | --user-agent AGENT | --referer URL\nRequest bodies: --post-data DATA | --post-file FILE | --method METHOD [--body-data DATA | --body-file FILE]\nBody files are read from the VFS. Downloads require explicit host authorization. Recursive mirroring is unsupported.\nTimeout (--timeout) is aggregate and host-capped; --tries=0 remains host-capped.\n",
|
|
2441
|
-
version: "virtual-bash wget 0.0 (bounded HTTP HTTPS)\n",
|
|
2442
|
-
status: (code) => code === 0 ? 0 : [1, 2, 3].includes(code) ? 2 : [23, 26].includes(code) ? 3 : code === 60 ? 5 : code === 22 ? 8 : 4
|
|
2443
|
-
});
|
|
2444
|
-
}
|
|
2445
|
-
|
|
2446
|
-
// packages/safe-bash/src/commands/network/fetch-transport.ts
|
|
2447
|
-
init_platform();
|
|
2448
|
-
function requestBody(source, signal) {
|
|
2449
|
-
if (!source) return void 0;
|
|
2450
|
-
const iterator = source[Symbol.asyncIterator]();
|
|
2451
|
-
return new ReadableStream({
|
|
2452
|
-
async pull(controller) {
|
|
2453
|
-
try {
|
|
2454
|
-
signal.throwIfAborted();
|
|
2455
|
-
const next = await iterator.next();
|
|
2456
|
-
if (next.done) controller.close();
|
|
2457
|
-
else controller.enqueue(next.value);
|
|
2458
|
-
} catch (error) {
|
|
2459
|
-
controller.error(error);
|
|
2460
|
-
}
|
|
2461
|
-
},
|
|
2462
|
-
async cancel(reason) {
|
|
2463
|
-
await iterator.return?.(reason);
|
|
2464
|
-
}
|
|
2465
|
-
});
|
|
2466
|
-
}
|
|
2467
|
-
function createFetchTransport(options = {}) {
|
|
2468
|
-
const fetchRequest = options.fetch ?? globalThis.fetch;
|
|
2469
|
-
if (typeof fetchRequest !== "function") throw new TypeError("Fetch is unavailable");
|
|
2470
|
-
return async (input) => {
|
|
2471
|
-
input.signal.throwIfAborted();
|
|
2472
|
-
if (input.ca !== void 0) throw new CurlError(2, "Fetch transport cannot enforce request CA trust");
|
|
2473
|
-
if (input.httpVersion !== void 0) throw new CurlError(2, "Transport cannot enforce requested HTTP version");
|
|
2474
|
-
if (input.ignoreContentLength) throw new CurlError(2, "Transport cannot enforce ignored Content-Length");
|
|
2475
|
-
const url = new URL(input.url);
|
|
2476
|
-
if (url.protocol !== "http:" && url.protocol !== "https:") throw new CurlError(1, "Unsupported protocol");
|
|
2477
|
-
if (input.denyPrivateNetworks === true) throw new CurlError(7, "Fetch transport cannot enforce private-network denial");
|
|
2478
|
-
if (curlRequestTarget(input.url) !== url.pathname + url.search) {
|
|
2479
|
-
throw new CurlError(2, "Fetch transport cannot preserve the literal request target");
|
|
2480
|
-
}
|
|
2481
|
-
const stopped = new AbortController();
|
|
2482
|
-
const signal = AbortSignal.any([input.signal, stopped.signal]);
|
|
2483
|
-
const body = requestBody(input.body, signal);
|
|
2484
|
-
const headers = new Headers();
|
|
2485
|
-
for (const [name, value] of input.headers) headers.append(name, value);
|
|
2486
|
-
const request = new Request(url, {
|
|
2487
|
-
method: input.method,
|
|
2488
|
-
headers,
|
|
2489
|
-
body,
|
|
2490
|
-
credentials: "omit",
|
|
2491
|
-
redirect: "manual",
|
|
2492
|
-
signal,
|
|
2493
|
-
...body ? { duplex: "half" } : {}
|
|
2494
|
-
});
|
|
2495
|
-
let response;
|
|
2496
|
-
const dispose = async () => {
|
|
2497
|
-
stopped.abort();
|
|
2498
|
-
if (response?.body) await response.body.cancel().catch(() => void 0);
|
|
2499
|
-
};
|
|
2500
|
-
input.registerCleanup?.(dispose);
|
|
2501
|
-
response = await fetchRequest(request);
|
|
2502
|
-
const reader = response.body?.getReader();
|
|
2503
|
-
const responseBody = reader ? (async function* () {
|
|
2504
|
-
try {
|
|
2505
|
-
while (true) {
|
|
2506
|
-
input.signal.throwIfAborted();
|
|
2507
|
-
const next = await reader.read();
|
|
2508
|
-
if (next.done) return;
|
|
2509
|
-
yield next.value;
|
|
2510
|
-
}
|
|
2511
|
-
} finally {
|
|
2512
|
-
reader.releaseLock();
|
|
2513
|
-
}
|
|
2514
|
-
})() : (async function* () {
|
|
2515
|
-
})();
|
|
2516
|
-
return {
|
|
2517
|
-
status: response.status,
|
|
2518
|
-
statusText: response.statusText,
|
|
2519
|
-
headers: [...response.headers.entries()],
|
|
2520
|
-
body: responseBody,
|
|
2521
|
-
contentDecoded: options.contentDecoded ?? true,
|
|
2522
|
-
dispose
|
|
2523
|
-
};
|
|
2524
|
-
};
|
|
2525
|
-
}
|
|
2526
|
-
|
|
2527
|
-
// packages/safe-bash/src/commands/network/authorizer.ts
|
|
2528
|
-
init_platform();
|
|
2529
|
-
|
|
2530
|
-
// packages/safe-bash/src/commands/network/private-address.ts
|
|
2531
|
-
init_platform();
|
|
2532
|
-
function privateIPv4(first, second) {
|
|
2533
|
-
return first === 10 || first === 127 || first === 0 || first === 169 && second === 254 || first === 192 && second === 168 || first === 172 && second >= 16 && second <= 31 || first === 100 && second >= 64 && second <= 127 || first === 198 && second >= 18 && second <= 19 || first >= 224;
|
|
2534
|
-
}
|
|
2535
|
-
function privateHostname(input) {
|
|
2536
|
-
const hostname = input.toLowerCase().replace(/\.$/u, "").replace(/^\[|\]$/gu, "");
|
|
2537
|
-
if (hostname === "localhost" || hostname.endsWith(".localhost") || hostname === "::1") return true;
|
|
2538
|
-
if (hostname.includes(":")) {
|
|
2539
|
-
const [prefix, suffix] = hostname.split("::");
|
|
2540
|
-
const leading = prefix ? prefix.split(":") : [];
|
|
2541
|
-
const trailing = suffix ? suffix.split(":") : [];
|
|
2542
|
-
const hextets = new Array(8).fill(0);
|
|
2543
|
-
for (let index = 0; index < leading.length; index++) hextets[index] = Number.parseInt(leading[index], 16);
|
|
2544
|
-
for (let index = 0; index < trailing.length; index++) hextets[8 - trailing.length + index] = Number.parseInt(trailing[index], 16);
|
|
2545
|
-
if (hextets.every((value) => value === 0)) return true;
|
|
2546
|
-
const mappedIPv4 = hextets.slice(0, 5).every((value) => value === 0) && hextets[5] === 65535;
|
|
2547
|
-
const translatedIPv4 = hextets.slice(0, 4).every((value) => value === 0) && hextets[4] === 65535 && hextets[5] === 0;
|
|
2548
|
-
const nat64IPv4 = hextets[0] === 100 && hextets[1] === 65435 && hextets.slice(2, 6).every((value) => value === 0);
|
|
2549
|
-
const compatibleIPv4 = hextets.slice(0, 6).every((value) => value === 0);
|
|
2550
|
-
if (mappedIPv4 || translatedIPv4 || nat64IPv4 || compatibleIPv4) {
|
|
2551
|
-
return privateIPv4(hextets[6] >>> 8, hextets[6] & 255);
|
|
2552
|
-
}
|
|
2553
|
-
if (hextets[0] === 8194) return privateIPv4(hextets[1] >>> 8, hextets[1] & 255);
|
|
2554
|
-
return (hextets[0] & 65024) === 64512 || (hextets[0] & 65472) === 65152 || (hextets[0] & 65472) === 65216 || (hextets[0] & 65280) === 65280 || hextets[0] === 100 && hextets[1] === 65435 && hextets[2] === 1;
|
|
2555
|
-
}
|
|
2556
|
-
const parts = hostname.split(".");
|
|
2557
|
-
if (parts.length !== 4 || parts.some((part) => !part || Array.from(part).some((character) => character < "0" || character > "9"))) return false;
|
|
2558
|
-
const octets = parts.map(Number);
|
|
2559
|
-
if (octets.some((value) => value < 0 || value > 255)) return false;
|
|
2560
|
-
const [first, second] = octets;
|
|
2561
|
-
return privateIPv4(first, second);
|
|
2562
|
-
}
|
|
2563
|
-
|
|
2564
|
-
// packages/safe-bash/src/commands/network/authorizer.ts
|
|
2565
|
-
function createOriginAuthorizer(allowlist = "*", options = {}) {
|
|
2566
|
-
const denyPrivateNetworks = options.denyPrivateNetworks === true;
|
|
2567
|
-
const origins = /* @__PURE__ */ new Set();
|
|
2568
|
-
const hosts = /* @__PURE__ */ new Set();
|
|
2569
|
-
for (const entry of allowlist === "*" ? [] : allowlist) {
|
|
2570
|
-
const value = entry.trim();
|
|
2571
|
-
if (!value) throw new TypeError("Origin allowlist entries must not be empty");
|
|
2572
|
-
if (value.includes("://")) {
|
|
2573
|
-
const url = new URL(value);
|
|
2574
|
-
if (url.pathname !== "/" || url.search || url.hash || url.username || url.password) {
|
|
2575
|
-
throw new TypeError(`Origin allowlist entry must be an origin: ${entry}`);
|
|
2576
|
-
}
|
|
2577
|
-
origins.add(url.origin);
|
|
2578
|
-
} else {
|
|
2579
|
-
if (value.includes("/") || value.includes("@")) throw new TypeError(`Invalid hostname allowlist entry: ${entry}`);
|
|
2580
|
-
hosts.add(value.toLowerCase().replace(/\.$/u, ""));
|
|
2581
|
-
}
|
|
2582
|
-
}
|
|
2583
|
-
return (request) => {
|
|
2584
|
-
const url = new URL(request.url);
|
|
2585
|
-
const hostname = url.hostname.toLowerCase().replace(/\.$/u, "");
|
|
2586
|
-
if (denyPrivateNetworks) {
|
|
2587
|
-
request.requirePrivateNetworkDeny?.();
|
|
2588
|
-
if (privateHostname(hostname)) return false;
|
|
2589
|
-
}
|
|
2590
|
-
return allowlist === "*" || origins.has(url.origin) || hosts.has(hostname);
|
|
2591
|
-
};
|
|
2592
|
-
}
|
|
2593
|
-
|
|
2594
|
-
// packages/safe-bash/src/commands/network/public.ts
|
|
2595
|
-
function createNetworkCommands(options) {
|
|
2596
|
-
return [createCurlCommand(options), createWgetCommand(options)];
|
|
2597
|
-
}
|
|
2598
|
-
function networkCommands(options) {
|
|
2599
|
-
const definitions = createNetworkCommands(options);
|
|
2600
|
-
return {
|
|
2601
|
-
name: "network-commands",
|
|
2602
|
-
setup(host) {
|
|
2603
|
-
if (!options.replace) {
|
|
2604
|
-
for (const definition of definitions) if (host.commands.has(definition.name)) throw new Error(`Command already registered: ${definition.name}`);
|
|
2605
|
-
}
|
|
2606
|
-
for (const definition of definitions) host.commands.register(definition, { replace: options.replace ?? false });
|
|
2607
|
-
}
|
|
2608
|
-
};
|
|
2609
|
-
}
|
|
2610
|
-
var createCurlCommands = createNetworkCommands;
|
|
2611
|
-
var curlCommands = networkCommands;
|
|
2612
|
-
var CURL_HELP = "Usage: curl [HTTP(S) URL] [-X METHOD] [-H HEADER] [-d DATA] [-L] [-o VFSFILE]\nExplicit host authorization is required. See network/README.md for supported flags and limits.\n";
|
|
2613
|
-
var CURL_VERSION = "virtual-bash curl 0.0 (HTTP HTTPS; Node streaming transport)\n";
|
|
2614
|
-
var WGET_HELP = "Usage: wget [-O FILE|-] [-q|-nv] [-T SECONDS] [-t COUNT] [URL ...]\nDownloads: --spider | -c/--continue | -nc/--no-clobber | -P/--directory-prefix DIR | --content-disposition\nInput: -i/--input-file FILE (VFS URL list; '-' reads stdin)\nRequest headers: --header 'NAME: VALUE' | --user-agent AGENT | --referer URL\nRequest bodies: --post-data DATA | --post-file FILE | --method METHOD [--body-data DATA | --body-file FILE]\nBody files are read from the VFS. Downloads require explicit host authorization. Recursive mirroring is unsupported.\nTimeout (--timeout) is aggregate and host-capped; --tries=0 remains host-capped.\n";
|
|
2615
|
-
var WGET_VERSION = "virtual-bash wget 0.0 (bounded HTTP HTTPS)\n";
|
|
2616
|
-
function evalSyncCurl(opArgs) {
|
|
2617
|
-
if (opArgs.length === 1) {
|
|
2618
|
-
if (opArgs[0] === "-V" || opArgs[0] === "--version") return CURL_VERSION;
|
|
2619
|
-
if (opArgs[0] === "-h" || opArgs[0] === "--help") return CURL_HELP;
|
|
2620
|
-
}
|
|
2621
|
-
return void 0;
|
|
2622
|
-
}
|
|
2623
|
-
function evalSyncWget(opArgs) {
|
|
2624
|
-
if (opArgs.length === 1) {
|
|
2625
|
-
if (opArgs[0] === "-V" || opArgs[0] === "--version") return WGET_VERSION;
|
|
2626
|
-
if (opArgs[0] === "-h" || opArgs[0] === "--help") return WGET_HELP;
|
|
2627
|
-
}
|
|
2628
|
-
return void 0;
|
|
2629
|
-
}
|
|
2630
|
-
syncCommandEvaluators.evalSyncCurl = evalSyncCurl;
|
|
2631
|
-
syncCommandEvaluators.evalSyncWget = evalSyncWget;
|
|
2632
|
-
|
|
2633
|
-
export {
|
|
2634
|
-
filesystem_output_exports,
|
|
2635
|
-
defaultNetworkLimits,
|
|
2636
|
-
cloudflareWorkerNetworkLimits,
|
|
2637
|
-
CurlError,
|
|
2638
|
-
createCurlCommand,
|
|
2639
|
-
createWgetCommand,
|
|
2640
|
-
createFetchTransport,
|
|
2641
|
-
createOriginAuthorizer,
|
|
2642
|
-
createNetworkCommands,
|
|
2643
|
-
networkCommands,
|
|
2644
|
-
createCurlCommands,
|
|
2645
|
-
curlCommands,
|
|
2646
|
-
evalSyncCurl,
|
|
2647
|
-
evalSyncWget
|
|
2648
|
-
};
|
|
2649
|
-
//# sourceMappingURL=chunk-GVKBUQDX.js.map
|