@qlover/fe-corekit 1.2.1 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.umd.js","sources":["../../interface/executor/Executor.ts","../../interface/executor/ExecutorError.ts","../../interface/request/RequestError.ts"],"sourcesContent":["import { ExecutorError } from './ExecutorError';\nimport { ExecutorPlugin, Task } from './ExecutorPlugin';\n\n/**\n * Base executor class providing plugin management and execution pipeline\n *\n * The Executor pattern implements a pluggable execution pipeline that allows:\n * 1. Pre-processing of input data\n * 2. Post-processing of results\n * 3. Error handling\n * 4. Custom execution logic\n *\n * execNoError returns all errors as they are., and if there is a plugin onerror handler chain in which an error occurs, it will also return the error instead of throwing it.\n *\n * @abstract\n * @class Executor\n * @category Executor\n * @example\n * ```typescript\n * // Create an executor instance\n * const executor = new AsyncExecutor();\n *\n * // Add plugins\n * executor.use(new LoggerPlugin());\n * executor.use(new RetryPlugin({ maxAttempts: 3 }));\n *\n * // Execute a task\n * const result = await executor.exec(async (data) => {\n * return await someAsyncOperation(data);\n * });\n * ```\n */\nexport abstract class Executor<ExecutorConfig = unknown> {\n /**\n * Array of active plugins\n *\n * - Purpose: Stores and manages executor plugins\n * - Core Concept: Ordered plugin pipeline\n * - Main Features:\n * - Maintains plugin execution order\n * - Supports plugin lifecycle management\n * - Primary Use: Plugin orchestration and execution\n *\n * @example\n * ```typescript\n * protected plugins = [\n * new LoggerPlugin(),\n * new RetryPlugin()\n * ];\n * ```\n */\n protected plugins: ExecutorPlugin[] = [];\n\n /**\n * Creates a new Executor instance\n *\n * - Purpose: Initialize executor with optional configuration\n * - Core Concept: Configurable executor setup\n * - Main Features: Configuration injection\n * - Primary Use: Executor instantiation\n *\n * @param {ExecutorConfig} config - Optional configuration object\n *\n * @example\n * ```typescript\n * const executor = new Executor({\n * // config options\n * });\n * ```\n */\n constructor(protected config?: ExecutorConfig) {}\n\n /**\n * Add a plugin to the executor\n *\n * - Purpose: Extends executor functionality through plugins\n * - Core Concept: Plugin registration and deduplication\n * - Main Features:\n * - Prevents duplicate plugins if onlyOne is true\n * - Maintains plugin execution order\n * - Primary Use: Adding new capabilities to executor\n *\n * @param plugin - Plugin instance to add\n *\n * @example\n * ```typescript\n * executor.use(new LoggerPlugin());\n * executor.use(new RetryPlugin({ maxAttempts: 3 }));\n * ```\n *\n * @example\n *\n * Use a plain object as a plugin\n * ```typescript\n * executor.use({\n * onBefore: (data) => ({ ...data, modified: true })\n * });\n * ```\n */\n use(plugin: ExecutorPlugin): void {\n if (\n this.plugins.find(\n (p) =>\n p === plugin ||\n p.pluginName === plugin.pluginName ||\n p.constructor === plugin.constructor\n ) &&\n plugin.onlyOne\n ) {\n console.warn(`Plugin ${plugin.pluginName} is already used, skip adding`);\n return;\n }\n\n this.plugins.push(plugin);\n }\n\n /**\n * Execute a plugin hook\n *\n * - Purpose: Provides plugin hook execution mechanism\n * - Core Concept: Plugin lifecycle management\n * - Main Features:\n * - Dynamic hook execution\n * - Support for async and sync hooks\n * - Primary Use: Running plugin lifecycle methods\n *\n * @param plugins - Plugins to execute\n * @param name - Hook name to execute\n * @param args - Arguments for the hook\n *\n * @example\n * ```typescript\n * await executor.runHook(plugins, 'beforeExec', data);\n * ```\n */\n abstract runHooks(\n plugins: ExecutorPlugin[],\n name: keyof ExecutorPlugin,\n ...args: unknown[]\n ): void | unknown | Promise<void | unknown>;\n\n /**\n * Execute a task with plugin pipeline\n *\n * - Purpose: Core task execution with plugin support\n * - Core Concept: Task execution pipeline\n * - Main Features:\n * - Plugin hook integration\n * - Error handling\n * - Primary Use: Running tasks through the executor pipeline\n *\n * @param task - Task to execute\n * @param data - Optional input data for task\n * @throws {ExecutorError} If task execution fails\n *\n * @example\n * ```typescript\n * const result = await executor.exec(async (data) => {\n * return await processData(data);\n * });\n * ```\n */\n abstract exec<Result, Params = unknown>(\n task: Task<Result, Params>\n ): Promise<Result> | Result;\n\n /**\n * Execute a task with plugin pipeline and input data\n *\n * - Purpose: Core task execution with plugin support and input data\n * - Core Concept: Task execution pipeline with data\n * - Main Features:\n * - Plugin hook integration\n * - Error handling\n * - Primary Use: Running tasks with input data through the executor pipeline\n *\n * @param data - Input data for task\n * @param task - Task to execute\n * @throws {ExecutorError} If task execution fails\n *\n * @example\n * ```typescript\n * const result = await executor.exec(data, async (data) => {\n * return await processData(data);\n * });\n * ```\n */\n abstract exec<Result, Params = unknown>(\n data: unknown,\n task: Task<Result, Params>\n ): Promise<Result> | Result;\n\n /**\n * Execute a task without throwing errors\n *\n * - Purpose: Safe task execution with error wrapping\n * - Core Concept: Error-safe execution pipeline\n * - Main Features:\n * - Error wrapping in ExecutorError\n * - Non-throwing execution\n * - Primary Use: When error handling is preferred over exceptions\n *\n * @param task - Task to execute\n *\n * @example\n * ```typescript\n * const result = await executor.execNoError(async (data) => {\n * return await riskyOperation(data);\n * });\n * if (result instanceof ExecutorError) {\n * console.error('Task failed:', result);\n * }\n * ```\n */\n abstract execNoError<Result, Params = unknown>(\n task: Task<Result, Params>\n ): Promise<Result | ExecutorError> | Result | ExecutorError;\n\n /**\n * Execute a task with input data without throwing errors\n *\n * - Purpose: Safe task execution with error wrapping and input data\n * - Core Concept: Error-safe execution pipeline with data\n * - Main Features:\n * - Error wrapping in ExecutorError\n * - Non-throwing execution\n * - Primary Use: When error handling is preferred over exceptions with input data\n *\n * @param data - Input data for task\n * @param task - Task to execute\n *\n * @example\n * ```typescript\n * const result = await executor.execNoError(data, async (data) => {\n * return await riskyOperation(data);\n * });\n * if (result instanceof ExecutorError) {\n * console.error('Task failed:', result);\n * }\n * ```\n */\n abstract execNoError<Result, Params = unknown>(\n data: unknown,\n task: Task<Result, Params>\n ): Promise<Result | ExecutorError> | Result | ExecutorError;\n}\n","/**\n * Custom error class for executor operations.\n *\n * This class provides a structured way to handle errors that occur during executor operations.\n * It extends the standard Error class to include an error identification string, which can be used\n * to categorize and manage errors more effectively.\n *\n * @category Executor\n *\n * @example\n * ```typescript\n * try {\n * // some executor operation\n * } catch (error) {\n * throw new ExecutorError('EXECUTOR_ERROR', error);\n * }\n * ```\n *\n * @example\n *\n * create an error with a message from a string\n *\n * ```typescript\n * const error = new ExecutorError('ERROR_ID', 'This is an error message');\n *\n * // => error.message is 'This is an error message'\n * // => error.id is 'ERROR_ID'\n * ```\n */\nexport class ExecutorError extends Error {\n /**\n * Constructs a new ExecutorError.\n *\n * if originalError is a string, it will be used as the error message.\n * if originalError is an Error object, its message will be used as the error message.\n * if originalError is not provided, the error message will be the id.\n *\n * @param id - A unique identifier for the error, used for categorization and tracking.\n * @param originalError - The original error message or Error object that triggered this error.\n * This parameter is optional.\n */\n constructor(\n public id: string,\n originalError?: string | Error\n ) {\n super(\n originalError instanceof Error\n ? originalError.message\n : originalError || id\n );\n\n // if originalError is an Error object, use its stack\n if (originalError instanceof Error && 'stack' in originalError) {\n this.stack = originalError.stack;\n }\n\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n","import { ExecutorError } from '../executor';\n\n/**\n * Represents a custom error class for handling request-related errors in the application\n *\n * RequestError extends the base ExecutorError class to provide specific error handling\n * for HTTP requests and fetch operations. It works in conjunction with RequestErrorID\n * to categorize different types of request failures.\n *\n * @since 1.0.14\n *\n * @example\n * ```typescript\n * try {\n * await fetchData(url);\n * } catch (error) {\n * if (error instanceof RequestError) {\n * // Handle request specific error\n * console.error('Request failed:', error.message);\n * }\n * }\n * ```\n */\nexport class RequestError extends ExecutorError {}\n\n/**\n * Error IDs for different fetch request failure scenarios\n * Used to identify specific error types in error handling\n *\n * @description\n * This enum provides a standardized set of error identifiers that can be used\n * to categorize and handle different types of request failures in a consistent manner.\n * Each error ID represents a specific failure scenario that might occur during HTTP requests.\n *\n * @example\n * ```typescript\n * if (error.id === RequestErrorID.RESPONSE_NOT_OK) {\n * // Handle non-200 response\n * } else if (error.id === RequestErrorID.ABORT_ERROR) {\n * // Handle aborted request\n * }\n * ```\n */\nexport enum RequestErrorID {\n /** Generic fetch request error */\n REQUEST_ERROR = 'REQUEST_ERROR',\n /** Environment doesn't support fetch API */\n ENV_FETCH_NOT_SUPPORT = 'ENV_FETCH_NOT_SUPPORT',\n /** No fetcher function provided */\n FETCHER_NONE = 'FETCHER_NONE',\n /** Response status is not OK (not in 200-299 range) */\n RESPONSE_NOT_OK = 'RESPONSE_NOT_OK',\n /** Request was aborted */\n ABORT_ERROR = 'ABORT_ERROR',\n /** URL is not provided */\n URL_NONE = 'URL_NONE'\n}\n"],"names":["RequestErrorID"],"mappings":";;;;;;EAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA4BG;AACH,MAAA,QAAA,kBAAA,YAAA;EAqBE;;;;;;;;;;;;;;;;EAgBG;EACH,IAAA,SAAA,QAAA,CAAsB,MAAuB,EAAA;UAAvB,IAAM,CAAA,MAAA,GAAN,MAAM;EArC5B;;;;;;;;;;;;;;;;;EAiBG;UACO,IAAO,CAAA,OAAA,GAAqB,EAAE;;EAqBxC;;;;;;;;;;;;;;;;;;;;;;;;;;EA0BG;MACH,QAAG,CAAA,SAAA,CAAA,GAAA,GAAH,UAAI,MAAsB,EAAA;EACxB,QAAA,IACE,IAAI,CAAC,OAAO,CAAC,IAAI,CACf,UAAC,CAAC,EAAA;cACA,OAAA,CAAC,KAAK,MAAM;EACZ,gBAAA,CAAC,CAAC,UAAU,KAAK,MAAM,CAAC,UAAU;EAClC,gBAAA,CAAC,CAAC,WAAW,KAAK,MAAM,CAAC,WAAW;EAFpC,SAEoC,CACvC;cACD,MAAM,CAAC,OAAO,EACd;cACA,OAAO,CAAC,IAAI,CAAC,SAAA,CAAA,MAAA,CAAU,MAAM,CAAC,UAAU,EAA+B,+BAAA,CAAA,CAAC;cACxE;;EAGF,QAAA,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC;OAC1B;MAmIH,OAAC,QAAA;EAAD,CAAC,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ECrPD;;;;;;;;;;;;;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BAAc,GAazB,EAAA,CAAA,CAAA;;;;;;;;;;"}
@@ -1 +1,170 @@
1
- "use strict";var t=require("crypto"),e=require("buffer"),i=require("zlib"),n=function(){function n(t,e){void 0===e&&(e="base64"),this.encoding=e,this.ALGORITHM="aes-128-cbc",this.IV_LENGTH=16,this.KEY_LENGTH=16,this.KEY=this.validateKey(t)}return n.prototype.validateKey=function(t){var i=e.Buffer.from(t.slice(0,this.KEY_LENGTH));if(i.length!==this.KEY_LENGTH)throw new RangeError("Invalid key length. Expected ".concat(this.KEY_LENGTH," bytes, got ").concat(i.length," bytes"));return i},n.prototype.encrypt=function(e){var n=t.randomBytes(this.IV_LENGTH),r=t.createCipheriv(this.ALGORITHM,this.KEY,n),o=i.deflateSync(e),c=r.update(o,void 0,this.encoding);return c+=r.final(this.encoding),"".concat(c,":").concat(n.toString(this.encoding))},n.prototype.decrypt=function(n){var r=n.split(":"),o=r[0],c=r[1],s=t.createDecipheriv(this.ALGORITHM,this.KEY,e.Buffer.from(c,this.encoding)),a=e.Buffer.from(s.update(o,this.encoding,"binary"),"binary");return a=e.Buffer.concat([a,s.final()]),i.inflateSync(a).toString()},n}(),r=function(){function i(t,e){void 0===e&&(e="base64"),this.encoding=e,this.ALGORITHM="aes-256-cbc",this.KEY_LENGTH=32,this.KEY=this.validateKey(t)}return i.prototype.validateKey=function(t){var i=e.Buffer.from(t.slice(0,this.KEY_LENGTH));if(i.length!==this.KEY_LENGTH)throw new RangeError("Invalid key length. Expected ".concat(this.KEY_LENGTH," bytes, got ").concat(i.length," bytes"));return i},i.prototype.encrypt=function(e){var i=t.randomBytes(16),n=t.createCipheriv(this.ALGORITHM,this.KEY,i),r=n.update(e,"utf8",this.encoding);return r+=n.final(this.encoding),"".concat(r,":").concat(i.toString(this.encoding))},i.prototype.decrypt=function(i){var n=i.split(":"),r=n[0],o=n[1],c=t.createDecipheriv(this.ALGORITHM,this.KEY,e.Buffer.from(o,this.encoding)),s=c.update(r,this.encoding,"utf8");return s+=c.final("utf8")},i}();exports.StringEntrypt=r,exports.StringZlibEncrypt=n;
1
+ 'use strict';
2
+
3
+ var crypto = require('crypto');
4
+ var buffer = require('buffer');
5
+ var zlib = require('zlib');
6
+
7
+ /**
8
+ * String encryption implementation with Zlib compression
9
+ * Combines AES encryption with data compression
10
+ *
11
+ * Features:
12
+ * - AES-128-CBC encryption
13
+ * - Zlib compression
14
+ * - IV support
15
+ * - Configurable encoding
16
+ *
17
+ * @implements {Encryptor<string, string>}
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * const encryptor = new StringZlibEncrypt('my-16-char-key!!');
22
+ *
23
+ * // Encrypt and compress
24
+ * const encrypted = encryptor.encrypt('large text data');
25
+ *
26
+ * // Decrypt and decompress
27
+ * const decrypted = encryptor.decrypt(encrypted);
28
+ * ```
29
+ */
30
+ var StringZlibEncrypt = /** @class */ (function () {
31
+ /**
32
+ * Creates a new StringZlibEncrypt instance
33
+ * @param encryptionKey - Key used for encryption/decryption
34
+ * @param encoding - Output encoding format
35
+ * @throws {RangeError} If key length is invalid
36
+ */
37
+ function StringZlibEncrypt(encryptionKey, encoding) {
38
+ if (encoding === void 0) { encoding = 'base64'; }
39
+ this.encoding = encoding;
40
+ this.ALGORITHM = 'aes-128-cbc';
41
+ this.IV_LENGTH = 16;
42
+ this.KEY_LENGTH = 16;
43
+ this.KEY = this.validateKey(encryptionKey);
44
+ }
45
+ /**
46
+ * Validates and processes encryption key
47
+ * Ensures key meets length requirements
48
+ *
49
+ * @param key - Raw encryption key
50
+ * @returns Validated key buffer
51
+ * @throws {RangeError} If key length is invalid
52
+ */
53
+ StringZlibEncrypt.prototype.validateKey = function (key) {
54
+ var keyBuffer = buffer.Buffer.from(key.slice(0, this.KEY_LENGTH));
55
+ if (keyBuffer.length !== this.KEY_LENGTH) {
56
+ throw new RangeError("Invalid key length. Expected ".concat(this.KEY_LENGTH, " bytes, got ").concat(keyBuffer.length, " bytes"));
57
+ }
58
+ return keyBuffer;
59
+ };
60
+ /**
61
+ * Encrypts and compresses a string value
62
+ * Applies compression before encryption
63
+ *
64
+ * @param value - String to encrypt
65
+ * @returns Encrypted and compressed string with IV
66
+ */
67
+ StringZlibEncrypt.prototype.encrypt = function (value) {
68
+ var iv = crypto.randomBytes(this.IV_LENGTH);
69
+ var cipher = crypto.createCipheriv(this.ALGORITHM, this.KEY, iv);
70
+ var compressedValue = zlib.deflateSync(value);
71
+ var encrypted = cipher.update(compressedValue, undefined, this.encoding);
72
+ encrypted += cipher.final(this.encoding);
73
+ return "".concat(encrypted, ":").concat(iv.toString(this.encoding));
74
+ };
75
+ /**
76
+ * Decrypts and decompresses an encrypted string
77
+ * Applies decryption before decompression
78
+ *
79
+ * @param encryptedData - Encrypted string with IV
80
+ * @returns Original string
81
+ */
82
+ StringZlibEncrypt.prototype.decrypt = function (encryptedData) {
83
+ var _a = encryptedData.split(':'), encrypted = _a[0], iv = _a[1];
84
+ var decipher = crypto.createDecipheriv(this.ALGORITHM, this.KEY, buffer.Buffer.from(iv, this.encoding));
85
+ var decrypted = buffer.Buffer.from(decipher.update(encrypted, this.encoding, 'binary'), 'binary');
86
+ // console.log(decrypted);
87
+ // console.log(decipher.final());
88
+ decrypted = buffer.Buffer.concat([decrypted, decipher.final()]);
89
+ var decompressedValue = zlib.inflateSync(decrypted);
90
+ return decompressedValue.toString();
91
+ };
92
+ return StringZlibEncrypt;
93
+ }());
94
+
95
+ /**
96
+ * Represents a string encryption utility using AES-256-CBC algorithm.
97
+ *
98
+ * This class provides methods to encrypt and decrypt string values securely.
99
+ * It ensures that the encryption key meets the required length for AES-256.
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * const encryptor = new StringEntrypt('your-encryption-key');
104
+ * const encrypted = encryptor.encrypt('your-string');
105
+ * const decrypted = encryptor.decrypt(encrypted);
106
+ * ```
107
+ */
108
+ var StringEntrypt = /** @class */ (function () {
109
+ /**
110
+ * Creates a new StringEntrypt instance
111
+ * @param encryptionKey - Key used for encryption/decryption
112
+ * @param encoding - Output encoding format
113
+ * @throws {RangeError} If key length is invalid
114
+ */
115
+ function StringEntrypt(encryptionKey, encoding) {
116
+ if (encoding === void 0) { encoding = 'base64'; }
117
+ this.encoding = encoding;
118
+ this.ALGORITHM = 'aes-256-cbc';
119
+ this.KEY_LENGTH = 32; // AES-256 needs 32 bytes key
120
+ this.KEY = this.validateKey(encryptionKey);
121
+ }
122
+ /**
123
+ * Validates and processes encryption key
124
+ * Ensures key meets length requirements
125
+ *
126
+ * @param key - Raw encryption key
127
+ * @returns Validated key buffer
128
+ * @throws {RangeError} If key length is invalid
129
+ */
130
+ StringEntrypt.prototype.validateKey = function (key) {
131
+ var keyBuffer = buffer.Buffer.from(key.slice(0, this.KEY_LENGTH));
132
+ if (keyBuffer.length !== this.KEY_LENGTH) {
133
+ throw new RangeError("Invalid key length. Expected ".concat(this.KEY_LENGTH, " bytes, got ").concat(keyBuffer.length, " bytes"));
134
+ }
135
+ return keyBuffer;
136
+ };
137
+ /**
138
+ * Encrypts a string value
139
+ * Uses random IV for each encryption
140
+ *
141
+ * @param value - String to encrypt
142
+ * @returns Encrypted string with IV
143
+ */
144
+ StringEntrypt.prototype.encrypt = function (value) {
145
+ var iv = crypto.randomBytes(16);
146
+ var cipher = crypto.createCipheriv(this.ALGORITHM, this.KEY, iv);
147
+ var encrypted = cipher.update(value, 'utf8', this.encoding);
148
+ encrypted += cipher.final(this.encoding);
149
+ return "".concat(encrypted, ":").concat(iv.toString(this.encoding));
150
+ };
151
+ /**
152
+ * Decrypts an encrypted string
153
+ * Extracts IV from encrypted data
154
+ *
155
+ * @param encryptedData - Encrypted string with IV
156
+ * @returns Original string
157
+ */
158
+ StringEntrypt.prototype.decrypt = function (encryptedData) {
159
+ var _a = encryptedData.split(':'), encrypted = _a[0], iv = _a[1];
160
+ var decipher = crypto.createDecipheriv(this.ALGORITHM, this.KEY, buffer.Buffer.from(iv, this.encoding));
161
+ var decrypted = decipher.update(encrypted, this.encoding, 'utf8');
162
+ decrypted += decipher.final('utf8');
163
+ return decrypted;
164
+ };
165
+ return StringEntrypt;
166
+ }());
167
+
168
+ exports.StringEntrypt = StringEntrypt;
169
+ exports.StringZlibEncrypt = StringZlibEncrypt;
170
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs.js","sources":["../../server/encrypt/StringZlibEncrypt.ts","../../server/encrypt/StringEntrypt.ts"],"sourcesContent":["import crypto from 'crypto';\nimport { Buffer } from 'buffer';\nimport zlib from 'zlib';\nimport { Encryptor } from '../../interface';\n\n/**\n * String encryption implementation with Zlib compression\n * Combines AES encryption with data compression\n *\n * Features:\n * - AES-128-CBC encryption\n * - Zlib compression\n * - IV support\n * - Configurable encoding\n *\n * @implements {Encryptor<string, string>}\n *\n * @example\n * ```typescript\n * const encryptor = new StringZlibEncrypt('my-16-char-key!!');\n *\n * // Encrypt and compress\n * const encrypted = encryptor.encrypt('large text data');\n *\n * // Decrypt and decompress\n * const decrypted = encryptor.decrypt(encrypted);\n * ```\n */\nexport class StringZlibEncrypt implements Encryptor<string, string> {\n private ALGORITHM = 'aes-128-cbc';\n private KEY: Buffer;\n private IV_LENGTH = 16;\n private KEY_LENGTH = 16;\n\n /**\n * Creates a new StringZlibEncrypt instance\n * @param encryptionKey - Key used for encryption/decryption\n * @param encoding - Output encoding format\n * @throws {RangeError} If key length is invalid\n */\n constructor(\n encryptionKey: string,\n private readonly encoding: globalThis.BufferEncoding = 'base64'\n ) {\n this.KEY = this.validateKey(encryptionKey);\n }\n\n /**\n * Validates and processes encryption key\n * Ensures key meets length requirements\n *\n * @param key - Raw encryption key\n * @returns Validated key buffer\n * @throws {RangeError} If key length is invalid\n */\n private validateKey(key: string): Buffer {\n const keyBuffer = Buffer.from(key.slice(0, this.KEY_LENGTH));\n if (keyBuffer.length !== this.KEY_LENGTH) {\n throw new RangeError(\n `Invalid key length. Expected ${this.KEY_LENGTH} bytes, got ${keyBuffer.length} bytes`\n );\n }\n return keyBuffer;\n }\n\n /**\n * Encrypts and compresses a string value\n * Applies compression before encryption\n *\n * @param value - String to encrypt\n * @returns Encrypted and compressed string with IV\n */\n encrypt(value: string): string {\n const iv = crypto.randomBytes(this.IV_LENGTH);\n const cipher = crypto.createCipheriv(this.ALGORITHM, this.KEY, iv);\n\n const compressedValue = zlib.deflateSync(value);\n let encrypted = cipher.update(compressedValue, undefined, this.encoding);\n encrypted += cipher.final(this.encoding);\n\n return `${encrypted}:${iv.toString(this.encoding)}`;\n }\n\n /**\n * Decrypts and decompresses an encrypted string\n * Applies decryption before decompression\n *\n * @param encryptedData - Encrypted string with IV\n * @returns Original string\n */\n decrypt(encryptedData: string): string {\n const [encrypted, iv] = encryptedData.split(':');\n const decipher = crypto.createDecipheriv(\n this.ALGORITHM,\n this.KEY,\n Buffer.from(iv, this.encoding)\n );\n\n let decrypted = Buffer.from(\n decipher.update(encrypted, this.encoding, 'binary'),\n 'binary'\n );\n\n // console.log(decrypted);\n // console.log(decipher.final());\n\n decrypted = Buffer.concat([decrypted, decipher.final()]);\n const decompressedValue = zlib.inflateSync(decrypted);\n\n return decompressedValue.toString();\n }\n}\n","import crypto from 'crypto';\nimport { Buffer } from 'buffer';\nimport { Encryptor } from '../../interface';\n\n/**\n * Represents a string encryption utility using AES-256-CBC algorithm.\n *\n * This class provides methods to encrypt and decrypt string values securely.\n * It ensures that the encryption key meets the required length for AES-256.\n *\n * @example\n * ```typescript\n * const encryptor = new StringEntrypt('your-encryption-key');\n * const encrypted = encryptor.encrypt('your-string');\n * const decrypted = encryptor.decrypt(encrypted);\n * ```\n */\nexport class StringEntrypt implements Encryptor<string, string> {\n private ALGORITHM = 'aes-256-cbc';\n private KEY: Buffer;\n private KEY_LENGTH = 32; // AES-256 needs 32 bytes key\n\n /**\n * Creates a new StringEntrypt instance\n * @param encryptionKey - Key used for encryption/decryption\n * @param encoding - Output encoding format\n * @throws {RangeError} If key length is invalid\n */\n constructor(\n encryptionKey: string,\n private readonly encoding: globalThis.BufferEncoding = 'base64'\n ) {\n this.KEY = this.validateKey(encryptionKey);\n }\n\n /**\n * Validates and processes encryption key\n * Ensures key meets length requirements\n *\n * @param key - Raw encryption key\n * @returns Validated key buffer\n * @throws {RangeError} If key length is invalid\n */\n private validateKey(key: string): Buffer {\n const keyBuffer = Buffer.from(key.slice(0, this.KEY_LENGTH));\n\n if (keyBuffer.length !== this.KEY_LENGTH) {\n throw new RangeError(\n `Invalid key length. Expected ${this.KEY_LENGTH} bytes, got ${keyBuffer.length} bytes`\n );\n }\n\n return keyBuffer;\n }\n\n /**\n * Encrypts a string value\n * Uses random IV for each encryption\n *\n * @param value - String to encrypt\n * @returns Encrypted string with IV\n */\n encrypt(value: string): string {\n const iv = crypto.randomBytes(16);\n const cipher = crypto.createCipheriv(this.ALGORITHM, this.KEY, iv);\n let encrypted = cipher.update(value, 'utf8', this.encoding);\n encrypted += cipher.final(this.encoding);\n return `${encrypted}:${iv.toString(this.encoding)}`;\n }\n\n /**\n * Decrypts an encrypted string\n * Extracts IV from encrypted data\n *\n * @param encryptedData - Encrypted string with IV\n * @returns Original string\n */\n decrypt(encryptedData: string): string {\n const [encrypted, iv] = encryptedData.split(':');\n const decipher = crypto.createDecipheriv(\n this.ALGORITHM,\n this.KEY,\n Buffer.from(iv, this.encoding)\n );\n let decrypted = decipher.update(encrypted, this.encoding, 'utf8');\n decrypted += decipher.final('utf8');\n return decrypted;\n }\n}\n"],"names":["Buffer"],"mappings":";;;;;;AAKA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACH,IAAA,iBAAA,kBAAA,YAAA;AAME;;;;;AAKG;IACH,SACE,iBAAA,CAAA,aAAqB,EACJ,QAA8C,EAAA;AAA9C,QAAA,IAAA,QAAA,KAAA,MAAA,EAAA,EAAA,QAA8C,GAAA,QAAA,CAAA;QAA9C,IAAQ,CAAA,QAAA,GAAR,QAAQ;QAbnB,IAAS,CAAA,SAAA,GAAG,aAAa;QAEzB,IAAS,CAAA,SAAA,GAAG,EAAE;QACd,IAAU,CAAA,UAAA,G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,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,QAAQ,CAAC,CAC/B;AACD,QAAA,IAAI,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC;AACjE,QAAA,SAAS,IAAI,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC;AACnC,QAAA,OAAO,SAAS;KACjB;IACH,OAAC,aAAA;AAAD,CAAC,EAAA;;;;;"}
@@ -1 +1,167 @@
1
- import t from"crypto";import{Buffer as i}from"buffer";import e from"zlib";var n=function(){function n(t,i){void 0===i&&(i="base64"),this.encoding=i,this.ALGORITHM="aes-128-cbc",this.IV_LENGTH=16,this.KEY_LENGTH=16,this.KEY=this.validateKey(t)}return n.prototype.validateKey=function(t){var e=i.from(t.slice(0,this.KEY_LENGTH));if(e.length!==this.KEY_LENGTH)throw new RangeError("Invalid key length. Expected ".concat(this.KEY_LENGTH," bytes, got ").concat(e.length," bytes"));return e},n.prototype.encrypt=function(i){var n=t.randomBytes(this.IV_LENGTH),o=t.createCipheriv(this.ALGORITHM,this.KEY,n),r=e.deflateSync(i),c=o.update(r,void 0,this.encoding);return c+=o.final(this.encoding),"".concat(c,":").concat(n.toString(this.encoding))},n.prototype.decrypt=function(n){var o=n.split(":"),r=o[0],c=o[1],a=t.createDecipheriv(this.ALGORITHM,this.KEY,i.from(c,this.encoding)),s=i.from(a.update(r,this.encoding,"binary"),"binary");return s=i.concat([s,a.final()]),e.inflateSync(s).toString()},n}(),o=function(){function e(t,i){void 0===i&&(i="base64"),this.encoding=i,this.ALGORITHM="aes-256-cbc",this.KEY_LENGTH=32,this.KEY=this.validateKey(t)}return e.prototype.validateKey=function(t){var e=i.from(t.slice(0,this.KEY_LENGTH));if(e.length!==this.KEY_LENGTH)throw new RangeError("Invalid key length. Expected ".concat(this.KEY_LENGTH," bytes, got ").concat(e.length," bytes"));return e},e.prototype.encrypt=function(i){var e=t.randomBytes(16),n=t.createCipheriv(this.ALGORITHM,this.KEY,e),o=n.update(i,"utf8",this.encoding);return o+=n.final(this.encoding),"".concat(o,":").concat(e.toString(this.encoding))},e.prototype.decrypt=function(e){var n=e.split(":"),o=n[0],r=n[1],c=t.createDecipheriv(this.ALGORITHM,this.KEY,i.from(r,this.encoding)),a=c.update(o,this.encoding,"utf8");return a+=c.final("utf8")},e}();export{o as StringEntrypt,n as StringZlibEncrypt};
1
+ import crypto from 'crypto';
2
+ import { Buffer } from 'buffer';
3
+ import zlib from 'zlib';
4
+
5
+ /**
6
+ * String encryption implementation with Zlib compression
7
+ * Combines AES encryption with data compression
8
+ *
9
+ * Features:
10
+ * - AES-128-CBC encryption
11
+ * - Zlib compression
12
+ * - IV support
13
+ * - Configurable encoding
14
+ *
15
+ * @implements {Encryptor<string, string>}
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * const encryptor = new StringZlibEncrypt('my-16-char-key!!');
20
+ *
21
+ * // Encrypt and compress
22
+ * const encrypted = encryptor.encrypt('large text data');
23
+ *
24
+ * // Decrypt and decompress
25
+ * const decrypted = encryptor.decrypt(encrypted);
26
+ * ```
27
+ */
28
+ var StringZlibEncrypt = /** @class */ (function () {
29
+ /**
30
+ * Creates a new StringZlibEncrypt instance
31
+ * @param encryptionKey - Key used for encryption/decryption
32
+ * @param encoding - Output encoding format
33
+ * @throws {RangeError} If key length is invalid
34
+ */
35
+ function StringZlibEncrypt(encryptionKey, encoding) {
36
+ if (encoding === void 0) { encoding = 'base64'; }
37
+ this.encoding = encoding;
38
+ this.ALGORITHM = 'aes-128-cbc';
39
+ this.IV_LENGTH = 16;
40
+ this.KEY_LENGTH = 16;
41
+ this.KEY = this.validateKey(encryptionKey);
42
+ }
43
+ /**
44
+ * Validates and processes encryption key
45
+ * Ensures key meets length requirements
46
+ *
47
+ * @param key - Raw encryption key
48
+ * @returns Validated key buffer
49
+ * @throws {RangeError} If key length is invalid
50
+ */
51
+ StringZlibEncrypt.prototype.validateKey = function (key) {
52
+ var keyBuffer = Buffer.from(key.slice(0, this.KEY_LENGTH));
53
+ if (keyBuffer.length !== this.KEY_LENGTH) {
54
+ throw new RangeError("Invalid key length. Expected ".concat(this.KEY_LENGTH, " bytes, got ").concat(keyBuffer.length, " bytes"));
55
+ }
56
+ return keyBuffer;
57
+ };
58
+ /**
59
+ * Encrypts and compresses a string value
60
+ * Applies compression before encryption
61
+ *
62
+ * @param value - String to encrypt
63
+ * @returns Encrypted and compressed string with IV
64
+ */
65
+ StringZlibEncrypt.prototype.encrypt = function (value) {
66
+ var iv = crypto.randomBytes(this.IV_LENGTH);
67
+ var cipher = crypto.createCipheriv(this.ALGORITHM, this.KEY, iv);
68
+ var compressedValue = zlib.deflateSync(value);
69
+ var encrypted = cipher.update(compressedValue, undefined, this.encoding);
70
+ encrypted += cipher.final(this.encoding);
71
+ return "".concat(encrypted, ":").concat(iv.toString(this.encoding));
72
+ };
73
+ /**
74
+ * Decrypts and decompresses an encrypted string
75
+ * Applies decryption before decompression
76
+ *
77
+ * @param encryptedData - Encrypted string with IV
78
+ * @returns Original string
79
+ */
80
+ StringZlibEncrypt.prototype.decrypt = function (encryptedData) {
81
+ var _a = encryptedData.split(':'), encrypted = _a[0], iv = _a[1];
82
+ var decipher = crypto.createDecipheriv(this.ALGORITHM, this.KEY, Buffer.from(iv, this.encoding));
83
+ var decrypted = Buffer.from(decipher.update(encrypted, this.encoding, 'binary'), 'binary');
84
+ // console.log(decrypted);
85
+ // console.log(decipher.final());
86
+ decrypted = Buffer.concat([decrypted, decipher.final()]);
87
+ var decompressedValue = zlib.inflateSync(decrypted);
88
+ return decompressedValue.toString();
89
+ };
90
+ return StringZlibEncrypt;
91
+ }());
92
+
93
+ /**
94
+ * Represents a string encryption utility using AES-256-CBC algorithm.
95
+ *
96
+ * This class provides methods to encrypt and decrypt string values securely.
97
+ * It ensures that the encryption key meets the required length for AES-256.
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * const encryptor = new StringEntrypt('your-encryption-key');
102
+ * const encrypted = encryptor.encrypt('your-string');
103
+ * const decrypted = encryptor.decrypt(encrypted);
104
+ * ```
105
+ */
106
+ var StringEntrypt = /** @class */ (function () {
107
+ /**
108
+ * Creates a new StringEntrypt instance
109
+ * @param encryptionKey - Key used for encryption/decryption
110
+ * @param encoding - Output encoding format
111
+ * @throws {RangeError} If key length is invalid
112
+ */
113
+ function StringEntrypt(encryptionKey, encoding) {
114
+ if (encoding === void 0) { encoding = 'base64'; }
115
+ this.encoding = encoding;
116
+ this.ALGORITHM = 'aes-256-cbc';
117
+ this.KEY_LENGTH = 32; // AES-256 needs 32 bytes key
118
+ this.KEY = this.validateKey(encryptionKey);
119
+ }
120
+ /**
121
+ * Validates and processes encryption key
122
+ * Ensures key meets length requirements
123
+ *
124
+ * @param key - Raw encryption key
125
+ * @returns Validated key buffer
126
+ * @throws {RangeError} If key length is invalid
127
+ */
128
+ StringEntrypt.prototype.validateKey = function (key) {
129
+ var keyBuffer = Buffer.from(key.slice(0, this.KEY_LENGTH));
130
+ if (keyBuffer.length !== this.KEY_LENGTH) {
131
+ throw new RangeError("Invalid key length. Expected ".concat(this.KEY_LENGTH, " bytes, got ").concat(keyBuffer.length, " bytes"));
132
+ }
133
+ return keyBuffer;
134
+ };
135
+ /**
136
+ * Encrypts a string value
137
+ * Uses random IV for each encryption
138
+ *
139
+ * @param value - String to encrypt
140
+ * @returns Encrypted string with IV
141
+ */
142
+ StringEntrypt.prototype.encrypt = function (value) {
143
+ var iv = crypto.randomBytes(16);
144
+ var cipher = crypto.createCipheriv(this.ALGORITHM, this.KEY, iv);
145
+ var encrypted = cipher.update(value, 'utf8', this.encoding);
146
+ encrypted += cipher.final(this.encoding);
147
+ return "".concat(encrypted, ":").concat(iv.toString(this.encoding));
148
+ };
149
+ /**
150
+ * Decrypts an encrypted string
151
+ * Extracts IV from encrypted data
152
+ *
153
+ * @param encryptedData - Encrypted string with IV
154
+ * @returns Original string
155
+ */
156
+ StringEntrypt.prototype.decrypt = function (encryptedData) {
157
+ var _a = encryptedData.split(':'), encrypted = _a[0], iv = _a[1];
158
+ var decipher = crypto.createDecipheriv(this.ALGORITHM, this.KEY, Buffer.from(iv, this.encoding));
159
+ var decrypted = decipher.update(encrypted, this.encoding, 'utf8');
160
+ decrypted += decipher.final('utf8');
161
+ return decrypted;
162
+ };
163
+ return StringEntrypt;
164
+ }());
165
+
166
+ export { StringEntrypt, StringZlibEncrypt };
167
+ //# sourceMappingURL=index.es.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.es.js","sources":["../../server/encrypt/StringZlibEncrypt.ts","../../server/encrypt/StringEntrypt.ts"],"sourcesContent":["import crypto from 'crypto';\nimport { Buffer } from 'buffer';\nimport zlib from 'zlib';\nimport { Encryptor } from '../../interface';\n\n/**\n * String encryption implementation with Zlib compression\n * Combines AES encryption with data compression\n *\n * Features:\n * - AES-128-CBC encryption\n * - Zlib compression\n * - IV support\n * - Configurable encoding\n *\n * @implements {Encryptor<string, string>}\n *\n * @example\n * ```typescript\n * const encryptor = new StringZlibEncrypt('my-16-char-key!!');\n *\n * // Encrypt and compress\n * const encrypted = encryptor.encrypt('large text data');\n *\n * // Decrypt and decompress\n * const decrypted = encryptor.decrypt(encrypted);\n * ```\n */\nexport class StringZlibEncrypt implements Encryptor<string, string> {\n private ALGORITHM = 'aes-128-cbc';\n private KEY: Buffer;\n private IV_LENGTH = 16;\n private KEY_LENGTH = 16;\n\n /**\n * Creates a new StringZlibEncrypt instance\n * @param encryptionKey - Key used for encryption/decryption\n * @param encoding - Output encoding format\n * @throws {RangeError} If key length is invalid\n */\n constructor(\n encryptionKey: string,\n private readonly encoding: globalThis.BufferEncoding = 'base64'\n ) {\n this.KEY = this.validateKey(encryptionKey);\n }\n\n /**\n * Validates and processes encryption key\n * Ensures key meets length requirements\n *\n * @param key - Raw encryption key\n * @returns Validated key buffer\n * @throws {RangeError} If key length is invalid\n */\n private validateKey(key: string): Buffer {\n const keyBuffer = Buffer.from(key.slice(0, this.KEY_LENGTH));\n if (keyBuffer.length !== this.KEY_LENGTH) {\n throw new RangeError(\n `Invalid key length. Expected ${this.KEY_LENGTH} bytes, got ${keyBuffer.length} bytes`\n );\n }\n return keyBuffer;\n }\n\n /**\n * Encrypts and compresses a string value\n * Applies compression before encryption\n *\n * @param value - String to encrypt\n * @returns Encrypted and compressed string with IV\n */\n encrypt(value: string): string {\n const iv = crypto.randomBytes(this.IV_LENGTH);\n const cipher = crypto.createCipheriv(this.ALGORITHM, this.KEY, iv);\n\n const compressedValue = zlib.deflateSync(value);\n let encrypted = cipher.update(compressedValue, undefined, this.encoding);\n encrypted += cipher.final(this.encoding);\n\n return `${encrypted}:${iv.toString(this.encoding)}`;\n }\n\n /**\n * Decrypts and decompresses an encrypted string\n * Applies decryption before decompression\n *\n * @param encryptedData - Encrypted string with IV\n * @returns Original string\n */\n decrypt(encryptedData: string): string {\n const [encrypted, iv] = encryptedData.split(':');\n const decipher = crypto.createDecipheriv(\n this.ALGORITHM,\n this.KEY,\n Buffer.from(iv, this.encoding)\n );\n\n let decrypted = Buffer.from(\n decipher.update(encrypted, this.encoding, 'binary'),\n 'binary'\n );\n\n // console.log(decrypted);\n // console.log(decipher.final());\n\n decrypted = Buffer.concat([decrypted, decipher.final()]);\n const decompressedValue = zlib.inflateSync(decrypted);\n\n return decompressedValue.toString();\n }\n}\n","import crypto from 'crypto';\nimport { Buffer } from 'buffer';\nimport { Encryptor } from '../../interface';\n\n/**\n * Represents a string encryption utility using AES-256-CBC algorithm.\n *\n * This class provides methods to encrypt and decrypt string values securely.\n * It ensures that the encryption key meets the required length for AES-256.\n *\n * @example\n * ```typescript\n * const encryptor = new StringEntrypt('your-encryption-key');\n * const encrypted = encryptor.encrypt('your-string');\n * const decrypted = encryptor.decrypt(encrypted);\n * ```\n */\nexport class StringEntrypt implements Encryptor<string, string> {\n private ALGORITHM = 'aes-256-cbc';\n private KEY: Buffer;\n private KEY_LENGTH = 32; // AES-256 needs 32 bytes key\n\n /**\n * Creates a new StringEntrypt instance\n * @param encryptionKey - Key used for encryption/decryption\n * @param encoding - Output encoding format\n * @throws {RangeError} If key length is invalid\n */\n constructor(\n encryptionKey: string,\n private readonly encoding: globalThis.BufferEncoding = 'base64'\n ) {\n this.KEY = this.validateKey(encryptionKey);\n }\n\n /**\n * Validates and processes encryption key\n * Ensures key meets length requirements\n *\n * @param key - Raw encryption key\n * @returns Validated key buffer\n * @throws {RangeError} If key length is invalid\n */\n private validateKey(key: string): Buffer {\n const keyBuffer = Buffer.from(key.slice(0, this.KEY_LENGTH));\n\n if (keyBuffer.length !== this.KEY_LENGTH) {\n throw new RangeError(\n `Invalid key length. Expected ${this.KEY_LENGTH} bytes, got ${keyBuffer.length} bytes`\n );\n }\n\n return keyBuffer;\n }\n\n /**\n * Encrypts a string value\n * Uses random IV for each encryption\n *\n * @param value - String to encrypt\n * @returns Encrypted string with IV\n */\n encrypt(value: string): string {\n const iv = crypto.randomBytes(16);\n const cipher = crypto.createCipheriv(this.ALGORITHM, this.KEY, iv);\n let encrypted = cipher.update(value, 'utf8', this.encoding);\n encrypted += cipher.final(this.encoding);\n return `${encrypted}:${iv.toString(this.encoding)}`;\n }\n\n /**\n * Decrypts an encrypted string\n * Extracts IV from encrypted data\n *\n * @param encryptedData - Encrypted string with IV\n * @returns Original string\n */\n decrypt(encryptedData: string): string {\n const [encrypted, iv] = encryptedData.split(':');\n const decipher = crypto.createDecipheriv(\n this.ALGORITHM,\n this.KEY,\n Buffer.from(iv, this.encoding)\n );\n let decrypted = decipher.update(encrypted, this.encoding, 'utf8');\n decrypted += decipher.final('utf8');\n return decrypted;\n }\n}\n"],"names":[],"mappings":";;;;AAKA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACH,IAAA,iBAAA,kBAAA,YAAA;AAME;;;;;AAKG;IACH,SACE,iBAAA,CAAA,aAAqB,EACJ,QAA8C,EAAA;AAA9C,QAAA,IAAA,QAAA,KAAA,MAAA,EAAA,EAAA,QAA8C,GAAA,QAAA,CAAA;QAA9C,IAAQ,CAAA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4B;QAChD,IAAM,QAAQ,GAAG,MAAM,CAAC,gBAAgB,CACtC,IAAI,CAAC,SAAS,EACd,IAAI,CAAC,GAAG,EACR,MAAM,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,QAAQ,CAAC,CAC/B;AACD,QAAA,IAAI,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC;AACjE,QAAA,SAAS,IAAI,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC;AACnC,QAAA,OAAO,SAAS;KACjB;IACH,OAAC,aAAA;AAAD,CAAC,EAAA;;;;"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@qlover/fe-corekit",
3
3
  "description": "A corekit for frontwork",
4
- "version": "1.2.1",
4
+ "version": "1.2.5",
5
5
  "private": false,
6
6
  "type": "module",
7
7
  "files": [
@@ -58,7 +58,7 @@
58
58
  "merge": "^2.1.1"
59
59
  },
60
60
  "devDependencies": {
61
- "@qlover/fe-scripts": "latest",
61
+ "@qlover/env-loader": "^0.0.1",
62
62
  "@types/lodash": "^4.17.12",
63
63
  "axios": "^1.7.9"
64
64
  }