melperjs 17.0.0 → 17.1.1
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.
- package/README.md +2 -6
- package/dist/general-C6nIgKnW.cjs +651 -0
- package/dist/general.cjs +44 -0
- package/dist/general.mjs +369 -0
- package/dist/node.cjs +170 -0
- package/dist/node.mjs +142 -0
- package/docs/{docs.md → README.md} +4 -5
- package/docs/{index.md → general.md} +6 -6
- package/docs/node.md +1 -1
- package/package.json +26 -25
- package/lib/cjs/index.cjs +0 -474
- package/lib/cjs/node.cjs +0 -199
- package/lib/esm/index.mjs +0 -426
- package/lib/esm/node.mjs +0 -169
package/lib/esm/node.mjs
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import { promises as fsp } from "fs";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import crypto from "crypto";
|
|
5
|
-
import { networkInterfaces } from "os";
|
|
6
|
-
import { exec, execFileSync } from "child_process";
|
|
7
|
-
import { promisify } from "util";
|
|
8
|
-
import bcrypt from "bcryptjs";
|
|
9
|
-
import { CONSTANTS, checkEmpty } from "./index.mjs";
|
|
10
|
-
const execAsync = promisify(exec);
|
|
11
|
-
export function secureRandomBoolean() {
|
|
12
|
-
return secureRandomInteger(2) === 1;
|
|
13
|
-
}
|
|
14
|
-
export function secureRandomString(length, useNumbers = true, useUppercase = false) {
|
|
15
|
-
let characters = CONSTANTS.LOWER_CASE;
|
|
16
|
-
if (useUppercase) characters += CONSTANTS.UPPER_CASE;
|
|
17
|
-
if (useNumbers) characters += CONSTANTS.NUMBERS;
|
|
18
|
-
let result = '';
|
|
19
|
-
for (let i = 0; i < length; i++) {
|
|
20
|
-
result += characters[secureRandomInteger(0, characters.length)];
|
|
21
|
-
}
|
|
22
|
-
return result;
|
|
23
|
-
}
|
|
24
|
-
export function secureRandomHex(length) {
|
|
25
|
-
return crypto.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
|
|
26
|
-
}
|
|
27
|
-
export function secureRandomInteger(min, max = undefined) {
|
|
28
|
-
return crypto.randomInt(min, max);
|
|
29
|
-
}
|
|
30
|
-
export function secureRandomUuid(useDashes = true) {
|
|
31
|
-
const uuid = crypto.randomUUID();
|
|
32
|
-
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
33
|
-
}
|
|
34
|
-
export function secureRandomWeighted(object) {
|
|
35
|
-
if (checkEmpty(object)) return undefined;
|
|
36
|
-
const elements = Object.keys(object);
|
|
37
|
-
const weights = Object.values(object);
|
|
38
|
-
const totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
|
|
39
|
-
const randomNum = secureRandomInteger(0, totalWeight);
|
|
40
|
-
let weightSum = 0;
|
|
41
|
-
for (let i = 0; i < elements.length; i++) {
|
|
42
|
-
weightSum += weights[i];
|
|
43
|
-
if (randomNum < weightSum) {
|
|
44
|
-
return elements[i];
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
export function secureRandomElement(object) {
|
|
49
|
-
if (checkEmpty(object)) return undefined;
|
|
50
|
-
const values = Array.isArray(object) ? object : Object.values(object);
|
|
51
|
-
if (values.length === 0) return undefined;
|
|
52
|
-
return values[secureRandomInteger(0, values.length)];
|
|
53
|
-
}
|
|
54
|
-
export function uuidFromSeed(seed, useDashes = true) {
|
|
55
|
-
const hash = crypto.createHash('md5').update(seed).digest();
|
|
56
|
-
hash[6] = hash[6] & 0x0f | 0x30;
|
|
57
|
-
hash[8] = hash[8] & 0x3f | 0x80;
|
|
58
|
-
const hex = hash.toString('hex');
|
|
59
|
-
if (!useDashes) return hex;
|
|
60
|
-
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
61
|
-
}
|
|
62
|
-
export function hash(algorithm, data) {
|
|
63
|
-
return crypto.createHash(algorithm).update(data).digest("hex");
|
|
64
|
-
}
|
|
65
|
-
export function md5(data) {
|
|
66
|
-
return hash("md5", data);
|
|
67
|
-
}
|
|
68
|
-
export function sha256(data) {
|
|
69
|
-
return hash("sha256", data);
|
|
70
|
-
}
|
|
71
|
-
export function base64Encode(data) {
|
|
72
|
-
return Buffer.from(data).toString('base64');
|
|
73
|
-
}
|
|
74
|
-
export function base64Decode(data, encoding = 'utf8') {
|
|
75
|
-
return Buffer.from(data, 'base64').toString(encoding);
|
|
76
|
-
}
|
|
77
|
-
export function bcryptHash(plainText, {
|
|
78
|
-
key = "",
|
|
79
|
-
strength = 12,
|
|
80
|
-
preHash = true
|
|
81
|
-
} = {}) {
|
|
82
|
-
let input = plainText + key;
|
|
83
|
-
if (preHash) {
|
|
84
|
-
input = sha256(input);
|
|
85
|
-
}
|
|
86
|
-
return bcrypt.hashSync(input, strength);
|
|
87
|
-
}
|
|
88
|
-
export function bcryptVerify(plainText, hash, {
|
|
89
|
-
key = "",
|
|
90
|
-
preHash = true
|
|
91
|
-
} = {}) {
|
|
92
|
-
let input = plainText + key;
|
|
93
|
-
if (preHash) {
|
|
94
|
-
input = sha256(input);
|
|
95
|
-
}
|
|
96
|
-
return bcrypt.compareSync(input, hash);
|
|
97
|
-
}
|
|
98
|
-
export async function readJsonFile(filePath, defaultValue = {}) {
|
|
99
|
-
try {
|
|
100
|
-
const data = await fsp.readFile(filePath, 'utf8');
|
|
101
|
-
return JSON.parse(data);
|
|
102
|
-
} catch {
|
|
103
|
-
return defaultValue;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
export function readJsonFileSync(filePath, defaultValue = {}) {
|
|
107
|
-
try {
|
|
108
|
-
const data = fs.readFileSync(filePath, 'utf8');
|
|
109
|
-
return JSON.parse(data);
|
|
110
|
-
} catch {
|
|
111
|
-
return defaultValue;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
export function writeJsonFile(filePath, data) {
|
|
115
|
-
const jsonData = JSON.stringify(data);
|
|
116
|
-
return fsp.writeFile(filePath, jsonData, 'utf8');
|
|
117
|
-
}
|
|
118
|
-
export function writeJsonFileSync(filePath, data) {
|
|
119
|
-
const jsonData = JSON.stringify(data);
|
|
120
|
-
return fs.writeFileSync(filePath, jsonData, 'utf8');
|
|
121
|
-
}
|
|
122
|
-
export async function clearDirectory(directoryPath, keepDir = true) {
|
|
123
|
-
await fsp.rm(directoryPath, {
|
|
124
|
-
recursive: true,
|
|
125
|
-
force: true
|
|
126
|
-
});
|
|
127
|
-
if (keepDir) await fsp.mkdir(directoryPath, {
|
|
128
|
-
recursive: true
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
export function createNumberedDirs(mainDirectory, start = 0, end = 9) {
|
|
132
|
-
fs.mkdirSync(mainDirectory, {
|
|
133
|
-
recursive: true
|
|
134
|
-
});
|
|
135
|
-
for (let i = start; i <= end; i++) {
|
|
136
|
-
fs.mkdirSync(path.join(mainDirectory, `${i}`), {
|
|
137
|
-
recursive: true
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
export async function executeCommand(command) {
|
|
142
|
-
const {
|
|
143
|
-
stdout
|
|
144
|
-
} = await execAsync(command);
|
|
145
|
-
return stdout.trim();
|
|
146
|
-
}
|
|
147
|
-
export function hostIp() {
|
|
148
|
-
for (const list of Object.values(networkInterfaces())) {
|
|
149
|
-
for (const alias of list) {
|
|
150
|
-
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.address.startsWith('192.168.') && !alias.internal) {
|
|
151
|
-
return alias.address;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return '127.0.0.1';
|
|
156
|
-
}
|
|
157
|
-
export function gitVersion() {
|
|
158
|
-
try {
|
|
159
|
-
const raw = execFileSync('git', ['show', '-s', '--format=%ct', 'HEAD'], {
|
|
160
|
-
encoding: 'utf8'
|
|
161
|
-
}).trim();
|
|
162
|
-
const timestamp = parseInt(raw, 10);
|
|
163
|
-
if (isNaN(timestamp)) return "1.0";
|
|
164
|
-
const iso = new Date(timestamp * 1000).toISOString();
|
|
165
|
-
return `${iso.slice(2, 4)}${iso.slice(5, 7)}${iso.slice(8, 10)}.${iso.slice(11, 13)}${iso.slice(14, 16)}`;
|
|
166
|
-
} catch {
|
|
167
|
-
return "1.0";
|
|
168
|
-
}
|
|
169
|
-
}
|