Pixiv Client Kit (@redtidev/pixiv-client)
English | 中文
The Pixiv protocol/network code is an independent internal npm workspace package at packages/pixiv-client. PixivFlow is a consumer of the kit; the kit knows nothing about PixivFlow.
The kit is not a separate GitHub repository yet — see When to split out.
Dependency rule
PixivFlow (product) ──▶ @redtidev/pixiv-client (kit) ──▶ Pixiv HTTP API
│ │
│ └ no TargetConfig, StandaloneConfig, SQLite,
│ puppeteer, scheduler, logger impl, delivery
│
└ host adapters: PixivFlowPixivClient, TargetSearchRunner,
PixivAuthTokenProvider, SQLiteRateLimitStateStoreOne-directional only. src/__tests__/independence.test.ts in the package fails the build if the kit references host symbols (TargetConfig, StandaloneConfig, better-sqlite3, puppeteer, …) or imports anything outside its own src.
What the kit provides
- Transport — one HTTP stack (
globalThis.fetch+ undiciProxyAgentfor HTTP/HTTPS proxies; axios +socks-proxy-agentonly for SOCKS, which undici does not support). Per-request timeout viaAbortController, callerAbortSignalpropagation, transient HTTP retry, 401 single refresh, binary media fetch returningArrayBuffer(never writes files). - App-API services —
illustrations,novels,tags,users,mediawith typed option DTOs (IllustSearchOptions,RankingOptions, …). Protocol quirks preserved: v2→v1 novel detail fallback, novel text 3-tier fallback (/webview/v2/novelmarker parse →/v1/novel/text→ajax/novel/{id}with browser UA, no auth/App headers). - Typed errors —
PixivErrorhierarchy (PixivHttpError,PixivRateLimitError,PixivNotFoundError,PixivTimeoutError,PixivCircuitOpenError, …) carryingstatus,retryAfterMs,endpoint. No moremessage.includes('429'). - Pagination —
paginate(fetchPage, opts)/firstPage; bounded walks, abortable. - Coalescing — identical in-flight GETs merge into one request.
- Events —
onEventemits retry/rate-limit/circuit transitions; no token or URL-with-secret data.
Public surface
Only package.json exports (. → dist) is public. Everything else is internal. Entry points: createPixivClient(options), PixivClient, RateLimitGate, error classes, model types, option DTOs, the AccessTokenProvider port, RateLimitStateStore port, paginate.
The 429 design (single global gate)
There is exactly one gate per PixivClient; every request acquires a slot first.
- Real slot reservation, not bursty sleeps. Reservations are serialized through a promise-chain mutex; concurrent callers receive strictly increasing deadlines (A→0ms, B→1000ms, C→2000ms…), never a shared burst.
- Conservative defaults.
minIntervalMs: 1000+ 25% jitter; first 429 cooldown 60s; exponential 60→120→240→480, capped at 15min. ARetry-Afterheader is honored as a floor (max(retryAfter, ladder)), never as permission to shorten the cooldown. - Penalty decay. One success does not reset the penalty level; a configurable run of consecutive successes (
decaySuccesses, default 20) steps it down. State survives across requests viaRateLimitState. - Circuit breaker. CLOSED → OPEN after repeated 429s (
openThreshold), which fails fast withPixivCircuitOpenError; after the cooldown a single HALF_OPEN probe is allowed; success closes, failure reopens. - Persistence port.
RateLimitStateStore(load/save by scope); in-memory default. PixivFlow providesSQLiteRateLimitStateStore(rate_limit_statetable), so Fly suspend/resume and restarts do not forget an active Pixiv cooldown. - Observability.
client.getRateLimitStatus()returns cooldown remaining, penalty level, circuit state, last 429.doctorandhealthsurface it. - Retry ownership (no nested retries). The transport owns transient HTTP retries (connection reset, 5xx, one 429 round while the gate is healthy). The gate owns the 429 cooldown/backoff. The scheduler/download pipeline owns durable task-level retry across runs. No layer retries the same failure three times.
Host-side compatibility
PixivFlowPixivClient implements IPixivClient— the existing product interface, delegating to the kit. Product behavior (TargetConfig mapping, tag-OR merging, inter-pagerequestDelay, date-aware early-stop pagination, over-fetch, final sorting) lives in the host'sTargetSearchRunner/query-mapper, because it is PixivFlow behavior, not Pixiv protocol.PixivAuthTokenProvideradapts the existing OAuth/refresh-tokenPixivAuth(SQLite-cached tokens, PKCE/python login unchanged) to the kit's 3-method auth port.createPixivFlowClient(auth, config, database)is the single construction path used by all commands, the scheduler runtime and the WebUI.- Legacy error consumers still work:
NetworkError/is404Error/recovery and concurrency code accept kit typed errors via helpers insrc/utils/errors.ts(isRetryableNetworkError,rateLimitWaitMs). - Config stays backward compatible:
network.timeoutMs/retries/proxymap straight through. New opt-innetwork.requestPacingMsoverrides the kit's 1000ms default (0 disables pacing only; it cannot disable the 429 cooldown). The safe default replaces the old 500ms pacing.
Files
Host (src/):
| File | Role |
|---|---|
pixiv-client/PixivFlowPixivClient.ts | IPixivClient adapter over the kit |
pixiv-client/TargetSearchRunner.ts | host date/tag/pagination semantics |
pixiv-client/query-mapper.ts | TargetConfig → kit option DTOs |
pixiv-client/PixivAuthTokenProvider.ts | auth port adapter |
pixiv-client/createPixivFlowClient.ts | single factory (config, logger, SQLite store, scope) |
auth/PixivAuth.ts | OAuth/refresh token (moved from pixiv/AuthClient.ts) |
storage/repositories/RateLimitStateRepository.ts | SQLite gate-state adapter |
pixiv/PixivClient.ts, pixiv/AuthClient.ts | deprecated re-export shims |
Deleted: pixiv/PixivApiCore.ts, pixiv/RateLimitCoordinator.ts, pixiv/client/* (Illust/Novel/Media/Search services), pixiv/types.ts, pixiv/IPixivRequestHandler.ts and their 2,400+ lines of old tests. Exactly one Pixiv HTTP stack remains.
Dependency change: added undici (kit); removed unused https-proxy-agent.
The published artifact stays self-contained
pixivflow is consumed straight from npm by the Fly/Docker deployments, so the kit is bundled into the published tarball rather than resolved from the registry: the root package.json lists it in bundleDependencies, and npm ships the workspace package at node_modules/@redtidev/pixiv-client inside the tarball (its runtime deps — axios, socks-proxy-agent, undici — are already direct dependencies of pixivflow). Publishing the kit separately is only needed once an external consumer wants it directly; do NOT turn the dependency into a plain registry range before that, or every npm install pixivflow fails with a 404.
When to split out into its own repository
Not yet. The honest gating conditions are:
- A second real consumer. Today PixivFlow is the only consumer; the adapter/host boundary has only been proven one way. A second independent app (CLI, bot, another service) is what validates that the public surface is generally shaped.
- Stable public API.
createPixivClient, service methods, option DTOs, error names andgetRateLimitStatus()need a period without breaking change, then a 1.0 semver line. - Independent CI/release. Own lint/test/typecheck/publish pipeline (currently shares the workspace), versioned changelog, published tarball.
- Protocol coverage confidence. The Web-API boundary is intentionally a placeholder; confirm App-API-only is acceptable for external consumers or add Web endpoints first.
When all four hold, extracting is mechanical: move packages/pixiv-client to its own repo, publish as @redtidev/pixiv-client, switch the workspace dependency to the released version. The architecture test and exports surface already enforce the boundary that makes that move safe.