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/http-api/read-text-file.ts
Views: 687
1
/*
2
Read a text file.
3
4
EXAMPLE:
5
6
curl -u `cat .smc/secret_token`: -d path=a.txt -d content="bar" http://localhost:`cat .smc/api-server.port`/api/v1/write-text-file
7
*/
8
9
import { readFile } from "node:fs/promises";
10
11
import { client } from "./server";
12
13
export default async function readTextFile({ path }): Promise<string> {
14
if (client == null) throw Error("client must be defined");
15
16
const dbg = client.dbg("read-text-file");
17
dbg(`path="${path}"`);
18
if (typeof path != "string") {
19
throw Error(`provide the path as a string -- got path="${path}"`);
20
}
21
22
return (await readFile(path)).toString();
23
}
24
25