manifest.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. const http = require("http");
  3. const unzip = require("unzip");
  4. const fs = require("fs");
  5. function isBinaryFileRequired(flashSpecification, fileName) {
  6. return flashSpecification.map(binary => binary.path).indexOf(fileName) !== -1;
  7. }
  8. function addBufferToBinary(flashSpecification, fileName, buffer) {
  9. flashSpecification.forEach((element, index) => {
  10. if (flashSpecification[index].path === fileName) {
  11. flashSpecification[index].buffer = buffer;
  12. }
  13. });
  14. }
  15. function prepareBinaries(manifest, callback) {
  16. const flashContents = manifest.flash;
  17. const downloadRequest = http.get(manifest.download, (response) => {
  18. response.pipe(unzip.Parse()).on('entry', (entry) => {
  19. const fileName = entry.path;
  20. if (isBinaryFileRequired(flashContents, fileName)) {
  21. let body;
  22. entry.on("data", function(data){
  23. if(body) {
  24. body = Buffer.concat([body, data])
  25. } else {
  26. body = data;
  27. }
  28. }).on("end", () => {
  29. addBufferToBinary(flashContents, fileName, body);
  30. }).on("error", callback);
  31. } else {
  32. entry.autodrain();
  33. }
  34. }).on("close", () => {
  35. console.log("close");
  36. callback(null, flashContents);
  37. });
  38. response.on("error", callback);
  39. });
  40. }
  41. module.exports = prepareBinaries;