@e-mc/cloud 0.7.1 → 0.8.1
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/index.js +58 -56
- package/package.json +5 -4
- package/util.d.ts +3 -0
- package/util.js +44 -3
package/index.js
CHANGED
|
@@ -210,53 +210,52 @@ class Cloud extends core_1.ClientDb {
|
|
|
210
210
|
let pending = (downloadMap || (downloadMap = {}))[location];
|
|
211
211
|
if (pending) {
|
|
212
212
|
pending.add(downloadUri);
|
|
213
|
+
continue;
|
|
213
214
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
if (
|
|
225
|
-
|
|
226
|
-
fs.renameSync(value, destUri);
|
|
227
|
-
}
|
|
228
|
-
else {
|
|
229
|
-
fs.copyFileSync(value, destUri);
|
|
230
|
-
}
|
|
231
|
-
size = this.addDownload(destUri);
|
|
215
|
+
pending = new Set([downloadUri]);
|
|
216
|
+
download.admin = data.admin;
|
|
217
|
+
const task = instance.downloadObject(data.service, instance.getCredential(data), data.bucket, download, async (value) => {
|
|
218
|
+
let result;
|
|
219
|
+
if (value && !instance.aborted) {
|
|
220
|
+
const items = Array.from(pending);
|
|
221
|
+
for (let i = 0, length = items.length, size, copy; i < length; ++i) {
|
|
222
|
+
const destUri = items[i];
|
|
223
|
+
try {
|
|
224
|
+
if (typeof value === 'string') {
|
|
225
|
+
if (!copy && i === length - 1) {
|
|
226
|
+
fs.renameSync(value, destUri);
|
|
232
227
|
}
|
|
233
228
|
else {
|
|
234
|
-
fs.
|
|
235
|
-
this.addDownload(size = value.length);
|
|
229
|
+
fs.copyFileSync(value, destUri);
|
|
236
230
|
}
|
|
237
|
-
this.
|
|
238
|
-
this.formatMessage(64 /* LOG_TYPE.CLOUD */, data.service, ["Download success" /* VAL_CLOUD.DOWNLOAD_FILE */, (0, types_1.formatSize)(size)], destUri, { ...Cloud.LOG_CLOUD_DOWNLOAD });
|
|
239
|
-
result || (result = destUri);
|
|
231
|
+
size = this.addDownload(destUri);
|
|
240
232
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
233
|
+
else {
|
|
234
|
+
fs.writeFileSync(destUri, value);
|
|
235
|
+
this.addDownload(size = value.length);
|
|
236
|
+
}
|
|
237
|
+
this.add(destUri);
|
|
238
|
+
this.formatMessage(64 /* LOG_TYPE.CLOUD */, data.service, ["Download success" /* VAL_CLOUD.DOWNLOAD_FILE */, (0, types_1.formatSize)(size)], destUri, { ...Cloud.LOG_CLOUD_DOWNLOAD });
|
|
239
|
+
result || (result = destUri);
|
|
240
|
+
}
|
|
241
|
+
catch (err) {
|
|
242
|
+
if (!copy && core_1.ClientDb.isErrorCode(err, 'EXDEV')) {
|
|
243
|
+
copy = true;
|
|
244
|
+
--i;
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
instance.writeFail(["Unable to write file" /* ERR_MESSAGE.WRITE_FILE */, path.basename(destUri)], err, { type: 32 /* LOG_TYPE.FILE */, fatal: !!active, startTime });
|
|
249
248
|
}
|
|
250
249
|
}
|
|
251
250
|
}
|
|
252
|
-
return Promise.resolve(result);
|
|
253
|
-
})
|
|
254
|
-
.catch(err => instance.writeFail(["Download failed" /* ERR_CLOUD.DOWNLOAD_FAIL */, path.basename(downloadUri)], err, { type: 64 /* LOG_TYPE.CLOUD */, startTime }));
|
|
255
|
-
if (active || waitStatus || this.incremental === 'staging') {
|
|
256
|
-
tasks.push(task);
|
|
257
251
|
}
|
|
258
|
-
|
|
252
|
+
return result;
|
|
253
|
+
})
|
|
254
|
+
.catch(err => instance.writeFail(["Download failed" /* ERR_CLOUD.DOWNLOAD_FAIL */, path.basename(downloadUri)], err, { type: 64 /* LOG_TYPE.CLOUD */, startTime }));
|
|
255
|
+
if (active || waitStatus || this.incremental === 'staging') {
|
|
256
|
+
tasks.push(task);
|
|
259
257
|
}
|
|
258
|
+
downloadMap[location] = pending;
|
|
260
259
|
}
|
|
261
260
|
}
|
|
262
261
|
}
|
|
@@ -284,20 +283,23 @@ class Cloud extends core_1.ClientDb {
|
|
|
284
283
|
state.localStorage.set(file, upload);
|
|
285
284
|
}
|
|
286
285
|
const callback = async (value) => {
|
|
287
|
-
if (
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
return;
|
|
286
|
+
if ((0, types_1.isString)(value) && !ignoreProcess) {
|
|
287
|
+
for (const { instance: document } of host.Document) {
|
|
288
|
+
if (document.cloudUpload && await document.cloudUpload(state, file, value, active)) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
293
291
|
}
|
|
294
292
|
}
|
|
295
293
|
};
|
|
296
294
|
const task = new Promise(resolve => {
|
|
297
295
|
const { service, bucket = state.bucketGroup, admin } = storage;
|
|
296
|
+
let minStreamSize = upload.minStreamSize;
|
|
297
|
+
if ((0, types_1.isString)(minStreamSize)) {
|
|
298
|
+
minStreamSize = (0, types_1.formatSize)(minStreamSize);
|
|
299
|
+
}
|
|
298
300
|
const credential = instance.getCredential(storage, true);
|
|
299
301
|
const uploading = [];
|
|
300
|
-
getFiles(file, upload).forEach((group, index) => {
|
|
302
|
+
getFiles(file, upload).forEach(async (group, index) => {
|
|
301
303
|
let fileGroup;
|
|
302
304
|
if (index === 0 && group.length > 1) {
|
|
303
305
|
if (SERVICE_CLIENT.get(service)?.CLOUD_UPLOAD_FROMDISK) {
|
|
@@ -309,7 +311,7 @@ class Cloud extends core_1.ClientDb {
|
|
|
309
311
|
const value = group[i];
|
|
310
312
|
try {
|
|
311
313
|
if (fs.existsSync(value)) {
|
|
312
|
-
fileGroup.push([fs.readFileSync(value), path.extname(value), value]);
|
|
314
|
+
fileGroup.push([typeof minStreamSize === 'number' ? await this.streamFile(value, { minStreamSize, cache: false, signal: instance.signal }) : fs.readFileSync(value), path.extname(value), value]);
|
|
313
315
|
}
|
|
314
316
|
}
|
|
315
317
|
catch (err) {
|
|
@@ -336,7 +338,7 @@ class Cloud extends core_1.ClientDb {
|
|
|
336
338
|
else if (upload.overwrite) {
|
|
337
339
|
filename = path.basename(localUri);
|
|
338
340
|
}
|
|
339
|
-
buffer = file.sourceUTF8 ? Buffer.from(file.sourceUTF8, file.encoding) : host.getBuffer(file);
|
|
341
|
+
buffer = file.sourceUTF8 ? Buffer.from(file.sourceUTF8, file.encoding) : typeof minStreamSize === 'number' ? await host.getBuffer(file, minStreamSize).catch(() => host.getBuffer(file)) : host.getBuffer(file);
|
|
340
342
|
}
|
|
341
343
|
else {
|
|
342
344
|
contentType = this.lookupMime(path.basename(localUri)) || file.mimeType;
|
|
@@ -435,7 +437,7 @@ class Cloud extends core_1.ClientDb {
|
|
|
435
437
|
}
|
|
436
438
|
return super.setQueryResult(service, credential, queryString, result, sessionKey);
|
|
437
439
|
}
|
|
438
|
-
createBucket(service, credential, bucket, publicRead, options) {
|
|
440
|
+
async createBucket(service, credential, bucket, publicRead, options) {
|
|
439
441
|
if (this.aborted) {
|
|
440
442
|
return Promise.reject((0, types_1.createAbortError)());
|
|
441
443
|
}
|
|
@@ -465,7 +467,7 @@ class Cloud extends core_1.ClientDb {
|
|
|
465
467
|
return Promise.reject(err);
|
|
466
468
|
}
|
|
467
469
|
}
|
|
468
|
-
setBucketPolicy(service, credential, bucket, options) {
|
|
470
|
+
async setBucketPolicy(service, credential, bucket, options) {
|
|
469
471
|
if (this.aborted) {
|
|
470
472
|
return Promise.reject((0, types_1.createAbortError)());
|
|
471
473
|
}
|
|
@@ -486,7 +488,7 @@ class Cloud extends core_1.ClientDb {
|
|
|
486
488
|
return Promise.reject(err);
|
|
487
489
|
}
|
|
488
490
|
}
|
|
489
|
-
setBucketWebsite(service, credential, bucket, options) {
|
|
491
|
+
async setBucketWebsite(service, credential, bucket, options) {
|
|
490
492
|
if (this.aborted) {
|
|
491
493
|
return Promise.reject((0, types_1.createAbortError)());
|
|
492
494
|
}
|
|
@@ -507,7 +509,7 @@ class Cloud extends core_1.ClientDb {
|
|
|
507
509
|
return Promise.reject(err);
|
|
508
510
|
}
|
|
509
511
|
}
|
|
510
|
-
deleteObjects(service, credential, bucket, recursive = true) {
|
|
512
|
+
async deleteObjects(service, credential, bucket, recursive = true) {
|
|
511
513
|
if (this.aborted) {
|
|
512
514
|
return Promise.reject((0, types_1.createAbortError)());
|
|
513
515
|
}
|
|
@@ -528,7 +530,7 @@ class Cloud extends core_1.ClientDb {
|
|
|
528
530
|
return Promise.reject(err);
|
|
529
531
|
}
|
|
530
532
|
}
|
|
531
|
-
uploadObject(service, credential, bucket, upload, localUri, beforeResolve) {
|
|
533
|
+
async uploadObject(service, credential, bucket, upload, localUri, beforeResolve) {
|
|
532
534
|
if (this.aborted) {
|
|
533
535
|
return Promise.reject((0, types_1.createAbortError)());
|
|
534
536
|
}
|
|
@@ -563,7 +565,7 @@ class Cloud extends core_1.ClientDb {
|
|
|
563
565
|
}
|
|
564
566
|
});
|
|
565
567
|
}
|
|
566
|
-
downloadObject(service, credential, bucket, download, beforeResolve) {
|
|
568
|
+
async downloadObject(service, credential, bucket, download, beforeResolve) {
|
|
567
569
|
if (this.aborted) {
|
|
568
570
|
return Promise.reject((0, types_1.createAbortError)());
|
|
569
571
|
}
|
|
@@ -740,7 +742,7 @@ class Cloud extends core_1.ClientDb {
|
|
|
740
742
|
}
|
|
741
743
|
resolveService(service, folder) {
|
|
742
744
|
let result;
|
|
743
|
-
if (service
|
|
745
|
+
if (!service.startsWith('@')) {
|
|
744
746
|
result = this.settings.imports?.[service] || util_1.IMPORTS[service];
|
|
745
747
|
}
|
|
746
748
|
else if (!folder && !service.startsWith("@squared-functions/" /* NAMESPACE.SQUARED_FUNCTIONS */)) {
|
|
@@ -752,15 +754,15 @@ class Cloud extends core_1.ClientDb {
|
|
|
752
754
|
const result = this.settings[service]?.[name];
|
|
753
755
|
return component ? (0, types_1.isObject)(result) ? result[component] : undefined : result;
|
|
754
756
|
}
|
|
755
|
-
commit() {
|
|
757
|
+
async commit() {
|
|
756
758
|
if (this.aborted) {
|
|
757
759
|
return Promise.reject((0, types_1.createAbortError)());
|
|
758
760
|
}
|
|
759
|
-
const items = this.pending.filter(item => !item.document).map(data => {
|
|
761
|
+
const items = this.pending.filter(item => !item.document).map(async (data) => {
|
|
760
762
|
data.ignoreCache ?? (data.ignoreCache = true);
|
|
761
763
|
return this.getDatabaseRows(data, true);
|
|
762
764
|
});
|
|
763
|
-
return items.length === 0 ?
|
|
765
|
+
return items.length === 0 ? false : this.allSettled(items, ["Execute unassigned queries" /* VAL_DB.EXEC_QUERYUNASSIGNED */, this.moduleName]).then(result => result.length > 0).catch(() => false);
|
|
764
766
|
}
|
|
765
767
|
getClient(service) {
|
|
766
768
|
let client = SERVICE_CLIENT.get(service);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/cloud",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Cloud constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -20,8 +20,9 @@
|
|
|
20
20
|
"license": "BSD 3-Clause",
|
|
21
21
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/core": "0.
|
|
24
|
-
"@e-mc/db": "0.
|
|
25
|
-
"@e-mc/types": "0.
|
|
23
|
+
"@e-mc/core": "0.8.1",
|
|
24
|
+
"@e-mc/db": "0.8.1",
|
|
25
|
+
"@e-mc/types": "0.8.1",
|
|
26
|
+
"mime-types": "^2.1.35"
|
|
26
27
|
}
|
|
27
28
|
}
|
package/util.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { UploadContent } from '../types/lib/cloud';
|
|
2
|
+
|
|
1
3
|
import type { Readable } from 'stream';
|
|
2
4
|
|
|
3
5
|
interface AuthValue {
|
|
@@ -8,6 +10,7 @@ interface AuthValue {
|
|
|
8
10
|
declare namespace util {
|
|
9
11
|
const IMPORTS: Record<string, string | undefined>;
|
|
10
12
|
function readableAsBuffer(stream: Readable): Promise<Buffer | null>;
|
|
13
|
+
function createKeyAndBody(filename: string, items: UploadContent[], mimeType?: string | FunctionType<void>, errorCallback?: FunctionType<void>): [string[], Buffer[], string[]];
|
|
11
14
|
function generateFilename(filename: string): (i: number) => [string, boolean];
|
|
12
15
|
function getBasicAuth(auth: AuthValue): string;
|
|
13
16
|
function getBasicAuth(username: unknown, password?: unknown): string;
|
package/util.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hasBasicAuth = exports.getBasicAuth = exports.formatError = exports.generateFilename = exports.readableAsBuffer = exports.IMPORTS = void 0;
|
|
3
|
+
exports.hasBasicAuth = exports.getBasicAuth = exports.formatError = exports.generateFilename = exports.createKeyAndBody = exports.readableAsBuffer = exports.IMPORTS = void 0;
|
|
4
4
|
const path = require("path");
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
const mime = require("mime-types");
|
|
5
7
|
const types_1 = require("../types");
|
|
6
8
|
const util_1 = require("../db/util");
|
|
7
9
|
Object.defineProperty(exports, "getBasicAuth", { enumerable: true, get: function () { return util_1.getBasicAuth; } });
|
|
@@ -18,7 +20,7 @@ exports.IMPORTS = {
|
|
|
18
20
|
"minio": "@pi-r/minio",
|
|
19
21
|
"oci": "@pi-r/oci"
|
|
20
22
|
};
|
|
21
|
-
function readableAsBuffer(stream) {
|
|
23
|
+
async function readableAsBuffer(stream) {
|
|
22
24
|
return new Promise((resolve, reject) => {
|
|
23
25
|
let result = null;
|
|
24
26
|
stream
|
|
@@ -31,6 +33,45 @@ function readableAsBuffer(stream) {
|
|
|
31
33
|
});
|
|
32
34
|
}
|
|
33
35
|
exports.readableAsBuffer = readableAsBuffer;
|
|
36
|
+
function createKeyAndBody(filename, items, mimeType, errorCallback) {
|
|
37
|
+
if (typeof mimeType === 'function') {
|
|
38
|
+
errorCallback = mimeType;
|
|
39
|
+
mimeType = undefined;
|
|
40
|
+
}
|
|
41
|
+
const key = [];
|
|
42
|
+
const body = [];
|
|
43
|
+
const type = [];
|
|
44
|
+
for (let [content, ext, localFile] of items) {
|
|
45
|
+
try {
|
|
46
|
+
switch (typeof content) {
|
|
47
|
+
case 'string':
|
|
48
|
+
if (fs.existsSync(content)) {
|
|
49
|
+
content = fs.readFileSync(content);
|
|
50
|
+
}
|
|
51
|
+
else if (path.isAbsolute(content)) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
content = Buffer.from(content);
|
|
56
|
+
}
|
|
57
|
+
break;
|
|
58
|
+
case 'object':
|
|
59
|
+
break;
|
|
60
|
+
default:
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const output = filename + ext;
|
|
64
|
+
key.push(ext === '.map' && localFile ? path.basename(localFile) : output);
|
|
65
|
+
body.push(content);
|
|
66
|
+
type.push(ext !== '.map' && mime.lookup(output) || mimeType || 'application/octet-stream');
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
errorCallback?.(err);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return [key, body, type];
|
|
73
|
+
}
|
|
74
|
+
exports.createKeyAndBody = createKeyAndBody;
|
|
34
75
|
function generateFilename(filename) {
|
|
35
76
|
let basename, suffix;
|
|
36
77
|
return (i) => {
|
|
@@ -45,7 +86,7 @@ function generateFilename(filename) {
|
|
|
45
86
|
suffix = filename.substring(j);
|
|
46
87
|
}
|
|
47
88
|
}
|
|
48
|
-
return basename && suffix ? [basename + `_${i}` + suffix, true] : [(0, types_1.
|
|
89
|
+
return basename && suffix ? [basename + `_${i}` + suffix, true] : [(0, types_1.incrementUUID)() + path.extname(filename), false];
|
|
49
90
|
};
|
|
50
91
|
}
|
|
51
92
|
exports.generateFilename = generateFilename;
|