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/next/lib/share/proxy/get-public-path-info-url.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
// provided by nextjs: https://nextjs.org/blog/next-9-4#improved-built-in-fetch-support
7
declare var fetch;
8
9
import { RAW_MAX_SIZE_BYTES } from "./api";
10
11
export default async function getPublicPathInfoUrl(url: string) {
12
if (!url.startsWith("url/")) {
13
throw Error("url must start with 'url/'");
14
}
15
// TODO: more than just text documents...
16
let content: string | undefined = undefined;
17
let err: Error | undefined = undefined;
18
for (const start of ["https://", "http://"]) {
19
try {
20
content = await (
21
await fetch(`${start}${url.slice("url/".length)}`, {
22
size: RAW_MAX_SIZE_BYTES,
23
})
24
).text();
25
break;
26
} catch (_err) {
27
err = _err;
28
}
29
}
30
if (content == null) {
31
throw err;
32
}
33
return {
34
contents: { content, size: content.length },
35
relativePath: "",
36
projectTitle: `Document at ${url}`,
37
};
38
}
39
40