diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json index 77212674c05521d134accb8d8b5e1071cca233c4..30fcfdf6c6c4c65194e7f441830d8b24a845c306 100644 --- a/packages/@jsonql/koa/package.json +++ b/packages/@jsonql/koa/package.json @@ -53,7 +53,7 @@ "debug": "^4.1.1", "fs-extra": "^8.1.0", "jsonql-constants": "^1.8.10", - "jsonql-koa": "^1.4.16", + "jsonql-koa": "^1.4.17", "jsonql-params-validator": "^1.4.11", "koa": "^2.11.0", "koa-bodyparser": "^4.2.1", diff --git a/packages/@jsonql/koa/tests/fixtures/resolvers/query/get-something.js b/packages/@jsonql/koa/tests/fixtures/resolvers/query/get-something.js index cbfa8b7d329dc50c391ea3017a336adb085c3c8e..0529c38c847c62ee7103ff63348e5a89601c522d 100644 --- a/packages/@jsonql/koa/tests/fixtures/resolvers/query/get-something.js +++ b/packages/@jsonql/koa/tests/fixtures/resolvers/query/get-something.js @@ -3,6 +3,9 @@ * just a pass over method to get something from server b * @return {object} an object contains two different time stamps */ -module.exports = function getSomething() { - const client = getSomething.client() +module.exports = async function getSomething() { + const client = await getSomething.client() + // just pass it straight through + const msg = await client.query.sendingOutSomething() + return `${msg} via getSomething()` } diff --git a/packages/@jsonql/koa/tests/fixtures/resolvers/socket/ms-pass.js b/packages/@jsonql/koa/tests/fixtures/resolvers/socket/ms-pass.js index 11b2dc34564906aa5efd562eb6864e8b2d71208a..1fc2fea24312d825f9baf330cd08ad6c1bcbd70b 100644 --- a/packages/@jsonql/koa/tests/fixtures/resolvers/socket/ms-pass.js +++ b/packages/@jsonql/koa/tests/fixtures/resolvers/socket/ms-pass.js @@ -1,9 +1,20 @@ +const debug = require('debug')('jsonql-koa:test:basic') /** * just a pass over method to call server B * @param {string} msg an incoming message * @return {object} contain everything and send it back */ module.exports = function msPass(msg) { - // then we call the client and get something from it - + try { + // then we call the client and get something from it + const client = msPass.client() + + return client.socket.giveNumber() + .then(result => { + return `${msg} received on ${result}` + }) + } catch(e) { + debug(e) + throw new Error(e) + } } diff --git a/packages/@jsonql/koa/tests/ms.test.js b/packages/@jsonql/koa/tests/ms.test.js index 4aa56eb6ee139faad00be722d54f5a51e6ad68fb..108cefbf317dcaf83e7f333c5023d89d020fcc4b 100644 --- a/packages/@jsonql/koa/tests/ms.test.js +++ b/packages/@jsonql/koa/tests/ms.test.js @@ -15,12 +15,11 @@ const createMsTestServer = require('./fixtures/ms-test-servers') const getClientA = async () => ( await jsonqlNodeClient({ hostname: `http://localhost:${PORT_A}`, - contractDir: join(__dirname, 'fixtures', 'contract', 'client1') + contractDir: join(__dirname, 'fixtures', 'contract', 'client1'), + serverType: 'ws' }) ) - - test.before(t => { t.context.serverA = createMsTestServer('A') t.context.serverB = createMsTestServer('B') @@ -54,10 +53,29 @@ test(`It should able to connect to server B directly`, async t => { test(`It should able to connect to another service via the internal nodeClient`, async t => { const client1 = await getClientA() - - + const msg = await client1.query.getSomething() + debug(msg) + t.truthy(msg) }) -test.todo(`It should able to connect to the another client via socket connection`) +test.cb.only(`It should able to connect to the another client via socket connection`, t => { + t.plan(2) + getClientA() + .then(client => { + + t.pass() // should get a client here + + client.socket.msPass(`Something`) + .then(result => { + debug(result) + t.truthy(result) + t.end() + }) + }) + .catch(err => { + debug('test error!', err) + }) + +}) diff --git a/packages/constants/README.md b/packages/constants/README.md index 90ba71ca4a4b3f1b3b1b56bc375ac91fa8d05e00..2bd7caf9b632c465a628334264345343a9f328d0 100755 --- a/packages/constants/README.md +++ b/packages/constants/README.md @@ -117,6 +117,8 @@ non-javascript to develop your tool. You can also use the included `constants.js - LOGIN_PROP_NAME - CLIENT_PROP_NAME - USERDATA_PROP_NAME +- INIT_CLIENT_PROP_KEY +- INIT_CONTRACT_PROP_KEY - DEFAULT_WS_WAIT_TIME - TIMEOUT_ERR_MSG - NOT_LOGIN_ERR_MSG diff --git a/packages/constants/constants.json b/packages/constants/constants.json index 5d7d8319dcfe0d38bd12c721130b6cf9d907578b..7cc280c35f6684afc548fed7dd93344ccba48e1f 100644 --- a/packages/constants/constants.json +++ b/packages/constants/constants.json @@ -148,6 +148,8 @@ "LOGIN_PROP_NAME": "login", "CLIENT_PROP_NAME": "client", "USERDATA_PROP_NAME": "userdata", + "INIT_CLIENT_PROP_KEY": "nodeClient", + "INIT_CONTRACT_PROP_KEY": "initContract", "DEFAULT_WS_WAIT_TIME": 5000, "TIMEOUT_ERR_MSG": "timeout", "NOT_LOGIN_ERR_MSG": "NOT LOGIN", diff --git a/packages/constants/main.js b/packages/constants/main.js index 6d1b79d65a2d93077db343a4fff7b9312900297a..6b493cd51f89c4a8f55b6bb333b2b0dbb56fd0c6 100644 --- a/packages/constants/main.js +++ b/packages/constants/main.js @@ -148,6 +148,8 @@ module.exports = { "LOGIN_PROP_NAME": "login", "CLIENT_PROP_NAME": "client", "USERDATA_PROP_NAME": "userdata", + "INIT_CLIENT_PROP_KEY": "nodeClient", + "INIT_CONTRACT_PROP_KEY": "initContract", "DEFAULT_WS_WAIT_TIME": 5000, "TIMEOUT_ERR_MSG": "timeout", "NOT_LOGIN_ERR_MSG": "NOT LOGIN", diff --git a/packages/constants/module.js b/packages/constants/module.js index 29f56d0d27b7d70ec7aea75b990061358f6b43ed..3c5213db570a25ca60153909027192524512a708 100644 --- a/packages/constants/module.js +++ b/packages/constants/module.js @@ -159,6 +159,10 @@ export const LOGIN_PROP_NAME = 'login'; // new @1.8.6 // this one is for nodeClient inject into the resolver export const CLIENT_PROP_NAME = 'client'; export const USERDATA_PROP_NAME = 'userdata'; +// this are property keys used in the configuration options +// and re-use elsewhere +export const INIT_CLIENT_PROP_KEY = 'nodeClient'; +export const INIT_CONTRACT_PROP_KEY = 'initContract'; // this is the default time to wait for reply if exceed this then we // trigger an error --> 5 seconds diff --git a/packages/constants/package.json b/packages/constants/package.json index c6376990d86343d92574a78af68cdae75a638744..e77c5e21f88c38a2eff459ccabe747c9346fa0ae 100755 --- a/packages/constants/package.json +++ b/packages/constants/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-constants", - "version": "1.8.10", + "version": "1.8.11", "description": "All the share constants for json:ql tools", "main": "main.js", "module": "module.js", diff --git a/packages/koa/package.json b/packages/koa/package.json index c959c227dc5d29cbd4343f984c578e29cdda97df..43399f4964a1c98503436e16fe2720b73c60cb43 100644 --- a/packages/koa/package.json +++ b/packages/koa/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-koa", - "version": "1.4.17", + "version": "1.4.18", "description": "jsonql Koa middleware", "main": "main.js", "module": "index.js", @@ -80,7 +80,7 @@ "jsonql-jwt": "^1.3.4", "jsonql-node-client": "^1.2.4", "jsonql-params-validator": "^1.4.11", - "jsonql-resolver": "^0.9.7", + "jsonql-resolver": "^0.9.8", "jsonql-utils": "^0.8.5", "jsonql-web-console": "^0.4.3", "koa-compose": "^4.1.0", diff --git a/packages/koa/src/middlewares/core-middleware.js b/packages/koa/src/middlewares/core-middleware.js index fdf8c607d8082077973ac846e83f7ab0461984ac..22a73d3e7a781a1e87ab14cc6d8683cc9d2cb7c8 100644 --- a/packages/koa/src/middlewares/core-middleware.js +++ b/packages/koa/src/middlewares/core-middleware.js @@ -1,6 +1,6 @@ // The core of the jsonql middleware import { QUERY_NAME, MUTATION_NAME } from 'jsonql-constants' -import { resolveMethod } from 'jsonql-resolver' +import { resolverRenderHandler } from 'jsonql-resolver' import { getDebug } from '../utils' const debug = getDebug('core') @@ -19,7 +19,7 @@ export default function coreMiddleware(opts) { // debug(`Is jsonql query`, contract, opts) // The problem is - the config is correct // but the contract return the previous one - return resolveMethod(ctx, resolverType, opts, contract) + return resolverRenderHandler(ctx, resolverType, opts, contract) } } else { await next() diff --git a/packages/koa/src/options/index.js b/packages/koa/src/options/index.js index e63c9edb2aa11d905f53e108d8050bb8c6253118..9f52a9f9f192ab69b4b7ab701b0261a4f005a5c3 100644 --- a/packages/koa/src/options/index.js +++ b/packages/koa/src/options/index.js @@ -1,4 +1,7 @@ // wrap all the options and method in one instead of all over the places +// @TODO all this should move to the jsonql-utils because when start dev for other +// framework this will get re-use + import { join, resolve } from 'path' import fsx from 'fs-extra' import _ from 'lodash' @@ -6,13 +9,14 @@ import _ from 'lodash' const { CHECKED_KEY } = require('jsonql-constants') import { checkConfig, isString } from 'jsonql-params-validator' import { rsaPemKeys } from 'jsonql-jwt' - +import { validateClientConfig, clientsGenerator } from 'jsonql-resolver' import { appProps, constProps, jwtProcessKey } from './options' import { isContract, chainFns, getDebug, inArray, injectToFn, objHasProp } from '../utils' import { getContract } from '../contracts' const debug = getDebug('config-check') /** + * @TODO move to jsonql-utils * break out from the applyAuthOptions because it's not suppose to be there * @NOTE v1.3.8 change it to a fully functional interface * @param {object} config configuration @@ -49,12 +53,13 @@ const applyGetContract = function(config) { } /** + * @TODO move to jsonql-utils * we need an extra step to cache some of the auth related configuration data - * ASYNC AWAIT IS A FUCKING JOKE * @param {object} config configuration * @return {object} config with extra property */ const applyAuthOptions = function(config) { + // @TODO need to get rip of the useJwt option! if (config.enableAuth && config.useJwt && !isString(config.useJwt)) { const { keysDir, publicKeyFileName, privateKeyFileName } = config; const publicKeyPath = join(keysDir, publicKeyFileName) @@ -70,6 +75,20 @@ const applyAuthOptions = function(config) { return config; } +/** + * @1.4.18 pre-config the node client is there is a configuration for it + * this will only call when using preConfigCheck + * @param {object} config the main configuration + * @return {object} the modified configuration if there is need for it + */ +const initNodeClient = function(config) { + + + + // @TODO + return config; +} + /** * @param {object} config configuration supply by developer * @return {object} configuration been checked @@ -99,7 +118,7 @@ export function preConfigCheck(wsDefaultOptions, wsConstProps, config = {}) { const _constProps = _.merge({}, wsConstProps, constProps) const now = Date.now() - const fn = chainFns(checkConfig, applyGetContract, applyAuthOptions) + const fn = chainFns(checkConfig, applyGetContract, applyAuthOptions, initNodeClient) const opts = fn(config, _defaultOptions, _constProps) // return opts; return injectToFn(opts, CHECKED_KEY, now) diff --git a/packages/koa/src/utils/jsonql-struct.js b/packages/koa/src/utils/jsonql-struct.js deleted file mode 100644 index 4c69c8e5e5b78fccf9bd9a1eb1fdb89e323ad163..0000000000000000000000000000000000000000 --- a/packages/koa/src/utils/jsonql-struct.js +++ /dev/null @@ -1,22 +0,0 @@ -// this is a struct like class to hold the property that we don't want to get overwrite - -export default class JsonqlStruct { - - constructor() { - this.__private__ = new Map() - } - - set(key, value) { - if (this.__private__.has(key)) { - console.error(`${key} already existed! overwrite with`, value) - } - this.__private__.set(key, value) - } - - get contract() { - return this.__private__.get('contract') - } - - - -} diff --git a/packages/koa/tests/jsonql.test.js b/packages/koa/tests/jsonql.test.js deleted file mode 100644 index 9b604f6e5a93de138444b5d2e322c1d6919ea74c..0000000000000000000000000000000000000000 --- a/packages/koa/tests/jsonql.test.js +++ /dev/null @@ -1,14 +0,0 @@ -// testing the new jsonqlStruct class - -const test = require('ava') - -const JsonqlStruct = require('../src/utils/jsonql-struct') - -test.before(t => { - - t.context.cls = new JsonqlStruct() -}) - -test.todo(`It should able to set some value`) - -test.todo(`It should not able to overwrite some value`) diff --git a/packages/resolver/index.js b/packages/resolver/index.js index 401c3eb9cf711673d11a33f8de3f8435ec9d5170..6c1add105dcd9f280ee00fd186b2a3eeaa729845 100644 --- a/packages/resolver/index.js +++ b/packages/resolver/index.js @@ -10,6 +10,16 @@ const { getLocalValidator, handleAuthMethods } = require('./src/handle-auth-methods') +// for 1.0.0 release (first on 0.9.8) +// we also export the node client generator parts +// when the middleware is use with socket server +// the client(s) will get get pre-config and init during the +// check option phrase then it could be re-use between modules +const { + injectNodeClient, + validateClientConfig, + clientsGenerator +} = require('./src/client') // @TODO use the same for the jsonql-ws-server as well module.exports = { @@ -20,6 +30,11 @@ module.exports = { getLocalValidator, executeResolver, resolverRenderHandler, + // for node clients + injectNodeClient, + validateClientConfig, + clientsGenerator // create an alias for integration in Koa middleware - resolveMethod: resolverRenderHandler + // remove in 0.9.8 + // resolveMethod: resolverRenderHandler } diff --git a/packages/resolver/package.json b/packages/resolver/package.json index 5bfbea0d7a4773f0b664bbff84f0f968d63a78d6..922e6b202bc3a2198b3cd38c54b4c9134f245303 100644 --- a/packages/resolver/package.json +++ b/packages/resolver/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-resolver", - "version": "0.9.7", + "version": "0.9.8", "description": "This is NOT for general use, please do not install it directly. This module is part of the jsonql tools supporting modules.", "main": "index.js", "files": [ @@ -38,7 +38,7 @@ "devDependencies": { "ava": "^2.4.0", "jsonql-contract": "^1.8.4", - "jsonql-koa": "^1.4.16", + "jsonql-koa": "^1.4.17", "server-io-core": "^1.2.0" }, "ava": { diff --git a/packages/resolver/src/client/clients-generator.js b/packages/resolver/src/client/clients-generator.js index bc6fcd5b058c01a65638a8ca974fbaebc825dab0..2aa64dffcdb8e8a0106e19aeb3dd96a1855a3245 100755 --- a/packages/resolver/src/client/clients-generator.js +++ b/packages/resolver/src/client/clients-generator.js @@ -2,20 +2,19 @@ // if they are not using it then is it possible to include this as // peer dependencies? const jsonqlNodeClient = require('jsonql-node-client') +const { injectToFn } = require('jsonql-utils') +const NAME_PROP = 'name' /** * create the node client using the already passed configurations - * @param {object} opts the clients configuration + * @param {object} clientConfigs the clients configuration * @return {promise} resolve to the node client */ -function clientsGenerator(opts) { +function clientsGenerator(clientConfigs) { return Promise.all( - opts.map(opt => { - let name = opt.name; - return jsonqlNodeClient(opt) - .then(client => { - client.name = name; - return client; - }) + clientConfigs.map(clientConfig => { + let name = clientConfig.name; + return jsonqlNodeClient(clientConfig) + .then(client => injectToFn(client, NAME_PROP, name)) }) ) } diff --git a/packages/resolver/src/provide-node-clients.js b/packages/resolver/src/provide-node-clients.js index 1275e65a3856f512bbde5d06cc135c902e29c844..714d47579b47db655bea290e0550334d33651c21 100644 --- a/packages/resolver/src/provide-node-clients.js +++ b/packages/resolver/src/provide-node-clients.js @@ -6,9 +6,16 @@ const { } = require('./client') const { getDebug } = require('./utils') const debug = getDebug('provide-node-clients') +// we need to change this to a class instead otherwise +// we might overwrite each others let clients = [] let hasClientConfig; +// @NOTE 0.9.8 before publish +// this part will have to completely rewrite to fix +// 1. when the client already init elsewhere +// 2. can not use just an array here, it cause problem when it's more than one client + /** * The top level methods to inject the clients into the resolver * @param {function} resolver the function to get injection diff --git a/packages/resolver/tests/fixtures/contract/es/contract.json b/packages/resolver/tests/fixtures/contract/es/contract.json index 30e4846f3333b512ee5f1a972e0205df907a9828..9fc01312df44768ec8227554970581bbdb8916ae 100644 --- a/packages/resolver/tests/fixtures/contract/es/contract.json +++ b/packages/resolver/tests/fixtures/contract/es/contract.json @@ -43,6 +43,6 @@ } }, "auth": {}, - "timestamp": 1574495660, + "timestamp": 1574734426, "sourceType": "module" }