UNPKG

3.2 kBJavaScriptView Raw
1import FileSystem from "./fs/index.js";
2import * as cidLog from "./common/cid-log.js";
3import * as debug from "./common/debug.js";
4import * as dataRoot from "./data-root.js";
5import * as ucan from "./ucan/internal.js";
6import { Branch } from "./path.js";
7import { authenticatedUsername } from "./common/index.js";
8/**
9 * Load a user's file system.
10 *
11 * @param permissions The permissions from initialise.
12 * Pass `null` if working without permissions
13 * @param username Optional, username of the user to load the file system from.
14 * Will try to load the file system of the authenticated user
15 * by default. Throws an error if there's no authenticated user.
16 * @param rootKey Optional, AES key to be the root key of a new filesystem.
17 * Will be used if a filesystem hasn't been created yet.
18 */
19export async function loadFileSystem(permissions, username, rootKey) {
20 let cid, fs;
21 // Look for username
22 username = username || (await authenticatedUsername() || undefined);
23 if (!username)
24 throw new Error("User hasn't authenticated yet");
25 // Ensure internal UCAN dictionary
26 await ucan.store([]);
27 // Determine the correct CID of the file system to load
28 const dataCid = navigator.onLine ? await dataRoot.lookup(username) : null;
29 const [logIdx, logLength] = dataCid ? await cidLog.index(dataCid) : [-1, 0];
30 if (!navigator.onLine) {
31 // Offline, use local CID
32 cid = await cidLog.newest();
33 }
34 else if (!dataCid) {
35 // No DNS CID yet
36 cid = await cidLog.newest();
37 if (cid)
38 debug.log("📓 No DNSLink, using local CID:", cid);
39 else
40 debug.log("📓 Creating a new file system");
41 }
42 else if (logIdx === 0) {
43 // DNS is up to date
44 cid = dataCid;
45 debug.log("📓 DNSLink is up to date:", cid);
46 }
47 else if (logIdx > 0) {
48 // DNS is outdated
49 cid = await cidLog.newest();
50 const idxLog = logIdx === 1 ? "1 newer local entry" : logIdx + " newer local entries";
51 debug.log("📓 DNSLink is outdated (" + idxLog + "), using local CID:", cid);
52 }
53 else {
54 // DNS is newer
55 cid = dataCid;
56 await cidLog.add(cid);
57 debug.log("📓 DNSLink is newer:", cid);
58 }
59 // If a file system exists, load it and return it
60 const p = permissions || undefined;
61 fs = cid ? await FileSystem.fromCID(cid, { permissions: p }) : null;
62 if (fs)
63 return fs;
64 // Otherwise make a new one
65 if (!rootKey)
66 throw new Error("Can't make new filesystem without a root AES key");
67 fs = await FileSystem.empty({ permissions: p, rootKey });
68 await addSampleData(fs);
69 // Fin
70 return fs;
71}
72// ㊙️
73async function addSampleData(fs) {
74 await fs.mkdir({ directory: [Branch.Private, "Apps"] });
75 await fs.mkdir({ directory: [Branch.Private, "Audio"] });
76 await fs.mkdir({ directory: [Branch.Private, "Documents"] });
77 await fs.mkdir({ directory: [Branch.Private, "Photos"] });
78 await fs.mkdir({ directory: [Branch.Private, "Video"] });
79 await fs.publish();
80}
81//# sourceMappingURL=filesystem.js.map
\No newline at end of file