d-drive-cli 1.1.2 → 1.2.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.
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.uploadCommand = uploadCommand;
7
7
  const fs_extra_1 = __importDefault(require("fs-extra"));
8
8
  const path_1 = __importDefault(require("path"));
9
+ const stream_1 = require("stream");
9
10
  const chalk_1 = __importDefault(require("chalk"));
10
11
  const ora_1 = __importDefault(require("ora"));
11
12
  const form_data_1 = __importDefault(require("form-data"));
@@ -61,24 +62,56 @@ async function uploadSingleFile(api, filePath, destination, showProgress) {
61
62
  console.log(chalk_1.default.cyan(`\nUploading: ${fileName}`));
62
63
  console.log(chalk_1.default.gray(`Size: ${formatFileSize(fileSize)}`));
63
64
  const formData = new form_data_1.default();
64
- formData.append('file', fs_extra_1.default.createReadStream(filePath));
65
+ // Create a passthrough so we can monitor bytes read from disk
66
+ const fileStream = fs_extra_1.default.createReadStream(filePath);
67
+ const pass = new stream_1.PassThrough();
68
+ // Pipe file stream into pass-through which is appended to form-data
69
+ fileStream.pipe(pass);
70
+ formData.append('file', pass, {
71
+ filename: fileName,
72
+ knownLength: fileSize,
73
+ });
65
74
  formData.append('path', destination);
75
+ // Ensure CLI uploads follow frontend behavior and request server-side encryption by default
76
+ formData.append('encrypt', 'true');
66
77
  let progressBar = null;
67
78
  if (showProgress) {
68
79
  progressBar = new progress_1.default('[:bar] :percent :etas', {
69
80
  complete: '█',
70
81
  incomplete: '░',
71
82
  width: 40,
72
- total: fileSize,
83
+ total: 1,
84
+ });
85
+ // Track bytes read from disk and update progress bar as fraction [0..1]
86
+ let uploaded = 0;
87
+ fileStream.on('data', (chunk) => {
88
+ const len = typeof chunk === 'string' ? Buffer.byteLength(chunk) : chunk.length;
89
+ uploaded += len;
90
+ if (progressBar) {
91
+ const ratio = Math.min(1, uploaded / fileSize);
92
+ progressBar.update(ratio);
93
+ }
73
94
  });
74
95
  }
96
+ // Ensure Content-Length is set for axios in Node
97
+ const headers = formData.getHeaders();
98
+ try {
99
+ const length = await new Promise((resolve, reject) => {
100
+ formData.getLength((err, len) => {
101
+ if (err)
102
+ return reject(err);
103
+ resolve(len);
104
+ });
105
+ });
106
+ headers['Content-Length'] = String(length);
107
+ }
108
+ catch (err) {
109
+ // ignore length error
110
+ }
75
111
  await api.post('/files/upload', formData, {
76
- headers: formData.getHeaders(),
77
- onUploadProgress: (progressEvent) => {
78
- if (progressBar && progressEvent.loaded) {
79
- progressBar.update(progressEvent.loaded / fileSize);
80
- }
81
- },
112
+ headers,
113
+ maxContentLength: Infinity,
114
+ maxBodyLength: Infinity,
82
115
  });
83
116
  }
84
117
  function formatFileSize(bytes) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "d-drive-cli",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "D-Drive CLI tool for developers",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
@@ -1,5 +1,6 @@
1
1
  import fs from 'fs-extra';
2
2
  import path from 'path';
3
+ import { PassThrough } from 'stream';
3
4
  import chalk from 'chalk';
4
5
  import ora from 'ora';
5
6
  import FormData from 'form-data';
@@ -85,8 +86,18 @@ async function uploadSingleFile(
85
86
  console.log(chalk.gray(`Size: ${formatFileSize(fileSize)}`));
86
87
 
87
88
  const formData = new FormData();
88
- formData.append('file', fs.createReadStream(filePath));
89
+ // Create a passthrough so we can monitor bytes read from disk
90
+ const fileStream = fs.createReadStream(filePath);
91
+ const pass = new PassThrough();
92
+ // Pipe file stream into pass-through which is appended to form-data
93
+ fileStream.pipe(pass);
94
+ formData.append('file', pass, {
95
+ filename: fileName,
96
+ knownLength: fileSize,
97
+ });
89
98
  formData.append('path', destination);
99
+ // Ensure CLI uploads follow frontend behavior and request server-side encryption by default
100
+ formData.append('encrypt', 'true');
90
101
 
91
102
  let progressBar: ProgressBar | null = null;
92
103
 
@@ -95,17 +106,39 @@ async function uploadSingleFile(
95
106
  complete: '█',
96
107
  incomplete: '░',
97
108
  width: 40,
98
- total: fileSize,
109
+ total: 1,
110
+ });
111
+
112
+ // Track bytes read from disk and update progress bar as fraction [0..1]
113
+ let uploaded = 0;
114
+ fileStream.on('data', (chunk: Buffer | string) => {
115
+ const len = typeof chunk === 'string' ? Buffer.byteLength(chunk) : chunk.length;
116
+ uploaded += len;
117
+ if (progressBar) {
118
+ const ratio = Math.min(1, uploaded / fileSize);
119
+ progressBar.update(ratio);
120
+ }
121
+ });
122
+ }
123
+
124
+ // Ensure Content-Length is set for axios in Node
125
+ const headers = formData.getHeaders();
126
+ try {
127
+ const length = await new Promise<number>((resolve, reject) => {
128
+ formData.getLength((err: any, len: number) => {
129
+ if (err) return reject(err);
130
+ resolve(len);
131
+ });
99
132
  });
133
+ headers['Content-Length'] = String(length);
134
+ } catch (err) {
135
+ // ignore length error
100
136
  }
101
137
 
102
138
  await api.post('/files/upload', formData, {
103
- headers: formData.getHeaders(),
104
- onUploadProgress: (progressEvent: any) => {
105
- if (progressBar && progressEvent.loaded) {
106
- progressBar.update(progressEvent.loaded / fileSize);
107
- }
108
- },
139
+ headers,
140
+ maxContentLength: Infinity,
141
+ maxBodyLength: Infinity,
109
142
  });
110
143
  }
111
144