Skip to content

Commit

Permalink
feat: support loadConfig asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer committed Jun 24, 2019
1 parent 93a05ea commit 42327b4
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 10 deletions.
7 changes: 7 additions & 0 deletions app/extend/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
async createOss(...args) {
return await this.oss.createInstanceAsync.call(this.oss, ...args);
},
};
8 changes: 6 additions & 2 deletions lib/oss.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const assert = require('assert');
const is = require('is-type-of');
const OSS = require('ali-oss');
const STS = OSS.STS;
const ClusterClient = OSS.ClusterClient;
Expand All @@ -13,8 +14,12 @@ function checkBucketConfig(config) {
}

module.exports = app => {
app.addSingleton('oss', (config, app) => {
app.addSingleton('oss', async (config, app) => {
config = Object.assign({}, config, { urllib: app.httpclient });
if (is.function(config.loadConfig)) {
const result = config.loadConfig(config, app);
config = is.promise(result) ? (await result) : result;
}
if (config.cluster) {
config.cluster.forEach(checkBucketConfig);
return new ClusterClient(config);
Expand All @@ -27,5 +32,4 @@ module.exports = app => {
checkBucketConfig(config);
return new OSS(config);
});
app.createOss = app.oss.createInstance.bind(app.oss);
};
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
"oss"
],
"dependencies": {
"ali-oss": "^6.0.1"
"ali-oss": "^6.1.1",
"is-type-of": "^1.2.1"
},
"devDependencies": {
"autod": "^3.0.1",
"autod": "^3.1.0",
"dotenv": "^6.1.0",
"egg": "^2.11.2",
"egg-bin": "^4.9.0",
"egg-mock": "^3.20.1",
"eslint": "^5.6.1",
"eslint-config-egg": "^7.1.0",
"egg": "^2.22.2",
"egg-bin": "^4.13.0",
"egg-mock": "^3.23.1",
"eslint": "^6.0.0",
"eslint-config-egg": "^7.4.1",
"is-type-of": "^1.2.1"
},
"engines": {
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/apps/oss-async-load/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const fs = require('fs');

module.exports = function(app) {
app.get('/uploadtest', async ctx => {
const name = 'oss-test-upload-' + process.version + '-' + Date.now();
ctx.body = await ctx.oss.put(name, fs.createReadStream(__filename));
});
};
11 changes: 11 additions & 0 deletions test/fixtures/apps/oss-async-load/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

exports.keys = '123';
exports.oss = {
default: {
async loadConfig(config) {
return Object.assign({}, require('../../../../config'));
},
},
client: {},
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/oss-async-load/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "oss-async-load"
}
4 changes: 3 additions & 1 deletion test/fixtures/apps/oss-not-init/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

module.exports = function(app) {
app.uploader = app.createOss(app.config.uploader);
app.beforeStart(async () => {
app.uploader = await app.createOss(app.config.uploader);
});
};
29 changes: 29 additions & 0 deletions test/oss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,33 @@ describe('test/oss.test.js', () => {
});
});

describe('oss async load config', () => {
let app;
let lastUploadFileName;
before(async () => {
app = mm.app({
baseDir: 'apps/oss-async-load',
});
await app.ready();
});
after(async () => {
if (lastUploadFileName) {
await app.oss.delete(lastUploadFileName);
}
await app.close();
});

it('should upload file stream to oss', async () => {
await app.httpRequest()
.get('/uploadtest')
.expect(function(res) {
lastUploadFileName = res.body.name;
assert(typeof res.body.name === 'string');
assert(/^https?:\/\/egg\-oss\-unittest\.\w+/.test(res.body.url));
assert(res.body.res.status === 200);
})
.expect(200);
});
});

});

0 comments on commit 42327b4

Please sign in to comment.