sptc 0.0.22 → 0.0.24

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
@@ -15,6 +15,7 @@ const program=new Command()
15
15
  .option('-n, --workers <number>', 'Workers count.', 1)
16
16
  .option('-s, --slient', 'Slient mode.')
17
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.')
18
19
  .action(({
19
20
  port,
20
21
  locally,
@@ -24,6 +25,7 @@ const program=new Command()
24
25
  workers,
25
26
  slient,
26
27
  traverse,
28
+ debug,
27
29
  })=>{
28
30
  const disableLog=slient || require('cluster').isWorker
29
31
 
@@ -37,8 +39,11 @@ const program=new Command()
37
39
  const argv=[port, locally, {
38
40
  serverDir: workdir,
39
41
  routerEntry: router,
40
- exts: exts.split(',').map(x=>x.trim()).filter(x=>x),
41
- debug: true,
42
+ exts: exts.split(',').map(x=>{
43
+ const r=x.trim()
44
+ return r.indexOf('.')===0? r: '.'+r
45
+ }).filter(x=>x),
46
+ debug,
42
47
  traverse,
43
48
  }]
44
49
 
@@ -52,6 +57,7 @@ const program=new Command()
52
57
  const {getLocalIpv4Addresses}=require('../utils')
53
58
  console.log('`sptc-http-server` has been launched with the following option:')
54
59
  const o={
60
+ debug,
55
61
  workdir,
56
62
  routerEntry: router || null,
57
63
  }
@@ -59,8 +59,12 @@ function CGI(req, res, option) {
59
59
  function _executeFile(fn) {
60
60
  const ext=getExtension(fn)
61
61
  if(!exts.includes(ext)) {
62
- res.writeHead(403, {'content-type': 'text/plain'})
63
- res.end(`extensions does not match: \`${ext}\``)
62
+ if(!debug) {
63
+ res.writeHead(403, {'content-type': 'text/plain'})
64
+ res.end(`extensions does not match: \`${ext}\``)
65
+ }else{
66
+ fsResponse(fn, res)
67
+ }
64
68
  return
65
69
  }
66
70
  executeSptcFile(fn, reqCtx, handler)
@@ -71,7 +75,7 @@ function CGI(req, res, option) {
71
75
  return
72
76
  }
73
77
 
74
- if(!traverse) {
78
+ if(!debug && !traverse) {
75
79
  res.writeHead(404, {'content-type': 'text/html'})
76
80
  res.end(`<h3>File does not exist: \`${router || file.pathname}\`</h3>`)
77
81
  return
@@ -84,7 +88,7 @@ function CGI(req, res, option) {
84
88
  }
85
89
 
86
90
  res.writeHead(404, {'content-type': 'text/html'})
87
- res.end(traverseDirectory(srvDir, file.pathname, exts))
91
+ res.end(traverseDirectory(srvDir, file.pathname, exts, debug))
88
92
 
89
93
  }
90
94
 
@@ -118,7 +122,7 @@ function buildRequestContext(req, res, {srvDir, debug, env, isRouterMode}) {
118
122
  }
119
123
 
120
124
  reqCtx.setStatus=(code, text)=>{
121
- state.statusCode=200
125
+ state.statusCode=code
122
126
  if(text!==undefined) state.statusText=text
123
127
  }
124
128
  reqCtx.setResponseHeaders=headers=>{
@@ -182,8 +186,22 @@ function createWithCacheStore() {
182
186
  return withCache
183
187
  }
184
188
 
189
+ function fsResponse(fn, res) {
190
+ const fs=require('fs')
191
+ const ext=getExtension(fn)
192
+ res.writeHead(200, {
193
+ 'content-type': ({
194
+ '.js': 'text/javascript',
195
+ '.css': 'text/css',
196
+ '.png': 'image/png',
197
+ '.jpg': 'image/jpeg',
198
+ '.html': 'text/html',
199
+ })[ext] || 'application/octet-stream',
200
+ })
201
+ fs.createReadStream(fn).pipe(res)
202
+ }
185
203
 
186
- function traverseDirectory(ref, dir, exts) {
204
+ function traverseDirectory(ref, dir, exts, debug) {
187
205
  let ret=`
188
206
  <!doctype html>
189
207
  <meta charset=utf8 />
@@ -198,7 +216,7 @@ function traverseDirectory(ref, dir, exts) {
198
216
  ret+=`<a class="dir" href="${encodeURIComponent(dirs[i].fn)}/">${dirs[i].fn}</a> <br/>`
199
217
  }
200
218
  for(let i=0; i<files.length; i++) {
201
- if(exts.includes(getExtension(files[i].fn))) {
219
+ if(debug || exts.includes(getExtension(files[i].fn))) {
202
220
  ret+=`<a href="${encodeURIComponent(files[i].fn)}">${files[i].fn}</a> <br/>`
203
221
  }else{
204
222
  ret+=`<b>${files[i].fn}</b> ${files[i].size} <br/>`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sptc",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "description": "Simple Pretreat Toolkit CLI",
5
5
  "main": "",
6
6
  "engines": {