Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F4429509
test_fs_readfile_url.mjs
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
test_fs_readfile_url.mjs
View Options
import * as fs from 'node:fs';
import * as fsp from 'node:fs/promises';
import path from 'node:path';
function assert(condition, message) {
if (!condition) throw new Error(message);
}
async function readFileCallback(url, encoding) {
return await new Promise((resolve, reject) => {
fs.readFile(url, encoding, (error, data) => {
if (error) reject(error);
else resolve(data);
});
});
}
async function main() {
const dirPath = path.join(import.meta.dirname, '.fs url tmp');
const filePath = path.join(dirPath, 'url file.txt');
const content = 'hello from file URL reads';
fs.rmSync(dirPath, { recursive: true, force: true });
fs.mkdirSync(dirPath, { recursive: true });
fs.writeFileSync(filePath, content);
const fileUrl = new URL(`file://${encodeURI(filePath)}`);
try {
const syncContent = fs.readFileSync(fileUrl, 'utf8');
assert(syncContent === content, `expected sync read to match, got ${JSON.stringify(syncContent)}`);
const callbackContent = await readFileCallback(fileUrl, 'utf8');
assert(callbackContent === content, `expected callback read to match, got ${JSON.stringify(callbackContent)}`);
const promiseContent = await fsp.readFile(fileUrl, 'utf8');
assert(promiseContent === content, `expected promise read to match, got ${JSON.stringify(promiseContent)}`);
console.log('fs readFile accepts file URLs');
} finally {
fs.rmSync(dirPath, { recursive: true, force: true });
}
}
await main();
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Sat, May 2, 3:57 AM (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
544025
Default Alt Text
test_fs_readfile_url.mjs (1 KB)
Attached To
Mode
rANT Ant
Attached
Detach File
Event Timeline
Log In to Comment