Initial commit
authorNick Downing <nick@ndcode.org>
Tue, 20 Nov 2018 00:10:10 +0000 (11:10 +1100)
committerNick Downing <nick@ndcode.org>
Tue, 20 Nov 2018 12:15:55 +0000 (23:15 +1100)
.gitignore [new file with mode: 0644]
LICENSE [new file with mode: 0644]
MinHTMLCache.js [new file with mode: 0644]
package.json [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..96632b3
--- /dev/null
@@ -0,0 +1,5 @@
+/ndcode-less_css_cache-*.tgz
+/node_modules
+/package-lock.json
+/yarn.lock
+/yarn-error.log
diff --git a/LICENSE b/LICENSE
new file mode 100644 (file)
index 0000000..aa360c1
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,20 @@
+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.
diff --git a/MinHTMLCache.js b/MinHTMLCache.js
new file mode 100644 (file)
index 0000000..71e4735
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * 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.
+ */
+
+let BuildCache = require('@ndcode/build_cache')
+let assert = require('assert')
+let disk_build = require('@ndcode/disk_build')
+let fs = require('fs')
+let html_minifier = require('@ndcode/html-minifier')
+let path = require('path')
+let util = require('util')
+
+let fs_readFile = util.promisify(fs.readFile)
+let fs_writeFile = util.promisify(fs.writeFile)
+
+let MinHTMLCache = function(diag) {
+  if (!this instanceof MinHTMLCache)
+    throw Error('MinHTMLCache is a constructor')
+  this.diag = diag || false
+
+  this.build_cache = new BuildCache()
+}
+
+MinHTMLCache.prototype.get = function(key) {
+  return /*await*/ this.build_cache.get(
+    key,
+    async result => {
+      let render = await disk_build(
+        key,
+        async temp_pathname => /*await*/ fs_writeFile(
+          temp_pathname,
+          html_minifier.minify(
+            await fs_readFile(key, {encoding: 'utf-8'}),
+            {
+              collapseWhitespace: true,
+              minifyCSS: true,
+              minifyJS: true
+            }
+          ),
+          {encoding: 'utf-8'}
+        ),
+        this.diag
+      )
+      assert(render.deps.length === 0)
+      result.value = await fs_readFile(render.pathname)
+    }
+  )
+}
+
+module.exports = MinHTMLCache
diff --git a/package.json b/package.json
new file mode 100644 (file)
index 0000000..822bfe5
--- /dev/null
@@ -0,0 +1,36 @@
+{
+  "name": "@ndcode/min_html_cache",
+  "version": "0.1.0",
+  "description": "HTML minimizer, caching front-end with live recompilation.",
+  "keywords": [
+    "Less",
+    "HTML",
+    "server-side",
+    "render",
+    "cache",
+    "live",
+    "recompile"
+  ],
+  "homepage": "https://www.ndcode.org",
+  "repository": {
+    "type": "git",
+    "url": "https://git.ndcode.org/public/min_html_cache.git"
+  },
+  "bugs": {
+    "email": "nick@ndcode.org"
+  },
+  "main": "MinHTMLCache.js",
+  "engines": {
+    "node": ">=0.4.0"
+  },
+  "directories": {},
+  "dependencies": {
+    "@ndcode/build_cache": "^0.1.0",
+    "@ndcode/disk_build": "^0.1.0",
+    "@ndcode/html-minifier": "^3.5.21",
+    "assert": "^1.4.1"
+  },
+  "devDependencies": {},
+  "maintainers": "Nick Downing <nick@ndcode.org>",
+  "license": "MIT"
+}