xcraft-core-utils 4.3.8 → 4.3.9

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.
@@ -1,135 +1,135 @@
1
- 'use strict';
2
-
3
- // create a unique, global symbol name
4
- // -----------------------------------
5
- const RUNNER_INSTANCE_KEY = Symbol.for('xcraft-core-utils.runnerInstance');
6
- const watt = require('gigawatts');
7
-
8
- class Runner {
9
- constructor() {
10
- this.totalRunning = 0;
11
- this.runningsGroups = [];
12
- this.runnings = {};
13
- this.run = this.run.bind(this);
14
- watt.wrapAll(this);
15
- }
16
-
17
- run(jobQueue) {
18
- //must be postponed?
19
- if (jobQueue.waitOn.length > 0) {
20
- const mustWait = this.runningsGroups.some((q) =>
21
- jobQueue.waitOn.includes(q)
22
- );
23
- if (mustWait) {
24
- if (!jobQueue.maxAttemptReached) {
25
- jobQueue.attempt++;
26
- setTimeout(this.run, jobQueue.waitDelay, jobQueue);
27
- return;
28
- } else {
29
- jobQueue.attempt = 0;
30
- }
31
- }
32
- }
33
-
34
- //queue is draining?
35
- if (jobQueue.running > 0 && jobQueue.waiting.size === 0) {
36
- return;
37
- }
38
-
39
- //jobqueue releasing
40
- for (let x = jobQueue.running; x < jobQueue.parallelLimit; x++) {
41
- const nextEntry = jobQueue.waiting.entries().next();
42
- if (nextEntry.done) {
43
- //we can leave, nothing is waiting us
44
- break;
45
- }
46
-
47
- //remove the jobEntry from queue
48
- const jobEntry = Object.assign({}, nextEntry.value);
49
- jobQueue.waiting.delete(jobEntry[0]);
50
-
51
- //log for journal inspections
52
- if (this.totalRunning === 0 && jobQueue.parallelLimit > 1) {
53
- jobQueue._dbg(`system are running new jobs`);
54
- }
55
-
56
- //priority tracking
57
- if (jobQueue.priorityGroup !== 'default' && jobQueue.running === 0) {
58
- if (!this.runnings[jobQueue.priorityGroup]) {
59
- this.runnings[jobQueue.priorityGroup] = {};
60
- }
61
- this.runnings[jobQueue.priorityGroup][jobQueue.name] = true;
62
- //update runnings group
63
- this.runningsGroups = Object.entries(this.runnings)
64
- .filter((e) => Object.values(e[1]).some((r) => r === true))
65
- .map((e) => e[0]);
66
- }
67
-
68
- //update counters
69
- jobQueue.running++;
70
- this.totalRunning++;
71
-
72
- //trigger job start on queue runner
73
- jobQueue.runner(jobEntry[1], (err) => {
74
- if (err) {
75
- jobQueue.err(err);
76
- }
77
- //end callback
78
-
79
- jobQueue.notify(jobQueue.runningSamples);
80
-
81
- //update counter
82
- jobQueue.running--;
83
- this.totalRunning--;
84
-
85
- //priority tracking
86
- if (jobQueue.priorityGroup !== 'default' && jobQueue.running === 0) {
87
- this.runnings[jobQueue.priorityGroup][jobQueue.name] = false;
88
- //update runnings group
89
- this.runningsGroups = Object.entries(this.runnings)
90
- .filter((e) => Object.values(e[1]).some((r) => r === true))
91
- .map((e) => e[0]);
92
- }
93
-
94
- //log
95
- if (this.totalRunning === 0 && jobQueue.waiting.size === 0) {
96
- jobQueue._dbg(`no more jobs running`);
97
- }
98
-
99
- //schedule a new run for this queue if needed
100
- if (jobQueue.waiting.size > 0) {
101
- setTimeout(this.run, 0, jobQueue);
102
- }
103
- });
104
- }
105
- }
106
- }
107
-
108
- // check if the global object has this symbol
109
- // add it if it does not have the symbol, yet
110
- // ------------------------------------------
111
- const globalSymbols = Object.getOwnPropertySymbols(global);
112
- const hasInstance = globalSymbols.indexOf(RUNNER_INSTANCE_KEY) > -1;
113
- if (!hasInstance) {
114
- global[RUNNER_INSTANCE_KEY] = new Runner();
115
- }
116
-
117
- // define the singleton API
118
- // ------------------------
119
-
120
- const singleton = {};
121
-
122
- Object.defineProperty(singleton, 'instance', {
123
- get: function () {
124
- return global[RUNNER_INSTANCE_KEY];
125
- },
126
- });
127
-
128
- // ensure the API is never changed
129
- // -------------------------------
130
-
131
- Object.freeze(singleton);
132
-
133
- // export the singleton API only
134
- // -----------------------------
135
- module.exports = singleton;
1
+ 'use strict';
2
+
3
+ // create a unique, global symbol name
4
+ // -----------------------------------
5
+ const RUNNER_INSTANCE_KEY = Symbol.for('xcraft-core-utils.runnerInstance');
6
+ const watt = require('gigawatts');
7
+
8
+ class Runner {
9
+ constructor() {
10
+ this.totalRunning = 0;
11
+ this.runningsGroups = [];
12
+ this.runnings = {};
13
+ this.run = this.run.bind(this);
14
+ watt.wrapAll(this);
15
+ }
16
+
17
+ run(jobQueue) {
18
+ //must be postponed?
19
+ if (jobQueue.waitOn.length > 0) {
20
+ const mustWait = this.runningsGroups.some((q) =>
21
+ jobQueue.waitOn.includes(q)
22
+ );
23
+ if (mustWait) {
24
+ if (!jobQueue.maxAttemptReached) {
25
+ jobQueue.attempt++;
26
+ setTimeout(this.run, jobQueue.waitDelay, jobQueue);
27
+ return;
28
+ } else {
29
+ jobQueue.attempt = 0;
30
+ }
31
+ }
32
+ }
33
+
34
+ //queue is draining?
35
+ if (jobQueue.running > 0 && jobQueue.waiting.size === 0) {
36
+ return;
37
+ }
38
+
39
+ //jobqueue releasing
40
+ for (let x = jobQueue.running; x < jobQueue.parallelLimit; x++) {
41
+ const nextEntry = jobQueue.waiting.entries().next();
42
+ if (nextEntry.done) {
43
+ //we can leave, nothing is waiting us
44
+ break;
45
+ }
46
+
47
+ //remove the jobEntry from queue
48
+ const jobEntry = Object.assign({}, nextEntry.value);
49
+ jobQueue.waiting.delete(jobEntry[0]);
50
+
51
+ //log for journal inspections
52
+ if (this.totalRunning === 0 && jobQueue.parallelLimit > 1) {
53
+ jobQueue._dbg(`system are running new jobs`);
54
+ }
55
+
56
+ //priority tracking
57
+ if (jobQueue.priorityGroup !== 'default' && jobQueue.running === 0) {
58
+ if (!this.runnings[jobQueue.priorityGroup]) {
59
+ this.runnings[jobQueue.priorityGroup] = {};
60
+ }
61
+ this.runnings[jobQueue.priorityGroup][jobQueue.name] = true;
62
+ //update runnings group
63
+ this.runningsGroups = Object.entries(this.runnings)
64
+ .filter((e) => Object.values(e[1]).some((r) => r === true))
65
+ .map((e) => e[0]);
66
+ }
67
+
68
+ //update counters
69
+ jobQueue.running++;
70
+ this.totalRunning++;
71
+
72
+ //trigger job start on queue runner
73
+ jobQueue.runner(jobEntry[1], (err) => {
74
+ if (err) {
75
+ jobQueue.err(err);
76
+ }
77
+ //end callback
78
+
79
+ jobQueue.notify(jobQueue.runningSamples);
80
+
81
+ //update counter
82
+ jobQueue.running--;
83
+ this.totalRunning--;
84
+
85
+ //priority tracking
86
+ if (jobQueue.priorityGroup !== 'default' && jobQueue.running === 0) {
87
+ this.runnings[jobQueue.priorityGroup][jobQueue.name] = false;
88
+ //update runnings group
89
+ this.runningsGroups = Object.entries(this.runnings)
90
+ .filter((e) => Object.values(e[1]).some((r) => r === true))
91
+ .map((e) => e[0]);
92
+ }
93
+
94
+ //log
95
+ if (this.totalRunning === 0 && jobQueue.waiting.size === 0) {
96
+ jobQueue._dbg(`no more jobs running`);
97
+ }
98
+
99
+ //schedule a new run for this queue if needed
100
+ if (jobQueue.waiting.size > 0) {
101
+ setTimeout(this.run, 0, jobQueue);
102
+ }
103
+ });
104
+ }
105
+ }
106
+ }
107
+
108
+ // check if the global object has this symbol
109
+ // add it if it does not have the symbol, yet
110
+ // ------------------------------------------
111
+ const globalSymbols = Object.getOwnPropertySymbols(global);
112
+ const hasInstance = globalSymbols.indexOf(RUNNER_INSTANCE_KEY) > -1;
113
+ if (!hasInstance) {
114
+ global[RUNNER_INSTANCE_KEY] = new Runner();
115
+ }
116
+
117
+ // define the singleton API
118
+ // ------------------------
119
+
120
+ const singleton = {};
121
+
122
+ Object.defineProperty(singleton, 'instance', {
123
+ get: function () {
124
+ return global[RUNNER_INSTANCE_KEY];
125
+ },
126
+ });
127
+
128
+ // ensure the API is never changed
129
+ // -------------------------------
130
+
131
+ Object.freeze(singleton);
132
+
133
+ // export the singleton API only
134
+ // -----------------------------
135
+ module.exports = singleton;
package/lib/string.js CHANGED
@@ -1,15 +1,15 @@
1
- 'use strict';
2
-
3
- exports.camelcasify = function (str) {
4
- return str.replace(/(\.[a-z])/g, function (match) {
5
- return match.replace('.', '').toUpperCase();
6
- });
7
- };
8
-
9
- exports.capitalize = function (str) {
10
- return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
11
- };
12
-
13
- exports.jsify = (str) => {
14
- return str.replace(/-([a-z])/g, (m, g1) => g1.toUpperCase());
15
- };
1
+ 'use strict';
2
+
3
+ exports.camelcasify = function (str) {
4
+ return str.replace(/(\.[a-z])/g, function (match) {
5
+ return match.replace('.', '').toUpperCase();
6
+ });
7
+ };
8
+
9
+ exports.capitalize = function (str) {
10
+ return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
11
+ };
12
+
13
+ exports.jsify = (str) => {
14
+ return str.replace(/-([a-z])/g, (m, g1) => g1.toUpperCase());
15
+ };
package/lib/whereIs.js CHANGED
@@ -1,13 +1,13 @@
1
- const path = require('path');
2
- const fse = require('fs-extra');
3
-
4
- module.exports = function whereIs(bin) {
5
- var fullLocation = null;
6
-
7
- var exists = process.env.PATH.split(path.delimiter).some(function (location) {
8
- fullLocation = path.join(location, bin);
9
- return fse.existsSync(fullLocation);
10
- });
11
-
12
- return exists ? fullLocation : null;
13
- };
1
+ const path = require('path');
2
+ const fse = require('fs-extra');
3
+
4
+ module.exports = function whereIs(bin) {
5
+ var fullLocation = null;
6
+
7
+ var exists = process.env.PATH.split(path.delimiter).some(function (location) {
8
+ fullLocation = path.join(location, bin);
9
+ return fse.existsSync(fullLocation);
10
+ });
11
+
12
+ return exists ? fullLocation : null;
13
+ };
package/lib/yaml.js CHANGED
@@ -1,10 +1,10 @@
1
- 'use strict';
2
-
3
- var fs = require('fs');
4
-
5
- exports.fromFile = function (yamlFile) {
6
- var yaml = require('js-yaml');
7
-
8
- var data = fs.readFileSync(yamlFile, 'utf8');
9
- return yaml.safeLoad(data);
10
- };
1
+ 'use strict';
2
+
3
+ var fs = require('fs');
4
+
5
+ exports.fromFile = function (yamlFile) {
6
+ var yaml = require('js-yaml');
7
+
8
+ var data = fs.readFileSync(yamlFile, 'utf8');
9
+ return yaml.safeLoad(data);
10
+ };
package/package.json CHANGED
@@ -1,53 +1,53 @@
1
- {
2
- "name": "xcraft-core-utils",
3
- "version": "4.3.8",
4
- "description": "Xcraft utils",
5
- "main": "index.js",
6
- "engines": {
7
- "node": ">=14"
8
- },
9
- "scripts": {
10
- "test": "echo \"Error: no test specified\" && exit 1"
11
- },
12
- "keywords": [
13
- "xcraft",
14
- "utils",
15
- "helpers"
16
- ],
17
- "repository": {
18
- "type": "git",
19
- "url": "git+https://github.com/Xcraft-Inc/xcraft-core-utils.git"
20
- },
21
- "author": "Mathieu Schroeter",
22
- "license": "MIT",
23
- "dependencies": {
24
- "ansi-regex": "^2.1.1",
25
- "cli-color": "^1.2.0",
26
- "escape-regexp": "0.0.1",
27
- "escape-string-regexp": "^1.0.5",
28
- "figlet": "^1.2.1",
29
- "fs-extra": "^9.1.0",
30
- "gigawatts": "^4.0.1",
31
- "js-yaml": "^3.9.0",
32
- "linked-list": "^1.0.4",
33
- "lodash": "^4.17.20",
34
- "locks": "^0.2.2",
35
- "prop-types": "^15.7.2",
36
- "uuid": "^8.3.2",
37
- "xcraft-core-busclient": "^5.0.0",
38
- "xcraft-core-fs": "^2.0.0",
39
- "xcraft-core-log": "^2.0.0",
40
- "xcraft-core-utils": "^4.0.0"
41
- },
42
- "bugs": {
43
- "url": "https://github.com/Xcraft-Inc/xcraft-core-utils/issues"
44
- },
45
- "homepage": "https://github.com/Xcraft-Inc/xcraft-core-utils#readme",
46
- "devDependencies": {
47
- "should": "^11.2.1",
48
- "prettier": "2.0.4",
49
- "xcraft-dev-prettier": "^2.0.0",
50
- "xcraft-dev-rules": "^2.0.0"
51
- },
52
- "prettier": "xcraft-dev-prettier"
53
- }
1
+ {
2
+ "name": "xcraft-core-utils",
3
+ "version": "4.3.9",
4
+ "description": "Xcraft utils",
5
+ "main": "index.js",
6
+ "engines": {
7
+ "node": ">=14"
8
+ },
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "keywords": [
13
+ "xcraft",
14
+ "utils",
15
+ "helpers"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/Xcraft-Inc/xcraft-core-utils.git"
20
+ },
21
+ "author": "Mathieu Schroeter",
22
+ "license": "MIT",
23
+ "dependencies": {
24
+ "ansi-regex": "^2.1.1",
25
+ "cli-color": "^1.2.0",
26
+ "escape-regexp": "0.0.1",
27
+ "escape-string-regexp": "^1.0.5",
28
+ "figlet": "^1.2.1",
29
+ "fs-extra": "^9.1.0",
30
+ "gigawatts": "^4.0.1",
31
+ "js-yaml": "^3.9.0",
32
+ "linked-list": "^1.0.4",
33
+ "lodash": "^4.17.20",
34
+ "locks": "^0.2.2",
35
+ "prop-types": "^15.7.2",
36
+ "uuid": "^8.3.2",
37
+ "xcraft-core-busclient": "^5.0.0",
38
+ "xcraft-core-fs": "^2.0.0",
39
+ "xcraft-core-log": "^2.0.0",
40
+ "xcraft-core-utils": "^4.0.0"
41
+ },
42
+ "bugs": {
43
+ "url": "https://github.com/Xcraft-Inc/xcraft-core-utils/issues"
44
+ },
45
+ "homepage": "https://github.com/Xcraft-Inc/xcraft-core-utils#readme",
46
+ "devDependencies": {
47
+ "should": "^11.2.1",
48
+ "prettier": "2.0.4",
49
+ "xcraft-dev-prettier": "^2.0.0",
50
+ "xcraft-dev-rules": "^2.0.0"
51
+ },
52
+ "prettier": "xcraft-dev-prettier"
53
+ }
package/test/index.js CHANGED
@@ -1,68 +1,68 @@
1
- 'use strict';
2
-
3
- var should = require('should'); /* jshint ignore:line */
4
- var xUtils = require('../index.js');
5
-
6
- describe('xcraft-core-utils', function () {
7
- describe('#string#camelcasify ()', function () {
8
- var topics = [
9
- {
10
- in: '',
11
- out: '',
12
- },
13
- {
14
- in: 'foo.bar',
15
- out: 'fooBar',
16
- },
17
- {
18
- in: 'fooBar',
19
- out: 'fooBar',
20
- },
21
- {
22
- in: 'f.o.o.b.a.r',
23
- out: 'fOOBAR',
24
- },
25
- {
26
- in: 'fo.ob.ar',
27
- out: 'foObAr',
28
- },
29
- ];
30
-
31
- topics.forEach(function (item) {
32
- it('camelcasify ' + item.in, function () {
33
- xUtils.string.camelcasify(item.in).should.be.equal(item.out);
34
- });
35
- });
36
- });
37
-
38
- describe('#string#capitalize ()', function () {
39
- var strings = [
40
- {
41
- in: '',
42
- out: '',
43
- },
44
- {
45
- in: 'foobar',
46
- out: 'Foobar',
47
- },
48
- {
49
- in: 'FOOBAR',
50
- out: 'Foobar',
51
- },
52
- {
53
- in: 'fOOBAR',
54
- out: 'Foobar',
55
- },
56
- {
57
- in: 'Foobar',
58
- out: 'Foobar',
59
- },
60
- ];
61
-
62
- strings.forEach(function (str) {
63
- it('capitalize ' + str.in, function () {
64
- xUtils.string.capitalize(str.in).should.be.equal(str.out);
65
- });
66
- });
67
- });
68
- });
1
+ 'use strict';
2
+
3
+ var should = require('should'); /* jshint ignore:line */
4
+ var xUtils = require('../index.js');
5
+
6
+ describe('xcraft-core-utils', function () {
7
+ describe('#string#camelcasify ()', function () {
8
+ var topics = [
9
+ {
10
+ in: '',
11
+ out: '',
12
+ },
13
+ {
14
+ in: 'foo.bar',
15
+ out: 'fooBar',
16
+ },
17
+ {
18
+ in: 'fooBar',
19
+ out: 'fooBar',
20
+ },
21
+ {
22
+ in: 'f.o.o.b.a.r',
23
+ out: 'fOOBAR',
24
+ },
25
+ {
26
+ in: 'fo.ob.ar',
27
+ out: 'foObAr',
28
+ },
29
+ ];
30
+
31
+ topics.forEach(function (item) {
32
+ it('camelcasify ' + item.in, function () {
33
+ xUtils.string.camelcasify(item.in).should.be.equal(item.out);
34
+ });
35
+ });
36
+ });
37
+
38
+ describe('#string#capitalize ()', function () {
39
+ var strings = [
40
+ {
41
+ in: '',
42
+ out: '',
43
+ },
44
+ {
45
+ in: 'foobar',
46
+ out: 'Foobar',
47
+ },
48
+ {
49
+ in: 'FOOBAR',
50
+ out: 'Foobar',
51
+ },
52
+ {
53
+ in: 'fOOBAR',
54
+ out: 'Foobar',
55
+ },
56
+ {
57
+ in: 'Foobar',
58
+ out: 'Foobar',
59
+ },
60
+ ];
61
+
62
+ strings.forEach(function (str) {
63
+ it('capitalize ' + str.in, function () {
64
+ xUtils.string.capitalize(str.in).should.be.equal(str.out);
65
+ });
66
+ });
67
+ });
68
+ });
package/test/jobqueue.js CHANGED
@@ -1,33 +1,33 @@
1
- const xUtils = require('../index.js');
2
- //const watt = require('gigawatts');
3
- const jobs1 = [
4
- {id: 1, gen: 0},
5
- {id: 1, gen: 1},
6
- {id: 1, gen: 4},
7
- {id: 1, gen: 5},
8
- {id: 2, gen: 1},
9
- {id: 2, gen: 2},
10
- {id: 2, gen: 3},
11
- {id: 4, gen: 0},
12
- {id: 1, gen: 2},
13
- {id: 1, gen: 3},
14
- {id: 1, gen: 6},
15
- {id: 1, gen: 7},
16
- {id: 1, gen: 8},
17
- {id: 1, gen: 9},
18
- {id: 1, gen: 10},
19
- {id: 3, gen: 0},
20
- ];
21
-
22
- const runner = (job, done) => {
23
- setTimeout(() => {
24
- console.log(`[id:${job.id}] gen:${job.gen}`);
25
- done();
26
- }, 1000);
27
- };
28
-
29
- const queue = new xUtils.JobQueue('test', runner, 1);
30
-
31
- for (const j of jobs1.values()) {
32
- queue.push(j);
33
- }
1
+ const xUtils = require('../index.js');
2
+ //const watt = require('gigawatts');
3
+ const jobs1 = [
4
+ {id: 1, gen: 0},
5
+ {id: 1, gen: 1},
6
+ {id: 1, gen: 4},
7
+ {id: 1, gen: 5},
8
+ {id: 2, gen: 1},
9
+ {id: 2, gen: 2},
10
+ {id: 2, gen: 3},
11
+ {id: 4, gen: 0},
12
+ {id: 1, gen: 2},
13
+ {id: 1, gen: 3},
14
+ {id: 1, gen: 6},
15
+ {id: 1, gen: 7},
16
+ {id: 1, gen: 8},
17
+ {id: 1, gen: 9},
18
+ {id: 1, gen: 10},
19
+ {id: 3, gen: 0},
20
+ ];
21
+
22
+ const runner = (job, done) => {
23
+ setTimeout(() => {
24
+ console.log(`[id:${job.id}] gen:${job.gen}`);
25
+ done();
26
+ }, 1000);
27
+ };
28
+
29
+ const queue = new xUtils.JobQueue('test', runner, 1);
30
+
31
+ for (const j of jobs1.values()) {
32
+ queue.push(j);
33
+ }