diff --git a/config/package/package.json b/config/package/package.json index 39a1de8..4ef7579 100644 --- a/config/package/package.json +++ b/config/package/package.json @@ -15,7 +15,9 @@ "index.js", "index.js.map" ], - "dependencies": {}, + "dependencies": { + "debug": "4.3.1" + }, "devDependencies": { "@neutrinojs/airbnb-base": "^9.4.0", "@neutrinojs/copy": "9.4.0", diff --git a/packages/core/package.json b/packages/core/package.json index 907a876..3ea5344 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -20,6 +20,7 @@ "start.js.map" ], "dependencies": { + "debug": "4.3.1", "js-yaml": "3.14.0", "webpack-virtual-modules": "0.3.2" }, diff --git a/packages/core/src/client.js b/packages/core/src/client.js index e69de29..ff8b4c5 100644 --- a/packages/core/src/client.js +++ b/packages/core/src/client.js @@ -0,0 +1 @@ +export default {}; diff --git a/packages/core/src/index.js b/packages/core/src/index.js index 36b250a..d0deb04 100644 --- a/packages/core/src/index.js +++ b/packages/core/src/index.js @@ -14,7 +14,7 @@ export {default as WebpackPlugin} from './webpack-plugin'; export const Class = class {}; -export const $$latus = { +export default { hooks: { '@latus/core/config': () => ({ id: 'latus', diff --git a/packages/core/src/latus.js b/packages/core/src/latus.js index fcb9d76..c3ccb21 100644 --- a/packages/core/src/latus.js +++ b/packages/core/src/latus.js @@ -1,27 +1,40 @@ +import D from 'debug'; + import Middleware from './middleware'; +const debug = D('@latus/core/latus'); + export default class Plugins { constructor({config, modules}) { this.config = config; const paths = Object.keys(config); + debug('latus paths: %O', paths); this.hooks = {}; this.modules = modules; for (let i = 0; i < modules.length; i++) { - if (modules[i].$$latus) { - const {$$latus: {hooks}} = modules[i]; - const keys = Object.keys(hooks); - for (let j = 0; j < keys.length; j++) { - const key = keys[j]; - if (!this.hooks[key]) { - this.hooks[key] = []; + if (modules[i].default) { + const {default: {hooks}} = modules[i]; + if (hooks) { + const keys = Object.keys(hooks); + debug("hooks for '%s': %O", paths[i], keys); + for (let j = 0; j < keys.length; j++) { + const key = keys[j]; + if (!this.hooks[key]) { + this.hooks[key] = []; + } + this.hooks[key].push({ + plugin: paths[i], + fn: hooks[key], + }); } - this.hooks[key].push({ - plugin: paths[i], - fn: hooks[key], - }); } } + else { + throw new Error( + `'${paths[i]}' specified as a latus path, but there was no default export`, + ); + } } const defaultConfig = this.invoke('@latus/core/config'); const plugins = Object.keys(defaultConfig); @@ -33,6 +46,7 @@ export default class Plugins { }; } this.invokeFlat('@latus/core/config/alter', this.config); + debug('latus config: %O', this.config); } invoke(hook, ...args) { diff --git a/packages/db/package.json b/packages/db/package.json index d8c41b1..01e0316 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -17,6 +17,7 @@ ], "dependencies": { "@latus/core": "^1.0.0", + "debug": "4.3.1", "mkdirp": "^1.0.4", "mysql2": "^2.2.5", "sequelize": "^6.3.5" diff --git a/packages/db/src/db.js b/packages/db/src/db.js index b3a4c3c..961c370 100644 --- a/packages/db/src/db.js +++ b/packages/db/src/db.js @@ -54,7 +54,7 @@ export async function createDatabaseConnection(latus) { } catch (error) { // eslint-disable-next-line no-await-in-loop - await new Promise((resolve) => setTimeout(resolve, 50)); + await new Promise((resolve) => setTimeout(resolve, 250)); } } await sequelize.sync(); diff --git a/packages/db/src/index.js b/packages/db/src/index.js index 46b143e..e7b0120 100644 --- a/packages/db/src/index.js +++ b/packages/db/src/index.js @@ -10,7 +10,7 @@ const { NODE_ENV, } = process.env; -export const $$latus = { +export default { hooks: { '@latus/core/config': () => ({ database: 'db', diff --git a/packages/http/package.json b/packages/http/package.json index 42231aa..f84b1ed 100644 --- a/packages/http/package.json +++ b/packages/http/package.json @@ -19,6 +19,7 @@ "dependencies": { "@latus/core": "1.0.0", "@neutrinojs/web": "^9.1.0", + "debug": "4.3.1", "express": "^4.17.1", "memfs": "3.2.0", "neutrino": "9.4.0", diff --git a/packages/http/src/index.js b/packages/http/src/index.js index b04d22f..a4af0a1 100644 --- a/packages/http/src/index.js +++ b/packages/http/src/index.js @@ -1,7 +1,7 @@ import {createHttpServer} from './server'; // eslint-disable-next-line import/prefer-default-export -export const $$latus = { +export default { hooks: { '@latus/core/config': () => ({ middleware: [], diff --git a/packages/react/package.json b/packages/react/package.json index ca9d6d9..19392a7 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -19,6 +19,7 @@ ], "dependencies": { "@neutrinojs/react": "^9.4.0", + "debug": "4.3.1", "react": "^17.0.1", "react-dom": "^17.0.1", "react-hot-loader": "^4.13.0" diff --git a/packages/react/src/entry.js b/packages/react/src/entry.js index 6b91657..1fd5c8a 100644 --- a/packages/react/src/entry.js +++ b/packages/react/src/entry.js @@ -1,8 +1,7 @@ import React from 'react'; import {render} from 'react-dom'; -// eslint-disable-next-line import/prefer-default-export -export const $$latus = { +export default { hooks: { '@latus/http/client/up': (latus) => { const Components = latus.invoke('@latus/react/components'); diff --git a/packages/react/src/index.js b/packages/react/src/index.js index 89a56af..9f0ac5e 100644 --- a/packages/react/src/index.js +++ b/packages/react/src/index.js @@ -15,8 +15,7 @@ const middleware = (neutrino) => { }); }; -// eslint-disable-next-line import/prefer-default-export -export const $$latus = { +export default { hooks: { '@latus/core/config': () => ({ entry: './src/react', diff --git a/packages/redis/package.json b/packages/redis/package.json index 514660a..9c35160 100644 --- a/packages/redis/package.json +++ b/packages/redis/package.json @@ -19,6 +19,7 @@ ], "dependencies": { "connect-redis": "^5.0.0", + "debug": "4.3.1", "express-session": "^1.17.1", "mkdirp": "^1.0.4", "redis": "^3.0.2", diff --git a/packages/redis/src/create-client.js b/packages/redis/src/create-client.js index 55a38d9..7539e3d 100644 --- a/packages/redis/src/create-client.js +++ b/packages/redis/src/create-client.js @@ -9,5 +9,14 @@ export default ({config: {'@latus/redis': {host, port}}}) => ( redis.createClient( REDIS_PORT || port, REDIS_HOST || host, + { + retry_strategy: (options) => { + if (options.total_retry_time > 1000 * 60 * 5) { + return new Error('redis connection failed'); + } + // reconnect after + return Math.min(options.attempt * 100, 3000); + }, + }, ) ); diff --git a/packages/redis/src/docker.js b/packages/redis/src/docker.js index f28de63..824e014 100644 --- a/packages/redis/src/docker.js +++ b/packages/redis/src/docker.js @@ -1,9 +1,13 @@ +import D from 'debug'; + import {spawn} from 'child_process'; import mkdirp from 'mkdirp'; import createClient from './create-client'; +const debug = D('@latus/redis/docker'); + export default async function createDockerContainer(latus) { const {config: {'@latus/redis': {docker, port}}} = latus; const args = [ @@ -25,15 +29,14 @@ export default async function createDockerContainer(latus) { const {pid} = spawn('docker', args, {stdio: 'inherit'}); process.on('exit', () => process.kill(pid)); process.on('SIGINT', () => process.exit()); - // eslint-disable-next-line no-constant-condition - while (true) { - try { - createClient(); - break; - } - catch (error) { - // eslint-disable-next-line no-await-in-loop - await new Promise((resolve) => setTimeout(resolve, 50)); - } + let isConnected = false; + const client = createClient(latus); + client.on('connect', () => { + isConnected = true; + }); + while (!isConnected) { + // eslint-disable-next-line no-await-in-loop + await new Promise((resolve) => setTimeout(resolve, 250)); } + debug('connected to redis'); } diff --git a/packages/redis/src/index.js b/packages/redis/src/index.js index b3d9e82..763dde1 100644 --- a/packages/redis/src/index.js +++ b/packages/redis/src/index.js @@ -1,6 +1,7 @@ +import createClient from './create-client'; import createDockerContainer from './docker'; -export {default as createClient} from './create-client'; +export {createClient}; const { NODE_ENV, @@ -28,7 +29,7 @@ const safeKeys = async (client, pattern, caret) => { export const keys = (client, pattern) => safeKeys(client, pattern, 0); -export const $$latus = { +export default { hooks: { '@latus/core/config': () => ({ docker: true, diff --git a/packages/redis/src/session.js b/packages/redis/src/session.js index 7a7f4d6..20be91a 100644 --- a/packages/redis/src/session.js +++ b/packages/redis/src/session.js @@ -6,8 +6,7 @@ import {createClient} from './index'; // eslint-disable-next-line import/newline-after-import const RedisStore = require('connect-redis')(session); -// eslint-disable-next-line import/prefer-default-export -export const $$latus = { +export default { hooks: { '@latus/http/session': (latus) => ({ store: new RedisStore({client: createClient(latus)}), diff --git a/packages/repl/package.json b/packages/repl/package.json index b322398..4ad4987 100644 --- a/packages/repl/package.json +++ b/packages/repl/package.json @@ -15,7 +15,9 @@ "index.js", "index.js.map" ], - "dependencies": {}, + "dependencies": { + "debug": "4.3.1" + }, "devDependencies": { "@neutrinojs/airbnb-base": "^9.4.0", "@neutrinojs/copy": "9.4.0", diff --git a/packages/repl/src/index.js b/packages/repl/src/index.js index f4ba500..02cc1bd 100644 --- a/packages/repl/src/index.js +++ b/packages/repl/src/index.js @@ -1,7 +1,6 @@ import {createReplServer} from './server'; -// eslint-disable-next-line import/prefer-default-export -export const $$latus = { +export default { hooks: { '@latus/core/up': async (latus, next) => createReplServer(latus).then(next), }, diff --git a/packages/socket/package.json b/packages/socket/package.json index ed91667..2aedcf2 100644 --- a/packages/socket/package.json +++ b/packages/socket/package.json @@ -21,7 +21,7 @@ "@latus/core": "^1.0.0", "@latus/http": "^1.0.0", "buffer": "^6.0.3", - "debug": "^4.3.1", + "debug": "4.3.1", "socket.io": "2.3.0", "socket.io-client": "2.3.0" }, diff --git a/packages/socket/src/client/index.js b/packages/socket/src/client/index.js index 3e94fce..b077d28 100644 --- a/packages/socket/src/client/index.js +++ b/packages/socket/src/client/index.js @@ -1,7 +1,6 @@ import SocketClient from './socket'; -// eslint-disable-next-line import/prefer-default-export -export const $$latus = { +export default { hooks: { '@latus/http/client/up': (latus) => { const socket = new SocketClient(latus); diff --git a/packages/socket/src/index.js b/packages/socket/src/index.js index 4749728..b5a5b09 100644 --- a/packages/socket/src/index.js +++ b/packages/socket/src/index.js @@ -1,7 +1,6 @@ import {createSocketServer} from './lifecycle'; -// eslint-disable-next-line import/prefer-default-export -export const $$latus = { +export default { hooks: { '@latus/core/config': () => ({ middleware: [], diff --git a/packages/user/package.json b/packages/user/package.json index 38f1f59..6c699cf 100644 --- a/packages/user/package.json +++ b/packages/user/package.json @@ -24,6 +24,7 @@ "dependencies": { "@latus/db": "^1.0.0", "bcrypt": "^5.0.0", + "debug": "4.3.1", "express": "^4.17.1", "express-session": "1.17.1", "express-socket.io-session": "^1.3.5", diff --git a/packages/user/src/local.js b/packages/user/src/local.js index 49a1a9a..a73626f 100644 --- a/packages/user/src/local.js +++ b/packages/user/src/local.js @@ -23,8 +23,7 @@ const LocalUser = (User) => class LocalUser extends User { }; -// eslint-disable-next-line import/prefer-default-export -export const $$latus = { +export default { '@latus/db/decorate': (Models) => ({ ...Models, User: LocalUser(Models.User), diff --git a/packages/user/src/models/index.js b/packages/user/src/models/index.js index 92da946..3ca5174 100644 --- a/packages/user/src/models/index.js +++ b/packages/user/src/models/index.js @@ -3,7 +3,7 @@ import User from './user'; export {Permission, User}; -export const $$latus = { +export default { '@latus/db/models': () => ({ Permission, User, diff --git a/packages/user/src/passport.js b/packages/user/src/passport.js index 03bd475..924d4fc 100644 --- a/packages/user/src/passport.js +++ b/packages/user/src/passport.js @@ -1,8 +1,7 @@ import passport from 'passport'; import LogOps from 'passport/lib/http/request'; -// eslint-disable-next-line import/prefer-default-export -export const $$latus = { +export default { hooks: { '@latus/http/middleware': () => (req, res, next) => { passport.initialize()(req, res, () => { diff --git a/packages/user/src/session.js b/packages/user/src/session.js index e6c2610..13374b1 100644 --- a/packages/user/src/session.js +++ b/packages/user/src/session.js @@ -10,7 +10,7 @@ export const session = (options) => expressSession({ ...options, }); -export const $$latus = { +export default { hooks: { '@latus/http/middleware': (httpServer, latus) => (req, res, next) => { express.urlencoded({extended: true})(req, res, () => { diff --git a/template/src/react/index.jsx b/template/src/react/index.jsx index fa36dde..d1413d1 100644 --- a/template/src/react/index.jsx +++ b/template/src/react/index.jsx @@ -8,8 +8,7 @@ const App = () => ( ); -// eslint-disable-next-line import/prefer-default-export -export const $$latus = { +export default { hooks: { '@latus/react/components': () => hot(module)(App), }, diff --git a/template/yarn.lock b/template/yarn.lock index 2caa2b8..e2555d5 100644 --- a/template/yarn.lock +++ b/template/yarn.lock @@ -913,16 +913,17 @@ "@latus/core@1.0.0", "@latus/core@^1.0.0": version "1.0.0" - resolved "https://npm.i12e.cha0s.io/@latus%2fcore/-/core-1.0.0.tgz#954045688b47de8b0497bdd745aac1105376e131" - integrity sha512-wgaHvhbdfl8MF+qrLEYjyAudk9h97aAniCcAn68wIfPFyceffBcKW/eOOXB5Ytvj1LQndzUH2RmGwAogzOGniQ== + resolved "https://npm.i12e.cha0s.io/@latus%2fcore/-/core-1.0.0.tgz#96d217d84c2e5a8f2219adaaed556611e80e40c2" + integrity sha512-1lRlk0X8bnHULiMEq6YsyIuT4KptH4W7LzsVwiv/9IkZV1LdC8k8qll+S7HeIyOTMPprcll9mP9nCHjI+5nIUQ== dependencies: + debug "4.3.1" js-yaml "3.14.0" webpack-virtual-modules "0.3.2" "@latus/db@1.0.0", "@latus/db@^1.0.0": version "1.0.0" - resolved "https://npm.i12e.cha0s.io/@latus%2fdb/-/db-1.0.0.tgz#18ad46056edda1c2bcd0db4169d38f1dffdfb31f" - integrity sha512-3rNpWWcAktdVeEA28/2DdHnaVqkUrT8WC0Va7KDNZ0e2oQWbY8SNDZM7YsOw7u2hVe0eexwfpWSy3uScavhVCg== + resolved "https://npm.i12e.cha0s.io/@latus%2fdb/-/db-1.0.0.tgz#49f33a8fd89cb146503994bc8336a3d338d30b2a" + integrity sha512-nihdJ6IW7pUhlZCqxSccg1FSvJWuRuzI1Yu+P3jJCs9bsfjOa7allzYahR6e6FAlYjRXvjYKKCMrEk859iEoOw== dependencies: "@latus/core" "^1.0.0" mkdirp "^1.0.4" @@ -931,8 +932,8 @@ "@latus/http@1.0.0", "@latus/http@^1.0.0": version "1.0.0" - resolved "https://npm.i12e.cha0s.io/@latus%2fhttp/-/http-1.0.0.tgz#48b8d9f152f661e83fdb3d25f379c9438f9fcece" - integrity sha512-gMX7x2WgvwLoJ9lvByU9Qgg1O9BxrtzIrOpMLiqILAKNRcAE4hTnaWXjUhl87dosUBCSLyQHgpuy3Swp0RQBYQ== + resolved "https://npm.i12e.cha0s.io/@latus%2fhttp/-/http-1.0.0.tgz#4cf141318d8b3a756e57215c5418975656c20824" + integrity sha512-7Upe0XusYz0a9408ZvnisJWFRXDxAmyWkJpiCvMKlF67AKCvjrKFeZrBA5eMsDf08rihRcX9xz2W5YsDp0Snmw== dependencies: "@latus/core" "1.0.0" "@neutrinojs/web" "^9.1.0" @@ -945,8 +946,8 @@ "@latus/react@1.0.0": version "1.0.0" - resolved "https://npm.i12e.cha0s.io/@latus%2freact/-/react-1.0.0.tgz#2a442353836903fb0c2d0882c09a1e2e265c2042" - integrity sha512-tzyRFA3/KSNjMQk+JG7gOVtlo7Wnz6b7huVztOoXgA3ZVomfnVbsfMb4QB6Rt25H+O84zx6HWd614mQFSSpqdg== + resolved "https://npm.i12e.cha0s.io/@latus%2freact/-/react-1.0.0.tgz#05b3116e8c89fa5e915dc9d22837be5a98bee2e1" + integrity sha512-mt1CjwgL5sQ4pxaV+VVo6puItTJS699AaD9Vn4kvM9ZUu9blWrUnYzq14zVTIv4IBbKY2CyQdmckmL+Xd/xadQ== dependencies: "@neutrinojs/react" "^9.4.0" react "^17.0.1" @@ -955,10 +956,11 @@ "@latus/redis@1.0.0": version "1.0.0" - resolved "https://npm.i12e.cha0s.io/@latus%2fredis/-/redis-1.0.0.tgz#52dacef10de2c28f97b284c610cd8d99fffebdc4" - integrity sha512-pZpMhpg3QL8R3y82x0Bu4rf7cn3O8z6YE73IomCeaU4kStos+vNEL0xKC8OtGUqxuHwTxLgUu5RdeP8D4lAYIQ== + resolved "https://npm.i12e.cha0s.io/@latus%2fredis/-/redis-1.0.0.tgz#5db10f656c921e162b7a2e73a1c4d214ace69aec" + integrity sha512-9dGDRdlqbYbkFZH29BRZFHWSnkkqgykdaYCWfpodJyoVcFuiNZGL3rP4KW01m47N8PYaweOt/cPeO5xzrwB9rg== dependencies: connect-redis "^5.0.0" + debug "4.3.1" express-session "^1.17.1" mkdirp "^1.0.4" redis "^3.0.2" @@ -966,13 +968,13 @@ "@latus/repl@1.0.0": version "1.0.0" - resolved "https://npm.i12e.cha0s.io/@latus%2frepl/-/repl-1.0.0.tgz#4867c1347a593931d5e745ceadcc88e30835b79e" - integrity sha512-GOrVTveJGvzQKBlHdKIVPUez63h9bOZkFWsca7jnhkpYw9p7m6IBN5P4eU1C/bcErdF9SpJ9KUUBuZa3DwEeDA== + resolved "https://npm.i12e.cha0s.io/@latus%2frepl/-/repl-1.0.0.tgz#3c2af7fd4554e1d44d9694188fed13e01081f683" + integrity sha512-Dqgjm9pfS3zH8CC+lUioBMsqXw3Mjl05KEv+A4xPH3EmhKGEvPKHFfl1i2RkyNTLgaLB0mshKebcoDw0QVfKfQ== "@latus/socket@1.0.0": version "1.0.0" - resolved "https://npm.i12e.cha0s.io/@latus%2fsocket/-/socket-1.0.0.tgz#98a770ca362ce0f1b6a5969cec416765b96633c4" - integrity sha512-mR/WO57WAB5XeDzvvunkOoAR5RQqOxibppPkeR56JMslImUCLI1O+8YyJkSYYvNUJh3rjjVxPHNWpNk9ntT39A== + resolved "https://npm.i12e.cha0s.io/@latus%2fsocket/-/socket-1.0.0.tgz#72da0b6f9e0e57ae3693875c3b92aa7dd45866fb" + integrity sha512-icrYOjy5B77dQVveFohJElceMgJaZHfCAA50x6wtM60j+ZQinGMNuE04z+9sXDEvwddYLqtSDLJFbydgyn4lnA== dependencies: "@latus/core" "^1.0.0" "@latus/http" "^1.0.0" @@ -983,8 +985,8 @@ "@latus/user@1.0.0": version "1.0.0" - resolved "https://npm.i12e.cha0s.io/@latus%2fuser/-/user-1.0.0.tgz#178ffd1d3b0e4dc83c1f27907b92c1f5849090ae" - integrity sha512-8V6Tcy4E3soiaYJBb6cn6HtgTcFOCbAVzghf/hQXcSGvYLyr09aFYC82sc4m9ntjDSONu4uRHT3A7boOxBQwZA== + resolved "https://npm.i12e.cha0s.io/@latus%2fuser/-/user-1.0.0.tgz#60dfc2ae21752f0e5f53f9633ea6270288919fe0" + integrity sha512-kl/0T8QDcWBZ8rz6tbiI4ucnvVZ79D8bZkTuzAZN8UwTQmiBjqYQ5JFCBYXJNYuOoeF07Xc3aBmY+85QCk3BrQ== dependencies: "@latus/db" "^1.0.0" bcrypt "^5.0.0" @@ -2517,6 +2519,13 @@ debug@2.6.9, debug@^2.2.0, debug@^2.3.3, debug@^2.6.9, debug@~2.6.0: dependencies: ms "2.0.0" +debug@4.3.1, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1: + version "4.3.1" + resolved "https://npm.i12e.cha0s.io/debug/-/debug-4.3.1.tgz#f0d229c505e0c6d8c49ac553d1b13dc183f6b2ee" + integrity sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ== + dependencies: + ms "2.1.2" + debug@^3.0.0, debug@^3.2.6: version "3.2.7" resolved "https://npm.i12e.cha0s.io/debug/-/debug-3.2.7.tgz#72580b7e9145fb39b6676f9c5e5fb100b934179a" @@ -2524,13 +2533,6 @@ debug@^3.0.0, debug@^3.2.6: dependencies: ms "^2.1.1" -debug@^4.0.1, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1: - version "4.3.1" - resolved "https://npm.i12e.cha0s.io/debug/-/debug-4.3.1.tgz#f0d229c505e0c6d8c49ac553d1b13dc183f6b2ee" - integrity sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ== - dependencies: - ms "2.1.2" - debug@~3.1.0: version "3.1.0" resolved "https://npm.i12e.cha0s.io/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"