-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcurl.ts
29 lines (26 loc) · 843 Bytes
/
curl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/// <reference path="../userspace/index.d.ts" />
/**
* Download a file while printing its content to stdout
* @usage (-u) [url]
*/
if (typeof webcontainer !== 'function')
throw new Error('Missing webcontainer runtime');
webcontainer(async function curl(process) {
debugger;
const args = process.argv.slice(1);
let upload = args.findIndex(x => x === '-u');
if (args.length < (~upload ? 2 : 1))
return 1;
const options: RequestInit = {};
if (upload !== -1) {
options.method = 'POST';
options.body = process.stdin();
args.splice(upload, 1);
}
try {
const response = await fetch(args[0]!, options);
await response.body?.pipeTo(process.stdout());
} catch (err) {
await process.write(2, new TextEncoder().encode(`curl: cannot fetch: ${(err instanceof Error && err.message) || err}\n`));
}
});