Ver código fonte

Exchange data in Binary by default

- Data can now be exchanged between 2 peers in binary now without having
to resort to JSON
    - This should give this module parity with the browser peerjs
- Had to install `node-blob` and `blob-to-arraybuffer` to make this work
- Also had to change how `encodingQueue.ts` processed the queue and
reurned arrayBuffers
    - The node implementation of FileReader (from the `filereader`)
      module can not be used for multiple transactions. So, I replaced
      it with a promise based version of the same operation that seems
      to start with a fresh FileReader object with every transaction
- Added the `blobToArraybuffer()` function to the prototype of the Blob
itself for easier usage
    - Might lead to annoying confusion in the future though. :shrug:
Satnam Singh Brar 5 anos atrás
pai
commit
8cf172dff1
5 arquivos alterados com 92 adições e 13 exclusões
  1. 1 0
      examples/echo_client.js
  2. 5 0
      header_patch.js
  3. 10 0
      package-lock.json
  4. 2 0
      package.json
  5. 74 13
      patch.diff

+ 1 - 0
examples/echo_client.js

@@ -29,6 +29,7 @@ peer.on('open', async (localId) => {
 
     while (true) {
         const data = await askForInput('>');
+        console.log('Sending', data);
         conn.send(data);
     }
 })

+ 5 - 0
header_patch.js

@@ -7,3 +7,8 @@ fetch = require('node-fetch');
 WebSocket = require('ws');
 FileReader = require('filereader');
 
+Blob = require('node-blob');
+const blobToArraybuffer = require('blob-to-arraybuffer');
+Blob.prototype.arrayBuffer = function() {
+    return blobToArraybuffer(this);
+}

+ 10 - 0
package-lock.json

@@ -1355,6 +1355,11 @@
         "file-uri-to-path": "1.0.0"
       }
     },
+    "blob-to-arraybuffer": {
+      "version": "0.0.1",
+      "resolved": "https://registry.npmjs.org/blob-to-arraybuffer/-/blob-to-arraybuffer-0.0.1.tgz",
+      "integrity": "sha512-cn23j+k87nZKnJcBDx/8jyRAPVbBrtZPUBx7xhmRXzdVSsN+k0nRePQuC06tuPqXmcNcHGlt+rs+zrSe5wafKg=="
+    },
     "bn.js": {
       "version": "4.11.8",
       "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.8.tgz",
@@ -4825,6 +4830,11 @@
       "integrity": "sha512-2+DuKodWvwRTrCfKOeR24KIc5unKjOh8mz17NCzVnHWfjAdDqbfbjqh7gUT+BkXBRQM52+xCHciKWonJ3CbJMQ==",
       "dev": true
     },
+    "node-blob": {
+      "version": "0.0.2",
+      "resolved": "https://registry.npmjs.org/node-blob/-/node-blob-0.0.2.tgz",
+      "integrity": "sha512-82wiGzMht96gPQDUYaZBdZEVvYD9aEhU6Bt9KLCr4rADZPRd7dQVY2Yj0ZG/1vp4DhVkL49nJT/M3CiMTAt3ag=="
+    },
     "node-fetch": {
       "version": "2.6.0",
       "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",

+ 2 - 0
package.json

@@ -17,7 +17,9 @@
     "parcel": "^1.12.3"
   },
   "dependencies": {
+    "blob-to-arraybuffer": "0.0.1",
     "filereader": "^0.10.3",
+    "node-blob": "0.0.2",
     "node-fetch": "^2.6.0",
     "wrtc": "^0.4.2",
     "ws": "^7.2.0"

+ 74 - 13
patch.diff

@@ -1,16 +1,77 @@
-diff --git a/lib/dataconnection.ts b/lib/dataconnection.ts
-index aec3c05..f0ca925 100644
---- a/lib/dataconnection.ts
-+++ b/lib/dataconnection.ts
-@@ -58,7 +58,7 @@ export class DataConnection extends BaseConnection implements IDataConnection {
-       this.options.connectionId || DataConnection.ID_PREFIX + util.randomToken();
- 
-     this.label = this.options.label || this.connectionId;
--    this.serialization = this.options.serialization || SerializationType.Binary;
-+    this.serialization = this.options.serialization || SerializationType.JSON;
-     this.reliable = !!this.options.reliable;
- 
-     this._encodingQueue.on('done', (ab: ArrayBuffer) => {
+diff --git a/lib/encodingQueue.ts b/lib/encodingQueue.ts
+index f0b3e40..7595bcd 100644
+--- a/lib/encodingQueue.ts
++++ b/lib/encodingQueue.ts
+@@ -2,30 +2,11 @@ import { EventEmitter } from "eventemitter3";
+ import logger from "./logger";
+ 
+ export class EncodingQueue extends EventEmitter {
+-  readonly fileReader: FileReader = new FileReader();
+-
+   private _queue: Blob[] = [];
+   private _processing: boolean = false;
+ 
+   constructor() {
+     super();
+-
+-    this.fileReader.onload = (evt) => {
+-      this._processing = false;
+-
+-      if (evt.target) {
+-        this.emit('done', evt.target.result as ArrayBuffer);
+-      }
+-
+-      this.doNextTask();
+-    };
+-
+-    this.fileReader.onerror = (evt) => {
+-      logger.error(`EncodingQueue error:`, evt);
+-      this._processing = false;
+-      this.destroy();
+-      this.emit('error', evt);
+-    }
+   }
+ 
+   get queue(): Blob[] {
+@@ -41,6 +22,8 @@ export class EncodingQueue extends EventEmitter {
+   }
+ 
+   enque(blob: Blob): void {
++    // Need to define a `name` for the blob to make our `filereader` module process the blob
++    blob.name = 'mylovelylilblob';
+     this.queue.push(blob);
+ 
+     if (this.processing) return;
+@@ -49,7 +32,6 @@ export class EncodingQueue extends EventEmitter {
+   }
+ 
+   destroy(): void {
+-    this.fileReader.abort();
+     this._queue = [];
+   }
+ 
+@@ -59,6 +41,18 @@ export class EncodingQueue extends EventEmitter {
+ 
+     this._processing = true;
+ 
+-    this.fileReader.readAsArrayBuffer(this.queue.shift());
++    const currentBlob = this.queue.shift();
++    currentBlob.arrayBuffer().then((res) => {
++      this._processing = false;
++      if (res.target) {
++        this.emit('done', res.target.result as ArrayBuffer);
++      }
++      this.doNextTask();
++    }).catch((err) => {
++      logger.error(`EncodingQueue error:`, err);
++      this._processing = false;
++      this.destroy();
++      this.emit('error', err);
++    }
+   }
+-}
+\ No newline at end of file
++}
 diff --git a/lib/exports.ts b/lib/exports.ts
 index 5772d02..63a57e3 100644
 --- a/lib/exports.ts