Skip to content

Commit da50734

Browse files
committed
Use Blob messages to encode Blob and ArrayBuffer input
1 parent 5e95d95 commit da50734

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

assets/js/phoenix/longpoll.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import {
66
import Ajax from "./ajax"
77

88
let arrayBufferToBase64 = (buffer) => {
9-
let binary = ""
10-
let bytes = new Uint8Array(buffer)
11-
let len = bytes.byteLength
12-
for(let i = 0; i < len; i++){ binary += String.fromCharCode(bytes[i]) }
13-
return btoa(binary)
9+
return new Promise((resolve, reject) => {
10+
let reader = new FileReader()
11+
let blob = new Blob([buffer])
12+
reader.onload = e => {
13+
let dataStart = reader.result.indexOf(',')
14+
resolve(reader.result.slice(dataStart + 1))
15+
}
16+
reader.onerror = () => reject()
17+
reader.readAsDataURL(blob)
18+
})
1419
}
1520

1621
export default class LongPoll {
@@ -24,6 +29,7 @@ export default class LongPoll {
2429
this.currentBatch = null
2530
this.currentBatchTimer = null
2631
this.batchBuffer = []
32+
this.sendResolver = Promise.resolve()
2733
this.onopen = function (){ } // noop
2834
this.onerror = function (){ } // noop
2935
this.onmessage = function (){ } // noop
@@ -118,18 +124,26 @@ export default class LongPoll {
118124
// pushes against an empty buffer
119125

120126
send(body){
121-
if(typeof(body) !== "string"){ body = arrayBufferToBase64(body) }
122-
if(this.currentBatch){
123-
this.currentBatch.push(body)
124-
} else if(this.awaitingBatchAck){
125-
this.batchBuffer.push(body)
126-
} else {
127-
this.currentBatch = [body]
128-
this.currentBatchTimer = setTimeout(() => {
129-
this.batchSend(this.currentBatch)
130-
this.currentBatch = null
131-
}, 0)
132-
}
127+
this.sendResolver =
128+
this.sendResolver
129+
.then(() => {
130+
if(typeof(body) !== "string"){ return arrayBufferToBase64(body) }
131+
return body
132+
})
133+
.then(body => {
134+
if(this.currentBatch){
135+
this.currentBatch.push(body)
136+
} else if(this.awaitingBatchAck){
137+
this.batchBuffer.push(body)
138+
} else {
139+
this.currentBatch = [body]
140+
this.currentBatchTimer = setTimeout(() => {
141+
this.batchSend(this.currentBatch)
142+
this.currentBatch = null
143+
this.sendResolver = Promise.resolve()
144+
}, 0)
145+
}
146+
})
133147
}
134148

135149
batchSend(messages){

assets/js/phoenix/serializer.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default {
99
KINDS: {push: 0, reply: 1, broadcast: 2},
1010

1111
encode(msg, callback){
12-
if(msg.payload.constructor === ArrayBuffer){
12+
if(msg.payload.constructor === ArrayBuffer || msg.payload.constructor === Blob){
1313
return callback(this.binaryEncode(msg))
1414
} else {
1515
let payload = [msg.join_ref, msg.ref, msg.topic, msg.event, msg.payload]
@@ -45,11 +45,7 @@ export default {
4545
Array.from(topic, char => view.setUint8(offset++, char.charCodeAt(0)))
4646
Array.from(event, char => view.setUint8(offset++, char.charCodeAt(0)))
4747

48-
var combined = new Uint8Array(header.byteLength + payload.byteLength)
49-
combined.set(new Uint8Array(header), 0)
50-
combined.set(new Uint8Array(payload), header.byteLength)
51-
52-
return combined.buffer
48+
return new Blob([header, payload])
5349
},
5450

5551
binaryDecode(buffer){

0 commit comments

Comments
 (0)