Skip to content

Commit 013de9f

Browse files
committed
vfs: return string from mountPointURL
1 parent cf4eeec commit 013de9f

3 files changed

Lines changed: 15 additions & 28 deletions

File tree

doc/api/vfs.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ mounted.
208208
added: REPLACEME
209209
-->
210210

211-
* {URL | null}
211+
* {string | null}
212212

213-
The current mount point as a `file:` {URL}, or `null` when the VFS is
214-
not mounted. A fresh `URL` object is returned on each access, so
215-
mutating the result does not affect the instance.
213+
The current mount point as a `file:` URL string (the [`vfs.mountPoint`][]
214+
path converted with [`url.pathToFileURL()`][]), or `null` when the VFS
215+
is not mounted.
216216

217217
This is a convenience for addressing mounted files with URL-based
218218
APIs such as dynamic `import()`:
@@ -544,8 +544,10 @@ fields use synthetic but stable values:
544544
[`node:fs`]: fs.md
545545
[`require()`]: modules.md#requireid
546546
[`require.resolve()`]: modules.md#requireresolverequest-options
547+
[`url.pathToFileURL()`]: url.md#urlpathtofileurlpath-options
547548
[`vfs.mount()`]: #vfsmount
548549
[`vfs.mountPointURL`]: #vfsmountpointurl
550+
[`vfs.mountPoint`]: #vfsmountpoint
549551
[`vfs.unmount()`]: #vfsunmount
550552
[loading from `node_modules` folders]: modules.md#loading-from-node_modules-folders
551553
[the global folders]: modules.md#loading-from-the-global-folders

lib/internal/vfs/file_system.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ class VirtualFileSystem {
129129
}
130130

131131
/**
132-
* Gets the mount point as a `file:` URL, or null if not mounted.
133-
* A fresh URL object is returned on each access.
134-
* @returns {URL|null}
132+
* Gets the mount point as a `file:` URL string, or null if not
133+
* mounted.
134+
* @returns {string|null}
135135
*/
136136
get mountPointURL() {
137137
if (this[kMountPoint] === null) return null;
138138
const { pathToFileURL } = require('internal/url');
139-
return pathToFileURL(this[kMountPoint]);
139+
return pathToFileURL(this[kMountPoint]).href;
140140
}
141141

142142
/**

test/parallel/test-vfs-mount-point-url.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,21 @@ const vfs = require('node:vfs');
1313
assert.strictEqual(myVfs.mountPointURL, null);
1414
}
1515

16-
// Test: file: URL of the mount point while mounted, null again after
17-
// unmounting.
16+
// Test: file: URL string of the mount point while mounted, null again
17+
// after unmounting.
1818
{
1919
const myVfs = vfs.create();
2020
const mountPoint = myVfs.mount();
2121

2222
const url = myVfs.mountPointURL;
23-
assert.ok(url instanceof URL);
24-
assert.strictEqual(url.protocol, 'file:');
25-
assert.strictEqual(url.href, pathToFileURL(mountPoint).href);
23+
assert.strictEqual(typeof url, 'string');
24+
assert.ok(url.startsWith('file:'));
25+
assert.strictEqual(url, pathToFileURL(mountPoint).href);
2626

2727
myVfs.unmount();
2828
assert.strictEqual(myVfs.mountPointURL, null);
2929
}
3030

31-
// Test: each access returns a fresh URL object, so callers mutating the
32-
// result cannot corrupt the instance state.
33-
{
34-
const myVfs = vfs.create();
35-
myVfs.mount();
36-
37-
const first = myVfs.mountPointURL;
38-
first.pathname += '/tampered';
39-
const second = myVfs.mountPointURL;
40-
assert.notStrictEqual(first, second);
41-
assert.strictEqual(second.href, pathToFileURL(myVfs.mountPoint).href);
42-
43-
myVfs.unmount();
44-
}
45-
4631
// Test: the URL is usable to address files in the mounted VFS.
4732
{
4833
const myVfs = vfs.create();

0 commit comments

Comments
 (0)