Get indexing and search working
authorNick Downing <downing.nick@gmail.com>
Sun, 7 Oct 2018 07:13:05 +0000 (18:13 +1100)
committerNick Downing <downing.nick@gmail.com>
Sun, 7 Oct 2018 07:13:05 +0000 (18:13 +1100)
ndserver.js
package.json

index 4b328be..7a55e2c 100755 (executable)
@@ -11,6 +11,7 @@ var less = require('less/lib/less-node')
 var querystring = require('querystring')
 var util = require('util')
 var url = require('url')
+var zetjs = require('zetjs')
 
 var readFileAsync = util.promisify(fs.readFile)
 var statAsync = util.promisify(fs.stat)
@@ -45,6 +46,7 @@ var build_cache_js = new BuildCache()
 var build_cache_json = new BuildCache()
 var build_cache_less = new BuildCache()
 var build_cache_text = new BuildCache()
+var build_cache_zet = new BuildCache()
 
 var serve = function(res, status, mime_type, data) {
   res.statusCode = status
@@ -106,11 +108,7 @@ var app = async function(req, res, protocol) {
   for (var i = 1; i < path.length - 1; ++i) {
     dir_name += '/' + path[i]
     if (path[i].length === 0 || path[i].charAt(0) === '.') {
-      console.log(
-        site,
-        'bad path component',
-        dir_name
-      )
+      console.log(site, 'bad path component', dir_name)
       return die(res)
     }
     try {
@@ -129,20 +127,12 @@ var app = async function(req, res, protocol) {
         catch (err2) {
           if (err2.code !== 'ENOENT')
             throw err2
-          console.log(
-            site,
-            'directory not found',
-            dir_name
-          )
+          console.log(site, 'directory not found', dir_name)
           return die(res)
         }
       }
       if (!stats.isDirectory()) {
-        console.log(
-          site,
-          'not directory',
-          dir_name
-        )
+        console.log(site, 'not directory', dir_name)
         return die(res)
       }
     }
@@ -161,14 +151,14 @@ var app = async function(req, res, protocol) {
   var file_type = temp === -1 ? '' : file_name.substring(temp + 1)
   var mime_type = mime_types[file_type] || mime_type_default
 
-  var site_path = dir_name + '/' + file_name, data
+  var page = dir_name + '/' + file_name, data
   if (dir_name_is_pub) {
     try {
-      data = await readFileAsync(site_root + site_path)
+      data = await readFileAsync(site_root + page)
       console.log(
         site,
         'serving',
-        site_path,
+        page,
         'length',
         data.length,
         'from filesystem'
@@ -182,7 +172,7 @@ var app = async function(req, res, protocol) {
     }
   }
   else {
-    temp = site_path + '.pub'
+    temp = page + '.pub'
     try {
       data = await readFileAsync(site_root + temp)
       console.log(
@@ -203,13 +193,15 @@ var app = async function(req, res, protocol) {
 
     switch (file_type) {
     case 'html':
-      temp = site_path + '.js'
+      temp = page + '.js'
       try {
         var buffers = []
         var env = {
           lang: 'en',
-          page: temp,
-          root: site_root
+          page: page,
+          query: parsed_url.query,
+          site: site,
+          site_root: site_root
         }
         var out = str => {buffers.push(Buffer.from(str))}
         var req = async (str, type) => {
@@ -245,6 +237,23 @@ var app = async function(req, res, protocol) {
               build_cache_text.set(key, result)
             }
             break
+          case 'zet':
+            result = await build_cache_zet.get(key)
+            if (result === undefined) {
+              console.log(site, 'opening', key)
+              result = new zetjs.Index(key)
+              build_cache_zet.set(
+                key,
+                result,
+                [
+                  key + '.map.0',
+                  key + '.param.0',
+                  key + '.v.0',
+                  key + '.vocab.0'
+                ]
+              )
+            }
+            break
           default:
             assert(false) 
           }
@@ -270,7 +279,7 @@ var app = async function(req, res, protocol) {
       break
 
     case 'css':
-      temp = site_path + '.less'
+      temp = page + '.less'
       try {
         var key = site_root + temp
         var data = await build_cache_less.get(key)
@@ -322,11 +331,7 @@ var app = async function(req, res, protocol) {
     }
   }
 
-  console.log(
-    site,
-    'file not found',
-    site_path
-  )
+  console.log(site, 'file not found', page)
   return die(res)
  
   // enable this when we want to serve particular files programmatically:
index da2d9d8..46fc271 100644 (file)
@@ -13,7 +13,8 @@
     "jstemplate": "file:../jstemplate.git/jstemplate-3.4.9.tgz",
     "less": "^3.8.1",
     "querystring": "^0.2.0",
-    "url": "^0.11.0"
+    "url": "^0.11.0",
+    "zetjs": "file:../zettair.git/src/zetjs/zetjs-1.0.0.tgz"
   },
   "devDependencies": {},
   "scripts": {},