-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fs/unstable): add readLink and readLinkSync (#6373)
- Loading branch information
Showing
4 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
|
||
import { getNodeFs, isDeno } from "./_utils.ts"; | ||
import { mapError } from "./_map_error.ts"; | ||
|
||
/** | ||
* Resolves to the path destination of the named symbolic link. | ||
* | ||
* Throws Error if called with a hard link. | ||
* | ||
* Requires `allow-read` permission. | ||
* | ||
* @example Usage | ||
* ```ts ignore | ||
* import { readLink } from "@std/fs/unstable-read-link"; | ||
* import { symlink } from "@std/fs/unstable-symlink"; | ||
* await symlink("./test.txt", "./test_link.txt"); | ||
* const target = await readLink("./test_link.txt"); // full path of ./test.txt | ||
* ``` | ||
* | ||
* @tags allow-read | ||
* | ||
* @param path The path of the symbolic link. | ||
* @returns A promise that resolves to the file path pointed by the symbolic | ||
* link. | ||
*/ | ||
export async function readLink(path: string | URL): Promise<string> { | ||
if (isDeno) { | ||
return Deno.readLink(path); | ||
} else { | ||
try { | ||
return await getNodeFs().promises.readlink(path); | ||
} catch (error) { | ||
throw mapError(error); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Synchronously returns the path destination of the named symbolic link. | ||
* | ||
* Throws Error if called with a hard link. | ||
* | ||
* Requires `allow-read` permission. | ||
* | ||
* @example Usage | ||
* ```ts ignore | ||
* import { readLinkSync } from "@std/fs/unstable-read-link"; | ||
* import { symlinkSync } from "@std/fs/unstable-symlink"; | ||
* symlinkSync("./test.txt", "./test_link.txt"); | ||
* const target = readLinkSync("./test_link.txt"); // full path of ./test.txt | ||
* ``` | ||
* | ||
* @tags allow-read | ||
* | ||
* @param path The path of the symbolic link. | ||
* @returns The file path pointed by the symbolic link. | ||
*/ | ||
export function readLinkSync(path: string | URL): string { | ||
if (isDeno) { | ||
return Deno.readLinkSync(path); | ||
} else { | ||
try { | ||
return getNodeFs().readlinkSync(path); | ||
} catch (error) { | ||
throw mapError(error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
|
||
import { assertEquals, assertRejects, assertThrows } from "@std/assert"; | ||
import { readLink, readLinkSync } from "./unstable_read_link.ts"; | ||
import { NotFound } from "./unstable_errors.js"; | ||
import { | ||
linkSync, | ||
mkdtempSync, | ||
rmSync, | ||
symlinkSync, | ||
writeFileSync, | ||
} from "node:fs"; | ||
import { link, mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; | ||
import { tmpdir } from "node:os"; | ||
import { join, resolve } from "node:path"; | ||
|
||
Deno.test("readLink() can read through symlink", async () => { | ||
const tempDirPath = await mkdtemp(resolve(tmpdir(), "readLink_")); | ||
const testFile = join(tempDirPath, "testFile.txt"); | ||
const symlinkFile = join(tempDirPath, "testFile.txt.link"); | ||
|
||
await writeFile(testFile, "Hello, Standard Library"); | ||
await symlink(testFile, symlinkFile); | ||
|
||
const realFile = await readLink(symlinkFile); | ||
assertEquals(testFile, realFile); | ||
|
||
await rm(tempDirPath, { recursive: true, force: true }); | ||
}); | ||
|
||
Deno.test("readLink() rejects with Error when reading from a hard link", async () => { | ||
const tempDirPath = await mkdtemp(resolve(tmpdir(), "readLink_")); | ||
const testFile = join(tempDirPath, "testFile.txt"); | ||
const linkFile = join(tempDirPath, "testFile.txt.hlink"); | ||
|
||
await writeFile(testFile, "Hello, Standard Library"); | ||
await link(testFile, linkFile); | ||
|
||
await assertRejects(async () => { | ||
await readLink(linkFile); | ||
}, Error); | ||
|
||
await rm(tempDirPath, { recursive: true, force: true }); | ||
}); | ||
|
||
Deno.test("readLink() rejects with NotFound when reading through a non-existent file", async () => { | ||
await assertRejects(async () => { | ||
await readLink("non-existent-file.txt.link"); | ||
}, NotFound); | ||
}); | ||
|
||
Deno.test("readLinkSync() can read through symlink", () => { | ||
const tempDirPath = mkdtempSync(resolve(tmpdir(), "readLink_")); | ||
const testFile = join(tempDirPath, "testFile.txt"); | ||
const symlinkFile = join(tempDirPath, "testFile.txt.link"); | ||
|
||
writeFileSync(testFile, "Hello, Standard Library"); | ||
symlinkSync(testFile, symlinkFile); | ||
|
||
const realFile = readLinkSync(symlinkFile); | ||
assertEquals(testFile, realFile); | ||
|
||
rmSync(tempDirPath, { recursive: true, force: true }); | ||
}); | ||
|
||
Deno.test("readLinkSync() throws Error when reading from a hard link", () => { | ||
const tempDirPath = mkdtempSync(resolve(tmpdir(), "readLinkSync_")); | ||
const testFile = join(tempDirPath, "testFile.txt"); | ||
const linkFile = join(tempDirPath, "testFile.txt.hlink"); | ||
|
||
writeFileSync(testFile, "Hello, Standard Library!"); | ||
linkSync(testFile, linkFile); | ||
|
||
assertThrows(() => { | ||
readLinkSync(linkFile); | ||
}, Error); | ||
|
||
rmSync(tempDirPath, { recursive: true, force: true }); | ||
}); | ||
|
||
Deno.test("readLinkSync() throws NotFound when reading through a non-existent file", () => { | ||
assertThrows(() => { | ||
readLinkSync("non-existent-file.txt.hlink"); | ||
}, NotFound); | ||
}); |