Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding serialization types relevant to the new Dataconnection classes #1135

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ node_modules
.idea
npm-debug.log
.DS_STORE
pnpm-lock.yaml

test/output
browserstack.err
Expand Down
8 changes: 5 additions & 3 deletions lib/cborPeer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Peer, type SerializerMapping } from "./peer";
import { Peer, } from "./peer";
import { Cbor } from "./dataconnection/StreamConnection/Cbor";
import { SerializerMapping, } from "./optionInterfaces";
import { SerializationType } from "./enums";

export class CborPeer extends Peer {
protected override _defaultSerialization = SerializationType.CBOR;
override _serializers: SerializerMapping = {
Cbor,
default: Cbor,
cbor: Cbor,
};
}
3 changes: 2 additions & 1 deletion lib/dataconnection/StreamConnection/Cbor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Peer } from "../../peer.js";
import { Decoder, Encoder } from "cbor-x";
import { StreamConnection } from "./StreamConnection.js";
import { SerializationType } from "../../enums.js";

const NullValue = Symbol.for(null);

Expand All @@ -25,7 +26,7 @@ const iterateOver = async function* (stream: ReadableStream) {
};

export class Cbor extends StreamConnection {
readonly serialization = "Cbor";
readonly serialization = SerializationType.CBOR;
private _encoder = new Encoder();
private _decoder = new Decoder();
private _inc;
Expand Down
3 changes: 2 additions & 1 deletion lib/dataconnection/StreamConnection/MsgPack.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { decodeMultiStream, Encoder } from "@msgpack/msgpack";
import { StreamConnection } from "./StreamConnection.js";
import type { Peer } from "../../peer.js";
import { SerializationType } from "../../enums.js";

export class MsgPack extends StreamConnection {
readonly serialization = "MsgPack";
readonly serialization = SerializationType.MsgPack;
private _encoder = new Encoder();

constructor(peerId: string, provider: Peer, options: any) {
Expand Down
5 changes: 5 additions & 0 deletions lib/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,15 @@ export enum DataConnectionErrorType {
}

export enum SerializationType {
/// Buffered Connections
Binary = "binary",
BinaryUTF8 = "binary-utf8",
JSON = "json",
None = "raw",

/// Streaming Connections
CBOR = "cbor",
MsgPack = "msgpack",
}

export enum SocketEventType {
Expand Down
4 changes: 2 additions & 2 deletions lib/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export type {
export type { UtilSupportsObj } from "./util";
export type { DataConnection } from "./dataconnection/DataConnection";
export type { MediaConnection } from "./mediaconnection";
export type { LogLevel } from "./logger";
export { LogLevel } from "./logger";
export * from "./enums";

export { BufferedConnection } from "./dataconnection/BufferedConnection/BufferedConnection";
export { StreamConnection } from "./dataconnection/StreamConnection/StreamConnection";
export { Cbor } from "./dataconnection/StreamConnection/Cbor";
export { MsgPack } from "./dataconnection/StreamConnection/MsgPack";
export type { SerializerMapping } from "./peer";
export type { SerializerMapping } from "./optionInterfaces";

export { Peer, MsgPackPeer, CborPeer };

Expand Down
10 changes: 6 additions & 4 deletions lib/msgPackPeer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Peer, type SerializerMapping } from "./peer";
import { MsgPack } from "./exports";
import { Peer, } from "./peer";
import { SerializerMapping, } from "./optionInterfaces";
import { MsgPack } from "./dataconnection/StreamConnection/MsgPack";
import { SerializationType } from "./enums";

export class MsgPackPeer extends Peer {
protected override _defaultSerialization = SerializationType.CBOR;
override _serializers: SerializerMapping = {
MsgPack,
default: MsgPack,
msgpack: MsgPack,
};
}
38 changes: 36 additions & 2 deletions lib/optionInterfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import { DataConnection } from "./dataconnection/DataConnection";
import { SerializationType } from "./enums";
import { LogLevel } from "./logger";
import { Peer } from "./peer";


import { BinaryPack } from "./dataconnection/BufferedConnection/BinaryPack";
import { Raw } from "./dataconnection/BufferedConnection/Raw";
import { Json } from "./dataconnection/BufferedConnection/Json";


export interface AnswerOption {
/**
* Function which runs before create answer to modify sdp answer message.
Expand All @@ -13,10 +24,11 @@ export interface PeerJSOption {
secure?: boolean;
token?: string;
config?: RTCConfiguration;
debug?: number;
debug?: LogLevel;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s wrap this as a string, so a Typescript user is not forced to use the enum.

debug?: `${LogLevel}`

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I can convert this over to string literals like this:
image

Changing to suit this would mean doing this all over the application
github_issue_peers

Let me know if there is a better option or if you want to see this imlemented.

referrerPolicy?: ReferrerPolicy;
}


export interface PeerConnectOption {
/**
* A unique label by which you want to identify this data connection.
Expand All @@ -32,7 +44,7 @@ export interface PeerConnectOption {
* Can be any serializable type.
*/
metadata?: any;
serialization?: string;
serialization?: `${SerializationType}` | string;
reliable?: boolean;
}

Expand All @@ -49,3 +61,25 @@ export interface CallOption {
*/
sdpTransform?: Function;
}


type SerializationTypeConstructor = new (
peerId: string,
provider: Peer,
options: any,
) => DataConnection;

/**
* A mapping of serialization types to their implementations.
* Should map 1:1 with the serialization type property in the class.
*/
export type SerializerMapping = {
[key in string]: SerializationTypeConstructor;
}

export const defaultSerializers: SerializerMapping = {
raw: Raw,
json: Json,
binary: BinaryPack,
"binary-utf8": BinaryPack,
}
77 changes: 36 additions & 41 deletions lib/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ import { util } from "./util";
import logger, { LogLevel } from "./logger";
import { Socket } from "./socket";
import { MediaConnection } from "./mediaconnection";
import type { DataConnection } from "./dataconnection/DataConnection";
import { DataConnection } from "./dataconnection/DataConnection";
import {
ConnectionType,
PeerErrorType,
SerializationType,
ServerMessageType,
SocketEventType,
} from "./enums";
import type { ServerMessage } from "./servermessage";
import { API } from "./api";
import type {
CallOption,
PeerConnectOption,
PeerJSOption,
import {
SerializerMapping,
type CallOption,
type PeerConnectOption,
type PeerJSOption,
defaultSerializers,
} from "./optionInterfaces";
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 {
Expand Down Expand Up @@ -69,14 +68,6 @@ class PeerOptions implements PeerJSOption {

export { type PeerOptions };

export interface SerializerMapping {
[key: string]: new (
peerId: string,
provider: Peer,
options: any,
) => DataConnection;
}

export interface PeerEvents {
/**
* Emitted when a connection to the PeerServer is established.
Expand Down Expand Up @@ -107,20 +98,15 @@ export interface PeerEvents {
*/
error: (error: PeerError<`${PeerErrorType}`>) => void;
}

/**
* A peer who can initiate connections with other peers.
*/
export class Peer extends EventEmitterWithError<PeerErrorType, PeerEvents> {
private static readonly DEFAULT_KEY = "peerjs";

protected readonly _serializers: SerializerMapping = {
raw: Raw,
json: Json,
binary: BinaryPack,
"binary-utf8": BinaryPack,

default: BinaryPack,
};
protected readonly _serializers: SerializerMapping = defaultSerializers;
protected readonly _defaultSerialization: string = SerializationType.Binary;
private readonly _options: PeerOptions;
private readonly _api: API;
private readonly _socket: Socket;
Expand Down Expand Up @@ -232,7 +218,6 @@ export class Peer extends EventEmitterWithError<PeerErrorType, PeerEvents> {
token: util.randomToken(),
config: util.defaultConfig,
referrerPolicy: "strict-origin-when-cross-origin",
serializers: {},
...options,
};
this._options = options;
Expand Down Expand Up @@ -488,16 +473,12 @@ export class Peer extends EventEmitterWithError<PeerErrorType, PeerEvents> {
* @param options for specifying details about Peer Connection
*/
connect(peer: string, options: PeerConnectOption = {}): DataConnection {
options = {
serialization: "default",
...options,
};
if (this.disconnected) {
logger.warn(
"You cannot connect to a new Peer because you called " +
".disconnect() on this Peer and ended your connection with the " +
"server. You can create a new Peer to reconnect, or call reconnect " +
"on this peer if you believe its ID to still be available.",
".disconnect() on this Peer and ended your connection with the " +
"server. You can create a new Peer to reconnect, or call reconnect " +
"on this peer if you believe its ID to still be available.",
);
this.emitError(
PeerErrorType.Disconnected,
Expand All @@ -506,11 +487,25 @@ export class Peer extends EventEmitterWithError<PeerErrorType, PeerEvents> {
return;
}

const dataConnection = new this._serializers[options.serialization](
peer,
this,
options,
);
let dataConnection: DataConnection;
if (!options.serialization) {
dataConnection = new this._serializers[this._defaultSerialization](peer, this, options);
}
else {
if (!(options.serialization in this._serializers)) {
logger.warn(
"The serialization " + options.serialization + " is not in this peer's serialization mapping." +
"Please add it in the options when creating the peer."
);
return;
}
dataConnection = new this._serializers[options.serialization](
peer,
this,
options,
);
}

this._addConnection(peer, dataConnection);
return dataConnection;
}
Expand All @@ -529,8 +524,8 @@ export class Peer extends EventEmitterWithError<PeerErrorType, PeerEvents> {
if (this.disconnected) {
logger.warn(
"You cannot connect to a new Peer because you called " +
".disconnect() on this Peer and ended your connection with the " +
"server. You can create a new Peer to reconnect.",
".disconnect() on this Peer and ended your connection with the " +
"server. You can create a new Peer to reconnect.",
);
this.emitError(
PeerErrorType.Disconnected,
Expand Down Expand Up @@ -735,7 +730,7 @@ export class Peer extends EventEmitterWithError<PeerErrorType, PeerEvents> {
* the cloud server, email [email protected] to get the functionality enabled for
* your key.
*/
listAllPeers(cb = (_: any[]) => {}): void {
listAllPeers(cb = (_: any[]) => { }): void {
this._api
.listAllPeers()
.then((peers) => cb(peers))
Expand Down
Loading