|
@@ -1,4 +1,3 @@
|
|
|
-import { EventEmitter } from "eventemitter3";
|
|
|
import { util } from "./util";
|
|
|
import logger, { LogLevel } from "./logger";
|
|
|
import { Socket } from "./socket";
|
|
@@ -21,6 +20,8 @@ import { BinaryPack } from "./dataconnection/BufferedConnection/BinaryPack";
|
|
|
import { Raw } from "./dataconnection/BufferedConnection/Raw";
|
|
|
import { Json } from "./dataconnection/BufferedConnection/Json";
|
|
|
|
|
|
+import { EventEmitterWithError, PeerError } from "./peerError";
|
|
|
+
|
|
|
class PeerOptions implements PeerJSOption {
|
|
|
/**
|
|
|
* Prints log messages depending on the debug level passed in.
|
|
@@ -66,21 +67,7 @@ class PeerOptions implements PeerJSOption {
|
|
|
serializers?: SerializerMapping;
|
|
|
}
|
|
|
|
|
|
-class PeerError extends Error {
|
|
|
- constructor(type: PeerErrorType, err: Error | string) {
|
|
|
- if (typeof err === "string") {
|
|
|
- super(err);
|
|
|
- } else {
|
|
|
- super();
|
|
|
- Object.assign(this, err);
|
|
|
- }
|
|
|
-
|
|
|
- this.type = type;
|
|
|
- }
|
|
|
-
|
|
|
- type: PeerErrorType;
|
|
|
-}
|
|
|
-export type { PeerError, PeerOptions };
|
|
|
+export { type PeerOptions };
|
|
|
|
|
|
export type SerializerMapping = {
|
|
|
[key: string]: new (
|
|
@@ -90,7 +77,7 @@ export type SerializerMapping = {
|
|
|
) => DataConnection;
|
|
|
};
|
|
|
|
|
|
-export type PeerEvents = {
|
|
|
+export interface PeerEvents {
|
|
|
/**
|
|
|
* Emitted when a connection to the PeerServer is established.
|
|
|
*
|
|
@@ -118,12 +105,12 @@ export type PeerEvents = {
|
|
|
*
|
|
|
* Errors from the underlying socket and PeerConnections are forwarded here.
|
|
|
*/
|
|
|
- error: (error: PeerError) => void;
|
|
|
-};
|
|
|
+ error: (error: PeerError<`${PeerErrorType}`>) => void;
|
|
|
+}
|
|
|
/**
|
|
|
* A peer who can initiate connections with other peers.
|
|
|
*/
|
|
|
-export class Peer extends EventEmitter<PeerEvents> {
|
|
|
+export class Peer extends EventEmitterWithError<PeerErrorType, PeerEvents> {
|
|
|
private static readonly DEFAULT_KEY = "peerjs";
|
|
|
|
|
|
protected readonly _serializers: SerializerMapping = {
|
|
@@ -500,7 +487,7 @@ export class Peer extends EventEmitter<PeerEvents> {
|
|
|
* @param peer The brokering ID of the remote peer (their {@apilink Peer.id}).
|
|
|
* @param options for specifying details about Peer Connection
|
|
|
*/
|
|
|
- connect(peer: string, options: PeerConnectOption): DataConnection {
|
|
|
+ connect(peer: string, options: PeerConnectOption = {}): DataConnection {
|
|
|
options = {
|
|
|
serialization: "default",
|
|
|
...options,
|
|
@@ -640,13 +627,6 @@ export class Peer extends EventEmitter<PeerEvents> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /** Emits a typed error message. */
|
|
|
- emitError(type: PeerErrorType, err: string | Error): void {
|
|
|
- logger.error("Error:", err);
|
|
|
-
|
|
|
- this.emit("error", new PeerError(type, err));
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Destroys the Peer: closes all active connections as well as the connection
|
|
|
* to the server.
|