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

Add messaging & messaging-internal libraries (#15711)

This commit is contained in:
Justin Baur
2025-07-22 11:47:25 -04:00
committed by GitHub
parent e99abb49ec
commit a563e6d910
39 changed files with 347 additions and 105 deletions

2
.github/CODEOWNERS vendored
View File

@@ -94,6 +94,8 @@ libs/platform @bitwarden/team-platform-dev
libs/storage-core @bitwarden/team-platform-dev
libs/logging @bitwarden/team-platform-dev
libs/storage-test-utils @bitwarden/team-platform-dev
libs/messaging @bitwarden/team-platform-dev
libs/messaging-internal @bitwarden/team-platform-dev
# Web utils used across app and connectors
apps/web/src/utils/ @bitwarden/team-platform-dev
# Web core and shared files

View File

@@ -3,10 +3,8 @@ import { Subject, firstValueFrom } from "rxjs";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { MessageListener, MessageSender } from "@bitwarden/common/platform/messaging";
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
// eslint-disable-next-line no-restricted-imports
import { tagAsExternal } from "@bitwarden/common/platform/messaging/helpers";
import { SyncService } from "@bitwarden/common/vault/abstractions/sync/sync.service.abstraction";
import { tagAsExternal } from "@bitwarden/messaging-internal";
import { FullSyncMessage } from "./foreground-sync.service";
import { FULL_SYNC_FINISHED, SyncServiceListener } from "./sync-service.listener";

View File

@@ -1,3 +1,3 @@
// Export the new message sender as the legacy MessagingService to minimize changes in the initial PR,
// team specific PR's will come after.
export { MessageSender as MessagingService } from "../messaging/message.sender";
export { MessageSender as MessagingService } from "@bitwarden/messaging";

View File

@@ -1,4 +1 @@
export { MessageListener } from "./message.listener";
export { MessageSender } from "./message.sender";
export { Message, CommandDefinition } from "./types";
export { isExternalMessage } from "./helpers";
export * from "@bitwarden/messaging";

View File

@@ -1,5 +1 @@
// Built in implementations
export { SubjectMessageSender } from "./subject-message.sender";
// Helpers meant to be used only by other implementations
export { tagAsExternal, getCommand } from "./helpers";
export * from "@bitwarden/messaging-internal";

View File

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

View File

@@ -0,0 +1,5 @@
# messaging-internal
Owned by: platform
Internal details to accompany @bitwarden/messaging this library should not be consumed in non-platform code.

View File

@@ -0,0 +1,3 @@
import baseConfig from "../../eslint.config.mjs";
export default [...baseConfig];

View File

@@ -0,0 +1,10 @@
module.exports = {
displayName: "messaging-internal",
preset: "../../jest.preset.js",
testEnvironment: "node",
transform: {
"^.+\\.[tj]s$": ["ts-jest", { tsconfig: "<rootDir>/tsconfig.spec.json" }],
},
moduleFileExtensions: ["ts", "js", "html"],
coverageDirectory: "../../coverage/libs/messaging-internal",
};

View File

@@ -0,0 +1,11 @@
{
"name": "@bitwarden/messaging-internal",
"version": "0.0.1",
"description": "Internal details to accompany @bitwarden/messaging this library should not be consumed in non-platform code.",
"private": true,
"type": "commonjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "GPL-3.0",
"author": "platform"
}

View File

@@ -0,0 +1,33 @@
{
"name": "messaging-internal",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/messaging-internal/src",
"projectType": "library",
"tags": [],
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/messaging-internal",
"main": "libs/messaging-internal/src/index.ts",
"tsConfig": "libs/messaging-internal/tsconfig.lib.json",
"assets": ["libs/messaging-internal/*.md"]
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["libs/messaging-internal/**/*.ts"]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/messaging-internal/jest.config.js"
}
}
}
}

View File

@@ -1,7 +1,8 @@
import { Subject, firstValueFrom } from "rxjs";
import { getCommand, isExternalMessage, tagAsExternal } from "./helpers";
import { Message, CommandDefinition } from "./types";
import { CommandDefinition, isExternalMessage, Message } from "@bitwarden/messaging";
import { getCommand, tagAsExternal } from "./helpers";
describe("helpers", () => {
describe("getCommand", () => {

View File

@@ -1,6 +1,6 @@
import { map } from "rxjs";
import { CommandDefinition } from "./types";
import { CommandDefinition, EXTERNAL_SOURCE_TAG } from "@bitwarden/messaging";
export const getCommand = (
commandDefinition: CommandDefinition<Record<string, unknown>> | string,
@@ -12,12 +12,6 @@ export const getCommand = (
}
};
export const EXTERNAL_SOURCE_TAG = Symbol("externalSource");
export const isExternalMessage = (message: Record<PropertyKey, unknown>) => {
return message?.[EXTERNAL_SOURCE_TAG] === true;
};
export const tagAsExternal = <T extends Record<PropertyKey, unknown>>() => {
return map((message: T) => {
return Object.assign(message, { [EXTERNAL_SOURCE_TAG]: true });

View 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";

View 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();
});
});

View 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" });
},
);
});
});

View File

@@ -1,8 +1,8 @@
import { Subject } from "rxjs";
import { getCommand } from "./internal";
import { MessageSender } from "./message.sender";
import { Message, CommandDefinition } from "./types";
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>>>) {}

View File

@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": ["src/**/*.ts", "src/**/*.js"],
"exclude": ["**/build", "**/dist"]
}

View File

@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}

View File

@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.js", "src/**/*.spec.ts"]
}

View File

@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"moduleResolution": "node10",
"types": ["jest", "node"]
},
"include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"]
}

5
libs/messaging/README.md Normal file
View File

@@ -0,0 +1,5 @@
# messaging
Owned by: platform
Services for sending and recieving messages from different contexts of the same application.

View File

@@ -0,0 +1,3 @@
import baseConfig from "../../eslint.config.mjs";
export default [...baseConfig];

View File

@@ -0,0 +1,10 @@
module.exports = {
displayName: "messaging",
preset: "../../jest.preset.js",
testEnvironment: "node",
transform: {
"^.+\\.[tj]s$": ["ts-jest", { tsconfig: "<rootDir>/tsconfig.spec.json" }],
},
moduleFileExtensions: ["ts", "js", "html"],
coverageDirectory: "../../coverage/libs/messaging",
};

View File

@@ -0,0 +1,11 @@
{
"name": "@bitwarden/messaging",
"version": "0.0.1",
"description": "Services for sending and recieving messages from different contexts of the same application.",
"private": true,
"type": "commonjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "GPL-3.0",
"author": "platform"
}

View File

@@ -0,0 +1,33 @@
{
"name": "messaging",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/messaging/src",
"projectType": "library",
"tags": [],
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/messaging",
"main": "libs/messaging/src/index.ts",
"tsConfig": "libs/messaging/tsconfig.lib.json",
"assets": ["libs/messaging/*.md"]
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["libs/messaging/**/*.ts"]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/messaging/jest.config.js"
}
}
}
}

View File

@@ -0,0 +1,4 @@
export { MessageListener } from "./message.listener";
export { MessageSender } from "./message.sender";
export { Message, CommandDefinition } from "./types";
export { isExternalMessage, EXTERNAL_SOURCE_TAG } from "./is-external-message";

View File

@@ -0,0 +1,5 @@
export const EXTERNAL_SOURCE_TAG = Symbol("externalSource");
export const isExternalMessage = (message: Record<PropertyKey, unknown>) => {
return message?.[EXTERNAL_SOURCE_TAG] === true;
};

View File

@@ -1,6 +1,4 @@
import { Subject } from "rxjs";
import { subscribeTo } from "../../../spec/observable-tracker";
import { bufferCount, firstValueFrom, Subject } from "rxjs";
import { MessageListener } from "./message.listener";
import { Message, CommandDefinition } from "./types";
@@ -13,35 +11,33 @@ describe("MessageListener", () => {
describe("allMessages$", () => {
it("runs on all nexts", async () => {
const tracker = subscribeTo(sut.allMessages$);
const pausePromise = tracker.pauseUntilReceived(2);
const emissionsPromise = firstValueFrom(sut.allMessages$.pipe(bufferCount(2)));
subject.next({ command: "command1", test: 1 });
subject.next({ command: "command2", test: 2 });
await pausePromise;
const emissions = await emissionsPromise;
expect(tracker.emissions[0]).toEqual({ command: "command1", test: 1 });
expect(tracker.emissions[1]).toEqual({ command: "command2", test: 2 });
expect(emissions[0]).toEqual({ command: "command1", test: 1 });
expect(emissions[1]).toEqual({ command: "command2", test: 2 });
});
});
describe("messages$", () => {
it("runs on only my commands", async () => {
const tracker = subscribeTo(sut.messages$(testCommandDefinition));
const pausePromise = tracker.pauseUntilReceived(2);
const emissionsPromise = firstValueFrom(
sut.messages$(testCommandDefinition).pipe(bufferCount(2)),
);
subject.next({ command: "notMyCommand", test: 1 });
subject.next({ command: "myCommand", test: 2 });
subject.next({ command: "myCommand", test: 3 });
subject.next({ command: "notMyCommand", test: 4 });
await pausePromise;
const emissions = await emissionsPromise;
expect(tracker.emissions[0]).toEqual({ command: "myCommand", test: 2 });
expect(tracker.emissions[1]).toEqual({ command: "myCommand", test: 3 });
expect(emissions[0]).toEqual({ command: "myCommand", test: 2 });
expect(emissions[1]).toEqual({ command: "myCommand", test: 3 });
});
});
});

View File

@@ -0,0 +1,8 @@
import * as lib from "./index";
describe("messaging", () => {
// This test will fail until something is exported from index.ts
it("should work", () => {
expect(lib).toBeDefined();
});
});

View File

@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": ["src/**/*.ts", "src/**/*.js"],
"exclude": ["**/build", "**/dist"]
}

View File

@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}

View File

@@ -0,0 +1,16 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"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"
],
"exclude": ["jest.config.js", "src/**/*.spec.ts"]
}

View File

@@ -0,0 +1,17 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"moduleResolution": "node10",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts",
"../messaging-internal/src/subject-message.sender.spec.ts",
"../messaging-internal/src/helpers.spec.ts"
]
}

17
package-lock.json generated
View File

@@ -352,6 +352,15 @@
"version": "0.0.1",
"license": "GPL-3.0"
},
"libs/messaging": {
"name": "@bitwarden/messaging",
"version": "0.0.1",
"license": "GPL-3.0"
},
"libs/messaging-internal": {
"version": "0.0.1",
"license": "GPL-3.0"
},
"libs/node": {
"name": "@bitwarden/node",
"version": "0.0.0",
@@ -4591,6 +4600,14 @@
"resolved": "libs/logging",
"link": true
},
"node_modules/@bitwarden/messaging": {
"resolved": "libs/messaging",
"link": true
},
"node_modules/@bitwarden/messaging-internal": {
"resolved": "libs/messaging-internal",
"link": true
},
"node_modules/@bitwarden/node": {
"resolved": "libs/node",
"link": true

View File

@@ -38,6 +38,8 @@
"@bitwarden/key-management": ["./libs/key-management/src"],
"@bitwarden/key-management-ui": ["./libs/key-management-ui/src"],
"@bitwarden/logging": ["libs/logging/src"],
"@bitwarden/messaging": ["libs/messaging/src/index.ts"],
"@bitwarden/messaging-internal": ["libs/messaging-internal/src/index.ts"],
"@bitwarden/node/*": ["./libs/node/src/*"],
"@bitwarden/nx-plugin": ["libs/nx-plugin/src/index.ts"],
"@bitwarden/platform": ["./libs/platform/src"],