Front end of the Slack clone application.

index.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 nutil = require('util');
  11. var lazystream = require('lazystream');
  12. var normalizePath = require('normalize-path');
  13. var _ = require('lodash');
  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. function assertPath(path) {
  19. if (typeof path !== 'string') {
  20. throw new TypeError('Path must be a string. Received ' + nutils.inspect(path));
  21. }
  22. }
  23. utils.collectStream = function(source, callback) {
  24. var collection = [];
  25. var size = 0;
  26. source.on('error', callback);
  27. source.on('data', function(chunk) {
  28. collection.push(chunk);
  29. size += chunk.length;
  30. });
  31. source.on('end', function() {
  32. var buf = new Buffer(size);
  33. var offset = 0;
  34. collection.forEach(function(data) {
  35. data.copy(buf, offset);
  36. offset += data.length;
  37. });
  38. callback(null, buf);
  39. });
  40. };
  41. utils.dateify = function(dateish) {
  42. dateish = dateish || new Date();
  43. if (dateish instanceof Date) {
  44. dateish = dateish;
  45. } else if (typeof dateish === 'string') {
  46. dateish = new Date(dateish);
  47. } else {
  48. dateish = new Date();
  49. }
  50. return dateish;
  51. };
  52. // this is slightly different from lodash version
  53. utils.defaults = function(object, source, guard) {
  54. var args = arguments;
  55. args[0] = args[0] || {};
  56. return _.defaults.apply(_, args);
  57. };
  58. utils.isStream = function(source) {
  59. return source instanceof Stream;
  60. };
  61. utils.lazyReadStream = function(filepath) {
  62. return new lazystream.Readable(function() {
  63. return fs.createReadStream(filepath);
  64. });
  65. };
  66. utils.normalizeInputSource = function(source) {
  67. if (source === null) {
  68. return new Buffer(0);
  69. } else if (typeof source === 'string') {
  70. return new Buffer(source);
  71. } else if (utils.isStream(source) && !source._readableState) {
  72. var normalized = new PassThrough();
  73. source.pipe(normalized);
  74. return normalized;
  75. }
  76. return source;
  77. };
  78. utils.sanitizePath = function(filepath) {
  79. return normalizePath(filepath, false).replace(/^\w+:/, '').replace(/^(\.\.\/|\/)+/, '');
  80. };
  81. utils.trailingSlashIt = function(str) {
  82. return str.slice(-1) !== '/' ? str + '/' : str;
  83. };
  84. utils.unixifyPath = function(filepath) {
  85. return normalizePath(filepath, false).replace(/^\w+:/, '');
  86. };
  87. utils.walkdir = function(dirpath, base, callback) {
  88. var results = [];
  89. if (typeof base === 'function') {
  90. callback = base;
  91. base = dirpath;
  92. }
  93. fs.readdir(dirpath, function(err, list) {
  94. var i = 0;
  95. var file;
  96. var filepath;
  97. if (err) {
  98. return callback(err);
  99. }
  100. (function next() {
  101. file = list[i++];
  102. if (!file) {
  103. return callback(null, results);
  104. }
  105. filepath = path.join(dirpath, file);
  106. fs.stat(filepath, function(err, stats) {
  107. results.push({
  108. path: filepath,
  109. relative: path.relative(base, filepath).replace(/\\/g, '/'),
  110. stats: stats
  111. });
  112. if (stats && stats.isDirectory()) {
  113. utils.walkdir(filepath, base, function(err, res) {
  114. res.forEach(function(dirEntry) {
  115. results.push(dirEntry);
  116. });
  117. next();
  118. });
  119. } else {
  120. next();
  121. }
  122. });
  123. })();
  124. });
  125. };