From 93037c0bd9af047c1d8c312b5ef292d56939fdf5 Mon Sep 17 00:00:00 2001 From: joelchu Date: Wed, 6 Nov 2019 22:28:02 +0800 Subject: [PATCH 1/9] The sub ms still not working correctly --- packages/koa/package.json | 4 ++-- packages/koa/tests/node-client.test.js | 2 +- packages/node-client/tests/fixtures/server-with-socket.js | 2 +- packages/node-client/tests/socket.test.js | 6 ------ 4 files changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/koa/package.json b/packages/koa/package.json index 9b50b3e2..761556e8 100644 --- a/packages/koa/package.json +++ b/packages/koa/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-koa", - "version": "1.4.1", + "version": "1.4.2", "description": "jsonql Koa middleware", "main": "main.js", "module": "index.js", @@ -76,7 +76,7 @@ "jsonql-contract": "^1.8.3", "jsonql-errors": "^1.1.5", "jsonql-jwt": "^1.3.3", - "jsonql-node-client": "^1.1.11", + "jsonql-node-client": "^1.2.0", "jsonql-params-validator": "^1.4.11", "jsonql-resolver": "^0.9.4", "jsonql-utils": "^0.8.3", diff --git a/packages/koa/tests/node-client.test.js b/packages/koa/tests/node-client.test.js index d4a8ee7c..2668b9c1 100644 --- a/packages/koa/tests/node-client.test.js +++ b/packages/koa/tests/node-client.test.js @@ -58,7 +58,7 @@ test.skip(`First test calling the ${port} directly with the mutation call`, asyn t.truthy(result.indexOf(`ms service`)) }) -test.skip(`It should able to call a resolver that access another ms`, async t => { +test(`It should able to call a resolver that access another ms`, async t => { // the problem now is calling the 6001 but received the 8001 public contract? const client = await nodeClient({ hostname: `http://localhost:${port}`, diff --git a/packages/node-client/tests/fixtures/server-with-socket.js b/packages/node-client/tests/fixtures/server-with-socket.js index b29fcd69..e6f15d75 100644 --- a/packages/node-client/tests/fixtures/server-with-socket.js +++ b/packages/node-client/tests/fixtures/server-with-socket.js @@ -10,7 +10,7 @@ module.exports = function serverWithAuth(extra = {}) { const config = Object.assign({ resolverDir: join(__dirname,'resolvers'), // resuse this contract if there is one - contractDir: join(__dirname,'contract','tmp','server-with-auth'), + contractDir: join(__dirname,'contract','server-with-auth'), keysDir: join(__dirname, 'keys'), enableAuth: true }, extra) diff --git a/packages/node-client/tests/socket.test.js b/packages/node-client/tests/socket.test.js index e12fea04..ead7e677 100644 --- a/packages/node-client/tests/socket.test.js +++ b/packages/node-client/tests/socket.test.js @@ -21,12 +21,6 @@ test.after(t => { t.context.stop() }) -/* -test.skip(`Test if the server start at all`, t => { - t.pass() -}) -*/ - test.skip(`It should able to connect the server via http`, async t => { const result = await t.context.client.query.helloWorld() t.is('Hello world!', result) -- Gitee From 5dadaf4297d8871410eacac985878a4f7983b9dc Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 7 Nov 2019 13:24:19 +0800 Subject: [PATCH 2/9] port the server with socket from node-client --- .../koa/tests/helpers/server-with-socket.js | 32 ++++++++++++++++ packages/koa/tests/helpers/server.js | 38 ++++++++++++++----- packages/koa/tests/node-client.test.js | 5 +-- .../tests/fixtures/server-with-socket.js | 2 +- 4 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 packages/koa/tests/helpers/server-with-socket.js diff --git a/packages/koa/tests/helpers/server-with-socket.js b/packages/koa/tests/helpers/server-with-socket.js new file mode 100644 index 00000000..5260cc7a --- /dev/null +++ b/packages/koa/tests/helpers/server-with-socket.js @@ -0,0 +1,32 @@ +// for testing the server with socket +const { join } = require('path') +const server = require('server-io-core') +const { jsonqlKoa } = require('../../main') + +const jsonqlWsServer = require('jsonql-ws-server') + +// output +module.exports = function serverWithSocket(port = 8001, extra = {}) { + const config = Object.assign({ + resolverDir: join(__dirname,'resolvers'), + // resuse this contract if there is one + contractDir: join(__dirname,'contract','server-with-auth'), + keysDir: join(__dirname, 'keys'), + enableAuth: true + }, extra) + return Promise.resolve( + server({ + port: port, + debug: false, + open: false, + reload: false, + socket: false, + middlewares: [ + jsonqlKoa(config) + ] + }) + ).then(({webserver, stop}) => ( + jsonqlWsServer(config, webserver) + .then(() => ({stop, config, port})) + )) +} diff --git a/packages/koa/tests/helpers/server.js b/packages/koa/tests/helpers/server.js index 2a4812b6..c5b00497 100644 --- a/packages/koa/tests/helpers/server.js +++ b/packages/koa/tests/helpers/server.js @@ -1,22 +1,40 @@ // this will export the server for other test to use // const test = require('ava'); +const http = require('http') const Koa = require('koa') const { join } = require('path') const bodyparser = require('koa-bodyparser') const { jsonqlKoa } = require('../../main') -const { type, headers, dirs } = require('../fixtures/options') -const fsx = require('fs-extra') -const myKey = '4670994sdfkl'; +const dir = join(__dirname, '..', 'fixtures') +const contractDir = join(dir, 'tmp') + +const baseResolverDir = join(dir, 'resolvers') +const msResolverDir = join(dir, 'sub') + // add a dir to seperate the contract files -module.exports = (config={}, dir = '') => { +function createServer(config={}, dir = '') { + + const opts = Object.assign({},{ + contractDir: join(contractDir, dir) + }, config) + const app = new Koa() app.use(bodyparser()) - app.use(jsonqlKoa( - Object.assign({},{ - resolverDir: dirs.resolverDir, - contractDir: join(dirs.contractDir, dir) - }, config) - )) + app.use(jsonqlKoa(opts)) + return app; } + +function createBaseServer() { + +} + +function createMsServer() { + +} + +module.exports = { + createBaseServer, + createMsServer +} diff --git a/packages/koa/tests/node-client.test.js b/packages/koa/tests/node-client.test.js index 2668b9c1..efffe27f 100644 --- a/packages/koa/tests/node-client.test.js +++ b/packages/koa/tests/node-client.test.js @@ -4,14 +4,13 @@ const { join } = require('path') const fsx = require('fs-extra') const debug = require('debug')('jsonql-koa:test:node-client') const nodeClient = require('jsonql-node-client') -const serverIoCore = require('server-io-core') // setup -const jsonqlKoa = require('../') const hello = require('./helpers/hello') const baseDir = join(__dirname, 'fixtures') -const clientContractDir = join(__dirname, 'fixtures', 'tmp', 'client6002') const createServer = require('./helpers/server') +const clientContractDir = join(__dirname, 'fixtures', 'tmp', 'client6002') + const port = 6002; const dir = 'server6002'; const msPort = 8001; diff --git a/packages/node-client/tests/fixtures/server-with-socket.js b/packages/node-client/tests/fixtures/server-with-socket.js index e6f15d75..7e6b532b 100644 --- a/packages/node-client/tests/fixtures/server-with-socket.js +++ b/packages/node-client/tests/fixtures/server-with-socket.js @@ -6,7 +6,7 @@ const jsonqlKoa = require('./jsonql-koa') const jsonqlWsServer = require('jsonql-ws-server') // output -module.exports = function serverWithAuth(extra = {}) { +module.exports = function serverWithSocket(extra = {}) { const config = Object.assign({ resolverDir: join(__dirname,'resolvers'), // resuse this contract if there is one -- Gitee From 448a746324169ef6955ed35e1933d6e75f6f253c Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 7 Nov 2019 13:25:02 +0800 Subject: [PATCH 3/9] add jsonql-ws-server as dev dependencies --- packages/koa/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/koa/package.json b/packages/koa/package.json index 761556e8..59fad628 100644 --- a/packages/koa/package.json +++ b/packages/koa/package.json @@ -87,6 +87,7 @@ }, "devDependencies": { "ava": "^2.4.0", + "jsonql-ws-server": "^1.3.5", "jwt-decode": "^2.2.0", "koa-bodyparser": "^4.2.1", "nyc": "^14.1.1", -- Gitee From 8c58e423ff7110d915f3a5417419a74c85126801 Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 7 Nov 2019 15:09:24 +0800 Subject: [PATCH 4/9] add two socket interface for testing the ms --- packages/koa/package.json | 1 + .../koa/tests/fixtures/resolvers/socket/call-sub.js | 11 +++++++++++ .../tests/fixtures/resolvers/update-ms-service.js | 12 ------------ .../tests/fixtures/sub/resolver/socket/reply-msg.js | 11 +++++++++++ 4 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 packages/koa/tests/fixtures/resolvers/socket/call-sub.js delete mode 100644 packages/koa/tests/fixtures/resolvers/update-ms-service.js create mode 100644 packages/koa/tests/fixtures/sub/resolver/socket/reply-msg.js diff --git a/packages/koa/package.json b/packages/koa/package.json index 59fad628..307c6fa6 100644 --- a/packages/koa/package.json +++ b/packages/koa/package.json @@ -90,6 +90,7 @@ "jsonql-ws-server": "^1.3.5", "jwt-decode": "^2.2.0", "koa-bodyparser": "^4.2.1", + "nb-split-tasks": "^0.5.3", "nyc": "^14.1.1", "request": "^2.88.0", "server-io-core": "^1.2.0", diff --git a/packages/koa/tests/fixtures/resolvers/socket/call-sub.js b/packages/koa/tests/fixtures/resolvers/socket/call-sub.js new file mode 100644 index 00000000..278f0438 --- /dev/null +++ b/packages/koa/tests/fixtures/resolvers/socket/call-sub.js @@ -0,0 +1,11 @@ +/** + * Test interface to try to call the sub using socket client + * @param {string} msg a message + * @return {string} a reply from the sub + */ +module.exports = function callSub(msg) { + + let newMsg; /// fill this with the reply from the sub + + return `Got a ${newMsg || 'nothing'}` +} diff --git a/packages/koa/tests/fixtures/resolvers/update-ms-service.js b/packages/koa/tests/fixtures/resolvers/update-ms-service.js deleted file mode 100644 index 244aae63..00000000 --- a/packages/koa/tests/fixtures/resolvers/update-ms-service.js +++ /dev/null @@ -1,12 +0,0 @@ -const debug = require('debug')('jsonql-koa:mutation:update-ms-service') - -/** - * this will be calling a microserivce setup using the nodeClient - * @param {string} payload incoming - * @return {string} msg return from nodeClient - */ -module.exports = async function updateMsService(payload) { - const client = await updateMsService.client() - debug(client) - return client.query.subMsService(payload) -} diff --git a/packages/koa/tests/fixtures/sub/resolver/socket/reply-msg.js b/packages/koa/tests/fixtures/sub/resolver/socket/reply-msg.js new file mode 100644 index 00000000..8108b019 --- /dev/null +++ b/packages/koa/tests/fixtures/sub/resolver/socket/reply-msg.js @@ -0,0 +1,11 @@ +/** + * expect a call from someone else then modifiy the msg and reply it + * @param {string} msg incoming message + * @return {string} modified message + */ +module.exports = function replyMsg(msg) { + if (msg === 'hello') { + return `What's the matter?` + } + return `What do you want?` +} -- Gitee From f8a396d4f4434f9bf3c65559d160f6573e29e4d9 Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 7 Nov 2019 16:49:22 +0800 Subject: [PATCH 5/9] pre-generate the contract files and need to figure out during the init phrase guarantee the ws server can get the contract in time --- .../fixtures/log/server7001/contract.json | 224 ++++++++++++++++++ .../log/server7001/public-contract.json | 202 ++++++++++++++++ .../fixtures/log/server8001/contract.json | 74 ++++++ .../log/server8001/public-contract.json | 80 +++++++ .../koa/tests/helpers/server-with-socket.js | 15 +- packages/koa/tests/node-client.test.js | 75 +++--- packages/node-client/index.js | 2 +- 7 files changed, 635 insertions(+), 37 deletions(-) create mode 100644 packages/koa/tests/fixtures/log/server7001/contract.json create mode 100644 packages/koa/tests/fixtures/log/server7001/public-contract.json create mode 100644 packages/koa/tests/fixtures/log/server8001/contract.json create mode 100644 packages/koa/tests/fixtures/log/server8001/public-contract.json diff --git a/packages/koa/tests/fixtures/log/server7001/contract.json b/packages/koa/tests/fixtures/log/server7001/contract.json new file mode 100644 index 00000000..6d4c15a5 --- /dev/null +++ b/packages/koa/tests/fixtures/log/server7001/contract.json @@ -0,0 +1,224 @@ +{ + "query": { + "causeError": { + "file": "/home/joel/projects/open-source/jsonql/packages/koa/tests/fixtures/resolvers/query/cause-error.js", + "description": false, + "params": [ + { + "type": [ + "any" + ], + "name": "x", + "description": "param" + } + ], + "returns": [ + { + "type": [ + "any" + ], + "description": "unknown" + } + ] + }, + "getUser": { + "file": "/home/joel/projects/open-source/jsonql/packages/koa/tests/fixtures/resolvers/query/get-user.js", + "description": "This use a spread as parameter", + "params": [ + { + "type": [ + "string" + ], + "name": "args", + "variable": true, + "description": "passing unknown number of param" + } + ], + "returns": [ + { + "type": [ + "any" + ], + "description": "extract from last of the args" + } + ] + }, + "getSecretMsg": { + "file": "/home/joel/projects/open-source/jsonql/packages/koa/tests/fixtures/resolvers/query/private/get-secret-msg.js", + "description": "a hidden private method", + "params": [], + "returns": [ + { + "type": [ + "string" + ], + "description": "a secret message" + } + ] + }, + "alwaysAvailable": { + "public": true, + "file": "/home/joel/projects/open-source/jsonql/packages/koa/tests/fixtures/resolvers/query/public/always-available.js", + "description": "This is a public method that is always available", + "params": [], + "returns": [ + { + "type": [ + "string" + ], + "description": "a message" + } + ] + }, + "testList": { + "file": "/home/joel/projects/open-source/jsonql/packages/koa/tests/fixtures/resolvers/query/test-list.js", + "description": false, + "params": [ + { + "type": [ + "number" + ], + "name": "num", + "description": "a number" + } + ], + "returns": [ + { + "type": [ + "object" + ], + "description": "@TODO need to figure out how to give keys to the returns" + } + ] + } + }, + "mutation": { + "updateList": { + "file": "/home/joel/projects/open-source/jsonql/packages/koa/tests/fixtures/resolvers/mutation/update-list.js", + "description": false, + "params": [ + { + "type": [ + "object" + ], + "name": "payload", + "keys": [ + { + "type": [ + "number" + ], + "name": "user", + "parent": "payload" + } + ] + }, + { + "type": [ + "object" + ], + "name": "condition" + } + ], + "returns": [ + { + "type": [ + "object" + ], + "description": "with user as key" + } + ] + } + }, + "auth": { + "login": { + "file": "/home/joel/projects/open-source/jsonql/packages/koa/tests/fixtures/resolvers/auth/login.js", + "description": "Auth method", + "params": [ + { + "type": [ + "string" + ], + "name": "username", + "description": "user name" + }, + { + "type": [ + "string" + ], + "name": "password", + "description": "password" + } + ], + "returns": [ + { + "type": [ + "string", + "boolean" + ], + "description": "token on success, false on fail" + } + ] + }, + "logout": { + "file": "/home/joel/projects/open-source/jsonql/packages/koa/tests/fixtures/resolvers/auth/logout.js", + "description": false, + "params": [], + "returns": [ + { + "type": [ + "boolean" + ], + "description": "just return something" + } + ] + }, + "validator": { + "file": "/home/joel/projects/open-source/jsonql/packages/koa/tests/fixtures/resolvers/auth/validator.js", + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "token", + "description": "jwt" + } + ], + "returns": [ + { + "type": [ + "object", + "boolean" + ], + "description": "user data on success, false on fail" + } + ] + } + }, + "timestamp": 1573116438, + "sourceType": "script", + "socket": { + "callSub": { + "namespace": "jsonql/private", + "file": "/home/joel/projects/open-source/jsonql/packages/koa/tests/fixtures/resolvers/socket/call-sub.js", + "description": "Test interface to try to call the sub using socket client", + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "a message" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "a reply from the sub" + } + ] + } + } +} diff --git a/packages/koa/tests/fixtures/log/server7001/public-contract.json b/packages/koa/tests/fixtures/log/server7001/public-contract.json new file mode 100644 index 00000000..4720aa6f --- /dev/null +++ b/packages/koa/tests/fixtures/log/server7001/public-contract.json @@ -0,0 +1,202 @@ +{ + "query": { + "helloWorld": { + "description": "This is the stock resolver for testing purpose", + "params": [], + "returns": [ + { + "type": "string", + "description": "stock message" + } + ] + }, + "causeError": { + "description": false, + "params": [ + { + "type": [ + "any" + ], + "name": "x", + "description": "param" + } + ], + "returns": [ + { + "type": [ + "any" + ], + "description": "unknown" + } + ] + }, + "getUser": { + "description": "This use a spread as parameter", + "params": [ + { + "type": [ + "string" + ], + "name": "args", + "variable": true, + "description": "passing unknown number of param" + } + ], + "returns": [ + { + "type": [ + "any" + ], + "description": "extract from last of the args" + } + ] + }, + "getSecretMsg": { + "description": "a hidden private method", + "params": [], + "returns": [ + { + "type": [ + "string" + ], + "description": "a secret message" + } + ] + }, + "alwaysAvailable": { + "public": true, + "description": "This is a public method that is always available", + "params": [], + "returns": [ + { + "type": [ + "string" + ], + "description": "a message" + } + ] + }, + "testList": { + "description": false, + "params": [ + { + "type": [ + "number" + ], + "name": "num", + "description": "a number" + } + ], + "returns": [ + { + "type": [ + "object" + ], + "description": "@TODO need to figure out how to give keys to the returns" + } + ] + } + }, + "mutation": { + "updateList": { + "description": false, + "params": [ + { + "type": [ + "object" + ], + "name": "payload", + "keys": [ + { + "type": [ + "number" + ], + "name": "user", + "parent": "payload" + } + ] + }, + { + "type": [ + "object" + ], + "name": "condition" + } + ], + "returns": [ + { + "type": [ + "object" + ], + "description": "with user as key" + } + ] + } + }, + "auth": { + "login": { + "description": "Auth method", + "params": [ + { + "type": [ + "string" + ], + "name": "username", + "description": "user name" + }, + { + "type": [ + "string" + ], + "name": "password", + "description": "password" + } + ], + "returns": [ + { + "type": [ + "string", + "boolean" + ], + "description": "token on success, false on fail" + } + ] + }, + "logout": { + "description": false, + "params": [], + "returns": [ + { + "type": [ + "boolean" + ], + "description": "just return something" + } + ] + } + }, + "timestamp": 1573116438, + "socket": { + "callSub": { + "namespace": "jsonql/private", + "description": "Test interface to try to call the sub using socket client", + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "a message" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "a reply from the sub" + } + ] + } + } +} diff --git a/packages/koa/tests/fixtures/log/server8001/contract.json b/packages/koa/tests/fixtures/log/server8001/contract.json new file mode 100644 index 00000000..350fd6df --- /dev/null +++ b/packages/koa/tests/fixtures/log/server8001/contract.json @@ -0,0 +1,74 @@ +{ + "query": { + "subMsService": { + "file": "/home/joel/projects/open-source/jsonql/packages/koa/tests/fixtures/sub/resolver/query/sub-ms-service.js", + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "incoming message" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "out going message" + } + ] + } + }, + "mutation": { + "subUpdateMsService": { + "file": "/home/joel/projects/open-source/jsonql/packages/koa/tests/fixtures/sub/resolver/mutation/sub-update-ms-service.js", + "description": "create a mutation to test why the call said the payload already declared", + "params": [ + { + "type": [ + "string" + ], + "name": "payload", + "description": "incoming" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "output" + } + ] + } + }, + "auth": {}, + "timestamp": 1573116438, + "sourceType": "script", + "socket": { + "replyMsg": { + "file": "/home/joel/projects/open-source/jsonql/packages/koa/tests/fixtures/sub/resolver/socket/reply-msg.js", + "description": "expect a call from someone else then modifiy the msg and reply it", + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "incoming message" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "modified message" + } + ] + } + } +} diff --git a/packages/koa/tests/fixtures/log/server8001/public-contract.json b/packages/koa/tests/fixtures/log/server8001/public-contract.json new file mode 100644 index 00000000..f68e00da --- /dev/null +++ b/packages/koa/tests/fixtures/log/server8001/public-contract.json @@ -0,0 +1,80 @@ +{ + "query": { + "helloWorld": { + "description": "This is the stock resolver for testing purpose", + "params": [], + "returns": [ + { + "type": "string", + "description": "stock message" + } + ] + }, + "subMsService": { + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "incoming message" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "out going message" + } + ] + } + }, + "mutation": { + "subUpdateMsService": { + "description": "create a mutation to test why the call said the payload already declared", + "params": [ + { + "type": [ + "string" + ], + "name": "payload", + "description": "incoming" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "output" + } + ] + } + }, + "auth": {}, + "timestamp": 1573116438, + "socket": { + "replyMsg": { + "description": "expect a call from someone else then modifiy the msg and reply it", + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "incoming message" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "modified message" + } + ] + } + } +} diff --git a/packages/koa/tests/helpers/server-with-socket.js b/packages/koa/tests/helpers/server-with-socket.js index 5260cc7a..faf526f5 100644 --- a/packages/koa/tests/helpers/server-with-socket.js +++ b/packages/koa/tests/helpers/server-with-socket.js @@ -1,19 +1,18 @@ // for testing the server with socket const { join } = require('path') const server = require('server-io-core') -const { jsonqlKoa } = require('../../main') - const jsonqlWsServer = require('jsonql-ws-server') +const { jsonqlKoa } = require('../../main') + +const debug = require('debug')('jsonql-koa:fixtures:server-with-socket') +const baseDir = join(__dirname, '..', 'fixtures') // output module.exports = function serverWithSocket(port = 8001, extra = {}) { const config = Object.assign({ - resolverDir: join(__dirname,'resolvers'), - // resuse this contract if there is one - contractDir: join(__dirname,'contract','server-with-auth'), - keysDir: join(__dirname, 'keys'), - enableAuth: true + keysDir: join(baseDir, 'keys') }, extra) + debug(`Server ${port}`, config) return Promise.resolve( server({ port: port, @@ -27,6 +26,6 @@ module.exports = function serverWithSocket(port = 8001, extra = {}) { }) ).then(({webserver, stop}) => ( jsonqlWsServer(config, webserver) - .then(() => ({stop, config, port})) + .then(() => ({ stop })) )) } diff --git a/packages/koa/tests/node-client.test.js b/packages/koa/tests/node-client.test.js index efffe27f..503cb39b 100644 --- a/packages/koa/tests/node-client.test.js +++ b/packages/koa/tests/node-client.test.js @@ -5,47 +5,66 @@ const fsx = require('fs-extra') const debug = require('debug')('jsonql-koa:test:node-client') const nodeClient = require('jsonql-node-client') // setup -const hello = require('./helpers/hello') + const baseDir = join(__dirname, 'fixtures') -const createServer = require('./helpers/server') +const createServer = require('./helpers/server-with-socket') const clientContractDir = join(__dirname, 'fixtures', 'tmp', 'client6002') -const port = 6002; -const dir = 'server6002'; -const msPort = 8001; -const startSubServer = require('./helpers/sub-server') +const baseServerPort = 7001; +const msServerPort = 8001; + +const baseContractDir = join(baseDir, 'log', `server${baseServerPort}`) +const msContractDir = join(baseDir, 'log', `server${msServerPort}`) // base test setup test.before(async t => { - - t.context.baseApp = createServer({ - name: `server${port}`, + t.context.baseApp = await createServer(baseServerPort, { + resolverDir: join(baseDir, 'resolvers'), + contractDir: baseContractDir, + enableAuth: true, + name: `server${baseServerPort}`, + socketServerType: 'ws', clientConfig: [{ - hostname: `http://localhost:${msPort}`, - name: 'client0' + hostname: `http://localhost:${msServerPort}`, + name: 'client0', + serverType: 'ws' }] - }, dir) - t.context.baseServer = t.context.baseApp.listen(port) - - const { app, stop } = startSubServer(msPort) - - t.context.app = app - t.context.stop = stop + }) + t.context.msApp = await createServer(msServerPort, { + resolverDir: join(baseDir, 'sub', 'resolver'), + contractDir: msContractDir, + socketServerType: 'ws' + }) }) test.after(t => { - t.context.stop() - t.context.baseServer.close() - fsx.removeSync(clientContractDir) - fsx.removeSync(join(baseDir, 'tmp', dir)) + + t.context.baseApp.stop() + t.context.msApp.stop() + + // fsx.removeSync(baseContractDir) + // fsx.removeSync(msContractDir) }) -test.skip(`First test both server is running`, async t => { - const res1 = await hello(t.context.baseApp) - t.is(res1.status, 200) - const res2 = await hello(t.context.app) - t.is(res2.status, 200) +test(`First test server ${baseServerPort} and ${msServerPort} are running`, async t => { + + // test client + const baseAppClient = await nodeClient({ + hostname: `http://localhost:${baseServerPort}`, + serverType: 'ws' + }) + + const msAppClient = await nodeClient({ + hostname: `http://localhost:${msServerPort}`, + serverType: 'ws' + }) + + const res1 = await baseAppClient.query.helloWorld() + t.truthy(res1) + + const res2 = await msAppClient.query.helloWorld() + t.truthy(res2) }) test.skip(`First test calling the ${port} directly with the mutation call`, async t => { @@ -57,7 +76,7 @@ test.skip(`First test calling the ${port} directly with the mutation call`, asyn t.truthy(result.indexOf(`ms service`)) }) -test(`It should able to call a resolver that access another ms`, async t => { +test.skip(`It should able to call a resolver that access another ms`, async t => { // the problem now is calling the 6001 but received the 8001 public contract? const client = await nodeClient({ hostname: `http://localhost:${port}`, diff --git a/packages/node-client/index.js b/packages/node-client/index.js index 0f374bd8..a182d7c6 100755 --- a/packages/node-client/index.js +++ b/packages/node-client/index.js @@ -16,7 +16,7 @@ const debug = require('debug')('jsonql-node-client:main') * @param {object} config configuration the only require field is hostname * @return {object} promise to resolve the jsonql node client */ -module.exports = function(config) { +module.exports = function jsonqlNodeClient(config) { // make a copy const rawConfig = Object.assign({}, config) // start the generator -- Gitee From 90843f64a2a9fd60efd42fa50c6e09a88120ada0 Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 7 Nov 2019 18:36:38 +0800 Subject: [PATCH 6/9] Add a new debug output to see why when init multiple clients could cause error --- packages/koa/tests/node-client.test.js | 36 +++++++++-------- packages/node-client/package.json | 2 +- .../node-client/src/create-socket-client.js | 39 +++++++++++-------- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/packages/koa/tests/node-client.test.js b/packages/koa/tests/node-client.test.js index 503cb39b..406c90c8 100644 --- a/packages/koa/tests/node-client.test.js +++ b/packages/koa/tests/node-client.test.js @@ -36,6 +36,19 @@ test.before(async t => { contractDir: msContractDir, socketServerType: 'ws' }) + + // now init the clients connection + + t.context.baseAppClient = await nodeClient({ + hostname: `http://localhost:${baseServerPort}`, + serverType: 'ws' + }) + + t.context.msAppClient = await nodeClient({ + hostname: `http://localhost:${msServerPort}`, + serverType: 'ws' + }) + }) test.after(t => { @@ -49,28 +62,17 @@ test.after(t => { test(`First test server ${baseServerPort} and ${msServerPort} are running`, async t => { - // test client - const baseAppClient = await nodeClient({ - hostname: `http://localhost:${baseServerPort}`, - serverType: 'ws' - }) - - const msAppClient = await nodeClient({ - hostname: `http://localhost:${msServerPort}`, - serverType: 'ws' - }) - - const res1 = await baseAppClient.query.helloWorld() + const res1 = await t.context.baseAppClient.query.helloWorld() t.truthy(res1) - const res2 = await msAppClient.query.helloWorld() + const res2 = await t.context.msAppClient.query.helloWorld() t.truthy(res2) }) -test.skip(`First test calling the ${port} directly with the mutation call`, async t => { +test.skip(`First test calling the ${baseServerPort} directly with the mutation call`, async t => { const client = await nodeClient({ - hostname: `http://localhost:${msPort}`, - contractDir: join(__dirname, 'fixtures', 'tmp', `client${msPort}`) + hostname: `http://localhost:${msServerPort}`, + contractDir: join(__dirname, 'fixtures', 'tmp', `client${msServerPort}`) }) const result = await client.query.subMsService('testing') t.truthy(result.indexOf(`ms service`)) @@ -79,7 +81,7 @@ test.skip(`First test calling the ${port} directly with the mutation call`, asyn test.skip(`It should able to call a resolver that access another ms`, async t => { // the problem now is calling the 6001 but received the 8001 public contract? const client = await nodeClient({ - hostname: `http://localhost:${port}`, + hostname: `http://localhost:${baseServerPort}`, contractDir: clientContractDir }) diff --git a/packages/node-client/package.json b/packages/node-client/package.json index aa76224f..7305b20e 100755 --- a/packages/node-client/package.json +++ b/packages/node-client/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-node-client", - "version": "1.2.0", + "version": "1.2.1", "description": "jsonql node.js client", "main": "index.js", "scripts": { diff --git a/packages/node-client/src/create-socket-client.js b/packages/node-client/src/create-socket-client.js index 6547c5c1..f5ca06c3 100644 --- a/packages/node-client/src/create-socket-client.js +++ b/packages/node-client/src/create-socket-client.js @@ -35,24 +35,29 @@ function getSocketClient(serverType) { * @return {object} the modified client object if socketClientType presented */ function createSocketClient(client, config, contract, rawConfig) { - const { serverType } = config; - if (serverType) { - // need to pass the eventEmitter here to the config - rawConfig.eventEmitter = client.eventEmitter() - // also need to pass the contract - rawConfig.contract = contract; - const socketClient = getSocketClient(serverType) - // do our thing here - // @TODO need to check when we call the login method from - // the http client, is it triggering the same login event to the - // socket client - return socketClient(rawConfig) - .then(socketCalls => { - client[SOCKET_NAME] = socketCalls - return client; - }) + try { + const { serverType } = config; + if (serverType) { + // need to pass the eventEmitter here to the config + rawConfig.eventEmitter = client.eventEmitter() + // also need to pass the contract + rawConfig.contract = contract; + const socketClient = getSocketClient(serverType) + // do our thing here + // @TODO need to check when we call the login method from + // the http client, is it triggering the same login event to the + // socket client + return socketClient(rawConfig) + .then(socketCalls => { + client[SOCKET_NAME] = socketCalls + return client; + }) + } + return client; + } catch(e) { + debug('createSocketClient', client) + throw new JsonqlError('createSocketClient', e) } - return client; } module.exports = { createSocketClient } -- Gitee From 3a318d6a0d3c3e2629d8b9f6022882619b7845ac Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 7 Nov 2019 18:39:50 +0800 Subject: [PATCH 7/9] close the generator scope --- packages/node-client/src/generator.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/node-client/src/generator.js b/packages/node-client/src/generator.js index 208c8a20..596c362a 100755 --- a/packages/node-client/src/generator.js +++ b/packages/node-client/src/generator.js @@ -9,7 +9,7 @@ const debug = getDebug('generator') * @param {object} contract the static contract * @return {object} contract may be from server */ -const getContract = function(jsonqlInstance, contract = {}) { +function getContract(jsonqlInstance, contract = {}) { if (contract && (contract.query || contract.mutation)) { return Promise.resolve(contract) } @@ -23,7 +23,7 @@ const getContract = function(jsonqlInstance, contract = {}) { * @param {object} contract to match * @return {function} for use */ -const authMethodGenerator = (jsonqlInstance, name, opts, contract) => { +function authMethodGenerator(jsonqlInstance, name, opts, contract) { return (...args) => { const params = contract.auth[name].params; const values = params.map((p, i) => args[i]); @@ -45,7 +45,7 @@ const authMethodGenerator = (jsonqlInstance, name, opts, contract) => { * @param {object} contract * @return {object} constructed functions call */ -const generator = (jsonqlInstance, config, contract) => { +function generator(jsonqlInstance, config, contract) { let obj = {query: {}, mutation: {}, auth: {}}; // process the query first for (let queryFn in contract.query) { @@ -110,7 +110,6 @@ const generator = (jsonqlInstance, config, contract) => { } // the eventEmitter getter obj.eventEmitter = () => jsonqlInstance.eventEmitter - // we have to make this into a function if I want to // need to create a getter method for it otherwise // it's straight through pass to the instance property -- Gitee From aaa927f4643bc30ebb7e344323a9325a6888d365 Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 7 Nov 2019 21:20:28 +0800 Subject: [PATCH 8/9] add a time it took to see the extra generator speed --- packages/contract-cli/extra.js | 9 +++++++-- packages/contract-cli/package.json | 6 +++--- packages/node-client/package.json | 2 +- packages/node-client/tests/fixtures/jsonql-koa.js | 2 +- packages/node-client/tests/fixtures/jwt/contract.json | 2 +- packages/node-client/tests/fixtures/server.js | 2 +- packages/node-client/tests/main.test.js | 8 ++++++++ 7 files changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/contract-cli/extra.js b/packages/contract-cli/extra.js index edde258a..213bff72 100644 --- a/packages/contract-cli/extra.js +++ b/packages/contract-cli/extra.js @@ -8,7 +8,7 @@ const nbSplitTasks = require('nb-split-tasks') const contractApi = require('./index') const { DEFAULT_CONTRACT_FILE_NAME, PUBLIC_CONTRACT_FILE_NAME } = require('jsonql-constants') -const debug = require('debug')('jsonql-contract:contract-generator') +const debug = require('debug')('jsonql-contract:extra') /** * @param {string} contractDir where the contract is * @param {boolean} pub system of public @@ -52,11 +52,16 @@ const contractGenerator = function(opts, pub = false, start = false) { * @return {promise} resolve the contract */ const splitContractGenerator = function(config, pub = false) { + const startTime = Date.now() const pathToFn = join(__dirname, 'split.js') const args = [config, pub] const returnType = config.returnAs === 'json' ? 'object' : 'array' return nbSplitTasks(pathToFn, [args], returnType) - .then(contract => returnType === 'array' ? contract[0] : contract) + .then(contract => { + let endTime = Date.now() - startTime; + debug(`It took ${Math.round(endTime/1000)}`) + return returnType === 'array' ? contract[0] : contract + }) } diff --git a/packages/contract-cli/package.json b/packages/contract-cli/package.json index 9c383f62..706ffc4a 100755 --- a/packages/contract-cli/package.json +++ b/packages/contract-cli/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-contract", - "version": "1.8.3", + "version": "1.8.4", "description": "JS API / command line tool to generate the contract.json for jsonql", "main": "index.js", "files": [ @@ -49,7 +49,7 @@ "colors": "^1.4.0", "debug": "^4.1.1", "fs-extra": "^8.1.0", - "glob": "^7.1.5", + "glob": "^7.1.6", "jsdoc-api": "^5.0.4", "jsonql-constants": "^1.8.9", "jsonql-errors": "^1.1.5", @@ -57,7 +57,7 @@ "jsonql-utils": "^0.8.3", "kefir": "^3.8.6", "lodash": "^4.17.15", - "nb-split-tasks": "^0.5.3", + "nb-split-tasks": "^0.6.0", "yargs": "^14.2.0" }, "devDependencies": { diff --git a/packages/node-client/package.json b/packages/node-client/package.json index 7305b20e..925e9c30 100755 --- a/packages/node-client/package.json +++ b/packages/node-client/package.json @@ -8,7 +8,7 @@ "test": "ava --verbose", "test:debug": "DEBUG=jsonql-node-client*,jsonql-contract* ava --verbose", "test:nyc": "nyc ava --verbose", - "test:main": "DEBUG=jsonql-node-client* ava tests/main.test.js", + "test:main": "DEBUG=jsonql-* ava tests/main.test.js", "test:auth": "DEBUG=jsonql-* ava tests/auth.test.js", "test:valid": "DEBUG=jsonql-node-client* ava tests/validation.test.js", "test:config": "DEBUG=jsonql-node-client* ava tests/config.test.js", diff --git a/packages/node-client/tests/fixtures/jsonql-koa.js b/packages/node-client/tests/fixtures/jsonql-koa.js index fcc96ec8..3fcd5166 100644 --- a/packages/node-client/tests/fixtures/jsonql-koa.js +++ b/packages/node-client/tests/fixtures/jsonql-koa.js @@ -1,3 +1,3 @@ -const { jsonqlKoa } = require('jsonql-koa') +const { jsonqlKoa } = require('../../../koa/main' /*'jsonql-koa'*/) module.exports = jsonqlKoa; diff --git a/packages/node-client/tests/fixtures/jwt/contract.json b/packages/node-client/tests/fixtures/jwt/contract.json index 94270e3a..3f03c9cd 100644 --- a/packages/node-client/tests/fixtures/jwt/contract.json +++ b/packages/node-client/tests/fixtures/jwt/contract.json @@ -102,7 +102,7 @@ ] } }, - "timestamp": 1573028878, + "timestamp": 1573132736, "sourceType": "script", "socket": { "gateway": { diff --git a/packages/node-client/tests/fixtures/server.js b/packages/node-client/tests/fixtures/server.js index f6a26d31..95713e39 100755 --- a/packages/node-client/tests/fixtures/server.js +++ b/packages/node-client/tests/fixtures/server.js @@ -14,5 +14,5 @@ module.exports = function(port = 8888) { contractDir: join(__dirname,'contract','tmp' , 'server') }) ] - }); + }) } diff --git a/packages/node-client/tests/main.test.js b/packages/node-client/tests/main.test.js index 183a9390..012aa4bf 100755 --- a/packages/node-client/tests/main.test.js +++ b/packages/node-client/tests/main.test.js @@ -11,16 +11,24 @@ test.before(async t => { const { stop } = await server() t.context.stop = stop; // create client + t.context.client = await nodeClient({ hostname:'http://localhost:8888', contractDir: join(__dirname,'fixtures','contract','tmp', 'client') }) + }) test.after(async t => { t.context.stop() }) +/* +test(`Just to cause the jsonql-koa to run`, t => { + t.pass() +}) +*/ + test('Should able to say Hello world!' , async t => { // debug(t.context.client); const result = await t.context.client.query.helloWorld() -- Gitee From 096556bd30eb220869151e4c2c30560e258ecc4c Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 7 Nov 2019 21:27:20 +0800 Subject: [PATCH 9/9] Add a console.time and console.timeEnd to the extra generator to check out the time it took to generate the contract --- packages/contract-cli/extra.js | 9 +++++---- packages/contract-cli/test.js | 6 ++++-- packages/node-client/tests/fixtures/jwt/contract.json | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/contract-cli/extra.js b/packages/contract-cli/extra.js index 213bff72..83d240dc 100644 --- a/packages/contract-cli/extra.js +++ b/packages/contract-cli/extra.js @@ -52,19 +52,20 @@ const contractGenerator = function(opts, pub = false, start = false) { * @return {promise} resolve the contract */ const splitContractGenerator = function(config, pub = false) { - const startTime = Date.now() + console.time('generator') const pathToFn = join(__dirname, 'split.js') const args = [config, pub] const returnType = config.returnAs === 'json' ? 'object' : 'array' return nbSplitTasks(pathToFn, [args], returnType) .then(contract => { - let endTime = Date.now() - startTime; - debug(`It took ${Math.round(endTime/1000)}`) + const d = process.env.DEBUG + if (d && d.indexOf('jsonql-contract:extra') > -1) { + console.timeEnd('generator') + } return returnType === 'array' ? contract[0] : contract }) } - // export module.exports = { contractGenerator, diff --git a/packages/contract-cli/test.js b/packages/contract-cli/test.js index d3dc2a04..b5e03d08 100644 --- a/packages/contract-cli/test.js +++ b/packages/contract-cli/test.js @@ -14,7 +14,7 @@ watcher({ jsType: 'cjs' }); */ - +console.time('test') let startBase, fullYear; const increase = (base, percent, year, target) => { @@ -36,4 +36,6 @@ const increase = (base, percent, year, target) => { } } -increase(40000, 8, 20, 1000000); +increase(40000, 8, 20, 1000000) + +console.timeEnd('test') diff --git a/packages/node-client/tests/fixtures/jwt/contract.json b/packages/node-client/tests/fixtures/jwt/contract.json index 3f03c9cd..803b3982 100644 --- a/packages/node-client/tests/fixtures/jwt/contract.json +++ b/packages/node-client/tests/fixtures/jwt/contract.json @@ -102,7 +102,7 @@ ] } }, - "timestamp": 1573132736, + "timestamp": 1573133008, "sourceType": "script", "socket": { "gateway": { -- Gitee