diff --git a/packages/koa/package.json b/packages/koa/package.json index 3dce9f31b733baab887f3ee24808730ed7d5189c..51644ebc743db71eac0b03f511be600a252b18f1 100644 --- a/packages/koa/package.json +++ b/packages/koa/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-koa", - "version": "1.4.0", + "version": "1.4.1", "description": "jsonql Koa middleware", "main": "main.js", "module": "index.js", @@ -73,7 +73,7 @@ "esm": "^3.2.25", "fs-extra": "^8.1.0", "jsonql-constants": "^1.8.9", - "jsonql-contract": "^1.8.0", + "jsonql-contract": "^1.8.3", "jsonql-errors": "^1.1.5", "jsonql-jwt": "^1.3.3", "jsonql-node-client": "^1.1.11", diff --git a/packages/koa/src/contracts/get-contract.js b/packages/koa/src/contracts/get-contract.js index 0a476f3e9e9f3997d4ab2b4e3d1c34ae7d2e316e..f77235d4552c6be7dde0a3066ce84dc635c6eedf 100644 --- a/packages/koa/src/contracts/get-contract.js +++ b/packages/koa/src/contracts/get-contract.js @@ -1,83 +1,13 @@ // move all the code into it's folder // 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 { splitContractGenerator } from 'jsonql-contract/extra' -import { contractGenerator, readContract } from 'jsonql-contract/extra' - -import { getDebug } from '../utils' -const debug = getDebug('contract-generator') -// try to cache it -let contractCache = {}; /** - * getContract main + * getContract main - all the contract related methods move back to contract-cli * @param {object} config options * @param {boolean} pub public contract or not * @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'; - if (contractCache[key]) { - debug(`return ${key} contract from cache`, contractCache[key]) - // this is the problem - if we want to keep it consistent then - // we should always provide the contract key --> cache is optional! - // { contract: contractCache[key], cache: true } - return Promise.resolve(contractCache[key]) - } - ps.send({ config, pub }) - // return - return new Promise((resolver, rejecter) => { - ps.on('message', msg => { - if (msg.contract) { - // contractCache[key] = msg.contract; // <-- disable cache - // return - return resolver(msg.contract) - } - rejecter(msg) - }) - ps.on('error', err => { - debug('ps error', err) - rejecter(err) - }) - }) + return splitContractGenerator(config, pub) } diff --git a/packages/koa/src/contracts/run.js b/packages/koa/src/contracts/run.js deleted file mode 100644 index ea097a02b2436bfd9610dc2f1a5b20aa8cddb594..0000000000000000000000000000000000000000 --- a/packages/koa/src/contracts/run.js +++ /dev/null @@ -1,20 +0,0 @@ -// /lib/contract-generator/run.js -// @BUG throw error when we call this using ES6 -const { contractGenerator, readContract } = require('jsonql-contract/extra') -// listening -process.on('message', m => { - const { config, pub } = m; - if (config) { - let contract = readContract(config.contractDir, pub) - if (contract !== false) { - return process.send({ contract }) - } - contractGenerator(config, pub) - .then(contract => { - process.send({ contract }) - }) - .catch(error => { - process.send({ error }) - }) - } -})