Skip to content

Commit

Permalink
refactor(@artusx/utils): split utils
Browse files Browse the repository at this point in the history
  • Loading branch information
thonatos committed May 17, 2024
1 parent ee8489d commit d3bef0f
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 90 deletions.
4 changes: 2 additions & 2 deletions packages/libs/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
88 changes: 0 additions & 88 deletions packages/libs/utils/src/utils.ts

This file was deleted.

40 changes: 40 additions & 0 deletions packages/libs/utils/src/utils/app.ts
Original file line number Diff line number Diff line change
@@ -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<number>('API_ID', 'number');

if (!apiID) {
return;
}

return apiID;
};

export const getProxy = () => {
const ip = getEnv<string>('PROXY_IP');
const port = getEnv<number>('PROXY_PORT', 'number');
const socksType = getEnv<number>('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,
};
};
13 changes: 13 additions & 0 deletions packages/libs/utils/src/utils/crypto.ts
Original file line number Diff line number Diff line change
@@ -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');
};
33 changes: 33 additions & 0 deletions packages/libs/utils/src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -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 = <T>(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;
};
3 changes: 3 additions & 0 deletions packages/libs/utils/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './app';
export * from './env';
export * from './crypto';

0 comments on commit d3bef0f

Please sign in to comment.