Change to ES6 modules
authorNick Downing <nick@ndcode.org>
Tue, 25 Jan 2022 06:42:04 +0000 (17:42 +1100)
committerNick Downing <nick@ndcode.org>
Tue, 25 Jan 2022 23:51:56 +0000 (10:51 +1100)
Listener.mjs [moved from Listener.js with 95% similarity]
Resources.mjs [moved from Resources.js with 96% similarity]
Server.mjs [moved from Server.js with 96% similarity]
Site.mjs [moved from Site.js with 96% similarity]
cli.js [deleted file]
cli.mjs [new file with mode: 0755]
index.js [deleted file]
index.mjs [new file with mode: 0644]
package.json

similarity index 95%
rename from Listener.js
rename to Listener.mjs
index 8dd0968..0e7c383 100644 (file)
  * IN THE SOFTWARE.
  */
 
-let assert = require('assert')
-let fs = require('fs')
-let http = require('http')
-let https = require('https')
-let util = require('util')
+import assert from 'assert'
+import fs from 'fs'
+import http from 'http'
+import https from 'https'
+import util from 'util'
 
 let fs_readFile = util.promisify(fs.readFile)
 
@@ -111,4 +111,4 @@ Listener.prototype.stop = async function() {
   }
 }
 
-module.exports = Listener
+export default Listener
similarity index 96%
rename from Resources.js
rename to Resources.mjs
index 640da5d..2fd51a3 100644 (file)
@@ -21,7 +21,7 @@
  * IN THE SOFTWARE.
  */
 
-let assert = require('assert')
+import assert from 'assert'
 
 let Resources = function(diag) {
   if (!this instanceof Resources)
@@ -31,7 +31,7 @@ let Resources = function(diag) {
 }
 
 Resources.prototype.ref = async function(key, factory_func, destroy_func) {
-  result = this.map.get(key)
+  let result = this.map.get(key)
   if (result === undefined) {
     result = {
       refs: 0,
@@ -75,4 +75,4 @@ Resources.prototype.unref = async function(key) {
   }
 }
 
-module.exports = Resources
+export default Resources
similarity index 96%
rename from Server.js
rename to Server.mjs
index e8ea272..3f2a2da 100644 (file)
--- a/Server.js
  * IN THE SOFTWARE.
  */
 
-let JSTCache = require('@ndcode/jst_cache')
-let Listener = require('./Listener')
-let Resources = require('./Resources')
-let Site = require('./Site')
-let assert = require('assert')
-let fs = require('fs')
-let jst_server = require('./index')
-let url = require('url')
-let util = require('util')
+import JSTCache from '@ndcode/jst_cache'
+import Listener from './Listener.mjs'
+import Resources from './Resources.mjs'
+import Site from './Site.mjs'
+import assert from 'assert'
+import fs from 'fs'
+import jst_server from './index.mjs'
+import url from 'url'
+import util from 'util'
 
 let fs_readFile = util.promisify(fs.readFile)
 
@@ -305,4 +305,4 @@ Server.prototype.respond = async function(request, response, protocol) {
   }
 }
 
-module.exports = Server
+export default Server
similarity index 96%
rename from Site.js
rename to Site.mjs
index fa5479c..80f7f33 100644 (file)
--- a/Site.js
+++ b/Site.mjs
  * IN THE SOFTWARE.
  */
 
-let JSONCache = require('@ndcode/json_cache')
-let JSONCacheRW = require('@ndcode/json_cache_rw')
-let JSTCache = require('@ndcode/jst_cache')
-let LessCSSCache = require('@ndcode/less_css_cache')
-let MenuCache = require('@ndcode/menu_cache')
-let MinCSSCache = require('@ndcode/min_css_cache')
-let MinJSCache = require('@ndcode/min_js_cache')
-let MinHTMLCache = require('@ndcode/min_html_cache')
-let MinSVGCache = require('@ndcode/min_svg_cache')
-let Resources = require('./Resources')
-let SassCSSCache = require('@ndcode/sass_css_cache')
-let TextCache = require('@ndcode/text_cache')
-let ZipCache = require('@ndcode/zip_cache')
-let assert = require('assert')
-let fs = require('fs')
-let jst_server = require('./index')
-let util = require('util')
+import JSONCache from '@ndcode/json_cache'
+import JSONCacheRW from '@ndcode/json_cache_rw'
+import JSTCache from '@ndcode/jst_cache'
+import LessCSSCache from '@ndcode/less_css_cache'
+import MenuCache from '@ndcode/menu_cache'
+import MinCSSCache from '@ndcode/min_css_cache'
+import MinJSCache from '@ndcode/min_js_cache'
+import MinHTMLCache from '@ndcode/min_html_cache'
+import MinSVGCache from '@ndcode/min_svg_cache'
+import Resources from './Resources.mjs'
+import SassCSSCache from '@ndcode/sass_css_cache'
+import TextCache from '@ndcode/text_cache'
+import ZipCache from '@ndcode/zip_cache'
+import assert from 'assert'
+import fs from 'fs'
+import jst_server from './index.mjs'
+import util from 'util'
 
 let fs_mkdir = util.promisify(fs.mkdir)
 let fs_stat = util.promisify(fs.stat)
@@ -751,4 +751,4 @@ Site.prototype.respond = async function(env) {
   return /*await*/ this.serve_path(env, this.root, components)
 }
 
-module.exports = Site
+export default Site
diff --git a/cli.js b/cli.js
deleted file mode 100755 (executable)
index d0219f7..0000000
--- a/cli.js
+++ /dev/null
@@ -1,66 +0,0 @@
-#!/usr/bin/env node
-
-/*
- * Copyright (C) 2018 Nick Downing <nick@ndcode.org>
- * SPDX-License-Identifier: MIT
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to
- * deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
- */
-
-// must load index first, to avoid circular dependency issue
-let jst_server = require('./index')
-
-let JSTCache = require('@ndcode/jst_cache')
-let Resources = require('./Resources')
-let Server = require('./Server')
-
-;(
-  async () => {
-    let resources = new Resources()
-    let jst_cache = await resources.ref(
-      'jst_cache:.',
-      async () => new JSTCache('.', {_jst_server: jst_server}, true)
-    )
-    let server = undefined
-
-    // refresh the config immediately, then every 1 second,
-    // use setTimeout() instead of setInterval() to avoid bunches
-    // of calls after the computer has been suspended for a while
-    let refresh_config = async () => {
-      try {
-        let config = await jst_cache.get('_config/server.jst', true)
-        if (config !== undefined) {
-          let prev_server = server
-          let new_server = await config(resources, prev_server)
-          await new_server.start() // exception here cancels server change
-          server = new_server
-          if (prev_server !== undefined)
-            await prev_server.stop() // exception here doesn't cancel change
-        }
-        await server.kick()
-      }
-      catch (err) {
-        console.error(err.stack || err.message)
-      }
-      setTimeout(refresh_config, 1000)
-      // returned Promise will be ignored
-    }
-    refresh_config()
-  }
-)()
diff --git a/cli.mjs b/cli.mjs
new file mode 100755 (executable)
index 0000000..2577a19
--- /dev/null
+++ b/cli.mjs
@@ -0,0 +1,58 @@
+#!/usr/bin/env node
+
+/*
+ * Copyright (C) 2018 Nick Downing <nick@ndcode.org>
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+// must load index first, to avoid circular dependency issue
+import jst_server from './index.mjs'
+
+import JSTCache from '@ndcode/jst_cache'
+import Resources from './Resources.mjs'
+import Server from './Server.mjs'
+
+let resources = new Resources()
+let jst_cache = await resources.ref(
+  'jst_cache:.',
+  async () => new JSTCache('.', {_jst_server: jst_server}, true)
+)
+let server = undefined
+
+// refresh the config every 1 second
+while (true) {
+  try {
+    let config = await jst_cache.get('_config/server.jst', true)
+    if (config !== undefined) {
+      let prev_server = server
+      let new_server = await config(resources, prev_server)
+      await new_server.start() // exception here cancels server change
+      server = new_server
+      if (prev_server !== undefined)
+        await prev_server.stop() // exception here doesn't cancel change
+    }
+    await server.kick()
+  }
+  catch (err) {
+    console.error(err.stack || err.message)
+  }
+  await new Promise(resolve => setTimeout(resolve, 1000))
+}
diff --git a/index.js b/index.js
deleted file mode 100644 (file)
index ebd8970..0000000
--- a/index.js
+++ /dev/null
@@ -1,3 +0,0 @@
-exports.Resources = require('./Resources')
-exports.Server = require('./Server')
-exports.Site = require('./Site')
diff --git a/index.mjs b/index.mjs
new file mode 100644 (file)
index 0000000..6ddd31a
--- /dev/null
+++ b/index.mjs
@@ -0,0 +1,5 @@
+import Resources from './Resources.mjs'
+import Server from './Server.mjs'
+import Site from './Site.mjs'
+
+export default {Resources, Server, Site}
index b7227d0..92c77b0 100644 (file)
@@ -18,9 +18,9 @@
     "email": "nick@ndcode.org"
   },
   "bin": {
-    "jst_server": "cli.js"
+    "jst_server": "cli.mjs"
   },
-  "main": "index.js",
+  "main": "index.mjs",
   "engines": {
     "node": ">=0.4.0"
   },