Преглед на файлове

Downloads, unzip and untar without disk i/o

Andrew Chalkley преди 9 години
родител
ревизия
e62fc33f3e
променени са 3 файла, в които са добавени 52 реда и са изтрити 6 реда
  1. 48 3
      manifestly/manifest.js
  2. 1 1
      manifestly/manifest.json
  3. 3 2
      package.json

+ 48 - 3
manifestly/manifest.js

@@ -1,19 +1,49 @@
 "use strict";
+const http = require("http");
+const unzip = require("unzip");
+var targz = require('tar.gz');
+const url = require("url");
+const fs = require("fs");
 
 class ManifestPreparer {
     constructor(options) {
-        this.recipes = options.recipes;
+        this.steps = options.steps;
         this.download = options.download;
+        this._validateSteps();
     }
 
     prepare() {
-        console.dir(this.recipes);
+        var unzipStep = this.steps[0]["unzip"];
+        var untarStep = this.steps[1]["untar"];
+
+        let fileName = `tmp/${this.download.split("/").pop()}`;
+
+        const downloadRequest = http.get(this.download, (response) => {
+            var body = "";
+            response.pipe(unzip.Parse()).on('entry', (entry) => {
+                const fileName = entry.path;
+                if (unzipStep.files.indexOf(fileName) !== -1) {
+                    entry.pipe(targz().createParseStream()).on('entry', (tarEntry) => {
+                        const fileName = tarEntry.path.split("/").pop();
+                        if (untarStep.files.indexOf(fileName) !== -1 ) {
+                            tarEntry.pipe(fs.createWriteStream(`tmp/${fileName}`));
+                        }
+                    });
+                } else {
+                    entry.autodrain();
+                }
+            });
+
+            response.on("error", (e) => console.error(e));
+        });
     }
 
+
     unzip(source, files) {
 
     }
 
+
     untar(source, files) {
 
     }
@@ -22,8 +52,23 @@ class ManifestPreparer {
 
     }
 
-    updateAddresses(){
+    /**
+     * Checks if the step functionality from the manifest.json file exists
+     * in the {ManifestPreparer}
+     * @private
+     */
+    _validateSteps() {
+        this.steps
+            .map(this._getStepName)
+            .forEach(step => {
+                if (!(typeof this[step] === "function")) {
+                    throw `${step} is not a valid step`;
+                }
+            });
+    }
 
+    _getStepName(step) {
+        return Object.keys(step)[0];
     }
 }
 

+ 1 - 1
manifestly/manifest.json

@@ -3,7 +3,7 @@
   "board": "ESP8266 ESP-12",
   "description": "Official Binaries for the Espruino Runtime for the ESP8266 MCU ESP-12",
   "download": "http://www.espruino.com/files/espruino_1v85.zip",
-  "recipes": [
+  "steps": [
     {
       "unzip": {
         "source": "espruino_1v85.zip",

+ 3 - 2
package.json

@@ -24,10 +24,11 @@
   },
   "homepage": "https://github.com/thingsSDK/especially-flasher#readme",
   "dependencies": {
-    "node-fetch": "^1.4.0",
     "bunyan": "^1.8.0",
     "node-binary": "^1.1.0",
-    "serialport": "^2.0.7-beta1"
+    "serialport": "^2.0.7-beta1",
+    "tar.gz": "^1.0.3",
+    "unzip": "^0.1.11"
   },
   "devDependencies": {
     "electron-prebuilt": "^0.36.9"