fix(build): remove metaloader to remove dataloader dependency

This commit is contained in:
SamTolmay 2021-08-17 11:27:44 +02:00
parent f2db270a22
commit f6f35a9134
No known key found for this signature in database
GPG Key ID: 655CB3F5AA745CF8
10 changed files with 28 additions and 161 deletions

View File

@ -17,8 +17,8 @@
*/
import createFileLoader from './loaders/fileLoader';
import createGetMeta from './utils/meta/getMeta';
import createWriteBuildArtifact from './utils/files/writeBuildArtifact';
import createMetaLoader from './loaders/metaLoader';
import addDefaultPages from './build/addDefaultPages/addDefaultPages';
import buildAuth from './build/buildAuth/buildAuth';
@ -57,7 +57,7 @@ async function build(options) {
try {
let components = await buildRefs({ context });
await testSchema({ components, context });
context.metaLoader = createMetaLoader({ components, context });
context.getMeta = createGetMeta(context);
await validateApp({ components, context });
await validateConfig({ components, context });
await addDefaultPages({ components, context });

View File

@ -36,14 +36,14 @@ async function buildBlock(block, blockContext) {
}
block.blockId = block.id;
block.id = `block:${blockContext.pageId}:${block.id}`;
await setBlockMeta(block, blockContext.metaLoader, blockContext.pageId);
await setBlockMeta(block, blockContext.getMeta, blockContext.pageId);
let newBlockContext = blockContext;
if (block.meta.category === 'context') {
newBlockContext = {
auth: blockContext.auth,
contextId: block.blockId,
metaLoader: blockContext.metaLoader,
getMeta: blockContext.getMeta,
pageId: blockContext.pageId,
requests: [],
};

View File

@ -48,12 +48,12 @@ async function buildPages({ components, context }) {
);
}
page.pageId = page.id;
await checkPageIsContext(page, context.metaLoader);
await checkPageIsContext(page, context.getMeta);
await buildBlock(page, {
auth: page.auth,
pageId: page.pageId,
requests: [],
metaLoader: context.metaLoader,
getMeta: context.getMeta,
});
// set page.id since buildBlock sets id as well.
page.id = `page:${page.pageId}`;

View File

@ -168,7 +168,7 @@ const auth = {
public: true,
};
const mockMetaLoader = (type) => {
const getMeta = (type) => {
const meta = blockMetas[type];
if (!meta) {
return null;
@ -176,11 +176,7 @@ const mockMetaLoader = (type) => {
return Promise.resolve(meta);
};
const metaLoader = {
load: mockMetaLoader,
};
const context = testContext({ logger, metaLoader });
const context = testContext({ logger, getMeta });
beforeEach(() => {
mockLogWarn.mockReset();

View File

@ -16,7 +16,7 @@
import { type } from '@lowdefy/helpers';
async function checkPageIsContext(page, metaLoader) {
async function checkPageIsContext(page, getMeta) {
if (type.isNone(page.type)) {
throw new Error(`Page type is not defined at ${page.pageId}.`);
}
@ -25,7 +25,7 @@ async function checkPageIsContext(page, metaLoader) {
`Page type is not a string at ${page.pageId}. Received ${JSON.stringify(page.type)}`
);
}
const meta = await metaLoader.load(page.type);
const meta = await getMeta(page.type);
if (!meta) {
throw new Error(
`Invalid block type at page ${page.pageId}. Received ${JSON.stringify(page.type)}`

View File

@ -16,7 +16,7 @@
import { type } from '@lowdefy/helpers';
async function setBlockMeta(block, metaLoader, pageId) {
async function setBlockMeta(block, getMeta, pageId) {
if (type.isNone(block.type)) {
throw new Error(`Block type is not defined at ${block.blockId} on page ${pageId}.`);
}
@ -27,7 +27,7 @@ async function setBlockMeta(block, metaLoader, pageId) {
)}`
);
}
const meta = await metaLoader.load(block.type);
const meta = await getMeta(block.type);
if (!meta) {
throw new Error(
`Invalid Block type at ${block.blockId} on page ${pageId}. Received ${JSON.stringify(

View File

@ -1,48 +0,0 @@
/*
Copyright 2020-2021 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { get } from '@lowdefy/helpers';
import Dataloader from 'dataloader';
import createGetMeta from '../utils/meta/getMeta';
function createMetaBatchLoader({ components, context }) {
const { blocksServerUrl, cacheDirectory } = context;
const { types } = components;
const getMeta = createGetMeta({ blocksServerUrl, cacheDirectory, types });
async function loader(keys) {
const fetched = [];
const promises = keys.map(async (key) => {
const item = await getMeta(key);
fetched.push(item);
});
await Promise.all(promises);
const returned = keys
.map((key) =>
fetched.find((item) => {
return get(item, 'type') === key;
})
)
.map((obj) => obj.meta);
return returned;
}
return loader;
}
function createMetaLoader(options) {
return new Dataloader(createMetaBatchLoader(options));
}
export default createMetaLoader;

View File

@ -1,85 +0,0 @@
/*
Copyright 2020-2021 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import createGetMeta from '../utils/meta/getMeta';
import createMetaLoader from './metaLoader';
jest.mock('../utils/meta/getMeta', () => {
const mockGetMeta = jest.fn();
return () => mockGetMeta;
});
const mockGetMeta = createGetMeta();
mockGetMeta.mockImplementation((type) => {
if (type === 'Type1') {
return {
type: 'Type1',
meta: {
key: 'value1',
},
};
}
if (type === 'Type2') {
return {
type: 'Type2',
meta: {
key: 'value2',
},
};
}
throw new Error('Not found.');
});
const loaderConfig = {
components: { types: {} },
context: { cacheDirectory: 'cacheDirectory' },
};
test('load meta', async () => {
const metaLoader = createMetaLoader(loaderConfig);
const res = await metaLoader.load('Type1');
expect(res).toEqual({
key: 'value1',
});
});
test('load meta, meta does not exist', async () => {
const metaLoader = createMetaLoader(loaderConfig);
await expect(metaLoader.load('DoesNotExist')).rejects.toThrow('Not found.');
});
test('load two files', async () => {
const metaLoader = createMetaLoader(loaderConfig);
const types = ['Type1', 'Type2'];
const res = await Promise.all(types.map((type) => metaLoader.load(type)));
expect(res).toEqual([
{
key: 'value1',
},
{
key: 'value2',
},
]);
});
test('load two meta, one does not exist', async () => {
const metaLoader = createMetaLoader(loaderConfig);
const types = ['Type1', 'DoesNotExist'];
await expect(Promise.all(types.map((type) => metaLoader.load(type)))).rejects.toThrow(
'Not found.'
);
});

View File

@ -19,7 +19,7 @@ function testContext({
configDirectory,
configLoader,
logger = {},
metaLoader,
getMeta,
} = {}) {
const defaultLogger = {
info: () => {},
@ -31,13 +31,11 @@ function testContext({
const context = {
configDirectory: configDirectory || '',
getMeta: getMeta || (() => {}),
writeBuildArtifact: writeBuildArtifact || (() => {}),
configLoader: {
load: () => {},
},
metaLoader: {
load: () => {},
},
};
context.logger = {
@ -47,9 +45,6 @@ function testContext({
if (configLoader) {
context.configLoader = configLoader;
}
if (metaLoader) {
context.metaLoader = metaLoader;
}
return context;
}

View File

@ -31,6 +31,8 @@ import createWriteMetaCache from './writeMetaCache';
import metaLocations from './metaLocations';
import fetchMetaUrl from './fetchMetaUrl';
const memoisedMeta = {};
function createGetMeta({ blocksServerUrl, cacheDirectory, types }) {
const allMetaLocations = {
...metaLocations({ blocksServerUrl }),
@ -39,6 +41,10 @@ function createGetMeta({ blocksServerUrl, cacheDirectory, types }) {
const fetchMetaCache = createFetchMetaCache({ cacheDirectory });
const writeMetaCache = createWriteMetaCache({ cacheDirectory });
async function getMeta(type) {
if (memoisedMeta[type]) {
memoisedMeta[type];
}
const location = allMetaLocations[type];
if (!location) {
throw new Error(
@ -51,19 +57,22 @@ function createGetMeta({ blocksServerUrl, cacheDirectory, types }) {
if (cacheMeta) {
meta = await fetchMetaCache(location);
if (meta)
if (meta) {
memoisedMeta[type] = meta;
return {
type,
meta,
};
}
meta = await fetchMetaUrl({ location, type });
if (cacheMeta) {
await writeMetaCache({ location, meta });
}
}
meta = await fetchMetaUrl({ location, type });
// TODO: implement Ajv schema check. Use testAjvSchema func from @lowdefy/ajv
if (meta && typeHelper.isString(meta.category) && meta.moduleFederation) {
memoisedMeta[type] = meta;
if (cacheMeta) {
await writeMetaCache({ location, meta });
}
return {
type,
meta,