sptc 0.0.33 → 0.0.35

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 CHANGED
@@ -1,39 +1,40 @@
1
1
  # Simple Pretreat Toolkit CLI
2
2
 
3
- This project is developing..
4
-
5
3
  ---
6
4
 
7
5
  ### sptc
8
6
 
9
7
  ```
10
- Usage: sptc [options]
8
+ Usage: sptc [options] <filename>
11
9
 
12
10
  sptc
13
11
 
14
12
  Options:
15
- -V, --version output the version number
16
- -f, --filename <string> Sptc filename.
17
- -d, --defines <string> Macro definition switches. If you want to use multiple switches, separate them with ",".
18
- (default: "")
19
- -h, --help display help for command
13
+ -V, --version output the version number
14
+ -d, --defines <string> Macro definition switches. Separate multiple switches with a comma. (default: "")
15
+ -h, --help display help for command
20
16
  ```
21
17
 
22
18
  ### sptcd
23
19
  ```
24
20
  Usage: sptc http server [options]
25
21
 
26
- A simple http server
22
+ A simple HTTP server
27
23
 
28
24
  Options:
29
25
  -V, --version output the version number
30
- -p, --port <number> Serve port. (default: 9090)
26
+ -p, --port <number> The port to serve on. (default: 9090)
31
27
  -l, --locally Only accepts local connections.
32
- -w, --workdir <string> Specify the working directory. (default: ".")
33
- -r, --router <string> Specify a file as the router entry. If specified, all requests will pass to this file.
34
- -e, --exts <string> Specify the valid extensions of executable sptc files. (default: ".sjs,.shtml")
35
- -n, --workers <number> Workers count. (default: 1)
36
- -s, --slient Slient mode.
28
+ -w, --workdir <string> Specifies the working directory. (default: ".")
29
+ -r, --router <string> Specifies a file to use as a router entry. If specified, all requests will be routed to
30
+ this file.
31
+ -e, --exts <string> Specifies the valid extensions for executable SPTC files. (default: ".s, .sjs, .sptc")
32
+ -n, --workers <number> The number of worker processes. (default: 1)
33
+ -s, --silent Enables silent mode.
34
+ -t, --traverse If the pathname is a directory, executes the index file under the pathname directory if
35
+ one exists. If no index file is found, traverses the directory. This option is ignored
36
+ if a router file is specified.
37
+ -d, --debug Enables debug mode.
37
38
  -h, --help display help for command
38
39
  ```
39
40
 
package/bin/sptc.js CHANGED
@@ -1,25 +1,15 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
- const {executeSptcFile}=require('../engine')
3
+ const {NodeCGI}=require('../dist/httpServer')
4
4
  const {Command}=require('commander')
5
5
  const version=require('../package.json').version
6
6
  const program=new Command()
7
7
  .name(`sptc`)
8
8
  .description(`sptc`)
9
9
  .version(version)
10
- .requiredOption('-f, --filename <string>', 'Sptc filename.')
11
- .option('-d, --defines <string>', 'Macro definition switches. If you want to use multiple switches, separate them with ",".', '')
12
- .action(({
13
- filename,
14
- defines,
15
- })=>{
16
- executeSptcFile(filename, {}, {
17
- write: x=>process.stdout.write(x),
18
- onError: e=>console.log(e),
19
- __DEV__: true,
20
- macroOption: {
21
- defs: defines.split(',').map(x=>x.trim()).filter(x=>x),
22
- },
23
- })
10
+ .arguments('<filename>', 'Sptc filename.')
11
+ .option('-d, --defines <string>', 'Macro definition switches. Separate multiple switches with a comma.', '')
12
+ .action((filename, {defines})=>{
13
+ NodeCGI(filename, defines)
24
14
  })
25
15
  .parse()
package/bin/sptcd.js CHANGED
@@ -1,21 +1,21 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
  const {FastCGI, FastCGI_FPM}=require('../dist/httpServer')
4
4
  const {Command}=require('commander')
5
5
  const version=require('../package.json').version
6
6
  const program=new Command()
7
7
  .name(`sptc http server`)
8
- .description(`A simple http server`)
8
+ .description(`A simple HTTP server`)
9
9
  .version(version)
10
- .requiredOption('-p, --port <number>', 'Serve port.', 9090)
10
+ .requiredOption('-p, --port <number>', 'The port to serve on.', 9090)
11
11
  .option('-l, --locally', 'Only accepts local connections.')
12
- .option('-w, --workdir <string>', 'Specify the working directory.', '.')
13
- .option('-r, --router <string>', 'Specify a file as the router entry. If specified, all requests will pass to this file.')
14
- .option('-e, --exts <string>', 'Specify the valid extensions of executable sptc files.', '.s,.sjs,.sptc')
15
- .option('-n, --workers <number>', 'Workers count.', 1)
16
- .option('-s, --slient', 'Slient mode.')
17
- .option('-t, --traverse', 'If pathname is a directory, execute the index file under the pathname directory if any index file exists. If no index file is found, traverse the directory. If a router file is specified, this option will be ignored.')
18
- .option('-d, --debug', 'Debug mode.')
12
+ .option('-w, --workdir <string>', 'Specifies the working directory.', '.')
13
+ .option('-r, --router <string>', 'Specifies a file to use as a router entry. If specified, all requests will be routed to this file.')
14
+ .option('-e, --exts <string>', 'Specifies the valid extensions for executable SPTC files.', '.s, .sjs, .sptc')
15
+ .option('-n, --workers <number>', 'The number of worker processes.', 1)
16
+ .option('-s, --silent', 'Enables silent mode.')
17
+ .option('-t, --traverse', 'If the pathname is a directory, executes the index file under the pathname directory if one exists. If no index file is found, traverses the directory. This option is ignored if a router file is specified.')
18
+ .option('-d, --debug', 'Enables debug mode.')
19
19
  .action(({
20
20
  port,
21
21
  locally,
@@ -92,6 +92,28 @@ function CGI(req, res, option) {
92
92
 
93
93
  }
94
94
 
95
+ function NodeCGI(filename, defines) {
96
+ const [reqCtx]=buildRequestContext({
97
+ url: filename,
98
+ headers: {},
99
+ }, null, {
100
+ srvDir: __dirname,
101
+ debug: true,
102
+ env: {
103
+ RUNTIME: 'NodeCGI',
104
+ },
105
+ isRouterMode: true,
106
+ })
107
+ executeSptcFile(reqCtx.$_REQUEST_FILE.pathname, reqCtx, {
108
+ write: x=>process.stdout.write(x),
109
+ onError: e=>console.log(e),
110
+ __DEV__: true,
111
+ macroOption: {
112
+ defs: defines.split(',').map(x=>x.trim()).filter(x=>x),
113
+ },
114
+ })
115
+ }
116
+
95
117
  function buildRequestContext(req, res, {srvDir, debug, env, isRouterMode}) {
96
118
  const {fullname, pathname, query}=getRequestFile(req, srvDir)
97
119
  const reqCtx={
@@ -246,4 +268,5 @@ function traverseDirectory(ref, dir, exts, debug) {
246
268
  module.exports={
247
269
  FastCGI,
248
270
  FastCGI_FPM,
271
+ NodeCGI,
249
272
  }
@@ -29,13 +29,59 @@ function buildMacroContext({filename, ...inherits}) {
29
29
  return ctx
30
30
  }
31
31
 
32
+ function evalExpression(exp, defs) {
33
+ let ret={
34
+ value: true,
35
+ symbol: 'and',
36
+ }
37
+ const stack=[]
38
+ for(let i=0; i<exp.length; i++) {
39
+ const c=exp[i]
40
+ if(c==='(') {
41
+ stack.push({...ret})
42
+ ret.value=true
43
+ ret.symbol='and'
44
+ }else if(c===')') {
45
+ const val=stack.pop()
46
+ if(val.symbol==='and') {
47
+ ret.value=val.value && ret.value
48
+ }else if(val.symbol==='or') {
49
+ ret.value=val.value || ret.value
50
+ }
51
+ ret.symbol=val.symbol
52
+ }else if(c===' ') {
53
+ while(exp[i+1]===' ') ++i;
54
+ }else{
55
+ let r=c
56
+ for(;;) {
57
+ const a=exp[i+1]
58
+ if(a && '() '.indexOf(a)===-1) {
59
+ r+=exp[++i]
60
+ }else{
61
+ break
62
+ }
63
+ }
64
+ if(r==='and' || r==='or') {
65
+ ret.symbol=r
66
+ }else{
67
+ if(ret.symbol==='and') {
68
+ ret.value=ret.value && defs.has(r)
69
+ }else if(ret.symbol==='or') {
70
+ ret.value=ret.value || defs.has(r)
71
+ }
72
+ }
73
+ }
74
+ }
75
+ return ret.value
76
+ }
77
+
32
78
  function execute(ctx, ast) {
33
79
  let ret=''
34
80
  for(let i=0; i<ast.length; i++) {
35
81
  const p=ast[i]
36
82
  if(p.type===O_IFDEF) {
37
83
  const {match, consequent, alternate}=p
38
- const sub_tree=ctx.defs.has(match)? consequent: alternate
84
+ const sub_tree=evalExpression(match, ctx.defs)? consequent: alternate
39
85
  ret+=execute(ctx, sub_tree)
40
86
  }else if(p.type===O_STR) {
41
87
  ret+=p.str
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sptc",
3
- "version": "0.0.33",
3
+ "version": "0.0.35",
4
4
  "description": "Simple Pretreat Toolkit CLI",
5
5
  "main": "engine/index.js",
6
6
  "engines": {
@@ -0,0 +1,11 @@
1
+ #ifdef AA and BB
2
+ AA and BB
3
+ #endif
4
+
5
+ #ifdef CC
6
+ CC
7
+ #endif
8
+
9
+ #ifdef DD or (BB and CC)
10
+ DD or (BB and CC)
11
+ #endif