@e-mc/cloud 0.8.0 → 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/LICENSE CHANGED
@@ -1,11 +1,11 @@
1
- Copyright 2023 An Pham
2
-
3
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
-
5
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
-
7
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
-
9
- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
-
1
+ Copyright 2023 An Pham
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
11
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import type { CloudConstructor, IFileManager } from '../types/lib';
2
- import type { CloudAsset } from '../types/lib/cloud';
3
-
4
- declare const Cloud: CloudConstructor<IFileManager<CloudAsset>>;
5
-
1
+ import type { CloudConstructor, IFileManager } from '../types/lib';
2
+ import type { CloudAsset } from '../types/lib/cloud';
3
+
4
+ declare const Cloud: CloudConstructor<IFileManager<CloudAsset>>;
5
+
6
6
  export = Cloud;
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
- else {
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);
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.writeFileSync(destUri, value);
235
- this.addDownload(size = value.length);
229
+ fs.copyFileSync(value, destUri);
236
230
  }
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);
231
+ size = this.addDownload(destUri);
240
232
  }
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 });
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 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
- downloadMap[location] = pending;
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 (ignoreProcess || !(0, types_1.isString)(value)) {
288
- return;
289
- }
290
- for (const { instance: document } of host.Document) {
291
- if (document.cloudUpload && await document.cloudUpload(state, file, value, active)) {
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/cloud",
3
- "version": "0.8.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.8.0",
24
- "@e-mc/db": "0.8.0",
25
- "@e-mc/types": "0.8.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; } });
@@ -31,6 +33,45 @@ async 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) => {