refactor (backend): port convert-milliseconds to backend-rs
This commit is contained in:
parent
70aa3704ef
commit
ceca260c92
6 changed files with 53 additions and 20 deletions
2
packages/backend-rs/index.d.ts
vendored
2
packages/backend-rs/index.d.ts
vendored
|
@ -132,6 +132,8 @@ export function isSelfHost(host?: string | undefined | null): boolean
|
||||||
export function isSameOrigin(uri: string): boolean
|
export function isSameOrigin(uri: string): boolean
|
||||||
export function extractHost(uri: string): string
|
export function extractHost(uri: string): string
|
||||||
export function toPuny(host: string): string
|
export function toPuny(host: string): string
|
||||||
|
/** Convert milliseconds to a human readable string */
|
||||||
|
export function formatMilliseconds(milliseconds: number): string
|
||||||
export function toMastodonId(firefishId: string): string | null
|
export function toMastodonId(firefishId: string): string | null
|
||||||
export function fromMastodonId(mastodonId: string): string | null
|
export function fromMastodonId(mastodonId: string): string | null
|
||||||
export function nyaify(text: string, lang?: string | undefined | null): string
|
export function nyaify(text: string, lang?: string | undefined | null): string
|
||||||
|
|
|
@ -310,7 +310,7 @@ if (!nativeBinding) {
|
||||||
throw new Error(`Failed to load native binding`)
|
throw new Error(`Failed to load native binding`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const { readServerConfig, stringToAcct, acctToString, checkWordMute, getFullApAccount, isSelfHost, isSameOrigin, extractHost, toPuny, toMastodonId, fromMastodonId, nyaify, hashPassword, verifyPassword, isOldPasswordAlgorithm, AntennaSrcEnum, MutedNoteReasonEnum, NoteVisibilityEnum, NotificationTypeEnum, PageVisibilityEnum, PollNotevisibilityEnum, RelayStatusEnum, UserEmojimodpermEnum, UserProfileFfvisibilityEnum, UserProfileMutingnotificationtypesEnum, initIdGenerator, getTimestamp, genId, secureRndstr } = nativeBinding
|
const { readServerConfig, stringToAcct, acctToString, checkWordMute, getFullApAccount, isSelfHost, isSameOrigin, extractHost, toPuny, formatMilliseconds, toMastodonId, fromMastodonId, nyaify, hashPassword, verifyPassword, isOldPasswordAlgorithm, AntennaSrcEnum, MutedNoteReasonEnum, NoteVisibilityEnum, NotificationTypeEnum, PageVisibilityEnum, PollNotevisibilityEnum, RelayStatusEnum, UserEmojimodpermEnum, UserProfileFfvisibilityEnum, UserProfileMutingnotificationtypesEnum, initIdGenerator, getTimestamp, genId, secureRndstr } = nativeBinding
|
||||||
|
|
||||||
module.exports.readServerConfig = readServerConfig
|
module.exports.readServerConfig = readServerConfig
|
||||||
module.exports.stringToAcct = stringToAcct
|
module.exports.stringToAcct = stringToAcct
|
||||||
|
@ -321,6 +321,7 @@ module.exports.isSelfHost = isSelfHost
|
||||||
module.exports.isSameOrigin = isSameOrigin
|
module.exports.isSameOrigin = isSameOrigin
|
||||||
module.exports.extractHost = extractHost
|
module.exports.extractHost = extractHost
|
||||||
module.exports.toPuny = toPuny
|
module.exports.toPuny = toPuny
|
||||||
|
module.exports.formatMilliseconds = formatMilliseconds
|
||||||
module.exports.toMastodonId = toMastodonId
|
module.exports.toMastodonId = toMastodonId
|
||||||
module.exports.fromMastodonId = fromMastodonId
|
module.exports.fromMastodonId = fromMastodonId
|
||||||
module.exports.nyaify = nyaify
|
module.exports.nyaify = nyaify
|
||||||
|
|
46
packages/backend-rs/src/misc/format_milliseconds.rs
Normal file
46
packages/backend-rs/src/misc/format_milliseconds.rs
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
/// Convert milliseconds to a human readable string
|
||||||
|
#[crate::export]
|
||||||
|
pub fn format_milliseconds(milliseconds: u32) -> String {
|
||||||
|
let mut seconds = milliseconds / 1000;
|
||||||
|
let mut minutes = seconds / 60;
|
||||||
|
let mut hours = minutes / 60;
|
||||||
|
let days = hours / 24;
|
||||||
|
|
||||||
|
seconds %= 60;
|
||||||
|
minutes %= 60;
|
||||||
|
hours %= 24;
|
||||||
|
|
||||||
|
let mut buf: Vec<String> = vec![];
|
||||||
|
|
||||||
|
if days > 0 {
|
||||||
|
buf.push(format!("{} day(s)", days));
|
||||||
|
}
|
||||||
|
if hours > 0 {
|
||||||
|
buf.push(format!("{} hour(s)", hours));
|
||||||
|
}
|
||||||
|
if minutes > 0 {
|
||||||
|
buf.push(format!("{} minute(s)", minutes));
|
||||||
|
}
|
||||||
|
if seconds > 0 {
|
||||||
|
buf.push(format!("{} second(s)", seconds));
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.join(", ")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod unit_test {
|
||||||
|
use super::format_milliseconds;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn format_milliseconds_test() {
|
||||||
|
assert_eq!(format_milliseconds(1000), "1 second(s)");
|
||||||
|
assert_eq!(format_milliseconds(1387938), "23 minute(s), 7 second(s)");
|
||||||
|
assert_eq!(format_milliseconds(34200457), "9 hour(s), 30 minute(s)");
|
||||||
|
assert_eq!(
|
||||||
|
format_milliseconds(998244353),
|
||||||
|
"11 day(s), 13 hour(s), 17 minute(s), 24 second(s)"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
pub mod acct;
|
pub mod acct;
|
||||||
pub mod check_word_mute;
|
pub mod check_word_mute;
|
||||||
pub mod convert_host;
|
pub mod convert_host;
|
||||||
|
pub mod format_milliseconds;
|
||||||
pub mod mastodon_id;
|
pub mod mastodon_id;
|
||||||
pub mod nyaify;
|
pub mod nyaify;
|
||||||
pub mod password;
|
pub mod password;
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
export function convertMilliseconds(ms: number) {
|
|
||||||
let seconds = Math.round(ms / 1000);
|
|
||||||
let minutes = Math.round(seconds / 60);
|
|
||||||
let hours = Math.round(minutes / 60);
|
|
||||||
const days = Math.round(hours / 24);
|
|
||||||
seconds %= 60;
|
|
||||||
minutes %= 60;
|
|
||||||
hours %= 24;
|
|
||||||
|
|
||||||
const result = [];
|
|
||||||
if (days > 0) result.push(`${days} day(s)`);
|
|
||||||
if (hours > 0) result.push(`${hours} hour(s)`);
|
|
||||||
if (minutes > 0) result.push(`${minutes} minute(s)`);
|
|
||||||
if (seconds > 0) result.push(`${seconds} second(s)`);
|
|
||||||
|
|
||||||
return result.join(", ");
|
|
||||||
}
|
|
|
@ -2,7 +2,7 @@ import Limiter from "ratelimiter";
|
||||||
import Logger from "@/services/logger.js";
|
import Logger from "@/services/logger.js";
|
||||||
import { redisClient } from "@/db/redis.js";
|
import { redisClient } from "@/db/redis.js";
|
||||||
import type { IEndpointMeta } from "./endpoints.js";
|
import type { IEndpointMeta } from "./endpoints.js";
|
||||||
import { convertMilliseconds } from "@/misc/convert-milliseconds.js";
|
import { formatMilliseconds } from "backend-rs";
|
||||||
|
|
||||||
const logger = new Logger("limiter");
|
const logger = new Logger("limiter");
|
||||||
|
|
||||||
|
@ -78,7 +78,7 @@ export const limiter = (
|
||||||
if (info.remaining === 0) {
|
if (info.remaining === 0) {
|
||||||
reject({
|
reject({
|
||||||
message: "RATE_LIMIT_EXCEEDED",
|
message: "RATE_LIMIT_EXCEEDED",
|
||||||
remainingTime: convertMilliseconds(info.resetMs - Date.now()),
|
remainingTime: formatMilliseconds(info.resetMs - Date.now()),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
ok();
|
ok();
|
||||||
|
|
Loading…
Reference in a new issue