Skip to content

Commit 25d7441

Browse files
committed
feat:concurrency args support
1 parent ff8c451 commit 25d7441

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

bin/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const ProgressBar = require('progress');
1111
const { CHUNK_SIZE } = require('../lib/constants');
1212
const { generateAuthorization, getRegistryInfo } = require('../lib/utils');
1313
const { getExistChunks: _getExistChunks, uploadChunk: _uploadChunk, uploadSuccess: _uploadSuccess } = require('../lib/request');
14+
const BlueBirdPromise = require("bluebird");
1415

1516
const argv = require('../lib/argv');
1617
const { requestUrl, version } = getRegistryInfo(argv.registry);
@@ -73,12 +74,15 @@ const upload = async (filePath, parts = []) => {
7374
logger.info('开始上传')
7475

7576
try {
76-
for (let currentChunkIndex = 1; currentChunkIndex <= totalChunk; currentChunkIndex++) {
77+
78+
const chunkIndexs = new Array(totalChunk).fill("").map((_,index) => index+1)
79+
80+
await BlueBirdPromise.map(chunkIndexs,(currentChunkIndex)=>{
7781
const start = (currentChunkIndex - 1) * CHUNK_SIZE;
7882
const end = ((start + CHUNK_SIZE) >= fileSize) ? fileSize : start + CHUNK_SIZE - 1;
7983
const stream = fs.createReadStream(filePath, { start, end })
8084
let buf = [];
81-
await new Promise((resolve) => {
85+
return new Promise((resolve) => {
8286
stream.on('data', data => {
8387
buf.push(data)
8488
})
@@ -93,7 +97,8 @@ const upload = async (filePath, parts = []) => {
9397
}).catch(error => {
9498
throw Error(error)
9599
})
96-
}
100+
}, { concurrency: argv.concurrency })
101+
97102
} catch (error) {
98103
logger.error(error.message);
99104
logger.error(error.stack);

lib/argv.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ const argv = require('yargs')
1515
alias: 'r',
1616
describe: '仓库路径',
1717
demandOption: true
18+
},
19+
concurrency: {
20+
alias: 'c',
21+
describe: '上传分块并行数',
22+
demandOption: true,
23+
default: 5,
1824
}
1925
})
2026
.alias('version', 'v')

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"license": "ISC",
1212
"dependencies": {
1313
"axios": "^0.20.0",
14+
"bluebird": "^3.7.2",
1415
"chalk": "^4.1.0",
1516
"cos-nodejs-sdk-v5": "^2.8.2",
1617
"form-data": "^3.0.0",

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ bcrypt-pbkdf@^1.0.0:
8484
dependencies:
8585
tweetnacl "^0.14.3"
8686

87+
bluebird@^3.7.2:
88+
version "3.7.2"
89+
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f"
90+
integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==
91+
8792
caseless@~0.12.0:
8893
version "0.12.0"
8994
resolved "https://registry.npm.taobao.org/caseless/download/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"

0 commit comments

Comments
 (0)