Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit 1d70904

Browse files
committed
limit simultaneous file uploads
1 parent 908bfd3 commit 1d70904

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

lib/deploy.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
var when = require("when"),
2-
nodefn = require("when/node"),
3-
model = require("./model");
1+
var when = require("when"),
2+
nodefn = require("when/node"),
3+
semaphore = require("semaphore"),
4+
model = require("./model");
45

56
if (typeof(require) !== 'undefined') {
67
var fs = require("graceful-fs");
@@ -42,24 +43,29 @@ Deploy.prototype = {
4243
progress && progress("start", {total: files.length});
4344

4445
var self = this;
46+
var sem = semaphore(20);
4547
var results = files.map(function(file) {
46-
return nodefn.call(fs.readFile, file.abs).then(function(data) {
47-
var filePath = file.rel.split("/").map(function(segment) {
48-
return encodeURIComponent(segment);
49-
}).join("/");
48+
sem.take(function() {
49+
return nodefn.call(fs.readFile, file.abs).then(function(data) {
50+
var filePath = file.rel.split("/").map(function(segment) {
51+
return encodeURIComponent(segment);
52+
}).join("/");
5053

51-
return self.client.request({
52-
url: "/deploys/" + self.id + "/files/" + filePath,
53-
type: "put",
54-
body: data,
55-
contentType: "application/octet-stream",
56-
ignoreResponse: true
57-
}).then(function(response) {
58-
progress && progress("upload", {file: file, total: files.length});
59-
return file;
60-
}).catch(function(response) {
61-
progress && progress("uploadError", {file:file, message: response.data});
62-
return when.reject(response.data);
54+
return self.client.request({
55+
url: "/deploys/" + self.id + "/files/" + filePath,
56+
type: "put",
57+
body: data,
58+
contentType: "application/octet-stream",
59+
ignoreResponse: true
60+
}).then(function(response) {
61+
progress && progress("upload", {file: file, total: files.length});
62+
sem.leave();
63+
return file;
64+
}).catch(function(response) {
65+
progress && progress("uploadError", {file:file, message: response.data});
66+
sem.leave();
67+
return when.reject(response.data);
68+
});
6369
});
6470
});
6571
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Netlify",
33
"name": "netlify",
44
"description": "Netlify API client",
5-
"version": "1.0.0",
5+
"version": "1.0.1",
66
"bugs": {
77
"url": "https://github.com/netlify/netlify-js/issues"
88
},
@@ -16,6 +16,7 @@
1616
"base64-js": ">=0.0.4",
1717
"glob": ">=3.2.6",
1818
"graceful-fs": "^3.0.4",
19+
"semaphore": "^1.0.5",
1920
"when": "^3.7.5"
2021
},
2122
"devDependencies": {

0 commit comments

Comments
 (0)