drivekit 1.1.0 → 1.1.2

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/index.js CHANGED
@@ -26,26 +26,58 @@ var init_esm_shims = __esm({
26
26
  // src/server/webdav.ts
27
27
  var webdav_exports = {};
28
28
  __export(webdav_exports, {
29
+ escapeXml: () => escapeXml,
29
30
  startWebDAVServer: () => startWebDAVServer
30
31
  });
31
- import http2 from "http";
32
+ import http3 from "http";
32
33
  import url from "url";
33
- import path6 from "path";
34
+ import path7 from "path";
35
+ function escapeXml(unsafe) {
36
+ return unsafe.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
37
+ }
34
38
  async function startWebDAVServer(drive2, options = {}) {
35
39
  const port = options.port || 8080;
36
40
  const host = options.host || "127.0.0.1";
37
41
  await drive2.init();
38
- const server = http2.createServer(async (req, res) => {
42
+ const server = http3.createServer(async (req, res) => {
39
43
  try {
44
+ if (options.auth?.username && options.auth?.password) {
45
+ const authHeader = req.headers["authorization"];
46
+ if (!authHeader || !authHeader.startsWith("Basic ")) {
47
+ res.writeHead(401, {
48
+ "WWW-Authenticate": 'Basic realm="DriveKit WebDAV"',
49
+ "Content-Type": "text/plain"
50
+ });
51
+ res.end("Authentication required");
52
+ return;
53
+ }
54
+ const credentials = Buffer.from(authHeader.replace("Basic ", ""), "base64").toString("utf8");
55
+ const [user, pass] = credentials.split(":");
56
+ if (user !== options.auth.username || pass !== options.auth.password) {
57
+ res.writeHead(401, {
58
+ "WWW-Authenticate": 'Basic realm="DriveKit WebDAV"',
59
+ "Content-Type": "text/plain"
60
+ });
61
+ res.end("Invalid credentials");
62
+ return;
63
+ }
64
+ }
40
65
  const parsedUrl = url.parse(req.url || "/", true);
41
66
  const decodedPath = decodeURIComponent(parsedUrl.pathname || "/");
42
67
  const virtualPath = decodedPath === "" ? "/" : decodedPath;
68
+ const baseName = path7.basename(virtualPath);
69
+ if (baseName.startsWith("._") || baseName === ".DS_Store" || baseName === ".Trashes" || baseName === ".Spotlight-V100" || baseName === ".fseventsd") {
70
+ res.writeHead(404, { "Content-Type": "text/plain" });
71
+ res.end("Not Found");
72
+ return;
73
+ }
43
74
  switch (req.method) {
44
75
  case "OPTIONS": {
45
76
  res.writeHead(200, {
46
77
  DAV: "1, 2",
47
78
  Allow: "OPTIONS, GET, HEAD, POST, PUT, DELETE, MKCOL, MOVE, COPY, PROPFIND",
48
- "MS-Author-Via": "DAV"
79
+ "MS-Author-Via": "DAV",
80
+ "Accept-Ranges": "bytes"
49
81
  });
50
82
  res.end();
51
83
  return;
@@ -53,6 +85,11 @@ async function startWebDAVServer(drive2, options = {}) {
53
85
  case "PROPFIND": {
54
86
  const depth = req.headers["depth"] || "1";
55
87
  const xml = await handlePropfind(drive2, virtualPath, depth);
88
+ if (xml === null) {
89
+ res.writeHead(404, { "Content-Type": "text/plain" });
90
+ res.end("Not Found");
91
+ return;
92
+ }
56
93
  res.writeHead(207, {
57
94
  "Content-Type": "application/xml; charset=utf-8"
58
95
  });
@@ -63,7 +100,7 @@ async function startWebDAVServer(drive2, options = {}) {
63
100
  case "HEAD": {
64
101
  if (virtualPath === "/" || virtualPath === "") {
65
102
  res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
66
- res.end("<h1>DriveKit WebDAV Server Running</h1><p>Connect via Finder or WebDAV client.</p>");
103
+ res.end("<h1>DriveKit WebDAV Server Running</h1><p>Connect via Finder, File Explorer, or WebDAV client.</p>");
67
104
  return;
68
105
  }
69
106
  try {
@@ -73,15 +110,42 @@ async function startWebDAVServer(drive2, options = {}) {
73
110
  res.end("Cannot GET a directory");
74
111
  return;
75
112
  }
76
- const stream = await drive2.createReadStream(virtualPath);
77
- res.writeHead(200, {
78
- "Content-Type": file.mimeType || "application/octet-stream",
79
- "Content-Length": file.size || void 0
80
- });
81
- if (req.method === "HEAD") {
82
- res.end();
113
+ const fileSize = file.size ? parseInt(file.size, 10) : void 0;
114
+ const rangeHeader = req.headers["range"];
115
+ if (rangeHeader && rangeHeader.startsWith("bytes=") && fileSize !== void 0) {
116
+ const parts = rangeHeader.replace("bytes=", "").split("-");
117
+ const start = parseInt(parts[0], 10);
118
+ const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
119
+ if (isNaN(start) || start >= fileSize || end !== void 0 && end >= fileSize) {
120
+ res.writeHead(416, { "Content-Range": `bytes */${fileSize}` });
121
+ res.end();
122
+ return;
123
+ }
124
+ const stream = await drive2.createReadStream(virtualPath, { start, end });
125
+ const chunkLength = end - start + 1;
126
+ res.writeHead(206, {
127
+ "Content-Type": file.mimeType || "application/octet-stream",
128
+ "Content-Range": `bytes ${start}-${end}/${fileSize}`,
129
+ "Content-Length": chunkLength,
130
+ "Accept-Ranges": "bytes"
131
+ });
132
+ if (req.method === "HEAD") {
133
+ res.end();
134
+ } else {
135
+ stream.pipe(res);
136
+ }
83
137
  } else {
84
- stream.pipe(res);
138
+ const stream = await drive2.createReadStream(virtualPath);
139
+ res.writeHead(200, {
140
+ "Content-Type": file.mimeType || "application/octet-stream",
141
+ "Content-Length": file.size || void 0,
142
+ "Accept-Ranges": "bytes"
143
+ });
144
+ if (req.method === "HEAD") {
145
+ res.end();
146
+ } else {
147
+ stream.pipe(res);
148
+ }
85
149
  }
86
150
  } catch (e) {
87
151
  res.writeHead(404, { "Content-Type": "text/plain" });
@@ -90,27 +154,29 @@ async function startWebDAVServer(drive2, options = {}) {
90
154
  return;
91
155
  }
92
156
  case "PUT": {
93
- const remoteDir = path6.dirname(virtualPath);
94
- const fileName = path6.basename(virtualPath);
157
+ const remoteDir = path7.dirname(virtualPath);
95
158
  await drive2.mkdirp(remoteDir);
96
- const chunks = [];
97
- req.on("data", (chunk) => chunks.push(chunk));
98
- req.on("end", async () => {
99
- try {
100
- const buffer = Buffer.concat(chunks);
101
- await drive2.upload({
102
- source: buffer,
103
- filename: fileName,
104
- parent: remoteDir,
105
- overwrite: true
106
- });
107
- res.writeHead(201, { "Content-Type": "text/plain" });
108
- res.end("Created");
109
- } catch (err) {
110
- res.writeHead(500, { "Content-Type": "text/plain" });
111
- res.end(err.message || "Upload failed");
159
+ let existed = false;
160
+ try {
161
+ const existingFile = await drive2.getFile(virtualPath);
162
+ if (existingFile && existingFile.id) {
163
+ existed = true;
112
164
  }
113
- });
165
+ } catch {
166
+ existed = false;
167
+ }
168
+ try {
169
+ const uploadStream = await drive2.createWriteStream(virtualPath, {
170
+ overwrite: true
171
+ });
172
+ req.pipe(uploadStream);
173
+ await uploadStream.done;
174
+ res.writeHead(existed ? 204 : 201, { "Content-Type": "text/plain" });
175
+ res.end(existed ? "" : "Created");
176
+ } catch (err) {
177
+ res.writeHead(500, { "Content-Type": "text/plain" });
178
+ res.end(err.message || "Upload failed");
179
+ }
114
180
  return;
115
181
  }
116
182
  case "MKCOL": {
@@ -142,14 +208,24 @@ async function startWebDAVServer(drive2, options = {}) {
142
208
  res.end("Destination header missing");
143
209
  return;
144
210
  }
145
- const destUrl = url.parse(destination);
146
- const destPath = decodeURIComponent(destUrl.pathname || "");
147
- const newName = path6.basename(destPath);
148
- const newParent = path6.dirname(destPath);
149
211
  try {
150
- await drive2.move(virtualPath, newParent);
151
- if (newName !== path6.basename(virtualPath)) {
152
- await drive2.rename(destPath, newName);
212
+ const file = await drive2.getFile(virtualPath);
213
+ if (!file?.id) {
214
+ res.writeHead(404, { "Content-Type": "text/plain" });
215
+ res.end("Source file not found");
216
+ return;
217
+ }
218
+ const destUrl = url.parse(destination);
219
+ const destPath = decodeURIComponent(destUrl.pathname || "");
220
+ const newName = path7.basename(destPath);
221
+ const newParentDir = path7.dirname(destPath);
222
+ const newParentFolderId = await drive2.mkdirp(newParentDir);
223
+ const currentParentDir = path7.dirname(virtualPath);
224
+ if (newParentDir !== currentParentDir) {
225
+ await drive2.move(file.id, newParentFolderId);
226
+ }
227
+ if (newName !== path7.basename(virtualPath)) {
228
+ await drive2.rename(file.id, newName);
153
229
  }
154
230
  res.writeHead(201);
155
231
  res.end("Moved");
@@ -159,6 +235,34 @@ async function startWebDAVServer(drive2, options = {}) {
159
235
  }
160
236
  return;
161
237
  }
238
+ case "COPY": {
239
+ const destination = req.headers["destination"];
240
+ if (!destination || typeof destination !== "string") {
241
+ res.writeHead(400, { "Content-Type": "text/plain" });
242
+ res.end("Destination header missing");
243
+ return;
244
+ }
245
+ try {
246
+ const file = await drive2.getFile(virtualPath);
247
+ if (!file?.id) {
248
+ res.writeHead(404, { "Content-Type": "text/plain" });
249
+ res.end("Source file not found");
250
+ return;
251
+ }
252
+ const destUrl = url.parse(destination);
253
+ const destPath = decodeURIComponent(destUrl.pathname || "");
254
+ const newName = path7.basename(destPath);
255
+ const newParentDir = path7.dirname(destPath);
256
+ const newParentFolderId = await drive2.mkdirp(newParentDir);
257
+ await drive2.copy(file.id, { parent: newParentFolderId, name: newName });
258
+ res.writeHead(201);
259
+ res.end("Copied");
260
+ } catch (err) {
261
+ res.writeHead(500, { "Content-Type": "text/plain" });
262
+ res.end(err.message);
263
+ }
264
+ return;
265
+ }
162
266
  default: {
163
267
  res.writeHead(501, { "Content-Type": "text/plain" });
164
268
  res.end("Method Not Implemented");
@@ -196,25 +300,38 @@ async function handlePropfind(drive2, virtualPath, depth) {
196
300
  virtualPath,
197
301
  isFolder,
198
302
  selfItem.size ? parseInt(selfItem.size, 10) : 0,
199
- selfItem.modifiedTime || (/* @__PURE__ */ new Date()).toISOString()
303
+ selfItem.modifiedTime || (/* @__PURE__ */ new Date()).toISOString(),
304
+ selfItem.mimeType || void 0
200
305
  )
201
306
  );
202
307
  } catch {
203
- return `<?xml version="1.0" encoding="utf-8"?><D:multistatus xmlns:D="DAV:"></D:multistatus>`;
308
+ return null;
204
309
  }
205
310
  }
206
311
  if (isFolder && depth !== "0") {
207
312
  try {
208
313
  const children = await drive2.ls(virtualPath);
314
+ const seenNames = /* @__PURE__ */ new Map();
209
315
  for (const child of children) {
316
+ let childName = child.name || "untitled";
317
+ if (seenNames.has(childName)) {
318
+ const count = seenNames.get(childName) + 1;
319
+ seenNames.set(childName, count);
320
+ const ext = path7.extname(childName);
321
+ const base = path7.basename(childName, ext);
322
+ childName = `${base} (${count})${ext}`;
323
+ } else {
324
+ seenNames.set(childName, 0);
325
+ }
210
326
  const childIsFolder = child.mimeType === "application/vnd.google-apps.folder";
211
- const childPath = virtualPath === "/" ? `/${child.name}` : `${virtualPath}/${child.name}`;
327
+ const childPath = virtualPath === "/" ? `/${childName}` : `${virtualPath}/${childName}`;
212
328
  responses.push(
213
329
  renderWebDAVResponse(
214
330
  childPath,
215
331
  childIsFolder,
216
332
  child.size ? parseInt(child.size, 10) : 0,
217
- child.modifiedTime || (/* @__PURE__ */ new Date()).toISOString()
333
+ child.modifiedTime || (/* @__PURE__ */ new Date()).toISOString(),
334
+ child.mimeType || void 0
218
335
  )
219
336
  );
220
337
  }
@@ -226,19 +343,25 @@ async function handlePropfind(drive2, virtualPath, depth) {
226
343
  ${responses.join("\n")}
227
344
  </D:multistatus>`;
228
345
  }
229
- function renderWebDAVResponse(href, isDir, size, modifiedTime) {
230
- const encodedHref = encodeURI(href);
346
+ function renderWebDAVResponse(href, isDir, size, modifiedTime, mimeType) {
347
+ let cleanHref = href;
348
+ if (isDir && !cleanHref.endsWith("/")) {
349
+ cleanHref += "/";
350
+ }
351
+ const encodedHref = encodeURI(cleanHref);
352
+ const displayName = escapeXml(path7.basename(href) || "root");
231
353
  const resourceType = isDir ? "<D:collection/>" : "";
232
354
  const dateFormatted = new Date(modifiedTime).toUTCString();
355
+ const contentTypeTag = !isDir && mimeType ? `
356
+ <D:getcontenttype>${escapeXml(mimeType)}</D:getcontenttype>` : "";
233
357
  return ` <D:response>
234
358
  <D:href>${encodedHref}</D:href>
235
359
  <D:propstat>
236
360
  <D:prop>
237
- <D:displayname>${path6.basename(href) || "root"}</D:displayname>
361
+ <D:displayname>${displayName}</D:displayname>
238
362
  <D:resourcetype>${resourceType}</D:resourcetype>
239
363
  <D:getcontentlength>${isDir ? "0" : size}</D:getcontentlength>
240
- <D:getlastmodified>${dateFormatted}</D:getlastmodified>
241
- <D:status>HTTP/1.1 200 OK</D:status>
364
+ <D:getlastmodified>${dateFormatted}</D:getlastmodified>${contentTypeTag}
242
365
  </D:prop>
243
366
  <D:status>HTTP/1.1 200 OK</D:status>
244
367
  </D:propstat>
@@ -257,7 +380,7 @@ init_esm_shims();
257
380
  // src/core/client.ts
258
381
  init_esm_shims();
259
382
  import fs6 from "fs";
260
- import path7 from "path";
383
+ import path8 from "path";
261
384
  import { drive } from "@googleapis/drive";
262
385
 
263
386
  // src/auth/authenticator.ts
@@ -311,6 +434,39 @@ var DriveUploadError = class extends DriveKitError {
311
434
  }
312
435
  };
313
436
 
437
+ // src/utils/http-agent.ts
438
+ init_esm_shims();
439
+ import https from "https";
440
+ import http from "http";
441
+ var sharedHttpsAgent = null;
442
+ var sharedHttpAgent = null;
443
+ function getOptimizedHttpsAgent(options) {
444
+ if (!sharedHttpsAgent) {
445
+ sharedHttpsAgent = new https.Agent({
446
+ keepAlive: options?.keepAlive ?? true,
447
+ maxSockets: options?.maxSockets ?? 64,
448
+ maxFreeSockets: options?.maxFreeSockets ?? 32,
449
+ keepAliveMsecs: options?.keepAliveMsecs ?? 6e4,
450
+ timeout: options?.timeout ?? 3e4,
451
+ noDelay: true
452
+ });
453
+ }
454
+ return sharedHttpsAgent;
455
+ }
456
+ function getOptimizedHttpAgent(options) {
457
+ if (!sharedHttpAgent) {
458
+ sharedHttpAgent = new http.Agent({
459
+ keepAlive: options?.keepAlive ?? true,
460
+ maxSockets: options?.maxSockets ?? 64,
461
+ maxFreeSockets: options?.maxFreeSockets ?? 32,
462
+ keepAliveMsecs: options?.keepAliveMsecs ?? 6e4,
463
+ timeout: options?.timeout ?? 3e4,
464
+ noDelay: true
465
+ });
466
+ }
467
+ return sharedHttpAgent;
468
+ }
469
+
314
470
  // src/auth/authenticator.ts
315
471
  var DEFAULT_SCOPES = [
316
472
  "https://www.googleapis.com/auth/drive"
@@ -334,44 +490,39 @@ var DriveAuthenticator = class {
334
490
  const oauth2Client = new googleAuth.OAuth2();
335
491
  oauth2Client.setCredentials({ access_token: this.config.accessToken });
336
492
  this.authClient = oauth2Client;
337
- return this.authClient;
338
- }
339
- if (this.config.oauth2Client) {
493
+ } else if (this.config.oauth2Client) {
340
494
  this.authClient = this.config.oauth2Client;
341
- return this.authClient;
342
- }
343
- if (this.config.oauth) {
495
+ } else if (this.config.oauth) {
344
496
  const { clientId, clientSecret, redirectUri = "http://localhost:3000/oauth2callback", refreshToken } = this.config.oauth;
345
497
  const oauth2Client = new googleAuth.OAuth2(clientId, clientSecret, redirectUri);
346
498
  if (refreshToken) {
347
499
  oauth2Client.setCredentials({ refresh_token: refreshToken });
348
500
  }
349
501
  this.authClient = oauth2Client;
350
- return this.authClient;
351
- }
352
- const keyFile = this.config.keyFilePath || process.env.GDRIVE_SERVICE_ACCOUNT_PATH || process.env.GOOGLE_APPLICATION_CREDENTIALS;
353
- if (keyFile) {
354
- if (!fs.existsSync(keyFile)) {
502
+ } else if (this.config.keyFilePath || process.env.GDRIVE_SERVICE_ACCOUNT_PATH || process.env.GOOGLE_APPLICATION_CREDENTIALS) {
503
+ const keyFile = this.config.keyFilePath || process.env.GDRIVE_SERVICE_ACCOUNT_PATH || process.env.GOOGLE_APPLICATION_CREDENTIALS;
504
+ if (keyFile && !fs.existsSync(keyFile)) {
355
505
  throw new DriveAuthError(`Service account file not found at: ${keyFile}`);
356
506
  }
357
- const auth2 = new googleAuth.GoogleAuth({
507
+ const auth = new googleAuth.GoogleAuth({
358
508
  keyFile,
359
509
  scopes
360
510
  });
361
- this.authClient = auth2;
362
- return this.authClient;
363
- }
364
- const creds = this.config.credentials || (process.env.GDRIVE_CREDENTIALS_JSON ? JSON.parse(process.env.GDRIVE_CREDENTIALS_JSON) : null);
365
- if (creds) {
366
- const auth2 = new googleAuth.GoogleAuth({
511
+ this.authClient = auth;
512
+ } else if (this.config.credentials || process.env.GDRIVE_CREDENTIALS_JSON) {
513
+ const creds = this.config.credentials || (process.env.GDRIVE_CREDENTIALS_JSON ? JSON.parse(process.env.GDRIVE_CREDENTIALS_JSON) : null);
514
+ const auth = new googleAuth.GoogleAuth({
367
515
  credentials: creds,
368
516
  scopes
369
517
  });
370
- this.authClient = auth2;
371
- return this.authClient;
518
+ this.authClient = auth;
519
+ } else {
520
+ const auth = new googleAuth.GoogleAuth({ scopes });
521
+ this.authClient = auth;
522
+ }
523
+ if (this.authClient?.transporter?.defaults) {
524
+ this.authClient.transporter.defaults.agent = getOptimizedHttpsAgent();
372
525
  }
373
- const auth = new googleAuth.GoogleAuth({ scopes });
374
- this.authClient = auth;
375
526
  return this.authClient;
376
527
  } catch (err) {
377
528
  if (err instanceof DriveAuthError) throw err;
@@ -510,6 +661,7 @@ var PathResolver = class {
510
661
  drive;
511
662
  rootFolderId;
512
663
  cache = /* @__PURE__ */ new Map();
664
+ inFlightResolves = /* @__PURE__ */ new Map();
513
665
  cacheTTL;
514
666
  enableCache;
515
667
  metadataCache;
@@ -533,8 +685,8 @@ var PathResolver = class {
533
685
  /**
534
686
  * Gets cache key for a path
535
687
  */
536
- getCacheKey(path8) {
537
- return `${this.rootFolderId}:${this.normalizePath(path8)}`;
688
+ getCacheKey(path9) {
689
+ return `${this.rootFolderId}:${this.normalizePath(path9)}`;
538
690
  }
539
691
  /**
540
692
  * Clears the path cache
@@ -548,8 +700,8 @@ var PathResolver = class {
548
700
  /**
549
701
  * Invalidate a single path from cache
550
702
  */
551
- invalidatePath(path8) {
552
- const norm = this.normalizePath(path8);
703
+ invalidatePath(path9) {
704
+ const norm = this.normalizePath(path9);
553
705
  const key = this.getCacheKey(norm);
554
706
  this.cache.delete(key);
555
707
  if (this.metadataCache) {
@@ -562,6 +714,7 @@ var PathResolver = class {
562
714
  /**
563
715
  * Resolves a path to a Google Drive item (file or folder).
564
716
  * If createIfMissing is true, any missing intermediate directories will be automatically created (like mkdir -p).
717
+ * Incorporates in-flight request coalescing to deduplicate concurrent resolutions of identical paths.
565
718
  */
566
719
  async resolve(virtualPath, options = {}) {
567
720
  const norm = this.normalizePath(virtualPath);
@@ -595,6 +748,20 @@ var PathResolver = class {
595
748
  };
596
749
  }
597
750
  }
751
+ const flightKey = `${this.rootFolderId}:${norm}:${options.createIfMissing ? "create" : "read"}`;
752
+ const inFlight = this.inFlightResolves.get(flightKey);
753
+ if (inFlight) {
754
+ return await inFlight;
755
+ }
756
+ const task = this.doResolve(virtualPath, norm, options);
757
+ this.inFlightResolves.set(flightKey, task);
758
+ try {
759
+ return await task;
760
+ } finally {
761
+ this.inFlightResolves.delete(flightKey);
762
+ }
763
+ }
764
+ async doResolve(virtualPath, norm, options) {
598
765
  const segments = norm.split("/").filter(Boolean);
599
766
  let currentParentId = this.rootFolderId;
600
767
  let currentPath = "";
@@ -646,7 +813,7 @@ var PathResolver = class {
646
813
  });
647
814
  let file = response.data.files?.[0];
648
815
  if (!file && currentParentId === this.rootFolderId) {
649
- const sharedQ = `name = '${escapedName}' and trashed = false`;
816
+ const sharedQ = `name = '${escapedName}' and sharedWithMe = true and trashed = false`;
650
817
  const sharedResponse = await withRetry(async () => {
651
818
  return await this.drive.files.list({
652
819
  q: sharedQ,
@@ -660,13 +827,14 @@ var PathResolver = class {
660
827
  file = sharedResponse.data.files?.[0];
661
828
  }
662
829
  if (file && file.id) {
830
+ const segmentParentId = currentParentId;
663
831
  currentParentId = file.id;
664
832
  if (this.metadataCache) {
665
833
  this.metadataCache.upsertFile({
666
834
  id: file.id,
667
835
  name: file.name || segment,
668
836
  mimeType: file.mimeType || "",
669
- parentId: currentParentId,
837
+ parentId: segmentParentId,
670
838
  path: currentPath,
671
839
  trashed: false
672
840
  });
@@ -704,13 +872,14 @@ var PathResolver = class {
704
872
  if (!newFolder.id) {
705
873
  throw new Error(`Failed to create folder "${segment}" under parent "${currentParentId}"`);
706
874
  }
875
+ const segmentParentId = currentParentId;
707
876
  currentParentId = newFolder.id;
708
877
  if (this.metadataCache) {
709
878
  this.metadataCache.upsertFile({
710
879
  id: newFolder.id,
711
880
  name: segment,
712
881
  mimeType: GOOGLE_WORKSPACE_MIME_TYPES.FOLDER,
713
- parentId: currentParentId,
882
+ parentId: segmentParentId,
714
883
  path: currentPath,
715
884
  trashed: false
716
885
  });
@@ -754,6 +923,7 @@ var PathResolver = class {
754
923
  init_esm_shims();
755
924
  import fs2 from "fs";
756
925
  import path2 from "path";
926
+ import { Transform } from "stream";
757
927
 
758
928
  // src/utils/format.ts
759
929
  init_esm_shims();
@@ -818,6 +988,15 @@ var ResumableUploader = class {
818
988
  const totalBytes = stat.size;
819
989
  const fileName = options.name || path2.basename(filePath);
820
990
  const mimeType = options.mimeType || lookupMimeType(filePath);
991
+ if (totalBytes <= MULTIPART_THRESHOLD) {
992
+ const readStream = fs2.createReadStream(filePath, { highWaterMark: 1024 * 1024 });
993
+ const stream = options.onProgress ? this.wrapStreamWithProgress(readStream, totalBytes, options.onProgress) : readStream;
994
+ return await this.directUpload(stream, {
995
+ ...options,
996
+ name: fileName,
997
+ mimeType
998
+ });
999
+ }
821
1000
  return await this.chunkedUpload({
822
1001
  filePath,
823
1002
  totalBytes,
@@ -833,6 +1012,16 @@ var ResumableUploader = class {
833
1012
  const totalBytes = buffer.length;
834
1013
  const fileName = options.name || "untitled";
835
1014
  const mimeType = options.mimeType || "application/octet-stream";
1015
+ if (totalBytes <= MULTIPART_THRESHOLD) {
1016
+ const { Readable } = await import("stream");
1017
+ const baseStream = Readable.from(totalBytes === 0 ? [] : [buffer]);
1018
+ const stream = options.onProgress ? this.wrapStreamWithProgress(baseStream, totalBytes, options.onProgress) : baseStream;
1019
+ return await this.directUpload(stream, {
1020
+ ...options,
1021
+ name: fileName,
1022
+ mimeType
1023
+ });
1024
+ }
836
1025
  return await this.chunkedUploadBuffer({
837
1026
  buffer,
838
1027
  totalBytes,
@@ -853,6 +1042,50 @@ var ResumableUploader = class {
853
1042
  mimeType
854
1043
  });
855
1044
  }
1045
+ /**
1046
+ * Calculates optimal chunk size based on file size for maximum throughput.
1047
+ */
1048
+ computeAdaptiveChunkSize(totalBytes, explicitChunkSize) {
1049
+ if (explicitChunkSize) {
1050
+ return Math.max(MIN_CHUNK_SIZE, Math.floor(explicitChunkSize / MIN_CHUNK_SIZE) * MIN_CHUNK_SIZE);
1051
+ }
1052
+ if (totalBytes > 128 * 1024 * 1024) {
1053
+ return 32 * 1024 * 1024;
1054
+ }
1055
+ if (totalBytes > 32 * 1024 * 1024) {
1056
+ return 16 * 1024 * 1024;
1057
+ }
1058
+ return this.defaultChunkSize;
1059
+ }
1060
+ /**
1061
+ * Attaches real-time progress monitoring to direct stream uploads.
1062
+ */
1063
+ wrapStreamWithProgress(stream, totalBytes, onProgress) {
1064
+ let bytesSent = 0;
1065
+ const startTime = Date.now();
1066
+ const progressTransform = new Transform({
1067
+ transform(chunk, _encoding, callback) {
1068
+ bytesSent += chunk.length;
1069
+ const elapsedSecs = (Date.now() - startTime) / 1e3;
1070
+ const bytesPerSecond = elapsedSecs > 0 ? Math.round(bytesSent / elapsedSecs) : 0;
1071
+ const remainingBytes = Math.max(0, totalBytes - bytesSent);
1072
+ const estimatedTimeRemainingSeconds = bytesPerSecond > 0 ? Math.round(remainingBytes / bytesPerSecond) : 0;
1073
+ const percentage = totalBytes > 0 ? Math.min(100, Math.round(bytesSent / totalBytes * 1e3) / 10) : 100;
1074
+ onProgress({
1075
+ bytesSent,
1076
+ totalBytes,
1077
+ percentage,
1078
+ bytesPerSecond,
1079
+ speedFormatted: formatSpeed(bytesPerSecond),
1080
+ estimatedTimeRemainingSeconds,
1081
+ chunkIndex: 1,
1082
+ totalChunks: 1
1083
+ });
1084
+ callback(null, chunk);
1085
+ }
1086
+ });
1087
+ return stream.pipe(progressTransform);
1088
+ }
856
1089
  /**
857
1090
  * Fast direct upload using googleapis multipart
858
1091
  */
@@ -885,7 +1118,7 @@ var ResumableUploader = class {
885
1118
  */
886
1119
  async chunkedUpload(params) {
887
1120
  const { filePath, totalBytes, fileName, mimeType, options } = params;
888
- const chunkSize = options.chunkSize ? Math.max(MIN_CHUNK_SIZE, Math.floor(options.chunkSize / MIN_CHUNK_SIZE) * MIN_CHUNK_SIZE) : this.defaultChunkSize;
1121
+ const chunkSize = this.computeAdaptiveChunkSize(totalBytes, options.chunkSize);
889
1122
  const sessionUrl = await this.initiateResumableSession({
890
1123
  fileName,
891
1124
  mimeType,
@@ -950,7 +1183,7 @@ var ResumableUploader = class {
950
1183
  */
951
1184
  async chunkedUploadBuffer(params) {
952
1185
  const { buffer, totalBytes, fileName, mimeType, options } = params;
953
- const chunkSize = options.chunkSize ? Math.max(MIN_CHUNK_SIZE, Math.floor(options.chunkSize / MIN_CHUNK_SIZE) * MIN_CHUNK_SIZE) : this.defaultChunkSize;
1186
+ const chunkSize = this.computeAdaptiveChunkSize(totalBytes, options.chunkSize);
954
1187
  const sessionUrl = await this.initiateResumableSession({
955
1188
  fileName,
956
1189
  mimeType,
@@ -1029,7 +1262,8 @@ var ResumableUploader = class {
1029
1262
  url: "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&supportsAllDrives=true",
1030
1263
  method: "POST",
1031
1264
  headers,
1032
- data: metadata
1265
+ data: metadata,
1266
+ agent: getOptimizedHttpsAgent()
1033
1267
  });
1034
1268
  });
1035
1269
  const sessionUrl = res.headers?.get ? res.headers.get("location") : res.headers?.location || res.headers?.["location"];
@@ -1055,6 +1289,7 @@ var ResumableUploader = class {
1055
1289
  method: "PUT",
1056
1290
  headers,
1057
1291
  data: buffer,
1292
+ agent: getOptimizedHttpsAgent(),
1058
1293
  validateStatus: (status) => status >= 200 && status < 300 || status === 308
1059
1294
  });
1060
1295
  if (res.status === 200 || res.status === 201) {
@@ -1088,7 +1323,7 @@ init_esm_shims();
1088
1323
  import fs3 from "fs";
1089
1324
  import path3 from "path";
1090
1325
  import { pipeline } from "stream/promises";
1091
- import { Transform } from "stream";
1326
+ import { Transform as Transform2 } from "stream";
1092
1327
  var Downloader = class {
1093
1328
  drive;
1094
1329
  constructor(drive2) {
@@ -1098,17 +1333,22 @@ var Downloader = class {
1098
1333
  * Downloads a file from Google Drive to local destination path or returns a readable stream.
1099
1334
  */
1100
1335
  async download(fileId, options = {}) {
1101
- const metaRes = await withRetry(async () => {
1102
- return await this.drive.files.get({
1103
- fileId,
1104
- fields: "id, name, mimeType, size",
1105
- supportsAllDrives: true
1336
+ let name = options.metadata?.name;
1337
+ let mimeType = options.metadata?.mimeType;
1338
+ let totalBytes = options.metadata?.size;
1339
+ if (!name || !mimeType) {
1340
+ const metaRes = await withRetry(async () => {
1341
+ return await this.drive.files.get({
1342
+ fileId,
1343
+ fields: "id, name, mimeType, size",
1344
+ supportsAllDrives: true
1345
+ });
1106
1346
  });
1107
- });
1108
- const file = metaRes.data;
1109
- const name = file.name || "downloaded-file";
1110
- const mimeType = file.mimeType || "application/octet-stream";
1111
- const totalBytes = file.size ? parseInt(file.size, 10) : void 0;
1347
+ const file = metaRes.data;
1348
+ name = file.name || "downloaded-file";
1349
+ mimeType = file.mimeType || "application/octet-stream";
1350
+ totalBytes = file.size ? parseInt(file.size, 10) : void 0;
1351
+ }
1112
1352
  let downloadStream;
1113
1353
  let finalFileName = name;
1114
1354
  if (isGoogleWorkspaceDoc(mimeType)) {
@@ -1139,7 +1379,7 @@ var Downloader = class {
1139
1379
  }
1140
1380
  let bytesRead = 0;
1141
1381
  const startTime = Date.now();
1142
- const progressTracker = new Transform({
1382
+ const progressTracker = new Transform2({
1143
1383
  transform(chunk, encoding, callback) {
1144
1384
  bytesRead += chunk.length;
1145
1385
  if (options.onProgress) {
@@ -1164,7 +1404,7 @@ var Downloader = class {
1164
1404
  destPath = path3.join(destPath, finalFileName);
1165
1405
  }
1166
1406
  await fs3.promises.mkdir(path3.dirname(destPath), { recursive: true });
1167
- const writeStream = fs3.createWriteStream(destPath);
1407
+ const writeStream = fs3.createWriteStream(destPath, { highWaterMark: 1024 * 1024 });
1168
1408
  await pipeline(monitoredStream, writeStream);
1169
1409
  return {
1170
1410
  filePath: destPath,
@@ -1407,6 +1647,7 @@ var DriveQueryBuilder = class {
1407
1647
  const q = this.toQueryString();
1408
1648
  const orderBy = this.orderClauses.join(", ");
1409
1649
  let pageToken = void 0;
1650
+ const effectiveFields = this.requestedFields.includes("nextPageToken") ? this.requestedFields : `nextPageToken, ${this.requestedFields}`;
1410
1651
  do {
1411
1652
  const res = await withRetry(async () => {
1412
1653
  return await this.drive.files.list({
@@ -1414,7 +1655,7 @@ var DriveQueryBuilder = class {
1414
1655
  orderBy: orderBy || void 0,
1415
1656
  pageSize: this.limitNumber,
1416
1657
  pageToken,
1417
- fields: this.requestedFields,
1658
+ fields: effectiveFields,
1418
1659
  spaces: this.spaces,
1419
1660
  supportsAllDrives: true,
1420
1661
  includeItemsFromAllDrives: true
@@ -1538,7 +1779,7 @@ var DriveWatcher = class extends EventEmitter {
1538
1779
  const res = await withRetry(async () => {
1539
1780
  return await this.drive.changes.list({
1540
1781
  pageToken: nextPageToken,
1541
- fields: "nextPageToken, newStartPageToken, changes(changeType, fileId, removed, time, file(id, name, mimeType, size, modifiedTime, parents, trashed))",
1782
+ fields: "nextPageToken, newStartPageToken, changes(changeType, fileId, removed, time, file(id, name, mimeType, size, createdTime, modifiedTime, parents, trashed))",
1542
1783
  supportsAllDrives: true,
1543
1784
  includeItemsFromAllDrives: true
1544
1785
  });
@@ -1629,16 +1870,24 @@ var StorageCleaner = class {
1629
1870
  async findDuplicates(options = {}) {
1630
1871
  const mode = options.by || "name";
1631
1872
  const maxScan = options.maxScan || 1e3;
1632
- const res = await withRetry(async () => {
1633
- return await this.drive.files.list({
1634
- q: 'trashed = false and mimeType != "application/vnd.google-apps.folder"',
1635
- pageSize: Math.min(1e3, maxScan),
1636
- fields: "files(id, name, size, md5Checksum, modifiedTime)",
1637
- supportsAllDrives: true,
1638
- includeItemsFromAllDrives: true
1873
+ const files = [];
1874
+ let pageToken = void 0;
1875
+ do {
1876
+ const remaining = maxScan - files.length;
1877
+ if (remaining <= 0) break;
1878
+ const res = await withRetry(async () => {
1879
+ return await this.drive.files.list({
1880
+ q: 'trashed = false and mimeType != "application/vnd.google-apps.folder"',
1881
+ pageSize: Math.min(1e3, remaining),
1882
+ pageToken,
1883
+ fields: "nextPageToken, files(id, name, size, md5Checksum, modifiedTime)",
1884
+ supportsAllDrives: true,
1885
+ includeItemsFromAllDrives: true
1886
+ });
1639
1887
  });
1640
- });
1641
- const files = res.data.files || [];
1888
+ files.push(...res.data.files || []);
1889
+ pageToken = res.data.nextPageToken || void 0;
1890
+ } while (pageToken && files.length < maxScan);
1642
1891
  const map = /* @__PURE__ */ new Map();
1643
1892
  for (const f of files) {
1644
1893
  if (!f.id) continue;
@@ -1813,7 +2062,7 @@ var SmartSyncEngine = class {
1813
2062
 
1814
2063
  // src/auth/browser-login.ts
1815
2064
  init_esm_shims();
1816
- import http from "http";
2065
+ import http2 from "http";
1817
2066
  import { exec } from "child_process";
1818
2067
  import { auth as googleAuth2 } from "@googleapis/drive";
1819
2068
  async function loginWithBrowser(options) {
@@ -1831,7 +2080,7 @@ async function loginWithBrowser(options) {
1831
2080
  scope: scopes
1832
2081
  });
1833
2082
  return new Promise((resolve, reject) => {
1834
- const server = http.createServer(async (req, res) => {
2083
+ const server = http2.createServer(async (req, res) => {
1835
2084
  try {
1836
2085
  const reqUrl = new URL(req.url || "/", `http://localhost:${port}`);
1837
2086
  if (reqUrl.pathname === "/oauth2callback") {
@@ -1915,7 +2164,11 @@ ${authUrl}
1915
2164
  });
1916
2165
  });
1917
2166
  server.on("error", (err) => {
1918
- reject(err);
2167
+ if (err.code === "EADDRINUSE") {
2168
+ reject(new Error(`Port ${port} is already in use by another process. Please specify an alternate port with --port <number>.`));
2169
+ } else {
2170
+ reject(err);
2171
+ }
1919
2172
  });
1920
2173
  });
1921
2174
  }
@@ -2048,12 +2301,39 @@ var DriveFolderRef = class {
2048
2301
 
2049
2302
  // src/core/plugins.ts
2050
2303
  init_esm_shims();
2304
+ import path5 from "path";
2051
2305
  var PluginManager = class {
2052
2306
  plugins = [];
2307
+ /**
2308
+ * Register a plugin. If a plugin with the same name already exists,
2309
+ * it replaces the old one to avoid duplicate side-effects.
2310
+ */
2053
2311
  use(plugin) {
2312
+ this.plugins = this.plugins.filter((p) => p.name !== plugin.name);
2054
2313
  this.plugins.push(plugin);
2055
2314
  return this;
2056
2315
  }
2316
+ /**
2317
+ * Unregister a plugin by its name.
2318
+ * Returns true if a plugin was found and removed, false otherwise.
2319
+ */
2320
+ unuse(pluginName) {
2321
+ const initialLen = this.plugins.length;
2322
+ this.plugins = this.plugins.filter((p) => p.name !== pluginName);
2323
+ return this.plugins.length < initialLen;
2324
+ }
2325
+ /**
2326
+ * Check if a plugin with given name is currently active
2327
+ */
2328
+ has(pluginName) {
2329
+ return this.plugins.some((p) => p.name === pluginName);
2330
+ }
2331
+ /**
2332
+ * Returns all currently registered plugins
2333
+ */
2334
+ getPlugins() {
2335
+ return [...this.plugins];
2336
+ }
2057
2337
  async runBeforeUpload(options) {
2058
2338
  let current = { ...options };
2059
2339
  for (const plugin of this.plugins) {
@@ -2097,45 +2377,62 @@ var PluginManager = class {
2097
2377
  };
2098
2378
  function createWebhookPlugin(options) {
2099
2379
  const events = options.events || ["upload"];
2100
- return {
2101
- name: "drivekit-official-webhook",
2102
- async afterUpload(file) {
2103
- if (!events.includes("upload")) return;
2380
+ const timeoutMs = options.timeout ?? 5e3;
2381
+ const maxRetries = options.retries ?? 0;
2382
+ const sendWebhook = async (event, payload) => {
2383
+ let lastError = null;
2384
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
2385
+ let timeoutId;
2386
+ const controller = new AbortController();
2387
+ timeoutId = setTimeout(() => controller.abort(), timeoutMs);
2104
2388
  try {
2105
- await fetch(options.url, {
2389
+ const res = await fetch(options.url, {
2106
2390
  method: "POST",
2107
2391
  headers: { "Content-Type": "application/json", ...options.headers || {} },
2108
- body: JSON.stringify({
2109
- event: "drivekit:upload",
2110
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
2111
- file: {
2112
- id: file.id,
2113
- name: file.name,
2114
- size: file.size,
2115
- mimeType: file.mimeType
2116
- }
2117
- })
2392
+ body: JSON.stringify(payload),
2393
+ signal: controller.signal
2118
2394
  });
2119
- } catch (e) {
2395
+ clearTimeout(timeoutId);
2396
+ if (!res.ok) {
2397
+ throw new Error(`Webhook returned HTTP ${res.status}: ${res.statusText}`);
2398
+ }
2399
+ return;
2400
+ } catch (err) {
2401
+ clearTimeout(timeoutId);
2402
+ lastError = err;
2403
+ if (attempt === maxRetries) {
2404
+ if (options.onError) {
2405
+ options.onError(lastError instanceof Error ? lastError : new Error(String(lastError)), event);
2406
+ }
2407
+ }
2120
2408
  }
2409
+ }
2410
+ };
2411
+ return {
2412
+ name: "drivekit-official-webhook",
2413
+ async afterUpload(file) {
2414
+ if (!events.includes("upload")) return;
2415
+ await sendWebhook("upload", {
2416
+ event: "drivekit:upload",
2417
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
2418
+ file: {
2419
+ id: file.id,
2420
+ name: file.name,
2421
+ size: file.size,
2422
+ mimeType: file.mimeType
2423
+ }
2424
+ });
2121
2425
  },
2122
2426
  async afterDownload(result) {
2123
2427
  if (!events.includes("download")) return;
2124
- try {
2125
- await fetch(options.url, {
2126
- method: "POST",
2127
- headers: { "Content-Type": "application/json", ...options.headers || {} },
2128
- body: JSON.stringify({
2129
- event: "drivekit:download",
2130
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
2131
- file: {
2132
- name: result.name,
2133
- size: result.size
2134
- }
2135
- })
2136
- });
2137
- } catch (e) {
2138
- }
2428
+ await sendWebhook("download", {
2429
+ event: "drivekit:download",
2430
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
2431
+ file: {
2432
+ name: result.name,
2433
+ size: result.size
2434
+ }
2435
+ });
2139
2436
  }
2140
2437
  };
2141
2438
  }
@@ -2143,15 +2440,13 @@ function createAutoNamePlugin(options) {
2143
2440
  return {
2144
2441
  name: "drivekit-official-autoname",
2145
2442
  beforeUpload(opts) {
2146
- let name = opts.filename;
2443
+ let name = opts.name || opts.filename;
2147
2444
  if (!name && typeof opts.source === "string") {
2148
- const pathModule2 = __require("path");
2149
- name = pathModule2.basename(opts.source);
2445
+ name = path5.basename(opts.source);
2150
2446
  }
2151
2447
  if (!name) return opts;
2152
- const pathModule = __require("path");
2153
- const ext = pathModule.extname(name);
2154
- const base = pathModule.basename(name, ext);
2448
+ const ext = path5.extname(name);
2449
+ const base = path5.basename(name, ext);
2155
2450
  let finalName = base;
2156
2451
  if (options.prefix) finalName = `${options.prefix}${finalName}`;
2157
2452
  if (options.appendTimestamp) {
@@ -2161,6 +2456,7 @@ function createAutoNamePlugin(options) {
2161
2456
  finalName = `${finalName}${ext}`;
2162
2457
  return {
2163
2458
  ...opts,
2459
+ name: finalName,
2164
2460
  filename: finalName
2165
2461
  };
2166
2462
  }
@@ -2170,7 +2466,7 @@ function createAutoNamePlugin(options) {
2170
2466
  // src/core/cache.ts
2171
2467
  init_esm_shims();
2172
2468
  import os from "os";
2173
- import path5 from "path";
2469
+ import path6 from "path";
2174
2470
  import fs5 from "fs";
2175
2471
  var SqliteMetadataCache = class {
2176
2472
  db = null;
@@ -2181,14 +2477,14 @@ var SqliteMetadataCache = class {
2181
2477
  if (customPath) {
2182
2478
  this.dbPath = customPath;
2183
2479
  } else {
2184
- const dir = path5.join(os.homedir(), ".drivekit");
2480
+ const dir = path6.join(os.homedir(), ".drivekit");
2185
2481
  if (!fs5.existsSync(dir)) {
2186
2482
  try {
2187
2483
  fs5.mkdirSync(dir, { recursive: true });
2188
2484
  } catch {
2189
2485
  }
2190
2486
  }
2191
- this.dbPath = path5.join(dir, "cache.db");
2487
+ this.dbPath = path6.join(dir, "cache.db");
2192
2488
  }
2193
2489
  }
2194
2490
  init() {
@@ -2206,7 +2502,7 @@ var SqliteMetadataCache = class {
2206
2502
  CREATE TABLE IF NOT EXISTS files (
2207
2503
  id TEXT PRIMARY KEY,
2208
2504
  name TEXT NOT NULL,
2209
- mimeType TEXT NOT NULL,
2505
+ mimeType TEXT,
2210
2506
  size INTEGER,
2211
2507
  parentId TEXT,
2212
2508
  path TEXT,
@@ -2292,7 +2588,7 @@ var SqliteMetadataCache = class {
2292
2588
  stmt.run(
2293
2589
  entry.id,
2294
2590
  entry.name,
2295
- entry.mimeType,
2591
+ entry.mimeType ?? null,
2296
2592
  entry.size ?? null,
2297
2593
  entry.parentId ?? null,
2298
2594
  entry.path ?? null,
@@ -2308,10 +2604,35 @@ var SqliteMetadataCache = class {
2308
2604
  return;
2309
2605
  }
2310
2606
  if (!this.initialized || !this.db || entries.length === 0) return;
2607
+ const stmt = this.db.prepare(`
2608
+ INSERT INTO files (id, name, mimeType, size, parentId, path, md5Checksum, trashed, modifiedTime, updatedAt)
2609
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2610
+ ON CONFLICT(id) DO UPDATE SET
2611
+ name = excluded.name,
2612
+ mimeType = excluded.mimeType,
2613
+ size = excluded.size,
2614
+ parentId = excluded.parentId,
2615
+ path = COALESCE(excluded.path, files.path),
2616
+ md5Checksum = excluded.md5Checksum,
2617
+ trashed = excluded.trashed,
2618
+ modifiedTime = excluded.modifiedTime,
2619
+ updatedAt = excluded.updatedAt
2620
+ `);
2311
2621
  this.db.exec("BEGIN TRANSACTION;");
2312
2622
  try {
2313
2623
  for (const entry of entries) {
2314
- this.upsertFile(entry);
2624
+ stmt.run(
2625
+ entry.id,
2626
+ entry.name,
2627
+ entry.mimeType ?? null,
2628
+ entry.size ?? null,
2629
+ entry.parentId ?? null,
2630
+ entry.path ?? null,
2631
+ entry.md5Checksum ? entry.md5Checksum.toLowerCase() : null,
2632
+ entry.trashed ? 1 : 0,
2633
+ entry.modifiedTime ?? null,
2634
+ entry.updatedAt ?? Date.now()
2635
+ );
2315
2636
  }
2316
2637
  this.db.exec("COMMIT;");
2317
2638
  } catch (e) {
@@ -2328,6 +2649,30 @@ var SqliteMetadataCache = class {
2328
2649
  const stmt = this.db.prepare("DELETE FROM files WHERE id = ?");
2329
2650
  stmt.run(id);
2330
2651
  }
2652
+ pruneMissing(activeIds) {
2653
+ if (this.fallbackMemory) {
2654
+ return this.fallbackMemory.pruneMissing(activeIds);
2655
+ }
2656
+ if (!this.initialized || !this.db || activeIds.length === 0) return 0;
2657
+ const idSet = new Set(activeIds);
2658
+ const rows = this.db.prepare("SELECT id FROM files").all();
2659
+ let pruned = 0;
2660
+ this.db.exec("BEGIN TRANSACTION;");
2661
+ try {
2662
+ const deleteStmt = this.db.prepare("DELETE FROM files WHERE id = ?");
2663
+ for (const row of rows) {
2664
+ if (!idSet.has(row.id)) {
2665
+ deleteStmt.run(row.id);
2666
+ pruned++;
2667
+ }
2668
+ }
2669
+ this.db.exec("COMMIT;");
2670
+ return pruned;
2671
+ } catch (e) {
2672
+ this.db.exec("ROLLBACK;");
2673
+ throw e;
2674
+ }
2675
+ }
2331
2676
  clear() {
2332
2677
  if (this.fallbackMemory) {
2333
2678
  this.fallbackMemory.clear();
@@ -2458,6 +2803,17 @@ var MemoryMetadataCache = class {
2458
2803
  deleteFile(id) {
2459
2804
  this.files.delete(id);
2460
2805
  }
2806
+ pruneMissing(activeIds) {
2807
+ const idSet = new Set(activeIds);
2808
+ let pruned = 0;
2809
+ for (const id of Array.from(this.files.keys())) {
2810
+ if (!idSet.has(id)) {
2811
+ this.files.delete(id);
2812
+ pruned++;
2813
+ }
2814
+ }
2815
+ return pruned;
2816
+ }
2461
2817
  clear() {
2462
2818
  this.files.clear();
2463
2819
  this.meta.clear();
@@ -2516,6 +2872,18 @@ var DriveKit = class _DriveKit {
2516
2872
  this.pluginManager.use(plugin);
2517
2873
  return this;
2518
2874
  }
2875
+ /**
2876
+ * Unregisters a middleware plugin by name
2877
+ */
2878
+ unuse(pluginName) {
2879
+ return this.pluginManager.unuse(pluginName);
2880
+ }
2881
+ /**
2882
+ * Access the underlying plugin manager instance
2883
+ */
2884
+ get plugins() {
2885
+ return this.pluginManager;
2886
+ }
2519
2887
  /**
2520
2888
  * Returns a fluent, chained reference to a specific file
2521
2889
  */
@@ -2534,7 +2902,8 @@ var DriveKit = class _DriveKit {
2534
2902
  async init() {
2535
2903
  if (!this.driveClient) {
2536
2904
  const auth = await this.authenticator.getAuthClient();
2537
- this.driveClient = drive({ version: "v3", auth });
2905
+ const agent = getOptimizedHttpsAgent();
2906
+ this.driveClient = drive({ version: "v3", auth, agent });
2538
2907
  if (this.options.enablePersistentCache !== false) {
2539
2908
  this.metadataCacheInstance = new SqliteMetadataCache(this.options.cacheDatabasePath);
2540
2909
  this.metadataCacheInstance.init();
@@ -2621,14 +2990,41 @@ var DriveKit = class _DriveKit {
2621
2990
  }
2622
2991
  }
2623
2992
  /**
2624
- * Lists contents of a folder (by folder ID or path)
2993
+ * Lists contents of a folder (by folder ID or path) with opportunistic tree caching
2625
2994
  */
2626
2995
  async ls(folderIdOrPath = "/") {
2627
- if (folderIdOrPath === "/" || folderIdOrPath === "root") {
2628
- return await this.query().raw("'root' in parents or sharedWithMe = true").notTrashed().orderBy("folder,name").list();
2996
+ let parentFolderId;
2997
+ let normalizedPath = null;
2998
+ let files;
2999
+ if (folderIdOrPath === "/" || folderIdOrPath === "root" || folderIdOrPath === this.rootFolderId) {
3000
+ parentFolderId = this.rootFolderId;
3001
+ normalizedPath = "/";
3002
+ const parentFilter = this.rootFolderId === "root" ? "'root' in parents or sharedWithMe = true" : `'${this.rootFolderId}' in parents`;
3003
+ files = await this.query().raw(parentFilter).notTrashed().orderBy("folder,name").list();
3004
+ } else {
3005
+ if (folderIdOrPath.startsWith("/")) {
3006
+ normalizedPath = folderIdOrPath.replace(/\/+$/, "") || "/";
3007
+ parentFolderId = await this.resolveId(normalizedPath);
3008
+ } else {
3009
+ parentFolderId = folderIdOrPath;
3010
+ }
3011
+ files = await this.query().inFolder(parentFolderId).notTrashed().orderBy("folder,name").list();
2629
3012
  }
2630
- const folderId = await this.resolveId(folderIdOrPath);
2631
- return await this.query().inFolder(folderId).notTrashed().orderBy("folder,name").list();
3013
+ if (this.metadataCacheInstance && files.length > 0) {
3014
+ const cacheEntries = files.filter((f) => !!f.id).map((f) => ({
3015
+ id: f.id,
3016
+ name: f.name || "untitled",
3017
+ mimeType: f.mimeType || "",
3018
+ size: f.size ? parseInt(f.size, 10) : void 0,
3019
+ parentId: parentFolderId,
3020
+ path: normalizedPath ? `${normalizedPath === "/" ? "" : normalizedPath}/${f.name}` : void 0,
3021
+ md5Checksum: f.md5Checksum || void 0,
3022
+ modifiedTime: f.modifiedTime || void 0,
3023
+ trashed: false
3024
+ }));
3025
+ this.metadataCacheInstance.upsertFiles(cacheEntries);
3026
+ }
3027
+ return files;
2632
3028
  }
2633
3029
  // ==========================================
2634
3030
  // FILE OPERATIONS (UPLOAD, DOWNLOAD, GET)
@@ -2647,7 +3043,7 @@ var DriveKit = class _DriveKit {
2647
3043
  if (parentId && parentId.startsWith("/")) {
2648
3044
  parentId = await this.mkdirp(parentId);
2649
3045
  }
2650
- const targetName = opts.name || opts.filename;
3046
+ const targetName = opts.name || opts.filename || (typeof opts.source === "string" ? path8.basename(opts.source) : void 0);
2651
3047
  if (opts.overwrite && targetName && parentId) {
2652
3048
  const existing = await this.query().inFolder(parentId).nameEquals(targetName).notTrashed().first();
2653
3049
  if (existing?.id) {
@@ -2668,7 +3064,7 @@ var DriveKit = class _DriveKit {
2668
3064
  }
2669
3065
  }
2670
3066
  if (existingId) {
2671
- const fileName = targetName || path7.basename(opts.source);
3067
+ const fileName = targetName || path8.basename(opts.source);
2672
3068
  const raw = await this.getRawClient();
2673
3069
  const copyRes = await withRetry(async () => {
2674
3070
  return await raw.files.copy({
@@ -2703,12 +3099,14 @@ var DriveKit = class _DriveKit {
2703
3099
  parent: parentId
2704
3100
  });
2705
3101
  if (this.metadataCacheInstance && uploaded.id) {
3102
+ const virtualPath = typeof opts.parent === "string" && opts.parent.startsWith("/") ? `${opts.parent === "/" ? "" : opts.parent}/${uploaded.name || targetName || "untitled"}` : void 0;
2706
3103
  this.metadataCacheInstance.upsertFile({
2707
3104
  id: uploaded.id,
2708
3105
  name: uploaded.name || targetName || "untitled",
2709
3106
  mimeType: uploaded.mimeType || "",
2710
3107
  size: uploaded.size ? parseInt(uploaded.size, 10) : void 0,
2711
3108
  parentId,
3109
+ path: virtualPath,
2712
3110
  trashed: false
2713
3111
  });
2714
3112
  }
@@ -2719,16 +3117,25 @@ var DriveKit = class _DriveKit {
2719
3117
  * - Automatic conversion for Google Docs, Sheets, and Slides
2720
3118
  * - Progress events
2721
3119
  * - Saves directly to file or returns Readable Stream
3120
+ * - Leverages local metadata cache to bypass redundant Google API roundtrips
2722
3121
  */
2723
3122
  async download(fileIdOrPath, options = {}) {
2724
3123
  const fileId = await this.resolveId(fileIdOrPath);
2725
- const processedOpts = await this.pluginManager.runBeforeDownload(fileId, options);
3124
+ let cachedMeta = options.metadata;
3125
+ if (!cachedMeta && this.metadataCacheInstance) {
3126
+ const cached = this.metadataCacheInstance.getById(fileId);
3127
+ if (cached && cached.name && cached.mimeType) {
3128
+ cachedMeta = { name: cached.name, mimeType: cached.mimeType, size: cached.size };
3129
+ }
3130
+ }
3131
+ const mergedOpts = cachedMeta ? { ...options, metadata: cachedMeta } : options;
3132
+ const processedOpts = await this.pluginManager.runBeforeDownload(fileId, mergedOpts);
2726
3133
  const downloader = await this.getDownloader();
2727
3134
  const result = await downloader.download(fileId, processedOpts);
2728
3135
  return await this.pluginManager.runAfterDownload(result);
2729
3136
  }
2730
3137
  /**
2731
- * Gets file metadata
3138
+ * Gets file metadata and updates the local metadata cache
2732
3139
  */
2733
3140
  async getFile(fileIdOrPath) {
2734
3141
  const fileId = await this.resolveId(fileIdOrPath);
@@ -2740,7 +3147,20 @@ var DriveKit = class _DriveKit {
2740
3147
  supportsAllDrives: true
2741
3148
  });
2742
3149
  });
2743
- return res.data;
3150
+ const file = res.data;
3151
+ if (this.metadataCacheInstance && file.id) {
3152
+ this.metadataCacheInstance.upsertFile({
3153
+ id: file.id,
3154
+ name: file.name || "",
3155
+ mimeType: file.mimeType || "",
3156
+ size: file.size ? parseInt(file.size, 10) : void 0,
3157
+ parentId: file.parents?.[0],
3158
+ path: fileIdOrPath.startsWith("/") ? fileIdOrPath : void 0,
3159
+ modifiedTime: file.modifiedTime || void 0,
3160
+ trashed: file.trashed || false
3161
+ });
3162
+ }
3163
+ return file;
2744
3164
  }
2745
3165
  /**
2746
3166
  * Reads file content as UTF-8 string
@@ -2783,6 +3203,9 @@ var DriveKit = class _DriveKit {
2783
3203
  if (this.pathResolverInstance && fileIdOrPath.startsWith("/")) {
2784
3204
  this.pathResolverInstance.invalidatePath(fileIdOrPath);
2785
3205
  }
3206
+ if (this.metadataCacheInstance) {
3207
+ this.metadataCacheInstance.deleteFile(fileId);
3208
+ }
2786
3209
  }
2787
3210
  /**
2788
3211
  * Move item to trash
@@ -2824,6 +3247,16 @@ var DriveKit = class _DriveKit {
2824
3247
  if (this.pathResolverInstance) {
2825
3248
  this.pathResolverInstance.clearCache();
2826
3249
  }
3250
+ if (this.metadataCacheInstance) {
3251
+ const cached = this.metadataCacheInstance.getById(fileId);
3252
+ if (cached) {
3253
+ this.metadataCacheInstance.upsertFile({
3254
+ ...cached,
3255
+ parentId: newParentId,
3256
+ path: void 0
3257
+ });
3258
+ }
3259
+ }
2827
3260
  return res.data;
2828
3261
  }
2829
3262
  /**
@@ -2866,6 +3299,16 @@ var DriveKit = class _DriveKit {
2866
3299
  if (this.pathResolverInstance) {
2867
3300
  this.pathResolverInstance.clearCache();
2868
3301
  }
3302
+ if (this.metadataCacheInstance) {
3303
+ const cached = this.metadataCacheInstance.getById(fileId);
3304
+ if (cached) {
3305
+ this.metadataCacheInstance.upsertFile({
3306
+ ...cached,
3307
+ name: newName,
3308
+ path: void 0
3309
+ });
3310
+ }
3311
+ }
2869
3312
  return res.data;
2870
3313
  }
2871
3314
  // ==========================================
@@ -2955,8 +3398,8 @@ var DriveKit = class _DriveKit {
2955
3398
  const entries = await fs6.promises.readdir(dir, { withFileTypes: true });
2956
3399
  const files = [];
2957
3400
  for (const entry of entries) {
2958
- const fullPath = path7.join(dir, entry.name);
2959
- const relPath = path7.join(base, entry.name);
3401
+ const fullPath = path8.join(dir, entry.name);
3402
+ const relPath = path8.join(base, entry.name);
2960
3403
  if (entry.isDirectory()) {
2961
3404
  const sub = await collectFiles(fullPath, relPath);
2962
3405
  files.push(...sub);
@@ -2974,11 +3417,11 @@ var DriveKit = class _DriveKit {
2974
3417
  if (remoteDirCache.has(relDir)) {
2975
3418
  return remoteDirCache.get(relDir);
2976
3419
  }
2977
- const parts = relDir.split(path7.sep).filter(Boolean);
3420
+ const parts = relDir.split(path8.sep).filter(Boolean);
2978
3421
  let currentParent = targetFolderId;
2979
3422
  let currentRel = "";
2980
3423
  for (const part of parts) {
2981
- currentRel = currentRel ? path7.join(currentRel, part) : part;
3424
+ currentRel = currentRel ? path8.join(currentRel, part) : part;
2982
3425
  if (remoteDirCache.has(currentRel)) {
2983
3426
  currentParent = remoteDirCache.get(currentRel);
2984
3427
  continue;
@@ -3007,8 +3450,8 @@ var DriveKit = class _DriveKit {
3007
3450
  };
3008
3451
  await pool.map(allFiles, async (fileItem) => {
3009
3452
  try {
3010
- const relDir = path7.dirname(fileItem.relPath);
3011
- const fileName = path7.basename(fileItem.relPath);
3453
+ const relDir = path8.dirname(fileItem.relPath);
3454
+ const fileName = path8.basename(fileItem.relPath);
3012
3455
  const parentFolderId = await ensureRemoteSubdir(relDir === "." ? "" : relDir);
3013
3456
  if (options.onFileStart) {
3014
3457
  options.onFileStart(fileItem.fullPath, fileItem.relPath);
@@ -3143,9 +3586,16 @@ var DriveKit = class _DriveKit {
3143
3586
  async createWriteStream(remotePath, options = {}) {
3144
3587
  await this.init();
3145
3588
  const { PassThrough } = await import("stream");
3146
- const passThrough = new PassThrough();
3147
- const remoteDir = path7.dirname(remotePath);
3148
- const fileName = options.name || options.filename || path7.basename(remotePath);
3589
+ const passThrough = new PassThrough({ highWaterMark: 1024 * 1024 });
3590
+ let resolveDone;
3591
+ let rejectDone;
3592
+ const donePromise = new Promise((resolve, reject) => {
3593
+ resolveDone = resolve;
3594
+ rejectDone = reject;
3595
+ });
3596
+ passThrough.done = donePromise;
3597
+ const remoteDir = path8.dirname(remotePath);
3598
+ const fileName = options.name || options.filename || path8.basename(remotePath);
3149
3599
  const targetParentId = remoteDir === "." || remoteDir === "/" ? this.rootFolderId : await this.mkdirp(remoteDir);
3150
3600
  const mimeType = options.mimeType || "application/octet-stream";
3151
3601
  const raw = this.driveClient;
@@ -3162,9 +3612,10 @@ var DriveKit = class _DriveKit {
3162
3612
  fields: "id, name, mimeType, size, md5Checksum",
3163
3613
  supportsAllDrives: true
3164
3614
  }).then((res) => {
3165
- if (res.data.id && this.metadataCacheInstance) {
3615
+ const file = res.data;
3616
+ if (file.id && this.metadataCacheInstance) {
3166
3617
  this.metadataCacheInstance.upsertFile({
3167
- id: res.data.id,
3618
+ id: file.id,
3168
3619
  name: fileName,
3169
3620
  mimeType,
3170
3621
  parentId: targetParentId,
@@ -3172,14 +3623,22 @@ var DriveKit = class _DriveKit {
3172
3623
  trashed: false
3173
3624
  });
3174
3625
  }
3626
+ resolveDone(file);
3175
3627
  }).catch((err) => {
3176
3628
  passThrough.destroy(err);
3629
+ rejectDone(err);
3177
3630
  });
3178
3631
  return passThrough;
3179
3632
  }
3180
3633
  // ==========================================
3181
3634
  // METADATA CACHE OPERATIONS
3182
3635
  // ==========================================
3636
+ /**
3637
+ * Synchronous getter for the active metadata cache instance
3638
+ */
3639
+ get cache() {
3640
+ return this.metadataCacheInstance;
3641
+ }
3183
3642
  /**
3184
3643
  * Returns the active persistent metadata cache instance (SQLite/Memory)
3185
3644
  */
@@ -3202,6 +3661,9 @@ var DriveKit = class _DriveKit {
3202
3661
  if (this.pathResolverInstance) {
3203
3662
  this.pathResolverInstance.clearCache();
3204
3663
  }
3664
+ if (this.metadataCacheInstance) {
3665
+ this.metadataCacheInstance.clear();
3666
+ }
3205
3667
  }
3206
3668
  /**
3207
3669
  * Crawls and indexes Google Drive metadata into the local SQLite database for instant 0ms lookups
@@ -3214,6 +3676,7 @@ var DriveKit = class _DriveKit {
3214
3676
  const client = this.driveClient;
3215
3677
  let pageToken = void 0;
3216
3678
  let totalScanned = 0;
3679
+ const rawFiles = [];
3217
3680
  do {
3218
3681
  const res = await withRetry(async () => {
3219
3682
  return await client.files.list({
@@ -3227,21 +3690,49 @@ var DriveKit = class _DriveKit {
3227
3690
  });
3228
3691
  });
3229
3692
  const files = res.data.files || [];
3230
- const entries = files.map((f) => ({
3231
- id: f.id,
3232
- name: f.name || "untitled",
3233
- mimeType: f.mimeType || "",
3234
- size: f.size ? parseInt(f.size, 10) : void 0,
3235
- parentId: f.parents?.[0],
3236
- md5Checksum: f.md5Checksum || void 0,
3237
- trashed: !!f.trashed,
3238
- modifiedTime: f.modifiedTime || void 0
3239
- }));
3240
- this.metadataCacheInstance.upsertFiles(entries);
3241
- totalScanned += entries.length;
3693
+ for (const f of files) {
3694
+ if (f.id) rawFiles.push(f);
3695
+ }
3696
+ totalScanned += files.length;
3242
3697
  options?.onProgress?.(totalScanned);
3243
3698
  pageToken = res.data.nextPageToken || void 0;
3244
3699
  } while (pageToken);
3700
+ const fileMap = /* @__PURE__ */ new Map();
3701
+ for (const f of rawFiles) {
3702
+ fileMap.set(f.id, f);
3703
+ }
3704
+ const pathMemo = /* @__PURE__ */ new Map();
3705
+ const rootId = this.rootFolderId;
3706
+ function computePath(id, visited = /* @__PURE__ */ new Set()) {
3707
+ if (pathMemo.has(id)) return pathMemo.get(id);
3708
+ if (visited.has(id)) return "";
3709
+ visited.add(id);
3710
+ const f = fileMap.get(id);
3711
+ if (!f || !f.name) return "";
3712
+ const parentId = f.parents?.[0];
3713
+ if (!parentId || parentId === "root" || parentId === rootId) {
3714
+ const p = `/${f.name}`;
3715
+ pathMemo.set(id, p);
3716
+ return p;
3717
+ }
3718
+ const parentPath = computePath(parentId, visited);
3719
+ const fullPath = parentPath ? `${parentPath}/${f.name}` : `/${f.name}`;
3720
+ pathMemo.set(id, fullPath);
3721
+ return fullPath;
3722
+ }
3723
+ const entries = rawFiles.map((f) => ({
3724
+ id: f.id,
3725
+ name: f.name || "untitled",
3726
+ mimeType: f.mimeType || "",
3727
+ size: f.size ? parseInt(f.size, 10) : void 0,
3728
+ parentId: f.parents?.[0],
3729
+ path: computePath(f.id),
3730
+ md5Checksum: f.md5Checksum || void 0,
3731
+ trashed: !!f.trashed,
3732
+ modifiedTime: f.modifiedTime || void 0
3733
+ }));
3734
+ this.metadataCacheInstance.upsertFiles(entries);
3735
+ this.metadataCacheInstance.pruneMissing(rawFiles.map((f) => f.id));
3245
3736
  this.metadataCacheInstance.setMeta("last_sync_timestamp", (/* @__PURE__ */ new Date()).toISOString());
3246
3737
  return this.metadataCacheInstance.getStats();
3247
3738
  }
@@ -3549,11 +4040,14 @@ export {
3549
4040
  createDrive,
3550
4041
  createWebhookPlugin,
3551
4042
  index_default as default,
4043
+ escapeXml,
3552
4044
  formatBytes,
3553
4045
  formatDuration,
3554
4046
  formatSpeed,
3555
4047
  getExtensionForMime,
3556
4048
  getFileIcon,
4049
+ getOptimizedHttpAgent,
4050
+ getOptimizedHttpsAgent,
3557
4051
  isGoogleWorkspaceDoc,
3558
4052
  isRetryableError,
3559
4053
  loginWithBrowser,