From 43ef337ab7333a65505a3b0316532d365548b80c Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 19 Mar 2020 12:56:55 +0800 Subject: [PATCH 1/3] jsonql-contract 1.9.0 --- packages/contract-cli/package.json | 4 ++-- .../src/generator/get-socket-auth-resolver.js | 17 ++++++++------- packages/contract-cli/tests/socket.test.js | 21 +++++++++---------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/contract-cli/package.json b/packages/contract-cli/package.json index 949e94f0..e3f53b61 100755 --- a/packages/contract-cli/package.json +++ b/packages/contract-cli/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-contract", - "version": "1.8.11", + "version": "1.9.0", "description": "JS API / command line tool to generate the contract.json for jsonql", "main": "index.js", "files": [ @@ -55,7 +55,7 @@ "fs-extra": "^8.1.0", "glob": "^7.1.6", "jsdoc-api": "^5.0.4", - "jsonql-constants": "^2.0.4", + "jsonql-constants": "^2.0.5", "jsonql-errors": "^1.2.1", "jsonql-params-validator": "^1.6.1", "jsonql-utils": "^1.2.4", diff --git a/packages/contract-cli/src/generator/get-socket-auth-resolver.js b/packages/contract-cli/src/generator/get-socket-auth-resolver.js index f078ce19..71e1372f 100644 --- a/packages/contract-cli/src/generator/get-socket-auth-resolver.js +++ b/packages/contract-cli/src/generator/get-socket-auth-resolver.js @@ -2,15 +2,19 @@ // these functions should not appear in the public-contract.json // its mainly for internal use const fsx = require('fs-extra') -const debug = require('debug')('jsonql-contract:generator:get-socket-auth-resolver') const { join, sep, dirname, basename } = require('path') -const { SOCKET_NAME, AUTH_NAME, EXT, INDEX_KEY } = require('jsonql-constants') +const { merge } = require('lodash') + +const { SOCKET_NAME, AUTH_NAME, EXT, INDEX_KEY, SOCKET_AUTH_NAME } = require('jsonql-constants') +const { inArray, isObjectHasKey } = require('jsonql-params-validator') + const { getResolverFiles } = require('./read-files-out-contract') -const { inArray } = require('jsonql-params-validator') const { getSourceType } = require('./get-source-type') const { parseFileToAst } = require('./parse-file-to-ast') const { chainPromises } = require('jsonql-utils') + + /** * Return the list of files from the folder first * @param {string} resolverDir where the base resolver directory @@ -130,9 +134,7 @@ function getSocketAuthResolver(config, sourceType = null, fileExt = EXT) { ) }) .then(contract => ({ - [SOCKET_NAME]: { - [AUTH_NAME]: contract - } + [SOCKET_AUTH_NAME]: contract })) } @@ -143,8 +145,9 @@ function getSocketAuthResolver(config, sourceType = null, fileExt = EXT) { * @return {object} contract with the extra bit */ function processSocketAuth(config, contract) { - if (config[SOCKET_NAME] && config.enableAuth === true) { + if (isObjectHasKey(contract, SOCKET_NAME) && config.enableAuth === true) { const { sourceType } = contract + return getSocketAuthResolver(config, sourceType) .then(partialContract => ( merge({}, contract, partialContract) diff --git a/packages/contract-cli/tests/socket.test.js b/packages/contract-cli/tests/socket.test.js index 71f6c841..b2fc6424 100644 --- a/packages/contract-cli/tests/socket.test.js +++ b/packages/contract-cli/tests/socket.test.js @@ -2,10 +2,12 @@ const test = require('ava') const { join } = require('path') const fsx = require('fs-extra') +const colors = require('colors/safe') const { SOCKET_NAME, AUTH_TYPE, RETURN_AS_JSON, + SOCKET_AUTH_NAME, DEFAULT_CONTRACT_FILE_NAME, PUBLIC_CONTRACT_FILE_NAME } = require('jsonql-constants') @@ -23,23 +25,21 @@ const contractDir = join(__dirname, 'fixtures', 'tmp', 'socket-with-auth') const baseContractFile = join(contractDir, DEFAULT_CONTRACT_FILE_NAME) const publicContractFile = join(contractDir, PUBLIC_CONTRACT_FILE_NAME) - -// const socketConfig = require('./fixtures/socket/config') const debug = require('debug')('jsonql-contract:test:socket') +const colorDebug = (str, ...args) => Reflect.apply(debug, null, [colors.black.bgBrightCyan(str)].concat(args)) test.before(async t => { t.context.config = await getConfig({ enableAuth: true, resolverDir }) }) - test.after(t => { - // fsx.removeSync(contractDir) + fsx.removeSync(contractDir) }) test.cb(`It should able to return a list of socket auth files`, t => { t.plan(1) - debug('resolverdir', resolverDir) + colorDebug('resolverdir', resolverDir) getSocketAuthResolverFiles(resolverDir) .then(files => { @@ -61,17 +61,15 @@ test.cb(`It should able to generate partial contract for socket auth`, t => { getSocketAuthResolver(t.context.config) .then(contract => { - - debug('partial contract', contract) + colorDebug('partial contract', contract) t.truthy(contract) t.end() }) - }) -test.cb.only(`It should able to generate new entry when socket / auth has content`, t => { +test.cb(`It should able to generate new entry when socket / auth has content`, t => { t.plan(1) generator({ resolverDir, @@ -80,8 +78,9 @@ test.cb.only(`It should able to generate new entry when socket / auth has conten returnAs: RETURN_AS_JSON }) .then(result => { - t.truthy(result) - debug('socket test output', result) + t.truthy(result[SOCKET_AUTH_NAME]) + + colorDebug('socket test output', result) t.end() }) }) -- Gitee From feeea9d30edcc038f6110389c120021a19118159 Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 19 Mar 2020 13:55:53 +0800 Subject: [PATCH 2/3] add back the missing FILE_PROP_KEY to constants --- packages/constants/package.json | 2 +- packages/constants/prop.js | 22 +++--- .../contract-cli/src/generator/files-op.js | 4 +- .../src/generator/get-socket-auth-resolver.js | 2 - .../contract-cli/src/public-contract/index.js | 73 +++++++++++++------ packages/contract-cli/tests/socket.test.js | 24 +++++- 6 files changed, 83 insertions(+), 44 deletions(-) diff --git a/packages/constants/package.json b/packages/constants/package.json index 2942c1b0..421e9565 100755 --- a/packages/constants/package.json +++ b/packages/constants/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-constants", - "version": "2.0.5", + "version": "2.0.6", "description": "All the share constants for jsonql modules", "main": "main.js", "module": "index.js", diff --git a/packages/constants/prop.js b/packages/constants/prop.js index 36b04e27..f28fa8b1 100644 --- a/packages/constants/prop.js +++ b/packages/constants/prop.js @@ -1,5 +1,5 @@ -// this is all the key name for the config check map -// all subfix with prop_key +// this is all the key name for the config check map +// all subfix with prop_key export const TYPE_KEY = 'type' export const OPTIONAL_KEY = 'optional' @@ -10,7 +10,7 @@ export const ALIAS_KEY = 'alias' // @TODO remove this later export const CHECKED_KEY = '__checked__' -// first part port from the ws-server-core +// first part port from the ws-server-core export const APP_DIR_PROP_KEY = 'appDir' export const AUTH_TO_PROP_KEY = 'authTimeout' @@ -28,7 +28,7 @@ export const SWITCH_USER_FN_NAME_PROP_KEY = 'switchUserHandlerName' export const PUBLIC_FN_DIR_PROP_KEY = 'publicResolverDir' export const PRIVATE_FN_DIR_DROP_KEY = 'privateResolverDir' -// socket specific +// socket specific export const ALGORITHM_PROP_KEY = 'algorithm' export const KEYS_DIR_PROP_KEY = 'keysDir' export const SOCKET_IO_AUTH_TYPE_PROP_KEY = 'socketIoAuthType' @@ -46,6 +46,7 @@ export const DEBUG_ON_PROP_KEY = 'debugOn' export const HOSTNAME_PROP_KEY = 'hostname' export const NAMESAPCE_PROP_KEY = 'namespace' +export const FILE_PROP_KEY = 'file' export const WS_OPT_PROP_KEY = 'wsOptions' @@ -83,18 +84,17 @@ export const JS_TYPE_PROP_KEY = 'jsType' export const EXPOSE_ERR_PROP_KEY = 'exposeError' export const CLIENT_CONFIG_PROP_KEY = 'clientConfig' -// this will combine all three options together -// because they are redudant if set this to true -// then whenever start up will build a fresh new contract like now -// if false then the developer has to manually build the contract +// this will combine all three options together +// because they are redudant if set this to true +// then whenever start up will build a fresh new contract like now +// if false then the developer has to manually build the contract // which is what production suppose to be export const AUTO_CONTRACT_PROP_KEY = 'autoCreateContract' -// still under development +// still under development export const VALIDATE_RETURNS_PROP_KEY = 'validateReturns' export const ENABLE_UPLOAD_PROP_KEY = 'enableFileUpload' export const FILE_UPLOAD_NAME_PROP_KEY = 'fileUploadName' export const FILE_UPLOAD_DIST_PROP_KEY = 'fileUploadDist' export const FILE_HANDLER_FN_NAME_PROP_KEY = 'fileHandlerName' -export const ENABLE_SPLIT_TASK_PROP_KEY = 'enableSplitTask' - +export const ENABLE_SPLIT_TASK_PROP_KEY = 'enableSplitTask' diff --git a/packages/contract-cli/src/generator/files-op.js b/packages/contract-cli/src/generator/files-op.js index bbb9c95f..86fe5a8b 100644 --- a/packages/contract-cli/src/generator/files-op.js +++ b/packages/contract-cli/src/generator/files-op.js @@ -56,9 +56,9 @@ const isContractExisted = function(config) { if (fsx.existsSync(dist)) { debug('[isContractExisted] found') const contract = fsx.readJsonSync(dist) - return isContract(contract) ? contract : false; + return isContract(contract) ? contract : false } - return false; + return false } // export diff --git a/packages/contract-cli/src/generator/get-socket-auth-resolver.js b/packages/contract-cli/src/generator/get-socket-auth-resolver.js index 71e1372f..6d098fb6 100644 --- a/packages/contract-cli/src/generator/get-socket-auth-resolver.js +++ b/packages/contract-cli/src/generator/get-socket-auth-resolver.js @@ -13,8 +13,6 @@ const { getSourceType } = require('./get-source-type') const { parseFileToAst } = require('./parse-file-to-ast') const { chainPromises } = require('jsonql-utils') - - /** * Return the list of files from the folder first * @param {string} resolverDir where the base resolver directory diff --git a/packages/contract-cli/src/public-contract/index.js b/packages/contract-cli/src/public-contract/index.js index d63f45eb..54f036e4 100644 --- a/packages/contract-cli/src/public-contract/index.js +++ b/packages/contract-cli/src/public-contract/index.js @@ -3,12 +3,19 @@ const { join } = require('path') const fsx = require('fs-extra') const { merge } = require('lodash') const { JsonqlError } = require('jsonql-errors') -const { +const { isObjectHasKey } = require('jsonql-params-validator') +const { SOCKET_NAME, QUERY_NAME, - AUTH_NAME + MUTATION_NAME, + AUTH_NAME, + SOCKET_AUTH_NAME, + NAMESAPCE_PROP_KEY } = require('jsonql-constants') +// @TODO this should port back to the jsonql-constants +const FILE_PROP_KEY = 'file' +const colors = require('colors/safe') const { getDebug } = require('../utils') const debug = getDebug('public-contract') /** @@ -22,38 +29,56 @@ const cleanForPublic = (json, config) => { enableAuth, loginHandlerName, logoutHandlerName, + disconnectHandlerName, validatorHandlerName - } = config; + } = config for (let type in json) { for (let fn in json[type]) { - delete json[type][fn].file - // @1.7.4 remove the description to reduce the size of the contract - // @ 1.7.6 this function move to the Koa instead - // because we want to keep the data in the file only remove it when serving up - // if (!contractWithDesc) { - // delete json[type][fn].description - // } + delete json[type][fn][FILE_PROP_KEY] + // @TODO for query and mutation we should remove the namespaces etc, they are for socket + switch (type) { + case QUERY_NAME: + case MUTATION_NAME: + if (isObjectHasKey(json[type][fn], NAMESAPCE_PROP_KEY) { + delete json[type][fn][NAMESAPCE_PROP_KEY] + } + // @TODO the public key + break + default: + + } } } // also if there is a validator field then delete it - if (json[AUTH_NAME][validatorHandlerName]) { - delete json[AUTH_NAME][validatorHandlerName] + const authValidators = [AUTH_NAME, SOCKET_AUTH_NAME] + for (let i = 0; i < authValidators.length; ++i) { + let av = authValidators[i] + if (json[av]) { + console.log(colors.rainbow(`found ${av}`)) + if (json[av][validatorHandlerName]) { + console.log(`${av} --> ${validatorHandlerName} FOUND?`) + delete json[av][validatorHandlerName] + } else { + console.log(colors.rainbow(`${av} --> ${validatorHandlerName} NOT FOUND?`)) + } + if (json[av][ disconnectHandlerName ]) { + delete json[av][disconnectHandlerName] + } + } else { + console.log(colors.rainbow(`${av} not found???`)) + } } // if it's not enableAuth then remove all of these + const handlers = [loginHandlerName, logoutHandlerName] if (!enableAuth) { - if (json[AUTH_NAME][loginHandlerName]) { - delete json[AUTH_NAME][loginHandlerName] - } - if (json[AUTH_NAME][logoutHandlerName]) { - delete json[AUTH_NAME][logoutHandlerName] - } - // check if there is any socket auth - if (json[SOCKET_NAME] && json[SOCKET_NAME][AUTH_NAME]) { - if (json[SOCKET_NAME][AUTH_NAME][validatorHandlerName]) { - delete json[SOCKET_NAME][AUTH_NAME][validatorHandlerName] + for (let j = 0; j < handlers.length; ++j) { + if (json[AUTH_NAME][handlers[j]]) { + delete json[AUTH_NAME][handlers[j]] } } } + // @TODO there are several more new features, interceptor, external, plugins, middlewares + // don't need this in the public json delete json.sourceType // export @@ -61,7 +86,7 @@ const cleanForPublic = (json, config) => { } /** - * @TODO + * @TODO * using the NODE_ENV to check if there is extra contract file * @param {string} contractDir directory store contract * @return {object} empty object when nothing @@ -77,7 +102,7 @@ function getEnvContractFile(contractDir) { } /** - * When this contract contain a socket field + * When this contract contain a socket field * then we can use this to inject extra into it * @param {object} contractJson the partially generated contract file * @param {object} helloContract the helloWorld contract diff --git a/packages/contract-cli/tests/socket.test.js b/packages/contract-cli/tests/socket.test.js index b2fc6424..399b8bee 100644 --- a/packages/contract-cli/tests/socket.test.js +++ b/packages/contract-cli/tests/socket.test.js @@ -4,6 +4,7 @@ const { join } = require('path') const fsx = require('fs-extra') const colors = require('colors/safe') const { + HELLO_FN, SOCKET_NAME, AUTH_TYPE, RETURN_AS_JSON, @@ -68,19 +69,34 @@ test.cb(`It should able to generate partial contract for socket auth`, t => { }) }) - test.cb(`It should able to generate new entry when socket / auth has content`, t => { t.plan(1) + generator({ + resolverDir, + contractDir, + enableAuth: true + // returnAs: RETURN_AS_JSON + }) + .then(result => { + t.truthy(result) + // colorDebug('socket test output', result) + t.end() + }) +}) + +test.cb.only(`Now test the public contract with socket`, t => { + t.plan(1) + generator({ resolverDir, contractDir, enableAuth: true, + public: true, returnAs: RETURN_AS_JSON }) .then(result => { - t.truthy(result[SOCKET_AUTH_NAME]) - - colorDebug('socket test output', result) + t.truthy(result[SOCKET_NAME][HELLO_FN]) + colorDebug('public contract', result) t.end() }) }) -- Gitee From f81b6f1e60edff73d5f9a509c4ea4a14c570783d Mon Sep 17 00:00:00 2001 From: joelchu Date: Thu, 19 Mar 2020 13:56:29 +0800 Subject: [PATCH 3/3] jsonql-constants to 2.0.6 --- packages/constants/README.md | 1 + packages/constants/browser.js | 1 + packages/constants/constants.json | 1 + packages/constants/index.js | 22 +++++++++++----------- packages/constants/main.js | 1 + 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/constants/README.md b/packages/constants/README.md index 9986af23..ad8c6615 100755 --- a/packages/constants/README.md +++ b/packages/constants/README.md @@ -137,6 +137,7 @@ Please consult the detail break down below. - DEBUG_ON_PROP_KEY - HOSTNAME_PROP_KEY - NAMESAPCE_PROP_KEY +- FILE_PROP_KEY - WS_OPT_PROP_KEY - CONTRACT_PROP_KEY - TOKEN_PROP_KEY diff --git a/packages/constants/browser.js b/packages/constants/browser.js index 4d7678c6..e6c842c1 100644 --- a/packages/constants/browser.js +++ b/packages/constants/browser.js @@ -138,6 +138,7 @@ var jsonqlConstants = { "DEBUG_ON_PROP_KEY": "debugOn", "HOSTNAME_PROP_KEY": "hostname", "NAMESAPCE_PROP_KEY": "namespace", + "FILE_PROP_KEY": "file", "WS_OPT_PROP_KEY": "wsOptions", "CONTRACT_PROP_KEY": "contract", "TOKEN_PROP_KEY": "token", diff --git a/packages/constants/constants.json b/packages/constants/constants.json index 6b9048b9..51a120b9 100644 --- a/packages/constants/constants.json +++ b/packages/constants/constants.json @@ -138,6 +138,7 @@ "DEBUG_ON_PROP_KEY": "debugOn", "HOSTNAME_PROP_KEY": "hostname", "NAMESAPCE_PROP_KEY": "namespace", + "FILE_PROP_KEY": "file", "WS_OPT_PROP_KEY": "wsOptions", "CONTRACT_PROP_KEY": "contract", "TOKEN_PROP_KEY": "token", diff --git a/packages/constants/index.js b/packages/constants/index.js index b996940f..a27d0f80 100644 --- a/packages/constants/index.js +++ b/packages/constants/index.js @@ -142,8 +142,8 @@ export const ORIGIN_HEADER_KEYS = ['Origin'] export const WILD_CARD_CHAR = '*' /* prop.js */ -// this is all the key name for the config check map -// all subfix with prop_key +// this is all the key name for the config check map +// all subfix with prop_key export const TYPE_KEY = 'type' export const OPTIONAL_KEY = 'optional' @@ -154,7 +154,7 @@ export const ALIAS_KEY = 'alias' // @TODO remove this later export const CHECKED_KEY = '__checked__' -// first part port from the ws-server-core +// first part port from the ws-server-core export const APP_DIR_PROP_KEY = 'appDir' export const AUTH_TO_PROP_KEY = 'authTimeout' @@ -172,7 +172,7 @@ export const SWITCH_USER_FN_NAME_PROP_KEY = 'switchUserHandlerName' export const PUBLIC_FN_DIR_PROP_KEY = 'publicResolverDir' export const PRIVATE_FN_DIR_DROP_KEY = 'privateResolverDir' -// socket specific +// socket specific export const ALGORITHM_PROP_KEY = 'algorithm' export const KEYS_DIR_PROP_KEY = 'keysDir' export const SOCKET_IO_AUTH_TYPE_PROP_KEY = 'socketIoAuthType' @@ -190,6 +190,7 @@ export const DEBUG_ON_PROP_KEY = 'debugOn' export const HOSTNAME_PROP_KEY = 'hostname' export const NAMESAPCE_PROP_KEY = 'namespace' +export const FILE_PROP_KEY = 'file' export const WS_OPT_PROP_KEY = 'wsOptions' @@ -227,21 +228,20 @@ export const JS_TYPE_PROP_KEY = 'jsType' export const EXPOSE_ERR_PROP_KEY = 'exposeError' export const CLIENT_CONFIG_PROP_KEY = 'clientConfig' -// this will combine all three options together -// because they are redudant if set this to true -// then whenever start up will build a fresh new contract like now -// if false then the developer has to manually build the contract +// this will combine all three options together +// because they are redudant if set this to true +// then whenever start up will build a fresh new contract like now +// if false then the developer has to manually build the contract // which is what production suppose to be export const AUTO_CONTRACT_PROP_KEY = 'autoCreateContract' -// still under development +// still under development export const VALIDATE_RETURNS_PROP_KEY = 'validateReturns' export const ENABLE_UPLOAD_PROP_KEY = 'enableFileUpload' export const FILE_UPLOAD_NAME_PROP_KEY = 'fileUploadName' export const FILE_UPLOAD_DIST_PROP_KEY = 'fileUploadDist' export const FILE_HANDLER_FN_NAME_PROP_KEY = 'fileHandlerName' -export const ENABLE_SPLIT_TASK_PROP_KEY = 'enableSplitTask' - +export const ENABLE_SPLIT_TASK_PROP_KEY = 'enableSplitTask' /* socket.js */ // the constants file is gettig too large diff --git a/packages/constants/main.js b/packages/constants/main.js index 58922d6b..59e4a33a 100644 --- a/packages/constants/main.js +++ b/packages/constants/main.js @@ -138,6 +138,7 @@ module.exports = { "DEBUG_ON_PROP_KEY": "debugOn", "HOSTNAME_PROP_KEY": "hostname", "NAMESAPCE_PROP_KEY": "namespace", + "FILE_PROP_KEY": "file", "WS_OPT_PROP_KEY": "wsOptions", "CONTRACT_PROP_KEY": "contract", "TOKEN_PROP_KEY": "token", -- Gitee