Improve 301 redirect and 404 not found, implement env.status so the jst templates...
authorNick Downing <downing.nick@gmail.com>
Sun, 21 Oct 2018 09:45:32 +0000 (20:45 +1100)
committerNick Downing <downing.nick@gmail.com>
Sun, 21 Oct 2018 10:01:09 +0000 (21:01 +1100)
Server.js
Site.js

index 9925556..cebcce5 100644 (file)
--- a/Server.js
+++ b/Server.js
@@ -72,15 +72,39 @@ Server.prototype.serve = function(response, status, mime_type, data) {
   response.end(data)
 }
 
-Server.prototype.die = function(response) {
-  let body = '<html><body>Page not found</body></html>'
-  this.serve(response, 404, this.mime_type_html, Buffer.from(body))
+Server.prototype.die = function(response, pathname) {
+  this.serve(
+    response,
+    404,
+    this.mime_type_html,
+    Buffer.from(
+      `<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
+<TITLE>404 Not Found</TITLE></HEAD><BODY>
+<H1>404 Not Found</H1>
+The document ${pathname} was not found.
+</BODY></HTML>
+`
+    )
+  )
 }
 
 Server.prototype.redirect = function(response, location) {
   response.statusCode = 301
   response.setHeader('Location', location)
-  response.end('Redirecting to ' + location)
+  this.serve(
+    response,
+    301,
+    this.mime_type_html,
+    Buffer.from(
+      `<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
+<TITLE>301 Moved</TITLE></HEAD><BODY>
+<H1>301 Moved</H1>
+The document has moved
+<A HREF="${location}">here</A>.
+</BODY></HTML>
+`
+    )
+  )
 }
 
 let site_factory_default = async root => new Site(root)
@@ -96,7 +120,7 @@ Server.prototype.respond = async function(request, response, protocol) {
 
     if (!Object.prototype.hasOwnProperty.call(this.sites, parsed_url.hostname)) {
       console.log('nonexistent site', parsed_url.hostname)
-      this.die(response)
+      this.die(response, parsed_url.pathname)
       return
     }
     let temp = this.sites[parsed_url.hostname]
@@ -150,6 +174,7 @@ Server.prototype.respond = async function(request, response, protocol) {
           pathname_pos: 0,
           response: response,
           request: request,
+          status: 200,
           server: this,
           site: site.object
         }
diff --git a/Site.js b/Site.js
index 1ca91b0..8ca2bb4 100644 (file)
--- a/Site.js
+++ b/Site.js
@@ -30,10 +30,9 @@ Site.prototype.get_email = function(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(await fs_readFile(path))
-      )
+      result.value = emailjs.this.server.connect(JSON.parse(text))
     }
   )
 }
@@ -45,8 +44,9 @@ Site.prototype.get_json = function(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(await fs_readFile(path))
+      result.value = JSON.parse(text)
     }
   )
 }
@@ -56,9 +56,10 @@ Site.prototype.get_less = function(dirname, 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(
-        await fs_readFile(path, {encoding: 'utf-8'}),
+        text,
         {
           //color: true,
           //compress: false,
@@ -92,8 +93,9 @@ Site.prototype.get_text = function(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 = await fs_readFile(path, {encoding: 'utf-8'})
+      result.value = text
     }
   )
 }
@@ -120,9 +122,9 @@ Site.prototype.get_zip = function(path) {
   return this.server.build_cache_zip.get(
     path,
     async result => {
-      console.log('getting', path, 'as zip')
-      result.value = {}
       let zipfile = await yauzl_open(path, {autoClose: false})
+      console.log('getting', path, 'as zip')
+
       let entries = []
       await new Promise(
         (resolve, reject) => {
@@ -131,6 +133,8 @@ Site.prototype.get_zip = function(path) {
           on('end', () => resolve())
         }
       )
+
+      result.value = {}
       for (let i = 0; i < entries.length; ++i) {
         let read_stream = await new Promise(
           (resolve, reject) => {
@@ -144,6 +148,7 @@ Site.prototype.get_zip = function(path) {
             )
           }
         )
+
         let write_stream = new stream_buffers.WritableStreamBuffer()
         let data = new Promise(
           (resolve, reject) => {
@@ -153,8 +158,9 @@ Site.prototype.get_zip = function(path) {
           }
         )
         read_stream.pipe(write_stream)
-        let path = '/' + entries[i].fileName
         data = await data
+
+        let path = '/' + entries[i].fileName
         console.log('entry path', path, 'size', data.length)
         result.value[path] = data
       }
@@ -166,6 +172,7 @@ Site.prototype.get_zip = function(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
@@ -175,12 +182,28 @@ Site.prototype.ensure_dir = async function(path) {
 
 // this is for read/write JSON files
 // they will not be reloaded from disk if modified
-Site.prototype.open_json = async function(path, default_value) {
-  return /*await*/ this.server.json_cache.open(this.root + path, default_value)
+Site.prototype.read_json = async function(path, default_value) {
+  return /*await*/ this.server.json_cache.read(
+    this.root + path,
+    default_value
+  )
 }
-Site.prototype.flush_json = async function(path) {
-  return /*await*/ this.server.json_cache.flush(this.root + path)
+Site.prototype.write_json = async function(path, value, timeout) {
+  return /*await*/ this.server.json_cache.write(
+    this.root + path,
+    value,
+    timeout
+  )
 }
+Site.prototype.modify_json =
+  async function(path, default_value, modify_func, timeout) {
+    return /*await*/ this.server.json_cache.modify(
+      this.root + path,
+      default_value,
+      modify_func,
+      timeout
+    )
+  }
 
 Site.prototype.serve_jst = async function(env, pathname) {
   let jst
@@ -198,7 +221,7 @@ Site.prototype.serve_jst = async function(env, pathname) {
   console.log(
     `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from jst`
   )
-  this.server.serve(env.response, 200, env.mime_type, data)
+  this.server.serve(env.response, env.status, env.mime_type, data)
   return true
 }
 
@@ -218,7 +241,7 @@ Site.prototype.serve_less = async function(env, pathname) {
   console.log(
     `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from less`
   )
-  this.server.serve(env.response, 200, env.mime_type, data)
+  this.server.serve(env.response, env.status, env.mime_type, data)
   return true
 }
 
@@ -235,7 +258,7 @@ Site.prototype.serve_fs = async function(env, pathname) {
   console.log(
     `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from fs`
   )
-  this.server.serve(env.response, 200, env.mime_type, data)
+  this.server.serve(env.response, env.status, env.mime_type, data)
   return true
 }
 
@@ -256,7 +279,7 @@ Site.prototype.serve_zip = async function(env, pathname) {
   console.log(
     `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from zip`
   )
-  this.server.serve(env.response, 200, env.mime_type, data)
+  this.server.serve(env.response, env.status, env.mime_type, data)
   return true
 }
 
@@ -296,7 +319,7 @@ Site.prototype.respond = async function(env) {
         console.log(
           `${env.parsed_url.host} empty directory name in ${env.pathname}`
         )
-        this.server.die(env.response)
+        this.server.die(env.response, env.pathname)
       }
       return
     }
@@ -308,7 +331,7 @@ Site.prototype.respond = async function(env) {
       console.log(
         `${env.parsed_url.host} bad component "${filename}" in ${env.pathname}`
       )
-      this.server.die(env.response)
+      this.server.die(env.response, env.pathname)
       return
     }
 
@@ -325,7 +348,7 @@ Site.prototype.respond = async function(env) {
         console.log(
           `${env.parsed_url.host} non-directory filetype "${filetype}" in ${env.pathname}`
         )
-        this.server.die(env.response)
+        this.server.die(env.response, env.pathname)
         return
       }
       env.mime_type = this.server.mime_types[filetype]
@@ -348,7 +371,7 @@ Site.prototype.respond = async function(env) {
       console.log(
         `${env.parsed_url.host} directory not found ${pathname}`
       )
-      this.server.die(env.response)
+      this.server.die(env.response, env.pathname)
       return
     }
     if (!stats.isDirectory()) {
@@ -357,7 +380,7 @@ Site.prototype.respond = async function(env) {
         `${env.parsed_url.host} not directory ${pathname}` :
         `${env.parsed_url.host} unknown filetype "${filetype}" in ${pathname}`
       )
-      this.server.die(env.response)
+      this.server.die(env.response, env.pathname)
       return
     }
   }    
@@ -371,7 +394,7 @@ Site.prototype.respond = async function(env) {
     console.log(
       `${env.parsed_url.host} file not found ${env.pathname}`
     )
-    this.server.die(env.response)
+    this.server.die(env.response, env.pathname)
   }
 }