quickwin 1.0.1 → 1.0.2
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/examples/exec_server.js +25 -3
- package/examples/exec_server.ts +21 -3
- package/package.json +1 -1
package/examples/exec_server.js
CHANGED
|
@@ -1,14 +1,36 @@
|
|
|
1
1
|
import { createServer } from '../lib/http-server.js';
|
|
2
2
|
import * as std from 'std';
|
|
3
|
+
function readAll(file) {
|
|
4
|
+
const chunks = [];
|
|
5
|
+
const buf = new Uint8Array(4096);
|
|
6
|
+
while (true) {
|
|
7
|
+
const n = file.read(buf.buffer, 0, 4096);
|
|
8
|
+
if (n <= 0)
|
|
9
|
+
break;
|
|
10
|
+
chunks.push(buf.slice(0, n));
|
|
11
|
+
}
|
|
12
|
+
let total = 0;
|
|
13
|
+
for (const c of chunks)
|
|
14
|
+
total += c.length;
|
|
15
|
+
const out = new Uint8Array(total);
|
|
16
|
+
let offset = 0;
|
|
17
|
+
for (const c of chunks) {
|
|
18
|
+
out.set(c, offset);
|
|
19
|
+
offset += c.length;
|
|
20
|
+
}
|
|
21
|
+
return new TextDecoder().decode(out);
|
|
22
|
+
}
|
|
3
23
|
const server = createServer(async (req) => {
|
|
4
24
|
if (req.method === 'POST' && new URL(req.url).pathname === '/exec') {
|
|
5
25
|
const body = await req.json();
|
|
6
26
|
const file = std.popen(body.cmd, 'r');
|
|
7
27
|
if (!file)
|
|
8
|
-
return new Response(
|
|
9
|
-
const output = file
|
|
28
|
+
return new Response('popen failed', { status: 500 });
|
|
29
|
+
const output = readAll(file);
|
|
10
30
|
file.close();
|
|
11
|
-
return new Response(
|
|
31
|
+
return new Response(output, {
|
|
32
|
+
headers: { 'Content-Type': 'application/octet-stream' }
|
|
33
|
+
});
|
|
12
34
|
}
|
|
13
35
|
return new Response('Not Found', { status: 404 });
|
|
14
36
|
});
|
package/examples/exec_server.ts
CHANGED
|
@@ -1,14 +1,32 @@
|
|
|
1
1
|
import { createServer } from '../lib/http-server.js'
|
|
2
2
|
import * as std from 'std'
|
|
3
3
|
|
|
4
|
+
function readAll(file: std.FILE): string {
|
|
5
|
+
const chunks: Uint8Array[] = []
|
|
6
|
+
const buf = new Uint8Array(4096)
|
|
7
|
+
while (true) {
|
|
8
|
+
const n = file.read(buf.buffer, 0, 4096)
|
|
9
|
+
if (n <= 0) break
|
|
10
|
+
chunks.push(buf.slice(0, n))
|
|
11
|
+
}
|
|
12
|
+
let total = 0
|
|
13
|
+
for (const c of chunks) total += c.length
|
|
14
|
+
const out = new Uint8Array(total)
|
|
15
|
+
let offset = 0
|
|
16
|
+
for (const c of chunks) { out.set(c, offset); offset += c.length }
|
|
17
|
+
return new TextDecoder().decode(out)
|
|
18
|
+
}
|
|
19
|
+
|
|
4
20
|
const server = createServer(async (req) => {
|
|
5
21
|
if (req.method === 'POST' && new URL(req.url).pathname === '/exec') {
|
|
6
22
|
const body = await req.json() as { cmd: string }
|
|
7
23
|
const file = std.popen(body.cmd, 'r')
|
|
8
|
-
if (!file) return new Response(
|
|
9
|
-
const output = file
|
|
24
|
+
if (!file) return new Response('popen failed', { status: 500 })
|
|
25
|
+
const output = readAll(file)
|
|
10
26
|
file.close()
|
|
11
|
-
return new Response(
|
|
27
|
+
return new Response(output, {
|
|
28
|
+
headers: { 'Content-Type': 'application/octet-stream' }
|
|
29
|
+
})
|
|
12
30
|
}
|
|
13
31
|
return new Response('Not Found', { status: 404 })
|
|
14
32
|
})
|