@ulm0/gonogo 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +30 -2
- package/bin/gonogo.js +18 -0
- package/dist/client/THIRD-PARTY-NOTICES.md +409 -0
- package/dist/client/apple-touch-icon.png +0 -0
- package/dist/client/assets/doto-latin-900-normal-D1bF_x6I.woff +0 -0
- package/dist/client/assets/geist-latin-400-normal-B40WzpMT.woff2 +0 -0
- package/dist/client/assets/geist-latin-400-normal-akEymXtG.woff +0 -0
- package/dist/client/assets/geist-latin-500-normal-BDXIbFrL.woff +0 -0
- package/dist/client/assets/geist-latin-500-normal-CTWBw9NS.woff2 +0 -0
- package/dist/client/assets/geist-latin-600-normal-CSETrqM2.woff2 +0 -0
- package/dist/client/assets/geist-latin-600-normal-DsPlZH-9.woff +0 -0
- package/dist/client/assets/geist-mono-latin-400-normal-B2yvC1Cq.woff +0 -0
- package/dist/client/assets/geist-mono-latin-400-normal-DTRLJnHl.woff2 +0 -0
- package/dist/client/assets/geist-mono-latin-500-normal-D-GG86Jb.woff +0 -0
- package/dist/client/assets/geist-mono-latin-500-normal-YINYabwD.woff2 +0 -0
- package/dist/client/assets/geist-mono-latin-600-normal-V-KvD_pi.woff2 +0 -0
- package/dist/client/assets/geist-mono-latin-600-normal-bZn07FKM.woff +0 -0
- package/dist/client/assets/index-B6MsSoeo.css +1 -0
- package/dist/client/assets/index-DRtiRh1Y.js +31 -0
- package/dist/client/favicon.ico +0 -0
- package/dist/client/favicon.svg +1 -0
- package/dist/client/index.html +19 -0
- package/dist/server/cleanroom.js +214 -0
- package/dist/server/cli.js +630 -0
- package/dist/server/command.js +44 -0
- package/dist/server/constants.js +427 -0
- package/dist/server/docking.js +541 -0
- package/dist/server/downlink.js +191 -0
- package/dist/server/home.js +497 -0
- package/dist/server/hooks.js +166 -0
- package/dist/server/hub.js +2484 -0
- package/dist/server/launcher.js +542 -0
- package/dist/server/livery.js +210 -0
- package/dist/server/logsearch.js +112 -0
- package/dist/server/mcp.js +196 -0
- package/dist/server/migrate.js +119 -0
- package/dist/server/missionlog.js +209 -0
- package/dist/server/payload.js +168 -0
- package/dist/server/propellant.js +314 -0
- package/dist/server/providers.js +137 -0
- package/dist/server/relay.js +300 -0
- package/dist/server/relaykinds.js +182 -0
- package/dist/server/schedules.js +277 -0
- package/dist/server/tasks.js +272 -0
- package/dist/server/terminal.js +635 -0
- package/dist/server/uplinks.js +356 -0
- package/dist/server/web.js +1466 -0
- package/dist/shared/cron.js +149 -0
- package/dist/shared/fuel.js +18 -0
- package/dist/shared/livery.js +348 -0
- package/dist/shared/positions.js +132 -0
- package/dist/shared/protocol.js +212 -0
- package/package.json +84 -3
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { API_ERROR_CODES, HOOK_EVENTS } from '../shared/protocol.js';
|
|
2
|
+
import { AGENT_NAME_PATTERN, MAX_HOOK_BODY_BYTES } from './constants.js';
|
|
3
|
+
/* ---- HTTP plumbing shared by hooks.ts, mcp.ts and web.ts (this file sits at the bottom of their import order) ---- */
|
|
4
|
+
export function isRecord(x) {
|
|
5
|
+
return typeof x === 'object' && x !== null && !Array.isArray(x);
|
|
6
|
+
}
|
|
7
|
+
/** Recognises a HubError by its name and code, not by instanceof (protocol.ts: the hub's class is not imported here). */
|
|
8
|
+
export function hubErrorInfo(e) {
|
|
9
|
+
if (!(e instanceof Error) || e.name !== 'HubError' || !('code' in e))
|
|
10
|
+
return null;
|
|
11
|
+
const { code } = e;
|
|
12
|
+
if (typeof code !== 'string' || !API_ERROR_CODES.includes(code))
|
|
13
|
+
return null;
|
|
14
|
+
const field = 'field' in e && typeof e.field === 'string' ? e.field : undefined;
|
|
15
|
+
const info = { code: code, message: e.message };
|
|
16
|
+
if (field !== undefined)
|
|
17
|
+
info.field = field;
|
|
18
|
+
return info;
|
|
19
|
+
}
|
|
20
|
+
/** The token of an `Authorization: Bearer <token>` header, or null. */
|
|
21
|
+
export function bearerToken(header) {
|
|
22
|
+
const m = header === undefined ? null : /^Bearer +(\S+)$/i.exec(header);
|
|
23
|
+
return m ? m[1] : null;
|
|
24
|
+
}
|
|
25
|
+
export function sendText(res, status, text) {
|
|
26
|
+
if (res.headersSent || res.writableEnded)
|
|
27
|
+
return;
|
|
28
|
+
res.writeHead(status, { 'Content-Type': 'text/plain', 'Content-Length': Buffer.byteLength(text) });
|
|
29
|
+
res.end(text);
|
|
30
|
+
}
|
|
31
|
+
export function sendJson(res, status, body, headers = {}) {
|
|
32
|
+
if (res.headersSent || res.writableEnded)
|
|
33
|
+
return;
|
|
34
|
+
const text = JSON.stringify(body);
|
|
35
|
+
res.writeHead(status, { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(text), ...headers });
|
|
36
|
+
res.end(text);
|
|
37
|
+
}
|
|
38
|
+
export function sendApiError(res, status, error, code, field, headers = {}) {
|
|
39
|
+
const body = field === undefined ? { error, code } : { error, code, field };
|
|
40
|
+
sendJson(res, status, body, headers);
|
|
41
|
+
}
|
|
42
|
+
export function sendUnauthorized(res) {
|
|
43
|
+
sendApiError(res, 401, 'unauthorized', 'unauthorized', undefined, { 'WWW-Authenticate': 'Bearer' });
|
|
44
|
+
}
|
|
45
|
+
export function sendNotFound(res, error = 'not found') {
|
|
46
|
+
sendApiError(res, 404, error, 'not_found');
|
|
47
|
+
}
|
|
48
|
+
export function sendMethodNotAllowed(res, allow) {
|
|
49
|
+
sendApiError(res, 405, 'method not allowed', 'bad_request', undefined, { Allow: allow });
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Collects a request body of at most `limit` bytes. Past the limit nothing more is kept and the rest is read and thrown
|
|
53
|
+
* away, so the client can finish sending and read the 413 instead of meeting a reset.
|
|
54
|
+
*/
|
|
55
|
+
export function readBody(req, limit) {
|
|
56
|
+
return new Promise((resolve) => {
|
|
57
|
+
const declared = Number(req.headers['content-length']);
|
|
58
|
+
if (req.destroyed) {
|
|
59
|
+
resolve({ ok: false, reason: 'aborted' });
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (req.readableEnded) {
|
|
63
|
+
resolve({ ok: true, body: Buffer.alloc(0) });
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (Number.isFinite(declared) && declared > limit) {
|
|
67
|
+
req.resume();
|
|
68
|
+
resolve({ ok: false, reason: 'too_large' });
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const chunks = [];
|
|
72
|
+
let size = 0;
|
|
73
|
+
let settled = false;
|
|
74
|
+
const settle = (result) => {
|
|
75
|
+
if (settled)
|
|
76
|
+
return;
|
|
77
|
+
settled = true;
|
|
78
|
+
req.removeListener('data', onData);
|
|
79
|
+
req.removeListener('end', onEnd);
|
|
80
|
+
req.removeListener('error', onAbort);
|
|
81
|
+
req.removeListener('close', onAbort);
|
|
82
|
+
resolve(result);
|
|
83
|
+
};
|
|
84
|
+
const onData = (chunk) => {
|
|
85
|
+
size += chunk.length;
|
|
86
|
+
if (size > limit) {
|
|
87
|
+
settle({ ok: false, reason: 'too_large' });
|
|
88
|
+
req.resume();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
chunks.push(chunk);
|
|
92
|
+
};
|
|
93
|
+
const onEnd = () => settle({ ok: true, body: Buffer.concat(chunks, size) });
|
|
94
|
+
const onAbort = () => settle({ ok: false, reason: 'aborted' });
|
|
95
|
+
req.on('data', onData);
|
|
96
|
+
req.on('end', onEnd);
|
|
97
|
+
req.on('error', onAbort);
|
|
98
|
+
req.on('close', onAbort);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
/* ---- the hook endpoint ---- */
|
|
102
|
+
function isHookEvent(event) {
|
|
103
|
+
return HOOK_EVENTS.includes(event);
|
|
104
|
+
}
|
|
105
|
+
/** Only a Stop reply may carry a decision, and only a block with a reason; every other shape lets the agent go on. */
|
|
106
|
+
function stopReplyBody(reply) {
|
|
107
|
+
if (isRecord(reply) && reply.decision === 'block' && typeof reply.reason === 'string' && reply.reason !== '') {
|
|
108
|
+
return { decision: 'block', reason: reply.reason };
|
|
109
|
+
}
|
|
110
|
+
return {};
|
|
111
|
+
}
|
|
112
|
+
export function createHookHandler(deps) {
|
|
113
|
+
const { hub, log } = deps;
|
|
114
|
+
const nameRe = new RegExp(AGENT_NAME_PATTERN);
|
|
115
|
+
return async (req, res, agentName, event) => {
|
|
116
|
+
const token = bearerToken(req.headers.authorization);
|
|
117
|
+
const identity = token === null ? null : deps.resolveToken(token);
|
|
118
|
+
if (!identity) {
|
|
119
|
+
sendUnauthorized(res);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (req.method !== 'POST') {
|
|
123
|
+
sendMethodNotAllowed(res, 'POST');
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (!nameRe.test(agentName) || !isHookEvent(event)) {
|
|
127
|
+
sendNotFound(res);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (identity.agent !== agentName) {
|
|
131
|
+
sendApiError(res, 403, 'token does not belong to this agent', 'forbidden');
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const body = await readBody(req, MAX_HOOK_BODY_BYTES);
|
|
135
|
+
if (!body.ok) {
|
|
136
|
+
if (body.reason === 'too_large')
|
|
137
|
+
sendApiError(res, 413, 'hook body too large', 'too_large');
|
|
138
|
+
else
|
|
139
|
+
res.destroy();
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
let payload = null;
|
|
143
|
+
try {
|
|
144
|
+
payload = JSON.parse(body.body.toString('utf8'));
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
// answered below: a body that is not JSON is the same 400 as one that is not an object
|
|
148
|
+
}
|
|
149
|
+
if (!isRecord(payload)) {
|
|
150
|
+
sendApiError(res, 400, 'hook body must be a JSON object', 'bad_request');
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
let reply;
|
|
154
|
+
try {
|
|
155
|
+
// the hub reads every field defensively (protocol.ts), so the unchecked shape is what it expects
|
|
156
|
+
reply = hub.onHook({ agent: agentName, generation: identity.generation, event, payload: payload });
|
|
157
|
+
}
|
|
158
|
+
catch (e) {
|
|
159
|
+
log.error(`hook ${event} of ${agentName} failed`, e);
|
|
160
|
+
sendApiError(res, 500, 'internal error', 'internal');
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
log.debug(`hook ${event} of ${agentName}`);
|
|
164
|
+
sendJson(res, 200, event === 'Stop' ? stopReplyBody(reply) : {});
|
|
165
|
+
};
|
|
166
|
+
}
|