Skip to content
Closed
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
7 changes: 4 additions & 3 deletions examples/callbacks/notify-fetch-data.callback.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { logger } from "examples/drive";
import { getInfoItem } from "examples/info-items-manager";

import { TFetchDataCallback } from "@/types/callbacks.type";
import { sleep } from "@/utils";

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

export const fetchDataCallback = async (id: string, callback: TCallback) => {
export const fetchDataCallback = async (id: string, callback: Parameters<TFetchDataCallback>[1]) => {
logger.info({ fn: "fetchDataCallback", id });
const path = await getInfoItem(id);

let finish = false;
Expand Down
3 changes: 1 addition & 2 deletions examples/get-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const { data } = z.object({ file: z.string() }).safeParse(argv);
if (data) {
const path = data.file;
const state = drive.getPlaceholderState(path);
const pendingStates = drive.getPlaceholderWithStatePending();
logger.info({ state, pendingStates });
logger.info({ state });
} else {
logger.error("Por favor especifica un archivo con --file <path>");
}
8 changes: 4 additions & 4 deletions examples/handlers/handle-change-size.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { drive, logger } from "examples/drive";
import { v4 } from "uuid";

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);
const id = v4();
drive.convertToPlaceholder(task.path, id);
drive.updateFileIdentity(task.path, id, false);
} catch (error) {
logger.error("handleChangeSize", error);
}
Expand Down
5 changes: 4 additions & 1 deletion examples/populate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { v4 } from "uuid";

import settings from "./settings";

const rootFile1 = join(settings.syncRootPath, v4());
const rootFileName1 = v4();
const rootZipFileName = `${v4()}.zip`;
const rootFile1 = join(settings.syncRootPath, rootFileName1);
const rootFile2ChangeSize = join(settings.syncRootPath, `change-size-${v4()}.txt`);
const rootFile3 = join(settings.syncRootPath, `${v4()}.txt`);
const rootFile3Moved = join(settings.syncRootPath, `moved-${v4()}.txt`);
Expand All @@ -22,6 +24,7 @@ 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(`cd ${settings.syncRootPath} && tar -cf ${rootZipFileName} ${rootFileName1}`); // Sync
execSync(`mv ${rootFile3} ${rootFile3Moved}`); // Sync
execSync(`mkdir ${rootFolder1}`); // Sync
execSync(`mkdir ${rootFolder2}`); // Cloud (no files inside)
Expand Down
23 changes: 12 additions & 11 deletions examples/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ const handlers = { handleAdd, handleHydrate, handleDehydrate, handleChangeSize }
const notify = { onTaskSuccess: async () => undefined, onTaskProcessing: async () => undefined };
const queueManager = new QueueManager(handlers, notify, settings.queuePersistPath);

drive.registerSyncRoot(settings.driveName, settings.driveVersion, settings.providerid, callbacks, settings.iconPath);
drive.connectSyncRoot();

try {
initInfoItems();
drive.watchAndWait(settings.syncRootPath, queueManager, settings.watcherLogPath);
} catch (error) {
logger.error(error);
drive.disconnectSyncRoot();
VirtualDrive.unregisterSyncRoot(settings.syncRootPath);
}
drive.registerSyncRoot(settings.driveName, settings.driveVersion, settings.providerid, callbacks, settings.iconPath).then(() => {
drive.connectSyncRoot();

try {
initInfoItems();
drive.watchAndWait(settings.syncRootPath, queueManager, settings.watcherLogPath);
} catch (error) {
logger.error(error);
drive.disconnectSyncRoot();
VirtualDrive.unregisterSyncRoot(settings.syncRootPath);
}
});
12 changes: 7 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Addon } from "./src/addon-wrapper";
import { QueueManager } from "./src/queue/queue-manager";
import { QueueItem, typeQueue, HandleAction, HandleActions } from "./src/queue/queueManager";
import VirtualDrive from "./src/virtual-drive";
import { Addon } from "@/addon-wrapper";
import { QueueManager } from "@/queue/queue-manager";
import { QueueItem, typeQueue, HandleAction, HandleActions } from "@/queue/queueManager";
import { Callbacks } from "@/types/callbacks.type";
import { PinState, SyncState } from "@/types/placeholder.type";
import VirtualDrive from "@/virtual-drive";

export { Addon, VirtualDrive, QueueItem, typeQueue, HandleAction, HandleActions, QueueManager };
export { Addon, VirtualDrive, QueueItem, typeQueue, HandleAction, HandleActions, QueueManager, Callbacks, PinState, SyncState };
19 changes: 0 additions & 19 deletions jest.config.js

This file was deleted.

11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"types": "dist/index.d.ts",
"scripts": {
"========== Testing ==========": "",
"test": "jest",
"vitest": "vitest",
"vitest:once": "yarn vitest --run",
"vitest:one": "yarn vitest related x",
"test": "vitest",
"test:once": "yarn vitest --run",
"test:one": "yarn vitest related x",
"========== Build ==========": "",
"clean": "node-gyp clean",
"build:gyp": "node-gyp configure build",
Expand All @@ -29,15 +28,11 @@
"gypfile": true,
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^5.2.1",
"@types/jest": "^29.5.12",
"@types/lodash.chunk": "^4.2.9",
"@types/node": "^20.5.0",
"@types/yargs": "^17.0.32",
"jest": "^29.7.0",
"jest-mock-extended": "^4.0.0-beta1",
"nodemon": "^3.1.9",
"prettier": "^3.4.2",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"tsc-alias": "^1.8.10",
"typescript": "^5.1.6",
Expand Down
5 changes: 0 additions & 5 deletions src/addon-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,4 @@ export class Addon {
const result = await addon.hydrateFile(path);
return this.parseAddonZod("hydrateFile", result);
}

getPlaceholderAttribute({ path }: { path: string }) {
const result = addon.getPlaceholderAttribute(path);
return this.parseAddonZod("getPlaceholderAttribute", result);
}
}
1 change: 0 additions & 1 deletion src/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export type TAddon = {
path: string,
): any;
hydrateFile(path: string): Promise<z.infer<typeof addonZod.hydrateFile>>;
getPlaceholderAttribute(path: string): z.infer<typeof addonZod.getPlaceholderAttribute>;
dehydrateFile(path: string): z.infer<typeof addonZod.dehydrateFile>;
connectSyncRoot(path: string, callbacks: InputSyncCallbacks): z.infer<typeof addonZod.connectSyncRoot>;
convertToPlaceholder(path: string, id: string): z.infer<typeof addonZod.convertToPlaceholder>;
Expand Down
5 changes: 0 additions & 5 deletions src/addon/addon-zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ export const addonZod = {
convertToPlaceholder: z.boolean(),
dehydrateFile: z.boolean(),
getFileIdentity: z.string(),
getPlaceholderAttribute: z.object({ attribute: z.union([z.literal(0), z.literal(1), z.literal(2)]) }).transform(({ attribute }) => {
if (attribute === 1) return "NOT_PINNED";
if (attribute === 2) return "PINNED";
return "OTHER";
}),
getPlaceholderState: z.object({ pinState: z.nativeEnum(PinState), syncState: z.nativeEnum(SyncState) }),
getPlaceholderWithStatePending: z.array(z.string()),
hydrateFile: z.undefined(),
Expand Down
30 changes: 30 additions & 0 deletions src/get-placeholder-states.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { readdir } from "fs/promises";
import { join } from "path";

import { isFileInDevice } from "./is-file-in-device";
import VirtualDrive from "./virtual-drive";

type TProps = {
self: VirtualDrive;
path: string;
};

export const getPlaceholderStates = async ({ self, path }: TProps) => {
const files = await readdir(path, { withFileTypes: true });

const promises = files.map(async (file) => {
const filePath = join(path, file.name);

if (file.isDirectory()) {
return getPlaceholderStates({ self, path: filePath });
} else {
const status = self.getPlaceholderState(filePath);
if (isFileInDevice(status)) {
const id = self.getFileIdentity(filePath);
self.watcher.fileInDevice.add(id);

Check failure on line 24 in src/get-placeholder-states.ts

View workflow job for this annotation

GitHub Actions / type-check

Property 'watcher' is private and only accessible within class 'VirtualDrive'.

Check failure on line 24 in src/get-placeholder-states.ts

View workflow job for this annotation

GitHub Actions / type-check

Property 'watcher' is private and only accessible within class 'VirtualDrive'.
}
}
});

await Promise.all(promises);
};
12 changes: 12 additions & 0 deletions src/is-file-in-device.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PinState, SyncState } from "./types/placeholder.type";

type TProps = {
syncState: SyncState;
pinState: PinState;
};

export const isFileInDevice = ({ syncState, pinState }: TProps) => {
const inSync = syncState === SyncState.InSync;
const isHydrated = pinState === PinState.AlwaysLocal || pinState === PinState.Unspecified;
return inSync && isHydrated;
};
7 changes: 6 additions & 1 deletion src/types/callbacks.type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
export type NapiCallbackFunction = (...args: any[]) => any;

export type TFetchDataCallback = (
id: string,
callback: (data: boolean, path: string, errorHandler?: () => void) => Promise<{ finished: boolean; progress: number }>,
) => void;

export type InputSyncCallbacks = {
fetchDataCallback?: NapiCallbackFunction;
fetchDataCallback: TFetchDataCallback;
validateDataCallback?: NapiCallbackFunction;
cancelFetchDataCallback?: NapiCallbackFunction;
fetchPlaceholdersCallback?: NapiCallbackFunction;
Expand Down
14 changes: 0 additions & 14 deletions src/types/placeholder.type.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
// Enum para SyncState
export enum SyncState {
Undefined = -1,
NotInSync = 0,
InSync = 1,
}

// Enum para PinState
export enum PinState {
Inherited = 0,
AlwaysLocal = 1,
OnlineOnly = 2,
Unspecified = 3,
Excluded = 4,
}

// Tipo para combinar ambos estados en un objeto de estado
export type Status = {
pinState: PinState;
syncState: SyncState;
};

export enum Attributes {
OTHER = 0,
NOT_PINNED = 1,
PINNED = 2,
}
Loading