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

Commit

Permalink
limit simultaneous file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
biilmann committed Aug 28, 2016
1 parent 908bfd3 commit 1d70904
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
44 changes: 25 additions & 19 deletions lib/deploy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var when = require("when"),
nodefn = require("when/node"),
model = require("./model");
var when = require("when"),
nodefn = require("when/node"),
semaphore = require("semaphore"),
model = require("./model");

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

var self = this;
var sem = semaphore(20);
var results = files.map(function(file) {
return nodefn.call(fs.readFile, file.abs).then(function(data) {
var filePath = file.rel.split("/").map(function(segment) {
return encodeURIComponent(segment);
}).join("/");
sem.take(function() {
return nodefn.call(fs.readFile, file.abs).then(function(data) {
var filePath = file.rel.split("/").map(function(segment) {
return encodeURIComponent(segment);
}).join("/");

return self.client.request({
url: "/deploys/" + self.id + "/files/" + filePath,
type: "put",
body: data,
contentType: "application/octet-stream",
ignoreResponse: true
}).then(function(response) {
progress && progress("upload", {file: file, total: files.length});
return file;
}).catch(function(response) {
progress && progress("uploadError", {file:file, message: response.data});
return when.reject(response.data);
return self.client.request({
url: "/deploys/" + self.id + "/files/" + filePath,
type: "put",
body: data,
contentType: "application/octet-stream",
ignoreResponse: true
}).then(function(response) {
progress && progress("upload", {file: file, total: files.length});
sem.leave();
return file;
}).catch(function(response) {
progress && progress("uploadError", {file:file, message: response.data});
sem.leave();
return when.reject(response.data);
});
});
});
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Netlify",
"name": "netlify",
"description": "Netlify API client",
"version": "1.0.0",
"version": "1.0.1",
"bugs": {
"url": "https://github.com/netlify/netlify-js/issues"
},
Expand All @@ -16,6 +16,7 @@
"base64-js": ">=0.0.4",
"glob": ">=3.2.6",
"graceful-fs": "^3.0.4",
"semaphore": "^1.0.5",
"when": "^3.7.5"
},
"devDependencies": {
Expand Down

0 comments on commit 1d70904

Please sign in to comment.