@thedriveai/device-helper 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +101 -61
- package/dist/core.d.ts +741 -0
- package/dist/core.js +1591 -0
- package/dist/lib.d.ts +70 -5
- package/dist/lib.js +80 -40
- package/package.json +16 -3
package/dist/lib.d.ts
CHANGED
|
@@ -69,13 +69,22 @@ interface CatalogChange {
|
|
|
69
69
|
export declare const DEVICE_PROTOCOL_VERSION = 2;
|
|
70
70
|
/** The socket.io namespace devices connect to. */
|
|
71
71
|
export declare const DEVICE_NAMESPACE = "/devices";
|
|
72
|
-
|
|
72
|
+
/**
|
|
73
|
+
* What the helper runs on. `ios` and `android` are the mobile app sharing a picked folder (DEVICE_MOUNTS.md §28): the
|
|
74
|
+
* same protocol, answered only while the app is open.
|
|
75
|
+
*/
|
|
76
|
+
export type DevicePlatform = "darwin" | "linux" | "win32" | "docker" | "ios" | "android";
|
|
73
77
|
/** A folder the helper shares, as it announces it. The id is minted by the helper and survives a path change. */
|
|
74
78
|
export interface DeviceRoot {
|
|
75
79
|
id: string;
|
|
76
80
|
label: string;
|
|
77
81
|
/** Whether a delete here goes to a trash the person can recover from (Finder, the Recycle Bin, a --trash-dir). */
|
|
78
82
|
trash: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* `folder`: that trash is DEVICE_TRASH_FOLDER inside the shared folder itself (a phone), so the person is told to
|
|
85
|
+
* look there rather than in an OS trash. Absent = the host's own trash.
|
|
86
|
+
*/
|
|
87
|
+
trashIn?: "folder";
|
|
79
88
|
/** The helper refuses every write to this root. */
|
|
80
89
|
readOnly: boolean;
|
|
81
90
|
/**
|
|
@@ -99,11 +108,17 @@ export interface DeviceRoot {
|
|
|
99
108
|
}
|
|
100
109
|
export interface DeviceHello {
|
|
101
110
|
protocol: number;
|
|
111
|
+
/**
|
|
112
|
+
* `push` (a phone only): the install's Expo push token. With it the server can wake THIS phone when work needs
|
|
113
|
+
* its folders and the app is not open (DEVICE_MOUNTS.md §29). Honoured only while its owner has the same token
|
|
114
|
+
* registered for the assistant's pushes; never shown to anyone.
|
|
115
|
+
*/
|
|
102
116
|
device: {
|
|
103
117
|
id: string;
|
|
104
118
|
name: string;
|
|
105
119
|
platform: DevicePlatform;
|
|
106
120
|
helperVersion: string;
|
|
121
|
+
push?: string;
|
|
107
122
|
};
|
|
108
123
|
roots: DeviceRoot[];
|
|
109
124
|
/**
|
|
@@ -517,6 +532,44 @@ declare class RootIdentityStore {
|
|
|
517
532
|
/** Forget the record: the next probe records whatever stands at the path. */
|
|
518
533
|
forget(): void;
|
|
519
534
|
}
|
|
535
|
+
type OpErrorCode = Exclude<RpcErrorCode, "RESET">;
|
|
536
|
+
declare class OpError extends Error {
|
|
537
|
+
readonly code: OpErrorCode;
|
|
538
|
+
constructor(code: OpErrorCode, message: string);
|
|
539
|
+
}
|
|
540
|
+
interface ReadOpened {
|
|
541
|
+
size: number | null;
|
|
542
|
+
/** What to call the file in a log line. */
|
|
543
|
+
label: string;
|
|
544
|
+
/** The bytes, in order. Iterated once; stopping early (the server aborted, the line dropped) releases the file. */
|
|
545
|
+
chunks(chunkBytes: number): AsyncIterable<Uint8Array>;
|
|
546
|
+
}
|
|
547
|
+
interface RootPort {
|
|
548
|
+
/** The change feed's last durable position, as the nudge to the server carries it. */
|
|
549
|
+
readonly position: string;
|
|
550
|
+
stat(params: RpcParams["stat"]): Promise<RpcResults["stat"]>;
|
|
551
|
+
list(params: RpcParams["list"]): Promise<RpcResults["list"]>;
|
|
552
|
+
enumerate(params: RpcParams["enumerate"]): Promise<RpcResults["enumerate"]>;
|
|
553
|
+
changes(params: RpcParams["changes"]): Promise<RpcResults["changes"]>;
|
|
554
|
+
mkdir(params: RpcParams["mkdir"]): Promise<RpcResults["mkdir"]>;
|
|
555
|
+
move(params: RpcParams["move"]): Promise<RpcResults["move"]>;
|
|
556
|
+
copy(params: RpcParams["copy"]): Promise<RpcResults["copy"]>;
|
|
557
|
+
trash(params: RpcParams["trash"]): Promise<RpcResults["trash"]>;
|
|
558
|
+
write(params: RpcParams["write"]): Promise<RpcResults["write"]>;
|
|
559
|
+
openWrite(params: RpcParams["write_open"]): Promise<RpcResults["write_open"]>;
|
|
560
|
+
/** One chunk, in order; true once it is kept (the server's permission to send the next). */
|
|
561
|
+
appendWrite(chunk: {
|
|
562
|
+
stream: string;
|
|
563
|
+
seq: number;
|
|
564
|
+
data: Uint8Array | ArrayBuffer;
|
|
565
|
+
}): Promise<boolean>;
|
|
566
|
+
closeWrite(params: RpcParams["write_close"]): Promise<RpcResults["write_close"]>;
|
|
567
|
+
/** The server gave up, or the line went: what was written so far is dropped, the destination untouched. */
|
|
568
|
+
abortWrite(stream: string): Promise<void>;
|
|
569
|
+
hash(params: RpcParams["hash"]): Promise<RpcResults["hash"]>;
|
|
570
|
+
media(params: RpcParams["media"]): Promise<RpcResults["media"]>;
|
|
571
|
+
openRead(params: RpcParams["read"]): ReadOpened | Promise<ReadOpened>;
|
|
572
|
+
}
|
|
520
573
|
type RootState = "ok" | "denied" | "missing";
|
|
521
574
|
interface RootRuntime {
|
|
522
575
|
config: RootConfig;
|
|
@@ -621,7 +674,7 @@ export declare class RootOps {
|
|
|
621
674
|
*/
|
|
622
675
|
media(params: RpcParams["media"]): Promise<RpcResults["media"]>;
|
|
623
676
|
/** Open a file for streaming: the stream itself is driven by the client (chunks with acks). */
|
|
624
|
-
openRead(params: RpcParams["read"]): {
|
|
677
|
+
openRead(params: RpcParams["read"]): ReadOpened & {
|
|
625
678
|
abs: string;
|
|
626
679
|
size: number;
|
|
627
680
|
name: string;
|
|
@@ -698,7 +751,7 @@ declare class RootWatcher {
|
|
|
698
751
|
/** Tests: wait for queued observations. */
|
|
699
752
|
settle(): Promise<void>;
|
|
700
753
|
}
|
|
701
|
-
export declare const HELPER_VERSION = "0.
|
|
754
|
+
export declare const HELPER_VERSION = "0.2.0";
|
|
702
755
|
interface LiveRoot {
|
|
703
756
|
announce: DeviceRoot;
|
|
704
757
|
ops: RootOps;
|
|
@@ -749,7 +802,7 @@ interface DeviceClientDeps {
|
|
|
749
802
|
device: DeviceHello["device"];
|
|
750
803
|
roots: () => Array<{
|
|
751
804
|
announce: DeviceRoot;
|
|
752
|
-
ops:
|
|
805
|
+
ops: RootPort;
|
|
753
806
|
}>;
|
|
754
807
|
/** Roots the person stopped sharing, until the server has heard it (§17). */
|
|
755
808
|
retired?: () => string[];
|
|
@@ -763,8 +816,12 @@ interface DeviceClientDeps {
|
|
|
763
816
|
reconnectAfterMs?: number;
|
|
764
817
|
/** How long a burst of changes on one root is let settle before the server is nudged. */
|
|
765
818
|
changeDebounceMs?: number;
|
|
819
|
+
/** Anything a root threw, as the protocol's error. A host with its own error vocabulary (Node's errno) maps it here. */
|
|
820
|
+
toError?: (error: unknown) => OpError;
|
|
821
|
+
/** A stream id for a read an older server did not name (a uuid). Today's server always names its own. */
|
|
822
|
+
newStreamId?: () => string;
|
|
766
823
|
}
|
|
767
|
-
|
|
824
|
+
declare class CoreDeviceClient {
|
|
768
825
|
private readonly deps;
|
|
769
826
|
private readonly replies;
|
|
770
827
|
private readonly aborted;
|
|
@@ -792,9 +849,17 @@ export declare class DeviceClient {
|
|
|
792
849
|
*/
|
|
793
850
|
handle(request: RpcRequest): Promise<RpcResult<unknown>>;
|
|
794
851
|
private dispatch;
|
|
852
|
+
/**
|
|
853
|
+
* The bytes of one read, a chunk at a time, each sent only once the server acknowledged the one before. One chunk
|
|
854
|
+
* is held back so the last can say it is the last (an empty file sends one empty, last chunk). Leaving the loop
|
|
855
|
+
* early (an abort, a dead line) ends the iteration, which is what releases the file.
|
|
856
|
+
*/
|
|
795
857
|
private pump;
|
|
796
858
|
private send;
|
|
797
859
|
}
|
|
860
|
+
export declare class DeviceClient extends CoreDeviceClient {
|
|
861
|
+
constructor(deps: DeviceClientDeps);
|
|
862
|
+
}
|
|
798
863
|
export declare function rootHasTrash(root: RootConfig): boolean;
|
|
799
864
|
|
|
800
865
|
export {};
|
package/dist/lib.js
CHANGED
|
@@ -48,7 +48,7 @@ __export(lib_exports, {
|
|
|
48
48
|
module.exports = __toCommonJS(lib_exports);
|
|
49
49
|
|
|
50
50
|
// src/index.ts
|
|
51
|
-
var
|
|
51
|
+
var import_fs14 = __toESM(require("fs"));
|
|
52
52
|
var import_socket = require("socket.io-client");
|
|
53
53
|
|
|
54
54
|
// src/config.ts
|
|
@@ -60,11 +60,12 @@ var import_crypto = require("crypto");
|
|
|
60
60
|
// src/nasFolders.ts
|
|
61
61
|
var import_fs = __toESM(require("fs"));
|
|
62
62
|
var import_path = __toESM(require("path"));
|
|
63
|
+
|
|
64
|
+
// src/nasNames.ts
|
|
63
65
|
var NAS_RECYCLE_BINS = [
|
|
64
66
|
{ vendor: "QNAP", name: "@Recycle" },
|
|
65
67
|
{ vendor: "Synology", name: "#recycle" }
|
|
66
68
|
];
|
|
67
|
-
var FALLBACK_BIN = NAS_RECYCLE_BINS.find((b) => b.vendor === "Synology");
|
|
68
69
|
var NAS_SNAPSHOT_FOLDERS = ["#snapshot", "@Recently-Snapshot"];
|
|
69
70
|
var HOUSEKEEPING = new Set([...NAS_RECYCLE_BINS.map((b) => b.name), ...NAS_SNAPSHOT_FOLDERS].map((n) => n.toLowerCase()));
|
|
70
71
|
function isNasHousekeepingName(name) {
|
|
@@ -73,6 +74,9 @@ function isNasHousekeepingName(name) {
|
|
|
73
74
|
function isNasRecycleBinName(name) {
|
|
74
75
|
return NAS_RECYCLE_BINS.some((b) => b.name.toLowerCase() === name.toLowerCase());
|
|
75
76
|
}
|
|
77
|
+
|
|
78
|
+
// src/nasFolders.ts
|
|
79
|
+
var FALLBACK_BIN = NAS_RECYCLE_BINS.find((b) => b.vendor === "Synology");
|
|
76
80
|
function shareRecycleBin(rootPath, itemAbs) {
|
|
77
81
|
let root = import_path.default.resolve(rootPath);
|
|
78
82
|
try {
|
|
@@ -185,6 +189,15 @@ var import_path5 = __toESM(require("path"));
|
|
|
185
189
|
// src/fsitems.ts
|
|
186
190
|
var import_fs3 = __toESM(require("fs"));
|
|
187
191
|
var import_path3 = __toESM(require("path"));
|
|
192
|
+
|
|
193
|
+
// src/ignoredNames.ts
|
|
194
|
+
var IGNORED_NAMES = /* @__PURE__ */ new Set([".DS_Store", "Thumbs.db", "desktop.ini", "@eaDir", "$RECYCLE.BIN", "System Volume Information", ".Trashes", ".Trash", ".Spotlight-V100", ".fseventsd", ".TemporaryItems", "lost+found", "node_modules", ".git", "__pycache__", "site-packages", "bower_components"]);
|
|
195
|
+
function isIgnoredFileName(name, backslashSeparates = true) {
|
|
196
|
+
if (backslashSeparates && name.includes("\\")) return true;
|
|
197
|
+
return name.startsWith(".") || IGNORED_NAMES.has(name) || isNasHousekeepingName(name) || name.startsWith("~$") || name.endsWith(".crdownload") || name.endsWith(".part") || name.endsWith(".tmp");
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/fsitems.ts
|
|
188
201
|
var PathError = class extends Error {
|
|
189
202
|
constructor(code, message) {
|
|
190
203
|
super(message);
|
|
@@ -192,12 +205,10 @@ var PathError = class extends Error {
|
|
|
192
205
|
this.name = "PathError";
|
|
193
206
|
}
|
|
194
207
|
};
|
|
195
|
-
var IGNORED_NAMES = /* @__PURE__ */ new Set([".DS_Store", "Thumbs.db", "desktop.ini", "@eaDir", "$RECYCLE.BIN", "System Volume Information", ".Trashes", ".Trash", ".Spotlight-V100", ".fseventsd", ".TemporaryItems", "lost+found", "node_modules", ".git", "__pycache__", "site-packages", "bower_components"]);
|
|
196
208
|
var TRASH_MARKER = ".thedrive-trash";
|
|
197
209
|
var DEPENDENCY_MARKERS = ["pyvenv.cfg", TRASH_MARKER];
|
|
198
210
|
function isIgnoredName(name) {
|
|
199
|
-
|
|
200
|
-
return name.startsWith(".") || IGNORED_NAMES.has(name) || isNasHousekeepingName(name) || name.startsWith("~$") || name.endsWith(".crdownload") || name.endsWith(".part") || name.endsWith(".tmp");
|
|
211
|
+
return isIgnoredFileName(name, process.platform !== "win32");
|
|
201
212
|
}
|
|
202
213
|
function isDependencyFolder(abs) {
|
|
203
214
|
for (const marker of DEPENDENCY_MARKERS) if (import_fs3.default.existsSync(import_path3.default.join(abs, marker))) return true;
|
|
@@ -1563,7 +1574,7 @@ async function trashItem(root, abs, devicePath) {
|
|
|
1563
1574
|
}
|
|
1564
1575
|
if (root.trash !== "os" || process.env.TDAI_DOCKER === "1") throw new Error("this folder has no trash");
|
|
1565
1576
|
if (process.platform === "darwin") {
|
|
1566
|
-
await runFile("osascript", ["-e", "on run argv", "-e",
|
|
1577
|
+
await runFile("osascript", ["-e", "on run argv", "-e", "set trashedItem to (POSIX file (item 1 of argv)) as alias", "-e", 'tell application "Finder" to delete trashedItem', "-e", "end run", abs]);
|
|
1567
1578
|
return;
|
|
1568
1579
|
}
|
|
1569
1580
|
if (process.platform === "linux") {
|
|
@@ -1596,7 +1607,7 @@ async function markDownloaded(abs, platform = process.platform) {
|
|
|
1596
1607
|
}
|
|
1597
1608
|
}
|
|
1598
1609
|
|
|
1599
|
-
// src/
|
|
1610
|
+
// src/opError.ts
|
|
1600
1611
|
var OpError = class extends Error {
|
|
1601
1612
|
constructor(code, message) {
|
|
1602
1613
|
super(message);
|
|
@@ -1604,6 +1615,16 @@ var OpError = class extends Error {
|
|
|
1604
1615
|
this.name = "OpError";
|
|
1605
1616
|
}
|
|
1606
1617
|
};
|
|
1618
|
+
var CODES = /* @__PURE__ */ new Set(["NOT_FOUND", "EXISTS", "PERMISSION_DENIED", "READ_ONLY", "NO_TRASH", "TOO_LARGE", "BAD_PATH", "UNSUPPORTED", "UNAVAILABLE", "ONLINE_ONLY", "INTERNAL"]);
|
|
1619
|
+
function asOpError(error) {
|
|
1620
|
+
if (error instanceof OpError) return error;
|
|
1621
|
+
const code = error?.code;
|
|
1622
|
+
const message = error?.message;
|
|
1623
|
+
const text = typeof message === "string" && message ? message : String(error);
|
|
1624
|
+
return new OpError(typeof code === "string" && CODES.has(code) ? code : "INTERNAL", text);
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
// src/ops.ts
|
|
1607
1628
|
function toOpError(error) {
|
|
1608
1629
|
if (error instanceof OpError) return error;
|
|
1609
1630
|
if (error instanceof PathError) return new OpError(error.code, error.message);
|
|
@@ -2118,7 +2139,8 @@ var RootOps = class {
|
|
|
2118
2139
|
if (!stat || !stat.isFile()) throw new OpError("NOT_FOUND", `no such file: ${resolved.device}`);
|
|
2119
2140
|
if (!params.allowFetch && placeholdersFor(import_fs11.default.realpathSync(this.rootPath)).has(resolved.abs)) throw new OpError("ONLINE_ONLY", `${deviceBasename(resolved.device)} is online-only (its bytes are in the cloud, not on this computer); reading it would download it`);
|
|
2120
2141
|
if (typeof params.maxBytes === "number" && params.maxBytes > 0 && stat.size > params.maxBytes) throw new OpError("TOO_LARGE", `${deviceBasename(resolved.device)} is ${stat.size} bytes; the limit for this read is ${params.maxBytes}`);
|
|
2121
|
-
|
|
2142
|
+
const abs = resolved.abs;
|
|
2143
|
+
return { abs, size: stat.size, name: deviceBasename(resolved.device), label: abs, chunks: (chunkBytes) => import_fs11.default.createReadStream(abs, { highWaterMark: chunkBytes }) };
|
|
2122
2144
|
}
|
|
2123
2145
|
/**
|
|
2124
2146
|
* The watcher's report of a path that may have changed: stat it and journal the difference, if there is one.
|
|
@@ -2514,8 +2536,17 @@ var RootWatcher = class {
|
|
|
2514
2536
|
};
|
|
2515
2537
|
|
|
2516
2538
|
// src/client.ts
|
|
2517
|
-
var import_fs13 = __toESM(require("fs"));
|
|
2518
2539
|
var import_crypto4 = require("crypto");
|
|
2540
|
+
|
|
2541
|
+
// src/clientCore.ts
|
|
2542
|
+
function looseStreamId() {
|
|
2543
|
+
const hex = (n) => Array.from({ length: n }, () => Math.floor(Math.random() * 16).toString(16)).join("");
|
|
2544
|
+
return `${hex(8)}-${hex(4)}-4${hex(3)}-a${hex(3)}-${hex(12)}`;
|
|
2545
|
+
}
|
|
2546
|
+
function background(timer) {
|
|
2547
|
+
timer.unref?.();
|
|
2548
|
+
return timer;
|
|
2549
|
+
}
|
|
2519
2550
|
var WRITES = /* @__PURE__ */ new Set(["mkdir", "move", "copy", "trash", "write", "write_open", "write_close"]);
|
|
2520
2551
|
var REPLY_MEMORY_MS = 5 * 6e4;
|
|
2521
2552
|
var REPLY_MEMORY_MAX = 2e3;
|
|
@@ -2564,7 +2595,7 @@ var RecentReplies = class {
|
|
|
2564
2595
|
return this.byId.size;
|
|
2565
2596
|
}
|
|
2566
2597
|
};
|
|
2567
|
-
var
|
|
2598
|
+
var CoreDeviceClient = class {
|
|
2568
2599
|
constructor(deps) {
|
|
2569
2600
|
this.deps = deps;
|
|
2570
2601
|
}
|
|
@@ -2596,8 +2627,7 @@ var DeviceClient = class {
|
|
|
2596
2627
|
} catch {
|
|
2597
2628
|
}
|
|
2598
2629
|
}, this.deps.changeDebounceMs ?? 1500);
|
|
2599
|
-
|
|
2600
|
-
this.pendingNudges.set(rootId, timer);
|
|
2630
|
+
this.pendingNudges.set(rootId, background(timer));
|
|
2601
2631
|
}
|
|
2602
2632
|
start() {
|
|
2603
2633
|
const { socket } = this.deps;
|
|
@@ -2619,10 +2649,9 @@ var DeviceClient = class {
|
|
|
2619
2649
|
if (reason === "io server disconnect" && socket.connect) {
|
|
2620
2650
|
const base = this.deps.reconnectAfterMs ?? 5e3;
|
|
2621
2651
|
const wait = Math.min(base * 2 ** Math.min(this.refusals, 10), Math.max(base, 10 * 6e4));
|
|
2622
|
-
|
|
2652
|
+
background(setTimeout(() => {
|
|
2623
2653
|
if (!socket.connected) socket.connect?.();
|
|
2624
|
-
}, wait);
|
|
2625
|
-
timer.unref?.();
|
|
2654
|
+
}, wait));
|
|
2626
2655
|
}
|
|
2627
2656
|
});
|
|
2628
2657
|
socket.on("connect_error", (error) => this.deps.log(`cannot connect: ${error.message}`));
|
|
@@ -2662,6 +2691,7 @@ var DeviceClient = class {
|
|
|
2662
2691
|
this.deps.log(`stopped sharing ${refused.length} folder(s) that were removed while this device was away`);
|
|
2663
2692
|
this.deps.retireRequested?.(refused);
|
|
2664
2693
|
}
|
|
2694
|
+
if (reply?.ok) this.superseded = false;
|
|
2665
2695
|
if (reply?.ok) {
|
|
2666
2696
|
this.refusals = 0;
|
|
2667
2697
|
this.deps.log(`connected as ${this.deps.device.name}: ${hello.roots.length} folder(s) shared`);
|
|
@@ -2723,30 +2753,34 @@ var DeviceClient = class {
|
|
|
2723
2753
|
case "media":
|
|
2724
2754
|
return { ok: true, result: await ops.media(params) };
|
|
2725
2755
|
case "read": {
|
|
2726
|
-
const opened = ops.openRead(params);
|
|
2756
|
+
const opened = await ops.openRead(params);
|
|
2727
2757
|
const named = typeof params.stream === "string" && /^[0-9a-f-]{36}$/i.test(params.stream) ? params.stream : null;
|
|
2728
|
-
const stream = named ?? (
|
|
2758
|
+
const stream = named ?? (this.deps.newStreamId ?? looseStreamId)();
|
|
2729
2759
|
this.open.add(stream);
|
|
2730
|
-
|
|
2731
|
-
void this.pump(stream, opened
|
|
2732
|
-
});
|
|
2760
|
+
setTimeout(() => {
|
|
2761
|
+
void this.pump(stream, opened);
|
|
2762
|
+
}, 0);
|
|
2733
2763
|
return { ok: true, result: { stream, size: opened.size, mimeType: "application/octet-stream" } };
|
|
2734
2764
|
}
|
|
2735
2765
|
default:
|
|
2736
2766
|
return { ok: false, error: { code: "UNSUPPORTED", message: `unknown method ${String(request.method)}` } };
|
|
2737
2767
|
}
|
|
2738
2768
|
} catch (error) {
|
|
2739
|
-
const e =
|
|
2769
|
+
const e = (this.deps.toError ?? asOpError)(error);
|
|
2740
2770
|
return { ok: false, error: { code: e.code, message: e.message } };
|
|
2741
2771
|
}
|
|
2742
2772
|
}
|
|
2743
|
-
|
|
2773
|
+
/**
|
|
2774
|
+
* The bytes of one read, a chunk at a time, each sent only once the server acknowledged the one before. One chunk
|
|
2775
|
+
* is held back so the last can say it is the last (an empty file sends one empty, last chunk). Leaving the loop
|
|
2776
|
+
* early (an abort, a dead line) ends the iteration, which is what releases the file.
|
|
2777
|
+
*/
|
|
2778
|
+
async pump(stream, opened) {
|
|
2744
2779
|
const size = this.deps.chunkBytes ?? STREAM_CHUNK_BYTES;
|
|
2745
2780
|
let seq = 0;
|
|
2746
|
-
const file = import_fs13.default.createReadStream(abs, { highWaterMark: size });
|
|
2747
2781
|
try {
|
|
2748
2782
|
let last = null;
|
|
2749
|
-
for await (const part of
|
|
2783
|
+
for await (const part of opened.chunks(size)) {
|
|
2750
2784
|
if (this.aborted.has(stream)) break;
|
|
2751
2785
|
if (last) {
|
|
2752
2786
|
if (!await this.send({ stream, seq, data: last, last: false })) {
|
|
@@ -2757,11 +2791,10 @@ var DeviceClient = class {
|
|
|
2757
2791
|
}
|
|
2758
2792
|
last = part;
|
|
2759
2793
|
}
|
|
2760
|
-
if (!this.aborted.has(stream)) await this.send({ stream, seq, data: last ??
|
|
2794
|
+
if (!this.aborted.has(stream)) await this.send({ stream, seq, data: last ?? new Uint8Array(0), last: true });
|
|
2761
2795
|
} catch (error) {
|
|
2762
|
-
this.deps.log(`read of ${
|
|
2796
|
+
this.deps.log(`read of ${opened.label} failed: ${error.message}`);
|
|
2763
2797
|
} finally {
|
|
2764
|
-
file.destroy();
|
|
2765
2798
|
this.open.delete(stream);
|
|
2766
2799
|
this.aborted.delete(stream);
|
|
2767
2800
|
}
|
|
@@ -2775,20 +2808,27 @@ var DeviceClient = class {
|
|
|
2775
2808
|
}
|
|
2776
2809
|
};
|
|
2777
2810
|
|
|
2811
|
+
// src/client.ts
|
|
2812
|
+
var DeviceClient = class extends CoreDeviceClient {
|
|
2813
|
+
constructor(deps) {
|
|
2814
|
+
super({ toError: toOpError, newStreamId: import_crypto4.randomUUID, ...deps });
|
|
2815
|
+
}
|
|
2816
|
+
};
|
|
2817
|
+
|
|
2778
2818
|
// src/identity.ts
|
|
2779
|
-
var
|
|
2819
|
+
var import_fs13 = __toESM(require("fs"));
|
|
2780
2820
|
var import_path13 = __toESM(require("path"));
|
|
2781
2821
|
function identityOf(rootPath) {
|
|
2782
2822
|
let stat;
|
|
2783
2823
|
try {
|
|
2784
|
-
stat =
|
|
2824
|
+
stat = import_fs13.default.statSync(rootPath);
|
|
2785
2825
|
} catch {
|
|
2786
2826
|
return null;
|
|
2787
2827
|
}
|
|
2788
2828
|
if (!stat.isDirectory()) return null;
|
|
2789
2829
|
let parent = null;
|
|
2790
2830
|
try {
|
|
2791
|
-
parent =
|
|
2831
|
+
parent = import_fs13.default.statSync(import_path13.default.dirname(import_path13.default.resolve(rootPath)));
|
|
2792
2832
|
} catch {
|
|
2793
2833
|
}
|
|
2794
2834
|
const mount = !parent || parent.dev !== stat.dev || parent.ino === stat.ino;
|
|
@@ -2809,7 +2849,7 @@ var RootIdentityStore = class {
|
|
|
2809
2849
|
load() {
|
|
2810
2850
|
if (this.recorded !== void 0) return this.recorded;
|
|
2811
2851
|
try {
|
|
2812
|
-
const parsed = JSON.parse(
|
|
2852
|
+
const parsed = JSON.parse(import_fs13.default.readFileSync(this.file, "utf8"));
|
|
2813
2853
|
this.recorded = typeof parsed.dev === "number" && typeof parsed.ino === "number" ? { dev: parsed.dev, ino: parsed.ino, mount: parsed.mount === true } : null;
|
|
2814
2854
|
} catch {
|
|
2815
2855
|
this.recorded = null;
|
|
@@ -2819,9 +2859,9 @@ var RootIdentityStore = class {
|
|
|
2819
2859
|
save(identity) {
|
|
2820
2860
|
this.recorded = identity;
|
|
2821
2861
|
try {
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2862
|
+
import_fs13.default.mkdirSync(import_path13.default.dirname(this.file), { recursive: true });
|
|
2863
|
+
import_fs13.default.writeFileSync(`${this.file}.tmp`, JSON.stringify(identity));
|
|
2864
|
+
import_fs13.default.renameSync(`${this.file}.tmp`, this.file);
|
|
2825
2865
|
} catch {
|
|
2826
2866
|
}
|
|
2827
2867
|
}
|
|
@@ -2858,7 +2898,7 @@ var RootIdentityStore = class {
|
|
|
2858
2898
|
forget() {
|
|
2859
2899
|
this.recorded = void 0;
|
|
2860
2900
|
try {
|
|
2861
|
-
|
|
2901
|
+
import_fs13.default.unlinkSync(this.file);
|
|
2862
2902
|
} catch {
|
|
2863
2903
|
}
|
|
2864
2904
|
}
|
|
@@ -2869,7 +2909,7 @@ function looksLikeSharedFolder(rootPath, knownTopLevelNames) {
|
|
|
2869
2909
|
if (knownTopLevelNames.length < ADOPT_MIN_KNOWN) return false;
|
|
2870
2910
|
let present;
|
|
2871
2911
|
try {
|
|
2872
|
-
present = new Set(
|
|
2912
|
+
present = new Set(import_fs13.default.readdirSync(rootPath));
|
|
2873
2913
|
} catch {
|
|
2874
2914
|
return false;
|
|
2875
2915
|
}
|
|
@@ -2878,11 +2918,11 @@ function looksLikeSharedFolder(rootPath, knownTopLevelNames) {
|
|
|
2878
2918
|
}
|
|
2879
2919
|
|
|
2880
2920
|
// src/index.ts
|
|
2881
|
-
var HELPER_VERSION = "0.
|
|
2921
|
+
var HELPER_VERSION = "0.2.0";
|
|
2882
2922
|
var WATCHER_RETRY_MS = 5 * 6e4;
|
|
2883
2923
|
function probeRoot(rootPath, identity, options = {}) {
|
|
2884
2924
|
try {
|
|
2885
|
-
if (!
|
|
2925
|
+
if (!import_fs14.default.statSync(rootPath).isDirectory()) return "missing";
|
|
2886
2926
|
} catch {
|
|
2887
2927
|
return "missing";
|
|
2888
2928
|
}
|
|
@@ -2896,8 +2936,8 @@ function probeRoot(rootPath, identity, options = {}) {
|
|
|
2896
2936
|
}
|
|
2897
2937
|
}
|
|
2898
2938
|
try {
|
|
2899
|
-
|
|
2900
|
-
|
|
2939
|
+
import_fs14.default.closeSync(import_fs14.default.openSync(rootPath, "r"));
|
|
2940
|
+
import_fs14.default.readdirSync(rootPath);
|
|
2901
2941
|
return "ok";
|
|
2902
2942
|
} catch (error) {
|
|
2903
2943
|
const code = error.code;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thedriveai/device-helper",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "The Drive AI device helper: shares folders on a Mac, a PC or a NAS with the agent.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://thedrive.ai",
|
|
@@ -18,6 +18,17 @@
|
|
|
18
18
|
"type": "commonjs",
|
|
19
19
|
"main": "./dist/lib.js",
|
|
20
20
|
"types": "./dist/lib.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/lib.d.ts",
|
|
24
|
+
"default": "./dist/lib.js"
|
|
25
|
+
},
|
|
26
|
+
"./core": {
|
|
27
|
+
"types": "./dist/core.d.ts",
|
|
28
|
+
"default": "./dist/core.js"
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
21
32
|
"files": [
|
|
22
33
|
"dist"
|
|
23
34
|
],
|
|
@@ -29,10 +40,12 @@
|
|
|
29
40
|
},
|
|
30
41
|
"scripts": {
|
|
31
42
|
"dev": "tsx src/cli.ts",
|
|
32
|
-
"build": "npm run clean && npm run build:cli && npm run build:lib",
|
|
43
|
+
"build": "npm run clean && npm run build:cli && npm run build:lib && npm run build:core",
|
|
33
44
|
"build:cli": "esbuild src/cli.ts --tsconfig=tsconfig.json --bundle --platform=node --target=node20 --outfile=dist/cli.js --banner:js='#!/usr/bin/env node' && tsx scripts/finishBundle.ts dist/cli.js && chmod +x dist/cli.js",
|
|
34
45
|
"build:lib": "esbuild src/lib.ts --tsconfig=tsconfig.json --bundle --platform=node --target=node20 --external:socket.io-client --outfile=dist/lib.js && tsx scripts/finishBundle.ts dist/lib.js && dts-bundle-generator --project tsconfig.json --no-check --export-referenced-types=false --no-banner -o dist/lib.d.ts src/lib.ts",
|
|
35
|
-
"
|
|
46
|
+
"build:core": "esbuild src/core.ts --tsconfig=tsconfig.json --bundle --platform=neutral --format=cjs --target=es2020 --outfile=dist/core.js && tsx scripts/finishBundle.ts dist/core.js && dts-bundle-generator --project tsconfig.json --no-check --export-referenced-types=false --no-banner -o dist/core.d.ts src/core.ts",
|
|
47
|
+
"test": "tsx tests/helper.test.ts && tsx tests/portable.test.ts",
|
|
48
|
+
"test:portable": "tsx tests/portable.test.ts",
|
|
36
49
|
"pack:check": "tsx scripts/packCheck.ts",
|
|
37
50
|
"check-code": "tsc --noEmit -p tsconfig.json",
|
|
38
51
|
"prepack": "npm run build",
|