Skip to content

Commit 9abfeef

Browse files
qem19dave
andauthored
add concurrency limit (#4)
Co-authored-by: dave <aleksander.nau@cadolabs.io>
1 parent 87a361f commit 9abfeef

8 files changed

Lines changed: 63 additions & 34 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM node:alpine
22

3-
RUN yarn global add @Cado-Labs/ucdn
3+
RUN yarn global add @cadolabs/ucdn
44

55
ENTRYPOINT ["/usr/local/bin/ucdn"]

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ Tool for uploading assets to AWS S3.
55
## Installation
66

77
```sh
8-
$ yarn global add @Cado-Labs/ucdn
8+
$ yarn global add @cadolabs/ucdn
99
```
1010

1111
or
1212

1313
```sh
14-
$ npm i -g @Cado-Labs/ucdn
14+
$ npm i -g @cadolabs/ucdn
1515
```
1616

1717
## Usage
@@ -29,15 +29,16 @@ All following options except `config` and `config-key` can be configured using t
2929
Available AWS regions can be viewed at https://docs.aws.amazon.com/sns/latest/dg/sns-supported-regions-countries.html.
3030

3131
```
32-
--config, -c path to config file [default: "./ucdn.yml"]
33-
--config-key, -C root config key [default: null]
34-
--region, -r AWS geographical area [default: "eu-west-1"]
35-
--dir, -d assets directory [default: "dist/"]
36-
--bucket, -b AWS bucket for upload [required]
37-
--exclude, -e excluded extenstions [default: ["html","gz"]]
38-
--accessKeyId, --access-key-id AWS access key ID [required]
39-
--secretAccessKey, --secret-access-key AWS secret access key [required]
40-
--targetDir, --target-dir AWS bucket target directory [default: ""]
32+
--config, -c path to config file [default: "./ucdn.yml"]
33+
--config-key, -C root config key [default: null]
34+
--region, -r AWS geographical area [default: "eu-west-1"]
35+
--dir, -d assets directory [default: "dist/"]
36+
--bucket, -b AWS bucket for upload [required]
37+
--exclude, -e excluded extenstions [default: ["html","gz"]]
38+
--accessKeyId, --access-key-id AWS access key ID [required]
39+
--secretAccessKey, --secret-access-key AWS secret access key [required]
40+
--targetDir, --target-dir AWS bucket target directory [default: ""]
41+
--concurrencyLimit, --processes Limit of concurrent upload processes [default: 100]
4142
```
4243

4344
## Contributing

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const upload = require("./upload")
1+
import upload from "./upload.js"
22

3-
module.exports = {
3+
export default {
44
upload,
55
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
{
2-
"name": "@Cado-Labs/ucdn",
3-
"version": "1.0.0",
2+
"name": "@cadolabs/ucdn",
3+
"version": "1.1.0",
44
"bin": "ucdn",
55
"main": "index.js",
66
"repository": "git@github.com:Cado-Labs/ucdn.git",
77
"license": "MIT",
88
"author": "Aleksei Bespalov <nulldefiner@gmail.com>",
99
"homepage": "https://github.com/Cado-Labs/ucdn",
10+
"type": "module",
1011
"scripts": {
1112
"lint": "eslint . ucdn"
1213
},
1314
"dependencies": {
1415
"@aws-sdk/client-s3": "^3.421.0",
1516
"js-yaml": "^4.1.0",
1617
"mime-types": "^2.1.35",
18+
"p-limit": "^6.2.0",
1719
"yargs": "^17.7.2"
1820
},
1921
"devDependencies": {

ucdn

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#!/usr/bin/env node
22

3-
const yargs = require("yargs")
3+
import yargs from "yargs"
4+
import { hideBin } from 'yargs/helpers'
45

5-
const pkg = require("./package")
6-
const utils = require("./utils")
7-
const upload = require("./upload")
6+
import pkg from "./package.json" with { type: 'json' }
7+
import utils from "./utils.js"
8+
import upload from "./upload.js"
89

910
const yargsOptions = {
1011
config: {
@@ -59,9 +60,15 @@ const yargsOptions = {
5960
type: "string",
6061
default: "",
6162
},
63+
concurrencyLimit: {
64+
alias: "processes",
65+
describe: "Limit of concurrent upload processes",
66+
type: "integer",
67+
default: 100,
68+
},
6269
}
6370

64-
yargs
71+
yargs(hideBin(process.argv))
6572
.env("UCDN")
6673
.command("upload", "upload assets to AWS S3", {}, upload)
6774
.options(yargsOptions)
@@ -71,6 +78,7 @@ yargs
7178
.strict()
7279
.middleware(argv => {
7380
const config = utils.loadYamlFile(argv.config)
81+
7482
const configObject = argv.C ? (config[argv.C] || {}) : config
7583

7684
Object

upload.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const fs = require("fs")
2-
const path = require("path")
3-
const mime = require("mime-types")
4-
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3")
1+
import fs from "fs"
2+
import path from "path"
3+
import mime from "mime-types"
4+
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3"
5+
import pLimit from "p-limit"
56

67
const configDefaults = {
78
region: null,
@@ -11,6 +12,7 @@ const configDefaults = {
1112
accessKeyId: null,
1213
secretAccessKey: null,
1314
targetDir: null,
15+
concurrencyLimit: 100,
1416
}
1517

1618
const getConfig = passedConfig => {
@@ -33,14 +35,16 @@ const getKey = (file, root, targetDir) => {
3335
return path.join(targetDir, filepath)
3436
}
3537

36-
const uploadFile = (s3, config, file, key) => {
37-
return fs.promises.readFile(file).then(data => {
38+
const uploadFileFunc = (s3, config, file, key) => {
39+
return () => {
40+
const data = fs.readFileSync(file)
3841
const { bucket } = config
3942
const basename = path.basename(file)
4043
const contentType = mime.contentType(basename)
4144
const params = { Bucket: bucket, Body: data, Key: key, ContentType: contentType }
45+
4246
return s3.send(new PutObjectCommand(params))
43-
})
47+
}
4448
}
4549

4650
const upload = argv => {
@@ -61,9 +65,11 @@ const upload = argv => {
6165
return !exclude.includes(ext)
6266
})
6367

68+
const limit = pLimit(config.concurrencyLimit);
69+
6470
const promises = files.map(file => {
6571
const key = getKey(file, directory, targetDir)
66-
return uploadFile(s3, config, file, key)
72+
return limit(uploadFileFunc(s3, config, file, key))
6773
.then(() => console.log("Uploaded", file))
6874
})
6975

@@ -78,4 +84,4 @@ const upload = argv => {
7884
})
7985
}
8086

81-
module.exports = upload
87+
export default upload

utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const path = require("path")
2-
const fs = require("fs")
3-
const yaml = require("js-yaml")
1+
import path from "path"
2+
import fs from "fs"
3+
import yaml from "js-yaml"
44

55
const loadYamlFile = filePath => {
66
const configPath = path.resolve(filePath)
77
const content = fs.readFileSync(configPath)
88
return yaml.load(content)
99
}
1010

11-
module.exports = {
11+
export default {
1212
loadYamlFile,
1313
}

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,6 +2571,13 @@ p-limit@^3.0.2:
25712571
dependencies:
25722572
yocto-queue "^0.1.0"
25732573

2574+
p-limit@^6.2.0:
2575+
version "6.2.0"
2576+
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-6.2.0.tgz#c254d22ba6aeef441a3564c5e6c2f2da59268a0f"
2577+
integrity sha512-kuUqqHNUqoIWp/c467RI4X6mmyuojY5jGutNU0wVTmEOOfcuwLqyMVoAi9MKi2Ak+5i9+nhmrK4ufZE8069kHA==
2578+
dependencies:
2579+
yocto-queue "^1.1.1"
2580+
25742581
p-locate@^5.0.0:
25752582
version "5.0.0"
25762583
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-5.0.0.tgz#83c8315c6785005e3bd021839411c9e110e6d834"
@@ -3033,3 +3040,8 @@ yocto-queue@^0.1.0:
30333040
version "0.1.0"
30343041
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
30353042
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==
3043+
3044+
yocto-queue@^1.1.1:
3045+
version "1.2.1"
3046+
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.2.1.tgz#36d7c4739f775b3cbc28e6136e21aa057adec418"
3047+
integrity sha512-AyeEbWOu/TAXdxlV9wmGcR0+yh2j3vYPGOECcIj2S7MkrLyC7ne+oye2BKTItt0ii2PHk4cDy+95+LshzbXnGg==

0 commit comments

Comments
 (0)