diff --git a/packages/contract-cli/package.json b/packages/contract-cli/package.json index 666aafda6e4a64589fc548423e848c8e99f091ae..0589702419c8db44754b9bac46da678e0ea9aefb 100755 --- a/packages/contract-cli/package.json +++ b/packages/contract-cli/package.json @@ -55,7 +55,7 @@ "jsonql-utils": "^0.8.3", "kefir": "^3.8.6", "lodash": "^4.17.15", - "nb-split-tasks": "^0.5.1", + "nb-split-tasks": "^0.5.3", "yargs": "^14.2.0" }, "devDependencies": { diff --git a/packages/koa/package.json b/packages/koa/package.json index 1398c1e3f30649f90cff1244450f56debe36bc32..3dce9f31b733baab887f3ee24808730ed7d5189c 100644 --- a/packages/koa/package.json +++ b/packages/koa/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-koa", - "version": "1.3.11", + "version": "1.4.0", "description": "jsonql Koa middleware", "main": "main.js", "module": "index.js", @@ -73,8 +73,8 @@ "esm": "^3.2.25", "fs-extra": "^8.1.0", "jsonql-constants": "^1.8.9", - "jsonql-contract": "^1.7.22", - "jsonql-errors": "^1.1.3", + "jsonql-contract": "^1.8.0", + "jsonql-errors": "^1.1.5", "jsonql-jwt": "^1.3.3", "jsonql-node-client": "^1.1.11", "jsonql-params-validator": "^1.4.11", diff --git a/packages/koa/src/contracts/get-contract.js b/packages/koa/src/contracts/get-contract.js index af4b50b6fbfac9b2d984ebf58f1b9035e4998574..0a476f3e9e9f3997d4ab2b4e3d1c34ae7d2e316e 100644 --- a/packages/koa/src/contracts/get-contract.js +++ b/packages/koa/src/contracts/get-contract.js @@ -2,6 +2,9 @@ // we are going to fork the process to lighten the load when it start import { fork } from 'child_process' import { join } from 'path' +import os from 'os' + +import { contractGenerator, readContract } from 'jsonql-contract/extra' import { getDebug } from '../utils' const debug = getDebug('contract-generator') @@ -14,6 +17,43 @@ let contractCache = {}; * @return {object} Promise to resolve the contract json */ export function getContract(config, pub = false) { + if (os.cpus().length >= 8) { // at least a quad core + return forkGetContract(config, pub) + } + return getContractAction(config, pub) +} + +/** + * port back from run.js + * @param {object} config configuration + * @param {boolean} pub public or not + * @return {promise} resolve the contract + */ +function getContractAction(config, pub) { + return new Promise((resolver, rejecter) => { + if (config) { + let contract = readContract(config.contractDir, pub) + if (contract !== false) { + return resolver(contract) + } + contractGenerator(config, pub) + .then(contract => { + resolver(contract) + }) + .catch(error => { + rejecter(error) + }) + } + }) +} + +/** + * If we have enough cpu then call this one to get contract + * @param {object} config configuration + * @param {boolean} pub public or not + * @return {promise} resolve the contract + */ +function forkGetContract(config, pub) { const ps = fork(join(__dirname, 'run.js')) // @TODO this should not be hardcoded const key = pub ? 'public' : 'private';