Skip to content

Commit

Permalink
Fix ESLint errors in Mock Hub (#19301)
Browse files Browse the repository at this point in the history
Part of work for #17826
  • Loading branch information
timovv authored Dec 14, 2021
1 parent 4e6b5b8 commit 0c1a47c
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 137 deletions.
5 changes: 3 additions & 2 deletions sdk/eventhub/mock-hub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"main": "dist/index.js",
"types": "types/index.d.ts",
"sideEffects": false,
"scripts": {
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
"build": "npm run clean && tsc -p .",
Expand All @@ -24,8 +25,8 @@
"integration-test:browser": "echo skipped",
"integration-test:node": "echo skipped",
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
"lint:fix": "echo skipped",
"lint": "eslint package.json src --ext .ts -f html -o mock-hub-lintReport.html || exit 0",
"lint:fix": "eslint --no-eslintrc -c ../../eslintrc.internal.json package.json src --ext .ts --fix --fix-type [problem,suggestion]",
"lint": "eslint --no-eslintrc -c ../../.eslintrc.internal.json package.json src --ext .ts",
"pack": "npm pack 2>&1",
"prepare": "npm run build",
"test": "echo \"No tests implemented\"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Message, types } from "rhea";

/**
* Checks whether the provided message is requesting the partition info from the Event Hub.
* @param entityPath The path the client sent the request to.
* @param entityPath - The path the client sent the request to.
* Expected to be `$management` if the message is requesting runtime info.
* @param message The message sent by the client.
* @param message - The message sent by the client.
*/
export function isPartitionInfo(entityPath: string, message: Message): boolean {
if (entityPath !== "$management") {
Expand Down
4 changes: 2 additions & 2 deletions sdk/eventhub/mock-hub/src/messages/event-hubs/runtimeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Message, types } from "rhea";

/**
* Checks whether the provided message is requesting the EventHub's runtime info.
* @param entityPath The path the client sent the request to.
* @param entityPath - The path the client sent the request to.
* Expected to be `$management` if the message is requesting runtime info.
* @param message The message sent by the client.
* @param message - The message sent by the client.
*/
export function isHubRuntimeInfo(entityPath: string, message: Message): boolean {
if (entityPath !== "$management") {
Expand Down
31 changes: 16 additions & 15 deletions sdk/eventhub/mock-hub/src/sender/streamingPartitionSender.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { Sender, SenderEvents, DeliveryAnnotations, types } from "rhea";
import { AbortController, AbortSignalLike, AbortError } from "@azure/abort-controller";

import { MessageStore, MessageRecord } from "../storage/messageStore";
import { AbortController, AbortError, AbortSignalLike } from "@azure/abort-controller";
import { DeliveryAnnotations, Sender, SenderEvents, types } from "rhea";
import { MessageRecord, MessageStore } from "../storage/messageStore";
import { EventPosition } from "../utils/eventPosition";
import { Message } from "rhea";

Expand All @@ -23,11 +22,11 @@ export class StreamingPartitionSender {

/**
* Instantiates a `StreamingPartitionSender`.
* @param messageStore The `MessageStore` that contains all of the messages sent to the service.
* @param sender The sender link that should be used to send messages to.
* @param partitionId Specifies which partition to send messages from.
* @param startPosition Specifies which message to start iterating from.
* @param enableRuntimeMetric Indicates whether partition info should be sent on each event.
* @param messageStore - The `MessageStore` that contains all of the messages sent to the service.
* @param sender - The sender link that should be used to send messages to.
* @param partitionId - Specifies which partition to send messages from.
* @param startPosition - Specifies which message to start iterating from.
* @param enableRuntimeMetric - Indicates whether partition info should be sent on each event.
*/
constructor(
messageStore: MessageStore,
Expand All @@ -46,7 +45,7 @@ export class StreamingPartitionSender {
/**
* Starts sending messages.
*/
start() {
start(): void {
this._sendMessages().catch((err) => {
console.error(`Unexpected error while sending messages`, err);
});
Expand All @@ -55,11 +54,11 @@ export class StreamingPartitionSender {
/**
* Stops sending messages.
*/
stop() {
stop(): void {
this._abortController.abort();
}

private async _sendMessages() {
private async _sendMessages(): Promise<void> {
const abortSignal = this._abortController.signal;
const iterator = this._messageIterator;
const sender = this._sender;
Expand Down Expand Up @@ -112,7 +111,7 @@ export class StreamingPartitionSender {
// And away it goes!
sender.send(outgoingMessage);
} catch (err) {
if ((err as any)?.name !== "AbortError") {
if (err?.name !== "AbortError") {
console.error(`Unexpected error while streaming events: `, err);
}
}
Expand All @@ -121,15 +120,17 @@ export class StreamingPartitionSender {

private _waitForSendable(sender: Sender, abortSignal: AbortSignalLike): Promise<void> {
return new Promise((resolve, reject) => {
const onAbort = () => {
const onAbort = (): void => {
sender.removeListener(SenderEvents.sendable, onSendable);
abortSignal.removeEventListener("abort", onAbort);
reject(new AbortError("Cancelled operation."));
};
const onSendable = () => {

const onSendable = (): void => {
abortSignal.removeEventListener("abort", onAbort);
resolve();
};

sender.once(SenderEvents.sendable, onSendable);

abortSignal.addEventListener("abort", onAbort);
Expand Down
62 changes: 37 additions & 25 deletions sdk/eventhub/mock-hub/src/server/mockServer.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { ListenOptions } from "net";
import { EventEmitter } from "events";
import {
ConnectionError,
ConnectionEvents,
ConnectionOptions,
Container,
EventContext,
Message,
Receiver,
ReceiverEvents,
Sender,
create_container,
SenderEvents,
Receiver,
ConnectionEvents,
ConnectionError
create_container
} from "rhea";
import { EventEmitter } from "events";
import { ListenOptions } from "net";
import { convertBufferToMessages } from "../utils/convertBufferToMessage";

export interface MockServerOptions {
Expand Down Expand Up @@ -148,6 +148,8 @@ export class MockServer extends EventEmitter {
return new Promise((resolve, reject) => {
const options = this._options;
const ONE_MB = 1024 * 1024;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const listenOptions: ListenOptions & ConnectionOptions & any = {
port: options.port ?? 0,
max_frame_size: 65536,
Expand Down Expand Up @@ -183,6 +185,7 @@ export class MockServer extends EventEmitter {
emit(type: "receiverClose", event: ReceiverCloseEvent): boolean;
emit(type: "senderClose", event: SenderCloseEvent): boolean;
emit(type: "connectionClose", event: ConnectionCloseEvent): boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
emit(type: string, event: any): boolean {
return super.emit(type, event);
}
Expand All @@ -191,56 +194,57 @@ export class MockServer extends EventEmitter {
* Add new "receiverOpen" event listener.
* This event indicates when the remote peer has created a `Sender`
* and the server creates a `Receiver` link in response.
* @param type "receiverOpen"
* @param listener
* @param type - "receiverOpen"
* @param listener -
*/
public on(type: "receiverOpen", listener: (event: ReceiverOpenEvent) => void): this;
/**
* Add new "receiverClose" event listener.
* This event indicates when the remote peer has closed a `Sender`
* and the server closes a `Receiver` link in response.
* @param type "receiverClose"
* @param listener
* @param type - "receiverClose"
* @param listener -
*/
public on(type: "receiverClose", listener: (event: ReceiverCloseEvent) => void): this;
/**
* Add new "connectionOpen" event listener.
* This event indicates when the remote peer has created a connection to the server.
* @param type "connectionOpen"
* @param listener
* @param type - "connectionOpen"
* @param listener -
*/
public on(type: "connectionOpen", listener: (event: ConnectionOpenEvent) => void): this;
/**
* Add new "senderOpen" event listener.
* This event indicates when the remote peer has created a `Receiver`
* and the server creates a `Sender` link in response.
* @param type "senderOpen"
* @param listener
* @param type - "senderOpen"
* @param listener -
*/
public on(type: "senderOpen", listener: (event: SenderOpenEvent) => void): this;
/**
* Add new "senderClose" event listener.
* This event indicates when the remote peer has closed a `Receiver`
* and the server closes a `Sender` link in response.
* @param type "senderClose"
* @param listener
* @param type - "senderClose"
* @param listener -
*/
public on(type: "senderClose", listener: (event: SenderCloseEvent) => void): this;
/**
* Add new "connectionClose" event listener.
* This event indicates when the remote peer has closed a connection to the server.
* @param type "connectionClose"
* @param listener
* @param type - "connectionClose"
* @param listener -
*/
public on(type: "connectionClose", listener: (event: ConnectionCloseEvent) => void): this;
/**
* Add new "onMessage" event listener.
* This event indicates when the server has received a message from a remote peer.
* Messages are received over a `Receiver` link.
* @param type "connectionClose"
* @param listener
* @param type - "connectionClose"
* @param listener -
*/
public on(type: "onMessages", listener: (event: OnMessagesEvent) => void): this;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public on(type: string, listener: (event: any) => void): this {
return super.on(type, listener);
}
Expand All @@ -267,14 +271,18 @@ export class MockServer extends EventEmitter {
});
}

private _setupDefaultListeners() {
private _setupDefaultListeners(): void {
this._container.sasl_server_mechanisms.enable_anonymous();
this._container.sasl.server_add_external(this._container.sasl_server_mechanisms);
this._container.sasl_server_mechanisms["MSSBCBS"] = this._container.sasl_server_mechanisms[
"EXTERNAL"
];
this._container.on(ConnectionEvents.connectionError, () => {});
this._container.on(ConnectionEvents.protocolError, () => {});
this._container.on(ConnectionEvents.connectionError, () => {
/* do nothing */
});
this._container.on(ConnectionEvents.protocolError, () => {
/* do nothing */
});
this._container.on(ConnectionEvents.connectionOpen, (context: EventContext) => {
context.connection.on("error", function(this: typeof context.connection, err: Error) {
console.log(`Error occurred on connection:`, err?.message);
Expand Down Expand Up @@ -358,7 +366,7 @@ export class MockServer extends EventEmitter {
return incomingMessages;
}

private _handleMessage = (context: EventContext) => {
private _handleMessage = (context: EventContext): void => {
if (!context.message || !context.receiver) {
return;
}
Expand All @@ -375,7 +383,11 @@ export class MockServer extends EventEmitter {
});
};

private _sendMessage = (context: EventContext, outgoingMessage: Message, toLinkName?: string) => {
private _sendMessage = (
context: EventContext,
outgoingMessage: Message,
toLinkName?: string
): void => {
const sender = context.connection.find_sender(
(s: Sender) => s.name === toLinkName || s.target.address === toLinkName
);
Expand Down
Loading

0 comments on commit 0c1a47c

Please sign in to comment.