/**
* NTA Scraper — 国税庁サイトの HTML 取得層
*
* 責務:
* - `fetch` で国税庁サイトから HTML を取得
* - `iconv-lite` で Shift_JIS / UTF-8 を自動判定してデコード
* - User-Agent / Accept-Language を付与(DATA-SOURCES.md のマナー準拠)
* - 5xx / ネットワークエラーを指数バックオフで retry
* - 4xx は retry しない(永続エラー)
*
* 責務外:
* - HTML パース(章-項-号 の抽出など)→ parser 層(`tsutatsu-parser.ts` 等)
* - キャッシュ → `utils/cache.ts`(Phase 1 後半で導入予定)
*
* 上位レイヤは `cheerio.load(result.html)` を自分で呼んで利用する。
*/
/** スクレイピング結果 */
export interface NtaFetchResult {
/** デコード済み HTML 文字列。304 Not Modified 時は空文字 */
html: string;
/** リクエストした URL(一次情報源として citation に使う) */
sourceUrl: string;
/** 取得時刻 ISO 8601 */
fetchedAt: string;
/** デコードに使われた charset(小文字、'shift_jis' / 'utf-8' など)。304 時は空文字 */
charset: string;
/** HTTP ステータス。304 のときは 304、変更ありなら 200 */
status: number;
/**
* Phase 6-2 (v0.9.0): サーバが返した HTTP `Last-Modified` ヘッダ。
* RFC 7232 形式 (`Thu, 18 Jan 2024 09:30:37 GMT`)。
* 304 応答時は通常は不在 (キャッシュ側に既存値があるため)。
*/
lastModified?: string;
/** Phase 6-2 (v0.9.0): サーバが返した HTTP `ETag` ヘッダ */
etag?: string;
/**
* Phase 6-2 (v0.9.0): 304 Not Modified を受け取って parse をスキップした場合 `true`。
* 呼び出し側はこれを見て「DB の content_hash と fetched_at だけ更新」の経路に分岐する。
*/
notModified?: boolean;
}
/**
* 国税庁サイトへの要求がどう終わったか(v0.24.0、SPEC-NTA-COMMON-ERRORS-018・019)。
*
* - `http`: HTTP の応答を受け取った(`status` に HTTP ステータス。404 ページへの転送は 404)
* - `timeout`: このサーバーが応答を待ちきれずに打ち切った(`FETCH_CONFIG.timeoutMs`)
* - `unreachable`: 接続できなかった(例外の `cause.code` が `UNREACHABLE_CAUSE_CODES` のどれか)
* - `network`: そのほかのネットワークの失敗
*/
export type NtaFetchFailureKind = 'http' | 'timeout' | 'unreachable' | 'network';
/** SPEC-NTA-COMMON-ERRORS-019: 「接続できない」とみなす例外の `cause.code`(houki-egov-mcp の 028 と同じ表) */
export declare const UNREACHABLE_CAUSE_CODES: ReadonlySet;
/** スクレイピング失敗時の例外。url / status / cause と、失敗の種類(kind)を保持 */
export declare class NtaFetchError extends Error {
readonly url: string;
readonly status?: number;
readonly cause?: unknown;
/** 失敗の種類。指定しなければ、status があれば `http`、無ければ `network` */
readonly kind: NtaFetchFailureKind;
/** `kind` が `unreachable` のときの `cause.code`(例: `ENOTFOUND`) */
readonly causeCode?: string;
constructor(message: string, url: string, status?: number, cause?: unknown, failure?: {
kind?: NtaFetchFailureKind;
causeCode?: string;
});
}
/** `fetchNtaPage` のオプション */
export interface FetchNtaPageOptions {
/** タイムアウト (ms)。未指定なら `FETCH_CONFIG.timeoutMs` */
timeoutMs?: number;
/** 最大リトライ回数。未指定なら `FETCH_CONFIG.maxRetries` */
maxRetries?: number;
/**
* 指数バックオフの基準値 (ms)。未指定なら `FETCH_CONFIG.retryBaseMs`。
* テストで小さな値を入れて実行時間を縮められる。
*/
retryBaseMs?: number;
/** charset を強制指定(デバッグ用)。指定すれば auto-detect は skip */
forceCharset?: string;
/**
* `fetch` の差し替え(テスト用)。デフォルトは `globalThis.fetch`。
* シグネチャは標準 fetch と同じ。
*/
fetchImpl?: typeof fetch;
/**
* Phase 6-2 (v0.9.0): `If-Modified-Since` ヘッダに渡す値。
* RFC 7232 形式 (`Thu, 18 Jan 2024 09:30:37 GMT`) を期待。
* 指定するとサーバ側が同一 Last-Modified なら **304 Not Modified** を返し、
* `result.notModified === true` + `result.html === ''` で帰る。
*/
ifModifiedSince?: string;
/**
* Phase 6-2 (v0.9.0): `If-None-Match` ヘッダに渡す ETag 値。
* `ifModifiedSince` と併用可能 (両方を送ってサーバが任意の判定をする)。
*/
ifNoneMatch?: string;
}
/**
* 国税庁サイトから HTML を取得してデコードする。
*
* - 4xx 応答は retry せず即時 `NtaFetchError` を投げる
* - 5xx / ネットワークエラーは指数バックオフで `maxRetries` 回まで retry
*
* @param url 取得対象 URL(例: 消費税法基本通達のあるページ)
* @param options
* @returns デコード済み HTML + メタ
* @throws {NtaFetchError} 取得失敗時
*/
export declare function fetchNtaPage(url: string, options?: FetchNtaPageOptions): Promise;
/**
* Content-Type ヘッダ → HTML meta タグ → デフォルト Shift_JIS の順で charset を決定する。
*
* 国税庁サイトはページによって Shift_JIS / UTF-8 が混在するため auto-detect が必要。
*/
export declare function detectCharset(contentTypeHeader: string | null, htmlBytes: Buffer): string;
/**
* iconv-lite が認識する正規化された charset 名に揃える。
*/
export declare function normalizeCharset(charset: string): string;
/**
* 国税庁サイトの soft-404 判定 (Phase 5 Resilience Lv-3a)。
*
* 国税庁サイトは存在しない URL に対して
* 1. HTTP 302 → `https://www.nta.go.jp/error/404.htm` にリダイレクト
* 2. リダイレクト先は HTTP 200 を返す
* という構造で、parser から見ると「2007 bytes の謎の HTML」が降ってきて
* 「本体セレクタが見つかりません」という曖昧な失敗になる。
*
* `fetch` で `redirect: 'follow'` した結果として `res.url` が
* `/error/404.htm` で終わる、かつ要求 URL と異なる場合に soft-404 と判定する。
*
* 2026-05-11 Canary 失敗 (sozoku/, hojin/01/03.htm) で実際に踏んだケース。
*
* @param finalUrl `res.url` (redirect 追従後の最終 URL)
* @param requestedUrl 元の要求 URL
*/
export declare function isNtaSoft404(finalUrl: string, requestedUrl: string): boolean;
//# sourceMappingURL=nta-scraper.d.ts.map