let JSONCache = require('JSONCache')
let Site = require('./Site')
let assert = require('assert')
+let emailjs = require('emailjs')
let fs = require('fs')
let js_template = require('js_template')
+let less = require('less/lib/less-node')
+var stream_buffers = require('stream-buffers')
let url = require('url')
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 Server = function(socket_io, caching) {
if (!this instanceof Server)
this.socket_io.attach(server)
}
-Server.prototype.refresh_config = async function() {
- this.sites = await this.build_cache_json.get(
- 'config/sites.json',
+Server.prototype.get_email = function(path) {
+ return this.build_cache_email.get(
+ path,
async result => {
- result.value = JSON.parse(await fs_readFile('config/sites.json'))
+ let text = await fs_readFile(path, {encoding: 'utf-8'})
+ console.log('getting', path, 'as email')
+ result.value = emailjs.this.connect(JSON.parse(text))
}
)
- this.mime_types = await this.build_cache_json.get(
- 'config/mime_types.json',
+}
+
+// this is for read-only JSON files
+// they will be reloaded from disk if modified
+Server.prototype.get_json = function(path) {
+ return this.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(text)
+ }
+ )
+}
+
+Server.prototype.get_less = function(dirname, path) {
+ return this.build_cache_less.get(
+ path,
async result => {
- result.value = JSON.parse(await fs_readFile('config/mime_types.json'))
+ let text = await fs_readFile(path, {encoding: 'utf-8'})
+ console.log('getting', path, 'as less')
+ let render = await less.render(
+ text,
+ {
+ //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 + dirname],
+ //plugins: [],
+ //reUsePluginManager: true,
+ //rewriteUrls: false,
+ rootpath: this.root//,
+ //strictImports: false,
+ //strictUnits: false,
+ //urlArgs: ''
+ }
+ )
+ result.deps.concat(render.imports)
+ result.value = Buffer.from(render.css)
}
)
+}
+
+Server.prototype.get_text = function(path) {
+ return this.build_cache_text.get(
+ path,
+ async result => {
+ let text = await fs_readFile(path, {encoding: 'utf-8'})
+ console.log('getting', path, 'as text')
+ result.value = text
+ }
+ )
+}
+
+Server.prototype.get_zet = function(path) {
+ return this.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)
+ }
+ )
+}
+
+Server.prototype.get_zip = function(path) {
+ return this.build_cache_zip.get(
+ path,
+ async result => {
+ let zipfile = await yauzl_open(path, {autoClose: false})
+ console.log('getting', path, 'as zip')
+
+ let entries = []
+ await new Promise(
+ (resolve, reject) => {
+ zipfile.
+ on('entry', entry => {entries.push(entry)}).
+ on('end', () => resolve())
+ }
+ )
+
+ result.value = {}
+ 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)
+ data = await data
+
+ let path = '/' + entries[i].fileName
+ console.log('entry path', path, 'size', data.length)
+ result.value[path] = data
+ }
+ await zipfile.close()
+ }
+ )
+}
+
+Server.prototype.ensure_dir = async function(path) {
+ try {
+ await fs_mkdir(path)
+ console.log('create directory', 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
+Server.prototype.read_json = async function(path, default_value) {
+ return /*await*/ this.json_cache.read(
+ path,
+ default_value
+ )
+}
+Server.prototype.write_json = async function(path, value, timeout) {
+ return /*await*/ this.json_cache.write(
+ path,
+ value,
+ timeout
+ )
+}
+Server.prototype.modify_json =
+ async function(path, default_value, modify_func, timeout) {
+ return /*await*/ this.json_cache.modify(
+ path,
+ default_value,
+ modify_func,
+ timeout
+ )
+ }
+
+Server.prototype.refresh_config = async function() {
+ this.sites = await this.get_json('_config/sites.json')
+ this.mime_types = await this.get_json('_config/mime_types.json')
this.mime_type_html =
Object.prototype.hasOwnProperty.call(this.mime_types, '.html') ?
this.mime_types['.html'] :
response: response,
request: request,
status: 200,
+ serve_from: undefined,
server: this,
site: site.object
}
-let BuildCache = require('BuildCache')
-let JSONCache = require('JSONCache')
let assert = require('assert')
-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')
-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(server, root) {
if (!this instanceof Site)
}
Site.prototype.get_email = function(path) {
- path = this.root + 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(text))
- }
- )
+ return /*await*/ this.server.get_email(this.root + 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 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(text)
- }
- )
+ return /*await*/ this.server.get_json(this.root + path)
}
-
Site.prototype.get_less = function(dirname, path) {
- path = this.root + 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(
- text,
- {
- //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 + dirname],
- //plugins: [],
- //reUsePluginManager: true,
- //rewriteUrls: false,
- rootpath: this.root//,
- //strictImports: false,
- //strictUnits: false,
- //urlArgs: ''
- }
- )
- result.deps.concat(render.imports)
- result.value = Buffer.from(render.css)
- }
- )
+ return /*await*/ this.server.get_less(this.root + dirname, this.root + path)
}
-
Site.prototype.get_text = function(path) {
- path = this.root + 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 = text
- }
- )
+ return /*await*/ this.server.get_text(this.root + path)
}
-
Site.prototype.get_zet = function(path) {
- path = this.root + path
- return this.server.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)
- }
- )
+ return /*await*/ this.server.get_zet(this.root + path)
}
-
Site.prototype.get_zip = function(path) {
- path = this.root + path
- return this.server.build_cache_zip.get(
- path,
- async result => {
- let zipfile = await yauzl_open(path, {autoClose: false})
- console.log('getting', path, 'as zip')
-
- let entries = []
- await new Promise(
- (resolve, reject) => {
- zipfile.
- on('entry', entry => {entries.push(entry)}).
- on('end', () => resolve())
- }
- )
-
- result.value = {}
- 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)
- data = await data
-
- let path = '/' + entries[i].fileName
- console.log('entry path', path, 'size', data.length)
- result.value[path] = data
- }
- await zipfile.close()
- }
- )
+ return /*await*/ this.server.get_zip(this.root + 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
- throw err
- }
+ return /*await*/ this.server.ensure_dir(this.root + path)
}
// this is for read/write JSON files
// they will not be reloaded from disk if modified
Site.prototype.read_json = async function(path, default_value) {
- return /*await*/ this.server.json_cache.read(
+ return /*await*/ this.server.read_json(
this.root + path,
default_value
)
}
Site.prototype.write_json = async function(path, value, timeout) {
- return /*await*/ this.server.json_cache.write(
+ return /*await*/ this.server.write_json(
this.root + path,
value,
timeout
}
Site.prototype.modify_json =
async function(path, default_value, modify_func, timeout) {
- return /*await*/ this.server.json_cache.modify(
+ return /*await*/ this.server.modify_json(
this.root + path,
default_value,
modify_func,
throw err
return false
}
+ env.serve_from = 'jst'
let out = []
await jst(env, out)
let data = Buffer.from(out.join(''))
console.log(
- `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from jst`
+ `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from ${env.serve_from}`
)
this.server.serve(env.response, env.status, env.mime_type, data)
return true
let data
try {
- data = await this.get_less(this.root, pathname)
+ data = await this.get_less('', pathname)
}
catch (err) {
if (err.code !== 'ENOENT')