@d0whc3r/node-gdrive 3.5.1 → 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 +480 -109
- package/dist/index.d.ts +2 -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,30 +298,30 @@ 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) {
|
|
302
307
|
if (typeof e.status == "number") return e.status;
|
|
303
308
|
if (typeof e.code == "number") return e.code;
|
|
304
309
|
let t = e.response;
|
|
305
310
|
if (typeof t?.status == "number") return t.status;
|
|
306
311
|
}
|
|
307
|
-
function
|
|
312
|
+
function I(e) {
|
|
308
313
|
if (!e || typeof e != "object") return !1;
|
|
309
|
-
let t = e, n =
|
|
310
|
-
if (n !== void 0 &&
|
|
314
|
+
let t = e, n = fe(t);
|
|
315
|
+
if (n !== void 0 && le.has(n)) return !0;
|
|
311
316
|
let r = typeof t.code == "string" ? t.code : void 0;
|
|
312
|
-
return !!(r &&
|
|
317
|
+
return !!(r && ue.has(r));
|
|
313
318
|
}
|
|
314
|
-
async function
|
|
315
|
-
let { maxRetries: n = 4, baseDelayMs: r = 500, maxDelayMs: i = 16e3, jitter: a = !0, sleep: o =
|
|
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) => {
|
|
316
321
|
try {
|
|
317
322
|
return await e();
|
|
318
323
|
} catch (e) {
|
|
319
|
-
if (t >= n || !
|
|
324
|
+
if (t >= n || !I(e)) throw e;
|
|
320
325
|
let c = Math.min(i, r * 2 ** t), l = a ? Math.round(c * (.5 + Math.random() * .5)) : c;
|
|
321
326
|
return await o(l), s(t + 1);
|
|
322
327
|
}
|
|
@@ -324,10 +329,376 @@ async function W(e, t = {}) {
|
|
|
324
329
|
return s(0);
|
|
325
330
|
}
|
|
326
331
|
// src/types.ts
|
|
327
|
-
var
|
|
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;
|
|
649
|
+
}
|
|
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();
|
|
665
|
+
} catch (e) {
|
|
666
|
+
throw await t.cleanup(), await h.rm(a, {
|
|
667
|
+
recursive: !0,
|
|
668
|
+
force: !0
|
|
669
|
+
}), e;
|
|
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;
|
|
698
|
+
}
|
|
328
699
|
// src/index.ts
|
|
329
|
-
s.extend(
|
|
330
|
-
var
|
|
700
|
+
s.extend(oe.default);
|
|
701
|
+
var De = new Set(R), Oe = /^(\d+)([a-zA-Z]+)$/, ke = {
|
|
331
702
|
s: "second",
|
|
332
703
|
second: "second",
|
|
333
704
|
seconds: "second",
|
|
@@ -350,20 +721,20 @@ var K = new Set(G), q = /^(\d+)([a-zA-Z]+)$/, J = {
|
|
|
350
721
|
year: "year",
|
|
351
722
|
years: "year"
|
|
352
723
|
};
|
|
353
|
-
function
|
|
724
|
+
function Z(e) {
|
|
354
725
|
return e.replaceAll("\\", "\\\\").replaceAll("'", "\\'");
|
|
355
726
|
}
|
|
356
|
-
function
|
|
357
|
-
let t =
|
|
358
|
-
if (!t || !n) throw Error(`${
|
|
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).`);
|
|
359
730
|
let r = Number(t[1]);
|
|
360
|
-
if (!Number.isFinite(r) || r <= 0) throw Error(`${
|
|
731
|
+
if (!Number.isFinite(r) || r <= 0) throw Error(`${C.TAG} Invalid timeSpace "${e}": amount must be a positive integer.`);
|
|
361
732
|
return {
|
|
362
733
|
amount: r,
|
|
363
734
|
unit: n
|
|
364
735
|
};
|
|
365
736
|
}
|
|
366
|
-
async function
|
|
737
|
+
async function $(e, t, n) {
|
|
367
738
|
let r = Math.max(1, Math.floor(t)), i = Array.from({ length: e.length }), a = 0;
|
|
368
739
|
async function o() {
|
|
369
740
|
if (a >= e.length) return;
|
|
@@ -373,7 +744,7 @@ async function Z(e, t, n) {
|
|
|
373
744
|
let s = Array.from({ length: Math.min(r, e.length) }, () => o());
|
|
374
745
|
return await Promise.all(s), i;
|
|
375
746
|
}
|
|
376
|
-
var
|
|
747
|
+
var Ae = class {
|
|
377
748
|
DEFAULT_FIELDS = [
|
|
378
749
|
"createdTime",
|
|
379
750
|
"id",
|
|
@@ -396,7 +767,7 @@ var Q = class {
|
|
|
396
767
|
_drive;
|
|
397
768
|
_initPromise;
|
|
398
769
|
constructor(e = {}) {
|
|
399
|
-
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({
|
|
400
771
|
credentialsFile: e.credentialsFile,
|
|
401
772
|
tokenFile: e.tokenFile,
|
|
402
773
|
serviceAccountFile: e.serviceAccountFile,
|
|
@@ -443,9 +814,9 @@ var Q = class {
|
|
|
443
814
|
await this.ensureInitiated();
|
|
444
815
|
let r;
|
|
445
816
|
if (!e) r = this.DEFAULT_FIELDS;
|
|
446
|
-
else if (e === !0) r =
|
|
817
|
+
else if (e === !0) r = R;
|
|
447
818
|
else {
|
|
448
|
-
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}"`);
|
|
449
820
|
r = e;
|
|
450
821
|
}
|
|
451
822
|
let i = {
|
|
@@ -457,13 +828,13 @@ var Q = class {
|
|
|
457
828
|
return n && (i.q = n), this.getListFiles(i);
|
|
458
829
|
}
|
|
459
830
|
async getFile(e) {
|
|
460
|
-
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`));
|
|
461
832
|
let t = {
|
|
462
833
|
fileId: e,
|
|
463
834
|
...this.sharedDriveItemParams
|
|
464
835
|
};
|
|
465
836
|
try {
|
|
466
|
-
let { data: e } = await
|
|
837
|
+
let { data: e } = await L(() => this.drive.files.get(t), this.retryOptions);
|
|
467
838
|
return e;
|
|
468
839
|
} catch (e) {
|
|
469
840
|
throw this.genericError(e);
|
|
@@ -472,34 +843,34 @@ var Q = class {
|
|
|
472
843
|
async resolveDownloadFileId(e, t) {
|
|
473
844
|
if (t) {
|
|
474
845
|
let n = await this.findFolderId(t, !1), r = await this.findFile(e, n);
|
|
475
|
-
if (!r?.id) throw Error(`${
|
|
846
|
+
if (!r?.id) throw Error(`${C.TAG} File "${e}" not found in folder "${t}"`);
|
|
476
847
|
return r.id;
|
|
477
848
|
}
|
|
478
849
|
try {
|
|
479
|
-
return (await
|
|
850
|
+
return (await L(() => this.drive.files.get({
|
|
480
851
|
fileId: e,
|
|
481
852
|
fields: "id, name, mimeType",
|
|
482
853
|
...this.sharedDriveItemParams
|
|
483
854
|
}), this.retryOptions)).data.id ?? e;
|
|
484
855
|
} catch {
|
|
485
856
|
let t = await this.findFile(e);
|
|
486
|
-
if (!t?.id) throw Error(`${
|
|
857
|
+
if (!t?.id) throw Error(`${C.TAG} File "${e}" not found`);
|
|
487
858
|
return t.id;
|
|
488
859
|
}
|
|
489
860
|
}
|
|
490
861
|
async downloadFile(t, n, a = {}) {
|
|
491
|
-
if (await this.ensureInitiated(), !t) throw Error(`${
|
|
862
|
+
if (await this.ensureInitiated(), !t) throw Error(`${C.TAG} File id or name is required for download`);
|
|
492
863
|
let o = await this.resolveDownloadFileId(t, a.folderName);
|
|
493
864
|
e.mkdirSync(r.dirname(r.resolve(n)), { recursive: !0 });
|
|
494
865
|
let s, c = n;
|
|
495
866
|
a.decryptKey && (s = this.createPrivateTempDir(), c = r.join(s, "download.enc"));
|
|
496
867
|
try {
|
|
497
|
-
let t = await
|
|
868
|
+
let t = await L(() => this.drive.files.get({
|
|
498
869
|
fileId: o,
|
|
499
870
|
alt: "media",
|
|
500
871
|
...this.sharedDriveItemParams
|
|
501
872
|
}, { responseType: "stream" }), this.retryOptions), r = e.createWriteStream(c, { mode: a.decryptKey ? 384 : 420 });
|
|
502
|
-
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;
|
|
503
874
|
} catch (t) {
|
|
504
875
|
throw a.decryptKey || e.rmSync(n, { force: !0 }), this.genericError(t);
|
|
505
876
|
} finally {
|
|
@@ -510,15 +881,15 @@ var Q = class {
|
|
|
510
881
|
}
|
|
511
882
|
}
|
|
512
883
|
async emptyTrash() {
|
|
513
|
-
return await this.ensureInitiated(),
|
|
884
|
+
return await this.ensureInitiated(), L(() => this.drive.files.emptyTrash(), this.retryOptions);
|
|
514
885
|
}
|
|
515
886
|
async deleteFile(e) {
|
|
516
887
|
if (await this.ensureInitiated(), e.id) try {
|
|
517
|
-
let { data: t } = await
|
|
888
|
+
let { data: t } = await L(() => this.drive.files.delete({
|
|
518
889
|
fileId: e.id,
|
|
519
890
|
...this.sharedDriveItemParams
|
|
520
891
|
}), this.retryOptions);
|
|
521
|
-
return e.name && this.logger.warn(`${
|
|
892
|
+
return e.name && this.logger.warn(`${C.TAG} Deleted ${e.isFolder ? "folder" : "file"}: ${e.name}`), t;
|
|
522
893
|
} catch (e) {
|
|
523
894
|
throw this.genericError(e);
|
|
524
895
|
}
|
|
@@ -529,15 +900,15 @@ var Q = class {
|
|
|
529
900
|
name: f,
|
|
530
901
|
mimeType: s ? "application/octet-stream" : c.contentType(d) || void 0
|
|
531
902
|
}, m = await this.getUploadFolderId(n || !1, a, p, l);
|
|
532
|
-
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);
|
|
533
904
|
await this.replaceExistingFile(o, f, m);
|
|
534
905
|
let h, g = t;
|
|
535
|
-
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));
|
|
536
907
|
try {
|
|
537
|
-
let t = e.statSync(g).size, { data: n } = await
|
|
908
|
+
let t = e.statSync(g).size, { data: n } = await L(async () => {
|
|
538
909
|
let n = e.createReadStream(g);
|
|
539
910
|
n.on("error", (e) => {
|
|
540
|
-
this.logger.error(`${
|
|
911
|
+
this.logger.error(`${C.TAG} Read stream error for "${g}":`, e);
|
|
541
912
|
});
|
|
542
913
|
let r = {
|
|
543
914
|
requestBody: p,
|
|
@@ -551,7 +922,7 @@ var Q = class {
|
|
|
551
922
|
bytesRead: e.bytesRead,
|
|
552
923
|
totalBytes: t,
|
|
553
924
|
percentage: n
|
|
554
|
-
}), n >= 100 && this.logger.warn(`${
|
|
925
|
+
}), n >= 100 && this.logger.warn(`${C.TAG} Upload ${f}: ${Math.round(n)}% complete`);
|
|
555
926
|
};
|
|
556
927
|
try {
|
|
557
928
|
return await this.drive.files.create(r, { onUploadProgress: i });
|
|
@@ -598,7 +969,7 @@ var Q = class {
|
|
|
598
969
|
}
|
|
599
970
|
n && typeof n == "string" && y.length > 0 && !(l ?? this.defaultDryRun) && await this.findFolderId(n, c ?? !0);
|
|
600
971
|
let e = {};
|
|
601
|
-
return await
|
|
972
|
+
return await $(y, m, async (t) => {
|
|
602
973
|
let i = await this.uploadFile(t, n, {
|
|
603
974
|
replace: o,
|
|
604
975
|
create: c,
|
|
@@ -616,7 +987,7 @@ var Q = class {
|
|
|
616
987
|
}
|
|
617
988
|
}
|
|
618
989
|
async cleanOlder(e, t, n = {}) {
|
|
619
|
-
let { amount: r, unit: i } =
|
|
990
|
+
let { amount: r, unit: i } = Q(e);
|
|
620
991
|
await this.ensureInitiated();
|
|
621
992
|
let a = "";
|
|
622
993
|
return t && (a = await this.findFolderId(t, !1)), this.deleteOlder(r, i, a, n);
|
|
@@ -630,7 +1001,7 @@ var Q = class {
|
|
|
630
1001
|
}
|
|
631
1002
|
if (i.some((e) => r.matchesGlob(t, e) || r.matchesGlob(r.basename(t), e))) return;
|
|
632
1003
|
let n = e.lstatSync(t);
|
|
633
|
-
if (n.isSymbolicLink() && !a) this.logger.warn(`${
|
|
1004
|
+
if (n.isSymbolicLink() && !a) this.logger.warn(`${C.TAG} Skipping symlink: ${t}`);
|
|
634
1005
|
else if ((n.isSymbolicLink() ? e.statSync(t) : n).isDirectory()) {
|
|
635
1006
|
let n = e.readdirSync(t).map((e) => r.join(t, e));
|
|
636
1007
|
for (let e of n) s(e);
|
|
@@ -644,7 +1015,7 @@ var Q = class {
|
|
|
644
1015
|
let i = r ? {
|
|
645
1016
|
...e,
|
|
646
1017
|
pageToken: r
|
|
647
|
-
} : { ...e }, { data: a } = await
|
|
1018
|
+
} : { ...e }, { data: a } = await L(() => this.drive.files.list(i), this.retryOptions);
|
|
648
1019
|
a.files?.length && t.push(...a.files), a.nextPageToken && await n(a.nextPageToken);
|
|
649
1020
|
};
|
|
650
1021
|
try {
|
|
@@ -656,8 +1027,8 @@ var Q = class {
|
|
|
656
1027
|
async replaceExistingFile(e, t, n) {
|
|
657
1028
|
let r = await this.findFile(t, n);
|
|
658
1029
|
if (r) {
|
|
659
|
-
if (!e) throw Error(`${
|
|
660
|
-
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);
|
|
661
1032
|
}
|
|
662
1033
|
}
|
|
663
1034
|
async getUploadFolderId(e, t, n, r = !1) {
|
|
@@ -669,16 +1040,16 @@ var Q = class {
|
|
|
669
1040
|
}
|
|
670
1041
|
async deleteOlder(e, t, n, r = {}) {
|
|
671
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"];
|
|
672
|
-
n && l.push(`'${
|
|
1043
|
+
n && l.push(`'${Z(n)}' in parents`), a === 0 && l.push(`createdTime <= '${c.toISOString()}'`);
|
|
673
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) => {
|
|
674
1045
|
let n = e.createdTime ? s(e.createdTime).valueOf() : 0;
|
|
675
1046
|
return (t.createdTime ? s(t.createdTime).valueOf() : 0) - n;
|
|
676
1047
|
}), d = (a > 0 ? u.slice(a) : u).filter((e) => e.createdTime && s(e.createdTime).isSameOrBefore(c));
|
|
677
1048
|
if (i) {
|
|
678
|
-
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"}`);
|
|
679
1050
|
return d;
|
|
680
1051
|
}
|
|
681
|
-
return await
|
|
1052
|
+
return await $(d, o, async (e) => {
|
|
682
1053
|
await this.deleteFile(e);
|
|
683
1054
|
}), d;
|
|
684
1055
|
}
|
|
@@ -693,7 +1064,7 @@ var Q = class {
|
|
|
693
1064
|
return e.map((e) => this.parseFileMeta(e));
|
|
694
1065
|
}
|
|
695
1066
|
genericError(e) {
|
|
696
|
-
return this.logger.error(`${
|
|
1067
|
+
return this.logger.error(`${C.TAG} The API returned an error: ${String(e)}`), e;
|
|
697
1068
|
}
|
|
698
1069
|
inFlightFolders = /* @__PURE__ */ new Map();
|
|
699
1070
|
findFolderId(e, t = !1) {
|
|
@@ -706,31 +1077,31 @@ var Q = class {
|
|
|
706
1077
|
}
|
|
707
1078
|
async resolveFolderId(e, t = !1) {
|
|
708
1079
|
let n = e.split("/").map((e) => e.trim()).filter(Boolean);
|
|
709
|
-
if (n.length === 0) throw Error(`${
|
|
1080
|
+
if (n.length === 0) throw Error(`${C.TAG} Invalid folder path "${e}"`);
|
|
710
1081
|
return this.resolveFolderSegments(n, 0, this.rootParentId, t, e);
|
|
711
1082
|
}
|
|
712
1083
|
async resolveFolderSegments(e, t, n, r, i) {
|
|
713
1084
|
if (t >= e.length) return n;
|
|
714
1085
|
let a = e[t], o = [
|
|
715
|
-
`name = '${
|
|
1086
|
+
`name = '${Z(a)}'`,
|
|
716
1087
|
`mimeType = '${this.MIME_FOLDER}'`,
|
|
717
1088
|
"trashed = false",
|
|
718
|
-
`'${
|
|
1089
|
+
`'${Z(n)}' in parents`
|
|
719
1090
|
].join(" and "), s = (await this.listFiles(void 0, 100, o)).find((e) => e.isFolder && !e.isDeleted && e.name === a && e.id), c;
|
|
720
1091
|
if (s?.id) c = s.id;
|
|
721
1092
|
else if (r) {
|
|
722
1093
|
let e = await this.createFolder(a, n);
|
|
723
|
-
if (!e.id) throw Error(`${
|
|
1094
|
+
if (!e.id) throw Error(`${C.TAG} Failed to create folder "${a}"`);
|
|
724
1095
|
c = e.id;
|
|
725
|
-
} else throw Error(`${
|
|
1096
|
+
} else throw Error(`${C.TAG} Folder "${i}" does not exist and will not be created`);
|
|
726
1097
|
return this.resolveFolderSegments(e, t + 1, c, r, i);
|
|
727
1098
|
}
|
|
728
1099
|
async findFile(e, t) {
|
|
729
1100
|
let n = t || this.rootParentId, r = [
|
|
730
|
-
`name = '${
|
|
1101
|
+
`name = '${Z(e)}'`,
|
|
731
1102
|
`mimeType != '${this.MIME_FOLDER}'`,
|
|
732
1103
|
"trashed = false",
|
|
733
|
-
`'${
|
|
1104
|
+
`'${Z(n)}' in parents`
|
|
734
1105
|
].join(" and ");
|
|
735
1106
|
return (await this.listFiles(void 0, 100, r)).find((n) => !n.isFolder && !n.isDeleted && n.name === e && (!t || !!n.parents?.includes(t))) ?? null;
|
|
736
1107
|
}
|
|
@@ -741,12 +1112,12 @@ var Q = class {
|
|
|
741
1112
|
};
|
|
742
1113
|
t && t !== "root" && (n.parents = [t]);
|
|
743
1114
|
try {
|
|
744
|
-
let { data: t } = await
|
|
1115
|
+
let { data: t } = await L(() => this.drive.files.create({
|
|
745
1116
|
requestBody: n,
|
|
746
1117
|
fields: "id",
|
|
747
1118
|
...this.sharedDriveItemParams
|
|
748
1119
|
}), this.retryOptions);
|
|
749
|
-
return t?.id && this.logger.warn(`${
|
|
1120
|
+
return t?.id && this.logger.warn(`${C.TAG} Created folder "${e}" with id: "${t.id}"`), t;
|
|
750
1121
|
} catch (e) {
|
|
751
1122
|
throw this.genericError(e);
|
|
752
1123
|
}
|
|
@@ -757,7 +1128,7 @@ var Q = class {
|
|
|
757
1128
|
}
|
|
758
1129
|
sanitizeFilename(e) {
|
|
759
1130
|
let t = r.basename(e);
|
|
760
|
-
if (!t || t === "." || t === "..") throw Error(`${
|
|
1131
|
+
if (!t || t === "." || t === "..") throw Error(`${C.TAG} Invalid output filename: "${e}"`);
|
|
761
1132
|
return t;
|
|
762
1133
|
}
|
|
763
1134
|
compressFiles(t, n, i = {}) {
|
|
@@ -781,14 +1152,14 @@ var Q = class {
|
|
|
781
1152
|
tempDir: s
|
|
782
1153
|
}));
|
|
783
1154
|
}), m.on("warning", (e) => {
|
|
784
|
-
this.logger.warn(`${
|
|
1155
|
+
this.logger.warn(`${C.TAG} Warning compressing file`, e), e.code !== "ENOENT" && p(e);
|
|
785
1156
|
}), m.on("error", (e) => {
|
|
786
|
-
this.logger.error(`${
|
|
1157
|
+
this.logger.error(`${C.TAG} Error compressing file`, e), p(e);
|
|
787
1158
|
}), m.pipe(a);
|
|
788
1159
|
try {
|
|
789
1160
|
for (let n of t) {
|
|
790
1161
|
let t = e.lstatSync(n);
|
|
791
|
-
if (t.isSymbolicLink() && !u) this.logger.warn(`${
|
|
1162
|
+
if (t.isSymbolicLink() && !u) this.logger.warn(`${C.TAG} Skipping symlink: ${n}`);
|
|
792
1163
|
else if ((t.isSymbolicLink() ? e.statSync(n) : t).isDirectory()) m.directory(n, r.basename(n));
|
|
793
1164
|
else {
|
|
794
1165
|
let t = r.basename(n), i = e.createReadStream(n);
|
|
@@ -809,4 +1180,4 @@ var Q = class {
|
|
|
809
1180
|
});
|
|
810
1181
|
}
|
|
811
1182
|
};
|
|
812
|
-
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 };
|