mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 08:13:42 +00:00
Add messaging & messaging-internal libraries (#15711)
This commit is contained in:
47
libs/messaging-internal/src/helpers.spec.ts
Normal file
47
libs/messaging-internal/src/helpers.spec.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Subject, firstValueFrom } from "rxjs";
|
||||
|
||||
import { CommandDefinition, isExternalMessage, Message } from "@bitwarden/messaging";
|
||||
|
||||
import { getCommand, tagAsExternal } from "./helpers";
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
19
libs/messaging-internal/src/helpers.ts
Normal file
19
libs/messaging-internal/src/helpers.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { map } from "rxjs";
|
||||
|
||||
import { CommandDefinition, EXTERNAL_SOURCE_TAG } from "@bitwarden/messaging";
|
||||
|
||||
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 });
|
||||
});
|
||||
};
|
||||
5
libs/messaging-internal/src/index.ts
Normal file
5
libs/messaging-internal/src/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
// Built in implementations
|
||||
export { SubjectMessageSender } from "./subject-message.sender";
|
||||
|
||||
// Helpers meant to be used only by other implementations
|
||||
export { tagAsExternal, getCommand } from "./helpers";
|
||||
8
libs/messaging-internal/src/messaging-internal.spec.ts
Normal file
8
libs/messaging-internal/src/messaging-internal.spec.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import * as lib from "./index";
|
||||
|
||||
describe("messaging-internal", () => {
|
||||
// This test will fail until something is exported from index.ts
|
||||
it("should work", () => {
|
||||
expect(lib).toBeDefined();
|
||||
});
|
||||
});
|
||||
59
libs/messaging-internal/src/subject-message.sender.spec.ts
Normal file
59
libs/messaging-internal/src/subject-message.sender.spec.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { bufferCount, firstValueFrom, Subject } from "rxjs";
|
||||
|
||||
import { CommandDefinition, Message } from "@bitwarden/messaging";
|
||||
|
||||
import { SubjectMessageSender } from "./subject-message.sender";
|
||||
|
||||
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" });
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
17
libs/messaging-internal/src/subject-message.sender.ts
Normal file
17
libs/messaging-internal/src/subject-message.sender.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Subject } from "rxjs";
|
||||
|
||||
import { CommandDefinition, Message, MessageSender } from "@bitwarden/messaging";
|
||||
|
||||
import { getCommand } from "./helpers";
|
||||
|
||||
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 }));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user