memfs 3.5.3 → 4.0.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/lib/Dirent.js CHANGED
@@ -1,49 +1,48 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Dirent = void 0;
4
- var constants_1 = require("./constants");
5
- var encoding_1 = require("./encoding");
6
- var S_IFMT = constants_1.constants.S_IFMT, S_IFDIR = constants_1.constants.S_IFDIR, S_IFREG = constants_1.constants.S_IFREG, S_IFBLK = constants_1.constants.S_IFBLK, S_IFCHR = constants_1.constants.S_IFCHR, S_IFLNK = constants_1.constants.S_IFLNK, S_IFIFO = constants_1.constants.S_IFIFO, S_IFSOCK = constants_1.constants.S_IFSOCK;
4
+ const constants_1 = require("./constants");
5
+ const encoding_1 = require("./encoding");
6
+ const { S_IFMT, S_IFDIR, S_IFREG, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO, S_IFSOCK } = constants_1.constants;
7
7
  /**
8
8
  * A directory entry, like `fs.Dirent`.
9
9
  */
10
- var Dirent = /** @class */ (function () {
11
- function Dirent() {
10
+ class Dirent {
11
+ constructor() {
12
12
  this.name = '';
13
13
  this.mode = 0;
14
14
  }
15
- Dirent.build = function (link, encoding) {
16
- var dirent = new Dirent();
17
- var mode = link.getNode().mode;
15
+ static build(link, encoding) {
16
+ const dirent = new Dirent();
17
+ const { mode } = link.getNode();
18
18
  dirent.name = (0, encoding_1.strToEncoding)(link.getName(), encoding);
19
19
  dirent.mode = mode;
20
20
  return dirent;
21
- };
22
- Dirent.prototype._checkModeProperty = function (property) {
21
+ }
22
+ _checkModeProperty(property) {
23
23
  return (this.mode & S_IFMT) === property;
24
- };
25
- Dirent.prototype.isDirectory = function () {
24
+ }
25
+ isDirectory() {
26
26
  return this._checkModeProperty(S_IFDIR);
27
- };
28
- Dirent.prototype.isFile = function () {
27
+ }
28
+ isFile() {
29
29
  return this._checkModeProperty(S_IFREG);
30
- };
31
- Dirent.prototype.isBlockDevice = function () {
30
+ }
31
+ isBlockDevice() {
32
32
  return this._checkModeProperty(S_IFBLK);
33
- };
34
- Dirent.prototype.isCharacterDevice = function () {
33
+ }
34
+ isCharacterDevice() {
35
35
  return this._checkModeProperty(S_IFCHR);
36
- };
37
- Dirent.prototype.isSymbolicLink = function () {
36
+ }
37
+ isSymbolicLink() {
38
38
  return this._checkModeProperty(S_IFLNK);
39
- };
40
- Dirent.prototype.isFIFO = function () {
39
+ }
40
+ isFIFO() {
41
41
  return this._checkModeProperty(S_IFIFO);
42
- };
43
- Dirent.prototype.isSocket = function () {
42
+ }
43
+ isSocket() {
44
44
  return this._checkModeProperty(S_IFSOCK);
45
- };
46
- return Dirent;
47
- }());
45
+ }
46
+ }
48
47
  exports.Dirent = Dirent;
49
48
  exports.default = Dirent;
package/lib/Stats.js CHANGED
@@ -1,20 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Stats = void 0;
4
- var constants_1 = require("./constants");
5
- var getBigInt_1 = require("./getBigInt");
6
- var S_IFMT = constants_1.constants.S_IFMT, S_IFDIR = constants_1.constants.S_IFDIR, S_IFREG = constants_1.constants.S_IFREG, S_IFBLK = constants_1.constants.S_IFBLK, S_IFCHR = constants_1.constants.S_IFCHR, S_IFLNK = constants_1.constants.S_IFLNK, S_IFIFO = constants_1.constants.S_IFIFO, S_IFSOCK = constants_1.constants.S_IFSOCK;
4
+ const constants_1 = require("./constants");
5
+ const getBigInt_1 = require("./getBigInt");
6
+ const { S_IFMT, S_IFDIR, S_IFREG, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO, S_IFSOCK } = constants_1.constants;
7
7
  /**
8
8
  * Statistics about a file/directory, like `fs.Stats`.
9
9
  */
10
- var Stats = /** @class */ (function () {
11
- function Stats() {
12
- }
13
- Stats.build = function (node, bigint) {
14
- if (bigint === void 0) { bigint = false; }
15
- var stats = new Stats();
16
- var uid = node.uid, gid = node.gid, atime = node.atime, mtime = node.mtime, ctime = node.ctime;
17
- var getStatNumber = !bigint ? function (number) { return number; } : getBigInt_1.default;
10
+ class Stats {
11
+ static build(node, bigint = false) {
12
+ const stats = new Stats();
13
+ const { uid, gid, atime, mtime, ctime } = node;
14
+ const getStatNumber = !bigint ? number => number : getBigInt_1.default;
18
15
  // Copy all values on Stats from Node, so that if Node values
19
16
  // change, values on Stats would still be the old ones,
20
17
  // just like in Node fs.
@@ -31,39 +28,38 @@ var Stats = /** @class */ (function () {
31
28
  stats.birthtime = ctime;
32
29
  stats.atimeMs = getStatNumber(atime.getTime());
33
30
  stats.mtimeMs = getStatNumber(mtime.getTime());
34
- var ctimeMs = getStatNumber(ctime.getTime());
31
+ const ctimeMs = getStatNumber(ctime.getTime());
35
32
  stats.ctimeMs = ctimeMs;
36
33
  stats.birthtimeMs = ctimeMs;
37
34
  stats.dev = getStatNumber(0);
38
35
  stats.mode = getStatNumber(node.mode);
39
36
  stats.nlink = getStatNumber(node.nlink);
40
37
  return stats;
41
- };
42
- Stats.prototype._checkModeProperty = function (property) {
38
+ }
39
+ _checkModeProperty(property) {
43
40
  return (Number(this.mode) & S_IFMT) === property;
44
- };
45
- Stats.prototype.isDirectory = function () {
41
+ }
42
+ isDirectory() {
46
43
  return this._checkModeProperty(S_IFDIR);
47
- };
48
- Stats.prototype.isFile = function () {
44
+ }
45
+ isFile() {
49
46
  return this._checkModeProperty(S_IFREG);
50
- };
51
- Stats.prototype.isBlockDevice = function () {
47
+ }
48
+ isBlockDevice() {
52
49
  return this._checkModeProperty(S_IFBLK);
53
- };
54
- Stats.prototype.isCharacterDevice = function () {
50
+ }
51
+ isCharacterDevice() {
55
52
  return this._checkModeProperty(S_IFCHR);
56
- };
57
- Stats.prototype.isSymbolicLink = function () {
53
+ }
54
+ isSymbolicLink() {
58
55
  return this._checkModeProperty(S_IFLNK);
59
- };
60
- Stats.prototype.isFIFO = function () {
56
+ }
57
+ isFIFO() {
61
58
  return this._checkModeProperty(S_IFIFO);
62
- };
63
- Stats.prototype.isSocket = function () {
59
+ }
60
+ isSocket() {
64
61
  return this._checkModeProperty(S_IFSOCK);
65
- };
66
- return Stats;
67
- }());
62
+ }
63
+ }
68
64
  exports.Stats = Stats;
69
65
  exports.default = Stats;
package/lib/encoding.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.strToEncoding = exports.assertEncoding = exports.ENCODING_UTF8 = void 0;
4
- var buffer_1 = require("./internal/buffer");
5
- var errors = require("./internal/errors");
4
+ const buffer_1 = require("./internal/buffer");
5
+ const errors = require("./internal/errors");
6
6
  exports.ENCODING_UTF8 = 'utf8';
7
7
  function assertEncoding(encoding) {
8
8
  if (encoding && !buffer_1.Buffer.isEncoding(encoding))
@@ -0,0 +1,82 @@
1
+ export interface IPermissionStatus {
2
+ name: string;
3
+ state: 'granted' | 'denied' | 'prompt';
4
+ }
5
+ export interface IFileSystemHandle {
6
+ kind: 'file' | 'directory';
7
+ name: string;
8
+ isSameEntry(fileSystemHandle: IFileSystemHandle): boolean;
9
+ queryPermission(fileSystemHandlePermissionDescriptor: NodeFileSystemHandlePermissionDescriptor): IPermissionStatus;
10
+ remove(options?: {
11
+ recursive?: boolean;
12
+ }): Promise<void>;
13
+ requestPermission(fileSystemHandlePermissionDescriptor: NodeFileSystemHandlePermissionDescriptor): IPermissionStatus;
14
+ }
15
+ export interface NodeFileSystemHandlePermissionDescriptor {
16
+ mode: 'read' | 'readwrite';
17
+ }
18
+ export interface IFileSystemDirectoryHandle extends IFileSystemHandle {
19
+ keys(): AsyncIterableIterator<string>;
20
+ entries(): AsyncIterableIterator<[string, IFileSystemHandle]>;
21
+ values(): AsyncIterableIterator<IFileSystemHandle>;
22
+ getDirectoryHandle(name: string, options?: GetDirectoryHandleOptions): Promise<IFileSystemDirectoryHandle>;
23
+ getFileHandle(name: string, options?: GetFileHandleOptions): Promise<IFileSystemFileHandle>;
24
+ removeEntry(name: string, options?: RemoveEntryOptions): Promise<void>;
25
+ resolve(possibleDescendant: IFileSystemHandle): Promise<string[] | null>;
26
+ }
27
+ export interface GetDirectoryHandleOptions {
28
+ /**
29
+ * A boolean value, which defaults to `false`. When set to `true` if the directory
30
+ * is not found, one with the specified name will be created and returned.
31
+ */
32
+ create?: boolean;
33
+ }
34
+ export interface GetFileHandleOptions {
35
+ /**
36
+ * A Boolean. Default `false`. When set to `true` if the file is not found,
37
+ * one with the specified name will be created and returned.
38
+ */
39
+ create?: boolean;
40
+ }
41
+ export interface RemoveEntryOptions {
42
+ /**
43
+ * A boolean value, which defaults to `false`. When set to true entries will
44
+ * be removed recursively.
45
+ */
46
+ recursive?: boolean;
47
+ }
48
+ export interface IFileSystemFileHandle extends IFileSystemHandle {
49
+ getFile(): Promise<File>;
50
+ createSyncAccessHandle: undefined | (() => Promise<IFileSystemSyncAccessHandle>);
51
+ createWritable(options?: CreateWritableOptions): Promise<IFileSystemWritableFileStream>;
52
+ }
53
+ export interface CreateWritableOptions {
54
+ keepExistingData?: boolean;
55
+ }
56
+ export interface IFileSystemSyncAccessHandle {
57
+ close(): Promise<void>;
58
+ flush(): Promise<void>;
59
+ getSize(): Promise<number>;
60
+ read(buffer: ArrayBuffer | ArrayBufferView, options?: FileSystemReadWriteOptions): Promise<number>;
61
+ truncate(newSize: number): Promise<void>;
62
+ write(buffer: ArrayBuffer | ArrayBufferView | DataView, options?: FileSystemReadWriteOptions): Promise<number>;
63
+ }
64
+ export interface FileSystemReadWriteOptions {
65
+ /**
66
+ * A number representing the offset in bytes that the file should be read from.
67
+ */
68
+ at?: number;
69
+ }
70
+ export interface IFileSystemWritableFileStream extends WritableStream {
71
+ seek(position: number): Promise<void>;
72
+ truncate(size: number): Promise<void>;
73
+ write(chunk: Data): Promise<void>;
74
+ write(params: FileSystemWritableFileStreamParams): Promise<void>;
75
+ }
76
+ export interface FileSystemWritableFileStreamParams {
77
+ type: 'write' | 'truncate' | 'seek';
78
+ data?: Data;
79
+ position?: number;
80
+ size?: number;
81
+ }
82
+ export type Data = ArrayBuffer | ArrayBufferView | Uint8Array | Uint8ClampedArray | Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array | Float32Array | Float64Array | BigUint64Array | BigInt64Array | DataView | Blob | string;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/lib/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import Stats from './Stats';
2
2
  import Dirent from './Dirent';
3
3
  import { Volume as _Volume, StatWatcher, FSWatcher, IReadStream, IWriteStream, DirectoryJSON } from './volume';
4
- import { IPromisesAPI } from './promises';
5
4
  import { constants } from './constants';
5
+ import type { FsPromisesApi } from './node/types';
6
6
  export { DirectoryJSON };
7
7
  export declare const Volume: typeof _Volume;
8
8
  export declare const vol: _Volume;
@@ -14,8 +14,22 @@ export interface IFs extends _Volume {
14
14
  FSWatcher: new () => FSWatcher;
15
15
  ReadStream: new (...args: any[]) => IReadStream;
16
16
  WriteStream: new (...args: any[]) => IWriteStream;
17
- promises: IPromisesAPI;
17
+ promises: FsPromisesApi;
18
18
  _toUnixTimestamp: any;
19
19
  }
20
20
  export declare function createFsFromVolume(vol: _Volume): IFs;
21
21
  export declare const fs: IFs;
22
+ /**
23
+ * Creates a new file system instance.
24
+ *
25
+ * @param json File system structure expressed as a JSON object.
26
+ * Use `null` for empty directories and empty string for empty files.
27
+ * @param cwd Current working directory. The JSON structure will be created
28
+ * relative to this path.
29
+ * @returns A `memfs` file system instance, which is a drop-in replacement for
30
+ * the `fs` module.
31
+ */
32
+ export declare const memfs: (json?: DirectoryJSON, cwd?: string) => IFs;
33
+ export type IFsWithVolume = IFs & {
34
+ __vol: _Volume;
35
+ };
package/lib/index.js CHANGED
@@ -1,48 +1,50 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.fs = exports.createFsFromVolume = exports.vol = exports.Volume = void 0;
15
- var Stats_1 = require("./Stats");
16
- var Dirent_1 = require("./Dirent");
17
- var volume_1 = require("./volume");
18
- var _a = require('fs-monkey/lib/util/lists'), fsSyncMethods = _a.fsSyncMethods, fsAsyncMethods = _a.fsAsyncMethods;
19
- var constants_1 = require("./constants");
20
- var F_OK = constants_1.constants.F_OK, R_OK = constants_1.constants.R_OK, W_OK = constants_1.constants.W_OK, X_OK = constants_1.constants.X_OK;
3
+ exports.memfs = exports.fs = exports.createFsFromVolume = exports.vol = exports.Volume = void 0;
4
+ const Stats_1 = require("./Stats");
5
+ const Dirent_1 = require("./Dirent");
6
+ const volume_1 = require("./volume");
7
+ const { fsSyncMethods, fsAsyncMethods } = require('fs-monkey/lib/util/lists');
8
+ const constants_1 = require("./constants");
9
+ const { F_OK, R_OK, W_OK, X_OK } = constants_1.constants;
21
10
  exports.Volume = volume_1.Volume;
22
11
  // Default volume.
23
12
  exports.vol = new volume_1.Volume();
24
13
  function createFsFromVolume(vol) {
25
- var fs = { F_OK: F_OK, R_OK: R_OK, W_OK: W_OK, X_OK: X_OK, constants: constants_1.constants, Stats: Stats_1.default, Dirent: Dirent_1.default };
14
+ const fs = { F_OK, R_OK, W_OK, X_OK, constants: constants_1.constants, Stats: Stats_1.default, Dirent: Dirent_1.default };
26
15
  // Bind FS methods.
27
- for (var _i = 0, fsSyncMethods_1 = fsSyncMethods; _i < fsSyncMethods_1.length; _i++) {
28
- var method = fsSyncMethods_1[_i];
16
+ for (const method of fsSyncMethods)
29
17
  if (typeof vol[method] === 'function')
30
18
  fs[method] = vol[method].bind(vol);
31
- }
32
- for (var _a = 0, fsAsyncMethods_1 = fsAsyncMethods; _a < fsAsyncMethods_1.length; _a++) {
33
- var method = fsAsyncMethods_1[_a];
19
+ for (const method of fsAsyncMethods)
34
20
  if (typeof vol[method] === 'function')
35
21
  fs[method] = vol[method].bind(vol);
36
- }
37
22
  fs.StatWatcher = vol.StatWatcher;
38
23
  fs.FSWatcher = vol.FSWatcher;
39
24
  fs.WriteStream = vol.WriteStream;
40
25
  fs.ReadStream = vol.ReadStream;
41
26
  fs.promises = vol.promises;
42
27
  fs._toUnixTimestamp = volume_1.toUnixTimestamp;
28
+ fs.__vol = vol;
43
29
  return fs;
44
30
  }
45
31
  exports.createFsFromVolume = createFsFromVolume;
46
32
  exports.fs = createFsFromVolume(exports.vol);
47
- module.exports = __assign(__assign({}, module.exports), exports.fs);
33
+ /**
34
+ * Creates a new file system instance.
35
+ *
36
+ * @param json File system structure expressed as a JSON object.
37
+ * Use `null` for empty directories and empty string for empty files.
38
+ * @param cwd Current working directory. The JSON structure will be created
39
+ * relative to this path.
40
+ * @returns A `memfs` file system instance, which is a drop-in replacement for
41
+ * the `fs` module.
42
+ */
43
+ const memfs = (json = {}, cwd = '/') => {
44
+ const volume = exports.Volume.fromJSON(json, cwd);
45
+ const fs = createFsFromVolume(volume);
46
+ return fs;
47
+ };
48
+ exports.memfs = memfs;
49
+ module.exports = Object.assign(Object.assign({}, module.exports), exports.fs);
48
50
  module.exports.semantic = true;
@@ -1,25 +1,12 @@
1
1
  "use strict";
2
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
- if (ar || !(i in from)) {
5
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
- ar[i] = from[i];
7
- }
8
- }
9
- return to.concat(ar || Array.prototype.slice.call(from));
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.bufferFrom = exports.bufferAllocUnsafe = exports.Buffer = void 0;
13
- var buffer_1 = require("buffer");
4
+ const buffer_1 = require("buffer");
14
5
  Object.defineProperty(exports, "Buffer", { enumerable: true, get: function () { return buffer_1.Buffer; } });
15
- function bufferV0P12Ponyfill(arg0) {
16
- var args = [];
17
- for (var _i = 1; _i < arguments.length; _i++) {
18
- args[_i - 1] = arguments[_i];
19
- }
20
- return new (buffer_1.Buffer.bind.apply(buffer_1.Buffer, __spreadArray([void 0, arg0], args, false)))();
6
+ function bufferV0P12Ponyfill(arg0, ...args) {
7
+ return new buffer_1.Buffer(arg0, ...args);
21
8
  }
22
- var bufferAllocUnsafe = buffer_1.Buffer.allocUnsafe || bufferV0P12Ponyfill;
9
+ const bufferAllocUnsafe = buffer_1.Buffer.allocUnsafe || bufferV0P12Ponyfill;
23
10
  exports.bufferAllocUnsafe = bufferAllocUnsafe;
24
- var bufferFrom = buffer_1.Buffer.from || bufferV0P12Ponyfill;
11
+ const bufferFrom = buffer_1.Buffer.from || bufferV0P12Ponyfill;
25
12
  exports.bufferFrom = bufferFrom;