Skip to content

Commit

Permalink
fs: explicitly truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Mar 8, 2025
1 parent ea4d351 commit b37ef44
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/node/headless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ async function main() {
// TODO: need a hook to wait for sync to complete instead of this
await new Promise((resolve) => setTimeout(resolve, 5000));

// Synchornize the filesystem
await proj.syncFs();
// Sychronize the filesystem
await proj.syncFs({
useProjectName: true,
});
process.exit(0);
} catch (e) {
console.error('FATAL:', e);
Expand Down
1 change: 1 addition & 0 deletions src/services/opfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export async function getFileHandle(
*/
export async function write(handle: FileSystemFileHandle, content: Uint8Array): Promise<void> {
const writable = await handle.createWritable();
await writable.truncate(0);
await writable.write(content);
await writable.close();
}
Expand Down
32 changes: 22 additions & 10 deletions src/services/workspace-proj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ export class WorkspaceProj {
* @param path Path in the project.
*/
public async download(path: string) {
const fsPath = await this.syncFs(path);
const fsPath = await this.syncFs({ path });
if (fsPath.endsWith('/')) {
const handle = await opfs.getDirectoryHandle(fsPath);
await opfs.download(handle, {
Expand Down Expand Up @@ -434,18 +434,20 @@ export class WorkspaceProj {
/**
* Synchronize the project to the OPFS.
*
* @param prefix The prefix to synchronize, relative path in the project.
*
* @returns The path to the project in the OPFS + prefix.
*/
public async syncFs(prefix: string = '/'): Promise<string> {
public async syncFs(opts?: SyncFsOpts): Promise<string> {
let prefix = opts?.path ?? '/';

// Check if this is a single file to sync
if (prefix[prefix.length - 1] !== '/') {
return this.syncFsFile(prefix);
return this.syncFsFile(opts);
}

// Get the folder in the FS
prefix = utils.normalizePath(prefix);
const basedir = `${this.manager.group}/${this.uuid}`;
const basepdir = opts?.useProjectName ? this.name : this.uuid;
const basedir = `${this.manager.group}/${basepdir}`;
const folder = await opfs.getDirectoryHandle(basedir + prefix, { create: true });

// TODO: show progress
Expand Down Expand Up @@ -518,10 +520,11 @@ export class WorkspaceProj {

/**
* Sync a single file in the OPFS.
*
* @param path Path to the file in the project.
*/
private async syncFsFile(path: string): Promise<string> {
private async syncFsFile(opts?: SyncFsOpts): Promise<string> {
let path = opts?.path ?? '/';

// Check if this is a folder
if (path.endsWith('/')) throw new Error('Cannot sync folder as file');

// Get metadata for the file
Expand All @@ -530,7 +533,8 @@ export class WorkspaceProj {
if (!meta) throw new Error(`File not found: ${path}`);

// Get the file in the FS
const basedir = `${this.manager.group}/${this.uuid}`;
const basepdir = opts?.useProjectName ? this.name : this.uuid;
const basedir = `${this.manager.group}/${basepdir}`;
const folder = await opfs.getDirectoryHandle(basedir, { create: true });
const fileHandle = await opfs.getFileHandle(path, { create: true, root: folder });
if (!fileHandle) throw new Error(`File could not be created: ${path}`);
Expand Down Expand Up @@ -573,3 +577,11 @@ export class WorkspaceProj {
});
}
}

/** Options for the FileSystem Sync module */
type SyncFsOpts = {
/** The path to synchronize, relative in the project. */
path?: string;
/** Use the project name as the folder name in the OPFS, instead of UUID. */
useProjectName?: boolean;
};

0 comments on commit b37ef44

Please sign in to comment.