Skip to content

Commit

Permalink
feat: serialize ipc
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Jun 29, 2024
1 parent 76fccb5 commit 6d2a2e9
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 29 deletions.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isMainThread, parentPort } from "worker_threads"
import { postMessageToMain } from "./lib/ipc"
import { Semaphore } from "./semaphore"
import { SYNC_INTERVAL } from "./constants"
import { serializeError } from "./utils"

/**
* SyncWorker
Expand Down Expand Up @@ -71,7 +72,9 @@ export class SyncWorker {
if (e instanceof Error) {
postMessageToMain({
type: "error",
data: e
data: {
error: serializeError(e)
}
})
}
}
Expand Down Expand Up @@ -150,4 +153,5 @@ export class SyncWorker {
}
}

export * from "./utils"
export default SyncWorker
13 changes: 8 additions & 5 deletions src/lib/filesystems/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
promiseAllSettledChunked,
isRelativePathIgnoredByDefault,
isDirectoryPathIgnoredByDefault,
isSystemPathIgnoredByDefault
isSystemPathIgnoredByDefault,
serializeError
} from "../../utils"
import pathModule from "path"
import process from "process"
Expand Down Expand Up @@ -259,15 +260,17 @@ export class LocalFileSystem {
* @returns {Promise<void>}
*/
public async waitForLocalDirectoryChanges(): Promise<void> {
const waitTimeout = SYNC_INTERVAL * 2

await new Promise<void>(resolve => {
if (Date.now() > this.lastDirectoryChangeTimestamp + SYNC_INTERVAL) {
if (Date.now() > this.lastDirectoryChangeTimestamp + waitTimeout) {
resolve()

return
}

const wait = setInterval(() => {
if (Date.now() > this.lastDirectoryChangeTimestamp + SYNC_INTERVAL) {
if (Date.now() > this.lastDirectoryChangeTimestamp + waitTimeout) {
clearInterval(wait)

resolve()
Expand Down Expand Up @@ -456,7 +459,7 @@ export class LocalFileSystem {
type: "error",
relativePath,
localPath,
error: err
error: serializeError(err)
}
})
},
Expand Down Expand Up @@ -523,7 +526,7 @@ export class LocalFileSystem {
type: "error",
relativePath,
localPath,
error: e
error: serializeError(e)
}
})
}
Expand Down
7 changes: 4 additions & 3 deletions src/lib/filesystems/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
isNameOverMaxLength,
isValidPath,
isDirectoryPathIgnoredByDefault,
isRelativePathIgnoredByDefault
isRelativePathIgnoredByDefault,
serializeError
} from "../../utils"
import { v4 as uuidv4 } from "uuid"
import { LOCAL_TRASH_NAME } from "../../constants"
Expand Down Expand Up @@ -812,7 +813,7 @@ export class RemoteFileSystem {
type: "error",
relativePath,
localPath,
error: err
error: serializeError(err)
}
})
},
Expand Down Expand Up @@ -871,7 +872,7 @@ export class RemoteFileSystem {
type: "error",
relativePath,
localPath,
error: e
error: serializeError(e)
}
})
}
Expand Down
39 changes: 30 additions & 9 deletions src/lib/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import State from "./state"
import { postMessageToMain } from "./ipc"
import { isMainThread, parentPort } from "worker_threads"
import Ignorer from "../ignorer"
import { serializeError } from "../utils"

/**
* Sync
Expand Down Expand Up @@ -106,9 +107,9 @@ export class Sync {

pauseSignal.resume()
} else if (message.type === "updateLocalIgnorer" && message.syncPair.uuid === this.syncPair.uuid) {
this.localIgnorer.update(message.data).catch(console.error)
this.localIgnorer.update(message.data?.content).catch(console.error)
} else if (message.type === "updateRemoteIgnorer" && message.syncPair.uuid === this.syncPair.uuid) {
this.remoteIgnorer.update(message.data).catch(console.error)
this.remoteIgnorer.update(message.data?.content).catch(console.error)
} else if (message.type === "pauseSyncPair" && message.syncPair.uuid === this.syncPair.uuid) {
this.paused = true
} else if (message.type === "resumeSyncPair" && message.syncPair.uuid === this.syncPair.uuid) {
Expand Down Expand Up @@ -204,19 +205,28 @@ export class Sync {
postMessageToMain({
type: "localTreeErrors",
syncPair: this.syncPair,
data: currentLocalTree.errors
data: {
errors: currentLocalTree.errors.map(e => ({
...e,
error: serializeError(e.error)
}))
}
})

postMessageToMain({
type: "localTreeIgnored",
syncPair: this.syncPair,
data: currentLocalTree.ignored
data: {
ignored: currentLocalTree.ignored
}
})

postMessageToMain({
type: "remoteTreeIgnored",
syncPair: this.syncPair,
data: currentRemoteTree.ignored
data: {
ignored: currentRemoteTree.ignored
}
})

postMessageToMain({
Expand All @@ -240,7 +250,9 @@ export class Sync {
postMessageToMain({
type: "deltas",
syncPair: this.syncPair,
data: deltas
data: {
deltas
}
})

console.log({ deltas, localErrors: currentLocalTree.errors })
Expand All @@ -263,8 +275,15 @@ export class Sync {
type: "doneTasks",
syncPair: this.syncPair,
data: {
tasks: doneTasks,
errors
tasks: doneTasks.map(task => ({
path: task.path,
type: task.type,
...(task.type === "uploadFile" ? { item: task.item } : {})
})),
errors: errors.map(e => ({
...e,
error: serializeError(e.error)
}))
}
})

Expand Down Expand Up @@ -315,7 +334,9 @@ export class Sync {
postMessageToMain({
type: "cycleError",
syncPair: this.syncPair,
data: e
data: {
error: serializeError(e)
}
})
}
} finally {
Expand Down
39 changes: 28 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type LocalTreeError, type LocalTreeIgnored } from "./lib/filesystems/lo
import { type Delta } from "./lib/deltas"
import { type DoneTask, type TaskError } from "./lib/tasks"
import { type RemoteTreeIgnored } from "./lib/filesystems/remote"
import { type SerializedError } from "./utils"

export type SyncMode = "twoWay" | "localToCloud" | "localBackup" | "cloudToLocal" | "cloudBackup"

Expand Down Expand Up @@ -78,30 +79,38 @@ export type SyncMessage =
type: "error"
relativePath: string
localPath: string
error: Error
error: SerializedError
}
}
| {
type: "localTreeErrors"
data: LocalTreeError[]
data: {
errors: Prettify<Omit<LocalTreeError, "error"> & { error: SerializedError }>[]
}
}
| {
type: "localTreeIgnored"
data: LocalTreeIgnored[]
data: {
ignored: LocalTreeIgnored[]
}
}
| {
type: "remoteTreeIgnored"
data: RemoteTreeIgnored[]
data: {
ignored: RemoteTreeIgnored[]
}
}
| {
type: "deltas"
data: Delta[]
data: {
deltas: Delta[]
}
}
| {
type: "doneTasks"
data: {
tasks: DoneTask[]
errors: TaskError[]
tasks: Prettify<Omit<DoneTask, "stats">>[]
errors: Prettify<Omit<TaskError, "error"> & { error: SerializedError }>[]
}
}
| {
Expand All @@ -112,7 +121,9 @@ export type SyncMessage =
}
| {
type: "cycleError"
data: Error
data: {
error: SerializedError
}
}
| {
type: "cycleSuccess"
Expand Down Expand Up @@ -190,20 +201,26 @@ export type SyncMessage =
}
| {
type: "error"
data: Error
data: {
error: SerializedError
}
}
| {
type: "syncPairsUpdated"
}
| {
type: "updateLocalIgnorer"
syncPair: SyncPair
data?: string
data?: {
content?: string
}
}
| {
type: "updateRemoteIgnorer"
syncPair: SyncPair
data?: string
data?: {
content?: string
}
}
| {
type: "resetSyncPairCache"
Expand Down
25 changes: 25 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,28 @@ export const isDirectoryPathIgnoredByDefault = memoize((path: string): boolean =

return false
})

export type SerializedError = {
name: string
message: string
stack?: string
stringified: string
}

export function serializeError(error: Error): SerializedError {
return {
name: error.name,
message: error.message,
stack: error.stack,
stringified: `${error.name}: ${error.message}`
}
}

export function deserializeError(serializedError: SerializedError): Error {
const error = new Error(serializedError.message)

error.name = serializedError.name
error.stack = serializedError.stack

return error
}

0 comments on commit 6d2a2e9

Please sign in to comment.