sptc 0.0.6 → 0.0.8

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/bin/sptcd.js CHANGED
@@ -11,7 +11,7 @@ const program=new Command()
11
11
  .option('-l, --locally', 'Only accepts local connections.')
12
12
  .option('-w, --workdir <string>', 'Specify the working directory.', '.')
13
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.', '.sjs,.shtml')
14
+ .option('-e, --exts <string>', 'Specify the valid extensions of executable sptc files.', '.s,.sjs,.sptc')
15
15
  .option('-n, --workers <number>', 'Workers count.', 1)
16
16
  .option('-s, --slient', 'Slient mode.')
17
17
  .action(({
@@ -28,12 +28,31 @@ const program=new Command()
28
28
  routerEntry: router,
29
29
  exts: exts.split(',').map(x=>x.trim()).filter(x=>x),
30
30
  debug: true,
31
- slientMode: slient,
32
31
  }]
33
32
  if(workers>1) {
34
33
  FastCGI_FPM(workers, ...argv)
35
34
  }else{
36
35
  FastCGI(...argv)
37
36
  }
37
+
38
+ if(slient || require('cluster').isWorker) return;
39
+ const {getLocalIpv4Addresses}=require('../utils')
40
+ console.log('`sptc-http-server` has been launched with the following option:')
41
+ const o={
42
+ workdir,
43
+ routerEntry: router,
44
+ }
45
+ if(locally) {
46
+ o.serve='127.0.0.1:'+port
47
+ }else{
48
+ o.serves=getLocalIpv4Addresses().map(x=>x+':'+port)
49
+ }
50
+ o.runtime=workers>1? 'FastCGI_FPM': 'FastCGI'
51
+ if(workers>1) {
52
+ o.workers=workers
53
+ }
54
+ console.log(o)
55
+
56
+
38
57
  })
39
58
  .parse()
@@ -3,7 +3,6 @@ const {
3
3
  safe_path,
4
4
  fileExists,
5
5
  getExtension,
6
- getLocalIpv4Addresses,
7
6
  }=require('../utils')
8
7
  const {executeSptcFile}=require('../engine')
9
8
 
@@ -15,6 +14,7 @@ function FastCGI_FPM(n, port, locally, option={}) {
15
14
  option.env={
16
15
  RUNTIME: 'FastCGI_FPM',
17
16
  }
17
+ option.workers=n
18
18
  fpm(n, _=>FastCGI(port, locally, option))
19
19
  }
20
20
 
@@ -24,7 +24,6 @@ function FastCGI(port, locally, option) {
24
24
  const {
25
25
  serverDir='.',
26
26
  routerEntry=null,
27
- slientMode=false,
28
27
  ..._option
29
28
  }=option
30
29
  _option.env=_option.env || {
@@ -34,21 +33,7 @@ function FastCGI(port, locally, option) {
34
33
  _option.router=routerEntry? safe_path(_option.srvDir, routerEntry): null
35
34
  http.createServer((req, res)=>{
36
35
  CGI(req, res, _option)
37
- }).listen(port, locally? '127.0.0.1': '0.0.0.0', _=>{
38
- console.log()
39
- console.log('`sptc-http-server` has been launched with the following option:')
40
- const o={
41
- workdir: _option.srvDir,
42
- routerEntry: _option.router,
43
- }
44
- if(locally) {
45
- o.serve='127.0.0.1:'+port
46
- }else{
47
- o.serves=getLocalIpv4Addresses().map(x=>x+':'+port)
48
- }
49
- console.log(o)
50
- console.log()
51
- })
36
+ }).listen(port, locally? '127.0.0.1': '0.0.0.0')
52
37
  }
53
38
 
54
39
  function CGI(req, res, option) {
@@ -64,13 +49,13 @@ function CGI(req, res, option) {
64
49
  const fn=router || file.fullname
65
50
  if(!fileExists(fn)) {
66
51
  res.writeHead(404, {'content-type': 'text/plain'})
67
- res.end(`\`${router || file.pathname}\` does not exist`)
52
+ res.end(`file does not exist: \`${router || file.pathname}\``)
68
53
  return
69
54
  }
70
55
  const ext=getExtension(fn)
71
56
  if(!exts.includes(ext)) {
72
57
  res.writeHead(403, {'content-type': 'text/plain'})
73
- res.end(`\`${ext}\` does not match`)
58
+ res.end(`extensions does not match: \`${ext}\``)
74
59
  return
75
60
  }
76
61
  executeSptcFile(fn, reqCtx, handler)
@@ -86,7 +71,6 @@ function buildRequestContext(req, res, {srvDir, debug, env}) {
86
71
  fullname,
87
72
  pathname,
88
73
  },
89
- $_DEBUG: debug,
90
74
  $_WORKDIR: srvDir,
91
75
  }
92
76
  reqCtx.$_GLOBAL=reqCtx
@@ -94,9 +78,11 @@ function buildRequestContext(req, res, {srvDir, debug, env}) {
94
78
  const state={
95
79
  statusCode: 200,
96
80
  statusText: 'OK',
97
- responseHeaders: {'content-type': 'text/html'},
81
+ responseHeaders: {'content-type': 'text/html; charset=utf8'},
98
82
  error: null,
99
83
  headerFlushed: false,
84
+
85
+ is_debug: debug,
100
86
  }
101
87
  function flushHeader() {
102
88
  if(state.headerFlushed) return;
@@ -115,8 +101,9 @@ function buildRequestContext(req, res, {srvDir, debug, env}) {
115
101
  flushHeader()
116
102
  }
117
103
  reqCtx.setDebug=x=>{
118
- reqCtx.$_DEBUG=x
104
+ state.is_debug=x
119
105
  }
106
+ reqCtx.isDebug=_=>state.is_debug
120
107
 
121
108
  return [reqCtx, {
122
109
  write: x=>{
@@ -128,7 +115,9 @@ function buildRequestContext(req, res, {srvDir, debug, env}) {
128
115
  res.end()
129
116
  },
130
117
  onError: e=>{
131
- if(reqCtx.$_DEBUG) console.log(e)
118
+ if(state.is_debug) console.log(e)
119
+ flushHeader()
120
+ res.end(state.is_debug? e.stack: '')
132
121
  },
133
122
  }]
134
123
  }
@@ -172,6 +172,9 @@ function buildContext(ctx0, option) {
172
172
  ctx.Sync={
173
173
  Push: (...x)=>{
174
174
  if(priv.isEnd) return;
175
+ for(let i=x.length; i--; ) {
176
+ if(typeof x[i]==='function') x[i]=x[i]()
177
+ }
175
178
  priv.syncs.push(...x)
176
179
  },
177
180
  }
@@ -189,15 +192,18 @@ function buildContext(ctx0, option) {
189
192
 
190
193
  ctx._exports={}
191
194
  ctx.exports=o=>{
195
+ if('__getter__' in o) {
196
+ throw new Error('`__getter__` cannot be overrided')
197
+ }
192
198
  Object.assign(ctx._exports, o)
193
199
  }
194
200
 
195
201
  ctx.eval=code=>(new vm.Script(code)).runInNewContext(ctx)
196
202
 
197
- ctx.include=(inc_filename, payload={})=>{
203
+ ctx.include=(inc_filename, inc_payload={})=>{
198
204
  const inc=path.resolve(ctx0.__dirname, inc_filename)
199
205
  const [_ctx0, _vm]=Compiler.compileSptcFile(inc)
200
- const [_ctx, _]=buildContext({...payload, ..._ctx0}, {
206
+ const [_ctx, _]=buildContext({...ctx0, ...inc_payload, ..._ctx0}, {
201
207
  write: option.write,
202
208
  // end: ctx.end,
203
209
  onError: option.onError,
@@ -205,7 +211,7 @@ function buildContext(ctx0, option) {
205
211
  readAsBinary,
206
212
  })
207
213
  const ret=executeVm(_vm, _ctx, priv)
208
- return [_ctx._exports, x=>ret.constructor('return '+x)()]
214
+ return Object.assign({}, _ctx._exports, {__getter__: (...x)=>ret.constructor('return '+(x.length>1? '['+x.join(',')+']': x))()})
209
215
  }
210
216
 
211
217
  ctx.__autoload=fn=>{
@@ -222,8 +228,8 @@ function buildContext(ctx0, option) {
222
228
  if(priv.__autoload_func) {
223
229
  let inc_fn=priv.__autoload_func(prop)
224
230
  if(inc_fn) {
225
- const [{default: d}, q]=ctx.include(inc_fn)
226
- return priv.__autoload_vars[prop]=d || q(prop)
231
+ const e=ctx.include(inc_fn)
232
+ return priv.__autoload_vars[prop]=e.default || e.__getter__(prop)
227
233
  }
228
234
  }
229
235
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sptc",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "Simple Pretreat Toolkit CLI",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -11,6 +11,6 @@
11
11
  "sptcd": "./bin/sptcd.js"
12
12
  },
13
13
  "dependencies": {
14
- "commander": "^10.0.1"
14
+ "commander": "10.0.1"
15
15
  }
16
16
  }
@@ -37,7 +37,7 @@ __autoload(varname=>{
37
37
  if(varname==='TestModel') return './TestModel.sjs'
38
38
  })
39
39
 
40
- const [v, _]=include('./ee.sjs', {P: 55})
40
+ const v=include('./ee.sjs', {P: 55})
41
41
  echo('['+v.ee+']')
42
42
 
43
43
  Sync.Push((async _=>{
@@ -14,4 +14,4 @@ Sync.Push(new Promise(r=>{
14
14
  echo('xxxxxxxxx')
15
15
  r()
16
16
  }, 2e3)
17
- }))
17
+ }))