@zenfs/core 0.0.11 → 0.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 (40) hide show
  1. package/dist/ApiError.d.ts +1 -1
  2. package/dist/ApiError.js +17 -16
  3. package/dist/backends/AsyncMirror.js +43 -49
  4. package/dist/backends/AsyncStore.js +326 -383
  5. package/dist/backends/FolderAdapter.js +8 -19
  6. package/dist/backends/Locked.d.ts +7 -8
  7. package/dist/backends/Locked.js +97 -143
  8. package/dist/backends/OverlayFS.js +240 -284
  9. package/dist/backends/SyncStore.js +2 -2
  10. package/dist/backends/backend.d.ts +6 -6
  11. package/dist/browser.min.js +23 -6
  12. package/dist/browser.min.js.map +4 -4
  13. package/dist/emulation/callbacks.d.ts +109 -72
  14. package/dist/emulation/callbacks.js +40 -47
  15. package/dist/emulation/dir.d.ts +55 -0
  16. package/dist/emulation/dir.js +104 -0
  17. package/dist/emulation/fs.d.ts +1 -2
  18. package/dist/emulation/fs.js +0 -1
  19. package/dist/emulation/index.d.ts +3 -0
  20. package/dist/emulation/index.js +3 -0
  21. package/dist/emulation/promises.d.ts +71 -50
  22. package/dist/emulation/promises.js +244 -328
  23. package/dist/emulation/shared.d.ts +14 -0
  24. package/dist/emulation/shared.js +4 -3
  25. package/dist/emulation/streams.d.ts +102 -0
  26. package/dist/emulation/streams.js +55 -0
  27. package/dist/emulation/sync.d.ts +81 -52
  28. package/dist/emulation/sync.js +117 -69
  29. package/dist/file.d.ts +11 -5
  30. package/dist/file.js +79 -76
  31. package/dist/filesystem.d.ts +15 -23
  32. package/dist/filesystem.js +186 -273
  33. package/dist/index.d.ts +3 -3
  34. package/dist/index.js +39 -53
  35. package/dist/stats.d.ts +80 -27
  36. package/dist/stats.js +142 -93
  37. package/dist/utils.d.ts +2 -4
  38. package/dist/utils.js +80 -77
  39. package/package.json +4 -2
  40. package/readme.md +2 -40
@@ -1,18 +1,12 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { ApiError, ErrorCode } from '../ApiError.js';
11
2
  import * as constants from './constants.js';
12
3
  export { constants };
13
4
  import { FileFlag } from '../file.js';
14
5
  import { normalizePath, normalizeMode, getFdForFile, normalizeOptions, fd2file, fdMap, normalizeTime, cred, nop, resolveFS, fixError, mounts } from './shared.js';
15
- import { encode } from '../utils.js';
6
+ import { BigIntStats } from '../stats.js';
7
+ import { decode, encode } from '../utils.js';
8
+ import { Dirent } from './dir.js';
9
+ import { join } from './path.js';
16
10
  /**
17
11
  * Utility for FS ops. It handles
18
12
  * - path normalization (for the first parameter to the FS op)
@@ -25,18 +19,16 @@ import { encode } from '../utils.js';
25
19
  * @param args the rest of the parameters are passed to the FS function. Note that the first parameter is required to be a path
26
20
  * @returns
27
21
  */
28
- function doOp(...[name, resolveSymlinks, path, ...args]) {
29
- return __awaiter(this, void 0, void 0, function* () {
30
- path = normalizePath(path);
31
- const { fs, path: resolvedPath } = resolveFS(resolveSymlinks && (yield exists(path)) ? yield realpath(path) : path);
32
- try {
33
- // @ts-expect-error 2556 (since ...args is not correctly picked up as being a tuple)
34
- return fs[name](resolvedPath, ...args);
35
- }
36
- catch (e) {
37
- throw fixError(e, { [resolvedPath]: path });
38
- }
39
- });
22
+ async function doOp(...[name, resolveSymlinks, path, ...args]) {
23
+ path = normalizePath(path);
24
+ const { fs, path: resolvedPath } = resolveFS(resolveSymlinks && (await exists(path)) ? await realpath(path) : path);
25
+ try {
26
+ // @ts-expect-error 2556 (since ...args is not correctly picked up as being a tuple)
27
+ return fs[name](resolvedPath, ...args);
28
+ }
29
+ catch (e) {
30
+ throw fixError(e, { [resolvedPath]: path });
31
+ }
40
32
  }
41
33
  // fs.promises
42
34
  /**
@@ -44,66 +36,48 @@ function doOp(...[name, resolveSymlinks, path, ...args]) {
44
36
  * @param oldPath
45
37
  * @param newPath
46
38
  */
47
- export function rename(oldPath, newPath) {
48
- return __awaiter(this, void 0, void 0, function* () {
49
- oldPath = normalizePath(oldPath);
50
- newPath = normalizePath(newPath);
51
- const _old = resolveFS(oldPath);
52
- const _new = resolveFS(newPath);
53
- const paths = { [_old.path]: oldPath, [_new.path]: newPath };
54
- try {
55
- if (_old === _new) {
56
- return _old.fs.rename(_old.path, _new.path, cred);
57
- }
58
- const data = yield readFile(oldPath);
59
- yield writeFile(newPath, data);
60
- yield unlink(oldPath);
39
+ export async function rename(oldPath, newPath) {
40
+ oldPath = normalizePath(oldPath);
41
+ newPath = normalizePath(newPath);
42
+ const _old = resolveFS(oldPath);
43
+ const _new = resolveFS(newPath);
44
+ const paths = { [_old.path]: oldPath, [_new.path]: newPath };
45
+ try {
46
+ if (_old === _new) {
47
+ return _old.fs.rename(_old.path, _new.path, cred);
61
48
  }
62
- catch (e) {
63
- throw fixError(e, paths);
64
- }
65
- });
49
+ const data = await readFile(oldPath);
50
+ await writeFile(newPath, data);
51
+ await unlink(oldPath);
52
+ }
53
+ catch (e) {
54
+ throw fixError(e, paths);
55
+ }
66
56
  }
67
57
  /**
68
58
  * Test whether or not the given path exists by checking with the file system.
69
59
  * @param path
70
60
  */
71
- export function exists(path) {
72
- return __awaiter(this, void 0, void 0, function* () {
73
- path = normalizePath(path);
74
- try {
75
- const { fs, path: resolvedPath } = resolveFS(path);
76
- return fs.exists(resolvedPath, cred);
77
- }
78
- catch (e) {
79
- if (e.errno == ErrorCode.ENOENT) {
80
- return false;
81
- }
82
- throw e;
61
+ export async function exists(path) {
62
+ path = normalizePath(path);
63
+ try {
64
+ const { fs, path: resolvedPath } = resolveFS(path);
65
+ return fs.exists(resolvedPath, cred);
66
+ }
67
+ catch (e) {
68
+ if (e.errno == ErrorCode.ENOENT) {
69
+ return false;
83
70
  }
84
- });
71
+ throw e;
72
+ }
85
73
  }
86
- /**
87
- * `stat`.
88
- * @param path
89
- * @returns Stats
90
- */
91
- export function stat(path) {
92
- return __awaiter(this, void 0, void 0, function* () {
93
- return doOp('stat', true, path, cred);
94
- });
74
+ export async function stat(path, options) {
75
+ const stats = await doOp('stat', true, path, cred);
76
+ return options?.bigint ? BigIntStats.clone(stats) : stats;
95
77
  }
96
- /**
97
- * `lstat`.
98
- * `lstat()` is identical to `stat()`, except that if path is a symbolic link,
99
- * then the link itself is stat-ed, not the file that it refers to.
100
- * @param path
101
- * @return [ZenFS.node.fs.Stats]
102
- */
103
- export function lstat(path) {
104
- return __awaiter(this, void 0, void 0, function* () {
105
- return doOp('stat', false, path, cred);
106
- });
78
+ export async function lstat(path, options) {
79
+ const stats = await doOp('stat', false, path, cred);
80
+ return options?.bigint ? BigIntStats.clone(stats) : stats;
107
81
  }
108
82
  // FILE-ONLY METHODS
109
83
  /**
@@ -111,22 +85,18 @@ export function lstat(path) {
111
85
  * @param path
112
86
  * @param len
113
87
  */
114
- export function truncate(path, len = 0) {
115
- return __awaiter(this, void 0, void 0, function* () {
116
- if (len < 0) {
117
- throw new ApiError(ErrorCode.EINVAL);
118
- }
119
- return doOp('truncate', true, path, len, cred);
120
- });
88
+ export async function truncate(path, len = 0) {
89
+ if (len < 0) {
90
+ throw new ApiError(ErrorCode.EINVAL);
91
+ }
92
+ return doOp('truncate', true, path, len, cred);
121
93
  }
122
94
  /**
123
95
  * `unlink`.
124
96
  * @param path
125
97
  */
126
- export function unlink(path) {
127
- return __awaiter(this, void 0, void 0, function* () {
128
- return doOp('unlink', false, path, cred);
129
- });
98
+ export async function unlink(path) {
99
+ return doOp('unlink', false, path, cred);
130
100
  }
131
101
  /**
132
102
  * file open.
@@ -135,122 +105,110 @@ export function unlink(path) {
135
105
  * @param flags
136
106
  * @param mode defaults to `0644`
137
107
  */
138
- export function open(path, flag, mode = 0o644) {
139
- return __awaiter(this, void 0, void 0, function* () {
140
- const file = yield doOp('open', true, path, FileFlag.getFileFlag(flag), normalizeMode(mode, 0o644), cred);
141
- return getFdForFile(file);
142
- });
143
- }
144
- export function readFile(filename, arg2 = {}) {
145
- return __awaiter(this, void 0, void 0, function* () {
146
- const options = normalizeOptions(arg2, null, 'r', null);
147
- const flag = FileFlag.getFileFlag(options.flag);
148
- if (!flag.isReadable()) {
149
- throw new ApiError(ErrorCode.EINVAL, 'Flag passed to readFile must allow for reading.');
150
- }
151
- return doOp('readFile', true, filename, options.encoding, flag, cred);
152
- });
153
- }
154
- export function writeFile(filename, data, arg3) {
155
- return __awaiter(this, void 0, void 0, function* () {
156
- const options = normalizeOptions(arg3, 'utf8', 'w', 0o644);
157
- const flag = FileFlag.getFileFlag(options.flag);
158
- if (!flag.isWriteable()) {
159
- throw new ApiError(ErrorCode.EINVAL, 'Flag passed to writeFile must allow for writing.');
160
- }
161
- return doOp('writeFile', true, filename, data, options.encoding, flag, options.mode, cred);
162
- });
163
- }
164
- export function appendFile(filename, data, arg3) {
165
- return __awaiter(this, void 0, void 0, function* () {
166
- const options = normalizeOptions(arg3, 'utf8', 'a', 0o644);
167
- const flag = FileFlag.getFileFlag(options.flag);
168
- if (!flag.isAppendable()) {
169
- throw new ApiError(ErrorCode.EINVAL, 'Flag passed to appendFile must allow for appending.');
170
- }
171
- return doOp('appendFile', true, filename, data, options.encoding, flag, options.mode, cred);
172
- });
173
- }
174
- // FILE DESCRIPTOR METHODS
175
- /**
176
- * `fstat`.
177
- * `fstat()` is identical to `stat()`, except that the file to be stat-ed is
178
- * specified by the file descriptor `fd`.
179
- * @param fd
180
- * @return [ZenFS.node.fs.Stats]
181
- */
182
- export function fstat(fd) {
183
- return __awaiter(this, void 0, void 0, function* () {
184
- return fd2file(fd).stat();
185
- });
108
+ export async function open(path, flag, mode = 0o644) {
109
+ const file = await doOp('open', true, path, FileFlag.getFileFlag(flag), normalizeMode(mode, 0o644), cred);
110
+ return getFdForFile(file);
111
+ }
112
+ export async function readFile(filename, arg2 = {}) {
113
+ const options = normalizeOptions(arg2, null, 'r', null);
114
+ const flag = FileFlag.getFileFlag(options.flag);
115
+ if (!flag.isReadable()) {
116
+ throw new ApiError(ErrorCode.EINVAL, 'Flag passed to readFile must allow for reading.');
117
+ }
118
+ const data = await doOp('readFile', true, filename, flag, cred);
119
+ switch (options.encoding) {
120
+ case 'utf8':
121
+ case 'utf-8':
122
+ return decode(data);
123
+ default:
124
+ return data;
125
+ }
126
+ }
127
+ export async function writeFile(filename, data, arg3) {
128
+ const options = normalizeOptions(arg3, 'utf8', 'w', 0o644);
129
+ const flag = FileFlag.getFileFlag(options.flag);
130
+ if (!flag.isWriteable()) {
131
+ throw new ApiError(ErrorCode.EINVAL, 'Flag passed to writeFile must allow for writing.');
132
+ }
133
+ if (typeof data != 'string' && !options.encoding) {
134
+ throw new ApiError(ErrorCode.EINVAL, 'Encoding not specified');
135
+ }
136
+ const encodedData = typeof data == 'string' ? encode(data) : data;
137
+ return doOp('writeFile', true, filename, encodedData, flag, options.mode, cred);
138
+ }
139
+ export async function appendFile(filename, data, arg3) {
140
+ const options = normalizeOptions(arg3, 'utf8', 'a', 0o644);
141
+ const flag = FileFlag.getFileFlag(options.flag);
142
+ if (!flag.isAppendable()) {
143
+ throw new ApiError(ErrorCode.EINVAL, 'Flag passed to appendFile must allow for appending.');
144
+ }
145
+ if (typeof data != 'string' && !options.encoding) {
146
+ throw new ApiError(ErrorCode.EINVAL, 'Encoding not specified');
147
+ }
148
+ const encodedData = typeof data == 'string' ? encode(data) : data;
149
+ return doOp('appendFile', true, filename, encodedData, flag, options.mode, cred);
150
+ }
151
+ export async function fstat(fd, options) {
152
+ const stats = await fd2file(fd).stat();
153
+ return options?.bigint ? BigIntStats.clone(stats) : stats;
186
154
  }
187
155
  /**
188
156
  * close.
189
157
  * @param fd
190
158
  */
191
- export function close(fd) {
192
- return __awaiter(this, void 0, void 0, function* () {
193
- yield fd2file(fd).close();
194
- fdMap.delete(fd);
195
- return;
196
- });
159
+ export async function close(fd) {
160
+ await fd2file(fd).close();
161
+ fdMap.delete(fd);
162
+ return;
197
163
  }
198
164
  /**
199
165
  * ftruncate.
200
166
  * @param fd
201
167
  * @param len
202
168
  */
203
- export function ftruncate(fd, len = 0) {
204
- return __awaiter(this, void 0, void 0, function* () {
205
- const file = fd2file(fd);
206
- if (len < 0) {
207
- throw new ApiError(ErrorCode.EINVAL);
208
- }
209
- return file.truncate(len);
210
- });
169
+ export async function ftruncate(fd, len = 0) {
170
+ const file = fd2file(fd);
171
+ if (len < 0) {
172
+ throw new ApiError(ErrorCode.EINVAL);
173
+ }
174
+ return file.truncate(len);
211
175
  }
212
176
  /**
213
177
  * fsync.
214
178
  * @param fd
215
179
  */
216
- export function fsync(fd) {
217
- return __awaiter(this, void 0, void 0, function* () {
218
- return fd2file(fd).sync();
219
- });
180
+ export async function fsync(fd) {
181
+ return fd2file(fd).sync();
220
182
  }
221
183
  /**
222
184
  * fdatasync.
223
185
  * @param fd
224
186
  */
225
- export function fdatasync(fd) {
226
- return __awaiter(this, void 0, void 0, function* () {
227
- return fd2file(fd).datasync();
228
- });
229
- }
230
- export function write(fd, arg2, arg3, arg4, arg5) {
231
- return __awaiter(this, void 0, void 0, function* () {
232
- let buffer, offset = 0, length, position;
233
- if (typeof arg2 === 'string') {
234
- // Signature 1: (fd, string, [position?, [encoding?]])
235
- position = typeof arg3 === 'number' ? arg3 : null;
236
- const encoding = (typeof arg4 === 'string' ? arg4 : 'utf8');
237
- offset = 0;
238
- buffer = encode(arg2);
239
- length = buffer.length;
240
- }
241
- else {
242
- // Signature 2: (fd, buffer, offset, length, position?)
243
- buffer = arg2;
244
- offset = arg3;
245
- length = arg4;
246
- position = typeof arg5 === 'number' ? arg5 : null;
247
- }
248
- const file = fd2file(fd);
249
- if (position === undefined || position === null) {
250
- position = file.getPos();
251
- }
252
- return file.write(buffer, offset, length, position);
253
- });
187
+ export async function fdatasync(fd) {
188
+ return fd2file(fd).datasync();
189
+ }
190
+ export async function write(fd, arg2, arg3, arg4, arg5) {
191
+ let buffer, offset = 0, length, position;
192
+ if (typeof arg2 === 'string') {
193
+ // Signature 1: (fd, string, [position?, [encoding?]])
194
+ position = typeof arg3 === 'number' ? arg3 : null;
195
+ const encoding = (typeof arg4 === 'string' ? arg4 : 'utf8');
196
+ offset = 0;
197
+ buffer = encode(arg2, encoding);
198
+ length = buffer.length;
199
+ }
200
+ else {
201
+ // Signature 2: (fd, buffer, offset, length, position?)
202
+ buffer = arg2;
203
+ offset = arg3;
204
+ length = arg4;
205
+ position = typeof arg5 === 'number' ? arg5 : null;
206
+ }
207
+ const file = fd2file(fd);
208
+ if (position === undefined || position === null) {
209
+ position = file.getPos();
210
+ }
211
+ return file.write(buffer, offset, length, position);
254
212
  }
255
213
  /**
256
214
  * Read data from the file specified by `fd`.
@@ -264,14 +222,12 @@ export function write(fd, arg2, arg3, arg4, arg5) {
264
222
  * in the file. If position is null, data will be read from the current file
265
223
  * position.
266
224
  */
267
- export function read(fd, buffer, offset, length, position) {
268
- return __awaiter(this, void 0, void 0, function* () {
269
- const file = fd2file(fd);
270
- if (isNaN(+position)) {
271
- position = file.getPos();
272
- }
273
- return file.read(buffer, offset, length, position);
274
- });
225
+ export async function read(fd, buffer, offset, length, position) {
226
+ const file = fd2file(fd);
227
+ if (isNaN(+position)) {
228
+ position = file.getPos();
229
+ }
230
+ return file.read(buffer, offset, length, position);
275
231
  }
276
232
  /**
277
233
  * `fchown`.
@@ -279,21 +235,17 @@ export function read(fd, buffer, offset, length, position) {
279
235
  * @param uid
280
236
  * @param gid
281
237
  */
282
- export function fchown(fd, uid, gid) {
283
- return __awaiter(this, void 0, void 0, function* () {
284
- return fd2file(fd).chown(uid, gid);
285
- });
238
+ export async function fchown(fd, uid, gid) {
239
+ return fd2file(fd).chown(uid, gid);
286
240
  }
287
241
  /**
288
242
  * `fchmod`.
289
243
  * @param fd
290
244
  * @param mode
291
245
  */
292
- export function fchmod(fd, mode) {
293
- return __awaiter(this, void 0, void 0, function* () {
294
- const numMode = typeof mode === 'string' ? parseInt(mode, 8) : mode;
295
- return fd2file(fd).chmod(numMode);
296
- });
246
+ export async function fchmod(fd, mode) {
247
+ const numMode = typeof mode === 'string' ? parseInt(mode, 8) : mode;
248
+ return fd2file(fd).chmod(numMode);
297
249
  }
298
250
  /**
299
251
  * Change the file timestamps of a file referenced by the supplied file
@@ -302,53 +254,44 @@ export function fchmod(fd, mode) {
302
254
  * @param atime
303
255
  * @param mtime
304
256
  */
305
- export function futimes(fd, atime, mtime) {
306
- return __awaiter(this, void 0, void 0, function* () {
307
- return fd2file(fd).utimes(normalizeTime(atime), normalizeTime(mtime));
308
- });
257
+ export async function futimes(fd, atime, mtime) {
258
+ return fd2file(fd).utimes(normalizeTime(atime), normalizeTime(mtime));
309
259
  }
310
260
  // DIRECTORY-ONLY METHODS
311
261
  /**
312
262
  * `rmdir`.
313
263
  * @param path
314
264
  */
315
- export function rmdir(path) {
316
- return __awaiter(this, void 0, void 0, function* () {
317
- return doOp('rmdir', true, path, cred);
318
- });
265
+ export async function rmdir(path) {
266
+ return doOp('rmdir', true, path, cred);
319
267
  }
320
268
  /**
321
269
  * `mkdir`.
322
270
  * @param path
323
271
  * @param mode defaults to `0777`
324
272
  */
325
- export function mkdir(path, mode) {
326
- return __awaiter(this, void 0, void 0, function* () {
327
- return doOp('mkdir', true, path, normalizeMode(mode, 0o777), cred);
328
- });
329
- }
330
- /**
331
- * `readdir`. Reads the contents of a directory.
332
- * @param path
333
- * @return [String[]]
334
- */
335
- export function readdir(path) {
336
- return __awaiter(this, void 0, void 0, function* () {
337
- path = normalizePath(path);
338
- const entries = yield doOp('readdir', true, path, cred);
339
- const points = [...mounts.keys()];
340
- for (const point of points) {
341
- if (point.startsWith(path)) {
342
- const entry = point.slice(path.length);
343
- if (entry.includes('/') || entry.length == 0) {
344
- // ignore FSs mounted in subdirectories and any FS mounted to `path`.
345
- continue;
346
- }
347
- entries.push(entry);
273
+ export async function mkdir(path, mode) {
274
+ return doOp('mkdir', true, path, normalizeMode(mode, 0o777), cred);
275
+ }
276
+ export async function readdir(path, options) {
277
+ path = normalizePath(path);
278
+ const entries = await doOp('readdir', true, path, cred);
279
+ const points = [...mounts.keys()];
280
+ for (const point of points) {
281
+ if (point.startsWith(path)) {
282
+ const entry = point.slice(path.length);
283
+ if (entry.includes('/') || entry.length == 0) {
284
+ // ignore FSs mounted in subdirectories and any FS mounted to `path`.
285
+ continue;
348
286
  }
287
+ entries.push(entry);
349
288
  }
350
- return entries;
351
- });
289
+ }
290
+ const values = [];
291
+ for (const entry of entries) {
292
+ values.push(options?.withFileTypes ? new Dirent(entry, await stat(join(path, entry))) : entry);
293
+ }
294
+ return values;
352
295
  }
353
296
  // SYMLINK METHODS
354
297
  /**
@@ -356,11 +299,9 @@ export function readdir(path) {
356
299
  * @param srcpath
357
300
  * @param dstpath
358
301
  */
359
- export function link(srcpath, dstpath) {
360
- return __awaiter(this, void 0, void 0, function* () {
361
- dstpath = normalizePath(dstpath);
362
- return doOp('link', false, srcpath, dstpath, cred);
363
- });
302
+ export async function link(srcpath, dstpath) {
303
+ dstpath = normalizePath(dstpath);
304
+ return doOp('link', false, srcpath, dstpath, cred);
364
305
  }
365
306
  /**
366
307
  * `symlink`.
@@ -368,24 +309,16 @@ export function link(srcpath, dstpath) {
368
309
  * @param dstpath
369
310
  * @param type can be either `'dir'` or `'file'` (default is `'file'`)
370
311
  */
371
- export function symlink(srcpath, dstpath, type = 'file') {
372
- return __awaiter(this, void 0, void 0, function* () {
373
- if (!['file', 'dir', 'junction'].includes(type)) {
374
- throw new ApiError(ErrorCode.EINVAL, 'Invalid type: ' + type);
375
- }
376
- dstpath = normalizePath(dstpath);
377
- return doOp('symlink', false, srcpath, dstpath, type, cred);
378
- });
312
+ export async function symlink(srcpath, dstpath, type = 'file') {
313
+ if (!['file', 'dir', 'junction'].includes(type)) {
314
+ throw new ApiError(ErrorCode.EINVAL, 'Invalid type: ' + type);
315
+ }
316
+ dstpath = normalizePath(dstpath);
317
+ return doOp('symlink', false, srcpath, dstpath, type, cred);
379
318
  }
380
- /**
381
- * readlink.
382
- * @param path
383
- * @return [String]
384
- */
385
- export function readlink(path) {
386
- return __awaiter(this, void 0, void 0, function* () {
387
- return doOp('readlink', false, path, cred);
388
- });
319
+ export async function readlink(path, options) {
320
+ const value = await doOp('readlink', false, path, cred);
321
+ return encode(value, typeof options == 'object' ? options.encoding : options);
389
322
  }
390
323
  // PROPERTY OPERATIONS
391
324
  /**
@@ -394,10 +327,8 @@ export function readlink(path) {
394
327
  * @param uid
395
328
  * @param gid
396
329
  */
397
- export function chown(path, uid, gid) {
398
- return __awaiter(this, void 0, void 0, function* () {
399
- return doOp('chown', true, path, uid, gid, cred);
400
- });
330
+ export async function chown(path, uid, gid) {
331
+ return doOp('chown', true, path, uid, gid, cred);
401
332
  }
402
333
  /**
403
334
  * `lchown`.
@@ -405,38 +336,32 @@ export function chown(path, uid, gid) {
405
336
  * @param uid
406
337
  * @param gid
407
338
  */
408
- export function lchown(path, uid, gid) {
409
- return __awaiter(this, void 0, void 0, function* () {
410
- return doOp('chown', false, path, uid, gid, cred);
411
- });
339
+ export async function lchown(path, uid, gid) {
340
+ return doOp('chown', false, path, uid, gid, cred);
412
341
  }
413
342
  /**
414
343
  * `chmod`.
415
344
  * @param path
416
345
  * @param mode
417
346
  */
418
- export function chmod(path, mode) {
419
- return __awaiter(this, void 0, void 0, function* () {
420
- const numMode = normalizeMode(mode, -1);
421
- if (numMode < 0) {
422
- throw new ApiError(ErrorCode.EINVAL, `Invalid mode.`);
423
- }
424
- return doOp('chmod', true, path, numMode, cred);
425
- });
347
+ export async function chmod(path, mode) {
348
+ const numMode = normalizeMode(mode, -1);
349
+ if (numMode < 0) {
350
+ throw new ApiError(ErrorCode.EINVAL, `Invalid mode.`);
351
+ }
352
+ return doOp('chmod', true, path, numMode, cred);
426
353
  }
427
354
  /**
428
355
  * `lchmod`.
429
356
  * @param path
430
357
  * @param mode
431
358
  */
432
- export function lchmod(path, mode) {
433
- return __awaiter(this, void 0, void 0, function* () {
434
- const numMode = normalizeMode(mode, -1);
435
- if (numMode < 1) {
436
- throw new ApiError(ErrorCode.EINVAL, `Invalid mode.`);
437
- }
438
- return doOp('chmod', false, normalizePath(path), numMode, cred);
439
- });
359
+ export async function lchmod(path, mode) {
360
+ const numMode = normalizeMode(mode, -1);
361
+ if (numMode < 1) {
362
+ throw new ApiError(ErrorCode.EINVAL, `Invalid mode.`);
363
+ }
364
+ return doOp('chmod', false, normalizePath(path), numMode, cred);
440
365
  }
441
366
  /**
442
367
  * Change file timestamps of the file referenced by the supplied path.
@@ -444,10 +369,8 @@ export function lchmod(path, mode) {
444
369
  * @param atime
445
370
  * @param mtime
446
371
  */
447
- export function utimes(path, atime, mtime) {
448
- return __awaiter(this, void 0, void 0, function* () {
449
- return doOp('utimes', true, path, normalizeTime(atime), normalizeTime(mtime), cred);
450
- });
372
+ export async function utimes(path, atime, mtime) {
373
+ return doOp('utimes', true, path, normalizeTime(atime), normalizeTime(mtime), cred);
451
374
  }
452
375
  /**
453
376
  * Change file timestamps of the file referenced by the supplied path.
@@ -455,68 +378,61 @@ export function utimes(path, atime, mtime) {
455
378
  * @param atime
456
379
  * @param mtime
457
380
  */
458
- export function lutimes(path, atime, mtime) {
459
- return __awaiter(this, void 0, void 0, function* () {
460
- return doOp('utimes', false, path, normalizeTime(atime), normalizeTime(mtime), cred);
461
- });
381
+ export async function lutimes(path, atime, mtime) {
382
+ return doOp('utimes', false, path, normalizeTime(atime), normalizeTime(mtime), cred);
462
383
  }
463
384
  /**
464
385
  * `realpath`.
465
386
  * @param path
466
- * @param cache An object literal of mapped paths that can be used to
467
- * force a specific path resolution or avoid additional `fs.stat` calls for
468
- * known real paths.
469
- * @return [String]
387
+ * @param options
388
+ * @return resolved path
389
+ *
390
+ * Note: This *Can not* use doOp since doOp depends on it
470
391
  */
471
- export function realpath(path, cache = {}) {
472
- return __awaiter(this, void 0, void 0, function* () {
473
- path = normalizePath(path);
474
- const { fs, path: resolvedPath, mountPoint } = resolveFS(path);
475
- try {
476
- const stats = yield fs.stat(resolvedPath, cred);
477
- if (!stats.isSymbolicLink()) {
478
- return path;
479
- }
480
- const dst = mountPoint + normalizePath(yield fs.readlink(resolvedPath, cred));
481
- return realpath(dst);
482
- }
483
- catch (e) {
484
- throw fixError(e, { [resolvedPath]: path });
392
+ export async function realpath(path, options) {
393
+ path = normalizePath(path);
394
+ const { fs, path: resolvedPath, mountPoint } = resolveFS(path);
395
+ try {
396
+ const stats = await fs.stat(resolvedPath, cred);
397
+ if (!stats.isSymbolicLink()) {
398
+ return path;
485
399
  }
486
- });
400
+ const dst = mountPoint + normalizePath(await fs.readlink(resolvedPath, cred));
401
+ return realpath(dst);
402
+ }
403
+ catch (e) {
404
+ throw fixError(e, { [resolvedPath]: path });
405
+ }
487
406
  }
488
- export function watchFile(filename, arg2, listener = nop) {
489
- return __awaiter(this, void 0, void 0, function* () {
490
- throw new ApiError(ErrorCode.ENOTSUP);
491
- });
407
+ export async function watchFile(filename, arg2, listener = nop) {
408
+ throw new ApiError(ErrorCode.ENOTSUP);
492
409
  }
493
- export function unwatchFile(filename, listener = nop) {
494
- return __awaiter(this, void 0, void 0, function* () {
495
- throw new ApiError(ErrorCode.ENOTSUP);
496
- });
410
+ export async function unwatchFile(filename, listener = nop) {
411
+ throw new ApiError(ErrorCode.ENOTSUP);
497
412
  }
498
- export function watch(filename, arg2, listener = nop) {
499
- return __awaiter(this, void 0, void 0, function* () {
500
- throw new ApiError(ErrorCode.ENOTSUP);
501
- });
413
+ export async function watch(filename, arg2, listener = nop) {
414
+ throw new ApiError(ErrorCode.ENOTSUP);
502
415
  }
503
416
  /**
504
417
  * `access`.
505
418
  * @param path
506
419
  * @param mode
507
420
  */
508
- export function access(path, mode = 0o600) {
509
- return __awaiter(this, void 0, void 0, function* () {
510
- return doOp('access', true, path, mode, cred);
511
- });
512
- }
513
- export function createReadStream(path, options) {
514
- return __awaiter(this, void 0, void 0, function* () {
515
- throw new ApiError(ErrorCode.ENOTSUP);
516
- });
517
- }
518
- export function createWriteStream(path, options) {
519
- return __awaiter(this, void 0, void 0, function* () {
520
- throw new ApiError(ErrorCode.ENOTSUP);
521
- });
421
+ export async function access(path, mode = 0o600) {
422
+ return doOp('access', true, path, mode, cred);
423
+ }
424
+ export async function createReadStream(path, options) {
425
+ throw new ApiError(ErrorCode.ENOTSUP);
426
+ }
427
+ export async function createWriteStream(path, options) {
428
+ throw new ApiError(ErrorCode.ENOTSUP);
429
+ }
430
+ export async function rm(path) {
431
+ throw new ApiError(ErrorCode.ENOTSUP);
432
+ }
433
+ export async function mkdtemp(path) {
434
+ throw new ApiError(ErrorCode.ENOTSUP);
435
+ }
436
+ export async function copyFile(path) {
437
+ throw new ApiError(ErrorCode.ENOTSUP);
522
438
  }