UNPKG

lemon-core

Version:
252 lines (251 loc) 7.12 kB
/** * `engine/utilities.ts` * - Simple Logger with timestamp + color * * @author Steve Jung <steve@lemoncloud.io> * @date 2018-05-23 initial version * @date 2019-11-26 cleanup and optimized for `lemon-core#v2` * * @copyright (C) lemoncloud.io 2019 - All Rights Reserved. */ import { EngineCore } from './types'; import $jwt, { DecodeOptions } from 'jsonwebtoken'; declare const COLORS: { red: string; green: string; yellow: string; blue: string; magenta: string; cyan: string; white: string; }; export interface JwtCommon { /** * expired at */ exp?: number; /** * issued at * = Math.floor(current_ms / 1000) */ iat?: number; /** * issuer name. */ iss?: string; } export declare type JwtAlgorithm = 'HS256' | 'HS384' | 'HS512' | 'RS256' | 'RS384' | 'RS512' | 'ES256' | 'ES384' | 'ES512' | 'PS256' | 'PS384' | 'PS512' | 'none'; /** * class: Utilities * - various functions */ export declare class Utilities { private _$; private log; private err; private name; constructor(_$: EngineCore); get_env(name: string, def_val?: string): any; env(name: string, def_val?: string): any; is_dev(): boolean; load_data_yaml(name: any, folder?: string): Promise<any>; load_sync_yaml(name: string, folder?: string): any; extend(a: any, b: any): any; isset(x: any): boolean; empty(x: any): boolean; min(a: any, b: any): any; max(a: any, b: any): any; round(a: any): number; json(o: any, isSorted?: any): string; /** * timestamp string like `2020-02-22` */ static timestamp(date?: undefined | number | Date, timeZone?: number): string; /** * parse timestamp to date. */ static datetime(dt?: string | number | Date, timeZone?: number): Date; ts(d?: undefined | number | Date, timeZone?: number): string; dt(dt?: string | number | Date, timeZone?: number): Date; now(): Date; /** * 현재 시간값 (number of milliseconds since midnight of January 1, 1970.) * * @returns {number} */ current_time_ms(shift?: number): number; /** * NameSpace Maker. */ NS(ns: string, color?: keyof typeof COLORS, len?: number, delim?: string): string; /** * escape string for mysql. */ escape(str: string, urldecode?: any): string; /** * check if integer * @param x any number or string */ isInteger(x: any): boolean; /** * convert as integer number. * @param x any number or string * @param def default value. */ N(x: any, def?: number): number; /** * parse as float number (like 1.01) * @param x any number or string * @param def default value. */ F(x: any, def?: number): number; /** * parse float by len * ``` * FN(0.333333, 2) = 0.33 * ``` * @param x any numbe or string * @param len decimal length * @param mode 'round' | 'floor' */ FN(x: any, len: number, mode?: 'round' | 'floor'): number; /** * parse float by decimal point 2 */ F2: (x: any, mode?: 'round' | 'floor') => number; /** * parse float by decimal point 3 */ F3: (x: any, mode?: 'round' | 'floor') => number; /** * convert and cut string like `abcd....z` */ S: (_: any, h?: number, t?: number, delim?: string) => string; /** * remove internal properties which starts with _ or $ */ cleanup(node: any): any; updated(that: any, that2: any): any; copy($N: any): any; copy_node(node: any, isClear?: boolean): any; /** * clean up all member without only KEY member. */ bare_node($N: any, opts?: any): any; /** * get keys in difference. */ diff(obj1: any, obj2: any): string[]; /** * check if equal between 2 object. * - inspired from `underscore` module originally, and optimized for compartibility. */ protected isEqual(obj1: any, obj2: any): boolean; /** * calcualte node differences */ diff_node(obj1: any, obj2: any): string[]; /** * get 32-bits hash value. * * @param data */ hash(data: any): string; promise(param: any): Promise<unknown>; promise_sequence(array: any, func: any): Promise<unknown>; /** * get md5 hash */ md5(data: any, digest: 'latin1' | 'hex' | 'base64'): string; /** * get hmac hash */ hmac(data: any, KEY?: string, algorithm?: string, encoding?: 'latin1' | 'hex' | 'base64'): string; /** * parse query-string. * * @param query */ qs_parse(query: string): any; /** * stringify as querystring. * @param query */ qs_stringify(query: { [key: string]: any; }): string; /** * group as qs */ readonly qs: { /** * parse qs string */ parse: (q: string) => any; /** * stringify qs object */ stringify: (q: { [key: string]: any; }) => string; }; /** * get crypto object. */ readonly crypto: (passwd: string, algorithm?: string) => { encrypt: (val: string) => string; decrypt: (msg: string) => string; }; /** * get crypto2 object (w/ Cipheriv). * - to avoid `(node:66818) Warning: Use Cipheriv for counter mode of aes-256-ctr` * * @param passwd password to crypt * @param algorithm (default as `aes-256-ctr`) * @param ivNumb iv number to populate. (default as 0, or -1 use random) * @param magic magic string to verify (default `LM!#`) */ readonly crypto2: (passwd: string, algorithm?: string, ivNumb?: number, magic?: string) => { encrypt: (val: string) => string; decrypt: (msg: string) => string; }; /** * builder for `JWTHelper` * @param passcode string for verification. * @param current_ms (optional) current time in millisecond (required to verify `exp`) */ readonly jwt: <T = any>(passcode?: string, current_ms?: number) => { /** * use `jsonwebtoken` directly. */ readonly $: typeof $jwt; /** * encode object to token string * - Synchronous Sign with default (HS256: HMAC SHA256) * * @param data object * @param algorithm algorithm to use */ encode: (data: T & JwtCommon, algorithm?: JwtAlgorithm) => string; /** * decode token string * * @param token string */ decode: (token: string, options?: DecodeOptions) => T & JwtCommon; /** * verify token * - Synchronous Verify with default (HS256: HMAC SHA256) * * @param token * @param algorithm * @throws `jwt expired` if exp has expired!. */ verify: (token: string, algorithm?: JwtAlgorithm) => T & JwtCommon; }; /** * get UUID as `uuid.v4()` */ uuid(): string; } export {};