1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-06 00:13:28 +00:00

refactor(libs): consolidate messaging-internal into messaging library (#16386)

This change eliminates the circular dependency between messaging and messaging-internal libraries by merging them into a single messaging library.

Previously, messaging-internal imported from @bitwarden/messaging while messaging tried to import from @bitwarden/messaging-internal, creating an unresolvable circular dependency. This also violated Nx best practices by using cross-library file includes in tsconfig.lib.json.

Changes made:
- Moved all messaging-internal code (SubjectMessageSender, helpers, tests) into libs/messaging/src/
- Updated all imports to use relative paths instead of @bitwarden/messaging imports
- Removed the entire messaging-internal library and its configuration files
- Updated external references in apps/browser to import from @bitwarden/messaging
- Fixed libs/messaging/tsconfig.lib.json to use standard src/**/*.ts pattern
- Updated libs/common internal.ts to re-export from messaging instead of messaging-internal

The messaging library now exports both public APIs and internal implementations, which is a cleaner architecture than maintaining two separate libraries with circular dependencies.

Fixes rootDir configuration issues identified in the Nx library systematic fix project.
This commit is contained in:
Addison Beck
2025-09-12 07:04:13 -04:00
committed by GitHub
parent 24c8b2dc5d
commit 7ac75a6c52
22 changed files with 14 additions and 133 deletions

View File

@@ -0,0 +1,47 @@
import { Subject, firstValueFrom } from "rxjs";
import { getCommand, tagAsExternal } from "./helpers";
import { isExternalMessage } from "./is-external-message";
import { CommandDefinition, Message } from "./types";
describe("helpers", () => {
describe("getCommand", () => {
it("can get the command from just a string", () => {
const command = getCommand("myCommand");
expect(command).toEqual("myCommand");
});
it("can get the command from a message definition", () => {
const commandDefinition = new CommandDefinition<Record<string, unknown>>("myCommand");
const command = getCommand(commandDefinition);
expect(command).toEqual("myCommand");
});
});
describe("tag integration", () => {
it("can tag and identify as tagged", async () => {
const messagesSubject = new Subject<Message<Record<string, unknown>>>();
const taggedMessages = messagesSubject.asObservable().pipe(tagAsExternal());
const firstValuePromise = firstValueFrom(taggedMessages);
messagesSubject.next({ command: "test" });
const result = await firstValuePromise;
expect(isExternalMessage(result)).toEqual(true);
});
});
describe("isExternalMessage", () => {
it.each([null, { command: "myCommand", test: "object" }, undefined] as Message<
Record<string, unknown>
>[])("returns false when value is %s", (value: Message<Record<string, unknown>>) => {
expect(isExternalMessage(value)).toBe(false);
});
});
});

View File

@@ -0,0 +1,20 @@
import { map } from "rxjs";
import { EXTERNAL_SOURCE_TAG } from "./is-external-message";
import { CommandDefinition } from "./types";
export const getCommand = (
commandDefinition: CommandDefinition<Record<string, unknown>> | string,
) => {
if (typeof commandDefinition === "string") {
return commandDefinition;
} else {
return commandDefinition.command;
}
};
export const tagAsExternal = <T extends Record<PropertyKey, unknown>>() => {
return map((message: T) => {
return Object.assign(message, { [EXTERNAL_SOURCE_TAG]: true });
});
};

View File

@@ -2,3 +2,7 @@ export { MessageListener } from "./message.listener";
export { MessageSender } from "./message.sender";
export { Message, CommandDefinition } from "./types";
export { isExternalMessage, EXTERNAL_SOURCE_TAG } from "./is-external-message";
// Internal implementations
export { SubjectMessageSender } from "./subject-message.sender";
export { tagAsExternal, getCommand } from "./helpers";

View File

@@ -0,0 +1,58 @@
import { bufferCount, firstValueFrom, Subject } from "rxjs";
import { SubjectMessageSender } from "./subject-message.sender";
import { CommandDefinition, Message } from "./types";
describe("SubjectMessageSender", () => {
const subject = new Subject<Message<{ test: number }>>();
const subjectObservable = subject.asObservable();
const sut = new SubjectMessageSender(subject);
describe("send", () => {
it("will send message with command from message definition", async () => {
const commandDefinition = new CommandDefinition<{ test: number }>("myCommand");
const emissionsPromise = firstValueFrom(subjectObservable.pipe(bufferCount(1)));
sut.send(commandDefinition, { test: 1 });
const emissions = await emissionsPromise;
expect(emissions[0]).toEqual({ command: "myCommand", test: 1 });
});
it("will send message with command from normal string", async () => {
const emissionsPromise = firstValueFrom(subjectObservable.pipe(bufferCount(1)));
sut.send("myCommand", { test: 1 });
const emissions = await emissionsPromise;
expect(emissions[0]).toEqual({ command: "myCommand", test: 1 });
});
it("will send message with object even if payload not given", async () => {
const emissionsPromise = firstValueFrom(subjectObservable.pipe(bufferCount(1)));
sut.send("myCommand");
const emissions = await emissionsPromise;
expect(emissions[0]).toEqual({ command: "myCommand" });
});
it.each([null, undefined])(
"will send message with object even if payload is null-ish (%s)",
async (payloadValue) => {
const emissionsPromise = firstValueFrom(subjectObservable.pipe(bufferCount(1)));
sut.send("myCommand", payloadValue);
const emissions = await emissionsPromise;
expect(emissions[0]).toEqual({ command: "myCommand" });
},
);
});
});

View File

@@ -0,0 +1,17 @@
import { Subject } from "rxjs";
import { getCommand } from "./helpers";
import { MessageSender } from "./message.sender";
import { CommandDefinition, Message } from "./types";
export class SubjectMessageSender implements MessageSender {
constructor(private readonly messagesSubject: Subject<Message<Record<string, unknown>>>) {}
send<T extends Record<string, unknown>>(
commandDefinition: string | CommandDefinition<T>,
payload: Record<string, unknown> | T = {},
): void {
const command = getCommand(commandDefinition);
this.messagesSubject.next(Object.assign(payload ?? {}, { command: command }));
}
}

View File

@@ -5,12 +5,6 @@
"declaration": true,
"types": ["node"]
},
"include": [
"src/**/*.ts",
"../messaging-internal/src/subject-message.sender.spec.ts",
"../messaging-internal/src/subject-message.sender.ts",
"../messaging-internal/src/helpers.spec.ts",
"../messaging-internal/src/helpers.ts"
],
"include": ["src/**/*.ts"],
"exclude": ["jest.config.js", "src/**/*.spec.ts"]
}