dsh-ustc-campus 0.2.15 → 0.3.0

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.
@@ -44,11 +44,11 @@ const MAX_LIMIT = 50;
44
44
  const MAX_PAGE = 100;
45
45
  const MAX_BODY_BYTES = 2 * 1024 * 1024;
46
46
  const REQUEST_TIMEOUT_MS = 20_000;
47
- const NAVIGATION_TIMEOUT_MS = 25_000;
47
+ const SESSION_TIMEOUT_MS = 8_000;
48
48
  const MAX_PAGE_ROWS = 500;
49
49
  const MAX_OCCUPIED_INTERVALS = 200;
50
- const SESSION_POLLS = 60;
51
50
  const SESSION_POLL_MS = 250;
51
+ const LOGIN_SETTLE_POLLS = 3;
52
52
 
53
53
  const DATE_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/;
54
54
  const CONTROL_PATTERN = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/g;
@@ -289,13 +289,13 @@ function parseEnvelope(text) {
289
289
  return { kind: 'ok', envelope: body };
290
290
  }
291
291
 
292
- async function getEnvelope(context, url) {
292
+ async function getEnvelope(context, url, timeout = REQUEST_TIMEOUT_MS) {
293
293
  const request = context?.request;
294
294
  if (!request || typeof request.get !== 'function') return { kind: 'not_connected' };
295
295
  let response;
296
296
  try {
297
297
  response = await request.get(url, {
298
- timeout: REQUEST_TIMEOUT_MS,
298
+ timeout,
299
299
  maxRedirects: 0,
300
300
  failOnStatusCode: false,
301
301
  headers: { accept: 'application/json' }
@@ -384,13 +384,13 @@ function checkPageSite(page) {
384
384
  return { kind: 'ok' };
385
385
  }
386
386
 
387
- async function navigateToEntry(page) {
387
+ async function navigateToEntry(page, timeout) {
388
388
  if (!page || typeof page.goto !== 'function') return { kind: 'failure' };
389
389
  let response;
390
390
  try {
391
391
  response = await page.goto(ENTRY_URL, {
392
392
  waitUntil: 'commit',
393
- timeout: NAVIGATION_TIMEOUT_MS
393
+ timeout
394
394
  });
395
395
  } catch {
396
396
  const failedUrl = currentUrl(page);
@@ -409,40 +409,41 @@ async function navigateToEntry(page) {
409
409
  }
410
410
  return site;
411
411
  }
412
-
413
- async function readUserInfo(context) {
414
- const info = await getEnvelope(context, officialUrl(USER_INFO_PATH));
412
+ async function readUserInfo(context, timeout) {
413
+ const info = await getEnvelope(context, officialUrl(USER_INFO_PATH), timeout);
415
414
  if (info.kind !== 'ok') return info;
416
415
  const data = info.envelope.data;
417
416
  if (!data || typeof data !== 'object' || Array.isArray(data)) return { kind: 'failure' };
418
417
  return { kind: 'ok' };
419
418
  }
420
419
 
421
- async function verifySession(context, page) {
422
- let info = await readUserInfo(context);
423
- if (info.kind === 'not_connected' && page && typeof page.waitForTimeout === 'function') {
424
- // Allow automatic CAS SSO redirect chains to finish before treating identity host as a login wall.
425
- for (let attempt = 0; attempt < SESSION_POLLS; attempt += 1) {
426
- const url = currentUrl(page);
427
- if (url && IDENTITY_HOSTS.has(url.hostname) && url.hostname === 'id.ustc.edu.cn' && typeof page.locator === 'function') {
420
+ async function verifySession(context, page, deadline) {
421
+ let lastUrl;
422
+ let settled = 0;
423
+ let info = { kind: 'not_connected' };
424
+ while (Date.now() < deadline) {
425
+ const url = page && currentUrl(page);
426
+ if (url && IDENTITY_HOSTS.has(url.hostname)) {
427
+ if (typeof page.waitForTimeout !== 'function') return info;
428
+ if (typeof page.locator === 'function') {
428
429
  try {
429
- if (await page.locator('input[type="password"]').count() > 0) {
430
- return { kind: 'not_connected' };
431
- }
432
- } catch { /* ignore */ }
433
- }
434
- try {
435
- await page.waitForTimeout(SESSION_POLL_MS);
436
- } catch {
437
- break;
430
+ if (await page.locator('input[type="password"]').count() > 0) return info;
431
+ } catch { /* redirect may replace the document */ }
438
432
  }
439
- info = await readUserInfo(context);
440
- if (info.kind !== 'not_connected') break;
433
+ settled = url.href === lastUrl ? settled + 1 : 1;
434
+ lastUrl = url.href;
435
+ if (settled >= LOGIN_SETTLE_POLLS) return info;
436
+ } else {
437
+ if (page && checkPageSite(page).kind !== 'ok') return { kind: 'failure' };
438
+ const remaining = deadline - Date.now();
439
+ if (remaining <= 0) break;
440
+ info = await readUserInfo(context, Math.min(2_000, remaining));
441
+ if (info.kind !== 'not_connected' || typeof page?.waitForTimeout !== 'function') return info;
441
442
  }
442
- }
443
- if (page && info.kind !== 'ok') {
444
- const site = checkPageSite(page);
445
- if (site.kind !== 'ok') return site;
443
+ const remaining = deadline - Date.now();
444
+ if (remaining <= 0) break;
445
+ try { await page.waitForTimeout(Math.min(SESSION_POLL_MS, remaining)); }
446
+ catch { break; }
446
447
  }
447
448
  return info;
448
449
  }
@@ -457,7 +458,8 @@ async function openSession(context) {
457
458
  if (active) return active;
458
459
 
459
460
  const run = (async () => {
460
- if (typeof context.newPage !== 'function') return verifySession(context, null);
461
+ const deadline = Date.now() + SESSION_TIMEOUT_MS;
462
+ if (typeof context.newPage !== 'function') return verifySession(context, null, deadline);
461
463
  let page;
462
464
  try {
463
465
  page = await context.newPage();
@@ -466,9 +468,11 @@ async function openSession(context) {
466
468
  }
467
469
  try {
468
470
  await installNavigationGuard(page);
469
- const navigation = await navigateToEntry(page);
471
+ const remaining = deadline - Date.now();
472
+ if (remaining <= 0) return { kind: 'failure' };
473
+ const navigation = await navigateToEntry(page, Math.min(3_000, remaining));
470
474
  if (navigation.kind !== 'ok') return navigation;
471
- return await verifySession(context, page);
475
+ return await verifySession(context, page, deadline);
472
476
  } finally {
473
477
  await closePage(page);
474
478
  }
@@ -753,7 +757,7 @@ export function createLibrarySpaces({ withSchoolContext } = {}) {
753
757
  const returned = await withSchoolContext(async (context) => {
754
758
  captured = await operation(context);
755
759
  return captured;
756
- });
760
+ }, { refreshCas: false });
757
761
  if (isResult(returned)) return returned;
758
762
  if (isResult(captured)) return captured;
759
763
  return fallback('failure');