diff --git a/app/extend/application.js b/app/extend/application.js new file mode 100644 index 0000000..9645685 --- /dev/null +++ b/app/extend/application.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports = { + async createOss(...args) { + return await this.oss.createInstanceAsync.call(this.oss, ...args); + }, +}; diff --git a/lib/oss.js b/lib/oss.js index 0232ced..64559a9 100644 --- a/lib/oss.js +++ b/lib/oss.js @@ -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; @@ -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); @@ -27,5 +32,4 @@ module.exports = app => { checkBucketConfig(config); return new OSS(config); }); - app.createOss = app.oss.createInstance.bind(app.oss); }; diff --git a/package.json b/package.json index e6c1f4a..cb39f59 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/fixtures/apps/oss-async-load/app/router.js b/test/fixtures/apps/oss-async-load/app/router.js new file mode 100644 index 0000000..d362142 --- /dev/null +++ b/test/fixtures/apps/oss-async-load/app/router.js @@ -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)); + }); +}; diff --git a/test/fixtures/apps/oss-async-load/config/config.default.js b/test/fixtures/apps/oss-async-load/config/config.default.js new file mode 100644 index 0000000..75aad6d --- /dev/null +++ b/test/fixtures/apps/oss-async-load/config/config.default.js @@ -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: {}, +}; diff --git a/test/fixtures/apps/oss-async-load/package.json b/test/fixtures/apps/oss-async-load/package.json new file mode 100644 index 0000000..a5badf6 --- /dev/null +++ b/test/fixtures/apps/oss-async-load/package.json @@ -0,0 +1,3 @@ +{ + "name": "oss-async-load" +} diff --git a/test/fixtures/apps/oss-not-init/app.js b/test/fixtures/apps/oss-not-init/app.js index b6b625f..533d7f9 100644 --- a/test/fixtures/apps/oss-not-init/app.js +++ b/test/fixtures/apps/oss-not-init/app.js @@ -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); + }); }; diff --git a/test/oss.test.js b/test/oss.test.js index 5170ac6..4ccec34 100644 --- a/test/oss.test.js +++ b/test/oss.test.js @@ -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); + }); + }); + });