prepare_binaries.js 1.9 KB

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