Skip to content

Commit

Permalink
Fix directory creation in fs init in beekeeper
Browse files Browse the repository at this point in the history
  • Loading branch information
mtyszczak committed Jan 14, 2025
1 parent 48db589 commit 0d86144
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ globalThis.createBeekeeperTestFor = async function createBeekeeperTestFor (env)

const beekeeper = await import(locBeekeeper);

const bk = await beekeeper.default({ storageRoot: '.beekeeper', enableLogs: false });
const bk = await beekeeper.default({ storageRoot: env === "web" ? "/storage_root" : '.beekeeper', enableLogs: false });

return {
provider: beekeeper,
Expand Down
26 changes: 24 additions & 2 deletions programs/beekeeper/beekeeper_wasm/src/detailed/fs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BeekeeperModule } from "../beekeeper.js";
import { BeekeeperError } from "../errors.js";
import { safeWasmCall } from "../util/wasm_error.js";

export class BeekeeperFileSystem {
Expand All @@ -16,9 +17,30 @@ export class BeekeeperFileSystem {
});
}

private ensureCreateDir(paths: string[]) {
// We need an absolute path in web environment in order to create proper directories
const dir = (process.env.ROLLUP_TARGET_ENV === "web" ? "/" : "") + paths.join("/");

let analysis = safeWasmCall(() => this.fs.analyzePath(dir));

if (!analysis.exists) {
if (paths.length > 1)
this.ensureCreateDir(paths.slice(0, -1));

safeWasmCall(() => this.fs.mkdir(dir));
}
}

public init(walletDir: string): Promise<void> {
if(!safeWasmCall(() => this.fs.analyzePath(walletDir).exists))
safeWasmCall(() => this.fs.mkdir(walletDir));
if(process.env.ROLLUP_TARGET_ENV === "web" && !walletDir.startsWith("/"))
throw new BeekeeperError("Storage root directory must be an absolute path in web environment");

const walletDirPathParts = walletDir.split("/").filter(node => !!node && node !== ".");

if (walletDirPathParts.length === 0)
throw new BeekeeperError("Storage root directory must not be empty");

this.ensureCreateDir(walletDirPathParts);

if(process.env.ROLLUP_TARGET_ENV === "web")
safeWasmCall(() => this.fs.mount(this.fs.filesystems.IDBFS, {}, walletDir));
Expand Down
19 changes: 17 additions & 2 deletions programs/beekeeper/beekeeper_wasm/src/util/wasm_error.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import { BeekeeperError } from "../errors.js";

// TODO: Check for errno on Windows
const stringifyError = (error: unknown): string => {
if (error instanceof Error)
return error.message;

const name = error && typeof error === "object" && "name" in error ? error.name : undefined;

if (name === "ErrnoError" && "errno" in (error as object)) {
const errno = (error as any).errno as number;
return `ErrnoError: #${errno}`;
}

return "Unknown error" + (name ? `: ${name}` : "");
};

export const safeWasmCall = <T extends () => any>(fn: T): ReturnType<T> => {
try {
return fn()
} catch (error) {
throw new BeekeeperError(`Error during Wasm call: ${error instanceof Error ? error.message : error}`);
throw new BeekeeperError(`Error during Wasm call: ${stringifyError(error)}`);
}
};

export const safeAsyncWasmCall = async <T extends () => any>(fn: T): Promise<ReturnType<T>> => {
try {
return await fn();
} catch (error) {
throw new BeekeeperError(`Error during Wasm call: ${error instanceof Error ? error.message : error}`);
throw new BeekeeperError(`Error during Wasm call: ${stringifyError(error)}`);
}
};

0 comments on commit 0d86144

Please sign in to comment.