Skip to content

Commit

Permalink
mashroom-utils refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nonblocking committed Dec 19, 2023
1 parent 6f3833a commit 5102d0f
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

## [unreleased]

* mashroom-utils refactoring: Added an index file that should be used exclusively to import utils
**BREAKING CHANGE**: If you have used mashroom-utils in your custom plugins you have to change the imports
* LDAP Security Provider: Fixed escaping of special characters in the DN. Didn't work if the same special character occurred multiple times.
* Dropped support for Node.js 16 - required is now 18 or 20
* All 3rd party libraries upgrades and switched to npm workspaces for dependency management
Expand Down
2 changes: 1 addition & 1 deletion packages/core/mashroom-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

Part of [Mashroom Server](https://www.mashroom-server.com), a **Microfrontend Integration Platform**.

This package contains some shared utilities for _Mashroom Server_ plugins.
This package contains some shared Node.js utilities for _Mashroom Server_ plugins.
19 changes: 0 additions & 19 deletions packages/core/mashroom-utils/src/error-utils.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/mashroom-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

export * as clusterUtils from './cluster-utils';
export * as configUtils from './config-utils';
export * as errorUtils from './error-utils';
export * as fileTypeUtils from './file-type-utils';
export * as headerUtils from './header-utils';
export * as htmlUtils from './html-utils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

import {isES6Module} from '@mashroom/mashroom-utils/lib/file-type-utils';
import getClientServices from './client_services';

import type {
Expand All @@ -17,7 +16,7 @@ const loadJs = (path: string): Promise<void> => {
return new Promise((resolve, reject) => {
console.info('Loading JS resource: ', path);
const scriptElem = document.createElement('script');
if (isES6Module(path)) {
if (path.indexOf('.mjs') !== -1) {
scriptElem.type = 'module';
}
scriptElem.src = path;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

import {topicMatcher} from '@mashroom/mashroom-utils/lib/messaging-utils';
import {
WINDOW_VAR_REMOTE_MESSAGING_CONNECT_PATH,
WINDOW_VAR_REMOTE_MESSAGING_PRIVATE_USER_TOPIC,
Expand All @@ -15,6 +13,8 @@ import type {
} from '../../../../type-definitions';

const REMOTE_MESSAGING_TOPIC_PREFIX = 'remote:';
const REMOTE_MESSAGING_WILDCARDS_MULTI = ['#'];
const REMOTE_MESSAGING_WILDCARDS_SINGLE = ['+', '*'];
const REMOTE_MESSAGING_CONNECT_PATH: string | undefined = (global as any)[WINDOW_VAR_REMOTE_MESSAGING_CONNECT_PATH];
const REMOTE_MESSAGING_PRIVATE_USER_TOPIC: string | undefined = (global as any)[WINDOW_VAR_REMOTE_MESSAGING_PRIVATE_USER_TOPIC];

Expand Down Expand Up @@ -250,9 +250,14 @@ export default class MashroomPortalMessageBusImpl implements MashroomPortalMaste

let subscriptions: Array<Subscription> = [];
Object.keys(this._subscriptionMap)
.filter((t) => topicMatcher(t, topic))
.forEach((t) => {
subscriptions = subscriptions.concat(this._subscriptionMap[t]);
.filter((pattern) => {
let regex = `^${pattern}$`;
REMOTE_MESSAGING_WILDCARDS_SINGLE.forEach((w) => regex = regex.replace(w, '[^/]+'));
REMOTE_MESSAGING_WILDCARDS_MULTI.forEach((w) => regex = regex.replace(w, '.*'));
return !!topic.match(new RegExp(regex));
})
.forEach((pattern) => {
subscriptions = subscriptions.concat(this._subscriptionMap[pattern]);
});
console.debug(`Subscriptions for remote topic ${topic}: ${subscriptions ? subscriptions.length : 0}`);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

import {isES6Module} from '@mashroom/mashroom-utils/lib/file-type-utils';
import {WINDOW_VAR_PORTAL_INLINED_STYLE_APPS} from '../../../backend/constants';

import type {LoadedPortalAppInternal} from './MashroomPortalAppServiceImpl';
Expand Down Expand Up @@ -57,7 +56,7 @@ export default class ResourceManager {
};

scriptElem.src = path;
if (isES6Module(path)) {
if (path.indexOf('.mjs') !== -1) {
scriptElem.type = 'module';
}
scriptElem.addEventListener('error', (error: any) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@

import {mergeStackTrace} from '@mashroom/mashroom-utils/lib/error-utils';
const mergeStackTrace = (stackTrace: string, rootCauseStackTrace: string | null | undefined): string => {
if (!rootCauseStackTrace) {
return stackTrace;
}

const entriesToMerge = stackTrace.split('\n');
const baseEntries = rootCauseStackTrace.split('\n');

const newEntries: Array<string> = [];
entriesToMerge.forEach((entry) => {
if (baseEntries.includes(entry)) {
return;
}
newEntries.push(entry);
});

return [...newEntries, ...baseEntries].join('\n');
};

export class RestError extends Error {
constructor(private statusCode: number, message: string, rootCauseStack?: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,70 @@ describe('MashroomPortalMessageBusImpl', () => {
const messageBus = new MashroomPortalMessageBusImpl();
// @ts-ignore
messageBus._remoteMessageClient = {};

expect(messageBus.getRemoteUserPrivateTopic()).toBe('user/testuser');
});

it('returns the user private topic of another user', () => {
const messageBus = new MashroomPortalMessageBusImpl();
// @ts-ignore
messageBus._remoteMessageClient = {};

expect(messageBus.getRemoteUserPrivateTopic('[email protected]')).toBe('user/[email protected]');
});

it('processes single level wildcards in remote topics correctly', async () => {
const messageBus = new MashroomPortalMessageBusImpl();
// @ts-ignore
messageBus._remoteMessageClient = {
subscribe() {
return Promise.resolve();
}
};

let match = false;
messageBus.subscribe(`${messageBus.getRemotePrefix()}foo/*/bar/+/xx`, () => {
match = true;
});

// @ts-ignore
messageBus._handleRemoteMessage({}, 'foo/1/bar/2/3/xx');
await new Promise((resolve) => setTimeout(resolve, 100));

expect(match).toBeFalsy();

// @ts-ignore
messageBus._handleRemoteMessage({}, 'foo/1/bar/2/xx');
await new Promise((resolve) => setTimeout(resolve, 100));

expect(match).toBeTruthy();
});

it('processes multi level wildcards in remote topics correctly', async () => {
const messageBus = new MashroomPortalMessageBusImpl();
// @ts-ignore
messageBus._remoteMessageClient = {
subscribe() {
return Promise.resolve();
}
};

let match = false;
messageBus.subscribe(`${messageBus.getRemotePrefix()}foo/#/xx`, () => {
match = true;
});

// @ts-ignore
messageBus._handleRemoteMessage({}, 'foo/1/bar/2/xy');
await new Promise((resolve) => setTimeout(resolve, 100));

expect(match).toBeFalsy();

// @ts-ignore
messageBus._handleRemoteMessage({}, 'foo/1/bar/2/xx');
await new Promise((resolve) => setTimeout(resolve, 100));

expect(match).toBeTruthy();
});

it('should unsubscribe after app unload', () => {
const messageBus = new MashroomPortalMessageBusImpl();
const messageBusApp1 = messageBus.getAppInstance('app1');
Expand Down

0 comments on commit 5102d0f

Please sign in to comment.