|
@@ -1,76 +1,47 @@
|
|
"use strict";
|
|
"use strict";
|
|
const http = require("http");
|
|
const http = require("http");
|
|
const unzip = require("unzip");
|
|
const unzip = require("unzip");
|
|
-var targz = require('tar.gz');
|
|
|
|
-const url = require("url");
|
|
|
|
const fs = require("fs");
|
|
const fs = require("fs");
|
|
|
|
|
|
-class ManifestPreparer {
|
|
|
|
- constructor(options) {
|
|
|
|
- this.steps = options.steps;
|
|
|
|
- this.download = options.download;
|
|
|
|
- this._validateSteps();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- prepare() {
|
|
|
|
- var unzipStep = this.steps[0]["unzip"];
|
|
|
|
- var untarStep = this.steps[1]["untar"];
|
|
|
|
|
|
+function isBinaryFileRequired(flashSpecification, fileName) {
|
|
|
|
+ return flashSpecification.map(binary => binary.path).indexOf(fileName) !== -1;
|
|
|
|
+}
|
|
|
|
|
|
- let fileName = `tmp/${this.download.split("/").pop()}`;
|
|
|
|
|
|
+function addBufferToBinary(flashSpecification, fileName, buffer) {
|
|
|
|
+ flashSpecification.forEach((element, index) => {
|
|
|
|
+ if (flashSpecification[index].path === fileName) {
|
|
|
|
+ flashSpecification[index].buffer = buffer;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+}
|
|
|
|
|
|
- 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));
|
|
|
|
|
|
+function prepareBinaries(manifest, callback) {
|
|
|
|
+ const flashContents = manifest.flash;
|
|
|
|
+ const downloadRequest = http.get(manifest.download, (response) => {
|
|
|
|
+ response.pipe(unzip.Parse()).on('entry', (entry) => {
|
|
|
|
+ const fileName = entry.path;
|
|
|
|
+ if (isBinaryFileRequired(flashContents, fileName)) {
|
|
|
|
+ let body;
|
|
|
|
+ entry.on("data", function(data){
|
|
|
|
+ if(body) {
|
|
|
|
+ body = Buffer.concat([body, data])
|
|
|
|
+ } else {
|
|
|
|
+ body = data;
|
|
|
|
+ }
|
|
|
|
+ }).on("end", () => {
|
|
|
|
+ addBufferToBinary(flashContents, fileName, body);
|
|
|
|
+ }).on("error", callback);
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ entry.autodrain();
|
|
|
|
+ }
|
|
|
|
+ }).on("close", () => {
|
|
|
|
+ console.log("close");
|
|
|
|
+ callback(null, flashContents);
|
|
});
|
|
});
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- unzip(source, files) {
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- untar(source, files) {
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- flash() {
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 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];
|
|
|
|
- }
|
|
|
|
|
|
+ response.on("error", callback);
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-module.exports = {ManifestPreparer};
|
|
|
|
|
|
+module.exports = prepareBinaries;
|