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.
- package/LICENSE +201 -24
- package/README.md +21 -92
- package/lib/Dirent.d.ts +2 -1
- package/lib/__tests__/util.d.ts +13 -0
- package/lib/__tests__/util.js +29 -0
- package/lib/consts/AMODE.d.ts +9 -0
- package/lib/consts/FLAG.d.ts +22 -0
- package/lib/consts/FLAG.js +2 -0
- package/lib/fsa/types.d.ts +3 -3
- package/lib/fsa-to-node/FsaNodeCore.d.ts +32 -0
- package/lib/fsa-to-node/FsaNodeCore.js +168 -0
- package/lib/fsa-to-node/FsaNodeDirent.d.ts +13 -0
- package/lib/fsa-to-node/FsaNodeDirent.js +31 -0
- package/lib/fsa-to-node/FsaNodeFs.d.ts +171 -0
- package/lib/fsa-to-node/FsaNodeFs.js +842 -0
- package/lib/fsa-to-node/FsaNodeFsOpenFile.d.ts +24 -0
- package/lib/fsa-to-node/FsaNodeFsOpenFile.js +46 -0
- package/lib/fsa-to-node/FsaNodeReadStream.d.ts +24 -0
- package/lib/fsa-to-node/FsaNodeReadStream.js +100 -0
- package/lib/fsa-to-node/FsaNodeStats.d.ts +30 -0
- package/lib/fsa-to-node/FsaNodeStats.js +52 -0
- package/lib/fsa-to-node/FsaNodeWriteStream.d.ts +48 -0
- package/lib/fsa-to-node/FsaNodeWriteStream.js +162 -0
- package/lib/fsa-to-node/__tests__/FsaNodeFs.test.d.ts +1 -0
- package/lib/fsa-to-node/__tests__/FsaNodeFs.test.js +873 -0
- package/lib/fsa-to-node/__tests__/util.test.d.ts +1 -0
- package/lib/fsa-to-node/__tests__/util.test.js +28 -0
- package/lib/fsa-to-node/constants.d.ts +3 -0
- package/lib/fsa-to-node/constants.js +2 -0
- package/lib/fsa-to-node/index.d.ts +2 -0
- package/lib/fsa-to-node/index.js +7 -0
- package/lib/fsa-to-node/json.d.ts +4 -0
- package/lib/fsa-to-node/json.js +7 -0
- package/lib/fsa-to-node/types.d.ts +37 -0
- package/lib/fsa-to-node/types.js +2 -0
- package/lib/fsa-to-node/util.d.ts +4 -0
- package/lib/fsa-to-node/util.js +40 -0
- package/lib/fsa-to-node/worker/FsaNodeSyncAdapterWorker.d.ts +10 -0
- package/lib/fsa-to-node/worker/FsaNodeSyncAdapterWorker.js +72 -0
- package/lib/fsa-to-node/worker/FsaNodeSyncWorker.d.ts +20 -0
- package/lib/fsa-to-node/worker/FsaNodeSyncWorker.js +190 -0
- package/lib/fsa-to-node/worker/SyncMessenger.d.ts +24 -0
- package/lib/fsa-to-node/worker/SyncMessenger.js +80 -0
- package/lib/fsa-to-node/worker/constants.d.ts +8 -0
- package/lib/fsa-to-node/worker/constants.js +2 -0
- package/lib/fsa-to-node/worker/types.d.ts +17 -0
- package/lib/fsa-to-node/worker/types.js +2 -0
- package/lib/index.d.ts +5 -4
- package/lib/index.js +1 -1
- package/lib/node/constants.d.ts +41 -0
- package/lib/node/constants.js +56 -0
- package/lib/node/options.d.ts +23 -0
- package/lib/node/options.js +94 -0
- package/lib/node/promises.d.ts +1 -1
- package/lib/node/promises.js +1 -3
- package/lib/node/types/FsCommonObjects.d.ts +17 -0
- package/lib/node/types/FsCommonObjects.js +2 -0
- package/lib/node/types/{sync.d.ts → FsSynchronousApi.d.ts} +36 -37
- package/lib/node/types/FsSynchronousApi.js +2 -0
- package/lib/node/types/callback.d.ts +57 -58
- package/lib/node/types/index.d.ts +1 -1
- package/lib/node/types/misc.d.ts +6 -8
- package/lib/node/types/options.d.ts +20 -6
- package/lib/node/types/promises.d.ts +2 -2
- package/lib/node/util.d.ts +19 -0
- package/lib/node/util.js +265 -1
- package/lib/node-to-fsa/NodeFileSystemDirectoryHandle.d.ts +71 -0
- package/lib/node-to-fsa/NodeFileSystemDirectoryHandle.js +264 -0
- package/lib/node-to-fsa/NodeFileSystemFileHandle.d.ts +28 -0
- package/lib/node-to-fsa/NodeFileSystemFileHandle.js +73 -0
- package/lib/node-to-fsa/NodeFileSystemHandle.d.ts +33 -0
- package/lib/node-to-fsa/NodeFileSystemHandle.js +53 -0
- package/lib/node-to-fsa/NodeFileSystemSyncAccessHandle.d.ts +42 -0
- package/lib/node-to-fsa/NodeFileSystemSyncAccessHandle.js +116 -0
- package/lib/node-to-fsa/NodeFileSystemWritableFileStream.d.ts +54 -0
- package/lib/node-to-fsa/NodeFileSystemWritableFileStream.js +195 -0
- package/lib/node-to-fsa/NodePermissionStatus.d.ts +8 -0
- package/lib/node-to-fsa/NodePermissionStatus.js +13 -0
- package/lib/node-to-fsa/__tests__/NodeFileSystemDirectoryHandle.test.d.ts +1 -0
- package/lib/node-to-fsa/__tests__/NodeFileSystemDirectoryHandle.test.js +627 -0
- package/lib/node-to-fsa/__tests__/NodeFileSystemFileHandle.test.d.ts +1 -0
- package/lib/node-to-fsa/__tests__/NodeFileSystemFileHandle.test.js +191 -0
- package/lib/node-to-fsa/__tests__/NodeFileSystemHandle.test.d.ts +1 -0
- package/lib/node-to-fsa/__tests__/NodeFileSystemHandle.test.js +49 -0
- package/lib/node-to-fsa/__tests__/NodeFileSystemSyncAccessHandle.test.d.ts +1 -0
- package/lib/node-to-fsa/__tests__/NodeFileSystemSyncAccessHandle.test.js +183 -0
- package/lib/node-to-fsa/__tests__/NodeFileSystemWritableFileStream.test.d.ts +1 -0
- package/lib/node-to-fsa/__tests__/NodeFileSystemWritableFileStream.test.js +106 -0
- package/lib/node-to-fsa/__tests__/scenarios.test.d.ts +1 -0
- package/lib/node-to-fsa/__tests__/scenarios.test.js +46 -0
- package/lib/node-to-fsa/__tests__/util.test.d.ts +1 -0
- package/lib/node-to-fsa/__tests__/util.test.js +25 -0
- package/lib/node-to-fsa/index.d.ts +7 -0
- package/lib/node-to-fsa/index.js +26 -0
- package/lib/node-to-fsa/types.d.ts +12 -0
- package/lib/node-to-fsa/types.js +2 -0
- package/lib/node-to-fsa/util.d.ts +11 -0
- package/lib/node-to-fsa/util.js +33 -0
- package/lib/node.d.ts +1 -1
- package/lib/volume.d.ts +45 -126
- package/lib/volume.js +204 -562
- package/lib/webfs/index.d.ts +1 -0
- package/lib/webfs/index.js +22 -0
- package/package.json +28 -5
- /package/lib/{node/types/sync.js → consts/AMODE.js} +0 -0
|
@@ -0,0 +1,842 @@
|
|
|
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 __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
12
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
13
|
+
var m = o[Symbol.asyncIterator], i;
|
|
14
|
+
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);
|
|
15
|
+
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); }); }; }
|
|
16
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
17
|
+
};
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.FsaNodeFs = void 0;
|
|
20
|
+
const optHelpers = require("../node/options");
|
|
21
|
+
const util = require("../node/util");
|
|
22
|
+
const promises_1 = require("../node/promises");
|
|
23
|
+
const util_1 = require("./util");
|
|
24
|
+
const constants_1 = require("../node/constants");
|
|
25
|
+
const encoding_1 = require("../encoding");
|
|
26
|
+
const FsaNodeDirent_1 = require("./FsaNodeDirent");
|
|
27
|
+
const constants_2 = require("../constants");
|
|
28
|
+
const FsaNodeStats_1 = require("./FsaNodeStats");
|
|
29
|
+
const process_1 = require("../process");
|
|
30
|
+
const FsaNodeWriteStream_1 = require("./FsaNodeWriteStream");
|
|
31
|
+
const FsaNodeReadStream_1 = require("./FsaNodeReadStream");
|
|
32
|
+
const FsaNodeCore_1 = require("./FsaNodeCore");
|
|
33
|
+
const notSupported = () => {
|
|
34
|
+
throw new Error('Method not supported by the File System Access API.');
|
|
35
|
+
};
|
|
36
|
+
const noop = () => { };
|
|
37
|
+
/**
|
|
38
|
+
* Constructs a Node.js `fs` API from a File System Access API
|
|
39
|
+
* [`FileSystemDirectoryHandle` object](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle).
|
|
40
|
+
*/
|
|
41
|
+
class FsaNodeFs extends FsaNodeCore_1.FsaNodeCore {
|
|
42
|
+
constructor() {
|
|
43
|
+
// ------------------------------------------------------------ FsPromisesApi
|
|
44
|
+
super(...arguments);
|
|
45
|
+
this.promises = (0, promises_1.createPromisesApi)(this);
|
|
46
|
+
// ------------------------------------------------------------ FsCallbackApi
|
|
47
|
+
this.open = (path, flags, a, b) => {
|
|
48
|
+
let mode = a;
|
|
49
|
+
let callback = b;
|
|
50
|
+
if (typeof a === 'function') {
|
|
51
|
+
mode = 438 /* MODE.DEFAULT */;
|
|
52
|
+
callback = a;
|
|
53
|
+
}
|
|
54
|
+
mode = mode || 438 /* MODE.DEFAULT */;
|
|
55
|
+
const modeNum = util.modeToNumber(mode);
|
|
56
|
+
const filename = util.pathToFilename(path);
|
|
57
|
+
const flagsNum = util.flagsToNumber(flags);
|
|
58
|
+
this.__open(filename, flagsNum, modeNum).then(openFile => callback(null, openFile.fd), error => callback(error));
|
|
59
|
+
};
|
|
60
|
+
this.close = (fd, callback) => {
|
|
61
|
+
util.validateFd(fd);
|
|
62
|
+
this.getFileByFdAsync(fd, 'close')
|
|
63
|
+
.then(file => file.close())
|
|
64
|
+
.then(() => {
|
|
65
|
+
this.fds.delete(fd);
|
|
66
|
+
this.releasedFds.push(fd);
|
|
67
|
+
callback(null);
|
|
68
|
+
}, error => {
|
|
69
|
+
callback(error);
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
this.read = (fd, buffer, offset, length, position, callback) => {
|
|
73
|
+
util.validateCallback(callback);
|
|
74
|
+
// This `if` branch is from Node.js
|
|
75
|
+
if (length === 0) {
|
|
76
|
+
return process_1.default.nextTick(() => {
|
|
77
|
+
if (callback)
|
|
78
|
+
callback(null, 0, buffer);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
const openFile = yield this.getFileByFd(fd, 'read');
|
|
83
|
+
const file = yield openFile.file.getFile();
|
|
84
|
+
const src = yield file.arrayBuffer();
|
|
85
|
+
const slice = new Uint8Array(src, Number(position), Number(length));
|
|
86
|
+
const dest = new Uint8Array(buffer.buffer, buffer.byteOffset + offset, slice.length);
|
|
87
|
+
dest.set(slice, 0);
|
|
88
|
+
return slice.length;
|
|
89
|
+
}))().then(bytesWritten => callback(null, bytesWritten, buffer), error => callback(error));
|
|
90
|
+
};
|
|
91
|
+
this.readFile = (id, a, b) => {
|
|
92
|
+
const [opts, callback] = optHelpers.optsAndCbGenerator(optHelpers.getReadFileOptions)(a, b);
|
|
93
|
+
const flagsNum = util.flagsToNumber(opts.flag);
|
|
94
|
+
return this.__getFileById(id, 'readFile')
|
|
95
|
+
.then(file => file.getFile())
|
|
96
|
+
.then(file => file.arrayBuffer())
|
|
97
|
+
.then(data => {
|
|
98
|
+
const buffer = Buffer.from(data);
|
|
99
|
+
callback(null, util.bufferToEncoding(buffer, opts.encoding));
|
|
100
|
+
})
|
|
101
|
+
.catch(error => {
|
|
102
|
+
callback(error);
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
this.write = (fd, a, b, c, d, e) => {
|
|
106
|
+
const [, asStr, buf, offset, length, position, cb] = util.getWriteArgs(fd, a, b, c, d, e);
|
|
107
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
const openFile = yield this.getFileByFd(fd, 'write');
|
|
109
|
+
const data = buf.subarray(offset, offset + length);
|
|
110
|
+
yield openFile.write(data, position);
|
|
111
|
+
return length;
|
|
112
|
+
}))().then(bytesWritten => cb(null, bytesWritten, asStr ? a : buf), error => cb(error));
|
|
113
|
+
};
|
|
114
|
+
this.writev = (fd, buffers, a, b) => {
|
|
115
|
+
util.validateFd(fd);
|
|
116
|
+
let position = null;
|
|
117
|
+
let callback;
|
|
118
|
+
if (typeof a === 'function') {
|
|
119
|
+
callback = a;
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
position = Number(a);
|
|
123
|
+
callback = b;
|
|
124
|
+
}
|
|
125
|
+
util.validateCallback(callback);
|
|
126
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
127
|
+
const openFile = yield this.getFileByFd(fd, 'writev');
|
|
128
|
+
const length = buffers.length;
|
|
129
|
+
let bytesWritten = 0;
|
|
130
|
+
for (let i = 0; i < length; i++) {
|
|
131
|
+
const data = buffers[i];
|
|
132
|
+
yield openFile.write(data, position);
|
|
133
|
+
bytesWritten += data.byteLength;
|
|
134
|
+
position = null;
|
|
135
|
+
}
|
|
136
|
+
return bytesWritten;
|
|
137
|
+
}))().then(bytesWritten => callback(null, bytesWritten, buffers), error => callback(error));
|
|
138
|
+
};
|
|
139
|
+
this.writeFile = (id, data, a, b) => {
|
|
140
|
+
let options = a;
|
|
141
|
+
let callback = b;
|
|
142
|
+
if (typeof a === 'function') {
|
|
143
|
+
options = optHelpers.writeFileDefaults;
|
|
144
|
+
callback = a;
|
|
145
|
+
}
|
|
146
|
+
const cb = util.validateCallback(callback);
|
|
147
|
+
const opts = optHelpers.getWriteFileOptions(options);
|
|
148
|
+
const flagsNum = util.flagsToNumber(opts.flag);
|
|
149
|
+
const modeNum = util.modeToNumber(opts.mode);
|
|
150
|
+
const buf = util.dataToBuffer(data, opts.encoding);
|
|
151
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
152
|
+
const createIfMissing = !!(flagsNum & 64 /* FLAG.O_CREAT */);
|
|
153
|
+
const file = yield this.__getFileById(id, 'writeFile', createIfMissing);
|
|
154
|
+
const writable = yield file.createWritable({ keepExistingData: false });
|
|
155
|
+
yield writable.write(buf);
|
|
156
|
+
yield writable.close();
|
|
157
|
+
}))().then(() => cb(null), error => cb(error));
|
|
158
|
+
};
|
|
159
|
+
this.copyFile = (src, dest, a, b) => {
|
|
160
|
+
const srcFilename = util.pathToFilename(src);
|
|
161
|
+
const destFilename = util.pathToFilename(dest);
|
|
162
|
+
let flags;
|
|
163
|
+
let callback;
|
|
164
|
+
if (typeof a === 'function') {
|
|
165
|
+
flags = 0;
|
|
166
|
+
callback = a;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
flags = a;
|
|
170
|
+
callback = b;
|
|
171
|
+
}
|
|
172
|
+
util.validateCallback(callback);
|
|
173
|
+
const [oldFolder, oldName] = (0, util_1.pathToLocation)(srcFilename);
|
|
174
|
+
const [newFolder, newName] = (0, util_1.pathToLocation)(destFilename);
|
|
175
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
const oldFile = yield this.getFile(oldFolder, oldName, 'copyFile');
|
|
177
|
+
const newDir = yield this.getDir(newFolder, false, 'copyFile');
|
|
178
|
+
const newFile = yield newDir.getFileHandle(newName, { create: true });
|
|
179
|
+
const writable = yield newFile.createWritable({ keepExistingData: false });
|
|
180
|
+
const oldData = yield oldFile.getFile();
|
|
181
|
+
yield writable.write(yield oldData.arrayBuffer());
|
|
182
|
+
yield writable.close();
|
|
183
|
+
}))().then(() => callback(null), error => callback(error));
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* @todo There is a proposal for native "self remove" operation.
|
|
187
|
+
* @see https://github.com/whatwg/fs/blob/main/proposals/Remove.md
|
|
188
|
+
*/
|
|
189
|
+
this.unlink = (path, callback) => {
|
|
190
|
+
const filename = util.pathToFilename(path);
|
|
191
|
+
const [folder, name] = (0, util_1.pathToLocation)(filename);
|
|
192
|
+
this.getDir(folder, false, 'unlink')
|
|
193
|
+
.then(dir => dir.removeEntry(name))
|
|
194
|
+
.then(() => callback(null), error => {
|
|
195
|
+
if (error && typeof error === 'object') {
|
|
196
|
+
switch (error.name) {
|
|
197
|
+
case 'NotFoundError': {
|
|
198
|
+
callback(util.createError('ENOENT', 'unlink', filename));
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
case 'InvalidModificationError': {
|
|
202
|
+
callback(util.createError('EISDIR', 'unlink', filename));
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
callback(error);
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
this.realpath = (path, a, b) => {
|
|
211
|
+
const [opts, callback] = optHelpers.getRealpathOptsAndCb(a, b);
|
|
212
|
+
let pathFilename = util.pathToFilename(path);
|
|
213
|
+
if (pathFilename[0] !== "/" /* FsaToNodeConstants.Separator */)
|
|
214
|
+
pathFilename = "/" /* FsaToNodeConstants.Separator */ + pathFilename;
|
|
215
|
+
callback(null, (0, encoding_1.strToEncoding)(pathFilename, opts.encoding));
|
|
216
|
+
};
|
|
217
|
+
this.stat = (path, a, b) => {
|
|
218
|
+
const [{ bigint = false, throwIfNoEntry = true }, callback] = optHelpers.getStatOptsAndCb(a, b);
|
|
219
|
+
const filename = util.pathToFilename(path);
|
|
220
|
+
const [folder, name] = (0, util_1.pathToLocation)(filename);
|
|
221
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
const handle = yield this.getFileOrDir(folder, name, 'stat');
|
|
223
|
+
return yield this.getHandleStats(bigint, handle);
|
|
224
|
+
}))().then(stats => callback(null, stats), error => callback(error));
|
|
225
|
+
};
|
|
226
|
+
this.lstat = this.stat;
|
|
227
|
+
this.fstat = (fd, a, b) => {
|
|
228
|
+
const [{ bigint = false, throwIfNoEntry = true }, callback] = optHelpers.getStatOptsAndCb(a, b);
|
|
229
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
230
|
+
const openFile = yield this.getFileByFd(fd, 'fstat');
|
|
231
|
+
return yield this.getHandleStats(bigint, openFile.file);
|
|
232
|
+
}))().then(stats => callback(null, stats), error => callback(error));
|
|
233
|
+
};
|
|
234
|
+
/**
|
|
235
|
+
* @todo There is a proposal for native move support.
|
|
236
|
+
* @see https://github.com/whatwg/fs/blob/main/proposals/MovingNonOpfsFiles.md
|
|
237
|
+
*/
|
|
238
|
+
this.rename = (oldPath, newPath, callback) => {
|
|
239
|
+
const oldPathFilename = util.pathToFilename(oldPath);
|
|
240
|
+
const newPathFilename = util.pathToFilename(newPath);
|
|
241
|
+
const [oldFolder, oldName] = (0, util_1.pathToLocation)(oldPathFilename);
|
|
242
|
+
const [newFolder, newName] = (0, util_1.pathToLocation)(newPathFilename);
|
|
243
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
244
|
+
const oldFile = yield this.getFile(oldFolder, oldName, 'rename');
|
|
245
|
+
const newDir = yield this.getDir(newFolder, false, 'rename');
|
|
246
|
+
const newFile = yield newDir.getFileHandle(newName, { create: true });
|
|
247
|
+
const writable = yield newFile.createWritable({ keepExistingData: false });
|
|
248
|
+
const oldData = yield oldFile.getFile();
|
|
249
|
+
yield writable.write(yield oldData.arrayBuffer());
|
|
250
|
+
yield writable.close();
|
|
251
|
+
const oldDir = yield this.getDir(oldFolder, false, 'rename');
|
|
252
|
+
yield oldDir.removeEntry(oldName);
|
|
253
|
+
}))().then(() => callback(null), error => callback(error));
|
|
254
|
+
};
|
|
255
|
+
this.exists = (path, callback) => {
|
|
256
|
+
const filename = util.pathToFilename(path);
|
|
257
|
+
if (typeof callback !== 'function')
|
|
258
|
+
throw Error(constants_1.ERRSTR.CB);
|
|
259
|
+
this.access(path, 0 /* AMODE.F_OK */, error => callback(!error));
|
|
260
|
+
};
|
|
261
|
+
this.access = (path, a, b) => {
|
|
262
|
+
let mode = 0 /* AMODE.F_OK */;
|
|
263
|
+
let callback;
|
|
264
|
+
if (typeof a !== 'function') {
|
|
265
|
+
mode = a | 0; // cast to number
|
|
266
|
+
callback = util.validateCallback(b);
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
callback = a;
|
|
270
|
+
}
|
|
271
|
+
const filename = util.pathToFilename(path);
|
|
272
|
+
const [folder, name] = (0, util_1.pathToLocation)(filename);
|
|
273
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
274
|
+
const node = folder.length || name ? yield this.getFileOrDir(folder, name, 'access') : yield this.root;
|
|
275
|
+
const checkIfCanExecute = mode & 1 /* AMODE.X_OK */;
|
|
276
|
+
if (checkIfCanExecute)
|
|
277
|
+
throw util.createError('EACCESS', 'access', filename);
|
|
278
|
+
const checkIfCanWrite = mode & 2 /* AMODE.W_OK */;
|
|
279
|
+
switch (node.kind) {
|
|
280
|
+
case 'file': {
|
|
281
|
+
if (checkIfCanWrite) {
|
|
282
|
+
try {
|
|
283
|
+
const file = node;
|
|
284
|
+
const writable = yield file.createWritable();
|
|
285
|
+
yield writable.close();
|
|
286
|
+
}
|
|
287
|
+
catch (_a) {
|
|
288
|
+
throw util.createError('EACCESS', 'access', filename);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
case 'directory': {
|
|
294
|
+
if (checkIfCanWrite) {
|
|
295
|
+
const dir = node;
|
|
296
|
+
const canWrite = yield (0, util_1.testDirectoryIsWritable)(dir);
|
|
297
|
+
if (!canWrite)
|
|
298
|
+
throw util.createError('EACCESS', 'access', filename);
|
|
299
|
+
}
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
default: {
|
|
303
|
+
throw util.createError('EACCESS', 'access', filename);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}))().then(() => callback(null), error => callback(error));
|
|
307
|
+
};
|
|
308
|
+
this.appendFile = (id, data, a, b) => {
|
|
309
|
+
const [opts, callback] = optHelpers.getAppendFileOptsAndCb(a, b);
|
|
310
|
+
const buffer = util.dataToBuffer(data, opts.encoding);
|
|
311
|
+
this.getFileByIdOrCreate(id, 'appendFile')
|
|
312
|
+
.then(file => (() => __awaiter(this, void 0, void 0, function* () {
|
|
313
|
+
const blob = yield file.getFile();
|
|
314
|
+
const writable = yield file.createWritable({ keepExistingData: true });
|
|
315
|
+
yield writable.write({
|
|
316
|
+
type: 'write',
|
|
317
|
+
data: buffer,
|
|
318
|
+
position: blob.size,
|
|
319
|
+
});
|
|
320
|
+
yield writable.close();
|
|
321
|
+
}))())
|
|
322
|
+
.then(() => callback(null), error => callback(error));
|
|
323
|
+
};
|
|
324
|
+
this.readdir = (path, a, b) => {
|
|
325
|
+
const [options, callback] = optHelpers.getReaddirOptsAndCb(a, b);
|
|
326
|
+
const filename = util.pathToFilename(path);
|
|
327
|
+
const [folder, name] = (0, util_1.pathToLocation)(filename);
|
|
328
|
+
if (name)
|
|
329
|
+
folder.push(name);
|
|
330
|
+
this.getDir(folder, false, 'readdir')
|
|
331
|
+
.then(dir => (() => __awaiter(this, void 0, void 0, function* () {
|
|
332
|
+
var _a, e_1, _b, _c, _d, e_2, _e, _f;
|
|
333
|
+
if (options.withFileTypes) {
|
|
334
|
+
const list = [];
|
|
335
|
+
try {
|
|
336
|
+
for (var _g = true, _h = __asyncValues(dir.entries()), _j; _j = yield _h.next(), _a = _j.done, !_a;) {
|
|
337
|
+
_c = _j.value;
|
|
338
|
+
_g = false;
|
|
339
|
+
try {
|
|
340
|
+
const [name, handle] = _c;
|
|
341
|
+
const dirent = new FsaNodeDirent_1.FsaNodeDirent(name, handle.kind);
|
|
342
|
+
list.push(dirent);
|
|
343
|
+
}
|
|
344
|
+
finally {
|
|
345
|
+
_g = true;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
350
|
+
finally {
|
|
351
|
+
try {
|
|
352
|
+
if (!_g && !_a && (_b = _h.return)) yield _b.call(_h);
|
|
353
|
+
}
|
|
354
|
+
finally { if (e_1) throw e_1.error; }
|
|
355
|
+
}
|
|
356
|
+
if (!util.isWin && options.encoding !== 'buffer')
|
|
357
|
+
list.sort((a, b) => {
|
|
358
|
+
if (a.name < b.name)
|
|
359
|
+
return -1;
|
|
360
|
+
if (a.name > b.name)
|
|
361
|
+
return 1;
|
|
362
|
+
return 0;
|
|
363
|
+
});
|
|
364
|
+
return list;
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
const list = [];
|
|
368
|
+
try {
|
|
369
|
+
for (var _k = true, _l = __asyncValues(dir.keys()), _m; _m = yield _l.next(), _d = _m.done, !_d;) {
|
|
370
|
+
_f = _m.value;
|
|
371
|
+
_k = false;
|
|
372
|
+
try {
|
|
373
|
+
const key = _f;
|
|
374
|
+
list.push(key);
|
|
375
|
+
}
|
|
376
|
+
finally {
|
|
377
|
+
_k = true;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
382
|
+
finally {
|
|
383
|
+
try {
|
|
384
|
+
if (!_k && !_d && (_e = _l.return)) yield _e.call(_l);
|
|
385
|
+
}
|
|
386
|
+
finally { if (e_2) throw e_2.error; }
|
|
387
|
+
}
|
|
388
|
+
if (!util.isWin && options.encoding !== 'buffer')
|
|
389
|
+
list.sort();
|
|
390
|
+
return list;
|
|
391
|
+
}
|
|
392
|
+
}))())
|
|
393
|
+
.then(res => callback(null, res), err => callback(err));
|
|
394
|
+
};
|
|
395
|
+
this.readlink = (path, a, b) => {
|
|
396
|
+
const [opts, callback] = optHelpers.getDefaultOptsAndCb(a, b);
|
|
397
|
+
const filename = util.pathToFilename(path);
|
|
398
|
+
const buffer = Buffer.from(filename);
|
|
399
|
+
callback(null, util.bufferToEncoding(buffer, opts.encoding));
|
|
400
|
+
};
|
|
401
|
+
/** @todo Could this use `FileSystemSyncAccessHandle.flush` through a Worker thread? */
|
|
402
|
+
this.fsync = (fd, callback) => {
|
|
403
|
+
callback(null);
|
|
404
|
+
};
|
|
405
|
+
this.fdatasync = (fd, callback) => {
|
|
406
|
+
callback(null);
|
|
407
|
+
};
|
|
408
|
+
this.ftruncate = (fd, a, b) => {
|
|
409
|
+
const len = typeof a === 'number' ? a : 0;
|
|
410
|
+
const callback = util.validateCallback(typeof a === 'number' ? b : a);
|
|
411
|
+
this.getFileByFdAsync(fd)
|
|
412
|
+
.then(file => file.file.createWritable({ keepExistingData: true }))
|
|
413
|
+
.then(writable => writable.truncate(len).then(() => writable.close()))
|
|
414
|
+
.then(() => callback(null), error => callback(error));
|
|
415
|
+
};
|
|
416
|
+
this.truncate = (path, a, b) => {
|
|
417
|
+
const len = typeof a === 'number' ? a : 0;
|
|
418
|
+
const callback = util.validateCallback(typeof a === 'number' ? b : a);
|
|
419
|
+
this.open(path, 'r+', (error, fd) => {
|
|
420
|
+
if (error)
|
|
421
|
+
callback(error);
|
|
422
|
+
else {
|
|
423
|
+
this.ftruncate(fd, len, error => {
|
|
424
|
+
if (error)
|
|
425
|
+
this.close(fd, () => callback(error));
|
|
426
|
+
else
|
|
427
|
+
this.close(fd, callback);
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
};
|
|
432
|
+
this.futimes = (fd, atime, mtime, callback) => {
|
|
433
|
+
callback(null);
|
|
434
|
+
};
|
|
435
|
+
this.utimes = (path, atime, mtime, callback) => {
|
|
436
|
+
callback(null);
|
|
437
|
+
};
|
|
438
|
+
this.mkdir = (path, a, b) => {
|
|
439
|
+
var _a;
|
|
440
|
+
const opts = optHelpers.getMkdirOptions(a);
|
|
441
|
+
const callback = util.validateCallback(typeof a === 'function' ? a : b);
|
|
442
|
+
// const modeNum = modeToNumber(opts.mode, 0o777);
|
|
443
|
+
const filename = util.pathToFilename(path);
|
|
444
|
+
const [folder, name] = (0, util_1.pathToLocation)(filename);
|
|
445
|
+
// TODO: need to throw if directory already exists
|
|
446
|
+
this.getDir(folder, (_a = opts.recursive) !== null && _a !== void 0 ? _a : false)
|
|
447
|
+
.then(dir => dir.getDirectoryHandle(name, { create: true }))
|
|
448
|
+
.then(() => callback(null), error => {
|
|
449
|
+
if (error && typeof error === 'object') {
|
|
450
|
+
switch (error.name) {
|
|
451
|
+
case 'NotFoundError': {
|
|
452
|
+
const err = util.createError('ENOENT', 'mkdir', folder.join('/'));
|
|
453
|
+
callback(err);
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
callback(error);
|
|
459
|
+
});
|
|
460
|
+
};
|
|
461
|
+
this.mkdtemp = (prefix, a, b) => {
|
|
462
|
+
const [{ encoding }, callback] = optHelpers.getDefaultOptsAndCb(a, b);
|
|
463
|
+
if (!prefix || typeof prefix !== 'string')
|
|
464
|
+
throw new TypeError('filename prefix is required');
|
|
465
|
+
if (!util.nullCheck(prefix))
|
|
466
|
+
return;
|
|
467
|
+
const filename = prefix + util.genRndStr6();
|
|
468
|
+
this.mkdir(filename, 511 /* MODE.DIR */, err => {
|
|
469
|
+
if (err)
|
|
470
|
+
callback(err);
|
|
471
|
+
else
|
|
472
|
+
callback(null, (0, encoding_1.strToEncoding)(filename, encoding));
|
|
473
|
+
});
|
|
474
|
+
};
|
|
475
|
+
this.rmdir = (path, a, b) => {
|
|
476
|
+
const options = optHelpers.getRmdirOptions(a);
|
|
477
|
+
const callback = util.validateCallback(typeof a === 'function' ? a : b);
|
|
478
|
+
const [folder, name] = (0, util_1.pathToLocation)(util.pathToFilename(path));
|
|
479
|
+
if (!name && options.recursive)
|
|
480
|
+
return this.rmAll(callback);
|
|
481
|
+
this.getDir(folder, false, 'rmdir')
|
|
482
|
+
.then(dir => dir.getDirectoryHandle(name).then(() => dir))
|
|
483
|
+
.then(dir => { var _a; return dir.removeEntry(name, { recursive: (_a = options.recursive) !== null && _a !== void 0 ? _a : false }); })
|
|
484
|
+
.then(() => callback(null), error => {
|
|
485
|
+
if (error && typeof error === 'object') {
|
|
486
|
+
switch (error.name) {
|
|
487
|
+
case 'NotFoundError': {
|
|
488
|
+
const err = util.createError('ENOENT', 'rmdir', folder.join('/'));
|
|
489
|
+
callback(err);
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
case 'InvalidModificationError': {
|
|
493
|
+
const err = util.createError('ENOTEMPTY', 'rmdir', folder.join('/'));
|
|
494
|
+
callback(err);
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
callback(error);
|
|
500
|
+
});
|
|
501
|
+
};
|
|
502
|
+
this.rm = (path, a, b) => {
|
|
503
|
+
const [options, callback] = optHelpers.getRmOptsAndCb(a, b);
|
|
504
|
+
const [folder, name] = (0, util_1.pathToLocation)(util.pathToFilename(path));
|
|
505
|
+
if (!name && options.recursive)
|
|
506
|
+
return this.rmAll(callback);
|
|
507
|
+
this.getDir(folder, false, 'rmdir')
|
|
508
|
+
.then(dir => { var _a; return dir.removeEntry(name, { recursive: (_a = options.recursive) !== null && _a !== void 0 ? _a : false }); })
|
|
509
|
+
.then(() => callback(null), error => {
|
|
510
|
+
if (options.force) {
|
|
511
|
+
callback(null);
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
if (error && typeof error === 'object') {
|
|
515
|
+
switch (error.name) {
|
|
516
|
+
case 'NotFoundError': {
|
|
517
|
+
const err = util.createError('ENOENT', 'rmdir', folder.join('/'));
|
|
518
|
+
callback(err);
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
case 'InvalidModificationError': {
|
|
522
|
+
const err = util.createError('ENOTEMPTY', 'rmdir', folder.join('/'));
|
|
523
|
+
callback(err);
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
callback(error);
|
|
529
|
+
});
|
|
530
|
+
};
|
|
531
|
+
this.fchmod = (fd, mode, callback) => {
|
|
532
|
+
callback(null);
|
|
533
|
+
};
|
|
534
|
+
this.chmod = (path, mode, callback) => {
|
|
535
|
+
callback(null);
|
|
536
|
+
};
|
|
537
|
+
this.lchmod = (path, mode, callback) => {
|
|
538
|
+
callback(null);
|
|
539
|
+
};
|
|
540
|
+
this.fchown = (fd, uid, gid, callback) => {
|
|
541
|
+
callback(null);
|
|
542
|
+
};
|
|
543
|
+
this.chown = (path, uid, gid, callback) => {
|
|
544
|
+
callback(null);
|
|
545
|
+
};
|
|
546
|
+
this.lchown = (path, uid, gid, callback) => {
|
|
547
|
+
callback(null);
|
|
548
|
+
};
|
|
549
|
+
this.createWriteStream = (path, options) => {
|
|
550
|
+
var _a;
|
|
551
|
+
const defaults = {
|
|
552
|
+
encoding: 'utf8',
|
|
553
|
+
flags: 'w',
|
|
554
|
+
autoClose: true,
|
|
555
|
+
emitClose: true,
|
|
556
|
+
};
|
|
557
|
+
const optionsObj = optHelpers.getOptions(defaults, options);
|
|
558
|
+
const filename = util.pathToFilename(path);
|
|
559
|
+
const flags = util.flagsToNumber((_a = optionsObj.flags) !== null && _a !== void 0 ? _a : 'w');
|
|
560
|
+
const fd = optionsObj.fd ? (typeof optionsObj.fd === 'number' ? optionsObj.fd : optionsObj.fd.fd) : 0;
|
|
561
|
+
const handle = fd ? this.getFileByFdAsync(fd) : this.__open(filename, flags, 0);
|
|
562
|
+
const stream = new FsaNodeWriteStream_1.FsaNodeWriteStream(handle, filename, optionsObj);
|
|
563
|
+
if (optionsObj.autoClose) {
|
|
564
|
+
stream.once('finish', () => {
|
|
565
|
+
handle.then(file => this.close(file.fd, () => { }));
|
|
566
|
+
});
|
|
567
|
+
stream.once('error', () => {
|
|
568
|
+
handle.then(file => this.close(file.fd, () => { }));
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
return stream;
|
|
572
|
+
};
|
|
573
|
+
this.createReadStream = (path, options) => {
|
|
574
|
+
const defaults = {
|
|
575
|
+
flags: 'r',
|
|
576
|
+
fd: null,
|
|
577
|
+
mode: 0o666,
|
|
578
|
+
autoClose: true,
|
|
579
|
+
emitClose: true,
|
|
580
|
+
start: 0,
|
|
581
|
+
end: Infinity,
|
|
582
|
+
highWaterMark: 64 * 1024,
|
|
583
|
+
fs: null,
|
|
584
|
+
signal: null,
|
|
585
|
+
};
|
|
586
|
+
const optionsObj = optHelpers.getOptions(defaults, options);
|
|
587
|
+
const filename = util.pathToFilename(path);
|
|
588
|
+
const flags = util.flagsToNumber(optionsObj.flags);
|
|
589
|
+
const fd = optionsObj.fd ? (typeof optionsObj.fd === 'number' ? optionsObj.fd : optionsObj.fd.fd) : 0;
|
|
590
|
+
const handle = fd ? this.getFileByFdAsync(fd) : this.__open(filename, flags, 0);
|
|
591
|
+
const stream = new FsaNodeReadStream_1.FsaNodeReadStream(this, handle, filename, optionsObj);
|
|
592
|
+
return stream;
|
|
593
|
+
};
|
|
594
|
+
this.symlink = notSupported;
|
|
595
|
+
this.link = notSupported;
|
|
596
|
+
/**
|
|
597
|
+
* @todo Watchers could be implemented in the future on top of `FileSystemObserver`,
|
|
598
|
+
* which is currently a proposal.
|
|
599
|
+
* @see https://github.com/whatwg/fs/blob/main/proposals/FileSystemObserver.md
|
|
600
|
+
*/
|
|
601
|
+
this.watchFile = notSupported;
|
|
602
|
+
this.unwatchFile = notSupported;
|
|
603
|
+
this.watch = notSupported;
|
|
604
|
+
// --------------------------------------------------------- FsSynchronousApi
|
|
605
|
+
this.statSync = (path, options) => {
|
|
606
|
+
var _a;
|
|
607
|
+
const { bigint = true, throwIfNoEntry = true } = optHelpers.getStatOptions(options);
|
|
608
|
+
const filename = util.pathToFilename(path);
|
|
609
|
+
const location = (0, util_1.pathToLocation)(filename);
|
|
610
|
+
const adapter = this.getSyncAdapter();
|
|
611
|
+
const res = adapter.call('stat', location);
|
|
612
|
+
const stats = new FsaNodeStats_1.FsaNodeStats(bigint, (_a = res.size) !== null && _a !== void 0 ? _a : 0, res.kind);
|
|
613
|
+
return stats;
|
|
614
|
+
};
|
|
615
|
+
this.lstatSync = this.statSync;
|
|
616
|
+
this.fstatSync = (fd, options) => {
|
|
617
|
+
const filename = this.getFileName(fd);
|
|
618
|
+
return this.statSync(filename, options);
|
|
619
|
+
};
|
|
620
|
+
this.accessSync = (path, mode = 0 /* AMODE.F_OK */) => {
|
|
621
|
+
const filename = util.pathToFilename(path);
|
|
622
|
+
mode = mode | 0;
|
|
623
|
+
const adapter = this.getSyncAdapter();
|
|
624
|
+
adapter.call('access', [filename, mode]);
|
|
625
|
+
};
|
|
626
|
+
this.readFileSync = (id, options) => {
|
|
627
|
+
const opts = optHelpers.getReadFileOptions(options);
|
|
628
|
+
const flagsNum = util.flagsToNumber(opts.flag);
|
|
629
|
+
const filename = this.getFileName(id);
|
|
630
|
+
const adapter = this.getSyncAdapter();
|
|
631
|
+
const uint8 = adapter.call('readFile', [filename, opts]);
|
|
632
|
+
const buffer = Buffer.from(uint8.buffer, uint8.byteOffset, uint8.byteLength);
|
|
633
|
+
return util.bufferToEncoding(buffer, opts.encoding);
|
|
634
|
+
};
|
|
635
|
+
this.writeFileSync = (id, data, options) => {
|
|
636
|
+
const opts = optHelpers.getWriteFileOptions(options);
|
|
637
|
+
const flagsNum = util.flagsToNumber(opts.flag);
|
|
638
|
+
const modeNum = util.modeToNumber(opts.mode);
|
|
639
|
+
const buf = util.dataToBuffer(data, opts.encoding);
|
|
640
|
+
const filename = this.getFileName(id);
|
|
641
|
+
const adapter = this.getSyncAdapter();
|
|
642
|
+
adapter.call('writeFile', [filename, util.bufToUint8(buf), opts]);
|
|
643
|
+
};
|
|
644
|
+
this.appendFileSync = (id, data, options) => {
|
|
645
|
+
const opts = optHelpers.getAppendFileOpts(options);
|
|
646
|
+
if (!opts.flag || util.isFd(id))
|
|
647
|
+
opts.flag = 'a';
|
|
648
|
+
const filename = this.getFileName(id);
|
|
649
|
+
const buf = util.dataToBuffer(data, opts.encoding);
|
|
650
|
+
const adapter = this.getSyncAdapter();
|
|
651
|
+
adapter.call('appendFile', [filename, util.bufToUint8(buf), opts]);
|
|
652
|
+
};
|
|
653
|
+
this.closeSync = (fd) => {
|
|
654
|
+
util.validateFd(fd);
|
|
655
|
+
const file = this.getFileByFd(fd, 'close');
|
|
656
|
+
file.close().catch(() => { });
|
|
657
|
+
this.fds.delete(fd);
|
|
658
|
+
this.releasedFds.push(fd);
|
|
659
|
+
};
|
|
660
|
+
this.existsSync = (path) => {
|
|
661
|
+
try {
|
|
662
|
+
this.statSync(path);
|
|
663
|
+
return true;
|
|
664
|
+
}
|
|
665
|
+
catch (_a) {
|
|
666
|
+
return false;
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
this.copyFileSync = (src, dest, flags) => {
|
|
670
|
+
const srcFilename = util.pathToFilename(src);
|
|
671
|
+
const destFilename = util.pathToFilename(dest);
|
|
672
|
+
const adapter = this.getSyncAdapter();
|
|
673
|
+
adapter.call('copy', [srcFilename, destFilename, flags]);
|
|
674
|
+
};
|
|
675
|
+
this.renameSync = (oldPath, newPath) => {
|
|
676
|
+
const srcFilename = util.pathToFilename(oldPath);
|
|
677
|
+
const destFilename = util.pathToFilename(newPath);
|
|
678
|
+
const adapter = this.getSyncAdapter();
|
|
679
|
+
adapter.call('move', [srcFilename, destFilename]);
|
|
680
|
+
};
|
|
681
|
+
this.rmdirSync = (path, opts) => {
|
|
682
|
+
const filename = util.pathToFilename(path);
|
|
683
|
+
const adapter = this.getSyncAdapter();
|
|
684
|
+
adapter.call('rmdir', [filename, opts]);
|
|
685
|
+
};
|
|
686
|
+
this.rmSync = (path, options) => {
|
|
687
|
+
const filename = util.pathToFilename(path);
|
|
688
|
+
const adapter = this.getSyncAdapter();
|
|
689
|
+
adapter.call('rm', [filename, options]);
|
|
690
|
+
};
|
|
691
|
+
this.mkdirSync = (path, options) => {
|
|
692
|
+
const opts = optHelpers.getMkdirOptions(options);
|
|
693
|
+
const modeNum = util.modeToNumber(opts.mode, 0o777);
|
|
694
|
+
const filename = util.pathToFilename(path);
|
|
695
|
+
return this.getSyncAdapter().call('mkdir', [filename, options]);
|
|
696
|
+
};
|
|
697
|
+
this.mkdtempSync = (prefix, options) => {
|
|
698
|
+
const { encoding } = optHelpers.getDefaultOpts(options);
|
|
699
|
+
if (!prefix || typeof prefix !== 'string')
|
|
700
|
+
throw new TypeError('filename prefix is required');
|
|
701
|
+
util.nullCheck(prefix);
|
|
702
|
+
const result = this.getSyncAdapter().call('mkdtemp', [prefix, options]);
|
|
703
|
+
return (0, encoding_1.strToEncoding)(result, encoding);
|
|
704
|
+
};
|
|
705
|
+
this.readlinkSync = (path, options) => {
|
|
706
|
+
const opts = optHelpers.getDefaultOpts(options);
|
|
707
|
+
const filename = util.pathToFilename(path);
|
|
708
|
+
const buffer = Buffer.from(filename);
|
|
709
|
+
return util.bufferToEncoding(buffer, opts.encoding);
|
|
710
|
+
};
|
|
711
|
+
this.truncateSync = (id, len) => {
|
|
712
|
+
if (util.isFd(id))
|
|
713
|
+
return this.ftruncateSync(id, len);
|
|
714
|
+
const filename = util.pathToFilename(id);
|
|
715
|
+
this.getSyncAdapter().call('trunc', [filename, Number(len) || 0]);
|
|
716
|
+
};
|
|
717
|
+
this.ftruncateSync = (fd, len) => {
|
|
718
|
+
const filename = this.getFileName(fd);
|
|
719
|
+
this.truncateSync(filename, len);
|
|
720
|
+
};
|
|
721
|
+
this.unlinkSync = (path) => {
|
|
722
|
+
const filename = util.pathToFilename(path);
|
|
723
|
+
this.getSyncAdapter().call('unlink', [filename]);
|
|
724
|
+
};
|
|
725
|
+
this.readdirSync = (path, options) => {
|
|
726
|
+
const opts = optHelpers.getReaddirOptions(options);
|
|
727
|
+
const filename = util.pathToFilename(path);
|
|
728
|
+
const adapter = this.getSyncAdapter();
|
|
729
|
+
const list = adapter.call('readdir', [filename]);
|
|
730
|
+
if (opts.withFileTypes) {
|
|
731
|
+
const res = [];
|
|
732
|
+
for (const entry of list)
|
|
733
|
+
res.push(new FsaNodeDirent_1.FsaNodeDirent(entry.name, entry.kind));
|
|
734
|
+
return res;
|
|
735
|
+
}
|
|
736
|
+
else {
|
|
737
|
+
const res = [];
|
|
738
|
+
for (const entry of list) {
|
|
739
|
+
const buffer = Buffer.from(entry.name);
|
|
740
|
+
res.push(util.bufferToEncoding(buffer, opts.encoding));
|
|
741
|
+
}
|
|
742
|
+
return res;
|
|
743
|
+
}
|
|
744
|
+
};
|
|
745
|
+
this.realpathSync = (path, options) => {
|
|
746
|
+
let filename = util.pathToFilename(path);
|
|
747
|
+
const { encoding } = optHelpers.getRealpathOptions(options);
|
|
748
|
+
if (filename[0] !== "/" /* FsaToNodeConstants.Separator */)
|
|
749
|
+
filename = "/" /* FsaToNodeConstants.Separator */ + filename;
|
|
750
|
+
return (0, encoding_1.strToEncoding)(filename, encoding);
|
|
751
|
+
};
|
|
752
|
+
this.readSync = (fd, buffer, offset, length, position) => {
|
|
753
|
+
util.validateFd(fd);
|
|
754
|
+
const filename = this.getFileName(fd);
|
|
755
|
+
const adapter = this.getSyncAdapter();
|
|
756
|
+
const uint8 = adapter.call('read', [filename, position, length]);
|
|
757
|
+
const dest = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
758
|
+
dest.set(uint8, offset);
|
|
759
|
+
return uint8.length;
|
|
760
|
+
};
|
|
761
|
+
this.writeSync = (fd, a, b, c, d) => {
|
|
762
|
+
const [, buf, offset, length, position] = util.getWriteSyncArgs(fd, a, b, c, d);
|
|
763
|
+
const filename = this.getFileName(fd);
|
|
764
|
+
const data = new Uint8Array(buf.buffer, buf.byteOffset + offset, length);
|
|
765
|
+
return this.getSyncAdapter().call('write', [filename, data, position || null]);
|
|
766
|
+
};
|
|
767
|
+
this.openSync = (path, flags, mode = 438 /* MODE.DEFAULT */) => {
|
|
768
|
+
const modeNum = util.modeToNumber(mode);
|
|
769
|
+
const filename = util.pathToFilename(path);
|
|
770
|
+
const flagsNum = util.flagsToNumber(flags);
|
|
771
|
+
const adapter = this.getSyncAdapter();
|
|
772
|
+
const handle = adapter.call('open', [filename, flagsNum, modeNum]);
|
|
773
|
+
const openFile = this.__open2(handle, filename, flagsNum, modeNum);
|
|
774
|
+
return openFile.fd;
|
|
775
|
+
};
|
|
776
|
+
this.fdatasyncSync = noop;
|
|
777
|
+
this.fsyncSync = noop;
|
|
778
|
+
this.chmodSync = noop;
|
|
779
|
+
this.chownSync = noop;
|
|
780
|
+
this.fchmodSync = noop;
|
|
781
|
+
this.fchownSync = noop;
|
|
782
|
+
this.futimesSync = noop;
|
|
783
|
+
this.lchmodSync = noop;
|
|
784
|
+
this.lchownSync = noop;
|
|
785
|
+
this.utimesSync = noop;
|
|
786
|
+
this.symlinkSync = notSupported;
|
|
787
|
+
this.linkSync = notSupported;
|
|
788
|
+
// ---------------------------------------------------------- FsCommonObjects
|
|
789
|
+
this.F_OK = constants_2.constants.F_OK;
|
|
790
|
+
this.R_OK = constants_2.constants.R_OK;
|
|
791
|
+
this.W_OK = constants_2.constants.W_OK;
|
|
792
|
+
this.X_OK = constants_2.constants.X_OK;
|
|
793
|
+
this.constants = constants_2.constants;
|
|
794
|
+
this.Dirent = FsaNodeDirent_1.FsaNodeDirent;
|
|
795
|
+
this.Stats = (FsaNodeStats_1.FsaNodeStats);
|
|
796
|
+
this.WriteStream = FsaNodeWriteStream_1.FsaNodeWriteStream;
|
|
797
|
+
this.ReadStream = FsaNodeReadStream_1.FsaNodeReadStream;
|
|
798
|
+
this.StatFs = 0;
|
|
799
|
+
this.Dir = 0;
|
|
800
|
+
this.StatsWatcher = 0;
|
|
801
|
+
this.FSWatcher = 0;
|
|
802
|
+
}
|
|
803
|
+
getHandleStats(bigint, handle) {
|
|
804
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
805
|
+
let size = 0;
|
|
806
|
+
if (handle.kind === 'file') {
|
|
807
|
+
const file = handle;
|
|
808
|
+
const fileData = yield file.getFile();
|
|
809
|
+
size = fileData.size;
|
|
810
|
+
}
|
|
811
|
+
const stats = new FsaNodeStats_1.FsaNodeStats(bigint, bigint ? BigInt(size) : size, handle.kind);
|
|
812
|
+
return stats;
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
rmAll(callback) {
|
|
816
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
817
|
+
var _a, e_3, _b, _c;
|
|
818
|
+
const root = yield this.root;
|
|
819
|
+
try {
|
|
820
|
+
for (var _d = true, _e = __asyncValues(root.keys()), _f; _f = yield _e.next(), _a = _f.done, !_a;) {
|
|
821
|
+
_c = _f.value;
|
|
822
|
+
_d = false;
|
|
823
|
+
try {
|
|
824
|
+
const name = _c;
|
|
825
|
+
yield root.removeEntry(name, { recursive: true });
|
|
826
|
+
}
|
|
827
|
+
finally {
|
|
828
|
+
_d = true;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
833
|
+
finally {
|
|
834
|
+
try {
|
|
835
|
+
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
836
|
+
}
|
|
837
|
+
finally { if (e_3) throw e_3.error; }
|
|
838
|
+
}
|
|
839
|
+
}))().then(() => callback(null), error => callback(error));
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
exports.FsaNodeFs = FsaNodeFs;
|