release: WRNexusJS 0.4.0
This commit is contained in:
@@ -1,8 +1,4 @@
|
||||
import type {
|
||||
CaptchaChallengeRecord,
|
||||
CaptchaResponseTokenRecord,
|
||||
CaptchaStore,
|
||||
} from "../types.ts";
|
||||
import type { CaptchaChallengeRecord, CaptchaResponseTokenRecord, CaptchaStore } from "../types.ts";
|
||||
|
||||
export interface RedisCaptchaClient {
|
||||
get(key: string): Promise<string | null> | string | null;
|
||||
@@ -94,7 +90,10 @@ export class RedisCaptchaStore implements CaptchaStore {
|
||||
return this.read<CaptchaResponseTokenRecord>(this.tokenKey(tokenHash));
|
||||
}
|
||||
|
||||
async consumeToken(tokenHash: string, now: number): Promise<CaptchaResponseTokenRecord | undefined> {
|
||||
async consumeToken(
|
||||
tokenHash: string,
|
||||
now: number,
|
||||
): Promise<CaptchaResponseTokenRecord | undefined> {
|
||||
const key = this.tokenKey(tokenHash);
|
||||
if (this.redis.eval) {
|
||||
const raw = await this.redis.eval(CONSUME_TOKEN, {
|
||||
@@ -107,7 +106,9 @@ export class RedisCaptchaStore implements CaptchaStore {
|
||||
const current = await this.read<CaptchaResponseTokenRecord>(key);
|
||||
if (!current || current.expiresAt <= now || current.consumedAt) return undefined;
|
||||
current.consumedAt = now;
|
||||
await this.redis.set(key, JSON.stringify(current), { px: Math.max(1, current.expiresAt - now) });
|
||||
await this.redis.set(key, JSON.stringify(current), {
|
||||
px: Math.max(1, current.expiresAt - now),
|
||||
});
|
||||
return current;
|
||||
});
|
||||
}
|
||||
@@ -138,7 +139,9 @@ export class RedisCaptchaStore implements CaptchaStore {
|
||||
if (!current || current.expiresAt <= now || current.consumedAt) return undefined;
|
||||
if (operation === "attempt") current.attempts += 1;
|
||||
else current.consumedAt = now;
|
||||
await this.redis.set(key, JSON.stringify(current), { px: Math.max(1, current.expiresAt - now) });
|
||||
await this.redis.set(key, JSON.stringify(current), {
|
||||
px: Math.max(1, current.expiresAt - now),
|
||||
});
|
||||
return current;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user