Merge resources into Server, resources.json_cache becomes server.json_cache etc
authorNick Downing <downing.nick@gmail.com>
Sun, 21 Oct 2018 01:34:23 +0000 (12:34 +1100)
committerNick Downing <downing.nick@gmail.com>
Sun, 21 Oct 2018 01:34:23 +0000 (12:34 +1100)
Server.js
Site.js
resources.js [deleted file]

index 9fd9d45..e306d03 100644 (file)
--- a/Server.js
+++ b/Server.js
@@ -1,18 +1,29 @@
+let BuildCache = require('BuildCache')
+let JSONCache = require('JSONCache')
 let Site = require('./Site')
 let assert = require('assert')
 let fs = require('fs')
 let js_template = require('js_template')
 let url = require('url')
 let util = require('util')
-let resources = require('./resources')
 
 let fs_readFile = util.promisify(fs.readFile)
 
 let Server = function(caching) {
   if (!this instanceof Server)
     throw Error('Server is a constructor')
+
   this.caching = caching || false
   this.site_cache = {}
+
+  this.build_cache_email = new BuildCache()
+  this.build_cache_json = new BuildCache()
+  this.build_cache_less = new BuildCache()
+  this.build_cache_text = new BuildCache()
+  this.build_cache_zet = new BuildCache()
+  this.build_cache_zip = new BuildCache()
+  this.json_cache = new JSONCache(true)
+
   this.sites = undefined
   this.mime_types = undefined
   this.mime_type_html = undefined
@@ -20,13 +31,13 @@ let Server = function(caching) {
 }
 
 Server.prototype.refresh_config = async function() {
-  this.sites = await resources.build_cache_json.get(
+  this.sites = await this.build_cache_json.get(
     'config/sites.json',
     async result => {
       result.value = JSON.parse(await fs_readFile('config/sites.json'))
     }
   )
-  this.mime_types = await resources.build_cache_json.get(
+  this.mime_types = await this.build_cache_json.get(
     'config/mime_types.json',
     async result => {
       result.value = JSON.parse(await fs_readFile('config/mime_types.json'))
@@ -107,7 +118,7 @@ Server.prototype.respond = async function(request, response, protocol) {
       ) {
         site = {
           factory: site_factory,
-          object: await site_factory(temp.root)
+          object: await site_factory(this, temp.root)
         }
         this.site_cache[temp.root] = site
       }
diff --git a/Site.js b/Site.js
index 14fe05f..9970e73 100644 (file)
--- a/Site.js
+++ b/Site.js
@@ -6,7 +6,6 @@ let emailjs = require('emailjs')
 let fs = require('fs')
 let js_template = require('js_template')
 let less = require('less/lib/less-node')
-let resources = require('./resources')
 var stream_buffers = require('stream-buffers')
 let util = require('util')
 let url = require('url')
@@ -18,19 +17,20 @@ let fs_readFile = util.promisify(fs.readFile)
 let fs_stat = util.promisify(fs.stat)
 let yauzl_open = util.promisify(yauzl.open)
 
-let Site = function(root) {
+let Site = function(server, root) {
   if (!this instanceof Site)
     throw Error('Site is a constructor')
+  this.server = server
   this.root = root
 }
 
 Site.prototype.get_email = function(path) {
   path = this.root + path
-  return resources.build_cache_email.get(
+  return this.server.build_cache_email.get(
     path,
     async result => {
       console.log('getting', path, 'as email')
-      result.value = emailjs.env.server.connect(
+      result.value = emailjs.this.server.connect(
         JSON.parse(await fs_readFile(path))
       )
     }
@@ -41,7 +41,7 @@ Site.prototype.get_email = function(path) {
 // they will be reloaded from disk if modified
 Site.prototype.get_json = function(path) {
   path = this.root + path
-  return resources.build_cache_json.get(
+  return this.server.build_cache_json.get(
     path,
     async result => {
       console.log('getting', path, 'as json')
@@ -52,7 +52,7 @@ Site.prototype.get_json = function(path) {
 
 Site.prototype.get_less = function(dirname, path) {
   path = this.root + path
-  return resources.build_cache_less.get(
+  return this.server.build_cache_less.get(
     path,
     async result => {
       console.log('getting', path, 'as less')
@@ -88,7 +88,7 @@ Site.prototype.get_less = function(dirname, path) {
 
 Site.prototype.get_text = function(path) {
   path = this.root + path
-  return resources.build_cache_text.get(
+  return this.server.build_cache_text.get(
     path,
     async result => {
       console.log('getting', path, 'as text')
@@ -99,7 +99,7 @@ Site.prototype.get_text = function(path) {
 
 Site.prototype.get_zet = function(path) {
   path = this.root + path
-  return resources.build_cache_zet.get(
+  return this.server.build_cache_zet.get(
     path,
     async result => {
       console.log('getting', path, 'as zet')
@@ -116,7 +116,7 @@ Site.prototype.get_zet = function(path) {
 
 Site.prototype.get_zip = function(path) {
   path = this.root + path
-  return resources.build_cache_zip.get(
+  return this.server.build_cache_zip.get(
     path,
     async result => {
       console.log('getting', path, 'as zip')
@@ -175,10 +175,10 @@ 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*/ resources.json_cache.open(this.root + path, default_value)
+  return /*await*/ this.server.json_cache.open(this.root + path, default_value)
 }
 Site.prototype.flush_json = async function(path) {
-  return /*await*/ resources.json_cache.flush(this.root + path)
+  return /*await*/ this.server.json_cache.flush(this.root + path)
 }
 
 Site.prototype.serve_jst = async function(env, pathname) {
@@ -199,15 +199,15 @@ Site.prototype.serve_jst = async function(env, pathname) {
     // (for files we're guaranteed to be on last pathname component)
     let filetype = env.pathname.slice(env.pathname_pos) 
     assert(
-      Object.prototype.hasOwnProperty.call(env.server.mime_types, filetype)
+      Object.prototype.hasOwnProperty.call(this.server.mime_types, filetype)
     )
-    mime_type = env.server.mime_types[filetype]
+    mime_type = this.server.mime_types[filetype]
   }
   let data = Buffer.from(out.join(''))
   console.log(
     `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from jst`
   )
-  env.server.serve(env.response, 200, mime_type, data)
+  this.server.serve(env.response, 200, mime_type, data)
   return true
 }
 
@@ -227,7 +227,7 @@ Site.prototype.serve_less = async function(env, pathname) {
   console.log(
     `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from less`
   )
-  env.server.serve(env.response, 200, env.server.mime_types['.css'], data)
+  this.server.serve(env.response, 200, this.server.mime_types['.css'], data)
   return true
 }
 
@@ -245,7 +245,7 @@ Site.prototype.serve_fs = async function(env, pathname) {
     `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from fs`
   )
   let filetype = env.pathname.slice(env.pathname_pos)
-  env.server.serve(env.response, 200, env.server.mime_types[filetype], data)
+  this.server.serve(env.response, 200, this.server.mime_types[filetype], data)
   return true
 }
 
@@ -267,7 +267,7 @@ Site.prototype.serve_zip = async function(env, pathname) {
     `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from zip`
   )
   let filetype = env.pathname.slice(env.pathname_pos)
-  env.server.serve(env.response, 200, env.server.mime_types[filetype], data)
+  this.server.serve(env.response, 200, this.server.mime_types[filetype], data)
   return true
 }
 
@@ -278,7 +278,7 @@ Site.prototype.respond = async function(env) {
       console.log(
         `${env.parsed_url.host} redirecting ${env.pathname} to ${pathname}`
       )
-      env.server.redirect(
+      this.server.redirect(
         env.response,
         pathname + (env.parsed_url.search || '')
       )
@@ -298,7 +298,7 @@ Site.prototype.respond = async function(env) {
         console.log(
           `${env.parsed_url.host} redirecting ${env.pathname} to ${pathname}`
         )
-        env.server.redirect(
+        this.server.redirect(
           env.response,
           pathname + (env.parsed_url.search || '')
         )
@@ -307,7 +307,7 @@ Site.prototype.respond = async function(env) {
         console.log(
           `${env.parsed_url.host} empty directory name in ${env.pathname}`
         )
-        env.server.die(env.response)
+        this.server.die(env.response)
       }
       return
     }
@@ -319,7 +319,7 @@ Site.prototype.respond = async function(env) {
       console.log(
         `${env.parsed_url.host} bad component "${filename}" in ${env.pathname}`
       )
-      env.server.die(env.response)
+      this.server.die(env.response)
       return
     }
 
@@ -330,13 +330,13 @@ Site.prototype.respond = async function(env) {
 
     if (
       filetype.length !== 0 &&
-      Object.prototype.hasOwnProperty.call(env.server.mime_types, filetype)
+      Object.prototype.hasOwnProperty.call(this.server.mime_types, filetype)
     ) {
       if (j < env.pathname.length) {
         console.log(
           `${env.parsed_url.host} non-directory filetype "${filetype}" in ${env.pathname}`
         )
-        env.server.die(env.response)
+        this.server.die(env.response)
         return
       }
       env.pathname_pos = i + k // advance to "." at start of filetype
@@ -358,14 +358,14 @@ Site.prototype.respond = async function(env) {
       console.log(
         `${env.parsed_url.host} directory not found: ${pathname}`
       )
-      env.server.die(env.response)
+      this.server.die(env.response)
       return
     }
     if (!stats.isDirectory()) {
       console.log(
         `${env.parsed_url.host} not directory: ${pathname}`
       )
-      env.server.die(env.response)
+      this.server.die(env.response)
       return
     }
   }    
@@ -379,7 +379,7 @@ Site.prototype.respond = async function(env) {
     console.log(
       `${env.parsed_url.host} file not found ${env.pathname}`
     )
-    env.server.die(env.response)
+    this.server.die(env.response)
   }
 }
 
diff --git a/resources.js b/resources.js
deleted file mode 100644 (file)
index 0e4daa5..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-let BuildCache = require('BuildCache')
-let JSONCache = require('JSONCache')
-
-exports.build_cache_email = new BuildCache()
-exports.build_cache_json = new BuildCache()
-exports.build_cache_less = new BuildCache()
-exports.build_cache_text = new BuildCache()
-exports.build_cache_zet = new BuildCache()
-exports.build_cache_zip = new BuildCache()
-exports.json_cache = new JSONCache(true)