From a2e775b64f051b010f4e322b7b6a7a1cde4fd6dd Mon Sep 17 00:00:00 2001 From: cha0s Date: Mon, 22 Jan 2024 10:32:39 -0600 Subject: [PATCH] refactor: web config --- packages/build/src/server.js | 1 - packages/core/src/index.js | 6 ++++++ packages/core/src/server/index.js | 8 -------- packages/redux/src/client/index.js | 2 +- packages/redux/src/index.js | 3 +++ packages/redux/src/server.js | 5 ----- packages/socket/src/index.js | 16 ++++++---------- packages/web/package.json | 1 + packages/web/src/index.js | 6 ++++++ packages/web/src/server/config.js | 29 +++++------------------------ packages/web/src/server/index.js | 3 --- 11 files changed, 28 insertions(+), 52 deletions(-) create mode 100644 packages/web/src/index.js diff --git a/packages/build/src/server.js b/packages/build/src/server.js index cc52aa7..37c61fc 100644 --- a/packages/build/src/server.js +++ b/packages/build/src/server.js @@ -2,4 +2,3 @@ export {dump as dumpYml, load as loadYml} from 'js-yaml'; export {default as webpack} from 'webpack'; export * from '../build/webpack'; - diff --git a/packages/core/src/index.js b/packages/core/src/index.js index 059f489..7356f6b 100644 --- a/packages/core/src/index.js +++ b/packages/core/src/index.js @@ -7,3 +7,9 @@ export { ByType, Flecks, } from '../build/flecks'; + +export const hooks = { + '@flecks/web.config': async (req, flecks) => ({ + id: flecks.get('@flecks/core.id'), + }), +}; diff --git a/packages/core/src/server/index.js b/packages/core/src/server/index.js index 453abec..e544670 100644 --- a/packages/core/src/server/index.js +++ b/packages/core/src/server/index.js @@ -1,11 +1,3 @@ export {glob} from 'glob'; export {JsonStream, transform} from '../../build/stream'; - -export const hooks = { - '@flecks/web.config': async (req, flecks) => ({ - '@flecks/core': { - id: flecks.get('@flecks/core.id'), - }, - }), -}; diff --git a/packages/redux/src/client/index.js b/packages/redux/src/client/index.js index 5049502..45a8502 100644 --- a/packages/redux/src/client/index.js +++ b/packages/redux/src/client/index.js @@ -10,7 +10,7 @@ export const hooks = { const slices = await flecks.invokeMergeUnique('@flecks/redux.slices'); const reducer = createReducer(flecks, slices); // Hydrate from server. - const {preloadedState} = flecks.get('@flecks/redux/client'); + const {preloadedState} = flecks.get('@flecks/redux'); const store = await configureStore(flecks, reducer, {preloadedState}); flecks.redux = store; return [Provider, {store}]; diff --git a/packages/redux/src/index.js b/packages/redux/src/index.js index fed21b7..704f175 100644 --- a/packages/redux/src/index.js +++ b/packages/redux/src/index.js @@ -7,4 +7,7 @@ export * from './actions'; export const hooks = { '@flecks/socket.packets': Flecks.provide(require.context('./packets', false, /\.js$/)), + '@flecks/web.config': async (req) => ({ + preloadedState: req.redux.getState(), + }), }; diff --git a/packages/redux/src/server.js b/packages/redux/src/server.js index 86b097b..5fb0189 100644 --- a/packages/redux/src/server.js +++ b/packages/redux/src/server.js @@ -30,9 +30,4 @@ export const hooks = { req.redux = await configureStore(flecks, reducer, {preloadedState}); next(); }, - '@flecks/web.config': async (req) => ({ - '@flecks/redux/client': { - preloadedState: req.redux.getState(), - }, - }), }; diff --git a/packages/socket/src/index.js b/packages/socket/src/index.js index aea8c83..323e8dd 100644 --- a/packages/socket/src/index.js +++ b/packages/socket/src/index.js @@ -11,21 +11,17 @@ export const hooks = { '@flecks/core.starting': (flecks) => { flecks.socket.Packets = flecks.gather('@flecks/socket.packets', {check: badPacketsCheck}); }, - '@flecks/web.config': async ( - req, - {config: {'@flecks/socket': {'packets.decorate': decorators = ['...']}}}, - ) => ({ - '@flecks/socket': { - 'packets.decorate': decorators.filter( - (decorator) => 'server' !== decorator.split('/').pop(), - ), - }, - }), '@flecks/socket.packets': (flecks) => ({ Bundle: Bundle(flecks), Redirect, Refresh, }), + '@flecks/web.config': async (req, flecks) => ({ + 'packets.decorate': ( + flecks.get('@flecks/socket.packets.decorate', ['...']) + .filter((decorator) => 'server' !== decorator.split('/').pop()) + ), + }), }; export const mixin = (Flecks) => class FlecksWithSocket extends Flecks { diff --git a/packages/web/package.json b/packages/web/package.json index c813616..b651b33 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -19,6 +19,7 @@ }, "files": [ "entry.js", + "index.js", "runtime.js", "server.js", "server/build/entry.js", diff --git a/packages/web/src/index.js b/packages/web/src/index.js new file mode 100644 index 0000000..e4a93d9 --- /dev/null +++ b/packages/web/src/index.js @@ -0,0 +1,6 @@ +export const hooks = { + '@flecks/web.config': async (req, flecks) => { + const {appMountId, title} = flecks.get('@flecks/web'); + return {appMountId, title}; + }, +}; diff --git a/packages/web/src/server/config.js b/packages/web/src/server/config.js index 5ac7bf0..60d8363 100644 --- a/packages/web/src/server/config.js +++ b/packages/web/src/server/config.js @@ -4,30 +4,11 @@ const { NODE_ENV, } = process.env; -export const configSource = async (flecks, req) => { - req.onlyAllow = (object, keys) => ( - Object.fromEntries( - Object.entries(object) - .map(([key, value]) => [key, keys.includes(key) ? value : undefined]), - ) - ); - const httpConfig = await flecks.invokeMergeAsync('@flecks/web.config', req); - const config = Object.fromEntries( - Object.entries(flecks.web.config) - .filter(([path]) => !path.endsWith('/server')) - .map(([path, config]) => [path, {...config, ...httpConfig[path]}]), - ); - // Fold in any bespoke configuration. - Object.keys(httpConfig) - .forEach((key) => { - if (!(key in config)) { - config[key] = httpConfig[key]; - } - }); - return `window[Symbol.for('@flecks/web.config')] = JSON.parse(decodeURIComponent("${ - encodeURIComponent(JSON.stringify(config)) - }"));`; -}; +export const configSource = async (flecks, req) => ( + `window[Symbol.for('@flecks/web.config')] = JSON.parse(decodeURIComponent("${ + encodeURIComponent(JSON.stringify(await flecks.invokeAsync('@flecks/web.config', req))) + }"));` +); class InlineConfig extends Transform { diff --git a/packages/web/src/server/index.js b/packages/web/src/server/index.js index 5528f7a..82f329d 100644 --- a/packages/web/src/server/index.js +++ b/packages/web/src/server/index.js @@ -8,9 +8,6 @@ const debug = D('@flecks/web/server'); export {configSource}; export const hooks = { - '@flecks/web.config': async ({onlyAllow}, flecks) => ({ - '@flecks/web': onlyAllow(flecks.get('@flecks/web'), ['appMountId', 'title']), - }), '@flecks/web.routes': (flecks) => [ { method: 'get',