Skip to content

Commit

Permalink
feat: smoke tests, more ipc events, update/remove syncs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Jun 30, 2024
1 parent 6ac5eb5 commit 9414e90
Show file tree
Hide file tree
Showing 8 changed files with 555 additions and 63 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"wait-on": "^7.2.0"
},
"dependencies": {
"@filen/sdk": "^0.1.129",
"@filen/sdk": "^0.1.130",
"@parcel/watcher": "^2.4.1",
"fs-extra": "^11.2.0",
"ignore": "^5.3.1",
Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ export class SyncWorker {
private async initSyncPairs(pairs: SyncPair[]): Promise<void> {
await this.initSyncPairsMutex.acquire()

const currentSyncPairsUUIDs = this.syncPairs.map(pair => pair.uuid)
const newSyncPairsUUIDs = pairs.map(pair => pair.uuid)

try {
const promises: Promise<void>[] = []

Expand All @@ -135,6 +138,12 @@ export class SyncWorker {
}

await Promise.all(promises)

for (const uuid of currentSyncPairsUUIDs) {
if (!newSyncPairsUUIDs.includes(uuid) && this.syncs[uuid]) {
this.syncs[uuid]!.removed = true
}
}
} catch (e) {
this.logger.log("error", e, "index.initSyncPairs")

Expand Down
42 changes: 20 additions & 22 deletions src/lib/filesystems/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,6 @@ export class LocalFileSystem {
return await fs.stat(localPath)
}

/**
* Delete a file/directory inside the local sync path.
* @date 3/3/2024 - 10:05:55 PM
*
* @public
* @async
* @param {{ relativePath: string; permanent?: boolean }} param0
* @param {string} param0.relativePath
* @param {boolean} [param0.permanent=false]
* @returns {Promise<void>}
*/
public async unlink({ relativePath, permanent = false }: { relativePath: string; permanent?: boolean }): Promise<void> {
const localPath = pathModule.join(this.sync.syncPair.localPath, relativePath)

Expand All @@ -355,17 +344,6 @@ export class LocalFileSystem {
})
}

/**
* Rename a file/directory inside the local sync path. Recursively creates intermediate directories if needed.
* @date 3/2/2024 - 12:41:15 PM
*
* @public
* @async
* @param {{ fromRelativePath: string; toRelativePath: string }} param0
* @param {string} param0.fromRelativePath
* @param {string} param0.toRelativePath
* @returns {Promise<fs.Stats>}
*/
public async rename({ fromRelativePath, toRelativePath }: { fromRelativePath: string; toRelativePath: string }): Promise<fs.Stats> {
const fromLocalPath = pathModule.join(this.sync.syncPair.localPath, fromRelativePath)
const toLocalPath = pathModule.join(this.sync.syncPair.localPath, toRelativePath)
Expand Down Expand Up @@ -535,6 +513,26 @@ export class LocalFileSystem {
delete this.sync.abortControllers[signalKey]
}
}

public async isPathWritable(path: string): Promise<boolean> {
try {
await fs.access(path, fs.constants.W_OK | fs.constants.R_OK)

return true
} catch {
return false
}
}

public async isPathReadable(path: string): Promise<boolean> {
try {
await fs.access(path, fs.constants.R_OK)

return true
} catch {
return false
}
}
}

export default LocalFileSystem
14 changes: 14 additions & 0 deletions src/lib/filesystems/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,20 @@ export class RemoteFileSystem {
delete this.sync.abortControllers[signalKey]
}
}

public async remotePathExisting(): Promise<boolean> {
try {
const present = await this.sync.sdk.api(3).dir().present({ uuid: this.sync.syncPair.remoteParentUUID })

if (!present.present || present.trash) {
return false
}

return true
} catch {
return false
}
}
}

export default RemoteFileSystem
66 changes: 65 additions & 1 deletion src/lib/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class Sync {
public mode: SyncMode
public excludeDotFiles: boolean
public readonly worker: SyncWorker
public removed: boolean = false

/**
* Creates an instance of Sync.
Expand Down Expand Up @@ -138,7 +139,14 @@ export class Sync {
this.isInitialized = true

try {
//local/remote smoke test
const [localSmokeTest, remoteSmokeTest] = await Promise.all([
this.localFileSystem.isPathWritable(this.syncPair.localPath),
this.remoteFileSystem.remotePathExisting()
])

if (!localSmokeTest || !remoteSmokeTest) {
throw new Error("Smoke tests failed. Either the local or remote path is not readable/writable")
}

await Promise.all([
this.localFileSystem.startDirectoryWatcher(),
Expand All @@ -158,6 +166,15 @@ export class Sync {
}

private async run(): Promise<void> {
if (this.removed) {
postMessageToMain({
type: "syncPairRemoved",
syncPair: this.syncPair
})

return
}

if (this.paused) {
postMessageToMain({
type: "cyclePaused",
Expand All @@ -181,6 +198,53 @@ export class Sync {
syncPair: this.syncPair
})

const [localSmokeTest, remoteSmokeTest] = await Promise.all([
this.localFileSystem.isPathWritable(this.syncPair.localPath),
this.remoteFileSystem.remotePathExisting()
])

if (!localSmokeTest) {
setTimeout(() => {
this.run()
}, SYNC_INTERVAL)

postMessageToMain({
type: "cycleLocalSmokeTestFailed",
syncPair: this.syncPair,
data: {
error: serializeError(new Error("Local path is not writable."))
}
})

postMessageToMain({
type: "cycleRestarting",
syncPair: this.syncPair
})

return
}

if (!remoteSmokeTest) {
setTimeout(() => {
this.run()
}, SYNC_INTERVAL)

postMessageToMain({
type: "cycleLocalSmokeTestFailed",
syncPair: this.syncPair,
data: {
error: serializeError(new Error("Remote path is not present or in the trash."))
}
})

postMessageToMain({
type: "cycleRestarting",
syncPair: this.syncPair
})

return
}

try {
postMessageToMain({
type: "cycleWaitingForLocalDirectoryChangesStarted",
Expand Down
Loading

0 comments on commit 9414e90

Please sign in to comment.