+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
}
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'))
) {
site = {
factory: site_factory,
- object: await site_factory(temp.root)
+ object: await site_factory(this, temp.root)
}
this.site_cache[temp.root] = site
}
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')
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))
)
}
// 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')
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')
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')
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')
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')
// 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) {
// (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
}
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
}
`${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
}
`${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
}
console.log(
`${env.parsed_url.host} redirecting ${env.pathname} to ${pathname}`
)
- env.server.redirect(
+ this.server.redirect(
env.response,
pathname + (env.parsed_url.search || '')
)
console.log(
`${env.parsed_url.host} redirecting ${env.pathname} to ${pathname}`
)
- env.server.redirect(
+ this.server.redirect(
env.response,
pathname + (env.parsed_url.search || '')
)
console.log(
`${env.parsed_url.host} empty directory name in ${env.pathname}`
)
- env.server.die(env.response)
+ this.server.die(env.response)
}
return
}
console.log(
`${env.parsed_url.host} bad component "${filename}" in ${env.pathname}`
)
- env.server.die(env.response)
+ this.server.die(env.response)
return
}
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
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
}
}
console.log(
`${env.parsed_url.host} file not found ${env.pathname}`
)
- env.server.die(env.response)
+ this.server.die(env.response)
}
}