@rowengine/common 1.1.7 → 1.1.9

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.
@@ -1,13 +1,3 @@
1
- interface UploadFileToGCS {
2
- filePath: string;
3
- gcsFilePath: string;
4
- }
5
- interface UploadFileToGCSResponse {
6
- filename: string;
7
- size: number;
8
- }
9
- export declare const uploadFileToGCS: ({ filePath, gcsFilePath, }: UploadFileToGCS) => Promise<UploadFileToGCSResponse>;
10
- export declare const uploadFolderToGCS: (folderPath: string, gcsPath: string) => Promise<void>;
1
+ export declare function uploadFolderToGCS(folderPath: string, gcsPath: string): Promise<void>;
11
2
  export declare const getFilesFromGCSFolder: (gcsFolderPath: string) => Promise<string[]>;
12
3
  export declare const downloadProjectFileFromGCS: (projectId: string, fileName: string, destinationPath: string) => Promise<string | null>;
13
- export {};
package/build/utils/gc.js CHANGED
@@ -12,54 +12,58 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.downloadProjectFileFromGCS = exports.getFilesFromGCSFolder = exports.uploadFolderToGCS = exports.uploadFileToGCS = void 0;
16
- const gc_storage_1 = __importDefault(require("./gc-storage"));
17
- const path_1 = __importDefault(require("path"));
15
+ exports.downloadProjectFileFromGCS = exports.getFilesFromGCSFolder = void 0;
16
+ exports.uploadFolderToGCS = uploadFolderToGCS;
17
+ const promises_1 = __importDefault(require("fs/promises"));
18
18
  const fs_1 = __importDefault(require("fs"));
19
+ const path_1 = __importDefault(require("path"));
19
20
  const p_limit_1 = __importDefault(require("p-limit"));
20
- const uploadFileToGCS = ({ filePath, gcsFilePath, }) => {
21
- return new Promise((resolve, reject) => {
22
- if (!fs_1.default.existsSync(filePath))
23
- throw new Error('File not found');
24
- const { size } = fs_1.default.statSync(filePath);
25
- const blob = gc_storage_1.default.file(gcsFilePath);
26
- const blobStream = blob.createWriteStream({
27
- resumable: false,
28
- });
29
- const readStream = fs_1.default.createReadStream(filePath);
30
- readStream.pipe(blobStream);
31
- blobStream.on('error', (err) => {
32
- reject(err);
33
- });
34
- blobStream.on('finish', () => {
35
- resolve({
36
- filename: path_1.default.basename(filePath),
37
- size: size / 1024 / 1024,
38
- });
39
- });
21
+ const promises_2 = require("stream/promises");
22
+ const gc_storage_1 = __importDefault(require("./gc-storage"));
23
+ // Tune this based on VM size
24
+ const UPLOAD_CONCURRENCY = 24;
25
+ // ---- 1️⃣ Walk directory once (no recursion during upload)
26
+ function walkDir(dir_1) {
27
+ return __awaiter(this, arguments, void 0, function* (dir, baseDir = dir) {
28
+ const entries = yield promises_1.default.readdir(dir, { withFileTypes: true });
29
+ const results = yield Promise.all(entries.map((entry) => __awaiter(this, void 0, void 0, function* () {
30
+ const abs = path_1.default.join(dir, entry.name);
31
+ if (entry.isDirectory()) {
32
+ return walkDir(abs, baseDir);
33
+ }
34
+ const stat = yield promises_1.default.stat(abs);
35
+ return [
36
+ {
37
+ relativePath: path_1.default.relative(baseDir, abs),
38
+ absolutePath: abs,
39
+ size: stat.size,
40
+ },
41
+ ];
42
+ })));
43
+ return results.flat();
40
44
  });
41
- };
42
- exports.uploadFileToGCS = uploadFileToGCS;
43
- const uploadFolderToGCS = (folderPath, gcsPath) => __awaiter(void 0, void 0, void 0, function* () {
44
- const limit = (0, p_limit_1.default)(8);
45
- const files = fs_1.default.readdirSync(folderPath);
46
- let promises = [];
47
- for (const file of files) {
48
- // If file is directory, we need to upload it recursively.
49
- if (fs_1.default.lstatSync(path_1.default.join(folderPath, file)).isDirectory()) {
50
- yield (0, exports.uploadFolderToGCS)(path_1.default.join(folderPath, file), path_1.default.join(gcsPath, file));
51
- }
52
- else {
53
- const filePath = path_1.default.join(folderPath, file);
54
- const gcsFilePath = path_1.default.join(gcsPath, file);
55
- promises.push(limit(() => {
56
- return (0, exports.uploadFileToGCS)({ filePath, gcsFilePath });
57
- }));
58
- }
59
- }
60
- const res = yield Promise.all(promises);
61
- });
62
- exports.uploadFolderToGCS = uploadFolderToGCS;
45
+ }
46
+ function uploadFileToGCS(absolutePath, gcsPath, size) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ const file = gc_storage_1.default.file(gcsPath);
49
+ const resumable = size > 5 * 1024 * 1024; // 5MB threshold
50
+ yield (0, promises_2.pipeline)(fs_1.default.createReadStream(absolutePath), file.createWriteStream({
51
+ resumable,
52
+ validation: false,
53
+ gzip: false,
54
+ metadata: {
55
+ cacheControl: 'public, max-age=31536000',
56
+ },
57
+ }));
58
+ });
59
+ }
60
+ function uploadFolderToGCS(folderPath, gcsPath) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ const limit = (0, p_limit_1.default)(UPLOAD_CONCURRENCY);
63
+ const files = yield walkDir(folderPath);
64
+ yield Promise.all(files.map(({ relativePath, absolutePath, size }) => limit(() => uploadFileToGCS(absolutePath, path_1.default.posix.join(gcsPath, relativePath), size))));
65
+ });
66
+ }
63
67
  const getFilesFromGCSFolder = (gcsFolderPath) => __awaiter(void 0, void 0, void 0, function* () {
64
68
  const options = {
65
69
  prefix: gcsFolderPath.endsWith('/')
@@ -1,2 +1,3 @@
1
1
  export * from './grades';
2
2
  export * from './gc';
3
+ export { default as gcStorage } from './gc-storage';
@@ -13,6 +13,12 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
13
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
16
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.gcStorage = void 0;
17
21
  __exportStar(require("./grades"), exports);
18
22
  __exportStar(require("./gc"), exports);
23
+ var gc_storage_1 = require("./gc-storage");
24
+ Object.defineProperty(exports, "gcStorage", { enumerable: true, get: function () { return __importDefault(gc_storage_1).default; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rowengine/common",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "private": false,