Skip to content

Commit

Permalink
fix: replace deprecated use of Buffer()
Browse files Browse the repository at this point in the history
  • Loading branch information
rocwind committed Aug 19, 2021
1 parent 9b19534 commit dd2a141
Show file tree
Hide file tree
Showing 9 changed files with 1,109 additions and 917 deletions.
2 changes: 1 addition & 1 deletion core/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ middleware.checkToken = function (req, res, next) {
}
} else if (_.eq(authArr[0], "Basic")) {
authType = 2;
var b = new Buffer(authArr[1], "base64");
var b = Buffer.from(authArr[1], "base64");
var user = _.split(b.toString(), ":");
authToken = _.get(user, "1");
}
Expand Down
2 changes: 1 addition & 1 deletion core/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = common;

common.detectIsTextFile = function (filePath) {
var fd = fs.openSync(filePath, "r");
var buffer = new Buffer(4096);
var buffer = Buffer.alloc(4096);
fs.readSync(fd, buffer, 0, 4096, 0);
fs.closeSync(fd);
var rs = jschardet.detect(buffer);
Expand Down
131 changes: 65 additions & 66 deletions core/utils/qetag.js
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;
176 changes: 102 additions & 74 deletions test/api/accessKeys/accessKeys.test.js
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();
});
});
});

});
Loading

0 comments on commit dd2a141

Please sign in to comment.