@d0whc3r/node-gdrive 3.5.0 → 3.6.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/README.md +101 -15
- package/bin/cli.js +2 -2
- package/dist/db/index.d.ts +19 -0
- package/dist/db/providers/mongodb.d.ts +2 -0
- package/dist/db/providers/mysql.d.ts +2 -0
- package/dist/db/providers/postgres.d.ts +2 -0
- package/dist/db/providers/sqlite.d.ts +2 -0
- package/dist/db/spawn.d.ts +29 -0
- package/dist/db/types.d.ts +56 -0
- package/dist/gdrive.cjs.js +3 -1
- package/dist/gdrive.esm.mjs +533 -152
- package/dist/index.d.ts +4 -0
- package/package.json +1 -1
package/dist/gdrive.esm.mjs
CHANGED
|
@@ -10,17 +10,22 @@ import l from "node:crypto";
|
|
|
10
10
|
import u from "node:http";
|
|
11
11
|
import { styleText as d } from "node:util";
|
|
12
12
|
import { CodeChallengeMethod as f, GoogleAuth as p, OAuth2Client as m } from "google-auth-library";
|
|
13
|
+
import h from "node:fs/promises";
|
|
14
|
+
import { spawn as g } from "node:child_process";
|
|
15
|
+
import { PassThrough as _ } from "node:stream";
|
|
16
|
+
import v from "mysqldump";
|
|
17
|
+
import { DatabaseSync as y, backup as ee } from "node:sqlite";
|
|
13
18
|
// \0rolldown/runtime.js
|
|
14
|
-
var
|
|
15
|
-
if (t && typeof t == "object" || typeof t == "function") for (var i =
|
|
19
|
+
var te = Object.create, b = Object.defineProperty, ne = Object.getOwnPropertyDescriptor, re = Object.getOwnPropertyNames, ie = Object.getPrototypeOf, x = Object.prototype.hasOwnProperty, S = (e, t) => () => (t || (e((t = { exports: {} }).exports, t, t.exports), e = null), t.exports), ae = (e, t, n, r) => {
|
|
20
|
+
if (t && typeof t == "object" || typeof t == "function") for (var i = re(t), a = 0, o = i.length, s; a < o; a++) s = i[a], !x.call(e, s) && s !== n && b(e, s, {
|
|
16
21
|
get: ((e) => t[e]).bind(null, s),
|
|
17
|
-
enumerable: !(r =
|
|
22
|
+
enumerable: !(r = ne(t, s)) || r.enumerable
|
|
18
23
|
});
|
|
19
24
|
return e;
|
|
20
|
-
},
|
|
25
|
+
}, oe = /* @__PURE__ */ ((e, t, n) => (n = e == null ? {} : te(ie(e)), ae(t || !e || !e.__esModule || !x.call(e, "default") ? b(n, "default", {
|
|
21
26
|
value: e,
|
|
22
27
|
enumerable: !0
|
|
23
|
-
}) : n, e)))((/* @__PURE__ */
|
|
28
|
+
}) : n, e)))((/* @__PURE__ */ S(((e, t, n) => {
|
|
24
29
|
(function(n, r) {
|
|
25
30
|
typeof e == "object" && t !== void 0 ? t.exports = r() : typeof define == "function" && define.amd ? define(r) : (n = typeof globalThis < "u" ? globalThis : n || self).dayjs_plugin_isSameOrBefore = r();
|
|
26
31
|
})(n, (function() {
|
|
@@ -30,7 +35,7 @@ var h = Object.create, g = Object.defineProperty, _ = Object.getOwnPropertyDescr
|
|
|
30
35
|
};
|
|
31
36
|
};
|
|
32
37
|
}));
|
|
33
|
-
})))()),
|
|
38
|
+
})))()), C = class {
|
|
34
39
|
static _tokenFile;
|
|
35
40
|
static _credentialsFile;
|
|
36
41
|
static _serviceAccountFile;
|
|
@@ -65,7 +70,7 @@ var h = Object.create, g = Object.defineProperty, _ = Object.getOwnPropertyDescr
|
|
|
65
70
|
static reset() {
|
|
66
71
|
this._tokenFile = void 0, this._credentialsFile = void 0, this._serviceAccountFile = void 0, this._tag = void 0;
|
|
67
72
|
}
|
|
68
|
-
},
|
|
73
|
+
}, w = ["https://www.googleapis.com/auth/drive"], T = class {
|
|
69
74
|
ready = !0;
|
|
70
75
|
scopes;
|
|
71
76
|
tokenFile;
|
|
@@ -76,7 +81,7 @@ var h = Object.create, g = Object.defineProperty, _ = Object.getOwnPropertyDescr
|
|
|
76
81
|
_oAuth2Client;
|
|
77
82
|
_googleAuth;
|
|
78
83
|
constructor(e = {}) {
|
|
79
|
-
this.tokenFile = e.tokenFile ??
|
|
84
|
+
this.tokenFile = e.tokenFile ?? C.TOKEN_FILE, this.credentialsFile = e.credentialsFile ?? C.CREDENTIALS_FILE, this.serviceAccountFile = e.serviceAccountFile ?? C.SERVICE_ACCOUNT_FILE, this.useADC = e.useADC ?? !1, this.scopes = e.scopes ?? w, this.logger = e.logger ?? console, this.serviceAccountFile ? t.existsSync(this.serviceAccountFile) || (this.ready = !1, this.logger.error(d(["bold", "red"], `${C.TAG} ERROR: Service account file not found in ${this.serviceAccountFile}`))) : !this.useADC && !this.existsCredentials && (this.ready = !1, this.logger.error(d(["bold", "red"], `${C.TAG} ERROR: Credentials file not found in ${this.credentialsFile}`)));
|
|
80
85
|
}
|
|
81
86
|
get isServiceAccountOrADC() {
|
|
82
87
|
return !!this.serviceAccountFile || this.useADC;
|
|
@@ -90,7 +95,7 @@ var h = Object.create, g = Object.defineProperty, _ = Object.getOwnPropertyDescr
|
|
|
90
95
|
async initiate() {
|
|
91
96
|
if (!this.ready) {
|
|
92
97
|
let e = this.serviceAccountFile ?? this.credentialsFile;
|
|
93
|
-
throw Error(`${
|
|
98
|
+
throw Error(`${C.TAG} Credentials file not found in ${e}`);
|
|
94
99
|
}
|
|
95
100
|
return this.isServiceAccountOrADC ? (this.googleAuthClient(), !0) : this.existsToken ? (this.oAuth2Client(!0), !0) : this.generateAccessToken();
|
|
96
101
|
}
|
|
@@ -112,7 +117,7 @@ var h = Object.create, g = Object.defineProperty, _ = Object.getOwnPropertyDescr
|
|
|
112
117
|
oAuth2Client(e = !0, n) {
|
|
113
118
|
if (!this._oAuth2Client) {
|
|
114
119
|
let r = JSON.parse(t.readFileSync(this.credentialsFile, "utf8")), i = r.installed ?? r.web;
|
|
115
|
-
if (!i) throw Error(`${
|
|
120
|
+
if (!i) throw Error(`${C.TAG} Invalid credentials file format in ${this.credentialsFile}`);
|
|
116
121
|
let { client_secret: a, client_id: o, redirect_uris: s } = i;
|
|
117
122
|
if (this._oAuth2Client = new m(o, a, n ?? s?.[0] ?? "http://127.0.0.1"), this.attachTokenListener(this._oAuth2Client), e && this.existsToken) {
|
|
118
123
|
let e = JSON.parse(t.readFileSync(this.tokenFile, "utf8"));
|
|
@@ -133,7 +138,7 @@ var h = Object.create, g = Object.defineProperty, _ = Object.getOwnPropertyDescr
|
|
|
133
138
|
};
|
|
134
139
|
this.persistTokenSync(r);
|
|
135
140
|
} catch (e) {
|
|
136
|
-
this.logger.warn(`${
|
|
141
|
+
this.logger.warn(`${C.TAG} Failed to persist refreshed token:`, e);
|
|
137
142
|
}
|
|
138
143
|
});
|
|
139
144
|
}
|
|
@@ -150,7 +155,7 @@ var h = Object.create, g = Object.defineProperty, _ = Object.getOwnPropertyDescr
|
|
|
150
155
|
o || (o = !0, a.close(), e());
|
|
151
156
|
};
|
|
152
157
|
a.on("error", (e) => {
|
|
153
|
-
s(() => i(Error(`${
|
|
158
|
+
s(() => i(Error(`${C.TAG} OAuth loopback server failed`, { cause: e })));
|
|
154
159
|
}), a.on("request", (n, o) => {
|
|
155
160
|
(async () => {
|
|
156
161
|
try {
|
|
@@ -161,15 +166,15 @@ var h = Object.create, g = Object.defineProperty, _ = Object.getOwnPropertyDescr
|
|
|
161
166
|
}
|
|
162
167
|
let l = c.searchParams.get("state"), u = c.searchParams.get("code"), d = c.searchParams.get("error");
|
|
163
168
|
if (d) {
|
|
164
|
-
o.writeHead(400, { "Content-Type": "text/plain" }), o.end(`Authorization failed: ${d}`), s(() => i(/* @__PURE__ */ Error(`${
|
|
169
|
+
o.writeHead(400, { "Content-Type": "text/plain" }), o.end(`Authorization failed: ${d}`), s(() => i(/* @__PURE__ */ Error(`${C.TAG} OAuth authorization error: ${d}`)));
|
|
165
170
|
return;
|
|
166
171
|
}
|
|
167
172
|
if (!l || l !== e) {
|
|
168
|
-
o.writeHead(400, { "Content-Type": "text/plain" }), o.end("Invalid OAuth state parameter"), s(() => i(/* @__PURE__ */ Error(`${
|
|
173
|
+
o.writeHead(400, { "Content-Type": "text/plain" }), o.end("Invalid OAuth state parameter"), s(() => i(/* @__PURE__ */ Error(`${C.TAG} OAuth state mismatch`)));
|
|
169
174
|
return;
|
|
170
175
|
}
|
|
171
176
|
if (!u) {
|
|
172
|
-
o.writeHead(400, { "Content-Type": "text/plain" }), o.end("Missing authorization code"), s(() => i(/* @__PURE__ */ Error(`${
|
|
177
|
+
o.writeHead(400, { "Content-Type": "text/plain" }), o.end("Missing authorization code"), s(() => i(/* @__PURE__ */ Error(`${C.TAG} Missing OAuth authorization code`)));
|
|
173
178
|
return;
|
|
174
179
|
}
|
|
175
180
|
let f = `http://127.0.0.1:${a.address().port}/oauth2callback`, p = this.oAuth2Client(!1, f), { tokens: m } = await p.getToken({
|
|
@@ -177,7 +182,7 @@ var h = Object.create, g = Object.defineProperty, _ = Object.getOwnPropertyDescr
|
|
|
177
182
|
codeVerifier: t,
|
|
178
183
|
redirect_uri: f
|
|
179
184
|
});
|
|
180
|
-
p.setCredentials(m), this.persistTokenSync(m), o.writeHead(200, { "Content-Type": "text/plain" }), o.end("Authentication successful! You can close this window."), this.logger.info(`${
|
|
185
|
+
p.setCredentials(m), this.persistTokenSync(m), o.writeHead(200, { "Content-Type": "text/plain" }), o.end("Authentication successful! You can close this window."), this.logger.info(`${C.TAG} Token stored to`, this.tokenFile), s(() => r(!0));
|
|
181
186
|
} catch (e) {
|
|
182
187
|
o.writeHead(500, { "Content-Type": "text/plain" }), o.end("Error exchanging OAuth authorization code."), s(() => i(e instanceof Error ? e : Error(String(e))));
|
|
183
188
|
}
|
|
@@ -192,38 +197,38 @@ var h = Object.create, g = Object.defineProperty, _ = Object.getOwnPropertyDescr
|
|
|
192
197
|
code_challenge: n,
|
|
193
198
|
code_challenge_method: f.S256
|
|
194
199
|
});
|
|
195
|
-
this.logger.info(`${
|
|
200
|
+
this.logger.info(`${C.TAG} Authorize this app by visiting this url:`, r);
|
|
196
201
|
});
|
|
197
202
|
});
|
|
198
203
|
}
|
|
199
|
-
},
|
|
200
|
-
function
|
|
204
|
+
}, E = Buffer.from("NGDE", "ascii"), D = 1, O = 16, k = 12, A = 16, se = 32, j = E.length + 1 + O + k;
|
|
205
|
+
function M(e, t) {
|
|
201
206
|
if (!e) throw Error("Encryption passphrase must not be empty");
|
|
202
|
-
return l.scryptSync(e, t,
|
|
207
|
+
return l.scryptSync(e, t, se);
|
|
203
208
|
}
|
|
204
|
-
function
|
|
205
|
-
let n = l.randomBytes(
|
|
209
|
+
function ce(e, t) {
|
|
210
|
+
let n = l.randomBytes(O), r = l.randomBytes(k), i = M(t, n), a = l.createCipheriv("aes-256-gcm", i, r), o = Buffer.concat([a.update(e), a.final()]), s = a.getAuthTag();
|
|
206
211
|
return Buffer.concat([
|
|
207
|
-
|
|
208
|
-
Buffer.from([
|
|
212
|
+
E,
|
|
213
|
+
Buffer.from([D]),
|
|
209
214
|
n,
|
|
210
215
|
r,
|
|
211
216
|
o,
|
|
212
217
|
s
|
|
213
218
|
]);
|
|
214
219
|
}
|
|
215
|
-
function
|
|
216
|
-
if (e.length <
|
|
217
|
-
if (!e.subarray(0,
|
|
218
|
-
let n = e[
|
|
219
|
-
if (n !==
|
|
220
|
-
let r =
|
|
220
|
+
function N(e, t) {
|
|
221
|
+
if (e.length < j + A) throw Error("Encrypted payload is too short or corrupted");
|
|
222
|
+
if (!e.subarray(0, E.length).equals(E)) throw Error("Invalid encrypted file header");
|
|
223
|
+
let n = e[E.length];
|
|
224
|
+
if (n !== D) throw Error(`Unsupported encryption format version: ${String(n)}`);
|
|
225
|
+
let r = E.length + 1, i = r + O, a = e.subarray(r, i), o = e.subarray(i, j), s = e.subarray(j, e.length - A), c = e.subarray(e.length - A), u = M(t, a), d = l.createDecipheriv("aes-256-gcm", u, o);
|
|
221
226
|
return d.setAuthTag(c), Buffer.concat([d.update(s), d.final()]);
|
|
222
227
|
}
|
|
223
|
-
async function
|
|
224
|
-
let o = l.randomBytes(
|
|
225
|
-
|
|
226
|
-
Buffer.from([
|
|
228
|
+
async function P(t, n, a) {
|
|
229
|
+
let o = l.randomBytes(O), s = l.randomBytes(k), c = M(a, o), u = Buffer.concat([
|
|
230
|
+
E,
|
|
231
|
+
Buffer.from([D]),
|
|
227
232
|
o,
|
|
228
233
|
s
|
|
229
234
|
]);
|
|
@@ -242,24 +247,24 @@ async function L(t, n, a) {
|
|
|
242
247
|
throw f.destroy(), p.destroy(), e.rmSync(n, { force: !0 }), Error(`Failed to encrypt file "${t}"`, { cause: r });
|
|
243
248
|
}
|
|
244
249
|
}
|
|
245
|
-
async function
|
|
250
|
+
async function F(t, n, a) {
|
|
246
251
|
let o = e.statSync(t);
|
|
247
|
-
if (o.size <
|
|
248
|
-
let s = e.openSync(t, "r"), c = Buffer.alloc(
|
|
252
|
+
if (o.size < j + A) throw Error("Encrypted file is too short or corrupted");
|
|
253
|
+
let s = e.openSync(t, "r"), c = Buffer.alloc(j), u = Buffer.alloc(A);
|
|
249
254
|
try {
|
|
250
|
-
e.readSync(s, c, 0,
|
|
255
|
+
e.readSync(s, c, 0, j, 0), e.readSync(s, u, 0, A, o.size - A);
|
|
251
256
|
} finally {
|
|
252
257
|
e.closeSync(s);
|
|
253
258
|
}
|
|
254
|
-
if (!c.subarray(0,
|
|
255
|
-
let d = c[
|
|
256
|
-
if (d !==
|
|
257
|
-
let f =
|
|
259
|
+
if (!c.subarray(0, E.length).equals(E)) throw Error("Invalid encrypted file header");
|
|
260
|
+
let d = c[E.length];
|
|
261
|
+
if (d !== D) throw Error(`Unsupported encryption format version: ${String(d)}`);
|
|
262
|
+
let f = E.length + 1, p = f + O, m = c.subarray(f, p), h = c.subarray(p, j), g = M(a, m), _ = l.createDecipheriv("aes-256-gcm", g, h);
|
|
258
263
|
_.setAuthTag(u), e.mkdirSync(r.dirname(n), {
|
|
259
264
|
recursive: !0,
|
|
260
265
|
mode: 448
|
|
261
266
|
});
|
|
262
|
-
let v = o.size -
|
|
267
|
+
let v = o.size - j - A, y = e.createWriteStream(n, { mode: 384 });
|
|
263
268
|
try {
|
|
264
269
|
if (v === 0) {
|
|
265
270
|
let e = _.final();
|
|
@@ -268,8 +273,8 @@ async function R(t, n, a) {
|
|
|
268
273
|
});
|
|
269
274
|
} else {
|
|
270
275
|
let n = e.createReadStream(t, {
|
|
271
|
-
start:
|
|
272
|
-
end: o.size -
|
|
276
|
+
start: j,
|
|
277
|
+
end: o.size - A - 1
|
|
273
278
|
});
|
|
274
279
|
await i(n, _, y);
|
|
275
280
|
}
|
|
@@ -279,13 +284,13 @@ async function R(t, n, a) {
|
|
|
279
284
|
}
|
|
280
285
|
}
|
|
281
286
|
// src/retry.ts
|
|
282
|
-
var
|
|
287
|
+
var le = /* @__PURE__ */ new Set([
|
|
283
288
|
429,
|
|
284
289
|
500,
|
|
285
290
|
502,
|
|
286
291
|
503,
|
|
287
292
|
504
|
|
288
|
-
]),
|
|
293
|
+
]), ue = /* @__PURE__ */ new Set([
|
|
289
294
|
"ECONNRESET",
|
|
290
295
|
"ETIMEDOUT",
|
|
291
296
|
"ENOTFOUND",
|
|
@@ -293,33 +298,407 @@ var z = /* @__PURE__ */ new Set([
|
|
|
293
298
|
"EPIPE",
|
|
294
299
|
"UND_ERR_CONNECT_TIMEOUT"
|
|
295
300
|
]);
|
|
296
|
-
function
|
|
301
|
+
function de(e) {
|
|
297
302
|
return new Promise((t) => {
|
|
298
303
|
setTimeout(t, e);
|
|
299
304
|
});
|
|
300
305
|
}
|
|
301
|
-
function
|
|
306
|
+
function fe(e) {
|
|
307
|
+
if (typeof e.status == "number") return e.status;
|
|
308
|
+
if (typeof e.code == "number") return e.code;
|
|
309
|
+
let t = e.response;
|
|
310
|
+
if (typeof t?.status == "number") return t.status;
|
|
311
|
+
}
|
|
312
|
+
function I(e) {
|
|
302
313
|
if (!e || typeof e != "object") return !1;
|
|
303
|
-
let t = e, n =
|
|
304
|
-
if (n !== void 0 &&
|
|
314
|
+
let t = e, n = fe(t);
|
|
315
|
+
if (n !== void 0 && le.has(n)) return !0;
|
|
305
316
|
let r = typeof t.code == "string" ? t.code : void 0;
|
|
306
|
-
return !!(r &&
|
|
317
|
+
return !!(r && ue.has(r));
|
|
318
|
+
}
|
|
319
|
+
async function L(e, t = {}) {
|
|
320
|
+
let { maxRetries: n = 4, baseDelayMs: r = 500, maxDelayMs: i = 16e3, jitter: a = !0, sleep: o = de } = t, s = async (t) => {
|
|
321
|
+
try {
|
|
322
|
+
return await e();
|
|
323
|
+
} catch (e) {
|
|
324
|
+
if (t >= n || !I(e)) throw e;
|
|
325
|
+
let c = Math.min(i, r * 2 ** t), l = a ? Math.round(c * (.5 + Math.random() * .5)) : c;
|
|
326
|
+
return await o(l), s(t + 1);
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
return s(0);
|
|
330
|
+
}
|
|
331
|
+
// src/types.ts
|
|
332
|
+
var R = /* @__PURE__ */ "appProperties.capabilities.contentHints.createdTime.description.explicitlyTrashed.fileExtension.folderColorRgb.fullFileExtension.hasAugmentedPermissions.hasThumbnail.headRevisionId.iconLink.id.imageMediaMetadata.isAppAuthorized.lastModifyingUser.md5Checksum.mimeType.modifiedByMe.modifiedByMeTime.modifiedTime.name.originalFilename.ownedByMe.owners.parents.properties.quotaBytesUsed.shared.sharedWithMeTime.sharingUser.size.spaces.starred.teamDriveId.thumbnailLink.thumbnailVersion.trashed.trashedTime.trashingUser.version.videoMediaMetadata.viewedByMe.viewedByMeTime.viewersCanCopyContent.webContentLink.webViewLink.writersCanShare".split("."), z = 16384;
|
|
333
|
+
function B(e) {
|
|
334
|
+
let t = (process.env.PATH || "").split(r.delimiter).filter(Boolean), n = process.platform === "win32" ? (process.env.PATHEXT || ".EXE;.CMD;.BAT").split(";") : [""];
|
|
335
|
+
for (let i of e) if (i.includes(r.sep) || i.includes("/")) {
|
|
336
|
+
if (V(i)) return i;
|
|
337
|
+
} else for (let e of t) for (let t of n) {
|
|
338
|
+
let n = r.join(e, `${i}${t}`);
|
|
339
|
+
if (V(n)) return n;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
function V(t) {
|
|
343
|
+
try {
|
|
344
|
+
return e.statSync(t).isFile() ? (e.accessSync(t, e.constants.X_OK), !0) : !1;
|
|
345
|
+
} catch {
|
|
346
|
+
return !1;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
function pe(e) {
|
|
350
|
+
return e.replace(/([a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^:@/\s]+):([^@/\s]+)@/g, "$1:***@");
|
|
351
|
+
}
|
|
352
|
+
function H(e) {
|
|
353
|
+
return e.replace(/[^a-zA-Z0-9._-]/g, "_").replace(/^_+|_+$/g, "") || "db";
|
|
354
|
+
}
|
|
355
|
+
function U(e) {
|
|
356
|
+
let t = g(e.binary, e.args, {
|
|
357
|
+
env: e.env ? {
|
|
358
|
+
...process.env,
|
|
359
|
+
...e.env
|
|
360
|
+
} : process.env,
|
|
361
|
+
stdio: [
|
|
362
|
+
"ignore",
|
|
363
|
+
"pipe",
|
|
364
|
+
"pipe"
|
|
365
|
+
],
|
|
366
|
+
shell: !1
|
|
367
|
+
}), n = new _(), r = [], i = 0, a, o = () => (a ||= Promise.resolve(e.onCleanup?.()), a);
|
|
368
|
+
t.stderr.on("data", (e) => {
|
|
369
|
+
if (i < z) {
|
|
370
|
+
let t = z - i;
|
|
371
|
+
r.push(e.subarray(0, t)), i += Math.min(e.length, t);
|
|
372
|
+
}
|
|
373
|
+
}), t.stdout.pipe(n, { end: !1 });
|
|
374
|
+
let s = !1, c, l, u = new Promise((e, t) => {
|
|
375
|
+
c = e, l = t;
|
|
376
|
+
});
|
|
377
|
+
u.catch(() => void 0);
|
|
378
|
+
let d = (e) => {
|
|
379
|
+
s || (s = !0, o(), n.destroyed || n.destroy(e), l(e));
|
|
380
|
+
};
|
|
381
|
+
return t.on("error", (t) => {
|
|
382
|
+
t.code === "ENOENT" ? d(/* @__PURE__ */ Error(`${e.tag} "${e.binary}" not found in PATH. ${e.installHint}`)) : d(/* @__PURE__ */ Error(`${e.tag} Failed to start "${e.binary}": ${t.message}`));
|
|
383
|
+
}), t.on("close", (t, i) => {
|
|
384
|
+
if (s) return;
|
|
385
|
+
if (t === 0) {
|
|
386
|
+
s = !0, o(), n.end(), c();
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
let a = Buffer.concat(r).toString("utf8").trim(), l = a ? `: ${pe(a)}` : "", u = i ? `terminated by signal ${i}` : `exited with code ${t ?? "unknown"}`;
|
|
390
|
+
d(/* @__PURE__ */ Error(`${e.tag} ${e.binary} ${u}${l}`));
|
|
391
|
+
}), {
|
|
392
|
+
kind: "stream",
|
|
393
|
+
engine: e.engine,
|
|
394
|
+
filename: e.filename,
|
|
395
|
+
mimeType: e.mimeType,
|
|
396
|
+
stream: n,
|
|
397
|
+
wait: () => u,
|
|
398
|
+
cleanup: async () => {
|
|
399
|
+
!s && !t.killed && t.kill("SIGTERM"), n.destroyed || n.destroy(), await o();
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
// src/db/providers/mongodb.ts
|
|
404
|
+
function W(e) {
|
|
405
|
+
return !!(e && /^mongodb(?:\+srv)?:\/\//i.test(e));
|
|
406
|
+
}
|
|
407
|
+
function G(e) {
|
|
408
|
+
return JSON.stringify(e);
|
|
409
|
+
}
|
|
410
|
+
var me = {
|
|
411
|
+
engine: "mongodb",
|
|
412
|
+
aliases: ["mongo"],
|
|
413
|
+
async dump(e) {
|
|
414
|
+
let t = e.tag ?? "[db]", i = B(["mongodump"]);
|
|
415
|
+
if (!i) throw Error(`${t} "mongodump" not found in PATH. Install mongodb-database-tools or use the :db Docker image.`);
|
|
416
|
+
let a = e.url || process.env.MONGODB_URI || (W(process.env.DATABASE_URL) ? process.env.DATABASE_URL : void 0), o = ["--archive"], c = "mongodb", l = "";
|
|
417
|
+
if (a) {
|
|
418
|
+
let t = "";
|
|
419
|
+
try {
|
|
420
|
+
t = new URL(a).pathname.replace(/^\/+/, "");
|
|
421
|
+
} catch {}
|
|
422
|
+
c = e.database || t || "mongodb", l = `uri: ${G(a)}\n`, e.database && o.push("--db", e.database);
|
|
423
|
+
} else {
|
|
424
|
+
let n = e.host || process.env.MONGO_HOST || "localhost", r = e.port ?? (process.env.MONGO_PORT ? Number(process.env.MONGO_PORT) : 27017), i = e.user || process.env.MONGO_USER || "", a = e.password ?? process.env.MONGO_PASSWORD ?? "", s = e.database || process.env.MONGO_DATABASE || "", u = process.env.MONGO_AUTH_DB || "";
|
|
425
|
+
if (!s && !e.host && !process.env.MONGO_HOST) throw Error(`${t} Missing MongoDB configuration. Set $MONGODB_URI (or $MONGO_DATABASE / $MONGO_HOST / --db-url).`);
|
|
426
|
+
c = s || "all", o.push("--host", n, "--port", String(r)), s && o.push("--db", s), i && o.push("--username", i), u && o.push("--authenticationDatabase", u), a && (l = `password: ${G(a)}\n`);
|
|
427
|
+
}
|
|
428
|
+
let u;
|
|
429
|
+
if (l) {
|
|
430
|
+
u = await h.mkdtemp(r.join(n.tmpdir(), "db-mongo-cfg-")), await h.chmod(u, 448);
|
|
431
|
+
let e = r.join(u, "mongodump.conf");
|
|
432
|
+
await h.writeFile(e, l, { mode: 384 }), o.push(`--config=${e}`);
|
|
433
|
+
}
|
|
434
|
+
return e.extraArgs?.length && o.push(...e.extraArgs), U({
|
|
435
|
+
engine: "mongodb",
|
|
436
|
+
binary: i,
|
|
437
|
+
args: o,
|
|
438
|
+
filename: e.filename || `mongodump-${H(c)}-${s().format("YYYY-MM-DD.HHmmss")}.archive`,
|
|
439
|
+
mimeType: "application/octet-stream",
|
|
440
|
+
tag: t,
|
|
441
|
+
installHint: "Install mongodb-database-tools or use the :db Docker image.",
|
|
442
|
+
onCleanup: async () => {
|
|
443
|
+
u && await h.rm(u, {
|
|
444
|
+
recursive: !0,
|
|
445
|
+
force: !0
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
// src/db/providers/mysql.ts
|
|
452
|
+
function he(e) {
|
|
453
|
+
return !!(e && /^(?:mysql|mariadb):\/\//i.test(e));
|
|
454
|
+
}
|
|
455
|
+
function ge(e, t) {
|
|
456
|
+
let n = e.url || (he(process.env.DATABASE_URL) ? process.env.DATABASE_URL : void 0);
|
|
457
|
+
if (n) {
|
|
458
|
+
let r = new URL(n), i = e.host || r.hostname || "localhost", a = e.port ?? (r.port ? Number(r.port) : 3306), o = e.user || decodeURIComponent(r.username || ""), s = e.password ?? decodeURIComponent(r.password || ""), c = e.database || r.pathname.replace(/^\/+/, "");
|
|
459
|
+
if (!o || !c) throw Error(`${t} Invalid MySQL/MariaDB URL: username and database name are required.`);
|
|
460
|
+
return {
|
|
461
|
+
host: i,
|
|
462
|
+
port: a,
|
|
463
|
+
user: o,
|
|
464
|
+
password: s,
|
|
465
|
+
database: c
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
let r = e.host || process.env.MYSQL_HOST || "localhost", i = e.port ?? (process.env.MYSQL_PORT ? Number(process.env.MYSQL_PORT) : 3306), a = e.user || process.env.MYSQL_USER || "", o = e.password ?? process.env.MYSQL_PASSWORD ?? "", s = e.database || process.env.MYSQL_DATABASE || "";
|
|
469
|
+
if (!a || !o || !s) throw Error(`${t} Missing required MySQL environment variables ($MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_HOST, $MYSQL_PORT) or --db-url.`);
|
|
470
|
+
return {
|
|
471
|
+
host: r,
|
|
472
|
+
port: i,
|
|
473
|
+
user: a,
|
|
474
|
+
password: o,
|
|
475
|
+
database: s
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
async function _e(e, t) {
|
|
479
|
+
let { dump: i } = await v({ connection: {
|
|
480
|
+
host: e.host,
|
|
481
|
+
port: e.port,
|
|
482
|
+
user: e.user,
|
|
483
|
+
password: e.password,
|
|
484
|
+
database: e.database
|
|
485
|
+
} }), a = await h.mkdtemp(r.join(n.tmpdir(), "db-mysql-"));
|
|
486
|
+
await h.chmod(a, 448);
|
|
487
|
+
let o = r.join(a, t), s = Object.values(i).filter((e) => !!e).map((e) => e.replace(/^# /gm, "-- ")).join("\n\n");
|
|
488
|
+
return await h.writeFile(o, s, { mode: 384 }), {
|
|
489
|
+
kind: "file",
|
|
490
|
+
engine: "mysql",
|
|
491
|
+
filename: t,
|
|
492
|
+
mimeType: "application/sql",
|
|
493
|
+
filePath: o,
|
|
494
|
+
tempDir: a,
|
|
495
|
+
cleanup: async () => {
|
|
496
|
+
await h.rm(a, {
|
|
497
|
+
recursive: !0,
|
|
498
|
+
force: !0
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
var ve = {
|
|
504
|
+
engine: "mysql",
|
|
505
|
+
aliases: ["mariadb"],
|
|
506
|
+
async dump(e) {
|
|
507
|
+
let t = e.tag ?? "[db]", n = ge(e, t), r = e.filename || `mysqldump-${H(n.database)}-${s().format("YYYY-MM-DD.HHmmss")}.sql`, i = B(["mysqldump", "mariadb-dump"]);
|
|
508
|
+
if (!i) return _e(n, r);
|
|
509
|
+
let a = [
|
|
510
|
+
"--single-transaction",
|
|
511
|
+
"--quick",
|
|
512
|
+
"--routines",
|
|
513
|
+
"--triggers",
|
|
514
|
+
"-h",
|
|
515
|
+
n.host,
|
|
516
|
+
"-P",
|
|
517
|
+
String(n.port),
|
|
518
|
+
"-u",
|
|
519
|
+
n.user,
|
|
520
|
+
...e.extraArgs ?? [],
|
|
521
|
+
n.database
|
|
522
|
+
], o = {};
|
|
523
|
+
return n.password && (o.MYSQL_PWD = n.password), U({
|
|
524
|
+
engine: "mysql",
|
|
525
|
+
binary: i,
|
|
526
|
+
args: a,
|
|
527
|
+
env: o,
|
|
528
|
+
filename: r,
|
|
529
|
+
mimeType: "application/sql",
|
|
530
|
+
tag: t,
|
|
531
|
+
installHint: "Install mariadb-client / mysql-client or use the :db Docker image."
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
}, ye = {
|
|
535
|
+
plain: {
|
|
536
|
+
ext: "sql",
|
|
537
|
+
mimeType: "application/sql"
|
|
538
|
+
},
|
|
539
|
+
custom: {
|
|
540
|
+
ext: "dump",
|
|
541
|
+
mimeType: "application/octet-stream"
|
|
542
|
+
},
|
|
543
|
+
tar: {
|
|
544
|
+
ext: "tar",
|
|
545
|
+
mimeType: "application/x-tar"
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
function be(e) {
|
|
549
|
+
return !!(e && /^postgres(?:ql)?:\/\//i.test(e));
|
|
550
|
+
}
|
|
551
|
+
function xe(e) {
|
|
552
|
+
let t = new URL(e), n = t.password ? decodeURIComponent(t.password) : void 0, r = t.pathname.replace(/^\/+/, "") || "postgres";
|
|
553
|
+
return t.password = "", {
|
|
554
|
+
sanitizedDbNameArg: t.toString(),
|
|
555
|
+
password: n,
|
|
556
|
+
database: r
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
var Se = {
|
|
560
|
+
engine: "postgres",
|
|
561
|
+
aliases: ["pg", "postgresql"],
|
|
562
|
+
async dump(e) {
|
|
563
|
+
let t = e.tag ?? "[db]", n = e.format ?? "plain", r = ye[n];
|
|
564
|
+
if (!r) throw Error(`${t} Unsupported PostgreSQL dump format "${String(n)}". Expected one of: plain, custom, tar.`);
|
|
565
|
+
let i = B(["pg_dump"]);
|
|
566
|
+
if (!i) throw Error(`${t} "pg_dump" not found in PATH. Install postgresql-client (e.g. apt install postgresql-client / brew install libpq) or use the :db Docker image.`);
|
|
567
|
+
let a = e.url || (be(process.env.DATABASE_URL) ? process.env.DATABASE_URL : void 0), o = ["--no-password", `--format=${n}`], c = {}, l;
|
|
568
|
+
if (a) {
|
|
569
|
+
let t = xe(a);
|
|
570
|
+
l = e.database || t.database, (e.password ?? t.password) && (c.PGPASSWORD = e.password ?? t.password), o.push(`--dbname=${t.sanitizedDbNameArg}`);
|
|
571
|
+
} else {
|
|
572
|
+
let n = e.host || process.env.PGHOST || "localhost", r = e.port ?? (process.env.PGPORT ? Number(process.env.PGPORT) : 5432), i = e.user || process.env.PGUSER || "", a = e.password ?? process.env.PGPASSWORD, o = e.database || process.env.PGDATABASE || "";
|
|
573
|
+
if (!o) throw Error(`${t} Missing PostgreSQL database configuration. Set $PGDATABASE (or $DATABASE_URL / --db-url).`);
|
|
574
|
+
l = o, c.PGHOST = n, c.PGPORT = String(r), i && (c.PGUSER = i), a !== void 0 && a !== "" && (c.PGPASSWORD = a), c.PGDATABASE = o;
|
|
575
|
+
}
|
|
576
|
+
return e.extraArgs?.length && o.push(...e.extraArgs), U({
|
|
577
|
+
engine: "postgres",
|
|
578
|
+
binary: i,
|
|
579
|
+
args: o,
|
|
580
|
+
env: c,
|
|
581
|
+
filename: e.filename || `pgdump-${H(l)}-${s().format("YYYY-MM-DD.HHmmss")}.${r.ext}`,
|
|
582
|
+
mimeType: r.mimeType,
|
|
583
|
+
tag: t,
|
|
584
|
+
installHint: "Install postgresql-client or use the :db Docker image."
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
// src/db/providers/sqlite.ts
|
|
589
|
+
function Ce(e) {
|
|
590
|
+
return !!(e && /^(?:sqlite3?:|file:)/i.test(e));
|
|
591
|
+
}
|
|
592
|
+
function we(t, n) {
|
|
593
|
+
let i = t.database || t.url || process.env.SQLITE_PATH || (Ce(process.env.DATABASE_URL) ? process.env.DATABASE_URL : "");
|
|
594
|
+
if (!i) throw Error(`${n} Missing SQLite database path. Set $SQLITE_PATH (or --db-url sqlite:///path/to/db.sqlite).`);
|
|
595
|
+
let a = i;
|
|
596
|
+
/^sqlite3?:\/\//i.test(i) ? a = decodeURIComponent(i.replace(/^sqlite3?:\/\//i, "")) : /^sqlite3?:/i.test(i) ? a = decodeURIComponent(i.replace(/^sqlite3?:/i, "")) : /^file:/i.test(i) && (a = decodeURIComponent(i.replace(/^file:(\/\/)?/i, "")));
|
|
597
|
+
let o = r.resolve(a);
|
|
598
|
+
if (!e.existsSync(o) || !e.statSync(o).isFile()) throw Error(`${n} SQLite database file not found: "${o}"`);
|
|
599
|
+
return o;
|
|
600
|
+
}
|
|
601
|
+
// src/db/index.ts
|
|
602
|
+
var K = [
|
|
603
|
+
Se,
|
|
604
|
+
ve,
|
|
605
|
+
me,
|
|
606
|
+
{
|
|
607
|
+
engine: "sqlite",
|
|
608
|
+
aliases: ["sqlite3"],
|
|
609
|
+
async dump(e) {
|
|
610
|
+
let t = we(e, e.tag ?? "[db]"), i = r.basename(t, r.extname(t)), a = e.filename || `sqlite-${H(i)}-${s().format("YYYY-MM-DD.HHmmss")}.sqlite`, o = await h.mkdtemp(r.join(n.tmpdir(), "db-sqlite-"));
|
|
611
|
+
await h.chmod(o, 448);
|
|
612
|
+
let c = r.join(o, a), l = new y(t, { readOnly: !0 });
|
|
613
|
+
try {
|
|
614
|
+
await ee(l, c), await h.chmod(c, 384);
|
|
615
|
+
} catch (e) {
|
|
616
|
+
throw await h.rm(o, {
|
|
617
|
+
recursive: !0,
|
|
618
|
+
force: !0
|
|
619
|
+
}), e;
|
|
620
|
+
} finally {
|
|
621
|
+
l.close();
|
|
622
|
+
}
|
|
623
|
+
return {
|
|
624
|
+
kind: "file",
|
|
625
|
+
engine: "sqlite",
|
|
626
|
+
filename: a,
|
|
627
|
+
mimeType: "application/vnd.sqlite3",
|
|
628
|
+
filePath: c,
|
|
629
|
+
tempDir: o,
|
|
630
|
+
cleanup: async () => {
|
|
631
|
+
await h.rm(o, {
|
|
632
|
+
recursive: !0,
|
|
633
|
+
force: !0
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
], q = /* @__PURE__ */ new Map();
|
|
640
|
+
for (let e of K) {
|
|
641
|
+
q.set(e.engine, e);
|
|
642
|
+
for (let t of e.aliases) q.set(t, e);
|
|
643
|
+
}
|
|
644
|
+
var J = K.map((e) => e.engine);
|
|
645
|
+
function Y(e, t = "[db]") {
|
|
646
|
+
let n = e.trim().toLowerCase(), r = q.get(n);
|
|
647
|
+
if (!r) throw Error(`${t} Unsupported database engine "${e}". Supported engines: ${J.join(", ")}.`);
|
|
648
|
+
return r.engine;
|
|
307
649
|
}
|
|
308
|
-
|
|
309
|
-
let
|
|
310
|
-
|
|
311
|
-
|
|
650
|
+
function X(e, t = "[db]") {
|
|
651
|
+
let n = e.trim();
|
|
652
|
+
if (/^postgres(?:ql)?:\/\//i.test(n)) return "postgres";
|
|
653
|
+
if (/^(?:mysql|mariadb):\/\//i.test(n)) return "mysql";
|
|
654
|
+
if (/^mongodb(?:\+srv)?:\/\//i.test(n)) return "mongodb";
|
|
655
|
+
if (/^(?:sqlite3?:|file:)/i.test(n)) return "sqlite";
|
|
656
|
+
throw Error(`${t} Could not detect database engine from URL. Supported schemes: postgresql://, mysql://, mariadb://, mongodb://, sqlite://.`);
|
|
657
|
+
}
|
|
658
|
+
async function Te(t) {
|
|
659
|
+
let a = await h.mkdtemp(r.join(n.tmpdir(), `db-${t.engine}-`));
|
|
660
|
+
await h.chmod(a, 448);
|
|
661
|
+
let o = r.join(a, r.basename(t.filename));
|
|
662
|
+
try {
|
|
663
|
+
let n = e.createWriteStream(o, { mode: 384 });
|
|
664
|
+
await i(t.stream, n), await t.wait();
|
|
312
665
|
} catch (e) {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
666
|
+
throw await t.cleanup(), await h.rm(a, {
|
|
667
|
+
recursive: !0,
|
|
668
|
+
force: !0
|
|
669
|
+
}), e;
|
|
316
670
|
}
|
|
671
|
+
return {
|
|
672
|
+
kind: "file",
|
|
673
|
+
engine: t.engine,
|
|
674
|
+
filename: t.filename,
|
|
675
|
+
mimeType: t.mimeType,
|
|
676
|
+
filePath: o,
|
|
677
|
+
tempDir: a,
|
|
678
|
+
cleanup: async () => {
|
|
679
|
+
await h.rm(a, {
|
|
680
|
+
recursive: !0,
|
|
681
|
+
force: !0
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
async function Ee(e) {
|
|
687
|
+
let t = e.tag ?? "[db]", n;
|
|
688
|
+
if (e.engine) n = Y(e.engine, t);
|
|
689
|
+
else if (e.url) n = X(e.url, t);
|
|
690
|
+
else if (process.env.DATABASE_URL) n = X(process.env.DATABASE_URL, t);
|
|
691
|
+
else throw Error(`${t} Database engine not specified. Pass --db <${J.join("|")}> or --db-url <uri>.`);
|
|
692
|
+
let r = await q.get(n).dump({
|
|
693
|
+
...e,
|
|
694
|
+
engine: n,
|
|
695
|
+
tag: t
|
|
696
|
+
});
|
|
697
|
+
return e.preferFile && r.kind === "stream" ? Te(r) : r;
|
|
317
698
|
}
|
|
318
|
-
// src/types.ts
|
|
319
|
-
var W = /* @__PURE__ */ "appProperties.capabilities.contentHints.createdTime.description.explicitlyTrashed.fileExtension.folderColorRgb.fullFileExtension.hasAugmentedPermissions.hasThumbnail.headRevisionId.iconLink.id.imageMediaMetadata.isAppAuthorized.lastModifyingUser.md5Checksum.mimeType.modifiedByMe.modifiedByMeTime.modifiedTime.name.originalFilename.ownedByMe.owners.parents.properties.quotaBytesUsed.shared.sharedWithMeTime.sharingUser.size.spaces.starred.teamDriveId.thumbnailLink.thumbnailVersion.trashed.trashedTime.trashingUser.version.videoMediaMetadata.viewedByMe.viewedByMeTime.viewersCanCopyContent.webContentLink.webViewLink.writersCanShare".split(".");
|
|
320
699
|
// src/index.ts
|
|
321
|
-
s.extend(
|
|
322
|
-
var
|
|
700
|
+
s.extend(oe.default);
|
|
701
|
+
var De = new Set(R), Oe = /^(\d+)([a-zA-Z]+)$/, ke = {
|
|
323
702
|
s: "second",
|
|
324
703
|
second: "second",
|
|
325
704
|
seconds: "second",
|
|
@@ -342,31 +721,30 @@ var G = new Set(W), K = /^(\d+)(s|m|h|d|w|M|y|seconds?|minutes?|hours?|days?|wee
|
|
|
342
721
|
year: "year",
|
|
343
722
|
years: "year"
|
|
344
723
|
};
|
|
345
|
-
function
|
|
724
|
+
function Z(e) {
|
|
346
725
|
return e.replaceAll("\\", "\\\\").replaceAll("'", "\\'");
|
|
347
726
|
}
|
|
348
|
-
function
|
|
349
|
-
let t =
|
|
350
|
-
if (!t) throw Error(`${
|
|
351
|
-
let
|
|
352
|
-
if (!Number.isFinite(
|
|
727
|
+
function Q(e) {
|
|
728
|
+
let t = Oe.exec(e), n = t ? ke[t[2]] : void 0;
|
|
729
|
+
if (!t || !n) throw Error(`${C.TAG} Invalid timeSpace "${e}". Expected "<number><unit>" where unit is one of: s, m, h, d, w, M, y, second(s), minute(s), hour(s), day(s), week(s), month(s), year(s).`);
|
|
730
|
+
let r = Number(t[1]);
|
|
731
|
+
if (!Number.isFinite(r) || r <= 0) throw Error(`${C.TAG} Invalid timeSpace "${e}": amount must be a positive integer.`);
|
|
353
732
|
return {
|
|
354
|
-
amount:
|
|
355
|
-
unit:
|
|
733
|
+
amount: r,
|
|
734
|
+
unit: n
|
|
356
735
|
};
|
|
357
736
|
}
|
|
358
|
-
async function
|
|
737
|
+
async function $(e, t, n) {
|
|
359
738
|
let r = Math.max(1, Math.floor(t)), i = Array.from({ length: e.length }), a = 0;
|
|
360
739
|
async function o() {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
}
|
|
740
|
+
if (a >= e.length) return;
|
|
741
|
+
let t = a;
|
|
742
|
+
a += 1, i[t] = await n(e[t]), await o();
|
|
365
743
|
}
|
|
366
744
|
let s = Array.from({ length: Math.min(r, e.length) }, () => o());
|
|
367
745
|
return await Promise.all(s), i;
|
|
368
746
|
}
|
|
369
|
-
var
|
|
747
|
+
var Ae = class {
|
|
370
748
|
DEFAULT_FIELDS = [
|
|
371
749
|
"createdTime",
|
|
372
750
|
"id",
|
|
@@ -389,7 +767,7 @@ var Z = class {
|
|
|
389
767
|
_drive;
|
|
390
768
|
_initPromise;
|
|
391
769
|
constructor(e = {}) {
|
|
392
|
-
this.logger = e.logger ?? console, this.sharedDriveId = e.sharedDriveId, this.defaultDryRun = e.dryRun ?? !1, this.defaultConcurrency = e.concurrency ?? 4, this.defaultCompressionLevel = e.compressionLevel ?? 6, this.defaultOnProgress = e.onProgress, this.retryOptions = e.retry ?? {}, e.drive && (this._drive = e.drive), e.auth && (this.injectedAuth = e.auth), !e.drive && !e.auth && (this.gdriveAuth = new
|
|
770
|
+
this.logger = e.logger ?? console, this.sharedDriveId = e.sharedDriveId, this.defaultDryRun = e.dryRun ?? !1, this.defaultConcurrency = e.concurrency ?? 4, this.defaultCompressionLevel = e.compressionLevel ?? 6, this.defaultOnProgress = e.onProgress, this.retryOptions = e.retry ?? {}, e.drive && (this._drive = e.drive), e.auth && (this.injectedAuth = e.auth), !e.drive && !e.auth && (this.gdriveAuth = new T({
|
|
393
771
|
credentialsFile: e.credentialsFile,
|
|
394
772
|
tokenFile: e.tokenFile,
|
|
395
773
|
serviceAccountFile: e.serviceAccountFile,
|
|
@@ -436,9 +814,9 @@ var Z = class {
|
|
|
436
814
|
await this.ensureInitiated();
|
|
437
815
|
let r;
|
|
438
816
|
if (!e) r = this.DEFAULT_FIELDS;
|
|
439
|
-
else if (e === !0) r =
|
|
817
|
+
else if (e === !0) r = R;
|
|
440
818
|
else {
|
|
441
|
-
for (let t of e) if (!
|
|
819
|
+
for (let t of e) if (!De.has(t)) throw Error(`${C.TAG} Invalid Drive file field: "${t}"`);
|
|
442
820
|
r = e;
|
|
443
821
|
}
|
|
444
822
|
let i = {
|
|
@@ -450,46 +828,49 @@ var Z = class {
|
|
|
450
828
|
return n && (i.q = n), this.getListFiles(i);
|
|
451
829
|
}
|
|
452
830
|
async getFile(e) {
|
|
453
|
-
if (await this.ensureInitiated(), !e) throw this.genericError(/* @__PURE__ */ Error(`${
|
|
831
|
+
if (await this.ensureInitiated(), !e) throw this.genericError(/* @__PURE__ */ Error(`${C.TAG} File id not found`));
|
|
454
832
|
let t = {
|
|
455
833
|
fileId: e,
|
|
456
834
|
...this.sharedDriveItemParams
|
|
457
835
|
};
|
|
458
836
|
try {
|
|
459
|
-
let { data: e } = await
|
|
837
|
+
let { data: e } = await L(() => this.drive.files.get(t), this.retryOptions);
|
|
460
838
|
return e;
|
|
461
839
|
} catch (e) {
|
|
462
840
|
throw this.genericError(e);
|
|
463
841
|
}
|
|
464
842
|
}
|
|
465
|
-
async
|
|
466
|
-
if (
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
fileId: t,
|
|
843
|
+
async resolveDownloadFileId(e, t) {
|
|
844
|
+
if (t) {
|
|
845
|
+
let n = await this.findFolderId(t, !1), r = await this.findFile(e, n);
|
|
846
|
+
if (!r?.id) throw Error(`${C.TAG} File "${e}" not found in folder "${t}"`);
|
|
847
|
+
return r.id;
|
|
848
|
+
}
|
|
849
|
+
try {
|
|
850
|
+
return (await L(() => this.drive.files.get({
|
|
851
|
+
fileId: e,
|
|
475
852
|
fields: "id, name, mimeType",
|
|
476
853
|
...this.sharedDriveItemParams
|
|
477
|
-
}), this.retryOptions);
|
|
478
|
-
e.data.id && (o = e.data.id);
|
|
854
|
+
}), this.retryOptions)).data.id ?? e;
|
|
479
855
|
} catch {
|
|
480
|
-
let
|
|
481
|
-
if (!
|
|
482
|
-
|
|
856
|
+
let t = await this.findFile(e);
|
|
857
|
+
if (!t?.id) throw Error(`${C.TAG} File "${e}" not found`);
|
|
858
|
+
return t.id;
|
|
483
859
|
}
|
|
860
|
+
}
|
|
861
|
+
async downloadFile(t, n, a = {}) {
|
|
862
|
+
if (await this.ensureInitiated(), !t) throw Error(`${C.TAG} File id or name is required for download`);
|
|
863
|
+
let o = await this.resolveDownloadFileId(t, a.folderName);
|
|
484
864
|
e.mkdirSync(r.dirname(r.resolve(n)), { recursive: !0 });
|
|
485
|
-
let s, c =
|
|
865
|
+
let s, c = n;
|
|
866
|
+
a.decryptKey && (s = this.createPrivateTempDir(), c = r.join(s, "download.enc"));
|
|
486
867
|
try {
|
|
487
|
-
let t = await
|
|
868
|
+
let t = await L(() => this.drive.files.get({
|
|
488
869
|
fileId: o,
|
|
489
870
|
alt: "media",
|
|
490
871
|
...this.sharedDriveItemParams
|
|
491
872
|
}, { responseType: "stream" }), this.retryOptions), r = e.createWriteStream(c, { mode: a.decryptKey ? 384 : 420 });
|
|
492
|
-
return await i(t.data, r), a.decryptKey && await
|
|
873
|
+
return await i(t.data, r), a.decryptKey && await F(c, n, a.decryptKey), n;
|
|
493
874
|
} catch (t) {
|
|
494
875
|
throw a.decryptKey || e.rmSync(n, { force: !0 }), this.genericError(t);
|
|
495
876
|
} finally {
|
|
@@ -500,15 +881,15 @@ var Z = class {
|
|
|
500
881
|
}
|
|
501
882
|
}
|
|
502
883
|
async emptyTrash() {
|
|
503
|
-
return await this.ensureInitiated(),
|
|
884
|
+
return await this.ensureInitiated(), L(() => this.drive.files.emptyTrash(), this.retryOptions);
|
|
504
885
|
}
|
|
505
886
|
async deleteFile(e) {
|
|
506
887
|
if (await this.ensureInitiated(), e.id) try {
|
|
507
|
-
let { data: t } = await
|
|
888
|
+
let { data: t } = await L(() => this.drive.files.delete({
|
|
508
889
|
fileId: e.id,
|
|
509
890
|
...this.sharedDriveItemParams
|
|
510
891
|
}), this.retryOptions);
|
|
511
|
-
return e.name && this.logger.warn(`${
|
|
892
|
+
return e.name && this.logger.warn(`${C.TAG} Deleted ${e.isFolder ? "folder" : "file"}: ${e.name}`), t;
|
|
512
893
|
} catch (e) {
|
|
513
894
|
throw this.genericError(e);
|
|
514
895
|
}
|
|
@@ -519,15 +900,15 @@ var Z = class {
|
|
|
519
900
|
name: f,
|
|
520
901
|
mimeType: s ? "application/octet-stream" : c.contentType(d) || void 0
|
|
521
902
|
}, m = await this.getUploadFolderId(n || !1, a, p, l);
|
|
522
|
-
if (l) return this.logger.info(`${
|
|
903
|
+
if (l) return this.logger.info(`${C.TAG} [dry-run] Would upload "${f}"${n && typeof n == "string" ? ` to "${n}"` : ""}`), this.parseFileMeta(p);
|
|
523
904
|
await this.replaceExistingFile(o, f, m);
|
|
524
905
|
let h, g = t;
|
|
525
|
-
s && (h = this.createPrivateTempDir(), g = r.join(h, f), await
|
|
906
|
+
s && (h = this.createPrivateTempDir(), g = r.join(h, f), await P(t, g, s));
|
|
526
907
|
try {
|
|
527
|
-
let t = e.statSync(g).size, { data: n } = await
|
|
908
|
+
let t = e.statSync(g).size, { data: n } = await L(async () => {
|
|
528
909
|
let n = e.createReadStream(g);
|
|
529
910
|
n.on("error", (e) => {
|
|
530
|
-
this.logger.error(`${
|
|
911
|
+
this.logger.error(`${C.TAG} Read stream error for "${g}":`, e);
|
|
531
912
|
});
|
|
532
913
|
let r = {
|
|
533
914
|
requestBody: p,
|
|
@@ -541,7 +922,7 @@ var Z = class {
|
|
|
541
922
|
bytesRead: e.bytesRead,
|
|
542
923
|
totalBytes: t,
|
|
543
924
|
percentage: n
|
|
544
|
-
}), n >= 100 && this.logger.warn(`${
|
|
925
|
+
}), n >= 100 && this.logger.warn(`${C.TAG} Upload ${f}: ${Math.round(n)}% complete`);
|
|
545
926
|
};
|
|
546
927
|
try {
|
|
547
928
|
return await this.drive.files.create(r, { onUploadProgress: i });
|
|
@@ -588,7 +969,7 @@ var Z = class {
|
|
|
588
969
|
}
|
|
589
970
|
n && typeof n == "string" && y.length > 0 && !(l ?? this.defaultDryRun) && await this.findFolderId(n, c ?? !0);
|
|
590
971
|
let e = {};
|
|
591
|
-
return await
|
|
972
|
+
return await $(y, m, async (t) => {
|
|
592
973
|
let i = await this.uploadFile(t, n, {
|
|
593
974
|
replace: o,
|
|
594
975
|
create: c,
|
|
@@ -606,7 +987,7 @@ var Z = class {
|
|
|
606
987
|
}
|
|
607
988
|
}
|
|
608
989
|
async cleanOlder(e, t, n = {}) {
|
|
609
|
-
let { amount: r, unit: i } =
|
|
990
|
+
let { amount: r, unit: i } = Q(e);
|
|
610
991
|
await this.ensureInitiated();
|
|
611
992
|
let a = "";
|
|
612
993
|
return t && (a = await this.findFolderId(t, !1)), this.deleteOlder(r, i, a, n);
|
|
@@ -620,7 +1001,7 @@ var Z = class {
|
|
|
620
1001
|
}
|
|
621
1002
|
if (i.some((e) => r.matchesGlob(t, e) || r.matchesGlob(r.basename(t), e))) return;
|
|
622
1003
|
let n = e.lstatSync(t);
|
|
623
|
-
if (n.isSymbolicLink() && !a) this.logger.warn(`${
|
|
1004
|
+
if (n.isSymbolicLink() && !a) this.logger.warn(`${C.TAG} Skipping symlink: ${t}`);
|
|
624
1005
|
else if ((n.isSymbolicLink() ? e.statSync(t) : n).isDirectory()) {
|
|
625
1006
|
let n = e.readdirSync(t).map((e) => r.join(t, e));
|
|
626
1007
|
for (let e of n) s(e);
|
|
@@ -630,16 +1011,15 @@ var Z = class {
|
|
|
630
1011
|
return o;
|
|
631
1012
|
}
|
|
632
1013
|
async getListFiles(e) {
|
|
633
|
-
let t = [], n
|
|
1014
|
+
let t = [], n = async (r) => {
|
|
1015
|
+
let i = r ? {
|
|
1016
|
+
...e,
|
|
1017
|
+
pageToken: r
|
|
1018
|
+
} : { ...e }, { data: a } = await L(() => this.drive.files.list(i), this.retryOptions);
|
|
1019
|
+
a.files?.length && t.push(...a.files), a.nextPageToken && await n(a.nextPageToken);
|
|
1020
|
+
};
|
|
634
1021
|
try {
|
|
635
|
-
|
|
636
|
-
let r = n ? {
|
|
637
|
-
...e,
|
|
638
|
-
pageToken: n
|
|
639
|
-
} : { ...e }, { data: i } = await U(() => this.drive.files.list(r), this.retryOptions);
|
|
640
|
-
i.files?.length && t.push(...i.files), n = i.nextPageToken ?? void 0;
|
|
641
|
-
} while (n);
|
|
642
|
-
return this.parseFilesMeta(t);
|
|
1022
|
+
return await n(), this.parseFilesMeta(t);
|
|
643
1023
|
} catch (e) {
|
|
644
1024
|
throw this.genericError(e);
|
|
645
1025
|
}
|
|
@@ -647,8 +1027,8 @@ var Z = class {
|
|
|
647
1027
|
async replaceExistingFile(e, t, n) {
|
|
648
1028
|
let r = await this.findFile(t, n);
|
|
649
1029
|
if (r) {
|
|
650
|
-
if (!e) throw Error(`${
|
|
651
|
-
this.logger.warn(`${
|
|
1030
|
+
if (!e) throw Error(`${C.TAG} File "${t}" already exists and will not be replaced`);
|
|
1031
|
+
this.logger.warn(`${C.TAG} File "${t}" already exists and will be deleted before upload`), await this.deleteFile(r);
|
|
652
1032
|
}
|
|
653
1033
|
}
|
|
654
1034
|
async getUploadFolderId(e, t, n, r = !1) {
|
|
@@ -660,16 +1040,16 @@ var Z = class {
|
|
|
660
1040
|
}
|
|
661
1041
|
async deleteOlder(e, t, n, r = {}) {
|
|
662
1042
|
let i = r.dryRun ?? this.defaultDryRun, a = Math.max(0, r.keepLast ?? 0), o = r.concurrency ?? this.defaultConcurrency, c = s().subtract(e, t), l = [`mimeType != '${this.MIME_FOLDER}'`, "trashed = false"];
|
|
663
|
-
n && l.push(`'${
|
|
1043
|
+
n && l.push(`'${Z(n)}' in parents`), a === 0 && l.push(`createdTime <= '${c.toISOString()}'`);
|
|
664
1044
|
let u = (await this.listFiles(void 0, 1e3, l.join(" and "))).filter((e) => !e.isDeleted && !e.isFolder).filter((e) => !n || !!e.parents?.includes(n)).sort((e, t) => {
|
|
665
1045
|
let n = e.createdTime ? s(e.createdTime).valueOf() : 0;
|
|
666
1046
|
return (t.createdTime ? s(t.createdTime).valueOf() : 0) - n;
|
|
667
1047
|
}), d = (a > 0 ? u.slice(a) : u).filter((e) => e.createdTime && s(e.createdTime).isSameOrBefore(c));
|
|
668
1048
|
if (i) {
|
|
669
|
-
for (let e of d) this.logger.info(`${
|
|
1049
|
+
for (let e of d) this.logger.info(`${C.TAG} [dry-run] Would delete file: ${e.name ?? e.id ?? "unknown"}`);
|
|
670
1050
|
return d;
|
|
671
1051
|
}
|
|
672
|
-
return await
|
|
1052
|
+
return await $(d, o, async (e) => {
|
|
673
1053
|
await this.deleteFile(e);
|
|
674
1054
|
}), d;
|
|
675
1055
|
}
|
|
@@ -684,7 +1064,7 @@ var Z = class {
|
|
|
684
1064
|
return e.map((e) => this.parseFileMeta(e));
|
|
685
1065
|
}
|
|
686
1066
|
genericError(e) {
|
|
687
|
-
return this.logger.error(`${
|
|
1067
|
+
return this.logger.error(`${C.TAG} The API returned an error: ${String(e)}`), e;
|
|
688
1068
|
}
|
|
689
1069
|
inFlightFolders = /* @__PURE__ */ new Map();
|
|
690
1070
|
findFolderId(e, t = !1) {
|
|
@@ -697,30 +1077,31 @@ var Z = class {
|
|
|
697
1077
|
}
|
|
698
1078
|
async resolveFolderId(e, t = !1) {
|
|
699
1079
|
let n = e.split("/").map((e) => e.trim()).filter(Boolean);
|
|
700
|
-
if (n.length === 0) throw Error(`${
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
1080
|
+
if (n.length === 0) throw Error(`${C.TAG} Invalid folder path "${e}"`);
|
|
1081
|
+
return this.resolveFolderSegments(n, 0, this.rootParentId, t, e);
|
|
1082
|
+
}
|
|
1083
|
+
async resolveFolderSegments(e, t, n, r, i) {
|
|
1084
|
+
if (t >= e.length) return n;
|
|
1085
|
+
let a = e[t], o = [
|
|
1086
|
+
`name = '${Z(a)}'`,
|
|
1087
|
+
`mimeType = '${this.MIME_FOLDER}'`,
|
|
1088
|
+
"trashed = false",
|
|
1089
|
+
`'${Z(n)}' in parents`
|
|
1090
|
+
].join(" and "), s = (await this.listFiles(void 0, 100, o)).find((e) => e.isFolder && !e.isDeleted && e.name === a && e.id), c;
|
|
1091
|
+
if (s?.id) c = s.id;
|
|
1092
|
+
else if (r) {
|
|
1093
|
+
let e = await this.createFolder(a, n);
|
|
1094
|
+
if (!e.id) throw Error(`${C.TAG} Failed to create folder "${a}"`);
|
|
1095
|
+
c = e.id;
|
|
1096
|
+
} else throw Error(`${C.TAG} Folder "${i}" does not exist and will not be created`);
|
|
1097
|
+
return this.resolveFolderSegments(e, t + 1, c, r, i);
|
|
717
1098
|
}
|
|
718
1099
|
async findFile(e, t) {
|
|
719
1100
|
let n = t || this.rootParentId, r = [
|
|
720
|
-
`name = '${
|
|
1101
|
+
`name = '${Z(e)}'`,
|
|
721
1102
|
`mimeType != '${this.MIME_FOLDER}'`,
|
|
722
1103
|
"trashed = false",
|
|
723
|
-
`'${
|
|
1104
|
+
`'${Z(n)}' in parents`
|
|
724
1105
|
].join(" and ");
|
|
725
1106
|
return (await this.listFiles(void 0, 100, r)).find((n) => !n.isFolder && !n.isDeleted && n.name === e && (!t || !!n.parents?.includes(t))) ?? null;
|
|
726
1107
|
}
|
|
@@ -731,12 +1112,12 @@ var Z = class {
|
|
|
731
1112
|
};
|
|
732
1113
|
t && t !== "root" && (n.parents = [t]);
|
|
733
1114
|
try {
|
|
734
|
-
let { data: t } = await
|
|
1115
|
+
let { data: t } = await L(() => this.drive.files.create({
|
|
735
1116
|
requestBody: n,
|
|
736
1117
|
fields: "id",
|
|
737
1118
|
...this.sharedDriveItemParams
|
|
738
1119
|
}), this.retryOptions);
|
|
739
|
-
return t?.id && this.logger.warn(`${
|
|
1120
|
+
return t?.id && this.logger.warn(`${C.TAG} Created folder "${e}" with id: "${t.id}"`), t;
|
|
740
1121
|
} catch (e) {
|
|
741
1122
|
throw this.genericError(e);
|
|
742
1123
|
}
|
|
@@ -747,7 +1128,7 @@ var Z = class {
|
|
|
747
1128
|
}
|
|
748
1129
|
sanitizeFilename(e) {
|
|
749
1130
|
let t = r.basename(e);
|
|
750
|
-
if (!t || t === "." || t === "..") throw Error(`${
|
|
1131
|
+
if (!t || t === "." || t === "..") throw Error(`${C.TAG} Invalid output filename: "${e}"`);
|
|
751
1132
|
return t;
|
|
752
1133
|
}
|
|
753
1134
|
compressFiles(t, n, i = {}) {
|
|
@@ -771,14 +1152,14 @@ var Z = class {
|
|
|
771
1152
|
tempDir: s
|
|
772
1153
|
}));
|
|
773
1154
|
}), m.on("warning", (e) => {
|
|
774
|
-
this.logger.warn(`${
|
|
1155
|
+
this.logger.warn(`${C.TAG} Warning compressing file`, e), e.code !== "ENOENT" && p(e);
|
|
775
1156
|
}), m.on("error", (e) => {
|
|
776
|
-
this.logger.error(`${
|
|
1157
|
+
this.logger.error(`${C.TAG} Error compressing file`, e), p(e);
|
|
777
1158
|
}), m.pipe(a);
|
|
778
1159
|
try {
|
|
779
1160
|
for (let n of t) {
|
|
780
1161
|
let t = e.lstatSync(n);
|
|
781
|
-
if (t.isSymbolicLink() && !u) this.logger.warn(`${
|
|
1162
|
+
if (t.isSymbolicLink() && !u) this.logger.warn(`${C.TAG} Skipping symlink: ${n}`);
|
|
782
1163
|
else if ((t.isSymbolicLink() ? e.statSync(n) : t).isDirectory()) m.directory(n, r.basename(n));
|
|
783
1164
|
else {
|
|
784
1165
|
let t = r.basename(n), i = e.createReadStream(n);
|
|
@@ -799,4 +1180,4 @@ var Z = class {
|
|
|
799
1180
|
});
|
|
800
1181
|
}
|
|
801
1182
|
};
|
|
802
|
-
export {
|
|
1183
|
+
export { T as Auth, C as Config, Ae as GDrive, J as SUPPORTED_DB_ENGINES, R as allFileTypes, N as decryptBuffer, F as decryptFile, X as detectDbEngineFromUrl, Ee as dumpDatabase, ce as encryptBuffer, P as encryptFile, Z as escapeDriveQuery, I as isRetryableError, Y as normalizeDbEngine, Q as parseTimeSpace, L as withRetry };
|