forked from lisong/code-push-server
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: replace deprecated use of Buffer()
- Loading branch information
Showing
9 changed files
with
1,109 additions
and
917 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,78 @@ | ||
// 计算文件的eTag,参数为buffer或者readableStream或者文件路径 | ||
function getEtag(buffer,callback){ | ||
function getEtag(buffer, callback) { | ||
// 判断传入的参数是buffer还是stream还是filepath | ||
var mode = "buffer"; | ||
|
||
// 判断传入的参数是buffer还是stream还是filepath | ||
var mode = 'buffer'; | ||
if (typeof buffer === "string") { | ||
buffer = require("fs").createReadStream(buffer); | ||
mode = "stream"; | ||
} else if (buffer instanceof require("stream")) { | ||
mode = "stream"; | ||
} | ||
|
||
if(typeof buffer === 'string'){ | ||
buffer = require('fs').createReadStream(buffer); | ||
mode='stream'; | ||
}else if(buffer instanceof require('stream')){ | ||
mode='stream'; | ||
} | ||
// sha1算法 | ||
var sha1 = function (content) { | ||
var crypto = require("crypto"); | ||
var sha1 = crypto.createHash("sha1"); | ||
sha1.update(content); | ||
return sha1.digest(); | ||
}; | ||
|
||
// sha1算法 | ||
var sha1 = function(content){ | ||
var crypto = require('crypto'); | ||
var sha1 = crypto.createHash('sha1'); | ||
sha1.update(content); | ||
return sha1.digest(); | ||
}; | ||
// 以4M为单位分割 | ||
var blockSize = 4 * 1024 * 1024; | ||
var sha1String = []; | ||
var prefix = 0x16; | ||
var blockCount = 0; | ||
|
||
// 以4M为单位分割 | ||
var blockSize = 4*1024*1024; | ||
var sha1String = []; | ||
var prefix = 0x16; | ||
var blockCount = 0; | ||
switch (mode) { | ||
case "buffer": | ||
var bufferSize = buffer.length; | ||
blockCount = Math.ceil(bufferSize / blockSize); | ||
|
||
switch(mode){ | ||
case 'buffer': | ||
var bufferSize = buffer.length; | ||
blockCount = Math.ceil(bufferSize / blockSize); | ||
for (var i = 0; i < blockCount; i++) { | ||
sha1String.push(sha1(buffer.slice(i * blockSize, (i + 1) * blockSize))); | ||
} | ||
process.nextTick(function () { | ||
callback(calcEtag()); | ||
}); | ||
break; | ||
case "stream": | ||
var stream = buffer; | ||
stream.on("readable", function () { | ||
var chunk; | ||
while ((chunk = stream.read(blockSize))) { | ||
sha1String.push(sha1(chunk)); | ||
blockCount++; | ||
} | ||
}); | ||
stream.on("end", function () { | ||
callback(calcEtag()); | ||
}); | ||
break; | ||
} | ||
|
||
for(var i=0;i<blockCount;i++){ | ||
sha1String.push(sha1(buffer.slice(i*blockSize,(i+1)*blockSize))); | ||
} | ||
process.nextTick(function(){ | ||
callback(calcEtag()); | ||
}); | ||
break; | ||
case 'stream': | ||
var stream = buffer; | ||
stream.on('readable', function() { | ||
var chunk; | ||
while (chunk = stream.read(blockSize)) { | ||
sha1String.push(sha1(chunk)); | ||
blockCount++; | ||
} | ||
}); | ||
stream.on('end',function(){ | ||
callback(calcEtag()); | ||
}); | ||
break; | ||
} | ||
function calcEtag() { | ||
if (!sha1String.length) { | ||
return "Fto5o-5ea0sNMlW_75VgGJCv2AcJ"; | ||
} | ||
var sha1Buffer = Buffer.concat(sha1String, blockCount * 20); | ||
|
||
function calcEtag(){ | ||
if(!sha1String.length){ | ||
return 'Fto5o-5ea0sNMlW_75VgGJCv2AcJ'; | ||
} | ||
var sha1Buffer = Buffer.concat(sha1String,blockCount * 20); | ||
// 如果大于4M,则对各个块的sha1结果再次sha1 | ||
if (blockCount > 1) { | ||
prefix = 0x96; | ||
sha1Buffer = sha1(sha1Buffer); | ||
} | ||
|
||
// 如果大于4M,则对各个块的sha1结果再次sha1 | ||
if(blockCount > 1){ | ||
prefix = 0x96; | ||
sha1Buffer = sha1(sha1Buffer); | ||
} | ||
|
||
sha1Buffer = Buffer.concat( | ||
[new Buffer([prefix]),sha1Buffer], | ||
sha1Buffer.length + 1 | ||
); | ||
|
||
return sha1Buffer.toString('base64') | ||
.replace(/\//g,'_').replace(/\+/g,'-'); | ||
|
||
} | ||
sha1Buffer = Buffer.concat( | ||
[Buffer.from([prefix]), sha1Buffer], | ||
sha1Buffer.length + 1 | ||
); | ||
|
||
return sha1Buffer | ||
.toString("base64") | ||
.replace(/\//g, "_") | ||
.replace(/\+/g, "-"); | ||
} | ||
} | ||
|
||
module.exports = getEtag; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,121 @@ | ||
var app = require('../../../app'); | ||
var request = require('supertest')(app); | ||
var app = require("../../../app"); | ||
var request = require("supertest")(app); | ||
var should = require("should"); | ||
var security = require('../../../core/utils/security'); | ||
var factory = require('../../../core/utils/factory'); | ||
var _ = require('lodash'); | ||
var security = require("../../../core/utils/security"); | ||
var factory = require("../../../core/utils/factory"); | ||
var _ = require("lodash"); | ||
|
||
describe('api/accessKeys/accessKeys.test.js', function() { | ||
var account = '522539441@qq.com'; | ||
var password = '123456'; | ||
describe("api/accessKeys/accessKeys.test.js", function () { | ||
var account = "522539441@qq.com"; | ||
var password = "123456"; | ||
var authToken; | ||
var friendlyName = 'test'; | ||
var newFriendlyName = 'newtest'; | ||
before(function(done){ | ||
request.post('/auth/login') | ||
.send({ | ||
account: account, | ||
password: password | ||
}) | ||
.end(function(err, res) { | ||
should.not.exist(err); | ||
var rs = JSON.parse(res.text); | ||
rs.should.containEql({status:"OK"}); | ||
authToken = (new Buffer(`auth:${_.get(rs, 'results.tokens')}`)).toString('base64'); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('create accessKeys', function(done) { | ||
it('should create accessKeys successful', function(done) { | ||
request.post(`/accessKeys`) | ||
.set('Authorization', `Basic ${authToken}`) | ||
.send({createdBy: 'tablee', friendlyName: friendlyName, ttl: 30*24*60*60}) | ||
.end(function(err, res) { | ||
var friendlyName = "test"; | ||
var newFriendlyName = "newtest"; | ||
before(function (done) { | ||
request | ||
.post("/auth/login") | ||
.send({ | ||
account: account, | ||
password: password, | ||
}) | ||
.end(function (err, res) { | ||
should.not.exist(err); | ||
res.status.should.equal(200); | ||
var rs = JSON.parse(res.text); | ||
rs.should.have.properties('accessKey'); | ||
rs.accessKey.should.have.properties(['name', 'createdTime', 'createdBy', | ||
'expires', 'description', 'friendlyName']); | ||
rs.should.containEql({ status: "OK" }); | ||
authToken = Buffer.from(`auth:${_.get(rs, "results.tokens")}`).toString( | ||
"base64" | ||
); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe("create accessKeys", function (done) { | ||
it("should create accessKeys successful", function (done) { | ||
request | ||
.post(`/accessKeys`) | ||
.set("Authorization", `Basic ${authToken}`) | ||
.send({ | ||
createdBy: "tablee", | ||
friendlyName: friendlyName, | ||
ttl: 30 * 24 * 60 * 60, | ||
}) | ||
.end(function (err, res) { | ||
should.not.exist(err); | ||
res.status.should.equal(200); | ||
var rs = JSON.parse(res.text); | ||
rs.should.have.properties("accessKey"); | ||
rs.accessKey.should.have.properties([ | ||
"name", | ||
"createdTime", | ||
"createdBy", | ||
"expires", | ||
"description", | ||
"friendlyName", | ||
]); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should not create accessKeys successful when friendlyName exist', function(done) { | ||
request.post(`/accessKeys`) | ||
.set('Authorization', `Basic ${authToken}`) | ||
.send({createdBy: 'tablee', friendlyName: friendlyName, ttl: 30*24*60*60}) | ||
.end(function(err, res) { | ||
should.not.exist(err); | ||
res.status.should.equal(406); | ||
res.text.should.equal(`The access key "${friendlyName}" already exists.`); | ||
done(); | ||
}); | ||
it("should not create accessKeys successful when friendlyName exist", function (done) { | ||
request | ||
.post(`/accessKeys`) | ||
.set("Authorization", `Basic ${authToken}`) | ||
.send({ | ||
createdBy: "tablee", | ||
friendlyName: friendlyName, | ||
ttl: 30 * 24 * 60 * 60, | ||
}) | ||
.end(function (err, res) { | ||
should.not.exist(err); | ||
res.status.should.equal(406); | ||
res.text.should.equal( | ||
`The access key "${friendlyName}" already exists.` | ||
); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('list accessKeys', function(done) { | ||
it('should list accessKeys successful', function(done) { | ||
request.get(`/accessKeys`) | ||
.set('Authorization', `Basic ${authToken}`) | ||
.send() | ||
.end(function(err, res) { | ||
should.not.exist(err); | ||
res.status.should.equal(200); | ||
var rs = JSON.parse(res.text); | ||
rs.should.have.properties('accessKeys'); | ||
rs.accessKeys.should.be.an.instanceOf(Array); | ||
rs.accessKeys.should.matchEach(function(it) { | ||
return it.should.have.properties(['name', 'createdTime', 'createdBy', | ||
'expires', 'description', 'friendlyName']); | ||
describe("list accessKeys", function (done) { | ||
it("should list accessKeys successful", function (done) { | ||
request | ||
.get(`/accessKeys`) | ||
.set("Authorization", `Basic ${authToken}`) | ||
.send() | ||
.end(function (err, res) { | ||
should.not.exist(err); | ||
res.status.should.equal(200); | ||
var rs = JSON.parse(res.text); | ||
rs.should.have.properties("accessKeys"); | ||
rs.accessKeys.should.be.an.instanceOf(Array); | ||
rs.accessKeys.should.matchEach(function (it) { | ||
return it.should.have.properties([ | ||
"name", | ||
"createdTime", | ||
"createdBy", | ||
"expires", | ||
"description", | ||
"friendlyName", | ||
]); | ||
}); | ||
done(); | ||
}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('delete accessKeys', function(done) { | ||
it('should delete accessKeys successful', function(done) { | ||
request.delete(`/accessKeys/${encodeURI(newFriendlyName)}`) | ||
.set('Authorization', `Basic ${authToken}`) | ||
.send() | ||
.end(function(err, res) { | ||
should.not.exist(err); | ||
res.status.should.equal(200); | ||
var rs = JSON.parse(res.text); | ||
rs.should.have.properties('friendlyName'); | ||
done(); | ||
}); | ||
describe("delete accessKeys", function (done) { | ||
it("should delete accessKeys successful", function (done) { | ||
request | ||
.delete(`/accessKeys/${encodeURI(newFriendlyName)}`) | ||
.set("Authorization", `Basic ${authToken}`) | ||
.send() | ||
.end(function (err, res) { | ||
should.not.exist(err); | ||
res.status.should.equal(200); | ||
var rs = JSON.parse(res.text); | ||
rs.should.have.properties("friendlyName"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
}); |
Oops, something went wrong.