memfs 3.1.3 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # [3.2.0](https://github.com/streamich/memfs/compare/v3.1.3...v3.2.0) (2020-05-19)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * 'fromJSON()' did not consider cwd when creating directories ([3d6ee3b](https://github.com/streamich/memfs/commit/3d6ee3b2c0eef0345ba2bd400e9836f2d685321f))
7
+
8
+
9
+ ### Features
10
+
11
+ * support nested objects in 'fromJSON()' ([f8c329c](https://github.com/streamich/memfs/commit/f8c329c8e57c85cc4a394a74802af1f37dcedefd))
12
+
1
13
  ## [3.1.3](https://github.com/streamich/memfs/compare/v3.1.2...v3.1.3) (2020-05-14)
2
14
 
3
15
 
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # memfs
2
2
 
3
- [![][npm-badge]][npm-url] [![][travis-badge]][travis-url]
3
+ [![][chat-badge]][chat] [![][npm-badge]][npm-url] [![][travis-badge]][travis-url]
4
4
 
5
5
  In-memory file-system with [Node's `fs` API](https://nodejs.org/api/fs.html).
6
6
 
@@ -107,6 +107,8 @@ require('/index'); // hi world
107
107
  - [`fs-monkey`][fs-monkey] - monkey-patches Node's `fs` module and `require` function
108
108
  - [`libfs`](https://github.com/streamich/full-js/blob/master/src/lib/fs.ts) - real filesystem (that executes UNIX system calls) implemented in JavaScript
109
109
 
110
+ [chat]: https://onp4.com/@vadim/~memfs
111
+ [chat-badge]: https://img.shields.io/badge/Chat-%F0%9F%92%AC-green?style=flat&logo=chat&link=https://onp4.com/@vadim/~memfs
110
112
  [npm-url]: https://www.npmjs.com/package/memfs
111
113
  [npm-badge]: https://img.shields.io/npm/v/memfs.svg
112
114
  [travis-url]: https://travis-ci.org/streamich/memfs
package/lib/volume.d.ts CHANGED
@@ -101,12 +101,19 @@ export declare function dataToStr(data: TData, encoding?: string): string;
101
101
  export declare function dataToBuffer(data: TData, encoding?: string): Buffer;
102
102
  export declare function bufferToEncoding(buffer: Buffer, encoding?: TEncodingExtended): TDataOut;
103
103
  export declare function toUnixTimestamp(time: any): any;
104
- export declare type DirectoryJSON = Record<string, string | null>;
104
+ declare type DirectoryContent = string | null;
105
+ export interface DirectoryJSON {
106
+ [key: string]: DirectoryContent;
107
+ }
108
+ export interface NestedDirectoryJSON {
109
+ [key: string]: DirectoryContent | NestedDirectoryJSON;
110
+ }
105
111
  /**
106
112
  * `Volume` represents a file system.
107
113
  */
108
114
  export declare class Volume {
109
115
  static fromJSON(json: DirectoryJSON, cwd?: string): Volume;
116
+ static fromNestedJSON(json: NestedDirectoryJSON, cwd?: string): Volume;
110
117
  /**
111
118
  * Global file descriptor counter. UNIX file descriptors start from 0 and go sequentially
112
119
  * up, so here, in order not to conflict with them, we choose some big number and descrease
@@ -163,6 +170,7 @@ export declare class Volume {
163
170
  private _toJSON;
164
171
  toJSON(paths?: PathLike | PathLike[], json?: {}, isRelative?: boolean): DirectoryJSON;
165
172
  fromJSON(json: DirectoryJSON, cwd?: string): void;
173
+ fromNestedJSON(json: NestedDirectoryJSON, cwd?: string): void;
166
174
  reset(): void;
167
175
  mountSync(mountpoint: string, json: DirectoryJSON): void;
168
176
  private openLink;
@@ -378,3 +386,4 @@ export declare class FSWatcher extends EventEmitter {
378
386
  start(path: PathLike, persistent?: boolean, recursive?: boolean, encoding?: BufferEncoding): void;
379
387
  close(): void;
380
388
  }
389
+ export {};
package/lib/volume.js CHANGED
@@ -38,17 +38,7 @@ var util = require("util");
38
38
  var promises_1 = require("./promises");
39
39
  var resolveCrossPlatform = pathModule.resolve;
40
40
  var O_RDONLY = constants_1.constants.O_RDONLY, O_WRONLY = constants_1.constants.O_WRONLY, O_RDWR = constants_1.constants.O_RDWR, O_CREAT = constants_1.constants.O_CREAT, O_EXCL = constants_1.constants.O_EXCL, O_TRUNC = constants_1.constants.O_TRUNC, O_APPEND = constants_1.constants.O_APPEND, O_SYNC = constants_1.constants.O_SYNC, O_DIRECTORY = constants_1.constants.O_DIRECTORY, F_OK = constants_1.constants.F_OK, COPYFILE_EXCL = constants_1.constants.COPYFILE_EXCL, COPYFILE_FICLONE_FORCE = constants_1.constants.COPYFILE_FICLONE_FORCE;
41
- var sep;
42
- var relative;
43
- if (pathModule.posix) {
44
- var posix = pathModule.posix;
45
- sep = posix.sep;
46
- relative = posix.relative;
47
- }
48
- else {
49
- sep = pathModule.sep;
50
- relative = pathModule.relative;
51
- }
41
+ var _a = pathModule.posix ? pathModule.posix : pathModule, sep = _a.sep, relative = _a.relative, join = _a.join, dirname = _a.dirname;
52
42
  var isWin = process_1.default.platform === 'win32';
53
43
  var kMinPoolSpace = 128;
54
44
  // const kMaxLength = require('buffer').kMaxLength;
@@ -410,6 +400,29 @@ function validateGid(gid) {
410
400
  if (typeof gid !== 'number')
411
401
  throw TypeError(ERRSTR.GID);
412
402
  }
403
+ function flattenJSON(nestedJSON) {
404
+ var flatJSON = {};
405
+ function flatten(pathPrefix, node) {
406
+ for (var path in node) {
407
+ var contentOrNode = node[path];
408
+ var joinedPath = join(pathPrefix, path);
409
+ if (typeof contentOrNode === 'string') {
410
+ flatJSON[joinedPath] = contentOrNode;
411
+ }
412
+ else if (typeof contentOrNode === 'object' && contentOrNode !== null && Object.keys(contentOrNode).length > 0) {
413
+ // empty directories need an explicit entry and therefore get handled in `else`, non-empty ones are implicitly considered
414
+ flatten(joinedPath, contentOrNode);
415
+ }
416
+ else {
417
+ // without this branch null, empty-object or non-object entries would not be handled in the same way
418
+ // by both fromJSON() and fromNestedJSON()
419
+ flatJSON[joinedPath] = null;
420
+ }
421
+ }
422
+ }
423
+ flatten('', nestedJSON);
424
+ return flatJSON;
425
+ }
413
426
  /**
414
427
  * `Volume` represents a file system.
415
428
  */
@@ -486,6 +499,11 @@ var Volume = /** @class */ (function () {
486
499
  vol.fromJSON(json, cwd);
487
500
  return vol;
488
501
  };
502
+ Volume.fromNestedJSON = function (json, cwd) {
503
+ var vol = new Volume();
504
+ vol.fromNestedJSON(json, cwd);
505
+ return vol;
506
+ };
489
507
  Object.defineProperty(Volume.prototype, "promises", {
490
508
  get: function () {
491
509
  if (this.promisesApi === null)
@@ -729,18 +747,14 @@ var Volume = /** @class */ (function () {
729
747
  }
730
748
  return json;
731
749
  };
732
- // fromJSON(json: {[filename: string]: string}, cwd: string = '/') {
733
750
  Volume.prototype.fromJSON = function (json, cwd) {
734
751
  if (cwd === void 0) { cwd = process_1.default.cwd(); }
735
752
  for (var filename in json) {
736
753
  var data = json[filename];
754
+ filename = resolve(filename, cwd);
737
755
  if (typeof data === 'string') {
738
- filename = resolve(filename, cwd);
739
- var steps = filenameToSteps(filename);
740
- if (steps.length > 1) {
741
- var dirname = sep + steps.slice(0, steps.length - 1).join(sep);
742
- this.mkdirpBase(dirname, 511 /* DIR */);
743
- }
756
+ var dir = dirname(filename);
757
+ this.mkdirpBase(dir, 511 /* DIR */);
744
758
  this.writeFileSync(filename, data);
745
759
  }
746
760
  else {
@@ -748,6 +762,9 @@ var Volume = /** @class */ (function () {
748
762
  }
749
763
  }
750
764
  };
765
+ Volume.prototype.fromNestedJSON = function (json, cwd) {
766
+ this.fromJSON(flattenJSON(json), cwd);
767
+ };
751
768
  Volume.prototype.reset = function () {
752
769
  this.ino = 0;
753
770
  this.inodes = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "memfs",
3
- "version": "3.1.3",
3
+ "version": "3.2.0",
4
4
  "description": "In-memory file-system with Node's fs API.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",