chore: importFs handles it's own error (#9971)

This commit is contained in:
Nikolay Vitkov 2023-04-04 17:29:29 +02:00 committed by GitHub
parent 0b1e20cf5b
commit 327febe2c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 34 deletions

View File

@ -2116,18 +2116,9 @@ export class Page extends EventEmitter {
return; return;
} }
try { const fs = await importFSPromises();
const fs = await importFSPromises();
await fs.writeFile(path, buffer); await fs.writeFile(path, buffer);
} catch (error) {
if (error instanceof TypeError) {
throw new Error(
'Can only pass a file path in a Node-like environment.'
);
}
throw error;
}
} }
/** /**

View File

@ -883,17 +883,7 @@ export class Frame {
} }
if (path) { if (path) {
let fs: typeof import('fs/promises'); const fs = await importFSPromises();
try {
fs = await importFSPromises();
} catch (error) {
if (error instanceof TypeError) {
throw new Error(
'Can only pass a file path in a Node-like environment.'
);
}
throw error;
}
content = await fs.readFile(path, 'utf8'); content = await fs.readFile(path, 'utf8');
content += '/*# sourceURL=' + path.replace(/\n/g, '') + '*/'; content += '/*# sourceURL=' + path.replace(/\n/g, '') + '*/';

View File

@ -369,7 +369,16 @@ export async function importFSPromises(): Promise<
typeof import('fs/promises') typeof import('fs/promises')
> { > {
if (!fs) { if (!fs) {
fs = await import('fs/promises'); try {
fs = await import('fs/promises');
} catch (error) {
if (error instanceof TypeError) {
throw new Error(
'Cannot write to a path outside of a Node-like environment.'
);
}
throw error;
}
} }
return fs; return fs;
} }
@ -383,17 +392,7 @@ export async function getReadableAsBuffer(
): Promise<Buffer | null> { ): Promise<Buffer | null> {
const buffers = []; const buffers = [];
if (path) { if (path) {
let fs: typeof import('fs/promises'); const fs = await importFSPromises();
try {
fs = await importFSPromises();
} catch (error) {
if (error instanceof TypeError) {
throw new Error(
'Cannot write to a path outside of a Node-like environment.'
);
}
throw error;
}
const fileHandle = await fs.open(path, 'w+'); const fileHandle = await fs.open(path, 'w+');
for await (const chunk of readable) { for await (const chunk of readable) {
buffers.push(chunk); buffers.push(chunk);