@makano/rew 1.5.3 → 1.5.5
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/lib/rew/functions/import.js +10 -10
- package/lib/rew/misc/findAppInfo.js +3 -3
- package/lib/rew/misc/findAppPath.js +3 -7
- package/lib/rew/misc/strace.js +1 -1
- package/lib/rew/modules/compiler.js +42 -42
- package/lib/rew/modules/context.js +5 -5
- package/lib/rew/modules/runtime.js +5 -4
- package/lib/rew/pkgs/serve.app.js +21 -15
- package/lib/rew/pkgs/serve.js +63 -10
- package/package.json +1 -1
@@ -20,7 +20,7 @@ module.exports.cleanCache = () => {
|
|
20
20
|
while(cachedFiles.length) cachedFiles.pop();
|
21
21
|
};
|
22
22
|
const lookUpInOtherApps = (fullPath) => {
|
23
|
-
straceLog('===> WARN:
|
23
|
+
straceLog('===> WARN: Lookup slows process');
|
24
24
|
const con = conf({});
|
25
25
|
const name = fullPath.indexOf('/') ? fullPath.split('/')[0] : fullPath;
|
26
26
|
let dpath = fullPath.indexOf('/') ? fullPath.split('/')[1] : '';
|
@@ -51,10 +51,10 @@ module.exports.imp = function (runPath, context) {
|
|
51
51
|
let exports,
|
52
52
|
ispkg = findPackage(filename);
|
53
53
|
|
54
|
-
straceLog('
|
54
|
+
straceLog('import file', '"' + filename + '"', 'as type', type);
|
55
55
|
|
56
56
|
if (filename.startsWith('@') && context.app) {
|
57
|
-
straceLog('===>
|
57
|
+
straceLog('===> from app root');
|
58
58
|
filename = filename.replace('@', context.app.path);
|
59
59
|
}
|
60
60
|
|
@@ -62,7 +62,7 @@ module.exports.imp = function (runPath, context) {
|
|
62
62
|
if(path.extname(filepath) == '.qrew') options.qrew = true;
|
63
63
|
|
64
64
|
const lookUp = () => {
|
65
|
-
straceLog('===>
|
65
|
+
straceLog('===> lookUp()');
|
66
66
|
const otherPath = lookUpInOtherApps(filename);
|
67
67
|
if (!otherPath) throw new Error('Module "' + filename + '" not found');
|
68
68
|
else filepath = otherPath;
|
@@ -80,7 +80,7 @@ module.exports.imp = function (runPath, context) {
|
|
80
80
|
typeof ext == 'string' ? existsSync(filepath + ext) : existsSync(filepath + (ext.ext || '')),
|
81
81
|
);
|
82
82
|
if (resolve) {
|
83
|
-
straceLog('===>
|
83
|
+
straceLog('===> resolve()');
|
84
84
|
filepath += typeof resolve == 'string' ? resolve : resolve.ext;
|
85
85
|
if (typeof resolve == 'object' && resolve.options) {
|
86
86
|
if (resolve.options.type) type = resolve.options.type;
|
@@ -91,7 +91,7 @@ module.exports.imp = function (runPath, context) {
|
|
91
91
|
}
|
92
92
|
|
93
93
|
const exec = (coptions = {}) => {
|
94
|
-
straceLog('===>
|
94
|
+
straceLog('===> execute(importFile)');
|
95
95
|
const r = runPath(
|
96
96
|
filepath,
|
97
97
|
{
|
@@ -113,7 +113,7 @@ module.exports.imp = function (runPath, context) {
|
|
113
113
|
}
|
114
114
|
|
115
115
|
if (ispkg) {
|
116
|
-
straceLog('===>
|
116
|
+
straceLog('===> findPackage()');
|
117
117
|
const pkg = getPackage(filename)(context, options);
|
118
118
|
exports = pkg._onImport ? pkg._onImport() : pkg;
|
119
119
|
if(options.useDefaultForPackages) exports = { default: exports };
|
@@ -124,10 +124,10 @@ module.exports.imp = function (runPath, context) {
|
|
124
124
|
} else if (type == 'js') {
|
125
125
|
exports = exec({ compile: false });
|
126
126
|
} else if (type == 'yaml' || type == 'json' || type == 'text') {
|
127
|
-
straceLog('===>
|
127
|
+
straceLog('===> getRawFile()');
|
128
128
|
const f = getFile(filepath);
|
129
129
|
if (type == 'yaml') {
|
130
|
-
straceLog('===>
|
130
|
+
straceLog('===> fromYaml()');
|
131
131
|
exports = _returns(options, importYaml(f.path, f));
|
132
132
|
} else if (type == 'json') {
|
133
133
|
straceLog('===>');
|
@@ -137,7 +137,7 @@ module.exports.imp = function (runPath, context) {
|
|
137
137
|
exports = _returns(options, {});
|
138
138
|
}
|
139
139
|
} else {
|
140
|
-
straceLog('===>
|
140
|
+
straceLog('===> fromText()');
|
141
141
|
exports = _returns(options, f.content);
|
142
142
|
}
|
143
143
|
}
|
@@ -6,11 +6,11 @@ const { straceLog } = require('./strace');
|
|
6
6
|
|
7
7
|
module.exports.findAppInfo = function (filepath) {
|
8
8
|
const appPath = findAppPath(path.dirname(filepath));
|
9
|
-
straceLog('
|
9
|
+
straceLog('findAppInfo() for', filepath);
|
10
10
|
if (appPath) {
|
11
11
|
const config = jsYaml.load(readFileSync(path.join(appPath, 'app.yaml')));
|
12
|
-
straceLog('==>
|
13
|
-
straceLog('==>
|
12
|
+
straceLog('==> INFO Found config at:', '"' + appPath + '"');
|
13
|
+
straceLog('==> INFO App Package:', '"' + config?.manifest?.package + '"');
|
14
14
|
return {
|
15
15
|
path: appPath,
|
16
16
|
config,
|
@@ -3,23 +3,19 @@ const fs = require('fs'); // Import the 'path' module
|
|
3
3
|
const { straceLog } = require('./strace');
|
4
4
|
|
5
5
|
module.exports.findAppPath = (currentDir = __dirname) => {
|
6
|
-
|
7
|
-
straceLog('FINDAPP() for', currentDir);
|
6
|
+
straceLog('findApp() for', '"' + currentDir + '"');
|
8
7
|
const appYamlPath = path.join(currentDir, 'app.yaml');
|
9
8
|
if (fs.existsSync(appYamlPath)) {
|
10
9
|
|
11
|
-
straceLog('==>
|
10
|
+
straceLog('==> INFO found path', `"${appYamlPath}"`);
|
12
11
|
return currentDir;
|
13
12
|
}
|
14
13
|
|
15
|
-
// If not found, move up a directory level
|
16
14
|
const parentDir = path.dirname(currentDir);
|
17
15
|
|
18
|
-
// Check if we reached the root directory
|
19
16
|
if (parentDir === currentDir) {
|
20
|
-
return null;
|
17
|
+
return null;
|
21
18
|
}
|
22
19
|
|
23
|
-
// Recursively call the function on the parent directory
|
24
20
|
return module.exports.findAppPath(parentDir);
|
25
21
|
};
|
package/lib/rew/misc/strace.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module.exports.straceLog = function(...logs){
|
2
2
|
if(process.straceMode){
|
3
|
-
console.log(`[${new Date().
|
3
|
+
console.log(`[${new Date().toLocaleTimeString()} ${new Date().getDate()}/${new Date().getMonth()}/${new Date().getFullYear()}] [${process.pid}]`, ...logs.map(i => i.replace(/^([=>]+)/, '\x1b[34m$1\x1b[39m').replace(/WARN:/g, '\x1b[30m\x1b[43m WARN \x1b[39m\x1b[49m').replace(/INFO/g, '\x1b[30m\x1b[44m INFO \x1b[39m\x1b[49m').replace(/^"(.+)"$/, '\x1b[32m$1\x1b[39m').replace(/!([A-Z-+]+)/, (_, a) => `\x1b[30m\x1b[4${a.startsWith('-') ? '5' : a.startsWith('+') ? '6' : '2'}m ${a.replace(/^[-+]/, '')} \x1b[39m\x1b[49m`)));
|
4
4
|
}
|
5
5
|
}
|
@@ -137,15 +137,15 @@ const fnextToken = (i, tokens, type, value) => {
|
|
137
137
|
function declareAlias(aliases, token) {
|
138
138
|
const regex = /^#declare(\*)?\s+(\w+)\s+"([^"]+)"\s*=\s*([\s\S]*);$/;
|
139
139
|
const match = token.value.trim().match(regex);
|
140
|
-
straceLog('
|
141
|
-
straceLog('==>
|
140
|
+
straceLog('declareCase()');
|
141
|
+
straceLog('==> WARN: Experimental feature detected');
|
142
142
|
|
143
143
|
if (match) {
|
144
144
|
const isPublic = !!match[1];
|
145
145
|
const type = match[2] == "key" ? 'IDENTIFIER' : match[2];
|
146
146
|
let name = match[3];
|
147
147
|
let value = match[4].trim();
|
148
|
-
straceLog('==> DECLARE', name, 'as', value);
|
148
|
+
straceLog('==> INFO !DECLARE', name, 'as', value);
|
149
149
|
|
150
150
|
let aliasValue = value.startsWith('${')
|
151
151
|
? new Function('token', 'tokens', 'code', 'hooks', 'index', 'setIndex', value.slice(2, -1))
|
@@ -210,7 +210,7 @@ function declareAlias(aliases, token) {
|
|
210
210
|
aliases[type][name] = aliasValue;
|
211
211
|
|
212
212
|
if(isPublic){
|
213
|
-
straceLog('==>', '
|
213
|
+
straceLog('==>', 'INFO Declaration Globalized');
|
214
214
|
execOptions._syntaxAliases[type] = execOptions._syntaxAliases[type] || {};
|
215
215
|
execOptions._syntaxAliases[type][name] = aliasValue;
|
216
216
|
}
|
@@ -234,11 +234,11 @@ const stdTypes = (isPublic) => {
|
|
234
234
|
return r;
|
235
235
|
};
|
236
236
|
const includeFile = (includeContent, options) => {
|
237
|
-
straceLog('
|
237
|
+
straceLog('include()', includeContent);
|
238
238
|
const dontInclude = includeContent.startsWith('*');
|
239
239
|
if(dontInclude) {
|
240
240
|
includeContent = includeContent.slice(1);
|
241
|
-
straceLog('==>
|
241
|
+
straceLog('==> INFO ingoring output', includeContent);
|
242
242
|
};
|
243
243
|
const fp = path.resolve(path.dirname(options.filename || ''), includeContent);
|
244
244
|
let packageName = options.filename ? (existsSync(fp) ? fp : includeContent) : includeContent;
|
@@ -257,13 +257,13 @@ const includeFile = (includeContent, options) => {
|
|
257
257
|
if(packageName == 'std'){
|
258
258
|
r = _inc(stdTypes(dontInclude), true);
|
259
259
|
} else if (existsSync(packageName)) {
|
260
|
-
straceLog('==>
|
260
|
+
straceLog('==> includeFile(', '"'+packageName+'"', ')');
|
261
261
|
r = _inc(packageName);
|
262
262
|
} else {
|
263
263
|
const packageName = includeContent.match('/') ? includeContent.split('/')[0] : includeContent;
|
264
264
|
const headerFile = includeContent.match('/') ? includeContent.replace(packageName+'/', '') : 'main.h.coffee';
|
265
265
|
const pathname = path.join(CONFIG_PATH, packageName, 'app', headerFile);
|
266
|
-
straceLog('==>
|
266
|
+
straceLog('==> includePackage(', '"'+packageName+'"');
|
267
267
|
if(existsSync(pathname)) r = _inc(pathname);
|
268
268
|
}
|
269
269
|
if(dontInclude){
|
@@ -277,12 +277,12 @@ function useImp(token, options){
|
|
277
277
|
token.value.startsWith('"#') ||
|
278
278
|
token.value.startsWith("'#")
|
279
279
|
)){
|
280
|
-
straceLog('==>
|
280
|
+
straceLog('==> INFO imp() Uses HEADER');
|
281
281
|
const dem = token.value.slice(0, 1);
|
282
282
|
const value = token.value.slice(1, -1);
|
283
283
|
let packageName = value.slice(1);
|
284
284
|
token.value = dem+packageName+dem;
|
285
|
-
straceLog('
|
285
|
+
straceLog('imp() with header for', `"${packageName}"`);
|
286
286
|
return includeFile(packageName !== 'std' ? packageName : '*'+packageName, options);
|
287
287
|
}
|
288
288
|
return '';
|
@@ -344,7 +344,7 @@ function insertAt(array, index, ...values) {
|
|
344
344
|
}
|
345
345
|
|
346
346
|
function compileRewStuff(content, options) {
|
347
|
-
straceLog('
|
347
|
+
straceLog('tokeinze(currentFile)');
|
348
348
|
const tokens = tokenizeCoffeeScript(content);
|
349
349
|
let result = '';
|
350
350
|
let multilineDeclareBuffer = [];
|
@@ -412,17 +412,17 @@ function compileRewStuff(content, options) {
|
|
412
412
|
|
413
413
|
if (token.type === 'COMMENT' && token.value.slice(1).trim().startsWith('@jsx')) {
|
414
414
|
options.jsx = true;
|
415
|
-
straceLog('
|
415
|
+
straceLog('jsx().with(comments)');
|
416
416
|
if(token.value.split('@jsx')[1].trim()){
|
417
417
|
options.jsxPragma = token.value.split('@jsx')[1].trim();
|
418
|
-
straceLog('
|
418
|
+
straceLog('jsx().withPragma(', `"${options.jsxPragma}"`, ')');
|
419
419
|
}
|
420
420
|
}
|
421
421
|
|
422
422
|
if (token.type === 'COMMENT' && token.value.slice(1).trim() === '@cls') {
|
423
423
|
options.cls = true;
|
424
|
-
straceLog('
|
425
|
-
straceLog('===> HIGHLY EXPERIMENTAL FEATURE DETECTED');
|
424
|
+
straceLog('cliSyntax::enable()');
|
425
|
+
straceLog('===> WARN: HIGHLY EXPERIMENTAL FEATURE DETECTED');
|
426
426
|
}
|
427
427
|
|
428
428
|
if (options.cls && token.type === 'OTHER' && token.value === '-' && nextToken.value == '-' && tokens[i-1]?.type == 'WHITESPACE') {
|
@@ -442,12 +442,12 @@ function compileRewStuff(content, options) {
|
|
442
442
|
|
443
443
|
if (tokens[i-1]?.value !== '.' && token.type === 'IDENTIFIER' && token.value === 'export' && !options.keepImports) {
|
444
444
|
token.value = 'pub';
|
445
|
-
straceLog('
|
445
|
+
straceLog('INFO !TRANSLATE converting export to pub');
|
446
446
|
}
|
447
447
|
|
448
448
|
if (tokens[i-1]?.value !== '.' && token.type === 'IDENTIFIER' && token.value === 'package' && nextToken.type == 'STRING') {
|
449
449
|
token.value = 'appPackage';
|
450
|
-
straceLog('
|
450
|
+
straceLog('changeAppPackage()');
|
451
451
|
}
|
452
452
|
|
453
453
|
if (
|
@@ -465,7 +465,7 @@ function compileRewStuff(content, options) {
|
|
465
465
|
}
|
466
466
|
|
467
467
|
if (tokens[i-1]?.value !== '.' && token.type === 'IDENTIFIER' && token.value === 'using' && !options.disableUse) {
|
468
|
-
straceLog('
|
468
|
+
straceLog('!+DIRECTIVE using()');
|
469
469
|
const next = nextToken?.value;
|
470
470
|
if(next in USING_DEFAULT) {
|
471
471
|
const { use } = USING_DEFAULT[next];
|
@@ -475,7 +475,7 @@ function compileRewStuff(content, options) {
|
|
475
475
|
|
476
476
|
const { token: nextNextToken } = gnextToken(i, 3, tokens) || {};
|
477
477
|
if(nextNextToken.value == "as") nextNextToken.value = ",";
|
478
|
-
} else straceLog('==> UNKNOWN');
|
478
|
+
} else straceLog('==> !-UNKNOWN');
|
479
479
|
}
|
480
480
|
|
481
481
|
if (token.type === 'IDENTIFIER' && token.value === 'as' && !options.keepImports) {
|
@@ -493,15 +493,15 @@ function compileRewStuff(content, options) {
|
|
493
493
|
|
494
494
|
if (tokens[i-1]?.value !== '.' && token.type === 'IDENTIFIER' && token.value === 'import' && !options.keepImports) {
|
495
495
|
// console.log(nextToken.type);
|
496
|
-
straceLog('
|
497
|
-
straceLog('==> WARN:
|
496
|
+
straceLog('import()');
|
497
|
+
straceLog('==> WARN: Slows compilation');
|
498
498
|
let ind = i + n + 2;
|
499
499
|
let isAs = false;
|
500
500
|
let usedDefault = false;
|
501
501
|
|
502
502
|
let defaultName;
|
503
503
|
if (nextToken.type === 'STRING') {
|
504
|
-
straceLog('==> SIMPLE');
|
504
|
+
straceLog('==> !SIMPLE');
|
505
505
|
if(useImp(nextToken, options)) updateAliases(aliases);
|
506
506
|
result += `inc ${nextToken.value}`;
|
507
507
|
i += n;
|
@@ -538,7 +538,7 @@ function compileRewStuff(content, options) {
|
|
538
538
|
.filter((t, i, arr) => !arr[i+1]?.match(':') && !arr[i-1]?.match(':'))
|
539
539
|
.join(', ');
|
540
540
|
|
541
|
-
straceLog('==>', exports, 'from', nameToken.value);
|
541
|
+
straceLog('==> !COMPLEX', exports, 'from', nameToken.value);
|
542
542
|
|
543
543
|
if(useImp(nameToken, options)) updateAliases(aliases);
|
544
544
|
result += `{ ${exports} } ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken.value}`;
|
@@ -550,7 +550,7 @@ function compileRewStuff(content, options) {
|
|
550
550
|
const nameToken = fnextToken(asToken.ti, tokens, 'STRING');
|
551
551
|
const nextToken = fnextToken(asToken.ti + 1, tokens, 'IDENTIFIER');
|
552
552
|
defaultName = nextToken.value;
|
553
|
-
straceLog('==>', defaultName, 'from', nameToken.value);
|
553
|
+
straceLog('==> !COMPLEX', defaultName, 'from', nameToken.value);
|
554
554
|
if(useImp(nameToken, options)) updateAliases(aliases);
|
555
555
|
result += `${defaultName} ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken.value}`;
|
556
556
|
i = ind + 6;
|
@@ -570,7 +570,7 @@ function compileRewStuff(content, options) {
|
|
570
570
|
.flat(1)
|
571
571
|
.filter((t, i, arr) => !arr[i+1]?.match(':') && !arr[i-1]?.match(':'))
|
572
572
|
.join(', ');
|
573
|
-
straceLog('==>', defaultName, 'and', exports, 'from', nameToken.value);
|
573
|
+
straceLog('==> !COMPLEX', defaultName, 'and', exports, 'from', nameToken.value);
|
574
574
|
if(useImp(nameToken, options)) updateAliases(aliases);
|
575
575
|
result += `{ default: ${defaultName}, ${exports} } ${options.type == 'coffee' ? '=' : ':='} inc ${nameToken?.value || ''}`;
|
576
576
|
i = closingBraceToken.ti + 4;
|
@@ -589,7 +589,7 @@ function compileRewStuff(content, options) {
|
|
589
589
|
if (nextLastToken?.value == 'assert') {
|
590
590
|
result += ', ';
|
591
591
|
const assertionToken = gnextToken(nextLastToken.ti, 2, tokens);
|
592
|
-
straceLog('==> ASSERT', assertionToken);
|
592
|
+
straceLog('==> !ASSERT', assertionToken);
|
593
593
|
if(assertionToken.token.type == 'OTHER' && assertionToken.token.value == '{'){
|
594
594
|
hooks.push({
|
595
595
|
index: assertionToken.token.ti,
|
@@ -607,7 +607,7 @@ function compileRewStuff(content, options) {
|
|
607
607
|
}
|
608
608
|
|
609
609
|
if (tokens[i-1]?.value !== '.' && token.type === 'IDENTIFIER' && (token.value === 'imp' || token.value === 'inc')) {
|
610
|
-
straceLog('
|
610
|
+
straceLog('!+DIRECTIVE imp()');
|
611
611
|
let { token: t1 } = gnextToken(i, 1, tokens) || {};
|
612
612
|
let { token: t2 } = gnextToken(i, 2, tokens) || {};
|
613
613
|
let r = '';
|
@@ -634,7 +634,7 @@ function compileRewStuff(content, options) {
|
|
634
634
|
nextToken.value &&
|
635
635
|
nextToken.value !== 'undefined' && !options.keepImports
|
636
636
|
) {
|
637
|
-
straceLog('
|
637
|
+
straceLog('!+DIRECTIVE pub()');
|
638
638
|
let next = {...nextToken}, isClass = false;
|
639
639
|
if(next.value == 'default'){
|
640
640
|
i += 2;
|
@@ -643,7 +643,7 @@ function compileRewStuff(content, options) {
|
|
643
643
|
next.value = gnextToken(i, n + 1, tokens)?.token.value || "default";
|
644
644
|
isClass = true;
|
645
645
|
}
|
646
|
-
straceLog('==> PUBLIC', next.value);
|
646
|
+
straceLog('==> !PUBLIC', next.value);
|
647
647
|
hooks.push({
|
648
648
|
index: i + 1,
|
649
649
|
value: `"${next.value}", ${isClass ? `${next.value} = ` : ''}`,
|
@@ -653,13 +653,13 @@ function compileRewStuff(content, options) {
|
|
653
653
|
const aliasType = aliases[token.type];
|
654
654
|
// if(token.value == 'sidewest') console.log(aliases, token.value, token.type);
|
655
655
|
if (aliasType && Object.keys(aliasType).includes(token.value)) {
|
656
|
-
straceLog('
|
656
|
+
straceLog('!+DIRECTIVE alias()', token.type);
|
657
657
|
const aliasValue = aliasType[token.value];
|
658
658
|
if (typeof aliasValue === 'function') {
|
659
|
-
straceLog('==>
|
659
|
+
straceLog('==> INFO Execute alias:', token.value);
|
660
660
|
result += aliasValue(token, tokens, result, hooks, i, (n) => i = n) || "";
|
661
661
|
} else {
|
662
|
-
straceLog('==>
|
662
|
+
straceLog('==> INFO Literal alias:', token.value);
|
663
663
|
result += aliasValue;
|
664
664
|
}
|
665
665
|
continue;
|
@@ -697,12 +697,12 @@ function compileRewStuff(content, options) {
|
|
697
697
|
}
|
698
698
|
|
699
699
|
const compileCivetStuff = (file, options) => {
|
700
|
-
straceLog('
|
700
|
+
straceLog('compileCivet(currentFile)');
|
701
701
|
const preCompileOptions = {
|
702
702
|
filename: file.path,
|
703
703
|
...options
|
704
704
|
};
|
705
|
-
straceLog('
|
705
|
+
straceLog('prepareOptions(currentFile).as(', `"${JSON.stringify(preCompileOptions)}"`, ')');
|
706
706
|
|
707
707
|
if(options?.type == 'js' || file?.path?.endsWith('.js')){
|
708
708
|
return {
|
@@ -724,7 +724,7 @@ const compileCivetStuff = (file, options) => {
|
|
724
724
|
};
|
725
725
|
|
726
726
|
let compiled = options.async ? compileCivet(prepared, compileOptions) : wait(compileCivet, prepared, compileOptions);
|
727
|
-
straceLog('==>
|
727
|
+
straceLog('==> !COMPILER civetCompile(fileContent)');
|
728
728
|
|
729
729
|
return {
|
730
730
|
compiled,
|
@@ -745,10 +745,10 @@ const cpl = (module.exports.compile = function (file, options = {}) {
|
|
745
745
|
compiledCode = result.compiled;
|
746
746
|
|
747
747
|
const babelify = (code, options) => {
|
748
|
-
straceLog('
|
749
|
-
if(doJSX) straceLog('==> WITH JSX');
|
750
|
-
if(doTypes) straceLog('==> WITH
|
751
|
-
if(doDecorators) straceLog('==> WITH DECORATORS');
|
748
|
+
straceLog('!COMPILER babel()');
|
749
|
+
if(doJSX) straceLog('==> INFO !-WITH JSX');
|
750
|
+
if(doTypes) straceLog('==> INFO !-WITH Types');
|
751
|
+
if(doDecorators) straceLog('==> INFO !-WITH DECORATORS');
|
752
752
|
return babel.transformSync(code, {
|
753
753
|
presets: [
|
754
754
|
...(doJSX ? [[babelReact, { throwIfNamespace: false, pragmaFrag: options.jsxPragmaFrag || execOptions.jsxPragmaFrag, pragma: options.jsxPragma || execOptions.jsxPragma }]] : [])
|
@@ -782,7 +782,7 @@ const cpl = (module.exports.compile = function (file, options = {}) {
|
|
782
782
|
});
|
783
783
|
|
784
784
|
module.exports.compileFile = function (filepath, options = {}) {
|
785
|
-
straceLog('
|
785
|
+
straceLog('compile(currentFile)');
|
786
786
|
const f = typeof filepath == "object" ? filepath : getFile(filepath);
|
787
787
|
if(typeof filepath == "object") filepath = filepath.path;
|
788
788
|
let qrew = false;
|
@@ -791,13 +791,13 @@ module.exports.compileFile = function (filepath, options = {}) {
|
|
791
791
|
qrew = true
|
792
792
|
f.content = from_qrew(readFileSync(f.path), options.package || findAppInfo(filepath)?.config.manifest.package || path.basename(filepath).split('.').slice(0, -1).join('.')).toString();
|
793
793
|
options.type = f.content.split('\n')[0]?.match(/"initFile (.+)"/)?.[1]?.split('.').pop();
|
794
|
-
straceLog('
|
794
|
+
straceLog('decodeCrew(currentFile).as(', `"${options.type}"`,')');
|
795
795
|
}
|
796
796
|
|
797
797
|
let compiled_code = cpl(f, { ...options });
|
798
798
|
|
799
799
|
if(options.onlyCompile && !qrew){
|
800
|
-
straceLog('
|
800
|
+
straceLog('writeAndQuit(compileData)');
|
801
801
|
if(compiled_code instanceof Promise){
|
802
802
|
compiled_code.then((r) => {
|
803
803
|
console.log(r);
|
@@ -25,7 +25,7 @@ module.exports.prepareContext = function (
|
|
25
25
|
filepath = "",
|
26
26
|
runPath = () => {},
|
27
27
|
) {
|
28
|
-
straceLog('
|
28
|
+
straceLog('!-NEW context()');
|
29
29
|
if (mainFile == "") mainFile = filepath;
|
30
30
|
/** @type {Record<string, any>} */
|
31
31
|
let context = {
|
@@ -82,7 +82,7 @@ module.exports.prepareContext = function (
|
|
82
82
|
this['@cb'] = cb;
|
83
83
|
}
|
84
84
|
});
|
85
|
-
}, out: {...process.stdout, cols: process.stdout.columns, rows: process.stdout.rows, put: (...logs) => context.print(...logs), strace: (...logs) => straceLog('==>
|
85
|
+
}, out: {...process.stdout, cols: process.stdout.columns, rows: process.stdout.rows, put: (...logs) => context.print(...logs), strace: (...logs) => straceLog('==> !+OUT straceOut(): ', ...logs ), write: (logs) => context.printf(logs+'\n') }, in: {...process.stdin, read: (...args) => context.input(...args)}, define: (name, object) => {
|
86
86
|
if(Array.isArray(name) && name.length == 2 && typeof name[0] == 'string'){
|
87
87
|
object = name[1];
|
88
88
|
name = name[0];
|
@@ -242,14 +242,14 @@ module.exports.prepareContext = function (
|
|
242
242
|
if(context.app?.config?.exec?.['auto import']){
|
243
243
|
const autoipath = path.join(context.app.path, context.app.config?.exec?.['auto import']);
|
244
244
|
if(autoipath !== filepath){
|
245
|
-
straceLog('==>
|
245
|
+
straceLog('==> !ACTION autoImport()', autoipath);
|
246
246
|
const all = context.imp(path.relative(path.dirname(filepath), autoipath));
|
247
247
|
for(let i in all) context[i] = all[i];
|
248
248
|
}
|
249
249
|
}
|
250
250
|
|
251
251
|
if(!context.app){
|
252
|
-
straceLog('==>
|
252
|
+
straceLog('==> WARN: App not found');
|
253
253
|
context.appPackage = (packageName) => context.app = { config: { manifest: { package: packageName } } }
|
254
254
|
} else {
|
255
255
|
context.appPackage = context.mod = (packageName) => context.module.modset = packageName;
|
@@ -265,7 +265,7 @@ module.exports.prepareContext = function (
|
|
265
265
|
context.module.main ||
|
266
266
|
(options.fromMain == true && options.as == "main")
|
267
267
|
) {
|
268
|
-
straceLog('==>
|
268
|
+
straceLog('==> !MAIN', filepath);
|
269
269
|
context.opt = {
|
270
270
|
set: (key, value) => (execOptions[key] = value),
|
271
271
|
get: (key) => execOptions[key],
|
@@ -21,17 +21,17 @@ module.exports.runPath = function runPath(filepath, options = {}, custom_context
|
|
21
21
|
if(filepath.endsWith('.coffee')) options.type = 'coffee';
|
22
22
|
if(filepath.endsWith('.qrew')) options.type = 'qrew';
|
23
23
|
|
24
|
-
straceLog('
|
24
|
+
straceLog('!+DECLARE currentFile =', filepath, 'as', options.type || '!UNKNOWN');
|
25
25
|
|
26
26
|
if(options.import?.async) options.async = true;
|
27
|
-
if(options.async) straceLog('ASYNCMODE()
|
27
|
+
if(options.async) straceLog('==> !ASYNCMODE currentFile::async()');
|
28
28
|
let { compiled_code, file } = compileFile(options.code ? { content: options.code, path: filepath } : filepath, options);
|
29
|
-
straceLog('
|
29
|
+
straceLog('==> INFO Compile done with compileData');
|
30
30
|
// context.module.compiled = compiled_code;
|
31
31
|
// context.process.exit = (int) => process.exit(int);
|
32
32
|
|
33
33
|
const doCode = () => {
|
34
|
-
straceLog('
|
34
|
+
straceLog('runCode(compileData)');
|
35
35
|
const context = options.import?.takeThisContext ? custom_context : prepareContext(custom_context, options, file.path, runPath);
|
36
36
|
|
37
37
|
if(context.app){
|
@@ -51,6 +51,7 @@ module.exports.runPath = function runPath(filepath, options = {}, custom_context
|
|
51
51
|
let execd = exec(compiled_code, context, file.content);
|
52
52
|
|
53
53
|
if(context.module.main && (context.module.exports?.main || (typeof context.module.exports == "function" && context.module.exports.name == 'main'))){
|
54
|
+
straceLog('call(currentFile::mainFn)');
|
54
55
|
const mainFn = context.module.exports.main ?? context.module.exports;
|
55
56
|
let ctx = context;
|
56
57
|
if(mainFn._class){
|
@@ -7,6 +7,14 @@ module.exports = (context) => {
|
|
7
7
|
|
8
8
|
let appOptions = {};
|
9
9
|
|
10
|
+
const additionalParams = {
|
11
|
+
text: svr.prototype.text,
|
12
|
+
html: svr.prototype.html,
|
13
|
+
status: svr.prototype.status,
|
14
|
+
json: svr.prototype.json,
|
15
|
+
error: svr.prototype.error
|
16
|
+
};
|
17
|
+
|
10
18
|
const mkRouter = (options = {}) => svr.prototype.router({ type: 'auto', ...appOptions, ...options });
|
11
19
|
|
12
20
|
class Events {
|
@@ -251,7 +259,7 @@ module.exports = (context) => {
|
|
251
259
|
that.controller.register(method, _path, cb, middleWare);
|
252
260
|
return cb;
|
253
261
|
}
|
254
|
-
const mkReq = (usage) => (req,
|
262
|
+
const mkReq = (usage) => (req, ...args) => usage.call(req, req, ...args);
|
255
263
|
class SControllerContext extends Context('controller') {
|
256
264
|
|
257
265
|
middleWares = [];
|
@@ -317,18 +325,21 @@ module.exports = (context) => {
|
|
317
325
|
}
|
318
326
|
|
319
327
|
function mkBody(req){
|
328
|
+
if(!req.data) return null;
|
320
329
|
const type = req.headers.get('content-type');
|
321
330
|
if(type == 'application/json'){
|
322
331
|
return req.data.json();
|
323
|
-
} else {
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
332
|
+
} else if(type.startsWith('multipart/form-data')){
|
333
|
+
return req.data.multipart();
|
334
|
+
} else if(type == 'multipart/form-data'){
|
335
|
+
return req.data;
|
336
|
+
} else try {
|
337
|
+
return req.data.json();
|
338
|
+
} catch(e){
|
339
|
+
return req.data?.text();
|
329
340
|
}
|
330
341
|
}
|
331
|
-
|
342
|
+
let f;
|
332
343
|
class SController extends Injectable {
|
333
344
|
|
334
345
|
constructor(root){
|
@@ -341,14 +352,9 @@ module.exports = (context) => {
|
|
341
352
|
return async (req, ctx) => {
|
342
353
|
let context = {...{
|
343
354
|
request: req,
|
355
|
+
params: req.params || {},
|
344
356
|
body: mkBody(req)
|
345
|
-
},...ctx, ...(cb.parent || {}), ...
|
346
|
-
text: svr.prototype.text,
|
347
|
-
html: svr.prototype.html,
|
348
|
-
status: svr.prototype.status,
|
349
|
-
json: svr.prototype.json,
|
350
|
-
error: svr.prototype.error
|
351
|
-
}};
|
357
|
+
},...ctx, ...(cb.parent || {}), ...additionalParams};
|
352
358
|
return await cb.call(context, ...Object.keys(context).map(i => context[i]));
|
353
359
|
}
|
354
360
|
}
|
package/lib/rew/pkgs/serve.js
CHANGED
@@ -30,7 +30,65 @@ module.exports = (context) => {
|
|
30
30
|
return new Request(url, options);
|
31
31
|
}
|
32
32
|
|
33
|
-
|
33
|
+
|
34
|
+
function parseMultipart(req, content) {
|
35
|
+
const contentType = req.headers.get('content-type');
|
36
|
+
const boundary = contentType.split('boundary=')[1];
|
37
|
+
if (!boundary) throw new Error('No boundary in multipart/form-data');
|
38
|
+
|
39
|
+
const body = content;
|
40
|
+
const parts = body.split(`--${boundary}`).filter(part => part.trim() !== '--' && part.trim() !== '');
|
41
|
+
|
42
|
+
const fields = {};
|
43
|
+
const files = {};
|
44
|
+
|
45
|
+
parts.forEach(part => {
|
46
|
+
const [headers, ...content] = part.split('\r\n\r\n');
|
47
|
+
const contentBody = content.join('\r\n\r\n').trim();
|
48
|
+
const headerLines = headers.split('\r\n');
|
49
|
+
|
50
|
+
let name, filename, contentType;
|
51
|
+
headerLines.forEach(line => {
|
52
|
+
if (line.startsWith('Content-Disposition')) {
|
53
|
+
const matchName = /name="([^"]+)"/.exec(line);
|
54
|
+
const matchFilename = /filename="([^"]+)"/.exec(line);
|
55
|
+
if (matchName) name = matchName[1];
|
56
|
+
if (matchFilename) filename = matchFilename[1];
|
57
|
+
}
|
58
|
+
if (line.startsWith('Content-Type')) {
|
59
|
+
contentType = line.split(': ')[1];
|
60
|
+
}
|
61
|
+
});
|
62
|
+
|
63
|
+
if (filename) {
|
64
|
+
files[name] = {
|
65
|
+
filename,
|
66
|
+
contentType,
|
67
|
+
...handleBuffer(Buffer.from(contentBody, 'binary'))
|
68
|
+
};
|
69
|
+
} else {
|
70
|
+
fields[name] = contentBody;
|
71
|
+
}
|
72
|
+
});
|
73
|
+
|
74
|
+
return { ...fields, ...files };
|
75
|
+
}
|
76
|
+
|
77
|
+
function handleBuffer(buffer){
|
78
|
+
return {
|
79
|
+
text(){
|
80
|
+
return buffer.toString();
|
81
|
+
},
|
82
|
+
json(){
|
83
|
+
return JSON.parse(buffer.toString());
|
84
|
+
},
|
85
|
+
buffer(){
|
86
|
+
return buffer;
|
87
|
+
}
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
function getBody(req, request){
|
34
92
|
return new Promise((resolve, reject) => {
|
35
93
|
const chunks = [];
|
36
94
|
|
@@ -41,14 +99,9 @@ module.exports = (context) => {
|
|
41
99
|
req.on('end', () => {
|
42
100
|
let buffer = chunks.length ? Buffer.concat(chunks) : null;
|
43
101
|
resolve(buffer ? {
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
json(){
|
48
|
-
return JSON.parse(buffer.toString());
|
49
|
-
},
|
50
|
-
buffer(){
|
51
|
-
return buffer;
|
102
|
+
...handleBuffer(buffer),
|
103
|
+
multipart(){
|
104
|
+
return parseMultipart(request, buffer.toString());
|
52
105
|
}
|
53
106
|
} : null);
|
54
107
|
});
|
@@ -81,7 +134,7 @@ module.exports = (context) => {
|
|
81
134
|
|
82
135
|
context.getRealRequest = () => req;
|
83
136
|
|
84
|
-
request.data = await getBody(req);
|
137
|
+
request.data = await getBody(req, request);
|
85
138
|
|
86
139
|
if(this.options.fetch == 'router'){
|
87
140
|
if(!Object.keys(this.options.routers).length) throw new Error('No fetch function nor routers found');
|