prepare_binaries.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. const request = require("request");
  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 = request(manifest.download)
  20. .pipe(unzip.Parse())
  21. .on('entry', (entry) => {
  22. const fileName = entry.path;
  23. if (isBinaryFileRequired(flashContents, fileName)) {
  24. eventEmitter.emit("entry", {
  25. display: `Extracting ${fileName}`,
  26. stage: "start"
  27. });
  28. let body;
  29. entry.on("data", function(data){
  30. if(body) {
  31. body = Buffer.concat([body, data]);
  32. } else {
  33. body = data;
  34. }
  35. }).on("end", () => {
  36. eventEmitter.emit("entry", {
  37. display: `Extracted ${fileName}`,
  38. stage: "end"
  39. });
  40. addBufferToBinary(flashContents, fileName, body);
  41. }).on("error", callback);
  42. } else {
  43. entry.autodrain();
  44. }
  45. }).on("close", () => {
  46. callback(null, flashContents);
  47. });
  48. return eventEmitter;
  49. }
  50. module.exports = prepareBinaries;