222 lines
5.7 KiB
TypeScript
222 lines
5.7 KiB
TypeScript
|
import EventEmitter from "eventemitter3";
|
||
|
import MockPeer from "./MockPeer";
|
||
|
import { PeerConnectOption } from "peerjs";
|
||
|
|
||
|
export class MockDataConnection extends EventEmitter {
|
||
|
public options: PeerConnectOption;
|
||
|
public peer: string;
|
||
|
public provider: MockPeer;
|
||
|
public open: boolean = false;
|
||
|
private _connection?: MockDataConnection;
|
||
|
public id: string;
|
||
|
public label: string;
|
||
|
|
||
|
public constructor(
|
||
|
peer: string,
|
||
|
provider: MockPeer,
|
||
|
options?: PeerConnectOption
|
||
|
) {
|
||
|
super();
|
||
|
this.options = {
|
||
|
serialization: "binary",
|
||
|
reliable: false
|
||
|
};
|
||
|
if (options) {
|
||
|
this.options = Object.assign(this.options, options);
|
||
|
}
|
||
|
this.id = "fake_" + Math.random().toString(32);
|
||
|
this.label = this.options.label ? this.options.label : this.id;
|
||
|
this.provider = provider;
|
||
|
this.peer = peer;
|
||
|
}
|
||
|
|
||
|
public send(message: any) {
|
||
|
if (!this.open) {
|
||
|
this.emit(
|
||
|
"error",
|
||
|
new Error(
|
||
|
"Connection is not open. You should listen for the `open` event before sending messages."
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
const connection = this._connection;
|
||
|
if (connection) {
|
||
|
connection.receive(message);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public receive(message: any) {
|
||
|
this.emit("data", message);
|
||
|
}
|
||
|
|
||
|
public close() {
|
||
|
if (!this.open) {
|
||
|
return;
|
||
|
}
|
||
|
this.open = false;
|
||
|
this.emit("close");
|
||
|
}
|
||
|
|
||
|
public _setRemote(connection: MockDataConnection) {
|
||
|
this._connection = connection;
|
||
|
this._connection.on("open", () => {
|
||
|
if (!this.open) {
|
||
|
this.open = true;
|
||
|
this.emit("open");
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
public _configureDataChannel(
|
||
|
mocks: Record<string, MockPeer>,
|
||
|
options?: PeerConnectOption
|
||
|
) {
|
||
|
if (!this._connection && this.peer in mocks) {
|
||
|
this._setRemote(
|
||
|
mocks[this.peer]._negotiate(this.provider.id, this, options)
|
||
|
);
|
||
|
this.open = true;
|
||
|
this.emit("open");
|
||
|
} else {
|
||
|
throw new Error(`Peer(${this.peer}) not found`);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public get metadata() {
|
||
|
return this.options.metadata;
|
||
|
}
|
||
|
|
||
|
public get reliable() {
|
||
|
return this.options.reliable ? this.options.reliable : false;
|
||
|
}
|
||
|
|
||
|
public get serialization() {
|
||
|
return this.options.serialization ? this.options.serialization : "binary";
|
||
|
}
|
||
|
|
||
|
public get peerConnection() {
|
||
|
return this._connection;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
UNIMPLEMENTED STUFF
|
||
|
*/
|
||
|
|
||
|
public dataChannel: unknown;
|
||
|
public bufferSize: unknown;
|
||
|
public type: unknown;
|
||
|
public canTrickleIceCandidates: unknown;
|
||
|
public connectionState: unknown;
|
||
|
public currentLocalDescription: unknown;
|
||
|
public currentRemoteDescription: unknown;
|
||
|
public iceConnectionState: unknown;
|
||
|
public iceGatheringState: unknown;
|
||
|
public idpErrorInfo: unknown;
|
||
|
public idpLoginUrl: unknown;
|
||
|
public localDescription: unknown;
|
||
|
public onconnectionstatechange: unknown;
|
||
|
public ondatachannel: unknown;
|
||
|
public onicecandidate: unknown;
|
||
|
public onicecandidateerror: unknown;
|
||
|
public oniceconnectionstatechange: unknown;
|
||
|
public onicegatheringstatechange: unknown;
|
||
|
public onnegotiationneeded: unknown;
|
||
|
public onsignalingstatechange: unknown;
|
||
|
public onstatsended: unknown;
|
||
|
public ontrack: unknown;
|
||
|
public peerIdentity: unknown;
|
||
|
public pendingLocalDescription: unknown;
|
||
|
public pendingRemoteDescription: unknown;
|
||
|
public remoteDescription: unknown;
|
||
|
public sctp: unknown;
|
||
|
public signalingState: unknown;
|
||
|
|
||
|
public addIceCandidate(
|
||
|
candidate: RTCIceCandidateInit | RTCIceCandidate
|
||
|
): Promise<void> {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public addTrack(track: MediaStreamTrack, ...streams: MediaStream[]): unknown {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public addTransceiver(
|
||
|
trackOrKind: MediaStreamTrack | string,
|
||
|
init?: RTCRtpTransceiverInit
|
||
|
): unknown {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public createAnswer(options?: RTCOfferOptions): Promise<unknown> {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public createDataChannel(
|
||
|
label: string,
|
||
|
dataChannelDict?: RTCDataChannelInit
|
||
|
): unknown {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public createOffer(options?: RTCOfferOptions): Promise<unknown> {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public getConfiguration(): unknown {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public getIdentityAssertion(): Promise<string> {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public getReceivers(): unknown[] {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public getSenders(): unknown[] {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public getStats(selector?: MediaStreamTrack | null): Promise<RTCStatsReport> {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public getTransceivers(): unknown[] {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public removeTrack(sender: RTCRtpSender): void {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public setConfiguration(configuration: RTCConfiguration): void {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public setIdentityProvider(
|
||
|
provider: string,
|
||
|
options?: RTCIdentityProviderOptions
|
||
|
) {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public setLocalDescription(
|
||
|
description: RTCSessionDescriptionInit
|
||
|
): Promise<void> {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
public setRemoteDescription(
|
||
|
description: RTCSessionDescriptionInit
|
||
|
): Promise<void> {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
|
||
|
addEventListener(
|
||
|
type: string,
|
||
|
listener: EventListenerOrEventListenerObject,
|
||
|
options?: boolean | AddEventListenerOptions
|
||
|
): void {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
removeEventListener(
|
||
|
type: string,
|
||
|
listener: EventListenerOrEventListenerObject,
|
||
|
options?: boolean | EventListenerOptions
|
||
|
): void {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
|
||
|
dispatchEvent(event: Event): boolean {
|
||
|
throw new Error("not implemented");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default MockDataConnection;
|