diff --git a/packages/utoo-web/src/webpackLoaders/polyfills/fsPolyfill.ts b/packages/utoo-web/src/webpackLoaders/polyfills/fsPolyfill.ts deleted file mode 100644 index e21d8141c..000000000 --- a/packages/utoo-web/src/webpackLoaders/polyfills/fsPolyfill.ts +++ /dev/null @@ -1,341 +0,0 @@ -import { Buffer } from "buffer"; -import path from "path"; -import { Stats } from "../../types"; -import { promises } from "./fsPromisesPolyfill"; -import { workerData } from "./workerThreadsPolyfill"; - -function resolvePath(p: string): string { - // @ts-ignore - const cwd = self.process?.cwd?.() || workerData?.cwd || "/"; - return path.resolve(cwd, p); -} - -function getFs() { - // @ts-ignore - const fs = workerData.fs; - if (!fs) { - throw new Error("FS not initialized"); - } - return fs; -} - -// --- Synchronous API (via WASM Project Sync APIs) --- - -const textDecoder = new TextDecoder(); - -export function readFileSync(path: string, options: any) { - const fs = getFs(); - const result = fs.readSync(resolvePath(path)); - - if ( - options === "utf8" || - options === "utf-8" || - (options && (options.encoding === "utf8" || options.encoding === "utf-8")) - ) { - return textDecoder.decode(result); - } - return Buffer.from(result); -} - -export function readdirSync(path: string, options?: any) { - const fs = getFs(); - const entries = fs.readDirSync(resolvePath(path)); - - if (options?.withFileTypes) { - return entries.map((e: any) => ({ - name: e.name, - isFile: () => e.type === "file", - isDirectory: () => e.type === "directory", - isSymbolicLink: () => false, - })); - } - return entries.map((e: any) => e.name); -} - -export function writeFileSync( - path: string, - data: string | Uint8Array, - options?: any, -) { - const fs = getFs(); - let content: Uint8Array; - if (typeof data === "string") { - content = new TextEncoder().encode(data); - } else { - content = data; - } - fs.writeSync(resolvePath(path), content); -} - -export function mkdirSync(path: string, options?: any) { - const fs = getFs(); - const recursive = options?.recursive || false; - if (recursive) { - fs.createDirAllSync(resolvePath(path)); - } else { - fs.createDirSync(resolvePath(path)); - } -} - -export function rmSync(path: string, options?: any) { - const fs = getFs(); - const recursive = options?.recursive || false; - if (recursive) { - fs.removeDirSync(resolvePath(path), true); - } else { - fs.removeFileSync(resolvePath(path)); - } -} - -export function rmdirSync(path: string, options?: any) { - const fs = getFs(); - const recursive = options?.recursive || false; - fs.removeDirSync(resolvePath(path), recursive); -} - -export function copyFileSync(src: string, dst: string) { - const fs = getFs(); - fs.copyFileSync(resolvePath(src), resolvePath(dst)); -} - -export function statSync(p: string) { - const fs = getFs(); - const metadata = fs.metadataSync(resolvePath(p)); - - let type, size; - // @ts-ignore - if (typeof metadata.toJSON === "function") { - // @ts-ignore - const json = metadata.toJSON(); - type = json.type; - size = json.file_size; - } else { - // @ts-ignore - type = metadata.type; - // @ts-ignore - size = metadata.file_size; - } - - return new Stats({ - type: type === "directory" ? "directory" : "file", - size: Number(size || 0), - atimeMs: Date.now(), - mtimeMs: Date.now(), - ctimeMs: Date.now(), - birthtimeMs: Date.now(), - }); -} - -export function lstatSync(p: string) { - return statSync(p); -} - -export function realpathSync(p: string) { - return p; -} - -export function accessSync(path: string, mode?: number) { - statSync(path); -} - -export function existsSync(path: string) { - try { - statSync(path); - return true; - } catch (e) { - return false; - } -} - -// --- Asynchronous API (via WASM Project) --- - -export function readFile(path: string, options: any, cb: Function) { - if (typeof options === "function") { - cb = options; - options = {}; - } - const encoding = - options === "utf8" || - options === "utf-8" || - options?.encoding === "utf8" || - options?.encoding === "utf-8" - ? "utf8" - : undefined; - - const p = resolvePath(path); - const fs = getFs(); - - const promise = encoding ? fs.readToString(p) : fs.read(p); - - promise - .then((data: any) => { - cb(null, encoding ? data : Buffer.from(data)); - }) - .catch((e: any) => cb(e)); -} - -export function readdir(path: string, options: any, cb: Function) { - if (typeof options === "function") { - cb = options; - options = {}; - } - getFs() - .readDir(resolvePath(path)) - .then((entries: any[]) => { - const result = entries.map((e: any) => { - const json = e.toJSON() as any; - if (options?.withFileTypes) { - return { - name: json.name, - isFile: () => json.type === "file", - isDirectory: () => json.type === "directory", - isSymbolicLink: () => false, - }; - } - return json.name; - }); - cb(null, result); - }) - .catch((e: any) => cb(e)); -} - -export function writeFile( - path: string, - data: string | Uint8Array, - options: any, - cb: Function, -) { - if (typeof options === "function") { - cb = options; - options = {}; - } - const p = resolvePath(path); - const fs = getFs(); - - const promise = - typeof data === "string" ? fs.writeString(p, data) : fs.write(p, data); - - promise.then(() => cb(null)).catch((e: any) => cb(e)); -} - -export function mkdir(path: string, options: any, cb: Function) { - if (typeof options === "function") { - cb = options; - options = {}; - } - const p = resolvePath(path); - const fs = getFs(); - const promise = options?.recursive ? fs.createDirAll(p) : fs.createDir(p); - - promise.then(() => cb(null)).catch((e: any) => cb(e)); -} - -export function rm(path: string, options: any, cb: Function) { - if (typeof options === "function") { - cb = options; - options = {}; - } - const p = resolvePath(path); - const fs = getFs(); - - fs.metadata(p) - .then((metadata: any) => { - const type = (metadata.toJSON() as any).type; - if (type === "file") { - return fs.removeFile(p); - } else { - return fs.removeDir(p, !!options?.recursive); - } - }) - .then(() => cb(null)) - .catch((e: any) => cb(e)); -} - -export function rmdir(path: string, options: any, cb: Function) { - if (typeof options === "function") { - cb = options; - options = {}; - } - getFs() - .removeDir(resolvePath(path), !!options?.recursive) - .then(() => cb(null)) - .catch((e: any) => cb(e)); -} - -export function copyFile(src: string, dst: string, cb: Function) { - getFs() - .copyFile(resolvePath(src), resolvePath(dst)) - .then(() => cb(null)) - .catch((e: any) => cb(e)); -} - -export function stat(p: string, cb: Function) { - getFs() - .metadata(resolvePath(p)) - .then((metadata: any) => { - const json = metadata.toJSON() as any; - cb( - null, - new Stats({ - type: json.type, - size: Number(json.file_size || 0), - }), - ); - }) - .catch((e: any) => cb(e)); -} - -export function lstat(p: string, cb: Function) { - stat(p, cb); -} - -export function realpath(p: string, cb: Function) { - cb(null, p); -} - -export function access(p: string, mode: number | Function, cb?: Function) { - if (typeof mode === "function") { - cb = mode; - mode = 0; - } - stat(p, (err: any) => { - if (cb) cb(err); - }); -} - -export const constants = { - F_OK: 0, - R_OK: 4, - W_OK: 2, - X_OK: 1, -}; - -export default { - readFile, - readFileSync, - readdir, - readdirSync, - writeFile, - writeFileSync, - mkdir, - mkdirSync, - rm, - rmSync, - rmdir, - rmdirSync, - copyFile, - copyFileSync, - stat, - statSync, - lstat, - lstatSync, - realpath, - realpathSync, - access, - accessSync, - existsSync, - promises, - constants, -}; - -export { promises }; diff --git a/packages/utoo-web/src/webpackLoaders/polyfills/fsPolyfill/promises.ts b/packages/utoo-web/src/webpackLoaders/polyfills/fsPolyfill/promises.ts new file mode 100644 index 000000000..367598df5 --- /dev/null +++ b/packages/utoo-web/src/webpackLoaders/polyfills/fsPolyfill/promises.ts @@ -0,0 +1,150 @@ +import { Buffer } from "buffer"; +import { Stats } from "../../../types"; +import { getFs, resolvePath, translateError } from "./utils"; + +export const promises = { + readFile: async (p: string, options?: any) => { + const encoding = + options === "utf8" || + options === "utf-8" || + options?.encoding === "utf8" || + options?.encoding === "utf-8" + ? "utf8" + : undefined; + const fs = getFs(); + const resolvedPath = resolvePath(p); + try { + const data = await (encoding + ? fs.readToString(resolvedPath) + : fs.read(resolvedPath)); + return encoding ? data : Buffer.from(data); + } catch (e) { + throw translateError(e, p, "open"); + } + }, + writeFile: async (p: string, data: string | Uint8Array, options?: any) => { + const fs = getFs(); + const resolvedPath = resolvePath(p); + try { + if (typeof data === "string") { + await fs.writeString(resolvedPath, data); + } else { + await fs.write(resolvedPath, data); + } + } catch (e) { + throw translateError(e, p, "open"); + } + }, + readdir: async (p: string, options?: any) => { + const resolvedPath = resolvePath(p); + try { + const entries = await getFs().readDir(resolvedPath); + return entries.map((e: any) => { + const json = e.toJSON() as any; + if (options?.withFileTypes) { + return { + name: json.name, + isFile: () => json.type === "file", + isDirectory: () => json.type === "directory", + isSymbolicLink: () => false, + }; + } + return json.name; + }); + } catch (e) { + throw translateError(e, p, "scandir"); + } + }, + mkdir: async (p: string, options?: any) => { + const fs = getFs(); + const resolvedPath = resolvePath(p); + try { + if (options?.recursive) { + await fs.createDirAll(resolvedPath); + } else { + await fs.createDir(resolvedPath); + } + } catch (e) { + throw translateError(e, p, "mkdir"); + } + }, + rm: async (p: string, options?: any) => { + const fs = getFs(); + const resolvedPath = resolvePath(p); + try { + const metadata = await fs.metadata(resolvedPath); + const type = (metadata.toJSON() as any).type; + if (type === "file") { + await fs.removeFile(resolvedPath); + } else { + await fs.removeDir(resolvedPath, !!options?.recursive); + } + } catch (e) { + throw translateError(e, p, "rm"); + } + }, + rmdir: async (p: string, options?: any) => { + const resolvedPath = resolvePath(p); + try { + return await getFs().removeDir(resolvedPath, !!options?.recursive); + } catch (e) { + throw translateError(e, p, "rmdir"); + } + }, + copyFile: async (src: string, dst: string) => { + try { + return await getFs().copyFile(resolvePath(src), resolvePath(dst)); + } catch (e) { + throw translateError(e, src, "copyfile"); + } + }, + stat: async (p: string) => { + const resolvedPath = resolvePath(p); + try { + const metadata = await getFs().metadata(resolvedPath); + const json = metadata.toJSON() as any; + return new Stats({ + type: json.type, + size: Number(json.file_size || 0), + }); + } catch (e) { + throw translateError(e, p, "stat"); + } + }, + lstat: async (p: string) => { + const resolvedPath = resolvePath(p); + try { + const metadata = await getFs().metadata(resolvedPath); + const json = metadata.toJSON() as any; + return new Stats({ + type: json.type, + size: Number(json.file_size || 0), + }); + } catch (e) { + throw translateError(e, p, "lstat"); + } + }, + realpath: async (p: string) => p, + access: async (p: string, mode?: number) => { + const resolvedPath = resolvePath(p); + try { + await getFs().metadata(resolvedPath); + } catch (e) { + throw translateError(e, p, "access"); + } + }, +}; + +export const readFile = promises.readFile; +export const writeFile = promises.writeFile; +export const readdir = promises.readdir; +export const mkdir = promises.mkdir; +export const rm = promises.rm; +export const rmdir = promises.rmdir; +export const copyFile = promises.copyFile; +export const stat = promises.stat; +export const lstat = promises.lstat; +export const realpath = promises.realpath; +export const access = promises.access; + +export default promises; diff --git a/packages/utoo-web/src/webpackLoaders/polyfills/fsPolyfill/sync.ts b/packages/utoo-web/src/webpackLoaders/polyfills/fsPolyfill/sync.ts new file mode 100644 index 000000000..86093707e --- /dev/null +++ b/packages/utoo-web/src/webpackLoaders/polyfills/fsPolyfill/sync.ts @@ -0,0 +1,426 @@ +import { Buffer } from "buffer"; +import { Stats } from "../../../types"; +import { promises } from "./promises"; +import { getFs, resolvePath, translateError } from "./utils"; + +// --- Synchronous API (via WASM Project Sync APIs) --- + +const textDecoder = new TextDecoder(); + +export function readFileSync(path: string, options: any) { + const fs = getFs(); + const resolvedPath = resolvePath(path); + try { + const result = fs.readSync(resolvedPath); + + if ( + options === "utf8" || + options === "utf-8" || + (options && (options.encoding === "utf8" || options.encoding === "utf-8")) + ) { + return textDecoder.decode(result); + } + return Buffer.from(result); + } catch (e) { + throw translateError(e, path, "open"); + } +} + +export function readdirSync(path: string, options?: any) { + const fs = getFs(); + const resolvedPath = resolvePath(path); + try { + const entries = fs.readDirSync(resolvedPath); + + if (options?.withFileTypes) { + return entries.map((e: any) => ({ + name: e.name, + isFile: () => e.type === "file", + isDirectory: () => e.type === "directory", + isSymbolicLink: () => false, + })); + } + return entries.map((e: any) => e.name); + } catch (e) { + throw translateError(e, path, "scandir"); + } +} + +export function writeFileSync( + path: string, + data: string | Uint8Array, + options?: any, +) { + const fs = getFs(); + const resolvedPath = resolvePath(path); + try { + let content: Uint8Array; + if (typeof data === "string") { + content = new TextEncoder().encode(data); + } else { + content = data; + } + fs.writeSync(resolvedPath, content); + } catch (e) { + throw translateError(e, path, "open"); + } +} + +export function mkdirSync(path: string, options?: any) { + const fs = getFs(); + const resolvedPath = resolvePath(path); + try { + const recursive = options?.recursive || false; + if (recursive) { + fs.createDirAllSync(resolvedPath); + } else { + fs.createDirSync(resolvedPath); + } + } catch (e) { + throw translateError(e, path, "mkdir"); + } +} + +export function rmSync(path: string, options?: any) { + const fs = getFs(); + const resolvedPath = resolvePath(path); + try { + const recursive = options?.recursive || false; + if (recursive) { + fs.removeDirSync(resolvedPath, true); + } else { + fs.removeFileSync(resolvedPath); + } + } catch (e) { + throw translateError(e, path, "rm"); + } +} + +export function rmdirSync(path: string, options?: any) { + const fs = getFs(); + const resolvedPath = resolvePath(path); + try { + const recursive = options?.recursive || false; + fs.removeDirSync(resolvedPath, recursive); + } catch (e) { + throw translateError(e, path, "rmdir"); + } +} + +export function copyFileSync(src: string, dst: string) { + const fs = getFs(); + try { + fs.copyFileSync(resolvePath(src), resolvePath(dst)); + } catch (e) { + throw translateError(e, src, "copyfile"); + } +} + +export function statSync(p: string) { + const fs = getFs(); + const resolvedPath = resolvePath(p); + try { + const metadata = fs.metadataSync(resolvedPath); + + let type, size; + // @ts-ignore + if (typeof metadata.toJSON === "function") { + // @ts-ignore + const json = metadata.toJSON(); + type = json.type; + size = json.file_size; + } else { + // @ts-ignore + type = metadata.type; + // @ts-ignore + size = metadata.file_size; + } + + return new Stats({ + type: type === "directory" ? "directory" : "file", + size: Number(size || 0), + atimeMs: Date.now(), + mtimeMs: Date.now(), + ctimeMs: Date.now(), + birthtimeMs: Date.now(), + }); + } catch (e) { + throw translateError(e, p, "stat"); + } +} + +export function lstatSync(p: string) { + const fs = getFs(); + const resolvedPath = resolvePath(p); + try { + const metadata = fs.metadataSync(resolvedPath); + + let type, size; + // @ts-ignore + if (typeof metadata.toJSON === "function") { + // @ts-ignore + const json = metadata.toJSON(); + type = json.type; + size = json.file_size; + } else { + // @ts-ignore + type = metadata.type; + // @ts-ignore + size = metadata.file_size; + } + + return new Stats({ + type: type === "directory" ? "directory" : "file", + size: Number(size || 0), + atimeMs: Date.now(), + mtimeMs: Date.now(), + ctimeMs: Date.now(), + birthtimeMs: Date.now(), + }); + } catch (e) { + throw translateError(e, p, "lstat"); + } +} + +export function realpathSync(p: string) { + return p; +} + +export function accessSync(p: string, mode?: number) { + const fs = getFs(); + const resolvedPath = resolvePath(p); + try { + fs.metadataSync(resolvedPath); + } catch (e) { + throw translateError(e, p, "access"); + } +} + +export function existsSync(path: string) { + try { + statSync(path); + return true; + } catch (e) { + return false; + } +} + +// --- Asynchronous API (via WASM Project) --- + +export function readFile(path: string, options: any, cb: Function) { + if (typeof options === "function") { + cb = options; + options = {}; + } + const encoding = + options === "utf8" || + options === "utf-8" || + options?.encoding === "utf8" || + options?.encoding === "utf-8" + ? "utf8" + : undefined; + + const resolvedPath = resolvePath(path); + const fs = getFs(); + + const promise = encoding + ? fs.readToString(resolvedPath) + : fs.read(resolvedPath); + + promise + .then((data: any) => { + cb(null, encoding ? data : Buffer.from(data)); + }) + .catch((e: any) => cb(translateError(e, path, "open"))); +} + +export function readdir(path: string, options: any, cb: Function) { + if (typeof options === "function") { + cb = options; + options = {}; + } + getFs() + .readDir(resolvePath(path)) + .then((entries: any[]) => { + const result = entries.map((e: any) => { + const json = e.toJSON() as any; + if (options?.withFileTypes) { + return { + name: json.name, + isFile: () => json.type === "file", + isDirectory: () => json.type === "directory", + isSymbolicLink: () => false, + }; + } + return json.name; + }); + cb(null, result); + }) + .catch((e: any) => cb(translateError(e, path, "scandir"))); +} + +export function writeFile( + path: string, + data: string | Uint8Array, + options: any, + cb: Function, +) { + if (typeof options === "function") { + cb = options; + options = {}; + } + const resolvedPath = resolvePath(path); + const fs = getFs(); + + const promise = + typeof data === "string" + ? fs.writeString(resolvedPath, data) + : fs.write(resolvedPath, data); + + promise + .then(() => cb(null)) + .catch((e: any) => cb(translateError(e, path, "open"))); +} + +export function mkdir(path: string, options: any, cb: Function) { + if (typeof options === "function") { + cb = options; + options = {}; + } + const resolvedPath = resolvePath(path); + const fs = getFs(); + const promise = options?.recursive + ? fs.createDirAll(resolvedPath) + : fs.createDir(resolvedPath); + + promise + .then(() => cb(null)) + .catch((e: any) => cb(translateError(e, path, "mkdir"))); +} + +export function rm(path: string, options: any, cb: Function) { + if (typeof options === "function") { + cb = options; + options = {}; + } + const resolvedPath = resolvePath(path); + const fs = getFs(); + + fs.metadata(resolvedPath) + .then((metadata: any) => { + const type = (metadata.toJSON() as any).type; + if (type === "file") { + return fs.removeFile(resolvedPath); + } else { + return fs.removeDir(resolvedPath, !!options?.recursive); + } + }) + .then(() => cb(null)) + .catch((e: any) => cb(translateError(e, path, "rm"))); +} + +export function rmdir(path: string, options: any, cb: Function) { + if (typeof options === "function") { + cb = options; + options = {}; + } + getFs() + .removeDir(resolvePath(path), !!options?.recursive) + .then(() => cb(null)) + .catch((e: any) => cb(translateError(e, path, "rmdir"))); +} + +export function copyFile(src: string, dst: string, cb: Function) { + getFs() + .copyFile(resolvePath(src), resolvePath(dst)) + .then(() => cb(null)) + .catch((e: any) => cb(translateError(e, src, "copyfile"))); +} + +export function stat(p: string, cb: Function) { + getFs() + .metadata(resolvePath(p)) + .then((metadata: any) => { + const json = metadata.toJSON() as any; + cb( + null, + new Stats({ + type: json.type, + size: Number(json.file_size || 0), + }), + ); + }) + .catch((e: any) => cb(translateError(e, p, "stat"))); +} + +export function lstat(p: string, cb: Function) { + getFs() + .metadata(resolvePath(p)) + .then((metadata: any) => { + const json = metadata.toJSON() as any; + cb( + null, + new Stats({ + type: json.type, + size: Number(json.file_size || 0), + }), + ); + }) + .catch((e: any) => cb(translateError(e, p, "lstat"))); +} + +export function realpath(p: string, cb: Function) { + cb(null, p); +} + +export function access(p: string, mode: number | Function, cb?: Function) { + if (typeof mode === "function") { + cb = mode; + mode = 0; + } + getFs() + .metadata(resolvePath(p)) + .then(() => { + if (cb) cb(null); + }) + .catch((e: any) => { + if (cb) cb(translateError(e, p, "access")); + }); +} + +export const constants = { + F_OK: 0, + R_OK: 4, + W_OK: 2, + X_OK: 1, +}; + +export default { + readFile, + readFileSync, + readdir, + readdirSync, + writeFile, + writeFileSync, + mkdir, + mkdirSync, + rm, + rmSync, + rmdir, + rmdirSync, + copyFile, + copyFileSync, + stat, + statSync, + lstat, + lstatSync, + realpath, + realpathSync, + access, + accessSync, + existsSync, + promises, + constants, +}; + +export { promises }; diff --git a/packages/utoo-web/src/webpackLoaders/polyfills/fsPolyfill/utils.ts b/packages/utoo-web/src/webpackLoaders/polyfills/fsPolyfill/utils.ts new file mode 100644 index 000000000..029c99f31 --- /dev/null +++ b/packages/utoo-web/src/webpackLoaders/polyfills/fsPolyfill/utils.ts @@ -0,0 +1,32 @@ +import path from "path"; +import { workerData } from "../workerThreadsPolyfill"; + +export function resolvePath(p: string): string { + // @ts-ignore + const cwd = self.process?.cwd?.() || workerData?.cwd || "/"; + return path.resolve(cwd, p); +} + +export function getFs() { + // @ts-ignore + const fs = workerData.fs; + if (!fs) { + throw new Error("FS not initialized"); + } + return fs; +} + +export function translateError(error: any, path: string, syscall: string) { + const message = error.message || String(error); + if (message.includes("NotFound")) { + const e = new Error( + `ENOENT: no such file or directory, ${syscall} '${path}'`, + ); + (e as any).errno = -2; + (e as any).code = "ENOENT"; + (e as any).syscall = syscall; + (e as any).path = path; + return e; + } + return error; +} diff --git a/packages/utoo-web/src/webpackLoaders/polyfills/fsPromisesPolyfill.ts b/packages/utoo-web/src/webpackLoaders/polyfills/fsPromisesPolyfill.ts deleted file mode 100644 index d73fe375b..000000000 --- a/packages/utoo-web/src/webpackLoaders/polyfills/fsPromisesPolyfill.ts +++ /dev/null @@ -1,117 +0,0 @@ -import { Buffer } from "buffer"; -import path from "path"; -import { Stats } from "../../types"; -import { workerData } from "./workerThreadsPolyfill"; - -function resolvePath(p: string): string { - // @ts-ignore - const cwd = self.process?.cwd?.() || workerData?.cwd || "/"; - return path.resolve(cwd, p); -} - -function getFs() { - // @ts-ignore - const fs = workerData.fs; - if (!fs) { - throw new Error("FS not initialized"); - } - return fs; -} - -export const promises = { - readFile: async (p: string, options?: any) => { - const encoding = - options === "utf8" || - options === "utf-8" || - options?.encoding === "utf8" || - options?.encoding === "utf-8" - ? "utf8" - : undefined; - const fs = getFs(); - const path = resolvePath(p); - const data = await (encoding ? fs.readToString(path) : fs.read(path)); - return encoding ? data : Buffer.from(data); - }, - writeFile: async (p: string, data: string | Uint8Array, options?: any) => { - const fs = getFs(); - const path = resolvePath(p); - if (typeof data === "string") { - await fs.writeString(path, data); - } else { - await fs.write(path, data); - } - }, - readdir: async (p: string, options?: any) => { - const entries = await getFs().readDir(resolvePath(p)); - return entries.map((e: any) => { - const json = e.toJSON() as any; - if (options?.withFileTypes) { - return { - name: json.name, - isFile: () => json.type === "file", - isDirectory: () => json.type === "directory", - isSymbolicLink: () => false, - }; - } - return json.name; - }); - }, - mkdir: async (p: string, options?: any) => { - const fs = getFs(); - const path = resolvePath(p); - if (options?.recursive) { - await fs.createDirAll(path); - } else { - await fs.createDir(path); - } - }, - rm: async (p: string, options?: any) => { - const fs = getFs(); - const path = resolvePath(p); - const metadata = await fs.metadata(path); - const type = (metadata.toJSON() as any).type; - if (type === "file") { - await fs.removeFile(path); - } else { - await fs.removeDir(path, !!options?.recursive); - } - }, - rmdir: async (p: string, options?: any) => - getFs().removeDir(resolvePath(p), !!options?.recursive), - copyFile: async (src: string, dst: string) => - getFs().copyFile(resolvePath(src), resolvePath(dst)), - stat: async (p: string) => { - const metadata = await getFs().metadata(resolvePath(p)); - const json = metadata.toJSON() as any; - return new Stats({ - type: json.type, - size: Number(json.file_size || 0), - }); - }, - lstat: async (p: string) => { - const metadata = await getFs().metadata(resolvePath(p)); - const json = metadata.toJSON() as any; - return new Stats({ - type: json.type, - size: Number(json.file_size || 0), - }); - }, - realpath: async (p: string) => p, - access: async (p: string, mode?: number) => { - await getFs().metadata(resolvePath(p)); - }, -}; - -export const readFile = promises.readFile; -export const writeFile = promises.writeFile; -export const readdir = promises.readdir; -export const mkdir = promises.mkdir; -export const rm = promises.rm; -export const rmdir = promises.rmdir; -export const copyFile = promises.copyFile; -export const stat = promises.stat; -export const lstat = promises.lstat; -export const realpath = promises.realpath; -export const access = promises.access; - -export default promises; diff --git a/packages/utoo-web/src/webpackLoaders/polyfills/nodePolyFills.ts b/packages/utoo-web/src/webpackLoaders/polyfills/nodePolyFills.ts index d94893924..e6bba3555 100644 --- a/packages/utoo-web/src/webpackLoaders/polyfills/nodePolyFills.ts +++ b/packages/utoo-web/src/webpackLoaders/polyfills/nodePolyFills.ts @@ -1,4 +1,4 @@ -import * as fs from "./fsPolyfill"; +import * as fs from "./fsPolyfill/sync"; import * as workerThreads from "./workerThreadsPolyfill"; const buffer = require("buffer");