@aiquants/daily-report 0.20.0 → 0.21.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.
- package/README.md +229 -8
- package/dist/client.d.mts +201 -739
- package/dist/client.d.ts +201 -739
- package/dist/client.js +5 -6
- package/dist/client.mjs +5 -6
- package/dist/index.d.mts +5 -197
- package/dist/index.d.ts +5 -197
- package/dist/index.js +1 -2
- package/dist/index.mjs +1 -2
- package/dist/logger-D3krZrNK.d.mts +0 -14
- package/dist/logger-D3krZrNK.d.ts +0 -14
- package/dist/server.d.mts +114 -744
- package/dist/server.d.ts +114 -744
- package/dist/server.js +1 -2
- package/dist/server.mjs +1 -2
- package/dist/{sse-schema-ChSFm-4q.d.mts → sse-schema-DCxpdETM.d.mts} +1106 -1090
- package/dist/{sse-schema-BdUGThiz.d.ts → sse-schema-DoQw8_om.d.ts} +1106 -1090
- package/dist/types-CqgYQgbH.d.mts +0 -34
- package/dist/types-CqgYQgbH.d.ts +0 -34
- package/package.json +9 -10
- package/dist/client.js.map +0 -1
- package/dist/client.mjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/server.js.map +0 -1
- package/dist/server.mjs.map +0 -1
- package/src/client/components/business-day-thumb-overlay.tsx +0 -19
- package/src/client/components/daily-report-attachment-indicator.spec.tsx +0 -51
- package/src/client/components/daily-report-attachment-indicator.tsx +0 -45
- package/src/client/components/daily-report-attachment-list.spec.tsx +0 -94
- package/src/client/components/daily-report-attachment-list.tsx +0 -112
- package/src/client/components/daily-report-comment-item.tsx +0 -89
- package/src/client/components/daily-report-comment-section.tsx +0 -166
- package/src/client/components/daily-report-detail-list.tsx +0 -842
- package/src/client/components/daily-report-edit-form.tsx +0 -87
- package/src/client/components/daily-report-ids-stream-status.spec.tsx +0 -93
- package/src/client/components/daily-report-ids-stream-status.tsx +0 -67
- package/src/client/components/daily-report-list.tsx +0 -1232
- package/src/client/components/daily-report-page.spec.tsx +0 -119
- package/src/client/components/daily-report-page.tsx +0 -140
- package/src/client/components/daily-report-resize-panels.spec.tsx +0 -307
- package/src/client/components/daily-report-resolved-content.tsx +0 -207
- package/src/client/components/daily-report-scroll-restoration.spec.tsx +0 -422
- package/src/client/components/report-views.spec.tsx +0 -375
- package/src/client/components/unread-indicator.tsx +0 -13
- package/src/client/config-context.tsx +0 -184
- package/src/client/contexts/daily-report-action-context.spec.tsx +0 -433
- package/src/client/contexts/daily-report-action-context.tsx +0 -1147
- package/src/client/contexts/daily-report-error-context.spec.tsx +0 -66
- package/src/client/contexts/daily-report-error-context.tsx +0 -98
- package/src/client/hooks/use-animated-number.spec.ts +0 -103
- package/src/client/hooks/use-animated-number.ts +0 -69
- package/src/client/hooks/use-daily-report-comments.ts +0 -73
- package/src/client/hooks/use-daily-report-sse-connection.ts +0 -86
- package/src/client/hooks/use-daily-report.spec.ts +0 -546
- package/src/client/hooks/use-daily-report.ts +0 -549
- package/src/client/hooks/use-dynamic-viewport-height.ts +0 -127
- package/src/client/hooks/use-responsive-layout.spec.ts +0 -74
- package/src/client/hooks/use-responsive-layout.ts +0 -63
- package/src/client/route-helpers.spec.ts +0 -100
- package/src/client/route-helpers.ts +0 -61
- package/src/client/streaming/daily-report-ids-stream-client.spec.ts +0 -910
- package/src/client/streaming/daily-report-ids-stream-client.ts +0 -730
- package/src/client/streaming/daily-report-ids-stream-session.spec.ts +0 -401
- package/src/client/streaming/daily-report-ids-stream-session.ts +0 -319
- package/src/client/ui/button.tsx +0 -42
- package/src/client/ui/cn.ts +0 -14
- package/src/client/ui/input.tsx +0 -21
- package/src/client/ui/label.tsx +0 -16
- package/src/client/ui/switch.tsx +0 -19
- package/src/client/ui/tabs.tsx +0 -40
- package/src/client/ui/textarea.tsx +0 -19
- package/src/client/utils/constants.spec.ts +0 -96
- package/src/client/utils/constants.ts +0 -84
- package/src/client.ts +0 -28
- package/src/index.ts +0 -11
- package/src/lucide-icons.d.ts +0 -6
- package/src/server/authz.spec.ts +0 -57
- package/src/server/authz.ts +0 -99
- package/src/server/cache.spec.ts +0 -556
- package/src/server/cache.ts +0 -303
- package/src/server/etag.ts +0 -18
- package/src/server/external-source.ts +0 -60
- package/src/server/handlers.action.spec.ts +0 -44
- package/src/server/handlers.attachment.spec.ts +0 -470
- package/src/server/handlers.ids-stream.spec.ts +0 -618
- package/src/server/handlers.ts +0 -1638
- package/src/server/handlers.unauthorized-cookie.spec.ts +0 -187
- package/src/server/handlers.visibility.spec.ts +0 -774
- package/src/server/ports.ts +0 -222
- package/src/server/response.ts +0 -37
- package/src/server/schema.spec.ts +0 -30
- package/src/server/schema.ts +0 -344
- package/src/server/service.spec.ts +0 -1130
- package/src/server/service.ts +0 -2045
- package/src/server/sse-reader.spec.ts +0 -302
- package/src/server/sse-reader.ts +0 -267
- package/src/server/test-helpers/handlers-config.ts +0 -321
- package/src/server/transaction-retry.spec.ts +0 -63
- package/src/server/visibility.spec.ts +0 -403
- package/src/server/visibility.ts +0 -373
- package/src/server.spec.ts +0 -160
- package/src/server.ts +0 -131
- package/src/shared/business-date.spec.ts +0 -82
- package/src/shared/business-date.ts +0 -104
- package/src/shared/comment-adapter.spec.ts +0 -38
- package/src/shared/comment-adapter.ts +0 -80
- package/src/shared/ids-stream.spec.ts +0 -154
- package/src/shared/ids-stream.ts +0 -279
- package/src/shared/logger.ts +0 -47
- package/src/shared/sse-schema.spec.ts +0 -50
- package/src/shared/sse-schema.ts +0 -198
- package/src/shared/text-utils.spec.ts +0 -42
- package/src/shared/text-utils.ts +0 -65
- package/src/shared/types.ts +0 -109
- package/src/styles/standalone.entry.css +0 -51
package/src/server/cache.ts
DELETED
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* In-memory SQL result cache with TTL, snapshot, and cross-worker epoch (via redis) support.
|
|
3
|
-
* TTL・スナップショット・クロスワーカー epoch (redis 経由) を備えた SQL 結果のインメモリキャッシュ。
|
|
4
|
-
*
|
|
5
|
-
* epoch は任意注入の redis ポート経由で取得・更新する (未注入時は常に 0 = epoch 無効)。
|
|
6
|
-
*/
|
|
7
|
-
import type { DailyReportRedisProvider } from "./ports"
|
|
8
|
-
|
|
9
|
-
export type SqlResultCacheQueryOptions = {
|
|
10
|
-
forceRefresh?: boolean
|
|
11
|
-
snapshot?: boolean
|
|
12
|
-
ttlMsOverride?: number
|
|
13
|
-
epochKey?: string
|
|
14
|
-
/**
|
|
15
|
-
* ヒット時にバケット年齢が ttl × ratio を超えていたら、キャッシュ値を即返しつつ
|
|
16
|
-
* 裏で 1 本だけ再フェッチして置き換える (未指定は従来動作 = 何もしない)。
|
|
17
|
-
* 有効域は 0 <= ratio < 1: ヒット判定が「未失効」を要求するため年齢は常に ttl 未満であり、
|
|
18
|
-
* ratio 1 は事実上発火しない。ratio 0 は毎ヒット裏更新 (undefined とは区別される)。
|
|
19
|
-
* 定期的にアクセスされるキーの「TTL 切れ直後のコールド遭遇」を対話リクエストから排除する。
|
|
20
|
-
*/
|
|
21
|
-
refreshAheadRatio?: number
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Creates epoch helpers bound to an optional redis provider.
|
|
26
|
-
* 任意の redis プロバイダーに束縛された epoch ヘルパーを生成する処理。
|
|
27
|
-
*/
|
|
28
|
-
export const createEpochStore = (redis?: DailyReportRedisProvider) => ({
|
|
29
|
-
/**
|
|
30
|
-
* Gets the current epoch value, or null when it cannot be determined.
|
|
31
|
-
* 現在のエポック値を取得する処理。判定不能なら null。
|
|
32
|
-
*
|
|
33
|
-
* - `null` = redis 未注入、または一時的な失敗。**エポックによる横断無効化は判定不能**。
|
|
34
|
-
* - `0` = redis に到達したがキーが無い (一度も increment されていない正当な初期値)。
|
|
35
|
-
*
|
|
36
|
-
* ❗ `0` を「不明」の代用にしてはならない。記録側も `0` のとき両辺が一致してしまい、
|
|
37
|
-
* stale 検出が黙って無効化される (無効化されたこと自体がどこにも現れない)。
|
|
38
|
-
* 「常に 0 = safe 方向」が成り立つのは一時的失敗のときだけで、未注入時には偽である。
|
|
39
|
-
*/
|
|
40
|
-
async getEpoch(epochKey: string): Promise<number | null> {
|
|
41
|
-
try {
|
|
42
|
-
const client = await redis?.getClient()
|
|
43
|
-
if (!client) return null
|
|
44
|
-
const val = await client.get(epochKey)
|
|
45
|
-
return val ? Number(val) : 0
|
|
46
|
-
} catch {
|
|
47
|
-
return null
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
/**
|
|
51
|
-
* Increments the epoch counter in Redis.
|
|
52
|
-
* Redis のエポックカウンターをインクリメントする。
|
|
53
|
-
*/
|
|
54
|
-
async incrementEpoch(epochKey: string): Promise<void> {
|
|
55
|
-
try {
|
|
56
|
-
const client = await redis?.getClient()
|
|
57
|
-
if (!client) return
|
|
58
|
-
await client.incr(epochKey)
|
|
59
|
-
} catch {
|
|
60
|
-
// Redis エラー時は epoch 更新をスキップ (次回 GET は null = 判定不能 → TTL のみで判定)
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
export type EpochStore = ReturnType<typeof createEpochStore>
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* In-memory cache for SQL query results with TTL and snapshot support.
|
|
69
|
-
* TTLとスナップショット機能を備えたSQLクエリ結果のインメモリキャッシュ。
|
|
70
|
-
*/
|
|
71
|
-
export class SqlResultCache {
|
|
72
|
-
// epoch は「バケット書き込み時点のエポック」。null = 当時エポックが判定不能だった
|
|
73
|
-
private readonly buckets = new Map<string, { records: readonly unknown[]; expireAt: number; epoch: number | null }>()
|
|
74
|
-
private readonly inFlight = new Map<string, Promise<readonly unknown[]>>()
|
|
75
|
-
|
|
76
|
-
constructor(
|
|
77
|
-
private readonly config: { defaultTtlMs: number },
|
|
78
|
-
private readonly epochStore: EpochStore,
|
|
79
|
-
) {}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Invalidates a specific cache bucket.
|
|
83
|
-
* 指定されたキャッシュバケットを無効化します。
|
|
84
|
-
*/
|
|
85
|
-
invalidate = (cacheKey: string): void => {
|
|
86
|
-
this.buckets.delete(cacheKey)
|
|
87
|
-
this.inFlight.delete(cacheKey)
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Kicks a single background refetch when the bucket has aged past ttl * ratio.
|
|
92
|
-
* バケット年齢が ttl × ratio を超えたヒット時に、裏で 1 本だけ再フェッチを起動する処理。
|
|
93
|
-
*
|
|
94
|
-
* 応答は誰も待たない。完了までは旧バケットがそのまま生きているため、並行するヒットは
|
|
95
|
-
* 従来どおりキャッシュから即答する (バケットを先に消す forceRefresh 委譲はスタンピードに
|
|
96
|
-
* なるため使わない)。バケットが有効な間は失敗しても旧バケットが残るだけ (safe 方向)。
|
|
97
|
-
* 裏フェッチ中に TTL が完全に切れて in-flight へ相乗りした前面リクエストには、通常の
|
|
98
|
-
* in-flight 共有と同じく失敗が伝播する (相乗りしなくても自前フェッチが同様に失敗する状況)。
|
|
99
|
-
* in-flight が既に居れば起動しない (前面リクエストの結果で足りる)。
|
|
100
|
-
*/
|
|
101
|
-
private maybeRefreshAhead = <T>(opts: { cacheKey: string; fetcher: () => Promise<readonly T[]> } & SqlResultCacheQueryOptions): void => {
|
|
102
|
-
const ratio = opts.refreshAheadRatio
|
|
103
|
-
if (ratio === undefined) {
|
|
104
|
-
return
|
|
105
|
-
}
|
|
106
|
-
const { cacheKey } = opts
|
|
107
|
-
// 年齢は呼び出し元のスナップショットではなく生きているバケットで測る。epoch 照合の
|
|
108
|
-
// Redis await 中に進行中の裏フェッチが完了していると、古い expireAt のまま余分な
|
|
109
|
-
// 2 本目を起動してしまう (置換直後のバケットは若く、再フェッチ不要)
|
|
110
|
-
const bucket = this.buckets.get(cacheKey)
|
|
111
|
-
if (!bucket) {
|
|
112
|
-
return
|
|
113
|
-
}
|
|
114
|
-
const ttl = opts.ttlMsOverride ?? this.config.defaultTtlMs
|
|
115
|
-
const age = ttl - (bucket.expireAt - Date.now())
|
|
116
|
-
if (age < ttl * ratio) {
|
|
117
|
-
return
|
|
118
|
-
}
|
|
119
|
-
if (this.inFlight.has(cacheKey)) {
|
|
120
|
-
return
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// 旧バケットを残したまま裏で取得し、完了時に差し替える。無効化レースの規約は
|
|
124
|
-
// 前面経路と同一: 進行中に invalidate されたら (inFlight の同一性が崩れたら) 書かない
|
|
125
|
-
let promise!: Promise<readonly T[]>
|
|
126
|
-
promise = (async () => {
|
|
127
|
-
// ❗ epoch はフェッチ「開始前」に採取する。完了後に採取すると、フェッチ中に他ワーカーが
|
|
128
|
-
// 更新 (incrementEpoch) した後の値で旧データに新 epoch を刻んでしまい、epoch 照合
|
|
129
|
-
// (staleness の主防御線) が丸ごと無効化される。開始前採取なら不一致 → 再フェッチで自己回復
|
|
130
|
-
// epochKey 未指定は「エポック無し」— 0 (正当な初期値) ではなく null で表す
|
|
131
|
-
const epoch = opts.epochKey ? await this.epochStore.getEpoch(opts.epochKey) : null
|
|
132
|
-
const records = await opts.fetcher()
|
|
133
|
-
// ガード判定と書き込みの間に await を挟まない (この間に落ちた invalidate の取りこぼし防止)
|
|
134
|
-
if (this.inFlight.get(cacheKey) === promise) {
|
|
135
|
-
this.buckets.set(cacheKey, { records, expireAt: Date.now() + ttl, epoch })
|
|
136
|
-
}
|
|
137
|
-
return records
|
|
138
|
-
})()
|
|
139
|
-
// 登録することで、裏フェッチ中に TTL 切れした前面リクエストは相乗りできる
|
|
140
|
-
this.inFlight.set(cacheKey, promise as Promise<readonly unknown[]>)
|
|
141
|
-
promise
|
|
142
|
-
.catch(() => {})
|
|
143
|
-
.finally(() => {
|
|
144
|
-
// 無効化で消されたり別の登録に置き換わっていたら触らない
|
|
145
|
-
if (this.inFlight.get(cacheKey) === promise) {
|
|
146
|
-
this.inFlight.delete(cacheKey)
|
|
147
|
-
}
|
|
148
|
-
})
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Retrieves records from cache or fetches them if expired/missing.
|
|
153
|
-
* キャッシュからレコードを取得するか、期限切れや未存在の場合はフェッチします。
|
|
154
|
-
*/
|
|
155
|
-
getOrFetch = async <T>(
|
|
156
|
-
opts: {
|
|
157
|
-
cacheKey: string
|
|
158
|
-
fetcher: () => Promise<readonly T[]>
|
|
159
|
-
/**
|
|
160
|
-
* 本呼び出しが実際にフェッチャーを起動した瞬間に 1 度だけ呼ばれる。
|
|
161
|
-
*
|
|
162
|
-
* 「返す配列がこの呼び出し自身の新規フェッチ結果である」ことの**因果的な証明**として使う。
|
|
163
|
-
* バケットヒット・進行中フェッチへの相乗り・refresh-ahead の裏側更新では**呼ばれない** —
|
|
164
|
-
* それらの経路が返す配列は本呼び出しより古くなりうるためである。
|
|
165
|
-
*
|
|
166
|
-
* ids ストリームはこの合図で高速先頭ページのクエリを起動する。合図の後に始まる以上、
|
|
167
|
-
* 先頭ページは全量配列より古くなり得ず、「先頭ページに在って全量に無い行 = 削除済み」
|
|
168
|
-
* という判定が初めて健全になる (逆順を許すと作成直後の日報を削除してしまう)。
|
|
169
|
-
*/
|
|
170
|
-
onFetchStart?: () => void
|
|
171
|
-
} & SqlResultCacheQueryOptions,
|
|
172
|
-
): Promise<readonly T[]> => {
|
|
173
|
-
const { cacheKey, forceRefresh, snapshot } = opts
|
|
174
|
-
const bucket = this.buckets.get(cacheKey)
|
|
175
|
-
|
|
176
|
-
// 強制リフレッシュ時はキャッシュと進行中のリクエストをクリア
|
|
177
|
-
if (forceRefresh) {
|
|
178
|
-
this.buckets.delete(cacheKey)
|
|
179
|
-
this.inFlight.delete(cacheKey)
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// 有効なキャッシュがあれば返却 (epoch check 込み)
|
|
183
|
-
if (!forceRefresh && bucket && bucket.expireAt > Date.now()) {
|
|
184
|
-
if (opts.epochKey) {
|
|
185
|
-
// epochKey 指定時は Redis の epoch と比較して stale を検出する。三値の行列:
|
|
186
|
-
// 現在 null (未注入/一時失敗) → TTL のみで受理 (エポックは鮮度の加速器であって
|
|
187
|
-
// 認可境界ではない。障害中に毎リクエスト再フェッチで DB を叩き続けない)
|
|
188
|
-
// バケット null + 現在 数値 → stale (信号復帰後、バケットの世代は証明できない。
|
|
189
|
-
// null === 数値 が偽なので下の等値比較が自然にこの行を与える)
|
|
190
|
-
// 両方 数値 → 等値比較 (従来どおり)
|
|
191
|
-
// ❗ 型を number | null に広げても分岐は強制されない (=== は union をそのまま受ける)。
|
|
192
|
-
// この行列はテストだけが守っている — 特に「バケット数値 + 現在 null で受理」は
|
|
193
|
-
// 明示節が無いと偽になり、redis 一時障害中のチャーンが再発する
|
|
194
|
-
const currentEpoch = await this.epochStore.getEpoch(opts.epochKey)
|
|
195
|
-
if (currentEpoch === null || bucket.epoch === currentEpoch) {
|
|
196
|
-
this.maybeRefreshAhead(opts)
|
|
197
|
-
return snapshot ? (bucket.records.map((r) => structuredClone(r)) as T[]) : (bucket.records as T[])
|
|
198
|
-
}
|
|
199
|
-
// epoch 不一致 → stale bucket を除去して re-fetch へ
|
|
200
|
-
this.buckets.delete(cacheKey)
|
|
201
|
-
} else {
|
|
202
|
-
this.maybeRefreshAhead(opts)
|
|
203
|
-
return snapshot ? (bucket.records.map((r) => structuredClone(r)) as T[]) : (bucket.records as T[])
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// 進行中のリクエストがあれば相乗り
|
|
208
|
-
if (!forceRefresh && this.inFlight.has(cacheKey)) {
|
|
209
|
-
const shared = (await this.inFlight.get(cacheKey)) as readonly T[]
|
|
210
|
-
return snapshot ? shared.map((r) => structuredClone(r)) : shared
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// 新規フェッチとキャッシュ更新
|
|
214
|
-
// 自己参照 (this.inFlight.get(cacheKey) === promise) のため、定義前参照を回避する確定代入アサーション
|
|
215
|
-
let promise!: Promise<readonly T[]>
|
|
216
|
-
promise = (async () => {
|
|
217
|
-
// ❗ epoch はフェッチ「開始前」に採取する (理由は maybeRefreshAhead と同一:
|
|
218
|
-
// フェッチ中の他ワーカー更新を新 epoch で偽装しない。ガードと書き込みも同期隣接に保つ)
|
|
219
|
-
// epochKey 未指定は「エポック無し」— 0 (正当な初期値) ではなく null で表す
|
|
220
|
-
const epoch = opts.epochKey ? await this.epochStore.getEpoch(opts.epochKey) : null
|
|
221
|
-
// ❗ フェッチャー起動の直前に合図する。これより後に始まる処理は、この配列より
|
|
222
|
-
// 新しいスナップショットを見ることが保証される (順序の因果を呼び出し側へ渡す)。
|
|
223
|
-
// refresh-ahead の裏側更新には**置かない** — 返す配列は古いバケットのままなので
|
|
224
|
-
// 合図すると「新しい配列を得た」という嘘になる
|
|
225
|
-
opts.onFetchStart?.()
|
|
226
|
-
const records = await opts.fetcher()
|
|
227
|
-
// invalidation 中に完了したリクエストはキャッシュに書き込まない(レース防止)
|
|
228
|
-
// ただし forceRefresh の場合は常に書き込む
|
|
229
|
-
if (forceRefresh || this.inFlight.get(cacheKey) === promise) {
|
|
230
|
-
const expireAt = Date.now() + (opts.ttlMsOverride ?? this.config.defaultTtlMs)
|
|
231
|
-
this.buckets.set(cacheKey, { records, expireAt, epoch })
|
|
232
|
-
// 蓄積防止のため、サイズが一定値を超えたら期限切れキャッシュを一括クリーンアップ
|
|
233
|
-
if (this.buckets.size > 500) {
|
|
234
|
-
this.cleanExpired()
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
return records
|
|
238
|
-
})()
|
|
239
|
-
|
|
240
|
-
if (!forceRefresh) this.inFlight.set(cacheKey, promise as Promise<readonly unknown[]>)
|
|
241
|
-
|
|
242
|
-
try {
|
|
243
|
-
const result = await promise
|
|
244
|
-
return snapshot ? result.map((r) => structuredClone(r)) : result
|
|
245
|
-
} finally {
|
|
246
|
-
// ❗ identity チェック必須: 無効化が本リクエストの登録を消した後に後続リクエストが
|
|
247
|
-
// 登録し直していた場合、無条件 delete は「後続」の書き込みガードまで壊してしまう
|
|
248
|
-
// (2 本のフェッチが完走したのにバケットが空、という毒殺の温床)
|
|
249
|
-
if (this.inFlight.get(cacheKey) === promise) {
|
|
250
|
-
this.inFlight.delete(cacheKey)
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Clears the cache for a specific key.
|
|
257
|
-
* 指定されたキーのキャッシュをクリアします。
|
|
258
|
-
*/
|
|
259
|
-
flush = (cacheKey: string): void => {
|
|
260
|
-
this.buckets.delete(cacheKey)
|
|
261
|
-
this.inFlight.delete(cacheKey)
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Clears all cached buckets and in-flight requests.
|
|
266
|
-
* すべてのキャッシュバケットと進行中のリクエストを全クリアします。
|
|
267
|
-
*/
|
|
268
|
-
clearAll = (): void => {
|
|
269
|
-
this.buckets.clear()
|
|
270
|
-
this.inFlight.clear()
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Invalidates cache buckets matching a prefix.
|
|
275
|
-
* 指定されたプレフィックスに一致するキャッシュバケットを無効化します。
|
|
276
|
-
*/
|
|
277
|
-
invalidatePrefix = (prefix: string): void => {
|
|
278
|
-
for (const key of this.buckets.keys()) {
|
|
279
|
-
if (key.startsWith(prefix)) {
|
|
280
|
-
this.buckets.delete(key)
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
for (const key of this.inFlight.keys()) {
|
|
284
|
-
if (key.startsWith(prefix)) {
|
|
285
|
-
this.inFlight.delete(key)
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Cleans up all expired cache buckets to prevent memory accumulation.
|
|
292
|
-
* メモリー蓄積を防ぐため、期限切れのキャッシュバケットをすべてクリーンアップする処理。
|
|
293
|
-
*/
|
|
294
|
-
private cleanExpired(): void {
|
|
295
|
-
const now = Date.now()
|
|
296
|
-
// 期限切れのキーを削除
|
|
297
|
-
for (const [key, bucket] of this.buckets.entries()) {
|
|
298
|
-
if (bucket.expireAt <= now) {
|
|
299
|
-
this.buckets.delete(key)
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
package/src/server/etag.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ETag generation helper for JSON payloads.
|
|
3
|
-
* JSON ペイロード向けの ETag 生成ヘルパー。
|
|
4
|
-
*/
|
|
5
|
-
import { createHash } from "node:crypto"
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Generates a SHA-256 ETag for the given data.
|
|
9
|
-
* 指定されたデータの SHA-256 ETag を生成します。
|
|
10
|
-
*
|
|
11
|
-
* @param data The data to hash (will be JSON stringified).
|
|
12
|
-
* @returns The ETag string (wrapped in quotes).
|
|
13
|
-
*/
|
|
14
|
-
export const generateETag = (data: unknown): string => {
|
|
15
|
-
const json = JSON.stringify(data)
|
|
16
|
-
const hash = createHash("sha256").update(json).digest("hex")
|
|
17
|
-
return `"${hash}"`
|
|
18
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External source adapter contract for legacy/foreign daily-report tables.
|
|
3
|
-
* レガシー・外部由来の日報テーブルを取り込む外部ソースアダプタ契約。
|
|
4
|
-
*
|
|
5
|
-
* DailyReportHub.source_type がアダプタの `sourceType` に一致する行は、
|
|
6
|
-
* `table` を `Hub.source_id_num = idColumn` で LEFT JOIN し、その行を
|
|
7
|
-
* `mapRow` で表示フィールドへ変換する (例: 別システムのレガシー日報テーブルの取り込み)。
|
|
8
|
-
*/
|
|
9
|
-
import type { AnyMsSqlColumn } from "drizzle-orm/mssql-core"
|
|
10
|
-
import { createLogger, type DailyReportLogger, LogLevel } from "../shared/logger"
|
|
11
|
-
import type { DailyReportExternalComment, DailyReportInterviewer } from "../shared/types"
|
|
12
|
-
|
|
13
|
-
/** 外部ソース行から詳細表示へ供給するフィールド群。 */
|
|
14
|
-
export type ExternalReportFields = {
|
|
15
|
-
content?: string | null
|
|
16
|
-
employeeName?: string | null
|
|
17
|
-
category?: string | null
|
|
18
|
-
creationCategory?: string | null
|
|
19
|
-
visitTimeFrom?: string | null
|
|
20
|
-
visitTimeTo?: string | null
|
|
21
|
-
customerName?: string | null
|
|
22
|
-
interviewers?: DailyReportInterviewer[]
|
|
23
|
-
externalComments?: DailyReportExternalComment[]
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** 外部ソースアダプタ。 */
|
|
27
|
-
export type DailyReportExternalSource = {
|
|
28
|
-
/** DailyReportHub.source_type の一致値 (例 "legacy")。 */
|
|
29
|
-
sourceType: string
|
|
30
|
-
/** LEFT JOIN する drizzle テーブル。 */
|
|
31
|
-
table: unknown
|
|
32
|
-
/** Hub.source_id_num と突き合わせる ID 列。 */
|
|
33
|
-
idColumn: AnyMsSqlColumn
|
|
34
|
-
/** 結合行を表示フィールドへ変換する処理。 */
|
|
35
|
-
mapRow: (row: Record<string, unknown>) => ExternalReportFields
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const defaultLogger = createLogger(LogLevel.INFO, "[DailyReportExternalSource]")
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Parses JSON array payloads and maps each element (fail-soft: returns [] on error).
|
|
42
|
-
* JSON 配列のペイロードを解析し各要素を変換する処理 (エラー時は空配列)。
|
|
43
|
-
*/
|
|
44
|
-
export const transformJsonArray = <TRaw, TResult>(payload: string | null, label: string, mapper: (raw: TRaw) => TResult | null, logger: DailyReportLogger = defaultLogger): TResult[] => {
|
|
45
|
-
if (!payload) {
|
|
46
|
-
return []
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
try {
|
|
50
|
-
const parsed = JSON.parse(payload)
|
|
51
|
-
if (!Array.isArray(parsed)) {
|
|
52
|
-
logger.warn(`Unexpected ${label} format: not an array`)
|
|
53
|
-
return []
|
|
54
|
-
}
|
|
55
|
-
return (parsed as TRaw[]).map(mapper).filter((entry): entry is TResult => entry !== null)
|
|
56
|
-
} catch (error) {
|
|
57
|
-
logger.warn(`Failed to parse ${label}:`, error)
|
|
58
|
-
return []
|
|
59
|
-
}
|
|
60
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Action-endpoint tests: the dev-only clearCache gate.
|
|
3
|
-
* action エンドポイントのテスト: 開発専用 clearCache の封鎖ゲート。
|
|
4
|
-
*/
|
|
5
|
-
import { describe, expect, it } from "vitest"
|
|
6
|
-
import { createDailyReportHandlers } from "./handlers"
|
|
7
|
-
import { makeHandlersFixture } from "./test-helpers/handlers-config"
|
|
8
|
-
|
|
9
|
-
/** POST /action の引数を組み立てる (FormData ボディ付き)。 */
|
|
10
|
-
const makeActionArgs = (intent: string) => {
|
|
11
|
-
const body = new FormData()
|
|
12
|
-
body.set("intent", intent)
|
|
13
|
-
return {
|
|
14
|
-
request: new Request("http://localhost/daily_report/api/action", { method: "POST", body }),
|
|
15
|
-
params: { endpoint: "action" } as Record<string, string | undefined>,
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
describe("action / clearCache の封鎖ゲート", () => {
|
|
20
|
-
it("既定 (未注入) では clearCache を 400 で拒否し、service.clearCache を呼ばない", async () => {
|
|
21
|
-
// 認証済みなら誰でも全ワーカーのキャッシュを無制限破棄できてしまうため既定封鎖
|
|
22
|
-
const { config, calls } = makeHandlersFixture()
|
|
23
|
-
const { api } = createDailyReportHandlers(config)
|
|
24
|
-
const res = await api.action(makeActionArgs("clearCache"))
|
|
25
|
-
expect(res.status).toBe(400)
|
|
26
|
-
expect(calls.clearCache).toBe(0)
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
it("enableDevCacheClear:true のときだけ clearCache を実行する", async () => {
|
|
30
|
-
const { config, calls } = makeHandlersFixture({ enableDevCacheClear: true })
|
|
31
|
-
const { api } = createDailyReportHandlers(config)
|
|
32
|
-
const res = await api.action(makeActionArgs("clearCache"))
|
|
33
|
-
expect(res.status).toBe(200)
|
|
34
|
-
expect(calls.clearCache).toBe(1)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
it("未認証は clearCache 以前に 401 で弾く", async () => {
|
|
38
|
-
const { config, calls } = makeHandlersFixture({ user: null, enableDevCacheClear: true })
|
|
39
|
-
const { api } = createDailyReportHandlers(config)
|
|
40
|
-
const res = await api.action(makeActionArgs("clearCache"))
|
|
41
|
-
expect(res.status).toBe(401)
|
|
42
|
-
expect(calls.clearCache).toBe(0)
|
|
43
|
-
})
|
|
44
|
-
})
|