b2b-platform-utils 1.1.0 → 1.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/cryptoWrapper.js CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  // Purpose: PBKDF2-based password hashing/validation with a salt fetched from cache or config file.
4
- // Note: Public API preserved exactly.
5
4
 
6
5
  const crypto = require('crypto');
7
6
  const { getValue } = require('./localCache');
@@ -12,31 +11,30 @@ let keyLen = 64;
12
11
 
13
12
  module.exports = {
14
13
  /**
15
- * Get salt value from in-memory cache or fallback to config file.
14
+ * Get salt value from in-memory cache or fallback to config file ./config/config.json (resolved from process.cwd()).
16
15
  */
17
16
  getSalt: () => {
18
17
  let salt = getValue('salt');
19
- // Migrations/seeders flow when keys are not existing in runtime
20
18
  if (!salt) {
21
- let config = readFile('./config/config.json');
22
- config = JSON.parse(config);
23
- salt = config?.salt;
19
+ try {
20
+ let config = readFile('./config/config.json'); // resolved relative to service root
21
+ config = JSON.parse(config);
22
+ salt = config?.salt;
23
+ } catch {
24
+ // Intentionally swallow ENOENT/parse errors; caller will fail deterministically if salt is missing.
25
+ }
24
26
  }
25
27
  return salt;
26
28
  },
27
29
 
28
- /**
29
- * Derive a password hash using PBKDF2-SHA512.
30
- */
30
+ /** Derive a password hash using PBKDF2-SHA512. */
31
31
  generatePassword: (password) => {
32
32
  return crypto
33
33
  .pbkdf2Sync(password, module.exports.getSalt(), iterationsCount, keyLen, 'sha512')
34
34
  .toString('hex');
35
35
  },
36
36
 
37
- /**
38
- * Constant-time style comparison for provided password vs stored hash.
39
- */
37
+ /** Constant-time style comparison for provided password vs stored hash. */
40
38
  validatePassword: (password, hash) => {
41
39
  const derived = crypto
42
40
  .pbkdf2Sync(password, module.exports.getSalt(), iterationsCount, keyLen, 'sha512')
package/fileSystem.js CHANGED
@@ -1,28 +1,40 @@
1
1
  'use strict';
2
2
 
3
- // Purpose: Simple sync file helpers for small config/state blobs.
3
+ // Purpose: Simple sync file helpers that resolve relative paths from the service root (process.cwd()).
4
4
 
5
5
  const fs = require('fs');
6
+ const path = require('path');
7
+
8
+ /** Resolve absolute path; if relative, resolve from process.cwd(). */
9
+ function resolvePath(fileName) {
10
+ return path.isAbsolute(fileName) ? fileName : path.resolve(process.cwd(), fileName);
11
+ }
6
12
 
7
13
  module.exports = {
8
14
  /**
9
15
  * Save data as JSON into a file (overwrites if exists).
16
+ * Ensures parent directory exists.
10
17
  */
11
18
  saveFile: (fileName, data) => {
12
- fs.writeFileSync(fileName, JSON.stringify(data));
19
+ const p = resolvePath(fileName);
20
+ fs.mkdirSync(path.dirname(p), { recursive: true });
21
+ fs.writeFileSync(p, JSON.stringify(data));
13
22
  },
14
23
 
15
24
  /**
16
25
  * Read text content from a file using UTF-8.
26
+ * Relative paths are resolved from process.cwd().
17
27
  */
18
28
  readFile: (fileName) => {
19
- return fs.readFileSync(fileName, 'utf-8');
29
+ const p = resolvePath(fileName);
30
+ return fs.readFileSync(p, 'utf-8');
20
31
  },
21
32
 
22
33
  /**
23
34
  * Remove a file from the filesystem.
24
35
  */
25
36
  removeFile: (fileName) => {
26
- fs.unlinkSync(fileName);
37
+ const p = resolvePath(fileName);
38
+ fs.unlinkSync(p);
27
39
  }
28
40
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "b2b-platform-utils",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Shared utilities for Node.js microservices: errors map, local cache, logger, numbers, dates, filesystem, media optimization, paginator, slugger, crypto wrapper, sanitize HTML, sorting.",
5
5
  "type": "commonjs",
6
6
  "license": "KingSizer",
@@ -22,4 +22,4 @@
22
22
  "moment": "^2.30.1",
23
23
  "sharp": "^0.33.0"
24
24
  }
25
- }
25
+ }