Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,30 @@ export class Hub {
constructor(conf: Config) {
this.conf = conf;
}
start() {
async start() {
for (const p of this.peers) {
p.stop();
}
this.peers = [];
const couchdbPeers: Peer[] = [];
const storagePeers: Peer[] = [];
for (const peer of this.conf.peers) {
if (peer.type == "couchdb") {
const p = new PeerCouchDB(peer, this.dispatch.bind(this));
this.peers.push(p);
couchdbPeers.push(p);
} else if (peer.type == "storage") {
const p = new PeerStorage(peer, this.dispatch.bind(this));
this.peers.push(p);
storagePeers.push(p);
} else {
throw new Error(`Unexpected Peer type: ${(peer as any)?.name} - ${(peer as any)?.type}`);
}
}
for (const p of this.peers) {
// Start CouchDB peers first and wait for them to be ready
await Promise.all(couchdbPeers.map(p => p.start()));
// Then start storage peers
for (const p of storagePeers) {
p.start();
}
}
Expand Down
20 changes: 20 additions & 0 deletions PeerStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { scheduleOnceIfDuplicated } from "octagonal-wheels/concurrency/lock";
import { DispatchFun, Peer } from "./Peer.ts";
import chokidar from "chokidar";
import { walk } from 'fs/walk';
import { minimatch } from "minimatch";

import { scheduleTask } from "octagonal-wheels/concurrency/task";

Expand All @@ -20,6 +21,16 @@ export class PeerStorage extends Peer {
super(conf, dispatcher);
}

shouldIgnore(path: string): boolean {
if (!this.config.ignore || this.config.ignore.length === 0) return false;
for (const pattern of this.config.ignore) {
if (minimatch(path, pattern, { dot: true })) {
return true;
}
}
return false;
}

async delete(pathSrc: string): Promise<boolean> {
const lp = this.toLocalPath(pathSrc);
const path = this.toStoragePath(lp);
Expand Down Expand Up @@ -157,6 +168,10 @@ export class PeerStorage extends Peer {
const lP = this.toStoragePath(this.toLocalPath("."));
const path = this.toPosixPath(relative(lP, pathSrc));

if (this.shouldIgnore(path)) {
return;
}

const data = await this.get(path);

if (data === false) return;
Expand All @@ -177,6 +192,11 @@ export class PeerStorage extends Peer {
async dispatchDeleted(pathSrc: string) {
const lP = this.toStoragePath(this.toLocalPath("."));
const path = this.toPosixPath(relative(lP, pathSrc));

if (this.shouldIgnore(path)) {
return;
}

await scheduleOnceIfDuplicated(pathSrc, async () => {
await delay(250);
if (!await this.isRepeating(path, false)) {
Expand Down
3 changes: 2 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"pouchdb-mapreduce": "npm:pouchdb-mapreduce@^9.0.0",
"transform-pouch": "npm:transform-pouch@^2.0.0",
"chokidar": "npm:chokidar@^3.5.1",
"trystero": "https://github.com/vrtmrz/vrtmrz/trystero#9e892a93ec14eeb57ce806d272fbb7c3935256d8"
"trystero": "https://esm.sh/gh/vrtmrz/trystero@9e892a93ec14eeb57ce806d272fbb7c3935256d8",
"node-fetch": "npm:node-fetch-native@^1.6.4"
},
"lint": {
"rules": {
Expand Down
18 changes: 18 additions & 0 deletions deno.lock

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

2 changes: 1 addition & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ try {
}
console.log("LiveSync Bridge is now started!");
const hub = new Hub(config);
hub.start();
await hub.start();
2 changes: 2 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface PeerStorageConf {
args: string[]
}
useChokidar?: boolean;
/** Glob patterns to ignore (e.g., ["**\/.git/**", "node_modules/**"]) */
ignore?: string[];
}
export interface PeerCouchDBConf extends DirectFileManipulatorOptions {
type: "couchdb";
Expand Down