blob: 554b62bafdb02a7324842480e9cf390acee38aa6 (
plain)
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
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const key = "json-viewer-chunked-response";
function setResponse(response) {
setObjectState(key, response);
}
function getResponse() {
let response;
getObjectState(key, v => {
response = v;
});
return response;
}
function handleRequest(request, response) {
const { queryString } = request;
if (!queryString) {
response.processAsync();
setResponse(response);
response.setHeader("Content-Type", "application/json");
// Write something so that the JSON viewer app starts loading.
response.write(" ");
return;
}
const [command, value] = queryString.split("=");
switch (command) {
case "write":
getResponse().write(value);
break;
case "close":
getResponse().finish();
setResponse(null);
break;
}
response.setHeader("Content-Type", "text/plain");
response.write("ok");
}
|