-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
ProPablo
wants to merge
8
commits into
peers:master
Choose a base branch
from
ProPablo:serialization-types-add
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9242da7
enum changes
647b02e
trying to add the enums to export
ba9715b
add default and tests
0f1177a
Working changes and refactor to enums
287f04d
allow any string as well as an option to serialization
d511d68
adressing comments
45147b4
removing default serialization enum
d9ece0e
updated package json and package lock
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ node_modules | |
.idea | ||
npm-debug.log | ||
.DS_STORE | ||
pnpm-lock.yaml | ||
|
||
test/output | ||
browserstack.err | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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. | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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, | ||
|
@@ -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; | ||
} | ||
|
@@ -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, | ||
|
@@ -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)) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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}`
There was a problem hiding this comment.
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:

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

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