forked from rsenn/qjs-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-formparser.js
87 lines (70 loc) · 2.61 KB
/
test-formparser.js
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
import { AsyncIterator, Response, Request, Ringbuffer, Generator, Socket, FormParser, Hash, URL, createServer, client, fetch, getSessions, setLog, METHOD_GET, METHOD_POST, METHOD_OPTIONS, METHOD_PUT, METHOD_PATCH, METHOD_DELETE, METHOD_HEAD, LLL_ERR, LLL_WARN, LLL_NOTICE, LLL_INFO, LLL_DEBUG, LLL_PARSER, LLL_HEADER, LLL_EXT, LLL_CLIENT, LLL_LATENCY, LLL_USER, LLL_THREAD, LLL_ALL, logLevels } from 'net.so';
const args = [...(globalThis.scriptArgs ?? process.argv)];
const host = args[1] ?? 'localhost',
port = args[2] ? +args[2] : 30000;
// setLog(LLL_USER, (level, message) => /(HTTP|WS)/.test(message) && console.log('LOG', message));
const log = (fnName, ...args) => console.log(`\x1b[1;33m${fnName}\x1b[0m`, ...args);
createServer({
block: false,
tls: true,
host,
port,
protocol: 'http',
onRequest(req, resp) {
const ws = this;
const { headers } = req;
const type = headers.get('content-type');
if(typeof type == 'string' && type.startsWith('multipart/form-data')) {
console.log('MULTIPART!');
globalThis.gen = new Generator(async (push, stop) => {
new FormParser(
ws,
[
/*'file', 'image'*/
],
{
chunkSize: 256 ** 3,
onOpen(fp, name) {
log('onOpen', name);
this.name = name;
this.data = new Generator(() => {} /*, 4096*/);
const { data } = this;
/*const params = this.params;
log('onOpen', { params: params.map(n => [n, this[n]]) });*/
push([name, data]);
},
onContent(fp, buf) {
const { name, data } = this;
log('onContent', buf);
data.write(buf);
/* const params = this.params;
log('onContent', { params: params.map(n => [n, this[n]]) });*/
},
onClose(fp, name) {
const { data } = this;
log('onClose', name, data);
data.stop();
},
onFinalize(fp) {
const { name } = this;
const resp = new Response('done', { status: 200 });
log('onFinalize', resp);
stop();
return resp;
}
}
);
});
(async function() {
for await(let [name, data] of await gen) {
console.log('Gen', name, data);
for await(let chunk of data) {
console.log('chunk', chunk);
}
}
})();
} else {
console.log('onRequest', { ws, req, headers: Object.fromEntries(headers.entries()) });
}
}
});