Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
/examples/tmp
/node_modules
/node-win.log
/package-lock.json
/test-files
/venv
8 changes: 4 additions & 4 deletions examples/callbacks/cancel-fetch-data.callback.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
async function onCancelFetchDataCallback(fileId: string) {
console.log("[EXAMPLE] cancel fetch data: ", fileId);
}
import { logger } from "@/logger";

export default onCancelFetchDataCallback;
export const cancelFetchDataCallback = (fileId: string) => {
logger.info({ event: "cancelFetchDataCallback", fileId });
};
3 changes: 0 additions & 3 deletions examples/callbacks/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion examples/callbacks/notify-delete.callback.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { logger } from "@/logger";

export const notifyDeleteCallback = (fileId: string, callback: (response: boolean) => void) => {
logger.info({ event: "onDelete", fileId });
logger.info({ event: "notifyDeleteCallback", fileId });
callback(true);
};
4 changes: 2 additions & 2 deletions examples/callbacks/notify-fetch-data.callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { getInfoItem } from "examples/info-items-manager";

import { sleep } from "@/utils";

type CallbackResponse = (data: boolean, path: string, errorHandler?: () => void) => Promise<{ finished: boolean; progress: number }>;
type TCallback = (data: boolean, path: string, errorHandler?: () => void) => Promise<{ finished: boolean; progress: number }>;

export const fetchDataCallback = async (id: string, callback: CallbackResponse) => {
export const fetchDataCallback = async (id: string, callback: TCallback) => {
const path = await getInfoItem(id);

let finish = false;
Expand Down
20 changes: 5 additions & 15 deletions examples/callbacks/notify-message.callback.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
async function onMessageCallback(
message: string,
action: string,
errorName: string,
callback: (response: boolean) => void) {
try {
console.log("[EXAMPLE] Message received: ", message);
console.log("[EXAMPLE] Action: ", action);
console.log("[EXAMPLE] Error name: ", errorName);
await callback(true);
} catch (error) {
callback(false);
}
}
import { logger } from "@/logger";

export default onMessageCallback;
export const notifyMessageCallback = (message: string, action: string, errorName: string, callback: (response: boolean) => void) => {
logger.info({ event: "notifyMessageCallback", message, action, errorName });
callback(true);
};
31 changes: 5 additions & 26 deletions examples/callbacks/notify-rename.callback.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
async function onRenameCallback(newName: string, fileId: string): Promise<boolean> {
console.log("[EXAMPLE] File ID: " + fileId);
console.log("[EXAMPLE] New name: " + newName);
import { logger } from "@/logger";

const a = await (new Promise<boolean>((resolve, reject) => {
try {

setTimeout(() => {
resolve(true);
}, 1000)
} catch (err) {
reject(err);
}
}));

return a;
}

function onRenameCallbackWithCallback(newName: string, fileId: string, responseCallback: (response: boolean) => void) {
onRenameCallback(newName, fileId).then((response) => {
responseCallback(response);
}).catch((err) => {
responseCallback(false);
});
}

export default onRenameCallbackWithCallback;
export const notifyRenameCallback = (newName: string, fileId: string, callback: (response: boolean) => void) => {
logger.info({ event: "notifyRenameCallback", newName, fileId });
callback(true);
};
6 changes: 2 additions & 4 deletions examples/drive.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import VirtualDrive from "@/virtual-drive";

import settings from "./settings";

export const drive = new VirtualDrive(
settings.syncRootPath,
settings.defaultLogPath
);
export const drive = new VirtualDrive(settings.syncRootPath, settings.defaultLogPath);
22 changes: 12 additions & 10 deletions examples/get-state.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { drive } from "./drive";
import yargs from "yargs";
import z from "zod";

import { logger } from "@/logger";

import { drive } from "./drive";

// Configura yargs
const argv = yargs
.command("file", "El path del archivo para obtener el estado", {
path: {
Expand All @@ -13,14 +16,13 @@ const argv = yargs
.help()
.alias("help", "h").argv;

//@ts-ignore
if (argv.file) {
//@ts-ignore
const path = argv.file;
const { data } = z.object({ file: z.string() }).safeParse(argv);

if (data) {
const path = data.file;
const state = drive.getPlaceholderState(path);
console.log(`${path} state:`, state);
const states = drive.getPlaceholderWithStatePending();
console.log(`states:`, states);
const pendingStates = drive.getPlaceholderWithStatePending();
logger.info({ state, pendingStates });
} else {
console.log("Por favor especifica un archivo con --file <path>");
console.error("Por favor especifica un archivo con --file <path>");
}
16 changes: 16 additions & 0 deletions examples/handlers/handle-add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { drive } from "examples/drive";
import { addInfoItem } from "examples/info-items-manager";

import { logger } from "@/logger";
import { QueueItem } from "@/queue/queueManager";
import { v4 } from "uuid";

export const handleAdd = async (task: QueueItem) => {
try {
logger.info({ fn: "handleAdd", task });
const id = task.isFolder ? v4() : addInfoItem(task.path);
drive.convertToPlaceholder(task.path, id);
} catch (error) {
logger.error(error, "handleAdd");
}
};
16 changes: 16 additions & 0 deletions examples/handlers/handle-change-size.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { drive } from "examples/drive";

import { logger } from "@/logger";
import { QueueItem } from "@/queue/queueManager";

export const handleChangeSize = async (task: QueueItem) => {
try {
logger.info({ fn: "handleChangeSize", path: task.path });
const result = Math.random().toString(36).substring(2, 7);
drive.convertToPlaceholder(task.path, result);
drive.updateFileIdentity(task.path, result, false);
drive.updateSyncStatus(task.path, task.isFolder, true);
} catch (error) {
logger.error(error, "handleChangeSize");
}
};
13 changes: 13 additions & 0 deletions examples/handlers/handle-dehydrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { drive } from "examples/drive";

import { logger } from "@/logger";
import { QueueItem } from "@/queue/queueManager";

export const handleDehydrate = async (task: QueueItem) => {
try {
logger.info({ fn: "handleDehydrate", path: task.path });
drive.dehydrateFile(task.path);
} catch (error) {
logger.error(error, "handleDehydrate");
}
};
13 changes: 13 additions & 0 deletions examples/handlers/handle-hydrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { drive } from "examples/drive";

import { logger } from "@/logger";
import { QueueItem } from "@/queue/queueManager";

export const handleHydrate = async (task: QueueItem) => {
try {
logger.info({ fn: "handleHydrate", path: task.path });
await drive.hydrateFile(task.path);
} catch (error) {
logger.error(error, "handleHydrate");
}
};
34 changes: 17 additions & 17 deletions examples/info-items-manager.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
import { existsSync } from "fs";
import { copyFile, mkdir, readFile, writeFile } from "fs/promises";
import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { basename, join } from "path";
import { v4 } from "uuid";

import { TMP_PATH } from "./settings";

const infoItemsPath = join(TMP_PATH, "info-items.json");
const serverPath = join(TMP_PATH, "fake-server");

export const initInfoItems = async () => {
export const initInfoItems = () => {
if (!existsSync(infoItemsPath)) {
await writeFile(infoItemsPath, JSON.stringify({}));
writeFileSync(infoItemsPath, JSON.stringify({}));
}

if (!existsSync(serverPath)) {
await mkdir(serverPath);
mkdirSync(serverPath);
}
};

export const getInfoItems = async () => {
return JSON.parse(await readFile(infoItemsPath, "utf8"));
export const getInfoItems = () => {
return JSON.parse(readFileSync(infoItemsPath, "utf8"));
};

export const deleteInfoItems = async () => {
await writeFile(infoItemsPath, JSON.stringify({}));
export const deleteInfoItems = () => {
writeFileSync(infoItemsPath, JSON.stringify({}));
};

export const addInfoItem = async (itemPath: string) => {
export const addInfoItem = (itemPath: string) => {
const fileName = basename(itemPath);
const serverItemPath = join(serverPath, fileName);
await copyFile(itemPath, serverItemPath);
copyFileSync(itemPath, serverItemPath);

const id = v4();
const infoItems = await getInfoItems();
const infoItems = getInfoItems();
infoItems[id] = serverItemPath;
await writeFile(infoItemsPath, JSON.stringify(infoItems, null, 2));

writeFileSync(infoItemsPath, JSON.stringify(infoItems, null, 2));
return id;
};

export const getInfoItem = async (id: string) => {
const infoItems = await getInfoItems();
export const getInfoItem = (id: string) => {
const infoItems = getInfoItems();
return infoItems[id];
};
30 changes: 30 additions & 0 deletions examples/populate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import VirtualDrive from "@/virtual-drive";

import { execSync } from "child_process";
import { join } from "path";
import { v4 } from "uuid";

import settings from "./settings";

const rootFile1 = join(settings.syncRootPath, v4());
const rootFile2ChangeSize = join(settings.syncRootPath, `change-size-${v4()}.txt`);
const rootFile3 = join(settings.syncRootPath, `${v4()}.txt`);
const rootFile3Moved = join(settings.syncRootPath, `moved-${v4()}.txt`);
const rootFile4 = join(settings.syncRootPath, `${v4()}.txt`);
const rootFolder1 = join(settings.syncRootPath, v4());
const rootFolder2 = join(settings.syncRootPath, v4());
const folder1File1 = join(rootFolder1, `${v4()}.pdf`);
const folder1Folder1 = join(rootFolder1, v4());
const folder1Folder1File1 = join(folder1Folder1, `${v4()}.xlsx`);

execSync(`echo Hello, world! > ${rootFile1}`); // Sync
execSync(`echo Hello, world! > ${rootFile2ChangeSize}`);
execSync(`echo Hello, world! >> ${rootFile2ChangeSize}`); // Sync
execSync(`echo Hello, world! > ${rootFile3}`);
execSync(`type nul > ${rootFile4}`); // No sync (0 bytes)
execSync(`mv ${rootFile3} ${rootFile3Moved}`); // Sync
execSync(`mkdir ${rootFolder1}`); // Sync
execSync(`mkdir ${rootFolder2}`); // Cloud (no files inside)
execSync(`echo Hello, world! > ${folder1File1}`); // Sync
execSync(`mkdir ${folder1Folder1}`); // Sync
execSync(`echo Hello, world! > ${folder1Folder1File1}`); // Sync
Loading