let assert = require('assert')
+let logjson = (await import('@ndcode/logjson')).default
let EmailJSCache = require('@ndcode/emailjs_cache')
let XDate = require('xdate')
let ZettairCache = require('@ndcode/zettair_cache')
return async (resources, root, prev_site) => {
- let logjson = (await import('@ndcode/logjson')).default
-
- let CustomSite = function(resources, root, options, prev_site) {
- if (!this instanceof CustomSite)
- throw Error('CustomSite is a constructor')
- _jst_server.Site.call(this, resources, root, options, prev_site)
-
- this.database = undefined
- this.database_date = new XDate().toUTCString('yyyyMMdd')
- this.emailjs_cache = undefined
- this.zettair_cache = undefined
- }
+ class CustomSite extends _jst_server.Site {
+ constructor(resources, root, options, prev_site) {
+ super(resources, root, options, prev_site)
+ this.database = undefined
+ this.database_date = new XDate().toUTCString('yyyyMMdd')
+ this.emailjs_cache = undefined
+ this.zettair_cache = undefined
+ }
- CustomSite.prototype = Object.create(_jst_server.Site.prototype)
+ // called when the server starts or the site.jst file is modified
+ // in latter case it will carry over the previously created resource objects
+ async start() {
+ await _jst_server.Site.prototype.start.call(this)
+
+ assert(this.database === undefined)
+ this.database = await this.resources.ref(
+ 'database',
+ async () => {
+ let database = new logjson.Database()
+ await database.open(this.root + '/database.logjson')
+ return database
+ }
+ )
- // called when the server starts or the site.jst file is modified
- // in latter case it will carry over the previously created resource objects
- CustomSite.prototype.start = async function() {
- await _jst_server.Site.prototype.start.call(this)
+ assert(this.emailjs_cache === undefined)
+ this.emailjs_cache = await this.resources.ref(
+ 'emailjs_cache',
+ async () => new EmailJSCache(true)
+ )
- assert(this.database === undefined)
- this.database = await this.resources.ref(
- 'database',
- async () => {
- let database = new logjson.Database()
- await database.open(this.root + '/database.logjson')
- return database
- }
- )
-
- assert(this.emailjs_cache === undefined)
- this.emailjs_cache = await this.resources.ref(
- 'emailjs_cache',
- async () => new EmailJSCache(true)
- )
-
- assert(this.zettair_cache === undefined)
- this.zettair_cache = await this.resources.ref(
- 'zettair_cache',
- async () => new ZettairCache(true)
- )
- }
+ assert(this.zettair_cache === undefined)
+ this.zettair_cache = await this.resources.ref(
+ 'zettair_cache',
+ async () => new ZettairCache(true)
+ )
+ }
- // called when the server starts or the site.jst file is modified
- // in latter case the start() method of the new CustomSite object is called
- // first and then the stop() method of the old CustomSite object, so that the
- // reference counting can keep the resource objects alive during changeover
- CustomSite.prototype.stop = async function() {
- await _jst_server.Site.prototype.stop.call(this)
+ // called when the server starts or the site.jst file is modified
+ // in latter case the start() method of the new CustomSite object is called
+ // first and then the stop() method of the old CustomSite object, so that the
+ // reference counting can keep the resource objects alive during changeover
+ async stop() {
+ await _jst_server.Site.prototype.stop.call(this)
- assert(this.database !== undefined)
- await this.resources.unref('database')
+ assert(this.database !== undefined)
+ await this.resources.unref('database')
- assert(this.emailjs_cache !== undefined)
- await this.resources.unref('emailjs_cache')
+ assert(this.emailjs_cache !== undefined)
+ await this.resources.unref('emailjs_cache')
- assert(this.zettair_cache !== undefined)
- await this.resources.unref('zettair_cache')
- }
+ assert(this.zettair_cache !== undefined)
+ await this.resources.unref('zettair_cache')
+ }
- // called once per second, responsible for cache cleaning and flushing
- CustomSite.prototype.kick = async function() {
- await _jst_server.Site.prototype.kick.call(this)
+ // called once per second, responsible for cache cleaning and flushing
+ async kick() {
+ await _jst_server.Site.prototype.kick.call(this)
+
+ assert(this.database !== undefined)
+ await this.database.kick()
+
+ let new_database_date = new XDate().toUTCString('yyyyMMdd')
+ if (new_database_date !== this.database_date) {
+ console.log(
+ 'rotate database',
+ this.database_date,
+ '->',
+ new_database_date
+ )
+ await this.database.rotate('database.logjson.' + this.database_date)
+ this.database_date = new_database_date
+ }
- assert(this.database !== undefined)
- await this.database.kick()
+ assert(this.emailjs_cache !== undefined)
+ this.emailjs_cache.kick()
- let new_database_date = new XDate().toUTCString('yyyyMMdd')
- if (new_database_date !== this.database_date) {
- console.log(
- 'rotate database',
- this.database_date,
- '->',
- new_database_date
- )
- await this.database.rotate('database.logjson.' + this.database_date)
- this.database_date = new_database_date
+ assert(this.zettair_cache !== undefined)
+ this.zettair_cache.kick()
}
- assert(this.emailjs_cache !== undefined)
- this.emailjs_cache.kick()
-
- assert(this.zettair_cache !== undefined)
- this.zettair_cache.kick()
- }
-
- // retrieves a particular email account (loaded into an emailjs object)
- CustomSite.prototype.get_emailjs = function(pathname) {
- return /*await*/ this.emailjs_cache.get(this.root + pathname)
- }
+ // retrieves a particular email account (loaded into an emailjs object)
+ get_emailjs(pathname) {
+ return /*await*/ this.emailjs_cache.get(this.root + pathname)
+ }
- // retrieves a particular search index (node.js wrapper of a zettair object)
- CustomSite.prototype.get_zettair = function(pathname) {
- return /*await*/ this.zettair_cache.get(this.root + pathname)
- }
+ // retrieves a particular search index (node.js wrapper of a zettair object)
+ get_zettair(pathname) {
+ return /*await*/ this.zettair_cache.get(this.root + pathname)
+ }
- // customize the file search/serving algorithm for this particular website
- CustomSite.prototype.respond = async function(env) {
- if (
- env.parsed_url.pathname === '/node_modules' ||
- env.parsed_url.pathname.slice(0, 14) === '/node_modules/' ||
- env.parsed_url.pathname === '/package.json' ||
- env.parsed_url.pathname === '/package-lock.json'
- ) {
- this.die(env, `banned file ${env.parsed_url.pathname}`)
- return
+ // customize the file search/serving algorithm for this particular website
+ async respond(env) {
+ if (
+ env.parsed_url.pathname === '/node_modules' ||
+ env.parsed_url.pathname.slice(0, 14) === '/node_modules/' ||
+ env.parsed_url.pathname === '/package.json' ||
+ env.parsed_url.pathname === '/package-lock.json'
+ ) {
+ this.die(env, `banned file ${env.parsed_url.pathname}`)
+ return
+ }
+ return /*await*/ _jst_server.Site.prototype.respond.call(this, env)
}
- return /*await*/ _jst_server.Site.prototype.respond.call(this, env)
}
return new CustomSite(