Update to js_template.git commit 6d973a9, simplify how jst files are loaded, add...
authorNick Downing <nick@ndcode.org>
Mon, 29 Oct 2018 10:34:25 +0000 (21:34 +1100)
committerNick Downing <nick@ndcode.org>
Mon, 29 Oct 2018 10:34:25 +0000 (21:34 +1100)
Server.js
Site.js

index 3824c8c..1bbd1e9 100644 (file)
--- a/Server.js
+++ b/Server.js
@@ -48,12 +48,12 @@ Server.prototype.attach = function(server, protocol) {
     this.socket_io.attach(server)
 }
 
-Server.prototype.get_email = function(path) {
+Server.prototype.get_email = function(pathname) {
   return this.build_cache_email.get(
-    path,
+    pathname,
     async result => {
-      let text = await fs_readFile(path, {encoding: 'utf-8'})
-      console.log('getting', path, 'as email')
+      let text = await fs_readFile(pathname, {encoding: 'utf-8'})
+      console.log('getting', pathname, 'as email')
       result.value = emailjs.server.connect(JSON.parse(text))
     }
   )
@@ -61,30 +61,34 @@ Server.prototype.get_email = function(path) {
 
 // this is for read-only JSON files
 // they will be reloaded from disk if modified
-Server.prototype.get_json = function(path) {
+Server.prototype.get_json = function(pathname) {
   return this.build_cache_json.get(
-    path,
+    pathname,
     async result => {
-      let text = await fs_readFile(path, {encoding: 'utf-8'})
-      console.log('getting', path, 'as json')
+      let text = await fs_readFile(pathname, {encoding: 'utf-8'})
+      console.log('getting', pathname, 'as json')
       result.value = JSON.parse(text)
     }
   )
 }
 
-Server.prototype.get_less = function(dirname, path) {
+Server.prototype.get_jst = function(dirname, pathname) {
+  return js_template(dirname, dirname, pathname, {_server: this})
+}
+
+Server.prototype.get_less = function(dirname, pathname) {
   return this.build_cache_less.get(
-    path,
+    pathname,
     async result => {
-      let text = await fs_readFile(path, {encoding: 'utf-8'})
-      console.log('getting', path, 'as less')
+      let text = await fs_readFile(pathname, {encoding: 'utf-8'})
+      console.log('getting', pathname, 'as less')
       let render = await less.render(
         text,
         {
           //color: true,
           //compress: false,
           //depends: false,
-          filename: path,
+          filename: pathname,
           //globalVars: null,
           //ieCompat: false,
           //insecure: false,
@@ -92,11 +96,11 @@ Server.prototype.get_less = function(dirname, path) {
           //lint: false,
           //math: 0,
           //modifyVars: null,
-          paths: [this.root + dirname],
+          pathnames: [this.root + dirname],
           //plugins: [],
           //reUsePluginManager: true,
           //rewriteUrls: false,
-          rootpath: this.root//,
+          rootpathname: this.root//,
           //strictImports: false,
           //strictUnits: false,
           //urlArgs: ''
@@ -108,39 +112,39 @@ Server.prototype.get_less = function(dirname, path) {
   )
 }
 
-Server.prototype.get_text = function(path) {
+Server.prototype.get_text = function(pathname) {
   return this.build_cache_text.get(
-    path,
+    pathname,
     async result => {
-      let text = await fs_readFile(path, {encoding: 'utf-8'})
-      console.log('getting', path, 'as text')
+      let text = await fs_readFile(pathname, {encoding: 'utf-8'})
+      console.log('getting', pathname, 'as text')
       result.value = text
     }
   )
 }
 
-Server.prototype.get_zet = function(path) {
+Server.prototype.get_zet = function(pathname) {
   return this.build_cache_zet.get(
-    path,
+    pathname,
     async result => {
-      console.log('getting', path, 'as zet')
+      console.log('getting', pathname, 'as zet')
       result.deps = [
-        path + '.map.0',
-        path + '.param.0',
-        path + '.v.0',
-        path + '.vocab.0'
+        pathname + '.map.0',
+        pathname + '.param.0',
+        pathname + '.v.0',
+        pathname + '.vocab.0'
       ]
-      result.value = new zetjs.Index(path)
+      result.value = new zetjs.Index(pathname)
     }
   )
 }
 
-Server.prototype.get_zip = function(path) {
+Server.prototype.get_zip = function(pathname) {
   return this.build_cache_zip.get(
-    path,
+    pathname,
     async result => {
-      let zipfile = await yauzl_open(path, {autoClose: false})
-      console.log('getting', path, 'as zip')
+      let zipfile = await yauzl_open(pathname, {autoClose: false})
+      console.log('getting', pathname, 'as zip')
 
       let entries = []
       await new Promise(
@@ -177,19 +181,19 @@ Server.prototype.get_zip = function(path) {
         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
+        let pathname = '/' + entries[i].fileName
+        console.log('entry pathname', pathname, 'size', data.length)
+        result.value[pathname] = data
       }
       await zipfile.close()
     }
   )
 }
 
-Server.prototype.ensure_dir = async function(path) {
+Server.prototype.ensure_dir = async function(pathname) {
   try {
-    await fs_mkdir(path)
-    console.log('create directory', path)
+    await fs_mkdir(pathname)
+    console.log('create directory', pathname)
   }
   catch (err) {
     if (err.code !== 'EEXIST') // should check error type
@@ -199,23 +203,23 @@ Server.prototype.ensure_dir = async function(path) {
 
 // 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) {
+Server.prototype.read_json = async function(pathname, default_value) {
   return /*await*/ this.json_cache.read(
-    path,
+    pathname,
     default_value
   )
 }
-Server.prototype.write_json = async function(path, value, timeout) {
+Server.prototype.write_json = async function(pathname, value, timeout) {
   return /*await*/ this.json_cache.write(
-    path,
+    pathname,
     value,
     timeout
   )
 }
 Server.prototype.modify_json =
-  async function(path, default_value, modify_func, timeout) {
+  async function(pathname, default_value, modify_func, timeout) {
     return /*await*/ this.json_cache.modify(
-      path,
+      pathname,
       default_value,
       modify_func,
       timeout
@@ -324,11 +328,7 @@ Server.prototype.respond = async function(request, response, protocol) {
     case 'site':
       let site_factory
       try {
-        site_factory = await js_template(
-          temp.root,
-          temp.root,
-          'site_factory.jst'
-        )
+        site_factory = await this.get_jst(temp.root, 'site_factory.jst')
       }
       catch (err) {
         if (err.code !== 'ENOENT') // note: err.code might be undefined
@@ -364,7 +364,6 @@ Server.prototype.respond = async function(request, response, protocol) {
           response: response,
           request: request,
           status: 200,
-          server: this,
           site: site.object
         }
       )
diff --git a/Site.js b/Site.js
index 5e4152e..0067955 100644 (file)
--- a/Site.js
+++ b/Site.js
@@ -40,48 +40,51 @@ Site.prototype.redirect = function(env, pathname) {
   )
 }
 
-Site.prototype.get_email = function(path) {
-  return /*await*/ this.server.get_email(this.root + path)
+Site.prototype.get_email = function(pathname) {
+  return /*await*/ this.server.get_email(this.root + pathname)
 }
-Site.prototype.get_json = function(path) {
-  return /*await*/ this.server.get_json(this.root + path)
+Site.prototype.get_json = function(pathname) {
+  return /*await*/ this.server.get_json(this.root + pathname)
 }
-Site.prototype.get_less = function(dirname, path) {
-  return /*await*/ this.server.get_less(this.root + dirname, this.root + path)
+Site.prototype.get_jst = function(pathname) {
+  return /*await*/ this.server.get_jst(this.root, this.root + pathname)
 }
-Site.prototype.get_text = function(path) {
-  return /*await*/ this.server.get_text(this.root + path)
+Site.prototype.get_less = function(pathname) {
+  return /*await*/ this.server.get_less(this.root, this.root + pathname)
 }
-Site.prototype.get_zet = function(path) {
-  return /*await*/ this.server.get_zet(this.root + path)
+Site.prototype.get_text = function(pathname) {
+  return /*await*/ this.server.get_text(this.root + pathname)
 }
-Site.prototype.get_zip = function(path) {
-  return /*await*/ this.server.get_zip(this.root + path)
+Site.prototype.get_zet = function(pathname) {
+  return /*await*/ this.server.get_zet(this.root + pathname)
+}
+Site.prototype.get_zip = function(pathname) {
+  return /*await*/ this.server.get_zip(this.root + pathname)
 }
 
-Site.prototype.ensure_dir = async function(path) {
-  return /*await*/ this.server.ensure_dir(this.root + path)
+Site.prototype.ensure_dir = async function(pathname) {
+  return /*await*/ this.server.ensure_dir(this.root + pathname)
 }
 
 // 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) {
+Site.prototype.read_json = async function(pathname, default_value) {
   return /*await*/ this.server.read_json(
-    this.root + path,
+    this.root + pathname,
     default_value
   )
 }
-Site.prototype.write_json = async function(path, value, timeout) {
+Site.prototype.write_json = async function(pathname, value, timeout) {
   return /*await*/ this.server.write_json(
-    this.root + path,
+    this.root + pathname,
     value,
     timeout
   )
 }
 Site.prototype.modify_json =
-  async function(path, default_value, modify_func, timeout) {
+  async function(pathname, default_value, modify_func, timeout) {
     return /*await*/ this.server.modify_json(
-      this.root + path,
+      this.root + pathname,
       default_value,
       modify_func,
       timeout
@@ -91,7 +94,7 @@ Site.prototype.modify_json =
 Site.prototype.serve_jst = async function(env, pathname) {
   let jst
   try {
-    jst = await js_template(this.root, this.root, pathname)
+    jst = await this.get_jst(pathname)
   }
   catch (err) {
     if (err.code !== 'ENOENT')
@@ -108,7 +111,7 @@ Site.prototype.serve_less = async function(env, pathname) {
  
   let data 
   try {
-    data = await this.get_less('', pathname)
+    data = await this.get_less(pathname)
   }
   catch (err) {
     if (err.code !== 'ENOENT')