Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] feat: support loadConfig asynchronous #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样直接赋值的话 client 的配置会不会覆盖 default 啊(盲猜
测试用例里好像也没体现

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

@troyeagle troyeagle Jun 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样的话 default 中的异步函数由于延迟执行,会覆盖 client 里面已经设置好的重复字段;对于 client 和 default 都需要异步函数的情形还支持不了。
https://gist.github.com/troyeagle/b10774eff060cc6c6d1ed51ba8d842ec 可能是一种解决方法。总之用异步函数设定的值会打破原先 default 和 client 的加载顺序。求教在这种情形下应该怎么考虑设计

}
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));
});
};
14 changes: 14 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,14 @@
'use strict';

const assert = require('assert');

exports.keys = '123';
exports.oss = {
default: {
async loadConfig(config, app) {
assert(app && app.name === 'oss-async-load');
return Object.assign({}, require('../../../../config'), 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);
});
});

});