Skip to content

Commit

Permalink
Merge pull request #274 from hyper63/twilson63/feat-allow-load-on-ada…
Browse files Browse the repository at this point in the history
…pter-242

feat: enabled plugin load method to support promises (#242)
  • Loading branch information
twilson63 committed Jul 24, 2021
2 parents c9cf400 + d1ec247 commit 5dc265b
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 29 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/tests-core.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
name: test core
on:
push:
tags-ignore: '*'
branches:
- "**"
- main
- '**'
paths:
- "packages/core/**"
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
deno: [1.11.x]
deno: [1.12.x]
steps:
- uses: actions/checkout@v2
- name: Use Deno ${{ matrix.deno-version }}
Expand Down
4 changes: 4 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ The core package validates the config schema and plugin schemas
```
./scripts/test.sh
```

## License

Apache 2.0
7 changes: 4 additions & 3 deletions packages/core/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ export default async function main(config) {

config = validateConfig(config);

//load methods can return promises so entire plugin svc returns a promise -tnw
const adapters = await initAdapters(prop("adapters", config));

// TODO: validate config
const services = compose(
// add eventMgr to services
wrapCore,
assoc("middleware", propOr([], "middleware", config)),
assoc("events", eventMgr()),
loadPorts,
initAdapters,
prop("adapters"),
)(config);
)(adapters);

const app = config.app(services);

Expand Down
28 changes: 28 additions & 0 deletions packages/core/mod_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import opine from "https://x.nest.land/[email protected]/mod.js";
import dndb from "https://x.nest.land/[email protected]/mod.js";
import memory from "https://x.nest.land/[email protected]/mod.js";
import { superdeno } from "https://deno.land/x/[email protected]/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

import core from "./mod.js";

const test = Deno.test;
Deno.env.set("DENO_ENV", "test");

const config = {
app: opine,
adapters: [
{ port: "data", plugins: [dndb("./data/foo.db")] },
{ port: "cache", plugins: [memory()] },
],
middleware: [],
};

test("build hyper core", async () => {
const app = await core(config);
const res = await superdeno(app)
.get("/");
//console.log(res.body)

assertEquals(res.body.name, "hyper63");
});
2 changes: 1 addition & 1 deletion packages/core/scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

deno lint
deno fmt --check
deno test --unstable --allow-env lib/storage/*_test.js utils/*_test.js lib/cache/*_test.js lib/data/*_test.js lib/crawler/*_test.js
deno test --unstable --allow-env --allow-read --allow-net lib/storage/*_test.js utils/*_test.js lib/cache/*_test.js lib/data/*_test.js lib/crawler/*_test.js mod_test.js

2 changes: 1 addition & 1 deletion packages/core/utils/plugin-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function (plugin) {
});

const instance = schema.parse(plugin);
instance.load = schema.shape.load.validate(plugin.load);
//instance.load = schema.shape.load.validate(plugin.load);
instance.link = schema.shape.link.validate(plugin.link);

return instance;
Expand Down
33 changes: 15 additions & 18 deletions packages/core/utils/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const {
filter,
compose,
map,
mergeAll,
is,
reduce,
defaultTo,
fromPairs,
pluck,
pipeP,
reverse,
} = R;

Expand All @@ -18,11 +18,8 @@ const {
*
* @param {[]} plugins - a list of plugins
*/
function loadAdapterConfig(plugins = []) {
return compose(
reduce((acc, plugin) => defaultTo(acc, plugin.load(acc)), {}),
filter((plugin) => is(Function, plugin.load)),
)(plugins);
async function loadAdapterConfig(plugins = []) {
return await pipeP(...filter(is(Function), pluck("load", plugins)))({});
}

/**
Expand Down Expand Up @@ -57,12 +54,10 @@ function linkPlugins(plugins, adapterConfig) {
)(plugins);
}

function initAdapter(portAdapter) {
async function initAdapter(portAdapter) {
const { plugins } = portAdapter;
return compose(
(adapterConfig) => linkPlugins(plugins, adapterConfig),
loadAdapterConfig,
)(plugins || []);
const env = await loadAdapterConfig(plugins || []);
return linkPlugins(plugins, env);
}

/**
Expand All @@ -71,9 +66,11 @@ function initAdapter(portAdapter) {
*
* @param {[]} adapters - a list of port nodes from a hyper63 config
*/
export default function initAdapters(adapters) {
return compose(
fromPairs,
map((adapterNode) => [adapterNode.port, initAdapter(adapterNode)]),
)(adapters);
export default async function initAdapters(adapters) {
const svcs = await Promise.all(
map(async (adapterNode) => ({
[adapterNode.port]: await initAdapter(adapterNode),
}), adapters),
);
return mergeAll(svcs);
}
8 changes: 4 additions & 4 deletions packages/core/utils/plugins_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { assertEquals, assertObjectMatch } from "../dev_deps.js";

const test = Deno.test;

test("sucessfully compose plugins", () => {
test("sucessfully compose plugins", async () => {
const plugin1 = validate({
id: "plugin1",
port: "default",
load: (env) => ({ ...env, hello: "world" }),
load: (env) => Promise.resolve({ ...env, hello: "world" }),
link: (env) => () => ({ hello: () => env.hello }),
});

const plugin2 = (config) =>
validate({
id: "plugin2",
port: "default",
load: (env) => ({ ...env, ...config }),
load: (env) => Promise.resolve({ ...env, ...config }),
link: (env) => (plugin) => ({ ...plugin, beep: () => env }),
});

Expand All @@ -25,7 +25,7 @@ test("sucessfully compose plugins", () => {
{ port: "default", plugins: [plugin2({ foo: "bar" }), plugin1] },
],
};
const adapters = initAdapters(config.adapters);
const adapters = await initAdapters(config.adapters);

assertEquals(adapters.default.hello(), "world");
assertObjectMatch(adapters.default.beep(), { foo: "bar", hello: "world" });
Expand Down

0 comments on commit 5dc265b

Please sign in to comment.