@filen/sync 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,344 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RemoteFileSystem = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const semaphore_1 = require("../../semaphore");
9
+ const fs_extra_1 = __importDefault(require("fs-extra"));
10
+ class RemoteFileSystem {
11
+ constructor({ sync }) {
12
+ this.getDirectoryTreeCache = {
13
+ timestamp: 0,
14
+ tree: {},
15
+ uuids: {}
16
+ };
17
+ this.mutex = new semaphore_1.Semaphore(1);
18
+ this.mkdirMutex = new semaphore_1.Semaphore(1);
19
+ this.sync = sync;
20
+ }
21
+ async getDirectoryTree() {
22
+ // TODO: Actual implementation using cache + different endpoint with deviceId
23
+ // Account for duplicate path names for directories/files. Only keep the first one discovered in the tree.
24
+ const tree = {};
25
+ const dir = await this.sync.sdk.cloud().getDirectoryTree({ uuid: this.sync.syncPair.remoteParentUUID });
26
+ const uuids = {};
27
+ for (const path in dir) {
28
+ if (!dir[path] || dir[path].parent === "base" || path.startsWith(".filen.trash.local")) {
29
+ continue;
30
+ }
31
+ const item = Object.assign(Object.assign({}, dir[path]), { path });
32
+ tree[path] = item;
33
+ const treeItem = tree[path];
34
+ if (treeItem) {
35
+ uuids[treeItem.uuid] = item;
36
+ }
37
+ }
38
+ this.getDirectoryTreeCache = {
39
+ timestamp: Date.now(),
40
+ tree,
41
+ uuids
42
+ };
43
+ return { tree, uuids };
44
+ }
45
+ /**
46
+ * Find the corresponding UUID of the relative path.
47
+ * @date 3/3/2024 - 6:55:53 PM
48
+ *
49
+ * @public
50
+ * @async
51
+ * @param {{ relativePath: string; type?: FSItemType }} param0
52
+ * @param {string} param0.relativePath
53
+ * @param {FSItemType} param0.type
54
+ * @returns {Promise<string | null>}
55
+ */
56
+ async pathToItemUUID({ relativePath, type }) {
57
+ if (this.getDirectoryTreeCache.timestamp <= 0) {
58
+ await this.getDirectoryTree();
59
+ }
60
+ const acceptedTypes = !type ? ["directory", "file"] : type === "directory" ? ["directory"] : ["file"];
61
+ if (relativePath === "/" || relativePath === "." || relativePath.length <= 0) {
62
+ return this.sync.syncPair.remoteParentUUID;
63
+ }
64
+ if (this.getDirectoryTreeCache.tree[relativePath] && acceptedTypes.includes(this.getDirectoryTreeCache.tree[relativePath].type)) {
65
+ return this.getDirectoryTreeCache.tree[relativePath].uuid;
66
+ }
67
+ return null;
68
+ }
69
+ /**
70
+ * Create a directory inside the remote sync path. Recursively creates intermediate directories if needed.
71
+ * @date 3/2/2024 - 9:34:14 PM
72
+ *
73
+ * @public
74
+ * @async
75
+ * @param {{ relativePath: string }} param0
76
+ * @param {string} param0.relativePath
77
+ * @returns {Promise<string>}
78
+ */
79
+ async mkdir({ relativePath }) {
80
+ await this.mkdirMutex.acquire();
81
+ try {
82
+ if (relativePath === "/") {
83
+ return this.sync.syncPair.remoteParentUUID;
84
+ }
85
+ const exists = await this.pathToItemUUID({ relativePath });
86
+ if (exists) {
87
+ return exists;
88
+ }
89
+ const parentPath = path_1.default.posix.dirname(relativePath);
90
+ const basename = path_1.default.posix.basename(relativePath);
91
+ if (parentPath === "/" || parentPath === "." || parentPath.length <= 0) {
92
+ const uuid = await this.sync.sdk.cloud().createDirectory({ name: basename, parent: this.sync.syncPair.remoteParentUUID });
93
+ this.getDirectoryTreeCache.tree[relativePath] = {
94
+ type: "directory",
95
+ uuid,
96
+ name: basename,
97
+ size: 0,
98
+ path: relativePath
99
+ };
100
+ return uuid;
101
+ }
102
+ const pathEx = relativePath.split("/");
103
+ let builtPath = "/";
104
+ for (const part of pathEx) {
105
+ if (pathEx.length <= 0) {
106
+ continue;
107
+ }
108
+ builtPath = path_1.default.posix.join(builtPath, part);
109
+ if (!this.getDirectoryTreeCache.tree[builtPath]) {
110
+ const partBasename = path_1.default.posix.basename(builtPath);
111
+ const partParentPath = path_1.default.posix.dirname(builtPath);
112
+ const parentItem = this.getDirectoryTreeCache.tree[partParentPath];
113
+ if (!parentItem) {
114
+ continue;
115
+ }
116
+ const parentIsBase = partParentPath === "/" || partParentPath === "." || partParentPath === "";
117
+ const parentUUID = parentIsBase ? this.sync.syncPair.remoteParentUUID : parentItem.uuid;
118
+ const uuid = await this.sync.sdk.cloud().createDirectory({ name: partBasename, parent: parentUUID });
119
+ this.getDirectoryTreeCache.tree[relativePath] = {
120
+ type: "directory",
121
+ uuid,
122
+ name: partBasename,
123
+ size: 0,
124
+ path: relativePath
125
+ };
126
+ }
127
+ }
128
+ if (!this.getDirectoryTreeCache.tree[relativePath]) {
129
+ throw new Error(`Could not create directory at path ${relativePath}.`);
130
+ }
131
+ return this.getDirectoryTreeCache.tree[relativePath].uuid;
132
+ }
133
+ finally {
134
+ this.mkdirMutex.release();
135
+ }
136
+ }
137
+ /**
138
+ * Delete a file/directory inside the remote sync path.
139
+ * @date 3/3/2024 - 7:03:18 PM
140
+ *
141
+ * @public
142
+ * @async
143
+ * @param {{ relativePath: string; type?: FSItemType; permanent?: boolean }} param0
144
+ * @param {string} param0.relativePath
145
+ * @param {FSItemType} param0.type
146
+ * @param {boolean} [param0.permanent=false]
147
+ * @returns {Promise<void>}
148
+ */
149
+ async unlink({ relativePath, type, permanent = false }) {
150
+ await this.mutex.acquire();
151
+ try {
152
+ const uuid = await this.pathToItemUUID({ relativePath });
153
+ if (!uuid || !this.getDirectoryTreeCache.tree[relativePath]) {
154
+ return;
155
+ }
156
+ const acceptedTypes = !type ? ["directory", "file"] : type === "directory" ? ["directory"] : ["file"];
157
+ if (!acceptedTypes.includes(this.getDirectoryTreeCache.tree[relativePath].type)) {
158
+ return;
159
+ }
160
+ if (this.getDirectoryTreeCache.tree[relativePath].type === "directory") {
161
+ if (permanent) {
162
+ await this.sync.sdk.cloud().deleteDirectory({ uuid });
163
+ }
164
+ else {
165
+ await this.sync.sdk.cloud().trashDirectory({ uuid });
166
+ }
167
+ }
168
+ else {
169
+ if (permanent) {
170
+ await this.sync.sdk.cloud().deleteFile({ uuid });
171
+ }
172
+ else {
173
+ await this.sync.sdk.cloud().trashFile({ uuid });
174
+ }
175
+ }
176
+ delete this.getDirectoryTreeCache.tree[relativePath];
177
+ for (const entry in this.getDirectoryTreeCache.tree) {
178
+ if (entry.startsWith(relativePath + "/")) {
179
+ delete this.getDirectoryTreeCache.tree[entry];
180
+ }
181
+ }
182
+ }
183
+ finally {
184
+ this.mutex.release();
185
+ }
186
+ }
187
+ /**
188
+ * Rename a file/directory inside the remote sync path. Recursively creates intermediate directories if needed.
189
+ * @date 3/2/2024 - 9:35:12 PM
190
+ *
191
+ * @public
192
+ * @async
193
+ * @param {{ fromRelativePath: string; toRelativePath: string }} param0
194
+ * @param {string} param0.fromRelativePath
195
+ * @param {string} param0.toRelativePath
196
+ * @returns {Promise<void>}
197
+ */
198
+ async rename({ fromRelativePath, toRelativePath }) {
199
+ await this.mutex.acquire();
200
+ try {
201
+ if (fromRelativePath === "/" || fromRelativePath === toRelativePath) {
202
+ return;
203
+ }
204
+ const uuid = await this.pathToItemUUID({ relativePath: fromRelativePath });
205
+ const item = this.getDirectoryTreeCache.tree[fromRelativePath];
206
+ if (!uuid || !item) {
207
+ throw new Error(`Could not rename ${fromRelativePath} to ${toRelativePath}: Path not found.`);
208
+ }
209
+ const currentParentPath = path_1.default.posix.dirname(fromRelativePath);
210
+ const newParentPath = path_1.default.posix.dirname(toRelativePath);
211
+ const newBasename = path_1.default.posix.basename(toRelativePath);
212
+ const oldBasename = path_1.default.posix.basename(fromRelativePath);
213
+ const itemMetadata = item.type === "file"
214
+ ? ({
215
+ name: newBasename,
216
+ size: item.size,
217
+ mime: item.mime,
218
+ lastModified: item.lastModified,
219
+ creation: item.creation,
220
+ hash: item.hash,
221
+ key: item.key
222
+ })
223
+ : ({
224
+ name: newBasename
225
+ });
226
+ if (newParentPath === currentParentPath) {
227
+ if (toRelativePath === "/" || newBasename.length <= 0) {
228
+ return;
229
+ }
230
+ if (item.type === "directory") {
231
+ await this.sync.sdk.cloud().renameDirectory({ uuid, name: newBasename });
232
+ }
233
+ else {
234
+ await this.sync.sdk.cloud().renameFile({
235
+ uuid,
236
+ metadata: itemMetadata,
237
+ name: newBasename
238
+ });
239
+ }
240
+ const oldItem = this.getDirectoryTreeCache.tree[fromRelativePath];
241
+ if (oldItem) {
242
+ this.getDirectoryTreeCache.tree[toRelativePath] = Object.assign(Object.assign({}, oldItem), { name: newBasename });
243
+ }
244
+ delete this.getDirectoryTreeCache.tree[fromRelativePath];
245
+ }
246
+ else {
247
+ if (oldBasename !== newBasename) {
248
+ if (item.type === "directory") {
249
+ await this.sync.sdk.cloud().renameDirectory({ uuid, name: newBasename });
250
+ }
251
+ else {
252
+ await this.sync.sdk.cloud().renameFile({
253
+ uuid,
254
+ metadata: itemMetadata,
255
+ name: newBasename
256
+ });
257
+ }
258
+ }
259
+ if (newParentPath === "/" || newParentPath === "." || newParentPath === "") {
260
+ if (item.type === "directory") {
261
+ await this.sync.sdk
262
+ .cloud()
263
+ .moveDirectory({ uuid, to: this.sync.syncPair.remoteParentUUID, metadata: itemMetadata });
264
+ }
265
+ else {
266
+ await this.sync.sdk
267
+ .cloud()
268
+ .moveFile({ uuid, to: this.sync.syncPair.remoteParentUUID, metadata: itemMetadata });
269
+ }
270
+ }
271
+ else {
272
+ await this.mkdir({ relativePath: newParentPath });
273
+ const newParentItem = this.getDirectoryTreeCache.tree[newParentPath];
274
+ if (!newParentItem) {
275
+ throw new Error(`Could not find path ${newParentPath}.`);
276
+ }
277
+ if (item.type === "directory") {
278
+ await this.sync.sdk
279
+ .cloud()
280
+ .moveDirectory({ uuid, to: newParentItem.uuid, metadata: itemMetadata });
281
+ }
282
+ else {
283
+ await this.sync.sdk.cloud().moveFile({ uuid, to: newParentItem.uuid, metadata: itemMetadata });
284
+ }
285
+ }
286
+ const oldItem = this.getDirectoryTreeCache.tree[fromRelativePath];
287
+ if (oldItem) {
288
+ this.getDirectoryTreeCache.tree[toRelativePath] = Object.assign(Object.assign({}, oldItem), { name: newBasename });
289
+ }
290
+ delete this.getDirectoryTreeCache.tree[fromRelativePath];
291
+ for (const oldPath in this.getDirectoryTreeCache.tree) {
292
+ if (oldPath.startsWith(fromRelativePath + "/")) {
293
+ const newPath = oldPath.split(fromRelativePath).join(toRelativePath);
294
+ const oldItem = this.getDirectoryTreeCache.tree[oldPath];
295
+ if (oldItem) {
296
+ this.getDirectoryTreeCache.tree[newPath] = Object.assign(Object.assign({}, oldItem), { name: newBasename });
297
+ }
298
+ delete this.getDirectoryTreeCache.tree[oldPath];
299
+ }
300
+ }
301
+ }
302
+ }
303
+ finally {
304
+ this.mutex.release();
305
+ }
306
+ }
307
+ /**
308
+ * Download a remote file.
309
+ * @date 3/2/2024 - 9:41:59 PM
310
+ *
311
+ * @public
312
+ * @async
313
+ * @param {{ relativePath: string }} param0
314
+ * @param {string} param0.relativePath
315
+ * @returns {Promise<fs.Stats>}
316
+ */
317
+ async download({ relativePath }) {
318
+ const localPath = path_1.default.posix.join(this.sync.syncPair.localPath, relativePath);
319
+ const uuid = await this.pathToItemUUID({ relativePath });
320
+ const item = this.getDirectoryTreeCache.tree[relativePath];
321
+ if (!uuid || !item) {
322
+ throw new Error(`Could not download ${relativePath}: File not found.`);
323
+ }
324
+ if (item.type === "directory") {
325
+ throw new Error(`Could not download ${relativePath}: Not a file.`);
326
+ }
327
+ const tmpPath = await this.sync.sdk.cloud().downloadFileToLocal({
328
+ uuid,
329
+ bucket: item.bucket,
330
+ region: item.region,
331
+ chunks: item.chunks,
332
+ version: item.version,
333
+ key: item.key
334
+ });
335
+ await fs_extra_1.default.move(tmpPath, localPath, {
336
+ overwrite: true
337
+ });
338
+ await fs_extra_1.default.utimes(localPath, Date.now(), item.lastModified);
339
+ return await fs_extra_1.default.stat(localPath);
340
+ }
341
+ }
342
+ exports.RemoteFileSystem = RemoteFileSystem;
343
+ exports.default = RemoteFileSystem;
344
+ //# sourceMappingURL=remote.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remote.js","sourceRoot":"","sources":["../../../src/lib/filesystems/remote.ts"],"names":[],"mappings":";;;;;;AAEA,gDAA6B;AAC7B,+CAA2C;AAC3C,wDAAyB;AAQzB,MAAa,gBAAgB;IAU5B,YAAmB,EAAE,IAAI,EAAkB;QARpC,0BAAqB,GAAkF;YAC7G,SAAS,EAAE,CAAC;YACZ,IAAI,EAAE,EAAE;YACR,KAAK,EAAE,EAAE;SACT,CAAA;QACgB,UAAK,GAAG,IAAI,qBAAS,CAAC,CAAC,CAAC,CAAA;QACxB,eAAU,GAAG,IAAI,qBAAS,CAAC,CAAC,CAAC,CAAA;QAG7C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IACjB,CAAC;IAEM,KAAK,CAAC,gBAAgB;QAC5B,6EAA6E;QAC7E,0GAA0G;QAC1G,MAAM,IAAI,GAAwB,EAAE,CAAA;QACpC,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,gBAAgB,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,gBAAgB,EAAE,CAAC,CAAA;QACvG,MAAM,KAAK,GAAyB,EAAE,CAAA;QAEtC,KAAK,MAAM,IAAI,IAAI,GAAG,EAAE,CAAC;YACxB,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAE,CAAC,MAAM,KAAK,MAAM,IAAI,IAAI,CAAC,UAAU,CAAC,oBAAoB,CAAC,EAAE,CAAC;gBACzF,SAAQ;YACT,CAAC;YAED,MAAM,IAAI,mCACN,GAAG,CAAC,IAAI,CAAE,KACb,IAAI,GACJ,CAAA;YAED,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;YAEjB,MAAM,QAAQ,GAAG,IAAI,CAA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AC,qBAAqB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;QAE1D,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,sBAAsB,YAAY,mBAAmB,CAAC,CAAA;QACvE,CAAC;QAED,IAAI,IAAI,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;YAC/B,MAAM,IAAI,KAAK,CAAC,sBAAsB,YAAY,eAAe,CAAC,CAAA;QACnE,CAAC;QAED,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,mBAAmB,CAAC;YAC/D,IAAI;YACJ,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,GAAG,EAAE,IAAI,CAAC,GAAG;SACb,CAAC,CAAA;QAEF,MAAM,kBAAE,CAAC,IAAI,CAAC,OAAO,EAAE,SAAS,EAAE;YACjC,SAAS,EAAE,IAAI;SACf,CAAC,CAAA;QAEF,MAAM,kBAAE,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,IAAI,CAAC,YAAY,CAAC,CAAA;QAEzD,OAAO,MAAM,kBAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;IAChC,CAAC;CACD;AAzZD,4CAyZC;AAED,kBAAe,gBAAgB,CAAA"}
@@ -0,0 +1,43 @@
1
+ import type Sync from "./sync";
2
+ import type { RemoteTree } from "./filesystems/remote";
3
+ import type { LocalTree } from "./filesystems/local";
4
+ import type { DoneTask } from "./tasks";
5
+ /**
6
+ * State
7
+ * @date 3/1/2024 - 11:11:32 PM
8
+ *
9
+ * @export
10
+ * @class State
11
+ * @typedef {State}
12
+ */
13
+ export declare class State {
14
+ private readonly sync;
15
+ private readonly statePath;
16
+ /**
17
+ * Creates an instance of State.
18
+ * @date 3/1/2024 - 11:11:36 PM
19
+ *
20
+ * @constructor
21
+ * @public
22
+ * @param {{ sync: Sync }} param0
23
+ * @param {Sync} param0.sync
24
+ */
25
+ constructor({ sync }: {
26
+ sync: Sync;
27
+ });
28
+ applyDoneTasksToState({ doneTasks, currentLocalTree, currentRemoteTree }: {
29
+ doneTasks: DoneTask[];
30
+ currentLocalTree: LocalTree;
31
+ currentRemoteTree: RemoteTree;
32
+ }): {
33
+ currentLocalTree: LocalTree;
34
+ currentRemoteTree: RemoteTree;
35
+ };
36
+ saveLocalFileHashes(): Promise<void>;
37
+ loadLocalFileHashes(): Promise<void>;
38
+ initialize(): Promise<void>;
39
+ save(): Promise<void>;
40
+ loadPreviousTrees(): Promise<void>;
41
+ savePreviousTrees(): Promise<void>;
42
+ }
43
+ export default State;
@@ -0,0 +1,228 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.State = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const fs_extra_1 = __importDefault(require("fs-extra"));
9
+ const msgpackr_1 = require("msgpackr");
10
+ const STATE_VERSION = 1;
11
+ /**
12
+ * State
13
+ * @date 3/1/2024 - 11:11:32 PM
14
+ *
15
+ * @export
16
+ * @class State
17
+ * @typedef {State}
18
+ */
19
+ class State {
20
+ /**
21
+ * Creates an instance of State.
22
+ * @date 3/1/2024 - 11:11:36 PM
23
+ *
24
+ * @constructor
25
+ * @public
26
+ * @param {{ sync: Sync }} param0
27
+ * @param {Sync} param0.sync
28
+ */
29
+ constructor({ sync }) {
30
+ this.sync = sync;
31
+ this.statePath = path_1.default.join(this.sync.dbPath, "state", `v${STATE_VERSION}`);
32
+ }
33
+ applyDoneTasksToState({ doneTasks, currentLocalTree, currentRemoteTree }) {
34
+ // Work on the done tasks from "right to left" (descending order, path length).
35
+ // This ensures we pick up all individual files/directory movements (e.g. parent moved to /a/b while children are moved /c/d)
36
+ const tasks = doneTasks.sort((a, b) => b.path.split("/").length - a.path.split("/").length);
37
+ for (const task of tasks) {
38
+ switch (task.type) {
39
+ case "renameRemoteDirectory":
40
+ case "renameRemoteFile":
41
+ case "moveRemoteDirectory":
42
+ case "moveRemoteFile": {
43
+ for (const oldPath in currentRemoteTree.tree) {
44
+ if (oldPath.startsWith(task.from + "/") || oldPath === task.from) {
45
+ const newPath = oldPath.split(task.from).join(task.to);
46
+ const oldItem = currentRemoteTree.tree[oldPath];
47
+ if (oldItem) {
48
+ const item = Object.assign(Object.assign({}, oldItem), { path: newPath, name: path_1.default.posix.basename(newPath) });
49
+ currentRemoteTree.tree[newPath] = item;
50
+ delete currentRemoteTree.tree[oldPath];
51
+ }
52
+ }
53
+ }
54
+ for (const uuid in currentRemoteTree.uuids) {
55
+ const currentItem = currentRemoteTree.uuids[uuid];
56
+ if (!currentItem) {
57
+ continue;
58
+ }
59
+ const oldPath = currentItem.path;
60
+ if (oldPath.startsWith(task.from + "/") || oldPath === task.from) {
61
+ const newPath = oldPath.split(task.from).join(task.to);
62
+ const item = Object.assign(Object.assign({}, currentItem), { path: newPath, name: path_1.default.posix.basename(newPath) });
63
+ currentRemoteTree.uuids[uuid] = item;
64
+ }
65
+ }
66
+ break;
67
+ }
68
+ case "moveLocalDirectory":
69
+ case "moveLocalFile":
70
+ case "renameLocalDirectory":
71
+ case "renameLocalFile": {
72
+ for (const oldPath in currentLocalTree.tree) {
73
+ if (oldPath.startsWith(task.from + "/") || oldPath === task.from) {
74
+ const newPath = oldPath.split(task.from).join(task.to);
75
+ const oldItem = currentLocalTree.tree[oldPath];
76
+ if (oldItem) {
77
+ const item = Object.assign(Object.assign({}, oldItem), { path: newPath });
78
+ currentLocalTree.tree[newPath] = item;
79
+ delete currentLocalTree.tree[oldPath];
80
+ }
81
+ }
82
+ }
83
+ for (const inode in currentLocalTree.inodes) {
84
+ const currentItem = currentLocalTree.inodes[inode];
85
+ if (!currentItem) {
86
+ continue;
87
+ }
88
+ const oldPath = currentItem.path;
89
+ if (oldPath.startsWith(task.from + "/") || oldPath === task.from) {
90
+ const newPath = oldPath.split(task.from).join(task.to);
91
+ const item = Object.assign(Object.assign({}, currentItem), { path: newPath });
92
+ currentLocalTree.inodes[inode] = item;
93
+ }
94
+ }
95
+ break;
96
+ }
97
+ case "deleteLocalDirectory":
98
+ case "deleteLocalFile":
99
+ case "deleteRemoteDirectory":
100
+ case "deleteRemoteFile": {
101
+ for (const path in currentLocalTree.tree) {
102
+ if (path.startsWith(task.path + "/") || path === task.path) {
103
+ delete currentLocalTree.tree[path];
104
+ }
105
+ }
106
+ for (const inode in currentLocalTree.inodes) {
107
+ const currentItem = currentLocalTree.inodes[inode];
108
+ if (!currentItem) {
109
+ continue;
110
+ }
111
+ const path = currentItem.path;
112
+ if (path.startsWith(task.path + "/") || path === task.path) {
113
+ delete currentLocalTree.inodes[inode];
114
+ }
115
+ }
116
+ for (const path in currentRemoteTree.tree) {
117
+ if (path.startsWith(task.path + "/") || path === task.path) {
118
+ delete currentRemoteTree.tree[path];
119
+ }
120
+ }
121
+ for (const uuid in currentRemoteTree.uuids) {
122
+ const currentItem = currentRemoteTree.uuids[uuid];
123
+ if (!currentItem) {
124
+ continue;
125
+ }
126
+ const path = currentItem.path;
127
+ if (path.startsWith(task.path + "/") || path === task.path) {
128
+ delete currentRemoteTree.uuids[uuid];
129
+ }
130
+ }
131
+ delete this.sync.localFileHashes[task.path];
132
+ break;
133
+ }
134
+ case "createRemoteDirectory": {
135
+ const item = {
136
+ name: path_1.default.posix.basename(task.path),
137
+ type: "directory",
138
+ uuid: task.uuid,
139
+ size: 0,
140
+ path: task.path
141
+ };
142
+ currentRemoteTree.tree[task.path] = item;
143
+ currentRemoteTree.uuids[item.uuid] = item;
144
+ break;
145
+ }
146
+ case "uploadFile": {
147
+ const item = Object.assign(Object.assign({}, task.item), { path: task.path });
148
+ currentRemoteTree.tree[task.path] = item;
149
+ currentRemoteTree.uuids[item.uuid] = item;
150
+ break;
151
+ }
152
+ case "createLocalDirectory": {
153
+ const item = {
154
+ lastModified: parseInt(task.stats.mtimeMs), // Sometimes comes as a float, but we need an int
155
+ type: "directory",
156
+ path: task.path,
157
+ creation: parseInt(task.stats.birthtimeMs), // Sometimes comes as a float, but we need an int
158
+ size: task.stats.size,
159
+ inode: task.stats.ino
160
+ };
161
+ currentLocalTree.tree[task.path] = item;
162
+ currentLocalTree.inodes[item.inode] = item;
163
+ break;
164
+ }
165
+ case "downloadFile": {
166
+ const item = {
167
+ lastModified: parseInt(task.stats.mtimeMs), // Sometimes comes as a float, but we need an int
168
+ type: "file",
169
+ path: task.path,
170
+ creation: parseInt(task.stats.birthtimeMs), // Sometimes comes as a float, but we need an int
171
+ size: task.stats.size,
172
+ inode: task.stats.ino
173
+ };
174
+ currentLocalTree.tree[task.path] = item;
175
+ currentLocalTree.inodes[item.inode] = item;
176
+ break;
177
+ }
178
+ }
179
+ }
180
+ return {
181
+ currentLocalTree,
182
+ currentRemoteTree
183
+ };
184
+ }
185
+ async saveLocalFileHashes() {
186
+ const path = path_1.default.join(this.statePath, "localFileHashes");
187
+ const serialized = (0, msgpackr_1.pack)(this.sync.localFileHashes);
188
+ await fs_extra_1.default.ensureDir(this.statePath);
189
+ await fs_extra_1.default.writeFile(path, serialized);
190
+ }
191
+ async loadLocalFileHashes() {
192
+ const path = path_1.default.join(this.statePath, "localFileHashes");
193
+ await fs_extra_1.default.ensureDir(this.statePath);
194
+ if (!(await fs_extra_1.default.exists(path))) {
195
+ return;
196
+ }
197
+ const buffer = await fs_extra_1.default.readFile(path);
198
+ this.sync.localFileHashes = (0, msgpackr_1.unpack)(buffer);
199
+ }
200
+ async initialize() {
201
+ await Promise.all([this.loadLocalFileHashes(), this.loadPreviousTrees()]);
202
+ }
203
+ async save() {
204
+ await Promise.all([this.saveLocalFileHashes(), this.savePreviousTrees()]);
205
+ }
206
+ async loadPreviousTrees() {
207
+ const localPath = path_1.default.join(this.statePath, "previousLocalTree");
208
+ const remotePath = path_1.default.join(this.statePath, "previousRemoteTree");
209
+ await fs_extra_1.default.ensureDir(this.statePath);
210
+ if (!(await fs_extra_1.default.exists(localPath)) || !(await fs_extra_1.default.exists(remotePath))) {
211
+ return;
212
+ }
213
+ const [localBuffer, remoteBuffer] = await Promise.all([fs_extra_1.default.readFile(localPath), fs_extra_1.default.readFile(remotePath)]);
214
+ this.sync.previousLocalTree = (0, msgpackr_1.unpack)(localBuffer);
215
+ this.sync.previousRemoteTree = (0, msgpackr_1.unpack)(remoteBuffer);
216
+ }
217
+ async savePreviousTrees() {
218
+ const localPath = path_1.default.join(this.statePath, "previousLocalTree");
219
+ const remotePath = path_1.default.join(this.statePath, "previousRemoteTree");
220
+ const localSerialized = (0, msgpackr_1.pack)(this.sync.previousLocalTree);
221
+ const remoteSerialized = (0, msgpackr_1.pack)(this.sync.previousRemoteTree);
222
+ await fs_extra_1.default.ensureDir(this.statePath);
223
+ await Promise.all([fs_extra_1.default.writeFile(localPath, localSerialized), fs_extra_1.default.writeFile(remotePath, remoteSerialized)]);
224
+ }
225
+ }
226
+ exports.State = State;
227
+ exports.default = State;
228
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../../src/lib/state.ts"],"names":[],"mappings":";;;;;;AACA,gDAA6B;AAC7B,wDAAyB;AACzB,uCAAuC;AAKvC,MAAM,aAAa,GAAG,CAAC,CAAA;AAEvB;;;;;;;GAOG;AACH,MAAa,KAAK;IAIjB;;;;;;;;OAQG;IACH,YAAmB,EAAE,IAAI,EAAkB;QAC1C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,SAAS,GAAG,cAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,aAAa,EAAE,CAAC,CAAA;IACjF,CAAC;IAEM,qBAAqB,CAAC,E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