From aa6edcfd9b031ab561c22b3565e4084e24a18edf Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Mon, 31 Jul 2017 01:13:52 +0200 Subject: [PATCH] add warning for message type when ArrayBuffer is expected --- src/Connector.js | 9 +++++++++ src/Encoding.js | 8 +++++++- test/encode-decode.js | 1 - 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Connector.js b/src/Connector.js index 2e65103c..3a22d776 100644 --- a/src/Connector.js +++ b/src/Connector.js @@ -181,10 +181,16 @@ export default function extendConnector (Y/* :any */) { } } send (uid, buffer) { + if (!(buffer instanceof ArrayBuffer || buffer instanceof Uint8Array)) { + throw new Error('Expected Message to be an ArrayBuffer or Uint8Array - please don\'t use this method to send custom messages') + } this.log('%s: Send \'%y\' to %s', this.userId, buffer, uid) this.logMessage('Message: %Y', buffer) } broadcast (buffer) { + if (!(buffer instanceof ArrayBuffer || buffer instanceof Uint8Array)) { + throw new Error('Expected Message to be an ArrayBuffer or Uint8Array - please don\'t use this method to send custom messages') + } this.log('%s: Broadcast \'%y\'', this.userId, buffer) this.logMessage('Message: %Y', buffer) } @@ -223,6 +229,9 @@ export default function extendConnector (Y/* :any */) { You received a raw message, and you know that it is intended for Yjs. Then call this function. */ async receiveMessage (sender, buffer) { + if (!(buffer instanceof ArrayBuffer || buffer instanceof Uint8Array)) { + throw new Error('Expected Message to be an ArrayBuffer or Uint8Array!') + } if (sender === this.userId) { return } diff --git a/src/Encoding.js b/src/Encoding.js index f759a421..2308d22e 100644 --- a/src/Encoding.js +++ b/src/Encoding.js @@ -66,7 +66,13 @@ export class BinaryEncoder { export class BinaryDecoder { constructor (buffer) { - this.uint8arr = new Uint8Array(buffer) + if (buffer instanceof ArrayBuffer) { + this.uint8arr = new Uint8Array(buffer) + } else if (buffer instanceof Uint8Array) { + this.uint8arr = buffer + } else { + throw new Error('Expected an ArrayBuffer or Uint8Array!') + } this.pos = 0 } skip8 () { diff --git a/test/encode-decode.js b/test/encode-decode.js index 53b1550c..7b9570ab 100644 --- a/test/encode-decode.js +++ b/test/encode-decode.js @@ -227,4 +227,3 @@ test('encode/decode Map operations', async function binMap (t) { info: 400 }) }) -