concurrency.js 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Krishnamurthy G B
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # concurrency
2
+ npm module to work with concurrency - worker threads and worker processes easily using simple functions and script files
package/index.js ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ *
3
+ * Package:
4
+ * Author: Ganesh B
5
+ * Description:
6
+ * Install: npm i --save
7
+ * Github: https://github.com/ganeshkbhat/concurrency
8
+ * npmjs Link: https://www.npmjs.com/package/
9
+ * File: index.js
10
+ * File Description:
11
+ *
12
+ */
13
+
14
+ /* eslint no-console: 0 */
15
+
16
+ 'use strict';
17
+
18
+
package/index.mjs ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ *
3
+ * Package:
4
+ * Author: Ganesh B
5
+ * Description:
6
+ * Install: npm i --save
7
+ * Github: https://github.com/ganeshkbhat/concurrency
8
+ * npmjs Link: https://www.npmjs.com/package/
9
+ * File: index.js
10
+ * File Description:
11
+ *
12
+ */
13
+
14
+ /* eslint no-console: 0 */
15
+
16
+ 'use strict';
17
+
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "concurrency.js",
3
+ "version": "0.0.1",
4
+ "description": "npm module to work with concurrency - worker threads and worker processes easily using simple functions and script files",
5
+ "main": "index.js",
6
+ "directories": {
7
+ "test": "test"
8
+ },
9
+ "exports": {
10
+ "require": "./index.js",
11
+ "import": "./index.mjs"
12
+ },
13
+ "devDependencies": {
14
+ "chai": "^4.3.6",
15
+ "mocha": "^10.1.0",
16
+ "nsp": "^3.2.1",
17
+ "nsp-api": "^1.0.3",
18
+ "safe-regex": "^2.1.1",
19
+ "sinon": "^14.0.1",
20
+ "unimported": "^1.22.0"
21
+ },
22
+ "scripts": {
23
+ "test": "echo \"Error: no test specified\" && exit 1"
24
+ },
25
+ "author": "Ganesh B",
26
+ "license": "MIT"
27
+ }
@@ -0,0 +1,97 @@
1
+ /**
2
+ *
3
+ * Package:
4
+ * Author: Ganesh B
5
+ * Description:
6
+ * Install: npm i --save
7
+ * Github: https://github.com/ganeshkbhat/concurrency
8
+ * npmjs Link: https://www.npmjs.com/package/
9
+ * File: index.js
10
+ * File Description:
11
+ *
12
+ */
13
+
14
+ /* eslint no-console: 0 */
15
+
16
+ 'use strict';
17
+
18
+
19
+ const path = require('path');
20
+ const fs = require('fs');
21
+
22
+
23
+ /**
24
+ *
25
+ *
26
+ * @param {*} module_name
27
+ * @return {*}
28
+ */
29
+ function _getRequireOrImport(module_name) {
30
+ if (process.versions.node.split('.')[0] > "14") {
31
+ return import(module_name);
32
+ }
33
+ return require(module_name);
34
+ }
35
+
36
+ /**
37
+ *
38
+ *
39
+ * @param {*} data
40
+ * @param {*} options
41
+ */
42
+ function _concurrencyThreads(filenameOrData, options) {
43
+ const { Worker } = _getRequireOrImport('worker_threads');
44
+ // const worker = new Worker('./worker-threads.js');
45
+ const worker = new Worker(filename);
46
+ if (!data.url) {
47
+ throw new Error("[require-urls] index.js: URL not present in data for fetch.js");
48
+ }
49
+
50
+ if (!data.callback) {
51
+ data.callback = function (contents, parentPort) {
52
+ const { get } = (options.protocol === "https") ? require("https") : require("http");
53
+ get(contents.url, (res) => {
54
+ let result = '';
55
+ res.on('data', (chunk) => result += chunk);
56
+ res.on('end', () => {
57
+ parentPort.postMessage(result);
58
+ });
59
+ }).on('error', (err) => parentPort.postMessage(err));
60
+ }.bind(null, null, null, data, options)
61
+ }
62
+
63
+ worker.postMessage({ ...data });
64
+ worker.on('message', function (result) {
65
+ return result;
66
+ });
67
+ }
68
+
69
+ function _concurrencyProcesses(filenameOrData, options) {
70
+ const { fork } = _getRequireOrImport('child_process');
71
+ const child = fork(filenameOrData);
72
+
73
+ if (!data.url) {
74
+ // throw new Error("[require-urls] index.js: URL not present in data for fetch.js");
75
+ }
76
+
77
+ if (!data.callback) {
78
+ data.callback = function (contents, processFork) {
79
+ const { get } = (options.protocol === "https") ? require("https") : require("http");
80
+ get(contents.url, (res) => {
81
+ let result = '';
82
+ res.on('data', (chunk) => result += chunk);
83
+ res.on('end', () => {
84
+ processFork.send(result);
85
+ });
86
+ }).on('error', (err) => processFork.send(err));
87
+ }.bind(null, null, null, data, options)
88
+ }
89
+
90
+ child.on('message', function (result) {
91
+ return result;
92
+ });
93
+ child.send({ ...data });
94
+ }
95
+
96
+ module.exports._concurrencyThreads = _concurrencyThreads;
97
+ module.exports._concurrencyProcesses = _concurrencyProcesses;
@@ -0,0 +1,57 @@
1
+ /**
2
+ *
3
+ * Package:
4
+ * Author: Ganesh B
5
+ * Description:
6
+ * Install: npm i --save
7
+ * Github: https://github.com/ganeshkbhat/concurrency
8
+ * npmjs Link: https://www.npmjs.com/package/
9
+ * File: index.js
10
+ * File Description:
11
+ *
12
+ */
13
+
14
+ /* eslint no-console: 0 */
15
+
16
+ 'use strict';
17
+
18
+
19
+ process.on('message', function (contents) {
20
+ return contents.callback(contents);
21
+ }.bind(contents, process));
22
+
23
+ // setInterval(() => {
24
+ // process.send("Message from parent:");
25
+ // }, 1000);
26
+ //
27
+ // process.on('exit', function (code) {
28
+ // setTimeout(function () {
29
+ // console.log("This will not run");
30
+ // }, 0);
31
+ // console.log('About to exit with code:', code);
32
+ // });
33
+ //
34
+ //
35
+ // // Printing to console
36
+ // process.stdout.write("Hello World!" + "\n");
37
+ //
38
+ // // Reading passed parameter
39
+ // process.argv.forEach(function(val, index, array) {
40
+ // console.log(index + ': ' + val);
41
+ // });
42
+ //
43
+ // // Getting executable path
44
+ // console.log(process.execPath);
45
+ //
46
+ // // Platform Information
47
+ // console.log(process.platform);
48
+ //
49
+ // // Print the current directory
50
+ // console.log('Current directory: ' + process.cwd());
51
+ //
52
+ // // Print the process version
53
+ // console.log('Current version: ' + process.version);
54
+ //
55
+ // // Print the memory usage
56
+ // console.log(process.memoryUsage());
57
+ //
@@ -0,0 +1,23 @@
1
+ /**
2
+ *
3
+ * Package:
4
+ * Author: Ganesh B
5
+ * Description:
6
+ * Install: npm i --save
7
+ * Github: https://github.com/ganeshkbhat/concurrency
8
+ * npmjs Link: https://www.npmjs.com/package/
9
+ * File: index.js
10
+ * File Description:
11
+ *
12
+ */
13
+
14
+ /* eslint no-console: 0 */
15
+
16
+ 'use strict';
17
+
18
+
19
+ const { parentPort } = require("worker_threads");
20
+
21
+ parentPort.on("message", function (contents) {
22
+ return contents.callback(contents, parentPort);
23
+ }.bind(contents, parentPort));
@@ -0,0 +1,44 @@
1
+ // /**
2
+ // *
3
+ // * Package:
4
+ // * Author: Ganesh B
5
+ // * Description:
6
+ // * Install: npm i --save
7
+ // * Github: https://github.com/ganeshkbhat/concurrency
8
+ // * npmjs Link: https://www.npmjs.com/package/
9
+ // * File: index.js
10
+ // * File Description:
11
+ // *
12
+ // */
13
+
14
+ // /* eslint no-console: 0 */
15
+
16
+ // 'use strict';
17
+
18
+
19
+ // const expect = require('chai').expect;
20
+
21
+ // describe('test-.mjs::require-urls: Test Suite for require-urls Files', function() {
22
+
23
+
24
+ // before(async function(){
25
+ //
26
+ // });
27
+
28
+
29
+ // describe ('test-.js::require-urls: [Test A] Test Suite for require-urls in main repo directory', function() {
30
+ // // it('[Test A] Test for ', function(done){
31
+ // // expect(200).to.equal(200);
32
+ // // done();
33
+ // // });
34
+
35
+ // it('[Test A] Test for ', function(done) {
36
+ // expect(100).to.equal(100);
37
+ // done();
38
+ // });
39
+
40
+ // });
41
+
42
+
43
+ // });
44
+