├── .eslintignore
├── test
├── fixtures
│ └── apps
│ │ └── hello
│ │ ├── package.json
│ │ ├── app
│ │ ├── router.js
│ │ └── controller
│ │ │ └── home.js
│ │ └── config
│ │ └── config.default.js
└── plugin.test.js
├── app
└── extend
│ ├── application.local.js
│ └── application.unittest.js
├── config
└── config.default.js
├── .gitignore
├── .eslintrc
├── .travis.yml
├── lib
└── application.js
├── appveyor.yml
├── .autod.conf.js
├── LISENCE
├── package.json
├── agent.js
└── README.md
/.eslintignore:
--------------------------------------------------------------------------------
1 | test/fixtures
2 | coverage
3 |
--------------------------------------------------------------------------------
/test/fixtures/apps/hello/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "datahub-test"
3 | }
4 |
--------------------------------------------------------------------------------
/app/extend/application.local.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | module.exports = require('../../lib/application');
4 |
--------------------------------------------------------------------------------
/app/extend/application.unittest.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | module.exports = require('../../lib/application');
4 |
--------------------------------------------------------------------------------
/config/config.default.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | exports.datahub = {
4 | port: 5678,
5 | hostname: 'localhost',
6 | };
7 |
--------------------------------------------------------------------------------
/test/fixtures/apps/hello/app/router.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | module.exports = app => {
4 | app.get('/', 'home');
5 | };
6 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | logs/
2 | run/
3 | npm-debug.log
4 | node_modules/
5 | package-lock.json
6 | coverage/
7 | .idea/
8 | .DS_Store
9 | .project
10 | Thumbs.db
11 | *.sw*
12 | *.un~
13 | *.tgz
14 |
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "parser": "babel-eslint",
3 | "extends": "eslint-config-egg",
4 | "parserOptions": {
5 | "ecmaFeatures": {
6 | "experimentalObjectRestSpread": true
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 | language: node_js
3 | node_js:
4 | - '10'
5 | - '12'
6 | install:
7 | - npm i npminstall && npminstall
8 | script:
9 | - npm run ci
10 | after_script:
11 | - npminstall codecov && codecov
12 |
--------------------------------------------------------------------------------
/test/fixtures/apps/hello/config/config.default.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const path = require('path');
4 |
5 | exports.keys = 'foo';
6 |
7 | exports.logger = {
8 | consoleLevel: 'INFO',
9 | coreLogger: {
10 | consoleLevel: 'INFO',
11 | },
12 | };
13 |
14 | exports.datahub = {
15 | store: path.join(__dirname, 'data'),
16 | };
17 |
--------------------------------------------------------------------------------
/lib/application.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const DataHubSDK = require('datahub-nodejs-sdk');
4 | const DATAHUB = Symbol('Application#datahubClient');
5 |
6 | module.exports = {
7 | datahubClient() {
8 | if (!this[DATAHUB]) {
9 | this[DATAHUB] = new DataHubSDK(this.config.datahub);
10 | }
11 | return this[DATAHUB];
12 | },
13 | };
14 |
--------------------------------------------------------------------------------
/appveyor.yml:
--------------------------------------------------------------------------------
1 | environment:
2 | matrix:
3 | - nodejs_version: '8'
4 | - nodejs_version: '10'
5 | - nodejs_version: '12'
6 |
7 | install:
8 | - ps: Install-Product node $env:nodejs_version
9 | - npm i npminstall && node_modules\.bin\npminstall
10 |
11 | test_script:
12 | - node --version
13 | - npm --version
14 | - npm run test
15 |
16 | build: off
17 |
--------------------------------------------------------------------------------
/test/fixtures/apps/hello/app/controller/home.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | // GET /
4 | module.exports = async ctx => {
5 | await ctx.app.datahubClient().switchScene({
6 | hub: 'openapi',
7 | pathname: 'api/create',
8 | scene: 'success',
9 | method: 'GET', // method is optional, default method is 'ALL'
10 | });
11 |
12 | ctx.body = {
13 | datahubClient: !!ctx.app.datahubClient(),
14 | };
15 | };
16 |
--------------------------------------------------------------------------------
/.autod.conf.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | module.exports = {
4 | write: true,
5 | prefix: '^',
6 | plugin: 'autod-egg',
7 | test: [
8 | 'test',
9 | 'benchmark',
10 | ],
11 | dep: [
12 | 'egg',
13 | 'egg-scripts',
14 | ],
15 | devdep: [
16 | 'egg-ci',
17 | 'egg-bin',
18 | 'egg-mock',
19 | 'autod',
20 | 'autod-egg',
21 | 'eslint',
22 | 'eslint-config-egg',
23 | ],
24 | exclude: [
25 | './test/fixtures',
26 | './dist',
27 | ],
28 | };
29 |
30 |
--------------------------------------------------------------------------------
/test/plugin.test.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const mm = require('egg-mock');
4 | const assert = require('assert');
5 |
6 | describe('test/plugin.test.js', () => {
7 | let app;
8 | before(() => {
9 | app = mm.app({
10 | baseDir: 'apps/hello',
11 | });
12 | return app.ready();
13 | });
14 |
15 | it('should pkg exists', () => {
16 | assert(app.config.pkg.name === 'datahub-test');
17 | });
18 |
19 | it('should datahubClient exists', async () => {
20 | const res = await app.httpRequest()
21 | .get('/');
22 | assert(res.status === 200);
23 | assert(res.body.datahubClient === true);
24 | });
25 | });
26 |
--------------------------------------------------------------------------------
/LISENCE:
--------------------------------------------------------------------------------
1 | MIT LICENSE
2 |
3 | Copyright (c) 2017 Alibaba Group Holding Limited and other contributors.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining
6 | a copy of this software and associated documentation files (the
7 | "Software"), to deal in the Software without restriction, including
8 | without limitation the rights to use, copy, modify, merge, publish,
9 | distribute, sublicense, and/or sell copies of the Software, and to
10 | permit persons to whom the Software is furnished to do so, subject to
11 | the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be
14 | included in all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "egg-datahub",
3 | "version": "4.2.0",
4 | "description": "Macaca DataHub plugin for Egg.js",
5 | "eggPlugin": {
6 | "name": "datahub",
7 | "env": [
8 | "local",
9 | "unittest"
10 | ]
11 | },
12 | "repository": {
13 | "type": "git",
14 | "url": "https://github.com/eggjs/egg-datahub.git"
15 | },
16 | "keywords": [
17 | "egg",
18 | "plugin",
19 | "egg-plugin",
20 | "eggPlugin"
21 | ],
22 | "files": [
23 | "app",
24 | "config",
25 | "lib",
26 | "agent.js"
27 | ],
28 | "dependencies": {
29 | "cross-spawn": "^7.0.1",
30 | "datahub-nodejs-sdk": "^2.2.1",
31 | "macaca-datahub": "^4.1.1"
32 | },
33 | "devDependencies": {
34 | "autod": "2",
35 | "autod-egg": "^1.0.0",
36 | "babel-eslint": "^8.2.3",
37 | "egg": "2",
38 | "egg-bin": "1",
39 | "egg-ci": "^1.8.0",
40 | "egg-mock": "^3.13.1",
41 | "eslint": "^6.0.1",
42 | "eslint-config-egg": "3",
43 | "git-contributor": "1",
44 | "husky": "^2.4.0"
45 | },
46 | "engines": {
47 | "node": ">=8.9.0"
48 | },
49 | "scripts": {
50 | "test": "npm run lint && npm run test-local",
51 | "test-local": "egg-bin test",
52 | "cov": "egg-bin cov",
53 | "lint": "eslint .",
54 | "ci": "npm run lint && npm run cov",
55 | "autod": "autod",
56 | "contributor": "git-contributor"
57 | },
58 | "husky": {
59 | "hooks": {
60 | "pre-commit": "npm run lint"
61 | }
62 | },
63 | "ci": {
64 | "version": "8, 10"
65 | },
66 | "license": "MIT"
67 | }
68 |
--------------------------------------------------------------------------------
/agent.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const spawn = require('cross-spawn');
4 |
5 | module.exports = class {
6 | constructor(app) {
7 | this.app = app;
8 | this.child = null;
9 | this._startPromise = new Promise((resolve, reject) => {
10 | this._startResolve = resolve;
11 | this._startReject = reject;
12 | });
13 | }
14 |
15 | async didLoad() {
16 | const { app } = this;
17 | const config = Object.assign({}, app.config.datahub);
18 | const binPath = require.resolve('macaca-datahub/bin/datahub-server.js');
19 |
20 | const child = this.child = spawn(
21 | binPath,
22 | [
23 | '-o',
24 | JSON.stringify(config),
25 | ],
26 | );
27 |
28 | child.stdout.setEncoding('utf8');
29 | child.stderr.setEncoding('utf8');
30 |
31 | const startTime = Date.now();
32 | child.stdout.on('data', data => {
33 | let log = data.trim();
34 | if (log.includes('DataHub server start at: http://')) {
35 | this._startResolve();
36 | log += ` (${Date.now() - startTime}ms)`;
37 | } else if (log.includes('DataHub start unsuccessfully')) {
38 | this._startReject(new Error(log));
39 | log += ` (${Date.now() - startTime}ms)`;
40 | }
41 | app.logger.info('[egg-datahub] %s', log);
42 | });
43 |
44 | child.stderr.on('data', data => {
45 | app.logger.error('[egg-datahub:error] %s', data.trim());
46 | });
47 |
48 | child.on('close', code => {
49 | const level = code === 0 ? 'info' : 'warn';
50 | app.logger[level]('[egg-datahub] datahub process#%s exit with code: %s', child.pid, code);
51 | });
52 | app.logger.info('[egg-datahub] datahub process#%s spawn success, config: %j', child.pid, config);
53 | // kill datahub process too
54 | process.on('exit', () => {
55 | process.kill(child.pid);
56 | });
57 | }
58 |
59 | async willReady() {
60 | await this._startPromise;
61 | }
62 |
63 | async beforeClose() {
64 | if (this.child) {
65 | this.app.logger.info('[egg-datahub] killing datahub process#%s', this.child.pid);
66 | this.child.kill();
67 | }
68 | }
69 | };
70 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # egg-datahub
2 |
3 | ---
4 |
5 | [![NPM version][npm-image]][npm-url]
6 | [![build status][travis-image]][travis-url]
7 | [![Test coverage][coveralls-image]][coveralls-url]
8 | [![node version][node-image]][node-url]
9 | [![egg version][egg-image]][egg-url]
10 | [![npm download][download-image]][download-url]
11 |
12 | [npm-image]: https://img.shields.io/npm/v/egg-datahub.svg
13 | [npm-url]: https://npmjs.org/package/egg-datahub
14 | [travis-image]: https://img.shields.io/travis/eggjs/egg-datahub.svg
15 | [travis-url]: https://travis-ci.org/eggjs/egg-datahub
16 | [coveralls-image]: https://img.shields.io/codecov/c/github/eggjs/egg-datahub.svg
17 | [coveralls-url]: https://codecov.io/gh/eggjs/egg-datahub
18 | [node-image]: https://img.shields.io/badge/node.js-%3E=_8-green.svg
19 | [node-url]: http://nodejs.org/download/
20 | [egg-image]: https://img.shields.io/badge/egg-%3E=_2-green.svg
21 | [egg-url]: https://github.com/eggjs/egg
22 | [download-image]: https://img.shields.io/npm/dm/egg-datahub.svg
23 | [download-url]: https://npmjs.org/package/egg-datahub
24 |
25 | > Macaca DataHub plugin for Egg.js
26 |
27 | ## Installation
28 |
29 | ```
30 | $ npm i egg-datahub --save-dev
31 | ```
32 |
33 | ## Configuration
34 |
35 | config/plugin.js
36 |
37 | ```javascript
38 | exports.datahub = {
39 | enable: true,
40 | package: 'egg-datahub'
41 | };
42 | ```
43 |
44 | config.unittest.js (default config)
45 |
46 | ```javascript
47 | exports.datahub = {
48 | port: 9200,
49 | hostname: 'localhost'
50 | };
51 | ```
52 |
53 | ## Example
54 |
55 | [hackernews-datahub](//github.com/eggjs/examples/tree/master/hackernews-datahub)
56 |
57 |
58 |
59 | ## Contributors
60 |
61 | |[
zivyangll](https://github.com/zivyangll)
|[
xudafeng](https://github.com/xudafeng)
|[
snapre](https://github.com/snapre)
|[
zhangyuheng](https://github.com/zhangyuheng)
|[
fengmk2](https://github.com/fengmk2)
|
62 | | :---: | :---: | :---: | :---: | :---: |
63 |
64 |
65 | This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Sun Mar 27 2022 22:44:45 GMT+0800`.
66 |
67 |
68 |
69 | ## License
70 |
71 | The MIT License (MIT)
72 |
--------------------------------------------------------------------------------