bajo-extra 2.7.0 → 2.8.0

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 CHANGED
@@ -4,6 +4,7 @@ import path from 'path'
4
4
  import { Readable } from 'stream'
5
5
  import numbro from 'numbro'
6
6
  import sharp from 'sharp'
7
+ import lockfile from '@bybrave/proper-lockfile2'
7
8
 
8
9
  /**
9
10
  * Plugin factory.
@@ -692,6 +693,50 @@ async function factory (pkgName) {
692
693
  }
693
694
  }
694
695
  }
696
+
697
+ /**
698
+ * Acquire a lock on a specified file to prevent concurrent access.
699
+ *
700
+ * @param {string} file - The path to the file to lock. If no path found, it will be saved in `{tmpDir}`. Can be in many formats, see {@link https://ardhi.github.io/bajo/App.html#getPluginFile|getPluginFile} for details.
701
+ * @param {Object} [options={}] - The options for locking the file.
702
+ * @returns {Promise<function>} - A promise that resolves to the lock release function.
703
+ */
704
+ lock = async (file, options = {}) => {
705
+ const { ensureFile = true, silent = true } = options
706
+ const { fs } = this.app.lib
707
+ options.fs = fs
708
+ try {
709
+ if (path.basename(file) === file) file = `${this.app.bajo.dir.tmp}/${file}`
710
+ else file = this.app.getPluginFile(file)
711
+ if (ensureFile) await fs.ensureFile(file)
712
+ return await lockfile.lock(file, options)
713
+ } catch (err) {
714
+ if (silent) return null
715
+ throw err
716
+ }
717
+ }
718
+
719
+ /**
720
+ * Unlock a specified file that was previously locked, allowing concurrent access again.
721
+ * @param {string} file - The path to the file to unlock. If no path found, it will be saved in `{tmpDir}`. Can be in many formats, see {@link https://ardhi.github.io/bajo/App.html#getPluginFile|getPluginFile} for details.
722
+ * @param {Object} [options={}] - The options for unlocking the file.
723
+ * @returns {Promise<function>} - A promise that resolves to the lock release function.
724
+ */
725
+ unlock = async (file, options = {}) => {
726
+ const { silent = true } = options
727
+ const { fs } = this.app.lib
728
+ options.fs = fs
729
+ try {
730
+ if (path.basename(file) === file) file = `${this.app.bajo.dir.tmp}/${file}`
731
+ else file = this.app.getPluginFile(file)
732
+ if (!fs.existsSync(file)) return true
733
+ await lockfile.unlock(file)
734
+ return true
735
+ } catch (err) {
736
+ if (silent) return null
737
+ throw err
738
+ }
739
+ }
695
740
  }
696
741
 
697
742
  return BajoExtra
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo-extra",
3
- "version": "2.7.0",
3
+ "version": "2.8.0",
4
4
  "description": "Extra package for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -31,16 +31,17 @@
31
31
  },
32
32
  "homepage": "https://github.com/ardhi/bajo-extra#readme",
33
33
  "dependencies": {
34
+ "@bybrave/proper-lockfile2": "^5.0.0",
34
35
  "async": "^3.2.6",
35
36
  "bcrypt": "^6.0.0",
36
- "cheerio": "^1.1.2",
37
- "fast-jwt": "^6.1.0",
38
- "fast-xml-parser": "^5.3.3",
37
+ "cheerio": "^1.2.0",
38
+ "fast-jwt": "^6.2.4",
39
+ "fast-xml-parser": "^5.10.1",
39
40
  "numbro": "^2.5.0",
40
41
  "performant-array-to-tree": "^1.11.0",
41
42
  "sharp": "^0.34.5",
42
43
  "short-crypt": "^4.0.0",
43
- "undici": "^7.16.0"
44
+ "undici": "^8.9.0"
44
45
  },
45
46
  "devDependencies": {
46
47
  "chai": "^6.2.2",
package/wiki/CHANGES.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-07-28
4
+
5
+ - [2.8.0] Add `lock()` & `unlock()` for file locking
6
+
3
7
  ## 2026-06-28
4
8
 
5
9
  - [2.7.0] Add documentations
@@ -7,26 +11,26 @@
7
11
 
8
12
  ## 2026-06-12
9
13
 
10
- - [2.6.0] Necessary updates to ```bajo@2.18.0``` specs
14
+ - [2.6.0] Necessary updates to `bajo@2.18.0` specs
11
15
 
12
16
  ## 2026-05-03
13
17
 
14
- - [2.5.0] Add ```isHtmlLink```
18
+ - [2.5.0] Add `isHtmlLink`
15
19
 
16
20
  ## 2026-05-02
17
21
 
18
- - [2.4.0] Add ```sharp``` as library
19
- - [2.4.0] Add ```thumbnailSizes()```
20
- - [2.4.0] Add ```createThumbnail()```
22
+ - [2.4.0] Add `sharp` as library
23
+ - [2.4.0] Add `thumbnailSizes()`
24
+ - [2.4.0] Add `createThumbnail()`
21
25
 
22
26
  ## 2026-04-25
23
27
 
24
- - [2.3.0] Add ```isSha256()```
28
+ - [2.3.0] Add `isSha256()`
25
29
 
26
30
  ## 2026-01-16
27
31
 
28
- - [2.2.1] Bug fix on ```fetchUrl()``` for url with query string
32
+ - [2.2.1] Bug fix on `fetchUrl()` for url with query string
29
33
 
30
34
  ## 2025-12-28
31
35
 
32
- - [2.2.0] Ported to ```bajo@2.2.x``` specs
36
+ - [2.2.0] Ported to `bajo@2.2.x` specs