index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * archiver-utils
  3. *
  4. * Copyright (c) 2015 Chris Talkington.
  5. * Licensed under the MIT license.
  6. * https://github.com/archiverjs/archiver-utils/blob/master/LICENSE
  7. */
  8. var fs = require('graceful-fs');
  9. var path = require('path');
  10. var isStream = require('is-stream');
  11. var lazystream = require('lazystream');
  12. var normalizePath = require('normalize-path');
  13. var defaults = require('lodash/defaults');
  14. var Stream = require('stream').Stream;
  15. var PassThrough = require('readable-stream').PassThrough;
  16. var utils = module.exports = {};
  17. utils.file = require('./file.js');
  18. utils.collectStream = function(source, callback) {
  19. var collection = [];
  20. var size = 0;
  21. source.on('error', callback);
  22. source.on('data', function(chunk) {
  23. collection.push(chunk);
  24. size += chunk.length;
  25. });
  26. source.on('end', function() {
  27. var buf = Buffer.alloc(size);
  28. var offset = 0;
  29. collection.forEach(function(data) {
  30. data.copy(buf, offset);
  31. offset += data.length;
  32. });
  33. callback(null, buf);
  34. });
  35. };
  36. utils.dateify = function(dateish) {
  37. dateish = dateish || new Date();
  38. if (dateish instanceof Date) {
  39. dateish = dateish;
  40. } else if (typeof dateish === 'string') {
  41. dateish = new Date(dateish);
  42. } else {
  43. dateish = new Date();
  44. }
  45. return dateish;
  46. };
  47. // this is slightly different from lodash version
  48. utils.defaults = function(object, source, guard) {
  49. var args = arguments;
  50. args[0] = args[0] || {};
  51. return defaults(...args);
  52. };
  53. utils.isStream = function(source) {
  54. return isStream(source);
  55. };
  56. utils.lazyReadStream = function(filepath) {
  57. return new lazystream.Readable(function() {
  58. return fs.createReadStream(filepath);
  59. });
  60. };
  61. utils.normalizeInputSource = function(source) {
  62. if (source === null) {
  63. return Buffer.alloc(0);
  64. } else if (typeof source === 'string') {
  65. return Buffer.from(source);
  66. } else if (utils.isStream(source)) {
  67. // Always pipe through a PassThrough stream to guarantee pausing the stream if it's already flowing,
  68. // since it will only be processed in a (distant) future iteration of the event loop, and will lose
  69. // data if already flowing now.
  70. return source.pipe(new PassThrough());
  71. }
  72. return source;
  73. };
  74. utils.sanitizePath = function(filepath) {
  75. return normalizePath(filepath, false).replace(/^\w+:/, '').replace(/^(\.\.\/|\/)+/, '');
  76. };
  77. utils.trailingSlashIt = function(str) {
  78. return str.slice(-1) !== '/' ? str + '/' : str;
  79. };
  80. utils.unixifyPath = function(filepath) {
  81. return normalizePath(filepath, false).replace(/^\w+:/, '');
  82. };
  83. utils.walkdir = function(dirpath, base, callback) {
  84. var results = [];
  85. if (typeof base === 'function') {
  86. callback = base;
  87. base = dirpath;
  88. }
  89. fs.readdir(dirpath, function(err, list) {
  90. var i = 0;
  91. var file;
  92. var filepath;
  93. if (err) {
  94. return callback(err);
  95. }
  96. (function next() {
  97. file = list[i++];
  98. if (!file) {
  99. return callback(null, results);
  100. }
  101. filepath = path.join(dirpath, file);
  102. fs.stat(filepath, function(err, stats) {
  103. results.push({
  104. path: filepath,
  105. relative: path.relative(base, filepath).replace(/\\/g, '/'),
  106. stats: stats
  107. });
  108. if (stats && stats.isDirectory()) {
  109. utils.walkdir(filepath, base, function(err, res) {
  110. if(err){
  111. return callback(err);
  112. }
  113. res.forEach(function(dirEntry) {
  114. results.push(dirEntry);
  115. });
  116. next();
  117. });
  118. } else {
  119. next();
  120. }
  121. });
  122. })();
  123. });
  124. };