sptc 0.0.14 → 0.0.16

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/dist/router.s ADDED
@@ -0,0 +1,46 @@
1
+ <?js
2
+
3
+ const {$error, $alias, $index_action}=include(__SETTING__)
4
+
5
+ function resolve(pathname) {
6
+ let _pathname=pathname
7
+ for(let a in $alias) {
8
+ if(a.indexOf('*')>-1) {
9
+ if(pathname.indexOf(a.substr(0, a.indexOf('*')))===0) {
10
+ _pathname=$alias[a]
11
+ break
12
+ }
13
+ }else if(a===pathname) {
14
+ _pathname=$alias[a]
15
+ break
16
+ }
17
+ }
18
+ return _pathname
19
+ }
20
+
21
+ function _call(x, ...e) {
22
+ if(!x) return;
23
+ const $c=x.substr(1, x.indexOf('/', 1)-1) || x.substr(1)
24
+ const c_len=2+$c.length
25
+ const $a=x.substr(c_len, (x+'/').indexOf('/', c_len)-c_len) || $index_action
26
+ const c=include(__APP__+'/controllers/'+$c+'.s')[$c+'Controller']
27
+ if(isDebug()) {
28
+ console.log('==>', {
29
+ pathname: $_REQUEST_FILE['pathname'],
30
+ Controller: $c,
31
+ Action: $a+'Action',
32
+ })
33
+ }
34
+ const _c=new c(...e)
35
+ return _c[$a+'Action']()
36
+ }
37
+
38
+ async function execute(pathname, ...argv) {
39
+ const unlock=Sync.Lock()
40
+ try{
41
+ await _call(pathname, ...argv)
42
+ }catch(e) {
43
+ await _call($error, e)
44
+ }
45
+ unlock()
46
+ }
package/engine/index.js CHANGED
@@ -3,7 +3,7 @@ const Interpreter=require('./interpreter')
3
3
  const CompilerMacro=require('./compiler-macro')
4
4
  const InterpreterMacro=require('./interpreter-macro')
5
5
 
6
- const {md5}=require('../utils')
6
+ const {md5, formatOutput}=require('../utils')
7
7
 
8
8
  function executeSptcFile(filename, payload, option={}) {
9
9
  const {__DEV__, macroOption, mockFileContent}=option
@@ -35,14 +35,7 @@ function executeSptcFileEx(...x) {
35
35
  }
36
36
  x[2].end=_=>{
37
37
  end && end()
38
- const xbuf=[]
39
- const is_str=typeof buf[0]==='string'
40
- for(let x of buf) {
41
- if(is_str && typeof x!=='string') x=x.toString('utf8')
42
- if(!is_str && typeof x==='string') x=Buffer.concat(x)
43
- xbuf.push(x)
44
- }
45
- resolve(is_str? xbuf.join(''): Buffer.concat(xbuf))
38
+ resolve(formatOutput(buf))
46
39
  }
47
40
  x[2].onError=e=>{
48
41
  onError && onError(e)
@@ -2,7 +2,8 @@ const _module=require('module')
2
2
  const vm=require('vm')
3
3
  const path=require('path')
4
4
  const Compiler=require('./compiler')
5
- const {generate_uuid, PromiseWithResolvers}=require('../utils')
5
+ const Utils=require('../utils')
6
+ const {generate_uuid, PromiseWithResolvers, formatOutput}=Utils
6
7
 
7
8
  /*
8
9
  const tt={x: 2, b: [2, 5], c: _=>2, d: /xx/ig}
@@ -125,6 +126,7 @@ function securityPatch() {
125
126
  }
126
127
 
127
128
  const NOOP=_=>null
129
+ const Application={}
128
130
  function buildContext(ctx0, option) {
129
131
 
130
132
  const {
@@ -155,6 +157,10 @@ function buildContext(ctx0, option) {
155
157
  isMaster: !masterPriv,
156
158
  })
157
159
 
160
+ ctx.Application=Application
161
+
162
+ ctx.Utils=Utils
163
+
158
164
  // Dynamically set the cache configuration.
159
165
  ctx.configSptcFileCache=(cacheVersion, cacheSeconds)=>{
160
166
  if(!isEntry) {
@@ -170,6 +176,13 @@ function buildContext(ctx0, option) {
170
176
  priv.echos.push(...x)
171
177
  }
172
178
 
179
+ ctx.readEchoed=as_str=>{
180
+ const {echos}=priv
181
+ const buf=[]
182
+ echos.splice(0).map(x=>buf.push(toWritable(x)))
183
+ return formatOutput(buf, as_str)
184
+ }
185
+
173
186
  ctx.flush=_=>{
174
187
  if(priv.isEnd) return;
175
188
  const {echos}=priv
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sptc",
3
- "version": "0.0.14",
3
+ "version": "0.0.16",
4
4
  "description": "Simple Pretreat Toolkit CLI",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -0,0 +1,4 @@
1
+ <?js
2
+ echo('444')
3
+ console.log(readEchoed(false))
4
+ echo(1)
package/utils/base.js CHANGED
@@ -7,6 +7,10 @@ function readTextFile(filename) {
7
7
  return fs.readFileSync(filename, 'utf8')
8
8
  }
9
9
 
10
+ function sleep(t) {
11
+ return new Promise(r=>setTimeout(r, t))
12
+ }
13
+
10
14
  function mtime(filename) {
11
15
  return fs.statSync(filename).mtime.getTime()
12
16
  }
@@ -64,6 +68,7 @@ function PromiseWithResolvers() {
64
68
 
65
69
  module.exports={
66
70
  readTextFile,
71
+ sleep,
67
72
  mtime,
68
73
  md5,
69
74
  safe_path,
@@ -0,0 +1,17 @@
1
+
2
+ function formatOutput(buf, as_str=undefined) {
3
+ const xbuf=[]
4
+ const is_str=as_str===undefined?
5
+ typeof buf[0]==='string':
6
+ as_str
7
+ for(let x of buf) {
8
+ if(is_str && typeof x!=='string') x=x.toString('utf8')
9
+ if(!is_str && typeof x==='string') x=Buffer.from(x)
10
+ xbuf.push(x)
11
+ }
12
+ return is_str? xbuf.join(''): Buffer.concat(xbuf)
13
+ }
14
+
15
+ module.exports={
16
+ formatOutput,
17
+ }
package/utils/index.js CHANGED
@@ -4,4 +4,5 @@ module.exports={
4
4
  ...require('./base'),
5
5
  ...require('./fpm'),
6
6
  ...require('./dir'),
7
+ ...require('./format'),
7
8
  }