Skip to content

Commit

Permalink
Fix TS Beekeeper not able to parse absolute directories in Node env
Browse files Browse the repository at this point in the history
  • Loading branch information
mtyszczak committed Jan 31, 2025
1 parent 4a9cfbd commit dc3c841
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions programs/beekeeper/beekeeper_wasm/src/detailed/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,32 @@ export class BeekeeperFileSystem {
});
}

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

let analysis = safeWasmCall(() => this.fs.analyzePath(dir), `analyzing path: '${dir}'`);

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

safeWasmCall(() => this.fs.mkdir(dir), `directory creation: '${dir}'`);
}
}

public async init(walletDir: string): Promise<void> {
if(this.isWebEnvironment && !walletDir.startsWith("/"))
const isAbsolutePath = walletDir.startsWith("/");

if(this.isWebEnvironment && !isAbsolutePath)
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);
this.ensureCreateDir(walletDirPathParts, isAbsolutePath);

if(this.isWebEnvironment)
safeWasmCall(() => this.fs.mount(this.fs.filesystems.IDBFS, {}, walletDir), "mounting IDBFS");
Expand Down

0 comments on commit dc3c841

Please sign in to comment.