Fix architecture review findings and align CLI metadata
This commit is contained in:
1 parent
780a4ba9c0
commit
50e4e492b4
83 files changed
+966
-792
No files matched your search
+18
-4
@@ -5,6 +5,7 @@ import { clearSealSession, getSealSession, setSealSession } from "./token-store.
|
||||
import { SealEnterpriseConfig, SealSession } from "./types.js";
|
||||
|
||||
const memoryCache = new Map<string, SealSession>();
|
||||
const pendingSessions = new Map<string, Promise<SealSession>>();
|
||||
|
||||
function isExpired(session: SealSession, refreshTtl: number): boolean {
|
||||
return Date.now() >= session.expiresAt - refreshTtl * 1000;
|
||||
@@ -32,10 +33,15 @@ export async function getSealSessionForCorp(corp: CorpConfig): Promise<SealSessi
|
||||
return stored;
|
||||
}
|
||||
|
||||
const next = await refreshSealSession(corp);
|
||||
memoryCache.set(corp.id, next);
|
||||
setSealSession(corp.id, next);
|
||||
return next;
|
||||
const pending = pendingSessions.get(corp.id);
|
||||
if (pending) return pending;
|
||||
const request = refreshSealSession(corp).then((next) => {
|
||||
memoryCache.set(corp.id, next);
|
||||
setSealSession(corp.id, next);
|
||||
return next;
|
||||
}).finally(() => pendingSessions.delete(corp.id));
|
||||
pendingSessions.set(corp.id, request);
|
||||
return request;
|
||||
}
|
||||
|
||||
export async function resolveSealEnterpriseConfig(
|
||||
@@ -48,3 +54,11 @@ export function clearSealToken(corpId: string): void {
|
||||
memoryCache.delete(corpId);
|
||||
clearSealSession(corpId);
|
||||
}
|
||||
|
||||
/** Reuse a concurrent refresh rather than invalidating a newer session on a late 401. */
|
||||
export async function recoverSealSessionForCorp(corp: CorpConfig, rejectedToken: string): Promise<SealSession> {
|
||||
const current = await getSealSessionForCorp(corp);
|
||||
if (current.token !== rejectedToken) return current;
|
||||
clearSealToken(corp.id);
|
||||
return getSealSessionForCorp(corp);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { afterEach, describe, expect, test } from "bun:test";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { chmodSync, mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import {
|
||||
@@ -80,6 +80,17 @@ describe("token-store", () => {
|
||||
expect(getSealSession("corp-1")).toBeUndefined();
|
||||
});
|
||||
|
||||
test.skipIf(process.platform === "win32")("tightens an existing token store without losing other corp caches", () => {
|
||||
const path = makeTempStore();
|
||||
const cache = { openapi: { token: "fixture-token", expiresAt: Date.now() + 3600000 } };
|
||||
setHoseAuthCache("other", cache);
|
||||
chmodSync(path, 0o644);
|
||||
setHoseAuthCache("target", cache);
|
||||
expect(statSync(path).mode & 0o777).toBe(0o600);
|
||||
expect(getHoseAuthCache("other")).toEqual(cache);
|
||||
expect(getHoseAuthCache("target")).toEqual(cache);
|
||||
});
|
||||
|
||||
test("sets, retrieves, and clears hose auth cache", () => {
|
||||
makeTempStore();
|
||||
const hoseCache = {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { existsSync, readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { writeSecureFile } from "../secure-file.js";
|
||||
import { SEAL_CONFIG_DIR } from "../paths.js";
|
||||
import {
|
||||
CorpTokenCache,
|
||||
@@ -41,8 +42,7 @@ function readStore(): TokenStore {
|
||||
|
||||
function writeStore(store: TokenStore): void {
|
||||
const path = resolveWriteStorePath();
|
||||
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
||||
writeFileSync(path, JSON.stringify(store, null, 2), { mode: 0o600 });
|
||||
writeSecureFile(path, JSON.stringify(store, null, 2));
|
||||
}
|
||||
|
||||
export function getSealSession(corpId: string): SealSession | undefined {
|
||||
|
||||
Reference in new issue
Block a user