CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/browser-websocket/realpath.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// path is assumed relative to the HOME directory
7
// return value is also relative to HOME directory -- if it is
8
// a symlink to something outside of the HOME directory, we just
9
// return the input! This is used for sync so this is the best
10
// we can do for now (see https://github.com/sagemathinc/cocalc/issues/4732)
11
import { realpath as fs_realpath } from "node:fs/promises";
12
13
// SMC_LOCAL_HUB_HOME is used for developing cocalc inside cocalc...
14
const HOME: string =
15
process.env.SMC_LOCAL_HUB_HOME ?? process.env.HOME ?? "/home/user";
16
17
export async function realpath(path: string): Promise<string> {
18
const fullpath = HOME + "/" + path;
19
const rpath = await fs_realpath(fullpath);
20
if (rpath == fullpath || !rpath.startsWith(HOME + "/")) {
21
return path;
22
}
23
return rpath.slice((HOME + "/").length);
24
}
25
26