@makano/rew 1.1.1 → 1.1.15
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/README.md +1 -1
- package/bin/rew +1 -1
- package/lib/rew/cli/cli.js +0 -2
- package/lib/rew/cli/run.js +5 -8
- package/lib/rew/cli/utils.js +1 -1
- package/lib/rew/const/opt.js +7 -0
- package/lib/rew/functions/import.js +34 -11
- package/lib/rew/functions/require.js +45 -0
- package/lib/rew/modules/context.js +13 -2
- package/lib/rew/pkgs/env.js +9 -0
- package/lib/rew/pkgs/modules/threads/worker.js +37 -0
- package/lib/rew/pkgs/threads.js +67 -0
- package/package.json +2 -6
package/README.md
CHANGED
package/bin/rew
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
const path = require('path');
|
|
3
3
|
const fs = require('fs');
|
|
4
|
-
const rew_mod = path.resolve(process.cwd(), '
|
|
4
|
+
const rew_mod = path.resolve(process.cwd(), 'snode_moduless/@makano/rew');
|
|
5
5
|
if(fs.existsSync(rew_mod)){
|
|
6
6
|
require(path.join(rew_mod, 'lib/rew/cli/cli.js'));
|
|
7
7
|
} else {
|
package/lib/rew/cli/cli.js
CHANGED
package/lib/rew/cli/run.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
1
|
const { run } = require('../main');
|
|
3
|
-
const { watch } = require('fs');
|
|
4
2
|
|
|
5
3
|
|
|
6
4
|
function exec(filePath){
|
|
@@ -8,13 +6,12 @@ function exec(filePath){
|
|
|
8
6
|
.context.module.imports;
|
|
9
7
|
}
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
process.on('message', ({ filePath, watch }) => {
|
|
9
|
+
const onmsg = ({ filePath, watch }) => {
|
|
13
10
|
const imports = exec(filePath);
|
|
14
11
|
if(watch){
|
|
15
12
|
process.send(imports);
|
|
16
|
-
process.exit();
|
|
17
|
-
} else {
|
|
18
|
-
process.exit();
|
|
19
13
|
}
|
|
20
|
-
|
|
14
|
+
process.off('message', onmsg);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
process.on('message', onmsg);
|
package/lib/rew/cli/utils.js
CHANGED
|
@@ -107,7 +107,7 @@ module.exports = {
|
|
|
107
107
|
const c = jsYaml.load(fs.readFileSync(confPath, { encoding: 'utf-8' }));
|
|
108
108
|
if(c.entry){
|
|
109
109
|
const r = path.resolve(root, c.entry);
|
|
110
|
-
const mod_path = path.resolve(root, '
|
|
110
|
+
const mod_path = path.resolve(root, 'snode_moduless/@makano/rew');
|
|
111
111
|
const mod_path_lib = path.join(mod_path, 'lib/rew/cli');
|
|
112
112
|
if(fs.existsSync(mod_path) && __dirname !== mod_path_lib){
|
|
113
113
|
const mod_path_utilsjs = path.join(mod_path_lib, '../main.js');
|
|
@@ -5,6 +5,9 @@ const { findPackage, getPackage } = require("../pkgs/pkgs");
|
|
|
5
5
|
const { existsSync, readFileSync } = require("fs");
|
|
6
6
|
const conf = require("../pkgs/conf");
|
|
7
7
|
const jsYaml = require("js-yaml");
|
|
8
|
+
const { execOptions } = require("../const/opt");
|
|
9
|
+
|
|
10
|
+
const cachedFiles = [];
|
|
8
11
|
|
|
9
12
|
const lookUpInOtherApps = (fullPath) => {
|
|
10
13
|
const con = conf({});
|
|
@@ -30,26 +33,45 @@ module.exports.imp = function (runPath, context) {
|
|
|
30
33
|
|
|
31
34
|
// console.log(typeof runPath);
|
|
32
35
|
|
|
33
|
-
|
|
36
|
+
const lookUp = () => {
|
|
34
37
|
const otherPath = lookUpInOtherApps(filename);
|
|
35
38
|
if(!otherPath) throw new Error('Module "'+filename+'" not found');
|
|
36
39
|
else filepath = otherPath;
|
|
37
40
|
}
|
|
38
41
|
|
|
42
|
+
const foundCache = cachedFiles.find(f => f.filepath == filepath);
|
|
43
|
+
|
|
44
|
+
if(!ispkg && foundCache){
|
|
45
|
+
exports = foundCache.exports;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if(!ispkg && !existsSync(filepath)){
|
|
49
|
+
if(Array.isArray(execOptions.resolveExtensions) && execOptions.resolveExtensions.length){
|
|
50
|
+
const resolve = execOptions.resolveExtensions.find(ext => typeof ext == "string" ? existsSync(filepath+ext) : existsSync(filepath+(ext.ext || '')));
|
|
51
|
+
if(resolve) {
|
|
52
|
+
filepath += typeof resolve == "string" ? resolve : resolve.ext;
|
|
53
|
+
if(typeof resolve == "object" && resolve.options){
|
|
54
|
+
if(resolve.options.type) type = resolve.options.type;
|
|
55
|
+
for(let i in resolve.options) options[i] = resolve.options[i];
|
|
56
|
+
}
|
|
57
|
+
} else lookUp();
|
|
58
|
+
} else lookUp();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const exec = (coptions = {}) => runPath(
|
|
62
|
+
filepath,
|
|
63
|
+
{ ...options, useContext: execOptions.sharedContext == false ? false : true, ...coptions },
|
|
64
|
+
execOptions.sharedContext == false ? {} : context,
|
|
65
|
+
).context.module.exports;
|
|
66
|
+
|
|
39
67
|
if (ispkg) {
|
|
40
68
|
exports = getPackage(filename)(context);
|
|
69
|
+
} else if(foundCache) {
|
|
70
|
+
|
|
41
71
|
} else if (type == "coffee") {
|
|
42
|
-
exports =
|
|
43
|
-
filepath,
|
|
44
|
-
{ ...options, useContext: true },
|
|
45
|
-
context,
|
|
46
|
-
).context.module.exports;
|
|
72
|
+
exports = exec({ });
|
|
47
73
|
} else if (type == "js") {
|
|
48
|
-
exports =
|
|
49
|
-
filepath,
|
|
50
|
-
{ ...options, useContext: true, compile: false },
|
|
51
|
-
context,
|
|
52
|
-
).context.module.exports;
|
|
74
|
+
exports = exec({ compile: false });
|
|
53
75
|
} else if (type == "yaml" || type == "json" || type == "text") {
|
|
54
76
|
const f = getFile(filepath);
|
|
55
77
|
if (type == "yaml") {
|
|
@@ -74,6 +96,7 @@ module.exports.imp = function (runPath, context) {
|
|
|
74
96
|
}
|
|
75
97
|
|
|
76
98
|
if(!ispkg) context.module.imports.push(filepath);
|
|
99
|
+
if(!ispkg) cachedFiles.push({ filepath, exports });
|
|
77
100
|
|
|
78
101
|
return exports;
|
|
79
102
|
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
module.exports.customRequire = function customRequire(modulePath, filePath) {
|
|
6
|
+
try{
|
|
7
|
+
return require(modulePath)
|
|
8
|
+
} catch(e){
|
|
9
|
+
const resolvedPath = resolveModulePath(modulePath, filePath);
|
|
10
|
+
return require(resolvedPath);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function resolveModulePath(modulePath, filePath) {
|
|
15
|
+
if (modulePath.startsWith('./') || modulePath.startsWith('../') || path.isAbsolute(modulePath)) {
|
|
16
|
+
return path.resolve(modulePath);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const paths = module.constructor._nodeModulePaths(path.dirname(filePath));
|
|
20
|
+
for (const basePath of paths) {
|
|
21
|
+
const fullPath = path.join(basePath, modulePath);
|
|
22
|
+
if (fs.existsSync(fullPath + '.js')) {
|
|
23
|
+
return fullPath + '.js';
|
|
24
|
+
}
|
|
25
|
+
if (fs.existsSync(fullPath + '.json')) {
|
|
26
|
+
return fullPath + '.json';
|
|
27
|
+
}
|
|
28
|
+
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
|
|
29
|
+
const packageJsonPath = path.join(fullPath, 'package.json');
|
|
30
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
31
|
+
const main = require(packageJsonPath).main || 'index.js';
|
|
32
|
+
const mainPath = path.join(fullPath, main);
|
|
33
|
+
if (fs.existsSync(mainPath)) {
|
|
34
|
+
return mainPath;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const indexPath = path.join(fullPath, 'index.js');
|
|
38
|
+
if (fs.existsSync(indexPath)) {
|
|
39
|
+
return indexPath;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
throw new Error(`Cannot find module '${modulePath}'`);
|
|
45
|
+
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
const defaultContext = require("../const/default");
|
|
2
|
+
const { execOptions } = require("../const/opt");
|
|
2
3
|
const emitter = require("../functions/emitter");
|
|
3
4
|
const { exportsFunction } = require("../functions/export");
|
|
4
5
|
const { imp } = require("../functions/import");
|
|
6
|
+
const { customRequire } = require("../functions/require");
|
|
7
|
+
|
|
5
8
|
|
|
6
9
|
module.exports.prepareContext = function (
|
|
7
10
|
custom_context,
|
|
@@ -16,6 +19,12 @@ module.exports.prepareContext = function (
|
|
|
16
19
|
filepath,
|
|
17
20
|
imports: []
|
|
18
21
|
},
|
|
22
|
+
opt: {
|
|
23
|
+
set: (key, value) => execOptions[key] = value,
|
|
24
|
+
get: (key) => execOptions[key],
|
|
25
|
+
push: (key, value) => execOptions[key]?.push(value),
|
|
26
|
+
pop: (key) => execOptions[key]?.pop()
|
|
27
|
+
}
|
|
19
28
|
};
|
|
20
29
|
if (options.useContext) {
|
|
21
30
|
context = {
|
|
@@ -28,9 +37,9 @@ module.exports.prepareContext = function (
|
|
|
28
37
|
...defaultContext,
|
|
29
38
|
require: (package) => {
|
|
30
39
|
try {
|
|
31
|
-
return require(package);
|
|
40
|
+
return execOptions.nativeRequire ? require(package) : customRequire(package, filepath);
|
|
32
41
|
} catch (e) {
|
|
33
|
-
throw new Error("Module not found");
|
|
42
|
+
throw new Error("Module "+package+" not found");
|
|
34
43
|
}
|
|
35
44
|
},
|
|
36
45
|
...custom_context,
|
|
@@ -44,6 +53,8 @@ module.exports.prepareContext = function (
|
|
|
44
53
|
argv: process.argv,
|
|
45
54
|
target: emitter(),
|
|
46
55
|
env: process.env,
|
|
56
|
+
cwd: () => process.cwd(),
|
|
57
|
+
arch: process.arch
|
|
47
58
|
};
|
|
48
59
|
// console.log(custom_context);
|
|
49
60
|
return context;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const { parentPort, workerData } = require('worker_threads');
|
|
2
|
+
const { exec } = require('../../../modules/runtime');
|
|
3
|
+
|
|
4
|
+
const target = {};
|
|
5
|
+
target.events = [];
|
|
6
|
+
target.on = (e, cb) => {
|
|
7
|
+
target.events.push({ e, cb });
|
|
8
|
+
}
|
|
9
|
+
target.off = (e) => {
|
|
10
|
+
target.events = target.events.filter(i => i.e !== e);
|
|
11
|
+
}
|
|
12
|
+
target.emit = (e, data) => {
|
|
13
|
+
target.events.filter(i => i.e == e).forEach(i => i.cb(data));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
parentPort.on('message', (data) => {
|
|
17
|
+
if (data.type === 'event') {
|
|
18
|
+
target.emit(data.event, data.data);
|
|
19
|
+
} else if (data.type === 'start') {
|
|
20
|
+
(async () => {
|
|
21
|
+
try {
|
|
22
|
+
exec(`(${workerData.cb}).call({ process }, context)`, { print: (...a) => console.log(...a), process: {
|
|
23
|
+
on: (e, cb) => { target.on(e, cb) },
|
|
24
|
+
off: (e) => { target.off(e) },
|
|
25
|
+
emit: (e) => { parentPort.postMessage({ type: 'event', event: e, data }) },
|
|
26
|
+
exit: (code) => process.exit(code),
|
|
27
|
+
finish: (data) => {
|
|
28
|
+
parentPort.postMessage({ type: 'result', result: data });
|
|
29
|
+
process.exit(0)
|
|
30
|
+
}
|
|
31
|
+
}, context: workerData.context })
|
|
32
|
+
} catch (e) {
|
|
33
|
+
parentPort.postMessage({ type: 'error', error: e.message });
|
|
34
|
+
}
|
|
35
|
+
})();
|
|
36
|
+
}
|
|
37
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const { Worker } = require('worker_threads');
|
|
2
|
+
const emitter = require('../functions/emitter');
|
|
3
|
+
const future = require('../functions/future');
|
|
4
|
+
const { struct } = require('../models/struct');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
module.exports = (context) => ({
|
|
8
|
+
thread: (cb) => {
|
|
9
|
+
const workers = [];
|
|
10
|
+
|
|
11
|
+
const stopWorker = (worker) => {
|
|
12
|
+
workers.splice(workers.indexOf(worker), 1);
|
|
13
|
+
worker.terminate()
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const athread = {
|
|
17
|
+
stopAll: () => {
|
|
18
|
+
if (!run) return;
|
|
19
|
+
workers.forEach(stopWorker);
|
|
20
|
+
},
|
|
21
|
+
start: (context) => {
|
|
22
|
+
let dataResult = future(() => {}), listener = emitter();
|
|
23
|
+
const worker = new Worker(path.resolve(__dirname, './modules/threads/worker.js'), {
|
|
24
|
+
workerData: { context, cb: cb.toString() }
|
|
25
|
+
});
|
|
26
|
+
workers.push(worker);
|
|
27
|
+
|
|
28
|
+
const stop = () => stopWorker(worker);
|
|
29
|
+
|
|
30
|
+
worker.on('message', (data) => {
|
|
31
|
+
if (data.type === 'result') {
|
|
32
|
+
dataResult.resolve(data.result);
|
|
33
|
+
stop();
|
|
34
|
+
} else if (data.type === 'error') {
|
|
35
|
+
reject(new Error(data.error));
|
|
36
|
+
stop();
|
|
37
|
+
} else if (data.type === 'event') {
|
|
38
|
+
listener.emit(data.event, data.data);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
worker.on('error', (error) => {
|
|
43
|
+
console.log(error);
|
|
44
|
+
stop();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
worker.on('exit', (code) => {
|
|
48
|
+
stop();
|
|
49
|
+
if (code !== 0) {
|
|
50
|
+
throw new Error(`Worker stopped with exit code ${code}`);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
worker.postMessage({ type: 'start' });
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
on: (e, cb) => listener.on(e, cb),
|
|
58
|
+
off: (e, cb) => listener.off(e, cb),
|
|
59
|
+
emit: (e, data) => worker?.postMessage({ type: 'event', event: e, data }),
|
|
60
|
+
get: () => dataResult.wait()
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return athread;
|
|
66
|
+
}
|
|
67
|
+
});
|
package/package.json
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@makano/rew",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.15",
|
|
4
4
|
"description": "A simple coffescript runtime",
|
|
5
5
|
"main": "lib/rew/main.js",
|
|
6
6
|
"directories": {
|
|
7
7
|
"lib": "lib"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
-
"test": "node test/test.js"
|
|
11
|
-
"docs:dev": "vitepress dev docs",
|
|
12
|
-
"docs:build": "vitepress build docs",
|
|
13
|
-
"docs:preview": "vitepress preview docs"
|
|
10
|
+
"test": "node test/test.js"
|
|
14
11
|
},
|
|
15
12
|
"files": [
|
|
16
13
|
"lib/",
|
|
@@ -32,7 +29,6 @@
|
|
|
32
29
|
"chokidar": "^3.6.0",
|
|
33
30
|
"js-yaml": "^4.1.0",
|
|
34
31
|
"vm": "^0.1.0",
|
|
35
|
-
"ws": "^8.17.0",
|
|
36
32
|
"yargs": "^17.7.2"
|
|
37
33
|
},
|
|
38
34
|
"devDependencies": {
|