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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { stripAnsiCode } from "jsr:@std/fmt@0.225.4/colors";
export type CommandOptions = {
args?: string[];
};
type ServeCommandParams = {
/*
* The command to run.
*/
command: string;
/*
* The environment variables to set.
*/
env?: Record<string, string>;
/*
* The arguments to pass to the command.
* If `true`, the arguments will be taken from the request path and query parameters.
* if `false`, the arguments will be empty.
*/
args?: string[] | boolean;
};
export function serveCommand(
params: ServeCommandParams,
): (req: Request) => Response | Promise<Response> {
return async (req: Request) => {
const url = new URL(req.url);
if (url.pathname === "/favicon.ico") {
return new Response(null, {
status: 404,
});
}
let pathname = url.pathname.slice(1);
if (pathname.endsWith("/")) {
pathname = pathname.slice(0, -1);
}
const requestArgs = pathname == ""
? []
: pathname.split("/").map(decodeURIComponent);
for (const [key, value] of url.searchParams.entries()) {
if (!value) {
if (key.length === 1) {
requestArgs.push(`-${key}`);
} else {
requestArgs.push(`--${key}`);
}
continue;
}
if (key.length === 1) {
requestArgs.push(`-${key}=${value}`);
} else {
requestArgs.push(`--${key}=${value}`);
}
}
const args: string[] = [];
if (Array.isArray(params.args)) {
if (requestArgs.length > 0) {
return new Response("Invalid arguments", {
status: 400,
headers: {
"content-type": "text/plain",
},
});
}
for (const arg of params.args) {
args.push(arg);
}
if (requestArgs.length) {
return new Response("Invalid arguments", {
status: 400,
headers: {
"content-type": "text/plain; charset=utf-8",
},
});
}
} else if (params.args) {
args.push(...requestArgs);
} else {
if (args.length > 0) {
return new Response("Invalid arguments", {
status: 400,
headers: {
"content-type": "text/plain; charset=utf-8",
},
});
}
}
const command = new Deno.Command(params.command, {
args,
env: params.env,
stdin: req.method === "POST" ? "piped" : "null",
stderr: "piped",
stdout: "piped",
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 25, 2024