@awesomeness-js/utils 1.0.17 → 1.0.19
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/index.js +19 -0
- package/package.json +1 -1
- package/secrets/dev.env +2 -1
- package/secrets/local.env +2 -1
- package/src/decrypt.js +17 -0
- package/src/encrypt.js +26 -0
- package/src/getAllFiles.js +41 -45
- package/src/password/check.js +9 -0
- package/src/password/hash.js +11 -0
- package/src/utils/shouldIgnore.js +38 -12
- package/test/css/hideMe.env +0 -0
- package/test/css/some.css +0 -0
- package/test/css/some.js +0 -0
- package/test/dontIgnore.js +0 -0
- package/test/ignoreFolder/ignoreMe.js +3 -0
- package/test/ignoreFolder2/ignoreMe.js +3 -0
- package/test/js/abc.test.js +0 -0
- package/test/js/hideMe.env +0 -0
- package/test/js/some.css +0 -0
- package/test/js/some.js +0 -0
- package/test/secret.test.js +1 -0
- package/test.js +29 -6
- package/types/decrypt.d.ts +1 -0
- package/types/encrypt.d.ts +5 -0
- package/types/hashPassword.d.ts +1 -0
- package/types/index.d.ts +19 -0
- package/types/password/check.d.ts +1 -0
- package/types/password/hash.d.ts +1 -0
- package/types/validatePassword.d.ts +1 -0
package/index.js
CHANGED
|
@@ -6,11 +6,17 @@
|
|
|
6
6
|
import _build from './src/build.js';
|
|
7
7
|
import _combineFiles from './src/combineFiles.js';
|
|
8
8
|
import _convertBytes from './src/convertBytes.js';
|
|
9
|
+
import _decrypt from './src/decrypt.js';
|
|
9
10
|
import _each from './src/each.js';
|
|
10
11
|
import _eachAsync from './src/eachAsync.js';
|
|
12
|
+
import _encrypt from './src/encrypt.js';
|
|
11
13
|
import _getAllFiles from './src/getAllFiles.js';
|
|
14
|
+
import _ignoreFolder_ignoreMe from './src/ignoreFolder/ignoreMe.js';
|
|
15
|
+
import _ignoreMe from './src/ignoreMe.js';
|
|
12
16
|
import _isUUID from './src/isUUID.js';
|
|
13
17
|
import _md5 from './src/md5.js';
|
|
18
|
+
import _password_check from './src/password/check.js';
|
|
19
|
+
import _password_hash from './src/password/hash.js';
|
|
14
20
|
import _setLocalEnvs from './src/setLocalEnvs.js';
|
|
15
21
|
import _toPennies from './src/toPennies.js';
|
|
16
22
|
import _utils_buildExportsTree from './src/utils/buildExportsTree.js';
|
|
@@ -28,9 +34,12 @@ import _uuid from './src/uuid.js';
|
|
|
28
34
|
export { _build as build };
|
|
29
35
|
export { _combineFiles as combineFiles };
|
|
30
36
|
export { _convertBytes as convertBytes };
|
|
37
|
+
export { _decrypt as decrypt };
|
|
31
38
|
export { _each as each };
|
|
32
39
|
export { _eachAsync as eachAsync };
|
|
40
|
+
export { _encrypt as encrypt };
|
|
33
41
|
export { _getAllFiles as getAllFiles };
|
|
42
|
+
export { _ignoreMe as ignoreMe };
|
|
34
43
|
export { _isUUID as isUUID };
|
|
35
44
|
export { _md5 as md5 };
|
|
36
45
|
export { _setLocalEnvs as setLocalEnvs };
|
|
@@ -60,14 +69,24 @@ export default {
|
|
|
60
69
|
* @returns {string} The converted bytes in a string format with appropriate units.
|
|
61
70
|
*/
|
|
62
71
|
convertBytes: _convertBytes,
|
|
72
|
+
decrypt: _decrypt,
|
|
63
73
|
each: _each,
|
|
64
74
|
eachAsync: _eachAsync,
|
|
75
|
+
encrypt: _encrypt,
|
|
65
76
|
getAllFiles: _getAllFiles,
|
|
77
|
+
ignoreMe: _ignoreMe,
|
|
66
78
|
isUUID: _isUUID,
|
|
67
79
|
md5: _md5,
|
|
68
80
|
setLocalEnvs: _setLocalEnvs,
|
|
69
81
|
toPennies: _toPennies,
|
|
70
82
|
uuid: _uuid,
|
|
83
|
+
ignoreFolder: {
|
|
84
|
+
ignoreMe: _ignoreFolder_ignoreMe,
|
|
85
|
+
},
|
|
86
|
+
password: {
|
|
87
|
+
check: _password_check,
|
|
88
|
+
hash: _password_hash,
|
|
89
|
+
},
|
|
71
90
|
utils: {
|
|
72
91
|
buildExportsTree: _utils_buildExportsTree,
|
|
73
92
|
buildFileDataList: _utils_buildFileDataList,
|
package/package.json
CHANGED
package/secrets/dev.env
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
JUST_A_TEST='Dev just a test'
|
|
1
|
+
JUST_A_TEST='Dev just a test'
|
|
2
|
+
AWESOMENESS_ENCRYPTION_KEY='12345678901234567890123456789012'
|
package/secrets/local.env
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
JUST_A_TEST='Local just a test'
|
|
1
|
+
JUST_A_TEST='Local just a test'
|
|
2
|
+
AWESOMENESS_ENCRYPTION_KEY='12345678901234567890123456789012'
|
package/src/decrypt.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createDecipheriv } from 'crypto';
|
|
2
|
+
|
|
3
|
+
export default function decrypt(encryptedData, key = process.env.AWESOMENESS_ENCRYPTION_KEY ?? 'YOU SHOULD HAVE REPLACED THIS... Slacker...') {
|
|
4
|
+
|
|
5
|
+
console.log({ key });
|
|
6
|
+
|
|
7
|
+
const { iv, authTag, cipherText } = encryptedData;
|
|
8
|
+
|
|
9
|
+
const decipher = createDecipheriv('aes-256-gcm', key, Buffer.from(iv, 'hex'));
|
|
10
|
+
decipher.setAuthTag(Buffer.from(authTag, 'hex'));
|
|
11
|
+
|
|
12
|
+
let decrypted = decipher.update(cipherText, 'hex', 'utf8');
|
|
13
|
+
decrypted += decipher.final('utf8');
|
|
14
|
+
|
|
15
|
+
return decrypted;
|
|
16
|
+
|
|
17
|
+
}
|
package/src/encrypt.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { randomBytes, createCipheriv } from 'crypto';
|
|
2
|
+
|
|
3
|
+
// Encrypt plaintext using AES-256-GCM
|
|
4
|
+
export default function encrypt(plainText, key = process.env.AWESOMENESS_ENCRYPTION_KEY ?? 'YOU SHOULD HAVE REPLACED THIS... Slacker...') {
|
|
5
|
+
|
|
6
|
+
console.log({ key });
|
|
7
|
+
|
|
8
|
+
// GCM typically uses a 12- or 16-byte IV/nonce
|
|
9
|
+
const iv = randomBytes(12);
|
|
10
|
+
|
|
11
|
+
const cipher = createCipheriv('aes-256-gcm', key, iv);
|
|
12
|
+
let encrypted = cipher.update(plainText, 'utf8', 'hex');
|
|
13
|
+
encrypted += cipher.final('hex');
|
|
14
|
+
|
|
15
|
+
// GCM provides an auth tag, which is required for decryption
|
|
16
|
+
const authTag = cipher.getAuthTag().toString('hex');
|
|
17
|
+
|
|
18
|
+
// Return all parts needed for safe decryption
|
|
19
|
+
return {
|
|
20
|
+
iv: iv.toString('hex'),
|
|
21
|
+
authTag,
|
|
22
|
+
cipherText: encrypted
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
|
package/src/getAllFiles.js
CHANGED
|
@@ -3,50 +3,46 @@ import { join } from 'path';
|
|
|
3
3
|
import shouldIgnore from './utils/shouldIgnore.js';
|
|
4
4
|
|
|
5
5
|
export default function getAllFiles(base, {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
dir = '.',
|
|
7
|
+
files = [],
|
|
8
|
+
ignore = [],
|
|
9
|
+
fileTypes = []
|
|
10
10
|
} = {}) {
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return files;
|
|
51
|
-
|
|
52
|
-
}
|
|
12
|
+
const directory = join(base, dir);
|
|
13
|
+
|
|
14
|
+
const sortedFiles = readdirSync(directory).sort();
|
|
15
|
+
|
|
16
|
+
sortedFiles.forEach(file => {
|
|
17
|
+
const fullPath = join(directory, file);
|
|
18
|
+
// 1) Generate the original "relative path"
|
|
19
|
+
const relativePath = join(dir, file).replace(/\\/g, '/');
|
|
20
|
+
|
|
21
|
+
// 2) Prepend a slash so patterns like "/css/*.js" will match "/css/some.js"
|
|
22
|
+
const pathForIgnore = '/' + relativePath.replace(/^\/*/, '');
|
|
23
|
+
|
|
24
|
+
// 3) Check with the leading slash path
|
|
25
|
+
if (shouldIgnore(pathForIgnore, ignore)) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Recurse if it's a directory
|
|
30
|
+
if (statSync(fullPath).isDirectory()) {
|
|
31
|
+
getAllFiles(base, {
|
|
32
|
+
dir: join(dir, file),
|
|
33
|
+
files,
|
|
34
|
+
ignore,
|
|
35
|
+
fileTypes
|
|
36
|
+
});
|
|
37
|
+
} else {
|
|
38
|
+
// Filter by file types if specified
|
|
39
|
+
if (fileTypes.length > 0 && !fileTypes.some(ext => file.endsWith(ext))) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
// 4) Store the original relative path (without leading slash) in `files` if you prefer
|
|
43
|
+
files.push(relativePath);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return files;
|
|
48
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { pbkdf2Sync } from 'crypto';
|
|
2
|
+
|
|
3
|
+
// Compare a plain-text password to a stored salt+hash
|
|
4
|
+
export default function validatePassword(password, storedHash) {
|
|
5
|
+
const [salt, originalHash] = storedHash.split('::');
|
|
6
|
+
const derivedKey = pbkdf2Sync(password, salt, 100000, 64, 'sha512');
|
|
7
|
+
return derivedKey.toString('hex') === originalHash;
|
|
8
|
+
}
|
|
9
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { randomBytes, pbkdf2Sync } from 'crypto';
|
|
2
|
+
|
|
3
|
+
// Hash a password using Node.js built-in crypto (no external deps).
|
|
4
|
+
export default function hashPassword(password) {
|
|
5
|
+
// Generate a random 16-byte salt
|
|
6
|
+
const salt = randomBytes(16).toString('hex');
|
|
7
|
+
// Derive a 64-byte key using PBKDF2 with 100k iterations (adjust as needed)
|
|
8
|
+
const derivedKey = pbkdf2Sync(password, salt, 100000, 64, 'sha512');
|
|
9
|
+
// Return salt and hash combined as a single string
|
|
10
|
+
return `${salt}::${derivedKey.toString('hex')}`;
|
|
11
|
+
}
|
|
@@ -1,17 +1,43 @@
|
|
|
1
1
|
export default function shouldIgnore(filePath, ignorePatterns) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
// filePath is already something like "/css/some.js"
|
|
3
|
+
return ignorePatterns.some(pattern => {
|
|
4
|
+
// pattern might be "/css/*.js" or "/ignoreFolder", etc.
|
|
5
|
+
let normalizedPattern = pattern.replace(/\\/g, '/');
|
|
6
|
+
|
|
7
|
+
// 1) Full directory ignore: "/ignoreFolder" => ignore "/ignoreFolder" + subdirectories
|
|
8
|
+
// If the pattern ends with "/", treat it as a directory path.
|
|
9
|
+
if (!normalizedPattern.includes('*')) {
|
|
10
|
+
// e.g. "/ignoreFolder/"
|
|
11
|
+
if (normalizedPattern.endsWith('/')) {
|
|
12
|
+
normalizedPattern = normalizedPattern.slice(0, -1); // remove trailing slash
|
|
13
|
+
}
|
|
14
|
+
return filePath === normalizedPattern || filePath.startsWith(normalizedPattern + '/');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 2) folder/* => Ignore all immediate children in that folder (no subfolders)
|
|
6
18
|
if (normalizedPattern.endsWith('/*')) {
|
|
7
|
-
const
|
|
8
|
-
|
|
19
|
+
const baseFolder = normalizedPattern.slice(0, -2); // e.g. "/css"
|
|
20
|
+
// e.g. filePath === "/css/some.js" => startsWith("/css/")
|
|
21
|
+
// But also ensure there's no further subfolder.
|
|
22
|
+
return filePath.startsWith(baseFolder + '/') &&
|
|
23
|
+
!filePath.slice(baseFolder.length + 1).includes('/');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 3) *.ext => extension-based ignore (any folder)
|
|
27
|
+
if (normalizedPattern.startsWith('*.')) {
|
|
28
|
+
const ext = normalizedPattern.slice(1); // remove '*'
|
|
29
|
+
return filePath.endsWith(ext);
|
|
9
30
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
31
|
+
|
|
32
|
+
// 4) folder/*.ext => only files with that extension in that folder (no subfolders)
|
|
33
|
+
if (normalizedPattern.includes('/*')) {
|
|
34
|
+
const [baseFolder, ext] = normalizedPattern.split('/*');
|
|
35
|
+
return filePath.startsWith(baseFolder + '/') &&
|
|
36
|
+
filePath.endsWith(ext) &&
|
|
37
|
+
!filePath.slice(baseFolder.length + 1).includes('/');
|
|
13
38
|
}
|
|
14
|
-
|
|
39
|
+
|
|
40
|
+
// 5) Exact match
|
|
41
|
+
return filePath === normalizedPattern;
|
|
15
42
|
});
|
|
16
|
-
|
|
17
|
-
}
|
|
43
|
+
}
|
|
File without changes
|
|
File without changes
|
package/test/css/some.js
ADDED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/test/js/some.css
ADDED
|
File without changes
|
package/test/js/some.js
ADDED
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// just a test file to ignore
|
package/test.js
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import utils from './index.js';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
let fileList2 = utils.getAllFiles('./test', {
|
|
4
|
+
// fileTypes: ['.css'],
|
|
5
|
+
// fileTypes: ['.js'],
|
|
6
|
+
ignore: [
|
|
7
|
+
"/ignoreFolder",
|
|
8
|
+
"/ignoreFolder2/",
|
|
9
|
+
"*.env",
|
|
10
|
+
"*.test.js",
|
|
11
|
+
"/css/*.js",
|
|
12
|
+
"/js/*.css"
|
|
13
|
+
]
|
|
14
|
+
})
|
|
8
15
|
|
|
9
16
|
console.log({fileList2});
|
|
10
17
|
|
|
@@ -32,4 +39,20 @@ await utils.setLocalEnvs('./secrets/local.env');
|
|
|
32
39
|
console.log('localEnv', process.env.JUST_A_TEST);
|
|
33
40
|
|
|
34
41
|
await utils.setLocalEnvs('./secrets/dev.env');
|
|
35
|
-
console.log('localEnv', process.env.JUST_A_TEST);
|
|
42
|
+
console.log('localEnv', process.env.JUST_A_TEST);
|
|
43
|
+
|
|
44
|
+
const storedHash = utils.password.hash('mySecret123');
|
|
45
|
+
console.log('Stored Hash:', storedHash);
|
|
46
|
+
console.log('Is valid (correct password)?', utils.password.check('mySecret123', storedHash));
|
|
47
|
+
console.log('Is valid (wrong password)?', utils.password.check('wrongPassword', storedHash));
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
// Example 256-bit key for AES-256
|
|
51
|
+
// In real usage, store this securely (e.g., an environment variable or key vault).
|
|
52
|
+
|
|
53
|
+
const secretMessage = 'This is top secret!';
|
|
54
|
+
const encrypted = utils.encrypt(secretMessage);
|
|
55
|
+
console.log('Encrypted Data:', encrypted);
|
|
56
|
+
|
|
57
|
+
const decrypted = utils.decrypt(encrypted);
|
|
58
|
+
console.log('Decrypted Data:', decrypted);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function decrypt(encryptedData: any, key?: any): any;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function hashPassword(password: any): string;
|
package/types/index.d.ts
CHANGED
|
@@ -6,11 +6,17 @@
|
|
|
6
6
|
import type _build from './build';
|
|
7
7
|
import type _combineFiles from './combineFiles';
|
|
8
8
|
import type _convertBytes from './convertBytes';
|
|
9
|
+
import type _decrypt from './decrypt';
|
|
9
10
|
import type _each from './each';
|
|
10
11
|
import type _eachAsync from './eachAsync';
|
|
12
|
+
import type _encrypt from './encrypt';
|
|
11
13
|
import type _getAllFiles from './getAllFiles';
|
|
14
|
+
import type _ignoreFolder_ignoreMe from './ignoreFolder/ignoreMe';
|
|
15
|
+
import type _ignoreMe from './ignoreMe';
|
|
12
16
|
import type _isUUID from './isUUID';
|
|
13
17
|
import type _md5 from './md5';
|
|
18
|
+
import type _password_check from './password/check';
|
|
19
|
+
import type _password_hash from './password/hash';
|
|
14
20
|
import type _setLocalEnvs from './setLocalEnvs';
|
|
15
21
|
import type _toPennies from './toPennies';
|
|
16
22
|
import type _utils_buildExportsTree from './utils/buildExportsTree';
|
|
@@ -28,9 +34,12 @@ import type _uuid from './uuid';
|
|
|
28
34
|
export declare const build: typeof _build;
|
|
29
35
|
export declare const combineFiles: typeof _combineFiles;
|
|
30
36
|
export declare const convertBytes: typeof _convertBytes;
|
|
37
|
+
export declare const decrypt: typeof _decrypt;
|
|
31
38
|
export declare const each: typeof _each;
|
|
32
39
|
export declare const eachAsync: typeof _eachAsync;
|
|
40
|
+
export declare const encrypt: typeof _encrypt;
|
|
33
41
|
export declare const getAllFiles: typeof _getAllFiles;
|
|
42
|
+
export declare const ignoreMe: typeof _ignoreMe;
|
|
34
43
|
export declare const isUUID: typeof _isUUID;
|
|
35
44
|
export declare const md5: typeof _md5;
|
|
36
45
|
export declare const setLocalEnvs: typeof _setLocalEnvs;
|
|
@@ -60,14 +69,24 @@ declare const _default: {
|
|
|
60
69
|
* @returns {string} The converted bytes in a string format with appropriate units.
|
|
61
70
|
*/
|
|
62
71
|
convertBytes: typeof _convertBytes;
|
|
72
|
+
decrypt: typeof _decrypt;
|
|
63
73
|
each: typeof _each;
|
|
64
74
|
eachAsync: typeof _eachAsync;
|
|
75
|
+
encrypt: typeof _encrypt;
|
|
65
76
|
getAllFiles: typeof _getAllFiles;
|
|
77
|
+
ignoreMe: typeof _ignoreMe;
|
|
66
78
|
isUUID: typeof _isUUID;
|
|
67
79
|
md5: typeof _md5;
|
|
68
80
|
setLocalEnvs: typeof _setLocalEnvs;
|
|
69
81
|
toPennies: typeof _toPennies;
|
|
70
82
|
uuid: typeof _uuid;
|
|
83
|
+
ignoreFolder: {
|
|
84
|
+
ignoreMe: typeof _ignoreFolder_ignoreMe,
|
|
85
|
+
},
|
|
86
|
+
password: {
|
|
87
|
+
check: typeof _password_check,
|
|
88
|
+
hash: typeof _password_hash,
|
|
89
|
+
},
|
|
71
90
|
utils: {
|
|
72
91
|
buildExportsTree: typeof _utils_buildExportsTree,
|
|
73
92
|
buildFileDataList: typeof _utils_buildFileDataList,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function validatePassword(password: any, storedHash: any): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function hashPassword(password: any): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function validatePassword(password: any, storedHash: any): boolean;
|