Skip to content

Commit 00efc7c

Browse files
committed
Fix lazy content lookups for dir resources
1 parent 0096022 commit 00efc7c

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

packages/php-wasm/universal/src/lib/write-files.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@ export async function writeFiles(
4545
await php.rmdir(root, { recursive: true });
4646
}
4747
}
48-
for (const [relativePath, content] of Object.entries(newFiles)) {
48+
newFiles = await newFiles;
49+
for (const relativePath of Object.keys(newFiles)) {
50+
const content = await newFiles[relativePath];
51+
4952
const filePath = joinPaths(root, relativePath);
5053
if (!(await php.fileExists(dirname(filePath)))) {
5154
await php.mkdir(dirname(filePath));
5255
}
5356
if (content instanceof Uint8Array || typeof content === 'string') {
5457
await php.writeFile(filePath, content);
5558
} else {
56-
const fileTreeContent = content as MaybePromise<FileTree>;
57-
await writeFiles(php, filePath, fileTreeContent);
59+
await writeFiles(php, filePath, content);
5860
}
5961
}
6062
}

packages/playground/blueprints/src/lib/resources.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -353,33 +353,34 @@ export class BlueprintAssetDirectoryResource extends VFSDirectoryResource {
353353
async function createLazyVFSFileTree(
354354
path: string,
355355
playground: UniversalPHP
356-
): Promise<FileTree> {
357-
const keys = await playground.listFiles(path);
358-
const keySet = new Set(keys);
356+
): Promise<FileTreeAsync> {
357+
const lazyFileTree: FileTreeAsync = {};
358+
359+
if (!(await playground.isDir(path))) {
360+
throw new Error(`Path "${path}" is not a directory`);
361+
}
362+
363+
for (const fileName of await playground.listFiles(path)) {
364+
Object.defineProperty(lazyFileTree, fileName, {
365+
configurable: false,
366+
enumerable: true,
367+
async get() {
368+
const fullPath = joinPaths(path, fileName);
359369

360-
return new Proxy<FileTree>(
361-
{},
362-
{
363-
ownKeys() {
364-
return keys;
365-
},
366-
async get(target, prop: string) {
367-
if (!keySet.has(prop)) {
368-
return undefined;
369-
}
370-
const fullPath = joinPaths(path, prop);
371370
if (!(await playground.fileExists(fullPath))) {
372371
return undefined;
373372
}
374373

375374
if (await playground.isDir(fullPath)) {
376375
return createLazyVFSFileTree(fullPath, playground);
377376
} else {
378-
return playground.readFileAsBuffer(joinPaths(path, prop));
377+
return playground.readFileAsBuffer(fullPath);
379378
}
380379
},
381-
}
382-
);
380+
});
381+
}
382+
383+
return lazyFileTree;
383384
}
384385

385386
/**

0 commit comments

Comments
 (0)