Move the get_XXX() routines, and the new open_json() and flush_json() routines which...
authorNick Downing <downing.nick@gmail.com>
Sat, 20 Oct 2018 04:53:00 +0000 (15:53 +1100)
committerNick Downing <downing.nick@gmail.com>
Sat, 20 Oct 2018 04:53:38 +0000 (15:53 +1100)
.gitignore
Site.js
config.js
package.json
resources.js

index 477c4b1..d5cdf20 100644 (file)
@@ -1,3 +1,2 @@
-/analytics
 /node_modules
-/package-lock.json
+/yarn-lock.json
diff --git a/Site.js b/Site.js
index f5666df..4b49138 100644 (file)
--- a/Site.js
+++ b/Site.js
@@ -1,15 +1,24 @@
+let BuildCache = require('BuildCache')
+let JSONCache = require('JSONCache')
 let assert = require('assert')
 let config = require('./config')
 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')
 let resources = require('./resources')
 let server = require('./server')
+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(root) {
   if (!this instanceof Site)
@@ -185,11 +194,7 @@ Site.prototype.app = async function(host, req, res, protocol) {
     case 'css':
       temp = page + '.less'
       try {
-        let data = await resources.get_less(
-          this.root + temp,
-          this.root,
-          dir_name
-        )
+        let data = await this.get_less(temp, dir_name)
         console.log(
           host,
           'serving',
@@ -228,4 +233,161 @@ Site.prototype.app = async function(host, req, res, protocol) {
   server.die(res)
 }
 
+Site.prototype.get_email = function(path) {
+  path = this.root + path
+  return resources.build_cache_email.get(
+    path,
+    async result => {
+      console.log('getting', path, 'as email')
+      result.value = emailjs.server.connect(
+        JSON.parse(await fs_readFile(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 resources.build_cache_json.get(
+    path,
+    async result => {
+      console.log('getting', path, 'as json')
+      result.value = JSON.parse(await fs_readFile(path))
+    }
+  )
+}
+
+Site.prototype.get_less = function(path, dir_name) {
+  path = this.root + path
+  return resources.build_cache_less.get(
+    path,
+    async result => {
+      console.log('getting', path, 'as less')
+      let render = await less.render(
+        await fs_readFile(path, {encoding: 'utf-8'}),
+        {
+          //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 + dir_name],
+          //plugins: [],
+          //reUsePluginManager: true,
+          //rewriteUrls: false,
+          rootpath: this.root//,
+          //strictImports: false,
+          //strictUnits: false,
+          //urlArgs: ''
+        }
+      )
+      result.deps.concat(render.imports)
+      result.value = Buffer.from(render.css)
+    }
+  )
+}
+
+Site.prototype.get_text = function(path) {
+  path = this.root + path
+  return resources.build_cache_text.get(
+    path,
+    async result => {
+      console.log('getting', path, 'as text')
+      result.value = await fs_readFile(path, {encoding: 'utf-8'})
+    }
+  )
+}
+
+Site.prototype.get_zet = function(path) {
+  path = this.root + path
+  return resources.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)
+    }
+  )
+}
+
+Site.prototype.get_zip = function(path) {
+  path = this.root + path
+  return resources.build_cache_zip.get(
+    path,
+    async result => {
+      console.log('getting', path, 'as zip')
+      result.value = {}
+      let zipfile = await yauzl_open(path, {autoClose: false})
+      let entries = []
+      await new Promise(
+        (resolve, reject) => {
+          zipfile.
+          on('entry', entry => {entries.push(entry)}).
+          on('end', () => resolve())
+        }
+      )
+      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)
+        let path = '/' + entries[i].fileName
+        data = await data
+        console.log('entry path', path, 'size', data.length)
+        result.value[path] = data
+      }
+      await zipfile.close()
+    }
+  )
+}
+
+Site.prototype.ensure_dir = async function(path) {
+  try {
+    await fs_mkdir(this.root + 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
+Site.prototype.open_json = async function(path, default_value) {
+  return /*await*/ resources.json_cache.open(this.root + path, default_value)
+}
+Site.prototype.flush_json = async function(path) {
+  return /*await*/ resources.json_cache.flush(this.root + path)
+}
+
 module.exports = Site
index f310910..00eee41 100644 (file)
--- a/config.js
+++ b/config.js
@@ -1,4 +1,8 @@
+let fs = require('fs')
 let resources = require('./resources')
+let util = require('util')
+
+let fs_readFile = util.promisify(fs.readFile)
 
 let sites
 let mime_types
@@ -6,8 +10,18 @@ let mime_type_html
 let mime_type_default = 'application/octet-stream'
 
 let refresh = async () => {
-  sites = await resources.get_json('config/sites.json')
-  mime_types = await resources.get_json('config/mime_types.json')
+  sites = await resources.build_cache_json.get(
+    'config/sites.json',
+    async result => {
+      result.value = JSON.parse(await fs_readFile('config/sites.json'))
+    }
+  )
+  mime_types = await resources.build_cache_json.get(
+    'config/mime_types.json',
+    async result => {
+      result.value = JSON.parse(await fs_readFile('config/mime_types.json'))
+    }
+  )
   mime_type_html =
     Object.prototype.hasOwnProperty.call(mime_types, 'html') ?
     mime_types.html :
index 705977a..f444a71 100644 (file)
@@ -5,8 +5,8 @@
   "main": "jst_server.js",
   "directories": {},
   "dependencies": {
-    "BuildCache": "../build_cache.git/BuildCache-1.0.0.tgz",
-    "JSONCache": "../json_cache.git/JSONCache-1.0.0.tgz",
+    "BuildCache": "../build_cache.git/BuildCache-v1.0.0.tgz",
+    "JSONCache": "../json_cache.git/JSONCache-v1.0.0.tgz",
     "babel-cli": "^6.26.0",
     "commander": "^2.18.0",
     "cookie": "^0.3.1",
@@ -21,7 +21,7 @@
     "url": "^0.11.0",
     "xdate": "^0.8.2",
     "yauzl": "^2.10.0",
-    "zetjs": "../zettair.git/src/zetjs/zetjs-1.0.0.tgz"
+    "zetjs": "../zettair.git/src/zetjs/zetjs-v1.0.0.tgz"
   },
   "devDependencies": {},
   "scripts": {},
index c7943bd..0e4daa5 100644 (file)
 let BuildCache = require('BuildCache')
 let JSONCache = require('JSONCache')
-let emailjs = require('emailjs')
-let fs = require('fs')
-let less = require('less/lib/less-node')
-var stream_buffers = require('stream-buffers')
-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 build_cache_email = new BuildCache()
-let get_email = path => build_cache_email.get(
-  path,
-  async result => {
-    console.log('parsing', path)
-    result.value = emailjs.server.connect(
-      JSON.parse(await fs_readFile(path))
-    )
-  }
-)
-
-// this is for read-only JSON files
-// they will be reloaded from disk if modified
-let build_cache_json = new BuildCache()
-let get_json = path => build_cache_json.get(
-  path,
-  async result => {
-    console.log('parsing', path)
-    result.value = JSON.parse(await fs_readFile(path))
-  }
-)
-
-let build_cache_less = new BuildCache()
-let get_less = (path, root, dir_name) => build_cache_less.get(
-  path,
-  async result => {
-    console.log('compiling', path)
-    let render = await less.render(
-      await fs_readFile(path, {encoding: 'utf-8'}),
-      {
-        //color: true,
-        //compress: false,
-        //depends: false,
-        filename: path,
-        //globalVars: null,
-        //ieCompat: false,
-        //insecure: false,
-        //javascriptEnabled: false,
-        //lint: false,
-        //math: 0,
-        //modifyVars: null,
-        paths: [root + dir_name],
-        //plugins: [],
-        //reUsePluginManager: true,
-        //rewriteUrls: false,
-        rootpath: root//,
-        //strictImports: false,
-        //strictUnits: false,
-        //urlArgs: ''
-      }
-    )
-    result.deps.concat(render.imports)
-    result.value = Buffer.from(render.css)
-  }
-)
-
-let build_cache_text = new BuildCache()
-let get_text = path => build_cache_text.get(
-  path,
-  async result => {
-    console.log('reading', path)
-    result.value = await fs_readFile(path, {encoding: 'utf-8'})
-  }
-)
-
-let build_cache_zet = new BuildCache()
-let get_zet = path => build_cache_zet.get(
-  path,
-  async result => {
-    console.log('opening', path)
-    result.deps = [
-      path + '.map.0',
-      path + '.param.0',
-      path + '.v.0',
-      path + '.vocab.0'
-    ]
-    result.value = new zetjs.Index(path)
-  }
-)
-
-let build_cache_zip = new BuildCache()
-let get_zip = path => build_cache_zip.get(
-  path,
-  async result => {
-    console.log('decompressing', path)
-    result.value = {}
-    let zipfile = await yauzl_open(path, {autoClose: false})
-    let entries = []
-    await new Promise(
-      (resolve, reject) => {
-        zipfile.
-        on('entry', entry => {entries.push(entry)}).
-        on('end', () => resolve())
-      }
-    )
-    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)
-      let path = '/' + entries[i].fileName
-      data = await data
-      console.log('entry path', path, 'size', data.length)
-      result.value[path] = data
-    }
-    await zipfile.close()
-  }
-)
-
-// this is for read/write JSON files
-// they will not be reloaded from disk if modified
-let try_mkdir = async path => {
-  try {
-    await fs_mkdir(path)
-  }
-  catch (err) {
-    if (err.code !== 'EEXIST') // should check error type
-      throw err
-  }
-}
-let json_cache = new JSONCache(true)
-
-exports.get_email = get_email
-exports.get_json = get_json
-exports.get_less = get_less
-exports.get_text = get_text
-exports.get_zet = get_zet
-exports.get_zip = get_zip
-exports.try_mkdir = try_mkdir
-exports.json_cache = json_cache
+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)