DefectingCat /incraseNumber.ts
Last active: 10 days ago
incraseNumber js javascript animation
/**
* 按间隔更新数字,数字增长的动画。
*
* @param start 动画开始的值
* @param end 动画最后结束的值
* @param updateFn 更新值状态的方法
* @param duration 动画总时长
* @param setpDuration 每次更新的间隔
* @param precision 数字精度
*
* 数字精度只会影响在动画时显示的值,不会影响最终值
*/
export function incraseNumber(
start = 0,
end: number,
updateFn: (state: number) => void,
duration = 500,
setpDuration = 25,
precision = 0
) {
DefectingCat /byte_iter_fixed.rs
Last active: a month ago
Byte iterator
struct ByteIter<'remainder> {
remainder: &'remainder [u8],
}
/* impl<'remainder> ByteIter<'remainder> {
fn next(&mut self) -> Option<&'remainder u8> {
if self.remainder.is_empty() {
None
} else {
let byte = &self.remainder[0];
self.remainder = &self.remainder[1..];
Some(byte)
}
}
} */
impl<'remainder> Iterator for ByteIter<'remainder> {
type Item = &'remainder u8;
fn next(&mut self) -> Option<Self::Item> {
if self.remainder.is_empty() {
DefectingCat /mod.ts
Last active: a month ago
Build css module className
/**
* clsx
*/
export function cn(...rest: string[]) {
return rest.join(' ');
}
/**
* 构建 cn 函数
*
* Usage:
*
* ```ts
* import styles from 'index.module.less';
* const cn = buildCN(styles);
* <div className={cn('left', 'align-center')}>
* ```
*/
export function buildCN(module: typeof import('*.less')) {
return (...rest: string[]) => {
DefectingCat /Tooltip.module.less
Last active: a month ago
Tooltip with CSS
.body {
display: flex;
}
.btn {
display: flex;
position: relative;
// &:hover {
// background: rgba(0, 0, 0, 0.15);
// }
}
.btn-right {
&::after {
position: absolute;
left: calc(100% + 0.14rem);
border-radius: 0.04rem;
padding: 0.05rem 0.06rem;
display: flex;
DefectingCat /Tooltip.tsx
Last active: a month ago
Tooltip with CSS
import styles from '@/projects/viewer/Layouts/Default/Tooltip.module.less';
import { buildCN } from '@/utils';
import { nanoid } from 'nanoid';
import { memo, useMemo } from 'react';
const cn = buildCN(styles);
export type ToolTipProps = {
title: string;
position?: 'top' | 'right';
} & React.DetailedHTMLProps<
React.HTMLAttributes<HTMLDivElement>,
HTMLDivElement
>;
/**
* 使用 CSS 的 Tooltip
*/
const Tooltip = ({ title, position, children, ...rest }: ToolTipProps) => {
const id = useMemo(() => `more-btn-${nanoid()}`, []);
DefectingCat /cve.rs
Last active: 3 months ago
rust memory vulnerability
fn main() {
let mut safety = &String::from("hello");
println!("address of safety {:p}: {}", &safety, safety);
{
let name = "xfy".to_string();
let name = expand(&name);
println!("address of name {:p}: {}", &name, name);
safety = &name;
}
println!("address of safety {:p}: {}", &safety, safety);
}
pub const fn lifetime_translator<'a, 'b, T>(_val_a: &'a &'b (), _val_b: &'b T) -> &'a T {
_val_b
}
pub fn lifetime_translator_mut<'a, 'b, T>(_val_a: &'a &'b (), _val_b: &'b mut T) -> &'a mut T {
_val_b
}
DefectingCat /password.rs
Last active: 3 months ago
rust argon2 password hash
use anyhow::{anyhow, Context};
use tokio::task;
use argon2::password_hash::SaltString;
use argon2::{password_hash, Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
pub async fn hash(password: String) -> anyhow::Result<String> {
task::spawn_blocking(move || {
let salt = SaltString::generate(rand::thread_rng());
Ok(Argon2::default()
.hash_password(password.as_bytes(), &salt)
.map_err(|e| anyhow!(e).context("failed to hash password"))?
.to_string())
})
.await
.context("panic in hash()")?
}
pub async fn verify(password: String, hash: String) -> anyhow::Result<bool> {
task::spawn_blocking(move || {
DefectingCat /count.sh
Last active: 4 months ago
count git commit by author
git log --since="2023-01-01" --until="2023-12-31" --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5
DefectingCat /header_reader.rs
Last active: 4 months ago
rust async read tcp stream as header to string
pub async fn read_headers<R>(reader: R) -> Result<String>
where
R: AsyncRead + std::marker::Unpin,
{
let mut request_string = String::new();
let mut reader = BufReader::new(reader);
loop {
let byte = reader.read_line(&mut request_string).await?;
if byte < 3 {
break;
}
}
Ok(request_string)
}
DefectingCat /mod.rs
Last active: 4 months ago
rust accept async function in generic
use std::{collections::HashMap, future::Future};
pub struct Rymo<'a, F, Fut>
where
F: FnOnce() -> Fut + 'static + Send + Sync,
Fut: Future<Output = ()>,
{
pub port: String,
pub handle: HashMap<&'a str, F>,
}
DefectingCat /proxy.rs
Last active: 4 months ago
axum reverse proxy with cache to redis
use super::error::{RouteError, RouteResult};
use crate::AppState;
use anyhow::{anyhow, Result};
use axum::{
body::Body,
extract::{Request, State},
http::{response::Parts, HeaderName, HeaderValue, Uri},
response::{IntoResponse, Response},
};
use http_body_util::BodyExt;
use hyper::{body::Bytes, HeaderMap};
use redis::{Client, Commands};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use std::{collections::HashMap, sync::Arc};
use tokio::sync::RwLock;
use tracing::error;
static BACKEND_URI: &str = "http://192.168.1.13:8086";