diff --git a/packages/@jsonql/security/main.js b/packages/@jsonql/security/main.js index 83a1928a5f9a07d6055472607d3c2a9fda8e6d85..1e2300a0c38f2057eb6b282a735504cd79e87c11 100644 --- a/packages/@jsonql/security/main.js +++ b/packages/@jsonql/security/main.js @@ -17,7 +17,8 @@ const { } = require('./src/server') const { - getNodeCache, + isNodeCache, + getNodeCache, getToken, createCSRFToken, isCSRFTokenExist @@ -43,6 +44,7 @@ module.exports = { decryptWithPrivatePem, // new csrf and node cache related stuff + isNodeCache, getNodeCache, getToken, createCSRFToken, diff --git a/packages/@jsonql/security/package.json b/packages/@jsonql/security/package.json index 4350f2321261574700b85fd45feab2d79572f9e2..3343adb37d70f499526f15055fc01c44f312ec1a 100644 --- a/packages/@jsonql/security/package.json +++ b/packages/@jsonql/security/package.json @@ -1,6 +1,6 @@ { "name": "@jsonql/security", - "version": "0.9.0", + "version": "0.9.1", "description": "jwt authentication helpers library for jsonql browser / node", "main": "main.js", "module": "index.js", @@ -39,7 +39,7 @@ "dependencies": { "colors": "^1.4.0", "fs-extra": "^9.0.0", - "jsonql-constants": "^2.0.10", + "jsonql-constants": "^2.0.11", "jsonql-errors": "^1.2.1", "jsonql-params-validator": "^1.6.2", "jsonql-utils": "^1.2.5", diff --git a/packages/@jsonql/security/src/cache/get-node-cache.js b/packages/@jsonql/security/src/cache/get-node-cache.js index a420d93a18fcc8ef9336a3b036ef8e85956c969c..8072c4a02441cd2c736342c7a22852a7d95b651b 100644 --- a/packages/@jsonql/security/src/cache/get-node-cache.js +++ b/packages/@jsonql/security/src/cache/get-node-cache.js @@ -20,9 +20,9 @@ maxKeys: (default: -1) specifies a maximum amount of keys that can be stored in // in this structure {CSRF-TOKEN: TIMESTAMP} // so you can check if this key actually exist or not // const debug = require('debug')('jsonql-ws-server-core:share:get-node-cache') -const nodeCache = require('node-cache') +const NodeCache = require('node-cache') // need to rethink should we do it like this -global.jsonqlLocalNodeCacheStore = {} +// global.jsonqlLocalNodeCacheStore = {} /** * We don't want to init the node cache everytime @@ -31,15 +31,12 @@ global.jsonqlLocalNodeCacheStore = {} * @return {object} the nodeCache instance */ function getNodeCache(opts = {}) { + return new NodeCache(opts) +} - return new nodeCache(opts) - /* - if (!global.localNodeCacheStore) { - debug(`create new node cache`) - global.localNodeCacheStore = - } - return global.localNodeCacheStore - */ +function isNodeCache(store) { + return store instanceof NodeCache } -module.exports = { getNodeCache } \ No newline at end of file + +module.exports = { getNodeCache, isNodeCache } \ No newline at end of file diff --git a/packages/@jsonql/security/src/cache/index.js b/packages/@jsonql/security/src/cache/index.js index c4ac17a95362fdd549f762cca29620404712e694..08cddae5d385fd878d4a2c4b7a7972c5498f04ff 100644 --- a/packages/@jsonql/security/src/cache/index.js +++ b/packages/@jsonql/security/src/cache/index.js @@ -1,9 +1,10 @@ // new node side cache module using node-cache and re-use everywhere -const { getNodeCache } = require('./get-node-cache') +const { getNodeCache,isNodeCache } = require('./get-node-cache') const { getToken, createCSRFToken, isCSRFTokenExist } = require('./csrf-methods') module.exports = { + isNodeCache, getNodeCache, getToken, createCSRFToken, diff --git a/packages/@jsonql/security/tests/cache.test.js b/packages/@jsonql/security/tests/cache.test.js index 4cb5b15355cb226dea96ad43e23f8836a79b6057..ea8aaf07538b6a3ae86f534a4843e03437d83ead 100644 --- a/packages/@jsonql/security/tests/cache.test.js +++ b/packages/@jsonql/security/tests/cache.test.js @@ -2,7 +2,7 @@ const test = require('ava') const { getNodeCache, - // getToken, + isNodeCache, createCSRFToken, isCSRFTokenExist } = require('../src/cache') @@ -11,6 +11,15 @@ test.before(t => { t.context.store = getNodeCache() }) +test(`Test the basic node-cache wrapper methods`, t => { + + const check = isNodeCache(t.context.store) + + t.true(check) + +}) + + test(`Test the CSRF token get and store`, t => { const token = createCSRFToken(t.context.store) diff --git a/packages/constants/README.md b/packages/constants/README.md index 7314baa5e79c3cd01fb42d0bb933cf652f92bc7c..2e792d41f1a94ad397acc38543cc77764f062d8e 100755 --- a/packages/constants/README.md +++ b/packages/constants/README.md @@ -196,6 +196,7 @@ Please consult the detail break down below. - ON_READY_FN_NAME - ON_LOGIN_FN_NAME - SEND_MSG_FN_NAME +- ON_MSG_FN_NAME - CLIENT_PROP_NAME - USERDATA_PROP_NAME - EMIT_REPLY_TYPE diff --git a/packages/constants/browser.js b/packages/constants/browser.js index 9fe8f908cfa9427d171cae9bc7b3a828f0fe7aef..bdd8764049a762bd3067566e631822c58160cb30 100644 --- a/packages/constants/browser.js +++ b/packages/constants/browser.js @@ -202,6 +202,7 @@ var jsonqlConstants = { "ON_READY_FN_NAME": "onReady", "ON_LOGIN_FN_NAME": "onLogin", "SEND_MSG_FN_NAME": "send", + "ON_MSG_FN_NAME": "on", "CLIENT_PROP_NAME": "client", "USERDATA_PROP_NAME": "userdata", "EMIT_REPLY_TYPE": "emit_reply", diff --git a/packages/constants/constants.json b/packages/constants/constants.json index 67d129fa54910c9b0e27cecab7e491526f845aa6..59eaca791a753794f719d8cf71c925ac2ddc735b 100644 --- a/packages/constants/constants.json +++ b/packages/constants/constants.json @@ -202,6 +202,7 @@ "ON_READY_FN_NAME": "onReady", "ON_LOGIN_FN_NAME": "onLogin", "SEND_MSG_FN_NAME": "send", + "ON_MSG_FN_NAME": "on", "CLIENT_PROP_NAME": "client", "USERDATA_PROP_NAME": "userdata", "EMIT_REPLY_TYPE": "emit_reply", diff --git a/packages/constants/index.js b/packages/constants/index.js index 60457e5985125022666262640d20055aece87182..16fe46fc0b8ace2f6db6f2b4cd5cd30c3863065d 100644 --- a/packages/constants/index.js +++ b/packages/constants/index.js @@ -308,7 +308,7 @@ export const ON_READY_FN_NAME = 'onReady' export const ON_LOGIN_FN_NAME = 'onLogin' // new @1.8.6 // the actual method name client.resolverName.send export const SEND_MSG_FN_NAME = 'send' - +export const ON_MSG_FN_NAME = 'on' // this one is for nodeClient inject into the resolver export const CLIENT_PROP_NAME = 'client' export const USERDATA_PROP_NAME = 'userdata' diff --git a/packages/constants/main.js b/packages/constants/main.js index 3b25e6cd1c84b1f8349398f30087f54721849c8f..bb059e788ebfe506bff305d6372240daf581d64b 100644 --- a/packages/constants/main.js +++ b/packages/constants/main.js @@ -202,6 +202,7 @@ module.exports = { "ON_READY_FN_NAME": "onReady", "ON_LOGIN_FN_NAME": "onLogin", "SEND_MSG_FN_NAME": "send", + "ON_MSG_FN_NAME": "on", "CLIENT_PROP_NAME": "client", "USERDATA_PROP_NAME": "userdata", "EMIT_REPLY_TYPE": "emit_reply", diff --git a/packages/constants/package.json b/packages/constants/package.json index abf33e8dd0c875427718e3e2d7ddae3edb467b9f..1f22f4e3a284083d2c58d10d17fd0f5319f249d6 100755 --- a/packages/constants/package.json +++ b/packages/constants/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-constants", - "version": "2.0.11", + "version": "2.0.12", "description": "All the share constants for jsonql modules", "main": "main.js", "module": "index.js", diff --git a/packages/constants/socket.js b/packages/constants/socket.js index a212dfc73ea3ef383a2cdd32b9230adec2f44aa1..0ba76834e2ac7fd933f0570967de430a51e8fe1f 100644 --- a/packages/constants/socket.js +++ b/packages/constants/socket.js @@ -57,7 +57,7 @@ export const ON_READY_FN_NAME = 'onReady' export const ON_LOGIN_FN_NAME = 'onLogin' // new @1.8.6 // the actual method name client.resolverName.send export const SEND_MSG_FN_NAME = 'send' - +export const ON_MSG_FN_NAME = 'on' // this one is for nodeClient inject into the resolver export const CLIENT_PROP_NAME = 'client' export const USERDATA_PROP_NAME = 'userdata' diff --git a/packages/resolver/package.json b/packages/resolver/package.json index 4f8aaf27e23a6e0d200535e2a047aa8978e2ee72..20361e3d103bfdf27c330f6b39bc947d78633df5 100644 --- a/packages/resolver/package.json +++ b/packages/resolver/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-resolver", - "version": "1.2.1", + "version": "1.2.2", "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": [ @@ -34,9 +34,9 @@ "url": "https://gitee.com/to1source/jsonql/issues" }, "dependencies": { - "@jsonql/security": "^0.9.0", + "@jsonql/security": "^0.9.1", "debug": "^4.1.1", - "jsonql-constants": "^2.0.10", + "jsonql-constants": "^2.0.11", "jsonql-errors": "^1.2.1", "jsonql-node-client": "^1.3.1", "jsonql-params-validator": "^1.6.2", diff --git a/packages/resolver/src/cache.js b/packages/resolver/src/cache.js index 8b5dcc29375ba06fc9d5fb8574e1e5306b162e4d..6cf044366cff6089fbef69d8afb41beaa2e37217 100644 --- a/packages/resolver/src/cache.js +++ b/packages/resolver/src/cache.js @@ -10,17 +10,7 @@ therefore simply cache this part is not enough */ -const NodeCache = require('node-cache') - -/** - * Wrapper method to init the NodeCache instance - * @param {object} [opts={}] configuration - * @return {object} node cache instance - */ -function initCacheStore(opts = {}) { - return new NodeCache(opts) -} - +const { isNodeCache } = require('@jsonql/security') /** * get it @@ -56,19 +46,8 @@ function cacheResolver(store) { } } -/** - * Just double check if the store is nodeCache - * @param {*} store - * @return {boolean} - */ -function isNodeCache(store) { - return store instanceof NodeCache -} - module.exports = { - initCacheStore, getCacheResolver, - cacheResolver, - isNodeCache + cacheResolver } \ No newline at end of file diff --git a/packages/resolver/tests/combine.test.js b/packages/resolver/tests/combine.test.js index 2324accc26ade6005225e90a57b602a8db79275c..27a33608f116bcb21228dab934709de9ad10e3a9 100644 --- a/packages/resolver/tests/combine.test.js +++ b/packages/resolver/tests/combine.test.js @@ -1,18 +1,17 @@ // test a new combine resolver and associate methods const test = require('ava') const { - isNodeCache, getCompleteSocketResolver, getCacheResolver } = require('../index') -const NodeCache = require('node-cache') +const { isNodeCache, getNodeCache } = require('@jsonql/security') const debug = require('debug')('jsonql-resolver:test:combine') const fsx = require('fs-extra') const { join } = require('path') const { injectToFn, objHasProp } = require('jsonql-utils') test.before(t => { - t.context.nodeCacheInstance = new NodeCache() + t.context.nodeCacheInstance = getNodeCache() t.context.contract = fsx.readJsonSync(join(__dirname, 'fixtures', 'contract', 'contract.json')) }) @@ -31,11 +30,11 @@ test(`Test the nodeCache interface`, t => { }) test(`Test the getCompleteSocketResolver method`, t => { - + const resolverName = 'sendSomething' const args = [ - 'sendSomething', + resolverName, {contract: t.context.contract}, - 'sendSomething', + resolverName, t.context.nodeCacheInstance ] @@ -66,7 +65,7 @@ test(`Test the getCompleteSocketResolver method`, t => { t.is(resolver.timestamp, ts) - const resolver1 = getCacheResolver('sendSomething', t.context.nodeCacheInstance) + const resolver1 = getCacheResolver(resolverName, t.context.nodeCacheInstance) const msg = resolver1('la la la') diff --git a/packages/ws-server-core/package.json b/packages/ws-server-core/package.json index 1e854a2f3127103c703e360c0885415bf24f1b2d..d90f381acfc72053b90d74133550c502857b38fe 100644 --- a/packages/ws-server-core/package.json +++ b/packages/ws-server-core/package.json @@ -27,16 +27,16 @@ "author": "Joel Chu ", "license": "MIT", "dependencies": { - "@jsonql/security": "^0.9.0", + "@jsonql/security": "^0.9.1", "@to1source/event": "^1.1.1", "colors": "^1.4.0", "debug": "^4.1.1", "esm": "^3.2.25", "fs-extra": "^9.0.0", - "jsonql-constants": "^2.0.10", + "jsonql-constants": "^2.0.11", "jsonql-errors": "^1.2.1", "jsonql-params-validator": "^1.6.2", - "jsonql-resolver": "^1.2.1", + "jsonql-resolver": "^1.2.2", "jsonql-utils": "^1.2.5", "lodash": "^4.17.15" }, diff --git a/packages/ws-server-core/src/options/index.js b/packages/ws-server-core/src/options/index.js index 9eac0cd37d9a20c66beacd9781dbc6ef8b2f78eb..a893b103e3f1c604526c4157e3cccefac1b903d0 100644 --- a/packages/ws-server-core/src/options/index.js +++ b/packages/ws-server-core/src/options/index.js @@ -4,7 +4,10 @@ const { join } = require('path') const fsx = require('fs-extra') const { JsonqlValidationError } = require('jsonql-errors') const { getNspInfoByConfig } = require('jsonql-utils') -const { getNodeCache } = require('@jsonql/security') +const { + getNodeCache, + isNodeCache +} = require('@jsonql/security') const { checkConfig, @@ -16,7 +19,8 @@ const { getContract } = require('../share/get-contract') const { PEM_EXT, PUBLIC_KEY_NAME, - PRIVATE_KEY_NAME + PRIVATE_KEY_NAME, + CACHE_STORE_PROP_KEY } = require('jsonql-constants') const { SECRET_MISSING_ERR } = require('./constants') const { @@ -48,6 +52,17 @@ function localCheckConfig(config) { return checkConfigAsync(config, wsDefaultOptions, wsConstProps) } +/** + * Wrapper method to check if there is already a nodeCache object, if not init a new one + * @param {object} opts configuration + * @return {object} node cache instance + */ +function getCacheStore(opts) { + return opts[CACHE_STORE_PROP_KEY] && isNodeCache(opts[CACHE_STORE_PROP_KEY]) + ? opts[CACHE_STORE_PROP_KEY] + : getNodeCache() +} + /** * We take the step two onward from the wsCheckConfig * @param {object} config configuration already checked @@ -79,7 +94,7 @@ function initWsServerOption(config) { } } // we init an cache object here now for re-use through out the app - opts.cacheStore = getNodeCache() + opts[CACHE_STORE_PROP_KEY] = getCacheStore(opts) return opts }) } diff --git a/packages/ws-server-core/src/share/add-property.js b/packages/ws-server-core/src/resolver/add-property.js similarity index 100% rename from packages/ws-server-core/src/share/add-property.js rename to packages/ws-server-core/src/resolver/add-property.js diff --git a/packages/ws-server-core/src/resolver/index.js b/packages/ws-server-core/src/resolver/index.js new file mode 100644 index 0000000000000000000000000000000000000000..6715167f30670f9f758da044b892532c3d22d21a --- /dev/null +++ b/packages/ws-server-core/src/resolver/index.js @@ -0,0 +1,3 @@ +// all these methods were in the share folder before +// now all group under the resolver because they are all related to resolver + diff --git a/packages/ws-server-core/src/share/resolve-socket-method.js b/packages/ws-server-core/src/resolver/resolve-socket-method.js similarity index 91% rename from packages/ws-server-core/src/share/resolve-socket-method.js rename to packages/ws-server-core/src/resolver/resolve-socket-method.js index 307c23570aa5b9dc64b217d31895c5002baef185..31ed229aa4658d1b92a347e9b9863b665e258fcc 100644 --- a/packages/ws-server-core/src/share/resolve-socket-method.js +++ b/packages/ws-server-core/src/resolver/resolve-socket-method.js @@ -1,9 +1,13 @@ // search for the resolver location +const { + SOCKET_NAME, + CACHE_STORE_PROP_KEY +} = require('jsonql-constants') const { JsonqlResolverAppError } = require('jsonql-errors') -const { SOCKET_NAME } = require('jsonql-constants') const { getResolver } = require('jsonql-resolver') -const { getRainbowDebug } = require('./helpers') +const { getRainbowDebug } = require('../share/helpers') const { addProperty } = require('./add-property') + const debug = getRainbowDebug('share:resolve-method') /** diff --git a/packages/ws-server-core/src/resolver/setup-on-method.js b/packages/ws-server-core/src/resolver/setup-on-method.js new file mode 100644 index 0000000000000000000000000000000000000000..d534482510f320b52873425eafadebfa9ee271c6 --- /dev/null +++ b/packages/ws-server-core/src/resolver/setup-on-method.js @@ -0,0 +1,7 @@ +// this is brand new resolver.on method to listen to the message + + + +function setupOnMethod(deliverFn, resolver, resolverName) { + +} \ No newline at end of file diff --git a/packages/ws-server-core/src/share/setup-send-method.js b/packages/ws-server-core/src/resolver/setup-send-method.js similarity index 90% rename from packages/ws-server-core/src/share/setup-send-method.js rename to packages/ws-server-core/src/resolver/setup-send-method.js index cc21db5d145c6c62f2afab7d3d09e4f3feea2bd9..82ff7cd200ae840c525ff0c8ef19738e03328c99 100644 --- a/packages/ws-server-core/src/share/setup-send-method.js +++ b/packages/ws-server-core/src/resolver/setup-send-method.js @@ -1,7 +1,7 @@ const { SEND_MSG_FN_NAME, EMIT_REPLY_TYPE } = require('jsonql-constants') const { objDefineProps } = require('jsonql-utils') -const { deliverMsg } = require('./resolver-methods') -const { nil, getRainbowDebug } = require('./helpers') +const { deliverMsg } = require('../share/resolver-methods') +const { nil, getRainbowDebug } = require('../share/helpers') const debug = getRainbowDebug('create-send-method', 'x') /** diff --git a/packages/ws-server-core/src/share/get-node-cache.js b/packages/ws-server-core/src/share/get-node-cache.js deleted file mode 100644 index b2a706e120f1bd21d3e830475bbaef7c82b09aa7..0000000000000000000000000000000000000000 --- a/packages/ws-server-core/src/share/get-node-cache.js +++ /dev/null @@ -1,34 +0,0 @@ -/* -stdTTL: (default: 0) the standard ttl as number in seconds for every generated cache element. 0 = unlimited -checkperiod: (default: 600) The period in seconds, as a number, used for the automatic delete check interval. 0 = no periodic check. -useClones: (default: true) en/disable cloning of variables. If true you'll get a copy of the cached variable. If false you'll save and get just the reference. -Note: -true is recommended if you want simplicity, because it'll behave like a server-based cache (it caches copies of plain data). -false is recommended if you want to achieve performance or save mutable objects or other complex types with mutability involved and wanted, because it'll only store references of your data. -Here's a simple code example showing the different behavior -deleteOnExpire: (default: true) whether variables will be deleted automatically when they expire. If true the variable will be deleted. If false the variable will remain. You are encouraged to handle the variable upon the event expired by yourself. -enableLegacyCallbacks: (default: false) re-enables the usage of callbacks instead of sync functions. Adds an additional cb argument to each function which resolves to (err, result). will be removed in node-cache v6.x. -maxKeys: (default: -1) specifies a maximum amount of keys that can be stored in the cache. If a new item is set and the cache is full, an error is thrown and the key will not be saved in the cache. -1 disables the key limit. -*/ - -// using node-cache to store can be throw away data -// here we implement a simple store to store the CSRF token -// in this structure {CSRF-TOKEN: TIMESTAMP} -// so you can check if this key actually exist or not -const debug = require('debug')('jsonql-ws-server-core:share:get-node-cache') -const nodeCache = require('node-cache') -global.localNodeCacheStore - -/** - * We don't want to init the node cache everytime - * so we put that in a global value - * @param {objec} opts configuration - * @return {object} the nodeCache instance - */ -module.exports = function getNodeCache(opts = {}) { - if (!global.localNodeCacheStore) { - debug(`create new node cache`) - global.localNodeCacheStore = new nodeCache(opts) - } - return global.localNodeCacheStore -}