@utoo/web 1.2.0-rc.5 → 1.2.0-rc.6

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 (38) hide show
  1. package/esm/{8df7dc7828270e8a8ece.wasm → fe3ccd485f4eb7cd1a1e.wasm} +0 -0
  2. package/esm/forkedProject.d.ts +2 -1
  3. package/esm/forkedProject.js +5 -0
  4. package/esm/internalProject.d.ts +3 -3
  5. package/esm/internalProject.js +33 -20
  6. package/esm/loaderWorker.js +1 -1
  7. package/esm/project.d.ts +2 -1
  8. package/esm/project.js +6 -1
  9. package/esm/sabcom.d.ts +6 -0
  10. package/esm/sabcom.js +53 -5
  11. package/esm/serviceWorker.js +1 -0
  12. package/esm/type.d.ts +38 -0
  13. package/esm/type.js +44 -0
  14. package/esm/utoo/index.d.ts +27 -24
  15. package/esm/utoo/index.js +104 -102
  16. package/esm/utoo/index_bg.wasm +0 -0
  17. package/esm/webpackLoaders/{worker/cjs.js → cjs.js} +71 -64
  18. package/esm/webpackLoaders/loaderWorkerPool.d.ts +2 -0
  19. package/esm/{loaderWorkerPool.js → webpackLoaders/loaderWorkerPool.js} +30 -18
  20. package/esm/webpackLoaders/polyfills/fsPolyfill.d.ts +78 -0
  21. package/esm/webpackLoaders/polyfills/fsPolyfill.js +279 -0
  22. package/esm/webpackLoaders/polyfills/fsPromisesPolyfill.d.ts +26 -0
  23. package/esm/webpackLoaders/polyfills/fsPromisesPolyfill.js +112 -0
  24. package/esm/webpackLoaders/{worker/type.d.ts → type.d.ts} +1 -0
  25. package/esm/webpackLoaders/{worker/index.js → worker.js} +8 -3
  26. package/package.json +3 -2
  27. package/esm/loaderWorkerPool.d.ts +0 -3
  28. package/esm/webpackLoaders/worker/polyfills/fsPolyfill.d.ts +0 -244
  29. package/esm/webpackLoaders/worker/polyfills/fsPolyfill.js +0 -369
  30. package/esm/webpackLoaders/worker/polyfills/fsPromisesPolyfill.d.ts +0 -9
  31. package/esm/webpackLoaders/worker/polyfills/fsPromisesPolyfill.js +0 -9
  32. /package/esm/webpackLoaders/{worker/cjs.d.ts → cjs.d.ts} +0 -0
  33. /package/esm/webpackLoaders/{worker/polyfills → polyfills}/nodePolyFills.d.ts +0 -0
  34. /package/esm/webpackLoaders/{worker/polyfills → polyfills}/nodePolyFills.js +0 -0
  35. /package/esm/webpackLoaders/{worker/polyfills → polyfills}/workerThreadsPolyfill.d.ts +0 -0
  36. /package/esm/webpackLoaders/{worker/polyfills → polyfills}/workerThreadsPolyfill.js +0 -0
  37. /package/esm/webpackLoaders/{worker/type.js → type.js} +0 -0
  38. /package/esm/webpackLoaders/{worker/index.d.ts → worker.d.ts} +0 -0
@@ -10,8 +10,9 @@ const searchPathsCache = {};
10
10
  const statSync = (p) => {
11
11
  if (p.includes("node_modules") &&
12
12
  Object.prototype.hasOwnProperty.call(statCache, p)) {
13
- if (statCache[p] === false)
13
+ if (statCache[p] === false) {
14
14
  throw new Error("ENOENT");
15
+ }
15
16
  return statCache[p];
16
17
  }
17
18
  try {
@@ -31,7 +32,7 @@ const existsSync = (p) => {
31
32
  statSync(p);
32
33
  return true;
33
34
  }
34
- catch (_a) {
35
+ catch (e) {
35
36
  return false;
36
37
  }
37
38
  };
@@ -89,7 +90,6 @@ const executeModule = (moduleCode, moduleId, id, importMaps, entrypoint) => {
89
90
  return finalExports;
90
91
  };
91
92
  const loadModule = (id, context, importMaps, entrypoint) => {
92
- var _a, _b, _c;
93
93
  const cacheKey = `${context}:${id}`;
94
94
  if (resolutionCache[cacheKey]) {
95
95
  const cachedId = resolutionCache[cacheKey];
@@ -149,22 +149,29 @@ const loadModule = (id, context, importMaps, entrypoint) => {
149
149
  break;
150
150
  currentDir = parent;
151
151
  }
152
+ // Ensure cwd/node_modules is always included
153
+ const cwd = nodePolyFills.process.cwd();
154
+ const cwdNodeModules = path.join(cwd, "node_modules");
155
+ if (!searchPaths.includes(cwdNodeModules)) {
156
+ searchPaths.push(cwdNodeModules);
157
+ }
152
158
  searchPathsCache[context] = searchPaths;
153
159
  }
154
160
  for (const nodeModulesDir of searchPaths) {
155
161
  const nodeModulesPath = path.join(nodeModulesDir, id);
156
162
  // Check package.json first
157
163
  const pkgJsonPath = path.join(nodeModulesPath, "package.json");
158
- if (existsSync(pkgJsonPath)) {
164
+ let pkg;
165
+ try {
166
+ const content = fs.readFileSync(pkgJsonPath, "utf8");
167
+ pkg = JSON.parse(content);
168
+ pkgJsonCache[pkgJsonPath] = pkg;
169
+ }
170
+ catch (e) {
171
+ // ignore
172
+ }
173
+ if (pkg) {
159
174
  try {
160
- let pkg;
161
- if (pkgJsonCache[pkgJsonPath]) {
162
- pkg = pkgJsonCache[pkgJsonPath];
163
- }
164
- else {
165
- pkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf8"));
166
- pkgJsonCache[pkgJsonPath] = pkg;
167
- }
168
175
  const mainField = pkg.main;
169
176
  if (mainField) {
170
177
  const candidates = [
@@ -174,36 +181,36 @@ const loadModule = (id, context, importMaps, entrypoint) => {
174
181
  path.resolve(nodeModulesPath, mainField, "index.js"),
175
182
  ];
176
183
  for (const candidate of candidates) {
177
- if (existsSync(candidate) && !statSync(candidate).isDirectory()) {
178
- try {
179
- resolvedId = candidate;
180
- moduleCode = fs.readFileSync(candidate, "utf8");
181
- moduleId = candidate;
182
- break;
183
- }
184
- catch (e) {
185
- // ignore
186
- }
184
+ try {
185
+ // Try reading directly to bypass potential statSync issues
186
+ moduleCode = fs.readFileSync(candidate, "utf8");
187
+ resolvedId = candidate;
188
+ moduleId = candidate;
189
+ break;
190
+ }
191
+ catch (e) {
192
+ // ignore
187
193
  }
188
194
  }
189
195
  }
190
196
  }
191
- catch (_d) { }
197
+ catch (e) {
198
+ // ignore
199
+ }
192
200
  }
193
201
  if (!moduleCode) {
194
202
  const extensions = ["", ".js", ".json", "/index.js"];
195
203
  for (const ext of extensions) {
196
204
  const p = nodeModulesPath + ext;
197
- if (existsSync(p) && !statSync(p).isDirectory()) {
198
- try {
199
- resolvedId = p;
200
- moduleCode = fs.readFileSync(p, "utf8");
201
- moduleId = p;
202
- break;
203
- }
204
- catch (e) {
205
- // ignore
206
- }
205
+ try {
206
+ // Try reading directly
207
+ moduleCode = fs.readFileSync(p, "utf8");
208
+ resolvedId = p;
209
+ moduleId = p;
210
+ break;
211
+ }
212
+ catch (e) {
213
+ // ignore
207
214
  }
208
215
  }
209
216
  }
@@ -211,29 +218,21 @@ const loadModule = (id, context, importMaps, entrypoint) => {
211
218
  break;
212
219
  }
213
220
  }
214
- // Fallback: Try resolving absolute path (handling CWD stripping)
221
+ // Fallback: Try resolving absolute path
215
222
  if (!moduleCode && id.startsWith("/")) {
216
- // @ts-ignore
217
- const cwd = ((_b = (_a = self.process) === null || _a === void 0 ? void 0 : _a.cwd) === null || _b === void 0 ? void 0 : _b.call(_a)) || ((_c = self.workerData) === null || _c === void 0 ? void 0 : _c.cwd) || "/";
218
- let relativeId = id;
219
- if (id.startsWith(cwd)) {
220
- relativeId = id.slice(cwd.length);
221
- if (relativeId.startsWith("/"))
222
- relativeId = relativeId.slice(1);
223
- }
224
223
  const extensions = ["", ".js", ".json", "/index.js"];
225
224
  for (const ext of extensions) {
226
- const p = relativeId + ext;
227
- if (existsSync(p) && !statSync(p).isDirectory()) {
228
- try {
229
- resolvedId = p; // Use relative path for FS ops
230
- moduleCode = fs.readFileSync(p, "utf8");
231
- moduleId = id; // Keep original absolute path as module ID
232
- break;
233
- }
234
- catch (e) {
235
- // ignore
236
- }
225
+ if (ext === "/index.js" && id.endsWith(".js"))
226
+ continue;
227
+ const p = id + ext;
228
+ try {
229
+ moduleCode = fs.readFileSync(p, "utf8");
230
+ resolvedId = p;
231
+ moduleId = id;
232
+ break;
233
+ }
234
+ catch (e) {
235
+ // ignore
237
236
  }
238
237
  }
239
238
  }
@@ -241,18 +240,17 @@ const loadModule = (id, context, importMaps, entrypoint) => {
241
240
  // Try extensions
242
241
  const extensions = ["", ".js", ".json", "/index.js"];
243
242
  for (const ext of extensions) {
243
+ if (ext === "/index.js" && id.endsWith(".js"))
244
+ continue;
244
245
  const p = resolvedId + ext;
245
- if (existsSync(p) && !statSync(p).isDirectory()) {
246
- try {
247
- resolvedId = p;
248
- moduleCode = fs.readFileSync(p, "utf8");
249
- moduleId = p;
250
- break;
251
- }
252
- catch (e) {
253
- console.error(`[Debug] Failed to read file ${p}:`, e);
254
- // ignore
255
- }
246
+ try {
247
+ moduleCode = fs.readFileSync(p, "utf8");
248
+ resolvedId = p;
249
+ moduleId = p;
250
+ break;
251
+ }
252
+ catch (e) {
253
+ // ignore
256
254
  }
257
255
  }
258
256
  }
@@ -265,6 +263,15 @@ const loadModule = (id, context, importMaps, entrypoint) => {
265
263
  return {};
266
264
  };
267
265
  export async function cjs(entrypoint, importMaps) {
266
+ // Clear caches to avoid stale data across runs
267
+ for (const key in statCache)
268
+ delete statCache[key];
269
+ for (const key in pkgJsonCache)
270
+ delete pkgJsonCache[key];
271
+ for (const key in resolutionCache)
272
+ delete resolutionCache[key];
273
+ for (const key in searchPathsCache)
274
+ delete searchPathsCache[key];
268
275
  await Promise.all(Object.entries(importMaps).map(async ([k, v]) => {
269
276
  if (v.startsWith("https://")) {
270
277
  try {
@@ -0,0 +1,2 @@
1
+ import { Binding } from "../type";
2
+ export declare const runLoaderWorkerPool: (binding: Binding, projectCwd: string, loaderWorkerUrl: string, loadersImportMap?: Record<string, string>) => Promise<void>;
@@ -1,8 +1,8 @@
1
- import * as sabcom from "./sabcom";
2
- import initWasm, { registerWorkerScheduler, workerCreated, } from "./utoo";
1
+ import * as sabcom from "../sabcom";
2
+ import initWasm, { Project as ProjectInternal, registerWorkerScheduler, workerCreated, } from "../utoo";
3
3
  let nextWorkerId = 0;
4
4
  const loaderWorkers = {};
5
- export const runLoaderWorkerPool = async (binding, projectCwd, projectInternal, loaderWorkerUrl, loadersImportMap) => {
5
+ export const runLoaderWorkerPool = async (binding, projectCwd, loaderWorkerUrl, loadersImportMap) => {
6
6
  registerWorkerScheduler(async (creation) => {
7
7
  const { options: { filename, cwd }, } = creation;
8
8
  nextWorkerId += 1;
@@ -13,15 +13,15 @@ export const runLoaderWorkerPool = async (binding, projectCwd, projectInternal,
13
13
  worker.onmessage = async (event) => {
14
14
  if (event.data === "sab_request") {
15
15
  await sabcom.handleSabRequest(sabHost, {
16
- read: (path) => projectInternal.read(path),
17
- readDir: (path) => projectInternal.readDir(path),
18
- writeString: (path, content) => projectInternal.writeString(path, content),
19
- createDirAll: (path) => projectInternal.createDirAll(path),
20
- createDir: (path) => projectInternal.createDir(path),
21
- metadata: (path) => projectInternal.metadata(path),
22
- removeFile: (path) => projectInternal.removeFile(path),
23
- removeDir: (path, recursive) => projectInternal.removeDir(path, recursive),
24
- copyFile: (src, dst) => projectInternal.copyFile(src, dst),
16
+ read: (path) => ProjectInternal.read(path),
17
+ readDir: (path) => ProjectInternal.readDir(path),
18
+ writeString: (path, content) => ProjectInternal.writeString(path, content),
19
+ createDirAll: (path) => ProjectInternal.createDirAll(path),
20
+ createDir: (path) => ProjectInternal.createDir(path),
21
+ metadata: (path) => ProjectInternal.metadata(path),
22
+ removeFile: (path) => ProjectInternal.removeFile(path),
23
+ removeDir: (path, recursive) => ProjectInternal.removeDir(path, recursive),
24
+ copyFile: (src, dst) => ProjectInternal.copyFile(src, dst),
25
25
  });
26
26
  }
27
27
  };
@@ -29,15 +29,26 @@ export const runLoaderWorkerPool = async (binding, projectCwd, projectInternal,
29
29
  let finalFilename = filename;
30
30
  if (projectCwd) {
31
31
  const sep = "/";
32
- const pCwd = projectCwd.endsWith(sep)
32
+ let pCwd = projectCwd.endsWith(sep)
33
33
  ? projectCwd.slice(0, -1)
34
34
  : projectCwd;
35
- let cCwd = cwd.startsWith(sep) ? cwd.slice(1) : cwd;
36
- if (cCwd === "." || cCwd === "./") {
37
- cCwd = "";
35
+ if (!pCwd.startsWith(sep)) {
36
+ pCwd = sep + pCwd;
38
37
  }
39
- finalCwd = cCwd ? `${pCwd}${sep}${cCwd}` : pCwd;
40
- if (!filename.startsWith("/")) {
38
+ if (cwd.startsWith(sep)) {
39
+ finalCwd = cwd;
40
+ }
41
+ else {
42
+ let cCwd = cwd;
43
+ if (cCwd === "." || cCwd === "./") {
44
+ cCwd = "";
45
+ }
46
+ finalCwd = cCwd ? `${pCwd}${sep}${cCwd}` : pCwd;
47
+ }
48
+ if (filename.startsWith(sep)) {
49
+ finalFilename = filename;
50
+ }
51
+ else {
41
52
  let fName = filename;
42
53
  if (fName.startsWith("./"))
43
54
  fName = fName.slice(2);
@@ -51,6 +62,7 @@ export const runLoaderWorkerPool = async (binding, projectCwd, projectInternal,
51
62
  {
52
63
  workerData: {
53
64
  cwd: finalCwd,
65
+ projectRoot: projectCwd,
54
66
  workerId: workerId,
55
67
  },
56
68
  loaderAssets: {
@@ -0,0 +1,78 @@
1
+ import { Buffer } from "buffer";
2
+ import { Stats } from "../../type";
3
+ import { promises } from "./fsPromisesPolyfill";
4
+ export declare function readFileSync(path: string, options: any): string | Buffer<any>;
5
+ export declare function readdirSync(path: string, options?: any): any;
6
+ export declare function writeFileSync(path: string, data: string | Uint8Array, options?: any): void;
7
+ export declare function mkdirSync(path: string, options?: any): void;
8
+ export declare function rmSync(path: string, options?: any): void;
9
+ export declare function rmdirSync(path: string, options?: any): void;
10
+ export declare function copyFileSync(src: string, dst: string): void;
11
+ export declare function statSync(p: string): Stats;
12
+ export declare function lstatSync(p: string): Stats;
13
+ export declare function realpathSync(p: string): string;
14
+ export declare function accessSync(path: string, mode?: number): void;
15
+ export declare function existsSync(path: string): boolean;
16
+ export declare function readFile(path: string, options: any, cb: Function): void;
17
+ export declare function readdir(path: string, options: any, cb: Function): void;
18
+ export declare function writeFile(path: string, data: string | Uint8Array, options: any, cb: Function): void;
19
+ export declare function mkdir(path: string, options: any, cb: Function): void;
20
+ export declare function rm(path: string, options: any, cb: Function): void;
21
+ export declare function rmdir(path: string, options: any, cb: Function): void;
22
+ export declare function copyFile(src: string, dst: string, cb: Function): void;
23
+ export declare function stat(p: string, cb: Function): void;
24
+ export declare function lstat(p: string, cb: Function): void;
25
+ export declare function realpath(p: string, cb: Function): void;
26
+ export declare function access(p: string, mode: number | Function, cb?: Function): void;
27
+ export declare const constants: {
28
+ F_OK: number;
29
+ R_OK: number;
30
+ W_OK: number;
31
+ X_OK: number;
32
+ };
33
+ declare const _default: {
34
+ readFile: typeof readFile;
35
+ readFileSync: typeof readFileSync;
36
+ readdir: typeof readdir;
37
+ readdirSync: typeof readdirSync;
38
+ writeFile: typeof writeFile;
39
+ writeFileSync: typeof writeFileSync;
40
+ mkdir: typeof mkdir;
41
+ mkdirSync: typeof mkdirSync;
42
+ rm: typeof rm;
43
+ rmSync: typeof rmSync;
44
+ rmdir: typeof rmdir;
45
+ rmdirSync: typeof rmdirSync;
46
+ copyFile: typeof copyFile;
47
+ copyFileSync: typeof copyFileSync;
48
+ stat: typeof stat;
49
+ statSync: typeof statSync;
50
+ lstat: typeof lstat;
51
+ lstatSync: typeof lstatSync;
52
+ realpath: typeof realpath;
53
+ realpathSync: typeof realpathSync;
54
+ access: typeof access;
55
+ accessSync: typeof accessSync;
56
+ existsSync: typeof existsSync;
57
+ promises: {
58
+ readFile: (p: string, options?: any) => Promise<any>;
59
+ writeFile: (p: string, data: string | Uint8Array, options?: any) => Promise<void>;
60
+ readdir: (p: string, options?: any) => Promise<any>;
61
+ mkdir: (p: string, options?: any) => Promise<void>;
62
+ rm: (p: string, options?: any) => Promise<void>;
63
+ rmdir: (p: string, options?: any) => Promise<any>;
64
+ copyFile: (src: string, dst: string) => Promise<any>;
65
+ stat: (p: string) => Promise<Stats>;
66
+ lstat: (p: string) => Promise<Stats>;
67
+ realpath: (p: string) => Promise<string>;
68
+ access: (p: string, mode?: number) => Promise<void>;
69
+ };
70
+ constants: {
71
+ F_OK: number;
72
+ R_OK: number;
73
+ W_OK: number;
74
+ X_OK: number;
75
+ };
76
+ };
77
+ export default _default;
78
+ export { promises };
@@ -0,0 +1,279 @@
1
+ import { Buffer } from "buffer";
2
+ import path from "path";
3
+ import * as sabcom from "../../sabcom";
4
+ import { Stats } from "../../type";
5
+ import { promises } from "./fsPromisesPolyfill";
6
+ function resolvePath(p) {
7
+ var _a, _b, _c;
8
+ // @ts-ignore
9
+ const cwd = ((_b = (_a = self.process) === null || _a === void 0 ? void 0 : _a.cwd) === null || _b === void 0 ? void 0 : _b.call(_a)) || ((_c = self.workerData) === null || _c === void 0 ? void 0 : _c.cwd) || "/";
10
+ return path.resolve(cwd, p);
11
+ }
12
+ function getSabClient() {
13
+ // @ts-ignore
14
+ const client = self.workerData.sabClient;
15
+ if (!client) {
16
+ throw new Error("Sync fs not supported (no sabClient)");
17
+ }
18
+ return client;
19
+ }
20
+ function getFs() {
21
+ // @ts-ignore
22
+ const fs = self.workerData.fs;
23
+ if (!fs) {
24
+ throw new Error("FS not initialized");
25
+ }
26
+ return fs;
27
+ }
28
+ // --- Synchronous API (via sabcom) ---
29
+ export function readFileSync(path, options) {
30
+ const client = getSabClient();
31
+ const result = client.call(sabcom.SAB_OP_READ_FILE, resolvePath(path));
32
+ if (options === "utf8" ||
33
+ options === "utf-8" ||
34
+ (options && (options.encoding === "utf8" || options.encoding === "utf-8"))) {
35
+ return new TextDecoder().decode(result);
36
+ }
37
+ return Buffer.from(result);
38
+ }
39
+ export function readdirSync(path, options) {
40
+ const client = getSabClient();
41
+ const result = client.call(sabcom.SAB_OP_READ_DIR, resolvePath(path));
42
+ const json = new TextDecoder().decode(result);
43
+ const entries = JSON.parse(json);
44
+ if (options === null || options === void 0 ? void 0 : options.withFileTypes) {
45
+ return entries.map((e) => ({
46
+ name: e.name,
47
+ isFile: () => e.type === "file",
48
+ isDirectory: () => e.type === "directory",
49
+ isSymbolicLink: () => false,
50
+ }));
51
+ }
52
+ return entries.map((e) => e.name);
53
+ }
54
+ export function writeFileSync(path, data, options) {
55
+ const client = getSabClient();
56
+ // TODO: handle binary data properly
57
+ const content = typeof data === "string" ? data : new TextDecoder().decode(data);
58
+ const payload = JSON.stringify({ path: resolvePath(path), data: content });
59
+ client.call(sabcom.SAB_OP_WRITE_FILE, payload);
60
+ }
61
+ export function mkdirSync(path, options) {
62
+ const client = getSabClient();
63
+ const recursive = (options === null || options === void 0 ? void 0 : options.recursive) || false;
64
+ const payload = JSON.stringify({ path: resolvePath(path), recursive });
65
+ client.call(sabcom.SAB_OP_MKDIR, payload);
66
+ }
67
+ export function rmSync(path, options) {
68
+ const client = getSabClient();
69
+ const recursive = (options === null || options === void 0 ? void 0 : options.recursive) || false;
70
+ const payload = JSON.stringify({ path: resolvePath(path), recursive });
71
+ client.call(sabcom.SAB_OP_RM, payload);
72
+ }
73
+ export function rmdirSync(path, options) {
74
+ const client = getSabClient();
75
+ const recursive = (options === null || options === void 0 ? void 0 : options.recursive) || false;
76
+ const payload = JSON.stringify({ path: resolvePath(path), recursive });
77
+ client.call(sabcom.SAB_OP_RMDIR, payload);
78
+ }
79
+ export function copyFileSync(src, dst) {
80
+ const client = getSabClient();
81
+ const payload = JSON.stringify({
82
+ src: resolvePath(src),
83
+ dst: resolvePath(dst),
84
+ });
85
+ client.call(sabcom.SAB_OP_COPY_FILE, payload);
86
+ }
87
+ export function statSync(p) {
88
+ const client = getSabClient();
89
+ const struct = client.callStat(resolvePath(p));
90
+ return new Stats({
91
+ type: struct.type === sabcom.STAT_TYPE_DIR ? "directory" : "file",
92
+ size: Number(struct.size),
93
+ atimeMs: struct.atimeMs,
94
+ mtimeMs: struct.mtimeMs,
95
+ ctimeMs: struct.ctimeMs,
96
+ birthtimeMs: struct.birthtimeMs,
97
+ });
98
+ }
99
+ export function lstatSync(p) {
100
+ return statSync(p);
101
+ }
102
+ export function realpathSync(p) {
103
+ return p;
104
+ }
105
+ export function accessSync(path, mode) {
106
+ statSync(path);
107
+ }
108
+ export function existsSync(path) {
109
+ try {
110
+ statSync(path);
111
+ return true;
112
+ }
113
+ catch (e) {
114
+ return false;
115
+ }
116
+ }
117
+ // --- Asynchronous API (via WASM Project) ---
118
+ export function readFile(path, options, cb) {
119
+ if (typeof options === "function") {
120
+ cb = options;
121
+ options = {};
122
+ }
123
+ const encoding = options === "utf8" ||
124
+ options === "utf-8" ||
125
+ (options === null || options === void 0 ? void 0 : options.encoding) === "utf8" ||
126
+ (options === null || options === void 0 ? void 0 : options.encoding) === "utf-8"
127
+ ? "utf8"
128
+ : undefined;
129
+ const p = resolvePath(path);
130
+ const fs = getFs();
131
+ const promise = encoding ? fs.readToString(p) : fs.read(p);
132
+ promise
133
+ .then((data) => {
134
+ cb(null, encoding ? data : Buffer.from(data));
135
+ })
136
+ .catch((e) => cb(e));
137
+ }
138
+ export function readdir(path, options, cb) {
139
+ if (typeof options === "function") {
140
+ cb = options;
141
+ options = {};
142
+ }
143
+ getFs()
144
+ .readDir(resolvePath(path))
145
+ .then((entries) => {
146
+ const result = entries.map((e) => {
147
+ const json = e.toJSON();
148
+ if (options === null || options === void 0 ? void 0 : options.withFileTypes) {
149
+ return {
150
+ name: json.name,
151
+ isFile: () => json.type === "file",
152
+ isDirectory: () => json.type === "directory",
153
+ isSymbolicLink: () => false,
154
+ };
155
+ }
156
+ return json.name;
157
+ });
158
+ cb(null, result);
159
+ })
160
+ .catch((e) => cb(e));
161
+ }
162
+ export function writeFile(path, data, options, cb) {
163
+ if (typeof options === "function") {
164
+ cb = options;
165
+ options = {};
166
+ }
167
+ const p = resolvePath(path);
168
+ const fs = getFs();
169
+ const promise = typeof data === "string" ? fs.writeString(p, data) : fs.write(p, data);
170
+ promise.then(() => cb(null)).catch((e) => cb(e));
171
+ }
172
+ export function mkdir(path, options, cb) {
173
+ if (typeof options === "function") {
174
+ cb = options;
175
+ options = {};
176
+ }
177
+ const p = resolvePath(path);
178
+ const fs = getFs();
179
+ const promise = (options === null || options === void 0 ? void 0 : options.recursive) ? fs.createDirAll(p) : fs.createDir(p);
180
+ promise.then(() => cb(null)).catch((e) => cb(e));
181
+ }
182
+ export function rm(path, options, cb) {
183
+ if (typeof options === "function") {
184
+ cb = options;
185
+ options = {};
186
+ }
187
+ const p = resolvePath(path);
188
+ const fs = getFs();
189
+ fs.metadata(p)
190
+ .then((metadata) => {
191
+ const type = metadata.toJSON().type;
192
+ if (type === "file") {
193
+ return fs.removeFile(p);
194
+ }
195
+ else {
196
+ return fs.removeDir(p, !!(options === null || options === void 0 ? void 0 : options.recursive));
197
+ }
198
+ })
199
+ .then(() => cb(null))
200
+ .catch((e) => cb(e));
201
+ }
202
+ export function rmdir(path, options, cb) {
203
+ if (typeof options === "function") {
204
+ cb = options;
205
+ options = {};
206
+ }
207
+ getFs()
208
+ .removeDir(resolvePath(path), !!(options === null || options === void 0 ? void 0 : options.recursive))
209
+ .then(() => cb(null))
210
+ .catch((e) => cb(e));
211
+ }
212
+ export function copyFile(src, dst, cb) {
213
+ getFs()
214
+ .copyFile(resolvePath(src), resolvePath(dst))
215
+ .then(() => cb(null))
216
+ .catch((e) => cb(e));
217
+ }
218
+ export function stat(p, cb) {
219
+ getFs()
220
+ .metadata(resolvePath(p))
221
+ .then((metadata) => {
222
+ const json = metadata.toJSON();
223
+ cb(null, new Stats({
224
+ type: json.type,
225
+ size: Number(json.file_size || 0),
226
+ }));
227
+ })
228
+ .catch((e) => cb(e));
229
+ }
230
+ export function lstat(p, cb) {
231
+ stat(p, cb);
232
+ }
233
+ export function realpath(p, cb) {
234
+ cb(null, p);
235
+ }
236
+ export function access(p, mode, cb) {
237
+ if (typeof mode === "function") {
238
+ cb = mode;
239
+ mode = 0;
240
+ }
241
+ stat(p, (err) => {
242
+ if (cb)
243
+ cb(err);
244
+ });
245
+ }
246
+ export const constants = {
247
+ F_OK: 0,
248
+ R_OK: 4,
249
+ W_OK: 2,
250
+ X_OK: 1,
251
+ };
252
+ export default {
253
+ readFile,
254
+ readFileSync,
255
+ readdir,
256
+ readdirSync,
257
+ writeFile,
258
+ writeFileSync,
259
+ mkdir,
260
+ mkdirSync,
261
+ rm,
262
+ rmSync,
263
+ rmdir,
264
+ rmdirSync,
265
+ copyFile,
266
+ copyFileSync,
267
+ stat,
268
+ statSync,
269
+ lstat,
270
+ lstatSync,
271
+ realpath,
272
+ realpathSync,
273
+ access,
274
+ accessSync,
275
+ existsSync,
276
+ promises,
277
+ constants,
278
+ };
279
+ export { promises };
@@ -0,0 +1,26 @@
1
+ import { Stats } from "../../type";
2
+ export declare const promises: {
3
+ readFile: (p: string, options?: any) => Promise<any>;
4
+ writeFile: (p: string, data: string | Uint8Array, options?: any) => Promise<void>;
5
+ readdir: (p: string, options?: any) => Promise<any>;
6
+ mkdir: (p: string, options?: any) => Promise<void>;
7
+ rm: (p: string, options?: any) => Promise<void>;
8
+ rmdir: (p: string, options?: any) => Promise<any>;
9
+ copyFile: (src: string, dst: string) => Promise<any>;
10
+ stat: (p: string) => Promise<Stats>;
11
+ lstat: (p: string) => Promise<Stats>;
12
+ realpath: (p: string) => Promise<string>;
13
+ access: (p: string, mode?: number) => Promise<void>;
14
+ };
15
+ export declare const readFile: (p: string, options?: any) => Promise<any>;
16
+ export declare const writeFile: (p: string, data: string | Uint8Array, options?: any) => Promise<void>;
17
+ export declare const readdir: (p: string, options?: any) => Promise<any>;
18
+ export declare const mkdir: (p: string, options?: any) => Promise<void>;
19
+ export declare const rm: (p: string, options?: any) => Promise<void>;
20
+ export declare const rmdir: (p: string, options?: any) => Promise<any>;
21
+ export declare const copyFile: (src: string, dst: string) => Promise<any>;
22
+ export declare const stat: (p: string) => Promise<Stats>;
23
+ export declare const lstat: (p: string) => Promise<Stats>;
24
+ export declare const realpath: (p: string) => Promise<string>;
25
+ export declare const access: (p: string, mode?: number) => Promise<void>;
26
+ export default promises;