From 5c13bbf6ea1d0f013f1b20aeec89e5583a8863e9 Mon Sep 17 00:00:00 2001 From: leemour Date: Thu, 26 Nov 2026 23:10:28 +0100 Subject: [PATCH] refactor: build on cli-core 1.3.0 and its shared storageOf and timeLimit - Pin @wirecat/cli-core 1.3.0 (store version 77, which cli-core 1.0 tools still open). The first help screen gains the `b24 session list` line cli-core 1.3.0 adds; contracts/help.snap follows. - `session start --credential-storage` is parsed by cli-core's `storageOf`, as `tokenSessionCommand` does, instead of an inline check. - The per-request deadline in the transport (calls and downloads) and in the OAuth token request is cli-core's `timeLimit`, not a copy of it. Only the error name (TimeoutError) is read, so behaviour is unchanged. - `session list` rows gain `state` (saved, missing, unknown), the field cli-core's shared session commands give since 1.1.0. b24's own `session start` and `status` stay: it signs in with a webhook or a local Bitrix24 application, and neither `tokenSessionCommand` nor `oauthSessionCommand` takes both. Old sign-ins keep their keyring entries, which this change does not touch. Co-Authored-By: Claude Opus 5.5 --- changes/refactor-cli-core-1.3-session.md | 9 ++++++++ contracts/help.snap | 1 + package.json | 2 +- pnpm-lock.yaml | 10 ++++----- src/bitrix/oauth.ts | 7 +++--- src/bitrix/transport.ts | 28 ++++++++++-------------- src/commands/session-app.test.ts | 7 +++++- src/commands/session.ts | 15 +++++++------ 8 files changed, 45 insertions(+), 34 deletions(-) create mode 100644 changes/refactor-cli-core-1.3-session.md diff --git a/changes/refactor-cli-core-1.3-session.md b/changes/refactor-cli-core-1.3-session.md new file mode 100644 index 0000000..1da0d62 --- /dev/null +++ b/changes/refactor-cli-core-1.3-session.md @@ -0,0 +1,9 @@ +### Changed — may break callers + +- **b24 is built on cli-core 1.3.0.** The store moves to version 77 on its first run, and tools on cli-core 1.0 still + open it. The first help screen names `b24 session list`. +- **`session list` rows have `state`**: `saved`, `missing` or `unknown`, as in the other WireCat CLIs. A profile whose + sign-in cannot be read stays `signed_in: null` and keeps its `error`. +- **`session start --credential-storage` with another value** says which value it got: + `--credential-storage takes keyring or file; got `. +- The deadline of each Bitrix24 request and of the token request is cli-core's `timeLimit`, not a copy of it. diff --git a/contracts/help.snap b/contracts/help.snap index ecbc458..f1537c0 100644 --- a/contracts/help.snap +++ b/contracts/help.snap @@ -31,6 +31,7 @@ Works with: linear Linear issues and projects, searchable with your other WireCat data Start here: b24 work session start (any profile name works) + b24 session list your profiles and their logged-in accounts Next: b24 commands every command, by section b24 guide scenarios: command sequences for common tasks b24 help options output, offline, timeouts, dry run diff --git a/package.json b/package.json index 8519e5d..a6d29be 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "docs:spell": "cspell lint --config cspell.json --no-progress --no-summary README.md SECURITY.md CHANGELOG.md AGENTS.md CLAUDE.md 'docs/*.md' 'skills/**/*.md'" }, "dependencies": { - "@wirecat/cli-core": "1.0.0", + "@wirecat/cli-core": "1.3.0", "commander": "15.0.0", "valibot": "1.5.0" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7f10748..8aa33e4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13,8 +13,8 @@ importers: .: dependencies: '@wirecat/cli-core': - specifier: 1.0.0 - version: 1.0.0(@wirecat/cli-base@1.0.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5))(commander@15.0.0)(typescript@7.0.2)(zod@4.6.7) + specifier: 1.3.0 + version: 1.3.0(@wirecat/cli-base@1.0.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5))(commander@15.0.0)(typescript@7.0.2)(zod@4.6.7) commander: specifier: 15.0.0 version: 15.0.0 @@ -1189,8 +1189,8 @@ packages: '@wirecat/cli-core-sqlite@1.1.0': resolution: {integrity: sha512-GpGlh/dAIX/nblBYCdT6DfWSLFxav9mxxeELQAfEuw+QZdT9unKxbi5xa02s/s4JS1P7n0PQx2sCABj4FlMRNw==} - '@wirecat/cli-core@1.0.0': - resolution: {integrity: sha512-0avQTqu3FAFKE0s+8Pz1Up/TMHTYqCdDn37lcZmkH30sMCMrgJwHzoG4Q0fjVzPsB85u+zBVHHhtQjF/eSTpuw==} + '@wirecat/cli-core@1.3.0': + resolution: {integrity: sha512-tCRgrnXf2YXNkRLFqRHf8iHcm6v1G4D2jn7coSsmqobmOqJvALPOvCiWpoPlVYGprIkXC4Twp+rR367Md2XULg==} engines: {node: ^22.16.0 || >=24} hasBin: true peerDependencies: @@ -3303,7 +3303,7 @@ snapshots: '@wirecat/cli-core-sqlite@1.1.0': {} - '@wirecat/cli-core@1.0.0(@wirecat/cli-base@1.0.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5))(commander@15.0.0)(typescript@7.0.2)(zod@4.6.7)': + '@wirecat/cli-core@1.3.0(@wirecat/cli-base@1.0.0(commander@15.0.0)(esbuild@0.28.2)(typescript@7.0.2)(vitest@5.0.5))(commander@15.0.0)(typescript@7.0.2)(zod@4.6.7)': dependencies: '@anthropic-ai/sdk': 1.3.0(zod@4.6.7) '@bomb.sh/tab': 0.0.22(commander@15.0.0) diff --git a/src/bitrix/oauth.ts b/src/bitrix/oauth.ts index 135ed1a..eadadae 100644 --- a/src/bitrix/oauth.ts +++ b/src/bitrix/oauth.ts @@ -8,6 +8,7 @@ import { systemKeyring, } from "@wirecat/cli-base" import { hostFetch } from "@wirecat/cli-base/http" +import { timeLimit } from "@wirecat/cli-core/cli" import { accountTokenCodec, credentialStorage, @@ -132,9 +133,7 @@ const exchange = async ( client_secret: context.client.clientSecret ?? "", [grant === "refresh_token" ? "refresh_token" : "code"]: given ?? "", }) - const deadline = new AbortController() - const timer = setTimeout(() => deadline.abort(), REQUEST_MS) - timer.unref() + const deadline = timeLimit(REQUEST_MS) const signal = context.signal ? AbortSignal.any([context.signal, deadline.signal]) : deadline.signal try { let response: Response @@ -177,7 +176,7 @@ const exchange = async ( scopes: typeof scope === "string" ? scope.split(",").filter(Boolean) : [], } } finally { - clearTimeout(timer) + deadline.clear() } } diff --git a/src/bitrix/transport.ts b/src/bitrix/transport.ts index e011df2..9e3af63 100644 --- a/src/bitrix/transport.ts +++ b/src/bitrix/transport.ts @@ -2,7 +2,14 @@ import { lookup as dnsLookup } from "node:dns/promises" import { isIP } from "node:net" import { CliError, isCliError, realSleep, type SleepLike, wallClock } from "@wirecat/cli-base" import { type FetchLike as HttpFetch, hostFetch, providerWaitMs, readJsonResponse } from "@wirecat/cli-base/http" -import { apiJson, currentAttempt, type EventSink, providerEvent, providerRequest } from "@wirecat/cli-core/cli" +import { + apiJson, + currentAttempt, + type EventSink, + providerEvent, + providerRequest, + timeLimit, +} from "@wirecat/cli-core/cli" import { fetchCrmFile, providerCall } from "@wirecat/cli-core/crm" import { isPublicAddress } from "@wirecat/cli-core/documents" import type { Pacer } from "@wirecat/cli-core/sends" @@ -228,12 +235,7 @@ export class BitrixTransport { endpointTemplate: onPortal ? loggedPath(url) : `${url.hostname}/…`, transport: "http", }) - const deadline = new AbortController() - const timer = setTimeout( - () => deadline.abort(new DOMException("download timed out", "TimeoutError")), - 6 * this.#timeoutMs, - ) - timer.unref() + const deadline = timeLimit(6 * this.#timeoutMs) const signal = AbortSignal.any([deadline.signal, ...(this.#signal ? [this.#signal] : [])]) try { const response = await fetch(url.href, { method: "GET", redirect: "error", signal }).catch((error) => { @@ -258,7 +260,7 @@ export class BitrixTransport { disposition: response.headers.get("content-disposition"), } } finally { - clearTimeout(timer) + deadline.clear() } } @@ -320,13 +322,7 @@ export class BitrixTransport { const app = "accessToken" in auth const token = app ? await auth.accessToken(run.refresh).catch(unsent) : undefined const path = `/rest/${run.api === 3 ? "api/" : ""}` - // Not AbortSignal.timeout: its timer holds the run's async context until the deadline (tg-cli, 2026-11-05). - const deadline = new AbortController() - const timer = setTimeout( - () => deadline.abort(new DOMException(`${method} timed out`, "TimeoutError")), - this.#timeoutMs, - ) - timer.unref() + const deadline = timeLimit(this.#timeoutMs) const end = providerRequest(method, this.#events ?? providerEvent, { method: "POST", endpointTemplate: `${path}${app ? "" : ":user/:token/"}${method}`, @@ -397,7 +393,7 @@ export class BitrixTransport { ...(typeof answer.next === "number" ? { next: answer.next } : {}), } } finally { - clearTimeout(timer) + deadline.clear() } } diff --git a/src/commands/session-app.test.ts b/src/commands/session-app.test.ts index 1aa3760..525aa05 100644 --- a/src/commands/session-app.test.ts +++ b/src/commands/session-app.test.ts @@ -157,7 +157,12 @@ describe("a profile signed in with a local application", () => { const status = await b24(["work", "session", "status", "--json"], { keyring }) expect(status.json().item).toMatchObject({ kind: "app", token_from: "keyring", portal: "example.bitrix24.test" }) const listed = await b24(["session", "list", "--json"], { keyring }) - expect(listed.json().items).toContainEqual({ profile: "work", signed_in: true, token_from: "keyring" }) + expect(listed.json().items).toContainEqual({ + profile: "work", + signed_in: true, + state: "saved", + token_from: "keyring", + }) expect((await b24(["work", "session", "end", "--json"], { keyring })).code).toBe(0) expect(keyring.get(...ENTRY)).toBeNull() const ended = await b24(["work", "session", "status", "--json"], { keyring }) diff --git a/src/commands/session.ts b/src/commands/session.ts index ac5696d..b1e3ea2 100644 --- a/src/commands/session.ts +++ b/src/commands/session.ts @@ -25,6 +25,8 @@ import { rootOf, secretInputOf, signInSteps, + storageOf, + type TokenStorage, } from "@wirecat/cli-core/cli" import { refuseTakenCrmAccount } from "@wirecat/cli-core/crm/cli" import { Command } from "commander" @@ -86,7 +88,7 @@ const answer = (command: Command, item: unknown) => renderAnswer(command, output const PASTE = "If the browser did not come back here, paste the address it ended on: " /** The local application's sign-in; the webhook's address and the application's key are never a flag (WC-SECR-5). */ -const startWithApp = async (command: Command, args: StartArgs, storage: "keyring" | "file") => { +const startWithApp = async (command: Command, args: StartArgs, storage: TokenStorage) => { const { env, profile, tokens, keyring, run, environment } = contextOf(command) const host = portalHost(args.portal ?? "") const clientId = (args.clientId ?? env[`${IDENTITY.envPrefix}_BITRIX24_CLIENT_ID`] ?? "").trim() @@ -163,7 +165,7 @@ const startWithApp = async (command: Command, args: StartArgs, storage: "keyring } interface StartArgs { - credentialStorage: string + credentialStorage: TokenStorage portal?: string clientId?: string } @@ -193,7 +195,8 @@ const startCommand = () => "--credential-storage ", "where the sign-in is kept: keyring (the default, which falls back to a private file by itself when no " + "keyring works), or file, not recommended", - "keyring", + storageOf, + "keyring" as TokenStorage, ) .option("--portal ", "sign in through a local application on this portal, such as example.bitrix24.ru") .option( @@ -203,8 +206,6 @@ const startCommand = () => ) .action(async function (this: Command, args: StartArgs) { const storage = args.credentialStorage - if (storage !== "keyring" && storage !== "file") - throw new CliError("validation_error", "--credential-storage takes keyring or file") const { env, profile, tokens, keyring } = contextOf(this) refuseSignInHere(this) if (args.portal !== undefined) return answer(this, await startWithApp(this, args, storage)) @@ -301,10 +302,10 @@ const listCommand = () => const items = profilesHere(IDENTITY, configuredProfiles, env).map((profile) => { try { const from = store.read(profile)?.source ?? appSourceOf(env, profile, environment.keyring) - return { profile, signed_in: from !== undefined, token_from: from ?? null } + return { profile, signed_in: from !== undefined, state: from ? "saved" : "missing", token_from: from ?? null } } catch (error) { if (!(error instanceof CliError)) throw error - return { profile, signed_in: null, token_from: null, error: error.message } + return { profile, signed_in: null, state: "unknown", token_from: null, error: error.message } } }) renderAnswer(