-/analytics
/node_modules
-/package-lock.json
+/yarn-lock.json
+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)
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',
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
+let fs = require('fs')
let resources = require('./resources')
+let util = require('util')
+
+let fs_readFile = util.promisify(fs.readFile)
let sites
let mime_types
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 :
"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",
"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": {},
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)