PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/memory-fs/lib/MemoryFileSystem.js

https://gitlab.com/limorelv/trelloApp
JavaScript | 315 lines | 304 code | 7 blank | 4 comment | 6 complexity | f097c1fda7119cccb70fc5ba2aeaf6fa MD5 | raw file
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var normalize = require("./normalize");
  6. var errors = require("errno");
  7. var stream = require("readable-stream");
  8. var ReadableStream = stream.Readable;
  9. var WritableStream = stream.Writable;
  10. function MemoryFileSystemError(err, path) {
  11. Error.call(this)
  12. if (Error.captureStackTrace)
  13. Error.captureStackTrace(this, arguments.callee)
  14. this.code = err.code;
  15. this.errno = err.errno;
  16. this.message = err.description;
  17. this.path = path;
  18. }
  19. MemoryFileSystemError.prototype = new Error();
  20. function MemoryFileSystem(data) {
  21. this.data = data || {};
  22. }
  23. module.exports = MemoryFileSystem;
  24. function isDir(item) {
  25. if(typeof item !== "object") return false;
  26. return item[""] === true;
  27. }
  28. function isFile(item) {
  29. if(typeof item !== "object") return false;
  30. return !item[""];
  31. }
  32. function pathToArray(path) {
  33. path = normalize(path);
  34. var nix = /^\//.test(path);
  35. if(!nix) {
  36. if(!/^[A-Za-z]:/.test(path)) {
  37. throw new MemoryFileSystemError(errors.code.EINVAL, path);
  38. }
  39. path = path.replace(/[\\\/]+/g, "\\"); // multi slashs
  40. path = path.split(/[\\\/]/);
  41. path[0] = path[0].toUpperCase();
  42. } else {
  43. path = path.replace(/\/+/g, "/"); // multi slashs
  44. path = path.substr(1).split("/");
  45. }
  46. if(!path[path.length-1]) path.pop();
  47. return path;
  48. }
  49. function trueFn() { return true; }
  50. function falseFn() { return false; }
  51. MemoryFileSystem.prototype.meta = function(_path) {
  52. var path = pathToArray(_path);
  53. var current = this.data;
  54. for(var i = 0; i < path.length - 1; i++) {
  55. if(!isDir(current[path[i]]))
  56. return null;
  57. current = current[path[i]];
  58. }
  59. return current[path[i]];
  60. }
  61. MemoryFileSystem.prototype.existsSync = function(_path) {
  62. return !!this.meta(_path);
  63. }
  64. MemoryFileSystem.prototype.statSync = function(_path) {
  65. var current = this.meta(_path);
  66. if(_path === "/" || isDir(current)) {
  67. return {
  68. isFile: falseFn,
  69. isDirectory: trueFn,
  70. isBlockDevice: falseFn,
  71. isCharacterDevice: falseFn,
  72. isSymbolicLink: falseFn,
  73. isFIFO: falseFn,
  74. isSocket: falseFn
  75. };
  76. } else if(isFile(current)) {
  77. return {
  78. isFile: trueFn,
  79. isDirectory: falseFn,
  80. isBlockDevice: falseFn,
  81. isCharacterDevice: falseFn,
  82. isSymbolicLink: falseFn,
  83. isFIFO: falseFn,
  84. isSocket: falseFn
  85. };
  86. } else {
  87. throw new MemoryFileSystemError(errors.code.ENOENT, _path);
  88. }
  89. };
  90. MemoryFileSystem.prototype.readFileSync = function(_path, encoding) {
  91. var path = pathToArray(_path);
  92. var current = this.data;
  93. for(var i = 0; i < path.length - 1; i++) {
  94. if(!isDir(current[path[i]]))
  95. throw new MemoryFileSystemError(errors.code.ENOENT, _path);
  96. current = current[path[i]];
  97. }
  98. if(!isFile(current[path[i]])) {
  99. if(isDir(current[path[i]]))
  100. throw new MemoryFileSystemError(errors.code.EISDIR, _path);
  101. else
  102. throw new MemoryFileSystemError(errors.code.ENOENT, _path);
  103. }
  104. current = current[path[i]];
  105. return encoding ? current.toString(encoding) : current;
  106. };
  107. MemoryFileSystem.prototype.readdirSync = function(_path) {
  108. if(_path === "/") return Object.keys(this.data).filter(Boolean);
  109. var path = pathToArray(_path);
  110. var current = this.data;
  111. for(var i = 0; i < path.length - 1; i++) {
  112. if(!isDir(current[path[i]]))
  113. throw new MemoryFileSystemError(errors.code.ENOENT, _path);
  114. current = current[path[i]];
  115. }
  116. if(!isDir(current[path[i]])) {
  117. if(isFile(current[path[i]]))
  118. throw new MemoryFileSystemError(errors.code.ENOTDIR, _path);
  119. else
  120. throw new MemoryFileSystemError(errors.code.ENOENT, _path);
  121. }
  122. return Object.keys(current[path[i]]).filter(Boolean);
  123. };
  124. MemoryFileSystem.prototype.mkdirpSync = function(_path) {
  125. var path = pathToArray(_path);
  126. if(path.length === 0) return;
  127. var current = this.data;
  128. for(var i = 0; i < path.length; i++) {
  129. if(isFile(current[path[i]]))
  130. throw new MemoryFileSystemError(errors.code.ENOTDIR, _path);
  131. else if(!isDir(current[path[i]]))
  132. current[path[i]] = {"":true};
  133. current = current[path[i]];
  134. }
  135. return;
  136. };
  137. MemoryFileSystem.prototype.mkdirSync = function(_path) {
  138. var path = pathToArray(_path);
  139. if(path.length === 0) return;
  140. var current = this.data;
  141. for(var i = 0; i < path.length - 1; i++) {
  142. if(!isDir(current[path[i]]))
  143. throw new MemoryFileSystemError(errors.code.ENOENT, _path);
  144. current = current[path[i]];
  145. }
  146. if(isDir(current[path[i]]))
  147. throw new MemoryFileSystemError(errors.code.EEXIST, _path);
  148. else if(isFile(current[path[i]]))
  149. throw new MemoryFileSystemError(errors.code.ENOTDIR, _path);
  150. current[path[i]] = {"":true};
  151. return;
  152. };
  153. MemoryFileSystem.prototype._remove = function(_path, name, testFn) {
  154. var path = pathToArray(_path);
  155. if(path.length === 0) {
  156. throw new MemoryFileSystemError(errors.code.EPERM, _path);
  157. }
  158. var current = this.data;
  159. for(var i = 0; i < path.length - 1; i++) {
  160. if(!isDir(current[path[i]]))
  161. throw new MemoryFileSystemError(errors.code.ENOENT, _path);
  162. current = current[path[i]];
  163. }
  164. if(!testFn(current[path[i]]))
  165. throw new MemoryFileSystemError(errors.code.ENOENT, _path);
  166. delete current[path[i]];
  167. return;
  168. };
  169. MemoryFileSystem.prototype.rmdirSync = function(_path) {
  170. return this._remove(_path, "Directory", isDir);
  171. };
  172. MemoryFileSystem.prototype.unlinkSync = function(_path) {
  173. return this._remove(_path, "File", isFile);
  174. };
  175. MemoryFileSystem.prototype.readlinkSync = function(_path) {
  176. throw new MemoryFileSystemError(errors.code.ENOSYS, _path);
  177. };
  178. MemoryFileSystem.prototype.writeFileSync = function(_path, content, encoding) {
  179. if(!content && !encoding) throw new Error("No content");
  180. var path = pathToArray(_path);
  181. if(path.length === 0) {
  182. throw new MemoryFileSystemError(errors.code.EISDIR, _path);
  183. }
  184. var current = this.data;
  185. for(var i = 0; i < path.length - 1; i++) {
  186. if(!isDir(current[path[i]]))
  187. throw new MemoryFileSystemError(errors.code.ENOENT, _path);
  188. current = current[path[i]];
  189. }
  190. if(isDir(current[path[i]]))
  191. throw new MemoryFileSystemError(errors.code.EISDIR, _path);
  192. current[path[i]] = encoding || typeof content === "string" ? new Buffer(content, encoding) : content;
  193. return;
  194. };
  195. MemoryFileSystem.prototype.join = require("./join");
  196. MemoryFileSystem.prototype.normalize = normalize;
  197. // stream functions
  198. MemoryFileSystem.prototype.createReadStream = function(path, options) {
  199. var stream = new ReadableStream();
  200. var done = false;
  201. var data;
  202. try {
  203. data = this.readFileSync(path);
  204. } catch (e) {
  205. stream._read = function() {
  206. if (done) {
  207. return;
  208. }
  209. done = true;
  210. this.emit('error', e);
  211. this.push(null);
  212. };
  213. return stream;
  214. }
  215. options = options || { };
  216. options.start = options.start || 0;
  217. options.end = options.end || data.length;
  218. stream._read = function() {
  219. if (done) {
  220. return;
  221. }
  222. done = true;
  223. this.push(data.slice(options.start, options.end));
  224. this.push(null);
  225. };
  226. return stream;
  227. };
  228. MemoryFileSystem.prototype.createWriteStream = function(path, options) {
  229. var stream = new WritableStream(), self = this;
  230. try {
  231. // Zero the file and make sure it is writable
  232. this.writeFileSync(path, new Buffer(0));
  233. } catch(e) {
  234. // This or setImmediate?
  235. stream.once('prefinish', function() {
  236. stream.emit('error', e);
  237. });
  238. return stream;
  239. }
  240. var bl = [ ], len = 0;
  241. stream._write = function(chunk, encoding, callback) {
  242. bl.push(chunk);
  243. len += chunk.length;
  244. self.writeFile(path, Buffer.concat(bl, len), callback);
  245. }
  246. return stream;
  247. };
  248. // async functions
  249. ["stat", "readdir", "mkdirp", "mkdir", "rmdir", "unlink", "readlink"].forEach(function(fn) {
  250. MemoryFileSystem.prototype[fn] = function(path, callback) {
  251. try {
  252. var result = this[fn + "Sync"](path);
  253. } catch(e) {
  254. return callback(e);
  255. }
  256. return callback(null, result);
  257. };
  258. });
  259. MemoryFileSystem.prototype.exists = function(path, callback) {
  260. return callback(this.existsSync(path));
  261. }
  262. MemoryFileSystem.prototype.readFile = function(path, optArg, callback) {
  263. if(!callback) {
  264. callback = optArg;
  265. optArg = undefined;
  266. }
  267. try {
  268. var result = this.readFileSync(path, optArg);
  269. } catch(e) {
  270. return callback(e);
  271. }
  272. return callback(null, result);
  273. };
  274. MemoryFileSystem.prototype.writeFile = function (path, content, encoding, callback) {
  275. if(!callback) {
  276. callback = encoding;
  277. encoding = undefined;
  278. }
  279. try {
  280. this.writeFileSync(path, content, encoding);
  281. } catch(e) {
  282. return callback(e);
  283. }
  284. return callback();
  285. };