Path: blob/master/src/packages/project/browser-websocket/canonical-path.ts
1447 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/* Return a normalized version of the path, which is always6relative to the user's home directory.78If the path is not in the user's home directory, we use9the symlink form ~/.smc/root to / to make it appear to10be in the home directory.11*/1213import { realpath } from "fs/promises";14import { resolve } from "path";1516export async function canonical_paths(paths: string[]): Promise<string[]> {17const v: string[] = [];1819const { HOME: HOME_ENV } = process.env;20if (HOME_ENV == null) {21throw Error("HOME environment variable must be defined");22}2324// realpath is necessary, because in some circumstances the home dir is made up of a symlink25const HOME = await realpath(HOME_ENV);2627for (let path of paths) {28path = await realpath(resolve(path));29if (path.startsWith(HOME)) {30v.push(path.slice(HOME.length + 1));31} else {32v.push(HOME + "/.smc/root" + path);33}34}3536return v;37}383940