memfs 4.0.0 → 4.1.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.
Files changed (105) hide show
  1. package/LICENSE +201 -24
  2. package/README.md +21 -92
  3. package/lib/Dirent.d.ts +2 -1
  4. package/lib/__tests__/util.d.ts +13 -0
  5. package/lib/__tests__/util.js +29 -0
  6. package/lib/consts/AMODE.d.ts +9 -0
  7. package/lib/consts/FLAG.d.ts +22 -0
  8. package/lib/consts/FLAG.js +2 -0
  9. package/lib/fsa/types.d.ts +3 -3
  10. package/lib/fsa-to-node/FsaNodeCore.d.ts +32 -0
  11. package/lib/fsa-to-node/FsaNodeCore.js +168 -0
  12. package/lib/fsa-to-node/FsaNodeDirent.d.ts +13 -0
  13. package/lib/fsa-to-node/FsaNodeDirent.js +31 -0
  14. package/lib/fsa-to-node/FsaNodeFs.d.ts +171 -0
  15. package/lib/fsa-to-node/FsaNodeFs.js +842 -0
  16. package/lib/fsa-to-node/FsaNodeFsOpenFile.d.ts +24 -0
  17. package/lib/fsa-to-node/FsaNodeFsOpenFile.js +46 -0
  18. package/lib/fsa-to-node/FsaNodeReadStream.d.ts +24 -0
  19. package/lib/fsa-to-node/FsaNodeReadStream.js +100 -0
  20. package/lib/fsa-to-node/FsaNodeStats.d.ts +30 -0
  21. package/lib/fsa-to-node/FsaNodeStats.js +52 -0
  22. package/lib/fsa-to-node/FsaNodeWriteStream.d.ts +48 -0
  23. package/lib/fsa-to-node/FsaNodeWriteStream.js +162 -0
  24. package/lib/fsa-to-node/__tests__/FsaNodeFs.test.d.ts +1 -0
  25. package/lib/fsa-to-node/__tests__/FsaNodeFs.test.js +873 -0
  26. package/lib/fsa-to-node/__tests__/util.test.d.ts +1 -0
  27. package/lib/fsa-to-node/__tests__/util.test.js +28 -0
  28. package/lib/fsa-to-node/constants.d.ts +3 -0
  29. package/lib/fsa-to-node/constants.js +2 -0
  30. package/lib/fsa-to-node/index.d.ts +2 -0
  31. package/lib/fsa-to-node/index.js +7 -0
  32. package/lib/fsa-to-node/json.d.ts +4 -0
  33. package/lib/fsa-to-node/json.js +7 -0
  34. package/lib/fsa-to-node/types.d.ts +37 -0
  35. package/lib/fsa-to-node/types.js +2 -0
  36. package/lib/fsa-to-node/util.d.ts +4 -0
  37. package/lib/fsa-to-node/util.js +40 -0
  38. package/lib/fsa-to-node/worker/FsaNodeSyncAdapterWorker.d.ts +10 -0
  39. package/lib/fsa-to-node/worker/FsaNodeSyncAdapterWorker.js +72 -0
  40. package/lib/fsa-to-node/worker/FsaNodeSyncWorker.d.ts +20 -0
  41. package/lib/fsa-to-node/worker/FsaNodeSyncWorker.js +190 -0
  42. package/lib/fsa-to-node/worker/SyncMessenger.d.ts +24 -0
  43. package/lib/fsa-to-node/worker/SyncMessenger.js +80 -0
  44. package/lib/fsa-to-node/worker/constants.d.ts +8 -0
  45. package/lib/fsa-to-node/worker/constants.js +2 -0
  46. package/lib/fsa-to-node/worker/types.d.ts +17 -0
  47. package/lib/fsa-to-node/worker/types.js +2 -0
  48. package/lib/index.d.ts +5 -4
  49. package/lib/index.js +1 -1
  50. package/lib/node/constants.d.ts +41 -0
  51. package/lib/node/constants.js +56 -0
  52. package/lib/node/options.d.ts +23 -0
  53. package/lib/node/options.js +94 -0
  54. package/lib/node/promises.d.ts +1 -1
  55. package/lib/node/promises.js +1 -3
  56. package/lib/node/types/FsCommonObjects.d.ts +17 -0
  57. package/lib/node/types/FsCommonObjects.js +2 -0
  58. package/lib/node/types/{sync.d.ts → FsSynchronousApi.d.ts} +36 -37
  59. package/lib/node/types/FsSynchronousApi.js +2 -0
  60. package/lib/node/types/callback.d.ts +57 -58
  61. package/lib/node/types/index.d.ts +1 -1
  62. package/lib/node/types/misc.d.ts +6 -8
  63. package/lib/node/types/options.d.ts +20 -6
  64. package/lib/node/types/promises.d.ts +2 -2
  65. package/lib/node/util.d.ts +19 -0
  66. package/lib/node/util.js +265 -1
  67. package/lib/node-to-fsa/NodeFileSystemDirectoryHandle.d.ts +71 -0
  68. package/lib/node-to-fsa/NodeFileSystemDirectoryHandle.js +264 -0
  69. package/lib/node-to-fsa/NodeFileSystemFileHandle.d.ts +28 -0
  70. package/lib/node-to-fsa/NodeFileSystemFileHandle.js +73 -0
  71. package/lib/node-to-fsa/NodeFileSystemHandle.d.ts +33 -0
  72. package/lib/node-to-fsa/NodeFileSystemHandle.js +53 -0
  73. package/lib/node-to-fsa/NodeFileSystemSyncAccessHandle.d.ts +42 -0
  74. package/lib/node-to-fsa/NodeFileSystemSyncAccessHandle.js +116 -0
  75. package/lib/node-to-fsa/NodeFileSystemWritableFileStream.d.ts +54 -0
  76. package/lib/node-to-fsa/NodeFileSystemWritableFileStream.js +195 -0
  77. package/lib/node-to-fsa/NodePermissionStatus.d.ts +8 -0
  78. package/lib/node-to-fsa/NodePermissionStatus.js +13 -0
  79. package/lib/node-to-fsa/__tests__/NodeFileSystemDirectoryHandle.test.d.ts +1 -0
  80. package/lib/node-to-fsa/__tests__/NodeFileSystemDirectoryHandle.test.js +627 -0
  81. package/lib/node-to-fsa/__tests__/NodeFileSystemFileHandle.test.d.ts +1 -0
  82. package/lib/node-to-fsa/__tests__/NodeFileSystemFileHandle.test.js +191 -0
  83. package/lib/node-to-fsa/__tests__/NodeFileSystemHandle.test.d.ts +1 -0
  84. package/lib/node-to-fsa/__tests__/NodeFileSystemHandle.test.js +49 -0
  85. package/lib/node-to-fsa/__tests__/NodeFileSystemSyncAccessHandle.test.d.ts +1 -0
  86. package/lib/node-to-fsa/__tests__/NodeFileSystemSyncAccessHandle.test.js +183 -0
  87. package/lib/node-to-fsa/__tests__/NodeFileSystemWritableFileStream.test.d.ts +1 -0
  88. package/lib/node-to-fsa/__tests__/NodeFileSystemWritableFileStream.test.js +106 -0
  89. package/lib/node-to-fsa/__tests__/scenarios.test.d.ts +1 -0
  90. package/lib/node-to-fsa/__tests__/scenarios.test.js +46 -0
  91. package/lib/node-to-fsa/__tests__/util.test.d.ts +1 -0
  92. package/lib/node-to-fsa/__tests__/util.test.js +25 -0
  93. package/lib/node-to-fsa/index.d.ts +7 -0
  94. package/lib/node-to-fsa/index.js +26 -0
  95. package/lib/node-to-fsa/types.d.ts +12 -0
  96. package/lib/node-to-fsa/types.js +2 -0
  97. package/lib/node-to-fsa/util.d.ts +11 -0
  98. package/lib/node-to-fsa/util.js +33 -0
  99. package/lib/node.d.ts +1 -1
  100. package/lib/volume.d.ts +45 -126
  101. package/lib/volume.js +204 -562
  102. package/lib/webfs/index.d.ts +1 -0
  103. package/lib/webfs/index.js +22 -0
  104. package/package.json +28 -5
  105. /package/lib/{node/types/sync.js → consts/AMODE.js} +0 -0
@@ -0,0 +1,264 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
12
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
13
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
14
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
15
+ return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
16
+ function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
17
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
18
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
19
+ function fulfill(value) { resume("next", value); }
20
+ function reject(value) { resume("throw", value); }
21
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
22
+ };
23
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
24
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
25
+ var m = o[Symbol.asyncIterator], i;
26
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
27
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
28
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.NodeFileSystemDirectoryHandle = void 0;
32
+ const NodeFileSystemHandle_1 = require("./NodeFileSystemHandle");
33
+ const util_1 = require("./util");
34
+ const NodeFileSystemFileHandle_1 = require("./NodeFileSystemFileHandle");
35
+ /**
36
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle
37
+ */
38
+ class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle_1.NodeFileSystemHandle {
39
+ constructor(fs, __path, ctx = {}) {
40
+ super('directory', (0, util_1.basename)(__path, ctx.separator || '/'));
41
+ this.fs = fs;
42
+ this.__path = __path;
43
+ this.ctx = (0, util_1.ctx)(ctx);
44
+ }
45
+ /**
46
+ * Returns a new array iterator containing the keys for each item in
47
+ * {@link NodeFileSystemDirectoryHandle} object.
48
+ *
49
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/keys
50
+ */
51
+ keys() {
52
+ return __asyncGenerator(this, arguments, function* keys_1() {
53
+ const list = yield __await(this.fs.promises.readdir(this.__path));
54
+ for (const name of list)
55
+ yield yield __await('' + name);
56
+ });
57
+ }
58
+ /**
59
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/entries
60
+ */
61
+ entries() {
62
+ return __asyncGenerator(this, arguments, function* entries_1() {
63
+ const { __path: path, fs, ctx } = this;
64
+ const list = yield __await(fs.promises.readdir(path, { withFileTypes: true }));
65
+ for (const d of list) {
66
+ const dirent = d;
67
+ const name = dirent.name + '';
68
+ const newPath = path + ctx.separator + name;
69
+ if (dirent.isDirectory())
70
+ yield yield __await([name, new NodeFileSystemDirectoryHandle(fs, newPath, ctx)]);
71
+ else if (dirent.isFile())
72
+ yield yield __await([name, new NodeFileSystemFileHandle_1.NodeFileSystemFileHandle(fs, name, ctx)]);
73
+ }
74
+ });
75
+ }
76
+ /**
77
+ * Returns a new array iterator containing the values for each index in the
78
+ * {@link FileSystemDirectoryHandle} object.
79
+ *
80
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/values
81
+ */
82
+ values() {
83
+ return __asyncGenerator(this, arguments, function* values_1() {
84
+ var _a, e_1, _b, _c;
85
+ try {
86
+ for (var _d = true, _e = __asyncValues(this.entries()), _f; _f = yield __await(_e.next()), _a = _f.done, !_a;) {
87
+ _c = _f.value;
88
+ _d = false;
89
+ try {
90
+ const [, value] = _c;
91
+ yield yield __await(value);
92
+ }
93
+ finally {
94
+ _d = true;
95
+ }
96
+ }
97
+ }
98
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
99
+ finally {
100
+ try {
101
+ if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e));
102
+ }
103
+ finally { if (e_1) throw e_1.error; }
104
+ }
105
+ });
106
+ }
107
+ /**
108
+ * Returns a {@link NodeFileSystemDirectoryHandle} for a subdirectory with the specified
109
+ * name within the directory handle on which the method is called.
110
+ *
111
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/getDirectoryHandle
112
+ * @param name A string representing the {@link NodeFileSystemHandle} name of
113
+ * the subdirectory you wish to retrieve.
114
+ * @param options An optional object containing options for the retrieved
115
+ * subdirectory.
116
+ */
117
+ getDirectoryHandle(name, options) {
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ (0, util_1.assertName)(name, 'getDirectoryHandle', 'FileSystemDirectoryHandle');
120
+ const filename = this.__path + this.ctx.separator + name;
121
+ try {
122
+ const stats = yield this.fs.promises.stat(filename);
123
+ if (!stats.isDirectory())
124
+ throw (0, util_1.newTypeMismatchError)();
125
+ return new NodeFileSystemDirectoryHandle(this.fs, filename, this.ctx);
126
+ }
127
+ catch (error) {
128
+ if (error instanceof DOMException)
129
+ throw error;
130
+ if (error && typeof error === 'object') {
131
+ switch (error.code) {
132
+ case 'ENOENT': {
133
+ if (options === null || options === void 0 ? void 0 : options.create) {
134
+ (0, util_1.assertCanWrite)(this.ctx.mode);
135
+ yield this.fs.promises.mkdir(filename);
136
+ return new NodeFileSystemDirectoryHandle(this.fs, filename, this.ctx);
137
+ }
138
+ throw (0, util_1.newNotFoundError)();
139
+ }
140
+ case 'EPERM':
141
+ case 'EACCES':
142
+ throw (0, util_1.newNotAllowedError)();
143
+ }
144
+ }
145
+ throw error;
146
+ }
147
+ });
148
+ }
149
+ /**
150
+ * Returns a {@link FileSystemFileHandle} for a file with the specified name,
151
+ * within the directory the method is called.
152
+ *
153
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/getFileHandle
154
+ * @param name A string representing the {@link NodeFileSystemHandle} name of
155
+ * the file you wish to retrieve.
156
+ * @param options An optional object containing options for the retrieved file.
157
+ */
158
+ getFileHandle(name, options) {
159
+ return __awaiter(this, void 0, void 0, function* () {
160
+ (0, util_1.assertName)(name, 'getFileHandle', 'FileSystemDirectoryHandle');
161
+ const filename = this.__path + this.ctx.separator + name;
162
+ try {
163
+ const stats = yield this.fs.promises.stat(filename);
164
+ if (!stats.isFile())
165
+ throw (0, util_1.newTypeMismatchError)();
166
+ return new NodeFileSystemFileHandle_1.NodeFileSystemFileHandle(this.fs, filename, this.ctx);
167
+ }
168
+ catch (error) {
169
+ if (error instanceof DOMException)
170
+ throw error;
171
+ if (error && typeof error === 'object') {
172
+ switch (error.code) {
173
+ case 'ENOENT': {
174
+ if (options === null || options === void 0 ? void 0 : options.create) {
175
+ (0, util_1.assertCanWrite)(this.ctx.mode);
176
+ yield this.fs.promises.writeFile(filename, '');
177
+ return new NodeFileSystemFileHandle_1.NodeFileSystemFileHandle(this.fs, filename, this.ctx);
178
+ }
179
+ throw (0, util_1.newNotFoundError)();
180
+ }
181
+ case 'EPERM':
182
+ case 'EACCES':
183
+ throw (0, util_1.newNotAllowedError)();
184
+ }
185
+ }
186
+ throw error;
187
+ }
188
+ });
189
+ }
190
+ /**
191
+ * Attempts to remove an entry if the directory handle contains a file or
192
+ * directory called the name specified.
193
+ *
194
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/removeEntry
195
+ * @param name A string representing the {@link FileSystemHandle} name of the
196
+ * entry you wish to remove.
197
+ * @param options An optional object containing options.
198
+ */
199
+ removeEntry(name, { recursive = false } = {}) {
200
+ return __awaiter(this, void 0, void 0, function* () {
201
+ (0, util_1.assertCanWrite)(this.ctx.mode);
202
+ (0, util_1.assertName)(name, 'removeEntry', 'FileSystemDirectoryHandle');
203
+ const filename = this.__path + this.ctx.separator + name;
204
+ const promises = this.fs.promises;
205
+ try {
206
+ const stats = yield promises.stat(filename);
207
+ if (stats.isFile()) {
208
+ yield promises.unlink(filename);
209
+ }
210
+ else if (stats.isDirectory()) {
211
+ yield promises.rmdir(filename, { recursive });
212
+ }
213
+ else
214
+ throw (0, util_1.newTypeMismatchError)();
215
+ }
216
+ catch (error) {
217
+ if (error instanceof DOMException)
218
+ throw error;
219
+ if (error && typeof error === 'object') {
220
+ switch (error.code) {
221
+ case 'ENOENT': {
222
+ throw (0, util_1.newNotFoundError)();
223
+ }
224
+ case 'EPERM':
225
+ case 'EACCES':
226
+ throw (0, util_1.newNotAllowedError)();
227
+ case 'ENOTEMPTY':
228
+ throw new DOMException('The object can not be modified in this way.', 'InvalidModificationError');
229
+ }
230
+ }
231
+ throw error;
232
+ }
233
+ });
234
+ }
235
+ /**
236
+ * The `resolve()` method of the {@link FileSystemDirectoryHandle} interface
237
+ * returns an {@link Array} of directory names from the parent handle to the specified
238
+ * child entry, with the name of the child entry as the last array item.
239
+ *
240
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/resolve
241
+ * @param possibleDescendant The {@link NodeFileSystemFileHandle} from which
242
+ * to return the relative path.
243
+ */
244
+ resolve(possibleDescendant) {
245
+ return __awaiter(this, void 0, void 0, function* () {
246
+ if (possibleDescendant instanceof NodeFileSystemDirectoryHandle ||
247
+ possibleDescendant instanceof NodeFileSystemFileHandle_1.NodeFileSystemFileHandle) {
248
+ const path = this.__path;
249
+ const childPath = possibleDescendant.__path;
250
+ if (!childPath.startsWith(path))
251
+ return null;
252
+ let relative = childPath.slice(path.length);
253
+ if (relative === '')
254
+ return [];
255
+ const separator = this.ctx.separator;
256
+ if (relative[0] === separator)
257
+ relative = relative.slice(1);
258
+ return relative.split(separator);
259
+ }
260
+ return null;
261
+ });
262
+ }
263
+ }
264
+ exports.NodeFileSystemDirectoryHandle = NodeFileSystemDirectoryHandle;
@@ -0,0 +1,28 @@
1
+ import { NodeFileSystemHandle } from './NodeFileSystemHandle';
2
+ import { NodeFileSystemWritableFileStream } from './NodeFileSystemWritableFileStream';
3
+ import type { NodeFsaContext, NodeFsaFs } from './types';
4
+ import type { IFileSystemFileHandle, IFileSystemSyncAccessHandle } from '../fsa/types';
5
+ export declare class NodeFileSystemFileHandle extends NodeFileSystemHandle implements IFileSystemFileHandle {
6
+ protected readonly fs: NodeFsaFs;
7
+ readonly __path: string;
8
+ protected readonly ctx: NodeFsaContext;
9
+ constructor(fs: NodeFsaFs, __path: string, ctx?: Partial<NodeFsaContext>);
10
+ /**
11
+ * Returns a {@link Promise} which resolves to a {@link File} object
12
+ * representing the state on disk of the entry represented by the handle.
13
+ *
14
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/getFile
15
+ */
16
+ getFile(): Promise<File>;
17
+ /**
18
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createSyncAccessHandle
19
+ */
20
+ get createSyncAccessHandle(): undefined | (() => Promise<IFileSystemSyncAccessHandle>);
21
+ /**
22
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createWritable
23
+ */
24
+ createWritable({ keepExistingData }?: CreateWritableOptions): Promise<NodeFileSystemWritableFileStream>;
25
+ }
26
+ export interface CreateWritableOptions {
27
+ keepExistingData?: boolean;
28
+ }
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.NodeFileSystemFileHandle = void 0;
13
+ const NodeFileSystemHandle_1 = require("./NodeFileSystemHandle");
14
+ const NodeFileSystemSyncAccessHandle_1 = require("./NodeFileSystemSyncAccessHandle");
15
+ const util_1 = require("./util");
16
+ const NodeFileSystemWritableFileStream_1 = require("./NodeFileSystemWritableFileStream");
17
+ class NodeFileSystemFileHandle extends NodeFileSystemHandle_1.NodeFileSystemHandle {
18
+ constructor(fs, __path, ctx = {}) {
19
+ ctx = (0, util_1.ctx)(ctx);
20
+ super('file', (0, util_1.basename)(__path, ctx.separator));
21
+ this.fs = fs;
22
+ this.__path = __path;
23
+ this.ctx = ctx;
24
+ }
25
+ /**
26
+ * Returns a {@link Promise} which resolves to a {@link File} object
27
+ * representing the state on disk of the entry represented by the handle.
28
+ *
29
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/getFile
30
+ */
31
+ getFile() {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ try {
34
+ const path = this.__path;
35
+ const promises = this.fs.promises;
36
+ const stats = yield promises.stat(path);
37
+ const data = yield promises.readFile(path);
38
+ const file = new File([data], this.name, { lastModified: stats.mtime.getTime() });
39
+ return file;
40
+ }
41
+ catch (error) {
42
+ if (error instanceof DOMException)
43
+ throw error;
44
+ if (error && typeof error === 'object') {
45
+ switch (error.code) {
46
+ case 'EPERM':
47
+ case 'EACCES':
48
+ throw (0, util_1.newNotAllowedError)();
49
+ }
50
+ }
51
+ throw error;
52
+ }
53
+ });
54
+ }
55
+ /**
56
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createSyncAccessHandle
57
+ */
58
+ get createSyncAccessHandle() {
59
+ if (!this.ctx.syncHandleAllowed)
60
+ return undefined;
61
+ return () => __awaiter(this, void 0, void 0, function* () { return new NodeFileSystemSyncAccessHandle_1.NodeFileSystemSyncAccessHandle(this.fs, this.__path, this.ctx); });
62
+ }
63
+ /**
64
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createWritable
65
+ */
66
+ createWritable({ keepExistingData = false } = { keepExistingData: false }) {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ (0, util_1.assertCanWrite)(this.ctx.mode);
69
+ return new NodeFileSystemWritableFileStream_1.NodeFileSystemWritableFileStream(this.fs, this.__path, keepExistingData);
70
+ });
71
+ }
72
+ }
73
+ exports.NodeFileSystemFileHandle = NodeFileSystemFileHandle;
@@ -0,0 +1,33 @@
1
+ import { NodePermissionStatus } from './NodePermissionStatus';
2
+ import type { IFileSystemHandle, FileSystemHandlePermissionDescriptor } from '../fsa/types';
3
+ /**
4
+ * Represents a File System Access API file handle `FileSystemHandle` object,
5
+ * which was created from a Node.js `fs` module.
6
+ *
7
+ * @see [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle)
8
+ */
9
+ export declare abstract class NodeFileSystemHandle implements IFileSystemHandle {
10
+ readonly kind: 'file' | 'directory';
11
+ readonly name: string;
12
+ constructor(kind: 'file' | 'directory', name: string);
13
+ /**
14
+ * Compares two handles to see if the associated entries (either a file or directory) match.
15
+ *
16
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/isSameEntry
17
+ */
18
+ isSameEntry(fileSystemHandle: NodeFileSystemHandle): boolean;
19
+ /**
20
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/queryPermission
21
+ */
22
+ queryPermission(fileSystemHandlePermissionDescriptor: FileSystemHandlePermissionDescriptor): NodePermissionStatus;
23
+ /**
24
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/remove
25
+ */
26
+ remove({ recursive }?: {
27
+ recursive?: boolean;
28
+ }): Promise<void>;
29
+ /**
30
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
31
+ */
32
+ requestPermission(fileSystemHandlePermissionDescriptor: FileSystemHandlePermissionDescriptor): NodePermissionStatus;
33
+ }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.NodeFileSystemHandle = void 0;
13
+ /**
14
+ * Represents a File System Access API file handle `FileSystemHandle` object,
15
+ * which was created from a Node.js `fs` module.
16
+ *
17
+ * @see [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle)
18
+ */
19
+ class NodeFileSystemHandle {
20
+ constructor(kind, name) {
21
+ this.kind = kind;
22
+ this.name = name;
23
+ }
24
+ /**
25
+ * Compares two handles to see if the associated entries (either a file or directory) match.
26
+ *
27
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/isSameEntry
28
+ */
29
+ isSameEntry(fileSystemHandle) {
30
+ return (this.constructor === fileSystemHandle.constructor && this.__path === fileSystemHandle.__path);
31
+ }
32
+ /**
33
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/queryPermission
34
+ */
35
+ queryPermission(fileSystemHandlePermissionDescriptor) {
36
+ throw new Error('Not implemented');
37
+ }
38
+ /**
39
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/remove
40
+ */
41
+ remove({ recursive } = { recursive: false }) {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ throw new Error('Not implemented');
44
+ });
45
+ }
46
+ /**
47
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
48
+ */
49
+ requestPermission(fileSystemHandlePermissionDescriptor) {
50
+ throw new Error('Not implemented');
51
+ }
52
+ }
53
+ exports.NodeFileSystemHandle = NodeFileSystemHandle;
@@ -0,0 +1,42 @@
1
+ import type { FileSystemReadWriteOptions, IFileSystemSyncAccessHandle } from '../fsa/types';
2
+ import type { NodeFsaContext, NodeFsaFs } from './types';
3
+ /**
4
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle
5
+ */
6
+ export declare class NodeFileSystemSyncAccessHandle implements IFileSystemSyncAccessHandle {
7
+ protected readonly fs: NodeFsaFs;
8
+ protected readonly path: string;
9
+ protected readonly ctx: NodeFsaContext;
10
+ protected readonly fd: number;
11
+ constructor(fs: NodeFsaFs, path: string, ctx: NodeFsaContext);
12
+ /**
13
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/close
14
+ */
15
+ close(): Promise<void>;
16
+ /**
17
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/flush
18
+ */
19
+ flush(): Promise<void>;
20
+ /**
21
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/getSize
22
+ */
23
+ getSize(): Promise<number>;
24
+ /**
25
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/read
26
+ */
27
+ read(buffer: ArrayBuffer | ArrayBufferView, options?: FileSystemReadWriteOptions): Promise<number>;
28
+ /**
29
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/truncate
30
+ * @param newSize The number of bytes to resize the file to.
31
+ */
32
+ truncate(newSize: number): Promise<void>;
33
+ /**
34
+ * Writes the content of a specified buffer to the file associated with the
35
+ * handle, optionally at a given offset.
36
+ *
37
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/write
38
+ * @param buffer
39
+ * @param options
40
+ */
41
+ write(buffer: ArrayBuffer | ArrayBufferView | DataView, options?: FileSystemReadWriteOptions): Promise<number>;
42
+ }
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.NodeFileSystemSyncAccessHandle = void 0;
13
+ const util_1 = require("./util");
14
+ /**
15
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle
16
+ */
17
+ class NodeFileSystemSyncAccessHandle {
18
+ constructor(fs, path, ctx) {
19
+ this.fs = fs;
20
+ this.path = path;
21
+ this.ctx = ctx;
22
+ this.fd = fs.openSync(path, 'r+');
23
+ }
24
+ /**
25
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/close
26
+ */
27
+ close() {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ (0, util_1.assertCanWrite)(this.ctx.mode);
30
+ this.fs.closeSync(this.fd);
31
+ });
32
+ }
33
+ /**
34
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/flush
35
+ */
36
+ flush() {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ (0, util_1.assertCanWrite)(this.ctx.mode);
39
+ this.fs.fsyncSync(this.fd);
40
+ });
41
+ }
42
+ /**
43
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/getSize
44
+ */
45
+ getSize() {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ return this.fs.statSync(this.path).size;
48
+ });
49
+ }
50
+ /**
51
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/read
52
+ */
53
+ read(buffer, options = {}) {
54
+ var _a;
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ const buf = buffer instanceof ArrayBuffer ? Buffer.from(buffer) : buffer;
57
+ try {
58
+ const size = this.fs.readSync(this.fd, buf, 0, buffer.byteLength, (_a = options.at) !== null && _a !== void 0 ? _a : 0);
59
+ return size;
60
+ }
61
+ catch (error) {
62
+ if (error instanceof DOMException)
63
+ throw error;
64
+ if (error && typeof error === 'object') {
65
+ switch (error.code) {
66
+ case 'EBADF': {
67
+ throw new DOMException('File handle already closed.', 'InvalidStateError');
68
+ }
69
+ }
70
+ }
71
+ throw error;
72
+ }
73
+ });
74
+ }
75
+ /**
76
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/truncate
77
+ * @param newSize The number of bytes to resize the file to.
78
+ */
79
+ truncate(newSize) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ (0, util_1.assertCanWrite)(this.ctx.mode);
82
+ this.fs.truncateSync(this.fd, newSize);
83
+ });
84
+ }
85
+ /**
86
+ * Writes the content of a specified buffer to the file associated with the
87
+ * handle, optionally at a given offset.
88
+ *
89
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/write
90
+ * @param buffer
91
+ * @param options
92
+ */
93
+ write(buffer, options = {}) {
94
+ var _a;
95
+ return __awaiter(this, void 0, void 0, function* () {
96
+ (0, util_1.assertCanWrite)(this.ctx.mode);
97
+ const buf = buffer instanceof ArrayBuffer ? Buffer.from(buffer) : buffer;
98
+ try {
99
+ return this.fs.writeSync(this.fd, buf, 0, buffer.byteLength, (_a = options.at) !== null && _a !== void 0 ? _a : 0);
100
+ }
101
+ catch (error) {
102
+ if (error instanceof DOMException)
103
+ throw error;
104
+ if (error && typeof error === 'object') {
105
+ switch (error.code) {
106
+ case 'EBADF': {
107
+ throw new DOMException('File handle already closed.', 'InvalidStateError');
108
+ }
109
+ }
110
+ }
111
+ throw error;
112
+ }
113
+ });
114
+ }
115
+ }
116
+ exports.NodeFileSystemSyncAccessHandle = NodeFileSystemSyncAccessHandle;
@@ -0,0 +1,54 @@
1
+ import type { Data, FileSystemWritableFileStreamParams, IFileSystemWritableFileStream } from '../fsa/types';
2
+ import type { IFileHandle } from '../node/types/misc';
3
+ import type { NodeFsaFs } from './types';
4
+ /**
5
+ * When Chrome writes to the file, it creates a copy of the file with extension
6
+ * `.crswap` and then replaces the original file with the copy only when the
7
+ * `close()` method is called. If the `abort()` method is called, the `.crswap`
8
+ * file is deleted.
9
+ *
10
+ * If a file name with with extension `.crswap` is already taken, it
11
+ * creates a new swap file with extension `.1.crswap` and so on.
12
+ */
13
+ export declare const createSwapFile: (fs: NodeFsaFs, path: string, keepExistingData: boolean) => Promise<[handle: IFileHandle, path: string]>;
14
+ interface SwapFile {
15
+ /** Swap file full path name. */
16
+ path: string;
17
+ /** Seek offset in the file. */
18
+ offset: number;
19
+ /** Node.js open FileHandle. */
20
+ handle?: IFileHandle;
21
+ /** Resolves when swap file is ready for operations. */
22
+ ready?: Promise<void>;
23
+ }
24
+ /**
25
+ * Is a WritableStream object with additional convenience methods, which
26
+ * operates on a single file on disk. The interface is accessed through the
27
+ * `FileSystemFileHandle.createWritable()` method.
28
+ *
29
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream
30
+ */
31
+ export declare class NodeFileSystemWritableFileStream extends WritableStream implements IFileSystemWritableFileStream {
32
+ protected readonly fs: NodeFsaFs;
33
+ protected readonly path: string;
34
+ protected readonly swap: SwapFile;
35
+ constructor(fs: NodeFsaFs, path: string, keepExistingData: boolean);
36
+ /**
37
+ * @sse https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream/seek
38
+ * @param position An `unsigned long` describing the byte position from the top
39
+ * (beginning) of the file.
40
+ */
41
+ seek(position: number): Promise<void>;
42
+ /**
43
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream/truncate
44
+ * @param size An `unsigned long` of the amount of bytes to resize the stream to.
45
+ */
46
+ truncate(size: number): Promise<void>;
47
+ protected writeBase(chunk: Data): Promise<void>;
48
+ /**
49
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream/write
50
+ */
51
+ write(chunk: Data): Promise<void>;
52
+ write(params: FileSystemWritableFileStreamParams): Promise<void>;
53
+ }
54
+ export {};