From d3bef0f5b703429a3d660419b7dfcc8c9f0376be Mon Sep 17 00:00:00 2001 From: Suyi Date: Fri, 17 May 2024 06:05:42 +0000 Subject: [PATCH] refactor(@artusx/utils): split utils --- packages/libs/utils/package.json | 4 +- packages/libs/utils/src/utils.ts | 88 ------------------------- packages/libs/utils/src/utils/app.ts | 40 +++++++++++ packages/libs/utils/src/utils/crypto.ts | 13 ++++ packages/libs/utils/src/utils/env.ts | 33 ++++++++++ packages/libs/utils/src/utils/index.ts | 3 + 6 files changed, 91 insertions(+), 90 deletions(-) delete mode 100644 packages/libs/utils/src/utils.ts create mode 100644 packages/libs/utils/src/utils/app.ts create mode 100644 packages/libs/utils/src/utils/crypto.ts create mode 100644 packages/libs/utils/src/utils/env.ts create mode 100644 packages/libs/utils/src/utils/index.ts diff --git a/packages/libs/utils/package.json b/packages/libs/utils/package.json index 8463008e..73885a4d 100644 --- a/packages/libs/utils/package.json +++ b/packages/libs/utils/package.json @@ -26,8 +26,8 @@ "default": "./lib/bootstrap.js" }, "./utils": { - "types": "./lib/utils.d.ts", - "default": "./lib/utils.js" + "types": "./lib/utils/index.d.ts", + "default": "./lib/utils/index.js" }, "./constants": { "types": "./lib/constants.d.ts", diff --git a/packages/libs/utils/src/utils.ts b/packages/libs/utils/src/utils.ts deleted file mode 100644 index 98c69f19..00000000 --- a/packages/libs/utils/src/utils.ts +++ /dev/null @@ -1,88 +0,0 @@ -import * as crypto from 'crypto'; - -// crypto -export const md5 = (signature: string) => { - return crypto.createHash('md5').update(signature).digest('hex'); -}; - -export const hmac = (password: string, salt: string) => { - return crypto.createHmac('sha256', salt).update(password).digest('hex'); -}; - -export const slat = () => { - return crypto.randomBytes(16).toString('hex'); -}; - -export const avatar = (email: string) => { - if (!email) { - return ''; - } - const hash = md5(email); - return `https://s.gravatar.com/avatar/${hash}`; -}; - -// env utils - -export const getBooleanFromEnv = (key: string, defaultValue: boolean = false): boolean => { - const value = (process.env[key] || '').toLowerCase(); - - if (!value) { - return defaultValue; - } - - if (value === 'true' || value === '1') { - return true; - } - - if (value === 'false' || value === '0') { - return true; - } - - return defaultValue; -}; - -export const getEnv = (key: string, type?: string): T => { - const value = process.env[key] || ''; - - let target: unknown = value; - - if (type === 'boolean') { - target = getBooleanFromEnv(key); - } - - if (type === 'number') { - target = parseInt(value); - } - - return target as T; -}; - -export const getApiId = () => { - const apiID = getEnv('API_ID', 'number'); - - if (!apiID) { - return; - } - - return apiID; -}; - -export const getProxy = () => { - const ip = getEnv('PROXY_IP'); - const port = getEnv('PROXY_PORT', 'number'); - const socksType = getEnv('PROXY_SOCKET_TYPE', 'number'); - - if (!ip || !port || !socksType) { - return; - } - - const protocol = socksType === 5 ? 'socks5' : 'socks4'; - const proxyString = `${protocol}://${ip}:${port}`; - - return { - ip, - port, - socksType, - proxyString, - }; -}; diff --git a/packages/libs/utils/src/utils/app.ts b/packages/libs/utils/src/utils/app.ts new file mode 100644 index 00000000..62180ace --- /dev/null +++ b/packages/libs/utils/src/utils/app.ts @@ -0,0 +1,40 @@ +import { md5 } from './crypto'; +import { getEnv } from './env'; + +export const avatar = (email: string) => { + if (!email) { + return ''; + } + const hash = md5(email); + return `https://s.gravatar.com/avatar/${hash}`; +}; + +export const getApiId = () => { + const apiID = getEnv('API_ID', 'number'); + + if (!apiID) { + return; + } + + return apiID; +}; + +export const getProxy = () => { + const ip = getEnv('PROXY_IP'); + const port = getEnv('PROXY_PORT', 'number'); + const socksType = getEnv('PROXY_SOCKET_TYPE', 'number'); + + if (!ip || !port || !socksType) { + return; + } + + const protocol = socksType === 5 ? 'socks5' : 'socks4'; + const proxyString = `${protocol}://${ip}:${port}`; + + return { + ip, + port, + socksType, + proxyString, + }; +}; diff --git a/packages/libs/utils/src/utils/crypto.ts b/packages/libs/utils/src/utils/crypto.ts new file mode 100644 index 00000000..b2388007 --- /dev/null +++ b/packages/libs/utils/src/utils/crypto.ts @@ -0,0 +1,13 @@ +import * as crypto from 'crypto'; + +export const md5 = (signature: string) => { + return crypto.createHash('md5').update(signature).digest('hex'); +}; + +export const hmac = (password: string, salt: string) => { + return crypto.createHmac('sha256', salt).update(password).digest('hex'); +}; + +export const slat = () => { + return crypto.randomBytes(16).toString('hex'); +}; diff --git a/packages/libs/utils/src/utils/env.ts b/packages/libs/utils/src/utils/env.ts new file mode 100644 index 00000000..97d04bbf --- /dev/null +++ b/packages/libs/utils/src/utils/env.ts @@ -0,0 +1,33 @@ +export const getBooleanFromEnv = (key: string, defaultValue: boolean = false): boolean => { + const value = (process.env[key] || '').toLowerCase(); + + if (!value) { + return defaultValue; + } + + if (value === 'true' || value === '1') { + return true; + } + + if (value === 'false' || value === '0') { + return true; + } + + return defaultValue; +}; + +export const getEnv = (key: string, type?: string): T => { + const value = process.env[key] || ''; + + let target: unknown = value; + + if (type === 'boolean') { + target = getBooleanFromEnv(key); + } + + if (type === 'number') { + target = parseInt(value); + } + + return target as T; +}; diff --git a/packages/libs/utils/src/utils/index.ts b/packages/libs/utils/src/utils/index.ts new file mode 100644 index 00000000..f687ade2 --- /dev/null +++ b/packages/libs/utils/src/utils/index.ts @@ -0,0 +1,3 @@ +export * from './app'; +export * from './env'; +export * from './crypto';