@luolingp/request-doctor 0.13.4 → 0.14.1
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/client.js +252 -220
- package/docs/01-/350/275/257/344/273/266/351/234/200/346/261/202/350/247/204/346/240/274/350/257/264/346/230/216.md +1 -1
- package/docs/02-/350/275/257/344/273/266/350/256/276/350/256/241/350/257/264/346/230/216.md +1 -1
- package/docs/03-/350/275/257/344/273/266/346/265/213/350/257/225/346/212/245/345/221/212.md +5 -5
- package/docs/04-/345/256/211/350/243/205/351/203/250/347/275/262/350/257/264/346/230/216.md +1 -1
- package/docs/05-/347/224/250/346/210/267/346/211/213/345/206/214.md +1 -1
- package/docs/06-/350/275/257/344/273/266/350/264/250/351/207/217/345/272/246/351/207/217/346/212/245/345/221/212.md +1 -1
- package/docs/quality-report.json +10 -10
- package/index.js +478 -466
- package/lib/allow-route.js +39 -0
- package/package.json +1 -1
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// 界面上的「授权」路由。
|
|
2
|
+
//
|
|
3
|
+
// 和 /doctor allow 走同一套规则,只是入口不同:界面上点按钮不该被要求去敲命令。
|
|
4
|
+
import { allowVersion } from './allow-version.js';
|
|
5
|
+
|
|
6
|
+
// 两条写路由共用同一句报错,省得各写各的措辞。
|
|
7
|
+
export const BAD_BODY = { ok: false, text: '请求体不是合法的 JSON' };
|
|
8
|
+
|
|
9
|
+
// 读请求体并把 spec 取出来:写路由都要这一段。
|
|
10
|
+
// 请求体坏了返回 undefined,空 spec 返回空串,两种情况调用方分开处理。
|
|
11
|
+
export function readSpec(req) {
|
|
12
|
+
return new Promise((resolve) => {
|
|
13
|
+
const chunks = [];
|
|
14
|
+
req.on('data', (chunk) => chunks.push(chunk));
|
|
15
|
+
req.on('end', () => {
|
|
16
|
+
try {
|
|
17
|
+
const body = JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}');
|
|
18
|
+
resolve(String(body.spec ?? '').trim());
|
|
19
|
+
} catch {
|
|
20
|
+
resolve(undefined);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 造一条授权路由。json 由调用方给,它负责响应头这些细节。
|
|
27
|
+
export function createAllowRoute(input) {
|
|
28
|
+
return {
|
|
29
|
+
kind: 'exact',
|
|
30
|
+
path: '/doctor/allow',
|
|
31
|
+
handler: (req, res) => {
|
|
32
|
+
readSpec(req).then(async (spec) => {
|
|
33
|
+
if (spec === undefined) return input.json(res, 400, BAD_BODY);
|
|
34
|
+
const result = await allowVersion(spec, input);
|
|
35
|
+
return input.json(res, 200, { ok: result.kind === 'success', text: result.text });
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|