Browse Source

add ability to change json stringify / json parse methods for DataChannel

afrokick 5 years ago
parent
commit
3c124faaf0
6 changed files with 13 additions and 6 deletions
  1. 4 2
      dist/peerjs.js
  2. 0 0
      dist/peerjs.js.map
  3. 0 0
      dist/peerjs.min.js
  4. 0 0
      dist/peerjs.min.js.map
  5. 2 0
      index.d.ts
  6. 7 4
      lib/dataconnection.ts

+ 4 - 2
dist/peerjs.js

@@ -8679,6 +8679,8 @@ function (_super) {
   function DataConnection(peerId, provider, options) {
     var _this = _super.call(this, peerId, provider, options) || this;
 
+    _this.jsonStringify = JSON.stringify;
+    _this.jsonParse = JSON.parse;
     _this._buffer = [];
     _this._bufferSize = 0;
     _this._buffering = false;
@@ -8790,7 +8792,7 @@ function (_super) {
         deserializedData = util_1.util.unpack(ab);
       }
     } else if (this.serialization === enums_1.SerializationType.JSON) {
-      deserializedData = JSON.parse(data);
+      deserializedData = this.jsonParse(data);
     } // Check if we've chunked--if so, piece things back together.
     // We're guaranteed that this isn't 0.
 
@@ -8884,7 +8886,7 @@ function (_super) {
     }
 
     if (this.serialization === enums_1.SerializationType.JSON) {
-      this._bufferedSend(JSON.stringify(data));
+      this._bufferedSend(this.jsonStringify(data));
     } else if (this.serialization === enums_1.SerializationType.Binary || this.serialization === enums_1.SerializationType.BinaryUTF8) {
       var blob = util_1.util.pack(data);
 

File diff suppressed because it is too large
+ 0 - 0
dist/peerjs.js.map


File diff suppressed because it is too large
+ 0 - 0
dist/peerjs.min.js


File diff suppressed because it is too large
+ 0 - 0
dist/peerjs.min.js.map


+ 2 - 0
index.d.ts

@@ -175,6 +175,8 @@ declare namespace Peer {
     serialization: string;
     type: string;
     bufferSize: number;
+    jsonStringify: (data: any) => string;
+    jsonParse: (data: string) => any;
   }
 
   interface MediaConnection {

+ 7 - 4
lib/dataconnection.ts

@@ -11,11 +11,12 @@ import { Peer } from "./peer";
 import { BaseConnection } from "./baseconnection";
 import { ServerMessage } from "./servermessage";
 import { EncodingQueue } from './encodingQueue';
+import { DataConnection as IDataConnection } from '../index';
 
 /**
  * Wraps a DataChannel between two Peers.
  */
-export class DataConnection extends BaseConnection {
+export class DataConnection extends BaseConnection implements IDataConnection {
   private static readonly ID_PREFIX = "dc_";
   private static readonly MAX_BUFFERED_AMOUNT = 8 * 1024 * 1024;
 
@@ -23,6 +24,8 @@ export class DataConnection extends BaseConnection {
   readonly label: string;
   readonly serialization: SerializationType;
   readonly reliable: boolean;
+  jsonStringify: (data: any) => string = JSON.stringify;
+  jsonParse: (data: string) => any = JSON.parse;
 
   get type() {
     return ConnectionType.Data;
@@ -129,7 +132,7 @@ export class DataConnection extends BaseConnection {
         deserializedData = util.unpack(ab);
       }
     } else if (this.serialization === SerializationType.JSON) {
-      deserializedData = JSON.parse(data as string);
+      deserializedData = this.jsonParse(data as string);
     }
 
     // Check if we've chunked--if so, piece things back together.
@@ -208,7 +211,7 @@ export class DataConnection extends BaseConnection {
   }
 
   /** Allows user to send data. */
-  send(data: any, chunked: boolean): void {
+  send(data: any, chunked?: boolean): void {
     if (!this.open) {
       super.emit(
         ConnectionEventType.Error,
@@ -220,7 +223,7 @@ export class DataConnection extends BaseConnection {
     }
 
     if (this.serialization === SerializationType.JSON) {
-      this._bufferedSend(JSON.stringify(data));
+      this._bufferedSend(this.jsonStringify(data));
     } else if (
       this.serialization === SerializationType.Binary ||
       this.serialization === SerializationType.BinaryUTF8

Some files were not shown because too many files changed in this diff