Initial commit
authorNick Downing <nick@ndcode.org>
Tue, 20 Nov 2018 01:22:51 +0000 (12:22 +1100)
committerNick Downing <nick@ndcode.org>
Tue, 20 Nov 2018 12:44:29 +0000 (23:44 +1100)
.gitignore [new file with mode: 0644]
LICENSE [new file with mode: 0644]
ZipCache.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..427c101
--- /dev/null
@@ -0,0 +1,5 @@
+/ndcode-emailjs_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/ZipCache.js b/ZipCache.js
new file mode 100644 (file)
index 0000000..68bce4b
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * 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 stream_buffers = require('stream-buffers')
+let util = require('util')
+let yauzl = require('yauzl')
+
+let yauzl_open = util.promisify(yauzl.open)
+
+let ZipCache = function(diag) {
+  if (!this instanceof ZipCache)
+    throw Error('ZipCache is a constructor')
+  this.diag = diag || false
+
+  this.build_cache = new BuildCache()
+}
+
+ZipCache.prototype.get = function(key) {
+  return /*await*/ this.build_cache.get(
+    key,
+    async result => {
+      let zipfile = await yauzl_open(key, {autoClose: false})
+      if (this.diag)
+        console.log(`loading zip file ${key}`)
+
+      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', err => {reject(err)})
+          }
+        )
+        read_stream.pipe(write_stream)
+        data = await data
+
+        let entry_pathname = '/' + entries[i].fileName
+        if (this.diag)
+          console.log(`entry pathname ${entry_pathname} size ${data.length}`)
+        result.value[entry_pathname] = data
+      }
+      await zipfile.close()
+    }
+  )
+}
+
+module.exports = ZipCache
diff --git a/package.json b/package.json
new file mode 100644 (file)
index 0000000..4bbe9eb
--- /dev/null
@@ -0,0 +1,32 @@
+{
+  "name": "@ndcode/zip_cache",
+  "version": "0.1.0",
+  "description": "Load zip files which can be changed while system is live.",
+  "keywords": [
+    "zip",
+    "persistent",
+    "cache",
+    "live"
+  ],
+  "homepage": "https://www.ndcode.org",
+  "repository": {
+    "type": "git",
+    "url": "https://git.ndcode.org/public/zip_cache.git"
+  },
+  "bugs": {
+    "email": "nick@ndcode.org"
+  },
+  "main": "ZipCache.js",
+  "engines": {
+    "node": ">=0.4.0"
+  },
+  "directories": {},
+  "dependencies": {
+    "@ndcode/build_cache": "^0.1.0",
+    "stream-buffers": "^3.0.2",
+    "yauzl": "^2.10.0"
+  },
+  "devDependencies": {},
+  "maintainers": "Nick Downing <nick@ndcode.org>",
+  "license": "MIT"
+}