Move config to _config, ssl to _ssl so they can be at site root, other tidy-ups
authorNick Downing <downing.nick@gmail.com>
Sun, 21 Oct 2018 11:15:15 +0000 (22:15 +1100)
committerNick Downing <downing.nick@gmail.com>
Sun, 21 Oct 2018 11:15:15 +0000 (22:15 +1100)
16 files changed:
Server.js
Site.js
_config/mime_types.json [moved from config/mime_types.json with 100% similarity]
_config/sites.json [moved from config/sites.json with 100% similarity]
_ssl/ca.conf [moved from ssl/ca.conf with 100% similarity]
_ssl/ca_cert.pem [moved from ssl/ca_cert.pem with 100% similarity]
_ssl/ca_cert.srl [moved from ssl/ca_cert.srl with 100% similarity]
_ssl/ca_key.pem [moved from ssl/ca_key.pem with 100% similarity]
_ssl/localhost.conf [moved from ssl/localhost.conf with 100% similarity]
_ssl/localhost_cert.pem [moved from ssl/localhost_cert.pem with 100% similarity]
_ssl/localhost_cert_bundle.pem [moved from ssl/localhost_cert_bundle.pem with 100% similarity]
_ssl/localhost_csr.pem [moved from ssl/localhost_csr.pem with 100% similarity]
_ssl/localhost_ext.conf [moved from ssl/localhost_ext.conf with 100% similarity]
_ssl/localhost_key.pem [moved from ssl/localhost_key.pem with 100% similarity]
_ssl/n.sh [moved from ssl/n.sh with 100% similarity]
jst_server.js

index 336e60e..27cb06d 100644 (file)
--- a/Server.js
+++ b/Server.js
@@ -2,12 +2,19 @@ let BuildCache = require('BuildCache')
 let JSONCache = require('JSONCache')
 let Site = require('./Site')
 let assert = require('assert')
+let emailjs = require('emailjs')
 let fs = require('fs')
 let js_template = require('js_template')
+let less = require('less/lib/less-node')
+var stream_buffers = require('stream-buffers')
 let url = require('url')
 let util = require('util')
+let yauzl = require('yauzl')
+let zetjs = require('zetjs')
 
+let fs_mkdir = util.promisify(fs.mkdir)
 let fs_readFile = util.promisify(fs.readFile)
+let yauzl_open = util.promisify(yauzl.open)
 
 let Server = function(socket_io, caching) {
   if (!this instanceof Server)
@@ -41,19 +48,183 @@ Server.prototype.attach = function(server, protocol) {
     this.socket_io.attach(server)
 }
 
-Server.prototype.refresh_config = async function() {
-  this.sites = await this.build_cache_json.get(
-    'config/sites.json',
+Server.prototype.get_email = function(path) {
+  return this.build_cache_email.get(
+    path,
     async result => {
-      result.value = JSON.parse(await fs_readFile('config/sites.json'))
+      let text = await fs_readFile(path, {encoding: 'utf-8'})
+      console.log('getting', path, 'as email')
+      result.value = emailjs.this.connect(JSON.parse(text))
     }
   )
-  this.mime_types = await this.build_cache_json.get(
-    'config/mime_types.json',
+}
+
+// this is for read-only JSON files
+// they will be reloaded from disk if modified
+Server.prototype.get_json = function(path) {
+  return this.build_cache_json.get(
+    path,
+    async result => {
+      let text = await fs_readFile(path, {encoding: 'utf-8'})
+      console.log('getting', path, 'as json')
+      result.value = JSON.parse(text)
+    }
+  )
+}
+
+Server.prototype.get_less = function(dirname, path) {
+  return this.build_cache_less.get(
+    path,
     async result => {
-      result.value = JSON.parse(await fs_readFile('config/mime_types.json'))
+      let text = await fs_readFile(path, {encoding: 'utf-8'})
+      console.log('getting', path, 'as less')
+      let render = await less.render(
+        text,
+        {
+          //color: true,
+          //compress: false,
+          //depends: false,
+          filename: path,
+          //globalVars: null,
+          //ieCompat: false,
+          //insecure: false,
+          //javascriptEnabled: false,
+          //lint: false,
+          //math: 0,
+          //modifyVars: null,
+          paths: [this.root + dirname],
+          //plugins: [],
+          //reUsePluginManager: true,
+          //rewriteUrls: false,
+          rootpath: this.root//,
+          //strictImports: false,
+          //strictUnits: false,
+          //urlArgs: ''
+        }
+      )
+      result.deps.concat(render.imports)
+      result.value = Buffer.from(render.css)
     }
   )
+}
+
+Server.prototype.get_text = function(path) {
+  return this.build_cache_text.get(
+    path,
+    async result => {
+      let text = await fs_readFile(path, {encoding: 'utf-8'})
+      console.log('getting', path, 'as text')
+      result.value = text
+    }
+  )
+}
+
+Server.prototype.get_zet = function(path) {
+  return this.build_cache_zet.get(
+    path,
+    async result => {
+      console.log('getting', path, 'as zet')
+      result.deps = [
+        path + '.map.0',
+        path + '.param.0',
+        path + '.v.0',
+        path + '.vocab.0'
+      ]
+      result.value = new zetjs.Index(path)
+    }
+  )
+}
+
+Server.prototype.get_zip = function(path) {
+  return this.build_cache_zip.get(
+    path,
+    async result => {
+      let zipfile = await yauzl_open(path, {autoClose: false})
+      console.log('getting', path, 'as zip')
+
+      let entries = []
+      await new Promise(
+        (resolve, reject) => {
+          zipfile.
+          on('entry', entry => {entries.push(entry)}).
+          on('end', () => resolve())
+        }
+      )
+
+      result.value = {}
+      for (let i = 0; i < entries.length; ++i) {
+        let read_stream = await new Promise(
+          (resolve, reject) => {
+            zipfile.openReadStream(
+              entries[i],
+              (err, stream) => {
+                if (err)
+                  reject(err)
+                resolve(stream)
+              }
+            )
+          }
+        )
+
+        let write_stream = new stream_buffers.WritableStreamBuffer()
+        let data = new Promise(
+          (resolve, reject) => {
+            write_stream.
+            on('finish', () => {resolve(write_stream.getContents())}).
+            on('error', () => {reject()})
+          }
+        )
+        read_stream.pipe(write_stream)
+        data = await data
+
+        let path = '/' + entries[i].fileName
+        console.log('entry path', path, 'size', data.length)
+        result.value[path] = data
+      }
+      await zipfile.close()
+    }
+  )
+}
+
+Server.prototype.ensure_dir = async function(path) {
+  try {
+    await fs_mkdir(path)
+    console.log('create directory', path)
+  }
+  catch (err) {
+    if (err.code !== 'EEXIST') // should check error type
+      throw err
+  }
+}
+
+// this is for read/write JSON files
+// they will not be reloaded from disk if modified
+Server.prototype.read_json = async function(path, default_value) {
+  return /*await*/ this.json_cache.read(
+    path,
+    default_value
+  )
+}
+Server.prototype.write_json = async function(path, value, timeout) {
+  return /*await*/ this.json_cache.write(
+    path,
+    value,
+    timeout
+  )
+}
+Server.prototype.modify_json =
+  async function(path, default_value, modify_func, timeout) {
+    return /*await*/ this.json_cache.modify(
+      path,
+      default_value,
+      modify_func,
+      timeout
+    )
+  }
+
+Server.prototype.refresh_config = async function() {
+  this.sites = await this.get_json('_config/sites.json')
+  this.mime_types = await this.get_json('_config/mime_types.json')
   this.mime_type_html =
     Object.prototype.hasOwnProperty.call(this.mime_types, '.html') ?
     this.mime_types['.html'] :
@@ -184,6 +355,7 @@ Server.prototype.respond = async function(request, response, protocol) {
           response: response,
           request: request,
           status: 200,
+          serve_from: undefined,
           server: this,
           site: site.object
         }
diff --git a/Site.js b/Site.js
index 8ca2bb4..53a228b 100644 (file)
--- a/Site.js
+++ b/Site.js
@@ -1,21 +1,10 @@
-let BuildCache = require('BuildCache')
-let JSONCache = require('JSONCache')
 let assert = require('assert')
-let cookie = require('cookie')
-let emailjs = require('emailjs')
 let fs = require('fs')
 let js_template = require('js_template')
-let less = require('less/lib/less-node')
-var stream_buffers = require('stream-buffers')
 let util = require('util')
-let url = require('url')
-let yauzl = require('yauzl')
-let zetjs = require('zetjs')
 
-let fs_mkdir = util.promisify(fs.mkdir)
 let fs_readFile = util.promisify(fs.readFile)
 let fs_stat = util.promisify(fs.stat)
-let yauzl_open = util.promisify(yauzl.open)
 
 let Site = function(server, root) {
   if (!this instanceof Site)
@@ -26,170 +15,38 @@ let Site = function(server, root) {
 }
 
 Site.prototype.get_email = function(path) {
-  path = this.root + path
-  return this.server.build_cache_email.get(
-    path,
-    async result => {
-      let text = await fs_readFile(path, {encoding: 'utf-8'})
-      console.log('getting', path, 'as email')
-      result.value = emailjs.this.server.connect(JSON.parse(text))
-    }
-  )
+  return /*await*/ this.server.get_email(this.root + path)
 }
-
-// this is for read-only JSON files
-// they will be reloaded from disk if modified
 Site.prototype.get_json = function(path) {
-  path = this.root + path
-  return this.server.build_cache_json.get(
-    path,
-    async result => {
-      let text = await fs_readFile(path, {encoding: 'utf-8'})
-      console.log('getting', path, 'as json')
-      result.value = JSON.parse(text)
-    }
-  )
+  return /*await*/ this.server.get_json(this.root + path)
 }
-
 Site.prototype.get_less = function(dirname, path) {
-  path = this.root + path
-  return this.server.build_cache_less.get(
-    path,
-    async result => {
-      let text = await fs_readFile(path, {encoding: 'utf-8'})
-      console.log('getting', path, 'as less')
-      let render = await less.render(
-        text,
-        {
-          //color: true,
-          //compress: false,
-          //depends: false,
-          filename: path,
-          //globalVars: null,
-          //ieCompat: false,
-          //insecure: false,
-          //javascriptEnabled: false,
-          //lint: false,
-          //math: 0,
-          //modifyVars: null,
-          paths: [this.root + dirname],
-          //plugins: [],
-          //reUsePluginManager: true,
-          //rewriteUrls: false,
-          rootpath: this.root//,
-          //strictImports: false,
-          //strictUnits: false,
-          //urlArgs: ''
-        }
-      )
-      result.deps.concat(render.imports)
-      result.value = Buffer.from(render.css)
-    }
-  )
+  return /*await*/ this.server.get_less(this.root + dirname, this.root + path)
 }
-
 Site.prototype.get_text = function(path) {
-  path = this.root + path
-  return this.server.build_cache_text.get(
-    path,
-    async result => {
-      let text = await fs_readFile(path, {encoding: 'utf-8'})
-      console.log('getting', path, 'as text')
-      result.value = text
-    }
-  )
+  return /*await*/ this.server.get_text(this.root + path)
 }
-
 Site.prototype.get_zet = function(path) {
-  path = this.root + path
-  return this.server.build_cache_zet.get(
-    path,
-    async result => {
-      console.log('getting', path, 'as zet')
-      result.deps = [
-        path + '.map.0',
-        path + '.param.0',
-        path + '.v.0',
-        path + '.vocab.0'
-      ]
-      result.value = new zetjs.Index(path)
-    }
-  )
+  return /*await*/ this.server.get_zet(this.root + path)
 }
-
 Site.prototype.get_zip = function(path) {
-  path = this.root + path
-  return this.server.build_cache_zip.get(
-    path,
-    async result => {
-      let zipfile = await yauzl_open(path, {autoClose: false})
-      console.log('getting', path, 'as zip')
-
-      let entries = []
-      await new Promise(
-        (resolve, reject) => {
-          zipfile.
-          on('entry', entry => {entries.push(entry)}).
-          on('end', () => resolve())
-        }
-      )
-
-      result.value = {}
-      for (let i = 0; i < entries.length; ++i) {
-        let read_stream = await new Promise(
-          (resolve, reject) => {
-            zipfile.openReadStream(
-              entries[i],
-              (err, stream) => {
-                if (err)
-                  reject(err)
-                resolve(stream)
-              }
-            )
-          }
-        )
-
-        let write_stream = new stream_buffers.WritableStreamBuffer()
-        let data = new Promise(
-          (resolve, reject) => {
-            write_stream.
-            on('finish', () => {resolve(write_stream.getContents())}).
-            on('error', () => {reject()})
-          }
-        )
-        read_stream.pipe(write_stream)
-        data = await data
-
-        let path = '/' + entries[i].fileName
-        console.log('entry path', path, 'size', data.length)
-        result.value[path] = data
-      }
-      await zipfile.close()
-    }
-  )
+  return /*await*/ this.server.get_zip(this.root + path)
 }
 
 Site.prototype.ensure_dir = async function(path) {
-  try {
-    await fs_mkdir(this.root + path)
-    console.log('create directory', path)
-  }
-  catch (err) {
-    if (err.code !== 'EEXIST') // should check error type
-      throw err
-  }
+  return /*await*/ this.server.ensure_dir(this.root + path)
 }
 
 // this is for read/write JSON files
 // they will not be reloaded from disk if modified
 Site.prototype.read_json = async function(path, default_value) {
-  return /*await*/ this.server.json_cache.read(
+  return /*await*/ this.server.read_json(
     this.root + path,
     default_value
   )
 }
 Site.prototype.write_json = async function(path, value, timeout) {
-  return /*await*/ this.server.json_cache.write(
+  return /*await*/ this.server.write_json(
     this.root + path,
     value,
     timeout
@@ -197,7 +54,7 @@ Site.prototype.write_json = async function(path, value, timeout) {
 }
 Site.prototype.modify_json =
   async function(path, default_value, modify_func, timeout) {
-    return /*await*/ this.server.json_cache.modify(
+    return /*await*/ this.server.modify_json(
       this.root + path,
       default_value,
       modify_func,
@@ -215,11 +72,12 @@ Site.prototype.serve_jst = async function(env, pathname) {
       throw err
     return false
   }
+  env.serve_from = 'jst'
   let out = []
   await jst(env, out)
   let data = Buffer.from(out.join(''))
   console.log(
-    `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from jst`
+    `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from ${env.serve_from}`
   )
   this.server.serve(env.response, env.status, env.mime_type, data)
   return true
@@ -231,7 +89,7 @@ Site.prototype.serve_less = async function(env, pathname) {
  
   let data 
   try {
-    data = await this.get_less(this.root, pathname)
+    data = await this.get_less('', pathname)
   }
   catch (err) {
     if (err.code !== 'ENOENT')
similarity index 100%
rename from config/sites.json
rename to _config/sites.json
similarity index 100%
rename from ssl/ca.conf
rename to _ssl/ca.conf
similarity index 100%
rename from ssl/ca_cert.pem
rename to _ssl/ca_cert.pem
similarity index 100%
rename from ssl/ca_cert.srl
rename to _ssl/ca_cert.srl
similarity index 100%
rename from ssl/ca_key.pem
rename to _ssl/ca_key.pem
similarity index 100%
rename from ssl/localhost.conf
rename to _ssl/localhost.conf
similarity index 100%
rename from ssl/localhost_csr.pem
rename to _ssl/localhost_csr.pem
similarity index 100%
rename from ssl/localhost_key.pem
rename to _ssl/localhost_key.pem
similarity index 100%
rename from ssl/n.sh
rename to _ssl/n.sh
index 24f74b6..f6b08cb 100755 (executable)
@@ -12,12 +12,12 @@ commander.version('1.0.0').option(
   'Enable caching'
 ).option(
   '-j, --ssl-cert [path]',
-  'Set SSL certificate [ssl/localhost_cert_bundle.pem]',
-  'ssl/localhost_cert_bundle.pem'
+  'Set SSL certificate [_ssl/localhost_cert_bundle.pem]',
+  '_ssl/localhost_cert_bundle.pem'
 ).option(
   '-k, --ssl-key [path]',
-  'Set SSL private key [ssl/localhost_key.pem]',
-  'ssl/localhost_key.pem'
+  'Set SSL private key [_ssl/localhost_key.pem]',
+  '_ssl/localhost_key.pem'
 ).option(
   '-p, --http-port [port]',
   'Set HTTP listen port, -1 disable [8080]',