|
1 | | -const querystring = require("querystring"); |
| 1 | +const querystring = require('querystring') |
2 | 2 |
|
3 | | -const exec = require("./utils/exec"); |
| 3 | +const exec = require('./utils/exec') |
4 | 4 |
|
5 | 5 | // 获取 env、query、body |
6 | 6 | async function getData() { |
7 | | - const env = process.env; |
| 7 | + const env = process.env |
8 | 8 |
|
9 | | - const assets = env.PATH_INFO.replace( |
10 | | - "/cgi/ThirdParty/code.editor/index.cgi", |
11 | | - "" |
12 | | - ); |
| 9 | + const assets = env.PATH_INFO.replace('/cgi/ThirdParty/code.editor/index.cgi', '') |
13 | 10 |
|
14 | 11 | if (assets) { |
15 | | - const path = assets === "/" ? "/index.html" : assets; |
16 | | - const cache = assets !== "/"; |
| 12 | + const path = assets === '/' ? '/index.html' : assets |
| 13 | + const cache = assets !== '/' |
17 | 14 |
|
18 | 15 | return { |
19 | 16 | env, |
20 | | - api: "read", |
| 17 | + api: 'read', |
21 | 18 | query: { path: `/var/apps/code.editor/target/server/dist${path}`, cache }, |
22 | 19 | body: {}, |
23 | | - }; |
| 20 | + } |
24 | 21 | } |
25 | 22 |
|
26 | 23 | const result = { |
27 | 24 | env, |
28 | | - api: env.HTTP_API_PATH || "", |
29 | | - query: querystring.parse(env.QUERY_STRING || ""), |
| 25 | + api: env.HTTP_API_PATH || '', |
| 26 | + query: querystring.parse(env.QUERY_STRING || ''), |
30 | 27 | body: {}, |
31 | | - }; |
| 28 | + } |
32 | 29 |
|
33 | | - if (env.REQUEST_METHOD === "POST") { |
34 | | - const contentLength = parseInt(env.CONTENT_LENGTH || "0"); |
| 30 | + if (env.REQUEST_METHOD === 'POST') { |
| 31 | + const contentLength = parseInt(env.CONTENT_LENGTH || '0') |
35 | 32 |
|
36 | 33 | if (contentLength > 0) { |
37 | 34 | const str = await new Promise((r) => { |
38 | | - let str = ""; |
| 35 | + let str = '' |
39 | 36 |
|
40 | | - process.stdin.on("data", (chunk) => { |
41 | | - str += chunk.toString(); |
42 | | - }); |
| 37 | + process.stdin.on('data', (chunk) => { |
| 38 | + str += chunk.toString() |
| 39 | + }) |
43 | 40 |
|
44 | | - process.stdin.on("end", () => { |
45 | | - r(str); |
46 | | - }); |
47 | | - }); |
| 41 | + process.stdin.on('end', () => { |
| 42 | + r(str) |
| 43 | + }) |
| 44 | + }) |
48 | 45 |
|
49 | 46 | try { |
50 | 47 | if (str.trim()) { |
51 | | - const type = env.CONTENT_TYPE || ""; |
| 48 | + const type = env.CONTENT_TYPE || '' |
52 | 49 |
|
53 | | - if (type.includes("application/x-www-form-urlencoded")) { |
54 | | - result.body = querystring.parse(str); |
55 | | - } else if (type.includes("application/json")) { |
56 | | - result.body = JSON.parse(str); |
| 50 | + if (type.includes('application/x-www-form-urlencoded')) { |
| 51 | + result.body = querystring.parse(str) |
| 52 | + } else if (type.includes('application/json')) { |
| 53 | + result.body = JSON.parse(str) |
57 | 54 | } else { |
58 | | - result.body = { raw: str }; |
| 55 | + result.body = { raw: str } |
59 | 56 | } |
60 | 57 | } |
61 | 58 | } catch (error) { |
62 | | - result.body = {}; |
| 59 | + result.body = {} |
63 | 60 | } |
64 | 61 | } |
65 | 62 | } |
66 | 63 |
|
67 | | - return result; |
| 64 | + return result |
68 | 65 | } |
69 | 66 |
|
70 | 67 | async function main() { |
71 | 68 | try { |
72 | | - const data = await getData(); |
| 69 | + const data = await getData() |
73 | 70 |
|
74 | | - const { type, body } = await exec(data); |
| 71 | + const { type, body } = await exec(data) |
75 | 72 |
|
76 | 73 | if (type) { |
77 | | - console.log(`Content-Type: ${type}`); |
78 | | - console.log(`Content-Length: ${body.size}`); |
79 | | - console.log(`Last-Modified: ${body.time}`); |
80 | | - console.log(""); |
81 | | - body.stream.pipe(process.stdout); |
| 74 | + console.log(`Content-Type: ${type}`) |
| 75 | + console.log(`Content-Length: ${body.size}`) |
| 76 | + console.log(`Last-Modified: ${body.time}`) |
| 77 | + console.log('') |
| 78 | + body.stream.pipe(process.stdout) |
82 | 79 | } else { |
83 | | - console.log("Content-Type: application/json; charset=utf-8\n"); |
84 | | - console.log(JSON.stringify(body)); |
| 80 | + console.log('Content-Type: application/json; charset=utf-8\n') |
| 81 | + console.log(JSON.stringify(body)) |
85 | 82 | } |
86 | 83 | } catch (error) { |
87 | | - console.log("Content-Type: application/json; charset=utf-8\n"); |
88 | | - console.log(JSON.stringify({ code: 400, msg: "调用错误" })); |
| 84 | + console.log('Content-Type: application/json; charset=utf-8\n') |
| 85 | + console.log(JSON.stringify({ code: 400, msg: '调用错误' })) |
89 | 86 | } |
90 | 87 | } |
91 | 88 |
|
92 | | -main(); |
| 89 | +main() |
0 commit comments