concurrency.js 0.0.2 → 0.0.3-beta

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.
@@ -0,0 +1,38 @@
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/concurrency.js
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
+
22
+ describe('test-.js::concurrency.js: [Test A] Test Suite for a simple promise', function () {
23
+
24
+ it('[Test A] Test for promise tests', async function (done) {
25
+ var result;
26
+ function testPromise(resolve, reject) {
27
+ return new Promise(function (resolve, reject) {
28
+ resolve({ msg: "testing" });
29
+ });
30
+ }
31
+ result = await testPromise();
32
+ console.log(result);
33
+ expect(result.msg).to.equal("testing");
34
+ // done();
35
+ });
36
+
37
+ });
38
+
@@ -0,0 +1,59 @@
1
+ // /**
2
+ // *
3
+ // * Package: concurrency.js
4
+ // * Author: Ganesh B
5
+ // * Description: npm module to work with concurrency - worker threads and worker processes easily using simple functions and script files
6
+ // * Install: npm i --save
7
+ // * Github: https://github.com/ganeshkbhat/concurrency
8
+ // * npmjs Link: https://www.npmjs.com/package/concurrency.js
9
+ // * File: test.demos.threads.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
+ // const path = require("path");
21
+ // const { _concurrencyThreads } = require("../index.js");
22
+
23
+
24
+ // describe('test-.js::concurrency.js: [Test A] Test Suite for concurrency.js .threads in main repo directory', function () {
25
+
26
+ // it('[Test A] Test for threads ', function (done) {
27
+ // var responses;
28
+ // _concurrencyThreads(__filename, {
29
+ // data: {
30
+ // message: "Testing data",
31
+ // url: "https://www.google.com"
32
+ // },
33
+ // childData: "Testing child data"
34
+ // }).then((d) => {
35
+ // console.log(JSON.stringify(d));
36
+ // responses = d;
37
+ // done();
38
+ // });
39
+ // expect(responses).to.equal({
40
+ // "message": [
41
+ // { "pid": 11660, "message": "\"Hello from child. - Thread: 11660", "threadId": 1 },
42
+ // {
43
+ // "closeChild": true,
44
+ // "pid": 11660,
45
+ // "childMessageData": [
46
+ // { "pid": 11660, "message": "Hello, world! - Server: 11660" },
47
+ // { "closeChild": true }],
48
+ // "result": [],
49
+ // "threadId": 1
50
+ // }
51
+ // ],
52
+ // "result": []
53
+ // });
54
+ // done();
55
+ // });
56
+
57
+ // });
58
+
59
+
@@ -0,0 +1,68 @@
1
+ // /**
2
+ // *
3
+ // * Package: concurrency.js
4
+ // * Author: Ganesh B
5
+ // * Description: npm module to work with concurrency - worker threads and worker processes easily using simple functions and script files
6
+ // * Install: npm i --save
7
+ // * Github: https://github.com/ganeshkbhat/concurrency
8
+ // * npmjs Link: https://www.npmjs.com/package/concurrency.js
9
+ // * File: test.demos.threads.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
+ // const path = require("path");
21
+ // const { _concurrencyThreads } = require("../index.js");
22
+
23
+ // describe('test-.js::concurrency.js: [Test A] Test Suite for concurrency.js .threads in main repo directory', function () {
24
+
25
+ // it('[Test A] Test for ', function (done) {
26
+ // var responses;
27
+ // let threads = _concurrencyThreadsAsync("C:\\Users\\GB\\Documents\\projects\\requireurl\\concurrency\\demos\\demos.threads.js", {
28
+ // data: {
29
+ // message: "Testing parent data",
30
+ // url: "https://www.google.com"
31
+ // }, childData: "Test data from child"
32
+ // });
33
+
34
+ // /**
35
+ // {
36
+ // "message":[
37
+ // {"pid":3764,"message":"\"Hello from child. - Thread: 3764","threadId":1},
38
+ // {"closeChild":true,"pid":3764,
39
+ // "childMessageData":[
40
+ // {"pid":3764,"message":"Hello, world! - Server: 3764"},
41
+ // {"closeChild":true}
42
+ // ],
43
+ // "result":[],
44
+ // "threadId":1
45
+ // }
46
+ // ],
47
+ // "result":[]
48
+ // }
49
+ // */
50
+
51
+ // expect(threads).to.equal({
52
+ // "message": [
53
+ // { "pid": 3764, "message": "\"Hello from child. - Thread: 3764", "threadId": 1 },
54
+ // {
55
+ // "closeChild": true, "pid": 3764,
56
+ // "childMessageData": [
57
+ // { "pid": 3764, "message": "Hello, world! - Server: 3764" },
58
+ // { "closeChild": true }
59
+ // ],
60
+ // "result": [],
61
+ // "threadId": 1
62
+ // }],
63
+ // "result": []
64
+ // });
65
+ // done();
66
+ // });
67
+ // });
68
+
@@ -1,97 +0,0 @@
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;