@lakutata/mutex 3.0.0-beta.5 → 3.0.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.
@@ -34,7 +34,17 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.loadDriver = loadDriver;
37
+ const core_1 = require("@lakutata/core");
37
38
  const MutexDriverNotFoundException_js_1 = require("../exceptions/MutexDriverNotFoundException.js");
39
+ /**
40
+ * The client packages of the drivers (optional dependencies), imported by their names written out: the bundlers follow
41
+ * them (a name given at run time is not bundled, the bundle has no node_modules to load it from)
42
+ */
43
+ const DRIVERS = {
44
+ 'redis': () => Promise.resolve().then(() => __importStar(require('redis'))),
45
+ 'pg': () => Promise.resolve().then(() => __importStar(require('pg'))),
46
+ 'mysql2/promise': () => Promise.resolve().then(() => __importStar(require('mysql2/promise')))
47
+ };
38
48
  /**
39
49
  * Whether an error reports the package itself as missing
40
50
  * @param error
@@ -52,13 +62,20 @@ function isPackageMissing(error, packageName) {
52
62
  * @param packageName
53
63
  */
54
64
  async function loadDriver(member, packageName) {
65
+ let missing;
55
66
  try {
56
- const driver = await Promise.resolve(`${packageName}`).then(s => __importStar(require(s)));
67
+ const driver = await (DRIVERS[packageName] ?? (() => Promise.resolve(`${packageName}`).then(s => __importStar(require(s)))))();
57
68
  return (driver[member] === undefined && driver.default ? driver.default : driver);
58
69
  }
59
70
  catch (e) {
60
71
  if (!isPackageMissing(e, packageName))
61
72
  throw e;
73
+ missing = e;
62
74
  }
63
- throw new MutexDriverNotFoundException_js_1.MutexDriverNotFoundException('Package "{packageName}" is required for this driver of the locks. Run "npm install {packageName}".', { packageName: packageName });
75
+ //A bundled application has the packages installed when it was bundled
76
+ const exception = (0, core_1.isPackaged)() ?
77
+ new MutexDriverNotFoundException_js_1.MutexDriverNotFoundException('Package "{packageName}" is required for this driver of the locks, and was not installed when the application was bundled: install it in the project, then bundle the application again.', { packageName: packageName }) :
78
+ new MutexDriverNotFoundException_js_1.MutexDriverNotFoundException('Package "{packageName}" is required for this driver of the locks. Run "npm install {packageName}".', { packageName: packageName });
79
+ exception.cause = missing;
80
+ throw exception;
64
81
  }
@@ -1,4 +1,14 @@
1
+ import { isPackaged } from '@lakutata/core';
1
2
  import { MutexDriverNotFoundException } from '../exceptions/MutexDriverNotFoundException.js';
3
+ /**
4
+ * The client packages of the drivers (optional dependencies), imported by their names written out: the bundlers follow
5
+ * them (a name given at run time is not bundled, the bundle has no node_modules to load it from)
6
+ */
7
+ const DRIVERS = {
8
+ 'redis': () => import('redis'),
9
+ 'pg': () => import('pg'),
10
+ 'mysql2/promise': () => import('mysql2/promise')
11
+ };
2
12
  /**
3
13
  * Whether an error reports the package itself as missing
4
14
  * @param error
@@ -16,13 +26,20 @@ function isPackageMissing(error, packageName) {
16
26
  * @param packageName
17
27
  */
18
28
  export async function loadDriver(member, packageName) {
29
+ let missing;
19
30
  try {
20
- const driver = await import(packageName);
31
+ const driver = await (DRIVERS[packageName] ?? (() => import(packageName)))();
21
32
  return (driver[member] === undefined && driver.default ? driver.default : driver);
22
33
  }
23
34
  catch (e) {
24
35
  if (!isPackageMissing(e, packageName))
25
36
  throw e;
37
+ missing = e;
26
38
  }
27
- throw new MutexDriverNotFoundException('Package "{packageName}" is required for this driver of the locks. Run "npm install {packageName}".', { packageName: packageName });
39
+ //A bundled application has the packages installed when it was bundled
40
+ const exception = isPackaged() ?
41
+ new MutexDriverNotFoundException('Package "{packageName}" is required for this driver of the locks, and was not installed when the application was bundled: install it in the project, then bundle the application again.', { packageName: packageName }) :
42
+ new MutexDriverNotFoundException('Package "{packageName}" is required for this driver of the locks. Run "npm install {packageName}".', { packageName: packageName });
43
+ exception.cause = missing;
44
+ throw exception;
28
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lakutata/mutex",
3
- "version": "3.0.0-beta.5",
3
+ "version": "3.0.0",
4
4
  "description": "The locks of the lakutata framework: mutual exclusion between the instances and the processes of an application, over memory, files, Redis, PostgreSQL or MySQL, held by tokens, renewed while they are held.",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/exports/Mutex.js",
@@ -30,7 +30,7 @@
30
30
  "test:stores": "sh test-env/run.sh"
31
31
  },
32
32
  "peerDependencies": {
33
- "@lakutata/core": "3.0.0-beta.5",
33
+ "@lakutata/core": "3.0.0",
34
34
  "mysql2": "^3.0.0",
35
35
  "pg": "^8.0.0",
36
36
  "redis": "^4.6.0 || ^5.0.0"