@d0whc3r/node-s3 1.3.12 → 1.4.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/LICENSE +21 -0
- package/README.md +226 -72
- package/bin/cli.js +2 -2
- package/dist/config.d.ts +36 -0
- package/dist/crypto.d.ts +3 -0
- package/dist/index.d.ts +31 -44
- package/dist/s3/buckets.d.ts +12 -0
- package/dist/s3/files.d.ts +37 -0
- package/dist/s3.cjs.js +2 -2
- package/dist/s3.es.mjs +772 -3
- package/dist/types.d.ts +55 -0
- package/package.json +81 -133
- package/CHANGELOG.md +0 -64
package/dist/s3.es.mjs
CHANGED
|
@@ -1,3 +1,772 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { CreateBucketCommand as e, DeleteBucketCommand as t, DeleteObjectsCommand as n, GetObjectCommand as r, HeadBucketCommand as i, HeadObjectCommand as a, ListBucketsCommand as o, ListObjectsV2Command as s, S3Client as c } from "@aws-sdk/client-s3";
|
|
2
|
+
import l from "node:fs";
|
|
3
|
+
import u from "node:fs/promises";
|
|
4
|
+
import d from "node:os";
|
|
5
|
+
import f from "node:path";
|
|
6
|
+
import { pipeline as p } from "node:stream/promises";
|
|
7
|
+
import { Upload as m } from "@aws-sdk/lib-storage";
|
|
8
|
+
import { ZipArchive as h } from "archiver";
|
|
9
|
+
import g from "dayjs";
|
|
10
|
+
import _ from "mime-types";
|
|
11
|
+
import v from "mysqldump";
|
|
12
|
+
import y from "node:crypto";
|
|
13
|
+
import { Transform as b } from "node:stream";
|
|
14
|
+
// src/config.ts
|
|
15
|
+
function x(e, t) {
|
|
16
|
+
return e === void 0 || e.trim() === "" ? t : [
|
|
17
|
+
"true",
|
|
18
|
+
"1",
|
|
19
|
+
"yes"
|
|
20
|
+
].includes(e.trim().toLowerCase());
|
|
21
|
+
}
|
|
22
|
+
function S(e, t) {
|
|
23
|
+
if (e === void 0 || e.trim() === "") return t;
|
|
24
|
+
let n = Number(e);
|
|
25
|
+
return Number.isNaN(n) ? t : n;
|
|
26
|
+
}
|
|
27
|
+
var C = class {
|
|
28
|
+
static API_VERSION = "2006-03-01";
|
|
29
|
+
static TAG = "[node-s3]";
|
|
30
|
+
static overrides = {};
|
|
31
|
+
static resetOverrides() {
|
|
32
|
+
this.overrides = {};
|
|
33
|
+
}
|
|
34
|
+
static get ENDPOINT() {
|
|
35
|
+
return "ENDPOINT" in this.overrides ? this.overrides.ENDPOINT : process.env.AWS_ENDPOINT_URL || process.env.ENDPOINT || "";
|
|
36
|
+
}
|
|
37
|
+
static set ENDPOINT(e) {
|
|
38
|
+
this.overrides.ENDPOINT = e;
|
|
39
|
+
}
|
|
40
|
+
static get BUCKET() {
|
|
41
|
+
return "BUCKET" in this.overrides ? this.overrides.BUCKET : process.env.BUCKET || "";
|
|
42
|
+
}
|
|
43
|
+
static set BUCKET(e) {
|
|
44
|
+
this.overrides.BUCKET = e;
|
|
45
|
+
}
|
|
46
|
+
static get REGION() {
|
|
47
|
+
return "REGION" in this.overrides ? this.overrides.REGION : process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || "us-east-1";
|
|
48
|
+
}
|
|
49
|
+
static set REGION(e) {
|
|
50
|
+
this.overrides.REGION = e;
|
|
51
|
+
}
|
|
52
|
+
static get ACCESS_KEY() {
|
|
53
|
+
return "ACCESS_KEY" in this.overrides ? this.overrides.ACCESS_KEY : process.env.AWS_ACCESS_KEY_ID || process.env.ACCESS_KEY || "";
|
|
54
|
+
}
|
|
55
|
+
static set ACCESS_KEY(e) {
|
|
56
|
+
this.overrides.ACCESS_KEY = e;
|
|
57
|
+
}
|
|
58
|
+
static get SECRET_KEY() {
|
|
59
|
+
return "SECRET_KEY" in this.overrides ? this.overrides.SECRET_KEY : process.env.AWS_SECRET_ACCESS_KEY || process.env.SECRET_KEY || "";
|
|
60
|
+
}
|
|
61
|
+
static set SECRET_KEY(e) {
|
|
62
|
+
this.overrides.SECRET_KEY = e;
|
|
63
|
+
}
|
|
64
|
+
static get SESSION_TOKEN() {
|
|
65
|
+
return "SESSION_TOKEN" in this.overrides ? this.overrides.SESSION_TOKEN : process.env.AWS_SESSION_TOKEN || "";
|
|
66
|
+
}
|
|
67
|
+
static set SESSION_TOKEN(e) {
|
|
68
|
+
this.overrides.SESSION_TOKEN = e;
|
|
69
|
+
}
|
|
70
|
+
static get MAX_RETRIES() {
|
|
71
|
+
return "MAX_RETRIES" in this.overrides ? this.overrides.MAX_RETRIES : S(process.env.MAX_RETRIES, 3);
|
|
72
|
+
}
|
|
73
|
+
static set MAX_RETRIES(e) {
|
|
74
|
+
this.overrides.MAX_RETRIES = e;
|
|
75
|
+
}
|
|
76
|
+
static get FORCE_PATH_STYLE() {
|
|
77
|
+
return "FORCE_PATH_STYLE" in this.overrides ? this.overrides.FORCE_PATH_STYLE : x(process.env.FORCE_PATH_STYLE, !0);
|
|
78
|
+
}
|
|
79
|
+
static set FORCE_PATH_STYLE(e) {
|
|
80
|
+
this.overrides.FORCE_PATH_STYLE = e;
|
|
81
|
+
}
|
|
82
|
+
static get SSL_ENABLED() {
|
|
83
|
+
return "SSL_ENABLED" in this.overrides ? this.overrides.SSL_ENABLED : x(process.env.SSL_ENABLED, !0);
|
|
84
|
+
}
|
|
85
|
+
static set SSL_ENABLED(e) {
|
|
86
|
+
this.overrides.SSL_ENABLED = e;
|
|
87
|
+
}
|
|
88
|
+
static get BACKUP_ENCRYPTION_KEY() {
|
|
89
|
+
return "BACKUP_ENCRYPTION_KEY" in this.overrides ? this.overrides.BACKUP_ENCRYPTION_KEY : process.env.BACKUP_ENCRYPTION_KEY || "";
|
|
90
|
+
}
|
|
91
|
+
static set BACKUP_ENCRYPTION_KEY(e) {
|
|
92
|
+
this.overrides.BACKUP_ENCRYPTION_KEY = e;
|
|
93
|
+
}
|
|
94
|
+
static get MYSQL_HOST() {
|
|
95
|
+
return "MYSQL_HOST" in this.overrides ? this.overrides.MYSQL_HOST : process.env.MYSQL_HOST || "localhost";
|
|
96
|
+
}
|
|
97
|
+
static set MYSQL_HOST(e) {
|
|
98
|
+
this.overrides.MYSQL_HOST = e;
|
|
99
|
+
}
|
|
100
|
+
static get MYSQL_PORT() {
|
|
101
|
+
return "MYSQL_PORT" in this.overrides ? this.overrides.MYSQL_PORT : S(process.env.MYSQL_PORT, 3306);
|
|
102
|
+
}
|
|
103
|
+
static set MYSQL_PORT(e) {
|
|
104
|
+
this.overrides.MYSQL_PORT = e;
|
|
105
|
+
}
|
|
106
|
+
static get MYSQL_USER() {
|
|
107
|
+
return "MYSQL_USER" in this.overrides ? this.overrides.MYSQL_USER : process.env.MYSQL_USER || "";
|
|
108
|
+
}
|
|
109
|
+
static set MYSQL_USER(e) {
|
|
110
|
+
this.overrides.MYSQL_USER = e;
|
|
111
|
+
}
|
|
112
|
+
static get MYSQL_PASSWORD() {
|
|
113
|
+
return "MYSQL_PASSWORD" in this.overrides ? this.overrides.MYSQL_PASSWORD : process.env.MYSQL_PASSWORD || "";
|
|
114
|
+
}
|
|
115
|
+
static set MYSQL_PASSWORD(e) {
|
|
116
|
+
this.overrides.MYSQL_PASSWORD = e;
|
|
117
|
+
}
|
|
118
|
+
static get MYSQL_DATABASE() {
|
|
119
|
+
return "MYSQL_DATABASE" in this.overrides ? this.overrides.MYSQL_DATABASE : process.env.MYSQL_DATABASE || "";
|
|
120
|
+
}
|
|
121
|
+
static set MYSQL_DATABASE(e) {
|
|
122
|
+
this.overrides.MYSQL_DATABASE = e;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
// src/s3/buckets.ts
|
|
126
|
+
function w(e) {
|
|
127
|
+
if (!e || typeof e != "object") return !1;
|
|
128
|
+
let t = e;
|
|
129
|
+
if (t.$metadata?.httpStatusCode === 404) return !0;
|
|
130
|
+
let n = [
|
|
131
|
+
"NotFound",
|
|
132
|
+
"NoSuchBucket",
|
|
133
|
+
"NoSuchKey",
|
|
134
|
+
"404"
|
|
135
|
+
];
|
|
136
|
+
return !!(t.name && n.includes(t.name) || t.Code && n.includes(t.Code));
|
|
137
|
+
}
|
|
138
|
+
function T(e, t) {
|
|
139
|
+
return e instanceof Error ? Error(`${C.TAG} ${t}: ${e.message}`, { cause: e }) : Error(`${C.TAG} ${t}: ${String(e)}`, { cause: e });
|
|
140
|
+
}
|
|
141
|
+
var E = class {
|
|
142
|
+
s3Sdk;
|
|
143
|
+
deleteAllContentFn;
|
|
144
|
+
constructor(e, t) {
|
|
145
|
+
this.s3Sdk = e, this.deleteAllContentFn = t;
|
|
146
|
+
}
|
|
147
|
+
async getBuckets() {
|
|
148
|
+
try {
|
|
149
|
+
return (await this.s3Sdk.send(new o({}))).Buckets || [];
|
|
150
|
+
} catch (e) {
|
|
151
|
+
throw T(e, "Failed to list buckets");
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
async createBucket(t) {
|
|
155
|
+
try {
|
|
156
|
+
return await this.s3Sdk.send(new e({ Bucket: t }));
|
|
157
|
+
} catch (e) {
|
|
158
|
+
throw T(e, `Failed to create bucket "${t}"`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
async removeBucket(e, n = !1) {
|
|
162
|
+
try {
|
|
163
|
+
return await this.s3Sdk.send(new t({ Bucket: e })), !0;
|
|
164
|
+
} catch (t) {
|
|
165
|
+
if (n && this.deleteAllContentFn) return await this.deleteAllContentFn(e), this.removeBucket(e, !1);
|
|
166
|
+
throw T(t, `Failed to remove bucket "${e}"`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async bucketExist(e) {
|
|
170
|
+
try {
|
|
171
|
+
return await this.s3Sdk.send(new i({ Bucket: e })), !0;
|
|
172
|
+
} catch (t) {
|
|
173
|
+
if (w(t)) return !1;
|
|
174
|
+
throw T(t, `Failed to check bucket "${e}"`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}, D = Buffer.from("NS3E", "ascii"), O = 1, k = 16, A = 12, j = 32, M = 16, N = D.length + 1 + k + A;
|
|
178
|
+
function P(e) {
|
|
179
|
+
if (!e) throw Error(`${C.TAG} Encryption key must be a non-empty string`);
|
|
180
|
+
let t = null, n = (n) => {
|
|
181
|
+
let i = y.randomBytes(k), a = y.randomBytes(A);
|
|
182
|
+
y.scrypt(e, i, j, (e, o) => {
|
|
183
|
+
e ? n(e) : (t = y.createCipheriv("aes-256-gcm", o, a), r.push(Buffer.concat([
|
|
184
|
+
D,
|
|
185
|
+
Buffer.from([O]),
|
|
186
|
+
i,
|
|
187
|
+
a
|
|
188
|
+
])), n());
|
|
189
|
+
});
|
|
190
|
+
}, r = new b({
|
|
191
|
+
transform(e, r, i) {
|
|
192
|
+
let a = () => {
|
|
193
|
+
try {
|
|
194
|
+
let n = t.update(e);
|
|
195
|
+
n.length > 0 && this.push(n), i();
|
|
196
|
+
} catch (e) {
|
|
197
|
+
i(e instanceof Error ? e : Error(String(e)));
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
t ? a() : n((e) => {
|
|
201
|
+
e ? i(e) : a();
|
|
202
|
+
});
|
|
203
|
+
},
|
|
204
|
+
flush(e) {
|
|
205
|
+
let r = () => {
|
|
206
|
+
try {
|
|
207
|
+
let n = t.final();
|
|
208
|
+
n.length > 0 && this.push(n), this.push(t.getAuthTag()), e();
|
|
209
|
+
} catch (t) {
|
|
210
|
+
e(t instanceof Error ? t : Error(String(t)));
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
t ? r() : n((t) => {
|
|
214
|
+
t ? e(t) : r();
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
return r;
|
|
219
|
+
}
|
|
220
|
+
function F(e) {
|
|
221
|
+
if (!e) throw Error(`${C.TAG} Decryption key must be a non-empty string`);
|
|
222
|
+
let t = Buffer.alloc(0), n = Buffer.alloc(0), r = null;
|
|
223
|
+
return new b({
|
|
224
|
+
transform(i, a, o) {
|
|
225
|
+
let s = (e) => {
|
|
226
|
+
try {
|
|
227
|
+
let t = n.length > 0 ? Buffer.concat([n, e]) : e;
|
|
228
|
+
if (t.length > M) {
|
|
229
|
+
let e = t.subarray(0, t.length - M);
|
|
230
|
+
n = t.subarray(t.length - M);
|
|
231
|
+
let i = r.update(e);
|
|
232
|
+
i.length > 0 && this.push(i);
|
|
233
|
+
} else n = t;
|
|
234
|
+
o();
|
|
235
|
+
} catch (e) {
|
|
236
|
+
o(e instanceof Error ? e : Error(String(e)));
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
if (r) {
|
|
240
|
+
s(i);
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
if (t = t.length > 0 ? Buffer.concat([t, i]) : i, t.length < N) {
|
|
244
|
+
o();
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (!t.subarray(0, D.length).equals(D)) {
|
|
248
|
+
o(/* @__PURE__ */ Error(`${C.TAG} Invalid encrypted stream header`));
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
let c = t[D.length];
|
|
252
|
+
if (c !== O) {
|
|
253
|
+
o(/* @__PURE__ */ Error(`${C.TAG} Unsupported encryption format version: ${c}`));
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
let l = D.length + 1, u = l + k, d = t.subarray(l, u), f = t.subarray(u, N), p = t.subarray(N);
|
|
257
|
+
t = Buffer.alloc(0), y.scrypt(e, d, j, (e, t) => {
|
|
258
|
+
e ? o(e) : (r = y.createDecipheriv("aes-256-gcm", t, f), s(p));
|
|
259
|
+
});
|
|
260
|
+
},
|
|
261
|
+
flush(e) {
|
|
262
|
+
if (!r) e(/* @__PURE__ */ Error(`${C.TAG} Encrypted stream is truncated or empty`));
|
|
263
|
+
else if (n.length !== M) e(/* @__PURE__ */ Error(`${C.TAG} Encrypted stream is missing authentication tag`));
|
|
264
|
+
else try {
|
|
265
|
+
r.setAuthTag(n);
|
|
266
|
+
let t = r.final();
|
|
267
|
+
t.length > 0 && this.push(t), e();
|
|
268
|
+
} catch (t) {
|
|
269
|
+
e(Error(`${C.TAG} Decryption failed: invalid key or corrupted data`, { cause: t }));
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
// src/s3/files.ts
|
|
275
|
+
var I = /^(\d+)(s|m|h|d|w|M|y|seconds?|minutes?|hours?|days?|weeks?|months?|years?)$/, L = {
|
|
276
|
+
s: "second",
|
|
277
|
+
second: "second",
|
|
278
|
+
seconds: "second",
|
|
279
|
+
m: "minute",
|
|
280
|
+
minute: "minute",
|
|
281
|
+
minutes: "minute",
|
|
282
|
+
h: "hour",
|
|
283
|
+
hour: "hour",
|
|
284
|
+
hours: "hour",
|
|
285
|
+
d: "day",
|
|
286
|
+
day: "day",
|
|
287
|
+
days: "day",
|
|
288
|
+
w: "week",
|
|
289
|
+
week: "week",
|
|
290
|
+
weeks: "week",
|
|
291
|
+
M: "month",
|
|
292
|
+
month: "month",
|
|
293
|
+
months: "month",
|
|
294
|
+
y: "year",
|
|
295
|
+
year: "year",
|
|
296
|
+
years: "year"
|
|
297
|
+
}, R = 1e3, z = 4, B = 6;
|
|
298
|
+
function V(...e) {
|
|
299
|
+
let t = [];
|
|
300
|
+
for (let n of e) {
|
|
301
|
+
if (!n) continue;
|
|
302
|
+
let e = n.replace(/\\/g, "/");
|
|
303
|
+
for (let r of e.split("/")) if (r && r !== ".") {
|
|
304
|
+
if (r === "..") throw Error(`${C.TAG} Invalid S3 key segment ".." in "${n}"`);
|
|
305
|
+
t.push(r);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return f.posix.join(...t);
|
|
309
|
+
}
|
|
310
|
+
function H(e) {
|
|
311
|
+
if (!e) return;
|
|
312
|
+
let t = V(e);
|
|
313
|
+
return t ? `${t}/` : void 0;
|
|
314
|
+
}
|
|
315
|
+
function U(e) {
|
|
316
|
+
let t = e.search(/[*?[{]/);
|
|
317
|
+
if (t === -1) return e;
|
|
318
|
+
let n = e.slice(0, t);
|
|
319
|
+
return n ? n.endsWith("/") || n.endsWith("\\") ? n.slice(0, -1) || "." : f.dirname(n) : ".";
|
|
320
|
+
}
|
|
321
|
+
function W(e, t) {
|
|
322
|
+
if (!t.length) return !1;
|
|
323
|
+
let n = e.replace(/\\/g, "/"), r = f.posix.basename(n);
|
|
324
|
+
return t.some((e) => f.matchesGlob(n, e) || f.matchesGlob(r, e));
|
|
325
|
+
}
|
|
326
|
+
async function G(e, t, n) {
|
|
327
|
+
let r = Array.from({ length: e.length }), i = Math.max(1, Math.min(t, e.length)), a = 0, o = async () => {
|
|
328
|
+
for (; a < e.length;) {
|
|
329
|
+
let t = a;
|
|
330
|
+
a += 1, r[t] = await n(e[t]);
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
return await Promise.all(Array.from({ length: i }, () => o())), r;
|
|
334
|
+
}
|
|
335
|
+
var K = class {
|
|
336
|
+
s3Sdk;
|
|
337
|
+
defaultUploadOptions = {
|
|
338
|
+
create: !0,
|
|
339
|
+
replace: !1
|
|
340
|
+
};
|
|
341
|
+
ensureBucketFn;
|
|
342
|
+
logger;
|
|
343
|
+
defaultDryRun;
|
|
344
|
+
defaultOnProgress;
|
|
345
|
+
constructor(e, t = {}) {
|
|
346
|
+
this.s3Sdk = e, this.ensureBucketFn = t.ensureBucket, this.logger = t.logger ?? console, this.defaultDryRun = t.dryRun ?? !1, this.defaultOnProgress = t.onProgress;
|
|
347
|
+
}
|
|
348
|
+
async getFiles(e, t) {
|
|
349
|
+
let n = t ? H(t) : void 0, r = [], i;
|
|
350
|
+
try {
|
|
351
|
+
do {
|
|
352
|
+
let t = await this.s3Sdk.send(new s({
|
|
353
|
+
Bucket: e,
|
|
354
|
+
...n ? { Prefix: n } : {},
|
|
355
|
+
...i ? { ContinuationToken: i } : {}
|
|
356
|
+
}));
|
|
357
|
+
t.Contents && r.push(...t.Contents), i = t.IsTruncated ? t.NextContinuationToken : void 0;
|
|
358
|
+
} while (i);
|
|
359
|
+
return r;
|
|
360
|
+
} catch (t) {
|
|
361
|
+
throw T(t, `Failed to list files in bucket "${e}"`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
async fileInfo(e, t) {
|
|
365
|
+
let n = V(t);
|
|
366
|
+
try {
|
|
367
|
+
return await this.s3Sdk.send(new a({
|
|
368
|
+
Bucket: e,
|
|
369
|
+
Key: n
|
|
370
|
+
}));
|
|
371
|
+
} catch (t) {
|
|
372
|
+
throw T(t, `Failed to get info for "${n}" in bucket "${e}"`);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
async fileExist(e, t) {
|
|
376
|
+
let n = V(t);
|
|
377
|
+
try {
|
|
378
|
+
return await this.s3Sdk.send(new a({
|
|
379
|
+
Bucket: e,
|
|
380
|
+
Key: n
|
|
381
|
+
})), !0;
|
|
382
|
+
} catch (t) {
|
|
383
|
+
if (w(t)) return !1;
|
|
384
|
+
throw T(t, `Failed to check existence of "${n}" in bucket "${e}"`);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
async deleteAllContent(e) {
|
|
388
|
+
let t = (await this.getFiles(e)).filter((e) => !!e.Key).map((e) => ({ Key: e.Key }));
|
|
389
|
+
return this.deleteFiles(e, t);
|
|
390
|
+
}
|
|
391
|
+
async uploadFile(e, t, n, r) {
|
|
392
|
+
let i = f.basename(t), a = V(n, i), o = {
|
|
393
|
+
...this.defaultUploadOptions,
|
|
394
|
+
...r
|
|
395
|
+
};
|
|
396
|
+
if (o.dryRun ?? this.defaultDryRun) return this.logger.info?.(`${C.TAG} [dry-run] Would upload "${t}" to "s3://${e}/${a}"`), {
|
|
397
|
+
Bucket: e,
|
|
398
|
+
Key: a,
|
|
399
|
+
$metadata: {}
|
|
400
|
+
};
|
|
401
|
+
let s = l.createReadStream(t);
|
|
402
|
+
try {
|
|
403
|
+
return await this.uploadStream(e, s, i, n, o);
|
|
404
|
+
} finally {
|
|
405
|
+
s.destroy();
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
async uploadStream(e, t, n, r, i) {
|
|
409
|
+
let a = f.basename(n);
|
|
410
|
+
if (!a || a === "." || a === "..") throw Error(`${C.TAG} Invalid upload filename: "${n}"`);
|
|
411
|
+
let o = V(r, a), s = _.contentType(a) || void 0, c = {
|
|
412
|
+
...this.defaultUploadOptions,
|
|
413
|
+
...i
|
|
414
|
+
};
|
|
415
|
+
if (c.dryRun ?? this.defaultDryRun) return this.logger.info?.(`${C.TAG} [dry-run] Would upload stream "${a}" to "s3://${e}/${o}"`), {
|
|
416
|
+
Bucket: e,
|
|
417
|
+
Key: o,
|
|
418
|
+
$metadata: {}
|
|
419
|
+
};
|
|
420
|
+
let l = this.getExpire(c);
|
|
421
|
+
if (c.skipBucketCheck || await this.createIfNeeded(c, e), !await this.canBeReplaced(c, e, o)) throw Error(`${C.TAG} File "${o}" already exists in bucket "${e}" and will not be replaced`);
|
|
422
|
+
let u = t, d;
|
|
423
|
+
if (c.encryptKey) {
|
|
424
|
+
let e = P(c.encryptKey);
|
|
425
|
+
t.on("error", (t) => {
|
|
426
|
+
e.destroy(t);
|
|
427
|
+
}), u = t.pipe(e), d = u;
|
|
428
|
+
}
|
|
429
|
+
let p = c.onProgress ?? this.defaultOnProgress;
|
|
430
|
+
try {
|
|
431
|
+
let t = new m({
|
|
432
|
+
client: this.s3Sdk,
|
|
433
|
+
params: {
|
|
434
|
+
Key: o,
|
|
435
|
+
Bucket: e,
|
|
436
|
+
Body: u,
|
|
437
|
+
Expires: l,
|
|
438
|
+
ContentType: s,
|
|
439
|
+
...c.storageClass ? { StorageClass: c.storageClass } : {},
|
|
440
|
+
...c.acl ? { ACL: c.acl } : {},
|
|
441
|
+
...c.serverSideEncryption ? { ServerSideEncryption: c.serverSideEncryption } : {},
|
|
442
|
+
...c.kmsKeyId ? { SSEKMSKeyId: c.kmsKeyId } : {}
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
return p && t.on("httpUploadProgress", (e) => {
|
|
446
|
+
p({
|
|
447
|
+
loaded: e.loaded,
|
|
448
|
+
total: e.total,
|
|
449
|
+
part: e.part,
|
|
450
|
+
key: o
|
|
451
|
+
});
|
|
452
|
+
}), await t.done();
|
|
453
|
+
} catch (t) {
|
|
454
|
+
throw T(t, `Failed to upload "${o}" to bucket "${e}"`);
|
|
455
|
+
} finally {
|
|
456
|
+
d?.destroy();
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
async uploadFiles(e, t, n, r = {}) {
|
|
460
|
+
let i = Array.isArray(t) ? t : [t], a = {
|
|
461
|
+
...this.defaultUploadOptions,
|
|
462
|
+
...r
|
|
463
|
+
}, o = a.dryRun ?? this.defaultDryRun, s = a.exclude ? Array.isArray(a.exclude) ? a.exclude : [a.exclude] : [], c = a.followSymlinks ?? !1;
|
|
464
|
+
!a.skipBucketCheck && !o && await this.createIfNeeded(a, e);
|
|
465
|
+
let l = {
|
|
466
|
+
replace: a.replace,
|
|
467
|
+
create: a.create,
|
|
468
|
+
expire: a.expire,
|
|
469
|
+
expireDate: a.expireDate,
|
|
470
|
+
storageClass: a.storageClass,
|
|
471
|
+
acl: a.acl,
|
|
472
|
+
serverSideEncryption: a.serverSideEncryption,
|
|
473
|
+
kmsKeyId: a.kmsKeyId,
|
|
474
|
+
dryRun: a.dryRun,
|
|
475
|
+
encryptKey: a.encryptKey,
|
|
476
|
+
onProgress: a.onProgress,
|
|
477
|
+
skipBucketCheck: !0
|
|
478
|
+
};
|
|
479
|
+
if (a.compress) {
|
|
480
|
+
let t = typeof a.compress == "string" ? a.compress : `zipped_${g().format("YYYY-MM-DD.HHmmss")}.zip`, { filePath: r, tmpDir: o } = await this.compressFiles(i, t, {
|
|
481
|
+
compressionLevel: a.compressionLevel,
|
|
482
|
+
followSymlinks: c,
|
|
483
|
+
exclude: s
|
|
484
|
+
});
|
|
485
|
+
try {
|
|
486
|
+
let t = await this.uploadFile(e, r, n, l);
|
|
487
|
+
return { [r]: t };
|
|
488
|
+
} finally {
|
|
489
|
+
await u.rm(o, {
|
|
490
|
+
recursive: !0,
|
|
491
|
+
force: !0
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
let d = await this.collectUploadTasks(i, n, a.baseDir, c, s), f = a.concurrency ?? z, p = {}, m = await G(d, f, async (t) => {
|
|
496
|
+
let n = await this.uploadFile(e, t.file, t.folderName, l);
|
|
497
|
+
return {
|
|
498
|
+
file: t.file,
|
|
499
|
+
response: n
|
|
500
|
+
};
|
|
501
|
+
});
|
|
502
|
+
for (let e of m) p[e.file] = e.response;
|
|
503
|
+
return p;
|
|
504
|
+
}
|
|
505
|
+
async downloadFile(e, t, n, i = {}) {
|
|
506
|
+
let a = V(t);
|
|
507
|
+
try {
|
|
508
|
+
let t = await this.s3Sdk.send(new r({
|
|
509
|
+
Bucket: e,
|
|
510
|
+
Key: a
|
|
511
|
+
}));
|
|
512
|
+
if (!t.Body) throw Error(`${C.TAG} Empty response body for "${a}"`);
|
|
513
|
+
await u.mkdir(f.dirname(f.resolve(n)), { recursive: !0 });
|
|
514
|
+
let o = t.Body, s = l.createWriteStream(n);
|
|
515
|
+
if (i.decryptKey) {
|
|
516
|
+
let e = F(i.decryptKey);
|
|
517
|
+
await p(o, e, s);
|
|
518
|
+
} else await p(o, s);
|
|
519
|
+
return n;
|
|
520
|
+
} catch (t) {
|
|
521
|
+
throw T(t, `Failed to download "${a}" from bucket "${e}"`);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
async cleanOlder(e, t, n, r = {}) {
|
|
525
|
+
let { value: i, unit: a } = this.getTimeSpace(t), o = g().subtract(i, a), s = (await this.getFiles(e, n)).filter((e) => !!(e.Key && e.LastModified));
|
|
526
|
+
s.sort((e, t) => (t.LastModified?.getTime() ?? 0) - (e.LastModified?.getTime() ?? 0));
|
|
527
|
+
let c = r.keepLast ?? 0, l = (c > 0 ? s.slice(c) : s).filter((e) => g(e.LastModified).isBefore(o)).map((e) => ({ Key: e.Key }));
|
|
528
|
+
if (r.dryRun ?? this.defaultDryRun) {
|
|
529
|
+
for (let e of l) e.Key && this.logger.info?.(`${C.TAG} [dry-run] Would delete file: ${e.Key}`);
|
|
530
|
+
return {
|
|
531
|
+
Deleted: l.map((e) => ({ Key: e.Key })),
|
|
532
|
+
Errors: [],
|
|
533
|
+
$metadata: {}
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
return this.deleteFiles(e, l);
|
|
537
|
+
}
|
|
538
|
+
async createDumpFile() {
|
|
539
|
+
let e = C.MYSQL_HOST, t = C.MYSQL_PORT, n = C.MYSQL_USER, r = C.MYSQL_PASSWORD, i = C.MYSQL_DATABASE;
|
|
540
|
+
if (!n || !r || !i) throw Error(`${C.TAG} Error in mysql-dump environment variables not defined: $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_HOST, $MYSQL_PORT`);
|
|
541
|
+
let { dump: a } = await v({ connection: {
|
|
542
|
+
host: e,
|
|
543
|
+
port: t,
|
|
544
|
+
user: n,
|
|
545
|
+
password: r,
|
|
546
|
+
database: i
|
|
547
|
+
} }), o = await u.mkdtemp(f.join(d.tmpdir(), "node-s3-dump-"));
|
|
548
|
+
await u.chmod(o, 448);
|
|
549
|
+
let s = f.join(o, `mysqldump-${g().format("YYYY-MM-DD.HHmmss")}.sql`), c = Object.values(a).map((e) => e && e.replace(/^# /gm, "-- ")).join("\n\n");
|
|
550
|
+
return await u.writeFile(s, c, { mode: 384 }), s;
|
|
551
|
+
}
|
|
552
|
+
async deleteFiles(e, t) {
|
|
553
|
+
let r = t.filter((e) => !!e.Key);
|
|
554
|
+
if (!r.length) return this.logger.info?.(`${C.TAG} No files deleted`), {
|
|
555
|
+
Deleted: [],
|
|
556
|
+
Errors: [],
|
|
557
|
+
$metadata: {}
|
|
558
|
+
};
|
|
559
|
+
let i = [], a = [], o = {};
|
|
560
|
+
try {
|
|
561
|
+
for (let t = 0; t < r.length; t += R) {
|
|
562
|
+
let s = { Objects: r.slice(t, t + R) }, c = await this.s3Sdk.send(new n({
|
|
563
|
+
Bucket: e,
|
|
564
|
+
Delete: s
|
|
565
|
+
}));
|
|
566
|
+
if (o = c.$metadata, c.Deleted?.length) {
|
|
567
|
+
i.push(...c.Deleted);
|
|
568
|
+
for (let e of c.Deleted) e.Key && this.logger.warn?.(`${C.TAG} Deleted file: ${e.Key}`);
|
|
569
|
+
}
|
|
570
|
+
c.Errors?.length && a.push(...c.Errors);
|
|
571
|
+
}
|
|
572
|
+
} catch (t) {
|
|
573
|
+
throw T(t, `Failed to delete files from bucket "${e}"`);
|
|
574
|
+
}
|
|
575
|
+
if (a.length > 0) {
|
|
576
|
+
let t = a.map((e) => `${e.Key ?? "unknown"} (${e.Code ?? "Error"}: ${e.Message ?? "unknown error"})`).join("; ");
|
|
577
|
+
throw Error(`${C.TAG} Failed to delete ${a.length} file(s) from bucket "${e}": ${t}`);
|
|
578
|
+
}
|
|
579
|
+
return i.length || this.logger.info?.(`${C.TAG} No files deleted`), {
|
|
580
|
+
Deleted: i,
|
|
581
|
+
Errors: a,
|
|
582
|
+
$metadata: o
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
async collectUploadTasks(e, t, n, r, i) {
|
|
586
|
+
let a = [];
|
|
587
|
+
for (let o of e) {
|
|
588
|
+
if (o.includes("*")) {
|
|
589
|
+
let e = U(o), s = l.globSync(o), c = this.extractBaseDir(e, n, t), u = await this.collectUploadTasks(s, c, e, r, i);
|
|
590
|
+
a.push(...u);
|
|
591
|
+
continue;
|
|
592
|
+
}
|
|
593
|
+
let e = await u.lstat(o);
|
|
594
|
+
if (e.isSymbolicLink()) {
|
|
595
|
+
if (!r) {
|
|
596
|
+
this.logger.warn?.(`${C.TAG} Skipping symlink: ${o}`);
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
if ((await u.stat(o)).isDirectory()) {
|
|
600
|
+
let e = (await u.readdir(o)).map((e) => f.join(o, e)), s = this.extractBaseDir(o, n, t), c = await this.collectUploadTasks(e, s, o, r, i);
|
|
601
|
+
a.push(...c);
|
|
602
|
+
continue;
|
|
603
|
+
}
|
|
604
|
+
} else if (e.isDirectory()) {
|
|
605
|
+
let e = (await u.readdir(o)).map((e) => f.join(o, e)), s = this.extractBaseDir(o, n, t), c = await this.collectUploadTasks(e, s, o, r, i);
|
|
606
|
+
a.push(...c);
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
W(o, i) || a.push({
|
|
610
|
+
file: o,
|
|
611
|
+
folderName: t
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
return a;
|
|
615
|
+
}
|
|
616
|
+
extractBaseDir(e, t, n) {
|
|
617
|
+
if (!t) return n;
|
|
618
|
+
let r = f.relative(t, e);
|
|
619
|
+
return !r || r === "." ? n : V(n, r);
|
|
620
|
+
}
|
|
621
|
+
async canBeReplaced(e, t, n) {
|
|
622
|
+
return !(!e.replace && await this.fileExist(t, n));
|
|
623
|
+
}
|
|
624
|
+
async createIfNeeded(e, t) {
|
|
625
|
+
e.create && this.ensureBucketFn && await this.ensureBucketFn(t);
|
|
626
|
+
}
|
|
627
|
+
getExpire(e) {
|
|
628
|
+
if (e.expireDate) return e.expireDate;
|
|
629
|
+
if (e.expire) {
|
|
630
|
+
let { value: t, unit: n } = this.getTimeSpace(e.expire);
|
|
631
|
+
return g().add(t, n).toDate();
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
getTimeSpace(e) {
|
|
635
|
+
let t = I.exec(e);
|
|
636
|
+
if (!t) throw Error(`${C.TAG} Invalid time definition "${e}". Expected format like "30s", "15m", "24h", "7d", "2w", "1M", or "1y"`);
|
|
637
|
+
let n = Number(t[1]), r = L[t[2]];
|
|
638
|
+
if (!r || Number.isNaN(n)) throw Error(`${C.TAG} Invalid time unit in "${e}"`);
|
|
639
|
+
return {
|
|
640
|
+
value: n,
|
|
641
|
+
unit: r
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
compressFiles(e, t, n = {}) {
|
|
645
|
+
let r = f.basename(t);
|
|
646
|
+
if (!r || r === "." || r === ".." || r.includes("*")) return Promise.reject(/* @__PURE__ */ Error(`${C.TAG} Invalid archive filename: "${t}"`));
|
|
647
|
+
let i = l.mkdtempSync(f.join(d.tmpdir(), "node-s3-"));
|
|
648
|
+
l.chmodSync(i, 448);
|
|
649
|
+
let a = f.join(i, r), o = n.followSymlinks ?? !1, s = n.exclude ?? [];
|
|
650
|
+
return new Promise((t, r) => {
|
|
651
|
+
let c = l.createWriteStream(a, { mode: 384 }), u = [], d = !1, p = (e) => {
|
|
652
|
+
d || (d = !0, u.forEach((e) => e.destroy()), c.destroy(), l.rmSync(i, {
|
|
653
|
+
recursive: !0,
|
|
654
|
+
force: !0
|
|
655
|
+
}), r(e instanceof Error ? e : Error(String(e))));
|
|
656
|
+
}, m = new h({ zlib: { level: n.compressionLevel ?? B } });
|
|
657
|
+
c.on("close", () => {
|
|
658
|
+
d || (d = !0, t({
|
|
659
|
+
filePath: a,
|
|
660
|
+
tmpDir: i
|
|
661
|
+
})), c.destroy();
|
|
662
|
+
}), c.on("error", p), m.on("warning", (e) => {
|
|
663
|
+
this.logger.warn?.(`${C.TAG} Warning compressing file`, e), e.code !== "ENOENT" && p(e);
|
|
664
|
+
}), m.on("error", p), m.pipe(c);
|
|
665
|
+
try {
|
|
666
|
+
for (let t of e) if (t.includes("*")) m.glob(t, s.length ? { ignore: s } : void 0);
|
|
667
|
+
else {
|
|
668
|
+
let e = l.lstatSync(t);
|
|
669
|
+
if (e.isSymbolicLink() && !o) {
|
|
670
|
+
this.logger.warn?.(`${C.TAG} Skipping symlink in archive: ${t}`);
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
if ((e.isSymbolicLink() ? l.statSync(t) : e).isDirectory()) m.directory(t, !1, (e) => !W(e.name, s) && e);
|
|
674
|
+
else {
|
|
675
|
+
if (W(t, s)) continue;
|
|
676
|
+
let e = f.basename(t), n = l.createReadStream(t);
|
|
677
|
+
n.on("error", p), m.append(n, { name: e }), u.push(n);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
} catch (e) {
|
|
681
|
+
p(e);
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
m.finalize().catch(p);
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
}, q = class {
|
|
688
|
+
bucket = "";
|
|
689
|
+
endpoint = "";
|
|
690
|
+
s3Sdk;
|
|
691
|
+
ownsClient = !1;
|
|
692
|
+
s3Buckets;
|
|
693
|
+
s3Files;
|
|
694
|
+
constructor(e = {}, t) {
|
|
695
|
+
this.setConfig(e, t);
|
|
696
|
+
}
|
|
697
|
+
setConfig(e = {}, t) {
|
|
698
|
+
this.s3Sdk && this.ownsClient && this.s3Sdk.destroy();
|
|
699
|
+
let n = e.endpoint || C.ENDPOINT || void 0, r = e.accessKeyId || C.ACCESS_KEY || void 0, i = e.secretAccessKey || C.SECRET_KEY || void 0, a = e.sessionToken || C.SESSION_TOKEN || void 0, o = e.region || C.REGION || "us-east-1", s = e.logger ?? console, l = !!(n && /^http:\/\//i.test(n)), u = e.sslEnabled === void 0 ? !l && C.SSL_ENABLED : e.sslEnabled;
|
|
700
|
+
l && e.sslEnabled === void 0 && s.warn?.(`${C.TAG} Using insecure HTTP endpoint "${n}"`);
|
|
701
|
+
let d = {
|
|
702
|
+
region: o,
|
|
703
|
+
tls: u,
|
|
704
|
+
maxAttempts: (e.maxRetries ?? C.MAX_RETRIES) + 1,
|
|
705
|
+
forcePathStyle: e.forcePathStyle === void 0 ? C.FORCE_PATH_STYLE : e.forcePathStyle,
|
|
706
|
+
...n ? { endpoint: n } : {},
|
|
707
|
+
...r && i ? { credentials: {
|
|
708
|
+
accessKeyId: r,
|
|
709
|
+
secretAccessKey: i,
|
|
710
|
+
...a ? { sessionToken: a } : {}
|
|
711
|
+
} } : {}
|
|
712
|
+
};
|
|
713
|
+
this.bucket = e.bucket === void 0 ? C.BUCKET : e.bucket, this.endpoint = n || "", t ? (this.s3Sdk = t, this.ownsClient = !1) : (this.s3Sdk = new c(d), this.ownsClient = !0), this.s3Files = new K(this.s3Sdk, {
|
|
714
|
+
ensureBucket: (e) => this.ensureBucket(e),
|
|
715
|
+
logger: s,
|
|
716
|
+
dryRun: e.dryRun,
|
|
717
|
+
onProgress: e.onProgress
|
|
718
|
+
}), this.s3Buckets = new E(this.s3Sdk, (e) => this.s3Files.deleteAllContent(e));
|
|
719
|
+
}
|
|
720
|
+
destroy() {
|
|
721
|
+
this.s3Sdk && this.ownsClient && (this.s3Sdk.destroy(), this.s3Sdk = void 0, this.ownsClient = !1);
|
|
722
|
+
}
|
|
723
|
+
getBuckets() {
|
|
724
|
+
return this.s3Buckets.getBuckets();
|
|
725
|
+
}
|
|
726
|
+
createBucket(e = this.bucket) {
|
|
727
|
+
return this.checkBucket(e), this.s3Buckets.createBucket(e);
|
|
728
|
+
}
|
|
729
|
+
removeBucket(e = !1, t = this.bucket) {
|
|
730
|
+
return this.checkBucket(t), this.s3Buckets.removeBucket(t, e);
|
|
731
|
+
}
|
|
732
|
+
bucketExist(e = this.bucket) {
|
|
733
|
+
return this.checkBucket(e), this.s3Buckets.bucketExist(e);
|
|
734
|
+
}
|
|
735
|
+
getFiles(e = this.bucket, t) {
|
|
736
|
+
return this.checkBucket(e), this.s3Files.getFiles(e, t);
|
|
737
|
+
}
|
|
738
|
+
fileInfo(e, t = this.bucket) {
|
|
739
|
+
return this.checkBucket(t), this.s3Files.fileInfo(t, e);
|
|
740
|
+
}
|
|
741
|
+
fileExist(e, t = this.bucket) {
|
|
742
|
+
return this.checkBucket(t), this.s3Files.fileExist(t, e);
|
|
743
|
+
}
|
|
744
|
+
uploadFile(e, t, n, r = this.bucket) {
|
|
745
|
+
return this.checkBucket(r), this.s3Files.uploadFile(r, e, t, n);
|
|
746
|
+
}
|
|
747
|
+
uploadFiles(e, t, n, r = this.bucket) {
|
|
748
|
+
return this.checkBucket(r), this.s3Files.uploadFiles(r, e, t, n);
|
|
749
|
+
}
|
|
750
|
+
uploadStream(e, t, n, r, i = this.bucket) {
|
|
751
|
+
return this.checkBucket(i), this.s3Files.uploadStream(i, e, t, n, r);
|
|
752
|
+
}
|
|
753
|
+
downloadFile(e, t, n, r = this.bucket) {
|
|
754
|
+
return this.checkBucket(r), this.s3Files.downloadFile(r, e, t, n);
|
|
755
|
+
}
|
|
756
|
+
deleteAllContent(e = this.bucket) {
|
|
757
|
+
return this.checkBucket(e), this.s3Files.deleteAllContent(e);
|
|
758
|
+
}
|
|
759
|
+
cleanOlder(e, t, n = this.bucket, r) {
|
|
760
|
+
return this.checkBucket(n), this.s3Files.cleanOlder(n, e, t, r);
|
|
761
|
+
}
|
|
762
|
+
createDumpFile() {
|
|
763
|
+
return this.s3Files.createDumpFile();
|
|
764
|
+
}
|
|
765
|
+
async ensureBucket(e) {
|
|
766
|
+
await this.s3Buckets.bucketExist(e) || await this.s3Buckets.createBucket(e);
|
|
767
|
+
}
|
|
768
|
+
checkBucket(e) {
|
|
769
|
+
if (!e) throw Error(`${C.TAG} Bucket not defined`);
|
|
770
|
+
}
|
|
771
|
+
};
|
|
772
|
+
export { q as S3Wrapper, F as createDecryptStream, P as createEncryptStream, V as normalizeS3Key };
|