@nbtca/prompt 1.5.18 → 1.5.20
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/README.md +38 -22
- package/dist/app/views/schedule-render.js +2 -1
- package/dist/app/views/schedule.js +4 -4
- package/dist/cli.js +3 -3
- package/dist/features/calendar-store.js +11 -16
- package/dist/features/calendar.js +65 -56
- package/dist/features/status.js +17 -10
- package/dist/i18n/locales/en.json +3 -2
- package/dist/i18n/locales/zh.json +3 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,45 +1,64 @@
|
|
|
1
1
|
# NBTCA Prompt
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Club events, the school calendar, your campus timetable, and the NBTCA knowledge
|
|
4
|
+
base in one terminal app. What you have loaded stays readable offline.
|
|
4
5
|
|
|
5
6
|
[](https://www.npmjs.com/package/@nbtca/prompt)
|
|
6
7
|
[](LICENSE)
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
- Node.js 20.12 or newer
|
|
9
|
+

|
|
11
10
|
|
|
12
11
|
## Install
|
|
13
12
|
|
|
13
|
+
Requires Node.js 20.12 or newer.
|
|
14
|
+
|
|
14
15
|
```bash
|
|
15
16
|
npm install --global @nbtca/prompt
|
|
16
17
|
nbtca
|
|
17
18
|
```
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
Or try it without installing: `npx @nbtca/prompt`.
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
## Interactive mode
|
|
23
|
+
|
|
24
|
+
`nbtca` opens a full-screen client. Press `1`–`5` or `Tab` to switch tabs, and
|
|
25
|
+
`?` to list every key.
|
|
26
|
+
|
|
27
|
+
- **Home**: today's classes and the next club events.
|
|
28
|
+
- **Schedule**: the current term, week, and next break. Log in to the campus
|
|
29
|
+
system to see your timetable and export it to any calendar app as `.ics`.
|
|
30
|
+
- **Events**: a countdown to the next event, plus week and month views, search,
|
|
31
|
+
history, and a one-year activity heatmap.
|
|
32
|
+
- **Docs**: read and search the knowledge base. It works even when GitHub is
|
|
33
|
+
unreachable.
|
|
34
|
+
- **Settings**: language, icons, and colors.
|
|
35
|
+
|
|
36
|
+
The first load needs a network connection. Cached events and school calendars
|
|
37
|
+
remain available without an age limit; a failed refresh shows a cached-data notice.
|
|
38
|
+
Docs are available offline after reading them or loading them through search,
|
|
39
|
+
subject to the local cache's 4 MiB size limit.
|
|
24
40
|
|
|
25
41
|
## Commands
|
|
26
42
|
|
|
27
43
|
```text
|
|
28
|
-
nbtca events [--json]
|
|
44
|
+
nbtca events [--today|--week|--month] [--search=<q>] [--json]
|
|
45
|
+
nbtca schedule login|terms|export|logout
|
|
46
|
+
nbtca status [--watch]
|
|
29
47
|
nbtca docs
|
|
30
|
-
nbtca
|
|
31
|
-
nbtca
|
|
32
|
-
nbtca
|
|
33
|
-
nbtca schedule export --term=2026:3 --week-one=YYYY-MM-DD
|
|
34
|
-
nbtca schedule logout
|
|
48
|
+
nbtca website|github|roadmap|repair [--open]
|
|
49
|
+
nbtca lang zh|en
|
|
50
|
+
nbtca update
|
|
35
51
|
```
|
|
36
52
|
|
|
37
|
-
|
|
38
|
-
|
|
53
|
+
`nbtca --help` lists every command and flag. Add `--plain` for output without
|
|
54
|
+
color.
|
|
39
55
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
56
|
+
Service checks wait up to 15 seconds by default. Use `--timeout=<ms>` to change
|
|
57
|
+
the limit. A timeout is shown separately from an offline result and appears as
|
|
58
|
+
`timedOut: true` in JSON. Failed public checks still return a nonzero exit code.
|
|
59
|
+
|
|
60
|
+
Prompt never stores your password. A saved campus session gets user-only file
|
|
61
|
+
permissions. See [SECURITY.md](SECURITY.md).
|
|
43
62
|
|
|
44
63
|
## Development
|
|
45
64
|
|
|
@@ -48,9 +67,6 @@ npm ci
|
|
|
48
67
|
npm run check
|
|
49
68
|
```
|
|
50
69
|
|
|
51
|
-
`check` runs formatting, lint, full TypeScript validation, tests, build, package
|
|
52
|
-
consumer checks and dependency audit.
|
|
53
|
-
|
|
54
70
|
## License
|
|
55
71
|
|
|
56
72
|
MIT
|
|
@@ -7,6 +7,7 @@ import { renderNextClassBanner, renderWeekGrid, renderUnresolvedItems, renderTod
|
|
|
7
7
|
import { sanitizeTerminalLine, truncate, truncateStart, visualWidth, wrapAnsiWithIndent, } from '../../core/text.js';
|
|
8
8
|
import { isoDayDifference } from '../../core/calendar-day.js';
|
|
9
9
|
import { loadingLines } from '../../core/components/spinner.js';
|
|
10
|
+
import { offlineNotice } from '../chrome.js';
|
|
10
11
|
function heading(label) {
|
|
11
12
|
return `${space.indent}${type.heading(label)}`;
|
|
12
13
|
}
|
|
@@ -260,7 +261,7 @@ function daysUntil(now, date) {
|
|
|
260
261
|
}
|
|
261
262
|
function renderPublicBody(state, now, bodyRows, cols) {
|
|
262
263
|
const trans = t();
|
|
263
|
-
const lines = [];
|
|
264
|
+
const lines = state.publicStale ? offlineNotice(cols) : [];
|
|
264
265
|
const w = state.publicWindow;
|
|
265
266
|
if (w === undefined) {
|
|
266
267
|
lines.push(...loadingLines(trans.common.loading, cols));
|
|
@@ -14,7 +14,7 @@ import { loginWithStudentPassword, restoreNbtSession, } from '../../auth/nbt-aut
|
|
|
14
14
|
import { createSessionStore } from '../../auth/session-store.js';
|
|
15
15
|
import { resolveTerm, relevantTerms, writePrivateIcs, isSessionExpired, JWXT_ORIGIN, safeMessage, } from '../../features/student-timetable.js';
|
|
16
16
|
import { termKey, loadWeekOne, saveWeekOne, saveTimetableCache, saveCurrentPointer, loadCurrentPointer, loadTimetableCache, clearScheduleCache, } from '../../features/schedule-store.js';
|
|
17
|
-
import {
|
|
17
|
+
import { loadSchoolCalendarOrCache } from '../../features/calendar.js';
|
|
18
18
|
import { currentAcademicWindow, inferWeekOneMonday, } from '@nbtca/nbtcal';
|
|
19
19
|
import { sanitizeAcademicTerm, sanitizeTimetable } from '../../features/timetable-sanitize.js';
|
|
20
20
|
import { addLocalDays, parseLocalMonday } from '../../core/calendar-day.js';
|
|
@@ -121,13 +121,13 @@ async function goToPublic(ctx, generation = lifecycleGeneration) {
|
|
|
121
121
|
state = { mode: 'public', publicField: buildPublicField() };
|
|
122
122
|
ctx.rerender();
|
|
123
123
|
try {
|
|
124
|
-
const cal = await
|
|
124
|
+
const { calendar: cal, stale } = await loadSchoolCalendarOrCache(ctx.signal);
|
|
125
125
|
if (!isLifecycleActive(ctx, generation))
|
|
126
126
|
return;
|
|
127
127
|
const now = new Date();
|
|
128
128
|
const windowEvents = cal.inRange(addLocalDays(now, -400), addLocalDays(now, 400));
|
|
129
129
|
const publicWindow = currentAcademicWindow(windowEvents, now);
|
|
130
|
-
state = { ...state, publicWindow };
|
|
130
|
+
state = { ...state, publicWindow, publicStale: stale };
|
|
131
131
|
}
|
|
132
132
|
catch {
|
|
133
133
|
if (!isLifecycleActive(ctx, generation))
|
|
@@ -139,7 +139,7 @@ async function goToPublic(ctx, generation = lifecycleGeneration) {
|
|
|
139
139
|
}
|
|
140
140
|
async function tryInferWeekOne(ctx, generation) {
|
|
141
141
|
try {
|
|
142
|
-
const cal = await
|
|
142
|
+
const { calendar: cal } = await loadSchoolCalendarOrCache(ctx.signal);
|
|
143
143
|
if (!isLifecycleActive(ctx, generation))
|
|
144
144
|
return null;
|
|
145
145
|
const now = new Date();
|
package/dist/cli.js
CHANGED
|
@@ -3,7 +3,7 @@ import { main } from './main.js';
|
|
|
3
3
|
import { loadCalendarOrCache, renderEventsTable, serializeEvents, toDisplayEvent, yearHeatmap, } from './features/calendar.js';
|
|
4
4
|
import { renderHeatmap } from './features/calendar-heatmap.js';
|
|
5
5
|
import { currentEvents, dayRange, monthRange, weekRange } from './features/calendar-query.js';
|
|
6
|
-
import { checkServices, countServiceHealth, hasServiceFailures, renderServiceStatusTable, serializeServiceStatus, } from './features/status.js';
|
|
6
|
+
import { checkServices, DEFAULT_STATUS_TIMEOUT_MS, countServiceHealth, hasServiceFailures, renderServiceStatusTable, serializeServiceStatus, } from './features/status.js';
|
|
7
7
|
import { pickIcon } from './core/icons.js';
|
|
8
8
|
import { applyColorModePreference } from './config/preferences.js';
|
|
9
9
|
import { openDocsInBrowser } from './features/docs.js';
|
|
@@ -314,7 +314,7 @@ async function runStatusCommand(flags) {
|
|
|
314
314
|
: 10;
|
|
315
315
|
const timeoutMs = timeoutFlag
|
|
316
316
|
? parseAsciiDecimalInteger(timeoutFlag.slice('--timeout='.length))
|
|
317
|
-
:
|
|
317
|
+
: DEFAULT_STATUS_TIMEOUT_MS;
|
|
318
318
|
const retries = retriesFlag
|
|
319
319
|
? parseAsciiDecimalInteger(retriesFlag.slice('--retries='.length))
|
|
320
320
|
: 1;
|
|
@@ -374,7 +374,7 @@ async function runStatusCommand(flags) {
|
|
|
374
374
|
const health = countServiceHealth(services);
|
|
375
375
|
clearScreen();
|
|
376
376
|
console.log(chalk.bold(`${trans.status.watchUpdated}: ${new Date().toLocaleString()}`));
|
|
377
|
-
console.log(chalk.dim(`${trans.status.up}: ${health.up} | ${trans.status.down}: ${health.down} | ${trans.status.watchHint}`));
|
|
377
|
+
console.log(chalk.dim(`${trans.status.up}: ${health.up} | ${trans.status.down}: ${health.down} | ${trans.status.timeout}: ${health.timedOut} | ${trans.status.watchHint}`));
|
|
378
378
|
console.log();
|
|
379
379
|
const useColor = !flags.has('--plain') && isTty(process.stdout.isTTY);
|
|
380
380
|
console.log(renderServiceStatusTable(services, { color: useColor }));
|
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { getStateDir, getWritableStateDir } from '../config/paths.js';
|
|
4
|
-
const FEED_FILE = 'calendar-feed.ics';
|
|
5
|
-
const VALIDATORS_FILE = 'calendar-feed.json';
|
|
6
|
-
const MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
7
4
|
const HEADER_VALUE = /^[\x21-\x7e][\x20-\x7e]*$/;
|
|
8
5
|
function headerValue(value) {
|
|
9
6
|
return typeof value === 'string' && HEADER_VALUE.test(value) ? value : undefined;
|
|
10
7
|
}
|
|
11
|
-
function readValidators(dir) {
|
|
8
|
+
function readValidators(name, dir) {
|
|
12
9
|
try {
|
|
13
|
-
const data = JSON.parse(fs.readFileSync(path.join(dir,
|
|
10
|
+
const data = JSON.parse(fs.readFileSync(path.join(dir, `${name}.json`), 'utf8'));
|
|
14
11
|
const etag = headerValue(data['etag']);
|
|
15
12
|
const lastModified = headerValue(data['lastModified']);
|
|
16
13
|
return { ...(etag ? { etag } : {}), ...(lastModified ? { lastModified } : {}) };
|
|
@@ -20,22 +17,22 @@ function readValidators(dir) {
|
|
|
20
17
|
}
|
|
21
18
|
}
|
|
22
19
|
// Validators are written after the text and read before it, so they can never be newer than the text.
|
|
23
|
-
export function saveFeedCache({ text, validators }, dir) {
|
|
20
|
+
export function saveFeedCache(name, { text, validators }, dir) {
|
|
24
21
|
try {
|
|
25
22
|
const target = dir ?? getWritableStateDir();
|
|
26
|
-
fs.writeFileSync(path.join(target,
|
|
27
|
-
touchFeedCache(validators, target);
|
|
23
|
+
fs.writeFileSync(path.join(target, `${name}.ics`), text, { encoding: 'utf8', mode: 0o600 });
|
|
24
|
+
touchFeedCache(name, validators, target);
|
|
28
25
|
}
|
|
29
26
|
catch {
|
|
30
27
|
/* best effort */
|
|
31
28
|
}
|
|
32
29
|
}
|
|
33
|
-
export function touchFeedCache(validators, dir) {
|
|
30
|
+
export function touchFeedCache(name, validators, dir) {
|
|
34
31
|
try {
|
|
35
32
|
const target = dir ?? getWritableStateDir();
|
|
36
33
|
const now = new Date();
|
|
37
|
-
fs.utimesSync(path.join(target,
|
|
38
|
-
fs.writeFileSync(path.join(target,
|
|
34
|
+
fs.utimesSync(path.join(target, `${name}.ics`), now, now);
|
|
35
|
+
fs.writeFileSync(path.join(target, `${name}.json`), JSON.stringify(validators), {
|
|
39
36
|
encoding: 'utf8',
|
|
40
37
|
mode: 0o600,
|
|
41
38
|
});
|
|
@@ -44,13 +41,11 @@ export function touchFeedCache(validators, dir) {
|
|
|
44
41
|
/* best effort */
|
|
45
42
|
}
|
|
46
43
|
}
|
|
47
|
-
export function loadFeedCache(
|
|
44
|
+
export function loadFeedCache(name, dir) {
|
|
48
45
|
try {
|
|
49
46
|
const source = dir ?? getStateDir();
|
|
50
|
-
const validators = readValidators(source);
|
|
51
|
-
const file = path.join(source,
|
|
52
|
-
if (Date.now() - fs.statSync(file).mtimeMs > maxAgeMs)
|
|
53
|
-
return null;
|
|
47
|
+
const validators = readValidators(name, source);
|
|
48
|
+
const file = path.join(source, `${name}.ics`);
|
|
54
49
|
return { text: fs.readFileSync(file, 'utf8'), validators };
|
|
55
50
|
}
|
|
56
51
|
catch {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { fetchFeedConditional, parseCalendar, createCalendar, FeedFetchError, FeedParseError, eventToICS, } from '@nbtca/nbtcal';
|
|
1
|
+
import { fetchFeedConditional, parseCalendar, createCalendar, FeedFetchError, FeedParseError, eventToICS, SCHOOL_FEED_URL, } from '@nbtca/nbtcal';
|
|
2
2
|
import chalk from 'chalk';
|
|
3
3
|
import { c, type, space, glyph, bodyWidth } from '../core/theme.js';
|
|
4
4
|
import { pickIcon } from '../core/icons.js';
|
|
@@ -24,57 +24,77 @@ function formatTime(date) {
|
|
|
24
24
|
return `${hours}:${minutes}`;
|
|
25
25
|
}
|
|
26
26
|
const MEMO_TTL_MS = 5 * 60 * 1000;
|
|
27
|
-
|
|
28
|
-
let
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
export function peekCalendar() {
|
|
34
|
-
if (memo)
|
|
27
|
+
function feedLoader(url, name) {
|
|
28
|
+
let memo;
|
|
29
|
+
let inFlight;
|
|
30
|
+
function remember(text, validators, fetchedAt) {
|
|
31
|
+
memo = { calendar: createCalendar(parseCalendar(text)), validators, fetchedAt };
|
|
35
32
|
return memo.calendar;
|
|
36
|
-
const cache = loadFeedCache();
|
|
37
|
-
if (cache === null)
|
|
38
|
-
return undefined;
|
|
39
|
-
try {
|
|
40
|
-
return remember(cache.text, cache.validators, 0);
|
|
41
33
|
}
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
function peek() {
|
|
35
|
+
if (memo)
|
|
36
|
+
return memo.calendar;
|
|
37
|
+
const cache = loadFeedCache(name);
|
|
38
|
+
if (cache === null)
|
|
39
|
+
return undefined;
|
|
40
|
+
try {
|
|
41
|
+
return remember(cache.text, cache.validators, 0);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
44
46
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
47
|
+
async function refetch(signal) {
|
|
48
|
+
try {
|
|
49
|
+
peek();
|
|
50
|
+
const result = await fetchFeedConditional(url, {
|
|
51
|
+
timeoutMs: 15000,
|
|
52
|
+
...(signal === undefined ? {} : { signal }),
|
|
53
|
+
validators: memo?.validators ?? {},
|
|
54
|
+
});
|
|
55
|
+
if (result.status === 'modified') {
|
|
56
|
+
const calendar = remember(result.text, result.validators, Date.now());
|
|
57
|
+
saveFeedCache(name, result);
|
|
58
|
+
return calendar;
|
|
59
|
+
}
|
|
60
|
+
if (!memo)
|
|
61
|
+
throw new FeedFetchError('Feed request failed: HTTP 304');
|
|
62
|
+
memo = { ...memo, validators: result.validators, fetchedAt: Date.now() };
|
|
63
|
+
touchFeedCache(name, result.validators);
|
|
64
|
+
return memo.calendar;
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
const detail = sanitizeTerminalLine(err instanceof FeedFetchError || err instanceof FeedParseError ? err.message : String(err));
|
|
68
|
+
throw new Error(`${t().calendar.error}: ${detail}`);
|
|
58
69
|
}
|
|
59
|
-
if (!memo)
|
|
60
|
-
throw new FeedFetchError('Feed request failed: HTTP 304');
|
|
61
|
-
memo = { ...memo, validators: result.validators, fetchedAt: Date.now() };
|
|
62
|
-
touchFeedCache(result.validators);
|
|
63
|
-
return memo.calendar;
|
|
64
70
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
71
|
+
async function load(signal) {
|
|
72
|
+
if (memo && Date.now() - memo.fetchedAt < MEMO_TTL_MS)
|
|
73
|
+
return memo.calendar;
|
|
74
|
+
inFlight ??= refetch(signal).finally(() => {
|
|
75
|
+
inFlight = undefined;
|
|
76
|
+
});
|
|
77
|
+
return inFlight;
|
|
68
78
|
}
|
|
79
|
+
async function loadOrCache(signal) {
|
|
80
|
+
try {
|
|
81
|
+
return { calendar: await load(signal), stale: false };
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
const cached = peek();
|
|
85
|
+
if (!cached)
|
|
86
|
+
throw err;
|
|
87
|
+
return { calendar: cached, stale: true };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return { peek, load, loadOrCache };
|
|
69
91
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return inFlight;
|
|
77
|
-
}
|
|
92
|
+
const eventsFeed = feedLoader(undefined, 'calendar-feed');
|
|
93
|
+
const schoolFeed = feedLoader(SCHOOL_FEED_URL, 'school-feed');
|
|
94
|
+
export const peekCalendar = eventsFeed.peek;
|
|
95
|
+
export const loadCalendarOrThrow = eventsFeed.load;
|
|
96
|
+
export const loadCalendarOrCache = eventsFeed.loadOrCache;
|
|
97
|
+
export const loadSchoolCalendarOrCache = schoolFeed.loadOrCache;
|
|
78
98
|
export function toDisplayEvent(e) {
|
|
79
99
|
const trans = t();
|
|
80
100
|
return {
|
|
@@ -104,17 +124,6 @@ export function eventTimeRange(e) {
|
|
|
104
124
|
? `${start}${dash}${formatTime(e.end)}`
|
|
105
125
|
: `${start} ${dash} ${endDate} ${formatTime(e.end)}`;
|
|
106
126
|
}
|
|
107
|
-
export async function loadCalendarOrCache(signal) {
|
|
108
|
-
try {
|
|
109
|
-
return { calendar: await loadCalendarOrThrow(signal), stale: false };
|
|
110
|
-
}
|
|
111
|
-
catch (err) {
|
|
112
|
-
const cached = peekCalendar();
|
|
113
|
-
if (!cached)
|
|
114
|
-
throw err;
|
|
115
|
-
return { calendar: cached, stale: true };
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
127
|
export function yearHeatmap(calendar, now) {
|
|
119
128
|
return calendar.heatmap({ ...pastYearRange(now), bucket: 'day' });
|
|
120
129
|
}
|
package/dist/features/status.js
CHANGED
|
@@ -4,6 +4,7 @@ import { pickIcon } from '../core/icons.js';
|
|
|
4
4
|
import { padEndV, sanitizeTerminalLine, visualWidth } from '../core/text.js';
|
|
5
5
|
import { c } from '../core/theme.js';
|
|
6
6
|
import { t } from '../i18n/index.js';
|
|
7
|
+
export const DEFAULT_STATUS_TIMEOUT_MS = 15_000;
|
|
7
8
|
function abortError(signal) {
|
|
8
9
|
return signal.reason instanceof Error
|
|
9
10
|
? signal.reason
|
|
@@ -80,6 +81,7 @@ function getServiceTargets() {
|
|
|
80
81
|
async function checkService(name, url, timeoutMs, signal) {
|
|
81
82
|
const start = Date.now();
|
|
82
83
|
const controller = new AbortController();
|
|
84
|
+
const timeoutReason = Symbol('timeout');
|
|
83
85
|
const onAbort = () => {
|
|
84
86
|
controller.abort(signal?.reason);
|
|
85
87
|
};
|
|
@@ -87,7 +89,7 @@ async function checkService(name, url, timeoutMs, signal) {
|
|
|
87
89
|
if (signal?.aborted)
|
|
88
90
|
onAbort();
|
|
89
91
|
const timeout = setTimeout(() => {
|
|
90
|
-
controller.abort();
|
|
92
|
+
controller.abort(timeoutReason);
|
|
91
93
|
}, timeoutMs);
|
|
92
94
|
let response;
|
|
93
95
|
try {
|
|
@@ -106,12 +108,9 @@ async function checkService(name, url, timeoutMs, signal) {
|
|
|
106
108
|
if (signal?.aborted)
|
|
107
109
|
throw abortError(signal);
|
|
108
110
|
const latencyMs = Date.now() - start;
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
: err.message
|
|
113
|
-
: String(err));
|
|
114
|
-
return { name, url, ok: false, latencyMs, error };
|
|
111
|
+
const timedOut = controller.signal.reason === timeoutReason;
|
|
112
|
+
const error = sanitizeTerminalLine(timedOut ? 'Request timed out' : err instanceof Error ? err.message : String(err));
|
|
113
|
+
return { name, url, ok: false, latencyMs, error, ...(timedOut ? { timedOut } : {}) };
|
|
115
114
|
}
|
|
116
115
|
finally {
|
|
117
116
|
clearTimeout(timeout);
|
|
@@ -141,7 +140,7 @@ async function checkServiceWithRetry(target, timeoutMs, retries, signal) {
|
|
|
141
140
|
};
|
|
142
141
|
}
|
|
143
142
|
export async function checkServices(options = {}) {
|
|
144
|
-
const timeoutMs = options.timeoutMs ??
|
|
143
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_STATUS_TIMEOUT_MS;
|
|
145
144
|
const retries = options.retries ?? 1;
|
|
146
145
|
if (options.signal?.aborted)
|
|
147
146
|
throw abortError(options.signal);
|
|
@@ -157,6 +156,7 @@ export function serializeServiceStatus(items) {
|
|
|
157
156
|
statusCode: item.statusCode ?? null,
|
|
158
157
|
latencyMs: item.latencyMs ?? null,
|
|
159
158
|
error: item.error ?? null,
|
|
159
|
+
timedOut: item.timedOut ?? false,
|
|
160
160
|
group: item.group ?? null,
|
|
161
161
|
intranet: item.intranet ?? false,
|
|
162
162
|
}));
|
|
@@ -167,13 +167,16 @@ export function hasServiceFailures(items) {
|
|
|
167
167
|
export function countServiceHealth(items) {
|
|
168
168
|
let up = 0;
|
|
169
169
|
let down = 0;
|
|
170
|
+
let timedOut = 0;
|
|
170
171
|
for (const item of items.filter((i) => !i.intranet)) {
|
|
171
172
|
if (item.ok)
|
|
172
173
|
up++;
|
|
174
|
+
else if (item.timedOut)
|
|
175
|
+
timedOut++;
|
|
173
176
|
else
|
|
174
177
|
down++;
|
|
175
178
|
}
|
|
176
|
-
return { up, down };
|
|
179
|
+
return { up, down, timedOut };
|
|
177
180
|
}
|
|
178
181
|
export function renderServiceStatusTable(items, options) {
|
|
179
182
|
const useColor = options?.color !== false;
|
|
@@ -181,11 +184,12 @@ export function renderServiceStatusTable(items, options) {
|
|
|
181
184
|
const applyDim = useColor ? chalk.dim : id;
|
|
182
185
|
const applyGreen = useColor ? chalk.green : id;
|
|
183
186
|
const applyRed = useColor ? chalk.red : id;
|
|
187
|
+
const applyYellow = useColor ? chalk.yellow : id;
|
|
184
188
|
const applyCyan = useColor ? chalk.cyan : id;
|
|
185
189
|
const applyLatency = useColor ? c.latency : (ms) => `${ms}ms`;
|
|
186
190
|
const trans = t();
|
|
187
191
|
const nameWidth = Math.max(...items.map((i) => visualWidth(i.name)), visualWidth(trans.status.service));
|
|
188
|
-
const statusWidth = 10;
|
|
192
|
+
const statusWidth = Math.max(10, visualWidth(trans.status.timeout) + 2);
|
|
189
193
|
const onIcon = pickIcon('●', '+');
|
|
190
194
|
const offIcon = pickIcon('✕', '!');
|
|
191
195
|
const lanIcon = pickIcon('○', 'o');
|
|
@@ -219,6 +223,9 @@ export function renderServiceStatusTable(items, options) {
|
|
|
219
223
|
else if (item.ok) {
|
|
220
224
|
statusLabel = applyGreen(`${onIcon} ${trans.status.up}`);
|
|
221
225
|
}
|
|
226
|
+
else if (item.timedOut) {
|
|
227
|
+
statusLabel = (item.intranet ? applyDim : applyYellow)(`${offIcon} ${trans.status.timeout}`);
|
|
228
|
+
}
|
|
222
229
|
else if (item.intranet) {
|
|
223
230
|
statusLabel = applyDim(`${lanIcon} ${trans.status.down}`);
|
|
224
231
|
}
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"current": "current",
|
|
10
10
|
"moreAbove": "{count} more above",
|
|
11
11
|
"moreBelow": "{count} more below",
|
|
12
|
-
"offline": "
|
|
12
|
+
"offline": "Refresh failed; showing cache"
|
|
13
13
|
},
|
|
14
14
|
"help": {
|
|
15
15
|
"title": "Keys",
|
|
@@ -181,7 +181,7 @@
|
|
|
181
181
|
"status": {
|
|
182
182
|
"checking": "Checking service status...",
|
|
183
183
|
"summaryOk": "All services are healthy",
|
|
184
|
-
"summaryFail": "Some
|
|
184
|
+
"summaryFail": "Some checks failed. A timeout does not confirm an outage.",
|
|
185
185
|
"summaryIntranet": "Public services are healthy; intranet availability depends on your network",
|
|
186
186
|
"service": "Service",
|
|
187
187
|
"health": "Health",
|
|
@@ -190,6 +190,7 @@
|
|
|
190
190
|
"url": "URL",
|
|
191
191
|
"up": "online",
|
|
192
192
|
"down": "offline",
|
|
193
|
+
"timeout": "timed out",
|
|
193
194
|
"groupNbtca": "NBTCA Services",
|
|
194
195
|
"groupExternal": "External",
|
|
195
196
|
"groupIntranet": "Intranet (LAN only)",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"current": "当前",
|
|
10
10
|
"moreAbove": "上方还有 {count} 项",
|
|
11
11
|
"moreBelow": "下方还有 {count} 项",
|
|
12
|
-
"offline": "
|
|
12
|
+
"offline": "无法刷新,显示上次获取的内容"
|
|
13
13
|
},
|
|
14
14
|
"help": {
|
|
15
15
|
"title": "快捷键",
|
|
@@ -181,7 +181,7 @@
|
|
|
181
181
|
"status": {
|
|
182
182
|
"checking": "正在检查服务状态...",
|
|
183
183
|
"summaryOk": "所有服务运行正常",
|
|
184
|
-
"summaryFail": "
|
|
184
|
+
"summaryFail": "部分检查未通过,超时不代表服务已经停止运行。",
|
|
185
185
|
"summaryIntranet": "公共服务运行正常;内网可用性取决于当前网络",
|
|
186
186
|
"service": "服务",
|
|
187
187
|
"health": "状态",
|
|
@@ -190,6 +190,7 @@
|
|
|
190
190
|
"url": "URL",
|
|
191
191
|
"up": "在线",
|
|
192
192
|
"down": "离线",
|
|
193
|
+
"timeout": "超时",
|
|
193
194
|
"groupNbtca": "NBTCA 服务",
|
|
194
195
|
"groupExternal": "外部平台",
|
|
195
196
|
"groupIntranet": "内网服务(仅校园网)",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nbtca/prompt",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.20",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
"interactive"
|
|
47
47
|
],
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@nbtca/docs": "^0.3.
|
|
50
|
-
"@nbtca/nbtcal": "^0.4.
|
|
49
|
+
"@nbtca/docs": "^0.3.5",
|
|
50
|
+
"@nbtca/nbtcal": "^0.4.4",
|
|
51
51
|
"chalk": "^5.6.2",
|
|
52
52
|
"marked": "^15.0.12"
|
|
53
53
|
},
|