Skip to content

Commit

Permalink
improve memory handling in voice/file uploads (#1121)
Browse files Browse the repository at this point in the history
  • Loading branch information
evan authored Mar 6, 2021
1 parent 3f602d1 commit db7da38
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 19 deletions.
5 changes: 4 additions & 1 deletion lib/util/MultipartData.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ class MultipartData {
if(filename) {
str += "; filename=\"" + filename + "\"";
}
if(data instanceof Buffer) {
if(ArrayBuffer.isView(data)) {
str +="\r\nContent-Type: application/octet-stream";
if(!(data instanceof Uint8Array)) {
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
}
} else if(typeof data === "object") {
str +="\r\nContent-Type: application/json";
data = Buffer.from(JSON.stringify(data));
Expand Down
8 changes: 4 additions & 4 deletions lib/voice/VoiceConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,9 @@ class VoiceConnection extends EventEmitter {
let data;
if(!NaCl) {
data = Buffer.allocUnsafe(msg.length - 12 - Sodium.crypto_secretbox_MACBYTES);
Sodium.crypto_secretbox_open_easy(data, msg.slice(12), this.nonce, this.secret);
Sodium.crypto_secretbox_open_easy(data, msg.subarray(12), this.nonce, this.secret);
} else {
if(!(data = NaCl.secretbox.open(msg.slice(12), nonce, this.secret))) {
if(!(data = NaCl.secretbox.open(msg.subarray(12), nonce, this.secret))) {
/**
* Fired to warn of something weird but non-breaking happening
* @event VoiceConnection#warn
Expand All @@ -532,7 +532,7 @@ class VoiceConnection extends EventEmitter {
const hasExtension = !!(msg[0] & 0b10000);
const cc = msg[0] & 0b1111;
if(cc > 0) {
data = data.slice(cc * 4);
data = data.subarray(cc * 4);
}
// Not a RFC5285 One Byte Header Extension (not negotiated)
if(hasExtension) { // RFC3550 5.3.1: RTP Header Extension
Expand All @@ -541,7 +541,7 @@ class VoiceConnection extends EventEmitter {
while(data[index] == 0) {
++index;
}
data = data.slice(index);
data = data.subarray(index);
}
if(this.receiveStreamOpus) {
/**
Expand Down
12 changes: 6 additions & 6 deletions lib/voice/streams/DCAOpusTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DCAOpusTransformer extends BaseTransformer {
}

buffer._index += opusLen;
this.push(buffer.slice(buffer._index - opusLen, buffer._index));
this.push(buffer.subarray(buffer._index - opusLen, buffer._index));
}

_transform(chunk, enc, cb) {
Expand All @@ -36,22 +36,22 @@ class DCAOpusTransformer extends BaseTransformer {
this._remainder = chunk;
return cb();
} else {
const dcaVersion = chunk.slice(0, 4);
const dcaVersion = chunk.subarray(0, 4);
if(dcaVersion[0] !== 68 || dcaVersion[1] !== 67 || dcaVersion[2] !== 65) { // DCA0 or invalid
this.head = true; // Attempt to play as if it were a DCA0 file
} else if(dcaVersion[3] === 49) { // DCA1
if(chunk.length < 8) {
this._remainder = chunk;
return cb();
}
const jsonLength = chunk.slice(4, 8).readInt32LE(0);
const jsonLength = chunk.subarray(4, 8).readInt32LE(0);
if(chunk.length < 8 + jsonLength) {
this._remainder = chunk;
return cb();
}
const jsonMetadata = chunk.slice(8, 8 + jsonLength);
const jsonMetadata = chunk.subarray(8, 8 + jsonLength);
this.emit("debug", jsonMetadata);
chunk = chunk.slice(8 + jsonLength);
chunk = chunk.subarray(8 + jsonLength);
this.head = true;
} else {
this.emit("error", new Error("Unsupported DCA version: " + dcaVersion.toString()));
Expand All @@ -65,7 +65,7 @@ class DCAOpusTransformer extends BaseTransformer {
const offset = chunk._index;
const ret = this.process(chunk);
if(ret) {
this._remainder = chunk.slice(offset);
this._remainder = chunk.subarray(offset);
cb();
return;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/voice/streams/OggOpusTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class OggOpusTransformer extends BaseTransformer {

for(let segment of segments) {
buffer._index += segment;
byte = (segment = buffer.slice(buffer._index - segment, buffer._index)).toString("utf8", 0, 8);
byte = (segment = buffer.subarray(buffer._index - segment, buffer._index)).toString("utf8", 0, 8);
if(this.head) {
if(byte === "OpusTags") {
this.emit("debug", segment.toString());
Expand Down Expand Up @@ -91,7 +91,7 @@ class OggOpusTransformer extends BaseTransformer {
const offset = chunk._index;
const ret = this.process(chunk);
if(ret) {
this._remainder = chunk.slice(offset);
this._remainder = chunk.subarray(offset);
if(ret instanceof Error) {
this.emit("error", ret);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/voice/streams/PCMOpusTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ class PCMOpusTransformer extends BaseTransformer {

while(chunk._index + this.pcmSize < chunk.length) {
chunk._index += this.pcmSize;
this.push(this.opus.encode(chunk.slice(chunk._index - this.pcmSize, chunk._index), this.frameSize));
this.push(this.opus.encode(chunk.subarray(chunk._index - this.pcmSize, chunk._index), this.frameSize));
}

if(chunk._index < chunk.length) {
this._remainder = chunk.slice(chunk._index);
this._remainder = chunk.subarray(chunk._index);
}

this.setTransformCB(cb);
Expand Down
2 changes: 1 addition & 1 deletion lib/voice/streams/VolumeTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class VolumeTransformer extends BaseTransformer {

let buf;
if(chunk.length & 1) {
this._remainder = chunk.slice(chunk.length - 1);
this._remainder = chunk.subarray(chunk.length - 1);
buf = Buffer.allocUnsafe(chunk.length - 1);
} else {
buf = Buffer.allocUnsafe(chunk.length);
Expand Down
6 changes: 3 additions & 3 deletions lib/voice/streams/WebmOpusTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class WebmOpusTransformer extends BaseTransformer {
process(type, info) {
if(type === TAG_TYPE_TAG) {
if(info.name === "SimpleBlock" && (info.data.readUInt8(0) & 0xF) === this.firstAudioTrack.TrackNumber) {
this.push(info.data.slice(4));
this.push(info.data.subarray(4));
return;
}
if(info.name === "CodecPrivate") {
Expand Down Expand Up @@ -109,7 +109,7 @@ class WebmOpusTransformer extends BaseTransformer {
return false;
}

tagObj.data = buffer.slice(buffer._index, buffer._index + tagObj.size);
tagObj.data = buffer.subarray(buffer._index, buffer._index + tagObj.size);
buffer._index += tagObj.size;
this._total += tagObj.size;
this._state = STATE_TAG;
Expand Down Expand Up @@ -193,7 +193,7 @@ class WebmOpusTransformer extends BaseTransformer {
}

if(chunk._index < chunk.length) {
this._remainder = chunk.slice(chunk._index);
this._remainder = chunk.subarray(chunk._index);
}

this.setTransformCB(cb);
Expand Down

0 comments on commit db7da38

Please sign in to comment.