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