@kne/fastify-app-manager 0.1.31 → 0.1.32
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/libs/controllers/app.js +9 -1
- package/libs/services/app.js +1 -0
- package/libs/services/link.js +105 -0
- package/package.json +1 -1
package/libs/controllers/app.js
CHANGED
|
@@ -944,7 +944,15 @@ module.exports = fp(async (fastify, options) => {
|
|
|
944
944
|
},
|
|
945
945
|
async (request, reply) => {
|
|
946
946
|
const { name, stream } = request.query;
|
|
947
|
-
await services.app.detail({ name });
|
|
947
|
+
const app = await services.app.detail({ name });
|
|
948
|
+
if (app.kind === 'linked' || app.options?.kind === 'linked') {
|
|
949
|
+
return services.link.proxy.logStream(app, {
|
|
950
|
+
reply,
|
|
951
|
+
request,
|
|
952
|
+
stream: stream || 'out',
|
|
953
|
+
actor: request.userInfo?.id || request.user?.id || null
|
|
954
|
+
});
|
|
955
|
+
}
|
|
948
956
|
const sse = createSseReply(reply, { heartbeatMs: options.sseHeartbeatMs });
|
|
949
957
|
const hub = services.app.getLogHub();
|
|
950
958
|
|
package/libs/services/app.js
CHANGED
|
@@ -1464,6 +1464,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
1464
1464
|
|
|
1465
1465
|
const removeLogFiles = async ({ name, files = [] }) => {
|
|
1466
1466
|
const app = await getByName(name);
|
|
1467
|
+
assertNotLinked(app, '删除远程日志');
|
|
1467
1468
|
const targets = [];
|
|
1468
1469
|
for (const fileName of files) {
|
|
1469
1470
|
if (isCurrentFileName(fileName)) {
|
package/libs/services/link.js
CHANGED
|
@@ -375,9 +375,113 @@ module.exports = fp(async (fastify, options) => {
|
|
|
375
375
|
return results.filter(Boolean);
|
|
376
376
|
};
|
|
377
377
|
|
|
378
|
+
/**
|
|
379
|
+
* 透传远程 SSE(logs/stream)。会 hijack reply;超时加长以免掐断长连接。
|
|
380
|
+
*/
|
|
381
|
+
const remoteStream = async (app, { path: opsPath, query, scope, reply, request, auditAction, actor } = {}) => {
|
|
382
|
+
const baseUrl = linkedBaseUrl(app);
|
|
383
|
+
if (!baseUrl) {
|
|
384
|
+
httpError(400, '应用缺少 options.baseUrl');
|
|
385
|
+
}
|
|
386
|
+
const action = auditAction || opsPath.replace(/^\/app-ops\//, '').replace(/\//g, '.');
|
|
387
|
+
const token = await getOpsToken(app, scope);
|
|
388
|
+
const url = new URL(opsPath.replace(/^\//, ''), `${baseUrl}/`);
|
|
389
|
+
if (query && typeof query === 'object') {
|
|
390
|
+
for (const [k, v] of Object.entries(query)) {
|
|
391
|
+
if (v !== undefined && v !== null && v !== '') {
|
|
392
|
+
url.searchParams.set(k, String(v));
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
const abort = new AbortController();
|
|
397
|
+
const onClose = () => abort.abort();
|
|
398
|
+
request.raw.on('close', onClose);
|
|
399
|
+
let response;
|
|
400
|
+
try {
|
|
401
|
+
response = await fetch(url, {
|
|
402
|
+
method: 'GET',
|
|
403
|
+
headers: { authorization: `Bearer ${token}`, accept: 'text/event-stream' },
|
|
404
|
+
signal: abort.signal
|
|
405
|
+
});
|
|
406
|
+
} catch (e) {
|
|
407
|
+
request.raw.off('close', onClose);
|
|
408
|
+
await proxyAudit
|
|
409
|
+
.append({
|
|
410
|
+
appName: app.name,
|
|
411
|
+
action,
|
|
412
|
+
result: 'error',
|
|
413
|
+
statusCode: 502,
|
|
414
|
+
actor,
|
|
415
|
+
detail: e.message
|
|
416
|
+
})
|
|
417
|
+
.catch(() => {});
|
|
418
|
+
httpError(502, e.message || '远程 SSE 连接失败');
|
|
419
|
+
}
|
|
420
|
+
if (!response.ok) {
|
|
421
|
+
request.raw.off('close', onClose);
|
|
422
|
+
const text = await response.text().catch(() => '');
|
|
423
|
+
await proxyAudit
|
|
424
|
+
.append({
|
|
425
|
+
appName: app.name,
|
|
426
|
+
action,
|
|
427
|
+
result: 'error',
|
|
428
|
+
statusCode: response.status,
|
|
429
|
+
actor,
|
|
430
|
+
detail: text.slice(0, 200)
|
|
431
|
+
})
|
|
432
|
+
.catch(() => {});
|
|
433
|
+
httpError(response.status >= 400 && response.status < 600 ? response.status : 502, text || `远程 ${response.status}`);
|
|
434
|
+
}
|
|
435
|
+
await proxyAudit
|
|
436
|
+
.append({
|
|
437
|
+
appName: app.name,
|
|
438
|
+
action,
|
|
439
|
+
result: 'ok',
|
|
440
|
+
statusCode: 200,
|
|
441
|
+
actor
|
|
442
|
+
})
|
|
443
|
+
.catch(() => {});
|
|
444
|
+
|
|
445
|
+
reply.hijack();
|
|
446
|
+
reply.raw.writeHead(200, {
|
|
447
|
+
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
448
|
+
'Cache-Control': 'no-cache, no-transform',
|
|
449
|
+
Connection: 'keep-alive',
|
|
450
|
+
'X-Accel-Buffering': 'no'
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
const { Readable } = require('node:stream');
|
|
454
|
+
const { pipeline } = require('node:stream/promises');
|
|
455
|
+
try {
|
|
456
|
+
const nodeStream = Readable.fromWeb(response.body);
|
|
457
|
+
await pipeline(nodeStream, reply.raw);
|
|
458
|
+
} catch (e) {
|
|
459
|
+
if (!abort.signal.aborted) {
|
|
460
|
+
fastify.log.warn({ err: e, app: app.name }, 'linked logs stream proxy ended');
|
|
461
|
+
}
|
|
462
|
+
} finally {
|
|
463
|
+
request.raw.off('close', onClose);
|
|
464
|
+
try {
|
|
465
|
+
reply.raw.end();
|
|
466
|
+
} catch (e) {
|
|
467
|
+
/* ignore */
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
|
|
378
472
|
const proxy = {
|
|
379
473
|
logs: (app, query) => remoteFetch(app, { path: '/app-ops/logs', query, scope: 'app-ops:logs:read' }),
|
|
380
474
|
logFiles: app => remoteFetch(app, { path: '/app-ops/logs/files', scope: 'app-ops:logs:read' }),
|
|
475
|
+
logStream: (app, { reply, request, stream, actor }) =>
|
|
476
|
+
remoteStream(app, {
|
|
477
|
+
path: '/app-ops/logs/stream',
|
|
478
|
+
query: stream ? { stream } : {},
|
|
479
|
+
scope: 'app-ops:logs:read',
|
|
480
|
+
reply,
|
|
481
|
+
request,
|
|
482
|
+
auditAction: 'logs.stream',
|
|
483
|
+
actor
|
|
484
|
+
}),
|
|
381
485
|
env: app => remoteFetch(app, { path: '/app-ops/env', scope: 'app-ops:env:read' }),
|
|
382
486
|
permissions: app => remoteFetch(app, { path: '/app-ops/permissions', scope: 'app-ops:permissions:read' }),
|
|
383
487
|
dbTables: app => remoteFetch(app, { path: '/app-ops/db/tables', scope: 'app-ops:db:read' }),
|
|
@@ -398,6 +502,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
398
502
|
decodeInstallKey,
|
|
399
503
|
proxy,
|
|
400
504
|
remoteFetch,
|
|
505
|
+
remoteStream,
|
|
401
506
|
listProxyAudit: proxyAudit.list,
|
|
402
507
|
applyPermissionSnapshot
|
|
403
508
|
}
|