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:
2
.github/CODEOWNERS
vendored
2
.github/CODEOWNERS
vendored
@@ -94,6 +94,8 @@ libs/platform @bitwarden/team-platform-dev
|
|||||||
libs/storage-core @bitwarden/team-platform-dev
|
libs/storage-core @bitwarden/team-platform-dev
|
||||||
libs/logging @bitwarden/team-platform-dev
|
libs/logging @bitwarden/team-platform-dev
|
||||||
libs/storage-test-utils @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
|
# Web utils used across app and connectors
|
||||||
apps/web/src/utils/ @bitwarden/team-platform-dev
|
apps/web/src/utils/ @bitwarden/team-platform-dev
|
||||||
# Web core and shared files
|
# Web core and shared files
|
||||||
|
|||||||
@@ -3,10 +3,8 @@ import { Subject, firstValueFrom } from "rxjs";
|
|||||||
|
|
||||||
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
||||||
import { MessageListener, MessageSender } from "@bitwarden/common/platform/messaging";
|
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 { SyncService } from "@bitwarden/common/vault/abstractions/sync/sync.service.abstraction";
|
||||||
|
import { tagAsExternal } from "@bitwarden/messaging-internal";
|
||||||
|
|
||||||
import { FullSyncMessage } from "./foreground-sync.service";
|
import { FullSyncMessage } from "./foreground-sync.service";
|
||||||
import { FULL_SYNC_FINISHED, SyncServiceListener } from "./sync-service.listener";
|
import { FULL_SYNC_FINISHED, SyncServiceListener } from "./sync-service.listener";
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
// Export the new message sender as the legacy MessagingService to minimize changes in the initial PR,
|
// Export the new message sender as the legacy MessagingService to minimize changes in the initial PR,
|
||||||
// team specific PR's will come after.
|
// team specific PR's will come after.
|
||||||
export { MessageSender as MessagingService } from "../messaging/message.sender";
|
export { MessageSender as MessagingService } from "@bitwarden/messaging";
|
||||||
|
|||||||
@@ -1,4 +1 @@
|
|||||||
export { MessageListener } from "./message.listener";
|
export * from "@bitwarden/messaging";
|
||||||
export { MessageSender } from "./message.sender";
|
|
||||||
export { Message, CommandDefinition } from "./types";
|
|
||||||
export { isExternalMessage } from "./helpers";
|
|
||||||
|
|||||||
@@ -1,5 +1 @@
|
|||||||
// Built in implementations
|
export * from "@bitwarden/messaging-internal";
|
||||||
export { SubjectMessageSender } from "./subject-message.sender";
|
|
||||||
|
|
||||||
// Helpers meant to be used only by other implementations
|
|
||||||
export { tagAsExternal, getCommand } from "./helpers";
|
|
||||||
|
|||||||
@@ -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" });
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
5
libs/messaging-internal/README.md
Normal file
5
libs/messaging-internal/README.md
Normal 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.
|
||||||
3
libs/messaging-internal/eslint.config.mjs
Normal file
3
libs/messaging-internal/eslint.config.mjs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import baseConfig from "../../eslint.config.mjs";
|
||||||
|
|
||||||
|
export default [...baseConfig];
|
||||||
10
libs/messaging-internal/jest.config.js
Normal file
10
libs/messaging-internal/jest.config.js
Normal 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",
|
||||||
|
};
|
||||||
11
libs/messaging-internal/package.json
Normal file
11
libs/messaging-internal/package.json
Normal 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"
|
||||||
|
}
|
||||||
33
libs/messaging-internal/project.json
Normal file
33
libs/messaging-internal/project.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import { Subject, firstValueFrom } from "rxjs";
|
import { Subject, firstValueFrom } from "rxjs";
|
||||||
|
|
||||||
import { getCommand, isExternalMessage, tagAsExternal } from "./helpers";
|
import { CommandDefinition, isExternalMessage, Message } from "@bitwarden/messaging";
|
||||||
import { Message, CommandDefinition } from "./types";
|
|
||||||
|
import { getCommand, tagAsExternal } from "./helpers";
|
||||||
|
|
||||||
describe("helpers", () => {
|
describe("helpers", () => {
|
||||||
describe("getCommand", () => {
|
describe("getCommand", () => {
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { map } from "rxjs";
|
import { map } from "rxjs";
|
||||||
|
|
||||||
import { CommandDefinition } from "./types";
|
import { CommandDefinition, EXTERNAL_SOURCE_TAG } from "@bitwarden/messaging";
|
||||||
|
|
||||||
export const getCommand = (
|
export const getCommand = (
|
||||||
commandDefinition: CommandDefinition<Record<string, unknown>> | string,
|
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>>() => {
|
export const tagAsExternal = <T extends Record<PropertyKey, unknown>>() => {
|
||||||
return map((message: T) => {
|
return map((message: T) => {
|
||||||
return Object.assign(message, { [EXTERNAL_SOURCE_TAG]: true });
|
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" });
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
import { Subject } from "rxjs";
|
import { Subject } from "rxjs";
|
||||||
|
|
||||||
import { getCommand } from "./internal";
|
import { CommandDefinition, Message, MessageSender } from "@bitwarden/messaging";
|
||||||
import { MessageSender } from "./message.sender";
|
|
||||||
import { Message, CommandDefinition } from "./types";
|
import { getCommand } from "./helpers";
|
||||||
|
|
||||||
export class SubjectMessageSender implements MessageSender {
|
export class SubjectMessageSender implements MessageSender {
|
||||||
constructor(private readonly messagesSubject: Subject<Message<Record<string, unknown>>>) {}
|
constructor(private readonly messagesSubject: Subject<Message<Record<string, unknown>>>) {}
|
||||||
6
libs/messaging-internal/tsconfig.eslint.json
Normal file
6
libs/messaging-internal/tsconfig.eslint.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.base.json",
|
||||||
|
"files": [],
|
||||||
|
"include": ["src/**/*.ts", "src/**/*.js"],
|
||||||
|
"exclude": ["**/build", "**/dist"]
|
||||||
|
}
|
||||||
13
libs/messaging-internal/tsconfig.json
Normal file
13
libs/messaging-internal/tsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.base.json",
|
||||||
|
"files": [],
|
||||||
|
"include": [],
|
||||||
|
"references": [
|
||||||
|
{
|
||||||
|
"path": "./tsconfig.lib.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "./tsconfig.spec.json"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
10
libs/messaging-internal/tsconfig.lib.json
Normal file
10
libs/messaging-internal/tsconfig.lib.json
Normal 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"]
|
||||||
|
}
|
||||||
10
libs/messaging-internal/tsconfig.spec.json
Normal file
10
libs/messaging-internal/tsconfig.spec.json
Normal 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
5
libs/messaging/README.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# messaging
|
||||||
|
|
||||||
|
Owned by: platform
|
||||||
|
|
||||||
|
Services for sending and recieving messages from different contexts of the same application.
|
||||||
3
libs/messaging/eslint.config.mjs
Normal file
3
libs/messaging/eslint.config.mjs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import baseConfig from "../../eslint.config.mjs";
|
||||||
|
|
||||||
|
export default [...baseConfig];
|
||||||
10
libs/messaging/jest.config.js
Normal file
10
libs/messaging/jest.config.js
Normal 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",
|
||||||
|
};
|
||||||
11
libs/messaging/package.json
Normal file
11
libs/messaging/package.json
Normal 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"
|
||||||
|
}
|
||||||
33
libs/messaging/project.json
Normal file
33
libs/messaging/project.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
4
libs/messaging/src/index.ts
Normal file
4
libs/messaging/src/index.ts
Normal 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";
|
||||||
5
libs/messaging/src/is-external-message.ts
Normal file
5
libs/messaging/src/is-external-message.ts
Normal 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;
|
||||||
|
};
|
||||||
@@ -1,6 +1,4 @@
|
|||||||
import { Subject } from "rxjs";
|
import { bufferCount, firstValueFrom, Subject } from "rxjs";
|
||||||
|
|
||||||
import { subscribeTo } from "../../../spec/observable-tracker";
|
|
||||||
|
|
||||||
import { MessageListener } from "./message.listener";
|
import { MessageListener } from "./message.listener";
|
||||||
import { Message, CommandDefinition } from "./types";
|
import { Message, CommandDefinition } from "./types";
|
||||||
@@ -13,35 +11,33 @@ describe("MessageListener", () => {
|
|||||||
|
|
||||||
describe("allMessages$", () => {
|
describe("allMessages$", () => {
|
||||||
it("runs on all nexts", async () => {
|
it("runs on all nexts", async () => {
|
||||||
const tracker = subscribeTo(sut.allMessages$);
|
const emissionsPromise = firstValueFrom(sut.allMessages$.pipe(bufferCount(2)));
|
||||||
|
|
||||||
const pausePromise = tracker.pauseUntilReceived(2);
|
|
||||||
|
|
||||||
subject.next({ command: "command1", test: 1 });
|
subject.next({ command: "command1", test: 1 });
|
||||||
subject.next({ command: "command2", test: 2 });
|
subject.next({ command: "command2", test: 2 });
|
||||||
|
|
||||||
await pausePromise;
|
const emissions = await emissionsPromise;
|
||||||
|
|
||||||
expect(tracker.emissions[0]).toEqual({ command: "command1", test: 1 });
|
expect(emissions[0]).toEqual({ command: "command1", test: 1 });
|
||||||
expect(tracker.emissions[1]).toEqual({ command: "command2", test: 2 });
|
expect(emissions[1]).toEqual({ command: "command2", test: 2 });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("messages$", () => {
|
describe("messages$", () => {
|
||||||
it("runs on only my commands", async () => {
|
it("runs on only my commands", async () => {
|
||||||
const tracker = subscribeTo(sut.messages$(testCommandDefinition));
|
const emissionsPromise = firstValueFrom(
|
||||||
|
sut.messages$(testCommandDefinition).pipe(bufferCount(2)),
|
||||||
const pausePromise = tracker.pauseUntilReceived(2);
|
);
|
||||||
|
|
||||||
subject.next({ command: "notMyCommand", test: 1 });
|
subject.next({ command: "notMyCommand", test: 1 });
|
||||||
subject.next({ command: "myCommand", test: 2 });
|
subject.next({ command: "myCommand", test: 2 });
|
||||||
subject.next({ command: "myCommand", test: 3 });
|
subject.next({ command: "myCommand", test: 3 });
|
||||||
subject.next({ command: "notMyCommand", test: 4 });
|
subject.next({ command: "notMyCommand", test: 4 });
|
||||||
|
|
||||||
await pausePromise;
|
const emissions = await emissionsPromise;
|
||||||
|
|
||||||
expect(tracker.emissions[0]).toEqual({ command: "myCommand", test: 2 });
|
expect(emissions[0]).toEqual({ command: "myCommand", test: 2 });
|
||||||
expect(tracker.emissions[1]).toEqual({ command: "myCommand", test: 3 });
|
expect(emissions[1]).toEqual({ command: "myCommand", test: 3 });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
8
libs/messaging/src/messaging.spec.ts
Normal file
8
libs/messaging/src/messaging.spec.ts
Normal 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();
|
||||||
|
});
|
||||||
|
});
|
||||||
6
libs/messaging/tsconfig.eslint.json
Normal file
6
libs/messaging/tsconfig.eslint.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.base.json",
|
||||||
|
"files": [],
|
||||||
|
"include": ["src/**/*.ts", "src/**/*.js"],
|
||||||
|
"exclude": ["**/build", "**/dist"]
|
||||||
|
}
|
||||||
13
libs/messaging/tsconfig.json
Normal file
13
libs/messaging/tsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.base.json",
|
||||||
|
"files": [],
|
||||||
|
"include": [],
|
||||||
|
"references": [
|
||||||
|
{
|
||||||
|
"path": "./tsconfig.lib.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "./tsconfig.spec.json"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
16
libs/messaging/tsconfig.lib.json
Normal file
16
libs/messaging/tsconfig.lib.json
Normal 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"]
|
||||||
|
}
|
||||||
17
libs/messaging/tsconfig.spec.json
Normal file
17
libs/messaging/tsconfig.spec.json
Normal 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
17
package-lock.json
generated
@@ -352,6 +352,15 @@
|
|||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"license": "GPL-3.0"
|
"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": {
|
"libs/node": {
|
||||||
"name": "@bitwarden/node",
|
"name": "@bitwarden/node",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
@@ -4591,6 +4600,14 @@
|
|||||||
"resolved": "libs/logging",
|
"resolved": "libs/logging",
|
||||||
"link": true
|
"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": {
|
"node_modules/@bitwarden/node": {
|
||||||
"resolved": "libs/node",
|
"resolved": "libs/node",
|
||||||
"link": true
|
"link": true
|
||||||
|
|||||||
@@ -38,6 +38,8 @@
|
|||||||
"@bitwarden/key-management": ["./libs/key-management/src"],
|
"@bitwarden/key-management": ["./libs/key-management/src"],
|
||||||
"@bitwarden/key-management-ui": ["./libs/key-management-ui/src"],
|
"@bitwarden/key-management-ui": ["./libs/key-management-ui/src"],
|
||||||
"@bitwarden/logging": ["libs/logging/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/node/*": ["./libs/node/src/*"],
|
||||||
"@bitwarden/nx-plugin": ["libs/nx-plugin/src/index.ts"],
|
"@bitwarden/nx-plugin": ["libs/nx-plugin/src/index.ts"],
|
||||||
"@bitwarden/platform": ["./libs/platform/src"],
|
"@bitwarden/platform": ["./libs/platform/src"],
|
||||||
|
|||||||
Reference in New Issue
Block a user