@jetthai/cuora 1.1.487 → 1.1.488-dev.1
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/.claude/commands/sync-ai-prompts.md +88 -1
- package/.claude/settings.local.json +3 -1
- package/.github/copilot-instructions.md +17 -6
- package/.husky/post-checkout +3 -3
- package/.husky/pre-push +1 -1
- package/.prettierignore +2 -1
- package/AGENTS.md +17 -6
- package/AI_PROMPT_SOURCE.md +17 -6
- package/CHANGELOG.md +422 -23
- package/CLAUDE.md +17 -6
- package/GEMINI.md +17 -6
- package/e2e/feature-map.md +19 -0
- package/package.json +1 -1
- package/prepare-for-pr/JT-1776243420.md +85 -0
- package/prepare-for-pr/TI-16026/STATE.md +18 -0
- package/prepare-for-pr/TI-16026/after-prepare-authorization.md +3 -0
- package/prepare-for-pr/TI-16026/analysis.md +38 -0
- package/prepare-for-pr/TI-16026/answer-analyze-01.md +1 -0
- package/prepare-for-pr/TI-16026/answer-commit-01.md +2 -0
- package/prepare-for-pr/TI-16026/answer-commit-02.md +1 -0
- package/prepare-for-pr/TI-16026/bundle.json +18 -0
- package/prepare-for-pr/TI-16026/checklist.md +196 -0
- package/prepare-for-pr/TI-16026/checklist.unit-test-original.md +39 -0
- package/prepare-for-pr/TI-16026/claude-after-model +1 -0
- package/prepare-for-pr/TI-16026/claude-commit-session +1 -0
- package/prepare-for-pr/TI-16026/claude-harvest-session +1 -0
- package/prepare-for-pr/TI-16026/claude-hops/01-analyze.err +1 -0
- package/prepare-for-pr/TI-16026/claude-hops/01-analyze.out +2 -0
- package/prepare-for-pr/TI-16026/claude-hops/02-analyze.err +1 -0
- package/prepare-for-pr/TI-16026/claude-hops/02-analyze.out +4 -0
- package/prepare-for-pr/TI-16026/claude-hops/03-dev.err +1 -0
- package/prepare-for-pr/TI-16026/claude-hops/03-dev.out +4 -0
- package/prepare-for-pr/TI-16026/claude-hops/04-dev.err +1 -0
- package/prepare-for-pr/TI-16026/claude-hops/04-dev.out +4 -0
- package/prepare-for-pr/TI-16026/claude-hops/05-dev.err +1 -0
- package/prepare-for-pr/TI-16026/claude-hops/05-dev.out +3 -0
- package/prepare-for-pr/TI-16026/claude-hops/06-after.err +1 -0
- package/prepare-for-pr/TI-16026/claude-hops/06-after.out +2 -0
- package/prepare-for-pr/TI-16026/claude-hops/07-commit.err +1 -0
- package/prepare-for-pr/TI-16026/claude-hops/07-commit.out +2 -0
- package/prepare-for-pr/TI-16026/claude-hops/08-commit.err +1 -0
- package/prepare-for-pr/TI-16026/claude-hops/08-commit.out +2 -0
- package/prepare-for-pr/TI-16026/claude-hops/09-commit.err +1 -0
- package/prepare-for-pr/TI-16026/claude-hops/09-commit.out +8 -0
- package/prepare-for-pr/TI-16026/claude-hops/10-harvest.err +1 -0
- package/prepare-for-pr/TI-16026/claude-hops/10-harvest.out +3 -0
- package/prepare-for-pr/TI-16026/claude-hops/11-harvest.err +1 -0
- package/prepare-for-pr/TI-16026/claude-hops/11-harvest.out +3 -0
- package/prepare-for-pr/TI-16026/commit-note.md +5 -0
- package/prepare-for-pr/TI-16026/evidence-index.json +215 -0
- package/prepare-for-pr/TI-16026/feature-map.before.md +61 -0
- package/prepare-for-pr/TI-16026/harvest-redaction.json +34 -0
- package/prepare-for-pr/TI-16026/harvest-redaction.out +37 -0
- package/prepare-for-pr/TI-16026/harvest.md +46 -0
- package/prepare-for-pr/TI-16026/lessons.md +5 -0
- package/prepare-for-pr/TI-16026/note-harvest-redaction.md +1 -0
- package/prepare-for-pr/TI-16026/open-questions.md +6 -0
- package/prepare-for-pr/TI-16026/publish.md +32 -0
- package/prepare-for-pr/TI-16026/review-convergence-response.md +3 -0
- package/prepare-for-pr/TI-16026/review-input/scope.md +18 -0
- package/prepare-for-pr/TI-16026/review-log.md +57 -0
- package/prepare-for-pr/TI-16026/review-notes.md +97 -0
- package/prepare-for-pr/TI-16026/review-response-r1.md +7 -0
- package/prepare-for-pr/TI-16026/self-test-report.md +67 -0
- package/prepare-for-pr/TI-16026/spec-proposal.md +34 -0
- package/prepare-for-pr/TI-16026/spec-snapshot.md +291 -0
- package/prepare-for-pr/TI-16026/state-log.md +57 -0
- package/prepare-for-pr/TI-16026/user-messages/01.md +12 -0
- package/prepare-for-pr/TI-16026/user-messages/02.md +20 -0
- package/prepare-for-pr/TI-16026/user-messages/03.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/04.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/05.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/06.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/07.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/08.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/09.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/10.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/11.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/12.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/13.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/14.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/15.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/16.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/17.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/18.md +10 -0
- package/prepare-for-pr/TI-16026/user-messages/19.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/20.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/21.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/22.md +1 -0
- package/prepare-for-pr/TI-16026/user-messages/23.md +8 -0
- package/prepare-for-pr/TI-16026/user-messages/24.md +24 -0
- package/prepare-for-pr/TI-16026/user-question.md +24 -0
- package/prepare-for-pr/TI-16026/verify/metrics-check.json +13 -0
- package/prepare-for-pr/TI-16026/verify/redaction.json +38 -0
- package/prepare-for-pr/TI-16026/verify/row-closure.json +45 -0
- package/prepare-for-pr/TI-16026/verify/s04-ws-capture.md +23 -0
- package/prepare-for-pr/TI-16026/verify-after.md +23 -0
- package/prepare-for-pr/TI-16026/verify.json +80 -0
- package/prepare-for-pr/TI-16026.md +408 -0
- package/scripts/auto-version.js +1 -0
- package/scripts/branch-auto-npm-publish.js +9 -2
- package/src/api/payment.js +1 -0
- package/src/machine/auth.js +36 -33
- package/src/machine/game_v2_1.js +15 -0
- package/src/machine/reward_cash.js +2 -2
- package/src/machine/user_sign_up.js +3 -0
- package/src/machine/withdraw/withdraw_manual_cash.js +17 -7
- package/src/request/request.js +103 -4
- package/src/util/constant.js +6 -0
- package/src/util/tokenStore.js +44 -0
- package/test/auth_self_timeout.test.js +171 -0
- package/test/request_self.test.js +288 -0
- package/test/withdraw_manual_cash.test.js +149 -0
- package/tests/session.test.mjs +177 -0
package/src/machine/game_v2_1.js
CHANGED
|
@@ -6,6 +6,8 @@ import { GAME_SEARCH } from '../util/constant.js'
|
|
|
6
6
|
// 對應 intersectionObserver 版本
|
|
7
7
|
export default function (request) {
|
|
8
8
|
const { appSecret } = request.config
|
|
9
|
+
// 不呼叫 game/logout 直接回 idle:session 已換新時,舊 session 的遊戲登出沒有意義
|
|
10
|
+
const abandonGame = { target: 'idle', actions: 'clearGameURL' }
|
|
9
11
|
return createMachine(
|
|
10
12
|
{
|
|
11
13
|
id: 'game',
|
|
@@ -160,6 +162,7 @@ export default function (request) {
|
|
|
160
162
|
EXIT_GAME: {
|
|
161
163
|
target: 'disconnect',
|
|
162
164
|
},
|
|
165
|
+
ABANDON_GAME: abandonGame,
|
|
163
166
|
},
|
|
164
167
|
},
|
|
165
168
|
demoGame: {
|
|
@@ -178,6 +181,7 @@ export default function (request) {
|
|
|
178
181
|
EXIT_GAME: {
|
|
179
182
|
target: 'disconnect',
|
|
180
183
|
},
|
|
184
|
+
ABANDON_GAME: abandonGame,
|
|
181
185
|
},
|
|
182
186
|
},
|
|
183
187
|
connectSwitching: {
|
|
@@ -196,6 +200,7 @@ export default function (request) {
|
|
|
196
200
|
EXIT_GAME: {
|
|
197
201
|
target: 'disconnect',
|
|
198
202
|
},
|
|
203
|
+
ABANDON_GAME: abandonGame,
|
|
199
204
|
},
|
|
200
205
|
},
|
|
201
206
|
playing: {
|
|
@@ -203,6 +208,7 @@ export default function (request) {
|
|
|
203
208
|
EXIT_GAME: {
|
|
204
209
|
target: 'disconnect',
|
|
205
210
|
},
|
|
211
|
+
ABANDON_GAME: abandonGame,
|
|
206
212
|
REFRESH: {
|
|
207
213
|
target: 'refreshing',
|
|
208
214
|
},
|
|
@@ -224,6 +230,7 @@ export default function (request) {
|
|
|
224
230
|
EXIT_GAME: {
|
|
225
231
|
target: 'disconnect',
|
|
226
232
|
},
|
|
233
|
+
ABANDON_GAME: abandonGame,
|
|
227
234
|
EXIT_DEMO_GAME: {
|
|
228
235
|
target: 'idle',
|
|
229
236
|
},
|
|
@@ -245,6 +252,9 @@ export default function (request) {
|
|
|
245
252
|
actions: 'setGameURL',
|
|
246
253
|
},
|
|
247
254
|
},
|
|
255
|
+
on: {
|
|
256
|
+
ABANDON_GAME: abandonGame,
|
|
257
|
+
},
|
|
248
258
|
},
|
|
249
259
|
refreshPending: {
|
|
250
260
|
after: {
|
|
@@ -252,6 +262,9 @@ export default function (request) {
|
|
|
252
262
|
target: 'connecting',
|
|
253
263
|
},
|
|
254
264
|
},
|
|
265
|
+
on: {
|
|
266
|
+
ABANDON_GAME: abandonGame,
|
|
267
|
+
},
|
|
255
268
|
},
|
|
256
269
|
disconnect: {
|
|
257
270
|
invoke: {
|
|
@@ -266,6 +279,7 @@ export default function (request) {
|
|
|
266
279
|
},
|
|
267
280
|
},
|
|
268
281
|
on: {
|
|
282
|
+
ABANDON_GAME: abandonGame,
|
|
269
283
|
LOAD: {
|
|
270
284
|
target: 'routing',
|
|
271
285
|
actions: 'setLoadable',
|
|
@@ -426,6 +440,7 @@ export default function (request) {
|
|
|
426
440
|
return event.data.gameWalletType
|
|
427
441
|
},
|
|
428
442
|
}),
|
|
443
|
+
clearGameURL: assign({ type: undefined, gameToken: undefined, gameHtml: undefined, gameURL: undefined, gameWalletType: undefined }),
|
|
429
444
|
setGameInfo: assign((context, event) => {
|
|
430
445
|
const { providerId, gameId, gameTypeId, mappingId, portrait, gameCode, name, gameName } = event.data.game
|
|
431
446
|
return { game: { providerId, gameId, gameTypeId, mappingId, portrait: portrait ? 1 : 0, gameCode, name, gameName } }
|
|
@@ -197,7 +197,7 @@ export default function (request, context) {
|
|
|
197
197
|
)
|
|
198
198
|
const getOtherRewardAPI = request.send(
|
|
199
199
|
activity.getRewardList({
|
|
200
|
-
rewardType: 'CASH,FREE,PRIZE',
|
|
200
|
+
rewardType: 'CASH,FREE,PRIZE,GAME',
|
|
201
201
|
count: 30,
|
|
202
202
|
appSecret,
|
|
203
203
|
orderColumn,
|
|
@@ -212,7 +212,7 @@ export default function (request, context) {
|
|
|
212
212
|
const lang = context.lang
|
|
213
213
|
const getOtherRewardAPI = request.send(
|
|
214
214
|
activity.getRewardList({
|
|
215
|
-
rewardType: 'CASH,FREE,PRIZE',
|
|
215
|
+
rewardType: 'CASH,FREE,PRIZE,GAME',
|
|
216
216
|
count: 30,
|
|
217
217
|
appSecret,
|
|
218
218
|
orderColumn,
|
|
@@ -107,6 +107,7 @@ export default function (request) {
|
|
|
107
107
|
return {
|
|
108
108
|
signUpData: {
|
|
109
109
|
provider: 'local',
|
|
110
|
+
birthday,
|
|
110
111
|
...pickBindData(event.data),
|
|
111
112
|
birthday,
|
|
112
113
|
email,
|
|
@@ -130,6 +131,7 @@ export default function (request) {
|
|
|
130
131
|
return {
|
|
131
132
|
signUpData: {
|
|
132
133
|
provider: 'phone',
|
|
134
|
+
birthday,
|
|
133
135
|
...pickBindData(event.data),
|
|
134
136
|
birthday,
|
|
135
137
|
email,
|
|
@@ -152,6 +154,7 @@ export default function (request) {
|
|
|
152
154
|
return {
|
|
153
155
|
signUpData: {
|
|
154
156
|
provider: 'email',
|
|
157
|
+
birthday,
|
|
155
158
|
...pickBindData(event.data),
|
|
156
159
|
birthday,
|
|
157
160
|
email,
|
|
@@ -36,9 +36,8 @@ export default function (request, context) {
|
|
|
36
36
|
target: 'checking',
|
|
37
37
|
actions: 'setInitData',
|
|
38
38
|
},
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
},
|
|
39
|
+
// submitted 的 REFRESH 失敗時無法確認訂單已結束,保留原 Pending 畫面讓下次 REFRESH 可重試
|
|
40
|
+
onError: [{ target: 'submitted', cond: 'isProcessing' }, { target: 'loaded' }],
|
|
42
41
|
},
|
|
43
42
|
},
|
|
44
43
|
checking: {
|
|
@@ -96,7 +95,11 @@ export default function (request, context) {
|
|
|
96
95
|
},
|
|
97
96
|
},
|
|
98
97
|
},
|
|
99
|
-
submitted: {
|
|
98
|
+
submitted: {
|
|
99
|
+
on: {
|
|
100
|
+
REFRESH: { target: 'loading', internal: false },
|
|
101
|
+
},
|
|
102
|
+
},
|
|
100
103
|
otp: {
|
|
101
104
|
type: 'parallel',
|
|
102
105
|
states: {
|
|
@@ -132,9 +135,16 @@ export default function (request, context) {
|
|
|
132
135
|
target: '#withdraw_manual_cash.submitted',
|
|
133
136
|
actions: 'setProcessing',
|
|
134
137
|
},
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
+
// 驗證碼錯誤(ERR_M129)留在 OTP 讓前台就地顯示錯誤、玩家重填;其他錯誤照舊回到 calculated
|
|
139
|
+
onError: [
|
|
140
|
+
{
|
|
141
|
+
target: 'idle',
|
|
142
|
+
cond: (context, event) => event.data?.code === 'ERR_M129',
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
target: '#withdraw_manual_cash.calculated',
|
|
146
|
+
},
|
|
147
|
+
],
|
|
138
148
|
},
|
|
139
149
|
},
|
|
140
150
|
reset: {
|
package/src/request/request.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import pickBy from 'lodash/pickBy.js'
|
|
2
|
-
import
|
|
2
|
+
import createTokenStore from '../util/tokenStore.js'
|
|
3
3
|
import { getErr998SentryExtra } from '../util/err998SentryExtra.js'
|
|
4
|
+
import { SELF_REQUEST_TIMEOUT_MS } from '../util/constant.js'
|
|
4
5
|
|
|
5
6
|
// 暫停 ERR_998 上報:在組裝 Sentry 資料之前就中止,不建立事件;仍照常 reject ERR_998 給呼叫端。恢復時改回 false 即可
|
|
6
7
|
const IS_ERR_998_REPORT_PAUSED = true
|
|
7
8
|
|
|
8
9
|
const CANCELLED_MESSAGE = 'Requests Cancelled'
|
|
9
10
|
|
|
11
|
+
const SELF_URL = 'self'
|
|
12
|
+
const TOKEN_EXPIRED_CODE = 'ERR_A108'
|
|
13
|
+
|
|
10
14
|
// const pickBy = require('lodash/pickBy')
|
|
11
15
|
const supportVersion2List = [
|
|
12
16
|
// 04-09 Update
|
|
@@ -159,10 +163,12 @@ export default class Request {
|
|
|
159
163
|
this.config = config
|
|
160
164
|
this.sentry = config.sentry
|
|
161
165
|
this.key = `cuora_${config.clientId}`
|
|
166
|
+
this.tokenStore = createTokenStore(this.key)
|
|
162
167
|
this.errorHandler = null
|
|
163
168
|
this.hosts = []
|
|
164
169
|
this.isBlocked = false
|
|
165
170
|
this.activeRequests = new Set() // { source, token }:僅追蹤非 public 且帶 token 的在途請求,供 revokeToken 取消
|
|
171
|
+
this.selfFlights = new Map() // 在途的 /self 讀取:同類別(channel)、同 token、同 descriptor 共用同一筆,結束即移除
|
|
166
172
|
this.revokedToken = null
|
|
167
173
|
this.tokenGate = null
|
|
168
174
|
this.clientIp = '' // 由外部 plugin(如 clientIp.client.js)非同步寫入,用於 ERR_998 Sentry 回報
|
|
@@ -281,7 +287,69 @@ export default class Request {
|
|
|
281
287
|
return !!this.axios.isCancel?.(error) || error?.message === CANCELLED_MESSAGE
|
|
282
288
|
}
|
|
283
289
|
|
|
290
|
+
_isSelf(req) {
|
|
291
|
+
return req.url.split('?')[0] === SELF_URL
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
_isSelfRead(req) {
|
|
295
|
+
return !req.isPublic && this._isSelf(req) && req.method.toUpperCase() === 'GET'
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
_isSelfWrite(req) {
|
|
299
|
+
return !req.isPublic && this._isSelf(req) && req.method.toUpperCase() === 'POST'
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// GET /self 成功(任何類別)即證明 token 仍有效:比照 auth machine 的 extendToken 續 5 分鐘。
|
|
303
|
+
// 主輪詢連續逾時期間,只要其他類別的 GET /self 有一筆成功就不該被 TTL 登出。
|
|
304
|
+
// POST /self 不續:後端只在 GET /self 續期 token
|
|
305
|
+
_extendToken(requestToken) {
|
|
306
|
+
if (requestToken && requestToken === this.getToken()) this.extendToken()
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// /self 讀取分三類(descriptor 的 channel):主輪詢 polling/免費遊戲輪詢 freeGame/單獨呼叫 standalone(預設)。
|
|
310
|
+
// 類別之間各發各的,不共用在途;同一類別內單一在途:在途期間相同的呼叫共用同一筆結果不另發。
|
|
311
|
+
// key 含 channel、token 與 descriptor:換身分或參數不同(錯誤處理不同)各自獨立;自帶 customToken 者由呼叫端管理,不共用
|
|
284
312
|
async send(req, customToken) {
|
|
313
|
+
const token = !customToken && this._isSelfRead(req) ? this.getToken() : null
|
|
314
|
+
if (!token) return this._send(req, customToken)
|
|
315
|
+
|
|
316
|
+
const key = JSON.stringify([req.channel || 'standalone', token, req.url, req.params ?? null, req.headers ?? null, !!req.silent])
|
|
317
|
+
const inFlight = this.selfFlights.get(key)
|
|
318
|
+
if (inFlight) return inFlight
|
|
319
|
+
|
|
320
|
+
const flight = this._send(req).finally(() => {
|
|
321
|
+
if (this.selfFlights.get(key) === flight) this.selfFlights.delete(key)
|
|
322
|
+
})
|
|
323
|
+
this.selfFlights.set(key, flight)
|
|
324
|
+
return flight
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// 逾時視同網路失敗(ERR_998:不進 errorHandler、不彈窗),並取消請求。
|
|
328
|
+
// 不用 axios 的 timeout:ECONNABORTED 會被下游 axios-retry 重試;取消的錯誤不帶 config,不會被重試
|
|
329
|
+
_requestWithTimeout(requestConfig, tracked, timeoutMs) {
|
|
330
|
+
let timer
|
|
331
|
+
const timeout = new Promise((resolve, reject) => {
|
|
332
|
+
timer = setTimeout(() => {
|
|
333
|
+
reject({ code: 'ERR_998', endpoint: requestConfig.url, isTimeout: true })
|
|
334
|
+
tracked?.source.cancel(CANCELLED_MESSAGE)
|
|
335
|
+
}, timeoutMs)
|
|
336
|
+
})
|
|
337
|
+
return Promise.race([this.axios.request(requestConfig), timeout]).finally(() => clearTimeout(timer))
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// ERR_A108 多半是續期的短暫落差:先以 /self 確認 session 仍在,成功才把原請求重送一次。
|
|
341
|
+
// /self 自己、已重送過的請求、送出後身分已變的請求都不重送
|
|
342
|
+
async _renewSession(req, requestToken) {
|
|
343
|
+
if (!requestToken || this._isSelf(req) || this.getToken() !== requestToken) return false
|
|
344
|
+
try {
|
|
345
|
+
await this._send({ url: SELF_URL, method: 'get', silent: true }, undefined, true)
|
|
346
|
+
} catch (error) {
|
|
347
|
+
return false
|
|
348
|
+
}
|
|
349
|
+
return this.getToken() === requestToken
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
async _send(req, customToken, isRetry) {
|
|
285
353
|
let tracked = null
|
|
286
354
|
let requestToken = null
|
|
287
355
|
try {
|
|
@@ -292,7 +360,7 @@ export default class Request {
|
|
|
292
360
|
if (!req.isPublic) this._throwIfRevoked()
|
|
293
361
|
|
|
294
362
|
// /self 是 gate 自己的探測請求,不可等待 gate,否則死鎖
|
|
295
|
-
if (this.tokenGate && !req.isPublic &&
|
|
363
|
+
if (this.tokenGate && !req.isPublic && !this._isSelf(req)) {
|
|
296
364
|
await this.tokenGate
|
|
297
365
|
this._throwIfRevoked()
|
|
298
366
|
if (!this.getToken()) return Promise.reject({ code: 'ERR_A103' })
|
|
@@ -317,15 +385,33 @@ export default class Request {
|
|
|
317
385
|
if (this.isBlocked && !configUrls.includes(requestConfig.url)) {
|
|
318
386
|
// 狀態是維護中&請求非siteConfig,終止所有的請求,除了維護頁需要的資料(siteConfig)
|
|
319
387
|
} else {
|
|
320
|
-
|
|
388
|
+
// 單一在途必須配逾時釋放:請求卡住不回時,共用它的輪詢會跟著永久停止
|
|
389
|
+
const isSelfWrite = this._isSelfWrite(req)
|
|
390
|
+
if (isSelfWrite) {
|
|
391
|
+
// 寫入與讀取相反:每次嘗試各 15 秒,逾時(ECONNABORTED)交給下游 axios-retry 照現行設定重試;
|
|
392
|
+
// shouldResetTimeout 讓每次重試各有完整期限,否則期限由所有嘗試共用
|
|
393
|
+
requestConfig.timeout = SELF_REQUEST_TIMEOUT_MS
|
|
394
|
+
requestConfig['axios-retry'] = { shouldResetTimeout: true }
|
|
395
|
+
}
|
|
396
|
+
const { data } = await (this._isSelfRead(req)
|
|
397
|
+
? this._requestWithTimeout(requestConfig, tracked, SELF_REQUEST_TIMEOUT_MS)
|
|
398
|
+
: this.axios.request(requestConfig))
|
|
399
|
+
if (this._isSelfRead(req)) this._extendToken(requestToken)
|
|
321
400
|
return data
|
|
322
401
|
}
|
|
323
402
|
} catch (error) {
|
|
324
403
|
if (this._isCancelled(error)) throw this._cancelledError()
|
|
404
|
+
if (error?.code === TOKEN_EXPIRED_CODE && !isRetry && (await this._renewSession(req, requestToken))) {
|
|
405
|
+
return await this._send(req, customToken, true)
|
|
406
|
+
}
|
|
325
407
|
// 不可列舉:避免 Bearer token 隨 JSON.stringify/Sentry 序列化外洩,但 error.requestToken 仍可讀取
|
|
326
408
|
if (requestToken && error && typeof error === 'object' && Object.isExtensible(error)) {
|
|
327
409
|
Object.defineProperty(error, 'requestToken', { value: requestToken, enumerable: false, configurable: true })
|
|
328
410
|
}
|
|
411
|
+
// POST /self 重試用完仍逾時:比照讀取的逾時(ERR_998:不進 errorHandler、不彈窗)
|
|
412
|
+
if (error?.code === 'ECONNABORTED' && this._isSelfWrite(req)) {
|
|
413
|
+
throw { code: 'ERR_998', endpoint: req.url, isTimeout: true }
|
|
414
|
+
}
|
|
329
415
|
if (error.code === 'ERR_F108') {
|
|
330
416
|
this.isBlocked = true
|
|
331
417
|
this.errorHandler && this.errorHandler(error)
|
|
@@ -421,6 +507,19 @@ export default class Request {
|
|
|
421
507
|
}
|
|
422
508
|
|
|
423
509
|
getToken() {
|
|
424
|
-
return
|
|
510
|
+
return this.tokenStore.get()
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
setToken(token) {
|
|
514
|
+
this.tokenStore.set(token)
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// 回傳續期當下的 token;已不存在時回傳 null
|
|
518
|
+
extendToken() {
|
|
519
|
+
return this.tokenStore.extend()
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
removeToken() {
|
|
523
|
+
this.tokenStore.clear()
|
|
425
524
|
}
|
|
426
525
|
}
|
package/src/util/constant.js
CHANGED
|
@@ -22,3 +22,9 @@ export const GAME_TYPE_LIST = [
|
|
|
22
22
|
// OTP 自動驗證:主動打的 /self 未通過後,最多再等幾次 auth 例行輪詢的 /self、等待上限多久
|
|
23
23
|
export const AUTO_VERIFY_MAX_USER_INFO_UPDATES = 2
|
|
24
24
|
export const AUTO_VERIFY_WAIT_MS = 5000
|
|
25
|
+
|
|
26
|
+
// 登入 token 的存活秒數:每次 /self 成功續期一次,逾時由瀏覽器自行移除 cookie
|
|
27
|
+
export const TOKEN_MAX_AGE = 300
|
|
28
|
+
|
|
29
|
+
// /self 讀取的逾時上限:後端 5 分鐘沒收到 /self 會移除 token,逾時釋放在途後由下一輪輪詢重發
|
|
30
|
+
export const SELF_REQUEST_TIMEOUT_MS = 15000
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import storage from './storage.js'
|
|
2
|
+
import { TOKEN_MAX_AGE } from './constant.js'
|
|
3
|
+
|
|
4
|
+
// document.cookie 在受限的環境(sandbox iframe、停用 cookie)會直接拋錯:讀不到視為沒有,寫不進去就算了
|
|
5
|
+
function read(name) {
|
|
6
|
+
const prefix = `${name}=`
|
|
7
|
+
try {
|
|
8
|
+
const pair = document.cookie.split('; ').find((item) => item.startsWith(prefix))
|
|
9
|
+
return pair ? decodeURIComponent(pair.slice(prefix.length)) || null : null
|
|
10
|
+
} catch (error) {
|
|
11
|
+
return null
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// 不帶 Domain:cookie 只屬於寫入它的網域,不跨子網域
|
|
16
|
+
function write(name, value, maxAge) {
|
|
17
|
+
try {
|
|
18
|
+
const secure = location.protocol === 'https:' ? '; Secure' : ''
|
|
19
|
+
document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAge}; SameSite=Lax${secure}`
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.error(`Error writing cookie: ${error}`)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default function createTokenStore(key) {
|
|
26
|
+
// token 原本存在 localStorage;改放 cookie 後不沿用舊值,殘留的一併清掉
|
|
27
|
+
storage.remove(key)
|
|
28
|
+
|
|
29
|
+
const store = {
|
|
30
|
+
get: () => read(key),
|
|
31
|
+
set(token) {
|
|
32
|
+
if (token) write(key, token, TOKEN_MAX_AGE)
|
|
33
|
+
else store.clear()
|
|
34
|
+
},
|
|
35
|
+
// 以現有的值重寫一次,讓存活時間重新起算
|
|
36
|
+
extend() {
|
|
37
|
+
const token = read(key)
|
|
38
|
+
if (token) write(key, token, TOKEN_MAX_AGE)
|
|
39
|
+
return token
|
|
40
|
+
},
|
|
41
|
+
clear: () => write(key, '', 0),
|
|
42
|
+
}
|
|
43
|
+
return store
|
|
44
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { registerHooks } from 'node:module'
|
|
4
|
+
import { interpret } from 'xstate'
|
|
5
|
+
|
|
6
|
+
// src/api/auth.js 以無副檔名的 'lodash/pickBy' 匯入(專案慣例,交由下游 bundler 解析),Node 原生 ESM 需補副檔名
|
|
7
|
+
registerHooks({
|
|
8
|
+
resolve: (specifier, context, nextResolve) => nextResolve(specifier === 'lodash/pickBy' ? 'lodash/pickBy.js' : specifier, context),
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
const store = new Map()
|
|
12
|
+
globalThis.localStorage = {
|
|
13
|
+
getItem: (k) => (store.has(k) ? store.get(k) : null),
|
|
14
|
+
setItem: (k, v) => store.set(k, v),
|
|
15
|
+
removeItem: (k) => store.delete(k),
|
|
16
|
+
}
|
|
17
|
+
globalThis.window = { atob: (value) => Buffer.from(value, 'base64').toString(), location: { search: '', protocol: 'https:' } }
|
|
18
|
+
|
|
19
|
+
const { default: auth } = await import('../src/machine/auth.js')
|
|
20
|
+
|
|
21
|
+
const TIMEOUT = { code: 'ERR_998', endpoint: 'self', isTimeout: true }
|
|
22
|
+
const NETWORK = { code: 'ERR_998', endpoint: 'self' }
|
|
23
|
+
// 不帶 rank:isNotInline 成立,inline 立即轉 login(多數帳號的實際路徑)
|
|
24
|
+
const USER = { uid_site: 'u@test' }
|
|
25
|
+
const INLINE_USER = { uid_site: 'u@test', rank: 1 }
|
|
26
|
+
const siteConfig = Buffer.from(JSON.stringify({ loginMethods: {}, registerMethods: {}, dtVerify: false })).toString('base64')
|
|
27
|
+
|
|
28
|
+
// selfResults 依序決定每一筆 /self 的結果(Error 物件以外視為成功回應);用完後一律成功
|
|
29
|
+
function start({ token = null, selfResults = [] } = {}) {
|
|
30
|
+
store.clear()
|
|
31
|
+
const calls = { self: 0, logout: 0 }
|
|
32
|
+
// auth machine 只經 Request 的 token API 讀寫 token(實作是 cookie),這裡以記憶體值代替
|
|
33
|
+
const session = { token }
|
|
34
|
+
const request = {
|
|
35
|
+
config: { appSecret: 's', clientId: 'test' },
|
|
36
|
+
getToken: () => session.token,
|
|
37
|
+
setToken: (value) => {
|
|
38
|
+
session.token = value || null
|
|
39
|
+
},
|
|
40
|
+
extendToken: () => session.token,
|
|
41
|
+
removeToken: () => {
|
|
42
|
+
session.token = null
|
|
43
|
+
},
|
|
44
|
+
async send(descriptor) {
|
|
45
|
+
if (descriptor.url === 'getConfig') return { config: siteConfig }
|
|
46
|
+
if (descriptor.url === 'login') return { token: 'token-new' }
|
|
47
|
+
if (descriptor.url === 'logout') return void calls.logout++
|
|
48
|
+
if (descriptor.url === 'self') {
|
|
49
|
+
const result = calls.self < selfResults.length ? selfResults[calls.self] : USER
|
|
50
|
+
calls.self++
|
|
51
|
+
if (result.code) throw result
|
|
52
|
+
return result
|
|
53
|
+
}
|
|
54
|
+
throw new Error(`Unexpected request: ${descriptor.url}`)
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
const machine = auth(request)
|
|
58
|
+
const visited = []
|
|
59
|
+
const service = interpret(machine.withContext({ ...machine.context, tick: 5 }))
|
|
60
|
+
service.onTransition((state) => visited.push(state.toStrings().at(-1)))
|
|
61
|
+
service.start()
|
|
62
|
+
return { service, calls, visited, session }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function waitFor(service, predicate) {
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
const timer = setTimeout(() => reject(new Error(`Timed out, state: ${JSON.stringify(service.state.value)}`)), 1000)
|
|
68
|
+
const check = (state) => {
|
|
69
|
+
if (!predicate(state)) return
|
|
70
|
+
clearTimeout(timer)
|
|
71
|
+
resolve(state)
|
|
72
|
+
}
|
|
73
|
+
service.onTransition(check)
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const count = (visited, name) => visited.filter((value) => value === name).length
|
|
78
|
+
|
|
79
|
+
test('開站帶 token:/self 逾時不登出,重發到成功後完成登入', async () => {
|
|
80
|
+
const { service, calls, visited, session } = start({ token: 'token-A', selfResults: [TIMEOUT, TIMEOUT] })
|
|
81
|
+
try {
|
|
82
|
+
await waitFor(service, (state) => state.matches('login.pending'))
|
|
83
|
+
assert.equal(count(visited, 'leaving'), 0)
|
|
84
|
+
assert.equal(count(visited, 'logout'), 0)
|
|
85
|
+
assert.equal(calls.logout, 0)
|
|
86
|
+
assert.equal(service.state.context.token, 'token-A')
|
|
87
|
+
assert.deepEqual(service.state.context.userInfo, USER)
|
|
88
|
+
assert.equal(session.token, 'token-A')
|
|
89
|
+
} finally {
|
|
90
|
+
service.stop()
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('開站帶 token:一般 ERR_998(非逾時)維持原行為,走 leaving 登出', async () => {
|
|
95
|
+
const { service, calls, visited, session } = start({ token: 'token-A', selfResults: [NETWORK] })
|
|
96
|
+
try {
|
|
97
|
+
await waitFor(service, (state) => state.matches('idle') && visited.includes('logoutFinish'))
|
|
98
|
+
assert.equal(count(visited, 'leaving'), 1)
|
|
99
|
+
assert.equal(calls.self, 1)
|
|
100
|
+
assert.equal(session.token, null)
|
|
101
|
+
} finally {
|
|
102
|
+
service.stop()
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('剛登入:/self 逾時改在 userInfoRetrying 重發,authenticated 只進入一次', async () => {
|
|
107
|
+
const { service, calls, visited } = start({ selfResults: [TIMEOUT, TIMEOUT] })
|
|
108
|
+
try {
|
|
109
|
+
await waitFor(service, (state) => state.matches('idle'))
|
|
110
|
+
service.send({ type: 'LOGIN_ID', data: { username: 'u', password: 'p' } })
|
|
111
|
+
await waitFor(service, (state) => state.matches('otpChecking'))
|
|
112
|
+
assert.equal(count(visited, 'authenticated'), 1)
|
|
113
|
+
assert.equal(count(visited, 'userInfoRetrying'), 2)
|
|
114
|
+
assert.equal(count(visited, 'leaving'), 0)
|
|
115
|
+
assert.equal(calls.self, 3)
|
|
116
|
+
assert.equal(service.state.context.token, 'token-new')
|
|
117
|
+
assert.deepEqual(service.state.context.userInfo, USER)
|
|
118
|
+
} finally {
|
|
119
|
+
service.stop()
|
|
120
|
+
}
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
test('剛登入:重發期間遇到非逾時錯誤仍走 leaving', async () => {
|
|
124
|
+
const { service, visited } = start({ selfResults: [TIMEOUT, { code: 'ERR_A113' }] })
|
|
125
|
+
try {
|
|
126
|
+
await waitFor(service, (state) => state.matches('idle'))
|
|
127
|
+
service.send({ type: 'LOGIN_ID', data: { username: 'u', password: 'p' } })
|
|
128
|
+
await waitFor(service, (state) => visited.includes('logoutFinish') && state.matches('idle'))
|
|
129
|
+
assert.equal(count(visited, 'userInfoRetrying'), 1)
|
|
130
|
+
assert.equal(count(visited, 'leaving'), 1)
|
|
131
|
+
} finally {
|
|
132
|
+
service.stop()
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
test('登入態輪詢:/self 逾時回 pending 等下一輪,不登出', async () => {
|
|
137
|
+
// 第 1 筆 tokenAuthenticated、第 2 筆 checkDeviceTokenDone 成功;第 3、4 筆(checkDeviceTokenDone 後的輪詢)逾時
|
|
138
|
+
const { service, calls, visited } = start({ token: 'token-A', selfResults: [USER, TIMEOUT, TIMEOUT, TIMEOUT] })
|
|
139
|
+
try {
|
|
140
|
+
await waitFor(service, () => calls.self >= 6)
|
|
141
|
+
assert.equal(count(visited, 'leaving'), 0)
|
|
142
|
+
assert.ok(service.state.matches('login'))
|
|
143
|
+
assert.equal(service.state.context.token, 'token-A')
|
|
144
|
+
assert.deepEqual(service.state.context.userInfo, USER)
|
|
145
|
+
} finally {
|
|
146
|
+
service.stop()
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('登入態輪詢:驗證失敗(非逾時)維持原行為,走 leaving 登出', async () => {
|
|
151
|
+
const { service, visited, session } = start({ token: 'token-A', selfResults: [USER, USER, { code: 'ERR_A113' }] })
|
|
152
|
+
try {
|
|
153
|
+
await waitFor(service, (state) => visited.includes('logoutFinish') && state.matches('idle'))
|
|
154
|
+
assert.equal(count(visited, 'leaving'), 1)
|
|
155
|
+
assert.equal(session.token, null)
|
|
156
|
+
} finally {
|
|
157
|
+
service.stop()
|
|
158
|
+
}
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
test('inline 輪詢:/self 逾時回 pending 等下一輪,不登出', async () => {
|
|
162
|
+
const { service, calls, visited } = start({ token: 'token-A', selfResults: [INLINE_USER, TIMEOUT, TIMEOUT, INLINE_USER, TIMEOUT] })
|
|
163
|
+
try {
|
|
164
|
+
await waitFor(service, () => calls.self >= 6)
|
|
165
|
+
assert.equal(count(visited, 'leaving'), 0)
|
|
166
|
+
assert.ok(visited.includes('inline.fetching'))
|
|
167
|
+
assert.equal(service.state.context.token, 'token-A')
|
|
168
|
+
} finally {
|
|
169
|
+
service.stop()
|
|
170
|
+
}
|
|
171
|
+
})
|